UNPKG

@cliz/inlets

Version:
158 lines (157 loc) 4.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Notification = exports.createContainer = exports.createCallbackCtainer = exports.createHostContainer = exports.shortUUID = void 0; const doreamon_1 = require("@zodash/doreamon"); const shorturl_1 = require("@zodash/shorturl"); const nobot_1 = require("@znode/nobot"); function shortUUID() { return (0, shorturl_1.default)(doreamon_1.default.uuid()); } exports.shortUUID = shortUUID; function createHostContainer() { const hosts = {}; function get(id) { if (id) { const _id = id.toLowerCase(); return hosts[_id]; } return hosts; } const has = (id) => { return !!hosts[id]; }; const bindWs = (wsSocket, subDomain) => { const uid = (subDomain || shortUUID()).toLowerCase(); hosts[uid] = { wsSocket, }; return uid; }; const unbindWs = (id) => { const _id = id.toLowerCase(); delete hosts[_id]; }; const bindTcp = (id, tcp) => { const _id = id.toLowerCase(); if (!hosts[_id]) return; hosts[_id].tcpSocket = tcp; }; return { get, has, bindWs, unbindWs, bindTcp, }; } exports.createHostContainer = createHostContainer; function createCallbackCtainer() { const callbacks = {}; const get = (tcpId, requestId) => { if (callbacks[tcpId] && callbacks[tcpId][requestId]) { return callbacks[tcpId][requestId]; } return null; }; const set = (tcpId, requestId, callback) => { if (!callbacks[tcpId]) { callbacks[tcpId] = {}; } callbacks[tcpId][requestId] = callback; }; const remove = (tcpId) => { delete callbacks[tcpId]; }; return { get, set, remove, }; } exports.createCallbackCtainer = createCallbackCtainer; function createContainer() { const containers = {}; function create(id, token, wsSocket, authentication) { const destroy = () => { const c = get(id); if (!c) { throw new Error(`invalid token for container`); } c.sourceServer && c.sourceServer.close(); remove(id); }; containers[id] = { wsSocket, token, type: authentication.type, port: authentication.port, sourcePort: authentication.tunnelPort, version: authentication.version, authType: authentication.authType, clientId: authentication.clientId, tunnelPort: authentication.tunnelPort, requests: {}, signature: authentication.signature, clientTimestamp: authentication.timestamp, destroy, }; wsSocket.on('disconnect', () => { destroy.apply(null); }); } function get(id) { return containers[id]; } function set(id, key, value) { let one = get(id); if (!one) { throw new Error(`invalid id for container`); } one[key] = value; } function remove(id) { delete containers[id]; } function registerRequest(containerId, requestId, sourceSocket) { const container = containers[containerId]; container.requests[requestId] = sourceSocket; } function connectRequest(containerId, requestId, targetSocket) { const container = containers[containerId]; const sourceSocket = container.requests[requestId]; delete container.requests[requestId]; if (requestId !== sourceSocket.requestId || requestId !== targetSocket.requestId) { throw new Error(`[connectRequest] request id 无法对应(current: ${requestId}, sourceSocket: ${sourceSocket.requestId}, targetSocket: ${targetSocket.requestId})`); } targetSocket.pipe(sourceSocket); sourceSocket.pipe(targetSocket); } return { create, get, set, remove, registerRequest, connectRequest, }; } exports.createContainer = createContainer; class Notification { constructor(config) { this.config = config; } async notify(title, message) { if (!this.config) { return; } const content = !Array.isArray(message) ? message : message.join('\n'); return (0, nobot_1.sendMessage)(this.config.provider, this.config.url, { title, content, }); } } exports.Notification = Notification;