@loaders.gl/core 4.2.0-alpha.3 → 4.2.0-alpha.5

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 (110) hide show
  1. package/dist/core-addons/write-file-browser.js +62 -1
  2. package/dist/dist.dev.js +366 -332
  3. package/dist/dist.min.js +15 -0
  4. package/dist/index.cjs +55 -68
  5. package/dist/index.cjs.map +7 -0
  6. package/dist/index.d.ts +20 -20
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +18 -1
  9. package/dist/iterators/batch-iterators/timed-batch-iterator.js +18 -13
  10. package/dist/iterators/make-iterator/make-array-buffer-iterator.d.ts +1 -1
  11. package/dist/iterators/make-iterator/make-array-buffer-iterator.d.ts.map +1 -1
  12. package/dist/iterators/make-iterator/make-array-buffer-iterator.js +21 -15
  13. package/dist/iterators/make-iterator/make-blob-iterator.d.ts +1 -1
  14. package/dist/iterators/make-iterator/make-blob-iterator.d.ts.map +1 -1
  15. package/dist/iterators/make-iterator/make-blob-iterator.js +18 -10
  16. package/dist/iterators/make-iterator/make-iterator.d.ts +1 -1
  17. package/dist/iterators/make-iterator/make-iterator.d.ts.map +1 -1
  18. package/dist/iterators/make-iterator/make-iterator.js +29 -18
  19. package/dist/iterators/make-iterator/make-stream-iterator.js +86 -23
  20. package/dist/iterators/make-iterator/make-string-iterator.d.ts +1 -1
  21. package/dist/iterators/make-iterator/make-string-iterator.d.ts.map +1 -1
  22. package/dist/iterators/make-iterator/make-string-iterator.js +20 -10
  23. package/dist/iterators/make-stream/make-stream.js +47 -29
  24. package/dist/javascript-utils/is-type.js +25 -19
  25. package/dist/lib/api/encode-table.js +40 -35
  26. package/dist/lib/api/encode.js +112 -73
  27. package/dist/lib/api/load-in-batches.js +35 -21
  28. package/dist/lib/api/load.js +35 -20
  29. package/dist/lib/api/loader-options.d.ts +2 -2
  30. package/dist/lib/api/loader-options.d.ts.map +1 -1
  31. package/dist/lib/api/loader-options.js +3 -1
  32. package/dist/lib/api/parse-in-batches.js +105 -69
  33. package/dist/lib/api/parse-sync.js +42 -33
  34. package/dist/lib/api/parse.js +73 -61
  35. package/dist/lib/api/register-loaders.js +23 -14
  36. package/dist/lib/api/select-loader.js +216 -163
  37. package/dist/lib/common.js +3 -1
  38. package/dist/lib/fetch/fetch-error-message.js +17 -12
  39. package/dist/lib/fetch/fetch-file.js +26 -15
  40. package/dist/lib/fetch/read-array-buffer.js +30 -15
  41. package/dist/lib/filesystems/browser-filesystem.js +126 -69
  42. package/dist/lib/filesystems/read-array-buffer.js +14 -6
  43. package/dist/lib/init.js +13 -7
  44. package/dist/lib/loader-utils/check-errors.js +37 -16
  45. package/dist/lib/loader-utils/get-data.js +110 -88
  46. package/dist/lib/loader-utils/get-fetch-function.js +24 -13
  47. package/dist/lib/loader-utils/loader-context.js +50 -31
  48. package/dist/lib/loader-utils/loggers.js +34 -44
  49. package/dist/lib/loader-utils/normalize-loader.js +45 -32
  50. package/dist/lib/loader-utils/option-defaults.js +38 -34
  51. package/dist/lib/loader-utils/option-utils.d.ts.map +1 -1
  52. package/dist/lib/loader-utils/option-utils.js +133 -80
  53. package/dist/lib/progress/fetch-progress.js +54 -47
  54. package/dist/lib/utils/log.js +4 -4
  55. package/dist/lib/utils/mime-type-utils.js +34 -11
  56. package/dist/lib/utils/resource-utils.js +77 -45
  57. package/dist/lib/utils/response-utils.js +97 -74
  58. package/dist/lib/utils/url-utils.js +6 -4
  59. package/dist/null-loader.js +44 -30
  60. package/dist/null-worker-node.js +1 -1
  61. package/dist/null-worker.js +1 -1
  62. package/dist/workers/null-worker-node.js +3 -1
  63. package/dist/workers/null-worker.js +3 -1
  64. package/package.json +7 -7
  65. package/src/lib/loader-utils/option-utils.ts +3 -1
  66. package/dist/core-addons/README.md +0 -1
  67. package/dist/core-addons/write-file-browser.js.map +0 -1
  68. package/dist/index.js.map +0 -1
  69. package/dist/iterators/batch-iterators/timed-batch-iterator.js.map +0 -1
  70. package/dist/iterators/make-iterator/make-array-buffer-iterator.js.map +0 -1
  71. package/dist/iterators/make-iterator/make-blob-iterator.js.map +0 -1
  72. package/dist/iterators/make-iterator/make-iterator.js.map +0 -1
  73. package/dist/iterators/make-iterator/make-stream-iterator.js.map +0 -1
  74. package/dist/iterators/make-iterator/make-string-iterator.js.map +0 -1
  75. package/dist/iterators/make-stream/make-stream.js.map +0 -1
  76. package/dist/javascript-utils/is-type.js.map +0 -1
  77. package/dist/lib/api/encode-table.js.map +0 -1
  78. package/dist/lib/api/encode.js.map +0 -1
  79. package/dist/lib/api/load-in-batches.js.map +0 -1
  80. package/dist/lib/api/load.js.map +0 -1
  81. package/dist/lib/api/loader-options.js.map +0 -1
  82. package/dist/lib/api/parse-in-batches.js.map +0 -1
  83. package/dist/lib/api/parse-sync.js.map +0 -1
  84. package/dist/lib/api/parse.js.map +0 -1
  85. package/dist/lib/api/register-loaders.js.map +0 -1
  86. package/dist/lib/api/select-loader.js.map +0 -1
  87. package/dist/lib/common.js.map +0 -1
  88. package/dist/lib/fetch/fetch-error-message.js.map +0 -1
  89. package/dist/lib/fetch/fetch-file.js.map +0 -1
  90. package/dist/lib/fetch/read-array-buffer.js.map +0 -1
  91. package/dist/lib/filesystems/browser-filesystem.js.map +0 -1
  92. package/dist/lib/filesystems/read-array-buffer.js.map +0 -1
  93. package/dist/lib/init.js.map +0 -1
  94. package/dist/lib/loader-utils/check-errors.js.map +0 -1
  95. package/dist/lib/loader-utils/get-data.js.map +0 -1
  96. package/dist/lib/loader-utils/get-fetch-function.js.map +0 -1
  97. package/dist/lib/loader-utils/loader-context.js.map +0 -1
  98. package/dist/lib/loader-utils/loggers.js.map +0 -1
  99. package/dist/lib/loader-utils/normalize-loader.js.map +0 -1
  100. package/dist/lib/loader-utils/option-defaults.js.map +0 -1
  101. package/dist/lib/loader-utils/option-utils.js.map +0 -1
  102. package/dist/lib/progress/fetch-progress.js.map +0 -1
  103. package/dist/lib/utils/log.js.map +0 -1
  104. package/dist/lib/utils/mime-type-utils.js.map +0 -1
  105. package/dist/lib/utils/resource-utils.js.map +0 -1
  106. package/dist/lib/utils/response-utils.js.map +0 -1
  107. package/dist/lib/utils/url-utils.js.map +0 -1
  108. package/dist/null-loader.js.map +0 -1
  109. package/dist/workers/null-worker-node.js.map +0 -1
  110. package/dist/workers/null-worker.js.map +0 -1
