Skip to content

Commit 4ff5247

Browse files
authored
Merge pull request #31 from val-town/master
Allow process spawn options to be set
2 parents 16c6036 + eb38a08 commit 4ff5247

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-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: 18 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,11 @@ export interface DenoWorkerOptions {
141141
*/
142142
allowHrtime?: boolean;
143143
};
144+
145+
/**
146+
* Options used to spawn the Deno child process
147+
*/
148+
spawnOptions: SpawnOptions;
144149
}
145150

146151
/**
@@ -192,6 +197,7 @@ export class DenoWorker {
192197
denoLockFilePath: '',
193198
denoCachedOnly: false,
194199
denoNoCheck: false,
200+
spawnOptions: {},
195201
},
196202
options || {}
197203
);
@@ -346,13 +352,17 @@ export class DenoWorker {
346352
}
347353
}
348354

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

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)