Skip to content

Commit e70bbf5

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

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
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: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,23 @@ import { MongoClientOptions } from '@mongosh/service-provider-core';
66
import { Runtime } from '@mongosh/browser-runtime-core';
77
import { EvaluationListener } from '@mongosh/shell-evaluator';
88
import spawnChildFromSource, { kill } from './spawn-child-from-source';
9-
import { Caller, createCaller, exposeAll, WithClose } from './rpc';
9+
import { Caller, createCaller } from './rpc';
10+
import { ChildProcessEvaluationListener } from './child-process-evaluation-listener';
1011
import type { WorkerRuntime as WorkerThreadWorkerRuntime } from './worker-runtime';
1112
import childProcessProxySrc from 'inline-entry-loader!./child-process-proxy';
1213

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

5423
evaluationListener: EvaluationListener | null = null;
5524

56-
private childProcessEvaluationListener!: WorkerEvaluationListener;
25+
private childProcessEvaluationListener!: ChildProcessEvaluationListener;
5726

5827
private childProcess!: ChildProcess;
5928

@@ -64,27 +33,31 @@ class WorkerRuntime implements Runtime {
6433
constructor(
6534
uri: string,
6635
driverOptions: MongoClientOptions = {},
67-
cliOptions: { nodb?: boolean } = {}
36+
cliOptions: { nodb?: boolean } = {},
37+
spawnOptions: SpawnOptionsWithoutStdio = {}
6838
) {
69-
this.initOptions = { uri, driverOptions, cliOptions };
39+
this.initOptions = { uri, driverOptions, cliOptions, spawnOptions };
7040
this.initWorkerPromise = this.initWorker();
7141
}
7242

7343
private async initWorker() {
74-
this.childProcess = await spawnChildFromSource(childProcessProxySrc);
44+
const { uri, driverOptions, cliOptions, spawnOptions } = this.initOptions;
45+
46+
this.childProcess = await spawnChildFromSource(
47+
childProcessProxySrc,
48+
spawnOptions
49+
);
7550

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

81-
this.childProcessEvaluationListener = new WorkerEvaluationListener(
56+
this.childProcessEvaluationListener = new ChildProcessEvaluationListener(
8257
this,
8358
this.childProcess
8459
);
8560

86-
const { uri, driverOptions, cliOptions } = this.initOptions;
87-
8861
await this.childProcessRuntime.init(uri, driverOptions, cliOptions);
8962
}
9063

0 commit comments

Comments
 (0)