UNPKG

expressmultithread

Version:

Fast, light-weight and low dependency [Express.js](https://www.npmjs.com/package/express) multithreaded router.

120 lines 4.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.child = void 0; const worker_threads_1 = require("worker_threads"); const register_1 = require("../functions/utils/register"); (0, register_1.registerTS)(); const config_1 = __importDefault(require("../config")); (0, register_1.registerTS)(config_1.default.tsconfigPath); const CallLoop_1 = require("../class/CallLoop"); const overrideConsole_1 = require("../functions/overrideConsole"); const pathToRoute_1 = require("../functions/pathToRoute"); const overrideRes_1 = require("../functions/overrideRes"); const postMessage_1 = require("../functions/utils/postMessage"); const importModule_1 = require("../functions/utils/importModule"); const types_1 = require("../types"); const strings_1 = require("../constants/strings"); class Child { constructor() { this.routes = {}; this.sources = []; this.id = worker_threads_1.workerData.id; if (!worker_threads_1.parentPort) throw new Error("No parent port"); config_1.default.overrideConsole && (0, overrideConsole_1.override)(this.id); this.initChild(); } ; async initChild() { config_1.default.awaitable && await config_1.default.awaitable; worker_threads_1.parentPort.on(strings_1.message, (data) => { const parsed = JSON.parse(data); switch (parsed.cmd) { case types_1.ParentCmd.setSource: this.setSources(parsed.source); break; case types_1.ParentCmd.request: this.handleRequest(parsed.req, parsed.id); break; default: throw new Error(`Unknown command : '${parsed.cmd}'`); } }); (0, postMessage_1.postParent)({ cmd: types_1.ChildCmd.ready, id: this.id }); config_1.default.verbose && console.info("Child ready"); } ; handleRequest(req, _id) { const k = req.method.toLowerCase() + req.path; if (!this.routes[k]) throw new Error(strings_1.routeNotFound); new CallLoop_1.CallLoop(req, (0, overrideRes_1.overrideRes)(this.id, _id), this.routes[k].callstack).handle(); } ; setSources(sources) { let mid = []; this.routes = {}; this.sources = sources; for (let i = 0; i < sources.length; i++) { const s = sources[i]; switch (s.type) { case types_1.SourceType.CONTROLLER: Object.assign(this.routes, this.applyMiddleware(s, mid)); break; case types_1.SourceType.GLOBAL_MIDDLEWARE: mid = this.applyHandler(s, mid); break; default: throw new Error(`Unknown source: '${sources[i].type}'`); } } } ; applyMiddleware(source, mid) { const routes = (0, pathToRoute_1.pathToRoute)(source.path); const keys = Object.keys(routes); for (let i = 0; i < keys.length; i++) { if (this.routes[keys[i]]) { console.warn(`Warning : Redefinition of route : ${keys[i]}, will override previous handling.`); } routes[keys[i]].callstack = mid.concat(routes[keys[i]].callstack); } return routes; } applyHandler(source, mid) { let cb = (0, importModule_1.importModule)(source.path)?.default; if (!cb || typeof cb !== strings_1.fnStr) throw new Error(`No default exported function on path : '${source.path}'`); source.args && source.args.length && (cb = cb(...source.args)); switch (cb.length) { case 3: mid.push(cb); break; case 4: const keys = Object.keys(this.routes); for (let i = 0; i < keys.length; i++) { this.routes[keys[i]].callstack = this.routes[keys[i]].callstack.concat(cb); } break; default: throw new Error(`Exported function is not a correct middleware : ${source.path}`); } return mid; } get _routes() { return this.routes; } get _sources() { return this.sources; } } if (worker_threads_1.isMainThread) throw new Error(strings_1.noMain); exports.child = new Child(); //# sourceMappingURL=Child.js.map