@@ -1,38 +1,57 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { getFetchFunction } from "./get-fetch-function.js";
2
5
  import { extractQueryString, stripQueryString } from "../utils/url-utils.js";
3
6
  import { path } from '@loaders.gl/loader-utils';
7
+ /**
8
+ * "sub" loaders invoked by other loaders get a "context" injected on `this`
9
+ * The context will inject core methods like `parse` and contain information
10
+ * about loaders and options passed in to the top-level `parse` call.
11
+ *
12
+ * @param context
13
+ * @param options
14
+ * @param previousContext
15
+ */
4
16
  export function getLoaderContext(context, options, parentContext) {
5
- if (parentContext) {
6
- return parentContext;
7
- }
8
- const newContext = {
9
- fetch: getFetchFunction(options, context),
10
- ...context
11
- };
12
- if (newContext.url) {
13
- const baseUrl = stripQueryString(newContext.url);
14
- newContext.baseUrl = baseUrl;
15
- newContext.queryString = extractQueryString(newContext.url);
16
- newContext.filename = path.filename(baseUrl);
17
- newContext.baseUrl = path.dirname(baseUrl);
18
- }
19
- if (!Array.isArray(newContext.loaders)) {
20
- newContext.loaders = null;
21
- }
22
- return newContext;
17
+ // For recursive calls, we already have a context
18
+ // TODO - add any additional loaders to context?
19
+ if (parentContext) {
20
+ return parentContext;
21
+ }
22
+ const newContext = {
23
+ fetch: getFetchFunction(options, context),
24
+ ...context
25
+ };
26
+ // Parse URLs so that subloaders can easily generate correct strings
27
+ if (newContext.url) {
28
+ const baseUrl = stripQueryString(newContext.url);
29
+ newContext.baseUrl = baseUrl;
30
+ newContext.queryString = extractQueryString(newContext.url);
31
+ newContext.filename = path.filename(baseUrl);
32
+ newContext.baseUrl = path.dirname(baseUrl);
33
+ }
34
+ // Recursive loading does not use single loader
35
+ if (!Array.isArray(newContext.loaders)) {
36
+ newContext.loaders = null;
37
+ }
38
+ return newContext;
23
39
  }
