@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,3 +1,6 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { compareArrayBuffers, path } from '@loaders.gl/loader-utils';
2
5
  import { normalizeLoader } from "../loader-utils/normalize-loader.js";
3
6
  import { log } from "../utils/log.js";
@@ -6,198 +9,248 @@ import { getRegisteredLoaders } from "./register-loaders.js";
6
9
  import { isBlob } from "../../javascript-utils/is-type.js";
7
10
  import { stripQueryString } from "../utils/url-utils.js";
8
11
  const EXT_PATTERN = /\.([^.]+)$/;
9
- export async function selectLoader(data) {
10
- let loaders = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
11
- let options = arguments.length > 2 ? arguments[2] : undefined;
12
- let context = arguments.length > 3 ? arguments[3] : undefined;
13
- if (!validHTTPResponse(data)) {
14
- return null;
15
- }
16
- let loader = selectLoaderSync(data, loaders, {
17
- ...options,
18
- nothrow: true
19
- }, context);
20
- if (loader) {
12
+ // TODO - Need a variant that peeks at streams for parseInBatches
13
+ // TODO - Detect multiple matching loaders? Use heuristics to grade matches?
14
+ // TODO - Allow apps to pass context to disambiguate between multiple matches (e.g. multiple .json formats)?
15
+ /**
16
+ * Find a loader that matches file extension and/or initial file content
17
+ * Search the loaders array argument for a loader that matches url extension or initial data
18
+ * Returns: a normalized loader
19
+ * @param data data to assist
20
+ * @param loaders
21
+ * @param options
22
+ * @param context used internally, applications should not provide this parameter
23
+ */
24
+ export async function selectLoader(data, loaders = [], options, context) {
25
+ if (!validHTTPResponse(data)) {
26
+ return null;
27
+ }
28
+ // First make a sync attempt, disabling exceptions
29
+ let loader = selectLoaderSync(data, loaders, { ...options, nothrow: true }, context);
30
+ if (loader) {
31
+ return loader;
32
+ }
33
+ // For Blobs and Files, try to asynchronously read a small initial slice and test again with that
34
+ // to see if we can detect by initial content
35
+ if (isBlob(data)) {
36
+ data = await data.slice(0, 10).arrayBuffer();
37
+ loader = selectLoaderSync(data, loaders, options, context);
38
+ }
39
+ // no loader available
40
+ if (!loader && !options?.nothrow) {
41
+ throw new Error(getNoValidLoaderMessage(data));
42
+ }
21
43
  return loader;
22
- }
23
- if (isBlob(data)) {
24
- data = await data.slice(0, 10).arrayBuffer();
25
- loader = selectLoaderSync(data, loaders, options, context);
26
- }
27
- if (!loader && !(options !== null && options !== void 0 && options.nothrow)) {
28
- throw new Error(getNoValidLoaderMessage(data));
29
- }
30
- return loader;
31
44
  }
32
- export function selectLoaderSync(data) {
33
- let loaders = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
34
- let options = arguments.length > 2 ? arguments[2] : undefined;
35
- let context = arguments.length > 3 ? arguments[3] : undefined;
36
- if (!validHTTPResponse(data)) {
37
- return null;
38
- }
39
- if (loaders && !Array.isArray(loaders)) {
40
- return normalizeLoader(loaders);
41
- }
42
- let candidateLoaders = [];
43
- if (loaders) {
44
- candidateLoaders = candidateLoaders.concat(loaders);
45
- }
46
- if (!(options !== null && options !== void 0 && options.ignoreRegisteredLoaders)) {
47
- candidateLoaders.push(...getRegisteredLoaders());
48
- }
49
- normalizeLoaders(candidateLoaders);
50
- const loader = selectLoaderInternal(data, candidateLoaders, options, context);
51
- if (!loader && !(options !== null && options !== void 0 && options.nothrow)) {
52
- throw new Error(getNoValidLoaderMessage(data));
53
- }
54
- return loader;
45
+ /**
46
+ * Find a loader that matches file extension and/or initial file content
47
+ * Search the loaders array argument for a loader that matches url extension or initial data
48
+ * Returns: a normalized loader
49
+ * @param data data to assist
50
+ * @param loaders
51
+ * @param options
52
+ * @param context used internally, applications should not provide this parameter
53
+ */
54
+ export function selectLoaderSync(data, loaders = [], options, context) {
55
+ if (!validHTTPResponse(data)) {
56
+ return null;
57
+ }
58
+ // eslint-disable-next-line complexity
59
+ // if only a single loader was provided (not as array), force its use
60
+ // TODO - Should this behavior be kept and documented?
61
+ if (loaders && !Array.isArray(loaders)) {
62
+ // TODO - remove support for legacy loaders
63
+ return normalizeLoader(loaders);
64
+ }
65
+ // Build list of candidate loaders that will be searched in order for a match
66
+ let candidateLoaders = [];
67
+ // First search supplied loaders
68
+ if (loaders) {
69
+ candidateLoaders = candidateLoaders.concat(loaders);
70
+ }
71
+ // Then fall back to registered loaders
72
+ if (!options?.ignoreRegisteredLoaders) {
73
+ candidateLoaders.push(...getRegisteredLoaders());
74
+ }
75
+ // TODO - remove support for legacy loaders
76
+ normalizeLoaders(candidateLoaders);
77
+ const loader = selectLoaderInternal(data, candidateLoaders, options, context);
78
+ // no loader available
79
+ if (!loader && !options?.nothrow) {
80
+ throw new Error(getNoValidLoaderMessage(data));
81
+ }
82
+ return loader;
55
83
  }
84
+ /** Implements loaders selection logic */
85
+ // eslint-disable-next-line complexity
56
86
  function selectLoaderInternal(data, loaders, options, context) {
57
- const url = getResourceUrl(data);
58
- const type = getResourceMIMEType(data);
59
- const testUrl = stripQueryString(url) || (context === null || context === void 0 ? void 0 : context.url);
60
- let loader = null;
61
- let reason = '';
62
- if (options !== null && options !== void 0 && options.mimeType) {
63
- loader = findLoaderByMIMEType(loaders, options === null || options === void 0 ? void 0 : options.mimeType);
64
- reason = `match forced by supplied MIME type ${options === null || options === void 0 ? void 0 : options.mimeType}`;
65
- }
66
- loader = loader || findLoaderByUrl(loaders, testUrl);
67
- reason = reason || (loader ? `matched url ${testUrl}` : '');
68
- loader = loader || findLoaderByMIMEType(loaders, type);
69
- reason = reason || (loader ? `matched MIME type ${type}` : '');
70
- loader = loader || findLoaderByInitialBytes(loaders, data);
71
- reason = reason || (loader ? `matched initial data ${getFirstCharacters(data)}` : '');
72
- if (options !== null && options !== void 0 && options.fallbackMimeType) {
73
- loader = loader || findLoaderByMIMEType(loaders, options === null || options === void 0 ? void 0 : options.fallbackMimeType);
74
- reason = reason || (loader ? `matched fallback MIME type ${type}` : '');
75
- }
76
- if (reason) {
77
- var _loader;
78
- log.log(1, `selectLoader selected ${(_loader = loader) === null || _loader === void 0 ? void 0 : _loader.name}: ${reason}.`);
79
- }
80
- return loader;
87
+ const url = getResourceUrl(data);
88
+ const type = getResourceMIMEType(data);
89
+ const testUrl = stripQueryString(url) || context?.url;
90
+ let loader = null;
91
+ let reason = '';
92
+ // if options.mimeType is supplied, it takes precedence
93
+ if (options?.mimeType) {
94
+ loader = findLoaderByMIMEType(loaders, options?.mimeType);
95
+ reason = `match forced by supplied MIME type ${options?.mimeType}`;
96
+ }
97
+ // Look up loader by url
98
+ loader = loader || findLoaderByUrl(loaders, testUrl);
99
+ reason = reason || (loader ? `matched url ${testUrl}` : '');
100
+ // Look up loader by mime type
101
+ loader = loader || findLoaderByMIMEType(loaders, type);
102
+ reason = reason || (loader ? `matched MIME type ${type}` : '');
103
+ // Look for loader via initial bytes (Note: not always accessible (e.g. Response, stream, async iterator)
104
+ // @ts-ignore Blob | Response
105
+ loader = loader || findLoaderByInitialBytes(loaders, data);
106
+ // @ts-ignore Blob | Response
107
+ reason = reason || (loader ? `matched initial data ${getFirstCharacters(data)}` : '');
108
+ // Look up loader by fallback mime type
109
+ if (options?.fallbackMimeType) {
110
+ loader = loader || findLoaderByMIMEType(loaders, options?.fallbackMimeType);
111
+ reason = reason || (loader ? `matched fallback MIME type ${type}` : '');
112
+ }
113
+ if (reason) {
114
+ log.log(1, `selectLoader selected ${loader?.name}: ${reason}.`);
115
+ }
116
+ return loader;
81
117
  }
118
+ /** Check HTTP Response */
82
119
  function validHTTPResponse(data) {
83
- if (data instanceof Response) {
84
- if (data.status === 204) {
85
- return false;
120
+ // HANDLE HTTP status
121
+ if (data instanceof Response) {
122
+ // 204 - NO CONTENT. This handles cases where e.g. a tile server responds with 204 for a missing tile
123
+ if (data.status === 204) {
124
+ return false;
125
+ }
86
126
  }
87
- }
88
- return true;
127
+ return true;
89
128
  }
129
+ /** Generate a helpful message to help explain why loader selection failed. */
90
130
  function getNoValidLoaderMessage(data) {
91
- const url = getResourceUrl(data);
92
- const type = getResourceMIMEType(data);
93
- let message = 'No valid loader found (';
94
- message += url ? `${path.filename(url)}, ` : 'no url provided, ';
95
- message += `MIME type: ${type ? `"${type}"` : 'not provided'}, `;
96
- const firstCharacters = data ? getFirstCharacters(data) : '';
97
- message += firstCharacters ? ` first bytes: "${firstCharacters}"` : 'first bytes: not available';
98
- message += ')';
99
- return message;
131
+ const url = getResourceUrl(data);
132
+ const type = getResourceMIMEType(data);
133
+ let message = 'No valid loader found (';
134
+ message += url ? `${path.filename(url)}, ` : 'no url provided, ';
135
+ message += `MIME type: ${type ? `"${type}"` : 'not provided'}, `;
136
+ // First characters are only accessible when called on data (string or arrayBuffer).
137
+ // @ts-ignore Blob | Response
138
+ const firstCharacters = data ? getFirstCharacters(data) : '';
139
+ message += firstCharacters ? ` first bytes: "${firstCharacters}"` : 'first bytes: not available';
140
+ message += ')';
141
+ return message;
100
142
  }
101
143
  function normalizeLoaders(loaders) {
102
- for (const loader of loaders) {
103
- normalizeLoader(loader);
104
- }
144
+ for (const loader of loaders) {
145
+ normalizeLoader(loader);
146
+ }
105
147
  }
148
+ // TODO - Would be nice to support http://example.com/file.glb?parameter=1
149
+ // E.g: x = new URL('http://example.com/file.glb?load=1'; x.pathname
106
150
  function findLoaderByUrl(loaders, url) {
107
- const match = url && EXT_PATTERN.exec(url);
108
- const extension = match && match[1];
109
- return extension ? findLoaderByExtension(loaders, extension) : null;
151
+ // Get extension
152
+ const match = url && EXT_PATTERN.exec(url);
153
+ const extension = match && match[1];
154
+ return extension ? findLoaderByExtension(loaders, extension) : null;
110
155
  }
111
156
  function findLoaderByExtension(loaders, extension) {
112
- extension = extension.toLowerCase();
113
- for (const loader of loaders) {
114
- for (const loaderExtension of loader.extensions) {
115
- if (loaderExtension.toLowerCase() === extension) {
116
- return loader;
117
- }
157
+ extension = extension.toLowerCase();
158
+ for (const loader of loaders) {
159
+ for (const loaderExtension of loader.extensions) {
160
+ if (loaderExtension.toLowerCase() === extension) {
161
+ return loader;
162
+ }
163
+ }
118
164
  }
119
- }
120
- return null;
165
+ return null;
121
166
  }
122
167
  function findLoaderByMIMEType(loaders, mimeType) {
123
- for (const loader of loaders) {
124
- if (loader.mimeTypes && loader.mimeTypes.includes(mimeType)) {
125
- return loader;
168
+ for (const loader of loaders) {
169
+ if (loader.mimeTypes && loader.mimeTypes.includes(mimeType)) {
170
+ return loader;
171
+ }
172
+ // Support referring to loaders using the "unregistered tree"
173
+ // https://en.wikipedia.org/wiki/Media_type#Unregistered_tree
174
+ if (mimeType === `application/x.${loader.id}`) {
175
+ return loader;
176
+ }
126
177
  }
127
- if (mimeType === `application/x.${loader.id}`) {
128
- return loader;
129
- }
130
- }
131
- return null;
178
+ return null;
132
179
  }
133
180
  function findLoaderByInitialBytes(loaders, data) {
134
- if (!data) {
135
- return null;
136
- }
137
- for (const loader of loaders) {
138
- if (typeof data === 'string') {
139
- if (testDataAgainstText(data, loader)) {
140
- return loader;
141
- }
142
- } else if (ArrayBuffer.isView(data)) {
143
- if (testDataAgainstBinary(data.buffer, data.byteOffset, loader)) {
144
- return loader;
145
- }
146
- } else if (data instanceof ArrayBuffer) {
147
- const byteOffset = 0;
148
- if (testDataAgainstBinary(data, byteOffset, loader)) {
149
- return loader;
150
- }
181
+ if (!data) {
182
+ return null;
183
+ }
184
+ for (const loader of loaders) {
185
+ if (typeof data === 'string') {
186
+ if (testDataAgainstText(data, loader)) {
187
+ return loader;
188
+ }
189
+ }
190
+ else if (ArrayBuffer.isView(data)) {
191
+ // Typed Arrays can have offsets into underlying buffer
192
+ if (testDataAgainstBinary(data.buffer, data.byteOffset, loader)) {
193
+ return loader;
194
+ }
195
+ }
196
+ else if (data instanceof ArrayBuffer) {
197
+ const byteOffset = 0;
198
+ if (testDataAgainstBinary(data, byteOffset, loader)) {
199
+ return loader;
200
+ }
201
+ }
202
+ // TODO Handle streaming case (requires creating a new AsyncIterator)
151
203
  }
152
- }
153
- return null;
204
+ return null;
154
205
  }
155
206
  function testDataAgainstText(data, loader) {
156
- if (loader.testText) {
157
- return loader.testText(data);
158
- }
159
- const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests];
160
- return tests.some(test => data.startsWith(test));
207
+ if (loader.testText) {
208
+ return loader.testText(data);
209
+ }
210
+ const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests];
211
+ return tests.some((test) => data.startsWith(test));
161
212
  }
162
213
  function testDataAgainstBinary(data, byteOffset, loader) {
163
- const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests];
164
- return tests.some(test => testBinary(data, byteOffset, loader, test));
214
+ const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests];
215
+ return tests.some((test) => testBinary(data, byteOffset, loader, test));
165
216
  }
166
217
  function testBinary(data, byteOffset, loader, test) {
167
- if (test instanceof ArrayBuffer) {
168
- return compareArrayBuffers(test, data, test.byteLength);
169
- }
170
- switch (typeof test) {
171
- case 'function':
172
- return test(data);
173
- case 'string':
174
- const magic = getMagicString(data, byteOffset, test.length);
175
- return test === magic;
176
- default:
177
- return false;
178
- }
218
+ if (test instanceof ArrayBuffer) {
219
+ return compareArrayBuffers(test, data, test.byteLength);
220
+ }
221
+ switch (typeof test) {
222
+ case 'function':
223
+ return test(data);
224
+ case 'string':
225
+ // Magic bytes check: If `test` is a string, check if binary data starts with that strings
226
+ const magic = getMagicString(data, byteOffset, test.length);
227
+ return test === magic;
228
+ default:
229
+ return false;
230
+ }
179
231
  }
180
- function getFirstCharacters(data) {
181
- let length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
182
- if (typeof data === 'string') {
183
- return data.slice(0, length);
184
- } else if (ArrayBuffer.isView(data)) {
185
- return getMagicString(data.buffer, data.byteOffset, length);
186
- } else if (data instanceof ArrayBuffer) {
187
- const byteOffset = 0;
188
- return getMagicString(data, byteOffset, length);
189
- }
190
- return '';
232
+ function getFirstCharacters(data, length = 5) {
233
+ if (typeof data === 'string') {
234
+ return data.slice(0, length);
235
+ }
236
+ else if (ArrayBuffer.isView(data)) {
237
+ // Typed Arrays can have offsets into underlying buffer
238
+ return getMagicString(data.buffer, data.byteOffset, length);
239
+ }
240
+ else if (data instanceof ArrayBuffer) {
241
+ const byteOffset = 0;
242
+ return getMagicString(data, byteOffset, length);
243
+ }
244
+ return '';
191
245
  }
192
246
  function getMagicString(arrayBuffer, byteOffset, length) {
193
- if (arrayBuffer.byteLength < byteOffset + length) {
194
- return '';
195
- }
196
- const dataView = new DataView(arrayBuffer);
197
- let magic = '';
198
- for (let i = 0; i < length; i++) {
199
- magic += String.fromCharCode(dataView.getUint8(byteOffset + i));
200
- }
201
- return magic;
247
+ if (arrayBuffer.byteLength < byteOffset + length) {
248
+ return '';
249
+ }
250
+ const dataView = new DataView(arrayBuffer);
251
+ let magic = '';
252
+ for (let i = 0; i < length; i++) {
253
+ magic += String.fromCharCode(dataView.getUint8(byteOffset + i));
254
+ }
255
+ return magic;
202
256
  }
203
- //# sourceMappingURL=select-loader.js.map
@@ -1,2 +1,4 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  export {};
2
- //# sourceMappingURL=common.js.map
@@ -1,18 +1,23 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  export function getErrorMessageFromResponseSync(response) {
2
- return `Failed to fetch resource ${response.url}(${response.status}): ${response.statusText} `;
5
+ return `Failed to fetch resource ${response.url}(${response.status}): ${response.statusText} `;
3
6
  }
4
7
  export async function getErrorMessageFromResponse(response) {
5
- let message = `Failed to fetch resource ${response.url} (${response.status}): `;
6
- try {
7
- const contentType = response.headers.get('Content-Type') || '';
8
- if (contentType.includes('application/json')) {
9
- message += await response.text();
10
- } else {
11
- message += response.statusText;
8
+ let message = `Failed to fetch resource ${response.url} (${response.status}): `;
9
+ try {
10
+ const contentType = response.headers.get('Content-Type') || '';
11
+ if (contentType.includes('application/json')) {
12
+ message += await response.text();
13
+ }
14
+ else {
15
+ message += response.statusText;
16
+ }
17
+ }
18
+ catch (error) {
19
+ // eslint forbids return in finally statement
20
+ return message;
12
21
  }
13
- } catch (error) {
14
22
  return message;
15
- }
16
- return message;
17
23
  }
18
- //# sourceMappingURL=fetch-error-message.js.map
@@ -1,26 +1,37 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { resolvePath } from '@loaders.gl/loader-utils';
2
5
  import { makeResponse } from "../utils/response-utils.js";
3
6
  export function isNodePath(url) {
4
- return !isRequestURL(url) && !isDataURL(url);
7
+ return !isRequestURL(url) && !isDataURL(url);
5
8
  }
6
9
  export function isRequestURL(url) {
7
- return url.startsWith('http:') || url.startsWith('https:');
10
+ return url.startsWith('http:') || url.startsWith('https:');
8
11
  }
9
12
  export function isDataURL(url) {
10
- return url.startsWith('data:');
13
+ return url.startsWith('data:');
11
14
  }
15
+ /**
16
+ * fetch API compatible function
17
+ * - Supports fetching from Node.js local file system paths
18
+ * - Respects pathPrefix and file aliases
19
+ */
12
20
  export async function fetchFile(urlOrData, fetchOptions) {
13
- if (typeof urlOrData === 'string') {
14
- const url = resolvePath(urlOrData);
15
- if (isNodePath(url)) {
16
- var _globalThis$loaders;
17
- if ((_globalThis$loaders = globalThis.loaders) !== null && _globalThis$loaders !== void 0 && _globalThis$loaders.fetchNode) {
18
- var _globalThis$loaders2;
19
- return (_globalThis$loaders2 = globalThis.loaders) === null || _globalThis$loaders2 === void 0 ? void 0 : _globalThis$loaders2.fetchNode(url, fetchOptions);
20
- }
21
+ if (typeof urlOrData === 'string') {
22
+ const url = resolvePath(urlOrData);
23
+ // Support fetching from local file system
24
+ if (isNodePath(url)) {
25
+ if (globalThis.loaders?.fetchNode) {
26
+ return globalThis.loaders?.fetchNode(url, fetchOptions);
27
+ }
28
+ // throw new Error(
29
+ // 'fetchFile: globalThis.loaders.fetchNode not defined. Install @loaders.gl/polyfills'
30
+ // );
31
+ }
32
+ // Call global fetch
33
+ return await fetch(url, fetchOptions);
21
34
  }
22
- return await fetch(url, fetchOptions);
23
- }
24
- return await makeResponse(urlOrData);
35
+ // TODO - should we still call fetch on non-URL inputs?
36
+ return await makeResponse(urlOrData);
25
37
  }
26
- //# sourceMappingURL=fetch-file.js.map
@@ -1,19 +1,34 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ /**
5
+ * Reads a chunk from a random access file
6
+ * @param file
7
+ * @param start
8
+ * @param length
9
+ * @returns
10
+ */
1
11
  export async function readArrayBuffer(file, start, length) {
2
- if (!(file instanceof Blob)) {
3
- file = new Blob([file]);
4
- }
5
- const slice = file.slice(start, start + length);
6
- return await readBlob(slice);
12
+ // TODO - we can do better for ArrayBuffer and string
13
+ if (!(file instanceof Blob)) {
14
+ file = new Blob([file]);
15
+ }
16
+ const slice = file.slice(start, start + length);
17
+ return await readBlob(slice);
7
18
  }
19
+ /**
20
+ * Read a slice of a Blob or File, without loading the entire file into memory
21
+ * The trick when reading File objects is to read successive "slices" of the File
22
+ * Per spec https://w3c.github.io/FileAPI/, slicing a File only updates the start and end fields
23
+ * Actually reading from file happens in `readAsArrayBuffer`
24
+ * @param blob to read
25
+ */
8
26
  export async function readBlob(blob) {
9
- return await new Promise((resolve, reject) => {
10
- const fileReader = new FileReader();
11
- fileReader.onload = event => {
12
- var _event$target;
13
- return resolve(event === null || event === void 0 ? void 0 : (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.result);
14
- };
15
- fileReader.onerror = error => reject(error);
16
- fileReader.readAsArrayBuffer(blob);
17
- });
27
+ return await new Promise((resolve, reject) => {
28
+ const fileReader = new FileReader();
29
+ fileReader.onload = (event) => resolve(event?.target?.result);
30
+ // TODO - reject with a proper Error
31
+ fileReader.onerror = (error) => reject(error);
32
+ fileReader.readAsArrayBuffer(blob);
33
+ });
18
34
  }
19
- //# sourceMappingURL=read-array-buffer.js.map