GeezerVison is showing github.com — process: split worker IO into internal/worker/io.js · nodejs/node@020a9ab
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js +39-4Lines changed: 39 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -140,9 +140,13 @@ function startup() { | 140 | 140 | } | 141 | 141 |
| 142 | 142 | if (isMainThread) { | 143 | | - mainThreadSetup.setupStdio(); | | 143 | + const { getStdout, getStdin, getStderr } = | | 144 | + NativeModule.require('internal/process/stdio').getMainThreadStdio(); | | 145 | + setupProcessStdio(getStdout, getStdin, getStderr); | 144 | 146 | } else { | 145 | | - workerThreadSetup.setupStdio(); | | 147 | + const { getStdout, getStdin, getStderr } = | | 148 | + workerThreadSetup.initializeWorkerStdio(); | | 149 | + setupProcessStdio(getStdout, getStdin, getStderr); | 146 | 150 | } | 147 | 151 |
| 148 | 152 | if (global.__coverage__) | @@ -318,8 +322,14 @@ function startup() { | 318 | 322 | function startExecution() { | 319 | 323 | // This means we are in a Worker context, and any script execution | 320 | 324 | // will be directed by the worker module. | 321 | | - if (internalBinding('worker').getEnvMessagePort() !== undefined) { | 322 | | - NativeModule.require('internal/worker').setupChild(); | | 325 | + if (!isMainThread) { | | 326 | + const workerThreadSetup = NativeModule.require( | | 327 | + 'internal/process/worker_thread_only' | | 328 | + ); | | 329 | + // Set up the message port and start listening | | 330 | + const { workerFatalExeception } = workerThreadSetup.setup(); | | 331 | + // Overwrite fatalException | | 332 | + process._fatalException = workerFatalExeception; | 323 | 333 | return; | 324 | 334 | } | 325 | 335 |
| @@ -511,6 +521,31 @@ function setupProcessObject() { | 511 | 521 | EventEmitter.call(process); | 512 | 522 | } | 513 | 523 |
| | 524 | +function setupProcessStdio(getStdout, getStdin, getStderr) { | | 525 | + Object.defineProperty(process, 'stdout', { | | 526 | + configurable: true, | | 527 | + enumerable: true, | | 528 | + get: getStdout | | 529 | + }); | | 530 | + | | 531 | + Object.defineProperty(process, 'stderr', { | | 532 | + configurable: true, | | 533 | + enumerable: true, | | 534 | + get: getStderr | | 535 | + }); | | 536 | + | | 537 | + Object.defineProperty(process, 'stdin', { | | 538 | + configurable: true, | | 539 | + enumerable: true, | | 540 | + get: getStdin | | 541 | + }); | | 542 | + | | 543 | + process.openStdin = function() { | | 544 | + process.stdin.resume(); | | 545 | + return process.stdin; | | 546 | + }; | | 547 | +} | | 548 | + | 514 | 549 | function setupGlobalVariables() { | 515 | 550 | Object.defineProperty(global, Symbol.toStringTag, { | 516 | 551 | value: 'global', | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/main_thread_only.js -10Lines changed: 0 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,15 +16,6 @@ const { | 16 | 16 | validateString | 17 | 17 | } = require('internal/validators'); | 18 | 18 |
| 19 | | -const { | 20 | | - setupProcessStdio, | 21 | | - getMainThreadStdio | 22 | | -} = require('internal/process/stdio'); | 23 | | - | 24 | | -function setupStdio() { | 25 | | - setupProcessStdio(getMainThreadStdio()); | 26 | | -} | 27 | | - | 28 | 19 | // The execution of this function itself should not cause any side effects. | 29 | 20 | function wrapProcessMethods(binding) { | 30 | 21 | function chdir(directory) { | @@ -174,7 +165,6 @@ function setupChildProcessIpcChannel() { | 174 | 165 | } | 175 | 166 |
| 176 | 167 | module.exports = { | 177 | | - setupStdio, | 178 | 168 | wrapProcessMethods, | 179 | 169 | setupSignalHandlers, | 180 | 170 | setupChildProcessIpcChannel, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/stdio.js -26Lines changed: 0 additions & 26 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -exports.setupProcessStdio = setupProcessStdio; | 4 | 3 | exports.getMainThreadStdio = getMainThreadStdio; | 5 | 4 |
| 6 | 5 | function dummyDestroy(err, cb) { cb(err); } | @@ -134,31 +133,6 @@ function getMainThreadStdio() { | 134 | 133 | }; | 135 | 134 | } | 136 | 135 |
| 137 | | -function setupProcessStdio({ getStdout, getStdin, getStderr }) { | 138 | | - Object.defineProperty(process, 'stdout', { | 139 | | - configurable: true, | 140 | | - enumerable: true, | 141 | | - get: getStdout | 142 | | - }); | 143 | | - | 144 | | - Object.defineProperty(process, 'stderr', { | 145 | | - configurable: true, | 146 | | - enumerable: true, | 147 | | - get: getStderr | 148 | | - }); | 149 | | - | 150 | | - Object.defineProperty(process, 'stdin', { | 151 | | - configurable: true, | 152 | | - enumerable: true, | 153 | | - get: getStdin | 154 | | - }); | 155 | | - | 156 | | - process.openStdin = function() { | 157 | | - process.stdin.resume(); | 158 | | - return process.stdin; | 159 | | - }; | 160 | | -} | 161 | | - | 162 | 136 | function createWritableStdioStream(fd) { | 163 | 137 | var stream; | 164 | 138 | const tty_wrap = internalBinding('tty_wrap'); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/worker_thread_only.js +41-10Lines changed: 41 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | // This file contains process bootstrappers that can only be | 4 | 4 | // run in the worker thread. | | 5 | +const { | | 6 | + getEnvMessagePort, | | 7 | + threadId | | 8 | +} = internalBinding('worker'); | | 9 | + | | 10 | +const debug = require('util').debuglog('worker'); | 5 | 11 |
| 6 | 12 | const { | 7 | | - setupProcessStdio | 8 | | -} = require('internal/process/stdio'); | | 13 | + kWaitingStreams, | | 14 | + ReadableWorkerStdio, | | 15 | + WritableWorkerStdio | | 16 | +} = require('internal/worker/io'); | 9 | 17 |
| 10 | 18 | const { | 11 | | - workerStdio | | 19 | + createMessageHandler, | | 20 | + createWorkerFatalExeception | 12 | 21 | } = require('internal/worker'); | 13 | 22 |
| 14 | | -function setupStdio() { | 15 | | - setupProcessStdio({ | 16 | | - getStdout: () => workerStdio.stdout, | 17 | | - getStderr: () => workerStdio.stderr, | 18 | | - getStdin: () => workerStdio.stdin | 19 | | - }); | | 23 | +const workerStdio = {}; | | 24 | + | | 25 | +function initializeWorkerStdio() { | | 26 | + const port = getEnvMessagePort(); | | 27 | + port[kWaitingStreams] = 0; | | 28 | + workerStdio.stdin = new ReadableWorkerStdio(port, 'stdin'); | | 29 | + workerStdio.stdout = new WritableWorkerStdio(port, 'stdout'); | | 30 | + workerStdio.stderr = new WritableWorkerStdio(port, 'stderr'); | | 31 | + | | 32 | + return { | | 33 | + getStdout() { return workerStdio.stdout; }, | | 34 | + getStderr() { return workerStdio.stderr; }, | | 35 | + getStdin() { return workerStdio.stdin; } | | 36 | + }; | | 37 | +} | | 38 | + | | 39 | +function setup() { | | 40 | + debug(`[${threadId}] is setting up worker child environment`); | | 41 | + | | 42 | + const port = getEnvMessagePort(); | | 43 | + const publicWorker = require('worker_threads'); | | 44 | + port.on('message', createMessageHandler(publicWorker, port, workerStdio)); | | 45 | + port.start(); | | 46 | + | | 47 | + return { | | 48 | + workerFatalExeception: createWorkerFatalExeception(port) | | 49 | + }; | 20 | 50 | } | 21 | 51 |
| 22 | 52 | module.exports = { | 23 | | - setupStdio | | 53 | + initializeWorkerStdio, | | 54 | + setup | 24 | 55 | }; |
|