Skip to content

Commit 95b3f1e

Browse files
committed
feat(node-runtime-worker-thread): Allow to configure spawn options of the child process created by worker runtime
1 parent e7da859 commit 95b3f1e

File tree

2 files changed

+51
-42
lines changed

2 files changed

+51
-42
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { ChildProcess } from 'child_process';
2+
import { EvaluationListener } from '@mongosh/shell-evaluator';
3+
import { exposeAll, WithClose } from './rpc';
4+
import type { WorkerRuntime } from './index';
5+
6+
export class ChildProcessEvaluationListener {
7+
exposedListener: WithClose<EvaluationListener>;
8+
9+
constructor(workerRuntime: WorkerRuntime, childProcess: ChildProcess) {
10+
this.exposedListener = exposeAll<EvaluationListener>(
11+
{
12+
onPrompt(question, type) {
13+
return (
14+
workerRuntime.evaluationListener?.onPrompt?.(question, type) ?? ''
15+
);
16+
},
17+
onPrint(values) {
18+
return workerRuntime.evaluationListener?.onPrint?.(values);
19+
},
20+
toggleTelemetry(enabled) {
21+
return workerRuntime.evaluationListener?.toggleTelemetry?.(enabled);
22+
},
23+
onClearCommand() {
24+
return workerRuntime.evaluationListener?.onClearCommand?.();
25+
},
26+
onExit() {
27+
return (
28+
workerRuntime.evaluationListener?.onExit?.() ??
29+
(Promise.resolve() as Promise<never>)
30+
);
31+
}
32+
},
33+
childProcess
34+
);
35+
}
36+
}

packages/node-runtime-worker-thread/src/index.ts

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,26 @@
11
import { once } from 'events';
2-
import { ChildProcess } from 'child_process';
2+
import { ChildProcess, SpawnOptionsWithoutStdio } from 'child_process';
33
import { MongoClientOptions } from '@mongosh/service-provider-core';
44
import { Runtime } from '@mongosh/browser-runtime-core';
55
import { EvaluationListener } from '@mongosh/shell-evaluator';
66
import spawnChildFromSource from './spawn-child-from-source';
7-
import { Caller, createCaller, exposeAll, WithClose } from './rpc';
7+
import { Caller, createCaller } from './rpc';
8+
import { ChildProcessEvaluationListener } from './child-process-evaluation-listener';
89
import type { WorkerRuntime as WorkerThreadWorkerRuntime } from './worker-runtime';
910
import childProcessProxySrc from 'inline-entry-loader!./child-process-proxy';
1011

1112
type ChildProcessRuntime = Caller<WorkerThreadWorkerRuntime>;
12-
13-
class WorkerEvaluationListener {
14-
exposedListener: WithClose<EvaluationListener>;
15-
16-
constructor(workerRuntime: WorkerRuntime, childProcess: ChildProcess) {
17-
this.exposedListener = exposeAll<EvaluationListener>(
18-
{
19-
onPrompt(question, type) {
20-
return (
21-
workerRuntime.evaluationListener?.onPrompt?.(question, type) ?? ''
22-
);
23-
},
24-
onPrint(values) {
25-
return workerRuntime.evaluationListener?.onPrint?.(values);
26-
},
27-
toggleTelemetry(enabled) {
28-
return workerRuntime.evaluationListener?.toggleTelemetry?.(enabled);
29-
},
30-
onClearCommand() {
31-
return workerRuntime.evaluationListener?.onClearCommand?.();
32-
},
33-
onExit() {
34-
return (
35-
workerRuntime.evaluationListener?.onExit?.() ??
36-
(Promise.resolve() as Promise<never>)
37-
);
38-
}
39-
},
40-
childProcess
41-
);
42-
}
43-
}
44-
4513
class WorkerRuntime implements Runtime {
4614
private initOptions: {
4715
uri: string;
4816
driverOptions: MongoClientOptions;
4917
cliOptions: { nodb?: boolean };
18+
spawnOptions: SpawnOptionsWithoutStdio;
5019
};
5120

5221
evaluationListener: EvaluationListener | null = null;
5322

54-
private childProcessEvaluationListener!: WorkerEvaluationListener;
23+
private childProcessEvaluationListener!: ChildProcessEvaluationListener;
5524

5625
private childProcess!: ChildProcess;
5726

@@ -62,27 +31,31 @@ class WorkerRuntime implements Runtime {
6231
constructor(
6332
uri: string,
6433
driverOptions: MongoClientOptions = {},
65-
cliOptions: { nodb?: boolean } = {}
34+
cliOptions: { nodb?: boolean } = {},
35+
spawnOptions: SpawnOptionsWithoutStdio = {}
6636
) {
67-
this.initOptions = { uri, driverOptions, cliOptions };
37+
this.initOptions = { uri, driverOptions, cliOptions, spawnOptions };
6838
this.initWorkerPromise = this.initWorker();
6939
}
7040

7141
private async initWorker() {
72-
this.childProcess = await spawnChildFromSource(childProcessProxySrc);
42+
const { uri, driverOptions, cliOptions, spawnOptions } = this.initOptions;
43+
44+
this.childProcess = await spawnChildFromSource(
45+
childProcessProxySrc,
46+
spawnOptions
47+
);
7348

7449
this.childProcessRuntime = createCaller(
7550
['init', 'evaluate', 'getCompletions', 'setEvaluationListener'],
7651
this.childProcess
7752
);
7853

79-
this.childProcessEvaluationListener = new WorkerEvaluationListener(
54+
this.childProcessEvaluationListener = new ChildProcessEvaluationListener(
8055
this,
8156
this.childProcess
8257
);
8358

84-
const { uri, driverOptions, cliOptions } = this.initOptions;
85-
8659
await this.childProcessRuntime.init(uri, driverOptions, cliOptions);
8760
}
8861

0 commit comments

Comments
 (0)