| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 255cd7e commit 71d43a5
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -510,6 +510,9 @@ changes: | |||
| 510 | 510 | - version: v13.2.0 | |
| 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 | ||
@@ -143,12 +147,22 @@ port.on('message', (message) => { | |||
| 143 | 147 | port.postMessage({ type: UP_AND_RUNNING }); | |
| 144 | 148 | if (doEval) { | |
| 145 | 149 | const { evalScript } = require('internal/process/execution'); | |
| 146 | - evalScript('[worker eval]', filename); | ||
| 150 | + const name = '[worker eval]'; | ||
| 151 | + // This is necessary for CJS module compilation. | ||
| 152 | + // TODO: pass this with something really internal. | ||
| 153 | + ObjectDefineProperty(process, '_eval', { | ||
| 154 | + configurable: true, | ||
| 155 | + enumerable: true, | ||
| 156 | + value: filename, | ||
| 157 | + }); | ||
| 158 | + process.argv.splice(1, 0, name); | ||
| 159 | + evalScript(name, filename); | ||
| 147 | 160 | } else { | |
| 148 | 161 | // script filename | |
| 149 | 162 | // runMain here might be monkey-patched by users in --require. | |
| 150 | 163 | // XXX: the monkey-patchability here should probably be deprecated. | |
| 151 | - CJSLoader.Module.runMain(process.argv[1] = filename); | ||
| 164 | + process.argv.splice(1, 0, filename); | ||
| 165 | + CJSLoader.Module.runMain(filename); | ||
| 152 | 166 | } | |
| 153 | 167 | } else if (message.type === STDIO_PAYLOAD) { | |
| 154 | 168 | const { stream, chunk, encoding } = message; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,9 +82,16 @@ class Worker extends EventEmitter { | |||
| 82 | 82 | validateString(filename, 'filename'); | |
| 83 | 83 | if (options.execArgv && !ArrayIsArray(options.execArgv)) { | |
| 84 | 84 | throw new ERR_INVALID_ARG_TYPE('options.execArgv', | |
| 85 | - 'array', | ||
| 85 | + 'Array', | ||
| 86 | 86 | options.execArgv); | |
| 87 | 87 | } | |
| 88 | + let argv; | ||
| 89 | + if (options.argv) { | ||
| 90 | + if (!ArrayIsArray(options.argv)) { | ||
| 91 | + throw new ERR_INVALID_ARG_TYPE('options.argv', 'Array', options.argv); | ||
| 92 | + } | ||
| 93 | + argv = options.argv.map(String); | ||
| 94 | + } | ||
| 88 | 95 | if (!options.eval) { | |
| 89 | 96 | if (!path.isAbsolute(filename) && !/^\.\.?[\\/]/.test(filename)) { | |
| 90 | 97 | throw new ERR_WORKER_PATH(filename); | |
@@ -154,6 +161,7 @@ class Worker extends EventEmitter { | |||
| 154 | 161 | this[kPublicPort].on('message', (message) => this.emit('message', message)); | |
| 155 | 162 | setupPortReferencing(this[kPublicPort], this, 'message'); | |
| 156 | 163 | this[kPort].postMessage({ | |
| 164 | + argv, | ||
| 157 | 165 | type: messageTypes.LOAD_SCRIPT, | |
| 158 | 166 | filename, | |
| 159 | 167 | 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