@loaders.gl/worker-utils 4.2.0-alpha.4 → 4.2.0-alpha.6

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 (81) hide show
  1. package/dist/index.cjs +225 -86
  2. package/dist/index.cjs.map +7 -0
  3. package/dist/index.d.ts +18 -18
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +19 -8
  6. package/dist/lib/async-queue/async-queue.js +83 -68
  7. package/dist/lib/env-utils/assert.js +10 -4
  8. package/dist/lib/env-utils/globals.js +19 -7
  9. package/dist/lib/env-utils/version.js +20 -11
  10. package/dist/lib/library-utils/library-utils.js +148 -73
  11. package/dist/lib/node/require-utils.node.js +72 -43
  12. package/dist/lib/node/worker_threads-browser.js +9 -2
  13. package/dist/lib/node/worker_threads.js +4 -2
  14. package/dist/lib/process-utils/child-process-proxy.d.ts +1 -3
  15. package/dist/lib/process-utils/child-process-proxy.d.ts.map +1 -1
  16. package/dist/lib/process-utils/child-process-proxy.js +103 -93
  17. package/dist/lib/process-utils/process-utils.js +28 -23
  18. package/dist/lib/worker-api/create-worker.d.ts +1 -1
  19. package/dist/lib/worker-api/create-worker.d.ts.map +1 -1
  20. package/dist/lib/worker-api/create-worker.js +77 -74
  21. package/dist/lib/worker-api/get-worker-url.d.ts +1 -1
  22. package/dist/lib/worker-api/get-worker-url.d.ts.map +1 -1
  23. package/dist/lib/worker-api/get-worker-url.js +52 -26
  24. package/dist/lib/worker-api/process-on-worker.d.ts +1 -1
  25. package/dist/lib/worker-api/process-on-worker.d.ts.map +1 -1
  26. package/dist/lib/worker-api/process-on-worker.js +70 -67
  27. package/dist/lib/worker-api/validate-worker-version.d.ts +1 -1
  28. package/dist/lib/worker-api/validate-worker-version.d.ts.map +1 -1
  29. package/dist/lib/worker-api/validate-worker-version.js +29 -14
  30. package/dist/lib/worker-farm/worker-body.d.ts +1 -1
  31. package/dist/lib/worker-farm/worker-body.d.ts.map +1 -1
  32. package/dist/lib/worker-farm/worker-body.js +109 -68
  33. package/dist/lib/worker-farm/worker-farm.d.ts +1 -1
  34. package/dist/lib/worker-farm/worker-farm.d.ts.map +1 -1
  35. package/dist/lib/worker-farm/worker-farm.js +80 -62
  36. package/dist/lib/worker-farm/worker-job.d.ts +2 -2
  37. package/dist/lib/worker-farm/worker-job.d.ts.map +1 -1
  38. package/dist/lib/worker-farm/worker-job.js +48 -32
  39. package/dist/lib/worker-farm/worker-pool.d.ts +3 -3
  40. package/dist/lib/worker-farm/worker-pool.d.ts.map +1 -1
  41. package/dist/lib/worker-farm/worker-pool.js +153 -111
  42. package/dist/lib/worker-farm/worker-thread.d.ts +1 -1
  43. package/dist/lib/worker-farm/worker-thread.d.ts.map +1 -1
  44. package/dist/lib/worker-farm/worker-thread.js +126 -94
  45. package/dist/lib/worker-utils/get-loadable-worker-url.js +54 -24
  46. package/dist/lib/worker-utils/get-transfer-list.js +79 -44
  47. package/dist/lib/worker-utils/remove-nontransferable-options.js +23 -14
  48. package/dist/null-worker-node.js +6 -1
  49. package/dist/null-worker-node.js.map +2 -2
  50. package/dist/null-worker.js +3 -1
  51. package/dist/null-worker.js.map +2 -2
  52. package/dist/types.js +3 -1
  53. package/dist/workers/null-worker.js +6 -3
  54. package/package.json +11 -11
  55. package/src/lib/process-utils/child-process-proxy.ts +1 -1
  56. package/src/lib/worker-farm/worker-body.ts +7 -2
  57. package/dist/index.js.map +0 -1
  58. package/dist/lib/async-queue/async-queue.js.map +0 -1
  59. package/dist/lib/env-utils/assert.js.map +0 -1
  60. package/dist/lib/env-utils/globals.js.map +0 -1
  61. package/dist/lib/env-utils/version.js.map +0 -1
  62. package/dist/lib/library-utils/library-utils.js.map +0 -1
  63. package/dist/lib/node/require-utils.node.js.map +0 -1
  64. package/dist/lib/node/worker_threads-browser.js.map +0 -1
  65. package/dist/lib/node/worker_threads.js.map +0 -1
  66. package/dist/lib/process-utils/child-process-proxy.js.map +0 -1
  67. package/dist/lib/process-utils/process-utils.js.map +0 -1
  68. package/dist/lib/worker-api/create-worker.js.map +0 -1
  69. package/dist/lib/worker-api/get-worker-url.js.map +0 -1
  70. package/dist/lib/worker-api/process-on-worker.js.map +0 -1
  71. package/dist/lib/worker-api/validate-worker-version.js.map +0 -1
  72. package/dist/lib/worker-farm/worker-body.js.map +0 -1
  73. package/dist/lib/worker-farm/worker-farm.js.map +0 -1
  74. package/dist/lib/worker-farm/worker-job.js.map +0 -1
  75. package/dist/lib/worker-farm/worker-pool.js.map +0 -1
  76. package/dist/lib/worker-farm/worker-thread.js.map +0 -1
  77. package/dist/lib/worker-utils/get-loadable-worker-url.js.map +0 -1
  78. package/dist/lib/worker-utils/get-transfer-list.js.map +0 -1
  79. package/dist/lib/worker-utils/remove-nontransferable-options.js.map +0 -1
  80. package/dist/types.js.map +0 -1
  81. package/dist/workers/null-worker.js.map +0 -1
@@ -1,89 +1,164 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ /* global importScripts */
1
5
  import { isBrowser, isWorker } from "../env-utils/globals.js";
2
6
  import * as node from "../node/require-utils.node.js";
3
7
  import { assert } from "../env-utils/assert.js";
4
8
  import { VERSION } from "../env-utils/version.js";
5
- const loadLibraryPromises = {};
6
- export async function loadLibrary(libraryUrl) {
7
- let moduleName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
8
- let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
9
- let libraryName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
10
- if (moduleName) {
11
- libraryUrl = getLibraryUrl(libraryUrl, moduleName, options, libraryName);
12
- }
13
- loadLibraryPromises[libraryUrl] = loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl);
14
- return await loadLibraryPromises[libraryUrl];
9
+ const loadLibraryPromises = {}; // promises
10
+ /**
11
+ * Dynamically loads a library ("module")
12
+ *
13
+ * - wasm library: Array buffer is returned
14
+ * - js library: Parse JS is returned
15
+ *
16
+ * Method depends on environment
17
+ * - browser - script element is created and installed on document
18
+ * - worker - eval is called on global context
19
+ * - node - file is required
20
+ *
21
+ * @param libraryUrl
22
+ * @param moduleName
23
+ * @param options
24
+ */
25
+ export async function loadLibrary(libraryUrl, moduleName = null, options = {}, libraryName = null) {
26
+ if (moduleName) {
27
+ libraryUrl = getLibraryUrl(libraryUrl, moduleName, options, libraryName);
28
+ }
29
+ // Ensure libraries are only loaded once
30
+ loadLibraryPromises[libraryUrl] =
31
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
32
+ loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl);
33
+ return await loadLibraryPromises[libraryUrl];
15
34
  }
