@mongosh/node-runtime-worker-thread 1.3.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/AUTHORS +1 -0
  2. package/dist/child-process-evaluation-listener.d.ts +1 -1
  3. package/dist/child-process-evaluation-listener.js +51 -0
  4. package/dist/child-process-evaluation-listener.js.map +1 -0
  5. package/dist/child-process-mongosh-bus.js +25 -0
  6. package/dist/child-process-mongosh-bus.js.map +1 -0
  7. package/dist/child-process-proxy.js +1 -1
  8. package/dist/child-process-proxy.js.map +1 -0
  9. package/dist/index.d.ts +2 -1
  10. package/dist/index.js +1 -1
  11. package/dist/index.js.map +1 -0
  12. package/dist/lock.js +37 -0
  13. package/dist/lock.js.map +1 -0
  14. package/dist/rpc.js +143 -0
  15. package/dist/rpc.js.map +1 -0
  16. package/dist/serializer.d.ts +3 -0
  17. package/dist/serializer.js +106 -0
  18. package/dist/serializer.js.map +1 -0
  19. package/dist/spawn-child-from-source.js +68 -0
  20. package/dist/spawn-child-from-source.js.map +1 -0
  21. package/dist/src/child-process-evaluation-listener.d.ts +10 -0
  22. package/dist/src/child-process-mongosh-bus.d.ts +9 -0
  23. package/dist/src/child-process-proxy.d.ts +1 -0
  24. package/dist/src/index.d.ts +29 -0
  25. package/dist/src/lock.d.ts +10 -0
  26. package/dist/src/rpc.d.ts +28 -0
  27. package/dist/src/serializer.d.ts +13 -0
  28. package/dist/src/spawn-child-from-source.d.ts +4 -0
  29. package/dist/src/worker-runtime.d.ts +14 -0
  30. package/dist/worker-runtime.d.ts +3 -1
  31. package/dist/worker-runtime.js +101 -97
  32. package/dist/worker-runtime.js.map +1 -0
  33. package/package.json +10 -10
  34. package/src/child-process-evaluation-listener.ts +1 -1
  35. package/src/index.spec.ts +8 -8
  36. package/src/index.ts +4 -3
  37. package/src/lock.spec.ts +1 -1
  38. package/src/rpc.spec.ts +4 -4
  39. package/src/rpc.ts +2 -2
  40. package/src/serializer.spec.ts +87 -1
  41. package/src/serializer.ts +32 -0
  42. package/src/spawn-child-from-source.spec.ts +3 -3
  43. package/src/worker-runtime.spec.ts +6 -6
  44. package/src/worker-runtime.ts +6 -7
package/dist/rpc.js ADDED
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createCaller = exports.exposeAll = exports.cancel = exports.close = exports.removeTrailingUndefined = exports.deserialize = exports.serialize = void 0;
7
+ const v8_1 = __importDefault(require("v8"));
8
+ const postmsg_rpc_1 = require("postmsg-rpc");
9
+ const serializer_1 = require("./serializer");
10
+ function serialize(data) {
11
+ return `data:;base64,${v8_1.default.serialize(data).toString('base64')}`;
12
+ }
13
+ exports.serialize = serialize;
14
+ function deserialize(str) {
15
+ if (/^data:;base64,.+/.test(str)) {
16
+ return v8_1.default.deserialize(Buffer.from(str.replace('data:;base64,', ''), 'base64'));
17
+ }
18
+ return str;
19
+ }
20
+ exports.deserialize = deserialize;
21
+ var RPCMessageTypes;
22
+ (function (RPCMessageTypes) {
23
+ RPCMessageTypes[RPCMessageTypes["Message"] = 0] = "Message";
24
+ RPCMessageTypes[RPCMessageTypes["Error"] = 1] = "Error";
25
+ })(RPCMessageTypes || (RPCMessageTypes = {}));
26
+ function isRPCError(data) {
27
+ return (data && typeof data === 'object' && data.type === RPCMessageTypes.Error);
28
+ }
29
+ function isMessageData(data) {
30
+ return data && typeof data === 'object' && 'id' in data && 'sender' in data;
31
+ }
32
+ function isServerMessageData(data) {
33
+ return isMessageData(data) && data.sender === 'postmsg-rpc/server';
34
+ }
35
+ function isClientMessageData(data) {
36
+ return isMessageData(data) && data.sender === 'postmsg-rpc/client';
37
+ }
38
+ function removeTrailingUndefined(arr) {
39
+ if (Array.isArray(arr)) {
40
+ arr = [...arr];
41
+ while (arr.length > 0 && arr[arr.length - 1] === undefined) {
42
+ arr.pop();
43
+ }
44
+ }
45
+ return arr;
46
+ }
47
+ exports.removeTrailingUndefined = removeTrailingUndefined;
48
+ function send(messageBus, data) {
49
+ if ('postMessage' in messageBus &&
50
+ typeof messageBus.postMessage === 'function') {
51
+ messageBus.postMessage(data);
52
+ }
53
+ if ('send' in messageBus && typeof messageBus.send === 'function') {
54
+ messageBus.send(data);
55
+ }
56
+ }
57
+ function getRPCOptions(messageBus) {
58
+ return {
59
+ addListener: messageBus.on.bind(messageBus),
60
+ removeListener: messageBus.off.bind(messageBus),
61
+ postMessage(data) {
62
+ if (isClientMessageData(data) && Array.isArray(data.args)) {
63
+ data.args = serialize(removeTrailingUndefined(data.args));
64
+ }
65
+ if (isServerMessageData(data)) {
66
+ try {
67
+ data.res = serialize(data.res);
68
+ }
69
+ catch (e) {
70
+ data.res = serialize({
71
+ type: RPCMessageTypes.Error,
72
+ payload: (0, serializer_1.serializeError)(e)
73
+ });
74
+ }
75
+ }
76
+ return send(messageBus, data);
77
+ },
78
+ getMessageData(data) {
79
+ if (isClientMessageData(data) &&
80
+ data.args &&
81
+ typeof data.args === 'string') {
82
+ data.args = deserialize(data.args);
83
+ }
84
+ if (isServerMessageData(data) && typeof data.res === 'string') {
85
+ data.res = deserialize(data.res);
86
+ }
87
+ return data;
88
+ }
89
+ };
90
+ }
91
+ exports.close = Symbol('@@rpc.close');
92
+ exports.cancel = Symbol('@@rpc.cancel');
93
+ function exposeAll(obj, messageBus) {
94
+ Object.entries(obj).forEach(([key, val]) => {
95
+ const { close } = (0, postmsg_rpc_1.expose)(key, async (...args) => {
96
+ try {
97
+ return { type: RPCMessageTypes.Message, payload: await val(...args) };
98
+ }
99
+ catch (e) {
100
+ return { type: RPCMessageTypes.Error, payload: (0, serializer_1.serializeError)(e) };
101
+ }
102
+ }, getRPCOptions(messageBus));
103
+ val.close = close;
104
+ });
105
+ Object.defineProperty(obj, exports.close, {
106
+ enumerable: false,
107
+ value() {
108
+ Object.values(obj).forEach((fn) => {
109
+ fn.close();
110
+ });
111
+ }
112
+ });
113
+ return obj;
114
+ }
115
+ exports.exposeAll = exposeAll;
116
+ function createCaller(methodNames, messageBus) {
117
+ const obj = {};
118
+ const inflight = new Set();
119
+ methodNames.forEach((name) => {
120
+ const c = (0, postmsg_rpc_1.caller)(name, getRPCOptions(messageBus));
121
+ obj[name] = async (...args) => {
122
+ const promise = c(...args);
123
+ inflight.add(promise);
124
+ const result = (await promise);
125
+ inflight.delete(promise);
126
+ if (isRPCError(result))
127
+ throw (0, serializer_1.deserializeError)(result.payload);
128
+ return result.payload;
129
+ };
130
+ });
131
+ Object.defineProperty(obj, exports.cancel, {
132
+ enumerable: false,
133
+ value() {
134
+ for (const cancelable of inflight) {
135
+ cancelable.cancel();
136
+ inflight.delete(cancelable);
137
+ }
138
+ }
139
+ });
140
+ return obj;
141
+ }
142
+ exports.createCaller = createCaller;
143
+ //# sourceMappingURL=rpc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.js","sourceRoot":"","sources":["../src/rpc.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,6CAA6C;AAC7C,6CAAgE;AAQhE,SAAgB,SAAS,CAAC,IAAa;IACrC,OAAO,gBAAgB,YAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;AACjE,CAAC;AAFD,8BAEC;AAED,SAAgB,WAAW,CAAc,GAAW;IAClD,IAAI,kBAAkB,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;QAChC,OAAO,YAAE,CAAC,WAAW,CACnB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,CAAC,EAAE,QAAQ,CAAC,CACxD,CAAC;KACH;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAPD,kCAOC;AAOD,IAAK,eAGJ;AAHD,WAAK,eAAe;IAClB,2DAAO,CAAA;IACP,uDAAK,CAAA;AACP,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,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QAC3B,MAAM,CAAC,GAAG,IAAA,oBAAM,EAAC,IAAc,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC,CAAC;QAC3D,GAAW,CAAC,IAAI,CAAC,GAAG,KAAK,EAAC,GAAG,IAAe,EAAE,EAAE;YAC/C,MAAM,OAAO,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;YAC3B,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YACtB,MAAM,MAAM,GAAG,CAAC,MAAM,OAAO,CAA0B,CAAC;YACxD,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YACzB,IAAI,UAAU,CAAC,MAAM,CAAC;gBAAE,MAAM,IAAA,6BAAgB,EAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAC/D,OAAO,MAAM,CAAC,OAAO,CAAC;QACxB,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;IACH,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,cAAM,EAAE;QACjC,UAAU,EAAE,KAAK;QACjB,KAAK;YACH,KAAK,MAAM,UAAU,IAAI,QAAQ,EAAE;gBACjC,UAAU,CAAC,MAAM,EAAE,CAAC;gBACpB,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;aAC7B;QACH,CAAC;KACF,CAAC,CAAC;IACH,OAAO,GAA+C,CAAC;AACzD,CAAC;AA3BD,oCA2BC"}
@@ -1,4 +1,5 @@
1
1
  import { RuntimeEvaluationResult } from '@mongosh/browser-runtime-core';
