@loaders.gl/scientific 5.0.0-beta.1 → 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.
- package/README.md +5 -2
- package/dist/index.cjs +78 -4110
- package/dist/index.cjs.map +4 -3
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/parsers/parse-npy.d.ts +15 -0
- package/dist/lib/parsers/parse-npy.d.ts.map +1 -0
- package/dist/lib/parsers/parse-npy.js +81 -0
- package/dist/lib/parsers/parse-npy.js.map +1 -0
- package/dist/netcdf/bundled.cjs +1 -2
- package/dist/netcdf/index.cjs +1 -2
- package/dist/netcdf/index.cjs.map +2 -2
- package/dist/netcdf/netcdf-loader-with-parser.cjs +1 -2
- package/dist/netcdf/netcdf-loader-with-parser.js +1 -1
- package/dist/netcdf/netcdf-loader-with-parser.js.map +1 -1
- package/dist/netcdf/netcdf-loader.cjs +5 -4
- package/dist/netcdf/netcdf-loader.cjs.map +7 -0
- 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/unbundled.cjs +1 -2
- package/dist/netcdf.min.js +3 -3
- package/dist/npy-format.d.ts +12 -0
- package/dist/npy-format.d.ts.map +1 -0
- package/dist/npy-format.js +15 -0
- package/dist/npy-format.js.map +1 -0
- package/dist/npy-loader-types.cjs +80 -0
- package/dist/npy-loader-types.cjs.map +7 -0
- package/dist/npy-loader-types.d.ts +73 -0
- package/dist/npy-loader-types.d.ts.map +1 -0
- package/dist/npy-loader-types.js +34 -0
- package/dist/npy-loader-types.js.map +1 -0
- package/dist/npy-loader.cjs +273 -0
- package/dist/npy-loader.cjs.map +6 -0
- package/dist/npy-loader.d.ts +27 -0
- package/dist/npy-loader.d.ts.map +1 -0
- package/dist/npy-loader.js +15 -0
- package/dist/npy-loader.js.map +1 -0
- package/dist/npy-worker.d.ts +2 -0
- package/dist/npy-worker.d.ts.map +1 -0
- package/dist/npy-worker.js +1057 -0
- package/dist/npy-worker.js.map +1 -0
- package/dist/vtk/index.cjs +4 -4118
- package/dist/vtk/index.cjs.map +2 -2
- package/dist/vtk/vtk-xml.js +4 -4
- package/dist/vtk/vtk-xml.js.map +1 -1
- package/dist/workers/npy-worker.d.ts +2 -0
- package/dist/workers/npy-worker.d.ts.map +1 -0
- package/dist/workers/npy-worker.js +7 -0
- package/dist/workers/npy-worker.js.map +1 -0
- package/dist/zarr/geo-zarr-source-loader.d.ts.map +1 -1
- package/dist/zarr/geo-zarr-source-loader.js +162 -43
- package/dist/zarr/geo-zarr-source-loader.js.map +1 -1
- package/dist/zarr/index.cjs +194 -43
- package/dist/zarr/index.cjs.map +2 -2
- package/dist/zarr/lib/spatial-multiscales.d.ts +13 -2
- package/dist/zarr/lib/spatial-multiscales.d.ts.map +1 -1
- package/dist/zarr/lib/spatial-multiscales.js +94 -8
- 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 +161 -0
- package/dist/zarr/zarr-loader-types.cjs.map +7 -0
- package/dist/zarr/zarr-loader-types.js +1 -1
- package/dist/zarr.dev.js +194 -43
- package/dist/zarr.min.js +40 -40
- package/package.json +42 -7
- package/src/index.ts +5 -0
- package/src/lib/parsers/parse-npy.ts +123 -0
- package/src/netcdf/netcdf-loader-with-parser.ts +1 -1
- package/src/npy-format.ts +17 -0
- package/src/npy-loader-types.ts +50 -0
- package/src/npy-loader.ts +21 -0
- package/src/npy-worker.ts +6 -0
- package/src/vtk/vtk-xml.ts +4 -4
- package/src/workers/npy-worker.ts +8 -0
- package/src/zarr/geo-zarr-source-loader.ts +195 -44
- package/src/zarr/lib/spatial-multiscales.ts +104 -8
|
@@ -0,0 +1,1057 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __esm = (fn, res, err) => function __init() {
|
|
6
|
+
if (err) throw err[0];
|
|
7
|
+
try {
|
|
8
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
9
|
+
} catch (e) {
|
|
10
|
+
throw err = [e], e;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var __export = (target, all) => {
|
|
14
|
+
for (var name in all)
|
|
15
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/lib/parsers/parse-npy.ts
|
|
19
|
+
function parseNPY(arrayBuffer, options) {
|
|
20
|
+
const view = new DataView(arrayBuffer);
|
|
21
|
+
const { header, headerEndOffset } = parseHeader(view);
|
|
22
|
+
const numpyType = header.descr;
|
|
23
|
+
const ArrayType = DTYPES[numpyType.slice(1, 3)];
|
|
24
|
+
if (!ArrayType) {
|
|
25
|
+
throw new Error(`Unimplemented type ${numpyType}`);
|
|
26
|
+
}
|
|
27
|
+
const nArrayElements = header.shape?.reduce((a2, b2) => a2 * b2);
|
|
28
|
+
const arrayByteLength = nArrayElements * ArrayType.BYTES_PER_ELEMENT;
|
|
29
|
+
if (arrayBuffer.byteLength < headerEndOffset + arrayByteLength) {
|
|
30
|
+
throw new Error("Buffer overflow");
|
|
31
|
+
}
|
|
32
|
+
const data = new ArrayType(arrayBuffer.slice(headerEndOffset, headerEndOffset + arrayByteLength));
|
|
33
|
+
if (numpyType[0] === ">" && LITTLE_ENDIAN_OS || numpyType[0] === "<" && !LITTLE_ENDIAN_OS) {
|
|
34
|
+
throw new Error("Incorrect endianness");
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
data,
|
|
38
|
+
header
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function parseHeader(view) {
|
|
42
|
+
const majorVersion = view.getUint8(6);
|
|
43
|
+
let offset = 8;
|
|
44
|
+
let headerLength;
|
|
45
|
+
if (majorVersion >= 2) {
|
|
46
|
+
headerLength = view.getUint32(offset, true);
|
|
47
|
+
offset += 4;
|
|
48
|
+
} else {
|
|
49
|
+
headerLength = view.getUint16(offset, true);
|
|
50
|
+
offset += 2;
|
|
51
|
+
}
|
|
52
|
+
const encoding = majorVersion <= 2 ? "latin1" : "utf-8";
|
|
53
|
+
const decoder = new TextDecoder(encoding);
|
|
54
|
+
const headerArray = new Uint8Array(view.buffer, offset, headerLength);
|
|
55
|
+
const headerText = decoder.decode(headerArray);
|
|
56
|
+
offset += headerLength;
|
|
57
|
+
const header = JSON.parse(
|
|
58
|
+
headerText.replace(/'/g, '"').replace("False", "false").replace("(", "[").replace(/,*\),*/g, "]")
|
|
59
|
+
);
|
|
60
|
+
return { header, headerEndOffset: offset };
|
|
61
|
+
}
|
|
62
|
+
var a, b, isLittleEndian, LITTLE_ENDIAN_OS, DTYPES;
|
|
63
|
+
var init_parse_npy = __esm({
|
|
64
|
+
"src/lib/parsers/parse-npy.ts"() {
|
|
65
|
+
"use strict";
|
|
66
|
+
a = new Uint32Array([305419896]);
|
|
67
|
+
b = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
|
|
68
|
+
isLittleEndian = !(b[0] === 18);
|
|
69
|
+
LITTLE_ENDIAN_OS = isLittleEndian;
|
|
70
|
+
DTYPES = {
|
|
71
|
+
u1: Uint8Array,
|
|
72
|
+
i1: Int8Array,
|
|
73
|
+
u2: Uint16Array,
|
|
74
|
+
i2: Int16Array,
|
|
75
|
+
u4: Uint32Array,
|
|
76
|
+
i4: Int32Array,
|
|
77
|
+
f4: Float32Array,
|
|
78
|
+
f8: Float64Array
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// src/npy-format.ts
|
|
84
|
+
var NPYFormat;
|
|
85
|
+
var init_npy_format = __esm({
|
|
86
|
+
"src/npy-format.ts"() {
|
|
87
|
+
"use strict";
|
|
88
|
+
NPYFormat = {
|
|
89
|
+
name: "NPY",
|
|
90
|
+
id: "npy",
|
|
91
|
+
module: "scientific",
|
|
92
|
+
encoding: "binary",
|
|
93
|
+
format: "npy",
|
|
94
|
+
extensions: ["npy"],
|
|
95
|
+
mimeTypes: [],
|
|
96
|
+
binary: true
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// src/npy-loader-types.ts
|
|
102
|
+
async function preload() {
|
|
103
|
+
const { NPYLoaderWithParser: NPYLoaderWithParser2 } = await Promise.resolve().then(() => (init_npy_loader(), npy_loader_exports));
|
|
104
|
+
return NPYLoaderWithParser2;
|
|
105
|
+
}
|
|
106
|
+
var VERSION, NPY_MAGIC_NUMBER, NPYLoader;
|
|
107
|
+
var init_npy_loader_types = __esm({
|
|
108
|
+
"src/npy-loader-types.ts"() {
|
|
109
|
+
"use strict";
|
|
110
|
+
init_npy_format();
|
|
111
|
+
VERSION = true ? "5.0.0-beta.4" : "latest";
|
|
112
|
+
NPY_MAGIC_NUMBER = new Uint8Array([147, 78, 85, 77, 80, 89]);
|
|
113
|
+
NPYLoader = {
|
|
114
|
+
...NPYFormat,
|
|
115
|
+
dataType: null,
|
|
116
|
+
batchType: null,
|
|
117
|
+
name: "NPY",
|
|
118
|
+
id: "npy",
|
|
119
|
+
module: "scientific",
|
|
120
|
+
version: VERSION,
|
|
121
|
+
worker: true,
|
|
122
|
+
extensions: ["npy"],
|
|
123
|
+
mimeTypes: [],
|
|
124
|
+
tests: [NPY_MAGIC_NUMBER.buffer],
|
|
125
|
+
options: {
|
|
126
|
+
npy: {}
|
|
127
|
+
},
|
|
128
|
+
preload
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// src/npy-loader.ts
|
|
134
|
+
var npy_loader_exports = {};
|
|
135
|
+
__export(npy_loader_exports, {
|
|
136
|
+
NPYLoaderWithParser: () => NPYLoaderWithParser
|
|
137
|
+
});
|
|
138
|
+
var _preload, metadata, NPYLoaderWithParser;
|
|
139
|
+
var init_npy_loader = __esm({
|
|
140
|
+
"src/npy-loader.ts"() {
|
|
141
|
+
"use strict";
|
|
142
|
+
init_parse_npy();
|
|
143
|
+
init_npy_loader_types();
|
|
144
|
+
({ preload: _preload, ...metadata } = NPYLoader);
|
|
145
|
+
NPYLoaderWithParser = {
|
|
146
|
+
...metadata,
|
|
147
|
+
parseSync: parseNPY,
|
|
148
|
+
parse: async (arrayBuffer, options) => parseNPY(arrayBuffer, options)
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
});
|
|
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
|
+
|
|
180
|
+
// src/workers/npy-worker.ts
|
|
181
|
+
init_npy_loader();
|
|
182
|
+
|
|
183
|
+
// ../../node_modules/@math.gl/core/dist/worker-utils/lib/node/worker_threads-browser.js
|
|
184
|
+
var parentPort = globalThis.process?.getBuiltinModule?.("node:worker_threads")?.parentPort ?? null;
|
|
185
|
+
|
|
186
|
+
// ../../node_modules/@math.gl/core/dist/worker-utils/lib/worker-utils/get-transfer-list.js
|
|
187
|
+
function getTransferList(object, recursive = true, transfers) {
|
|
188
|
+
const transfersSet = transfers || /* @__PURE__ */ new Set();
|
|
189
|
+
const seen = /* @__PURE__ */ new Set();
|
|
190
|
+
function visit(value) {
|
|
191
|
+
if (!value || typeof value !== "object" || seen.has(value))
|
|
192
|
+
return;
|
|
193
|
+
seen.add(value);
|
|
194
|
+
if (isTransferable(value))
|
|
195
|
+
transfersSet.add(value);
|
|
196
|
+
else if (ArrayBuffer.isView(value)) {
|
|
197
|
+
if (isTransferable(value.buffer))
|
|
198
|
+
transfersSet.add(value.buffer);
|
|
199
|
+
} else if (recursive && typeof MessageChannel !== "undefined" && value instanceof MessageChannel) {
|
|
200
|
+
visit(value.port1);
|
|
201
|
+
visit(value.port2);
|
|
202
|
+
} else if (recursive && value instanceof Map) {
|
|
203
|
+
for (const [key, item] of value) {
|
|
204
|
+
visit(key);
|
|
205
|
+
visit(item);
|
|
206
|
+
}
|
|
207
|
+
} else if (recursive && value instanceof Set) {
|
|
208
|
+
for (const item of value)
|
|
209
|
+
visit(item);
|
|
210
|
+
} else if (recursive) {
|
|
211
|
+
for (const key of Object.keys(value))
|
|
212
|
+
visit(value[key]);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
visit(object);
|
|
216
|
+
return transfers === void 0 ? Array.from(transfersSet) : [];
|
|
217
|
+
}
|
|
218
|
+
function isTransferable(object) {
|
|
219
|
+
if (!object) {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
if (object instanceof ArrayBuffer) {
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
if (typeof MessagePort !== "undefined" && object instanceof MessagePort) {
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
if (typeof ImageBitmap !== "undefined" && object instanceof ImageBitmap) {
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
if (typeof OffscreenCanvas !== "undefined" && object instanceof OffscreenCanvas) {
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ../../node_modules/@math.gl/core/dist/worker-utils/lib/worker-farm/worker-body.js
|
|
238
|
+
async function getParentPort() {
|
|
239
|
+
return parentPort;
|
|
240
|
+
}
|
|
241
|
+
var onMessageWrapperMap = /* @__PURE__ */ new Map();
|
|
242
|
+
var WorkerBody = class {
|
|
243
|
+
/** Check that we are actually in a worker thread */
|
|
244
|
+
static async inWorkerThread() {
|
|
245
|
+
const workerGlobalScope = globalThis.WorkerGlobalScope;
|
|
246
|
+
return typeof workerGlobalScope === "function" && globalThis instanceof workerGlobalScope || Boolean(await getParentPort());
|
|
247
|
+
}
|
|
248
|
+
/*
|
|
249
|
+
* (type: WorkerMessageType, payload: WorkerMessagePayload) => any
|
|
250
|
+
*/
|
|
251
|
+
static set onmessage(onMessage) {
|
|
252
|
+
async function handleMessage(message) {
|
|
253
|
+
const parentPort3 = await getParentPort();
|
|
254
|
+
const { type, payload } = parentPort3 ? message : message.data;
|
|
255
|
+
onMessage(type, payload);
|
|
256
|
+
}
|
|
257
|
+
getParentPort().then((parentPort3) => {
|
|
258
|
+
if (parentPort3) {
|
|
259
|
+
parentPort3.on("message", (message) => {
|
|
260
|
+
handleMessage(message);
|
|
261
|
+
});
|
|
262
|
+
parentPort3.on("exit", () => console.debug("Node worker closing"));
|
|
263
|
+
} else {
|
|
264
|
+
globalThis.onmessage = handleMessage;
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
static async addEventListener(onMessage) {
|
|
269
|
+
let onMessageWrapper = onMessageWrapperMap.get(onMessage);
|
|
270
|
+
if (!onMessageWrapper) {
|
|
271
|
+
onMessageWrapper = (message) => {
|
|
272
|
+
const messageData = getWorkerMessageData(message);
|
|
273
|
+
if (!messageData) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
onMessage(messageData.type, messageData.payload);
|
|
277
|
+
};
|
|
278
|
+
onMessageWrapperMap.set(onMessage, onMessageWrapper);
|
|
279
|
+
}
|
|
280
|
+
const parentPort3 = await getParentPort();
|
|
281
|
+
if (parentPort3) {
|
|
282
|
+
parentPort3.on("message", onMessageWrapper);
|
|
283
|
+
} else {
|
|
284
|
+
globalThis.addEventListener("message", onMessageWrapper);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
static async removeEventListener(onMessage) {
|
|
288
|
+
const onMessageWrapper = onMessageWrapperMap.get(onMessage);
|
|
289
|
+
onMessageWrapperMap.delete(onMessage);
|
|
290
|
+
if (!onMessageWrapper) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
const parentPort3 = await getParentPort();
|
|
294
|
+
if (parentPort3) {
|
|
295
|
+
parentPort3.off("message", onMessageWrapper);
|
|
296
|
+
} else {
|
|
297
|
+
globalThis.removeEventListener("message", onMessageWrapper);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Send a message from a worker to creating thread (main thread)
|
|
302
|
+
* @param type
|
|
303
|
+
* @param payload
|
|
304
|
+
*/
|
|
305
|
+
static async postMessage(type, payload, copy = false) {
|
|
306
|
+
const data = { source: "loaders.gl", type, payload };
|
|
307
|
+
const transferList = copy ? [] : getTransferList(payload);
|
|
308
|
+
const parentPort3 = await getParentPort();
|
|
309
|
+
if (parentPort3) {
|
|
310
|
+
parentPort3.postMessage(data, transferList);
|
|
311
|
+
} else {
|
|
312
|
+
globalThis.postMessage(data, transferList);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
function getWorkerMessageData(message) {
|
|
317
|
+
if (!message || typeof message !== "object") {
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
const messageData = "data" in message && message.type === "message" ? message.data : message;
|
|
321
|
+
return messageData && typeof messageData.source === "string" && messageData.source.startsWith("loaders.gl") ? messageData : null;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// ../../node_modules/@math.gl/core/dist/worker-utils/lib/async-queue/async-queue.js
|
|
325
|
+
var AsyncQueue = class {
|
|
326
|
+
constructor() {
|
|
327
|
+
this._values = [];
|
|
328
|
+
this._settlers = [];
|
|
329
|
+
this._closed = false;
|
|
330
|
+
}
|
|
331
|
+
/** Return an async iterator for this queue */
|
|
332
|
+
[Symbol.asyncIterator]() {
|
|
333
|
+
return this;
|
|
334
|
+
}
|
|
335
|
+
/** Push a new value - the async iterator will yield a promise resolved to this value */
|
|
336
|
+
push(value) {
|
|
337
|
+
return this.enqueue(value);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Push a new value - the async iterator will yield a promise resolved to this value
|
|
341
|
+
* Add an error - the async iterator will yield a promise rejected with this value
|
|
342
|
+
*/
|
|
343
|
+
enqueue(value) {
|
|
344
|
+
if (this._closed) {
|
|
345
|
+
throw new Error("Closed");
|
|
346
|
+
}
|
|
347
|
+
if (this._settlers.length > 0) {
|
|
348
|
+
if (this._values.length > 0) {
|
|
349
|
+
throw new Error("Illegal internal state");
|
|
350
|
+
}
|
|
351
|
+
const settler = this._settlers.shift();
|
|
352
|
+
if (value instanceof Error) {
|
|
353
|
+
settler.reject(value);
|
|
354
|
+
} else {
|
|
355
|
+
settler.resolve({ value });
|
|
356
|
+
}
|
|
357
|
+
} else {
|
|
358
|
+
this._values.push(value);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/** Indicate that we not waiting for more values - The async iterator will be done */
|
|
362
|
+
close() {
|
|
363
|
+
while (this._settlers.length > 0) {
|
|
364
|
+
const settler = this._settlers.shift();
|
|
365
|
+
settler.resolve({ done: true });
|
|
366
|
+
}
|
|
367
|
+
this._closed = true;
|
|
368
|
+
}
|
|
369
|
+
// ITERATOR IMPLEMENTATION
|
|
370
|
+
/** @returns a Promise for an IteratorResult */
|
|
371
|
+
next() {
|
|
372
|
+
if (this._values.length > 0) {
|
|
373
|
+
const value = this._values.shift();
|
|
374
|
+
if (value instanceof Error) {
|
|
375
|
+
return Promise.reject(value);
|
|
376
|
+
}
|
|
377
|
+
return Promise.resolve({ done: false, value });
|
|
378
|
+
}
|
|
379
|
+
if (this._closed) {
|
|
380
|
+
if (this._settlers.length > 0) {
|
|
381
|
+
throw new Error("Illegal internal state");
|
|
382
|
+
}
|
|
383
|
+
return Promise.resolve({ done: true, value: void 0 });
|
|
384
|
+
}
|
|
385
|
+
return new Promise((resolve, reject) => {
|
|
386
|
+
this._settlers.push({ resolve, reject });
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
// ../../node_modules/@math.gl/core/dist/worker-utils/lib/worker-api/shared-state.js
|
|
392
|
+
var SharedWorkerState = class {
|
|
393
|
+
constructor(buffer) {
|
|
394
|
+
if (typeof SharedArrayBuffer === "undefined" || !(buffer instanceof SharedArrayBuffer) || buffer.byteLength !== 16)
|
|
395
|
+
throw new Error("Expected a 16-byte shared worker control buffer");
|
|
396
|
+
this.buffer = buffer;
|
|
397
|
+
this.values = new Int32Array(buffer);
|
|
398
|
+
}
|
|
399
|
+
get snapshot() {
|
|
400
|
+
return {
|
|
401
|
+
cancelled: Atomics.load(this.values, 0) !== 0,
|
|
402
|
+
completed: Atomics.load(this.values, 1),
|
|
403
|
+
total: Atomics.load(this.values, 2),
|
|
404
|
+
revision: Atomics.load(this.values, 3)
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
cancel() {
|
|
408
|
+
Atomics.store(this.values, 0, 1);
|
|
409
|
+
Atomics.add(this.values, 3, 1);
|
|
410
|
+
Atomics.notify(this.values, 0);
|
|
411
|
+
}
|
|
412
|
+
throwIfCancelled() {
|
|
413
|
+
if (Atomics.load(this.values, 0) !== 0) {
|
|
414
|
+
const error = new Error("Shared worker task was cancelled");
|
|
415
|
+
error.name = "AbortError";
|
|
416
|
+
throw error;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/** Integer counters for application-defined work; messages still carry phase names and units. */
|
|
420
|
+
setProgress(completed, total = -1) {
|
|
421
|
+
if (!Number.isInteger(completed) || completed < 0 || completed > 2147483647 || !Number.isInteger(total) || total < -1 || total > 2147483647) {
|
|
422
|
+
throw new RangeError("Shared progress requires nonnegative int32 counts (or total -1)");
|
|
423
|
+
}
|
|
424
|
+
Atomics.store(this.values, 1, completed);
|
|
425
|
+
Atomics.store(this.values, 2, total);
|
|
426
|
+
Atomics.add(this.values, 3, 1);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
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
|
+
|
|
437
|
+
// ../../node_modules/@math.gl/core/dist/worker-utils/lib/worker-api/create-worker.js
|
|
438
|
+
var requestId = 0;
|
|
439
|
+
var activeController = null;
|
|
440
|
+
var activeInputBatches = null;
|
|
441
|
+
var activeOutputAcknowledgements = null;
|
|
442
|
+
async function createWorker(process2, processInBatches, preload2) {
|
|
443
|
+
if (!await WorkerBody.inWorkerThread()) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
const context = {
|
|
447
|
+
process: processOnMainThread
|
|
448
|
+
};
|
|
449
|
+
WorkerBody.onmessage = async (type, payload) => {
|
|
450
|
+
if (type === "cancel") {
|
|
451
|
+
activeController?.abort();
|
|
452
|
+
activeInputBatches?.close();
|
|
453
|
+
activeOutputAcknowledgements?.close();
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
const controller = type === "process" || type === "process-in-batches" ? new AbortController() : void 0;
|
|
457
|
+
if (controller)
|
|
458
|
+
activeController = controller;
|
|
459
|
+
const startedAt = performance.now();
|
|
460
|
+
const reporter = type === "process" || type === "process-in-batches" ? createProgressReporter() : void 0;
|
|
461
|
+
let sharedState;
|
|
462
|
+
const throwIfAborted = () => {
|
|
463
|
+
controller?.signal.throwIfAborted();
|
|
464
|
+
sharedState?.throwIfCancelled();
|
|
465
|
+
};
|
|
466
|
+
const jobContext = {
|
|
467
|
+
...context,
|
|
468
|
+
signal: controller?.signal,
|
|
469
|
+
sharedState,
|
|
470
|
+
throwIfAborted,
|
|
471
|
+
reportProgress: (progress) => {
|
|
472
|
+
if (!controller?.signal.aborted && !sharedState?.snapshot.cancelled)
|
|
473
|
+
reporter?.report(progress);
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
try {
|
|
477
|
+
if (controller && payload.options?.["sharedState"]) {
|
|
478
|
+
sharedState = new SharedWorkerState(payload.options["sharedState"]);
|
|
479
|
+
jobContext.sharedState = sharedState;
|
|
480
|
+
}
|
|
481
|
+
switch (type) {
|
|
482
|
+
case "preload":
|
|
483
|
+
await preload2?.();
|
|
484
|
+
WorkerBody.postMessage("done", {});
|
|
485
|
+
break;
|
|
486
|
+
case "process":
|
|
487
|
+
throwIfAborted();
|
|
488
|
+
validateOutputOwnership(payload.options?.["outputOwnership"]);
|
|
489
|
+
if (!process2) {
|
|
490
|
+
throw new Error("Worker does not support atomic processing");
|
|
491
|
+
}
|
|
492
|
+
const result = await process2(payload.input, payload.options || {}, jobContext, payload.context || {});
|
|
493
|
+
throwIfAborted();
|
|
494
|
+
reporter?.finish();
|
|
495
|
+
await WorkerBody.postMessage("done", {
|
|
496
|
+
result,
|
|
497
|
+
executionMs: performance.now() - startedAt
|
|
498
|
+
}, payload.options?.["outputOwnership"] === "copy");
|
|
499
|
+
break;
|
|
500
|
+
case "process-in-batches":
|
|
501
|
+
throwIfAborted();
|
|
502
|
+
validateOutputOwnership(payload.options?.["outputOwnership"]);
|
|
503
|
+
if (!processInBatches) {
|
|
504
|
+
throw new Error("Worker does not support batched processing");
|
|
505
|
+
}
|
|
506
|
+
const inputBatches = new AsyncQueue();
|
|
507
|
+
const outputAcknowledgements = new AsyncQueue();
|
|
508
|
+
activeInputBatches = inputBatches;
|
|
509
|
+
activeOutputAcknowledgements = outputAcknowledgements;
|
|
510
|
+
try {
|
|
511
|
+
const resultIterator = processInBatches(createDemandDrivenIterator(inputBatches), payload.options || {}, jobContext, payload.context || {});
|
|
512
|
+
for await (const batch of resultIterator) {
|
|
513
|
+
throwIfAborted();
|
|
514
|
+
await WorkerBody.postMessage("output-batch", { result: batch }, payload.options?.["outputOwnership"] === "copy");
|
|
515
|
+
await outputAcknowledgements.next();
|
|
516
|
+
}
|
|
517
|
+
throwIfAborted();
|
|
518
|
+
reporter?.finish();
|
|
519
|
+
await WorkerBody.postMessage("done", { executionMs: performance.now() - startedAt });
|
|
520
|
+
} finally {
|
|
521
|
+
activeInputBatches = null;
|
|
522
|
+
activeOutputAcknowledgements = null;
|
|
523
|
+
}
|
|
524
|
+
break;
|
|
525
|
+
case "input-batch":
|
|
526
|
+
if (!activeInputBatches) {
|
|
527
|
+
throw new Error("Worker has no active batched processing session");
|
|
528
|
+
}
|
|
529
|
+
activeInputBatches.push(payload.input);
|
|
530
|
+
break;
|
|
531
|
+
case "input-done":
|
|
532
|
+
if (!activeInputBatches) {
|
|
533
|
+
throw new Error("Worker has no active batched processing session");
|
|
534
|
+
}
|
|
535
|
+
activeInputBatches.close();
|
|
536
|
+
break;
|
|
537
|
+
case "output-ack":
|
|
538
|
+
if (!activeOutputAcknowledgements) {
|
|
539
|
+
throw new Error("Worker has no active batched processing session");
|
|
540
|
+
}
|
|
541
|
+
activeOutputAcknowledgements.push(void 0);
|
|
542
|
+
break;
|
|
543
|
+
default:
|
|
544
|
+
}
|
|
545
|
+
} catch (error) {
|
|
546
|
+
reporter?.finish(controller?.signal.aborted || sharedState?.snapshot.cancelled);
|
|
547
|
+
const message = error instanceof Error ? error.message : "";
|
|
548
|
+
await WorkerBody.postMessage("error", {
|
|
549
|
+
error: message,
|
|
550
|
+
errorName: error instanceof Error ? error.name : "Error"
|
|
551
|
+
});
|
|
552
|
+
} finally {
|
|
553
|
+
if (controller && activeController === controller)
|
|
554
|
+
activeController = null;
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
await WorkerBody.postMessage("ready", {});
|
|
558
|
+
}
|
|
559
|
+
async function* createDemandDrivenIterator(inputBatches) {
|
|
560
|
+
while (true) {
|
|
561
|
+
await WorkerBody.postMessage("input-request", {});
|
|
562
|
+
const nextBatch = await inputBatches.next();
|
|
563
|
+
if (nextBatch.done) {
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
yield nextBatch.value;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
function processOnMainThread(arrayBuffer, options = {}, jobContext = {}) {
|
|
570
|
+
const signal = activeController?.signal;
|
|
571
|
+
signal?.throwIfAborted();
|
|
572
|
+
return new Promise((resolve, reject) => {
|
|
573
|
+
const id = requestId++;
|
|
574
|
+
const cleanup = () => {
|
|
575
|
+
signal?.removeEventListener("abort", abort);
|
|
576
|
+
void WorkerBody.removeEventListener(onMessage);
|
|
577
|
+
};
|
|
578
|
+
const abort = () => {
|
|
579
|
+
cleanup();
|
|
580
|
+
reject(signal?.reason);
|
|
581
|
+
};
|
|
582
|
+
const onMessage = (type, payload) => {
|
|
583
|
+
if (payload.id !== id)
|
|
584
|
+
return;
|
|
585
|
+
if (type === "done") {
|
|
586
|
+
cleanup();
|
|
587
|
+
resolve(payload.result);
|
|
588
|
+
} else if (type === "error") {
|
|
589
|
+
cleanup();
|
|
590
|
+
reject(payload.error);
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
void WorkerBody.addEventListener(onMessage).then(async () => {
|
|
594
|
+
if (signal?.aborted) {
|
|
595
|
+
abort();
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
599
|
+
await WorkerBody.postMessage("process", {
|
|
600
|
+
id,
|
|
601
|
+
input: arrayBuffer,
|
|
602
|
+
options,
|
|
603
|
+
context: jobContext
|
|
604
|
+
});
|
|
605
|
+
}).catch((error) => {
|
|
606
|
+
cleanup();
|
|
607
|
+
reject(error);
|
|
608
|
+
});
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
function createProgressReporter() {
|
|
612
|
+
let pending2;
|
|
613
|
+
let finished = false;
|
|
614
|
+
let scheduled = false;
|
|
615
|
+
const flush = () => {
|
|
616
|
+
scheduled = false;
|
|
617
|
+
if (pending2) {
|
|
618
|
+
WorkerBody.postMessage("progress", { progress: pending2 });
|
|
619
|
+
pending2 = void 0;
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
return {
|
|
623
|
+
/** Records progress while retaining phase transitions. */
|
|
624
|
+
report(progress) {
|
|
625
|
+
if (finished)
|
|
626
|
+
return;
|
|
627
|
+
if (pending2 && pending2.phase !== progress.phase)
|
|
628
|
+
flush();
|
|
629
|
+
pending2 = { ...progress };
|
|
630
|
+
if (!scheduled) {
|
|
631
|
+
scheduled = true;
|
|
632
|
+
setTimeout(flush, 0);
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
/** Flushes final progress and suppresses reports from stale callbacks. */
|
|
636
|
+
finish(discard = false) {
|
|
637
|
+
if (discard)
|
|
638
|
+
pending2 = void 0;
|
|
639
|
+
flush();
|
|
640
|
+
finished = true;
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// ../../node_modules/@math.gl/core/dist/worker-utils/index.js
|
|
646
|
+
var import_meta = {};
|
|
647
|
+
var path = typeof __filename === "string" ? __filename.replace(/\\/g, "/") : "";
|
|
648
|
+
var moduleUrl = path ? `file://${path.startsWith("/") ? "" : "/"}${encodeURI(path).replace(/#/g, "%23").replace(/\?/g, "%3F")}` : import_meta.url;
|
|
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
|
+
|
|
917
|
+
// ../core/src/loader-utils/lib/worker-loader-utils/create-loader-worker.ts
|
|
918
|
+
async function createLoaderWorker(loader, selectLoader = () => loader) {
|
|
919
|
+
await createWorker2(
|
|
920
|
+
(input, options, workerContext, loaderContext) => processLoaderWorkerData(
|
|
921
|
+
selectLoader(options || {}),
|
|
922
|
+
input,
|
|
923
|
+
options,
|
|
924
|
+
workerContext,
|
|
925
|
+
loaderContext
|
|
926
|
+
),
|
|
927
|
+
(inputIterator, options, workerContext, loaderContext) => processLoaderWorkerBatches(
|
|
928
|
+
selectLoader(options || {}),
|
|
929
|
+
inputIterator,
|
|
930
|
+
options,
|
|
931
|
+
workerContext,
|
|
932
|
+
loaderContext
|
|
933
|
+
)
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
async function processLoaderWorkerData(loader, input, options = {}, workerContext, loaderContext = {}) {
|
|
937
|
+
const result = await parseData({
|
|
938
|
+
loader,
|
|
939
|
+
arrayBuffer: input,
|
|
940
|
+
options,
|
|
941
|
+
context: {
|
|
942
|
+
...loaderContext,
|
|
943
|
+
reportProgress: workerContext?.reportProgress,
|
|
944
|
+
coreApi: createWorkerCoreApi(),
|
|
945
|
+
_parse: createParseOnMainThread(workerContext?.process)
|
|
946
|
+
}
|
|
947
|
+
});
|
|
948
|
+
return loader.serializeWorkerResult ? loader.serializeWorkerResult(result, options, loaderContext) : result;
|
|
949
|
+
}
|
|
950
|
+
async function* processLoaderWorkerBatches(loader, inputIterator, options = {}, workerContext, loaderContext = {}) {
|
|
951
|
+
if (!loader.parseInBatches) {
|
|
952
|
+
throw new Error(`${loader.id} loader does not support batched parsing`);
|
|
953
|
+
}
|
|
954
|
+
const parserOptions = getWorkerLoaderOptions(loader, options);
|
|
955
|
+
const resultIterator = loader.parseInBatches(inputIterator, parserOptions, {
|
|
956
|
+
...loaderContext,
|
|
957
|
+
reportProgress: workerContext?.reportProgress,
|
|
958
|
+
coreApi: createWorkerCoreApi(),
|
|
959
|
+
_parse: createParseOnMainThread(workerContext?.process)
|
|
960
|
+
});
|
|
961
|
+
for await (const batch of resultIterator) {
|
|
962
|
+
yield loader.serializeWorkerBatch ? loader.serializeWorkerBatch(batch, options, loaderContext) : batch;
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
function createWorkerCoreApi() {
|
|
966
|
+
const unavailable = (methodName) => () => {
|
|
967
|
+
throw new Error(`context.coreApi.${methodName} is unavailable inside worker loaders.`);
|
|
968
|
+
};
|
|
969
|
+
return {
|
|
970
|
+
fetchFile: async (urlOrData, fetchOptions) => await fetch(urlOrData, fetchOptions),
|
|
971
|
+
parseSync: unavailable("parseSync"),
|
|
972
|
+
parse: unavailable("parse"),
|
|
973
|
+
parseFile: unavailable("parseFile"),
|
|
974
|
+
parseInBatches: unavailable("parseInBatches"),
|
|
975
|
+
load: unavailable("load"),
|
|
976
|
+
loadInBatches: unavailable("loadInBatches")
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
function createParseOnMainThread(processOnMainThread2) {
|
|
980
|
+
return (arrayBuffer, loaders, options, context) => {
|
|
981
|
+
if (!processOnMainThread2) {
|
|
982
|
+
throw new Error("Worker not set up to parse on main thread");
|
|
983
|
+
}
|
|
984
|
+
const parseArguments = getMainThreadParseArguments(loaders, options, context);
|
|
985
|
+
return processOnMainThread2(arrayBuffer, parseArguments.options, {
|
|
986
|
+
...parseArguments.context,
|
|
987
|
+
...Array.isArray(loaders) || loaders && isLoaderObject(loaders) ? {
|
|
988
|
+
_loaderIsArray: Array.isArray(loaders),
|
|
989
|
+
_loaderIds: (Array.isArray(loaders) ? loaders : [loaders]).map((loader) => loader.id)
|
|
990
|
+
} : {}
|
|
991
|
+
});
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
function getMainThreadParseArguments(loaders, options, context) {
|
|
995
|
+
if (options) {
|
|
996
|
+
return { options, context: getSerializableLoaderContext(context) };
|
|
997
|
+
}
|
|
998
|
+
if (Array.isArray(loaders) || loaders && isLoaderObject(loaders)) {
|
|
999
|
+
return { options: void 0, context: getSerializableLoaderContext(context) };
|
|
1000
|
+
}
|
|
1001
|
+
if (loaders && !Array.isArray(loaders)) {
|
|
1002
|
+
return { options: loaders };
|
|
1003
|
+
}
|
|
1004
|
+
return { options: void 0, context: getSerializableLoaderContext(context) };
|
|
1005
|
+
}
|
|
1006
|
+
function isLoaderObject(value) {
|
|
1007
|
+
return "id" in value && "extensions" in value;
|
|
1008
|
+
}
|
|
1009
|
+
function getSerializableLoaderContext(context) {
|
|
1010
|
+
if (!context) {
|
|
1011
|
+
return void 0;
|
|
1012
|
+
}
|
|
1013
|
+
const {
|
|
1014
|
+
fetch: fetch2,
|
|
1015
|
+
loaders,
|
|
1016
|
+
coreApi,
|
|
1017
|
+
signal,
|
|
1018
|
+
reportProgress,
|
|
1019
|
+
_parse,
|
|
1020
|
+
_parseSync,
|
|
1021
|
+
_parseInBatches,
|
|
1022
|
+
...serializableContext
|
|
1023
|
+
} = context;
|
|
1024
|
+
return JSON.parse(JSON.stringify(serializableContext));
|
|
1025
|
+
}
|
|
1026
|
+
async function parseData({
|
|
1027
|
+
loader,
|
|
1028
|
+
arrayBuffer,
|
|
1029
|
+
options,
|
|
1030
|
+
context
|
|
1031
|
+
}) {
|
|
1032
|
+
let data;
|
|
1033
|
+
let parser;
|
|
1034
|
+
if (loader.parse || loader.parseSync) {
|
|
1035
|
+
data = arrayBuffer;
|
|
1036
|
+
parser = loader.parse || loader.parseSync;
|
|
1037
|
+
} else if (loader.parseTextSync) {
|
|
1038
|
+
const textDecoder = new TextDecoder();
|
|
1039
|
+
data = textDecoder.decode(arrayBuffer);
|
|
1040
|
+
parser = loader.parseTextSync;
|
|
1041
|
+
} else {
|
|
1042
|
+
throw new Error(`Could not load data with ${loader.name} loader`);
|
|
1043
|
+
}
|
|
1044
|
+
options = getWorkerLoaderOptions(loader, options);
|
|
1045
|
+
return await parser(data, { ...options }, context, loader);
|
|
1046
|
+
}
|
|
1047
|
+
function getWorkerLoaderOptions(loader, options) {
|
|
1048
|
+
return {
|
|
1049
|
+
...options,
|
|
1050
|
+
modules: { ...loader.options?.modules, ...options.modules },
|
|
1051
|
+
core: { ...options.core, worker: false }
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
// src/workers/npy-worker.ts
|
|
1056
|
+
createLoaderWorker(NPYLoaderWithParser);
|
|
1057
|
+
})();
|