40
+ // eslint-disable-next-line complexity
24
41
  export function getLoadersFromContext(loaders, context) {
25
- if (loaders && !Array.isArray(loaders)) {
26
- return loaders;
27
- }
28
- let candidateLoaders;
29
- if (loaders) {
30
- candidateLoaders = Array.isArray(loaders) ? loaders : [loaders];
31
- }
32
- if (context && context.loaders) {
33
- const contextLoaders = Array.isArray(context.loaders) ? context.loaders : [context.loaders];
34
- candidateLoaders = candidateLoaders ? [...candidateLoaders, ...contextLoaders] : contextLoaders;
35
- }
36
- return candidateLoaders && candidateLoaders.length ? candidateLoaders : undefined;
42
+ // A single loader (non-array) indicates no selection desired. Force select.
43
+ if (loaders && !Array.isArray(loaders)) {
44
+ return loaders;
45
+ }
46
+ // Create a merged list
47
+ let candidateLoaders;
48
+ if (loaders) {
49
+ candidateLoaders = Array.isArray(loaders) ? loaders : [loaders];
50
+ }
51
+ if (context && context.loaders) {
52
+ const contextLoaders = Array.isArray(context.loaders) ? context.loaders : [context.loaders];
53
+ candidateLoaders = candidateLoaders ? [...candidateLoaders, ...contextLoaders] : contextLoaders;
54
+ }
55
+ // If no loaders, return null to look in globally registered loaders
56
+ return candidateLoaders && candidateLoaders.length ? candidateLoaders : undefined;
37
57
  }
38
- //# sourceMappingURL=loader-context.js.map
@@ -1,49 +1,39 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // probe.gl Log compatible loggers
1
5
  import { Log } from '@probe.gl/log';
2
- export const probeLog = new Log({
3
- id: 'loaders.gl'
4
- });
6
+ export const probeLog = new Log({ id: 'loaders.gl' });
7
+ // Logs nothing
5
8
  export class NullLog {
6
- log() {
7
- return () => {};
8
- }
9
- info() {
10
- return () => {};
11
- }
12
- warn() {
13
- return () => {};
14
- }
15
- error() {
16
- return () => {};
17
- }
9
+ log() {
10
+ return () => { };
11
+ }
12
+ info() {
13
+ return () => { };
14
+ }
15
+ warn() {
16
+ return () => { };
17
+ }
18
+ error() {
19
+ return () => { };
20
+ }
18
21
  }
