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

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
@@ -3,7 +3,7 @@
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
5
  import type {LoaderContext, LoaderOptions, FetchLike} from '@loaders.gl/loader-utils';
6
- import {isObject} from '@loaders.gl/loader-utils';
6
+ import {createAuthenticatedFetch, isObject} from '@loaders.gl/loader-utils';
7
7
  import {fetchFile} from '../fetch/fetch-file';
8
8
  import {getGlobalLoaderOptions} from './option-utils';
9
9
 
@@ -21,22 +21,23 @@ export function getFetchFunction(
21
21
 
22
22
  const loaderOptions = options || globalOptions;
23
23
  const fetchOption = loaderOptions.fetch ?? loaderOptions.core?.fetch;
24
+ let fetchFunction: FetchLike;
24
25
 
25
26
  // options.fetch can be a function
26
27
  if (typeof fetchOption === 'function') {
27
- return fetchOption;
28
+ fetchFunction = fetchOption;
29
+ } else if (isObject(fetchOption)) {
30
+ fetchFunction = context?.fetch || fetchFile;
31
+ } else if (context?.fetch) {
32
+ fetchFunction = context.fetch;
33
+ } else {
34
+ fetchFunction = fetchFile;
28
35
  }
29
36
 
30
- // options.fetch can be an options object
31
- if (isObject(fetchOption)) {
32
- return url => fetchFile(url, fetchOption as RequestInit);
33
- }
34
-
35
- // else refer to context (from parent loader) if available
36
- if (context?.fetch) {
37
- return context?.fetch;
38
- }
39
-
40
- // else return the default fetch function
41
- return fetchFile;
37
+ return createAuthenticatedFetch({
38
+ fetch: fetchFunction,
39
+ credentials: loaderOptions.core?.credentials || [],
40
+ authentications: loaderOptions.core?.authentications,
41
+ fetchOptions: isObject(fetchOption) ? (fetchOption as RequestInit) : undefined
42
+ });
42
43
  }
@@ -19,17 +19,22 @@ type LoaderContextProps = Omit<LoaderContext, 'fetch' | 'coreApi'> &
19
19
  *
20
20
  * @param context
21
21
  * @param options
22
- * @param previousContext
22
+ * @param parentContext
23
23
  */
24
24
  export function getLoaderContext(
25
25
  context: LoaderContextProps,
26
26
  options: StrictLoaderOptions,
27
27
  parentContext: LoaderContext | null
28
28
  ): LoaderContext {
29
- // For recursive calls, we already have a context
29
+ // For recursive calls, preserve the context while applying this loader's options
30
30
  // TODO - add any additional loaders to context?
31
31
  if (parentContext) {
32
- return parentContext;
32
+ return {
33
+ ...parentContext,
34
+ coreApi: parentContext.coreApi || context.coreApi || coreApi,
35
+ _parse: parentContext._parse || context._parse,
36
+ fetch: getFetchFunction(options, parentContext)
37
+ };
33
38
  }
34
39
 
35
40
  const newContext: LoaderContext = {
@@ -7,6 +7,7 @@ import {isBrowser} from '@loaders.gl/loader-utils';
7
7
  import {ConsoleLog} from './loggers';
8
8
 
9
9
  export const DEFAULT_LOADER_OPTIONS: LoaderOptions = {
10
+ searchParams: undefined,
10
11
  core: {
11
12
  baseUrl: undefined,
12
13
  // baseUrl
@@ -18,9 +19,11 @@ export const DEFAULT_LOADER_OPTIONS: LoaderOptions = {
18
19
  shape: undefined,
19
20
  log: new ConsoleLog(), // A probe.gl compatible (`log.log()()` syntax) that just logs to console
20
21
  useLocalLibraries: false,
22
+ subloaders: undefined,
21
23
 
22
24
  CDN: 'https://unpkg.com/@loaders.gl',
23
25
  worker: true, // By default, use worker if provided by loader.
26
+ workerThreshold: 0.1,
24
27
  maxConcurrency: 3, // How many worker instances should be created for each loader.
25
28
  maxMobileConcurrency: 1, // How many worker instances should be created for each loader on mobile devices.
26
29
  reuseWorkers: isBrowser, // By default reuse workers in browser (Node.js refuses to terminate if browsers are running)
@@ -26,6 +26,7 @@ const CORE_LOADER_OPTION_KEYS = [
26
26
  'useLocalLibraries',
27
27
  'CDN',
28
28
  'worker',
29
+ 'workerThreshold',
29
30
  'maxConcurrency',
30
31
  'maxMobileConcurrency',
31
32
  'reuseWorkers',
@@ -41,6 +42,40 @@ const CORE_LOADER_OPTION_KEYS = [
41
42
  'transforms'
42
43
  ] as const;
43
44
 
45
+ /**
46
+ * Appends loader search parameters to a URL while preserving existing parameters and fragments.
47
+ * @param url URL to update
48
+ * @param options Loader options containing optional search parameters
49
+ * @returns URL with the configured parameters appended
50
+ */
51
+ export function applySearchParamsToUrl(url: string, options?: LoaderOptions): string {
52
+ const searchParams = options?.searchParams;
53
+ if (!searchParams || Object.keys(searchParams).length === 0 || /^(?:data|blob):/i.test(url)) {
54
+ return url;
55
+ }
56
+
57
+ const hashIndex = url.indexOf('#');
58
+ const hash = hashIndex >= 0 ? url.slice(hashIndex) : '';
59
+ const urlWithoutHash = hashIndex >= 0 ? url.slice(0, hashIndex) : url;
60
+ const queryIndex = urlWithoutHash.indexOf('?');
61
+ const path = queryIndex >= 0 ? urlWithoutHash.slice(0, queryIndex) : urlWithoutHash;
62
+ const query = queryIndex >= 0 ? urlWithoutHash.slice(queryIndex + 1) : '';
63
+ const existingSearchParams = new URLSearchParams(query);
64
+ const appendedSearchParams = new URLSearchParams();
65
+ for (const [key, value] of Object.entries(searchParams)) {
66
+ if (!existingSearchParams.has(key)) {
67
+ appendedSearchParams.set(key, String(value));
68
+ }
69
+ }
70
+
71
+ const serializedAppendedSearchParams = appendedSearchParams.toString();
72
+ return `${path}${queryIndex >= 0 ? `?${query}` : ''}${
73
+ serializedAppendedSearchParams
74
+ ? `${queryIndex >= 0 ? '&' : '?'}${serializedAppendedSearchParams}`
75
+ : ''
76
+ }${hash}`;
77
+ }
78
+
44
79
  /**
45
80
  * Global state for loaders.gl. Stored on `globalThis.loaders._state`
46
81
  */
@@ -111,7 +146,10 @@ export function normalizeOptions(
111
146
  loaders = Array.isArray(loaders) ? loaders : [loaders];
112
147
 
113
148
  validateOptions(options, loaders);
114
- return normalizeLoaderOptions(normalizeOptionsInternal(loader, options, url));
149
+ validateWorkerThreshold(options);
150
+ const normalizedOptions = normalizeLoaderOptions(normalizeOptionsInternal(loader, options, url));
151
+ validateWorkerThreshold(normalizedOptions);
152
+ return normalizedOptions;
115
153
  }
116
154
 
117
155
  /**
@@ -159,6 +197,14 @@ function validateOptions(options: LoaderOptions, loaders: Loader[]): void {
159
197
  }
160
198
  }
161
199
 
200
+ /** Validates the automatic worker-selection threshold. */
201
+ function validateWorkerThreshold(options: LoaderOptions): void {
202
+ const threshold = options.core?.workerThreshold;
203
+ if (threshold !== undefined && (!Number.isFinite(threshold) || threshold < 0 || threshold > 1)) {
204
+ throw new Error('core.workerThreshold must be a finite number between 0 and 1');
205
+ }
206
+ }
207
+
162
208
  // eslint-disable-next-line max-params, complexity
163
209
  function validateOptionsObject(
164
210
  options: LoaderOptions,
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import {isResponse} from '@loaders.gl/loader-utils';
5
+ import {encodeArrayBufferToBase64, isResponse} from '@loaders.gl/loader-utils';
6
6
  import {FetchError} from '../fetch/fetch-error';
7
7
  import {getResourceContentLength, getResourceUrl, getResourceMIMEType} from './resource-utils';
8
8
  import {shortenUrlForDisplay} from './url-utils';
@@ -120,18 +120,8 @@ async function getInitialDataUrl(
120
120
  }
121
121
  if (resource instanceof ArrayBuffer) {
122
122
  const slice = resource.slice(0, INITIAL_DATA_LENGTH);
123
- const base64 = arrayBufferToBase64(slice);
123
+ const base64 = encodeArrayBufferToBase64(slice);
124
124
  return `data:base64,${base64}`;
125
125
  }
126
126
  return null;
127
127
  }
128
-
129
- // https://stackoverflow.com/questions/9267899/arraybuffer-to-base64-encoded-string
130
- function arrayBufferToBase64(buffer: ArrayBuffer): string {
131
- let binary = '';
132
- const bytes = new Uint8Array(buffer);
133
- for (let i = 0; i < bytes.byteLength; i++) {
134
- binary += String.fromCharCode(bytes[i]);
135
- }
136
- return btoa(binary);
137
- }