@loaders.gl/scientific 5.0.0-beta.2 → 5.0.0-beta.4

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.
@@ -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.2" : "latest";
111
+ VERSION = true ? "5.0.0-beta.4" : "latest";
112
112
  NPY_MAGIC_NUMBER = new Uint8Array([147, 78, 85, 77, 80, 89]);
113
113
  NPYLoader = {
114
114
  ...NPYFormat,
@@ -150,11 +150,38 @@
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
183
  // ../../node_modules/@math.gl/core/dist/worker-utils/lib/node/worker_threads-browser.js
157
- var parentPort = null;
184
+ var parentPort = globalThis.process?.getBuiltinModule?.("node:worker_threads")?.parentPort ?? null;
158
185
 
159
186
  // ../../node_modules/@math.gl/core/dist/worker-utils/lib/worker-utils/get-transfer-list.js
160
187
  function getTransferList(object, recursive = true, transfers) {
@@ -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);
@@ -220,16 +250,16 @@
220
250
  */
221
251
  static set onmessage(onMessage) {
222
252
  async function handleMessage(message) {
223
- const parentPort2 = await getParentPort();
224
- const { type, payload } = parentPort2 ? message : message.data;
253
+ const parentPort3 = await getParentPort();
254
+ const { type, payload } = parentPort3 ? message : message.data;
225
255
  onMessage(type, payload);
226
256
  }
227
- getParentPort().then((parentPort2) => {
228
- if (parentPort2) {
229
- parentPort2.on("message", (message) => {
257
+ getParentPort().then((parentPort3) => {
258
+ if (parentPort3) {
259
+ parentPort3.on("message", (message) => {
230
260
  handleMessage(message);
231
261
  });
232
- parentPort2.on("exit", () => console.debug("Node worker closing"));
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 parentPort2 = await getParentPort();
251
- if (parentPort2) {
252
- parentPort2.on("message", onMessageWrapper);
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 parentPort2 = await getParentPort();
264
- if (parentPort2) {
265
- parentPort2.off("message", onMessageWrapper);
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 parentPort2 = await getParentPort();
279
- if (parentPort2) {
280
- parentPort2.postMessage(data, transferList);
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
  }
@@ -397,12 +427,19 @@
397
427
  }
398
428
  };
399
429
 
430
+ // ../../node_modules/@math.gl/core/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
+
400
437
  // ../../node_modules/@math.gl/core/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(process, processInBatches) {
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
- if (!process) {
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 process(payload.input, payload.options || {}, jobContext, payload.context || {});
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 pending;
612
+ let pending2;
573
613
  let finished = false;
574
614
  let scheduled = false;
575
615
  const flush = () => {
576
616
  scheduled = false;
577
- if (pending) {
578
- WorkerBody.postMessage("progress", { progress: pending });
579
- pending = void 0;
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 (pending && pending.phase !== progress.phase)
627
+ if (pending2 && pending2.phase !== progress.phase)
588
628
  flush();
589
- pending = { ...progress };
629
+ pending2 = { ...progress };
590
630
  if (!scheduled) {
591
631
  scheduled = true;
592
632
  setTimeout(flush, 0);
@@ -595,7 +635,7 @@
595
635
  /** Flushes final progress and suppresses reports from stale callbacks. */
596
636
  finish(discard = false) {
597
637
  if (discard)
598
- pending = void 0;
638
+ pending2 = void 0;
599
639
  flush();
600
640
  finished = true;
601
641
  }
@@ -607,9 +647,276 @@
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/core/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/core/dist/tasks/task-runtime.js
688
+ function assertPayload(value) {
689
+ if (!value || typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype) {
690
+ throw new TypeError("Task payloads must be plain objects with named fields");
691
+ }
692
+ }
693
+ function taskContext(state, context = {}) {
694
+ const signal = context.signal ?? new AbortController().signal;
695
+ return {
696
+ state,
697
+ signal,
698
+ throwIfAborted: () => {
699
+ signal.throwIfAborted();
700
+ context.throwIfAborted?.();
701
+ },
702
+ reportProgress: (progress) => context.reportProgress?.({
703
+ ...progress,
704
+ phase: progress.phase ?? "process",
705
+ unit: progress.unit ?? "items"
706
+ })
707
+ };
708
+ }
709
+ var TaskRuntime = class {
710
+ constructor(task) {
711
+ this.task = task;
712
+ this.initialized = false;
713
+ }
714
+ async initialize(input, context = {}) {
715
+ if (this.initialized)
716
+ throw new Error("Task is already initialized");
717
+ assertPayload(input);
718
+ await preloadTaskCode(this.task, context.signal);
719
+ context.signal?.throwIfAborted();
720
+ this.state = await this.task.initialize?.(input, taskContext(void 0, context));
721
+ this.initialized = true;
722
+ return {};
723
+ }
724
+ async destroy() {
725
+ if (this.initialized) {
726
+ this.initialized = false;
727
+ const state = this.state;
728
+ this.state = void 0;
729
+ if (this.task.initialize)
730
+ await this.task.destroy?.(state);
731
+ }
732
+ return {};
733
+ }
734
+ async process(input, options, context = {}) {
735
+ if (!this.initialized)
736
+ throw new Error("Task is not initialized");
737
+ if (!this.task.process)
738
+ throw new Error("Task has no atomic processor");
739
+ assertPayload(input);
740
+ const result = await this.task.process(input, options, taskContext(this.state, context));
741
+ assertPayload(result);
742
+ return result;
743
+ }
744
+ async *stream(inputs, options, context = {}) {
745
+ if (!this.initialized)
746
+ throw new Error("Task is not initialized");
747
+ if (!this.task.processInBatches)
748
+ throw new Error("Task has no streaming processor");
749
+ for await (const output of this.task.processInBatches(inputs, options, taskContext(this.state, context))) {
750
+ assertPayload(output);
751
+ yield output;
752
+ }
753
+ }
754
+ };
755
+ async function initializeTaskWorker(task) {
756
+ if (!await WorkerBody.inWorkerThread())
757
+ throw new Error("initializeTaskWorker must run inside a worker");
758
+ if (installed)
759
+ throw new Error("A task is already installed in this worker");
760
+ installed = true;
761
+ const runtime = new TaskRuntime(task);
762
+ let streamOptions = {};
763
+ return createWorker(async (request, _options, context) => {
764
+ switch (request.operation) {
765
+ case "initialize":
766
+ return runtime.initialize(request.input, context);
767
+ case "destroy":
768
+ return runtime.destroy();
769
+ case "stream-options":
770
+ streamOptions = request.options;
771
+ return {};
772
+ case "process":
773
+ return runtime.process(request.input, request.options, context);
774
+ default:
775
+ throw new Error("Unknown task operation");
776
+ }
777
+ }, async function* (inputs, _options, context) {
778
+ async function* batches() {
779
+ for await (const input of inputs) {
780
+ assertPayload(input);
781
+ yield input;
782
+ }
783
+ }
784
+ try {
785
+ yield* runtime.stream(batches(), streamOptions, context);
786
+ } finally {
787
+ streamOptions = {};
788
+ }
789
+ }, () => preloadTaskCode(task));
790
+ }
791
+ var installed = false;
792
+
793
+ // ../core/src/worker-utils/lib/worker-api/task-runtime.ts
794
+ function normalizeTaskProgress(progress) {
795
+ return { ...progress, phase: progress.phase ?? "process", unit: progress.unit ?? "items" };
796
+ }
797
+ async function isWorkerThread() {
798
+ if (typeof self !== "undefined" && typeof document === "undefined" && typeof self.postMessage === "function")
799
+ return true;
800
+ if (typeof process !== "undefined" && process.versions?.node) {
801
+ const { isMainThread } = await Promise.resolve().then(() => (init_worker_threads_browser(), worker_threads_browser_exports));
802
+ return !isMainThread;
803
+ }
804
+ return false;
805
+ }
806
+ var initializeTaskWorker2 = initializeTaskWorker;
807
+
808
+ // ../core/src/worker-utils/lib/worker-utils/get-transfer-list.ts
809
+ function getTransferList2(object, recursive = true, transfers) {
810
+ const transfersSet = transfers || /* @__PURE__ */ new Set();
811
+ if (!object) {
812
+ } else if (isTransferable2(object)) {
813
+ transfersSet.add(object);
814
+ } else if (isTransferable2(object.buffer)) {
815
+ transfersSet.add(object.buffer);
816
+ } else if (ArrayBuffer.isView(object)) {
817
+ } else if (recursive && typeof object === "object") {
818
+ for (const key in object) {
819
+ getTransferList2(object[key], recursive, transfersSet);
820
+ }
821
+ }
822
+ return transfers === void 0 ? Array.from(transfersSet) : [];
823
+ }
824
+ function isTransferable2(object) {
825
+ if (!object) {
826
+ return false;
827
+ }
828
+ if (object instanceof ArrayBuffer) {
829
+ return true;
830
+ }
831
+ if (typeof MessagePort !== "undefined" && object instanceof MessagePort) {
832
+ return true;
833
+ }
834
+ if (typeof ImageBitmap !== "undefined" && object instanceof ImageBitmap) {
835
+ return true;
836
+ }
837
+ if (typeof OffscreenCanvas !== "undefined" && object instanceof OffscreenCanvas) {
838
+ return true;
839
+ }
840
+ return false;
841
+ }
842
+
843
+ // ../core/src/worker-utils/lib/worker-api/create-worker.ts
844
+ async function createWorker2(process2, processInBatches) {
845
+ if (!await isWorkerThread()) return;
846
+ await initializeTaskWorker2(createWorkerTask(process2, processInBatches));
847
+ }
848
+ function createWorkerTask(process2, processInBatches) {
849
+ return {
850
+ id: "loaders.gl",
851
+ initialize: (state) => state,
852
+ destroy: (state) => state.port?.close(),
853
+ process: async ({ value }, options, context) => {
854
+ if (!process2) throw new Error("Worker does not support atomic processing");
855
+ return { value: await process2(value, options, createContext(context), context.state.context) };
856
+ },
857
+ ...processInBatches ? {
858
+ processInBatches: async function* (inputs, options, context) {
859
+ async function* values() {
860
+ for await (const input of inputs) yield input.value;
861
+ }
862
+ for await (const value of processInBatches(
863
+ values(),
864
+ options,
865
+ createContext(context),
866
+ context.state.context
867
+ )) {
868
+ yield { value };
869
+ }
870
+ }
871
+ } : {}
872
+ };
873
+ }
874
+ function createContext(context) {
875
+ let requestIdentifier = 0;
876
+ const port = context.state.port;
877
+ return {
878
+ signal: context.signal,
879
+ throwIfAborted: () => context.throwIfAborted(),
880
+ reportProgress: (progress) => context.reportProgress(normalizeTaskProgress(progress)),
881
+ process: port ? (input, options = {}, jobContext = {}) => {
882
+ context.throwIfAborted();
883
+ const identifier = ++requestIdentifier;
884
+ return new Promise((resolve, reject) => {
885
+ const onMessage = (event) => {
886
+ if (event.data.identifier !== identifier) return;
887
+ cleanup();
888
+ if (event.data.error) {
889
+ const error = new Error(event.data.error.message);
890
+ error.name = event.data.error.name;
891
+ reject(error);
892
+ } else resolve(event.data.value);
893
+ };
894
+ const onAbort = () => {
895
+ cleanup();
896
+ reject(context.signal.reason);
897
+ };
898
+ const cleanup = () => {
899
+ port.removeEventListener("message", onMessage);
900
+ context.signal.removeEventListener("abort", onAbort);
901
+ };
902
+ port.addEventListener("message", onMessage);
903
+ context.signal.addEventListener("abort", onAbort, { once: true });
904
+ port.start();
905
+ try {
906
+ const request = { identifier, input, options, context: jobContext };
907
+ port.postMessage(request, getTransferList2(request));
908
+ } catch (error) {
909
+ cleanup();
910
+ reject(error);
911
+ }
912
+ });
913
+ } : void 0
914
+ };
915
+ }
916
+
610
917
  // ../core/src/loader-utils/lib/worker-loader-utils/create-loader-worker.ts
611
918
  async function createLoaderWorker(loader, selectLoader = () => loader) {
612
- await createWorker(
919
+ await createWorker2(
613
920
  (input, options, workerContext, loaderContext) => processLoaderWorkerData(
614
921
  selectLoader(options || {}),
615
922
  input,