redis-smq-common
Version:
Provides essential components and utilities shared across RedisSMQ packages.
95 lines • 4.13 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const path_1 = require("path");
const worker_threads_1 = require("worker_threads");
const index_js_1 = require("../types/index.js");
const callable_worker_thread_js_1 = require("./callable-worker-thread.js");
const worker_thread_message_js_1 = require("./worker-thread-message.js");
const runnable_worker_thread_js_1 = require("./runnable-worker-thread.js");
function isWorkerFunction(Worker) {
return typeof Worker === 'function';
}
function importWorkerInstance(filename, cb) {
if (!['.js', '.cjs'].includes((0, path_1.extname)(filename))) {
(0, worker_thread_message_js_1.exit)(index_js_1.EWorkerThreadChildExitCode.FILE_EXTENSION_ERROR);
}
try {
(0, fs_1.statSync)(filename);
}
catch (e) {
(0, worker_thread_message_js_1.exit)(index_js_1.EWorkerThreadChildExitCode.FILE_READ_ERROR, e);
}
Promise.resolve(`${filename}`).then(s => __importStar(require(s))).then((importedModule) => {
const fn = importedModule &&
typeof importedModule === 'object' &&
'default' in importedModule
? importedModule.default
: importedModule;
if (!isWorkerFunction(fn)) {
return (0, worker_thread_message_js_1.exit)(index_js_1.EWorkerThreadChildExitCode.INVALID_WORKER_TYPE);
}
cb(fn);
})
.catch(() => {
(0, worker_thread_message_js_1.exit)(index_js_1.EWorkerThreadChildExitCode.FILE_IMPORT_ERROR);
});
}
function isRunnableFunctionFactory(worker, type) {
return type === index_js_1.EWorkerType.RUNNABLE;
}
if (!worker_threads_1.isMainThread && worker_threads_1.parentPort) {
const { filename, type, initialPayload } = typeof worker_threads_1.workerData === 'object' ? worker_threads_1.workerData : {};
if (!filename ||
type === null ||
type === undefined ||
![index_js_1.EWorkerType.CALLABLE, index_js_1.EWorkerType.RUNNABLE].includes(type)) {
(0, worker_thread_message_js_1.exit)(index_js_1.EWorkerThreadChildExitCode.WORKER_DATA_REQUIRED);
}
else {
const messagePort = worker_threads_1.parentPort;
importWorkerInstance(filename, (worker) => {
if (isRunnableFunctionFactory(worker, type))
(0, runnable_worker_thread_js_1.handleRunnableWorker)(worker, messagePort, initialPayload);
else
(0, callable_worker_thread_js_1.handleCallableWorker)(worker, messagePort);
});
}
process.on('uncaughtException', (err) => {
(0, worker_thread_message_js_1.exit)(index_js_1.EWorkerThreadChildExitCode.UNCAUGHT_EXCEPTION, err);
});
}
//# sourceMappingURL=worker-thread.js.map