22
+ // Logs to console
19
23
  export class ConsoleLog {
20
- constructor() {
21
- this.console = void 0;
22
- this.console = console;
23
- }
24
- log() {
25
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
26
- args[_key] = arguments[_key];
27
- }
28
- return this.console.log.bind(this.console, ...args);
29
- }
30
- info() {
31
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
32
- args[_key2] = arguments[_key2];
33
- }
34
- return this.console.info.bind(this.console, ...args);
35
- }
36
- warn() {
37
- for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
38
- args[_key3] = arguments[_key3];
39
- }
40
- return this.console.warn.bind(this.console, ...args);
41
- }
42
- error() {
43
- for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
44
- args[_key4] = arguments[_key4];
45
- }
46
- return this.console.error.bind(this.console, ...args);
47
- }
24
+ constructor() {
25
+ this.console = console; // eslint-disable-line
26
+ }
27
+ log(...args) {
28
+ return this.console.log.bind(this.console, ...args);
29
+ }
30
+ info(...args) {
31
+ return this.console.info.bind(this.console, ...args);
32
+ }
33
+ warn(...args) {
34
+ return this.console.warn.bind(this.console, ...args);
35
+ }
36
+ error(...args) {
37
+ return this.console.error.bind(this.console, ...args);
38
+ }
48
39
  }
49
- //# sourceMappingURL=loggers.js.map
@@ -1,37 +1,50 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { assert } from '@loaders.gl/loader-utils';
2
5
  export function isLoaderObject(loader) {
3
- var _loader;
4
- if (!loader) {
5
- return false;
6
- }
7
- if (Array.isArray(loader)) {
8
- loader = loader[0];
9
- }
10
- const hasExtensions = Array.isArray((_loader = loader) === null || _loader === void 0 ? void 0 : _loader.extensions);
11
- return hasExtensions;
6
+ if (!loader) {
7
+ return false;
8
+ }
9
+ if (Array.isArray(loader)) {
10
+ loader = loader[0];
11
+ }
12
+ const hasExtensions = Array.isArray(loader?.extensions);
13
+ /* Now handled by types and worker loaders do not have these
14
+ let hasParser =
15
+ loader.parseTextSync ||
16
+ loader.parseSync ||
17
+ loader.parse ||
18
+ loader.parseStream || // TODO Remove, Replace with parseInBatches
19
+ loader.parseInBatches;
20
+ */
21
+ return hasExtensions;
12
22
  }
13
23
  export function normalizeLoader(loader) {
14
- var _loader2, _loader3;
15
- assert(loader, 'null loader');
16
- assert(isLoaderObject(loader), 'invalid loader');
17
- let options;
18
- if (Array.isArray(loader)) {
19
- options = loader[1];
20
- loader = loader[0];
21
- loader = {
22
- ...loader,
23
- options: {
24
- ...loader.options,
25
- ...options
26
- }
27
- };
28
- }
29
- if ((_loader2 = loader) !== null && _loader2 !== void 0 && _loader2.parseTextSync || (_loader3 = loader) !== null && _loader3 !== void 0 && _loader3.parseText) {
30
- loader.text = true;
31
- }
32
- if (!loader.text) {
33
- loader.binary = true;
34
- }
35
- return loader;
24
+ // This error is fairly easy to trigger by mixing up import statements etc
25
+ // So we make an exception and add a developer error message for this case
26
+ // To help new users from getting stuck here
27
+ assert(loader, 'null loader');
28
+ assert(isLoaderObject(loader), 'invalid loader');
29
+ // NORMALIZE [LOADER, OPTIONS] => LOADER
30
+ // If [loader, options], create a new loaders object with options merged in
31
+ let options;
32
+ if (Array.isArray(loader)) {
33
+ options = loader[1];
34
+ loader = loader[0];
35
+ loader = {
36
+ ...loader,
37
+ options: { ...loader.options, ...options }
38
+ };
39
+ }
40
+ // NORMALIZE text and binary flags
41
+ // Ensure at least one of text/binary flags are properly set
42
+ // @ts-expect-error
43
+ if (loader?.parseTextSync || loader?.parseText) {
44
+ loader.text = true;
45
+ }
46
+ if (!loader.text) {
47
+ loader.binary = true;
48
+ }
49
+ return loader;
36
50
  }
