This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const {EventEmitter} = require('events'); | |
| const {Worker, MessageChannel, parentPort} = require('worker_threads'); | |
| const WORKER_STATUS = { | |
| IDLE: Symbol('idle'), | |
| BUSY: Symbol('busy'), | |
| }; | |
| module.exports.WorkerPool = class WorkerPool extends EventEmitter{ | |
| constructor(script, size) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const workspacePath = '/some/workspace/path'; | |
| const workspaceModulesDir = path.join(workspacePath, 'node_modules'); | |
| function installModule(moduleName) { | |
| execSync(`npm install --silent ${moduleName}`, { cwd: workspacePath }); | |
| } | |
| function wrapRequire() { | |
| const originalRequire = Module.prototype.require; | |
| Module.prototype.require = function (moduleName) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| global.baapan = async (requiredModule) => { | |
| const [moduleName] = parsePath(requiredModule); | |
| if (!moduleName) { | |
| return console.error('Invalid module name provided!'); | |
| } | |
| return new Promise((resolve, reject) => { | |
| try { | |
| resolve(require(requiredModule)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "babel": { | |
| "env": { | |
| "test": { | |
| "plugins": [ | |
| "istanbul" | |
| ] | |
| } | |
| }, | |
| "presets": [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| InternalCallbackScope::~InternalCallbackScope() { | |
| Close(); | |
| } | |
| void InternalCallbackScope::Close() { | |
| // ...redacted | |
| if (async_context_.async_id != 0) { | |
| AsyncWrap::EmitAfter(env_, async_context_.async_id); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void AsyncWrap::EmitAsyncInit(Environment* env, | |
| Local<Object> object, | |
| Local<String> type, | |
| double async_id, | |
| double trigger_async_id) { | |
| CHECK(!object.IsEmpty()); | |
| CHECK(!type.IsEmpty()); | |
| AsyncHooks* async_hooks = env->async_hooks(); | |
| // Nothing to execute, so can continue normally. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static void SetupHooks(const FunctionCallbackInfo<Value>& args) { | |
| Environment* env = Environment::GetCurrent(args); | |
| CHECK(args[0]->IsObject()); | |
| // All of init, before, after, destroy are supplied by async_hooks | |
| // internally, so this should every only be called once. At which time all | |
| // the functions should be set. Detect this by checking if init !IsEmpty(). | |
| CHECK(env->async_hooks_init_function().IsEmpty()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Constructor of Async resource | |
| AsyncResource::AsyncResource(Isolate* isolate, | |
| Local<Object> resource, | |
| const char* name, | |
| async_id trigger_async_id) | |
| : env_(Environment::GetCurrent(isolate)), | |
| resource_(isolate, resource) { | |
| async_context_ = EmitAsyncInit(isolate, resource, name, | |
| trigger_async_id); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| this.server.pre((req, res, next) => { | |
| this.logger.debug(`Start ${req.method} ${req.url}`); | |
| const requestStart = process.hrtime(); | |
| res.on('finish', () => { | |
| const [reqTimeS, reqTimeNS] = process.hrtime(requestStart), | |
| requestDurationMS = (reqTimeS * 1e3 + reqTimeNS / 1e9).toFixed(2); | |
| this.logger.debug(`End ${req.method} ${req.url} (status=${res.statusCode} duration=${requestDurationMS}ms)`); | |
| }); | |
| next(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (err instanceof AuthenticationError) { | |
| return res.status(401).send('not authenticated'); | |
| } | |
| if (err instanceof UnauthorizedError) { | |
| return res.status(403).send('forbidden'); | |
| } | |
| if (err instanceof InvalidInputError) { | |
| return res.status(400).send('bad request'); |