| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2cc0482 commit 81e363a
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -510,6 +510,9 @@ changes: | |||
| 510 | 510 | - version: REPLACEME | |
| 511 | 511 | pr-url: https://github.com/nodejs/node/pull/26628 | |
| 512 | 512 | description: The `resourceLimits` option was introduced. | |
| 513 | + - version: REPLACEME | ||
| 514 | + pr-url: https://github.com/nodejs/node/pull/30559 | ||
| 515 | + description: The `argv` option was introduced. | ||
| 513 | 516 | --> | |
| 514 | 517 | ||
| 515 | 518 | * `filename` {string} The path to the Worker’s main script. Must be | |
@@ -518,6 +521,10 @@ changes: | |||
| 518 | 521 | If `options.eval` is `true`, this is a string containing JavaScript code | |
| 519 | 522 | rather than a path. | |
| 520 | 523 | * `options` {Object} | |
| 524 | + * `argv` {any[]} List of arguments which would be stringified and appended to | ||
| 525 | + `process.argv` in the worker. This is mostly similar to the `workerData` | ||
| 526 | + but the values will be available on the global `process.argv` as if they | ||
| 527 | + were passed as CLI options to the script. | ||
| 521 | 528 | * `env` {Object} If set, specifies the initial value of `process.env` inside | |
| 522 | 529 | the Worker thread. As a special value, [`worker.SHARE_ENV`][] may be used | |
| 523 | 530 | to specify that the parent thread and the child thread should share their | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -92,6 +92,7 @@ if (process.env.NODE_CHANNEL_FD) { | |||
| 92 | 92 | port.on('message', (message) => { | |
| 93 | 93 | if (message.type === LOAD_SCRIPT) { | |
| 94 | 94 | const { | |
| 95 | + argv, | ||
| 95 | 96 | cwdCounter, | |
| 96 | 97 | filename, | |
| 97 | 98 | doEval, | |
@@ -115,6 +116,9 @@ port.on('message', (message) => { | |||
| 115 | 116 | assert(!CJSLoader.hasLoadedAnyUserCJSModule); | |
| 116 | 117 | loadPreloadModules(); | |
| 117 | 118 | initializeFrozenIntrinsics(); | |
| 119 | + if (argv !== undefined) { | ||
| 120 | + process.argv = process.argv.concat(argv); | ||
| 121 | + } | ||
| 118 | 122 | publicWorker.parentPort = publicPort; | |
| 119 | 123 | publicWorker.workerData = workerData; | |
| 120 | 124 | ||
@@ -138,12 +142,22 @@ port.on('message', (message) => { | |||
| 138 | 142 | port.postMessage({ type: UP_AND_RUNNING }); | |
| 139 | 143 | if (doEval) { | |
| 140 | 144 | const { evalScript } = require('internal/process/execution'); | |
| 141 | - evalScript('[worker eval]', filename); | ||
| 145 | + const name = '[worker eval]'; | ||
| 146 | + // This is necessary for CJS module compilation. | ||
| 147 | + // TODO: pass this with something really internal. | ||
| 148 | + ObjectDefineProperty(process, '_eval', { | ||
| 149 | + configurable: true, | ||
| 150 | + enumerable: true, | ||
| 151 | + value: filename, | ||
| 152 | + }); | ||
| 153 | + process.argv.splice(1, 0, name); | ||
| 154 | + evalScript(name, filename); | ||
| 142 | 155 | } else { | |
| 143 | 156 | // script filename | |
| 144 | 157 | // runMain here might be monkey-patched by users in --require. | |
| 145 | 158 | // XXX: the monkey-patchability here should probably be deprecated. | |
| 146 | - CJSLoader.Module.runMain(process.argv[1] = filename); | ||
| 159 | + process.argv.splice(1, 0, filename); | ||
| 160 | + CJSLoader.Module.runMain(filename); | ||
| 147 | 161 | } | |
| 148 | 162 | } else if (message.type === STDIO_PAYLOAD) { | |
| 149 | 163 | const { stream, chunk, encoding } = message; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -86,9 +86,16 @@ class Worker extends EventEmitter { | |||
| 86 | 86 | validateString(filename, 'filename'); | |
| 87 | 87 | if (options.execArgv && !ArrayIsArray(options.execArgv)) { | |
| 88 | 88 | throw new ERR_INVALID_ARG_TYPE('options.execArgv', | |
| 89 | - 'array', | ||
| 89 | + 'Array', | ||
| 90 | 90 | options.execArgv); | |
| 91 | 91 | } | |
| 92 | + let argv; | ||
| 93 | + if (options.argv) { | ||
| 94 | + if (!ArrayIsArray(options.argv)) { | ||
| 95 | + throw new ERR_INVALID_ARG_TYPE('options.argv', 'Array', options.argv); | ||
| 96 | + } | ||
| 97 | + argv = options.argv.map(String); | ||
| 98 | + } | ||
| 92 | 99 | if (!options.eval) { | |
| 93 | 100 | if (!path.isAbsolute(filename) && !/^\.\.?[\\/]/.test(filename)) { | |
| 94 | 101 | throw new ERR_WORKER_PATH(filename); | |
@@ -158,6 +165,7 @@ class Worker extends EventEmitter { | |||
| 158 | 165 | this[kPublicPort].on('message', (message) => this.emit('message', message)); | |
| 159 | 166 | setupPortReferencing(this[kPublicPort], this, 'message'); | |
| 160 | 167 | this[kPort].postMessage({ | |
| 168 | + argv, | ||
| 161 | 169 | type: messageTypes.LOAD_SCRIPT, | |
| 162 | 170 | filename, | |
| 163 | 171 | doEval: !!options.eval, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,49 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const { Worker, isMainThread, workerData } = require('worker_threads'); | ||
| 5 | + | ||
| 6 | + if (isMainThread) { | ||
| 7 | + assert.throws(() => { | ||
| 8 | + new Worker(__filename, { argv: 'foo' }); | ||
| 9 | + }, { | ||
| 10 | + code: 'ERR_INVALID_ARG_TYPE' | ||
| 11 | + }); | ||
| 12 | + | ||
| 13 | + [ | ||
| 14 | + new Worker(__filename, { | ||
| 15 | + argv: [null, 'foo', 123, Symbol('bar')], | ||
| 16 | + // Asserts only if the worker is started by the test. | ||
| 17 | + workerData: 'assert-argv' | ||
| 18 | + }), | ||
| 19 | + new Worker(` | ||
| 20 | + const assert = require('assert'); | ||
| 21 | + assert.deepStrictEqual( | ||
| 22 | + process.argv, | ||
| 23 | + [process.execPath, '[worker eval]'] | ||
| 24 | + ); | ||
| 25 | + `, { | ||
| 26 | + eval: true | ||
| 27 | + }), | ||
| 28 | + new Worker(` | ||
| 29 | + const assert = require('assert'); | ||
| 30 | + assert.deepStrictEqual( | ||
| 31 | + process.argv, | ||
| 32 | + [process.execPath, '[worker eval]', 'null', 'foo', '123', | ||
| 33 | + String(Symbol('bar'))] | ||
| 34 | + ); | ||
| 35 | + `, { | ||
| 36 | + argv: [null, 'foo', 123, Symbol('bar')], | ||
| 37 | + eval: true | ||
| 38 | + }) | ||
| 39 | + ].forEach((worker) => { | ||
| 40 | + worker.on('exit', common.mustCall((code) => { | ||
| 41 | + assert.strictEqual(code, 0); | ||
| 42 | + })); | ||
| 43 | + }); | ||
| 44 | + } else if (workerData === 'assert-argv') { | ||
| 45 | + assert.deepStrictEqual( | ||
| 46 | + process.argv, | ||
| 47 | + [process.execPath, __filename, 'null', 'foo', '123', String(Symbol('bar'))] | ||
| 48 | + ); | ||
| 49 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments