@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,79 +1,136 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import { BlobFile } from '@loaders.gl/loader-utils';
|
|
5
|
+
/**
|
|
6
|
+
* FileSystem adapter for a browser FileList.
|
|
7
|
+
* Holds a list of browser 'File' objects.
|
|
8
|
+
*/
|
|
2
9
|
export class BrowserFileSystem {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
/**
|
|
11
|
+
* A FileSystem API wrapper around a list of browser 'File' objects
|
|
12
|
+
* @param files
|
|
13
|
+
* @param options
|
|
14
|
+
*/
|
|
15
|
+
constructor(files, options) {
|
|
16
|
+
this.files = {};
|
|
17
|
+
this.lowerCaseFiles = {};
|
|
18
|
+
this.usedFiles = {};
|
|
19
|
+
this._fetch = options?.fetch || fetch;
|
|
20
|
+
for (let i = 0; i < files.length; ++i) {
|
|
21
|
+
const file = files[i];
|
|
22
|
+
this.files[file.name] = file;
|
|
23
|
+
this.lowerCaseFiles[file.name.toLowerCase()] = file;
|
|
24
|
+
this.usedFiles[file.name] = false;
|
|
25
|
+
}
|
|
26
|
+
this.fetch = this.fetch.bind(this);
|
|
14
27
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
28
|
+
// implements IFileSystem
|
|
29
|
+
/**
|
|
30
|
+
* Implementation of fetch against this file system
|
|
31
|
+
* Delegates to global fetch for http{s}:// or data://
|
|
32
|
+
*/
|
|
33
|
+
async fetch(path, options) {
|
|
34
|
+
// Fallback to handle https:/http:/data: etc fetches
|
|
35
|
+
if (path.includes('://')) {
|
|
36
|
+
return this._fetch(path, options);
|
|
37
|
+
}
|
|
38
|
+
// Local fetches are served from the list of files
|
|
39
|
+
const file = this.files[path];
|
|
40
|
+
if (!file) {
|
|
41
|
+
return new Response(path, { status: 400, statusText: 'NOT FOUND' });
|
|
42
|
+
}
|
|
43
|
+
const headers = new Headers(options?.headers);
|
|
44
|
+
const range = headers.get('Range');
|
|
45
|
+
const bytes = range && /bytes=($1)-($2)/.exec(range);
|
|
46
|
+
if (bytes) {
|
|
47
|
+
const start = parseInt(bytes[1]);
|
|
48
|
+
const end = parseInt(bytes[2]);
|
|
49
|
+
// The trick when reading File objects is to read successive "slices" of the File
|
|
50
|
+
// Per spec https://w3c.github.io/FileAPI/, slicing a File should only update the start and end fields
|
|
51
|
+
// Actually reading from file should happen in `readAsArrayBuffer` (and as far we can tell it does)
|
|
52
|
+
const data = await file.slice(start, end).arrayBuffer();
|
|
53
|
+
const response = new Response(data);
|
|
54
|
+
Object.defineProperty(response, 'url', { value: path });
|
|
55
|
+
return response;
|
|
56
|
+
}
|
|
57
|
+
// return makeResponse()
|
|
58
|
+
const response = new Response(file);
|
|
59
|
+
Object.defineProperty(response, 'url', { value: path });
|
|
60
|
+
return response;
|
|
20
61
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
62
|
+
/**
|
|
63
|
+
* List filenames in this filesystem
|
|
64
|
+
* @param dirname
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
async readdir(dirname) {
|
|
68
|
+
const files = [];
|
|
69
|
+
for (const path in this.files) {
|
|
70
|
+
files.push(path);
|
|
71
|
+
}
|
|
72
|
+
// TODO filter by dirname
|
|
73
|
+
return files;
|
|
27
74
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
value: path
|
|
38
|
-
});
|
|
39
|
-
return response;
|
|
75
|
+
/**
|
|
76
|
+
* Return information (size) about files in this file system
|
|
77
|
+
*/
|
|
78
|
+
async stat(path, options) {
|
|
79
|
+
const file = this.files[path];
|
|
80
|
+
if (!file) {
|
|
81
|
+
throw new Error(path);
|
|
82
|
+
}
|
|
83
|
+
return { size: file.size };
|
|
40
84
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const files = [];
|
|
49
|
-
for (const path in this.files) {
|
|
50
|
-
files.push(path);
|
|
85
|
+
/**
|
|
86
|
+
* Just removes the file from the list
|
|
87
|
+
*/
|
|
88
|
+
async unlink(path) {
|
|
89
|
+
delete this.files[path];
|
|
90
|
+
delete this.lowerCaseFiles[path];
|
|
91
|
+
this.usedFiles[path] = true;
|
|
51
92
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (!file) {
|
|
57
|
-
throw new Error(path);
|
|
93
|
+
// implements IRandomAccessFileSystem
|
|
94
|
+
// RANDOM ACCESS
|
|
95
|
+
async openReadableFile(pathname, flags) {
|
|
96
|
+
return new BlobFile(this.files[pathname]);
|
|
58
97
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
async openReadableFile(pathname, flags) {
|
|
69
|
-
return new BlobFile(this.files[pathname]);
|
|
70
|
-
}
|
|
71
|
-
_getFile(path, used) {
|
|
72
|
-
const file = this.files[path] || this.lowerCaseFiles[path];
|
|
73
|
-
if (file && used) {
|
|
74
|
-
this.usedFiles[path] = true;
|
|
98
|
+
// PRIVATE
|
|
99
|
+
// Supports case independent paths, and file usage tracking
|
|
100
|
+
_getFile(path, used) {
|
|
101
|
+
// Prefer case match, but fall back to case independent.
|
|
102
|
+
const file = this.files[path] || this.lowerCaseFiles[path];
|
|
103
|
+
if (file && used) {
|
|
104
|
+
this.usedFiles[path] = true;
|
|
105
|
+
}
|
|
106
|
+
return file;
|
|
75
107
|
}
|
|
76
|
-
return file;
|
|
77
|
-
}
|
|
78
108
|
}
|
|
79
|
-
|
|
109
|
+
/*
|
|
110
|
+
* Read a range into a buffer
|
|
111
|
+
* @todo - handle position memory
|
|
112
|
+
* @param buffer is the buffer that the data (read from the fd) will be written to.
|
|
113
|
+
* @param offset is the offset in the buffer to start writing at.
|
|
114
|
+
* @param length is an integer specifying the number of bytes to read.
|
|
115
|
+
* @param position is an argument specifying where to begin reading from in the file. If position is null, data will be read from the current file position, and the file position will be updated. If position is an integer, the file position will remain unchanged.
|
|
116
|
+
*
|
|
117
|
+
async read(
|
|
118
|
+
fd: any,
|
|
119
|
+
buffer: ArrayBuffer,
|
|
120
|
+
offset: number = 0,
|
|
121
|
+
length: number = buffer.byteLength,
|
|
122
|
+
position: number | null = null
|
|
123
|
+
): Promise<{bytesRead: number; buffer: ArrayBuffer}> {
|
|
124
|
+
const file = fd as File;
|
|
125
|
+
const startPosition = 0; // position
|
|
126
|
+
const arrayBuffer = await file.slice(startPosition, startPosition + length).arrayBuffer();
|
|
127
|
+
// copy into target buffer
|
|
128
|
+
return {bytesRead: length, buffer: arrayBuffer};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async close(fd: number): Promise<void> {
|
|
132
|
+
// NO OP
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// fstat(fd: number): Promise<object>; // Stat
|
|
136
|
+
*/
|
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
/**
|
|
5
|
+
* Read a slice of a Blob or File, without loading the entire file into memory
|
|
6
|
+
* The trick when reading File objects is to read successive "slices" of the File
|
|
7
|
+
* Per spec https://w3c.github.io/FileAPI/, slicing a File only updates the start and end fields
|
|
8
|
+
* @param file to read
|
|
9
|
+
*/
|
|
1
10
|
export async function readArrayBuffer(file, start, length) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
11
|
+
if (file instanceof Blob) {
|
|
12
|
+
const slice = file.slice(start, start + length);
|
|
13
|
+
return await slice.arrayBuffer();
|
|
14
|
+
}
|
|
15
|
+
return await file.read(start, start + length);
|
|
7
16
|
}
|
|
8
|
-
//# sourceMappingURL=read-array-buffer.js.map
|
package/dist/lib/init.js
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import { log } from "./utils/log.js";
|
|
5
|
+
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
|
|
2
6
|
const version = typeof "4.2.0-alpha.4" !== 'undefined' ? "4.2.0-alpha.4" : '';
|
|
7
|
+
// @ts-ignore
|
|
3
8
|
if (!globalThis.loaders) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
log.log(1, `loaders.gl ${version}`)();
|
|
10
|
+
// @ts-ignore TS2339: Property 'loaders' does not exist on type 'Window & typeof globalThis'.
|
|
11
|
+
globalThis.loaders = Object.assign(globalThis.loaders || {}, {
|
|
12
|
+
VERSION: version,
|
|
13
|
+
log
|
|
14
|
+
});
|
|
9
15
|
}
|
|
16
|
+
// @ts-ignore
|
|
10
17
|
export default globalThis.loaders;
|
|
11
|
-
//# sourceMappingURL=init.js.map
|
|
@@ -1,22 +1,43 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
/**
|
|
5
|
+
* Check reponse status, if not OK extract error message and throw error
|
|
6
|
+
* @param response
|
|
7
|
+
*/
|
|
1
8
|
export async function checkFetchResponseStatus(response) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
if (!response.ok) {
|
|
10
|
+
let errorMessage = `fetch failed ${response.status} ${response.statusText}`;
|
|
11
|
+
try {
|
|
12
|
+
const text = await response.text();
|
|
13
|
+
if (text) {
|
|
14
|
+
errorMessage += `: ${getErrorText(text)}`;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
// ignore error
|
|
19
|
+
}
|
|
20
|
+
throw new Error(errorMessage);
|
|
21
|
+
}
|
|
12
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Check response status synchronously, if not OK extract error message and throw error
|
|
25
|
+
* Not able to extract as good an error message as the async version
|
|
26
|
+
* @param response
|
|
27
|
+
*/
|
|
13
28
|
export function checkFetchResponseStatusSync(response) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
throw new Error(`fetch failed ${response.status}`);
|
|
31
|
+
}
|
|
17
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Ad-hoc error message extractor
|
|
35
|
+
* @todo Handle XML, JSON, etc
|
|
36
|
+
* @param text
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
18
39
|
function getErrorText(text) {
|
|
19
|
-
|
|
20
|
-
|
|
40
|
+
// Look for HTML error texts
|
|
41
|
+
const matches = /<pre>(.*)<\/pre>/.exec(text);
|
|
42
|
+
return matches ? matches[1] : ` ${text.slice(0, 10)}...`;
|
|
21
43
|
}
|
|
22
|
-
//# sourceMappingURL=check-errors.js.map
|
|
@@ -1,103 +1,125 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import { concatenateArrayBuffersAsync } from '@loaders.gl/loader-utils';
|
|
2
5
|
import { isResponse, isReadableStream, isAsyncIterable, isIterable, isIterator, isBlob, isBuffer } from "../../javascript-utils/is-type.js";
|
|
3
6
|
import { makeIterator } from "../../iterators/make-iterator/make-iterator.js";
|
|
4
7
|
import { checkResponse, makeResponse } from "../utils/response-utils.js";
|
|
5
8
|
const ERR_DATA = 'Cannot convert supplied data type';
|
|
9
|
+
// eslint-disable-next-line complexity
|
|
6
10
|
export function getArrayBufferOrStringFromDataSync(data, loader, options) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
11
|
+
if (loader.text && typeof data === 'string') {
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
if (isBuffer(data)) {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
data = data.buffer;
|
|
17
|
+
}
|
|
18
|
+
if (data instanceof ArrayBuffer) {
|
|
19
|
+
const arrayBuffer = data;
|
|
20
|
+
if (loader.text && !loader.binary) {
|
|
21
|
+
const textDecoder = new TextDecoder('utf8');
|
|
22
|
+
return textDecoder.decode(arrayBuffer);
|
|
23
|
+
}
|
|
24
|
+
return arrayBuffer;
|
|
25
|
+
}
|
|
26
|
+
// We may need to handle offsets
|
|
27
|
+
if (ArrayBuffer.isView(data)) {
|
|
28
|
+
// TextDecoder is invoked on typed arrays and will handle offsets
|
|
29
|
+
if (loader.text && !loader.binary) {
|
|
30
|
+
const textDecoder = new TextDecoder('utf8');
|
|
31
|
+
return textDecoder.decode(data);
|
|
32
|
+
}
|
|
33
|
+
let arrayBuffer = data.buffer;
|
|
34
|
+
// Since we are returning the underlying arrayBuffer, we must create a new copy
|
|
35
|
+
// if this typed array / Buffer is a partial view into the ArryayBuffer
|
|
36
|
+
// TODO - this is a potentially unnecessary copy
|
|
37
|
+
const byteLength = data.byteLength || data.length;
|
|
38
|
+
if (data.byteOffset !== 0 || byteLength !== arrayBuffer.byteLength) {
|
|
39
|
+
// console.warn(`loaders.gl copying arraybuffer of length ${byteLength}`);
|
|
40
|
+
arrayBuffer = arrayBuffer.slice(data.byteOffset, data.byteOffset + byteLength);
|
|
41
|
+
}
|
|
42
|
+
return arrayBuffer;
|
|
43
|
+
}
|
|
44
|
+
throw new Error(ERR_DATA);
|
|
34
45
|
}
|
|
46
|
+
// Convert async iterator to a promise
|
|
35
47
|
export async function getArrayBufferOrStringFromData(data, loader, options) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
const isArrayBuffer = data instanceof ArrayBuffer || ArrayBuffer.isView(data);
|
|
49
|
+
if (typeof data === 'string' || isArrayBuffer) {
|
|
50
|
+
return getArrayBufferOrStringFromDataSync(data, loader, options);
|
|
51
|
+
}
|
|
52
|
+
// Blobs and files are FileReader compatible
|
|
53
|
+
if (isBlob(data)) {
|
|
54
|
+
data = await makeResponse(data);
|
|
55
|
+
}
|
|
56
|
+
if (isResponse(data)) {
|
|
57
|
+
const response = data;
|
|
58
|
+
await checkResponse(response);
|
|
59
|
+
return loader.binary ? await response.arrayBuffer() : await response.text();
|
|
60
|
+
}
|
|
61
|
+
if (isReadableStream(data)) {
|
|
62
|
+
// @ts-expect-error TS2559 options type
|
|
63
|
+
data = makeIterator(data, options);
|
|
64
|
+
}
|
|
65
|
+
if (isIterable(data) || isAsyncIterable(data)) {
|
|
66
|
+
// Assume arrayBuffer iterator - attempt to concatenate
|
|
67
|
+
return concatenateArrayBuffersAsync(data);
|
|
68
|
+
}
|
|
69
|
+
throw new Error(ERR_DATA);
|
|
55
70
|
}
|
|
56
71
|
export async function getAsyncIterableFromData(data, options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
if (isIterator(data)) {
|
|
73
|
+
return data;
|
|
74
|
+
}
|
|
75
|
+
if (isResponse(data)) {
|
|
76
|
+
const response = data;
|
|
77
|
+
// Note Since this function is not async, we currently can't load error message, just status
|
|
78
|
+
await checkResponse(response);
|
|
79
|
+
// TODO - bug in polyfill, body can be a Promise under Node.js
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/await-thenable
|
|
81
|
+
const body = await response.body;
|
|
82
|
+
// TODO - body can be null?
|
|
83
|
+
return makeIterator(body, options);
|
|
84
|
+
}
|
|
85
|
+
if (isBlob(data) || isReadableStream(data)) {
|
|
86
|
+
return makeIterator(data, options);
|
|
87
|
+
}
|
|
88
|
+
if (isAsyncIterable(data)) {
|
|
89
|
+
return data;
|
|
90
|
+
}
|
|
91
|
+
return getIterableFromData(data);
|
|
73
92
|
}
|
|
74
93
|
export async function getReadableStream(data) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
94
|
+
if (isReadableStream(data)) {
|
|
95
|
+
return data;
|
|
96
|
+
}
|
|
97
|
+
if (isResponse(data)) {
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
return data.body;
|
|
100
|
+
}
|
|
101
|
+
const response = await makeResponse(data);
|
|
102
|
+
// @ts-ignore
|
|
103
|
+
return response.body;
|
|
83
104
|
}
|
|
105
|
+
// HELPERS
|
|
84
106
|
function getIterableFromData(data) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
107
|
+
// generate an iterator that emits a single chunk
|
|
108
|
+
if (ArrayBuffer.isView(data)) {
|
|
109
|
+
return (function* oneChunk() {
|
|
110
|
+
yield data.buffer;
|
|
111
|
+
})();
|
|
112
|
+
}
|
|
113
|
+
if (data instanceof ArrayBuffer) {
|
|
114
|
+
return (function* oneChunk() {
|
|
115
|
+
yield data;
|
|
116
|
+
})();
|
|
117
|
+
}
|
|
118
|
+
if (isIterator(data)) {
|
|
119
|
+
return data;
|
|
120
|
+
}
|
|
121
|
+
if (isIterable(data)) {
|
|
122
|
+
return data[Symbol.iterator]();
|
|
123
|
+
}
|
|
124
|
+
throw new Error(ERR_DATA);
|
|
102
125
|
}
|
|
103
|
-
//# sourceMappingURL=get-data.js.map
|
|
@@ -1,18 +1,29 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
1
4
|
import { isObject } from "../../javascript-utils/is-type.js";
|
|
2
5
|
import { fetchFile } from "../fetch/fetch-file.js";
|
|
3
6
|
import { getGlobalLoaderOptions } from "./option-utils.js";
|
|
7
|
+
/**
|
|
8
|
+
* Gets the current fetch function from options and context
|
|
9
|
+
* @param options
|
|
10
|
+
* @param context
|
|
11
|
+
*/
|
|
4
12
|
export function getFetchFunction(options, context) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
const globalOptions = getGlobalLoaderOptions();
|
|
14
|
+
const loaderOptions = options || globalOptions;
|
|
15
|
+
// options.fetch can be a function
|
|
16
|
+
if (typeof loaderOptions.fetch === 'function') {
|
|
17
|
+
return loaderOptions.fetch;
|
|
18
|
+
}
|
|
19
|
+
// options.fetch can be an options object
|
|
20
|
+
if (isObject(loaderOptions.fetch)) {
|
|
21
|
+
return (url) => fetchFile(url, loaderOptions.fetch);
|
|
22
|
+
}
|
|
23
|
+
// else refer to context (from parent loader) if available
|
|
24
|
+
if (context?.fetch) {
|
|
25
|
+
return context?.fetch;
|
|
26
|
+
}
|
|
27
|
+
// else return the default fetch function
|
|
28
|
+
return fetchFile;
|
|
17
29
|
}
|
|
18
|
-
//# sourceMappingURL=get-fetch-function.js.map
|