@loaders.gl/core 5.0.0-alpha.1 → 5.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/LICENSE +14 -3
  2. package/dist/dist.dev.js +1246 -227
  3. package/dist/dist.min.js +3 -3
  4. package/dist/index.cjs +2695 -270
  5. package/dist/index.cjs.map +3 -4
  6. package/dist/iterators/make-iterator/make-stream-iterator.js +49 -10
  7. package/dist/iterators/make-iterator/make-stream-iterator.js.map +1 -1
  8. package/dist/lib/api/create-data-source.d.ts +1 -1
  9. package/dist/lib/api/create-data-source.d.ts.map +1 -1
  10. package/dist/lib/api/create-data-source.js +23 -2
  11. package/dist/lib/api/create-data-source.js.map +1 -1
  12. package/dist/lib/api/load-in-batches.d.ts.map +1 -1
  13. package/dist/lib/api/load-in-batches.js +17 -6
  14. package/dist/lib/api/load-in-batches.js.map +1 -1
  15. package/dist/lib/api/load-loader.d.ts +2 -0
  16. package/dist/lib/api/load-loader.d.ts.map +1 -1
  17. package/dist/lib/api/load-loader.js +113 -7
  18. package/dist/lib/api/load-loader.js.map +1 -1
  19. package/dist/lib/api/load.d.ts +3 -3
  20. package/dist/lib/api/load.d.ts.map +1 -1
  21. package/dist/lib/api/load.js +36 -6
  22. package/dist/lib/api/load.js.map +1 -1
  23. package/dist/lib/api/parse-file.d.ts.map +1 -1
  24. package/dist/lib/api/parse-file.js +6 -2
  25. package/dist/lib/api/parse-file.js.map +1 -1
  26. package/dist/lib/api/parse-in-batches.d.ts.map +1 -1
  27. package/dist/lib/api/parse-in-batches.js +12 -3
  28. package/dist/lib/api/parse-in-batches.js.map +1 -1
  29. package/dist/lib/api/parse.d.ts.map +1 -1
  30. package/dist/lib/api/parse.js +11 -3
  31. package/dist/lib/api/parse.js.map +1 -1
  32. package/dist/lib/api/preload.d.ts +6 -3
  33. package/dist/lib/api/preload.d.ts.map +1 -1
  34. package/dist/lib/api/preload.js +4 -5
  35. package/dist/lib/api/preload.js.map +1 -1
  36. package/dist/lib/filesystems/browser-filesystem.d.ts.map +1 -1
  37. package/dist/lib/filesystems/browser-filesystem.js +25 -11
  38. package/dist/lib/filesystems/browser-filesystem.js.map +1 -1
  39. package/dist/lib/init.js +1 -1
  40. package/dist/lib/init.js.map +1 -1
  41. package/dist/lib/loader-utils/get-fetch-function.d.ts.map +1 -1
  42. package/dist/lib/loader-utils/get-fetch-function.js +16 -10
  43. package/dist/lib/loader-utils/get-fetch-function.js.map +1 -1
  44. package/dist/lib/loader-utils/loader-context.d.ts +1 -1
  45. package/dist/lib/loader-utils/loader-context.d.ts.map +1 -1
  46. package/dist/lib/loader-utils/loader-context.js +8 -3
  47. package/dist/lib/loader-utils/loader-context.js.map +1 -1
  48. package/dist/lib/loader-utils/option-defaults.d.ts.map +1 -1
  49. package/dist/lib/loader-utils/option-defaults.js +3 -0
  50. package/dist/lib/loader-utils/option-defaults.js.map +1 -1
  51. package/dist/lib/loader-utils/option-utils.d.ts +7 -0
  52. package/dist/lib/loader-utils/option-utils.d.ts.map +1 -1
  53. package/dist/lib/loader-utils/option-utils.js +41 -1
  54. package/dist/lib/loader-utils/option-utils.js.map +1 -1
  55. package/dist/lib/utils/response-utils.js +2 -11
  56. package/dist/lib/utils/response-utils.js.map +1 -1
  57. package/dist/null-loader.js +1 -1
  58. package/dist/null-loader.js.map +1 -1
  59. package/dist/null-worker-node.js +130 -58
  60. package/dist/null-worker.js +130 -58
  61. package/package.json +6 -6
  62. package/src/iterators/make-iterator/make-stream-iterator.ts +48 -10
  63. package/src/lib/api/create-data-source.ts +31 -2
  64. package/src/lib/api/load-in-batches.ts +18 -11
  65. package/src/lib/api/load-loader.ts +157 -14
  66. package/src/lib/api/load.ts +87 -9
  67. package/src/lib/api/parse-file.ts +15 -2
  68. package/src/lib/api/parse-in-batches.ts +22 -3
  69. package/src/lib/api/parse.ts +18 -3
  70. package/src/lib/api/preload.ts +22 -7
  71. package/src/lib/filesystems/browser-filesystem.ts +25 -11
  72. package/src/lib/loader-utils/get-fetch-function.ts +15 -14
  73. package/src/lib/loader-utils/loader-context.ts +8 -3
  74. package/src/lib/loader-utils/option-defaults.ts +3 -0
  75. package/src/lib/loader-utils/option-utils.ts +47 -1
  76. package/src/lib/utils/response-utils.ts +2 -12
package/dist/index.cjs CHANGED
@@ -18,22 +18,22 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // dist/index.js
21
- var dist_exports = {};
22
- __export(dist_exports, {
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
23
  FetchError: () => FetchError,
24
- JSONLoader: () => import_loader_utils24.JSONLoader,
24
+ JSONLoader: () => JSONLoader,
25
25
  NullLoader: () => NullLoader,
26
26
  NullWorkerLoader: () => NullWorkerLoader,
27
- RequestScheduler: () => import_loader_utils23.RequestScheduler,
27
+ RequestScheduler: () => RequestScheduler,
28
28
  _BrowserFileSystem: () => BrowserFileSystem,
29
29
  _fetchProgress: () => fetchProgress,
30
30
  _selectSource: () => selectSource,
31
31
  _unregisterLoaders: () => _unregisterLoaders,
32
- assert: () => import_loader_utils26.assert,
33
- concatenateArrayBuffersAsync: () => import_loader_utils27.concatenateArrayBuffersAsync,
32
+ assert: () => assert,
33
+ concatenateArrayBuffersAsync: () => concatenateArrayBuffersAsync,
34
34
  coreApi: () => coreApi,
35
35
  createDataSource: () => createDataSource,
36
- document: () => import_loader_utils25.document,
36
+ document: () => document_,
37
37
  encode: () => encode,
38
38
  encodeInBatches: () => encodeInBatches,
39
39
  encodeSync: () => encodeSync,
@@ -44,28 +44,28 @@ __export(dist_exports, {
44
44
  encodeTextSync: () => encodeTextSync,
45
45
  encodeURLtoURL: () => encodeURLtoURL,
46
46
  fetchFile: () => fetchFile,
47
- forEach: () => import_loader_utils27.forEach,
47
+ forEach: () => forEach,
48
48
  getLoaderOptions: () => getGlobalLoaderOptions,
49
- getPathPrefix: () => import_loader_utils22.getPathPrefix,
50
- global: () => import_loader_utils25.global,
51
- isAsyncIterable: () => import_loader_utils29.isAsyncIterable,
52
- isBrowser: () => import_loader_utils25.isBrowser,
53
- isIterable: () => import_loader_utils29.isIterable,
54
- isIterator: () => import_loader_utils29.isIterator,
55
- isPromise: () => import_loader_utils29.isPromise,
56
- isPureObject: () => import_loader_utils29.isPureObject,
57
- isReadableStream: () => import_loader_utils29.isReadableStream,
58
- isResponse: () => import_loader_utils29.isResponse,
59
- isWorker: () => import_loader_utils25.isWorker,
60
- isWritableStream: () => import_loader_utils29.isWritableStream,
49
+ getPathPrefix: () => getPathPrefix,
50
+ global: () => global_,
51
+ isAsyncIterable: () => isAsyncIterable,
52
+ isBrowser: () => isBrowser,
53
+ isIterable: () => isIterable,
54
+ isIterator: () => isIterator,
55
+ isPromise: () => isPromise,
56
+ isPureObject: () => isPureObject,
57
+ isReadableStream: () => isReadableStream,
58
+ isResponse: () => isResponse,
59
+ isWorker: () => isWorker,
60
+ isWritableStream: () => isWritableStream,
61
61
  load: () => load,
62
62
  loadInBatches: () => loadInBatches,
63
63
  makeIterator: () => makeIterator,
64
- makeLineIterator: () => import_loader_utils28.makeLineIterator,
65
- makeNumberedLineIterator: () => import_loader_utils28.makeNumberedLineIterator,
64
+ makeLineIterator: () => makeLineIterator,
65
+ makeNumberedLineIterator: () => makeNumberedLineIterator,
66
66
  makeStream: () => makeStream,
67
- makeTextDecoderIterator: () => import_loader_utils28.makeTextDecoderIterator,
68
- makeTextEncoderIterator: () => import_loader_utils28.makeTextEncoderIterator,
67
+ makeTextDecoderIterator: () => makeTextDecoderIterator,
68
+ makeTextEncoderIterator: () => makeTextEncoderIterator,
69
69
  parse: () => parse,
70
70
  parseFile: () => parseFile,
71
71
  parseInBatches: () => parseInBatches,
@@ -74,21 +74,2201 @@ __export(dist_exports, {
74
74
  preloadSync: () => preloadSync,
75
75
  readArrayBuffer: () => readArrayBuffer,
76
76
  registerLoaders: () => registerLoaders,
77
- resolvePath: () => import_loader_utils22.resolvePath,
77
+ resolvePath: () => resolvePath,
78
78
  selectLoader: () => selectLoader,
79
79
  selectLoaderSync: () => selectLoaderSync,
80
- self: () => import_loader_utils25.self,
80
+ self: () => self_,
81
81
  setLoaderOptions: () => setGlobalOptions,
82
- setPathPrefix: () => import_loader_utils22.setPathPrefix,
83
- window: () => import_loader_utils25.window
82
+ setPathPrefix: () => setPathPrefix,
83
+ window: () => window_
84
84
  });
85
- module.exports = __toCommonJS(dist_exports);
85
+ module.exports = __toCommonJS(index_exports);
86
86
 
87
- // dist/lib/fetch/fetch-file.js
88
- var import_loader_utils3 = require("@loaders.gl/loader-utils");
87
+ // ../loader-utils/src/lib/env-utils/assert.ts
88
+ function assert(condition, message) {
89
+ if (!condition) {
90
+ throw new Error(message || "loader assertion failed.");
91
+ }
92
+ }
93
+
94
+ // ../loader-utils/src/lib/env-utils/globals.ts
95
+ var globals = {
96
+ self: typeof self !== "undefined" && self,
97
+ window: typeof window !== "undefined" && window,
98
+ global: typeof global !== "undefined" && global,
99
+ document: typeof document !== "undefined" && document
100
+ };
101
+ var self_ = globals.self || globals.window || globals.global || {};
102
+ var window_ = globals.window || globals.self || globals.global || {};
103
+ var global_ = globals.global || globals.self || globals.window || {};
104
+ var document_ = globals.document || {};
105
+ var isBrowser = (
106
+ // @ts-ignore process does not exist on browser
107
+ Boolean(typeof process !== "object" || String(process) !== "[object process]" || process.browser)
108
+ );
109
+ var isWorker = typeof importScripts === "function";
110
+ var matches = typeof process !== "undefined" && process.version && /v([0-9]*)/.exec(process.version);
111
+ var nodeVersion = matches && parseFloat(matches[1]) || 0;
112
+
113
+ // ../loader-utils/src/lib/log-utils/log.ts
114
+ var import_log = require("@probe.gl/log");
115
+ var VERSION = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
116
+ var version = VERSION[0] >= "0" && VERSION[0] <= "9" ? `v${VERSION}` : "";
117
+ function createLog() {
118
+ const log2 = new import_log.Log({ id: "loaders.gl" });
119
+ globalThis.loaders ||= {};
120
+ globalThis.loaders.log = log2;
121
+ globalThis.loaders.version = version;
122
+ globalThis.probe ||= {};
123
+ globalThis.probe.loaders = log2;
124
+ return log2;
125
+ }
126
+ var log = createLog();
127
+
128
+ // ../loader-utils/src/lib/javascript-utils/is-type.ts
129
+ var isBoolean = (value) => typeof value === "boolean";
130
+ var isFunction = (value) => typeof value === "function";
131
+ var isObject = (value) => value !== null && typeof value === "object";
132
+ var isPureObject = (value) => isObject(value) && value.constructor === {}.constructor;
133
+ var isSharedArrayBuffer = (value) => typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer;
134
+ var isArrayBufferLike = (value) => isObject(value) && typeof value.byteLength === "number" && typeof value.slice === "function";
135
+ var isPromise = (value) => isObject(value) && "then" in value && isFunction(value.then);
136
+ var isIterable = (value) => Boolean(value) && isFunction(value[Symbol.iterator]);
137
+ var isAsyncIterable = (value) => Boolean(value) && isFunction(value[Symbol.asyncIterator]);
138
+ var isIterator = (value) => Boolean(value) && isFunction(value.next);
139
+ var isResponse = (value) => typeof Response !== "undefined" && value instanceof Response || isObject(value) && isFunction(value.arrayBuffer) && isFunction(value.text) && isFunction(value.json);
140
+ var isBlob = (value) => typeof Blob !== "undefined" && value instanceof Blob;
141
+ var isWritableDOMStream = (value) => isObject(value) && isFunction(value.abort) && isFunction(value.getWriter);
142
+ var isReadableDOMStream = (value) => typeof ReadableStream !== "undefined" && value instanceof ReadableStream || isObject(value) && isFunction(value.tee) && isFunction(value.cancel) && isFunction(value.getReader);
143
+ var isWritableNodeStream = (value) => isObject(value) && isFunction(value.end) && isFunction(value.write) && isBoolean(value.writable);
144
+ var isReadableNodeStream = (value) => isObject(value) && isFunction(value.read) && isFunction(value.pipe) && isBoolean(value.readable);
145
+ var isReadableStream = (value) => isReadableDOMStream(value) || isReadableNodeStream(value);
146
+ var isWritableStream = (value) => isWritableDOMStream(value) || isWritableNodeStream(value);
147
+
148
+ // ../loader-utils/src/lib/option-utils/merge-options.ts
149
+ function mergeOptions(baseOptions, newOptions) {
150
+ return mergeOptionsRecursively(baseOptions || {}, newOptions);
151
+ }
152
+ function mergeOptionsRecursively(baseOptions, newOptions, level = 0) {
153
+ if (level > 3) {
154
+ return newOptions;
155
+ }
156
+ const options = { ...baseOptions };
157
+ for (const [key, newValue] of Object.entries(newOptions)) {
158
+ if (isPureObject(newValue)) {
159
+ options[key] = mergeOptionsRecursively(
160
+ options[key] || {},
161
+ newOptions[key],
162
+ level + 1
163
+ );
164
+ } else {
165
+ options[key] = newOptions[key];
166
+ }
167
+ }
168
+ return options;
169
+ }
170
+
171
+ // ../loader-utils/src/lib/module-utils/js-module-utils.ts
172
+ function registerJSModules(modules) {
173
+ globalThis.loaders ||= {};
174
+ globalThis.loaders.modules ||= {};
175
+ Object.assign(globalThis.loaders.modules, modules);
176
+ }
177
+
178
+ // ../worker-utils/src/lib/npm-tag.ts
179
+ var NPM_TAG = "latest";
180
+
181
+ // ../worker-utils/src/lib/env-utils/version.ts
182
+ function getVersion() {
183
+ var _a;
184
+ if (!((_a = globalThis._loadersgl_) == null ? void 0 : _a.version)) {
185
+ globalThis._loadersgl_ = globalThis._loadersgl_ || {};
186
+ if (typeof __VERSION__ === "undefined") {
187
+ globalThis._loadersgl_.version = NPM_TAG;
188
+ } else {
189
+ globalThis._loadersgl_.version = __VERSION__;
190
+ }
191
+ }
192
+ return globalThis._loadersgl_.version;
193
+ }
194
+ var VERSION2 = getVersion();
195
+
196
+ // ../worker-utils/src/lib/env-utils/assert.ts
197
+ function assert2(condition, message) {
198
+ if (!condition) {
199
+ throw new Error(message || "loaders.gl assertion failed.");
200
+ }
201
+ }
202
+
203
+ // ../worker-utils/src/lib/env-utils/globals.ts
204
+ var globals2 = {
205
+ self: typeof self !== "undefined" && self,
206
+ window: typeof window !== "undefined" && window,
207
+ global: typeof global !== "undefined" && global,
208
+ document: typeof document !== "undefined" && document
209
+ };
210
+ var self_2 = globals2.self || globals2.window || globals2.global || {};
211
+ var window_2 = globals2.window || globals2.self || globals2.global || {};
212
+ var global_2 = globals2.global || globals2.self || globals2.window || {};
213
+ var document_2 = globals2.document || {};
214
+ var isBrowser2 = (
215
+ // @ts-ignore process.browser
216
+ typeof process !== "object" || String(process) !== "[object process]" || process.browser
217
+ );
218
+ var isMobile = typeof window !== "undefined" && typeof window.orientation !== "undefined";
219
+ var matches2 = typeof process !== "undefined" && process.version && /v([0-9]*)/.exec(process.version);
220
+ var nodeVersion2 = matches2 && parseFloat(matches2[1]) || 0;
221
+
222
+ // ../worker-utils/src/lib/worker-farm/worker-job.ts
223
+ var WorkerJob = class {
224
+ name;
225
+ workerThread;
226
+ isRunning = true;
227
+ /** Promise that resolves when Job is done */
228
+ result;
229
+ _resolve = () => {
230
+ };
231
+ _reject = () => {
232
+ };
233
+ constructor(jobName, workerThread) {
234
+ this.name = jobName;
235
+ this.workerThread = workerThread;
236
+ this.result = new Promise((resolve2, reject) => {
237
+ this._resolve = resolve2;
238
+ this._reject = reject;
239
+ });
240
+ }
241
+ /**
242
+ * Send a message to the job's worker thread
243
+ * @param data any data structure, ideally consisting mostly of transferrable objects
244
+ */
245
+ postMessage(type, payload) {
246
+ this.workerThread.postMessage({
247
+ source: "loaders.gl",
248
+ // Lets worker ignore unrelated messages
249
+ type,
250
+ payload
251
+ });
252
+ }
253
+ /**
254
+ * Call to resolve the `result` Promise with the supplied value
255
+ */
256
+ done(value) {
257
+ if (!this.isRunning) {
258
+ return;
259
+ }
260
+ this.isRunning = false;
261
+ this._resolve(value);
262
+ }
263
+ /**
264
+ * Call to reject the `result` Promise with the supplied error
265
+ */
266
+ error(error) {
267
+ if (!this.isRunning) {
268
+ return;
269
+ }
270
+ this.isRunning = false;
271
+ this._reject(error);
272
+ }
273
+ /**
274
+ * Terminates the worker executing this job and rejects its result.
275
+ * @param reason Abort reason supplied by the caller, or a cross-runtime `AbortError`.
276
+ */
277
+ abort(reason) {
278
+ if (!this.isRunning) {
279
+ return;
280
+ }
281
+ const error = reason ?? createAbortError(`Worker job "${this.name}" was aborted`);
282
+ this.workerThread.destroy();
283
+ this.error(error);
284
+ }
285
+ };
286
+ function createAbortError(message) {
287
+ const error = new Error(message);
288
+ error.name = "AbortError";
289
+ return error;
290
+ }
291
+
292
+ // ../worker-utils/src/lib/node/worker_threads-browser.ts
293
+ var NodeWorker = class {
294
+ /** Restores the native worker constructor when this browser shim is embedded in a Node bundle. */
295
+ constructor(...arguments_) {
296
+ var _a;
297
+ const runtimeProcess = globalThis.process;
298
+ const WorkerConstructor = (_a = runtimeProcess == null ? void 0 : runtimeProcess.getBuiltinModule) == null ? void 0 : _a.call(runtimeProcess, "node:worker_threads").Worker;
299
+ if (WorkerConstructor) {
300
+ return new WorkerConstructor(...arguments_);
301
+ }
302
+ }
303
+ /** No-op browser fallback matching the native Worker API. */
304
+ terminate() {
305
+ }
306
+ };
307
+
308
+ // ../worker-utils/src/lib/worker-utils/get-loadable-worker-url.ts
309
+ var workerURLCache = /* @__PURE__ */ new Map();
310
+ function getLoadableWorkerURL(props) {
311
+ assert2(props.source && !props.url || !props.source && props.url);
312
+ let workerURL = workerURLCache.get(props.source || props.url);
313
+ if (!workerURL) {
314
+ if (props.url) {
315
+ workerURL = getLoadableWorkerURLFromURL(props.url);
316
+ workerURLCache.set(props.url, workerURL);
317
+ }
318
+ if (props.source) {
319
+ workerURL = getLoadableWorkerURLFromSource(props.source);
320
+ workerURLCache.set(props.source, workerURL);
321
+ }
322
+ }
323
+ assert2(workerURL);
324
+ return workerURL;
325
+ }
326
+ function getLoadableWorkerURLFromURL(url) {
327
+ if (!url.startsWith("http")) {
328
+ return url;
329
+ }
330
+ const workerSource = buildScriptSource(url);
331
+ return getLoadableWorkerURLFromSource(workerSource);
332
+ }
333
+ function getLoadableWorkerURLFromSource(workerSource) {
334
+ const blob = new Blob([workerSource], { type: "application/javascript" });
335
+ return URL.createObjectURL(blob);
336
+ }
337
+ function buildScriptSource(workerUrl) {
338
+ return `try {
339
+ importScripts('${workerUrl}');
340
+ } catch (error) {
341
+ console.error(error);
342
+ throw error;
343
+ }`;
344
+ }
345
+
346
+ // ../worker-utils/src/lib/worker-utils/get-transfer-list.ts
347
+ function getTransferList(object, recursive = true, transfers) {
348
+ const transfersSet = transfers || /* @__PURE__ */ new Set();
349
+ if (!object) {
350
+ } else if (isTransferable(object)) {
351
+ transfersSet.add(object);
352
+ } else if (isTransferable(object.buffer)) {
353
+ transfersSet.add(object.buffer);
354
+ } else if (ArrayBuffer.isView(object)) {
355
+ } else if (recursive && typeof object === "object") {
356
+ for (const key in object) {
357
+ getTransferList(object[key], recursive, transfersSet);
358
+ }
359
+ }
360
+ return transfers === void 0 ? Array.from(transfersSet) : [];
361
+ }
362
+ function isTransferable(object) {
363
+ if (!object) {
364
+ return false;
365
+ }
366
+ if (object instanceof ArrayBuffer) {
367
+ return true;
368
+ }
369
+ if (typeof MessagePort !== "undefined" && object instanceof MessagePort) {
370
+ return true;
371
+ }
372
+ if (typeof ImageBitmap !== "undefined" && object instanceof ImageBitmap) {
373
+ return true;
374
+ }
375
+ if (typeof OffscreenCanvas !== "undefined" && object instanceof OffscreenCanvas) {
376
+ return true;
377
+ }
378
+ return false;
379
+ }
380
+ function getTransferListForWriter(object) {
381
+ if (object === null) {
382
+ return {};
383
+ }
384
+ const clone = Object.assign({}, object);
385
+ Object.keys(clone).forEach((key) => {
386
+ if (typeof object[key] === "object" && !ArrayBuffer.isView(object[key]) && !(object[key] instanceof Array)) {
387
+ clone[key] = getTransferListForWriter(object[key]);
388
+ } else if (typeof clone[key] === "function" || clone[key] instanceof RegExp) {
389
+ clone[key] = {};
390
+ } else {
391
+ clone[key] = object[key];
392
+ }
393
+ });
394
+ return clone;
395
+ }
396
+
397
+ // ../worker-utils/src/lib/worker-farm/worker-thread.ts
398
+ var NOOP = () => {
399
+ };
400
+ var WorkerThread = class {
401
+ name;
402
+ source;
403
+ url;
404
+ /** Lazily resolves the classic worker URL used as a fallback. */
405
+ getUrl;
406
+ /** Creates a browser Worker directly. */
407
+ loadWorker;
408
+ terminated = false;
409
+ worker;
410
+ onMessage;
411
+ onError;
412
+ _loadableURL = "";
413
+ /** Checks if workers are supported on this platform */
414
+ static isSupported() {
415
+ return typeof Worker !== "undefined" && isBrowser2 || typeof NodeWorker !== "undefined" && !isBrowser2;
416
+ }
417
+ constructor(props) {
418
+ const { name, source, url, getUrl, loadWorker } = props;
419
+ assert2(source || url || getUrl || loadWorker);
420
+ this.name = name;
421
+ this.source = source;
422
+ this.url = url;
423
+ this.getUrl = getUrl;
424
+ this.loadWorker = loadWorker;
425
+ this.onMessage = NOOP;
426
+ this.onError = (error) => console.log(error);
427
+ this.worker = isBrowser2 ? this._createBrowserWorker() : this._createNodeWorker();
428
+ }
429
+ /**
430
+ * Terminate this worker thread
431
+ * @note Can free up significant memory
432
+ */
433
+ destroy() {
434
+ this.onMessage = NOOP;
435
+ this.onError = NOOP;
436
+ this.worker.terminate();
437
+ this.terminated = true;
438
+ }
439
+ /** Keeps this worker from preventing Node.js process exit while idle. */
440
+ unref() {
441
+ if (!isBrowser2 && typeof this.worker.unref === "function") {
442
+ this.worker.unref();
443
+ }
444
+ }
445
+ /** Keeps this worker alive while it is actively processing a job. */
446
+ ref() {
447
+ if (!isBrowser2 && typeof this.worker.ref === "function") {
448
+ this.worker.ref();
449
+ }
450
+ }
451
+ get isRunning() {
452
+ return Boolean(this.onMessage);
453
+ }
454
+ /**
455
+ * Send a message to this worker thread
456
+ * @param data any data structure, ideally consisting mostly of transferrable objects
457
+ * @param transferList If not supplied, calculated automatically by traversing data
458
+ */
459
+ postMessage(data, transferList) {
460
+ transferList = transferList || getTransferList(data);
461
+ this.worker.postMessage(data, transferList);
462
+ }
463
+ // PRIVATE
464
+ /**
465
+ * Generate a standard Error from an ErrorEvent
466
+ * @param event
467
+ */
468
+ _getErrorFromErrorEvent(event) {
469
+ let message = "Failed to load ";
470
+ message += `worker ${this.name} from ${this.url || "built-in worker factory"}. `;
471
+ if (event.message) {
472
+ message += `${event.message} in `;
473
+ }
474
+ if (event.lineno) {
475
+ message += `:${event.lineno}:${event.colno}`;
476
+ }
477
+ return new Error(message);
478
+ }
479
+ /**
480
+ * Creates a worker thread on the browser
481
+ */
482
+ _createBrowserWorker() {
483
+ var _a;
484
+ if (this.loadWorker) {
485
+ try {
486
+ const worker2 = this.loadWorker();
487
+ if (worker2) {
488
+ return this._initializeBrowserWorker(worker2);
489
+ }
490
+ } catch (error) {
491
+ if (!this.source && !this.url && !this.getUrl) {
492
+ throw error;
493
+ }
494
+ }
495
+ }
496
+ const url = this.url || ((_a = this.getUrl) == null ? void 0 : _a.call(this));
497
+ this._loadableURL = getLoadableWorkerURL({ source: this.source, url });
498
+ const worker = new Worker(this._loadableURL, { name: this.name });
499
+ return this._initializeBrowserWorker(worker);
500
+ }
501
+ /** Attaches protocol handlers to a browser worker. */
502
+ _initializeBrowserWorker(worker) {
503
+ worker.onmessage = (event) => {
504
+ if (!event.data) {
505
+ this.onError(new Error("No data received"));
506
+ } else {
507
+ this.onMessage(event.data);
508
+ }
509
+ };
510
+ worker.onerror = (error) => {
511
+ this.onError(this._getErrorFromErrorEvent(error));
512
+ this.terminated = true;
513
+ };
514
+ worker.onmessageerror = (event) => console.error(event);
515
+ return worker;
516
+ }
517
+ /**
518
+ * Creates a worker thread in node.js
519
+ * @todo https://nodejs.org/api/async_hooks.html#async-resource-worker-pool
520
+ */
521
+ _createNodeWorker() {
522
+ let worker;
523
+ if (this.url) {
524
+ const absolute = this.url.includes(":/") || this.url.startsWith("/");
525
+ const url = absolute ? this.url : `./${this.url}`;
526
+ const workerUrl = url.startsWith("file:") ? new URL(url) : url;
527
+ const type = this.url.endsWith(".ts") || this.url.endsWith(".mjs") ? "module" : "commonjs";
528
+ worker = new NodeWorker(workerUrl, { eval: false, type });
529
+ } else if (this.source) {
530
+ worker = new NodeWorker(this.source, { eval: true });
531
+ } else {
532
+ throw new Error("no worker");
533
+ }
534
+ worker.on("message", (data) => {
535
+ this.onMessage(data);
536
+ });
537
+ worker.on("error", (error) => {
538
+ this.onError(error);
539
+ });
540
+ worker.on("exit", (_code) => {
541
+ });
542
+ return worker;
543
+ }
544
+ };
545
+
546
+ // ../worker-utils/src/lib/worker-farm/worker-pool.ts
547
+ var WorkerPool = class {
548
+ name = "unnamed";
549
+ source;
550
+ // | Function;
551
+ url;
552
+ /** Lazily resolves a classic worker fallback URL. */
553
+ getUrl;
554
+ /** Creates a browser Worker directly. */
555
+ loadWorker;
556
+ maxConcurrency = 1;
557
+ maxMobileConcurrency = 1;
558
+ onDebug = () => {
559
+ };
560
+ reuseWorkers = true;
561
+ props = {};
562
+ jobQueue = [];
563
+ idleQueue = [];
564
+ activeJobs = /* @__PURE__ */ new Set();
565
+ count = 0;
566
+ isDestroyed = false;
567
+ /** Checks if workers are supported on this platform */
568
+ static isSupported() {
569
+ return WorkerThread.isSupported();
570
+ }
571
+ /**
572
+ * @param processor - worker function
573
+ * @param maxConcurrency - max count of workers
574
+ */
575
+ constructor(props) {
576
+ this.source = props.source;
577
+ this.url = props.url;
578
+ this.getUrl = props.getUrl;
579
+ this.loadWorker = props.loadWorker;
580
+ this.setProps(props);
581
+ }
582
+ /**
583
+ * Terminates all workers in the pool and aborts active jobs.
584
+ * @param reason Optional error delivered to active jobs.
585
+ * @note Can free up significant memory
586
+ */
587
+ destroy(reason = new Error("Worker pool was destroyed")) {
588
+ this.activeJobs.forEach((job) => job.abort(reason));
589
+ const queuedJobs = this.jobQueue.splice(0);
590
+ queuedJobs.forEach((queuedJob) => queuedJob.onReject(reason));
591
+ this.idleQueue.forEach((worker) => worker.destroy());
592
+ this.isDestroyed = true;
593
+ }
594
+ setProps(props) {
595
+ this.props = { ...this.props, ...props };
596
+ if (props.name !== void 0) {
597
+ this.name = props.name;
598
+ }
599
+ if (props.maxConcurrency !== void 0) {
600
+ this.maxConcurrency = props.maxConcurrency;
601
+ }
602
+ if (props.maxMobileConcurrency !== void 0) {
603
+ this.maxMobileConcurrency = props.maxMobileConcurrency;
604
+ }
605
+ if (props.reuseWorkers !== void 0) {
606
+ this.reuseWorkers = props.reuseWorkers;
607
+ }
608
+ if (props.onDebug !== void 0) {
609
+ this.onDebug = props.onDebug;
610
+ }
611
+ }
612
+ async startJob(name, onMessage2 = (job, type, data) => job.done(data), onError = (job, error) => job.error(error)) {
613
+ if (this.isDestroyed) {
614
+ return Promise.reject(new Error("Worker pool was destroyed"));
615
+ }
616
+ const startPromise = new Promise((onStart, onReject) => {
617
+ this.jobQueue.push({ name, onMessage: onMessage2, onError, onStart, onReject });
618
+ return this;
619
+ });
620
+ this._startQueuedJob();
621
+ return await startPromise;
622
+ }
623
+ // PRIVATE
624
+ /**
625
+ * Starts first queued job if worker is available or can be created
626
+ * Called when job is started and whenever a worker returns to the idleQueue
627
+ */
628
+ async _startQueuedJob() {
629
+ if (!this.jobQueue.length) {
630
+ return;
631
+ }
632
+ const workerThread = this._getAvailableWorker();
633
+ if (!workerThread) {
634
+ return;
635
+ }
636
+ const queuedJob = this.jobQueue.shift();
637
+ if (queuedJob) {
638
+ this.onDebug({
639
+ message: "Starting job",
640
+ name: queuedJob.name,
641
+ workerThread,
642
+ backlog: this.jobQueue.length
643
+ });
644
+ const job = new WorkerJob(queuedJob.name, workerThread);
645
+ this.activeJobs.add(job);
646
+ workerThread.ref();
647
+ workerThread.onMessage = (data) => queuedJob.onMessage(job, data.type, data.payload);
648
+ workerThread.onError = (error) => queuedJob.onError(job, error);
649
+ queuedJob.onStart(job);
650
+ try {
651
+ await job.result;
652
+ } catch {
653
+ } finally {
654
+ this.activeJobs.delete(job);
655
+ this.returnWorkerToQueue(workerThread);
656
+ }
657
+ }
658
+ }
659
+ /**
660
+ * Returns a worker to the idle queue
661
+ * Destroys the worker if
662
+ * - pool is destroyed
663
+ * - if this pool doesn't reuse workers
664
+ * - if maxConcurrency has been lowered
665
+ * @param worker
666
+ */
667
+ returnWorkerToQueue(worker) {
668
+ const shouldDestroyWorker = (
669
+ // Aborted jobs terminate their worker immediately.
670
+ worker.terminated || // If the pool is destroyed, there is no reason to keep the worker around
671
+ this.isDestroyed || // If the app has disabled worker reuse, any completed workers should be destroyed
672
+ !this.reuseWorkers || // If concurrency has been lowered, this worker might be surplus to requirements
673
+ this.count > this._getMaxConcurrency()
674
+ );
675
+ if (shouldDestroyWorker) {
676
+ if (!worker.terminated) {
677
+ worker.destroy();
678
+ }
679
+ this.count--;
680
+ } else {
681
+ worker.unref();
682
+ this.idleQueue.push(worker);
683
+ }
684
+ if (!this.isDestroyed) {
685
+ this._startQueuedJob();
686
+ }
687
+ }
688
+ /**
689
+ * Returns idle worker or creates new worker if maxConcurrency has not been reached
690
+ */
691
+ _getAvailableWorker() {
692
+ if (this.idleQueue.length > 0) {
693
+ return this.idleQueue.shift() || null;
694
+ }
695
+ if (this.count < this._getMaxConcurrency()) {
696
+ this.count++;
697
+ const name = `${this.name.toLowerCase()} (#${this.count} of ${this.maxConcurrency})`;
698
+ return new WorkerThread({
699
+ name,
700
+ source: this.source,
701
+ url: this.url,
702
+ getUrl: this.getUrl,
703
+ loadWorker: this.loadWorker
704
+ });
705
+ }
706
+ return null;
707
+ }
708
+ _getMaxConcurrency() {
709
+ return isMobile ? this.maxMobileConcurrency : this.maxConcurrency;
710
+ }
711
+ };
712
+
713
+ // ../worker-utils/src/lib/worker-farm/worker-farm.ts
714
+ var DEFAULT_PROPS = {
715
+ maxConcurrency: 3,
716
+ maxMobileConcurrency: 1,
717
+ reuseWorkers: true,
718
+ onDebug: () => {
719
+ }
720
+ };
721
+ var loadWorkerIds = /* @__PURE__ */ new WeakMap();
722
+ var nextLoadWorkerId = 1;
723
+ var WorkerFarm = class _WorkerFarm {
724
+ props;
725
+ workerPools = /* @__PURE__ */ new Map();
726
+ // singleton
727
+ static _workerFarm;
728
+ /** Checks if workers are supported on this platform */
729
+ static isSupported() {
730
+ return WorkerThread.isSupported();
731
+ }
732
+ /** Get the singleton instance of the global worker farm */
733
+ static getWorkerFarm(props = {}) {
734
+ _WorkerFarm._workerFarm = _WorkerFarm._workerFarm || new _WorkerFarm({});
735
+ _WorkerFarm._workerFarm.setProps(props);
736
+ return _WorkerFarm._workerFarm;
737
+ }
738
+ /** get global instance with WorkerFarm.getWorkerFarm() */
739
+ constructor(props) {
740
+ this.props = { ...DEFAULT_PROPS };
741
+ this.setProps(props);
742
+ this.workerPools = /* @__PURE__ */ new Map();
743
+ }
744
+ /**
745
+ * Terminate all workers in the farm and abort active jobs.
746
+ * @param reason Optional error delivered to active jobs.
747
+ * @note Can free up significant memory
748
+ */
749
+ destroy(reason) {
750
+ for (const workerPool of this.workerPools.values()) {
751
+ workerPool.destroy(reason);
752
+ }
753
+ this.workerPools = /* @__PURE__ */ new Map();
754
+ }
755
+ /**
756
+ * Set props used when initializing worker pools
757
+ * @param props
758
+ */
759
+ setProps(props) {
760
+ this.props = { ...this.props, ...props };
761
+ for (const workerPool of this.workerPools.values()) {
762
+ workerPool.setProps(this._getWorkerPoolProps());
763
+ }
764
+ }
765
+ /**
766
+ * Returns a worker pool for the specified worker
767
+ * @param options Worker target used to identify and initialize the pool.
768
+ * @param options.name Worker name used as the base pool identity.
769
+ * @param options.url Worker script URL.
770
+ * @param options.source Inline worker source.
771
+ * @param options.loadWorker Built-in browser worker factory.
772
+ * @example
773
+ * const job = WorkerFarm.getWorkerFarm().getWorkerPool({name, url}).startJob(...);
774
+ */
775
+ getWorkerPool(options) {
776
+ const { name, source, url, getUrl, loadWorker } = options;
777
+ const workerPoolKey = getWorkerPoolKey(options);
778
+ let workerPool = this.workerPools.get(workerPoolKey);
779
+ if (!workerPool) {
780
+ workerPool = new WorkerPool({
781
+ name,
782
+ source,
783
+ url,
784
+ getUrl,
785
+ loadWorker
786
+ });
787
+ workerPool.setProps(this._getWorkerPoolProps());
788
+ this.workerPools.set(workerPoolKey, workerPool);
789
+ }
790
+ return workerPool;
791
+ }
792
+ _getWorkerPoolProps() {
793
+ return {
794
+ maxConcurrency: this.props.maxConcurrency,
795
+ maxMobileConcurrency: this.props.maxMobileConcurrency,
796
+ reuseWorkers: this.props.reuseWorkers,
797
+ onDebug: this.props.onDebug
798
+ };
799
+ }
800
+ };
801
+ function getWorkerPoolKey(options) {
802
+ const { name, source, url, urlKey, loadWorker } = options;
803
+ const sourceKey = source ? `source:${source}` : "";
804
+ const urlKeyPart = url || urlKey ? `url:${url || urlKey}` : "";
805
+ const loadWorkerKey = loadWorker ? `loadWorker:${getLoadWorkerId(loadWorker)}` : "";
806
+ return [name, sourceKey, urlKeyPart, loadWorkerKey].filter(Boolean).join("|");
807
+ }
808
+ function getLoadWorkerId(loadWorker) {
809
+ let loadWorkerId = loadWorkerIds.get(loadWorker);
810
+ if (!loadWorkerId) {
811
+ loadWorkerId = nextLoadWorkerId++;
812
+ loadWorkerIds.set(loadWorker, loadWorkerId);
813
+ }
814
+ return loadWorkerId;
815
+ }
816
+
817
+ // ../worker-utils/src/lib/async-queue/async-queue.ts
818
+ var AsyncQueue = class {
819
+ _values;
820
+ _settlers;
821
+ _closed;
822
+ constructor() {
823
+ this._values = [];
824
+ this._settlers = [];
825
+ this._closed = false;
826
+ }
827
+ /** Return an async iterator for this queue */
828
+ [Symbol.asyncIterator]() {
829
+ return this;
830
+ }
831
+ /** Push a new value - the async iterator will yield a promise resolved to this value */
832
+ push(value) {
833
+ return this.enqueue(value);
834
+ }
835
+ /**
836
+ * Push a new value - the async iterator will yield a promise resolved to this value
837
+ * Add an error - the async iterator will yield a promise rejected with this value
838
+ */
839
+ enqueue(value) {
840
+ if (this._closed) {
841
+ throw new Error("Closed");
842
+ }
843
+ if (this._settlers.length > 0) {
844
+ if (this._values.length > 0) {
845
+ throw new Error("Illegal internal state");
846
+ }
847
+ const settler = this._settlers.shift();
848
+ if (value instanceof Error) {
849
+ settler.reject(value);
850
+ } else {
851
+ settler.resolve({ value });
852
+ }
853
+ } else {
854
+ this._values.push(value);
855
+ }
856
+ }
857
+ /** Indicate that we not waiting for more values - The async iterator will be done */
858
+ close() {
859
+ while (this._settlers.length > 0) {
860
+ const settler = this._settlers.shift();
861
+ settler.resolve({ done: true });
862
+ }
863
+ this._closed = true;
864
+ }
865
+ // ITERATOR IMPLEMENTATION
866
+ /** @returns a Promise for an IteratorResult */
867
+ next() {
868
+ if (this._values.length > 0) {
869
+ const value = this._values.shift();
870
+ if (value instanceof Error) {
871
+ return Promise.reject(value);
872
+ }
873
+ return Promise.resolve({ done: false, value });
874
+ }
875
+ if (this._closed) {
876
+ if (this._settlers.length > 0) {
877
+ throw new Error("Illegal internal state");
878
+ }
879
+ return Promise.resolve({ done: true, value: void 0 });
880
+ }
881
+ return new Promise((resolve2, reject) => {
882
+ this._settlers.push({ resolve: resolve2, reject });
883
+ });
884
+ }
885
+ };
886
+
887
+ // ../worker-utils/src/lib/worker-api/get-worker-url.ts
888
+ var warnedWorkerVersionFallbacks = /* @__PURE__ */ new Set();
889
+ function getWorkerName(worker) {
890
+ const warning = worker.version !== VERSION2 ? ` (worker-utils@${VERSION2})` : "";
891
+ return `${worker.name}@${worker.version}${warning}`;
892
+ }
893
+ function getCustomWorkerURL(worker, options = {}) {
894
+ var _a;
895
+ const workerOptions = options[worker.id] || {};
896
+ const workerFile = isBrowser2 ? worker.workerFile || `${worker.id}-worker.js` : worker.workerNode || `${worker.id}-worker-node.js`;
897
+ let url = workerOptions.workerUrl;
898
+ if (!url && worker.id === "compression") {
899
+ url = options.workerUrl;
900
+ }
901
+ const workerType = options._workerType || ((_a = options == null ? void 0 : options.core) == null ? void 0 : _a._workerType);
902
+ if (!url && workerType === "test") {
903
+ if (isBrowser2) {
904
+ url = `modules/${worker.module}/dist/${workerFile}`;
905
+ } else {
906
+ url = `modules/${worker.module}/src/workers/${worker.id}-worker-node.ts`;
907
+ }
908
+ }
909
+ return url || null;
910
+ }
911
+ function getWorkerURL(worker, options = {}) {
912
+ const customWorkerUrl = getCustomWorkerURL(worker, options);
913
+ const descriptorWorkerUrl = typeof worker.worker === "string" ? worker.worker : null;
914
+ const url = customWorkerUrl || descriptorWorkerUrl || getDefaultWorkerURL(worker, true);
915
+ assert2(url);
916
+ return url;
917
+ }
918
+ function getDefaultWorkerURL(worker, warn = false) {
919
+ const workerFile = isBrowser2 ? worker.workerFile || `${worker.id}-worker.js` : worker.workerNode || `${worker.id}-worker-node.js`;
920
+ let version2 = worker.version;
921
+ if (version2 === "latest") {
922
+ version2 = NPM_TAG;
923
+ }
924
+ const versionTag = version2 ? `@${version2}` : "";
925
+ const url = `https://unpkg.com/@loaders.gl/${worker.module}${versionTag}/dist/${workerFile}`;
926
+ if (warn) {
927
+ warnIfUsingNpmTagFallback(worker, url);
928
+ }
929
+ return url;
930
+ }
931
+ function warnIfUsingNpmTagFallback(worker, url) {
932
+ if (worker.version !== "latest") {
933
+ return;
934
+ }
935
+ const workerId = `${worker.module}:${worker.id}`;
936
+ if (warnedWorkerVersionFallbacks.has(workerId)) {
937
+ return;
938
+ }
939
+ warnedWorkerVersionFallbacks.add(workerId);
940
+ console.warn(
941
+ `loaders.gl: ${worker.name} loader worker version is "latest" because __VERSION__ was not injected. Fetching ${url} from CDN.`
942
+ );
943
+ }
944
+
945
+ // ../worker-utils/src/lib/worker-api/process-on-worker.ts
946
+ function canProcessOnWorker(worker, options) {
947
+ if (!WorkerFarm.isSupported()) {
948
+ return false;
949
+ }
950
+ const workerOptions = options == null ? void 0 : options[worker.id];
951
+ return Boolean(
952
+ (worker.worker || worker.loadWorker || (workerOptions == null ? void 0 : workerOptions.workerUrl)) && (options == null ? void 0 : options.worker)
953
+ );
954
+ }
955
+ async function processOnWorker(worker, data, options = {}, context = {}, jobContext = {}) {
956
+ var _a, _b, _c;
957
+ throwIfAborted(options.signal);
958
+ const name = getWorkerName(worker);
959
+ const workerFarm = WorkerFarm.getWorkerFarm(options);
960
+ const workerPool = workerFarm.getWorkerPool(getWorkerPoolTarget(worker, options, name));
961
+ const jobName = options.jobName || worker.name;
962
+ const job = await workerPool.startJob(
963
+ jobName,
964
+ // eslint-disable-next-line
965
+ onMessage.bind(null, context)
966
+ );
967
+ const abortJob = () => job.abort(getAbortReason(options.signal));
968
+ (_a = options.signal) == null ? void 0 : _a.addEventListener("abort", abortJob, { once: true });
969
+ if ((_b = options.signal) == null ? void 0 : _b.aborted) {
970
+ abortJob();
971
+ }
972
+ try {
973
+ const { signal: _signal, ...workerOptions } = options;
974
+ const transferableOptions = getTransferListForWriter(workerOptions);
975
+ const transferableContext = getTransferListForWriter(jobContext);
976
+ if (job.isRunning) {
977
+ job.postMessage("process", {
978
+ input: data,
979
+ options: transferableOptions,
980
+ context: transferableContext
981
+ });
982
+ }
983
+ const result = await job.result;
984
+ return result.result;
985
+ } finally {
986
+ (_c = options.signal) == null ? void 0 : _c.removeEventListener("abort", abortJob);
987
+ }
988
+ }
989
+ function processOnWorkerInBatches(worker, input, options = {}, context = {}, jobContext = {}) {
990
+ return processOnWorkerInBatchesIterator(worker, input, options, context, jobContext);
991
+ }
992
+ async function* processOnWorkerInBatchesIterator(worker, input, options, context, jobContext) {
993
+ var _a, _b, _c;
994
+ throwIfAborted(options.signal);
995
+ const name = getWorkerName(worker);
996
+ const workerFarm = WorkerFarm.getWorkerFarm(options);
997
+ const workerPool = workerFarm.getWorkerPool(getWorkerPoolTarget(worker, options, name));
998
+ const outputBatches = new AsyncQueue();
999
+ const inputIterator = getAsyncIterator(input);
1000
+ let outputFinished = false;
1001
+ let inputFinished = false;
1002
+ let inputRequest = Promise.resolve();
1003
+ const batchJobFailed = new Error("Worker batch job failed");
1004
+ const finishOutput = () => {
1005
+ if (!outputFinished) {
1006
+ outputFinished = true;
1007
+ outputBatches.close();
1008
+ }
1009
+ };
1010
+ const failOutput = (_error) => {
1011
+ if (!outputFinished) {
1012
+ outputFinished = true;
1013
+ outputBatches.enqueue(batchJobFailed);
1014
+ outputBatches.close();
1015
+ }
1016
+ };
1017
+ const job = await workerPool.startJob(
1018
+ options.jobName || worker.name,
1019
+ (activeJob, type, payload) => {
1020
+ switch (type) {
1021
+ case "input-request":
1022
+ inputRequest = inputRequest.then(async () => {
1023
+ const nextBatch = await inputIterator.next();
1024
+ if (!activeJob.isRunning) {
1025
+ return;
1026
+ }
1027
+ if (nextBatch.done) {
1028
+ inputFinished = true;
1029
+ activeJob.postMessage("input-done", {});
1030
+ } else {
1031
+ activeJob.postMessage("input-batch", { input: nextBatch.value });
1032
+ }
1033
+ }).catch((error) => activeJob.abort(error));
1034
+ break;
1035
+ case "output-batch":
1036
+ outputBatches.push(payload.result);
1037
+ break;
1038
+ case "done":
1039
+ activeJob.done(payload);
1040
+ finishOutput();
1041
+ break;
1042
+ case "error": {
1043
+ const error = new Error(payload.error || "Worker batch processing failed");
1044
+ activeJob.error(error);
1045
+ failOutput(error);
1046
+ break;
1047
+ }
1048
+ case "process":
1049
+ void onMessage(context, activeJob, type, payload);
1050
+ break;
1051
+ default: {
1052
+ const error = new Error(`Unexpected worker batch message: ${type}`);
1053
+ activeJob.error(error);
1054
+ failOutput(error);
1055
+ break;
1056
+ }
1057
+ }
1058
+ }
1059
+ );
1060
+ const abortJob = () => job.abort(getAbortReason(options.signal));
1061
+ (_a = options.signal) == null ? void 0 : _a.addEventListener("abort", abortJob, { once: true });
1062
+ if ((_b = options.signal) == null ? void 0 : _b.aborted) {
1063
+ abortJob();
1064
+ }
1065
+ void job.result.catch(failOutput);
1066
+ try {
1067
+ const { signal: _signal, ...workerOptions } = options;
1068
+ if (job.isRunning) {
1069
+ job.postMessage("process-in-batches", {
1070
+ options: getTransferListForWriter(workerOptions),
1071
+ context: getTransferListForWriter(jobContext)
1072
+ });
1073
+ }
1074
+ try {
1075
+ for await (const outputBatch of outputBatches) {
1076
+ yield outputBatch;
1077
+ if (job.isRunning) {
1078
+ job.postMessage("output-ack", {});
1079
+ }
1080
+ }
1081
+ } catch (error) {
1082
+ if (error === batchJobFailed) {
1083
+ await job.result;
1084
+ }
1085
+ throw error;
1086
+ }
1087
+ await job.result;
1088
+ } finally {
1089
+ (_c = options.signal) == null ? void 0 : _c.removeEventListener("abort", abortJob);
1090
+ if (job.isRunning) {
1091
+ job.abort(createAbortError2("Worker batch iterator was closed"));
1092
+ }
1093
+ if (!inputFinished) {
1094
+ closeInputIterator(inputIterator);
1095
+ }
1096
+ }
1097
+ }
1098
+ function throwIfAborted(signal) {
1099
+ if (!(signal == null ? void 0 : signal.aborted)) {
1100
+ return;
1101
+ }
1102
+ throw getAbortReason(signal);
1103
+ }
1104
+ function getAbortReason(signal) {
1105
+ return (signal == null ? void 0 : signal.reason) ?? createAbortError2("Worker job was aborted");
1106
+ }
1107
+ function createAbortError2(message) {
1108
+ const error = new Error(message);
1109
+ error.name = "AbortError";
1110
+ return error;
1111
+ }
1112
+ function getAsyncIterator(input) {
1113
+ if (Symbol.asyncIterator in input) {
1114
+ return input[Symbol.asyncIterator]();
1115
+ }
1116
+ return (async function* iterateSynchronously() {
1117
+ yield* input;
1118
+ })();
1119
+ }
1120
+ function closeInputIterator(inputIterator) {
1121
+ var _a;
1122
+ try {
1123
+ void Promise.resolve((_a = inputIterator.return) == null ? void 0 : _a.call(inputIterator)).catch(() => void 0);
1124
+ } catch {
1125
+ }
1126
+ }
1127
+ function getWorkerPoolTarget(worker, options, name) {
1128
+ if (options.source) {
1129
+ return { name, source: options.source };
1130
+ }
1131
+ const customWorkerUrl = getCustomWorkerURL(worker, options);
1132
+ if (customWorkerUrl) {
1133
+ return { name, url: customWorkerUrl };
1134
+ }
1135
+ if (isBrowser2 && worker.loadWorker) {
1136
+ return {
1137
+ name,
1138
+ loadWorker: worker.loadWorker,
1139
+ getUrl: () => getWorkerURL(worker, options),
1140
+ urlKey: getDefaultWorkerURL(worker)
1141
+ };
1142
+ }
1143
+ return { name, url: getWorkerURL(worker, options) };
1144
+ }
1145
+ async function onMessage(context, job, type, payload) {
1146
+ switch (type) {
1147
+ case "done":
1148
+ job.done(payload);
1149
+ break;
1150
+ case "error":
1151
+ job.error(new Error(payload.error));
1152
+ break;
1153
+ case "process":
1154
+ const { id, input, options } = payload;
1155
+ try {
1156
+ if (!context.process) {
1157
+ job.postMessage("error", { id, error: "Worker not set up to process on main thread" });
1158
+ return;
1159
+ }
1160
+ const result = await context.process(input, options, void 0, payload.context || {});
1161
+ job.postMessage("done", { id, result });
1162
+ } catch (error) {
1163
+ const message = error instanceof Error ? error.message : "unknown error";
1164
+ job.postMessage("error", { id, error: message });
1165
+ }
1166
+ break;
1167
+ default:
1168
+ console.warn(`process-on-worker: unknown message ${type}`);
1169
+ }
1170
+ }
1171
+
1172
+ // ../worker-utils/src/lib/worker-api/validate-worker-version.ts
1173
+ function validateWorkerVersion(worker, coreVersion = VERSION2) {
1174
+ assert2(worker, "no worker provided");
1175
+ const workerVersion = worker.version;
1176
+ if (!coreVersion || !workerVersion) {
1177
+ return false;
1178
+ }
1179
+ return true;
1180
+ }
1181
+
1182
+ // ../loader-utils/src/lib/worker-loader-utils/parse-with-worker.ts
1183
+ function canParseWithWorker(loader, options) {
1184
+ var _a, _b, _c;
1185
+ if ((_b = (_a = options == null ? void 0 : options.core) == null ? void 0 : _a.credentials) == null ? void 0 : _b.some(
1186
+ (credential) => credential.type === "request" || "token" in credential && typeof credential.token === "function"
1187
+ ))
1188
+ return false;
1189
+ const workerOptions = getWorkerOptions(options);
1190
+ const nodeWorkers = workerOptions._nodeWorkers;
1191
+ if (!isBrowser2 && !nodeWorkers) {
1192
+ return false;
1193
+ }
1194
+ if (loader.id === "las" && (((_c = options == null ? void 0 : options.las) == null ? void 0 : _c.shape) ?? "arrow-table") === "arrow-table") {
1195
+ return false;
1196
+ }
1197
+ if (loader.id === "csv" && !shouldParseCSVWithWorker(options)) {
1198
+ return false;
1199
+ }
1200
+ return Boolean(canProcessOnWorker(loader, workerOptions));
1201
+ }
1202
+ function shouldParseWithWorker(loader, data, options, context, estimateLoader = loader) {
1203
+ var _a, _b;
1204
+ if (!canParseWithWorker(loader, options)) {
1205
+ return false;
1206
+ }
1207
+ if (((_a = options == null ? void 0 : options.core) == null ? void 0 : _a.worker) !== "auto" || !estimateLoader.getWorkerEstimate) {
1208
+ return true;
1209
+ }
1210
+ try {
1211
+ const estimate = estimateLoader.getWorkerEstimate(data, options, context);
1212
+ if (estimate === void 0 || !Number.isFinite(estimate) || estimate < 0 || estimate > 1) {
1213
+ return true;
1214
+ }
1215
+ return estimate >= (((_b = options.core) == null ? void 0 : _b.workerThreshold) ?? 0.1);
1216
+ } catch {
1217
+ return true;
1218
+ }
1219
+ }
1220
+ async function parseWithWorker(loader, data, options, context, parseOnMainThread, signal) {
1221
+ const workerSignal = signal || getWorkerAbortSignal(options);
1222
+ const result = await processOnWorker(
1223
+ loader,
1224
+ data,
1225
+ { ...getWorkerOptions(options, loader), signal: workerSignal },
1226
+ {
1227
+ process: async (input, processOptions, _workerContext, parseContext) => {
1228
+ if (!parseOnMainThread) {
1229
+ throw new Error("Worker not set up to parse on main thread");
1230
+ }
1231
+ const { _loaderIds, _loaderIsArray, ...workerParseContext } = parseContext || {};
1232
+ const mainThreadContext = context || parseContext ? { ...context, ...workerParseContext } : void 0;
1233
+ return await callParseOnMainThread(
1234
+ parseOnMainThread,
1235
+ input,
1236
+ processOptions,
1237
+ mainThreadContext,
1238
+ resolveWorkerLoaders(loader, _loaderIds, _loaderIsArray, context, options)
1239
+ );
1240
+ }
1241
+ },
1242
+ getSerializableLoaderContext(context)
1243
+ );
1244
+ return isLoaderWithWorkerResultDeserializer(loader) ? loader.deserializeWorkerResult(result, options, context) : result;
1245
+ }
1246
+ async function* parseWithWorkerInBatches(loader, inputIterator, options, context, parseOnMainThread, signal) {
1247
+ const workerSignal = signal || getWorkerAbortSignal(options);
1248
+ const outputIterator = processOnWorkerInBatches(
1249
+ loader,
1250
+ inputIterator,
1251
+ { ...getWorkerOptions(options), signal: workerSignal },
1252
+ {
1253
+ process: async (input, processOptions, _workerContext, parseContext) => {
1254
+ if (!parseOnMainThread) {
1255
+ throw new Error("Worker not set up to parse on main thread");
1256
+ }
1257
+ const { _loaderIds, _loaderIsArray, ...workerParseContext } = parseContext || {};
1258
+ const mainThreadContext = context || parseContext ? { ...context, ...workerParseContext } : void 0;
1259
+ return await callParseOnMainThread(
1260
+ parseOnMainThread,
1261
+ input,
1262
+ processOptions,
1263
+ mainThreadContext,
1264
+ resolveWorkerLoaders(loader, _loaderIds, _loaderIsArray, context, options)
1265
+ );
1266
+ }
1267
+ },
1268
+ getSerializableLoaderContext(context)
1269
+ );
1270
+ for await (const batch of outputIterator) {
1271
+ yield loader.deserializeWorkerBatch ? loader.deserializeWorkerBatch(batch, options, context) : batch;
1272
+ }
1273
+ }
1274
+ function getWorkerAbortSignal(options) {
1275
+ var _a;
1276
+ const parquetSignal = (_a = options == null ? void 0 : options.parquet) == null ? void 0 : _a.signal;
1277
+ if (typeof AbortSignal !== "undefined" && parquetSignal instanceof AbortSignal) {
1278
+ return parquetSignal;
1279
+ }
1280
+ return void 0;
1281
+ }
1282
+ function callParseOnMainThread(parseOnMainThread, input, options, context, loaders) {
1283
+ if (!loaders && parseOnMainThread.length <= 2) {
1284
+ return parseOnMainThread(input, options);
1285
+ }
1286
+ return parseOnMainThread(input, loaders, options, context);
1287
+ }
1288
+ function resolveWorkerLoaders(loader, loaderIds, loaderIsArray, context, options) {
1289
+ if (!loaderIds) return void 0;
1290
+ const availableLoaders = /* @__PURE__ */ new Map();
1291
+ const visitedLoaders = /* @__PURE__ */ new Set();
1292
+ for (const candidate of [loader, ...(context == null ? void 0 : context.loaders) || []]) {
1293
+ visitLoader(candidate);
1294
+ }
1295
+ const selectedLoaders = loaderIds.map((loaderId) => {
1296
+ const selectedLoader = availableLoaders.get(loaderId);
1297
+ if (!selectedLoader) {
1298
+ throw new Error(
1299
+ `Worker requested unknown loader "${loaderId}". Declare it in loader.subloaders or pass it in context.loaders.`
1300
+ );
1301
+ }
1302
+ return selectedLoader;
1303
+ });
1304
+ return loaderIsArray ? selectedLoaders : selectedLoaders[0];
1305
+ function visitLoader(candidate) {
1306
+ var _a, _b;
1307
+ if (visitedLoaders.has(candidate)) return;
1308
+ visitedLoaders.add(candidate);
1309
+ availableLoaders.set(candidate.id, candidate);
1310
+ for (const [name, dependency] of Object.entries(candidate.subloaders || {})) {
1311
+ const replacement = ((_b = (_a = options == null ? void 0 : options.core) == null ? void 0 : _a.subloaders) == null ? void 0 : _b[name]) || dependency;
1312
+ visitLoader(replacement);
1313
+ availableLoaders.set(dependency.id, replacement);
1314
+ }
1315
+ }
1316
+ }
1317
+ function getWorkerOptions(options = {}, loader) {
1318
+ const serializedOptions = JSON.parse(
1319
+ JSON.stringify({
1320
+ ...options,
1321
+ ...options.core ? { core: { ...options.core, authentications: void 0 } } : {}
1322
+ })
1323
+ );
1324
+ const workerOptions = {
1325
+ ...serializedOptions.core,
1326
+ ...serializedOptions
1327
+ };
1328
+ if (workerOptions.worker === "auto") {
1329
+ workerOptions.worker = true;
1330
+ }
1331
+ if (loader) {
1332
+ workerOptions._workerLoaderId = loader.id;
1333
+ }
1334
+ return workerOptions;
1335
+ }
1336
+ function getSerializableLoaderContext(context) {
1337
+ if (!context) {
1338
+ return {};
1339
+ }
1340
+ const { fetch: fetch2, loaders, coreApi: coreApi2, _parse, _parseSync, _parseInBatches, ...serializableContext } = context;
1341
+ return JSON.parse(JSON.stringify(serializableContext));
1342
+ }
1343
+ function shouldParseCSVWithWorker(options) {
1344
+ var _a, _b;
1345
+ const csvOptions = options;
1346
+ return (((_a = csvOptions == null ? void 0 : csvOptions.csv) == null ? void 0 : _a.shape) ?? ((_b = csvOptions == null ? void 0 : csvOptions.core) == null ? void 0 : _b.shape)) === "arrow-table";
1347
+ }
1348
+ function isLoaderWithWorkerResultDeserializer(loader) {
1349
+ return typeof loader.deserializeWorkerResult === "function";
1350
+ }
1351
+
1352
+ // ../loader-utils/src/lib/worker-loader-utils/encode-with-worker.ts
1353
+ function canEncodeWithWorker(writer, options) {
1354
+ var _a, _b;
1355
+ if (!WorkerFarm.isSupported()) {
1356
+ return false;
1357
+ }
1358
+ const nodeWorkers = (options == null ? void 0 : options._nodeWorkers) ?? ((_a = options == null ? void 0 : options.core) == null ? void 0 : _a._nodeWorkers);
1359
+ const useWorkers = (options == null ? void 0 : options.worker) ?? ((_b = options == null ? void 0 : options.core) == null ? void 0 : _b.worker);
1360
+ if (!isBrowser && !nodeWorkers) {
1361
+ return false;
1362
+ }
1363
+ return Boolean(writer.worker && useWorkers);
1364
+ }
1365
+
1366
+ // ../loader-utils/src/lib/binary-utils/array-buffer-utils.ts
1367
+ function compareArrayBuffers(arrayBuffer1, arrayBuffer2, byteLength) {
1368
+ byteLength = byteLength || arrayBuffer1.byteLength;
1369
+ if (arrayBuffer1.byteLength < byteLength || arrayBuffer2.byteLength < byteLength) {
1370
+ return false;
1371
+ }
1372
+ const array1 = new Uint8Array(arrayBuffer1);
1373
+ const array2 = new Uint8Array(arrayBuffer2);
1374
+ for (let i = 0; i < array1.length; ++i) {
1375
+ if (array1[i] !== array2[i]) {
1376
+ return false;
1377
+ }
1378
+ }
1379
+ return true;
1380
+ }
1381
+ function concatenateArrayBuffers(...sources) {
1382
+ return concatenateArrayBuffersFromArray(sources);
1383
+ }
1384
+ function concatenateArrayBuffersFromArray(sources) {
1385
+ const sourceArrays = sources.map(
1386
+ (source2) => source2 instanceof ArrayBuffer ? new Uint8Array(source2) : source2
1387
+ );
1388
+ const byteLength = sourceArrays.reduce((length, typedArray) => length + typedArray.byteLength, 0);
1389
+ const result = new Uint8Array(byteLength);
1390
+ let offset = 0;
1391
+ for (const sourceArray of sourceArrays) {
1392
+ result.set(sourceArray, offset);
1393
+ offset += sourceArray.byteLength;
1394
+ }
1395
+ return result.buffer;
1396
+ }
1397
+
1398
+ // ../loader-utils/src/lib/binary-utils/encode-array-buffer-to-base64.ts
1399
+ function encodeArrayBufferToBase64(arrayBuffer) {
1400
+ const bytes = new Uint8Array(arrayBuffer);
1401
+ const encodedChunks = [];
1402
+ const chunkByteLength = 3 * 4096;
1403
+ for (let byteOffset = 0; byteOffset < bytes.length; byteOffset += chunkByteLength) {
1404
+ encodedChunks.push(
1405
+ btoa(String.fromCharCode(...bytes.subarray(byteOffset, byteOffset + chunkByteLength)))
1406
+ );
1407
+ }
1408
+ return encodedChunks.join("");
1409
+ }
1410
+
1411
+ // ../loader-utils/src/lib/iterators/text-iterators.ts
1412
+ async function* makeTextDecoderIterator(arrayBufferIterator, options = {}) {
1413
+ const textDecoder = new TextDecoder(void 0, options);
1414
+ for await (const arrayBuffer of arrayBufferIterator) {
1415
+ yield typeof arrayBuffer === "string" ? arrayBuffer : textDecoder.decode(arrayBuffer, { stream: true });
1416
+ }
1417
+ }
1418
+ async function* makeTextEncoderIterator(textIterator) {
1419
+ const textEncoder = new TextEncoder();
1420
+ for await (const text of textIterator) {
1421
+ yield typeof text === "string" ? textEncoder.encode(text).buffer : text;
1422
+ }
1423
+ }
1424
+ async function* makeLineIterator(textIterator) {
1425
+ let previous = "";
1426
+ for await (const textChunk of textIterator) {
1427
+ previous += textChunk;
1428
+ let eolIndex;
1429
+ eolIndex = previous.indexOf("\n");
1430
+ while (eolIndex >= 0) {
1431
+ const line = previous.slice(0, eolIndex + 1);
1432
+ previous = previous.slice(eolIndex + 1);
1433
+ yield line;
1434
+ eolIndex = previous.indexOf("\n");
1435
+ }
1436
+ }
1437
+ if (previous.length > 0) {
1438
+ yield previous;
1439
+ }
1440
+ }
1441
+ async function* makeNumberedLineIterator(lineIterator) {
1442
+ let counter = 1;
1443
+ for await (const line of lineIterator) {
1444
+ yield { counter, line };
1445
+ counter++;
1446
+ }
1447
+ }
1448
+
1449
+ // ../loader-utils/src/lib/iterators/async-iteration.ts
1450
+ async function forEach(iterable, visitor) {
1451
+ const iterator = toAsyncIterator(iterable);
1452
+ while (true) {
1453
+ const { done, value } = await iterator.next();
1454
+ if (done) {
1455
+ if (iterator.return) {
1456
+ iterator.return();
1457
+ }
1458
+ return;
1459
+ }
1460
+ const cancel = visitor(value);
1461
+ if (cancel) {
1462
+ return;
1463
+ }
1464
+ }
1465
+ }
1466
+ async function concatenateArrayBuffersAsync(asyncIterator) {
1467
+ const arrayBuffers = [];
1468
+ for await (const chunk of asyncIterator) {
1469
+ arrayBuffers.push(copyToArrayBuffer(chunk));
1470
+ }
1471
+ return concatenateArrayBuffers(...arrayBuffers);
1472
+ }
1473
+ function copyToArrayBuffer(chunk) {
1474
+ if (chunk instanceof ArrayBuffer) {
1475
+ return chunk;
1476
+ }
1477
+ if (ArrayBuffer.isView(chunk)) {
1478
+ const { buffer, byteOffset, byteLength } = chunk;
1479
+ return copyFromBuffer(buffer, byteOffset, byteLength);
1480
+ }
1481
+ return copyFromBuffer(chunk);
1482
+ }
1483
+ function copyFromBuffer(buffer, byteOffset = 0, byteLength = buffer.byteLength - byteOffset) {
1484
+ const view = new Uint8Array(buffer, byteOffset, byteLength);
1485
+ const copy = new Uint8Array(view.length);
1486
+ copy.set(view);
1487
+ return copy.buffer;
1488
+ }
1489
+ function toAsyncIterator(iterable) {
1490
+ if (typeof iterable[Symbol.asyncIterator] === "function") {
1491
+ return iterable[Symbol.asyncIterator]();
1492
+ }
1493
+ if (typeof iterable[Symbol.iterator] === "function") {
1494
+ const iterator = iterable[Symbol.iterator]();
1495
+ return iteratorToAsyncIterator(iterator);
1496
+ }
1497
+ return iterable;
1498
+ }
1499
+ function iteratorToAsyncIterator(iterator) {
1500
+ return {
1501
+ next(value) {
1502
+ return Promise.resolve(iterator.next(value));
1503
+ },
1504
+ return(value) {
1505
+ if (typeof iterator.return === "function") {
1506
+ return Promise.resolve(iterator.return(value));
1507
+ }
1508
+ return Promise.resolve({ done: true, value });
1509
+ },
1510
+ throw(error) {
1511
+ if (typeof iterator.throw === "function") {
1512
+ return Promise.resolve(iterator.throw(error));
1513
+ }
1514
+ return Promise.reject(error);
1515
+ }
1516
+ };
1517
+ }
1518
+
1519
+ // ../loader-utils/src/lib/request-utils/request-scheduler.ts
1520
+ var import_stats = require("@probe.gl/stats");
1521
+ var STAT_QUEUED_REQUESTS = "Queued Requests";
1522
+ var STAT_ACTIVE_REQUESTS = "Active Requests";
1523
+ var STAT_CANCELLED_REQUESTS = "Cancelled Requests";
1524
+ var STAT_QUEUED_REQUESTS_EVER = "Queued Requests Ever";
1525
+ var STAT_ACTIVE_REQUESTS_EVER = "Active Requests Ever";
1526
+ var DEFAULT_PROPS2 = {
1527
+ id: "request-scheduler",
1528
+ /** Specifies if the request scheduler should throttle incoming requests, mainly for comparative testing. */
1529
+ throttleRequests: true,
1530
+ /** The maximum number of simultaneous active requests. Un-throttled requests do not observe this limit. */
1531
+ maxRequests: 6,
1532
+ /**
1533
+ * Specifies a debounce time, in milliseconds. All requests are queued, until no new requests have
1534
+ * been added to the queue for this amount of time.
1535
+ */
1536
+ debounceTime: 0
1537
+ };
1538
+ var RequestScheduler = class {
1539
+ props;
1540
+ stats;
1541
+ activeRequestCount = 0;
1542
+ /** Tracks the number of active requests and prioritizes/cancels queued requests. */
1543
+ requestQueue = [];
1544
+ requestMap = /* @__PURE__ */ new Map();
1545
+ /** Pending arrival debounce or next-tick refill; only new arrivals replace this timer. */
1546
+ updateTimer = null;
1547
+ constructor(props = {}) {
1548
+ this.props = { ...DEFAULT_PROPS2, ...props };
1549
+ this.stats = new import_stats.Stats({ id: this.props.id });
1550
+ this.stats.get(STAT_QUEUED_REQUESTS);
1551
+ this.stats.get(STAT_ACTIVE_REQUESTS);
1552
+ this.stats.get(STAT_CANCELLED_REQUESTS);
1553
+ this.stats.get(STAT_QUEUED_REQUESTS_EVER);
1554
+ this.stats.get(STAT_ACTIVE_REQUESTS_EVER);
1555
+ }
1556
+ /**
1557
+ * Update scheduler props. Preserves active and queued requests.
1558
+ */
1559
+ setProps(props) {
1560
+ if (props.throttleRequests !== void 0) {
1561
+ this.props.throttleRequests = props.throttleRequests;
1562
+ }
1563
+ if (props.maxRequests !== void 0) {
1564
+ this.props.maxRequests = props.maxRequests;
1565
+ }
1566
+ if (props.debounceTime !== void 0) {
1567
+ this.props.debounceTime = props.debounceTime;
1568
+ }
1569
+ }
1570
+ /**
1571
+ * Called by an application that wants to issue a request, without having it deeply queued by the browser
1572
+ *
1573
+ * When the returned promise resolved, it is OK for the application to issue a request.
1574
+ * The promise resolves to an object that contains a `done` method.
1575
+ * When the application's request has completed (or failed), the application must call the `done` function
1576
+ *
1577
+ * @param handle
1578
+ * @param getPriority will be called when request "slots" open up,
1579
+ * allowing the caller to update priority or cancel the request
1580
+ * Highest priority executes first, priority < 0 cancels the request
1581
+ * @returns a promise
1582
+ * - resolves to a object (with a `done` field) when the request can be issued without queueing,
1583
+ * - resolves to `null` if the request has been cancelled (by the callback return < 0).
1584
+ * In this case the application should not issue the request
1585
+ */
1586
+ scheduleRequest(handle, getPriority = () => 0) {
1587
+ if (!this.props.throttleRequests) {
1588
+ return Promise.resolve({ done: () => {
1589
+ } });
1590
+ }
1591
+ if (this.requestMap.has(handle)) {
1592
+ return this.requestMap.get(handle);
1593
+ }
1594
+ const request = { handle, priority: 0, getPriority };
1595
+ const promise = new Promise((resolve2) => {
1596
+ request.resolve = resolve2;
1597
+ return request;
1598
+ });
1599
+ this.requestQueue.push(request);
1600
+ this.requestMap.set(handle, promise);
1601
+ this._issueNewRequests();
1602
+ return promise;
1603
+ }
1604
+ // PRIVATE
1605
+ _issueRequest(request) {
1606
+ const { handle, resolve: resolve2 } = request;
1607
+ let isDone = false;
1608
+ const done = () => {
1609
+ if (!isDone) {
1610
+ isDone = true;
1611
+ this.requestMap.delete(handle);
1612
+ this.activeRequestCount--;
1613
+ this.scheduleRefill();
1614
+ }
1615
+ };
1616
+ this.activeRequestCount++;
1617
+ return resolve2 ? resolve2({ done }) : Promise.resolve({ done });
1618
+ }
1619
+ /** Refills freed slots on the next tick while preserving any pending arrival debounce. */
1620
+ scheduleRefill() {
1621
+ if (this.updateTimer !== null) {
1622
+ return;
1623
+ }
1624
+ this.updateTimer = setTimeout(() => this._issueNewRequestsAsync(), 0);
1625
+ }
1626
+ /** Restarts the quiet period when a new request enters the queue. */
1627
+ _issueNewRequests() {
1628
+ if (this.updateTimer !== null) {
1629
+ clearTimeout(this.updateTimer);
1630
+ }
1631
+ this.updateTimer = setTimeout(() => this._issueNewRequestsAsync(), this.props.debounceTime);
1632
+ }
1633
+ /** Clears timer ownership before updating priorities and dispatching queued requests. */
1634
+ _issueNewRequestsAsync() {
1635
+ if (this.updateTimer !== null) {
1636
+ clearTimeout(this.updateTimer);
1637
+ }
1638
+ this.updateTimer = null;
1639
+ const freeSlots = Math.max(this.props.maxRequests - this.activeRequestCount, 0);
1640
+ if (freeSlots === 0) {
1641
+ return;
1642
+ }
1643
+ this._updateAllRequests();
1644
+ for (let i = 0; i < freeSlots; ++i) {
1645
+ const request = this.requestQueue.shift();
1646
+ if (request) {
1647
+ this._issueRequest(request);
1648
+ }
1649
+ }
1650
+ }
1651
+ /** Ensure all requests have updated priorities, and that no longer valid requests are cancelled */
1652
+ _updateAllRequests() {
1653
+ const requestQueue = this.requestQueue;
1654
+ for (let i = 0; i < requestQueue.length; ++i) {
1655
+ const request = requestQueue[i];
1656
+ if (!this._updateRequest(request)) {
1657
+ requestQueue.splice(i, 1);
1658
+ this.requestMap.delete(request.handle);
1659
+ i--;
1660
+ }
1661
+ }
1662
+ requestQueue.sort((a, b) => a.priority - b.priority);
1663
+ }
1664
+ /** Update a single request by calling the callback */
1665
+ _updateRequest(request) {
1666
+ request.priority = request.getPriority(request.handle);
1667
+ if (request.priority < 0) {
1668
+ request.resolve(null);
1669
+ return false;
1670
+ }
1671
+ return true;
1672
+ }
1673
+ };
89
1674
 
90
- // dist/lib/utils/response-utils.js
91
- var import_loader_utils2 = require("@loaders.gl/loader-utils");
1675
+ // ../loader-utils/src/lib/request-utils/authentication.ts
1676
+ function resolveCredentials(credentials, authentications = []) {
1677
+ if (credentials.every(isAuthentication)) return credentials;
1678
+ return credentials.map((credential, index) => {
1679
+ if (isAuthentication(credential)) return credential;
1680
+ const AuthenticationClass = authentications.find(
1681
+ (authentication2) => authentication2.type === (credential == null ? void 0 : credential.type)
1682
+ );
1683
+ if (!AuthenticationClass) {
1684
+ throw new Error(`No authentication class registered for credential at index ${index}.`);
1685
+ }
1686
+ let authentication;
1687
+ try {
1688
+ authentication = new AuthenticationClass(credential);
1689
+ } catch {
1690
+ throw new Error(`Invalid authentication configuration at index ${index}.`);
1691
+ }
1692
+ if (!isAuthentication(authentication)) {
1693
+ throw new Error(`Authentication class returned an invalid credential at index ${index}.`);
1694
+ }
1695
+ return authentication;
1696
+ });
1697
+ }
1698
+ function resolveAuthenticationOptions(options) {
1699
+ var _a, _b;
1700
+ if (!((_b = (_a = options.core) == null ? void 0 : _a.credentials) == null ? void 0 : _b.length)) return options;
1701
+ const credentials = resolveCredentials(options.core.credentials, options.core.authentications);
1702
+ if (credentials === options.core.credentials) return options;
1703
+ return { ...options, core: { ...options.core, credentials } };
1704
+ }
1705
+ async function resolveLoaderAuthenticationOptions(loader, url, options) {
1706
+ var _a, _b, _c, _d;
1707
+ if (!((_b = (_a = options.core) == null ? void 0 : _a.credentials) == null ? void 0 : _b.length) || options.core.credentials.every(isAuthentication)) {
1708
+ return options;
1709
+ }
1710
+ const contributions = await ((_c = loader == null ? void 0 : loader.getAuthentications) == null ? void 0 : _c.call(loader, url, options));
1711
+ if (!(contributions == null ? void 0 : contributions.length)) return resolveAuthenticationOptions(options);
1712
+ return resolveAuthenticationOptions({
1713
+ ...options,
1714
+ core: {
1715
+ ...options.core,
1716
+ authentications: [...((_d = options.core) == null ? void 0 : _d.authentications) || [], ...contributions]
1717
+ }
1718
+ });
1719
+ }
1720
+ function isAuthentication(credential) {
1721
+ if (!credential || typeof credential !== "object") return false;
1722
+ if (typeof credential.id !== "string" || !Array.isArray(credential.origins)) return false;
1723
+ if (credential.type === "request") return typeof credential.authenticate === "function";
1724
+ return (credential.type === "header" || credential.type === "query-parameter") && typeof credential.name === "string" && (typeof credential.token === "string" || typeof credential.token === "function") && Array.isArray(credential.refreshStatusCodes);
1725
+ }
1726
+
1727
+ // ../loader-utils/src/lib/request-utils/request-credentials.ts
1728
+ var AUTHENTICATED_FETCH = /* @__PURE__ */ Symbol("loaders.gl.authenticated-fetch");
1729
+ var refreshPromises = /* @__PURE__ */ new WeakMap();
1730
+ function createAuthenticatedFetch(options) {
1731
+ const suppliedFetch = options.fetch || ((url, requestOptions) => fetch(url, requestOptions));
1732
+ const existingMetadata = suppliedFetch[AUTHENTICATED_FETCH];
1733
+ const credentialResolutions = new Map(existingMetadata == null ? void 0 : existingMetadata.credentialResolutions);
1734
+ const resolvedCredentials = resolveCredentials(
1735
+ options.credentials.map((credential) => credentialResolutions.get(credential) || credential),
1736
+ options.authentications
1737
+ );
1738
+ options.credentials.forEach((credential, index) => {
1739
+ credentialResolutions.set(credential, resolvedCredentials[index]);
1740
+ });
1741
+ if (!options.fetchOptions && (!resolvedCredentials.length || (existingMetadata == null ? void 0 : existingMetadata.credentials.length) === resolvedCredentials.length && resolvedCredentials.every(
1742
+ (credential, index) => credential === existingMetadata.credentials[index]
1743
+ )))
1744
+ return suppliedFetch;
1745
+ const baseFetch = (existingMetadata == null ? void 0 : existingMetadata.fetch) || suppliedFetch;
1746
+ const credentials = existingMetadata ? [.../* @__PURE__ */ new Set([...resolvedCredentials, ...existingMetadata.credentials])] : resolvedCredentials;
1747
+ const fetchOptions = mergeFetchOptions(
1748
+ (existingMetadata == null ? void 0 : existingMetadata.fetchOptions) || {},
1749
+ options.fetchOptions
1750
+ );
1751
+ const tokenCredentials = credentials.filter(
1752
+ (credential) => credential.type !== "request"
1753
+ );
1754
+ const requestCredentials = credentials.filter(
1755
+ (credential) => credential.type === "request"
1756
+ );
1757
+ for (const credential of requestCredentials) normalizeRequestOrigins(credential.origins);
1758
+ const authenticatedFetch = async (url, requestOptions = {}) => {
1759
+ var _a, _b, _c, _d, _e, _f, _g;
1760
+ requestOptions = mergeFetchOptions(fetchOptions, requestOptions);
1761
+ if ((_a = requestOptions.signal) == null ? void 0 : _a.aborted) throw createAbortError3();
1762
+ if (!credentials.length) return baseFetch(url, requestOptions);
1763
+ const resolvedURL = parseHTTPURL(url);
1764
+ const authorization = resolvedURL ? await authorizeRequest(resolvedURL, requestOptions, tokenCredentials) : { url, options: requestOptions, credentials: [] };
1765
+ const signedRequest = await authenticateRequest(authorization, requestCredentials, "request");
1766
+ const response = await baseFetch(signedRequest.url, signedRequest.options);
1767
+ const refreshableCredentials = [];
1768
+ for (const resolvedCredential of authorization.credentials) {
1769
+ const { credential } = resolvedCredential;
1770
+ if (typeof credential.token === "function" && (isReplayableRequest(requestOptions) || ((_b = credential.canReplayRequest) == null ? void 0 : _b.call(credential, url, requestOptions))) && (isReplayableRequest(signedRequest.options) || ((_c = credential.canReplayRequest) == null ? void 0 : _c.call(credential, signedRequest.url, signedRequest.options))) && (credential.refreshStatusCodes.includes(response.status) || await ((_d = credential.shouldRefresh) == null ? void 0 : _d.call(credential, response))))
1771
+ refreshableCredentials.push(resolvedCredential);
1772
+ }
1773
+ const shouldRetrySignature = isReplayableRequest(requestOptions) && isReplayableRequest(signedRequest.options) && signedRequest.credentials.some(
1774
+ (credential) => {
1775
+ var _a2;
1776
+ return (_a2 = credential.refreshStatusCodes) == null ? void 0 : _a2.includes(response.status);
1777
+ }
1778
+ );
1779
+ if (!refreshableCredentials.length && !shouldRetrySignature) return response;
1780
+ if ((_e = requestOptions.signal) == null ? void 0 : _e.aborted) throw createAbortError3();
1781
+ const refreshedTokens = /* @__PURE__ */ new Map();
1782
+ await Promise.all(
1783
+ refreshableCredentials.map(async ({ credential }) => {
1784
+ const token = await refreshCredential(credential, url, response);
1785
+ if (token) refreshedTokens.set(credential, token);
1786
+ })
1787
+ );
1788
+ if (!refreshedTokens.size && !shouldRetrySignature) return response;
1789
+ await ((_f = response.body) == null ? void 0 : _f.cancel().catch(() => {
1790
+ }));
1791
+ if ((_g = requestOptions.signal) == null ? void 0 : _g.aborted) throw createAbortError3();
1792
+ const replayAuthorization = resolvedURL ? await authorizeRequest(
1793
+ resolvedURL,
1794
+ requestOptions,
1795
+ tokenCredentials,
1796
+ refreshedTokens,
1797
+ authorization.credentials
1798
+ ) : { url, options: requestOptions };
1799
+ const replayRequest = await authenticateRequest(
1800
+ replayAuthorization,
1801
+ requestCredentials,
1802
+ "retry",
1803
+ response
1804
+ );
1805
+ return baseFetch(replayRequest.url, replayRequest.options);
1806
+ };
1807
+ Object.defineProperty(authenticatedFetch, AUTHENTICATED_FETCH, {
1808
+ value: {
1809
+ fetch: baseFetch,
1810
+ credentials,
1811
+ credentialResolutions,
1812
+ fetchOptions
1813
+ }
1814
+ });
1815
+ return authenticatedFetch;
1816
+ }
1817
+ function mergeFetchOptions(defaultOptions, requestOptions) {
1818
+ const mergedOptions = { ...defaultOptions, ...requestOptions };
1819
+ if (defaultOptions.headers || (requestOptions == null ? void 0 : requestOptions.headers)) {
1820
+ const headers = new Headers(defaultOptions.headers);
1821
+ new Headers(requestOptions == null ? void 0 : requestOptions.headers).forEach((value, key) => headers.set(key, value));
1822
+ mergedOptions.headers = headers;
1823
+ }
1824
+ return mergedOptions;
1825
+ }
1826
+ async function authenticateRequest(request, credentials, reason, response) {
1827
+ const appliedCredentials = [];
1828
+ const signal = request.options.signal;
1829
+ for (const credential of credentials) {
1830
+ const origin = getRequestOrigin(request.url);
1831
+ if (!origin || !normalizeRequestOrigins(credential.origins).includes(origin)) continue;
1832
+ if (signal == null ? void 0 : signal.aborted) throw createAbortError3();
1833
+ const authenticatedRequest = await credential.authenticate({
1834
+ url: request.url,
1835
+ options: { ...request.options, headers: new Headers(request.options.headers) },
1836
+ reason,
1837
+ response: response ? { status: response.status, headers: new Headers(response.headers) } : void 0
1838
+ });
1839
+ if (!authenticatedRequest || typeof authenticatedRequest.url !== "string" || !authenticatedRequest.options || typeof authenticatedRequest.options !== "object") {
1840
+ throw new Error("Authentication callback returned an invalid request.");
1841
+ }
1842
+ request = {
1843
+ url: authenticatedRequest.url,
1844
+ options: { ...authenticatedRequest.options, ...signal ? { signal } : {} }
1845
+ };
1846
+ appliedCredentials.push(credential);
1847
+ }
1848
+ if (signal == null ? void 0 : signal.aborted) throw createAbortError3();
1849
+ return { ...request, credentials: appliedCredentials };
1850
+ }
1851
+ function getRequestOrigin(url) {
1852
+ try {
1853
+ const parsedURL = new URL(url);
1854
+ if (!parsedURL.host || parsedURL.username || parsedURL.password) return null;
1855
+ return parsedURL.origin !== "null" ? parsedURL.origin : `${parsedURL.protocol}//${parsedURL.host}`;
1856
+ } catch {
1857
+ return null;
1858
+ }
1859
+ }
1860
+ function normalizeRequestOrigins(origins) {
1861
+ if (!origins.length)
1862
+ throw new Error("Request authentication requires at least one exact origin.");
1863
+ return origins.map((origin) => {
1864
+ const resolvedOrigin = getRequestOrigin(origin);
1865
+ let url;
1866
+ try {
1867
+ url = new URL(origin);
1868
+ } catch {
1869
+ throw new Error("Invalid request authentication origin.");
1870
+ }
1871
+ if (!resolvedOrigin || url.pathname && url.pathname !== "/" || url.search || url.hash) {
1872
+ throw new Error("Invalid request authentication origin.");
1873
+ }
1874
+ return resolvedOrigin;
1875
+ });
1876
+ }
1877
+ async function authorizeRequest(sourceURL, requestOptions, credentials, tokenOverrides = /* @__PURE__ */ new Map(), previousCredentials = []) {
1878
+ const url = new URL(sourceURL);
1879
+ const headers = new Headers(requestOptions.headers);
1880
+ const resolvedCredentials = [];
1881
+ const previousTokens = new Map(
1882
+ previousCredentials.map(({ credential, token }) => [credential, token])
1883
+ );
1884
+ for (const credential of credentials) {
1885
+ if (!credential.origins.includes(url.origin)) continue;
1886
+ const overrideToken = tokenOverrides.get(credential);
1887
+ const previousToken = previousTokens.get(credential);
1888
+ const ownsQueryParameter = credential.type === "query-parameter" && previousToken !== void 0;
1889
+ const ownsHeader = credential.type === "header" && previousToken !== void 0;
1890
+ if (credential.type === "query-parameter" && url.searchParams.has(credential.name) && !ownsQueryParameter || credential.type === "header" && headers.has(credential.name) && !ownsHeader) {
1891
+ continue;
1892
+ }
1893
+ const token = overrideToken ?? previousToken ?? await resolveToken(credential.token, { url: sourceURL.toString(), reason: "request" });
1894
+ if (!token) continue;
1895
+ if (credential.type === "query-parameter") {
1896
+ url.searchParams.set(credential.name, token);
1897
+ } else {
1898
+ headers.set(credential.name, `${credential.prefix || ""}${token}`);
1899
+ }
1900
+ resolvedCredentials.push({ credential, token });
1901
+ }
1902
+ return {
1903
+ url: url.toString(),
1904
+ options: { ...requestOptions, headers },
1905
+ credentials: resolvedCredentials
1906
+ };
1907
+ }
1908
+ async function refreshCredential(credential, url, response) {
1909
+ const existingPromise = refreshPromises.get(credential);
1910
+ if (existingPromise) return existingPromise;
1911
+ const tokenProvider = credential.token;
1912
+ const promise = Promise.resolve(
1913
+ tokenProvider({
1914
+ url,
1915
+ reason: "refresh",
1916
+ response: { status: response.status, headers: response.headers }
1917
+ })
1918
+ ).finally(() => refreshPromises.delete(credential));
1919
+ refreshPromises.set(credential, promise);
1920
+ return promise;
1921
+ }
1922
+ async function resolveToken(token, context) {
1923
+ return typeof token === "function" ? token(context) : token;
1924
+ }
1925
+ function parseHTTPURL(url) {
1926
+ try {
1927
+ const parsedURL = new URL(url);
1928
+ return ["http:", "https:"].includes(parsedURL.protocol) ? parsedURL : null;
1929
+ } catch {
1930
+ return null;
1931
+ }
1932
+ }
1933
+ function isReplayableRequest(options) {
1934
+ const method = (options.method || "GET").toUpperCase();
1935
+ if (!["GET", "HEAD", "OPTIONS", "PUT", "DELETE"].includes(method)) return false;
1936
+ return !(options.body && typeof ReadableStream !== "undefined" && options.body instanceof ReadableStream);
1937
+ }
1938
+ function createAbortError3() {
1939
+ return new DOMException("The operation was aborted.", "AbortError");
1940
+ }
1941
+
1942
+ // ../loader-utils/src/lib/path-utils/file-aliases.ts
1943
+ var pathPrefix = "";
1944
+ var fileAliases = {};
1945
+ function setPathPrefix(prefix) {
1946
+ pathPrefix = prefix;
1947
+ }
1948
+ function getPathPrefix() {
1949
+ return pathPrefix;
1950
+ }
1951
+ function resolvePath(filename2) {
1952
+ for (const alias in fileAliases) {
1953
+ if (filename2.startsWith(alias)) {
1954
+ const replacement = fileAliases[alias];
1955
+ filename2 = filename2.replace(alias, replacement);
1956
+ }
1957
+ }
1958
+ if (!filename2.startsWith("http://") && !filename2.startsWith("https://")) {
1959
+ filename2 = `${pathPrefix}${filename2}`;
1960
+ }
1961
+ return filename2;
1962
+ }
1963
+
1964
+ // ../loader-utils/src/lib/path-utils/path.ts
1965
+ var path_exports = {};
1966
+ __export(path_exports, {
1967
+ dirname: () => dirname,
1968
+ filename: () => filename,
1969
+ join: () => join,
1970
+ resolve: () => resolve
1971
+ });
1972
+
1973
+ // ../loader-utils/src/lib/path-utils/get-cwd.ts
1974
+ function getCWD() {
1975
+ var _a;
1976
+ if (typeof process !== "undefined" && typeof process.cwd !== "undefined") {
1977
+ return process.cwd();
1978
+ }
1979
+ const pathname = (_a = globalThis.location) == null ? void 0 : _a.pathname;
1980
+ return (pathname == null ? void 0 : pathname.slice(0, pathname.lastIndexOf("/") + 1)) || "";
1981
+ }
1982
+
1983
+ // ../loader-utils/src/lib/path-utils/path.ts
1984
+ function filename(url) {
1985
+ const slashIndex = url ? url.lastIndexOf("/") : -1;
1986
+ return slashIndex >= 0 ? url.substr(slashIndex + 1) : url;
1987
+ }
1988
+ function dirname(url) {
1989
+ const slashIndex = url ? url.lastIndexOf("/") : -1;
1990
+ return slashIndex >= 0 ? url.substr(0, slashIndex) : "";
1991
+ }
1992
+ function join(...parts) {
1993
+ const separator = "/";
1994
+ parts = parts.map((part, index) => {
1995
+ if (index) {
1996
+ part = part.replace(new RegExp(`^${separator}`), "");
1997
+ }
1998
+ if (index !== parts.length - 1) {
1999
+ part = part.replace(new RegExp(`${separator}$`), "");
2000
+ }
2001
+ return part;
2002
+ });
2003
+ return parts.join(separator);
2004
+ }
2005
+ function resolve(...components) {
2006
+ const paths = [];
2007
+ for (let _i = 0; _i < components.length; _i++) {
2008
+ paths[_i] = components[_i];
2009
+ }
2010
+ let resolvedPath = "";
2011
+ let resolvedAbsolute = false;
2012
+ let cwd;
2013
+ for (let i = paths.length - 1; i >= -1 && !resolvedAbsolute; i--) {
2014
+ let path;
2015
+ if (i >= 0) {
2016
+ path = paths[i];
2017
+ } else {
2018
+ if (cwd === void 0) {
2019
+ cwd = getCWD();
2020
+ }
2021
+ path = cwd;
2022
+ }
2023
+ if (path.length === 0) {
2024
+ continue;
2025
+ }
2026
+ resolvedPath = `${path}/${resolvedPath}`;
2027
+ resolvedAbsolute = path.charCodeAt(0) === SLASH;
2028
+ }
2029
+ resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute);
2030
+ if (resolvedAbsolute) {
2031
+ return `/${resolvedPath}`;
2032
+ } else if (resolvedPath.length > 0) {
2033
+ return resolvedPath;
2034
+ }
2035
+ return ".";
2036
+ }
2037
+ var SLASH = 47;
2038
+ var DOT = 46;
2039
+ function normalizeStringPosix(path, allowAboveRoot) {
2040
+ let res = "";
2041
+ let lastSlash = -1;
2042
+ let dots = 0;
2043
+ let code;
2044
+ let isAboveRoot = false;
2045
+ for (let i = 0; i <= path.length; ++i) {
2046
+ if (i < path.length) {
2047
+ code = path.charCodeAt(i);
2048
+ } else if (code === SLASH) {
2049
+ break;
2050
+ } else {
2051
+ code = SLASH;
2052
+ }
2053
+ if (code === SLASH) {
2054
+ if (lastSlash === i - 1 || dots === 1) {
2055
+ } else if (lastSlash !== i - 1 && dots === 2) {
2056
+ if (res.length < 2 || !isAboveRoot || res.charCodeAt(res.length - 1) !== DOT || res.charCodeAt(res.length - 2) !== DOT) {
2057
+ if (res.length > 2) {
2058
+ const start = res.length - 1;
2059
+ let j = start;
2060
+ for (; j >= 0; --j) {
2061
+ if (res.charCodeAt(j) === SLASH) {
2062
+ break;
2063
+ }
2064
+ }
2065
+ if (j !== start) {
2066
+ res = j === -1 ? "" : res.slice(0, j);
2067
+ lastSlash = i;
2068
+ dots = 0;
2069
+ isAboveRoot = false;
2070
+ continue;
2071
+ }
2072
+ } else if (res.length === 2 || res.length === 1) {
2073
+ res = "";
2074
+ lastSlash = i;
2075
+ dots = 0;
2076
+ isAboveRoot = false;
2077
+ continue;
2078
+ }
2079
+ }
2080
+ if (allowAboveRoot) {
2081
+ if (res.length > 0) {
2082
+ res += "/..";
2083
+ } else {
2084
+ res = "..";
2085
+ }
2086
+ isAboveRoot = true;
2087
+ }
2088
+ } else {
2089
+ const slice = path.slice(lastSlash + 1, i);
2090
+ if (res.length > 0) {
2091
+ res += `/${slice}`;
2092
+ } else {
2093
+ res = slice;
2094
+ }
2095
+ isAboveRoot = false;
2096
+ }
2097
+ lastSlash = i;
2098
+ dots = 0;
2099
+ } else if (code === DOT && dots !== -1) {
2100
+ ++dots;
2101
+ } else {
2102
+ dots = -1;
2103
+ }
2104
+ }
2105
+ return res;
2106
+ }
2107
+
2108
+ // ../loader-utils/src/json-loader.ts
2109
+ var VERSION3 = typeof __VERSION__ !== "undefined" ? __VERSION__ : "latest";
2110
+ var JSONLoader = {
2111
+ dataType: null,
2112
+ batchType: null,
2113
+ name: "JSON",
2114
+ id: "json",
2115
+ module: "json",
2116
+ version: VERSION3,
2117
+ encoding: "json",
2118
+ format: "json",
2119
+ extensions: ["json", "geojson"],
2120
+ mimeTypes: ["application/json"],
2121
+ category: "json",
2122
+ text: true,
2123
+ parseTextSync,
2124
+ parse: async (arrayBuffer) => parseTextSync(new TextDecoder().decode(arrayBuffer)),
2125
+ options: {}
2126
+ };
2127
+ function parseTextSync(text) {
2128
+ return JSON.parse(text);
2129
+ }
2130
+
2131
+ // ../loader-utils/src/lib/node/buffer.browser.ts
2132
+ function toArrayBuffer(buffer) {
2133
+ return buffer;
2134
+ }
2135
+
2136
+ // ../loader-utils/src/lib/binary-utils/memory-conversion-utils.ts
2137
+ function isBuffer(value) {
2138
+ return value && typeof value === "object" && value.isBuffer;
2139
+ }
2140
+ function toArrayBuffer2(data) {
2141
+ if (isBuffer(data)) {
2142
+ return toArrayBuffer(data);
2143
+ }
2144
+ if (data instanceof ArrayBuffer) {
2145
+ return data;
2146
+ }
2147
+ if (isSharedArrayBuffer(data)) {
2148
+ return copyToArrayBuffer2(data);
2149
+ }
2150
+ if (ArrayBuffer.isView(data)) {
2151
+ const buffer = data.buffer;
2152
+ if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) {
2153
+ return buffer;
2154
+ }
2155
+ return buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
2156
+ }
2157
+ if (typeof data === "string") {
2158
+ const text = data;
2159
+ const uint8Array = new TextEncoder().encode(text);
2160
+ return uint8Array.buffer;
2161
+ }
2162
+ if (data && typeof data === "object" && data._toArrayBuffer) {
2163
+ return data._toArrayBuffer();
2164
+ }
2165
+ throw new Error("toArrayBuffer");
2166
+ }
2167
+ function ensureArrayBuffer(bufferSource) {
2168
+ if (bufferSource instanceof ArrayBuffer) {
2169
+ return bufferSource;
2170
+ }
2171
+ if (isSharedArrayBuffer(bufferSource)) {
2172
+ return copyToArrayBuffer2(bufferSource);
2173
+ }
2174
+ const { buffer, byteOffset, byteLength } = bufferSource;
2175
+ if (buffer instanceof ArrayBuffer && byteOffset === 0 && byteLength === buffer.byteLength) {
2176
+ return buffer;
2177
+ }
2178
+ return copyToArrayBuffer2(buffer, byteOffset, byteLength);
2179
+ }
2180
+ function copyToArrayBuffer2(buffer, byteOffset = 0, byteLength = buffer.byteLength - byteOffset) {
2181
+ const view = new Uint8Array(buffer, byteOffset, byteLength);
2182
+ const copy = new Uint8Array(view.length);
2183
+ copy.set(view);
2184
+ return copy.buffer;
2185
+ }
2186
+ function toArrayBufferView(data) {
2187
+ if (ArrayBuffer.isView(data)) {
2188
+ return data;
2189
+ }
2190
+ return new Uint8Array(data);
2191
+ }
2192
+
2193
+ // ../loader-utils/src/lib/files/blob-file.ts
2194
+ var BlobFile = class {
2195
+ handle;
2196
+ size;
2197
+ bigsize;
2198
+ url;
2199
+ constructor(blob) {
2200
+ this.handle = blob instanceof ArrayBuffer ? new Blob([blob]) : blob;
2201
+ this.size = blob instanceof ArrayBuffer ? blob.byteLength : blob.size;
2202
+ this.bigsize = BigInt(this.size);
2203
+ this.url = blob instanceof File ? blob.name : "";
2204
+ }
2205
+ async close() {
2206
+ }
2207
+ async stat() {
2208
+ return {
2209
+ size: this.handle.size,
2210
+ bigsize: BigInt(this.handle.size),
2211
+ isDirectory: false
2212
+ };
2213
+ }
2214
+ async read(start, length, signal) {
2215
+ if (signal == null ? void 0 : signal.aborted) {
2216
+ throw new Error("Request aborted");
2217
+ }
2218
+ const arrayBuffer = await this.handle.slice(Number(start), Number(start) + Number(length)).arrayBuffer();
2219
+ if (signal == null ? void 0 : signal.aborted) {
2220
+ throw new Error("Request aborted");
2221
+ }
2222
+ return arrayBuffer;
2223
+ }
2224
+ };
2225
+
2226
+ // ../loader-utils/src/lib/files/node-file-facade.ts
2227
+ var NOT_IMPLEMENTED = new Error("Not implemented");
2228
+ var NodeFileFacade = class {
2229
+ handle;
2230
+ size = 0;
2231
+ bigsize = 0n;
2232
+ url = "";
2233
+ constructor(url, flags, mode) {
2234
+ var _a;
2235
+ if ((_a = globalThis.loaders) == null ? void 0 : _a.NodeFile) {
2236
+ return new globalThis.loaders.NodeFile(url, flags, mode);
2237
+ }
2238
+ if (isBrowser) {
2239
+ throw new Error("Can't instantiate NodeFile in browser.");
2240
+ }
2241
+ throw new Error("Can't instantiate NodeFile. Make sure to import @loaders.gl/polyfills first.");
2242
+ }
2243
+ /** Read data */
2244
+ async read(start, length) {
2245
+ throw NOT_IMPLEMENTED;
2246
+ }
2247
+ /** Write to file. The number of bytes written will be returned */
2248
+ async write(arrayBuffer, offset, length) {
2249
+ throw NOT_IMPLEMENTED;
2250
+ }
2251
+ /** Get information about file */
2252
+ async stat() {
2253
+ throw NOT_IMPLEMENTED;
2254
+ }
2255
+ /** Truncates the file descriptor. Only available on NodeFile. */
2256
+ async truncate(length) {
2257
+ throw NOT_IMPLEMENTED;
2258
+ }
2259
+ /** Append data to a file. Only available on NodeFile. */
2260
+ async append(data) {
2261
+ throw NOT_IMPLEMENTED;
2262
+ }
2263
+ /** Close the file */
2264
+ async close() {
2265
+ }
2266
+ };
2267
+
2268
+ // ../loader-utils/src/source-types.ts
2269
+ function isSourceLoader(loader) {
2270
+ return Boolean(loader && typeof loader === "object" && "createDataSource" in loader);
2271
+ }
92
2272
 
93
2273
  // dist/lib/fetch/fetch-error.js
94
2274
  var FetchError = class extends Error {
@@ -106,9 +2286,6 @@ var FetchError = class extends Error {
106
2286
  response;
107
2287
  };
108
2288
 
109
- // dist/lib/utils/resource-utils.js
110
- var import_loader_utils = require("@loaders.gl/loader-utils");
111
-
112
2289
  // dist/lib/utils/mime-type-utils.js
113
2290
  var DATA_URL_PATTERN = /^data:([-\w.]+\/[-\w.+]+)(;|,)/;
114
2291
  var MIME_TYPE_PATTERN = /^([-\w.]+\/[-\w.+]+)/;
@@ -119,16 +2296,16 @@ function compareMIMETypes(mimeType1, mimeType2) {
119
2296
  return false;
120
2297
  }
121
2298
  function parseMIMEType(mimeString) {
122
- const matches = MIME_TYPE_PATTERN.exec(mimeString);
123
- if (matches) {
124
- return matches[1];
2299
+ const matches3 = MIME_TYPE_PATTERN.exec(mimeString);
2300
+ if (matches3) {
2301
+ return matches3[1];
125
2302
  }
126
2303
  return mimeString;
127
2304
  }
128
2305
  function parseMIMETypeFromURL(url) {
129
- const matches = DATA_URL_PATTERN.exec(url);
130
- if (matches) {
131
- return matches[1];
2306
+ const matches3 = DATA_URL_PATTERN.exec(url);
2307
+ if (matches3) {
2308
+ return matches3[1];
132
2309
  }
133
2310
  return "";
134
2311
  }
@@ -136,8 +2313,8 @@ function parseMIMETypeFromURL(url) {
136
2313
  // dist/lib/utils/url-utils.js
137
2314
  var QUERY_STRING_PATTERN = /\?.*/;
138
2315
  function extractQueryString(url) {
139
- const matches = url.match(QUERY_STRING_PATTERN);
140
- return matches && matches[0];
2316
+ const matches3 = url.match(QUERY_STRING_PATTERN);
2317
+ return matches3 && matches3[0];
141
2318
  }
142
2319
  function stripQueryString(url) {
143
2320
  return url.replace(QUERY_STRING_PATTERN, "");
@@ -153,10 +2330,10 @@ function shortenUrlForDisplay(url) {
153
2330
 
154
2331
  // dist/lib/utils/resource-utils.js
155
2332
  function getResourceUrl(resource) {
156
- if ((0, import_loader_utils.isResponse)(resource)) {
2333
+ if (isResponse(resource)) {
157
2334
  return resource.url;
158
2335
  }
159
- if ((0, import_loader_utils.isBlob)(resource)) {
2336
+ if (isBlob(resource)) {
160
2337
  const fileName = "name" in resource ? resource.name : "";
161
2338
  return fileName || "";
162
2339
  }
@@ -166,12 +2343,12 @@ function getResourceUrl(resource) {
166
2343
  return "";
167
2344
  }
168
2345
  function getResourceMIMEType(resource) {
169
- if ((0, import_loader_utils.isResponse)(resource)) {
2346
+ if (isResponse(resource)) {
170
2347
  const contentTypeHeader = resource.headers.get("content-type") || "";
171
2348
  const noQueryUrl = stripQueryString(resource.url);
172
2349
  return parseMIMEType(contentTypeHeader) || parseMIMETypeFromURL(noQueryUrl);
173
2350
  }
174
- if ((0, import_loader_utils.isBlob)(resource)) {
2351
+ if (isBlob(resource)) {
175
2352
  return resource.type || "";
176
2353
  }
177
2354
  if (typeof resource === "string") {
@@ -180,11 +2357,11 @@ function getResourceMIMEType(resource) {
180
2357
  return "";
181
2358
  }
182
2359
  function getResourceContentLength(resource) {
183
- if ((0, import_loader_utils.isResponse)(resource)) {
2360
+ if (isResponse(resource)) {
184
2361
  const response = resource;
185
2362
  return response.headers["content-length"] || -1;
186
2363
  }
187
- if ((0, import_loader_utils.isBlob)(resource)) {
2364
+ if (isBlob(resource)) {
188
2365
  const blob = resource;
189
2366
  return blob.size;
190
2367
  }
@@ -202,7 +2379,7 @@ function getResourceContentLength(resource) {
202
2379
 
203
2380
  // dist/lib/utils/response-utils.js
204
2381
  async function makeResponse(resource) {
205
- if ((0, import_loader_utils2.isResponse)(resource)) {
2382
+ if (isResponse(resource)) {
206
2383
  return resource;
207
2384
  }
208
2385
  const headers = {};
@@ -255,30 +2432,22 @@ async function getInitialDataUrl(resource) {
255
2432
  }
256
2433
  if (resource instanceof Blob) {
257
2434
  const blobSlice = resource.slice(0, 5);
258
- return await new Promise((resolve) => {
2435
+ return await new Promise((resolve2) => {
259
2436
  const reader = new FileReader();
260
2437
  reader.onload = (event) => {
261
2438
  var _a;
262
- return resolve((_a = event == null ? void 0 : event.target) == null ? void 0 : _a.result);
2439
+ return resolve2((_a = event == null ? void 0 : event.target) == null ? void 0 : _a.result);
263
2440
  };
264
2441
  reader.readAsDataURL(blobSlice);
265
2442
  });
266
2443
  }
267
2444
  if (resource instanceof ArrayBuffer) {
268
2445
  const slice = resource.slice(0, INITIAL_DATA_LENGTH);
269
- const base64 = arrayBufferToBase64(slice);
2446
+ const base64 = encodeArrayBufferToBase64(slice);
270
2447
  return `data:base64,${base64}`;
271
2448
  }
272
2449
  return null;
273
2450
  }
274
- function arrayBufferToBase64(buffer) {
275
- let binary = "";
276
- const bytes = new Uint8Array(buffer);
277
- for (let i = 0; i < bytes.byteLength; i++) {
278
- binary += String.fromCharCode(bytes[i]);
279
- }
280
- return btoa(binary);
281
- }
282
2451
 
283
2452
  // dist/lib/fetch/fetch-file.js
284
2453
  function isNodePath(url) {
@@ -293,7 +2462,7 @@ function isDataURL(url) {
293
2462
  async function fetchFile(urlOrData, fetchOptions) {
294
2463
  var _a, _b;
295
2464
  if (typeof urlOrData === "string") {
296
- const url = (0, import_loader_utils3.resolvePath)(urlOrData);
2465
+ const url = resolvePath(urlOrData);
297
2466
  if (isNodePath(url)) {
298
2467
  if ((_a = globalThis.loaders) == null ? void 0 : _a.fetchNode) {
299
2468
  return (_b = globalThis.loaders) == null ? void 0 : _b.fetchNode(url, fetchOptions);
@@ -313,23 +2482,20 @@ async function readArrayBuffer(file, start, length) {
313
2482
  return await readBlob(slice);
314
2483
  }
315
2484
  async function readBlob(blob) {
316
- return await new Promise((resolve, reject) => {
2485
+ return await new Promise((resolve2, reject) => {
317
2486
  const fileReader = new FileReader();
318
2487
  fileReader.onload = (event) => {
319
2488
  var _a;
320
- return resolve((_a = event == null ? void 0 : event.target) == null ? void 0 : _a.result);
2489
+ return resolve2((_a = event == null ? void 0 : event.target) == null ? void 0 : _a.result);
321
2490
  };
322
2491
  fileReader.onerror = (error) => reject(error);
323
2492
  fileReader.readAsArrayBuffer(blob);
324
2493
  });
325
2494
  }
326
2495
 
327
- // dist/lib/loader-utils/option-utils.js
328
- var import_loader_utils5 = require("@loaders.gl/loader-utils");
329
-
330
2496
  // dist/lib/loader-utils/loggers.js
331
- var import_log = require("@probe.gl/log");
332
- var probeLog = new import_log.Log({ id: "loaders.gl" });
2497
+ var import_log3 = require("@probe.gl/log");
2498
+ var probeLog = new import_log3.Log({ id: "loaders.gl" });
333
2499
  var NullLog = class {
334
2500
  /** Drops a log message. */
335
2501
  log() {
@@ -395,8 +2561,8 @@ var ConsoleLog = class {
395
2561
  };
396
2562
 
397
2563
  // dist/lib/loader-utils/option-defaults.js
398
- var import_loader_utils4 = require("@loaders.gl/loader-utils");
399
2564
  var DEFAULT_LOADER_OPTIONS = {
2565
+ searchParams: void 0,
400
2566
  core: {
401
2567
  baseUrl: void 0,
402
2568
  // baseUrl
@@ -409,14 +2575,16 @@ var DEFAULT_LOADER_OPTIONS = {
409
2575
  log: new ConsoleLog(),
410
2576
  // A probe.gl compatible (`log.log()()` syntax) that just logs to console
411
2577
  useLocalLibraries: false,
2578
+ subloaders: void 0,
412
2579
  CDN: "https://unpkg.com/@loaders.gl",
413
2580
  worker: true,
414
2581
  // By default, use worker if provided by loader.
2582
+ workerThreshold: 0.1,
415
2583
  maxConcurrency: 3,
416
2584
  // How many worker instances should be created for each loader.
417
2585
  maxMobileConcurrency: 1,
418
2586
  // How many worker instances should be created for each loader on mobile devices.
419
- reuseWorkers: import_loader_utils4.isBrowser,
2587
+ reuseWorkers: isBrowser,
420
2588
  // By default reuse workers in browser (Node.js refuses to terminate if browsers are running)
421
2589
  _nodeWorkers: false,
422
2590
  // By default do not support node workers
@@ -487,6 +2655,7 @@ var CORE_LOADER_OPTION_KEYS = [
487
2655
  "useLocalLibraries",
488
2656
  "CDN",
489
2657
  "worker",
2658
+ "workerThreshold",
490
2659
  "maxConcurrency",
491
2660
  "maxMobileConcurrency",
492
2661
  "reuseWorkers",
@@ -501,6 +2670,27 @@ var CORE_LOADER_OPTION_KEYS = [
501
2670
  "metadata",
502
2671
  "transforms"
503
2672
  ];
2673
+ function applySearchParamsToUrl(url, options) {
2674
+ const searchParams = options == null ? void 0 : options.searchParams;
2675
+ if (!searchParams || Object.keys(searchParams).length === 0 || /^(?:data|blob):/i.test(url)) {
2676
+ return url;
2677
+ }
2678
+ const hashIndex = url.indexOf("#");
2679
+ const hash = hashIndex >= 0 ? url.slice(hashIndex) : "";
2680
+ const urlWithoutHash = hashIndex >= 0 ? url.slice(0, hashIndex) : url;
2681
+ const queryIndex = urlWithoutHash.indexOf("?");
2682
+ const path = queryIndex >= 0 ? urlWithoutHash.slice(0, queryIndex) : urlWithoutHash;
2683
+ const query = queryIndex >= 0 ? urlWithoutHash.slice(queryIndex + 1) : "";
2684
+ const existingSearchParams = new URLSearchParams(query);
2685
+ const appendedSearchParams = new URLSearchParams();
2686
+ for (const [key, value] of Object.entries(searchParams)) {
2687
+ if (!existingSearchParams.has(key)) {
2688
+ appendedSearchParams.set(key, String(value));
2689
+ }
2690
+ }
2691
+ const serializedAppendedSearchParams = appendedSearchParams.toString();
2692
+ return `${path}${queryIndex >= 0 ? `?${query}` : ""}${serializedAppendedSearchParams ? `${queryIndex >= 0 ? "&" : "?"}${serializedAppendedSearchParams}` : ""}${hash}`;
2693
+ }
504
2694
  function getGlobalLoaderState() {
505
2695
  globalThis.loaders = globalThis.loaders || {};
506
2696
  const { loaders } = globalThis;
@@ -521,13 +2711,16 @@ function setGlobalOptions(options) {
521
2711
  const state = getGlobalLoaderState();
522
2712
  const globalOptions = getGlobalLoaderOptions();
523
2713
  state.globalOptions = normalizeOptionsInternal(globalOptions, options);
524
- (0, import_loader_utils5.registerJSModules)(options.modules);
2714
+ registerJSModules(options.modules);
525
2715
  }
526
2716
  function normalizeOptions(options, loader, loaders, url) {
527
2717
  loaders = loaders || [];
528
2718
  loaders = Array.isArray(loaders) ? loaders : [loaders];
529
2719
  validateOptions(options, loaders);
530
- return normalizeLoaderOptions(normalizeOptionsInternal(loader, options, url));
2720
+ validateWorkerThreshold(options);
2721
+ const normalizedOptions = normalizeLoaderOptions(normalizeOptionsInternal(loader, options, url));
2722
+ validateWorkerThreshold(normalizedOptions);
2723
+ return normalizedOptions;
531
2724
  }
532
2725
  function normalizeLoaderOptions(options) {
533
2726
  const normalized = cloneLoaderOptions(options);
@@ -551,11 +2744,18 @@ function validateOptions(options, loaders) {
551
2744
  validateOptionsObject(idOptions, loader.id, loaderOptions, deprecatedOptions, loaders);
552
2745
  }
553
2746
  }
2747
+ function validateWorkerThreshold(options) {
2748
+ var _a;
2749
+ const threshold = (_a = options.core) == null ? void 0 : _a.workerThreshold;
2750
+ if (threshold !== void 0 && (!Number.isFinite(threshold) || threshold < 0 || threshold > 1)) {
2751
+ throw new Error("core.workerThreshold must be a finite number between 0 and 1");
2752
+ }
2753
+ }
554
2754
  function validateOptionsObject(options, id, defaultOptions, deprecatedOptions, loaders) {
555
2755
  const loaderName = id || "Top level";
556
2756
  const prefix = id ? `${id}.` : "";
557
2757
  for (const key in options) {
558
- const isSubOptions = !id && (0, import_loader_utils5.isObject)(options[key]);
2758
+ const isSubOptions = !id && isObject(options[key]);
559
2759
  const isBaseUriOption = key === "baseUri" && !id;
560
2760
  const isWorkerUrlOption = key === "workerUrl" && id;
561
2761
  if (!(key in defaultOptions) && !isBaseUriOption && !isWorkerUrlOption) {
@@ -615,20 +2815,20 @@ function applyCoreShapeDefault(mergedOptions, userOptions, loader) {
615
2815
  return;
616
2816
  }
617
2817
  const loaderScopedDefaults = (_b = loader.options) == null ? void 0 : _b[loader.id];
618
- if (!(0, import_loader_utils5.isPureObject)(loaderScopedDefaults) || !("shape" in loaderScopedDefaults)) {
2818
+ if (!isPureObject(loaderScopedDefaults) || !("shape" in loaderScopedDefaults)) {
619
2819
  return;
620
2820
  }
621
2821
  const globalLoaderScopedOptions = getGlobalLoaderOptions()[loader.id];
622
- if ((0, import_loader_utils5.isPureObject)(globalLoaderScopedOptions) && "shape" in globalLoaderScopedOptions) {
2822
+ if (isPureObject(globalLoaderScopedOptions) && "shape" in globalLoaderScopedOptions) {
623
2823
  return;
624
2824
  }
625
2825
  const userLoaderScopedOptions = userOptions[loader.id];
626
- if ((0, import_loader_utils5.isPureObject)(userLoaderScopedOptions) && "shape" in userLoaderScopedOptions) {
2826
+ if (isPureObject(userLoaderScopedOptions) && "shape" in userLoaderScopedOptions) {
627
2827
  return;
628
2828
  }
629
2829
  const loaderScopedOptions = mergedOptions[loader.id];
630
2830
  mergedOptions[loader.id] = {
631
- ...(0, import_loader_utils5.isPureObject)(loaderScopedOptions) ? loaderScopedOptions : {},
2831
+ ...isPureObject(loaderScopedOptions) ? loaderScopedOptions : {},
632
2832
  shape: coreShape
633
2833
  };
634
2834
  }
@@ -636,7 +2836,7 @@ function mergeNestedFields(mergedOptions, options) {
636
2836
  for (const key in options) {
637
2837
  if (key in options) {
638
2838
  const value = options[key];
639
- if ((0, import_loader_utils5.isPureObject)(value) && (0, import_loader_utils5.isPureObject)(mergedOptions[key])) {
2839
+ if (isPureObject(value) && isPureObject(mergedOptions[key])) {
640
2840
  mergedOptions[key] = {
641
2841
  ...mergedOptions[key],
642
2842
  ...options[key]
@@ -655,7 +2855,7 @@ function addUrlOptions(options, url) {
655
2855
  const hasCoreBaseUrl = ((_a = options.core) == null ? void 0 : _a.baseUrl) !== void 0;
656
2856
  if (!hasCoreBaseUrl) {
657
2857
  options.core ||= {};
658
- options.core.baseUrl = import_loader_utils5.path.dirname(stripQueryString(url));
2858
+ options.core.baseUrl = path_exports.dirname(stripQueryString(url));
659
2859
  }
660
2860
  }
661
2861
  function cloneLoaderOptions(options) {
@@ -703,7 +2903,6 @@ function addDeprecatedTopLevelOptions(options) {
703
2903
  }
704
2904
 
705
2905
  // dist/lib/loader-utils/normalize-loader.js
706
- var import_loader_utils6 = require("@loaders.gl/loader-utils");
707
2906
  function isLoaderObject(loader) {
708
2907
  if (!loader) {
709
2908
  return false;
@@ -715,8 +2914,8 @@ function isLoaderObject(loader) {
715
2914
  return hasExtensions;
716
2915
  }
717
2916
  function normalizeLoader(loader) {
718
- (0, import_loader_utils6.assert)(loader, "null loader");
719
- (0, import_loader_utils6.assert)(isLoaderObject(loader), "invalid loader");
2917
+ assert(loader, "null loader");
2918
+ assert(isLoaderObject(loader), "invalid loader");
720
2919
  let options;
721
2920
  if (Array.isArray(loader)) {
722
2921
  options = loader[1];
@@ -760,7 +2959,6 @@ function _unregisterLoaders() {
760
2959
  }
761
2960
 
762
2961
  // dist/lib/api/select-loader.js
763
- var import_loader_utils7 = require("@loaders.gl/loader-utils");
764
2962
  var EXT_PATTERN = /\.([^.]+)$/;
765
2963
  async function selectLoader(data, loaders = [], options, context) {
766
2964
  if (!validHTTPResponse(data)) {
@@ -779,7 +2977,7 @@ async function selectLoader(data, loaders = [], options, context) {
779
2977
  if (loader) {
780
2978
  return loader;
781
2979
  }
782
- if ((0, import_loader_utils7.isBlob)(data)) {
2980
+ if (isBlob(data)) {
783
2981
  data = await data.slice(0, 10).arrayBuffer();
784
2982
  loader = selectLoaderSync(data, loaders, normalizedOptions, context);
785
2983
  }
@@ -843,7 +3041,7 @@ function selectLoaderInternal(data, loaders, options, context) {
843
3041
  reason = reason || (loader ? `matched MIME type ${type}` : "");
844
3042
  loader = loader || findLoaderByInitialBytes(loaders, data);
845
3043
  reason = reason || (loader ? `matched initial data ${getFirstCharacters(data)}` : "");
846
- if (!loader && (0, import_loader_utils7.isBlob)(data)) {
3044
+ if (!loader && isBlob(data)) {
847
3045
  loader = findSourceLoaderByTestData(loaders, data);
848
3046
  reason = reason || (loader ? "matched source testData" : "");
849
3047
  }
@@ -852,7 +3050,7 @@ function selectLoaderInternal(data, loaders, options, context) {
852
3050
  reason = reason || (loader ? `matched fallback MIME type ${type}` : "");
853
3051
  }
854
3052
  if (reason) {
855
- import_loader_utils7.log.log(1, `selectLoader selected ${loader == null ? void 0 : loader.name}: ${reason}.`);
3053
+ log.log(1, `selectLoader selected ${loader == null ? void 0 : loader.name}: ${reason}.`);
856
3054
  }
857
3055
  return loader;
858
3056
  }
@@ -868,7 +3066,7 @@ function getNoValidLoaderMessage(data) {
868
3066
  const url = getResourceUrl(data);
869
3067
  const type = getResourceMIMEType(data);
870
3068
  let message = "No valid loader found (";
871
- message += url ? `${import_loader_utils7.path.filename(url)}, ` : "no url provided, ";
3069
+ message += url ? `${path_exports.filename(url)}, ` : "no url provided, ";
872
3070
  message += `MIME type: ${type ? `"${type}"` : "not provided"}, `;
873
3071
  const firstCharacters = data ? getFirstCharacters(data) : "";
874
3072
  message += firstCharacters ? ` first bytes: "${firstCharacters}"` : "first bytes: not available";
@@ -898,7 +3096,7 @@ function findLoaderByExtension(loaders, extension) {
898
3096
  }
899
3097
  function findSourceLoaderByType(loaders, type) {
900
3098
  for (const loader of loaders) {
901
- if ((0, import_loader_utils7.isSourceLoader)(loader) && loader.type === type) {
3099
+ if (isSourceLoader(loader) && loader.type === type) {
902
3100
  return loader;
903
3101
  }
904
3102
  }
@@ -909,7 +3107,7 @@ function findSourceLoaderByTestURL(loaders, url) {
909
3107
  return null;
910
3108
  }
911
3109
  for (const loader of loaders) {
912
- if ((0, import_loader_utils7.isSourceLoader)(loader) && loader.testURL(url)) {
3110
+ if (isSourceLoader(loader) && loader.testURL(url)) {
913
3111
  return loader;
914
3112
  }
915
3113
  }
@@ -918,7 +3116,7 @@ function findSourceLoaderByTestURL(loaders, url) {
918
3116
  function findSourceLoaderByTestData(loaders, data) {
919
3117
  var _a;
920
3118
  for (const loader of loaders) {
921
- if ((0, import_loader_utils7.isSourceLoader)(loader)) {
3119
+ if (isSourceLoader(loader)) {
922
3120
  if ((_a = loader.testData) == null ? void 0 : _a.call(loader, data)) {
923
3121
  return loader;
924
3122
  }
@@ -972,12 +3170,12 @@ function testDataAgainstBinary(data, byteOffset, loader) {
972
3170
  return tests.some((test) => testBinary(data, byteOffset, loader, test));
973
3171
  }
974
3172
  function testBinary(data, byteOffset, loader, test) {
975
- if ((0, import_loader_utils7.isArrayBufferLike)(test)) {
976
- return (0, import_loader_utils7.compareArrayBuffers)(test, data, test.byteLength);
3173
+ if (isArrayBufferLike(test)) {
3174
+ return compareArrayBuffers(test, data, test.byteLength);
977
3175
  }
978
3176
  switch (typeof test) {
979
3177
  case "function":
980
- return test((0, import_loader_utils7.ensureArrayBuffer)(data));
3178
+ return test(ensureArrayBuffer(data));
981
3179
  case "string":
982
3180
  const magic = getMagicString(data, byteOffset, test.length);
983
3181
  return test === magic;
@@ -1008,15 +3206,7 @@ function getMagicString(arrayBuffer, byteOffset, length) {
1008
3206
  return magic;
1009
3207
  }
1010
3208
 
1011
- // dist/lib/api/parse.js
1012
- var import_loader_utils18 = require("@loaders.gl/loader-utils");
1013
- var import_worker_utils2 = require("@loaders.gl/worker-utils");
1014
-
1015
- // dist/lib/loader-utils/get-data.js
1016
- var import_loader_utils11 = require("@loaders.gl/loader-utils");
1017
-
1018
3209
  // dist/iterators/make-iterator/make-string-iterator.js
1019
- var import_loader_utils8 = require("@loaders.gl/loader-utils");
1020
3210
  var DEFAULT_CHUNK_SIZE = 256 * 1024;
1021
3211
  function* makeStringIterator(string, options) {
1022
3212
  const chunkSize = (options == null ? void 0 : options.chunkSize) || DEFAULT_CHUNK_SIZE;
@@ -1026,7 +3216,7 @@ function* makeStringIterator(string, options) {
1026
3216
  const chunkLength = Math.min(string.length - offset, chunkSize);
1027
3217
  const chunk = string.slice(offset, offset + chunkLength);
1028
3218
  offset += chunkLength;
1029
- yield (0, import_loader_utils8.ensureArrayBuffer)(textEncoder.encode(chunk));
3219
+ yield ensureArrayBuffer(textEncoder.encode(chunk));
1030
3220
  }
1031
3221
  }
1032
3222
 
@@ -1060,37 +3250,72 @@ async function* makeBlobIterator(blob, options) {
1060
3250
  }
1061
3251
 
1062
3252
  // dist/iterators/make-iterator/make-stream-iterator.js
1063
- var import_loader_utils9 = require("@loaders.gl/loader-utils");
1064
3253
  function makeStreamIterator(stream, options) {
1065
- return import_loader_utils9.isBrowser ? makeBrowserStreamIterator(stream, options) : makeNodeStreamIterator(stream, options);
3254
+ return isBrowser ? makeBrowserStreamIterator(stream, options) : makeNodeStreamIterator(stream, options);
1066
3255
  }
1067
3256
  async function* makeBrowserStreamIterator(stream, options) {
1068
3257
  const reader = stream.getReader();
1069
3258
  let nextBatchPromise;
3259
+ let streamFinished = false;
3260
+ let streamReadFailed = false;
3261
+ let iteratorFailed = false;
1070
3262
  try {
1071
3263
  while (true) {
1072
3264
  const currentBatchPromise = nextBatchPromise || reader.read();
1073
- if (options == null ? void 0 : options._streamReadAhead) {
1074
- nextBatchPromise = reader.read();
3265
+ nextBatchPromise = void 0;
3266
+ let batch;
3267
+ try {
3268
+ batch = await currentBatchPromise;
3269
+ } catch (error) {
3270
+ streamReadFailed = true;
3271
+ throw error;
1075
3272
  }
1076
- const { done, value } = await currentBatchPromise;
3273
+ const { done, value } = batch;
1077
3274
  if (done) {
3275
+ streamFinished = true;
1078
3276
  return;
1079
3277
  }
1080
- yield (0, import_loader_utils9.toArrayBuffer)(value);
3278
+ if (options == null ? void 0 : options._streamReadAhead) {
3279
+ nextBatchPromise = reader.read();
3280
+ void nextBatchPromise.catch(() => {
3281
+ });
3282
+ }
3283
+ yield toArrayBuffer2(value);
3284
+ }
3285
+ } catch (error) {
3286
+ iteratorFailed = true;
3287
+ throw error;
3288
+ } finally {
3289
+ let cleanupFailed = false;
3290
+ let cleanupError;
3291
+ if (!streamFinished && !streamReadFailed) {
3292
+ try {
3293
+ await reader.cancel();
3294
+ } catch (error) {
3295
+ cleanupFailed = true;
3296
+ cleanupError = error;
3297
+ }
3298
+ }
3299
+ try {
3300
+ reader.releaseLock();
3301
+ } catch (error) {
3302
+ if (!cleanupFailed) {
3303
+ cleanupFailed = true;
3304
+ cleanupError = error;
3305
+ }
3306
+ }
3307
+ if (!iteratorFailed && cleanupFailed) {
3308
+ throw cleanupError;
1081
3309
  }
1082
- } catch (_error) {
1083
- reader.releaseLock();
1084
3310
  }
1085
3311
  }
1086
3312
  async function* makeNodeStreamIterator(stream, options) {
1087
3313
  for await (const chunk of stream) {
1088
- yield (0, import_loader_utils9.toArrayBuffer)(chunk);
3314
+ yield toArrayBuffer2(chunk);
1089
3315
  }
1090
3316
  }
1091
3317
 
1092
3318
  // dist/iterators/make-iterator/make-iterator.js
1093
- var import_loader_utils10 = require("@loaders.gl/loader-utils");
1094
3319
  function makeIterator(data, options) {
1095
3320
  if (typeof data === "string") {
1096
3321
  return makeStringIterator(data, options);
@@ -1098,13 +3323,13 @@ function makeIterator(data, options) {
1098
3323
  if (data instanceof ArrayBuffer) {
1099
3324
  return makeArrayBufferIterator(data, options);
1100
3325
  }
1101
- if ((0, import_loader_utils10.isBlob)(data)) {
3326
+ if (isBlob(data)) {
1102
3327
  return makeBlobIterator(data, options);
1103
3328
  }
1104
- if ((0, import_loader_utils10.isReadableStream)(data)) {
3329
+ if (isReadableStream(data)) {
1105
3330
  return makeStreamIterator(data, options);
1106
3331
  }
1107
- if ((0, import_loader_utils10.isResponse)(data)) {
3332
+ if (isResponse(data)) {
1108
3333
  const responseBody = data.body;
1109
3334
  if (!responseBody) {
1110
3335
  throw new Error("Readable stream not available on Response");
@@ -1120,35 +3345,35 @@ function getArrayBufferOrStringFromDataSync(data, loader, options) {
1120
3345
  if (loader.text && typeof data === "string") {
1121
3346
  return data;
1122
3347
  }
1123
- if ((0, import_loader_utils11.isBuffer)(data)) {
3348
+ if (isBuffer(data)) {
1124
3349
  data = data.buffer;
1125
3350
  }
1126
- if ((0, import_loader_utils11.isArrayBufferLike)(data)) {
1127
- const bufferSource = (0, import_loader_utils11.toArrayBufferView)(data);
3351
+ if (isArrayBufferLike(data)) {
3352
+ const bufferSource = toArrayBufferView(data);
1128
3353
  if (loader.text && !loader.binary) {
1129
3354
  const textDecoder = new TextDecoder("utf8");
1130
3355
  return textDecoder.decode(bufferSource);
1131
3356
  }
1132
- return (0, import_loader_utils11.toArrayBuffer)(bufferSource);
3357
+ return toArrayBuffer2(bufferSource);
1133
3358
  }
1134
3359
  throw new Error(ERR_DATA);
1135
3360
  }
1136
3361
  async function getArrayBufferOrStringFromData(data, loader, options) {
1137
- if (typeof data === "string" || (0, import_loader_utils11.isArrayBufferLike)(data)) {
3362
+ if (typeof data === "string" || isArrayBufferLike(data)) {
1138
3363
  return getArrayBufferOrStringFromDataSync(data, loader, options);
1139
3364
  }
1140
- if ((0, import_loader_utils11.isBlob)(data)) {
3365
+ if (isBlob(data)) {
1141
3366
  data = await makeResponse(data);
1142
3367
  }
1143
- if ((0, import_loader_utils11.isResponse)(data)) {
3368
+ if (isResponse(data)) {
1144
3369
  await checkResponse(data);
1145
3370
  return loader.binary ? await data.arrayBuffer() : await data.text();
1146
3371
  }
1147
- if ((0, import_loader_utils11.isReadableStream)(data)) {
3372
+ if (isReadableStream(data)) {
1148
3373
  data = makeIterator(data, options);
1149
3374
  }
1150
- if ((0, import_loader_utils11.isIterable)(data) || (0, import_loader_utils11.isAsyncIterable)(data)) {
1151
- return (0, import_loader_utils11.concatenateArrayBuffersAsync)(data);
3375
+ if (isIterable(data) || isAsyncIterable(data)) {
3376
+ return concatenateArrayBuffersAsync(data);
1152
3377
  }
1153
3378
  throw new Error(ERR_DATA);
1154
3379
  }
@@ -1156,32 +3381,32 @@ async function getArrayBufferFromData(data, options) {
1156
3381
  if (typeof data === "string") {
1157
3382
  return new TextEncoder().encode(data).buffer;
1158
3383
  }
1159
- if ((0, import_loader_utils11.isArrayBufferLike)(data)) {
1160
- return (0, import_loader_utils11.toArrayBuffer)((0, import_loader_utils11.toArrayBufferView)(data));
3384
+ if (isArrayBufferLike(data)) {
3385
+ return toArrayBuffer2(toArrayBufferView(data));
1161
3386
  }
1162
- if ((0, import_loader_utils11.isBlob)(data)) {
3387
+ if (isBlob(data)) {
1163
3388
  data = await makeResponse(data);
1164
3389
  }
1165
- if ((0, import_loader_utils11.isResponse)(data)) {
3390
+ if (isResponse(data)) {
1166
3391
  await checkResponse(data);
1167
3392
  return await data.arrayBuffer();
1168
3393
  }
1169
- if ((0, import_loader_utils11.isReadableStream)(data)) {
3394
+ if (isReadableStream(data)) {
1170
3395
  data = makeIterator(data, options);
1171
3396
  }
1172
- if ((0, import_loader_utils11.isIterable)(data) || (0, import_loader_utils11.isAsyncIterable)(data)) {
1173
- return (0, import_loader_utils11.concatenateArrayBuffersAsync)(data);
3397
+ if (isIterable(data) || isAsyncIterable(data)) {
3398
+ return concatenateArrayBuffersAsync(data);
1174
3399
  }
1175
3400
  throw new Error(ERR_DATA);
1176
3401
  }
1177
3402
  async function getAsyncIterableFromData(data, options) {
1178
- if ((0, import_loader_utils11.isPromise)(data)) {
3403
+ if (isPromise(data)) {
1179
3404
  data = await data;
1180
3405
  }
1181
- if ((0, import_loader_utils11.isIterator)(data)) {
3406
+ if (isIterator(data)) {
1182
3407
  return data;
1183
3408
  }
1184
- if ((0, import_loader_utils11.isResponse)(data)) {
3409
+ if (isResponse(data)) {
1185
3410
  await checkResponse(data);
1186
3411
  const body = await data.body;
1187
3412
  if (!body) {
@@ -1189,68 +3414,73 @@ async function getAsyncIterableFromData(data, options) {
1189
3414
  }
1190
3415
  return makeIterator(body, options);
1191
3416
  }
1192
- if ((0, import_loader_utils11.isBlob)(data) || (0, import_loader_utils11.isReadableStream)(data)) {
3417
+ if (isBlob(data) || isReadableStream(data)) {
1193
3418
  return makeIterator(data, options);
1194
3419
  }
1195
- if ((0, import_loader_utils11.isAsyncIterable)(data)) {
3420
+ if (isAsyncIterable(data)) {
1196
3421
  return data;
1197
3422
  }
1198
- if ((0, import_loader_utils11.isIterable)(data)) {
3423
+ if (isIterable(data)) {
1199
3424
  return data;
1200
3425
  }
1201
3426
  return getIterableFromData(data);
1202
3427
  }
1203
3428
  function getIterableFromData(data) {
1204
3429
  if (ArrayBuffer.isView(data)) {
1205
- return function* oneChunk() {
1206
- yield (0, import_loader_utils11.toArrayBuffer)(data);
1207
- }();
3430
+ return (function* oneChunk() {
3431
+ yield toArrayBuffer2(data);
3432
+ })();
1208
3433
  }
1209
- if ((0, import_loader_utils11.isArrayBufferLike)(data)) {
1210
- return function* oneChunk() {
1211
- yield (0, import_loader_utils11.toArrayBuffer)(data);
1212
- }();
3434
+ if (isArrayBufferLike(data)) {
3435
+ return (function* oneChunk() {
3436
+ yield toArrayBuffer2(data);
3437
+ })();
1213
3438
  }
1214
- if ((0, import_loader_utils11.isIterator)(data)) {
3439
+ if (isIterator(data)) {
1215
3440
  return data;
1216
3441
  }
1217
- if ((0, import_loader_utils11.isIterable)(data)) {
3442
+ if (isIterable(data)) {
1218
3443
  return data[Symbol.iterator]();
1219
3444
  }
1220
3445
  throw new Error(ERR_DATA);
1221
3446
  }
1222
3447
 
1223
3448
  // dist/lib/loader-utils/get-fetch-function.js
1224
- var import_loader_utils12 = require("@loaders.gl/loader-utils");
1225
3449
  function getFetchFunction(options, context) {
1226
- var _a;
3450
+ var _a, _b, _c;
1227
3451
  const globalOptions = getGlobalLoaderOptions();
1228
3452
  const loaderOptions = options || globalOptions;
1229
3453
  const fetchOption = loaderOptions.fetch ?? ((_a = loaderOptions.core) == null ? void 0 : _a.fetch);
3454
+ let fetchFunction;
1230
3455
  if (typeof fetchOption === "function") {
1231
- return fetchOption;
1232
- }
1233
- if ((0, import_loader_utils12.isObject)(fetchOption)) {
1234
- return (url) => fetchFile(url, fetchOption);
1235
- }
1236
- if (context == null ? void 0 : context.fetch) {
1237
- return context == null ? void 0 : context.fetch;
3456
+ fetchFunction = fetchOption;
3457
+ } else if (isObject(fetchOption)) {
3458
+ fetchFunction = (context == null ? void 0 : context.fetch) || fetchFile;
3459
+ } else if (context == null ? void 0 : context.fetch) {
3460
+ fetchFunction = context.fetch;
3461
+ } else {
3462
+ fetchFunction = fetchFile;
1238
3463
  }
1239
- return fetchFile;
3464
+ return createAuthenticatedFetch({
3465
+ fetch: fetchFunction,
3466
+ credentials: ((_b = loaderOptions.core) == null ? void 0 : _b.credentials) || [],
3467
+ authentications: (_c = loaderOptions.core) == null ? void 0 : _c.authentications,
3468
+ fetchOptions: isObject(fetchOption) ? fetchOption : void 0
3469
+ });
1240
3470
  }
1241
3471
 
1242
- // dist/lib/loader-utils/loader-context.js
1243
- var import_loader_utils17 = require("@loaders.gl/loader-utils");
1244
-
1245
- // dist/lib/api/parse-file.js
1246
- var import_loader_utils13 = require("@loaders.gl/loader-utils");
1247
- var import_worker_utils = require("@loaders.gl/worker-utils");
1248
-
1249
3472
  // dist/lib/api/load-loader.js
1250
3473
  var loaderImplementationPromises = /* @__PURE__ */ new Map();
1251
3474
  var loaderImplementations = /* @__PURE__ */ new Map();
1252
3475
  async function getLoaderImplementation(loader, options, url) {
1253
3476
  var _a;
3477
+ if (isSourceLoader(loader)) {
3478
+ const source = loader.preload ? await loader.preload(url || "", options) : loader;
3479
+ if (!isSourceLoader(source)) {
3480
+ throw new Error(`${loader.id} source loader preload() did not return a runtime source loader`);
3481
+ }
3482
+ return source;
3483
+ }
1254
3484
  if (isLoaderWithParser(loader)) {
1255
3485
  return loader;
1256
3486
  }
@@ -1287,6 +3517,13 @@ async function getLoaderImplementation(loader, options, url) {
1287
3517
  }
1288
3518
  function getLoaderImplementationSync(loader) {
1289
3519
  var _a;
3520
+ if (loader.subloaders && !preparedLoaders.has(loader)) {
3521
+ const prepared = preparedDefaults.get(loader);
3522
+ if (prepared)
3523
+ return prepared;
3524
+ if (!isLoaderWithParser(loader))
3525
+ return null;
3526
+ }
1290
3527
  if (isLoaderWithParser(loader)) {
1291
3528
  return loader;
1292
3529
  }
@@ -1327,10 +3564,7 @@ function getLoaderImplementationCacheKey(loader, options) {
1327
3564
  var _a;
1328
3565
  const loaderOptions = options == null ? void 0 : options[loader.id];
1329
3566
  const defaultLoaderOptions = (_a = loader.options) == null ? void 0 : _a[loader.id];
1330
- return String((loaderOptions == null ? void 0 : loaderOptions.backend) || getDeprecatedImplementationBackend(loaderOptions == null ? void 0 : loaderOptions.implementation) || (defaultLoaderOptions == null ? void 0 : defaultLoaderOptions.backend) || "");
1331
- }
1332
- function getDeprecatedImplementationBackend(implementation) {
1333
- return implementation === "js" ? "typescript" : implementation;
3567
+ return String((loaderOptions == null ? void 0 : loaderOptions.backend) || (defaultLoaderOptions == null ? void 0 : defaultLoaderOptions.backend) || "");
1334
3568
  }
1335
3569
  async function importLoaderImplementation(implementationSpecifier, loaderId) {
1336
3570
  const moduleExports = await import(
@@ -1345,9 +3579,97 @@ async function importLoaderImplementation(implementationSpecifier, loaderId) {
1345
3579
  }
1346
3580
  throw new Error(`Could not find parser implementation for ${loaderId} in ${implementationSpecifier}`);
1347
3581
  }
3582
+ var preparedLoaders = /* @__PURE__ */ new WeakSet();
3583
+ var preparedDefaults = /* @__PURE__ */ new WeakMap();
3584
+ var subloaderPreparations = /* @__PURE__ */ new WeakMap();
3585
+ async function preloadLoaderDependencies(loader, options, url) {
3586
+ if (preparedLoaders.has(loader))
3587
+ return await getLoaderImplementation(loader, options, url);
3588
+ validateSubloaderGraph(loader, options);
3589
+ return await prepareLoaderDependencies(loader, options, url);
3590
+ }
3591
+ async function prepareLoaderDependencies(loader, options, url) {
3592
+ if (!options) {
3593
+ const cached = subloaderPreparations.get(loader);
3594
+ if (cached)
3595
+ return await cached;
3596
+ }
3597
+ const preparation = bindSubloaders(loader, options, url);
3598
+ if (!options) {
3599
+ subloaderPreparations.set(loader, preparation);
3600
+ void preparation.catch(() => subloaderPreparations.delete(loader));
3601
+ }
3602
+ const prepared = await preparation;
3603
+ if (!options)
3604
+ preparedDefaults.set(loader, prepared);
3605
+ return prepared;
3606
+ }
3607
+ function getSubloaders(loader, options) {
3608
+ var _a;
3609
+ const overrides = ((_a = options == null ? void 0 : options.core) == null ? void 0 : _a.subloaders) || {};
3610
+ return Object.fromEntries(Object.entries(loader.subloaders || {}).map(([name, dependency]) => [
3611
+ name,
3612
+ Object.hasOwn(overrides, name) ? overrides[name] : dependency
3613
+ ]));
3614
+ }
3615
+ function validateSubloaderGraph(loader, options) {
3616
+ visitLoader(loader, []);
3617
+ function visitLoader(currentLoader, ancestors) {
3618
+ if (ancestors.includes(currentLoader)) {
3619
+ throw new Error(`Subloader cycle: ${[...ancestors, currentLoader].map((value) => value.id).join(" -> ")}`);
3620
+ }
3621
+ for (const dependency of Object.values(getSubloaders(currentLoader, options))) {
3622
+ visitLoader(dependency, [...ancestors, currentLoader]);
3623
+ }
3624
+ }
3625
+ }
3626
+ async function bindSubloaders(loader, options, url) {
3627
+ const [implementation, entries] = await Promise.all([
3628
+ getLoaderImplementation(loader, options, url),
3629
+ Promise.all(Object.entries(getSubloaders(loader, options)).map(async ([name, dependency]) => [name, await prepareLoaderDependencies(dependency, options, url)]))
3630
+ ]);
3631
+ const subloaders = Object.fromEntries(entries);
3632
+ const defaultOptions = mergeOptions(implementation.options, options || {});
3633
+ defaultOptions.core = { ...defaultOptions.core, worker: false };
3634
+ const prepared = { ...implementation, options: defaultOptions, subloaders };
3635
+ delete prepared.preload;
3636
+ prepared.worker = false;
3637
+ for (const method of [
3638
+ "parseBlob",
3639
+ "createDataSource",
3640
+ "parse",
3641
+ "parseSync",
3642
+ "parseText",
3643
+ "parseTextSync",
3644
+ "parseInBatches",
3645
+ "parseFile",
3646
+ "parseFileInBatches",
3647
+ "parseUrl"
3648
+ ]) {
3649
+ const parser = implementation[method];
3650
+ if (typeof parser === "function") {
3651
+ prepared[method] = (data, parseOptions, context) => {
3652
+ var _a, _b;
3653
+ const forwardedOptions = mergeOptions(options, parseOptions || {});
3654
+ return parser.call(implementation, data, {
3655
+ ...forwardedOptions,
3656
+ core: {
3657
+ ...forwardedOptions.core,
3658
+ subloaders: ((_a = options == null ? void 0 : options.core) == null ? void 0 : _a.subloaders) || ((_b = parseOptions == null ? void 0 : parseOptions.core) == null ? void 0 : _b.subloaders),
3659
+ worker: false
3660
+ },
3661
+ [loader.id]: { ...forwardedOptions[loader.id], subloaders }
3662
+ }, context);
3663
+ };
3664
+ }
3665
+ }
3666
+ preparedLoaders.add(prepared);
3667
+ return prepared;
3668
+ }
1348
3669
 
1349
3670
  // dist/lib/api/parse-file.js
1350
3671
  async function parseFile(file, loaders, options, context) {
3672
+ var _a, _b;
1351
3673
  if (loaders && !Array.isArray(loaders) && !isLoaderObject(loaders)) {
1352
3674
  context = void 0;
1353
3675
  options = loaders;
@@ -1361,10 +3683,12 @@ async function parseFile(file, loaders, options, context) {
1361
3683
  if (!loader) {
1362
3684
  return null;
1363
3685
  }
1364
- if ((0, import_loader_utils13.isSourceLoader)(loader)) {
3686
+ if (isSourceLoader(loader)) {
1365
3687
  throw new Error(`${loader.id} is a SourceLoader. Use load() to create a runtime source object instead of parseFile().`);
1366
3688
  }
1367
- const strictOptions = normalizeOptions(options, loader, candidateLoaders, url);
3689
+ const normalizedOptions = normalizeOptions(options, loader, candidateLoaders, url);
3690
+ const authenticationLoader = !loader.getAuthentications && ((_b = (_a = normalizedOptions.core) == null ? void 0 : _a.credentials) == null ? void 0 : _b.length) ? await getLoaderImplementation(loader, normalizedOptions, url || (context == null ? void 0 : context.url)) : loader;
3691
+ const strictOptions = await resolveLoaderAuthenticationOptions(authenticationLoader, url, normalizedOptions);
1368
3692
  context = getLoaderContext(
1369
3693
  // @ts-expect-error candidateLoaders may be a single forced loader
1370
3694
  { url, _parse: parse, loaders: candidateLoaders },
@@ -1374,8 +3698,8 @@ async function parseFile(file, loaders, options, context) {
1374
3698
  return await parseFileWithLoader(loader, file, strictOptions, context);
1375
3699
  }
1376
3700
  async function parseFileWithLoader(loader, file, options, context) {
1377
- (0, import_worker_utils.validateWorkerVersion)(loader);
1378
- options = (0, import_loader_utils13.mergeOptions)(loader.options, options);
3701
+ validateWorkerVersion(loader);
3702
+ options = mergeOptions(loader.options, options);
1379
3703
  const loaderWithParser = await getLoaderImplementation(loader, options, context.url);
1380
3704
  if (loaderWithParser.parseFile) {
1381
3705
  return await loaderWithParser.parseFile(file, options, context);
@@ -1393,7 +3717,6 @@ async function parseFileWithLoader(loader, file, options, context) {
1393
3717
  }
1394
3718
 
1395
3719
  // dist/lib/api/parse-sync.js
1396
- var import_loader_utils14 = require("@loaders.gl/loader-utils");
1397
3720
  function parseSync(data, loaders, options, context) {
1398
3721
  if (!Array.isArray(loaders) && !isLoaderObject(loaders)) {
1399
3722
  context = void 0;
@@ -1407,7 +3730,7 @@ function parseSync(data, loaders, options, context) {
1407
3730
  if (!loader) {
1408
3731
  return null;
1409
3732
  }
1410
- if ((0, import_loader_utils14.isSourceLoader)(loader)) {
3733
+ if (isSourceLoader(loader)) {
1411
3734
  throw new Error(`${loader.id} is a SourceLoader. Use load() to create a runtime source object instead of parseSync().`);
1412
3735
  }
1413
3736
  const strictOptions = normalizeOptions(options, loader, candidateLoaders);
@@ -1433,10 +3756,52 @@ function parseWithLoaderSync(loader, data, options, context) {
1433
3756
  throw new Error(`${loader.name} loader: 'parseSync' not supported by this loader, use 'parse' instead. ${context.url || ""}`);
1434
3757
  }
1435
3758
 
3759
+ // ../schema-utils/src/lib/table/tables/table-accessors.ts
3760
+ function isTable(table) {
3761
+ var _a;
3762
+ const shape = typeof table === "object" && (table == null ? void 0 : table.shape);
3763
+ switch (shape) {
3764
+ case "array-row-table":
3765
+ case "object-row-table":
3766
+ return Array.isArray(table.data);
3767
+ case "geojson-table":
3768
+ return Array.isArray(table.features);
3769
+ case "columnar-table":
3770
+ return Boolean(table.data && typeof table.data === "object");
3771
+ case "arrow-table":
3772
+ return Boolean(((_a = table == null ? void 0 : table.data) == null ? void 0 : _a.numRows) !== void 0);
3773
+ default:
3774
+ return false;
3775
+ }
3776
+ }
3777
+ function getTableLength(table) {
3778
+ switch (table.shape) {
3779
+ case "array-row-table":
3780
+ case "object-row-table":
3781
+ return table.data.length;
3782
+ case "geojson-table":
3783
+ return table.features.length;
3784
+ case "arrow-table":
3785
+ const arrowTable = table.data;
3786
+ return arrowTable.numRows;
3787
+ case "columnar-table":
3788
+ for (const column of Object.values(table.data)) {
3789
+ return column.length || 0;
3790
+ }
3791
+ return 0;
3792
+ default:
3793
+ throw new Error("table");
3794
+ }
3795
+ }
3796
+
3797
+ // ../schema-utils/src/lib/table/batches/make-table-batch-iterator.ts
3798
+ function makeBatchFromTable(table) {
3799
+ return { ...table, length: getTableLength(table), batchType: "data" };
3800
+ }
3801
+
1436
3802
  // dist/lib/api/parse-in-batches.js
1437
- var import_schema_utils = require("@loaders.gl/schema-utils");
1438
- var import_loader_utils15 = require("@loaders.gl/loader-utils");
1439
3803
  async function parseInBatches(data, loaders, options, context) {
3804
+ var _a, _b;
1440
3805
  const loaderArray = Array.isArray(loaders) ? loaders : void 0;
1441
3806
  if (!Array.isArray(loaders) && !isLoaderObject(loaders)) {
1442
3807
  context = void 0;
@@ -1450,7 +3815,9 @@ async function parseInBatches(data, loaders, options, context) {
1450
3815
  if (!loader) {
1451
3816
  return [];
1452
3817
  }
1453
- const strictOptions = normalizeOptions(options, loader, loaderArray, url);
3818
+ const normalizedOptions = normalizeOptions(options, loader, loaderArray, url);
3819
+ const authenticationLoader = !loader.getAuthentications && ((_b = (_a = normalizedOptions.core) == null ? void 0 : _a.credentials) == null ? void 0 : _b.length) ? await getLoaderImplementation(loader, normalizedOptions, url || (context == null ? void 0 : context.url)) : loader;
3820
+ const strictOptions = await resolveLoaderAuthenticationOptions(authenticationLoader, url || "", normalizedOptions);
1454
3821
  context = getLoaderContext({ url, _parseInBatches: parseInBatches, _parse: parse, loaders: loaderArray }, strictOptions, context || null);
1455
3822
  const loaderWithParser = await getLoaderImplementation(loader, strictOptions, context.url);
1456
3823
  return await parseWithLoaderInBatches(loader, loaderWithParser, data, strictOptions, context);
@@ -1483,12 +3850,15 @@ async function parseToOutputIterator(loader, data, options, context) {
1483
3850
  const inputIterator = await getAsyncIterableFromData(data, options);
1484
3851
  const transformedIterator = await applyInputTransforms(inputIterator, ((_a = options == null ? void 0 : options.core) == null ? void 0 : _a.transforms) || []);
1485
3852
  if (loader.parseInBatches) {
3853
+ if (loader.id === "csv" && isBrowser && canParseWithWorker(loader, options)) {
3854
+ return parseWithWorkerInBatches(loader, transformedIterator, options, context, parse);
3855
+ }
1486
3856
  return loader.parseInBatches(transformedIterator, options, context);
1487
3857
  }
1488
3858
  return parseChunkInBatches(transformedIterator, loader, options, context);
1489
3859
  }
1490
3860
  async function* parseChunkInBatches(transformedIterator, loader, options, context) {
1491
- const arrayBuffer = await (0, import_loader_utils15.concatenateArrayBuffersAsync)(transformedIterator);
3861
+ const arrayBuffer = await concatenateArrayBuffersAsync(transformedIterator);
1492
3862
  const parsedData = await parse(
1493
3863
  arrayBuffer,
1494
3864
  loader,
@@ -1500,7 +3870,7 @@ async function* parseChunkInBatches(transformedIterator, loader, options, contex
1500
3870
  yield batch;
1501
3871
  }
1502
3872
  function convertDataToBatch(parsedData, loader) {
1503
- const batch = (0, import_schema_utils.isTable)(parsedData) ? (0, import_schema_utils.makeBatchFromTable)(parsedData) : {
3873
+ const batch = isTable(parsedData) ? makeBatchFromTable(parsedData) : {
1504
3874
  shape: "unknown",
1505
3875
  batchType: "data",
1506
3876
  data: parsedData,
@@ -1517,9 +3887,6 @@ async function applyInputTransforms(inputIterator, transforms = []) {
1517
3887
  return iteratorChain;
1518
3888
  }
1519
3889
 
1520
- // dist/lib/api/load.js
1521
- var import_loader_utils16 = require("@loaders.gl/loader-utils");
1522
-
1523
3890
  // dist/lib/api/load-in-batches.js
1524
3891
  function loadInBatches(files, loaders, options, context) {
1525
3892
  let loadersArray;
@@ -1530,17 +3897,24 @@ function loadInBatches(files, loaders, options, context) {
1530
3897
  } else {
1531
3898
  loadersArray = loaders;
1532
3899
  }
1533
- const fetch2 = getFetchFunction(options || {});
1534
3900
  if (!Array.isArray(files)) {
1535
- return loadOneFileInBatches(files, loadersArray, options || {}, fetch2);
3901
+ return loadOneFileInBatches(files, loadersArray, options || {});
1536
3902
  }
1537
- const promises = files.map((file) => loadOneFileInBatches(file, loadersArray, options || {}, fetch2));
3903
+ const promises = files.map((file) => loadOneFileInBatches(file, loadersArray, options || {}));
1538
3904
  return promises;
1539
3905
  }
1540
- async function loadOneFileInBatches(file, loaders, options, fetch2) {
3906
+ async function loadOneFileInBatches(file, loaders, options) {
3907
+ var _a, _b;
1541
3908
  if (typeof file === "string") {
1542
3909
  const url = file;
1543
- const response = await fetch2(url);
3910
+ const loader = await selectLoader(url, loaders, {
3911
+ ...options,
3912
+ core: { ...options.core, nothrow: true }
3913
+ });
3914
+ const authenticationLoader = loader && !loader.getAuthentications && ((_b = (_a = options.core) == null ? void 0 : _a.credentials) == null ? void 0 : _b.length) ? await getLoaderImplementation(loader, options, url) : loader;
3915
+ options = await resolveLoaderAuthenticationOptions(authenticationLoader, url, options);
3916
+ const fetch2 = getFetchFunction(options);
3917
+ const response = await fetch2(applySearchParamsToUrl(url, options));
1544
3918
  return Array.isArray(loaders) ? await parseInBatches(response, loaders, options) : await parseInBatches(response, loaders, options);
1545
3919
  }
1546
3920
  return Array.isArray(loaders) ? await parseInBatches(file, loaders, options) : await parseInBatches(file, loaders, options);
@@ -1559,7 +3933,10 @@ async function load(url, loaders, options, context) {
1559
3933
  resolvedLoaders = loaders;
1560
3934
  resolvedOptions = options;
1561
3935
  }
1562
- if (!Array.isArray(resolvedLoaders) && (0, import_loader_utils16.isSourceLoader)(resolvedLoaders)) {
3936
+ resolvedOptions ||= getGlobalLoaderOptions();
3937
+ if (!Array.isArray(resolvedLoaders) && isSourceLoader(resolvedLoaders)) {
3938
+ const sourceLoader = await resolveSourceLoader(resolvedLoaders, url, resolvedOptions);
3939
+ resolvedOptions = await resolveLoaderAuthenticationOptions(resolvedLoaders.getAuthentications ? resolvedLoaders : sourceLoader, typeof url === "string" ? url : "", resolvedOptions || {});
1563
3940
  const runtimeCoreApi = {
1564
3941
  fetchFile,
1565
3942
  parse,
@@ -1569,9 +3946,11 @@ async function load(url, loaders, options, context) {
1569
3946
  load,
1570
3947
  loadInBatches
1571
3948
  };
1572
- return resolvedLoaders.createDataSource(url, resolvedOptions || {}, runtimeCoreApi);
3949
+ return sourceLoader.createDataSource(url, resolvedOptions || {}, runtimeCoreApi);
1573
3950
  }
1574
- if (Array.isArray(resolvedLoaders) && resolvedLoaders.length === 1 && (0, import_loader_utils16.isSourceLoader)(resolvedLoaders[0])) {
3951
+ if (Array.isArray(resolvedLoaders) && resolvedLoaders.length === 1 && isSourceLoader(resolvedLoaders[0])) {
3952
+ const sourceLoader = await resolveSourceLoader(resolvedLoaders[0], url, resolvedOptions);
3953
+ resolvedOptions = await resolveLoaderAuthenticationOptions(resolvedLoaders[0].getAuthentications ? resolvedLoaders[0] : sourceLoader, typeof url === "string" ? url : "", resolvedOptions || {});
1575
3954
  const runtimeCoreApi = {
1576
3955
  fetchFile,
1577
3956
  parse,
@@ -1581,15 +3960,17 @@ async function load(url, loaders, options, context) {
1581
3960
  load,
1582
3961
  loadInBatches
1583
3962
  };
1584
- return resolvedLoaders[0].createDataSource(url, resolvedOptions || {}, runtimeCoreApi);
3963
+ return sourceLoader.createDataSource(url, resolvedOptions || {}, runtimeCoreApi);
1585
3964
  }
1586
- if (typeof url === "string" || (0, import_loader_utils16.isBlob)(url)) {
3965
+ if (typeof url === "string" || isBlob(url)) {
1587
3966
  const selectedLoader = await selectLoader(url, resolvedLoaders, {
1588
3967
  ...resolvedOptions,
1589
3968
  core: { ...resolvedOptions == null ? void 0 : resolvedOptions.core, nothrow: true }
1590
3969
  });
1591
- if (selectedLoader && (0, import_loader_utils16.isSourceLoader)(selectedLoader)) {
1592
- return selectedLoader.createDataSource(url, resolvedOptions || {}, {
3970
+ if (selectedLoader && isSourceLoader(selectedLoader)) {
3971
+ const sourceLoader = await resolveSourceLoader(selectedLoader, url, resolvedOptions);
3972
+ resolvedOptions = await resolveLoaderAuthenticationOptions(selectedLoader.getAuthentications ? selectedLoader : sourceLoader, typeof url === "string" ? url : "", resolvedOptions || {});
3973
+ return sourceLoader.createDataSource(url, resolvedOptions || {}, {
1593
3974
  fetchFile,
1594
3975
  parse,
1595
3976
  parseFile,
@@ -1599,13 +3980,24 @@ async function load(url, loaders, options, context) {
1599
3980
  loadInBatches
1600
3981
  });
1601
3982
  }
3983
+ if (selectedLoader && typeof url === "string") {
3984
+ const loaderImplementation = await getLoaderImplementation(selectedLoader, resolvedOptions, url);
3985
+ resolvedOptions = await resolveLoaderAuthenticationOptions(selectedLoader.getAuthentications ? selectedLoader : loaderImplementation, url, resolvedOptions || {});
3986
+ const parseUrl = loaderImplementation.parseUrl;
3987
+ if (parseUrl) {
3988
+ const requestUrl = applySearchParamsToUrl(url, resolvedOptions);
3989
+ return await parseUrl(requestUrl, resolvedOptions, context);
3990
+ }
3991
+ }
1602
3992
  }
3993
+ if (resolvedOptions)
3994
+ resolvedOptions = resolveAuthenticationOptions(resolvedOptions);
1603
3995
  const fetch2 = getFetchFunction(resolvedOptions);
1604
3996
  let data = url;
1605
3997
  if (typeof url === "string") {
1606
- data = await fetch2(url);
3998
+ data = await fetch2(applySearchParamsToUrl(url, resolvedOptions));
1607
3999
  }
1608
- if ((0, import_loader_utils16.isBlob)(url)) {
4000
+ if (isBlob(url)) {
1609
4001
  data = await fetch2(url);
1610
4002
  }
1611
4003
  if (typeof url === "string") {
@@ -1622,6 +4014,16 @@ async function load(url, loaders, options, context) {
1622
4014
  }
1623
4015
  return Array.isArray(resolvedLoaders) ? await parse(data, resolvedLoaders, resolvedOptions, context) : await parse(data, resolvedLoaders, resolvedOptions, context);
1624
4016
  }
4017
+ async function resolveSourceLoader(loader, url, options) {
4018
+ if (!loader.preload) {
4019
+ return loader;
4020
+ }
4021
+ const sourceLoader = await loader.preload(typeof url === "string" ? url : "", options);
4022
+ if (!isSourceLoader(sourceLoader)) {
4023
+ throw new Error(`${loader.id} source loader preload() did not return a runtime source loader`);
4024
+ }
4025
+ return sourceLoader;
4026
+ }
1625
4027
 
1626
4028
  // dist/lib/api/core-api.js
1627
4029
  var coreApi = Object.freeze({
@@ -1637,7 +4039,12 @@ var coreApi = Object.freeze({
1637
4039
  // dist/lib/loader-utils/loader-context.js
1638
4040
  function getLoaderContext(context, options, parentContext) {
1639
4041
  if (parentContext) {
1640
- return parentContext;
4042
+ return {
4043
+ ...parentContext,
4044
+ coreApi: parentContext.coreApi || context.coreApi || coreApi,
4045
+ _parse: parentContext._parse || context._parse,
4046
+ fetch: getFetchFunction(options, parentContext)
4047
+ };
1641
4048
  }
1642
4049
  const newContext = {
1643
4050
  ...context,
@@ -1648,8 +4055,8 @@ function getLoaderContext(context, options, parentContext) {
1648
4055
  const baseUrl = stripQueryString(newContext.url);
1649
4056
  newContext.baseUrl = baseUrl;
1650
4057
  newContext.queryString = extractQueryString(newContext.url);
1651
- newContext.filename = import_loader_utils17.path.filename(baseUrl);
1652
- newContext.baseUrl = import_loader_utils17.path.dirname(baseUrl);
4058
+ newContext.filename = path_exports.filename(baseUrl);
4059
+ newContext.baseUrl = path_exports.dirname(baseUrl);
1653
4060
  }
1654
4061
  if (!Array.isArray(newContext.loaders)) {
1655
4062
  newContext.loaders = null;
@@ -1673,6 +4080,7 @@ function getLoadersFromContext(loaders, context) {
1673
4080
 
1674
4081
  // dist/lib/api/parse.js
1675
4082
  async function parse(data, loaders, options, context) {
4083
+ var _a, _b;
1676
4084
  if (loaders && !Array.isArray(loaders) && !isLoaderObject(loaders)) {
1677
4085
  context = void 0;
1678
4086
  options = loaders;
@@ -1687,10 +4095,12 @@ async function parse(data, loaders, options, context) {
1687
4095
  if (!loader) {
1688
4096
  return null;
1689
4097
  }
1690
- if ((0, import_loader_utils18.isSourceLoader)(loader)) {
4098
+ if (isSourceLoader(loader)) {
1691
4099
  throw new Error(`${loader.id} is a SourceLoader. Use load() to create a runtime source object instead of parse().`);
1692
4100
  }
1693
- const strictOptions = normalizeOptions(options, loader, candidateLoaders, url);
4101
+ const normalizedOptions = normalizeOptions(options, loader, candidateLoaders, url);
4102
+ const authenticationLoader = !loader.getAuthentications && ((_b = (_a = normalizedOptions.core) == null ? void 0 : _a.credentials) == null ? void 0 : _b.length) ? await getLoaderImplementation(loader, normalizedOptions, url || (context == null ? void 0 : context.url)) : loader;
4103
+ const strictOptions = await resolveLoaderAuthenticationOptions(authenticationLoader, url || "", normalizedOptions);
1694
4104
  context = getLoaderContext(
1695
4105
  // @ts-expect-error
1696
4106
  { url, _parse: parse, loaders: candidateLoaders },
@@ -1700,17 +4110,20 @@ async function parse(data, loaders, options, context) {
1700
4110
  return await parseWithLoader(loader, data, strictOptions, context);
1701
4111
  }
1702
4112
  async function parseWithLoader(loader, data, options, context) {
1703
- (0, import_worker_utils2.validateWorkerVersion)(loader);
1704
- options = (0, import_loader_utils18.mergeOptions)(loader.options, options);
1705
- if ((0, import_loader_utils18.isResponse)(data)) {
4113
+ validateWorkerVersion(loader);
4114
+ options = mergeOptions(loader.options, options);
4115
+ if (isResponse(data)) {
1706
4116
  const { ok, redirected, status, statusText, type, url } = data;
1707
4117
  const headers = Object.fromEntries(data.headers.entries());
1708
4118
  context.response = { headers, ok, redirected, status, statusText, type, url };
1709
4119
  }
1710
4120
  const loaderWithParser = await getLoaderImplementation(loader, options, context.url);
1711
- if ((0, import_loader_utils18.canParseWithWorker)(loaderWithParser, options)) {
4121
+ if (shouldParseWithWorker(loaderWithParser, data, options, context, loader)) {
1712
4122
  data = await getArrayBufferFromData(data, options);
1713
- return await (0, import_loader_utils18.parseWithWorker)(loaderWithParser, data, options, context, parse);
4123
+ return await parseWithWorker(loaderWithParser, data, options, context, parse);
4124
+ }
4125
+ if (isBlob(data) && loaderWithParser.parseBlob) {
4126
+ return await loaderWithParser.parseBlob(data, options, context);
1714
4127
  }
1715
4128
  data = await getArrayBufferOrStringFromData(data, loader, options);
1716
4129
  return await parseWithLoaderImplementation(loaderWithParser, data, options, context);
@@ -1733,21 +4146,20 @@ async function parseWithLoaderImplementation(loader, data, options, context) {
1733
4146
 
1734
4147
  // dist/lib/api/preload.js
1735
4148
  async function preload(loader, options, url) {
1736
- return await getLoaderImplementation(loader, options, url);
4149
+ return loader.subloaders ? await preloadLoaderDependencies(loader, options, url) : await getLoaderImplementation(loader, options, url);
1737
4150
  }
1738
4151
  function preloadSync(loader) {
1739
4152
  return getLoaderImplementationSync(loader);
1740
4153
  }
1741
4154
 
1742
4155
  // dist/lib/api/encode-table.js
1743
- var import_loader_utils19 = require("@loaders.gl/loader-utils");
1744
4156
  async function encodeTable(data, writer, options) {
1745
4157
  if (writer.encode) {
1746
4158
  return await writer.encode(data, options);
1747
4159
  }
1748
4160
  if (writer.encodeText) {
1749
4161
  const text = await writer.encodeText(data, options);
1750
- return (0, import_loader_utils19.ensureArrayBuffer)(new TextEncoder().encode(text));
4162
+ return ensureArrayBuffer(new TextEncoder().encode(text));
1751
4163
  }
1752
4164
  if (writer.encodeInBatches) {
1753
4165
  const batches = encodeTableInBatches(data, writer, options);
@@ -1755,7 +4167,7 @@ async function encodeTable(data, writer, options) {
1755
4167
  for await (const batch of batches) {
1756
4168
  chunks.push(batch);
1757
4169
  }
1758
- return (0, import_loader_utils19.concatenateArrayBuffers)(...chunks);
4170
+ return concatenateArrayBuffers(...chunks);
1759
4171
  }
1760
4172
  throw new Error("Writer could not encode data");
1761
4173
  }
@@ -1782,16 +4194,14 @@ function getIterator(data) {
1782
4194
  }
1783
4195
 
1784
4196
  // dist/lib/api/encode.js
1785
- var import_loader_utils20 = require("@loaders.gl/loader-utils");
1786
- var import_worker_utils3 = require("@loaders.gl/worker-utils");
1787
4197
  async function encode(data, writer, options_) {
1788
4198
  const globalOptions = getGlobalLoaderOptions();
1789
4199
  const options = { ...globalOptions, ...options_ };
1790
4200
  if (writer.encodeURLtoURL) {
1791
4201
  return encodeWithCommandLineTool(writer, data, options);
1792
4202
  }
1793
- if ((0, import_loader_utils20.canEncodeWithWorker)(writer, options)) {
1794
- return await (0, import_worker_utils3.processOnWorker)(writer, data, options);
4203
+ if (canEncodeWithWorker(writer, options)) {
4204
+ return await processOnWorker(writer, data, options);
1795
4205
  }
1796
4206
  return await writer.encode(data, options);
1797
4207
  }
@@ -1800,7 +4210,7 @@ function encodeSync(data, writer, options) {
1800
4210
  return writer.encodeSync(data, options);
1801
4211
  }
1802
4212
  if (writer.encodeTextSync) {
1803
- return (0, import_loader_utils20.ensureArrayBuffer)(new TextEncoder().encode(writer.encodeTextSync(data, options)));
4213
+ return ensureArrayBuffer(new TextEncoder().encode(writer.encodeTextSync(data, options)));
1804
4214
  }
1805
4215
  throw new Error(`Writer ${writer.name} could not synchronously encode data`);
1806
4216
  }
@@ -1835,20 +4245,20 @@ function encodeInBatches(data, writer, options) {
1835
4245
  throw new Error(`Writer ${writer.name} could not encode in batches`);
1836
4246
  }
1837
4247
  async function encodeURLtoURL(inputUrl, outputUrl, writer, options) {
1838
- inputUrl = (0, import_loader_utils20.resolvePath)(inputUrl);
1839
- outputUrl = (0, import_loader_utils20.resolvePath)(outputUrl);
1840
- if (import_loader_utils20.isBrowser || !writer.encodeURLtoURL) {
4248
+ inputUrl = resolvePath(inputUrl);
4249
+ outputUrl = resolvePath(outputUrl);
4250
+ if (isBrowser || !writer.encodeURLtoURL) {
1841
4251
  throw new Error();
1842
4252
  }
1843
4253
  const outputFilename = await writer.encodeURLtoURL(inputUrl, outputUrl, options);
1844
4254
  return outputFilename;
1845
4255
  }
1846
4256
  async function encodeWithCommandLineTool(writer, data, options) {
1847
- if (import_loader_utils20.isBrowser) {
4257
+ if (isBrowser) {
1848
4258
  throw new Error(`Writer ${writer.name} not supported in browser`);
1849
4259
  }
1850
4260
  const tmpInputFilename = getTemporaryFilename("input");
1851
- const file = new import_loader_utils20.NodeFile(tmpInputFilename, "w");
4261
+ const file = new NodeFileFacade(tmpInputFilename, "w");
1852
4262
  await file.write(data);
1853
4263
  const tmpOutputFilename = getTemporaryFilename("output");
1854
4264
  const outputFilename = await encodeURLtoURL(tmpInputFilename, tmpOutputFilename, writer, options);
@@ -1859,13 +4269,13 @@ function getIterator2(data) {
1859
4269
  const dataIterator = [{ ...data, start: 0, end: data.length }];
1860
4270
  return dataIterator;
1861
4271
  }
1862
- function getTemporaryFilename(filename) {
1863
- return `/tmp/${filename}`;
4272
+ function getTemporaryFilename(filename2) {
4273
+ return `/tmp/${filename2}`;
1864
4274
  }
1865
4275
 
1866
4276
  // dist/lib/api/create-data-source.js
1867
- function createDataSource(data, sources, options) {
1868
- var _a;
4277
+ function createDataSource(data, sources, options = {}) {
4278
+ var _a, _b, _c, _d, _e;
1869
4279
  const resolvedOptions = options || {};
1870
4280
  const type = ((_a = resolvedOptions == null ? void 0 : resolvedOptions.core) == null ? void 0 : _a.type) || resolvedOptions.type || "auto";
1871
4281
  const source = sources.length === 1 ? sources[0] : type === "auto" ? selectLoaderSync(data, sources, {
@@ -1877,7 +4287,23 @@ function createDataSource(data, sources, options) {
1877
4287
  if (!source) {
1878
4288
  throw new Error("Not a valid source type");
1879
4289
  }
1880
- return source.createDataSource(data, resolvedOptions, coreApi);
4290
+ const contributions = ((_c = (_b = resolvedOptions.core) == null ? void 0 : _b.credentials) == null ? void 0 : _c.length) ? (_d = source.getAuthentications) == null ? void 0 : _d.call(source, typeof data === "string" ? data : "", resolvedOptions) : void 0;
4291
+ if (contributions && "then" in contributions) {
4292
+ void contributions.catch(() => {
4293
+ });
4294
+ throw new Error("Use load() with an asynchronous getAuthentications() hook.");
4295
+ }
4296
+ const authenticatedOptions = resolveAuthenticationOptions((contributions == null ? void 0 : contributions.length) ? {
4297
+ ...resolvedOptions,
4298
+ core: {
4299
+ ...resolvedOptions.core,
4300
+ authentications: [
4301
+ ...((_e = resolvedOptions.core) == null ? void 0 : _e.authentications) || [],
4302
+ ...contributions || []
4303
+ ]
4304
+ }
4305
+ } : resolvedOptions);
4306
+ return source.createDataSource(data, authenticatedOptions, coreApi);
1881
4307
  }
1882
4308
  function getSourceOfType(type, sources) {
1883
4309
  for (const service of sources) {
@@ -1911,10 +4337,6 @@ function getSourceOfType2(type, sources) {
1911
4337
  return null;
1912
4338
  }
1913
4339
 
1914
- // dist/index.js
1915
- var import_loader_utils22 = require("@loaders.gl/loader-utils");
1916
- var import_loader_utils23 = require("@loaders.gl/loader-utils");
1917
-
1918
4340
  // dist/iterators/make-stream/make-stream.js
1919
4341
  function makeStream(source, options) {
1920
4342
  if (globalThis.loaders.makeNodeStream) {
@@ -1955,14 +4377,14 @@ function makeStream(source, options) {
1955
4377
  }
1956
4378
 
1957
4379
  // dist/null-loader.js
1958
- var VERSION = true ? "5.0.0-alpha.1" : "latest";
4380
+ var VERSION4 = true ? "5.0.0-alpha.10" : "latest";
1959
4381
  var NullWorkerLoader = {
1960
4382
  dataType: null,
1961
4383
  batchType: null,
1962
4384
  name: "Null loader",
1963
4385
  id: "null",
1964
4386
  module: "core",
1965
- version: VERSION,
4387
+ version: VERSION4,
1966
4388
  worker: true,
1967
4389
  mimeTypes: ["application/x.empty"],
1968
4390
  extensions: ["null"],
@@ -1977,7 +4399,7 @@ var NullLoader = {
1977
4399
  name: "Null loader",
1978
4400
  id: "null",
1979
4401
  module: "core",
1980
- version: VERSION,
4402
+ version: VERSION4,
1981
4403
  mimeTypes: ["application/x.empty"],
1982
4404
  extensions: ["null"],
1983
4405
  parse: async (arrayBuffer, options, context) => parseSync2(arrayBuffer, options || {}, context),
@@ -1996,9 +4418,6 @@ function parseSync2(arrayBuffer, options, context) {
1996
4418
  return null;
1997
4419
  }
1998
4420
 
1999
- // dist/index.js
2000
- var import_loader_utils24 = require("@loaders.gl/loader-utils");
2001
-
2002
4421
  // dist/lib/progress/fetch-progress.js
2003
4422
  async function fetchProgress(response, onProgress, onDone = () => {
2004
4423
  }, onError = () => {
@@ -2047,7 +4466,6 @@ async function read(controller, reader, loadedBytes, totalBytes, onProgress, onD
2047
4466
  }
2048
4467
 
2049
4468
  // dist/lib/filesystems/browser-filesystem.js
2050
- var import_loader_utils21 = require("@loaders.gl/loader-utils");
2051
4469
  var BrowserFileSystem = class {
2052
4470
  _fetch;
2053
4471
  files = {};
@@ -2073,27 +4491,38 @@ var BrowserFileSystem = class {
2073
4491
  * Implementation of fetch against this file system
2074
4492
  * Delegates to global fetch for http{s}:// or data://
2075
4493
  */
2076
- async fetch(path4, options) {
2077
- if (path4.includes("://")) {
2078
- return this._fetch(path4, options);
4494
+ async fetch(path, options) {
4495
+ if (path.includes("://")) {
4496
+ return this._fetch(path, options);
2079
4497
  }
2080
- const file = this.files[path4];
4498
+ const file = this._getFile(path, true);
2081
4499
  if (!file) {
2082
- return new Response(path4, { status: 400, statusText: "NOT FOUND" });
4500
+ return new Response(path, { status: 400, statusText: "NOT FOUND" });
2083
4501
  }
2084
4502
  const headers = new Headers(options == null ? void 0 : options.headers);
2085
4503
  const range = headers.get("Range");
2086
- const bytes = range && /bytes=($1)-($2)/.exec(range);
4504
+ const bytes = range && /bytes=(\d+)-(\d+)/.exec(range);
2087
4505
  if (bytes) {
2088
4506
  const start = parseInt(bytes[1]);
2089
- const end = parseInt(bytes[2]);
2090
- const data = await file.slice(start, end).arrayBuffer();
2091
- const response2 = new Response(data);
2092
- Object.defineProperty(response2, "url", { value: path4 });
4507
+ const end = Math.min(parseInt(bytes[2]), file.size - 1);
4508
+ if (start > end) {
4509
+ const response3 = new Response(null, {
4510
+ status: 416,
4511
+ headers: { "Content-Range": `bytes */${file.size}` }
4512
+ });
4513
+ Object.defineProperty(response3, "url", { value: path });
4514
+ return response3;
4515
+ }
4516
+ const data = await file.slice(start, end + 1).arrayBuffer();
4517
+ const response2 = new Response(data, {
4518
+ status: 206,
4519
+ headers: { "Content-Range": `bytes ${start}-${end}/${file.size}` }
4520
+ });
4521
+ Object.defineProperty(response2, "url", { value: path });
2093
4522
  return response2;
2094
4523
  }
2095
4524
  const response = new Response(file);
2096
- Object.defineProperty(response, "url", { value: path4 });
4525
+ Object.defineProperty(response, "url", { value: path });
2097
4526
  return response;
2098
4527
  }
2099
4528
  /**
@@ -2101,51 +4530,47 @@ var BrowserFileSystem = class {
2101
4530
  * @param dirname
2102
4531
  * @returns
2103
4532
  */
2104
- async readdir(dirname) {
4533
+ async readdir(dirname2) {
2105
4534
  const files = [];
2106
- for (const path4 in this.files) {
2107
- files.push(path4);
4535
+ for (const path in this.files) {
4536
+ files.push(path);
2108
4537
  }
2109
4538
  return files;
2110
4539
  }
2111
4540
  /**
2112
4541
  * Return information (size) about files in this file system
2113
4542
  */
2114
- async stat(path4, options) {
2115
- const file = this.files[path4];
4543
+ async stat(path, options) {
4544
+ const file = this._getFile(path, false);
2116
4545
  if (!file) {
2117
- throw new Error(path4);
4546
+ throw new Error(path);
2118
4547
  }
2119
4548
  return { size: file.size };
2120
4549
  }
2121
4550
  /**
2122
4551
  * Just removes the file from the list
2123
4552
  */
2124
- async unlink(path4) {
2125
- delete this.files[path4];
2126
- delete this.lowerCaseFiles[path4];
2127
- this.usedFiles[path4] = true;
4553
+ async unlink(path) {
4554
+ const file = this._getFile(path, false);
4555
+ if (file) {
4556
+ delete this.files[file.name];
4557
+ delete this.lowerCaseFiles[file.name.toLowerCase()];
4558
+ this.usedFiles[file.name] = true;
4559
+ }
2128
4560
  }
2129
4561
  // implements IRandomAccessFileSystem
2130
4562
  // RANDOM ACCESS
2131
4563
  async openReadableFile(pathname, flags) {
2132
- return new import_loader_utils21.BlobFile(this.files[pathname]);
4564
+ return new BlobFile(this._getFile(pathname, true));
2133
4565
  }
2134
4566
  // PRIVATE
2135
4567
  // Supports case independent paths, and file usage tracking
2136
- _getFile(path4, used) {
2137
- const file = this.files[path4] || this.lowerCaseFiles[path4];
4568
+ _getFile(path, used) {
4569
+ const file = this.files[path] || this.lowerCaseFiles[path.toLowerCase()];
2138
4570
  if (file && used) {
2139
- this.usedFiles[path4] = true;
4571
+ this.usedFiles[path] = true;
2140
4572
  }
2141
4573
  return file;
2142
4574
  }
2143
4575
  };
2144
-
2145
- // dist/index.js
2146
- var import_loader_utils25 = require("@loaders.gl/loader-utils");
2147
- var import_loader_utils26 = require("@loaders.gl/loader-utils");
2148
- var import_loader_utils27 = require("@loaders.gl/loader-utils");
2149
- var import_loader_utils28 = require("@loaders.gl/loader-utils");
2150
- var import_loader_utils29 = require("@loaders.gl/loader-utils");
2151
4576
  //# sourceMappingURL=index.cjs.map