16
- export function getLibraryUrl(library, moduleName) {
17
- let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
18
- let libraryName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
19
- if (!options.useLocalLibraries && library.startsWith('http')) {
20
- return library;
21
- }
22
- libraryName = libraryName || library;
23
- const modules = options.modules || {};
24
- if (modules[libraryName]) {
25
- return modules[libraryName];
26
- }
27
- if (!isBrowser) {
28
- return `modules/${moduleName}/dist/libs/${libraryName}`;
29
- }
30
- if (options.CDN) {
31
- assert(options.CDN.startsWith('http'));
32
- return `${options.CDN}/${moduleName}@${VERSION}/dist/libs/${libraryName}`;
33
- }
34
- if (isWorker) {
35
- return `../src/libs/${libraryName}`;
36
- }
37
- return `modules/${moduleName}/src/libs/${libraryName}`;
35
+ // TODO - sort out how to resolve paths for main/worker and dev/prod
36
+ export function getLibraryUrl(library, moduleName, options = {}, libraryName = null) {
37
+ // Check if already a URL
38
+ if (!options.useLocalLibraries && library.startsWith('http')) {
39
+ return library;
40
+ }
41
+ libraryName = libraryName || library;
42
+ // Allow application to import and supply libraries through `options.modules`
43
+ const modules = options.modules || {};
44
+ if (modules[libraryName]) {
45
+ return modules[libraryName];
46
+ }
47
+ // Load from local files, not from CDN scripts in Node.js
48
+ // TODO - needs to locate the modules directory when installed!
49
+ if (!isBrowser) {
50
+ return `modules/${moduleName}/dist/libs/${libraryName}`;
51
+ }
52
+ // In browser, load from external scripts
53
+ if (options.CDN) {
54
+ assert(options.CDN.startsWith('http'));
55
+ return `${options.CDN}/${moduleName}@${VERSION}/dist/libs/${libraryName}`;
56
+ }
57
+ // TODO - loading inside workers requires paths relative to worker script location...
58
+ if (isWorker) {
59
+ return `../src/libs/${libraryName}`;
60
+ }
61
+ return `modules/${moduleName}/src/libs/${libraryName}`;
38
62
  }
39
63
  async function loadLibraryFromFile(libraryUrl) {
40
- if (libraryUrl.endsWith('wasm')) {
41
- return await loadAsArrayBuffer(libraryUrl);
42
- }
43
- if (!isBrowser) {
44
- try {
45
- return node && node.requireFromFile && (await node.requireFromFile(libraryUrl));
46
- } catch (error) {
47
- console.error(error);
48
- return null;
64
+ if (libraryUrl.endsWith('wasm')) {
65
+ return await loadAsArrayBuffer(libraryUrl);
66
+ }
67
+ if (!isBrowser) {
68
+ // TODO - Node doesn't yet support dynamic import from https URLs
69
+ // try {
70
+ // return await import(libraryUrl);
71
+ // } catch (error) {
72
+ // console.error(error);
73
+ // }
74
+ try {
75
+ return node && node.requireFromFile && (await node.requireFromFile(libraryUrl));
76
+ }
77
+ catch (error) {
78
+ console.error(error); // eslint-disable-line no-console
79
+ return null;
80
+ }
49
81
  }
50
- }
51
- if (isWorker) {
52
- return importScripts(libraryUrl);
53
- }
54
- const scriptSource = await loadAsText(libraryUrl);
55
- return loadLibraryFromString(scriptSource, libraryUrl);
82
+ if (isWorker) {
83
+ return importScripts(libraryUrl);
84
+ }
85
+ // TODO - fix - should be more secure than string parsing since observes CORS
86
+ // if (isBrowser) {
87
+ // return await loadScriptFromFile(libraryUrl);
88
+ // }
89
+ const scriptSource = await loadAsText(libraryUrl);
90
+ return loadLibraryFromString(scriptSource, libraryUrl);
91
+ }
92
+ /*
93
+ async function loadScriptFromFile(libraryUrl) {
94
+ const script = document.createElement('script');
95
+ script.src = libraryUrl;
96
+ return await new Promise((resolve, reject) => {
97
+ script.onload = data => {
98
+ resolve(data);
99
+ };
100
+ script.onerror = reject;
101
+ });
56
102
  }
103
+ */
104
+ // TODO - Needs security audit...
105
+ // - Raw eval call
106
+ // - Potentially bypasses CORS
107
+ // Upside is that this separates fetching and parsing
108
+ // we could create a`LibraryLoader` or`ModuleLoader`
57
109
  function loadLibraryFromString(scriptSource, id) {
58
- if (!isBrowser) {
59
- return node.requireFromString && node.requireFromString(scriptSource, id);
60
- }
61
- if (isWorker) {
62
- eval.call(globalThis, scriptSource);
110
+ if (!isBrowser) {
111
+ return node.requireFromString && node.requireFromString(scriptSource, id);
112
+ }
113
+ if (isWorker) {
114
+ // Use lvalue trick to make eval run in global scope
115
+ eval.call(globalThis, scriptSource); // eslint-disable-line no-eval
116
+ // https://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript
117
+ // http://perfectionkills.com/global-eval-what-are-the-options/
118
+ return null;
119
+ }
120
+ const script = document.createElement('script');
121
+ script.id = id;
122
+ // most browsers like a separate text node but some throw an error. The second method covers those.
123
+ try {
124
+ script.appendChild(document.createTextNode(scriptSource));
125
+ }
126
+ catch (e) {
127
+ script.text = scriptSource;
128
+ }
129
+ document.body.appendChild(script);
63
130
  return null;
64
- }
65
- const script = document.createElement('script');
66
- script.id = id;
67
- try {
68
- script.appendChild(document.createTextNode(scriptSource));
69
- } catch (e) {
70
- script.text = scriptSource;
71
- }
72
- document.body.appendChild(script);
73
- return null;
74
131
  }
132
+ // TODO - technique for module injection into worker, from THREE.DracoLoader...
133
+ /*
134
+ function combineWorkerWithLibrary(worker, jsContent) {
135
+ var fn = wWorker.toString();
136
+ var body = [
137
+ '// injected',
138
+ jsContent,
139
+ '',
140
+ '// worker',
141
+ fn.substring(fn.indexOf('{') + 1, fn.lastIndexOf('}'))
142
+ ].join('\n');
143
+ this.workerSourceURL = URL.createObjectURL(new Blob([body]));
144
+ }
145
+ */
75
146
  async function loadAsArrayBuffer(url) {
76
- if (isBrowser || !node.readFileAsArrayBuffer || url.startsWith('http')) {
77
- const response = await fetch(url);
78
- return await response.arrayBuffer();
79
- }
80
- return await node.readFileAsArrayBuffer(url);
147
+ if (isBrowser || !node.readFileAsArrayBuffer || url.startsWith('http')) {
148
+ const response = await fetch(url);
149
+ return await response.arrayBuffer();
150
+ }
151
+ return await node.readFileAsArrayBuffer(url);
81
152
  }
153
+ /**
154
+ * Load a file from local file system
155
+ * @param filename
156
+ * @returns
157
+ */
82
158
  async function loadAsText(url) {
83
- if (isBrowser || !node.readFileAsText || url.startsWith('http')) {
84
- const response = await fetch(url);
85
- return await response.text();
86
- }
87
- return await node.readFileAsText(url);
159
+ if (isBrowser || !node.readFileAsText || url.startsWith('http')) {
160
+ const response = await fetch(url);
161
+ return await response.text();
162
+ }
163
+ return await node.readFileAsText(url);
88
164
  }
89
- //# sourceMappingURL=library-utils.js.map
@@ -1,54 +1,83 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // Fork of https://github.com/floatdrop/require-from-string/blob/master/index.js
5
+ // Copyright (c) Vsevolod Strukchinsky <floatdrop@gmail.com> (github.com/floatdrop)
6
+ // MIT license
7
+ // this file is not visible to webpack (it is excluded in the package.json "browser" field).
1
8
  import Module from 'module';
2
9
  import * as path from 'path';
3
10
  import * as fs from 'fs';
11
+ /**
12
+ * Load a file from local file system
13
+ * @param filename
14
+ * @returns
15
+ */
4
16
  export async function readFileAsArrayBuffer(filename) {
5
- if (filename.startsWith('http')) {
6
- const response = await fetch(filename);
7
- return await response.arrayBuffer();
8
- }
9
- const buffer = fs.readFileSync(filename);
10
- return buffer.buffer;
17
+ if (filename.startsWith('http')) {
18
+ const response = await fetch(filename);
19
+ return await response.arrayBuffer();
20
+ }
21
+ const buffer = fs.readFileSync(filename);
22
+ return buffer.buffer;
11
23
  }
24
+ /**
25
+ * Load a file from local file system
26
+ * @param filename
27
+ * @returns
28
+ */
12
29
  export async function readFileAsText(filename) {
13
- if (filename.startsWith('http')) {
14
- const response = await fetch(filename);
15
- return await response.text();
16
- }
17
- const text = fs.readFileSync(filename, 'utf8');
18
- return text;
30
+ if (filename.startsWith('http')) {
31
+ const response = await fetch(filename);
32
+ return await response.text();
33
+ }
34
+ const text = fs.readFileSync(filename, 'utf8');
35
+ return text;
19
36
  }
37
+ // Node.js Dynamically require from file
38
+ // Relative names are resolved relative to cwd
39
+ // This indirect function is provided because webpack will try to bundle `module.require`.
40
+ // this file is not visible to webpack (it is excluded in the package.json "browser" field).
20
41
  export async function requireFromFile(filename) {
21
- if (filename.startsWith('http')) {
22
- const response = await fetch(filename);
23
- const code = await response.text();
42
+ if (filename.startsWith('http')) {
43
+ const response = await fetch(filename);
44
+ const code = await response.text();
45
+ return requireFromString(code);
46
+ }
47
+ if (!filename.startsWith('/')) {
48
+ filename = `${process.cwd()}/${filename}`;
49
+ }
50
+ const code = await fs.promises.readFile(filename, 'utf8');
24
51
  return requireFromString(code);
25
- }
26
- if (!filename.startsWith('/')) {
27
- filename = `${process.cwd()}/${filename}`;
28
- }
29
- const code = await fs.promises.readFile(filename, 'utf8');
30
- return requireFromString(code);
31
52
  }
32
- export function requireFromString(code) {
33
- var _module, _options, _options2;
34
- let filename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
35
- let options = arguments.length > 2 ? arguments[2] : undefined;
36
- if (typeof filename === 'object') {
37
- options = filename;
38
- filename = '';
39
- }
40
- if (typeof code !== 'string') {
41
- throw new Error(`code must be a string, not ${typeof code}`);
42
- }
43
- const paths = Module._nodeModulePaths(path.dirname(filename));
44
- const parent = typeof module !== 'undefined' && ((_module = module) === null || _module === void 0 ? void 0 : _module.parent);
45
- const newModule = new Module(filename, parent);
46
- newModule.filename = filename;
47
- newModule.paths = [].concat(((_options = options) === null || _options === void 0 ? void 0 : _options.prependPaths) || []).concat(paths).concat(((_options2 = options) === null || _options2 === void 0 ? void 0 : _options2.appendPaths) || []);
48
- newModule._compile(code, filename);
49
- if (parent && parent.children) {
50
- parent.children.splice(parent.children.indexOf(newModule), 1);
51
- }
52
- return newModule.exports;
53
+ // Dynamically require from string
54
+ // - `code` - Required - Type: string - Module code.
55
+ // - `filename` - Type: string - Default: '' - Optional filename.
56
+ // - `options.appendPaths` Type: Array List of paths, that will be appended to module paths.
57
+ // Useful, when you want to be able require modules from these paths.
58
+ // - `options.prependPaths` Type: Array Same as appendPaths, but paths will be prepended.
59
+ export function requireFromString(code, filename = '', options) {
60
+ if (typeof filename === 'object') {
61
+ options = filename;
62
+ filename = '';
63
+ }
64
+ if (typeof code !== 'string') {
65
+ throw new Error(`code must be a string, not ${typeof code}`);
66
+ }
67
+ // @ts-ignore
68
+ const paths = Module._nodeModulePaths(path.dirname(filename));
69
+ const parent = typeof module !== 'undefined' && module?.parent;
70
+ // @ts-ignore
71
+ const newModule = new Module(filename, parent);
72
+ newModule.filename = filename;
73
+ newModule.paths = []
74
+ .concat(options?.prependPaths || [])
75
+ .concat(paths)
76
+ .concat(options?.appendPaths || []);
77
+ // @ts-ignore
78
+ newModule._compile(code, filename);
79
+ if (parent && parent.children) {
80
+ parent.children.splice(parent.children.indexOf(newModule), 1);
81
+ }
82
+ return newModule.exports;
53
83
  }
54
- //# sourceMappingURL=require-utils.node.js.map
@@ -1,5 +1,12 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ /** Browser polyfill for Node.js built-in `worker_threads` module.
5
+ * These fills are non-functional, and just intended to ensure that
6
+ * `import 'worker_threads` doesn't break browser builds.
7
+ * The replacement is done in package.json browser field
8
+ */
1
9
  export class NodeWorker {
2
- terminate() {}
10
+ terminate() { }
3
11
  }
4
12
  export const parentPort = null;
5
- //# sourceMappingURL=worker_threads-browser.js.map
@@ -1,5 +1,7 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import * as WorkerThreads from 'worker_threads';
2
5
  export * from 'worker_threads';
3
- export const parentPort = WorkerThreads === null || WorkerThreads === void 0 ? void 0 : WorkerThreads.parentPort;
6
+ export const parentPort = WorkerThreads?.parentPort;
4
7
  export const NodeWorker = WorkerThreads.Worker;
5
- //# sourceMappingURL=worker_threads.js.map
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
- import * as ChildProcess from 'child_process';
3
1
  export type ChildProcessProxyProps = {
4
2
  command: string;
5
3
  arguments: string[];
@@ -13,7 +11,7 @@ export type ChildProcessProxyProps = {
13
11
  /** wait: 0 - infinity */
14
12
  wait?: number;
15
13
  /** Options passed on to Node'.js `spawn` */
16
- spawn?: ChildProcess.SpawnOptionsWithoutStdio;
14
+ spawn?: any;
17
15
  /** Should proceed if stderr stream recieved data */
18
16
  ignoreStderr?: boolean;
19
17
  /** Callback when the */
@@ -1 +1 @@
1
- {"version":3,"file":"child-process-proxy.d.ts","sourceRoot":"","sources":["../../../src/lib/process-utils/child-process-proxy.ts"],"names":[],"mappings":";AAQA,OAAO,KAAK,YAAY,MAAM,eAAe,CAAC;AAG9C,MAAM,MAAM,sBAAsB,GAAG;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,0CAA0C;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uBAAuB;IACvB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,8DAA8D;IAC9D,yBAAyB;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,4CAA4C;IAC5C,KAAK,CAAC,EAAE,YAAY,CAAC,wBAAwB,CAAC;IAC9C,oDAAoD;IACpD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,yBAAyB;IACzB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;IAC7C,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;CAChD,CAAC;AAaF;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,sBAAsB,CAAsB;IACnD,OAAO,CAAC,YAAY,CAA0C;IAC9D,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,YAAY,CAAC,CAAM;gBAGf,EAAC,EAAqB,EAAC;;KAAK;IAIxC,qDAAqD;IAC/C,KAAK,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,MAAM,CAAC;IAuD3D,oCAAoC;IAC9B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAO3B,yBAAyB;IACnB,IAAI,CAAC,UAAU,GAAE,MAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAYjD,WAAW,CAAC,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI;IAM9C,aAAa;CAKd"}
1
+ {"version":3,"file":"child-process-proxy.d.ts","sourceRoot":"","sources":["../../../src/lib/process-utils/child-process-proxy.ts"],"names":[],"mappings":"AAWA,MAAM,MAAM,sBAAsB,GAAG;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,0CAA0C;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uBAAuB;IACvB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,8DAA8D;IAC9D,yBAAyB;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,4CAA4C;IAC5C,KAAK,CAAC,EAAE,GAAG,CAAC;IACZ,oDAAoD;IACpD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,yBAAyB;IACzB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;IAC7C,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;CAChD,CAAC;AAaF;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,sBAAsB,CAAsB;IACnD,OAAO,CAAC,YAAY,CAA0C;IAC9D,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,YAAY,CAAC,CAAM;gBAGf,EAAC,EAAqB,EAAC;;KAAK;IAIxC,qDAAqD;IAC/C,KAAK,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,MAAM,CAAC;IAuD3D,oCAAoC;IAC9B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAO3B,yBAAyB;IACnB,IAAI,CAAC,UAAU,GAAE,MAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAYjD,WAAW,CAAC,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI;IAM9C,aAAa;CAKd"}
@@ -1,105 +1,115 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ /* eslint-disable no-console */
5
+ // Avoid using named imports for Node builtins to help with "empty" resolution
6
+ // for bundlers targeting browser environments. Access imports & types
7
+ // through the `ChildProcess` object (e.g. `ChildProcess.spawn`, `ChildProcess.ChildProcess`).
1
8
  import * as ChildProcess from 'child_process';
2
9
  import { getAvailablePort } from "./process-utils.js";
3
10
  const DEFAULT_PROPS = {
4
- command: '',
5
- arguments: [],
6
- port: 5000,
7
- autoPort: true,
8
- wait: 2000,
9
- onSuccess: processProxy => {
10
- console.log(`Started ${processProxy.props.command}`);
11
- }
11
+ command: '',
12
+ arguments: [],
13
+ port: 5000,
14
+ autoPort: true,
15
+ wait: 2000,
16
+ onSuccess: (processProxy) => {
17
+ console.log(`Started ${processProxy.props.command}`);
18
+ }
12
19
  };
20
+ /**
21
+ * Manager for a Node.js child process
22
+ * Prepares arguments, starts, stops and tracks output
23
+ */
13
24
  export default class ChildProcessProxy {
14
- constructor() {
15
- let {
16
- id = 'browser-driver'
17
- } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
18
- this.id = void 0;
19
- this.props = {
20
- ...DEFAULT_PROPS
21
- };
22
- this.childProcess = null;
23
- this.port = 0;
24
- this.successTimer = void 0;
25
- this.id = id;
26
- }
27
- async start(props) {
28
- props = {
29
- ...DEFAULT_PROPS,
30
- ...props
31
- };
32
- this.props = props;
33
- const args = [...props.arguments];
34
- this.port = Number(props.port);
35
- if (props.portArg) {
36
- if (props.autoPort) {
37
- this.port = await getAvailablePort(props.port);
38
- }
39
- args.push(props.portArg, String(this.port));
25
+ id;
26
+ props = { ...DEFAULT_PROPS };
27
+ childProcess = null;
28
+ port = 0;
29
+ successTimer; // NodeJS.Timeout;
30
+ // constructor(props?: {id?: string});
31
+ constructor({ id = 'browser-driver' } = {}) {
32
+ this.id = id;
40
33
  }
41
- return await new Promise((resolve, reject) => {
42
- try {
43
- this._setTimeout(() => {
44
- if (props.onSuccess) {
45
- props.onSuccess(this);
46
- }
47
- resolve({});
48
- });
49
- console.log(`Spawning ${props.command} ${props.arguments.join(' ')}`);
50
- const childProcess = ChildProcess.spawn(props.command, args, props.spawn);
51
- this.childProcess = childProcess;
52
- childProcess.stdout.on('data', data => {
53
- console.log(data.toString());
54
- });
55
- childProcess.stderr.on('data', data => {
56
- console.log(`Child process wrote to stderr: "${data}".`);
57
- if (!props.ignoreStderr) {
58
- this._clearTimeout();
59
- reject(new Error(data));
60
- }
34
+ /** Starts a child process with the provided props */
35
+ async start(props) {
36
+ props = { ...DEFAULT_PROPS, ...props };
37
+ this.props = props;
38
+ const args = [...props.arguments];
39
+ // If portArg is set, we can look up an available port
40
+ this.port = Number(props.port);
41
+ if (props.portArg) {
42
+ if (props.autoPort) {
43
+ this.port = await getAvailablePort(props.port);
44
+ }
45
+ args.push(props.portArg, String(this.port));
46
+ }
47
+ return await new Promise((resolve, reject) => {
48
+ try {
49
+ this._setTimeout(() => {
50
+ if (props.onSuccess) {
51
+ props.onSuccess(this);
52
+ }
53
+ resolve({});
54
+ });
55
+ console.log(`Spawning ${props.command} ${props.arguments.join(' ')}`);
56
+ const childProcess = ChildProcess.spawn(props.command, args, props.spawn);
57
+ this.childProcess = childProcess;
58
+ childProcess.stdout.on('data', (data) => {
59
+ console.log(data.toString());
60
+ });
61
+ childProcess.stderr.on('data', (data) => {
62
+ console.log(`Child process wrote to stderr: "${data}".`);
63
+ if (!props.ignoreStderr) {
64
+ this._clearTimeout();
65
+ reject(new Error(data));
66
+ }
67
+ });
68
+ childProcess.on('error', (error) => {
69
+ console.log(`Child process errored with ${error}`);
70
+ this._clearTimeout();
71
+ reject(error);
72
+ });
73
+ childProcess.on('close', (code) => {
74
+ console.log(`Child process exited with ${code}`);
75
+ this.childProcess = null;
76
+ this._clearTimeout();
77
+ resolve({});
78
+ });
79
+ }
80
+ catch (error) {
81
+ reject(error);
82
+ }
61
83
  });
62
- childProcess.on('error', error => {
63
- console.log(`Child process errored with ${error}`);
64
- this._clearTimeout();
65
- reject(error);
66
- });
67
- childProcess.on('close', code => {
68
- console.log(`Child process exited with ${code}`);
69
- this.childProcess = null;
70
- this._clearTimeout();
71
- resolve({});
72
- });
73
- } catch (error) {
74
- reject(error);
75
- }
76
- });
77
- }
78
- async stop() {
79
- if (this.childProcess) {
80
- this.childProcess.kill();
81
- this.childProcess = null;
82
84
  }
83
- }
84
- async exit() {
85
- let statusCode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
86
- try {
87
- await this.stop();
88
- process.exit(statusCode);
89
- } catch (error) {
90
- console.error(error.message || error);
91
- process.exit(1);
85
+ /** Stops a running child process */
86
+ async stop() {
87
+ if (this.childProcess) {
88
+ this.childProcess.kill();
89
+ this.childProcess = null;
90
+ }
91
+ }
92
+ /** Exits this process */
93
+ async exit(statusCode = 0) {
94
+ try {
95
+ await this.stop();
96
+ // eslint-disable-next-line no-process-exit
97
+ process.exit(statusCode);
98
+ }
99
+ catch (error) {
100
+ console.error(error.message || error);
101
+ // eslint-disable-next-line no-process-exit
102
+ process.exit(1);
103
+ }
92
104
  }
93
- }
94
- _setTimeout(callback) {
95
- if (Number(this.props.wait) > 0) {
96
- this.successTimer = setTimeout(callback, this.props.wait);
105
+ _setTimeout(callback) {
106
+ if (Number(this.props.wait) > 0) {
107
+ this.successTimer = setTimeout(callback, this.props.wait);
108
+ }
97
109
  }
98
- }
99
- _clearTimeout() {
100
- if (this.successTimer) {
101
- clearTimeout(this.successTimer);
110
+ _clearTimeout() {
111
+ if (this.successTimer) {
112
+ clearTimeout(this.successTimer);
113
+ }
102
114
  }
103
- }
104
115
  }
105
- //# sourceMappingURL=child-process-proxy.js.map