1
1
import { once } from 'events' ;
2
- import { ChildProcess } from 'child_process' ;
2
+ import { ChildProcess , SpawnOptionsWithoutStdio } from 'child_process' ;
3
3
import { MongoClientOptions } from '@mongosh/service-provider-core' ;
4
4
import { Runtime } from '@mongosh/browser-runtime-core' ;
5
5
import { EvaluationListener } from '@mongosh/shell-evaluator' ;
6
6
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' ;
8
9
import type { WorkerRuntime as WorkerThreadWorkerRuntime } from './worker-runtime' ;
9
10
import childProcessProxySrc from 'inline-entry-loader!./child-process-proxy' ;
10
11
11
12
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
-
45
13
class WorkerRuntime implements Runtime {
46
14
private initOptions : {
47
15
uri : string ;
48
16
driverOptions : MongoClientOptions ;
49
17
cliOptions : { nodb ?: boolean } ;
18
+ spawnOptions : SpawnOptionsWithoutStdio ;
50
19
} ;
51
20
52
21
evaluationListener : EvaluationListener | null = null ;
53
22
54
- private childProcessEvaluationListener ! : WorkerEvaluationListener ;
23
+ private childProcessEvaluationListener ! : ChildProcessEvaluationListener ;
55
24
56
25
private childProcess ! : ChildProcess ;
57
26
@@ -62,27 +31,31 @@ class WorkerRuntime implements Runtime {
62
31
constructor (
63
32
uri : string ,
64
33
driverOptions : MongoClientOptions = { } ,
65
- cliOptions : { nodb ?: boolean } = { }
34
+ cliOptions : { nodb ?: boolean } = { } ,
35
+ spawnOptions : SpawnOptionsWithoutStdio = { }
66
36
) {
67
- this . initOptions = { uri, driverOptions, cliOptions } ;
37
+ this . initOptions = { uri, driverOptions, cliOptions, spawnOptions } ;
68
38
this . initWorkerPromise = this . initWorker ( ) ;
69
39
}
70
40
71
41
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
+ ) ;
73
48
74
49
this . childProcessRuntime = createCaller (
75
50
[ 'init' , 'evaluate' , 'getCompletions' , 'setEvaluationListener' ] ,
76
51
this . childProcess
77
52
) ;
78
53
79
- this . childProcessEvaluationListener = new WorkerEvaluationListener (
54
+ this . childProcessEvaluationListener = new ChildProcessEvaluationListener (
80
55
this ,
81
56
this . childProcess
82
57
) ;
83
58
84
- const { uri, driverOptions, cliOptions } = this . initOptions ;
85
-
86
59
await this . childProcessRuntime . init ( uri , driverOptions , cliOptions ) ;
87
60
}
88
61
0 commit comments