UNPKG

node-red-contrib-vban

Version:
74 lines (73 loc) 3.71 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const TechnicalNode_1 = require("../lib/TechnicalNode"); const registerNode_1 = require("../lib/registerNode"); const vban_1 = require("vban"); const Security_1 = require("../lib/Security"); const NODE_NAME = 'vban-server'; class VBANServerNode extends TechnicalNode_1.TechnicalNode { init() { const _super = Object.create(null, { init: { get: () => super.init } }); var _a, _b; return __awaiter(this, void 0, void 0, function* () { yield _super.init.call(this); if (this.definition.allowedIPs) { this.security = new Security_1.Security(this.definition.allowedIPs); } this.node.server = new vban_1.VBANServer({ application: { color: { red: 255, blue: 0, green: 0 }, applicationName: (_a = this.definition.name) !== null && _a !== void 0 ? _a : 'Node-RED VBAN', applicationType: vban_1.EServicePINGApplicationType.SERVER, features: [vban_1.EServicePINGFeatures.MIDI, vban_1.EServicePINGFeatures.SERIAL, vban_1.EServicePINGFeatures.TXT] }, autoReplyToPing: (_b = this.definition.autoReplyToPing) !== null && _b !== void 0 ? _b : true, beforeProcessPacket: this.definition.allowedIPs ? (_msg, sender) => { var _a; return ((_a = this.security) === null || _a === void 0 ? void 0 : _a.check(sender.address)) || false; } : () => true }); this.node.server.setMaxListeners(0); this.node.startVBANServer = () => __awaiter(this, void 0, void 0, function* () { if (this.node.startVBANServerPromise) { yield this.node.startVBANServerPromise; } if (!this.node.server.isListening) { this.node.startVBANServerPromise = new Promise((res) => { var _a; this.node.server.bind(Number(this.definition.port) || 6980, (_a = this.definition.address) !== null && _a !== void 0 ? _a : '0.0.0.0', res); }); this.node.startVBANServerPromise.finally(() => (this.node.startVBANServerPromise = undefined)); } }); this.node.on('close', (_removed, done) => __awaiter(this, void 0, void 0, function* () { try { yield new Promise((res) => { this.node.server.close(() => res('')); }); } catch (e) { this.node.error(e.message); } finally { done(); } })); }); } } module.exports = (RED) => { (0, registerNode_1.registerNode)(RED, NODE_NAME, VBANServerNode); };