@math.gl/tasks 0.0.0-stage → 5.0.0-beta.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.
Files changed (212) hide show
  1. package/LICENSE +147 -0
  2. package/LOADERS-GL-LICENSE.txt +52 -0
  3. package/README.md +26 -2
  4. package/THIRD-PARTY-NOTICES.md +5 -0
  5. package/dist/index.cjs +3454 -0
  6. package/dist/index.cjs.map +6 -0
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +5 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/tasks/index.d.ts +9 -0
  12. package/dist/tasks/index.d.ts.map +1 -0
  13. package/dist/tasks/index.js +9 -0
  14. package/dist/tasks/index.js.map +1 -0
  15. package/dist/tasks/task-channel.d.ts +48 -0
  16. package/dist/tasks/task-channel.d.ts.map +1 -0
  17. package/dist/tasks/task-channel.js +166 -0
  18. package/dist/tasks/task-channel.js.map +1 -0
  19. package/dist/tasks/task-operation-queue.d.ts +26 -0
  20. package/dist/tasks/task-operation-queue.d.ts.map +1 -0
  21. package/dist/tasks/task-operation-queue.js +106 -0
  22. package/dist/tasks/task-operation-queue.js.map +1 -0
  23. package/dist/tasks/task-preload.d.ts +13 -0
  24. package/dist/tasks/task-preload.d.ts.map +1 -0
  25. package/dist/tasks/task-preload.js +96 -0
  26. package/dist/tasks/task-preload.js.map +1 -0
  27. package/dist/tasks/task-runner.d.ts +74 -0
  28. package/dist/tasks/task-runner.d.ts.map +1 -0
  29. package/dist/tasks/task-runner.js +811 -0
  30. package/dist/tasks/task-runner.js.map +1 -0
  31. package/dist/tasks/task-runtime.d.ts +27 -0
  32. package/dist/tasks/task-runtime.d.ts.map +1 -0
  33. package/dist/tasks/task-runtime.js +216 -0
  34. package/dist/tasks/task-runtime.js.map +1 -0
  35. package/dist/tasks/task-sync.d.ts +4 -0
  36. package/dist/tasks/task-sync.d.ts.map +1 -0
  37. package/dist/tasks/task-sync.js +95 -0
  38. package/dist/tasks/task-sync.js.map +1 -0
  39. package/dist/tasks/task-worker-channel.d.ts +11 -0
  40. package/dist/tasks/task-worker-channel.d.ts.map +1 -0
  41. package/dist/tasks/task-worker-channel.js +34 -0
  42. package/dist/tasks/task-worker-channel.js.map +1 -0
  43. package/dist/tasks/task.d.ts +132 -0
  44. package/dist/tasks/task.d.ts.map +1 -0
  45. package/dist/tasks/task.js +5 -0
  46. package/dist/tasks/task.js.map +1 -0
  47. package/dist/tasks/typed-task-dispatch.d.ts +30 -0
  48. package/dist/tasks/typed-task-dispatch.d.ts.map +1 -0
  49. package/dist/tasks/typed-task-dispatch.js +12 -0
  50. package/dist/tasks/typed-task-dispatch.js.map +1 -0
  51. package/dist/worker-utils/index.d.ts +25 -0
  52. package/dist/worker-utils/index.d.ts.map +1 -0
  53. package/dist/worker-utils/index.js +46 -0
  54. package/dist/worker-utils/index.js.map +1 -0
  55. package/dist/worker-utils/lib/async-queue/async-queue.d.ts +32 -0
  56. package/dist/worker-utils/lib/async-queue/async-queue.d.ts.map +1 -0
  57. package/dist/worker-utils/lib/async-queue/async-queue.js +88 -0
  58. package/dist/worker-utils/lib/async-queue/async-queue.js.map +1 -0
  59. package/dist/worker-utils/lib/env-utils/assert.d.ts +3 -0
  60. package/dist/worker-utils/lib/env-utils/assert.d.ts.map +1 -0
  61. package/dist/worker-utils/lib/env-utils/assert.js +13 -0
  62. package/dist/worker-utils/lib/env-utils/assert.js.map +1 -0
  63. package/dist/worker-utils/lib/env-utils/globals.d.ts +22 -0
  64. package/dist/worker-utils/lib/env-utils/globals.d.ts.map +1 -0
  65. package/dist/worker-utils/lib/env-utils/globals.js +30 -0
  66. package/dist/worker-utils/lib/env-utils/globals.js.map +1 -0
  67. package/dist/worker-utils/lib/env-utils/version.d.ts +3 -0
  68. package/dist/worker-utils/lib/env-utils/version.d.ts.map +1 -0
  69. package/dist/worker-utils/lib/env-utils/version.js +11 -0
  70. package/dist/worker-utils/lib/env-utils/version.js.map +1 -0
  71. package/dist/worker-utils/lib/library-utils/library-utils.d.ts +42 -0
  72. package/dist/worker-utils/lib/library-utils/library-utils.d.ts.map +1 -0
  73. package/dist/worker-utils/lib/library-utils/library-utils.js +251 -0
  74. package/dist/worker-utils/lib/library-utils/library-utils.js.map +1 -0
  75. package/dist/worker-utils/lib/node/worker_threads-browser.d.ts +15 -0
  76. package/dist/worker-utils/lib/node/worker_threads-browser.d.ts.map +1 -0
  77. package/dist/worker-utils/lib/node/worker_threads-browser.js +23 -0
  78. package/dist/worker-utils/lib/node/worker_threads-browser.js.map +1 -0
  79. package/dist/worker-utils/lib/node/worker_threads.d.ts +16 -0
  80. package/dist/worker-utils/lib/node/worker_threads.d.ts.map +1 -0
  81. package/dist/worker-utils/lib/node/worker_threads.js +7 -0
  82. package/dist/worker-utils/lib/node/worker_threads.js.map +1 -0
  83. package/dist/worker-utils/lib/npm-tag.d.ts +2 -0
  84. package/dist/worker-utils/lib/npm-tag.d.ts.map +1 -0
  85. package/dist/worker-utils/lib/npm-tag.js +5 -0
  86. package/dist/worker-utils/lib/npm-tag.js.map +1 -0
  87. package/dist/worker-utils/lib/process-utils/child-process-proxy.browser.d.ts +8 -0
  88. package/dist/worker-utils/lib/process-utils/child-process-proxy.browser.d.ts.map +1 -0
  89. package/dist/worker-utils/lib/process-utils/child-process-proxy.browser.js +18 -0
  90. package/dist/worker-utils/lib/process-utils/child-process-proxy.browser.js.map +1 -0
  91. package/dist/worker-utils/lib/process-utils/child-process-proxy.d.ts +45 -0
  92. package/dist/worker-utils/lib/process-utils/child-process-proxy.d.ts.map +1 -0
  93. package/dist/worker-utils/lib/process-utils/child-process-proxy.js +128 -0
  94. package/dist/worker-utils/lib/process-utils/child-process-proxy.js.map +1 -0
  95. package/dist/worker-utils/lib/process-utils/process-utils.d.ts +2 -0
  96. package/dist/worker-utils/lib/process-utils/process-utils.d.ts.map +1 -0
  97. package/dist/worker-utils/lib/process-utils/process-utils.js +32 -0
  98. package/dist/worker-utils/lib/process-utils/process-utils.js.map +1 -0
  99. package/dist/worker-utils/lib/worker-api/create-worker.d.ts +9 -0
  100. package/dist/worker-utils/lib/worker-api/create-worker.d.ts.map +1 -0
  101. package/dist/worker-utils/lib/worker-api/create-worker.js +229 -0
  102. package/dist/worker-utils/lib/worker-api/create-worker.js.map +1 -0
  103. package/dist/worker-utils/lib/worker-api/get-worker-url.d.ts +29 -0
  104. package/dist/worker-utils/lib/worker-api/get-worker-url.d.ts.map +1 -0
  105. package/dist/worker-utils/lib/worker-api/get-worker-url.js +101 -0
  106. package/dist/worker-utils/lib/worker-api/get-worker-url.js.map +1 -0
  107. package/dist/worker-utils/lib/worker-api/process-on-worker.d.ts +67 -0
  108. package/dist/worker-utils/lib/worker-api/process-on-worker.d.ts.map +1 -0
  109. package/dist/worker-utils/lib/worker-api/process-on-worker.js +429 -0
  110. package/dist/worker-utils/lib/worker-api/process-on-worker.js.map +1 -0
  111. package/dist/worker-utils/lib/worker-api/shared-state.d.ts +34 -0
  112. package/dist/worker-utils/lib/worker-api/shared-state.d.ts.map +1 -0
  113. package/dist/worker-utils/lib/worker-api/shared-state.js +105 -0
  114. package/dist/worker-utils/lib/worker-api/shared-state.js.map +1 -0
  115. package/dist/worker-utils/lib/worker-api/validate-output-ownership.d.ts +3 -0
  116. package/dist/worker-utils/lib/worker-api/validate-output-ownership.d.ts.map +1 -0
  117. package/dist/worker-utils/lib/worker-api/validate-output-ownership.js +10 -0
  118. package/dist/worker-utils/lib/worker-api/validate-output-ownership.js.map +1 -0
  119. package/dist/worker-utils/lib/worker-api/validate-worker-version.d.ts +9 -0
  120. package/dist/worker-utils/lib/worker-api/validate-worker-version.d.ts.map +1 -0
  121. package/dist/worker-utils/lib/worker-api/validate-worker-version.js +35 -0
  122. package/dist/worker-utils/lib/worker-api/validate-worker-version.js.map +1 -0
  123. package/dist/worker-utils/lib/worker-api/worker-resource.d.ts +33 -0
  124. package/dist/worker-utils/lib/worker-api/worker-resource.d.ts.map +1 -0
  125. package/dist/worker-utils/lib/worker-api/worker-resource.js +156 -0
  126. package/dist/worker-utils/lib/worker-api/worker-resource.js.map +1 -0
  127. package/dist/worker-utils/lib/worker-farm/worker-body.d.ts +20 -0
  128. package/dist/worker-utils/lib/worker-farm/worker-body.d.ts.map +1 -0
  129. package/dist/worker-utils/lib/worker-farm/worker-body.js +134 -0
  130. package/dist/worker-utils/lib/worker-farm/worker-body.js.map +1 -0
  131. package/dist/worker-utils/lib/worker-farm/worker-farm.d.ts +73 -0
  132. package/dist/worker-utils/lib/worker-farm/worker-farm.d.ts.map +1 -0
  133. package/dist/worker-utils/lib/worker-farm/worker-farm.js +135 -0
  134. package/dist/worker-utils/lib/worker-farm/worker-farm.js.map +1 -0
  135. package/dist/worker-utils/lib/worker-farm/worker-job.d.ts +47 -0
  136. package/dist/worker-utils/lib/worker-farm/worker-job.d.ts.map +1 -0
  137. package/dist/worker-utils/lib/worker-farm/worker-job.js +124 -0
  138. package/dist/worker-utils/lib/worker-farm/worker-job.js.map +1 -0
  139. package/dist/worker-utils/lib/worker-farm/worker-pool.d.ts +152 -0
  140. package/dist/worker-utils/lib/worker-farm/worker-pool.d.ts.map +1 -0
  141. package/dist/worker-utils/lib/worker-farm/worker-pool.js +584 -0
  142. package/dist/worker-utils/lib/worker-farm/worker-pool.js.map +1 -0
  143. package/dist/worker-utils/lib/worker-farm/worker-thread.d.ts +77 -0
  144. package/dist/worker-utils/lib/worker-farm/worker-thread.d.ts.map +1 -0
  145. package/dist/worker-utils/lib/worker-farm/worker-thread.js +215 -0
  146. package/dist/worker-utils/lib/worker-farm/worker-thread.js.map +1 -0
  147. package/dist/worker-utils/lib/worker-utils/get-input-byte-length.d.ts +5 -0
  148. package/dist/worker-utils/lib/worker-utils/get-input-byte-length.d.ts.map +1 -0
  149. package/dist/worker-utils/lib/worker-utils/get-input-byte-length.js +39 -0
  150. package/dist/worker-utils/lib/worker-utils/get-input-byte-length.js.map +1 -0
  151. package/dist/worker-utils/lib/worker-utils/get-loadable-worker-url.d.ts +15 -0
  152. package/dist/worker-utils/lib/worker-utils/get-loadable-worker-url.d.ts.map +1 -0
  153. package/dist/worker-utils/lib/worker-utils/get-loadable-worker-url.js +84 -0
  154. package/dist/worker-utils/lib/worker-utils/get-loadable-worker-url.js.map +1 -0
  155. package/dist/worker-utils/lib/worker-utils/get-transfer-list.d.ts +16 -0
  156. package/dist/worker-utils/lib/worker-utils/get-transfer-list.d.ts.map +1 -0
  157. package/dist/worker-utils/lib/worker-utils/get-transfer-list.js +127 -0
  158. package/dist/worker-utils/lib/worker-utils/get-transfer-list.js.map +1 -0
  159. package/dist/worker-utils/lib/worker-utils/remove-nontransferable-options.d.ts +6 -0
  160. package/dist/worker-utils/lib/worker-utils/remove-nontransferable-options.d.ts.map +1 -0
  161. package/dist/worker-utils/lib/worker-utils/remove-nontransferable-options.js +27 -0
  162. package/dist/worker-utils/lib/worker-utils/remove-nontransferable-options.js.map +1 -0
  163. package/dist/worker-utils/types.d.ts +165 -0
  164. package/dist/worker-utils/types.d.ts.map +1 -0
  165. package/dist/worker-utils/types.js +5 -0
  166. package/dist/worker-utils/types.js.map +1 -0
  167. package/dist/worker-utils/workers/null-worker.d.ts +2 -0
  168. package/dist/worker-utils/workers/null-worker.d.ts.map +1 -0
  169. package/dist/worker-utils/workers/null-worker.js +17 -0
  170. package/dist/worker-utils/workers/null-worker.js.map +1 -0
  171. package/package.json +42 -4
  172. package/src/index.ts +5 -0
  173. package/src/tasks/index.ts +33 -0
  174. package/src/tasks/task-channel.ts +191 -0
  175. package/src/tasks/task-operation-queue.ts +109 -0
  176. package/src/tasks/task-preload.ts +107 -0
  177. package/src/tasks/task-runner.ts +933 -0
  178. package/src/tasks/task-runtime.ts +244 -0
  179. package/src/tasks/task-sync.ts +100 -0
  180. package/src/tasks/task-worker-channel.ts +44 -0
  181. package/src/tasks/task.ts +168 -0
  182. package/src/tasks/typed-task-dispatch.ts +44 -0
  183. package/src/worker-utils/index.ts +105 -0
  184. package/src/worker-utils/lib/async-queue/async-queue.ts +100 -0
  185. package/src/worker-utils/lib/env-utils/assert.ts +14 -0
  186. package/src/worker-utils/lib/env-utils/globals.ts +40 -0
  187. package/src/worker-utils/lib/env-utils/version.ts +15 -0
  188. package/src/worker-utils/lib/library-utils/library-utils.ts +305 -0
  189. package/src/worker-utils/lib/node/worker_threads-browser.ts +44 -0
  190. package/src/worker-utils/lib/node/worker_threads.ts +21 -0
  191. package/src/worker-utils/lib/npm-tag.ts +5 -0
  192. package/src/worker-utils/lib/process-utils/child-process-proxy.browser.ts +23 -0
  193. package/src/worker-utils/lib/process-utils/child-process-proxy.ts +172 -0
  194. package/src/worker-utils/lib/process-utils/process-utils.ts +34 -0
  195. package/src/worker-utils/lib/worker-api/create-worker.ts +271 -0
  196. package/src/worker-utils/lib/worker-api/get-worker-url.ts +130 -0
  197. package/src/worker-utils/lib/worker-api/process-on-worker.ts +596 -0
  198. package/src/worker-utils/lib/worker-api/shared-state.ts +135 -0
  199. package/src/worker-utils/lib/worker-api/validate-output-ownership.ts +10 -0
  200. package/src/worker-utils/lib/worker-api/validate-worker-version.ts +44 -0
  201. package/src/worker-utils/lib/worker-api/worker-resource.ts +203 -0
  202. package/src/worker-utils/lib/worker-farm/worker-body.ts +154 -0
  203. package/src/worker-utils/lib/worker-farm/worker-farm.ts +172 -0
  204. package/src/worker-utils/lib/worker-farm/worker-job.ts +148 -0
  205. package/src/worker-utils/lib/worker-farm/worker-pool.ts +719 -0
  206. package/src/worker-utils/lib/worker-farm/worker-thread.ts +260 -0
  207. package/src/worker-utils/lib/worker-utils/get-input-byte-length.ts +34 -0
  208. package/src/worker-utils/lib/worker-utils/get-loadable-worker-url.ts +96 -0
  209. package/src/worker-utils/lib/worker-utils/get-transfer-list.ts +125 -0
  210. package/src/worker-utils/lib/worker-utils/remove-nontransferable-options.ts +27 -0
  211. package/src/worker-utils/types.ts +224 -0
  212. package/src/worker-utils/workers/null-worker.ts +20 -0