37
- //# sourceMappingURL=normalize-loader.js.map
@@ -1,40 +1,44 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { isBrowser } from '@loaders.gl/loader-utils';
2
5
  import { ConsoleLog } from "./loggers.js";
3
6
  export const DEFAULT_LOADER_OPTIONS = {
4
- fetch: null,
5
- mimeType: undefined,
6
- nothrow: false,
7
- log: new ConsoleLog(),
8
- useLocalLibraries: false,
9
- CDN: 'https://unpkg.com/@loaders.gl',
10
- worker: true,
11
- maxConcurrency: 3,
12
- maxMobileConcurrency: 1,
13
- reuseWorkers: isBrowser,
14
- _nodeWorkers: false,
15
- _workerType: '',
16
- limit: 0,
17
- _limitMB: 0,
18
- batchSize: 'auto',
19
- batchDebounceMs: 0,
20
- metadata: false,
21
- transforms: []
7
+ // baseUri
8
+ fetch: null,
9
+ mimeType: undefined,
10
+ nothrow: false,
11
+ log: new ConsoleLog(), // A probe.gl compatible (`log.log()()` syntax) that just logs to console
12
+ useLocalLibraries: false,
13
+ CDN: 'https://unpkg.com/@loaders.gl',
14
+ worker: true, // By default, use worker if provided by loader.
15
+ maxConcurrency: 3, // How many worker instances should be created for each loader.
16
+ maxMobileConcurrency: 1, // How many worker instances should be created for each loader on mobile devices.
17
+ reuseWorkers: isBrowser, // By default reuse workers in browser (Node.js refuses to terminate if browsers are running)
18
+ _nodeWorkers: false, // By default do not support node workers
19
+ _workerType: '', // 'test' to use locally generated workers
20
+ limit: 0,
21
+ _limitMB: 0,
22
+ batchSize: 'auto',
23
+ batchDebounceMs: 0,
24
+ metadata: false, // TODO - currently only implemented for parseInBatches, adds initial metadata batch,
25
+ transforms: []
22
26
  };
23
27
  export const REMOVED_LOADER_OPTIONS = {
24
- throws: 'nothrow',
25
- dataType: '(no longer used)',
26
- uri: 'baseUri',
27
- method: 'fetch.method',
28
- headers: 'fetch.headers',
29
- body: 'fetch.body',
30
- mode: 'fetch.mode',
31
- credentials: 'fetch.credentials',
32
- cache: 'fetch.cache',
33
- redirect: 'fetch.redirect',
34
- referrer: 'fetch.referrer',
35
- referrerPolicy: 'fetch.referrerPolicy',
36
- integrity: 'fetch.integrity',
37
- keepalive: 'fetch.keepalive',
38
- signal: 'fetch.signal'
28
+ throws: 'nothrow',
29
+ dataType: '(no longer used)',
30
+ uri: 'baseUri',
31
+ // Warn if fetch options are used on top-level
32
+ method: 'fetch.method',
33
+ headers: 'fetch.headers',
34
+ body: 'fetch.body',
35
+ mode: 'fetch.mode',
36
+ credentials: 'fetch.credentials',
37
+ cache: 'fetch.cache',
38
+ redirect: 'fetch.redirect',
39
+ referrer: 'fetch.referrer',
40
+ referrerPolicy: 'fetch.referrerPolicy',
41
+ integrity: 'fetch.integrity',
42
+ keepalive: 'fetch.keepalive',
43
+ signal: 'fetch.signal'
39
44
  };
40
- //# sourceMappingURL=option-defaults.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"option-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/loader-utils/option-utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,MAAM,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAKpE;;GAEG;AACH,KAAK,iBAAiB,GAAG;IACvB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,aAAa,EAAE,aAAa,CAAC;CAC9B,CAAC;AAEF;;;GAGG;AACH,wBAAgB,oBAAoB,IAAI,iBAAiB,CASxD;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,IAAI,aAAa,CAKtD;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI,CAK7D;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,aAAa,EACtB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,MAAM,EAAE,EAClB,GAAG,CAAC,EAAE,MAAM,GACX,aAAa,CAMf"}
1
+ {"version":3,"file":"option-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/loader-utils/option-utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,MAAM,EAAE,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAKpE;;GAEG;AACH,KAAK,iBAAiB,GAAG;IACvB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,aAAa,EAAE,aAAa,CAAC;CAC9B,CAAC;AAEF;;;GAGG;AACH,wBAAgB,oBAAoB,IAAI,iBAAiB,CAWxD;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,IAAI,aAAa,CAKtD;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI,CAK7D;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,aAAa,EACtB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,MAAM,EAAE,EAClB,GAAG,CAAC,EAAE,MAAM,GACX,aAAa,CAMf"}
@@ -1,106 +1,159 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { isPureObject, isObject } from "../../javascript-utils/is-type.js";
2
5
  import { probeLog, NullLog } from "./loggers.js";
3
6
  import { DEFAULT_LOADER_OPTIONS, REMOVED_LOADER_OPTIONS } from "./option-defaults.js";
7
+ /**
8
+ * Helper for safely accessing global loaders.gl variables
9
+ * Wraps initialization of global variable in function to defeat overly aggressive tree-shakers
10
+ */
4
11
  export function getGlobalLoaderState() {
5
- globalThis.loaders = globalThis.loaders || {};
6
- const {
7
- loaders
8
- } = globalThis;
9
- loaders._state = loaders._state || {};
10
- return loaders._state;
12
+ // @ts-ignore
13
+ globalThis.loaders = globalThis.loaders || {};
14
+ // @ts-ignore
15
+ const { loaders } = globalThis;
16
+ // Add _state object to keep separate from modules added to globalThis.loaders
17
+ if (!loaders._state) {
18
+ loaders._state = {};
19
+ }
20
+ return loaders._state;
11
21
  }
22
+ /**
23
+ * Store global loader options on the global object to increase chances of cross loaders-version interoperability
24
+ * NOTE: This use case is not reliable but can help when testing new versions of loaders.gl with existing frameworks
25
+ * @returns global loader options merged with default loader options
26
+ */
12
27
  export function getGlobalLoaderOptions() {
13
- const state = getGlobalLoaderState();
14
- state.globalOptions = state.globalOptions || {
15
- ...DEFAULT_LOADER_OPTIONS
16
- };
17
- return state.globalOptions;
28
+ const state = getGlobalLoaderState();
29
+ // Ensure all default loader options from this library are mentioned
30
+ state.globalOptions = state.globalOptions || { ...DEFAULT_LOADER_OPTIONS };
31
+ return state.globalOptions;
18
32
  }
33
+ /**
34
+ * Set global loader options
35
+ * @param options
36
+ */
19
37
  export function setGlobalOptions(options) {
20
- const state = getGlobalLoaderState();
21
- const globalOptions = getGlobalLoaderOptions();
22
- state.globalOptions = normalizeOptionsInternal(globalOptions, options);
38
+ const state = getGlobalLoaderState();
39
+ const globalOptions = getGlobalLoaderOptions();
40
+ // @ts-expect-error First param looks incorrect
41
+ state.globalOptions = normalizeOptionsInternal(globalOptions, options);
23
42
  }
43
+ /**
44
+ * Merges options with global opts and loader defaults, also injects baseUri
45
+ * @param options
46
+ * @param loader
47
+ * @param loaders
48
+ * @param url
49
+ */
24
50
  export function normalizeOptions(options, loader, loaders, url) {
25
- loaders = loaders || [];
26
- loaders = Array.isArray(loaders) ? loaders : [loaders];
27
- validateOptions(options, loaders);
28
- return normalizeOptionsInternal(loader, options, url);
51
+ loaders = loaders || [];
52
+ loaders = Array.isArray(loaders) ? loaders : [loaders];
53
+ validateOptions(options, loaders);
54
+ return normalizeOptionsInternal(loader, options, url);
29
55
  }
