@loaders.gl/core 4.4.2 → 5.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dist.dev.js +583 -162
- package/dist/dist.min.js +4 -3
- package/dist/index.cjs +499 -148
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/iterators/make-iterator/make-stream-iterator.js +1 -1
- package/dist/iterators/make-iterator/make-stream-iterator.js.map +1 -1
- package/dist/lib/api/core-api.d.ts +6 -0
- package/dist/lib/api/core-api.d.ts.map +1 -0
- package/dist/lib/api/core-api.js +23 -0
- package/dist/lib/api/core-api.js.map +1 -0
- package/dist/lib/api/create-data-source.d.ts +2 -2
- package/dist/lib/api/create-data-source.d.ts.map +1 -1
- package/dist/lib/api/create-data-source.js +15 -14
- package/dist/lib/api/create-data-source.js.map +1 -1
- package/dist/lib/api/load-in-batches.d.ts +4 -4
- package/dist/lib/api/load-in-batches.d.ts.map +1 -1
- package/dist/lib/api/load-in-batches.js +1 -1
- package/dist/lib/api/load-in-batches.js.map +1 -1
- package/dist/lib/api/load-loader.d.ts +8 -0
- package/dist/lib/api/load-loader.d.ts.map +1 -0
- package/dist/lib/api/load-loader.js +116 -0
- package/dist/lib/api/load-loader.js.map +1 -0
- package/dist/lib/api/load.d.ts +2 -2
- package/dist/lib/api/load.d.ts.map +1 -1
- package/dist/lib/api/load.js +52 -3
- package/dist/lib/api/load.js.map +1 -1
- package/dist/lib/api/parse-file.d.ts +15 -0
- package/dist/lib/api/parse-file.d.ts.map +1 -0
- package/dist/lib/api/parse-file.js +70 -0
- package/dist/lib/api/parse-file.js.map +1 -0
- package/dist/lib/api/parse-in-batches.d.ts +2 -2
- package/dist/lib/api/parse-in-batches.d.ts.map +1 -1
- package/dist/lib/api/parse-in-batches.js +6 -4
- package/dist/lib/api/parse-in-batches.js.map +1 -1
- package/dist/lib/api/parse-sync.d.ts +2 -2
- package/dist/lib/api/parse-sync.d.ts.map +1 -1
- package/dist/lib/api/parse-sync.js +10 -1
- package/dist/lib/api/parse-sync.js.map +1 -1
- package/dist/lib/api/parse.d.ts +2 -2
- package/dist/lib/api/parse.d.ts.map +1 -1
- package/dist/lib/api/parse.js +25 -16
- package/dist/lib/api/parse.js.map +1 -1
- package/dist/lib/api/preload.d.ts +11 -0
- package/dist/lib/api/preload.d.ts.map +1 -0
- package/dist/lib/api/preload.js +14 -0
- package/dist/lib/api/preload.js.map +1 -0
- package/dist/lib/api/register-loaders.js +1 -1
- package/dist/lib/api/register-loaders.js.map +1 -1
- package/dist/lib/api/select-loader.d.ts.map +1 -1
- package/dist/lib/api/select-loader.js +48 -5
- package/dist/lib/api/select-loader.js.map +1 -1
- package/dist/lib/api/select-source.d.ts +3 -3
- package/dist/lib/api/select-source.d.ts.map +1 -1
- package/dist/lib/api/select-source.js +2 -5
- package/dist/lib/api/select-source.js.map +1 -1
- package/dist/lib/fetch/fetch-error-message.js +1 -1
- package/dist/lib/fetch/fetch-error-message.js.map +1 -1
- package/dist/lib/init.js +1 -1
- package/dist/lib/init.js.map +1 -1
- package/dist/lib/loader-utils/check-errors.js +1 -1
- package/dist/lib/loader-utils/check-errors.js.map +1 -1
- package/dist/lib/loader-utils/get-data.d.ts +4 -0
- package/dist/lib/loader-utils/get-data.d.ts.map +1 -1
- package/dist/lib/loader-utils/get-data.js +26 -0
- package/dist/lib/loader-utils/get-data.js.map +1 -1
- package/dist/lib/loader-utils/get-fetch-function.d.ts +1 -1
- package/dist/lib/loader-utils/get-fetch-function.d.ts.map +1 -1
- package/dist/lib/loader-utils/get-fetch-function.js +1 -1
- package/dist/lib/loader-utils/get-fetch-function.js.map +1 -1
- package/dist/lib/loader-utils/loader-context.d.ts +1 -1
- package/dist/lib/loader-utils/loader-context.d.ts.map +1 -1
- package/dist/lib/loader-utils/loader-context.js +3 -1
- package/dist/lib/loader-utils/loader-context.js.map +1 -1
- package/dist/lib/loader-utils/loggers.d.ts +19 -0
- package/dist/lib/loader-utils/loggers.d.ts.map +1 -1
- package/dist/lib/loader-utils/loggers.js +28 -2
- package/dist/lib/loader-utils/loggers.js.map +1 -1
- package/dist/lib/loader-utils/option-defaults.d.ts +1 -0
- package/dist/lib/loader-utils/option-defaults.d.ts.map +1 -1
- package/dist/lib/loader-utils/option-defaults.js +2 -0
- package/dist/lib/loader-utils/option-defaults.js.map +1 -1
- package/dist/lib/loader-utils/option-utils.d.ts.map +1 -1
- package/dist/lib/loader-utils/option-utils.js +50 -2
- package/dist/lib/loader-utils/option-utils.js.map +1 -1
- package/dist/lib/utils/response-utils.js +3 -3
- package/dist/lib/utils/response-utils.js.map +1 -1
- package/dist/null-loader.d.ts.map +1 -1
- package/dist/null-loader.js +1 -1
- package/dist/null-loader.js.map +1 -1
- package/dist/null-worker-node.js +199 -36
- package/dist/null-worker.js +199 -36
- package/package.json +6 -6
- package/src/index.ts +10 -1
- package/src/iterators/make-iterator/make-stream-iterator.ts +1 -1
- package/src/lib/api/core-api.ts +25 -0
- package/src/lib/api/create-data-source.ts +24 -24
- package/src/lib/api/load-in-batches.ts +16 -11
- package/src/lib/api/load-loader.ts +163 -0
- package/src/lib/api/load.ts +78 -5
- package/src/lib/api/parse-file.ts +143 -0
- package/src/lib/api/parse-in-batches.ts +13 -5
- package/src/lib/api/parse-sync.ts +27 -2
- package/src/lib/api/parse.ts +44 -18
- package/src/lib/api/preload.ts +31 -0
- package/src/lib/api/register-loaders.ts +1 -1
- package/src/lib/api/select-loader.ts +57 -5
- package/src/lib/api/select-source.ts +7 -10
- package/src/lib/fetch/fetch-error-message.ts +1 -1
- package/src/lib/loader-utils/check-errors.ts +1 -1
- package/src/lib/loader-utils/get-data.ts +36 -0
- package/src/lib/loader-utils/get-fetch-function.ts +3 -2
- package/src/lib/loader-utils/loader-context.ts +5 -2
- package/src/lib/loader-utils/loggers.ts +28 -2
- package/src/lib/loader-utils/option-defaults.ts +2 -0
- package/src/lib/loader-utils/option-utils.ts +59 -2
- package/src/lib/utils/response-utils.ts +3 -3
- package/src/null-loader.ts +3 -3
package/dist/null-worker-node.js
CHANGED
|
@@ -153,38 +153,137 @@ function isKnownMessage(message) {
|
|
|
153
153
|
return type === "message" && data && typeof data.source === "string" && data.source.startsWith("loaders.gl");
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
// ../
|
|
156
|
+
// ../worker-utils/src/lib/async-queue/async-queue.ts
|
|
157
|
+
var AsyncQueue = class {
|
|
158
|
+
_values;
|
|
159
|
+
_settlers;
|
|
160
|
+
_closed;
|
|
161
|
+
constructor() {
|
|
162
|
+
this._values = [];
|
|
163
|
+
this._settlers = [];
|
|
164
|
+
this._closed = false;
|
|
165
|
+
}
|
|
166
|
+
/** Return an async iterator for this queue */
|
|
167
|
+
[Symbol.asyncIterator]() {
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
/** Push a new value - the async iterator will yield a promise resolved to this value */
|
|
171
|
+
push(value) {
|
|
172
|
+
return this.enqueue(value);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Push a new value - the async iterator will yield a promise resolved to this value
|
|
176
|
+
* Add an error - the async iterator will yield a promise rejected with this value
|
|
177
|
+
*/
|
|
178
|
+
enqueue(value) {
|
|
179
|
+
if (this._closed) {
|
|
180
|
+
throw new Error("Closed");
|
|
181
|
+
}
|
|
182
|
+
if (this._settlers.length > 0) {
|
|
183
|
+
if (this._values.length > 0) {
|
|
184
|
+
throw new Error("Illegal internal state");
|
|
185
|
+
}
|
|
186
|
+
const settler = this._settlers.shift();
|
|
187
|
+
if (value instanceof Error) {
|
|
188
|
+
settler.reject(value);
|
|
189
|
+
} else {
|
|
190
|
+
settler.resolve({ value });
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
this._values.push(value);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/** Indicate that we not waiting for more values - The async iterator will be done */
|
|
197
|
+
close() {
|
|
198
|
+
while (this._settlers.length > 0) {
|
|
199
|
+
const settler = this._settlers.shift();
|
|
200
|
+
settler.resolve({ done: true });
|
|
201
|
+
}
|
|
202
|
+
this._closed = true;
|
|
203
|
+
}
|
|
204
|
+
// ITERATOR IMPLEMENTATION
|
|
205
|
+
/** @returns a Promise for an IteratorResult */
|
|
206
|
+
next() {
|
|
207
|
+
if (this._values.length > 0) {
|
|
208
|
+
const value = this._values.shift();
|
|
209
|
+
if (value instanceof Error) {
|
|
210
|
+
return Promise.reject(value);
|
|
211
|
+
}
|
|
212
|
+
return Promise.resolve({ done: false, value });
|
|
213
|
+
}
|
|
214
|
+
if (this._closed) {
|
|
215
|
+
if (this._settlers.length > 0) {
|
|
216
|
+
throw new Error("Illegal internal state");
|
|
217
|
+
}
|
|
218
|
+
return Promise.resolve({ done: true, value: void 0 });
|
|
219
|
+
}
|
|
220
|
+
return new Promise((resolve, reject) => {
|
|
221
|
+
this._settlers.push({ resolve, reject });
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
// ../worker-utils/src/lib/worker-api/create-worker.ts
|
|
157
227
|
var requestId = 0;
|
|
158
|
-
|
|
228
|
+
var inputBatches;
|
|
229
|
+
var options;
|
|
230
|
+
async function createWorker(process, processInBatches) {
|
|
159
231
|
if (!await WorkerBody.inWorkerThread()) {
|
|
160
232
|
return;
|
|
161
233
|
}
|
|
234
|
+
const context = {
|
|
235
|
+
process: processOnMainThread
|
|
236
|
+
};
|
|
162
237
|
WorkerBody.onmessage = async (type, payload) => {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
238
|
+
try {
|
|
239
|
+
switch (type) {
|
|
240
|
+
case "preload":
|
|
241
|
+
WorkerBody.postMessage("done", {});
|
|
242
|
+
break;
|
|
243
|
+
case "process":
|
|
244
|
+
if (!process) {
|
|
245
|
+
throw new Error("Worker does not support atomic processing");
|
|
246
|
+
}
|
|
247
|
+
const result = await process(
|
|
248
|
+
payload.input,
|
|
249
|
+
payload.options || {},
|
|
250
|
+
context,
|
|
251
|
+
payload.context || {}
|
|
252
|
+
);
|
|
177
253
|
WorkerBody.postMessage("done", { result });
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
254
|
+
break;
|
|
255
|
+
case "process-in-batches":
|
|
256
|
+
if (!processInBatches) {
|
|
257
|
+
throw new Error("Worker does not support batched processing");
|
|
258
|
+
}
|
|
259
|
+
inputBatches = new AsyncQueue();
|
|
260
|
+
options = payload.options || {};
|
|
261
|
+
const resultIterator = processInBatches(
|
|
262
|
+
inputBatches,
|
|
263
|
+
options,
|
|
264
|
+
context,
|
|
265
|
+
payload.context || {}
|
|
266
|
+
);
|
|
267
|
+
for await (const batch of resultIterator) {
|
|
268
|
+
WorkerBody.postMessage("output-batch", { result: batch });
|
|
269
|
+
}
|
|
270
|
+
WorkerBody.postMessage("done", {});
|
|
271
|
+
break;
|
|
272
|
+
case "input-batch":
|
|
273
|
+
inputBatches.push(payload.input);
|
|
274
|
+
break;
|
|
275
|
+
case "input-done":
|
|
276
|
+
inputBatches.close();
|
|
277
|
+
break;
|
|
278
|
+
default:
|
|
279
|
+
}
|
|
280
|
+
} catch (error) {
|
|
281
|
+
const message = error instanceof Error ? error.message : "";
|
|
282
|
+
WorkerBody.postMessage("error", { error: message });
|
|
184
283
|
}
|
|
185
284
|
};
|
|
186
285
|
}
|
|
187
|
-
function
|
|
286
|
+
function processOnMainThread(arrayBuffer, options2 = {}, jobContext = {}) {
|
|
188
287
|
return new Promise((resolve, reject) => {
|
|
189
288
|
const id = requestId++;
|
|
190
289
|
const onMessage = (type, payload2) => {
|
|
@@ -204,21 +303,85 @@ function parseOnMainThread(arrayBuffer, loader, options, context) {
|
|
|
204
303
|
}
|
|
205
304
|
};
|
|
206
305
|
WorkerBody.addEventListener(onMessage);
|
|
207
|
-
const payload = { id, input: arrayBuffer, options };
|
|
306
|
+
const payload = { id, input: arrayBuffer, options: options2, context: jobContext };
|
|
208
307
|
WorkerBody.postMessage("process", payload);
|
|
209
308
|
});
|
|
210
309
|
}
|
|
310
|
+
|
|
311
|
+
// ../loader-utils/src/lib/worker-loader-utils/create-loader-worker.ts
|
|
312
|
+
async function createLoaderWorker(loader) {
|
|
313
|
+
await createWorker(
|
|
314
|
+
async (input, options2 = {}, workerContext, loaderContext = {}) => {
|
|
315
|
+
const result = await parseData({
|
|
316
|
+
loader,
|
|
317
|
+
arrayBuffer: input,
|
|
318
|
+
options: options2,
|
|
319
|
+
context: {
|
|
320
|
+
...loaderContext,
|
|
321
|
+
coreApi: createWorkerCoreApi(),
|
|
322
|
+
_parse: createParseOnMainThread(workerContext == null ? void 0 : workerContext.process)
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
return loader.serializeWorkerResult ? loader.serializeWorkerResult(result, options2, loaderContext) : result;
|
|
326
|
+
}
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
function createWorkerCoreApi() {
|
|
330
|
+
const unavailable = (methodName) => () => {
|
|
331
|
+
throw new Error(`context.coreApi.${methodName} is unavailable inside worker loaders.`);
|
|
332
|
+
};
|
|
333
|
+
return {
|
|
334
|
+
fetchFile: async (urlOrData, fetchOptions) => await fetch(urlOrData, fetchOptions),
|
|
335
|
+
parseSync: unavailable("parseSync"),
|
|
336
|
+
parse: unavailable("parse"),
|
|
337
|
+
parseFile: unavailable("parseFile"),
|
|
338
|
+
parseInBatches: unavailable("parseInBatches"),
|
|
339
|
+
load: unavailable("load"),
|
|
340
|
+
loadInBatches: unavailable("loadInBatches")
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
function createParseOnMainThread(processOnMainThread2) {
|
|
344
|
+
return (arrayBuffer, loaders, options2, context) => {
|
|
345
|
+
if (!processOnMainThread2) {
|
|
346
|
+
throw new Error("Worker not set up to parse on main thread");
|
|
347
|
+
}
|
|
348
|
+
const parseArguments = getMainThreadParseArguments(loaders, options2, context);
|
|
349
|
+
return processOnMainThread2(arrayBuffer, parseArguments.options, parseArguments.context);
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function getMainThreadParseArguments(loaders, options2, context) {
|
|
353
|
+
if (options2) {
|
|
354
|
+
return { options: options2, context: getSerializableLoaderContext(context) };
|
|
355
|
+
}
|
|
356
|
+
if (Array.isArray(loaders) || loaders && isLoaderObject(loaders)) {
|
|
357
|
+
return { options: void 0, context: getSerializableLoaderContext(context) };
|
|
358
|
+
}
|
|
359
|
+
if (loaders && !Array.isArray(loaders)) {
|
|
360
|
+
return { options: loaders };
|
|
361
|
+
}
|
|
362
|
+
return { options: void 0, context: getSerializableLoaderContext(context) };
|
|
363
|
+
}
|
|
364
|
+
function isLoaderObject(value) {
|
|
365
|
+
return "id" in value && "extensions" in value;
|
|
366
|
+
}
|
|
367
|
+
function getSerializableLoaderContext(context) {
|
|
368
|
+
if (!context) {
|
|
369
|
+
return void 0;
|
|
370
|
+
}
|
|
371
|
+
const { fetch: fetch2, loaders, coreApi, _parse, _parseSync, _parseInBatches, ...serializableContext } = context;
|
|
372
|
+
return JSON.parse(JSON.stringify(serializableContext));
|
|
373
|
+
}
|
|
211
374
|
async function parseData({
|
|
212
375
|
loader,
|
|
213
376
|
arrayBuffer,
|
|
214
|
-
options,
|
|
377
|
+
options: options2,
|
|
215
378
|
context
|
|
216
379
|
}) {
|
|
217
380
|
let data;
|
|
218
381
|
let parser;
|
|
219
|
-
if (loader.
|
|
382
|
+
if (loader.parse || loader.parseSync) {
|
|
220
383
|
data = arrayBuffer;
|
|
221
|
-
parser = loader.
|
|
384
|
+
parser = loader.parse || loader.parseSync;
|
|
222
385
|
} else if (loader.parseTextSync) {
|
|
223
386
|
const textDecoder = new TextDecoder();
|
|
224
387
|
data = textDecoder.decode(arrayBuffer);
|
|
@@ -226,19 +389,19 @@ async function parseData({
|
|
|
226
389
|
} else {
|
|
227
390
|
throw new Error(`Could not load data with ${loader.name} loader`);
|
|
228
391
|
}
|
|
229
|
-
|
|
230
|
-
...
|
|
392
|
+
options2 = {
|
|
393
|
+
...options2,
|
|
231
394
|
modules: loader && loader.options && loader.options.modules || {},
|
|
232
395
|
core: {
|
|
233
|
-
...
|
|
396
|
+
...options2.core,
|
|
234
397
|
worker: false
|
|
235
398
|
}
|
|
236
399
|
};
|
|
237
|
-
return await parser(data, { ...
|
|
400
|
+
return await parser(data, { ...options2 }, context, loader);
|
|
238
401
|
}
|
|
239
402
|
|
|
240
403
|
// src/null-loader.ts
|
|
241
|
-
var VERSION = true ? "
|
|
404
|
+
var VERSION = true ? "5.0.0-alpha.1" : "latest";
|
|
242
405
|
var NullLoader = {
|
|
243
406
|
dataType: null,
|
|
244
407
|
batchType: null,
|
|
@@ -248,11 +411,11 @@ var NullLoader = {
|
|
|
248
411
|
version: VERSION,
|
|
249
412
|
mimeTypes: ["application/x.empty"],
|
|
250
413
|
extensions: ["null"],
|
|
251
|
-
parse: async (arrayBuffer,
|
|
414
|
+
parse: async (arrayBuffer, options2, context) => parseSync(arrayBuffer, options2 || {}, context),
|
|
252
415
|
parseSync,
|
|
253
|
-
parseInBatches: async function* generator(asyncIterator,
|
|
416
|
+
parseInBatches: async function* generator(asyncIterator, options2, context) {
|
|
254
417
|
for await (const batch of asyncIterator) {
|
|
255
|
-
yield parseSync(batch,
|
|
418
|
+
yield parseSync(batch, options2, context);
|
|
256
419
|
}
|
|
257
420
|
},
|
|
258
421
|
tests: [() => false],
|
|
@@ -260,7 +423,7 @@ var NullLoader = {
|
|
|
260
423
|
null: {}
|
|
261
424
|
}
|
|
262
425
|
};
|
|
263
|
-
function parseSync(arrayBuffer,
|
|
426
|
+
function parseSync(arrayBuffer, options2, context) {
|
|
264
427
|
return null;
|
|
265
428
|
}
|
|
266
429
|
|
package/dist/null-worker.js
CHANGED
|
@@ -118,38 +118,137 @@
|
|
|
118
118
|
return type === "message" && data && typeof data.source === "string" && data.source.startsWith("loaders.gl");
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
// ../
|
|
121
|
+
// ../worker-utils/src/lib/async-queue/async-queue.ts
|
|
122
|
+
var AsyncQueue = class {
|
|
123
|
+
_values;
|
|
124
|
+
_settlers;
|
|
125
|
+
_closed;
|
|
126
|
+
constructor() {
|
|
127
|
+
this._values = [];
|
|
128
|
+
this._settlers = [];
|
|
129
|
+
this._closed = false;
|
|
130
|
+
}
|
|
131
|
+
/** Return an async iterator for this queue */
|
|
132
|
+
[Symbol.asyncIterator]() {
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
/** Push a new value - the async iterator will yield a promise resolved to this value */
|
|
136
|
+
push(value) {
|
|
137
|
+
return this.enqueue(value);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Push a new value - the async iterator will yield a promise resolved to this value
|
|
141
|
+
* Add an error - the async iterator will yield a promise rejected with this value
|
|
142
|
+
*/
|
|
143
|
+
enqueue(value) {
|
|
144
|
+
if (this._closed) {
|
|
145
|
+
throw new Error("Closed");
|
|
146
|
+
}
|
|
147
|
+
if (this._settlers.length > 0) {
|
|
148
|
+
if (this._values.length > 0) {
|
|
149
|
+
throw new Error("Illegal internal state");
|
|
150
|
+
}
|
|
151
|
+
const settler = this._settlers.shift();
|
|
152
|
+
if (value instanceof Error) {
|
|
153
|
+
settler.reject(value);
|
|
154
|
+
} else {
|
|
155
|
+
settler.resolve({ value });
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
this._values.push(value);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** Indicate that we not waiting for more values - The async iterator will be done */
|
|
162
|
+
close() {
|
|
163
|
+
while (this._settlers.length > 0) {
|
|
164
|
+
const settler = this._settlers.shift();
|
|
165
|
+
settler.resolve({ done: true });
|
|
166
|
+
}
|
|
167
|
+
this._closed = true;
|
|
168
|
+
}
|
|
169
|
+
// ITERATOR IMPLEMENTATION
|
|
170
|
+
/** @returns a Promise for an IteratorResult */
|
|
171
|
+
next() {
|
|
172
|
+
if (this._values.length > 0) {
|
|
173
|
+
const value = this._values.shift();
|
|
174
|
+
if (value instanceof Error) {
|
|
175
|
+
return Promise.reject(value);
|
|
176
|
+
}
|
|
177
|
+
return Promise.resolve({ done: false, value });
|
|
178
|
+
}
|
|
179
|
+
if (this._closed) {
|
|
180
|
+
if (this._settlers.length > 0) {
|
|
181
|
+
throw new Error("Illegal internal state");
|
|
182
|
+
}
|
|
183
|
+
return Promise.resolve({ done: true, value: void 0 });
|
|
184
|
+
}
|
|
185
|
+
return new Promise((resolve, reject) => {
|
|
186
|
+
this._settlers.push({ resolve, reject });
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// ../worker-utils/src/lib/worker-api/create-worker.ts
|
|
122
192
|
var requestId = 0;
|
|
123
|
-
|
|
193
|
+
var inputBatches;
|
|
194
|
+
var options;
|
|
195
|
+
async function createWorker(process, processInBatches) {
|
|
124
196
|
if (!await WorkerBody.inWorkerThread()) {
|
|
125
197
|
return;
|
|
126
198
|
}
|
|
199
|
+
const context = {
|
|
200
|
+
process: processOnMainThread
|
|
201
|
+
};
|
|
127
202
|
WorkerBody.onmessage = async (type, payload) => {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
203
|
+
try {
|
|
204
|
+
switch (type) {
|
|
205
|
+
case "preload":
|
|
206
|
+
WorkerBody.postMessage("done", {});
|
|
207
|
+
break;
|
|
208
|
+
case "process":
|
|
209
|
+
if (!process) {
|
|
210
|
+
throw new Error("Worker does not support atomic processing");
|
|
211
|
+
}
|
|
212
|
+
const result = await process(
|
|
213
|
+
payload.input,
|
|
214
|
+
payload.options || {},
|
|
215
|
+
context,
|
|
216
|
+
payload.context || {}
|
|
217
|
+
);
|
|
142
218
|
WorkerBody.postMessage("done", { result });
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
219
|
+
break;
|
|
220
|
+
case "process-in-batches":
|
|
221
|
+
if (!processInBatches) {
|
|
222
|
+
throw new Error("Worker does not support batched processing");
|
|
223
|
+
}
|
|
224
|
+
inputBatches = new AsyncQueue();
|
|
225
|
+
options = payload.options || {};
|
|
226
|
+
const resultIterator = processInBatches(
|
|
227
|
+
inputBatches,
|
|
228
|
+
options,
|
|
229
|
+
context,
|
|
230
|
+
payload.context || {}
|
|
231
|
+
);
|
|
232
|
+
for await (const batch of resultIterator) {
|
|
233
|
+
WorkerBody.postMessage("output-batch", { result: batch });
|
|
234
|
+
}
|
|
235
|
+
WorkerBody.postMessage("done", {});
|
|
236
|
+
break;
|
|
237
|
+
case "input-batch":
|
|
238
|
+
inputBatches.push(payload.input);
|
|
239
|
+
break;
|
|
240
|
+
case "input-done":
|
|
241
|
+
inputBatches.close();
|
|
242
|
+
break;
|
|
243
|
+
default:
|
|
244
|
+
}
|
|
245
|
+
} catch (error) {
|
|
246
|
+
const message = error instanceof Error ? error.message : "";
|
|
247
|
+
WorkerBody.postMessage("error", { error: message });
|
|
149
248
|
}
|
|
150
249
|
};
|
|
151
250
|
}
|
|
152
|
-
function
|
|
251
|
+
function processOnMainThread(arrayBuffer, options2 = {}, jobContext = {}) {
|
|
153
252
|
return new Promise((resolve, reject) => {
|
|
154
253
|
const id = requestId++;
|
|
155
254
|
const onMessage = (type, payload2) => {
|
|
@@ -169,21 +268,85 @@
|
|
|
169
268
|
}
|
|
170
269
|
};
|
|
171
270
|
WorkerBody.addEventListener(onMessage);
|
|
172
|
-
const payload = { id, input: arrayBuffer, options };
|
|
271
|
+
const payload = { id, input: arrayBuffer, options: options2, context: jobContext };
|
|
173
272
|
WorkerBody.postMessage("process", payload);
|
|
174
273
|
});
|
|
175
274
|
}
|
|
275
|
+
|
|
276
|
+
// ../loader-utils/src/lib/worker-loader-utils/create-loader-worker.ts
|
|
277
|
+
async function createLoaderWorker(loader) {
|
|
278
|
+
await createWorker(
|
|
279
|
+
async (input, options2 = {}, workerContext, loaderContext = {}) => {
|
|
280
|
+
const result = await parseData({
|
|
281
|
+
loader,
|
|
282
|
+
arrayBuffer: input,
|
|
283
|
+
options: options2,
|
|
284
|
+
context: {
|
|
285
|
+
...loaderContext,
|
|
286
|
+
coreApi: createWorkerCoreApi(),
|
|
287
|
+
_parse: createParseOnMainThread(workerContext?.process)
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
return loader.serializeWorkerResult ? loader.serializeWorkerResult(result, options2, loaderContext) : result;
|
|
291
|
+
}
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
function createWorkerCoreApi() {
|
|
295
|
+
const unavailable = (methodName) => () => {
|
|
296
|
+
throw new Error(`context.coreApi.${methodName} is unavailable inside worker loaders.`);
|
|
297
|
+
};
|
|
298
|
+
return {
|
|
299
|
+
fetchFile: async (urlOrData, fetchOptions) => await fetch(urlOrData, fetchOptions),
|
|
300
|
+
parseSync: unavailable("parseSync"),
|
|
301
|
+
parse: unavailable("parse"),
|
|
302
|
+
parseFile: unavailable("parseFile"),
|
|
303
|
+
parseInBatches: unavailable("parseInBatches"),
|
|
304
|
+
load: unavailable("load"),
|
|
305
|
+
loadInBatches: unavailable("loadInBatches")
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
function createParseOnMainThread(processOnMainThread2) {
|
|
309
|
+
return (arrayBuffer, loaders, options2, context) => {
|
|
310
|
+
if (!processOnMainThread2) {
|
|
311
|
+
throw new Error("Worker not set up to parse on main thread");
|
|
312
|
+
}
|
|
313
|
+
const parseArguments = getMainThreadParseArguments(loaders, options2, context);
|
|
314
|
+
return processOnMainThread2(arrayBuffer, parseArguments.options, parseArguments.context);
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
function getMainThreadParseArguments(loaders, options2, context) {
|
|
318
|
+
if (options2) {
|
|
319
|
+
return { options: options2, context: getSerializableLoaderContext(context) };
|
|
320
|
+
}
|
|
321
|
+
if (Array.isArray(loaders) || loaders && isLoaderObject(loaders)) {
|
|
322
|
+
return { options: void 0, context: getSerializableLoaderContext(context) };
|
|
323
|
+
}
|
|
324
|
+
if (loaders && !Array.isArray(loaders)) {
|
|
325
|
+
return { options: loaders };
|
|
326
|
+
}
|
|
327
|
+
return { options: void 0, context: getSerializableLoaderContext(context) };
|
|
328
|
+
}
|
|
329
|
+
function isLoaderObject(value) {
|
|
330
|
+
return "id" in value && "extensions" in value;
|
|
331
|
+
}
|
|
332
|
+
function getSerializableLoaderContext(context) {
|
|
333
|
+
if (!context) {
|
|
334
|
+
return void 0;
|
|
335
|
+
}
|
|
336
|
+
const { fetch: fetch2, loaders, coreApi, _parse, _parseSync, _parseInBatches, ...serializableContext } = context;
|
|
337
|
+
return JSON.parse(JSON.stringify(serializableContext));
|
|
338
|
+
}
|
|
176
339
|
async function parseData({
|
|
177
340
|
loader,
|
|
178
341
|
arrayBuffer,
|
|
179
|
-
options,
|
|
342
|
+
options: options2,
|
|
180
343
|
context
|
|
181
344
|
}) {
|
|
182
345
|
let data;
|
|
183
346
|
let parser;
|
|
184
|
-
if (loader.
|
|
347
|
+
if (loader.parse || loader.parseSync) {
|
|
185
348
|
data = arrayBuffer;
|
|
186
|
-
parser = loader.
|
|
349
|
+
parser = loader.parse || loader.parseSync;
|
|
187
350
|
} else if (loader.parseTextSync) {
|
|
188
351
|
const textDecoder = new TextDecoder();
|
|
189
352
|
data = textDecoder.decode(arrayBuffer);
|
|
@@ -191,19 +354,19 @@
|
|
|
191
354
|
} else {
|
|
192
355
|
throw new Error(`Could not load data with ${loader.name} loader`);
|
|
193
356
|
}
|
|
194
|
-
|
|
195
|
-
...
|
|
357
|
+
options2 = {
|
|
358
|
+
...options2,
|
|
196
359
|
modules: loader && loader.options && loader.options.modules || {},
|
|
197
360
|
core: {
|
|
198
|
-
...
|
|
361
|
+
...options2.core,
|
|
199
362
|
worker: false
|
|
200
363
|
}
|
|
201
364
|
};
|
|
202
|
-
return await parser(data, { ...
|
|
365
|
+
return await parser(data, { ...options2 }, context, loader);
|
|
203
366
|
}
|
|
204
367
|
|
|
205
368
|
// src/null-loader.ts
|
|
206
|
-
var VERSION = true ? "
|
|
369
|
+
var VERSION = true ? "5.0.0-alpha.1" : "latest";
|
|
207
370
|
var NullLoader = {
|
|
208
371
|
dataType: null,
|
|
209
372
|
batchType: null,
|
|
@@ -213,11 +376,11 @@
|
|
|
213
376
|
version: VERSION,
|
|
214
377
|
mimeTypes: ["application/x.empty"],
|
|
215
378
|
extensions: ["null"],
|
|
216
|
-
parse: async (arrayBuffer,
|
|
379
|
+
parse: async (arrayBuffer, options2, context) => parseSync(arrayBuffer, options2 || {}, context),
|
|
217
380
|
parseSync,
|
|
218
|
-
parseInBatches: async function* generator(asyncIterator,
|
|
381
|
+
parseInBatches: async function* generator(asyncIterator, options2, context) {
|
|
219
382
|
for await (const batch of asyncIterator) {
|
|
220
|
-
yield parseSync(batch,
|
|
383
|
+
yield parseSync(batch, options2, context);
|
|
221
384
|
}
|
|
222
385
|
},
|
|
223
386
|
tests: [() => false],
|
|
@@ -225,7 +388,7 @@
|
|
|
225
388
|
null: {}
|
|
226
389
|
}
|
|
227
390
|
};
|
|
228
|
-
function parseSync(arrayBuffer,
|
|
391
|
+
function parseSync(arrayBuffer, options2, context) {
|
|
229
392
|
return null;
|
|
230
393
|
}
|
|
231
394
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loaders.gl/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "The core API for working with loaders.gl loaders and writers",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -58,11 +58,11 @@
|
|
|
58
58
|
"build-worker-node": "esbuild src/workers/null-worker.ts --outfile=dist/null-worker-node.js --bundle --platform=node --target=node16 --define:__VERSION__=\\\"$npm_package_version\\\""
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@loaders.gl/loader-utils": "
|
|
62
|
-
"@loaders.gl/schema": "
|
|
63
|
-
"@loaders.gl/schema-utils": "
|
|
64
|
-
"@loaders.gl/worker-utils": "
|
|
61
|
+
"@loaders.gl/loader-utils": "5.0.0-alpha.1",
|
|
62
|
+
"@loaders.gl/schema": "5.0.0-alpha.1",
|
|
63
|
+
"@loaders.gl/schema-utils": "5.0.0-alpha.1",
|
|
64
|
+
"@loaders.gl/worker-utils": "5.0.0-alpha.1",
|
|
65
65
|
"@probe.gl/log": "^4.1.1"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "77a55028e8204e09df1258f10cb61ed35353b258"
|
|
68
68
|
}
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ export type {
|
|
|
7
7
|
Loader,
|
|
8
8
|
LoaderWithParser,
|
|
9
9
|
LoaderContext,
|
|
10
|
+
CoreAPI,
|
|
10
11
|
LoaderOptions,
|
|
11
12
|
Writer,
|
|
12
13
|
WriterOptions,
|
|
@@ -19,6 +20,7 @@ export type {
|
|
|
19
20
|
FileSystem,
|
|
20
21
|
RandomAccessFileSystem
|
|
21
22
|
} from '@loaders.gl/loader-utils';
|
|
23
|
+
export type {SourceLoader} from '@loaders.gl/loader-utils';
|
|
22
24
|
|
|
23
25
|
// FILE READING AND WRITING
|
|
24
26
|
export {fetchFile} from './lib/fetch/fetch-file';
|
|
@@ -35,8 +37,11 @@ export {selectLoader, selectLoaderSync} from './lib/api/select-loader';
|
|
|
35
37
|
|
|
36
38
|
// LOADING (READING + PARSING)
|
|
37
39
|
export {parse} from './lib/api/parse';
|
|
40
|
+
export {parseFile} from './lib/api/parse-file';
|
|
38
41
|
export {parseSync} from './lib/api/parse-sync';
|
|
39
42
|
export {parseInBatches} from './lib/api/parse-in-batches';
|
|
43
|
+
export {preload, preloadSync} from './lib/api/preload';
|
|
44
|
+
export {coreApi} from './lib/api/core-api';
|
|
40
45
|
|
|
41
46
|
export {load} from './lib/api/load';
|
|
42
47
|
export {loadInBatches} from './lib/api/load-in-batches';
|
|
@@ -59,7 +64,7 @@ export {makeIterator} from './iterators/make-iterator/make-iterator';
|
|
|
59
64
|
export {makeStream} from './iterators/make-stream/make-stream';
|
|
60
65
|
|
|
61
66
|
// CORE LOADERS
|
|
62
|
-
export {
|
|
67
|
+
export {NullLoader} from './null-loader';
|
|
63
68
|
export {JSONLoader} from '@loaders.gl/loader-utils';
|
|
64
69
|
|
|
65
70
|
// EXPERIMENTAL
|
|
@@ -95,3 +100,7 @@ export {
|
|
|
95
100
|
isReadableStream,
|
|
96
101
|
isWritableStream
|
|
97
102
|
} from '@loaders.gl/loader-utils';
|
|
103
|
+
|
|
104
|
+
// DEPRECATED EXPORTS
|
|
105
|
+
/** @deprecated Use NullLoader. */
|
|
106
|
+
export {NullWorkerLoader} from './null-loader';
|
|
@@ -63,7 +63,7 @@ async function* makeBrowserStreamIterator(
|
|
|
63
63
|
// Else yield the chunk
|
|
64
64
|
yield toArrayBuffer(value);
|
|
65
65
|
}
|
|
66
|
-
} catch (
|
|
66
|
+
} catch (_error) {
|
|
67
67
|
// TODO - examples makes it look like this should always be called,
|
|
68
68
|
// but that generates exceptions so only call it if we do not reach the end
|
|
69
69
|
reader.releaseLock();
|