@loaders.gl/worker-utils 4.2.0-alpha.3 → 4.2.0-alpha.5
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 +172 -63
- 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 +80 -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.js +101 -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 +106 -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 +77 -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 +44 -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 +151 -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 +120 -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/types.js +3 -1
- package/dist/workers/null-worker.js +6 -3
- package/package.json +4 -4
- 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,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,105 +1,113 @@
|
|
|
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
|
-
...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
|
+
// constructor(props?: {id?: string});
|
|
26
|
+
constructor({ id = 'browser-driver' } = {}) {
|
|
27
|
+
this.props = { ...DEFAULT_PROPS };
|
|
28
|
+
this.childProcess = null;
|
|
29
|
+
this.port = 0;
|
|
30
|
+
this.id = id;
|
|
40
31
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
32
|
+
/** Starts a child process with the provided props */
|
|
33
|
+
async start(props) {
|
|
34
|
+
props = { ...DEFAULT_PROPS, ...props };
|
|
35
|
+
this.props = props;
|
|
36
|
+
const args = [...props.arguments];
|
|
37
|
+
// If portArg is set, we can look up an available port
|
|
38
|
+
this.port = Number(props.port);
|
|
39
|
+
if (props.portArg) {
|
|
40
|
+
if (props.autoPort) {
|
|
41
|
+
this.port = await getAvailablePort(props.port);
|
|
42
|
+
}
|
|
43
|
+
args.push(props.portArg, String(this.port));
|
|
44
|
+
}
|
|
45
|
+
return await new Promise((resolve, reject) => {
|
|
46
|
+
try {
|
|
47
|
+
this._setTimeout(() => {
|
|
48
|
+
if (props.onSuccess) {
|
|
49
|
+
props.onSuccess(this);
|
|
50
|
+
}
|
|
51
|
+
resolve({});
|
|
52
|
+
});
|
|
53
|
+
console.log(`Spawning ${props.command} ${props.arguments.join(' ')}`);
|
|
54
|
+
const childProcess = ChildProcess.spawn(props.command, args, props.spawn);
|
|
55
|
+
this.childProcess = childProcess;
|
|
56
|
+
childProcess.stdout.on('data', (data) => {
|
|
57
|
+
console.log(data.toString());
|
|
58
|
+
});
|
|
59
|
+
childProcess.stderr.on('data', (data) => {
|
|
60
|
+
console.log(`Child process wrote to stderr: "${data}".`);
|
|
61
|
+
if (!props.ignoreStderr) {
|
|
62
|
+
this._clearTimeout();
|
|
63
|
+
reject(new Error(data));
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
childProcess.on('error', (error) => {
|
|
67
|
+
console.log(`Child process errored with ${error}`);
|
|
68
|
+
this._clearTimeout();
|
|
69
|
+
reject(error);
|
|
70
|
+
});
|
|
71
|
+
childProcess.on('close', (code) => {
|
|
72
|
+
console.log(`Child process exited with ${code}`);
|
|
73
|
+
this.childProcess = null;
|
|
74
|
+
this._clearTimeout();
|
|
75
|
+
resolve({});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
reject(error);
|
|
80
|
+
}
|
|
61
81
|
});
|
|
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
82
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
83
|
+
/** Stops a running child process */
|
|
84
|
+
async stop() {
|
|
85
|
+
if (this.childProcess) {
|
|
86
|
+
this.childProcess.kill();
|
|
87
|
+
this.childProcess = null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Exits this process */
|
|
91
|
+
async exit(statusCode = 0) {
|
|
92
|
+
try {
|
|
93
|
+
await this.stop();
|
|
94
|
+
// eslint-disable-next-line no-process-exit
|
|
95
|
+
process.exit(statusCode);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
console.error(error.message || error);
|
|
99
|
+
// eslint-disable-next-line no-process-exit
|
|
100
|
+
process.exit(1);
|
|
101
|
+
}
|
|
92
102
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
103
|
+
_setTimeout(callback) {
|
|
104
|
+
if (Number(this.props.wait) > 0) {
|
|
105
|
+
this.successTimer = setTimeout(callback, this.props.wait);
|
|
106
|
+
}
|
|
97
107
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
108
|
+
_clearTimeout() {
|
|
109
|
+
if (this.successTimer) {
|
|
110
|
+
clearTimeout(this.successTimer);
|
|
111
|
+
}
|
|
102
112
|
}
|
|
103
|
-
}
|
|
104
113
|
}
|
|
105
|
-
//# sourceMappingURL=child-process-proxy.js.map
|
|
@@ -1,26 +1,31 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import ChildProcess from 'child_process';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
5
|
+
// Get an available port
|
|
6
|
+
// Works on Unix systems
|
|
7
|
+
export function getAvailablePort(defaultPort = 3000) {
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
// Get a list of all ports in use
|
|
10
|
+
ChildProcess.exec('lsof -i -P -n | grep LISTEN', (error, stdout) => {
|
|
11
|
+
if (error) {
|
|
12
|
+
// likely no permission, e.g. CI
|
|
13
|
+
resolve(defaultPort);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const portsInUse = [];
|
|
17
|
+
const regex = /:(\d+) \(LISTEN\)/;
|
|
18
|
+
stdout.split('\n').forEach((line) => {
|
|
19
|
+
const match = regex.exec(line);
|
|
20
|
+
if (match) {
|
|
21
|
+
portsInUse.push(Number(match[1]));
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
let port = defaultPort;
|
|
25
|
+
while (portsInUse.includes(port)) {
|
|
26
|
+
port++;
|
|
27
|
+
}
|
|
28
|
+
resolve(port);
|
|
29
|
+
});
|
|
23
30
|
});
|
|
24
|
-
});
|
|
25
31
|
}
|
|
26
|
-
//# sourceMappingURL=process-utils.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { WorkerContext, Process, ProcessInBatches } from
|
|
1
|
+
import type { WorkerContext, Process, ProcessInBatches } from "../../types.js";
|
|
2
2
|
export type ProcessOnMainThread = (data: any, options?: {
|
|
3
3
|
[key: string]: any;
|
|
4
4
|
}, context?: WorkerContext) => any;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-worker.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-api/create-worker.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAGV,aAAa,EACb,OAAO,EACP,gBAAgB,EACjB,
|
|
1
|
+
{"version":3,"file":"create-worker.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-api/create-worker.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAGV,aAAa,EACb,OAAO,EACP,gBAAgB,EACjB,uBAAoB;AAUrB,MAAM,MAAM,mBAAmB,GAAG,CAChC,IAAI,EAAE,GAAG,EACT,OAAO,CAAC,EAAE;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAC,EAC9B,OAAO,CAAC,EAAE,aAAa,KACpB,GAAG,CAAC;AAET;;GAEG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE,OAAO,EAChB,gBAAgB,CAAC,EAAE,gBAAgB,GAClC,OAAO,CAAC,IAAI,CAAC,CAiDf"}
|
|
@@ -1,84 +1,87 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import AsyncQueue from "../async-queue/async-queue.js";
|
|
2
5
|
import WorkerBody from "../worker-farm/worker-body.js";
|
|
6
|
+
// import {validateWorkerVersion} from './validate-worker-version';
|
|
7
|
+
/** Counter for jobs */
|
|
3
8
|
let requestId = 0;
|
|
4
9
|
let inputBatches;
|
|
5
10
|
let options;
|
|
11
|
+
/**
|
|
12
|
+
* Set up a WebWorkerGlobalScope to talk with the main thread
|
|
13
|
+
*/
|
|
6
14
|
export async function createWorker(process, processInBatches) {
|
|
7
|
-
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
|
-
const context = {
|
|
11
|
-
process: processOnMainThread
|
|
12
|
-
};
|
|
13
|
-
WorkerBody.onmessage = async (type, payload) => {
|
|
14
|
-
try {
|
|
15
|
-
switch (type) {
|
|
16
|
-
case 'process':
|
|
17
|
-
if (!process) {
|
|
18
|
-
throw new Error('Worker does not support atomic processing');
|
|
19
|
-
}
|
|
20
|
-
const result = await process(payload.input, payload.options || {}, context);
|
|
21
|
-
WorkerBody.postMessage('done', {
|
|
22
|
-
result
|
|
23
|
-
});
|
|
24
|
-
break;
|
|
25
|
-
case 'process-in-batches':
|
|
26
|
-
if (!processInBatches) {
|
|
27
|
-
throw new Error('Worker does not support batched processing');
|
|
28
|
-
}
|
|
29
|
-
inputBatches = new AsyncQueue();
|
|
30
|
-
options = payload.options || {};
|
|
31
|
-
const resultIterator = processInBatches(inputBatches, options, context);
|
|
32
|
-
for await (const batch of resultIterator) {
|
|
33
|
-
WorkerBody.postMessage('output-batch', {
|
|
34
|
-
result: batch
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
WorkerBody.postMessage('done', {});
|
|
38
|
-
break;
|
|
39
|
-
case 'input-batch':
|
|
40
|
-
inputBatches.push(payload.input);
|
|
41
|
-
break;
|
|
42
|
-
case 'input-done':
|
|
43
|
-
inputBatches.close();
|
|
44
|
-
break;
|
|
45
|
-
default:
|
|
46
|
-
}
|
|
47
|
-
} catch (error) {
|
|
48
|
-
const message = error instanceof Error ? error.message : '';
|
|
49
|
-
WorkerBody.postMessage('error', {
|
|
50
|
-
error: message
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
function processOnMainThread(arrayBuffer) {
|
|
56
|
-
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
57
|
-
return new Promise((resolve, reject) => {
|
|
58
|
-
const id = requestId++;
|
|
59
|
-
const onMessage = (type, payload) => {
|
|
60
|
-
if (payload.id !== id) {
|
|
15
|
+
if (!(await WorkerBody.inWorkerThread())) {
|
|
61
16
|
return;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
WorkerBody.removeEventListener(onMessage);
|
|
66
|
-
resolve(payload.result);
|
|
67
|
-
break;
|
|
68
|
-
case 'error':
|
|
69
|
-
WorkerBody.removeEventListener(onMessage);
|
|
70
|
-
reject(payload.error);
|
|
71
|
-
break;
|
|
72
|
-
default:
|
|
73
|
-
}
|
|
17
|
+
}
|
|
18
|
+
const context = {
|
|
19
|
+
process: processOnMainThread
|
|
74
20
|
};
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
21
|
+
// eslint-disable-next-line complexity
|
|
22
|
+
WorkerBody.onmessage = async (type, payload) => {
|
|
23
|
+
try {
|
|
24
|
+
switch (type) {
|
|
25
|
+
case 'process':
|
|
26
|
+
if (!process) {
|
|
27
|
+
throw new Error('Worker does not support atomic processing');
|
|
28
|
+
}
|
|
29
|
+
const result = await process(payload.input, payload.options || {}, context);
|
|
30
|
+
WorkerBody.postMessage('done', { result });
|
|
31
|
+
break;
|
|
32
|
+
case 'process-in-batches':
|
|
33
|
+
if (!processInBatches) {
|
|
34
|
+
throw new Error('Worker does not support batched processing');
|
|
35
|
+
}
|
|
36
|
+
inputBatches = new AsyncQueue();
|
|
37
|
+
options = payload.options || {};
|
|
38
|
+
const resultIterator = processInBatches(inputBatches, options, context);
|
|
39
|
+
for await (const batch of resultIterator) {
|
|
40
|
+
WorkerBody.postMessage('output-batch', { result: batch });
|
|
41
|
+
}
|
|
42
|
+
WorkerBody.postMessage('done', {});
|
|
43
|
+
break;
|
|
44
|
+
case 'input-batch':
|
|
45
|
+
inputBatches.push(payload.input);
|
|
46
|
+
break;
|
|
47
|
+
case 'input-done':
|
|
48
|
+
inputBatches.close();
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
const message = error instanceof Error ? error.message : '';
|
|
55
|
+
WorkerBody.postMessage('error', { error: message });
|
|
56
|
+
}
|
|
80
57
|
};
|
|
81
|
-
WorkerBody.postMessage('process', payload);
|
|
82
|
-
});
|
|
83
58
|
}
|
|
84
|
-
|
|
59
|
+
function processOnMainThread(arrayBuffer, options = {}) {
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
const id = requestId++;
|
|
62
|
+
/**
|
|
63
|
+
*/
|
|
64
|
+
const onMessage = (type, payload) => {
|
|
65
|
+
if (payload.id !== id) {
|
|
66
|
+
// not ours
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
switch (type) {
|
|
70
|
+
case 'done':
|
|
71
|
+
WorkerBody.removeEventListener(onMessage);
|
|
72
|
+
resolve(payload.result);
|
|
73
|
+
break;
|
|
74
|
+
case 'error':
|
|
75
|
+
WorkerBody.removeEventListener(onMessage);
|
|
76
|
+
reject(payload.error);
|
|
77
|
+
break;
|
|
78
|
+
default:
|
|
79
|
+
// ignore
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
WorkerBody.addEventListener(onMessage);
|
|
83
|
+
// Ask the main thread to decode data
|
|
84
|
+
const payload = { id, input: arrayBuffer, options };
|
|
85
|
+
WorkerBody.postMessage('process', payload);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-worker-url.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-api/get-worker-url.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAC,
|
|
1
|
+
{"version":3,"file":"get-worker-url.d.ts","sourceRoot":"","sources":["../../../src/lib/worker-api/get-worker-url.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAC,YAAY,EAAE,aAAa,EAAC,uBAAoB;AAK7D;;GAEG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAG1D;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CA6CtF"}
|