56
+ // VALIDATE OPTIONS
57
+ /**
58
+ * Warn for unsupported options
59
+ * @param options
60
+ * @param loaders
61
+ */
30
62
  function validateOptions(options, loaders) {
31
- validateOptionsObject(options, null, DEFAULT_LOADER_OPTIONS, REMOVED_LOADER_OPTIONS, loaders);
32
- for (const loader of loaders) {
33
- const idOptions = options && options[loader.id] || {};
34
- const loaderOptions = loader.options && loader.options[loader.id] || {};
35
- const deprecatedOptions = loader.deprecatedOptions && loader.deprecatedOptions[loader.id] || {};
36
- validateOptionsObject(idOptions, loader.id, loaderOptions, deprecatedOptions, loaders);
37
- }
63
+ // Check top level options
64
+ validateOptionsObject(options, null, DEFAULT_LOADER_OPTIONS, REMOVED_LOADER_OPTIONS, loaders);
65
+ for (const loader of loaders) {
66
+ // Get the scoped, loader specific options from the user supplied options
67
+ const idOptions = ((options && options[loader.id]) || {});
68
+ // Get scoped, loader specific default and deprecated options from the selected loader
69
+ const loaderOptions = (loader.options && loader.options[loader.id]) || {};
70
+ const deprecatedOptions = (loader.deprecatedOptions && loader.deprecatedOptions[loader.id]) || {};
71
+ // Validate loader specific options
72
+ // @ts-ignore
73
+ validateOptionsObject(idOptions, loader.id, loaderOptions, deprecatedOptions, loaders);
74
+ }
38
75
  }
