Skip to content

Commit d63f103

Browse files
committed
Allow process spawn options to be set
1 parent 16c6036 commit d63f103

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DenoWorker.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ describe('DenoWorker', () => {
2020
const infiniteScript = readFileSync(infiniteFile, { encoding: 'utf-8' });
2121
const failFile = path.resolve(__dirname, './test/fail.js');
2222
const failScript = readFileSync(failFile, { encoding: 'utf-8' });
23+
const envFile = path.resolve(__dirname, './test/env.js');
24+
const envScript = readFileSync(envFile, { encoding: 'utf-8' });
2325

2426
afterEach(() => {
2527
if (worker) {
@@ -707,6 +709,41 @@ describe('DenoWorker', () => {
707709
);
708710
});
709711
});
712+
713+
describe('spawnOptions', async () => {
714+
it('should be able to pass spawn options', async () => {
715+
worker = new DenoWorker(envScript, {
716+
permissions: {
717+
allowEnv: true,
718+
},
719+
spawnOptions: {
720+
env: {
721+
...process.env,
722+
HELLO: 'WORLD',
723+
},
724+
},
725+
});
726+
727+
let ret: any;
728+
let resolve: any;
729+
let promise = new Promise((res, rej) => {
730+
resolve = res;
731+
});
732+
worker.onmessage = (e) => {
733+
ret = e.data;
734+
resolve();
735+
};
736+
737+
worker.postMessage({
738+
type: 'env',
739+
name: 'HELLO',
740+
});
741+
742+
await promise;
743+
744+
expect(ret).toEqual('WORLD');
745+
});
746+
});
710747
});
711748

712749
describe('data types', () => {

src/DenoWorker.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createServer, Server } from 'http';
22
import WebSocket, { Server as WSServer } from 'ws';
33
import { resolve } from 'path';
4-
import { ChildProcess, spawn } from 'child_process';
4+
import { ChildProcess, spawn, SpawnOptions } from 'child_process';
55
import {
66
serializeStructure,
77
deserializeStructure,
@@ -141,6 +141,8 @@ export interface DenoWorkerOptions {
141141
*/
142142
allowHrtime?: boolean;
143143
};
144+
145+
spawnOptions: SpawnOptions;
144146
}
145147

146148
/**
@@ -192,6 +194,7 @@ export class DenoWorker {
192194
denoLockFilePath: '',
193195
denoCachedOnly: false,
194196
denoNoCheck: false,
197+
spawnOptions: {},
195198
},
196199
options || {}
197200
);
@@ -346,13 +349,17 @@ export class DenoWorker {
346349
}
347350
}
348351

349-
this._process = spawn(this._options.denoExecutable, [
350-
'run',
351-
...runArgs,
352-
this._options.denoBootstrapScriptPath,
353-
connectAddress,
354-
...scriptArgs,
355-
]);
352+
this._process = spawn(
353+
this._options.denoExecutable,
354+
[
355+
'run',
356+
...runArgs,
357+
this._options.denoBootstrapScriptPath,
358+
connectAddress,
359+
...scriptArgs,
360+
],
361+
this._options.spawnOptions
362+
);
356363
this._process.on('exit', (code: number, signal: string) => {
357364
this.terminate();
358365

src/test/env.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
self.onmessage = (e) => {
2+
if (e.data.type === 'env') {
3+
self.postMessage(Deno.env.get(e.data.name));
4+
}
5+
};

0 commit comments

Comments
 (0)