@loaders.gl/worker-utils 4.2.0-alpha.4 → 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
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,YAAY,EAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,YAAY,EAAC,mBAAgB;AAI1C,YAAY,EACV,YAAY,EACZ,aAAa,EAEb,aAAa,EACb,iBAAiB,EACjB,iBAAiB,EACjB,oBAAoB,EACrB,mBAAgB;AAGjB,OAAO,EAAC,MAAM,EAAC,kCAA+B;AAC9C,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAC,mCAAgC;AAG5D,OAAO,EAAC,OAAO,IAAI,SAAS,EAAC,wCAAqC;AAClE,OAAO,EAAC,OAAO,IAAI,YAAY,EAAC,2CAAwC;AAGxE,OAAO,EAAC,OAAO,IAAI,UAAU,EAAC,yCAAsC;AACpE,OAAO,EAAC,OAAO,IAAI,UAAU,EAAC,yCAAsC;AACpE,OAAO,EAAC,OAAO,IAAI,UAAU,EAAC,yCAAsC;AAGpE,YAAY,EAAC,sBAAsB,EAAC,8CAA2C;AAC/E,OAAO,EAAC,eAAe,EAAE,kBAAkB,EAAC,8CAA2C;AACvF,OAAO,EAAC,YAAY,EAAC,0CAAuC;AAG5D,OAAO,EAAC,YAAY,EAAC,2CAAwC;AAC7D,OAAO,EAAC,qBAAqB,EAAC,oDAAiD;AAC/E,OAAO,EAAC,eAAe,EAAE,wBAAwB,EAAC,gDAA6C;AAG/F,OAAO,EAAC,aAAa,EAAE,WAAW,EAAC,6CAA0C;AAG7E,OAAO,EAAC,OAAO,IAAI,UAAU,EAAC,yCAAsC;AAGpE,OAAO,EAAC,OAAO,IAAI,iBAAiB,EAAC,mDAAgD;AAIrF,iEAAiE;AACjE,eAAO,MAAM,UAAU,EAAE,YAQxB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,37 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import { VERSION } from "./lib/env-utils/version.js";
|
|
5
|
+
// GENERAL UTILS
|
|
2
6
|
export { assert } from "./lib/env-utils/assert.js";
|
|
3
7
|
export { isBrowser, isWorker } from "./lib/env-utils/globals.js";
|
|
8
|
+
// WORKER UTILS - TYPES
|
|
4
9
|
export { default as WorkerJob } from "./lib/worker-farm/worker-job.js";
|
|
5
10
|
export { default as WorkerThread } from "./lib/worker-farm/worker-thread.js";
|
|
11
|
+
// WORKER FARMS
|
|
6
12
|
export { default as WorkerFarm } from "./lib/worker-farm/worker-farm.js";
|
|
7
13
|
export { default as WorkerPool } from "./lib/worker-farm/worker-pool.js";
|
|
8
14
|
export { default as WorkerBody } from "./lib/worker-farm/worker-body.js";
|
|
9
15
|
export { processOnWorker, canProcessOnWorker } from "./lib/worker-api/process-on-worker.js";
|
|
10
16
|
export { createWorker } from "./lib/worker-api/create-worker.js";
|
|
17
|
+
// WORKER UTILS - EXPORTS
|
|
11
18
|
export { getWorkerURL } from "./lib/worker-api/get-worker-url.js";
|
|
12
19
|
export { validateWorkerVersion } from "./lib/worker-api/validate-worker-version.js";
|
|
13
20
|
export { getTransferList, getTransferListForWriter } from "./lib/worker-utils/get-transfer-list.js";
|
|
21
|
+
// LIBRARY UTILS
|
|
14
22
|
export { getLibraryUrl, loadLibrary } from "./lib/library-utils/library-utils.js";
|
|
23
|
+
// PARSER UTILS
|
|
15
24
|
export { default as AsyncQueue } from "./lib/async-queue/async-queue.js";
|
|
25
|
+
// PROCESS UTILS
|
|
16
26
|
export { default as ChildProcessProxy } from "./lib/process-utils/child-process-proxy.js";
|
|
27
|
+
// WORKER OBJECTS
|
|
28
|
+
/** A null worker to test that worker processing is functional */
|
|
17
29
|
export const NullWorker = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
30
|
+
id: 'null',
|
|
31
|
+
name: 'null',
|
|
32
|
+
module: 'worker-utils',
|
|
33
|
+
version: VERSION,
|
|
34
|
+
options: {
|
|
35
|
+
null: {}
|
|
36
|
+
}
|
|
25
37
|
};
|
|
26
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1,75 +1,87 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
// From https://github.com/rauschma/async-iter-demo/tree/master/src under MIT license
|
|
5
|
+
// http://2ality.com/2016/10/asynchronous-iteration.html
|
|
6
|
+
/**
|
|
7
|
+
* Async Queue
|
|
8
|
+
* - AsyncIterable: An async iterator can be
|
|
9
|
+
* - Values can be pushed onto the queue
|
|
10
|
+
* @example
|
|
11
|
+
* const asyncQueue = new AsyncQueue();
|
|
12
|
+
* setTimeout(() => asyncQueue.enqueue('tick'), 1000);
|
|
13
|
+
* setTimeout(() => asyncQueue.enqueue(new Error('done')), 10000);
|
|
14
|
+
* for await (const value of asyncQueue) {
|
|
15
|
+
* console.log(value); // tick
|
|
16
|
+
* }
|
|
17
|
+
*/
|
|
3
18
|
export default class AsyncQueue {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
this._values = [];
|
|
9
|
-
this._settlers = [];
|
|
10
|
-
this._closed = false;
|
|
11
|
-
}
|
|
12
|
-
[_Symbol$asyncIterator]() {
|
|
13
|
-
return this;
|
|
14
|
-
}
|
|
15
|
-
push(value) {
|
|
16
|
-
return this.enqueue(value);
|
|
17
|
-
}
|
|
18
|
-
enqueue(value) {
|
|
19
|
-
if (this._closed) {
|
|
20
|
-
throw new Error('Closed');
|
|
19
|
+
constructor() {
|
|
20
|
+
this._values = []; // enqueues > dequeues
|
|
21
|
+
this._settlers = []; // dequeues > enqueues
|
|
22
|
+
this._closed = false;
|
|
21
23
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
} else {
|
|
30
|
-
settler.resolve({
|
|
31
|
-
value
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
} else {
|
|
35
|
-
this._values.push(value);
|
|
24
|
+
/** Return an async iterator for this queue */
|
|
25
|
+
[Symbol.asyncIterator]() {
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
/** Push a new value - the async iterator will yield a promise resolved to this value */
|
|
29
|
+
push(value) {
|
|
30
|
+
return this.enqueue(value);
|
|
36
31
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Push a new value - the async iterator will yield a promise resolved to this value
|
|
34
|
+
* Add an error - the async iterator will yield a promise rejected with this value
|
|
35
|
+
*/
|
|
36
|
+
enqueue(value) {
|
|
37
|
+
if (this._closed) {
|
|
38
|
+
throw new Error('Closed');
|
|
39
|
+
}
|
|
40
|
+
if (this._settlers.length > 0) {
|
|
41
|
+
if (this._values.length > 0) {
|
|
42
|
+
throw new Error('Illegal internal state');
|
|
43
|
+
}
|
|
44
|
+
const settler = this._settlers.shift();
|
|
45
|
+
if (value instanceof Error) {
|
|
46
|
+
settler.reject(value);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
settler.resolve({ value });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this._values.push(value);
|
|
54
|
+
}
|
|
44
55
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
return Promise.resolve({
|
|
54
|
-
done: false,
|
|
55
|
-
value
|
|
56
|
-
});
|
|
56
|
+
/** Indicate that we not waiting for more values - The async iterator will be done */
|
|
57
|
+
close() {
|
|
58
|
+
while (this._settlers.length > 0) {
|
|
59
|
+
const settler = this._settlers.shift();
|
|
60
|
+
settler.resolve({ done: true });
|
|
61
|
+
}
|
|
62
|
+
this._closed = true;
|
|
57
63
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
// ITERATOR IMPLEMENTATION
|
|
65
|
+
/** @returns a Promise for an IteratorResult */
|
|
66
|
+
next() {
|
|
67
|
+
// If values in queue, yield the first value
|
|
68
|
+
if (this._values.length > 0) {
|
|
69
|
+
const value = this._values.shift();
|
|
70
|
+
if (value instanceof Error) {
|
|
71
|
+
return Promise.reject(value);
|
|
72
|
+
}
|
|
73
|
+
return Promise.resolve({ done: false, value });
|
|
74
|
+
}
|
|
75
|
+
// If queue is closed, the iterator is done
|
|
76
|
+
if (this._closed) {
|
|
77
|
+
if (this._settlers.length > 0) {
|
|
78
|
+
throw new Error('Illegal internal state');
|
|
79
|
+
}
|
|
80
|
+
return Promise.resolve({ done: true, value: undefined });
|
|
81
|
+
}
|
|
82
|
+
// Yield a promise that waits for new values to be enqueued
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
this._settlers.push({ resolve, reject });
|
|
85
|
+
});
|
|
66
86
|
}
|
|
67
|
-
return new Promise((resolve, reject) => {
|
|
68
|
-
this._settlers.push({
|
|
69
|
-
resolve,
|
|
70
|
-
reject
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
87
|
}
|
|
75
|
-
//# sourceMappingURL=async-queue.js.map
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
// Replacement for the external assert method to reduce bundle size
|
|
5
|
+
// Note: We don't use the second "message" argument in calling code,
|
|
6
|
+
// so no need to support it here
|
|
7
|
+
/** Throws an `Error` with the optional `message` if `condition` is falsy */
|
|
1
8
|
export function assert(condition, message) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
9
|
+
if (!condition) {
|
|
10
|
+
throw new Error(message || 'loaders.gl assertion failed.');
|
|
11
|
+
}
|
|
5
12
|
}
|
|
6
|
-
//# sourceMappingURL=assert.js.map
|
|
@@ -1,17 +1,29 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
// Purpose: include this in your module to avoids adding dependencies on
|
|
5
|
+
// micro modules like 'global' and 'is-browser';
|
|
6
|
+
/* eslint-disable no-restricted-globals */
|
|
1
7
|
const globals = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
8
|
+
self: typeof self !== 'undefined' && self,
|
|
9
|
+
window: typeof window !== 'undefined' && window,
|
|
10
|
+
global: typeof global !== 'undefined' && global,
|
|
11
|
+
document: typeof document !== 'undefined' && document
|
|
6
12
|
};
|
|
7
13
|
const self_ = globals.self || globals.window || globals.global || {};
|
|
8
14
|
const window_ = globals.window || globals.self || globals.global || {};
|
|
9
15
|
const global_ = globals.global || globals.self || globals.window || {};
|
|
10
16
|
const document_ = globals.document || {};
|
|
11
17
|
export { self_ as self, window_ as window, global_ as global, document_ as document };
|
|
12
|
-
|
|
18
|
+
/** true if running in the browser, false if running in Node.js */
|
|
19
|
+
export const isBrowser =
|
|
20
|
+
// @ts-ignore process.browser
|
|
21
|
+
typeof process !== 'object' || String(process) !== '[object process]' || process.browser;
|
|
22
|
+
/** true if running on a worker thread */
|
|
13
23
|
export const isWorker = typeof importScripts === 'function';
|
|
24
|
+
/** true if running on a mobile device */
|
|
14
25
|
export const isMobile = typeof window !== 'undefined' && typeof window.orientation !== 'undefined';
|
|
26
|
+
// Extract node major version
|
|
15
27
|
const matches = typeof process !== 'undefined' && process.version && /v([0-9]*)/.exec(process.version);
|
|
16
|
-
|
|
17
|
-
|
|
28
|
+
/** Version of Node.js if running under Node, otherwise 0 */
|
|
29
|
+
export const nodeVersion = (matches && parseFloat(matches[1])) || 0;
|
|
@@ -1,16 +1,25 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
// Version constant cannot be imported, it needs to correspond to the build version of **this** module.
|
|
5
|
+
/**
|
|
6
|
+
* TODO - unpkg.com doesn't seem to have a `latest` specifier for alpha releases...
|
|
7
|
+
* 'beta' on beta branch, 'latest' on prod branch
|
|
8
|
+
*/
|
|
1
9
|
export const NPM_TAG = 'latest';
|
|
2
10
|
function getVersion() {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
if (!globalThis._loadersgl_?.version) {
|
|
12
|
+
globalThis._loadersgl_ = globalThis._loadersgl_ || {};
|
|
13
|
+
// __VERSION__ is injected by babel-plugin-version-inline
|
|
14
|
+
if (typeof "4.2.0-alpha.4" === 'undefined') {
|
|
15
|
+
// eslint-disable-next-line
|
|
16
|
+
console.warn('loaders.gl: The __VERSION__ variable is not injected using babel plugin. Latest unstable workers would be fetched from the CDN.');
|
|
17
|
+
globalThis._loadersgl_.version = NPM_TAG;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
globalThis._loadersgl_.version = "4.2.0-alpha.4";
|
|
21
|
+
}
|
|
11
22
|
}
|
|
12
|
-
|
|
13
|
-
return globalThis._loadersgl_.version;
|
|
23
|
+
return globalThis._loadersgl_.version;
|
|
14
24
|
}
|
|
15
25
|
export const VERSION = getVersion();
|
|
16
|
-
//# sourceMappingURL=version.js.map
|
|
@@ -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
|