76
+ // eslint-disable-next-line max-params, complexity
39
77
  function validateOptionsObject(options, id, defaultOptions, deprecatedOptions, loaders) {
40
- const loaderName = id || 'Top level';
41
- const prefix = id ? `${id}.` : '';
42
- for (const key in options) {
43
- const isSubOptions = !id && isObject(options[key]);
44
- const isBaseUriOption = key === 'baseUri' && !id;
45
- const isWorkerUrlOption = key === 'workerUrl' && id;
46
- if (!(key in defaultOptions) && !isBaseUriOption && !isWorkerUrlOption) {
47
- if (key in deprecatedOptions) {
48
- probeLog.warn(`${loaderName} loader option \'${prefix}${key}\' no longer supported, use \'${deprecatedOptions[key]}\'`)();
49
- } else if (!isSubOptions) {
50
- const suggestion = findSimilarOption(key, loaders);
51
- probeLog.warn(`${loaderName} loader option \'${prefix}${key}\' not recognized. ${suggestion}`)();
52
- }
78
+ const loaderName = id || 'Top level';
79
+ const prefix = id ? `${id}.` : '';
80
+ for (const key in options) {
81
+ // If top level option value is an object it could options for a loader, so ignore
82
+ const isSubOptions = !id && isObject(options[key]);
83
+ const isBaseUriOption = key === 'baseUri' && !id;
84
+ const isWorkerUrlOption = key === 'workerUrl' && id;
85
+ // <loader>.workerUrl requires special handling as it is now auto-generated and no longer specified as a default option.
86
+ if (!(key in defaultOptions) && !isBaseUriOption && !isWorkerUrlOption) {
87
+ // Issue deprecation warnings
88
+ if (key in deprecatedOptions) {
89
+ probeLog.warn(`${loaderName} loader option \'${prefix}${key}\' no longer supported, use \'${deprecatedOptions[key]}\'`)();
90
+ }
91
+ else if (!isSubOptions) {
92
+ const suggestion = findSimilarOption(key, loaders);
93
+ probeLog.warn(`${loaderName} loader option \'${prefix}${key}\' not recognized. ${suggestion}`)();
94
+ }
95
+ }
53
96
  }
54
- }
55
97
  }
56
98
  function findSimilarOption(optionKey, loaders) {
57
- const lowerCaseOptionKey = optionKey.toLowerCase();
58
- let bestSuggestion = '';
59
- for (const loader of loaders) {
60
- for (const key in loader.options) {
61
- if (optionKey === key) {
62
- return `Did you mean \'${loader.id}.${key}\'?`;
63
- }
64
- const lowerCaseKey = key.toLowerCase();
65
- const isPartialMatch = lowerCaseOptionKey.startsWith(lowerCaseKey) || lowerCaseKey.startsWith(lowerCaseOptionKey);
66
- if (isPartialMatch) {
67
- bestSuggestion = bestSuggestion || `Did you mean \'${loader.id}.${key}\'?`;
68
- }
99
+ const lowerCaseOptionKey = optionKey.toLowerCase();
100
+ let bestSuggestion = '';
101
+ for (const loader of loaders) {
102
+ for (const key in loader.options) {
103
+ if (optionKey === key) {
104
+ return `Did you mean \'${loader.id}.${key}\'?`;
105
+ }
106
+ const lowerCaseKey = key.toLowerCase();
107
+ const isPartialMatch = lowerCaseOptionKey.startsWith(lowerCaseKey) || lowerCaseKey.startsWith(lowerCaseOptionKey);
108
+ if (isPartialMatch) {
109
+ bestSuggestion = bestSuggestion || `Did you mean \'${loader.id}.${key}\'?`;
110
+ }
111
+ }
69
112
  }
70
- }
71
- return bestSuggestion;
113
+ return bestSuggestion;
72
114
  }
73
115
  function normalizeOptionsInternal(loader, options, url) {
74
- const loaderDefaultOptions = loader.options || {};
75
- const mergedOptions = {
76
- ...loaderDefaultOptions
77
- };
78
- addUrlOptions(mergedOptions, url);
79
- if (mergedOptions.log === null) {
80
- mergedOptions.log = new NullLog();
81
- }
82
- mergeNestedFields(mergedOptions, getGlobalLoaderOptions());
83
- mergeNestedFields(mergedOptions, options);
84
- return mergedOptions;
116
+ const loaderDefaultOptions = loader.options || {};
117
+ const mergedOptions = { ...loaderDefaultOptions };
118
+ addUrlOptions(mergedOptions, url);
119
+ // LOGGING: options.log can be set to `null` to defeat logging
120
+ if (mergedOptions.log === null) {
121
+ mergedOptions.log = new NullLog();
122
+ }
123
+ mergeNestedFields(mergedOptions, getGlobalLoaderOptions());
124
+ mergeNestedFields(mergedOptions, options);
125
+ return mergedOptions;
85
126
  }
127
+ // Merge nested options objects
86
128
  function mergeNestedFields(mergedOptions, options) {
87
- for (const key in options) {
88
- if (key in options) {
89
- const value = options[key];
90
- if (isPureObject(value) && isPureObject(mergedOptions[key])) {
91
- mergedOptions[key] = {
92
- ...mergedOptions[key],
93
- ...options[key]
94
- };
95
- } else {
96
- mergedOptions[key] = options[key];
97
- }
129
+ for (const key in options) {
130
+ // Check for nested options
131
+ // object in options => either no key in defaultOptions or object in defaultOptions
132
+ if (key in options) {
133
+ const value = options[key];
134
+ if (isPureObject(value) && isPureObject(mergedOptions[key])) {
135
+ mergedOptions[key] = {
136
+ ...mergedOptions[key],
137
+ ...options[key]
138
+ };
139
+ }
140
+ else {
141
+ mergedOptions[key] = options[key];
142
+ }
143
+ }
144
+ // else: No need to merge nested opts, and the initial merge already copied over the nested options
98
145
  }
99
- }
100
146
  }
147
+ /**
148
+ * Harvest information from the url
149
+ * @deprecated This is mainly there to support a hack in the GLTFLoader
150
+ * TODO - baseUri should be a directory, i.e. remove file component from baseUri
151
+ * TODO - extract extension?
152
+ * TODO - extract query parameters?
153
+ * TODO - should these be injected on context instead of options?
154
+ */
101
155
  function addUrlOptions(options, url) {
102
- if (url && !('baseUri' in options)) {
103
- options.baseUri = url;
104
- }
156
+ if (url && !('baseUri' in options)) {
157
+ options.baseUri = url;
158
+ }
105
159
  }
106
- //# sourceMappingURL=option-utils.js.map