@@ -0,0 +1,105 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import type {WorkerObject} from './types';
6
+ import {VERSION} from './lib/env-utils/version';
7
+ declare const __filename: string;
8
+
9
+ // TYPES
10
+ export type {
11
+ LoadWorker,
12
+ WorkerObject,
13
+ WorkerOptions,
14
+ WorkerProgress,
15
+ WorkerJobMetrics,
16
+ WorkerPoolStats,
17
+ WorkerContext,
18
+ WorkerJobContext,
19
+ Process,
20
+ ProcessInBatches,
21
+ // Protocol
22
+ WorkerMessage,
23
+ WorkerMessageType,
24
+ WorkerMessageData,
25
+ WorkerMessagePayload
26
+ } from './types';
27
+
28
+ // GENERAL UTILS
29
+ export {assert} from './lib/env-utils/assert';
30
+ export {isBrowser, isWorker} from './lib/env-utils/globals';
31
+
32
+ // WORKER UTILS - TYPES
33
+ export {default as WorkerJob} from './lib/worker-farm/worker-job';
34
+ export {default as WorkerThread} from './lib/worker-farm/worker-thread';
35
+
36
+ // WORKER FARMS
37
+ export {default as WorkerFarm} from './lib/worker-farm/worker-farm';
38
+ export {default as WorkerPool} from './lib/worker-farm/worker-pool';
39
+ export {default as WorkerBody} from './lib/worker-farm/worker-body';
40
+
41
+ // PROCESS ON WORKER
42
+ export type {ProcessOnWorkerOptions} from './lib/worker-api/process-on-worker';
43
+ export type {PreloadWorkerOptions} from './lib/worker-api/process-on-worker';
44
+ export {
45
+ processOnWorker,
46
+ processOnWorkerInBatches,
47
+ preloadWorker,
48
+ canProcessOnWorker,
49
+ canProcessOnWorkerInBatches
50
+ } from './lib/worker-api/process-on-worker';
51
+ export {createWorker} from './lib/worker-api/create-worker';
52
+
53
+ // WORKER UTILS - EXPORTS
54
+ export {getWorkerURL} from './lib/worker-api/get-worker-url';
55
+ export {validateWorkerVersion} from './lib/worker-api/validate-worker-version';
56
+ export {getTransferList, getTransferListForWriter} from './lib/worker-utils/get-transfer-list';
57
+
58
+ // LIBRARY UTILS
59
+ export {
60
+ extractLoadLibraryOptions,
61
+ getLibraryUrl,
62
+ loadLibrary,
63
+ type LoadLibraryOptions
64
+ } from './lib/library-utils/library-utils';
65
+
66
+ // PARSER UTILS
67
+ export {default as AsyncQueue} from './lib/async-queue/async-queue';
68
+
69
+ // WORKER OBJECTS
70
+
71
+ /** A null worker to test that worker processing is functional */
72
+ const path = typeof __filename === 'string' ? __filename.replace(/\\/g, '/') : '';
73
+ const moduleUrl = path
74
+ ? `file://${path.startsWith('/') ? '' : '/'}${encodeURI(path).replace(/#/g, '%23').replace(/\?/g, '%3F')}`
75
+ : import.meta.url;
76
+ export const NullWorker: WorkerObject = {
77
+ id: 'null',
78
+ name: 'null',
79
+ module: 'tasks',
80
+ packageName: '@math.gl/tasks',
81
+ // Drop the entire URL expression when classic bundles do not use this descriptor.
82
+ worker: /* @__PURE__ */ (() => new URL('./workers/null-worker.js', moduleUrl).href)(),
83
+ loadWorker: () =>
84
+ new Worker(new URL('./workers/null-worker.js', import.meta.url), {type: 'module'}),
85
+ version: VERSION,
86
+ options: {
87
+ null: {}
88
+ }
89
+ };
90
+
91
+ export type {WorkerAdmission, WorkerPoolProps} from './lib/worker-farm/worker-pool';
92
+ export {createWorkerResource, createResourceWorker} from './lib/worker-api/worker-resource';
93
+ export type {
94
+ WorkerResource,
95
+ WorkerResourceRunOptions,
96
+ WorkerResourceObject,
97
+ ResourceRequest
98
+ } from './lib/worker-api/worker-resource';
99
+
100
+ export {
101
+ getWorkerCapabilities,
102
+ createSharedWorkerState,
103
+ SharedWorkerState
104
+ } from './lib/worker-api/shared-state';
105
+ export type {WorkerCapabilities, SharedWorkerSnapshot} from './lib/worker-api/shared-state';
@@ -0,0 +1,100 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ // From https://github.com/rauschma/async-iter-demo/tree/master/src under MIT license
6
+ // http://2ality.com/2016/10/asynchronous-iteration.html
7
+
8
+ /**
9
+ * Async Queue
10
+ * - AsyncIterable: An async iterator can be
11
+ * - Values can be pushed onto the queue
12
+ * @example
13
+ * const asyncQueue = new AsyncQueue();
14
+ * setTimeout(() => asyncQueue.enqueue('tick'), 1000);
15
+ * setTimeout(() => asyncQueue.enqueue(new Error('done')), 10000);
16
+ * for await (const value of asyncQueue) {
17
+ * console.log(value); // tick
18
+ * }
19
+ */
20
+ export default class AsyncQueue<T> {
21
+ private _values: any[];
22
+ private _settlers: any[];
23
+ private _closed: boolean;
24
+
25
+ constructor() {
26
+ this._values = []; // enqueues > dequeues
27
+ this._settlers = []; // dequeues > enqueues
28
+ this._closed = false;
29
+ }
30
+
31
+ /** Return an async iterator for this queue */
32
+ [Symbol.asyncIterator](): AsyncIterator<T> {
33
+ return this;
34
+ }
35
+
36
+ /** Push a new value - the async iterator will yield a promise resolved to this value */
37
+ push(value: T): void {
38
+ return this.enqueue(value);
39
+ }
40
+
41
+ /**
42
+ * Push a new value - the async iterator will yield a promise resolved to this value
43
+ * Add an error - the async iterator will yield a promise rejected with this value
44
+ */
45
+ enqueue(value: T | Error): void {
46
+ if (this._closed) {
47
+ throw new Error('Closed');
48
+ }
49
+
50
+ if (this._settlers.length > 0) {
51
+ if (this._values.length > 0) {
52
+ throw new Error('Illegal internal state');
53
+ }
54
+ const settler = this._settlers.shift();
55
+ if (value instanceof Error) {
56
+ settler.reject(value);
57
+ } else {
58
+ settler.resolve({value});
59
+ }
60
+ } else {
61
+ this._values.push(value);
62
+ }
63
+ }
64
+
65
+ /** Indicate that we not waiting for more values - The async iterator will be done */
66
+ close(): void {
67
+ while (this._settlers.length > 0) {
68
+ const settler = this._settlers.shift();
69
+ settler.resolve({done: true});
70
+ }
71
+ this._closed = true;
72
+ }
73
+
74
+ // ITERATOR IMPLEMENTATION
75
+
76
+ /** @returns a Promise for an IteratorResult */
77
+ next(): Promise<IteratorResult<T, any>> {
78
+ // If values in queue, yield the first value
79
+ if (this._values.length > 0) {
80
+ const value = this._values.shift();
81
+ if (value instanceof Error) {
82
+ return Promise.reject(value);
83
+ }
84
+ return Promise.resolve({done: false, value});
85
+ }
86
+
87
+ // If queue is closed, the iterator is done
88
+ if (this._closed) {
89
+ if (this._settlers.length > 0) {
90
+ throw new Error('Illegal internal state');
91
+ }
92
+ return Promise.resolve({done: true, value: undefined});
93
+ }
94
+
95
+ // Yield a promise that waits for new values to be enqueued
96
+ return new Promise((resolve, reject) => {
97
+ this._settlers.push({resolve, reject});
98
+ });
99
+ }
100
+ }
@@ -0,0 +1,14 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ // Replacement for the external assert method to reduce bundle size
6
+ // Note: We don't use the second "message" argument in calling code,
7
+ // so no need to support it here
8
+
9
+ /** Throws an `Error` with the optional `message` if `condition` is falsy */
10
+ export function assert(condition: any, message?: string): void {
11
+ if (!condition) {
12
+ throw new Error(message || 'loaders.gl assertion failed.');
13
+ }
14
+ }
@@ -0,0 +1,40 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ // Purpose: include this in your module to avoids adding dependencies on
6
+ // micro modules like 'global' and 'is-browser';
7
+
8
+ /* eslint-disable no-restricted-globals */
9
+ const globals = {
10
+ self: typeof self !== 'undefined' && self,
11
+ window: typeof window !== 'undefined' && window,
12
+ global: typeof global !== 'undefined' && global,
13
+ document: typeof document !== 'undefined' && document
14
+ };
15
+
16
+ const self_: {[key: string]: any} = globals.self || globals.window || globals.global || {};
17
+ const window_: {[key: string]: any} = globals.window || globals.self || globals.global || {};
18
+ const global_: {[key: string]: any} = globals.global || globals.self || globals.window || {};
19
+ const document_: {[key: string]: any} = globals.document || {};
20
+
21
+ export {self_ as self, window_ as window, global_ as global, document_ as document};
22
+
23
+ /** true if running in the browser, false if running in Node.js */
24
+ export const isBrowser: boolean =
25
+ // @ts-ignore process.browser
26
+ typeof process !== 'object' || String(process) !== '[object process]' || process.browser;
27
+
28
+ /** true if running on a worker thread */
29
+ export const isWorker: boolean = typeof importScripts === 'function';
30
+
31
+ /** true if running on a mobile device */
32
+ export const isMobile: boolean =
33
+ typeof window !== 'undefined' && typeof window.orientation !== 'undefined';
34
+
35
+ // Extract node major version
36
+ const matches =
37
+ typeof process !== 'undefined' && process.version && /v([0-9]*)/.exec(process.version);
38
+
39
+ /** Version of Node.js if running under Node, otherwise 0 */
40
+ export const nodeVersion: number = (matches && parseFloat(matches[1])) || 0;
@@ -0,0 +1,15 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {NPM_TAG} from '../npm-tag';
6
+
7
+ declare let __VERSION__: string;
8
+
9
+ /** Return this package's injected version, independent of other loaded versions. */
10
+ function getVersion(): string {
11
+ return typeof __VERSION__ === 'undefined' ? NPM_TAG : __VERSION__;
12
+ }
13
+
14
+ /** Version of this worker-utils build, or the branch's npm fallback tag. */
15
+ export const VERSION = getVersion();
@@ -0,0 +1,305 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ /* global importScripts */
6
+ import {isBrowser, isWorker} from '../env-utils/globals';
7
+ import {assert} from '../env-utils/assert';
8
+ import {VERSION} from '../env-utils/version';
9
+
10
+ export type LoadLibraryOptions<ModulesT extends Record<string, any> = Record<string, any>> = {
11
+ useLocalLibraries?: boolean;
12
+ CDN?: string | null;
13
+ modules?: ModulesT;
14
+ // Core must not be supplied
15
+ core?: never;
16
+ };
17
+
18
+ type ExtractableLoadLibraryOptions<ModulesT extends Record<string, any> = Record<string, any>> = {
19
+ useLocalLibraries?: boolean;
20
+ CDN?: string | null;
21
+ modules?: ModulesT;
22
+ core?: {
23
+ useLocalLibraries?: boolean;
24
+ CDN?: string | null;
25
+ } | null;
26
+ };
27
+
28
+ const loadLibraryPromises: Record<string, Promise<any>> = {}; // promises
29
+
30
+ export function extractLoadLibraryOptions<
31
+ ModulesT extends Record<string, any> = Record<string, any>
32
+ >(options: ExtractableLoadLibraryOptions<ModulesT> = {}): LoadLibraryOptions<ModulesT> {
33
+ const useLocalLibraries = options.useLocalLibraries ?? options.core?.useLocalLibraries;
34
+ const CDN = getCDNOption(options.CDN, options.core?.CDN);
35
+ const modules = options.modules;
36
+
37
+ return {
38
+ ...(useLocalLibraries !== undefined ? {useLocalLibraries} : {}),
39
+ ...(CDN !== undefined ? {CDN} : {}),
40
+ ...(modules !== undefined ? {modules} : {})
41
+ };
42
+ }
43
+
44
+ /**
45
+ * Extracts a valid CDN option from normalized or legacy loader options.
46
+ * @param cdn Deprecated top-level CDN option.
47
+ * @param coreCDN Nested core CDN option.
48
+ * @returns CDN option if it is a string or null.
49
+ */
50
+ function getCDNOption(cdn: unknown, coreCDN: unknown): string | null | undefined {
51
+ if (typeof cdn === 'string' || cdn === null) {
52
+ return cdn as string | null;
53
+ }
54
+ if (typeof coreCDN === 'string' || coreCDN === null) {
55
+ return coreCDN as string | null;
56
+ }
57
+ return undefined;
58
+ }
59
+
60
+ /**
61
+ * Dynamically loads a library ("module")
62
+ *
63
+ * - wasm library: Array buffer is returned
64
+ * - js library: Parse JS is returned
65
+ *
66
+ * Method depends on environment
67
+ * - browser - script element is created and installed on document
68
+ * - classic worker - importScripts loads a classic wrapper
69
+ * - module worker - native import loads an ESM wrapper and returns its exports
70
+ * - node - file is required
71
+ *
72
+ * @param libraryUrl
73
+ * @param moduleName
74
+ * @param options
75
+ * @param libraryName Classic asset name used for URL resolution and overrides.
76
+ * @param moduleLibraryName ESM asset name used by module workers unless the classic URL is overridden.
77
+ */
78
+ export async function loadLibrary(
79
+ libraryUrl: string,
80
+ moduleName: string | null = null,
81
+ options: LoadLibraryOptions = {},
82
+ libraryName: string | null = null,
83
+ moduleLibraryName: string | null = null
84
+ ): Promise<any> {
85
+ let moduleLibraryUrl = libraryUrl;
86
+ if (moduleName) {
87
+ const requestedLibraryName = libraryName || libraryUrl;
88
+ const injectedLibrary = options.modules?.[requestedLibraryName];
89
+ if (injectedLibrary !== undefined && typeof injectedLibrary !== 'string') {
90
+ return injectedLibrary;
91
+ }
92
+ libraryUrl = getLibraryUrl(libraryUrl, moduleName, options, libraryName);
93
+ const hasLibraryOverride = typeof injectedLibrary === 'string';
94
+ moduleLibraryUrl =
95
+ moduleLibraryName &&
96
+ (!hasLibraryOverride || typeof options.modules?.[moduleLibraryName] === 'string')
97
+ ? getLibraryUrl(moduleLibraryName, moduleName, options, moduleLibraryName)
98
+ : libraryUrl;
99
+ }
100
+ // Workers may select different ESM adapters for the same classic asset.
101
+ const cacheKey = getLibraryCacheKey(libraryUrl, moduleLibraryUrl, isWorker);
102
+
103
+ loadLibraryPromises[cacheKey] =
104
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
105
+ loadLibraryPromises[cacheKey] || loadLibraryFromFile(libraryUrl, moduleLibraryUrl);
106
+ return await loadLibraryPromises[cacheKey];
107
+ }
108
+
109
+ /** Identifies a library load, including the alternate adapter available in workers. */
110
+ export function getLibraryCacheKey(
111
+ libraryUrl: string,
112
+ moduleLibraryUrl: string,
113
+ worker: boolean
114
+ ): string {
115
+ return worker ? JSON.stringify([libraryUrl, moduleLibraryUrl]) : libraryUrl;
116
+ }
117
+
118
+ // TODO - sort out how to resolve paths for main/worker and dev/prod
119
+ export function getLibraryUrl(
120
+ library: string,
121
+ moduleName?: string,
122
+ options: LoadLibraryOptions = {},
123
+ libraryName: string | null = null
124
+ ): string {
125
+ if (options?.core) {
126
+ throw new Error('loadLibrary: options.core must be pre-normalized');
127
+ }
128
+ libraryName = libraryName || library;
129
+
130
+ // Allow application to import and supply libraries through `options.modules`
131
+ // TODO - See js-module-utils in loader-utils
132
+ const modules = options.modules || {};
133
+ if (typeof modules[libraryName] === 'string') {
134
+ return modules[libraryName];
135
+ }
136
+
137
+ // Check if already a URL after checking overrides so callers can replace
138
+ // default CDN URLs with locally bundled assets.
139
+ if (!options.useLocalLibraries && library.startsWith('http')) {
140
+ return library;
141
+ }
142
+
143
+ // Load from local files, not from CDN scripts in Node.js
144
+ // TODO - needs to locate the modules directory when installed!
145
+ if (!isBrowser) {
146
+ return `modules/${moduleName}/dist/libs/${libraryName}`;
147
+ }
148
+
149
+ // In browser, load from external scripts
150
+ if (options.CDN) {
151
+ if (typeof options.CDN !== 'string') {
152
+ throw new Error('loadLibrary: options.CDN must be a string or null');
153
+ }
154
+ assert(options.CDN.startsWith('http'));
155
+ return `${options.CDN}/${moduleName}@${VERSION}/dist/libs/${libraryName}`;
156
+ }
157
+
158
+ // TODO - loading inside workers requires paths relative to worker script location...
159
+ if (isWorker) {
160
+ return libraryName.endsWith('.mjs') ? `./libs/${libraryName}` : `../src/libs/${libraryName}`;
161
+ }
162
+
163
+ return `modules/${moduleName}/src/libs/${libraryName}`;
164
+ }
165
+
166
+ async function loadLibraryFromFile(libraryUrl: string, moduleLibraryUrl: string): Promise<any> {
167
+ if (/\.wasm(?:[?#]|$)/i.test(libraryUrl)) {
168
+ return await loadAsArrayBuffer(libraryUrl);
169
+ }
170
+
171
+ if (!isBrowser) {
172
+ // TODO - Node doesn't yet support dynamic import from https URLs
173
+ // try {
174
+ // return await import(libraryUrl);
175
+ // } catch (error) {
176
+ // console.error(error);
177
+ // }
178
+ const {requireFromFile} = globalThis.loaders || {};
179
+ try {
180
+ const result = await requireFromFile?.(libraryUrl);
181
+ if (result || !libraryUrl.includes('/dist/libs/')) {
182
+ return result;
183
+ }
184
+ return await requireFromFile?.(libraryUrl.replace('/dist/libs/', '/src/libs/'));
185
+ } catch (error) {
186
+ if (libraryUrl.includes('/dist/libs/')) {
187
+ try {
188
+ return await requireFromFile?.(libraryUrl.replace('/dist/libs/', '/src/libs/'));
189
+ } catch {
190
+ // ignore
191
+ }
192
+ }
193
+ // biome-ignore lint/suspicious/noConsole: Preserve upstream worker diagnostics.
194
+ console.error(error); // eslint-disable-line no-console
195
+ return null;
196
+ }
197
+ }
198
+ if (isWorker) {
199
+ return loadLibraryInWorker(libraryUrl, moduleLibraryUrl);
200
+ }
201
+ // TODO - fix - should be more secure than string parsing since observes CORS
202
+ // if (isBrowser) {
203
+ // return await loadScriptFromFile(libraryUrl);
204
+ // }
205
+
206
+ const scriptSource = await loadAsText(libraryUrl);
207
+ return loadLibraryFromString(scriptSource, libraryUrl);
208
+ }
209
+
210
+ /** Loads classic scripts or native ESM exports without evaluating fetched source or masking failures. */
211
+ export async function loadLibraryInWorker(
212
+ libraryUrl: string,
213
+ moduleLibraryUrl: string = libraryUrl
214
+ ): Promise<unknown> {
215
+ try {
216
+ // A zero-argument probe rejects in module workers without loading any script.
217
+ importScripts();
218
+ } catch {
219
+ // Resolve local asset paths against the same worker location used by importScripts.
220
+ const moduleUrl = new URL(moduleLibraryUrl, globalThis.location.href).href;
221
+ const library = await import(/* webpackIgnore: true */ /* @vite-ignore */ moduleUrl);
222
+ return library.default ?? library;
223
+ }
224
+ return importScripts(libraryUrl);
225
+ }
226
+
227
+ /** Installs fetched library source on the browser main thread. Workers use script or module loading. */
228
+ function loadLibraryFromString(scriptSource: string, id: string): null | any {
229
+ const script = document.createElement('script');
230
+ script.id = id;
231
+ // most browsers like a separate text node but some throw an error. The second method covers those.
232
+ try {
233
+ script.appendChild(document.createTextNode(scriptSource));
234
+ } catch (_e) {
235
+ script.text = scriptSource;
236
+ }
237
+ document.body.appendChild(script);
238
+ return null;
239
+ }
240
+
241
+ async function loadAsArrayBuffer(url: string): Promise<ArrayBuffer> {
242
+ const {readFileAsArrayBuffer} = globalThis.loaders || {};
243
+ if (isBrowser || !readFileAsArrayBuffer || url.startsWith('http')) {
244
+ const response = await fetch(url);
245
+ if (!response.ok) throw new Error(`Failed to load ${url}: HTTP ${response.status}`);
246
+ return await response.arrayBuffer();
247
+ }
248
+ try {
249
+ return await readFileAsArrayBuffer(url);
250
+ } catch {
251
+ if (url.includes('/dist/libs/')) {
252
+ return await readFileAsArrayBuffer(url.replace('/dist/libs/', '/src/libs/'));
253
+ }
254
+ throw new Error(`Failed to load ArrayBuffer from ${url}`);
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Load a file from local file system
260
+ * @param filename
261
+ * @returns
262
+ */
263
+ async function loadAsText(url: string): Promise<string> {
264
+ const {readFileAsText} = globalThis.loaders || {};
265
+ if (isBrowser || !readFileAsText || url.startsWith('http')) {
266
+ const response = await fetch(url);
267
+ return await response.text();
268
+ }
269
+ try {
270
+ return await readFileAsText(url);
271
+ } catch {
272
+ if (url.includes('/dist/libs/')) {
273
+ return await readFileAsText(url.replace('/dist/libs/', '/src/libs/'));
274
+ }
275
+ throw new Error(`Failed to load text from ${url}`);
276
+ }
277
+ }
278
+
279
+ /*
280
+ async function loadScriptFromFile(libraryUrl) {
281
+ const script = document.createElement('script');
282
+ script.src = libraryUrl;
283
+ return await new Promise((resolve, reject) => {
284
+ script.onload = data => {
285
+ resolve(data);
286
+ };
287
+ script.onerror = reject;
288
+ });
289
+ }
290
+ */
291
+
292
+ // TODO - technique for module injection into worker, from THREE.DracoLoader...
293
+ /*
294
+ function combineWorkerWithLibrary(worker, jsContent) {
295
+ var fn = wWorker.toString();
296
+ var body = [
297
+ '// injected',
298
+ jsContent,
299
+ '',
300
+ '// worker',
301
+ fn.substring(fn.indexOf('{') + 1, fn.lastIndexOf('}'))
302
+ ].join('\n');
303
+ this.workerSourceURL = URL.createObjectURL(new Blob([body]));
304
+ }
305
+ */
@@ -0,0 +1,44 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ /** Browser polyfill for Node.js built-in `worker_threads` module.
6
+ * These fills are non-functional, and just intended to ensure that
7
+ * `import 'worker_threads` doesn't break browser builds.
8
+ * The replacement is done in package.json browser field
9
+ */
10
+ export class NodeWorker {
11
+ /** Restores the native worker constructor when this browser shim is embedded in a Node bundle. */
12
+ constructor(...arguments_: unknown[]) {
13
+ const runtimeProcess = (
14
+ globalThis as {
15
+ process?: {
16
+ getBuiltinModule?: (specifier: string) => {
17
+ Worker?: new (...workerArguments: unknown[]) => NodeWorker;
18
+ };
19
+ };
20
+ }
21
+ ).process;
22
+ const WorkerConstructor = runtimeProcess?.getBuiltinModule?.('node:worker_threads').Worker;
23
+ if (WorkerConstructor) {
24
+ return new WorkerConstructor(...arguments_);
25
+ }
26
+ }
27
+
28
+ /** No-op browser fallback matching the native Worker API. */
29
+ terminate() {}
30
+ }
31
+
32
+ export type {NodeWorker as NodeWorkerType};
33
+
34
+ /** Restores the native parent port when a browser-targeted CJS bundle runs in Node. */
35
+ export const parentPort =
36
+ (
37
+ globalThis as {
38
+ process?: {
39
+ getBuiltinModule?: (specifier: string) => {
40
+ parentPort?: import('./worker_threads').ParentPort | null;
41
+ };
42
+ };
43
+ }
44
+ ).process?.getBuiltinModule?.('node:worker_threads')?.parentPort ?? null;
@@ -0,0 +1,21 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {Worker, parentPort as nodeParentPort} from 'worker_threads';
6
+
7
+ /** The runtime surface used by the worker farm, without Node-only public types. */
8
+ export type NodeWorkerType = {
9
+ postMessage(value: unknown, transferList?: any[]): void;
10
+ terminate(): unknown;
11
+ on(event: string, listener: (...args: any[]) => void): unknown;
12
+ ref(): unknown;
13
+ unref(): unknown;
14
+ };
15
+ export type ParentPort = {
16
+ postMessage(value: unknown, transferList?: any[]): void;
17
+ on(event: string, listener: (...args: any[]) => void): unknown;
18
+ off(event: string, listener: (...args: any[]) => void): unknown;
19
+ };
20
+ export const parentPort: ParentPort | null = nodeParentPort as ParentPort | null;
21
+ export const NodeWorker: new (filename: string | URL, options?: any) => NodeWorkerType = Worker;
@@ -0,0 +1,5 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ export const NPM_TAG = 'latest';
@@ -0,0 +1,23 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import type {ChildProcessProxyProps} from './child-process-proxy';
6
+
7
+ export default class ChildProcessProxy {
8
+ constructor() {
9
+ throw new Error('ChildProcessProxy is not available in browser environments');
10
+ }
11
+
12
+ async start(props?: ChildProcessProxyProps): Promise<object> {
13
+ return await Promise.resolve({});
14
+ }
15
+
16
+ async stop(): Promise<void> {
17
+ return await Promise.resolve();
18
+ }
19
+
20
+ async exit(): Promise<void> {
21
+ return await Promise.resolve();
22
+ }
23
+ }