2
+ import type { DevtoolsConnectOptions } from '@mongosh/service-provider-server/lib/cli-service-provider';
2
3
  export declare function serializeError(err: Error): Error;
3
4
  export declare function deserializeError(err: any): Error;
4
5
  export declare enum SerializedResultTypes {
@@ -8,3 +9,5 @@ export declare enum SerializedResultTypes {
8
9
  }
9
10
  export declare function serializeEvaluationResult({ type, printable, source }: RuntimeEvaluationResult): RuntimeEvaluationResult;
10
11
  export declare function deserializeEvaluationResult({ type, printable, source }: RuntimeEvaluationResult): RuntimeEvaluationResult;
12
+ export declare function serializeConnectOptions(options?: Readonly<DevtoolsConnectOptions>): DevtoolsConnectOptions;
13
+ export declare function deserializeConnectOptions(options: Readonly<DevtoolsConnectOptions>): DevtoolsConnectOptions;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deserializeConnectOptions = exports.serializeConnectOptions = exports.deserializeEvaluationResult = exports.serializeEvaluationResult = exports.SerializedResultTypes = exports.deserializeError = exports.serializeError = void 0;
4
+ const util_1 = require("util");
5
+ const bson_1 = require("bson");
6
+ function isPrimitive(val) {
7
+ return (typeof val !== 'object' && typeof val !== 'function') || val === null;
8
+ }
9
+ function isError(val) {
10
+ return val && val.name && val.message && val.stack;
11
+ }
12
+ function getNames(obj) {
13
+ return Object.getOwnPropertyNames(obj);
14
+ }
15
+ function serializeError(err) {
16
+ const keys = getNames(err).concat('name');
17
+ return keys.reduce((acc, key) => {
18
+ acc[key] = err[key];
19
+ return acc;
20
+ }, {});
21
+ }
22
+ exports.serializeError = serializeError;
23
+ function deserializeError(err) {
24
+ return Object.assign(new Error(), err);
25
+ }
26
+ exports.deserializeError = deserializeError;
27
+ var SerializedResultTypes;
28
+ (function (SerializedResultTypes) {
29
+ SerializedResultTypes["SerializedErrorResult"] = "SerializedErrorResult";
30
+ SerializedResultTypes["InspectResult"] = "InspectResult";
31
+ SerializedResultTypes["SerializedShellApiResult"] = "SerializedShellApiResult";
32
+ })(SerializedResultTypes = exports.SerializedResultTypes || (exports.SerializedResultTypes = {}));
33
+ function serializeEvaluationResult({ type, printable, source }) {
34
+ if (isPrimitive(printable)) {
35
+ return { type, printable, source };
36
+ }
37
+ if (isError(printable)) {
38
+ return {
39
+ type: SerializedResultTypes.SerializedErrorResult,
40
+ printable: serializeError(printable),
41
+ source
42
+ };
43
+ }
44
+ if (type === null) {
45
+ return {
46
+ type: SerializedResultTypes.InspectResult,
47
+ printable: (0, util_1.inspect)(printable),
48
+ source
49
+ };
50
+ }
51
+ return {
52
+ type: SerializedResultTypes.SerializedShellApiResult,
53
+ printable: {
54
+ origType: type,
55
+ serializedValue: bson_1.EJSON.serialize(printable)
56
+ }
57
+ };
58
+ }
59
+ exports.serializeEvaluationResult = serializeEvaluationResult;
60
+ function deserializeEvaluationResult({ type, printable, source }) {
61
+ if (type === SerializedResultTypes.SerializedErrorResult) {
62
+ return { type, printable: deserializeError(printable), source };
63
+ }
64
+ if (type === SerializedResultTypes.SerializedShellApiResult) {
65
+ return {
66
+ type: printable.origType,
67
+ printable: bson_1.EJSON.deserialize(printable.serializedValue),
68
+ source
69
+ };
70
+ }
71
+ return { type, printable, source };
72
+ }
73
+ exports.deserializeEvaluationResult = deserializeEvaluationResult;
74
+ const autoEncryptionBSONOptions = [
75
+ 'schemaMap',
76
+ 'encryptedFieldsMap'
77
+ ];
78
+ function serializeConnectOptions(options = {}) {
79
+ var _a;
80
+ const serializedOptions = { ...options };
81
+ for (const autoEncryptionOption of autoEncryptionBSONOptions) {
82
+ if ((_a = serializedOptions.autoEncryption) === null || _a === void 0 ? void 0 : _a[autoEncryptionOption]) {
83
+ serializedOptions.autoEncryption = {
84
+ ...serializedOptions.autoEncryption,
85
+ [autoEncryptionOption]: bson_1.EJSON.serialize(serializedOptions.autoEncryption[autoEncryptionOption], { relaxed: false })
86
+ };
87
+ }
88
+ }
89
+ return serializedOptions;
90
+ }
91
+ exports.serializeConnectOptions = serializeConnectOptions;
92
+ function deserializeConnectOptions(options) {
93
+ var _a;
94
+ const deserializedOptions = { ...options };
95
+ for (const autoEncryptionOption of autoEncryptionBSONOptions) {
96
+ if ((_a = deserializedOptions.autoEncryption) === null || _a === void 0 ? void 0 : _a[autoEncryptionOption]) {
97
+ deserializedOptions.autoEncryption = {
98
+ ...deserializedOptions.autoEncryption,
99
+ [autoEncryptionOption]: bson_1.EJSON.deserialize(deserializedOptions.autoEncryption[autoEncryptionOption], { relaxed: false })
100
+ };
101
+ }
102
+ }
103
+ return deserializedOptions;
104
+ }
105
+ exports.deserializeConnectOptions = deserializeConnectOptions;
106
+ //# sourceMappingURL=serializer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serializer.js","sourceRoot":"","sources":["../src/serializer.ts"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,+BAA6B;AAI7B,SAAS,WAAW,CAClB,GAAQ;IAER,OAAO,CAAC,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,KAAK,UAAU,CAAC,IAAI,GAAG,KAAK,IAAI,CAAC;AAChF,CAAC;AAED,SAAS,OAAO,CAAC,GAAQ;IACvB,OAAO,GAAG,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC;AACrD,CAAC;AAED,SAAS,QAAQ,CAAI,GAAM;IACzB,OAAO,MAAM,CAAC,mBAAmB,CAAC,GAAG,CAAgB,CAAC;AACxD,CAAC;AAUD,SAAgB,cAAc,CAAC,GAAU;IAEvC,MAAM,IAAI,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC1C,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC7B,GAAW,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;QAC7B,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,EAAW,CAAC,CAAC;AAClB,CAAC;AAPD,wCAOC;AAKD,SAAgB,gBAAgB,CAAC,GAAQ;IACvC,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,EAAE,EAAE,GAAG,CAAC,CAAC;AACzC,CAAC;AAFD,4CAEC;AAED,IAAY,qBAIX;AAJD,WAAY,qBAAqB;IAC/B,wEAA+C,CAAA;IAC/C,wDAA+B,CAAA;IAC/B,8EAAqD,CAAA;AACvD,CAAC,EAJW,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,kEAkBC;AAED,MAAM,yBAAyB,GAAG;IAChC,WAAW;IACX,oBAAoB;CACZ,CAAC;AAEX,SAAgB,uBAAuB,CAAC,UAA4C,EAAE;;IACpF,MAAM,iBAAiB,GAAQ,EAAE,GAAG,OAAO,EAAE,CAAC;IAC9C,KAAK,MAAM,oBAAoB,IAAI,yBAAyB,EAAE;QAC5D,IAAI,MAAA,iBAAiB,CAAC,cAAc,0CAAG,oBAAoB,CAAC,EAAE;YAC5D,iBAAiB,CAAC,cAAc,GAAG;gBACjC,GAAG,iBAAiB,CAAC,cAAc;gBACnC,CAAC,oBAAoB,CAAC,EAAE,YAAK,CAAC,SAAS,CAAC,iBAAiB,CAAC,cAAc,CAAC,oBAAoB,CAAC,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;aACpH,CAAC;SACH;KACF;IACD,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AAXD,0DAWC;AAED,SAAgB,yBAAyB,CAAC,OAAyC;;IACjF,MAAM,mBAAmB,GAAQ,EAAE,GAAG,OAAO,EAAE,CAAC;IAChD,KAAK,MAAM,oBAAoB,IAAI,yBAAyB,EAAE;QAC5D,IAAI,MAAA,mBAAmB,CAAC,cAAc,0CAAG,oBAAoB,CAAC,EAAE;YAC9D,mBAAmB,CAAC,cAAc,GAAG;gBACnC,GAAG,mBAAmB,CAAC,cAAc;gBACrC,CAAC,oBAAoB,CAAC,EAAE,YAAK,CAAC,WAAW,CAAC,mBAAmB,CAAC,cAAc,CAAC,oBAAoB,CAAC,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;aACxH,CAAC;SACH;KACF;IACD,OAAO,mBAAmB,CAAC;AAC7B,CAAC;AAXD,8DAWC"}
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.kill = void 0;
4
+ const child_process_1 = require("child_process");
5
+ const events_1 = require("events");
6
+ async function kill(childProcess, code = 'SIGTERM') {
7
+ childProcess.kill(code);
8
+ if (childProcess.exitCode === null && childProcess.signalCode === null) {
9
+ await (0, events_1.once)(childProcess, 'exit');
10
+ }
11
+ }
12
+ exports.kill = kill;
13
+ function spawnChildFromSource(src, spawnOptions = {}, timeoutMs, _stdout = 'inherit', _stderr = 'inherit') {
14
+ return new Promise(async (resolve, reject) => {
15
+ const readyToken = Date.now().toString(32);
16
+ const childProcess = (0, child_process_1.spawn)(process.execPath, {
17
+ stdio: ['pipe', _stdout, _stderr, 'ipc'],
18
+ ...spawnOptions
19
+ });
20
+ if (!childProcess.stdin) {
21
+ await kill(childProcess);
22
+ return reject(new Error("Can't write src to the spawned process, missing stdin"));
23
+ }
24
+ let timeoutId;
25
+ function cleanupListeners() {
26
+ if (timeoutId) {
27
+ clearTimeout(timeoutId);
28
+ }
29
+ if (childProcess.stdin) {
30
+ childProcess.stdin.off('error', onWriteError);
31
+ }
32
+ childProcess.off('message', onMessage);
33
+ childProcess.off('exit', onExit);
34
+ }
35
+ function onExit(exitCode) {
36
+ if (exitCode && exitCode > 0) {
37
+ cleanupListeners();
38
+ reject(new Error('Child process exited with error before starting'));
39
+ }
40
+ }
41
+ async function onWriteError(error) {
42
+ cleanupListeners();
43
+ await kill(childProcess);
44
+ reject(error);
45
+ }
46
+ async function onTimeout() {
47
+ cleanupListeners();
48
+ await kill(childProcess);
49
+ reject(new Error('Timed out while waiting for child process to start'));
50
+ }
51
+ function onMessage(data) {
52
+ if (data === readyToken) {
53
+ cleanupListeners();
54
+ resolve(childProcess);
55
+ }
56
+ }
57
+ childProcess.on('message', onMessage);
58
+ childProcess.on('exit', onExit);
59
+ childProcess.stdin.on('error', onWriteError);
60
+ childProcess.stdin.write(src);
61
+ childProcess.stdin.write(`;process.send(${JSON.stringify(readyToken)})`);
62
+ childProcess.stdin.end();
63
+ timeoutId =
64
+ timeoutMs !== undefined ? setTimeout(onTimeout, timeoutMs) : null;
65
+ });
66
+ }
67
+ exports.default = spawnChildFromSource;
68
+ //# sourceMappingURL=spawn-child-from-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AS,KAAK,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACtE,CAAC,CAAC,CAAC;AACL,CAAC;AA5ED,uCA4EC"}
@@ -0,0 +1,10 @@
1
+ /// <reference types="node" />
2
+ import { ChildProcess } from 'child_process';
3
+ import { Exposed } from './rpc';
4
+ import type { WorkerRuntime } from './index';
5
+ import { RuntimeEvaluationListener } from '@mongosh/browser-runtime-core';
6
+ export declare class ChildProcessEvaluationListener {
7
+ exposedListener: Exposed<Required<Omit<RuntimeEvaluationListener, 'onLoad' | 'getCSFLELibraryOptions'>>>;
8
+ constructor(workerRuntime: WorkerRuntime, childProcess: ChildProcess);
9
+ terminate(): void;
10
+ }
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ import { ChildProcess } from 'child_process';
3
+ import { MongoshBus } from '@mongosh/types';
4
+ import { Exposed } from './rpc';
5
+ export declare class ChildProcessMongoshBus {
6
+ exposedEmitter: Exposed<MongoshBus>;
7
+ constructor(eventEmitter: MongoshBus, childProcess: ChildProcess);
8
+ terminate(): void;
9
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ /// <reference types="node" />
2
+ import { SpawnOptionsWithoutStdio } from 'child_process';
3
+ import { Runtime, RuntimeEvaluationListener, RuntimeEvaluationResult } from '@mongosh/browser-runtime-core';
4
+ import type { MongoshBus } from '@mongosh/types';
5
+ import type { CompassServiceProvider } from '@mongosh/service-provider-server';
6
+ declare type DevtoolsConnectOptions = Parameters<(typeof CompassServiceProvider)['connect']>[1];
7
+ declare class WorkerRuntime implements Runtime {
8
+ private initOptions;
9
+ evaluationListener: RuntimeEvaluationListener | null;
10
+ private eventEmitter;
11
+ private childProcessMongoshBus;
12
+ private childProcessEvaluationListener;
13
+ private childProcess;
14
+ private childProcessRuntime;
15
+ private initWorkerPromise;
16
+ private childProcessProxySrcPath;
17
+ constructor(uri: string, driverOptions?: DevtoolsConnectOptions, cliOptions?: {
18
+ nodb?: boolean;
19
+ }, spawnOptions?: SpawnOptionsWithoutStdio, eventEmitter?: MongoshBus);
20
+ private initWorker;
21
+ evaluate(code: string): Promise<RuntimeEvaluationResult>;
22
+ getCompletions(code: string): Promise<import("@mongosh/browser-runtime-core").Completion[]>;
23
+ getShellPrompt(): Promise<string>;
24
+ setEvaluationListener(listener: RuntimeEvaluationListener | null): RuntimeEvaluationListener | null;
25
+ terminate(): Promise<void>;
26
+ interrupt(): Promise<boolean>;
27
+ waitForRuntimeToBeReady(): Promise<void>;
28
+ }
29
+ export { WorkerRuntime };
@@ -0,0 +1,10 @@
1
+ export declare type UNLOCKED = 'UNLOCKED';
2
+ export declare class Lock {
3
+ private static UNLOCK_TOKEN;
4
+ private promise;
5
+ private resolve;
6
+ lock(): Promise<UNLOCKED>;
7
+ unlock(): boolean;
8
+ isLocked(): boolean;
9
+ isUnlockToken(resolvedValue: any): resolvedValue is UNLOCKED;
10
+ }
@@ -0,0 +1,28 @@
1
+ export declare function serialize(data: unknown): string;
2
+ export declare function deserialize<T = unknown>(str: string): T | string;
3
+ declare type RPCMessageBus = {
4
+ on: Function;
5
+ off: Function;
6
+ } & ({
7
+ postMessage: Function;
8
+ send?: never;
9
+ } | {
10
+ postMessage?: never;
11
+ send?: Function;
12
+ });
13
+ export declare function removeTrailingUndefined(arr: unknown[]): unknown[];
14
+ export declare const close: unique symbol;
15
+ export declare const cancel: unique symbol;
16
+ export declare type Exposed<T> = {
17
+ [k in keyof T]: T[k] & {
18
+ close(): void;
19
+ };
20
+ } & {
21
+ [close]: () => void;
22
+ };
23
+ export declare function exposeAll<O>(obj: O, messageBus: RPCMessageBus): Exposed<O>;
24
+ export declare type Caller<Impl, Keys extends keyof Impl = keyof Impl> = CancelableMethods<Pick<Impl, Keys>> & {
25
+ [cancel]: () => void;
26
+ };
27
+ export declare function createCaller<Impl extends {}>(methodNames: Extract<keyof Impl, string>[], messageBus: RPCMessageBus): Caller<Impl, typeof methodNames[number]>;
28
+ export {};
@@ -0,0 +1,13 @@
1
+ import { RuntimeEvaluationResult } from '@mongosh/browser-runtime-core';
2
+ import type { DevtoolsConnectOptions } from '@mongosh/service-provider-server/lib/cli-service-provider';
3
+ export declare function serializeError(err: Error): Error;
4
+ export declare function deserializeError(err: any): Error;
5
+ export declare enum SerializedResultTypes {
6
+ SerializedErrorResult = "SerializedErrorResult",
7
+ InspectResult = "InspectResult",
8
+ SerializedShellApiResult = "SerializedShellApiResult"
9
+ }
10
+ export declare function serializeEvaluationResult({ type, printable, source }: RuntimeEvaluationResult): RuntimeEvaluationResult;
11
+ export declare function deserializeEvaluationResult({ type, printable, source }: RuntimeEvaluationResult): RuntimeEvaluationResult;
12
+ export declare function serializeConnectOptions(options?: Readonly<DevtoolsConnectOptions>): DevtoolsConnectOptions;
13
+ export declare function deserializeConnectOptions(options: Readonly<DevtoolsConnectOptions>): DevtoolsConnectOptions;
@@ -0,0 +1,4 @@
1
+ /// <reference types="node" />
2
+ import { ChildProcess, SpawnOptions, StdioNull, StdioPipe } from 'child_process';
3
+ export declare function kill(childProcess: ChildProcess, code?: NodeJS.Signals | number): Promise<void>;
4
+ export default function spawnChildFromSource(src: string, spawnOptions?: Omit<SpawnOptions, 'stdio'>, timeoutMs?: number, _stdout?: StdioNull | StdioPipe, _stderr?: StdioNull | StdioPipe): Promise<ChildProcess>;
@@ -0,0 +1,14 @@
1
+ import { Runtime, RuntimeEvaluationListener } from '@mongosh/browser-runtime-core';
2
+ import { CompassServiceProvider } from '@mongosh/service-provider-server';
3
+ import { InterruptHandle } from 'interruptor';
4
+ declare type DevtoolsConnectOptions = Parameters<(typeof CompassServiceProvider)['connect']>[1];
5
+ export declare type WorkerRuntimeEvaluationListener = RuntimeEvaluationListener & {
6
+ onRunInterruptible(handle: InterruptHandle | null): void;
7
+ };
8
+ export declare type WorkerRuntime = Runtime & {
9
+ init(uri: string, driverOptions?: DevtoolsConnectOptions, cliOptions?: {
10
+ nodb?: boolean;
11
+ }): Promise<void>;
12
+ interrupt(): boolean;
13
+ };
14
+ export {};
@@ -1,6 +1,7 @@
1
1
  import { Runtime, RuntimeEvaluationListener } from '@mongosh/browser-runtime-core';
2
- import { DevtoolsConnectOptions } from '@mongosh/service-provider-server';
2
+ import { CompassServiceProvider } from '@mongosh/service-provider-server';
3
3
  import { InterruptHandle } from 'interruptor';
4
+ declare type DevtoolsConnectOptions = Parameters<(typeof CompassServiceProvider)['connect']>[1];
4
5
  export declare type WorkerRuntimeEvaluationListener = RuntimeEvaluationListener & {
5
6
  onRunInterruptible(handle: InterruptHandle | null): void;
6
7
  };
@@ -10,3 +11,4 @@ export declare type WorkerRuntime = Runtime & {
10
11
  }): Promise<void>;
11
12
  interrupt(): boolean;
12
13
  };
14
+ export {};