@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.
- package/dist/index.cjs +225 -86
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +18 -18
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -8
- package/dist/lib/async-queue/async-queue.js +83 -68
- package/dist/lib/env-utils/assert.js +10 -4
- package/dist/lib/env-utils/globals.js +19 -7
- package/dist/lib/env-utils/version.js +20 -11
- package/dist/lib/library-utils/library-utils.js +148 -73
- package/dist/lib/node/require-utils.node.js +72 -43
- package/dist/lib/node/worker_threads-browser.js +9 -2
- package/dist/lib/node/worker_threads.js +4 -2
- package/dist/lib/process-utils/child-process-proxy.d.ts +1 -3
- package/dist/lib/process-utils/child-process-proxy.d.ts.map +1 -1
- package/dist/lib/process-utils/child-process-proxy.js +103 -93
- package/dist/lib/process-utils/process-utils.js +28 -23
- package/dist/lib/worker-api/create-worker.d.ts +1 -1
- package/dist/lib/worker-api/create-worker.d.ts.map +1 -1
- package/dist/lib/worker-api/create-worker.js +77 -74
- package/dist/lib/worker-api/get-worker-url.d.ts +1 -1
- package/dist/lib/worker-api/get-worker-url.d.ts.map +1 -1
- package/dist/lib/worker-api/get-worker-url.js +52 -26
- package/dist/lib/worker-api/process-on-worker.d.ts +1 -1
- package/dist/lib/worker-api/process-on-worker.d.ts.map +1 -1
- package/dist/lib/worker-api/process-on-worker.js +70 -67
- package/dist/lib/worker-api/validate-worker-version.d.ts +1 -1
- package/dist/lib/worker-api/validate-worker-version.d.ts.map +1 -1
- package/dist/lib/worker-api/validate-worker-version.js +29 -14
- package/dist/lib/worker-farm/worker-body.d.ts +1 -1
- package/dist/lib/worker-farm/worker-body.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-body.js +109 -68
- package/dist/lib/worker-farm/worker-farm.d.ts +1 -1
- package/dist/lib/worker-farm/worker-farm.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-farm.js +80 -62
- package/dist/lib/worker-farm/worker-job.d.ts +2 -2
- package/dist/lib/worker-farm/worker-job.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-job.js +48 -32
- package/dist/lib/worker-farm/worker-pool.d.ts +3 -3
- package/dist/lib/worker-farm/worker-pool.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-pool.js +153 -111
- package/dist/lib/worker-farm/worker-thread.d.ts +1 -1
- package/dist/lib/worker-farm/worker-thread.d.ts.map +1 -1
- package/dist/lib/worker-farm/worker-thread.js +126 -94
- package/dist/lib/worker-utils/get-loadable-worker-url.js +54 -24
- package/dist/lib/worker-utils/get-transfer-list.js +79 -44
- package/dist/lib/worker-utils/remove-nontransferable-options.js +23 -14
- package/dist/null-worker-node.js +6 -1
- package/dist/null-worker-node.js.map +2 -2
- package/dist/null-worker.js +3 -1
- package/dist/null-worker.js.map +2 -2
- package/dist/types.js +3 -1
- package/dist/workers/null-worker.js +6 -3
- package/package.json +11 -11
- package/src/lib/process-utils/child-process-proxy.ts +1 -1
- package/src/lib/worker-farm/worker-body.ts +7 -2
- package/dist/index.js.map +0 -1
- package/dist/lib/async-queue/async-queue.js.map +0 -1
- package/dist/lib/env-utils/assert.js.map +0 -1
- package/dist/lib/env-utils/globals.js.map +0 -1
- package/dist/lib/env-utils/version.js.map +0 -1
- package/dist/lib/library-utils/library-utils.js.map +0 -1
- package/dist/lib/node/require-utils.node.js.map +0 -1
- package/dist/lib/node/worker_threads-browser.js.map +0 -1
- package/dist/lib/node/worker_threads.js.map +0 -1
- package/dist/lib/process-utils/child-process-proxy.js.map +0 -1
- package/dist/lib/process-utils/process-utils.js.map +0 -1
- package/dist/lib/worker-api/create-worker.js.map +0 -1
- package/dist/lib/worker-api/get-worker-url.js.map +0 -1
- package/dist/lib/worker-api/process-on-worker.js.map +0 -1
- package/dist/lib/worker-api/validate-worker-version.js.map +0 -1
- package/dist/lib/worker-farm/worker-body.js.map +0 -1
- package/dist/lib/worker-farm/worker-farm.js.map +0 -1
- package/dist/lib/worker-farm/worker-job.js.map +0 -1
- package/dist/lib/worker-farm/worker-pool.js.map +0 -1
- package/dist/lib/worker-farm/worker-thread.js.map +0 -1
- package/dist/lib/worker-utils/get-loadable-worker-url.js.map +0 -1
- package/dist/lib/worker-utils/get-transfer-list.js.map +0 -1
- package/dist/lib/worker-utils/remove-nontransferable-options.js.map +0 -1
- package/dist/types.js.map +0 -1
- 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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
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
|
|
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?:
|
|
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":"
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
105
|
+
_setTimeout(callback) {
|
|
106
|
+
if (Number(this.props.wait) > 0) {
|
|
107
|
+
this.successTimer = setTimeout(callback, this.props.wait);
|
|
108
|
+
}
|
|
97
109
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
110
|
+
_clearTimeout() {
|
|
111
|
+
if (this.successTimer) {
|
|
112
|
+
clearTimeout(this.successTimer);
|
|
113
|
+
}
|
|
102
114
|
}
|
|
103
|
-
}
|
|
104
115
|
}
|
|
105
|
-
//# sourceMappingURL=child-process-proxy.js.map
|