@loaders.gl/core 4.2.0-alpha.4 → 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.
- package/dist/core-addons/write-file-browser.js +62 -1
- package/dist/dist.dev.js +366 -332
- package/dist/dist.min.js +15 -0
- package/dist/index.cjs +55 -68
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +20 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -1
- package/dist/iterators/batch-iterators/timed-batch-iterator.js +18 -13
- package/dist/iterators/make-iterator/make-array-buffer-iterator.d.ts +1 -1
- package/dist/iterators/make-iterator/make-array-buffer-iterator.d.ts.map +1 -1
- package/dist/iterators/make-iterator/make-array-buffer-iterator.js +21 -15
- package/dist/iterators/make-iterator/make-blob-iterator.d.ts +1 -1
- package/dist/iterators/make-iterator/make-blob-iterator.d.ts.map +1 -1
- package/dist/iterators/make-iterator/make-blob-iterator.js +18 -10
- package/dist/iterators/make-iterator/make-iterator.d.ts +1 -1
- package/dist/iterators/make-iterator/make-iterator.d.ts.map +1 -1
- package/dist/iterators/make-iterator/make-iterator.js +29 -18
- package/dist/iterators/make-iterator/make-stream-iterator.js +86 -23
- package/dist/iterators/make-iterator/make-string-iterator.d.ts +1 -1
- package/dist/iterators/make-iterator/make-string-iterator.d.ts.map +1 -1
- package/dist/iterators/make-iterator/make-string-iterator.js +20 -10
- package/dist/iterators/make-stream/make-stream.js +47 -29
- package/dist/javascript-utils/is-type.js +25 -19
- package/dist/lib/api/encode-table.js +40 -35
- package/dist/lib/api/encode.js +112 -73
- package/dist/lib/api/load-in-batches.js +35 -21
- package/dist/lib/api/load.js +35 -20
- package/dist/lib/api/loader-options.d.ts +2 -2
- package/dist/lib/api/loader-options.d.ts.map +1 -1
- package/dist/lib/api/loader-options.js +3 -1
- package/dist/lib/api/parse-in-batches.js +105 -69
- package/dist/lib/api/parse-sync.js +42 -33
- package/dist/lib/api/parse.js +73 -61
- package/dist/lib/api/register-loaders.js +23 -14
- package/dist/lib/api/select-loader.js +216 -163
- package/dist/lib/common.js +3 -1
- package/dist/lib/fetch/fetch-error-message.js +17 -12
- package/dist/lib/fetch/fetch-file.js +26 -15
- package/dist/lib/fetch/read-array-buffer.js +30 -15
- package/dist/lib/filesystems/browser-filesystem.js +126 -69
- package/dist/lib/filesystems/read-array-buffer.js +14 -6
- package/dist/lib/init.js +12 -6
- package/dist/lib/loader-utils/check-errors.js +37 -16
- package/dist/lib/loader-utils/get-data.js +110 -88
- package/dist/lib/loader-utils/get-fetch-function.js +24 -13
- package/dist/lib/loader-utils/loader-context.js +50 -31
- package/dist/lib/loader-utils/loggers.js +34 -44
- package/dist/lib/loader-utils/normalize-loader.js +45 -32
- package/dist/lib/loader-utils/option-defaults.js +38 -34
- package/dist/lib/loader-utils/option-utils.d.ts.map +1 -1
- package/dist/lib/loader-utils/option-utils.js +133 -80
- package/dist/lib/progress/fetch-progress.js +54 -47
- package/dist/lib/utils/log.js +4 -4
- package/dist/lib/utils/mime-type-utils.js +34 -11
- package/dist/lib/utils/resource-utils.js +77 -45
- package/dist/lib/utils/response-utils.js +97 -74
- package/dist/lib/utils/url-utils.js +6 -4
- package/dist/null-loader.js +43 -29
- package/dist/workers/null-worker-node.js +3 -1
- package/dist/workers/null-worker.js +3 -1
- package/package.json +7 -7
- package/src/lib/loader-utils/option-utils.ts +3 -1
- package/dist/core-addons/README.md +0 -1
- package/dist/core-addons/write-file-browser.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/iterators/batch-iterators/timed-batch-iterator.js.map +0 -1
- package/dist/iterators/make-iterator/make-array-buffer-iterator.js.map +0 -1
- package/dist/iterators/make-iterator/make-blob-iterator.js.map +0 -1
- package/dist/iterators/make-iterator/make-iterator.js.map +0 -1
- package/dist/iterators/make-iterator/make-stream-iterator.js.map +0 -1
- package/dist/iterators/make-iterator/make-string-iterator.js.map +0 -1
- package/dist/iterators/make-stream/make-stream.js.map +0 -1
- package/dist/javascript-utils/is-type.js.map +0 -1
- package/dist/lib/api/encode-table.js.map +0 -1
- package/dist/lib/api/encode.js.map +0 -1
- package/dist/lib/api/load-in-batches.js.map +0 -1
- package/dist/lib/api/load.js.map +0 -1
- package/dist/lib/api/loader-options.js.map +0 -1
- package/dist/lib/api/parse-in-batches.js.map +0 -1
- package/dist/lib/api/parse-sync.js.map +0 -1
- package/dist/lib/api/parse.js.map +0 -1
- package/dist/lib/api/register-loaders.js.map +0 -1
- package/dist/lib/api/select-loader.js.map +0 -1
- package/dist/lib/common.js.map +0 -1
- package/dist/lib/fetch/fetch-error-message.js.map +0 -1
- package/dist/lib/fetch/fetch-file.js.map +0 -1
- package/dist/lib/fetch/read-array-buffer.js.map +0 -1
- package/dist/lib/filesystems/browser-filesystem.js.map +0 -1
- package/dist/lib/filesystems/read-array-buffer.js.map +0 -1
- package/dist/lib/init.js.map +0 -1
- package/dist/lib/loader-utils/check-errors.js.map +0 -1
- package/dist/lib/loader-utils/get-data.js.map +0 -1
- package/dist/lib/loader-utils/get-fetch-function.js.map +0 -1
- package/dist/lib/loader-utils/loader-context.js.map +0 -1
- package/dist/lib/loader-utils/loggers.js.map +0 -1
- package/dist/lib/loader-utils/normalize-loader.js.map +0 -1
- package/dist/lib/loader-utils/option-defaults.js.map +0 -1
- package/dist/lib/loader-utils/option-utils.js.map +0 -1
- package/dist/lib/progress/fetch-progress.js.map +0 -1
- package/dist/lib/utils/log.js.map +0 -1
- package/dist/lib/utils/mime-type-utils.js.map +0 -1
- package/dist/lib/utils/resource-utils.js.map +0 -1
- package/dist/lib/utils/response-utils.js.map +0 -1
- package/dist/lib/utils/url-utils.js.map +0 -1
- package/dist/null-loader.js.map +0 -1
- package/dist/workers/null-worker-node.js.map +0 -1
- 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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
84
|
-
if (data
|
|
85
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
103
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
-
|
|
128
|
-
return loader;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return null;
|
|
178
|
+
return null;
|
|
132
179
|
}
|
|
133
180
|
function findLoaderByInitialBytes(loaders, data) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
164
|
-
|
|
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
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
package/dist/lib/common.js
CHANGED
|
@@ -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
|
-
|
|
5
|
+
return `Failed to fetch resource ${response.url}(${response.status}): ${response.statusText} `;
|
|
3
6
|
}
|
|
4
7
|
export async function getErrorMessageFromResponse(response) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
7
|
+
return !isRequestURL(url) && !isDataURL(url);
|
|
5
8
|
}
|
|
6
9
|
export function isRequestURL(url) {
|
|
7
|
-
|
|
10
|
+
return url.startsWith('http:') || url.startsWith('https:');
|
|
8
11
|
}
|
|
9
12
|
export function isDataURL(url) {
|
|
10
|
-
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
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
|
-
|
|
3
|
-
file
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|