@loaders.gl/scientific 5.0.0-beta.2 → 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.
- package/README.md +6 -2
- package/dist/index.cjs +48 -4
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/netcdf/netcdf-loader.cjs +1 -1
- package/dist/netcdf/netcdf-loader.js +1 -1
- package/dist/netcdf/netcdf-source-loader-types.js +1 -1
- package/dist/netcdf/netcdf-source-loader.js +1 -1
- package/dist/netcdf.min.js +2 -2
- package/dist/npy-loader-types.cjs +1 -1
- package/dist/npy-loader-types.js +1 -1
- package/dist/npy-loader.cjs +1 -1
- package/dist/npy-worker.js +574 -39
- package/dist/nrrd/parse-nrrd.d.ts +22 -0
- package/dist/nrrd/parse-nrrd.d.ts.map +1 -0
- package/dist/nrrd/parse-nrrd.js +228 -0
- package/dist/nrrd/parse-nrrd.js.map +1 -0
- package/dist/nrrd-loader-types.cjs +58 -0
- package/dist/nrrd-loader-types.cjs.map +7 -0
- package/dist/nrrd-loader-types.d.ts +31 -0
- package/dist/nrrd-loader-types.d.ts.map +1 -0
- package/dist/nrrd-loader-types.js +25 -0
- package/dist/nrrd-loader-types.js.map +1 -0
- package/dist/nrrd-loader.cjs +252 -0
- package/dist/nrrd-loader.cjs.map +6 -0
- package/dist/nrrd-loader.d.ts +16 -0
- package/dist/nrrd-loader.d.ts.map +1 -0
- package/dist/nrrd-loader.js +8 -0
- package/dist/nrrd-loader.js.map +1 -0
- package/dist/vtk/index.cjs +26 -3
- package/dist/vtk/index.cjs.map +2 -2
- package/dist/vtk/vtk.d.ts.map +1 -1
- package/dist/vtk/vtk.js +19 -1
- package/dist/vtk/vtk.js.map +1 -1
- package/dist/vtk-loader-types.cjs +58 -0
- package/dist/vtk-loader-types.cjs.map +7 -0
- package/dist/vtk-loader-types.d.ts +34 -0
- package/dist/vtk-loader-types.d.ts.map +1 -0
- package/dist/vtk-loader-types.js +25 -0
- package/dist/vtk-loader-types.js.map +1 -0
- package/dist/vtk-loader.cjs +384 -0
- package/dist/vtk-loader.cjs.map +6 -0
- package/dist/vtk-loader.d.ts +21 -0
- package/dist/vtk-loader.d.ts.map +1 -0
- package/dist/vtk-loader.js +21 -0
- package/dist/vtk-loader.js.map +1 -0
- package/dist/zarr/geo-zarr-source-loader.js +1 -1
- package/dist/zarr/index.cjs +1 -1
- package/dist/zarr/index.cjs.map +1 -1
- package/dist/zarr/lib/spatial-multiscales.js +1 -1
- package/dist/zarr/lib/spatial-multiscales.js.map +1 -1
- package/dist/zarr/ome-zarr-source-loader.js +1 -1
- package/dist/zarr/zarr-loader-types.cjs +1 -1
- package/dist/zarr/zarr-loader-types.js +1 -1
- package/dist/zarr.dev.js +1 -1
- package/dist/zarr.min.js +3 -3
- package/package.json +19 -7
- package/src/index.ts +7 -1
- package/src/nrrd/parse-nrrd.ts +267 -0
- package/src/nrrd-loader-types.ts +25 -0
- package/src/nrrd-loader.ts +7 -0
- package/src/vtk/vtk.ts +28 -1
- package/src/vtk-loader-types.ts +25 -0
- package/src/vtk-loader.ts +19 -0
- package/src/zarr/lib/spatial-multiscales.ts +1 -1
package/dist/npy-worker.js
CHANGED
|
@@ -108,7 +108,7 @@
|
|
|
108
108
|
"src/npy-loader-types.ts"() {
|
|
109
109
|
"use strict";
|
|
110
110
|
init_npy_format();
|
|
111
|
-
VERSION = true ? "5.0.0-beta.
|
|
111
|
+
VERSION = true ? "5.0.0-beta.5" : "latest";
|
|
112
112
|
NPY_MAGIC_NUMBER = new Uint8Array([147, 78, 85, 77, 80, 89]);
|
|
113
113
|
NPYLoader = {
|
|
114
114
|
...NPYFormat,
|
|
@@ -150,13 +150,40 @@
|
|
|
150
150
|
}
|
|
151
151
|
});
|
|
152
152
|
|
|
153
|
+
// ../core/src/worker-utils/lib/node/worker_threads-browser.ts
|
|
154
|
+
var worker_threads_browser_exports = {};
|
|
155
|
+
__export(worker_threads_browser_exports, {
|
|
156
|
+
NodeWorker: () => NodeWorker,
|
|
157
|
+
parentPort: () => parentPort2
|
|
158
|
+
});
|
|
159
|
+
var NodeWorker, parentPort2;
|
|
160
|
+
var init_worker_threads_browser = __esm({
|
|
161
|
+
"../core/src/worker-utils/lib/node/worker_threads-browser.ts"() {
|
|
162
|
+
"use strict";
|
|
163
|
+
NodeWorker = class {
|
|
164
|
+
/** Restores the native worker constructor when this browser shim is embedded in a Node bundle. */
|
|
165
|
+
constructor(...arguments_) {
|
|
166
|
+
const runtimeProcess = globalThis.process;
|
|
167
|
+
const WorkerConstructor = runtimeProcess?.getBuiltinModule?.("node:worker_threads").Worker;
|
|
168
|
+
if (WorkerConstructor) {
|
|
169
|
+
return new WorkerConstructor(...arguments_);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** No-op browser fallback matching the native Worker API. */
|
|
173
|
+
terminate() {
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
parentPort2 = null;
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
153
180
|
// src/workers/npy-worker.ts
|
|
154
181
|
init_npy_loader();
|
|
155
182
|
|
|
156
|
-
// ../../node_modules/@math.gl/
|
|
157
|
-
var parentPort = null;
|
|
183
|
+
// ../../node_modules/@math.gl/tasks/dist/worker-utils/lib/node/worker_threads-browser.js
|
|
184
|
+
var parentPort = globalThis.process?.getBuiltinModule?.("node:worker_threads")?.parentPort ?? null;
|
|
158
185
|
|
|
159
|
-
// ../../node_modules/@math.gl/
|
|
186
|
+
// ../../node_modules/@math.gl/tasks/dist/worker-utils/lib/worker-utils/get-transfer-list.js
|
|
160
187
|
function getTransferList(object, recursive = true, transfers) {
|
|
161
188
|
const transfersSet = transfers || /* @__PURE__ */ new Set();
|
|
162
189
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -169,6 +196,9 @@
|
|
|
169
196
|
else if (ArrayBuffer.isView(value)) {
|
|
170
197
|
if (isTransferable(value.buffer))
|
|
171
198
|
transfersSet.add(value.buffer);
|
|
199
|
+
} else if (recursive && typeof MessageChannel !== "undefined" && value instanceof MessageChannel) {
|
|
200
|
+
visit(value.port1);
|
|
201
|
+
visit(value.port2);
|
|
172
202
|
} else if (recursive && value instanceof Map) {
|
|
173
203
|
for (const [key, item] of value) {
|
|
174
204
|
visit(key);
|
|
@@ -204,7 +234,7 @@
|
|
|
204
234
|
return false;
|
|
205
235
|
}
|
|
206
236
|
|
|
207
|
-
// ../../node_modules/@math.gl/
|
|
237
|
+
// ../../node_modules/@math.gl/tasks/dist/worker-utils/lib/worker-farm/worker-body.js
|
|
208
238
|
async function getParentPort() {
|
|
209
239
|
return parentPort;
|
|
210
240
|
}
|
|
@@ -220,16 +250,16 @@
|
|
|
220
250
|
*/
|
|
221
251
|
static set onmessage(onMessage) {
|
|
222
252
|
async function handleMessage(message) {
|
|
223
|
-
const
|
|
224
|
-
const { type, payload } =
|
|
253
|
+
const parentPort3 = await getParentPort();
|
|
254
|
+
const { type, payload } = parentPort3 ? message : message.data;
|
|
225
255
|
onMessage(type, payload);
|
|
226
256
|
}
|
|
227
|
-
getParentPort().then((
|
|
228
|
-
if (
|
|
229
|
-
|
|
257
|
+
getParentPort().then((parentPort3) => {
|
|
258
|
+
if (parentPort3) {
|
|
259
|
+
parentPort3.on("message", (message) => {
|
|
230
260
|
handleMessage(message);
|
|
231
261
|
});
|
|
232
|
-
|
|
262
|
+
parentPort3.on("exit", () => console.debug("Node worker closing"));
|
|
233
263
|
} else {
|
|
234
264
|
globalThis.onmessage = handleMessage;
|
|
235
265
|
}
|
|
@@ -247,9 +277,9 @@
|
|
|
247
277
|
};
|
|
248
278
|
onMessageWrapperMap.set(onMessage, onMessageWrapper);
|
|
249
279
|
}
|
|
250
|
-
const
|
|
251
|
-
if (
|
|
252
|
-
|
|
280
|
+
const parentPort3 = await getParentPort();
|
|
281
|
+
if (parentPort3) {
|
|
282
|
+
parentPort3.on("message", onMessageWrapper);
|
|
253
283
|
} else {
|
|
254
284
|
globalThis.addEventListener("message", onMessageWrapper);
|
|
255
285
|
}
|
|
@@ -260,9 +290,9 @@
|
|
|
260
290
|
if (!onMessageWrapper) {
|
|
261
291
|
return;
|
|
262
292
|
}
|
|
263
|
-
const
|
|
264
|
-
if (
|
|
265
|
-
|
|
293
|
+
const parentPort3 = await getParentPort();
|
|
294
|
+
if (parentPort3) {
|
|
295
|
+
parentPort3.off("message", onMessageWrapper);
|
|
266
296
|
} else {
|
|
267
297
|
globalThis.removeEventListener("message", onMessageWrapper);
|
|
268
298
|
}
|
|
@@ -272,12 +302,12 @@
|
|
|
272
302
|
* @param type
|
|
273
303
|
* @param payload
|
|
274
304
|
*/
|
|
275
|
-
static async postMessage(type, payload) {
|
|
305
|
+
static async postMessage(type, payload, copy = false) {
|
|
276
306
|
const data = { source: "loaders.gl", type, payload };
|
|
277
|
-
const transferList = getTransferList(payload);
|
|
278
|
-
const
|
|
279
|
-
if (
|
|
280
|
-
|
|
307
|
+
const transferList = copy ? [] : getTransferList(payload);
|
|
308
|
+
const parentPort3 = await getParentPort();
|
|
309
|
+
if (parentPort3) {
|
|
310
|
+
parentPort3.postMessage(data, transferList);
|
|
281
311
|
} else {
|
|
282
312
|
globalThis.postMessage(data, transferList);
|
|
283
313
|
}
|
|
@@ -291,7 +321,7 @@
|
|
|
291
321
|
return messageData && typeof messageData.source === "string" && messageData.source.startsWith("loaders.gl") ? messageData : null;
|
|
292
322
|
}
|
|
293
323
|
|
|
294
|
-
// ../../node_modules/@math.gl/
|
|
324
|
+
// ../../node_modules/@math.gl/tasks/dist/worker-utils/lib/async-queue/async-queue.js
|
|
295
325
|
var AsyncQueue = class {
|
|
296
326
|
constructor() {
|
|
297
327
|
this._values = [];
|
|
@@ -358,7 +388,7 @@
|
|
|
358
388
|
}
|
|
359
389
|
};
|
|
360
390
|
|
|
361
|
-
// ../../node_modules/@math.gl/
|
|
391
|
+
// ../../node_modules/@math.gl/tasks/dist/worker-utils/lib/worker-api/shared-state.js
|
|
362
392
|
var SharedWorkerState = class {
|
|
363
393
|
constructor(buffer) {
|
|
364
394
|
if (typeof SharedArrayBuffer === "undefined" || !(buffer instanceof SharedArrayBuffer) || buffer.byteLength !== 16)
|
|
@@ -397,12 +427,19 @@
|
|
|
397
427
|
}
|
|
398
428
|
};
|
|
399
429
|
|
|
400
|
-
// ../../node_modules/@math.gl/
|
|
430
|
+
// ../../node_modules/@math.gl/tasks/dist/worker-utils/lib/worker-api/validate-output-ownership.js
|
|
431
|
+
function validateOutputOwnership(ownership) {
|
|
432
|
+
if (ownership !== void 0 && ownership !== "copy" && ownership !== "transfer") {
|
|
433
|
+
throw new Error("Invalid worker output ownership");
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// ../../node_modules/@math.gl/tasks/dist/worker-utils/lib/worker-api/create-worker.js
|
|
401
438
|
var requestId = 0;
|
|
402
439
|
var activeController = null;
|
|
403
440
|
var activeInputBatches = null;
|
|
404
441
|
var activeOutputAcknowledgements = null;
|
|
405
|
-
async function createWorker(
|
|
442
|
+
async function createWorker(process2, processInBatches, preload2) {
|
|
406
443
|
if (!await WorkerBody.inWorkerThread()) {
|
|
407
444
|
return;
|
|
408
445
|
}
|
|
@@ -443,23 +480,26 @@
|
|
|
443
480
|
}
|
|
444
481
|
switch (type) {
|
|
445
482
|
case "preload":
|
|
483
|
+
await preload2?.();
|
|
446
484
|
WorkerBody.postMessage("done", {});
|
|
447
485
|
break;
|
|
448
486
|
case "process":
|
|
449
487
|
throwIfAborted();
|
|
450
|
-
|
|
488
|
+
validateOutputOwnership(payload.options?.["outputOwnership"]);
|
|
489
|
+
if (!process2) {
|
|
451
490
|
throw new Error("Worker does not support atomic processing");
|
|
452
491
|
}
|
|
453
|
-
const result = await
|
|
492
|
+
const result = await process2(payload.input, payload.options || {}, jobContext, payload.context || {});
|
|
454
493
|
throwIfAborted();
|
|
455
494
|
reporter?.finish();
|
|
456
495
|
await WorkerBody.postMessage("done", {
|
|
457
496
|
result,
|
|
458
497
|
executionMs: performance.now() - startedAt
|
|
459
|
-
});
|
|
498
|
+
}, payload.options?.["outputOwnership"] === "copy");
|
|
460
499
|
break;
|
|
461
500
|
case "process-in-batches":
|
|
462
501
|
throwIfAborted();
|
|
502
|
+
validateOutputOwnership(payload.options?.["outputOwnership"]);
|
|
463
503
|
if (!processInBatches) {
|
|
464
504
|
throw new Error("Worker does not support batched processing");
|
|
465
505
|
}
|
|
@@ -471,7 +511,7 @@
|
|
|
471
511
|
const resultIterator = processInBatches(createDemandDrivenIterator(inputBatches), payload.options || {}, jobContext, payload.context || {});
|
|
472
512
|
for await (const batch of resultIterator) {
|
|
473
513
|
throwIfAborted();
|
|
474
|
-
await WorkerBody.postMessage("output-batch", { result: batch });
|
|
514
|
+
await WorkerBody.postMessage("output-batch", { result: batch }, payload.options?.["outputOwnership"] === "copy");
|
|
475
515
|
await outputAcknowledgements.next();
|
|
476
516
|
}
|
|
477
517
|
throwIfAborted();
|
|
@@ -569,14 +609,14 @@
|
|
|
569
609
|
});
|
|
570
610
|
}
|
|
571
611
|
function createProgressReporter() {
|
|
572
|
-
let
|
|
612
|
+
let pending2;
|
|
573
613
|
let finished = false;
|
|
574
614
|
let scheduled = false;
|
|
575
615
|
const flush = () => {
|
|
576
616
|
scheduled = false;
|
|
577
|
-
if (
|
|
578
|
-
WorkerBody.postMessage("progress", { progress:
|
|
579
|
-
|
|
617
|
+
if (pending2) {
|
|
618
|
+
WorkerBody.postMessage("progress", { progress: pending2 });
|
|
619
|
+
pending2 = void 0;
|
|
580
620
|
}
|
|
581
621
|
};
|
|
582
622
|
return {
|
|
@@ -584,9 +624,9 @@
|
|
|
584
624
|
report(progress) {
|
|
585
625
|
if (finished)
|
|
586
626
|
return;
|
|
587
|
-
if (
|
|
627
|
+
if (pending2 && pending2.phase !== progress.phase)
|
|
588
628
|
flush();
|
|
589
|
-
|
|
629
|
+
pending2 = { ...progress };
|
|
590
630
|
if (!scheduled) {
|
|
591
631
|
scheduled = true;
|
|
592
632
|
setTimeout(flush, 0);
|
|
@@ -595,21 +635,516 @@
|
|
|
595
635
|
/** Flushes final progress and suppresses reports from stale callbacks. */
|
|
596
636
|
finish(discard = false) {
|
|
597
637
|
if (discard)
|
|
598
|
-
|
|
638
|
+
pending2 = void 0;
|
|
599
639
|
flush();
|
|
600
640
|
finished = true;
|
|
601
641
|
}
|
|
602
642
|
};
|
|
603
643
|
}
|
|
604
644
|
|
|
605
|
-
// ../../node_modules/@math.gl/
|
|
645
|
+
// ../../node_modules/@math.gl/tasks/dist/worker-utils/index.js
|
|
606
646
|
var import_meta = {};
|
|
607
647
|
var path = typeof __filename === "string" ? __filename.replace(/\\/g, "/") : "";
|
|
608
648
|
var moduleUrl = path ? `file://${path.startsWith("/") ? "" : "/"}${encodeURI(path).replace(/#/g, "%23").replace(/\?/g, "%3F")}` : import_meta.url;
|
|
609
649
|
|
|
650
|
+
// ../../node_modules/@math.gl/tasks/dist/tasks/task-preload.js
|
|
651
|
+
var pending = /* @__PURE__ */ new WeakMap();
|
|
652
|
+
var loaded = /* @__PURE__ */ new WeakSet();
|
|
653
|
+
function preloadTaskCode(task, signal) {
|
|
654
|
+
if (!task.preload)
|
|
655
|
+
return Promise.resolve();
|
|
656
|
+
let promise = pending.get(task);
|
|
657
|
+
if (!promise) {
|
|
658
|
+
promise = Promise.resolve().then(() => task.preload()).then(() => {
|
|
659
|
+
loaded.add(task);
|
|
660
|
+
});
|
|
661
|
+
pending.set(task, promise);
|
|
662
|
+
void promise.catch(() => pending.delete(task));
|
|
663
|
+
}
|
|
664
|
+
return withSignal(promise, signal);
|
|
665
|
+
}
|
|
666
|
+
function withSignal(operation, signal) {
|
|
667
|
+
if (!signal)
|
|
668
|
+
return operation;
|
|
669
|
+
return new Promise((resolve, reject) => {
|
|
670
|
+
const abort = () => {
|
|
671
|
+
signal.removeEventListener("abort", abort);
|
|
672
|
+
reject(signal.reason);
|
|
673
|
+
};
|
|
674
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
675
|
+
if (signal.aborted)
|
|
676
|
+
abort();
|
|
677
|
+
operation.then((value) => {
|
|
678
|
+
signal.removeEventListener("abort", abort);
|
|
679
|
+
resolve(value);
|
|
680
|
+
}, (error) => {
|
|
681
|
+
signal.removeEventListener("abort", abort);
|
|
682
|
+
reject(error);
|
|
683
|
+
});
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// ../../node_modules/@math.gl/tasks/dist/tasks/task-channel.js
|
|
688
|
+
var TaskRequestError = class extends Error {
|
|
689
|
+
};
|
|
690
|
+
var TaskChannel = class {
|
|
691
|
+
constructor(options) {
|
|
692
|
+
this.options = options;
|
|
693
|
+
this.nextId = 1;
|
|
694
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
695
|
+
this.incoming = /* @__PURE__ */ new Map();
|
|
696
|
+
}
|
|
697
|
+
/** Send a request without serializing other in-flight requests. */
|
|
698
|
+
request(operation, value, signal, transfer = []) {
|
|
699
|
+
if (this.failure)
|
|
700
|
+
return Promise.reject(this.failure.reason);
|
|
701
|
+
if (signal?.aborted)
|
|
702
|
+
return Promise.reject(signal.reason);
|
|
703
|
+
const id = this.nextId++;
|
|
704
|
+
return new Promise((resolve, reject) => {
|
|
705
|
+
const abort = () => {
|
|
706
|
+
const pending2 = this.pending.get(id);
|
|
707
|
+
if (!pending2)
|
|
708
|
+
return;
|
|
709
|
+
this.pending.delete(id);
|
|
710
|
+
pending2.cleanup();
|
|
711
|
+
reject(signal?.reason);
|
|
712
|
+
this.send({ source: "math.gl/tasks", kind: "cancel", id });
|
|
713
|
+
};
|
|
714
|
+
this.pending.set(id, {
|
|
715
|
+
resolve,
|
|
716
|
+
reject,
|
|
717
|
+
cleanup: () => signal?.removeEventListener("abort", abort)
|
|
718
|
+
});
|
|
719
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
720
|
+
this.send({ source: "math.gl/tasks", kind: "request", id, operation, value }, transfer);
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
/** Send a one-way notification. */
|
|
724
|
+
emit(operation, value, transfer = []) {
|
|
725
|
+
if (this.failure)
|
|
726
|
+
throw this.failure.reason;
|
|
727
|
+
this.send({ source: "math.gl/tasks", kind: "event", id: 0, operation, value }, transfer);
|
|
728
|
+
}
|
|
729
|
+
/** Dispatch a transport message; unrelated protocols are ignored. */
|
|
730
|
+
receive(message) {
|
|
731
|
+
if (this.failure || message?.source !== "math.gl/tasks")
|
|
732
|
+
return;
|
|
733
|
+
if (message.kind === "failure") {
|
|
734
|
+
const reason = new Error(message.error?.message ?? "Task channel failed");
|
|
735
|
+
this.close(reason);
|
|
736
|
+
this.options.onFailure?.(reason);
|
|
737
|
+
} else if (message.kind === "result" || message.kind === "error") {
|
|
738
|
+
const pending2 = this.pending.get(message.id);
|
|
739
|
+
if (!pending2)
|
|
740
|
+
return;
|
|
741
|
+
this.pending.delete(message.id);
|
|
742
|
+
pending2.cleanup();
|
|
743
|
+
if (message.kind === "result")
|
|
744
|
+
pending2.resolve(message.value);
|
|
745
|
+
else {
|
|
746
|
+
const error = new TaskRequestError(message.error?.message ?? "Task request failed");
|
|
747
|
+
error.name = message.error?.name ?? "Error";
|
|
748
|
+
pending2.reject(error);
|
|
749
|
+
}
|
|
750
|
+
} else if (message.kind === "cancel") {
|
|
751
|
+
this.incoming.get(message.id)?.abort(new DOMException("Task request cancelled", "AbortError"));
|
|
752
|
+
} else if (message.kind === "event") {
|
|
753
|
+
try {
|
|
754
|
+
this.options.handleEvent?.(message.operation, message.value);
|
|
755
|
+
} catch (error) {
|
|
756
|
+
this.fail(error);
|
|
757
|
+
}
|
|
758
|
+
} else if (message.kind === "request") {
|
|
759
|
+
if (this.incoming.has(message.id)) {
|
|
760
|
+
this.fail(new Error("Duplicate task request identifier"));
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
const controller = new AbortController();
|
|
764
|
+
this.incoming.set(message.id, controller);
|
|
765
|
+
void Promise.resolve().then(() => {
|
|
766
|
+
controller.signal.throwIfAborted();
|
|
767
|
+
return this.options.handleRequest(message.operation, message.value, controller.signal);
|
|
768
|
+
}).then((value) => {
|
|
769
|
+
if (!controller.signal.aborted && !this.failure) {
|
|
770
|
+
this.send({ source: "math.gl/tasks", kind: "result", id: message.id, value }, this.options.getResultTransferList?.(value, message.value));
|
|
771
|
+
}
|
|
772
|
+
}).catch((reason) => {
|
|
773
|
+
if (!controller.signal.aborted && !this.failure) {
|
|
774
|
+
this.send({
|
|
775
|
+
source: "math.gl/tasks",
|
|
776
|
+
kind: "error",
|
|
777
|
+
id: message.id,
|
|
778
|
+
error: {
|
|
779
|
+
name: reason instanceof Error ? reason.name : "Error",
|
|
780
|
+
message: reason instanceof Error ? reason.message : String(reason)
|
|
781
|
+
}
|
|
782
|
+
});
|
|
783
|
+
}
|
|
784
|
+
}).finally(() => this.incoming.delete(message.id));
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
/** Reject all outstanding calls with a common cause and abort incoming callbacks. */
|
|
788
|
+
close(reason = new Error("Task channel closed")) {
|
|
789
|
+
if (this.failure)
|
|
790
|
+
return;
|
|
791
|
+
this.failure = { reason };
|
|
792
|
+
for (const pending2 of this.pending.values()) {
|
|
793
|
+
pending2.cleanup();
|
|
794
|
+
pending2.reject(reason);
|
|
795
|
+
}
|
|
796
|
+
this.pending.clear();
|
|
797
|
+
for (const controller of this.incoming.values())
|
|
798
|
+
controller.abort(reason);
|
|
799
|
+
this.incoming.clear();
|
|
800
|
+
}
|
|
801
|
+
send(message, transfer = []) {
|
|
802
|
+
if (this.failure)
|
|
803
|
+
return;
|
|
804
|
+
try {
|
|
805
|
+
this.options.send(message, transfer);
|
|
806
|
+
} catch (error) {
|
|
807
|
+
this.fail(error);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
/** Invalidate the channel and notify the peer when the transport still works. */
|
|
811
|
+
fail(reason) {
|
|
812
|
+
if (this.failure)
|
|
813
|
+
return;
|
|
814
|
+
this.close(reason);
|
|
815
|
+
try {
|
|
816
|
+
this.options.send({
|
|
817
|
+
source: "math.gl/tasks",
|
|
818
|
+
kind: "failure",
|
|
819
|
+
id: 0,
|
|
820
|
+
error: {
|
|
821
|
+
name: reason instanceof Error ? reason.name : "Error",
|
|
822
|
+
message: reason instanceof Error ? reason.message : String(reason)
|
|
823
|
+
}
|
|
824
|
+
}, []);
|
|
825
|
+
} catch {
|
|
826
|
+
}
|
|
827
|
+
this.options.onFailure?.(reason);
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
// ../../node_modules/@math.gl/tasks/dist/tasks/task-runtime.js
|
|
832
|
+
function assertPayload(value) {
|
|
833
|
+
if (!value || typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype) {
|
|
834
|
+
throw new TypeError("Task payloads must be plain objects with named fields");
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
function clonePayload(value, ownership = "copy") {
|
|
838
|
+
assertPayload(value);
|
|
839
|
+
if (!["copy", "transfer", "share"].includes(ownership))
|
|
840
|
+
throw new TypeError("Invalid input ownership");
|
|
841
|
+
const transfer = getTransferList(value);
|
|
842
|
+
if (ownership === "share" && transfer.length)
|
|
843
|
+
throw new TypeError("Share requires shared storage");
|
|
844
|
+
return structuredClone(value, {
|
|
845
|
+
transfer: ownership === "transfer" ? transfer : []
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
function taskContext(state, context = {}) {
|
|
849
|
+
const signal = context.signal ?? new AbortController().signal;
|
|
850
|
+
return {
|
|
851
|
+
state,
|
|
852
|
+
signal,
|
|
853
|
+
requestHost: async (operation, payload, controls) => {
|
|
854
|
+
signal.throwIfAborted();
|
|
855
|
+
if (!context.requestHost)
|
|
856
|
+
throw new Error("No host request handler configured");
|
|
857
|
+
return context.requestHost(operation, payload, controls);
|
|
858
|
+
},
|
|
859
|
+
emitHost: (operation, payload) => {
|
|
860
|
+
signal.throwIfAborted();
|
|
861
|
+
context.emitHost?.(operation, payload);
|
|
862
|
+
},
|
|
863
|
+
throwIfAborted: () => {
|
|
864
|
+
signal.throwIfAborted();
|
|
865
|
+
context.throwIfAborted?.();
|
|
866
|
+
},
|
|
867
|
+
reportProgress: (progress) => context.reportProgress?.({
|
|
868
|
+
...progress,
|
|
869
|
+
phase: progress.phase ?? "process",
|
|
870
|
+
unit: progress.unit ?? "items"
|
|
871
|
+
})
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
var TaskRuntime = class {
|
|
875
|
+
constructor(task) {
|
|
876
|
+
this.task = task;
|
|
877
|
+
this.initialized = false;
|
|
878
|
+
}
|
|
879
|
+
async initialize(input, context = {}) {
|
|
880
|
+
if (this.initialized)
|
|
881
|
+
throw new Error("Task is already initialized");
|
|
882
|
+
assertPayload(input);
|
|
883
|
+
await preloadTaskCode(this.task, context.signal);
|
|
884
|
+
context.signal?.throwIfAborted();
|
|
885
|
+
this.state = await this.task.initialize?.(input, taskContext(void 0, context));
|
|
886
|
+
this.initialized = true;
|
|
887
|
+
return {};
|
|
888
|
+
}
|
|
889
|
+
async destroy() {
|
|
890
|
+
if (this.initialized) {
|
|
891
|
+
this.initialized = false;
|
|
892
|
+
const state = this.state;
|
|
893
|
+
this.state = void 0;
|
|
894
|
+
if (this.task.initialize)
|
|
895
|
+
await this.task.destroy?.(state);
|
|
896
|
+
}
|
|
897
|
+
return {};
|
|
898
|
+
}
|
|
899
|
+
async process(input, options, context = {}) {
|
|
900
|
+
if (!this.initialized)
|
|
901
|
+
throw new Error("Task is not initialized");
|
|
902
|
+
if (!this.task.process)
|
|
903
|
+
throw new Error("Task has no atomic processor");
|
|
904
|
+
assertPayload(input);
|
|
905
|
+
const result = await this.task.process(input, options, taskContext(this.state, context));
|
|
906
|
+
assertPayload(result);
|
|
907
|
+
return result;
|
|
908
|
+
}
|
|
909
|
+
async *stream(inputs, options, context = {}) {
|
|
910
|
+
if (!this.initialized)
|
|
911
|
+
throw new Error("Task is not initialized");
|
|
912
|
+
if (!this.task.processInBatches)
|
|
913
|
+
throw new Error("Task has no streaming processor");
|
|
914
|
+
for await (const output of this.task.processInBatches(inputs, options, taskContext(this.state, context))) {
|
|
915
|
+
assertPayload(output);
|
|
916
|
+
yield output;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
};
|
|
920
|
+
async function initializeTaskWorker(task) {
|
|
921
|
+
if (!await WorkerBody.inWorkerThread())
|
|
922
|
+
throw new Error("initializeTaskWorker must run inside a worker");
|
|
923
|
+
if (installed)
|
|
924
|
+
throw new Error("A task is already installed in this worker");
|
|
925
|
+
installed = true;
|
|
926
|
+
const runtime = new TaskRuntime(task);
|
|
927
|
+
let channelStream;
|
|
928
|
+
const channel = new TaskChannel({
|
|
929
|
+
getResultTransferList: (value, request) => request.outputOwnership === "transfer" ? getTransferList(value) : [],
|
|
930
|
+
send: (message, transfer) => {
|
|
931
|
+
const snapshot = structuredClone(message, { transfer });
|
|
932
|
+
void WorkerBody.postMessage("task-channel", snapshot).catch((reason) => channel.fail(reason));
|
|
933
|
+
},
|
|
934
|
+
handleRequest: async (operation, value, signal) => {
|
|
935
|
+
const request = value;
|
|
936
|
+
const context = {
|
|
937
|
+
signal,
|
|
938
|
+
reportProgress: (progress) => channel.emit("task:progress", { id: request.progressId, progress }),
|
|
939
|
+
requestHost: (operation2, payload, controls = {}) => {
|
|
940
|
+
const request2 = {
|
|
941
|
+
operation: operation2,
|
|
942
|
+
payload: clonePayload(payload, controls.inputOwnership),
|
|
943
|
+
outputOwnership: controls.outputOwnership ?? "copy"
|
|
944
|
+
};
|
|
945
|
+
return channel.request("task:host-request", request2, signal, getTransferList(request2));
|
|
946
|
+
},
|
|
947
|
+
emitHost: (operation2, payload) => channel.emit("task:host-event", { operation: operation2, payload, id: request.progressId })
|
|
948
|
+
};
|
|
949
|
+
switch (operation) {
|
|
950
|
+
case "initialize":
|
|
951
|
+
return runtime.initialize(request.input, context);
|
|
952
|
+
case "process":
|
|
953
|
+
return runtime.process(request.input, request.options, context);
|
|
954
|
+
case "stream-open": {
|
|
955
|
+
if (channelStream)
|
|
956
|
+
throw new Error("A task stream is already open");
|
|
957
|
+
async function* inputs() {
|
|
958
|
+
while (true) {
|
|
959
|
+
const result = await channel.request("task:next-input", { outputOwnership: "transfer" }, signal);
|
|
960
|
+
if (result.done)
|
|
961
|
+
return;
|
|
962
|
+
yield result.value;
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
channelStream = runtime.stream(inputs(), request.options, context)[Symbol.asyncIterator]();
|
|
966
|
+
return {};
|
|
967
|
+
}
|
|
968
|
+
case "stream-next":
|
|
969
|
+
if (!channelStream)
|
|
970
|
+
throw new Error("No active task stream");
|
|
971
|
+
return channelStream.next();
|
|
972
|
+
case "stream-close":
|
|
973
|
+
try {
|
|
974
|
+
await channelStream?.return?.();
|
|
975
|
+
} finally {
|
|
976
|
+
channelStream = void 0;
|
|
977
|
+
}
|
|
978
|
+
return {};
|
|
979
|
+
case "destroy":
|
|
980
|
+
return runtime.destroy();
|
|
981
|
+
default:
|
|
982
|
+
throw new Error("Unknown task channel operation");
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
await WorkerBody.addEventListener((type, payload) => {
|
|
987
|
+
if (type === "task-channel")
|
|
988
|
+
channel.receive(payload);
|
|
989
|
+
});
|
|
990
|
+
let streamOptions = {};
|
|
991
|
+
return createWorker(async (request, _options, context) => {
|
|
992
|
+
switch (request.operation) {
|
|
993
|
+
case "initialize":
|
|
994
|
+
return runtime.initialize(request.input, context);
|
|
995
|
+
case "destroy":
|
|
996
|
+
return runtime.destroy();
|
|
997
|
+
case "stream-options":
|
|
998
|
+
streamOptions = request.options;
|
|
999
|
+
return {};
|
|
1000
|
+
case "process":
|
|
1001
|
+
return runtime.process(request.input, request.options, context);
|
|
1002
|
+
default:
|
|
1003
|
+
throw new Error("Unknown task operation");
|
|
1004
|
+
}
|
|
1005
|
+
}, async function* (inputs, _options, context) {
|
|
1006
|
+
async function* batches() {
|
|
1007
|
+
for await (const input of inputs) {
|
|
1008
|
+
assertPayload(input);
|
|
1009
|
+
yield input;
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
try {
|
|
1013
|
+
yield* runtime.stream(batches(), streamOptions, context);
|
|
1014
|
+
} finally {
|
|
1015
|
+
streamOptions = {};
|
|
1016
|
+
}
|
|
1017
|
+
}, () => preloadTaskCode(task));
|
|
1018
|
+
}
|
|
1019
|
+
var installed = false;
|
|
1020
|
+
|
|
1021
|
+
// ../core/src/worker-utils/lib/worker-api/task-runtime.ts
|
|
1022
|
+
function normalizeTaskProgress(progress) {
|
|
1023
|
+
return { ...progress, phase: progress.phase ?? "process", unit: progress.unit ?? "items" };
|
|
1024
|
+
}
|
|
1025
|
+
async function isWorkerThread() {
|
|
1026
|
+
if (typeof self !== "undefined" && typeof document === "undefined" && typeof self.postMessage === "function")
|
|
1027
|
+
return true;
|
|
1028
|
+
if (typeof process !== "undefined" && process.versions?.node) {
|
|
1029
|
+
const { isMainThread } = await Promise.resolve().then(() => (init_worker_threads_browser(), worker_threads_browser_exports));
|
|
1030
|
+
return !isMainThread;
|
|
1031
|
+
}
|
|
1032
|
+
return false;
|
|
1033
|
+
}
|
|
1034
|
+
var initializeTaskWorker2 = initializeTaskWorker;
|
|
1035
|
+
|
|
1036
|
+
// ../core/src/worker-utils/lib/worker-utils/get-transfer-list.ts
|
|
1037
|
+
function getTransferList2(object, recursive = true, transfers) {
|
|
1038
|
+
const transfersSet = transfers || /* @__PURE__ */ new Set();
|
|
1039
|
+
if (!object) {
|
|
1040
|
+
} else if (isTransferable2(object)) {
|
|
1041
|
+
transfersSet.add(object);
|
|
1042
|
+
} else if (isTransferable2(object.buffer)) {
|
|
1043
|
+
transfersSet.add(object.buffer);
|
|
1044
|
+
} else if (ArrayBuffer.isView(object)) {
|
|
1045
|
+
} else if (recursive && typeof object === "object") {
|
|
1046
|
+
for (const key in object) {
|
|
1047
|
+
getTransferList2(object[key], recursive, transfersSet);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
return transfers === void 0 ? Array.from(transfersSet) : [];
|
|
1051
|
+
}
|
|
1052
|
+
function isTransferable2(object) {
|
|
1053
|
+
if (!object) {
|
|
1054
|
+
return false;
|
|
1055
|
+
}
|
|
1056
|
+
if (object instanceof ArrayBuffer) {
|
|
1057
|
+
return true;
|
|
1058
|
+
}
|
|
1059
|
+
if (typeof MessagePort !== "undefined" && object instanceof MessagePort) {
|
|
1060
|
+
return true;
|
|
1061
|
+
}
|
|
1062
|
+
if (typeof ImageBitmap !== "undefined" && object instanceof ImageBitmap) {
|
|
1063
|
+
return true;
|
|
1064
|
+
}
|
|
1065
|
+
if (typeof OffscreenCanvas !== "undefined" && object instanceof OffscreenCanvas) {
|
|
1066
|
+
return true;
|
|
1067
|
+
}
|
|
1068
|
+
return false;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
// ../core/src/worker-utils/lib/worker-api/create-worker.ts
|
|
1072
|
+
async function createWorker2(process2, processInBatches) {
|
|
1073
|
+
if (!await isWorkerThread()) return;
|
|
1074
|
+
await initializeTaskWorker2(createWorkerTask(process2, processInBatches));
|
|
1075
|
+
}
|
|
1076
|
+
function createWorkerTask(process2, processInBatches) {
|
|
1077
|
+
return {
|
|
1078
|
+
id: "loaders.gl",
|
|
1079
|
+
initialize: (state) => state,
|
|
1080
|
+
destroy: (state) => state.port?.close(),
|
|
1081
|
+
process: async ({ value }, options, context) => {
|
|
1082
|
+
if (!process2) throw new Error("Worker does not support atomic processing");
|
|
1083
|
+
return { value: await process2(value, options, createContext(context), context.state.context) };
|
|
1084
|
+
},
|
|
1085
|
+
...processInBatches ? {
|
|
1086
|
+
processInBatches: async function* (inputs, options, context) {
|
|
1087
|
+
async function* values() {
|
|
1088
|
+
for await (const input of inputs) yield input.value;
|
|
1089
|
+
}
|
|
1090
|
+
for await (const value of processInBatches(
|
|
1091
|
+
values(),
|
|
1092
|
+
options,
|
|
1093
|
+
createContext(context),
|
|
1094
|
+
context.state.context
|
|
1095
|
+
)) {
|
|
1096
|
+
yield { value };
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
} : {}
|
|
1100
|
+
};
|
|
1101
|
+
}
|
|
1102
|
+
function createContext(context) {
|
|
1103
|
+
let requestIdentifier = 0;
|
|
1104
|
+
const port = context.state.port;
|
|
1105
|
+
return {
|
|
1106
|
+
signal: context.signal,
|
|
1107
|
+
throwIfAborted: () => context.throwIfAborted(),
|
|
1108
|
+
reportProgress: (progress) => context.reportProgress(normalizeTaskProgress(progress)),
|
|
1109
|
+
process: port ? (input, options = {}, jobContext = {}) => {
|
|
1110
|
+
context.throwIfAborted();
|
|
1111
|
+
const identifier = ++requestIdentifier;
|
|
1112
|
+
return new Promise((resolve, reject) => {
|
|
1113
|
+
const onMessage = (event) => {
|
|
1114
|
+
if (event.data.identifier !== identifier) return;
|
|
1115
|
+
cleanup();
|
|
1116
|
+
if (event.data.error) {
|
|
1117
|
+
const error = new Error(event.data.error.message);
|
|
1118
|
+
error.name = event.data.error.name;
|
|
1119
|
+
reject(error);
|
|
1120
|
+
} else resolve(event.data.value);
|
|
1121
|
+
};
|
|
1122
|
+
const onAbort = () => {
|
|
1123
|
+
cleanup();
|
|
1124
|
+
reject(context.signal.reason);
|
|
1125
|
+
};
|
|
1126
|
+
const cleanup = () => {
|
|
1127
|
+
port.removeEventListener("message", onMessage);
|
|
1128
|
+
context.signal.removeEventListener("abort", onAbort);
|
|
1129
|
+
};
|
|
1130
|
+
port.addEventListener("message", onMessage);
|
|
1131
|
+
context.signal.addEventListener("abort", onAbort, { once: true });
|
|
1132
|
+
port.start();
|
|
1133
|
+
try {
|
|
1134
|
+
const request = { identifier, input, options, context: jobContext };
|
|
1135
|
+
port.postMessage(request, getTransferList2(request));
|
|
1136
|
+
} catch (error) {
|
|
1137
|
+
cleanup();
|
|
1138
|
+
reject(error);
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1141
|
+
} : void 0
|
|
1142
|
+
};
|
|
1143
|
+
}
|
|
1144
|
+
|
|
610
1145
|
// ../core/src/loader-utils/lib/worker-loader-utils/create-loader-worker.ts
|
|
611
1146
|
async function createLoaderWorker(loader, selectLoader = () => loader) {
|
|
612
|
-
await
|
|
1147
|
+
await createWorker2(
|
|
613
1148
|
(input, options, workerContext, loaderContext) => processLoaderWorkerData(
|
|
614
1149
|
selectLoader(options || {}),
|
|
615
1150
|
input,
|