@loaders.gl/core 4.2.0-alpha.4 → 4.2.0-alpha.6

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 (112) hide show
  1. package/dist/core-addons/write-file-browser.js +62 -1
  2. package/dist/dist.dev.js +366 -301
  3. package/dist/dist.min.js +15 -0
  4. package/dist/index.cjs +60 -71
  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 +106 -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 +127 -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 +35 -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 +4 -2
  61. package/dist/null-worker.js +4 -2
  62. package/dist/workers/null-worker-node.js +3 -1
  63. package/dist/workers/null-worker.js +3 -1
  64. package/package.json +12 -11
  65. package/src/lib/api/parse-in-batches.ts +6 -5
  66. package/src/lib/loader-utils/option-utils.ts +3 -1
  67. package/src/lib/utils/resource-utils.ts +1 -1
  68. package/dist/core-addons/README.md +0 -1
  69. package/dist/core-addons/write-file-browser.js.map +0 -1
  70. package/dist/index.js.map +0 -1
  71. package/dist/iterators/batch-iterators/timed-batch-iterator.js.map +0 -1
  72. package/dist/iterators/make-iterator/make-array-buffer-iterator.js.map +0 -1
  73. package/dist/iterators/make-iterator/make-blob-iterator.js.map +0 -1
  74. package/dist/iterators/make-iterator/make-iterator.js.map +0 -1
  75. package/dist/iterators/make-iterator/make-stream-iterator.js.map +0 -1
  76. package/dist/iterators/make-iterator/make-string-iterator.js.map +0 -1
  77. package/dist/iterators/make-stream/make-stream.js.map +0 -1
  78. package/dist/javascript-utils/is-type.js.map +0 -1
  79. package/dist/lib/api/encode-table.js.map +0 -1
  80. package/dist/lib/api/encode.js.map +0 -1
  81. package/dist/lib/api/load-in-batches.js.map +0 -1
  82. package/dist/lib/api/load.js.map +0 -1
  83. package/dist/lib/api/loader-options.js.map +0 -1
  84. package/dist/lib/api/parse-in-batches.js.map +0 -1
  85. package/dist/lib/api/parse-sync.js.map +0 -1
  86. package/dist/lib/api/parse.js.map +0 -1
  87. package/dist/lib/api/register-loaders.js.map +0 -1
  88. package/dist/lib/api/select-loader.js.map +0 -1
  89. package/dist/lib/common.js.map +0 -1
  90. package/dist/lib/fetch/fetch-error-message.js.map +0 -1
  91. package/dist/lib/fetch/fetch-file.js.map +0 -1
  92. package/dist/lib/fetch/read-array-buffer.js.map +0 -1
  93. package/dist/lib/filesystems/browser-filesystem.js.map +0 -1
  94. package/dist/lib/filesystems/read-array-buffer.js.map +0 -1
  95. package/dist/lib/init.js.map +0 -1
  96. package/dist/lib/loader-utils/check-errors.js.map +0 -1
  97. package/dist/lib/loader-utils/get-data.js.map +0 -1
  98. package/dist/lib/loader-utils/get-fetch-function.js.map +0 -1
  99. package/dist/lib/loader-utils/loader-context.js.map +0 -1
  100. package/dist/lib/loader-utils/loggers.js.map +0 -1
  101. package/dist/lib/loader-utils/normalize-loader.js.map +0 -1
  102. package/dist/lib/loader-utils/option-defaults.js.map +0 -1
  103. package/dist/lib/loader-utils/option-utils.js.map +0 -1
  104. package/dist/lib/progress/fetch-progress.js.map +0 -1
  105. package/dist/lib/utils/log.js.map +0 -1
  106. package/dist/lib/utils/mime-type-utils.js.map +0 -1
  107. package/dist/lib/utils/resource-utils.js.map +0 -1
  108. package/dist/lib/utils/response-utils.js.map +0 -1
  109. package/dist/lib/utils/url-utils.js.map +0 -1
  110. package/dist/null-loader.js.map +0 -1
  111. package/dist/workers/null-worker-node.js.map +0 -1
  112. package/dist/workers/null-worker.js.map +0 -1
@@ -1,79 +1,137 @@
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
- constructor(files, options) {
4
- this._fetch = void 0;
5
- this.files = {};
6
- this.lowerCaseFiles = {};
7
- this.usedFiles = {};
8
- this._fetch = (options === null || options === void 0 ? void 0 : options.fetch) || fetch;
9
- for (let i = 0; i < files.length; ++i) {
10
- const file = files[i];
11
- this.files[file.name] = file;
12
- this.lowerCaseFiles[file.name.toLowerCase()] = file;
13
- this.usedFiles[file.name] = false;
10
+ _fetch;
11
+ files = {};
12
+ lowerCaseFiles = {};
13
+ usedFiles = {};
14
+ /**
15
+ * A FileSystem API wrapper around a list of browser 'File' objects
16
+ * @param files
17
+ * @param options
18
+ */
19
+ constructor(files, options) {
20
+ this._fetch = options?.fetch || fetch;
21
+ for (let i = 0; i < files.length; ++i) {
22
+ const file = files[i];
23
+ this.files[file.name] = file;
24
+ this.lowerCaseFiles[file.name.toLowerCase()] = file;
25
+ this.usedFiles[file.name] = false;
26
+ }
27
+ this.fetch = this.fetch.bind(this);
14
28
  }
15
- this.fetch = this.fetch.bind(this);
16
- }
17
- async fetch(path, options) {
18
- if (path.includes('://')) {
19
- return this._fetch(path, options);
29
+ // implements IFileSystem
30
+ /**
31
+ * Implementation of fetch against this file system
32
+ * Delegates to global fetch for http{s}:// or data://
33
+ */
34
+ async fetch(path, options) {
35
+ // Fallback to handle https:/http:/data: etc fetches
36
+ if (path.includes('://')) {
37
+ return this._fetch(path, options);
38
+ }
39
+ // Local fetches are served from the list of files
40
+ const file = this.files[path];
41
+ if (!file) {
42
+ return new Response(path, { status: 400, statusText: 'NOT FOUND' });
43
+ }
44
+ const headers = new Headers(options?.headers);
45
+ const range = headers.get('Range');
46
+ const bytes = range && /bytes=($1)-($2)/.exec(range);
47
+ if (bytes) {
48
+ const start = parseInt(bytes[1]);
49
+ const end = parseInt(bytes[2]);
50
+ // The trick when reading File objects is to read successive "slices" of the File
51
+ // Per spec https://w3c.github.io/FileAPI/, slicing a File should only update the start and end fields
52
+ // Actually reading from file should happen in `readAsArrayBuffer` (and as far we can tell it does)
53
+ const data = await file.slice(start, end).arrayBuffer();
54
+ const response = new Response(data);
55
+ Object.defineProperty(response, 'url', { value: path });
56
+ return response;
57
+ }
58
+ // return makeResponse()
59
+ const response = new Response(file);
60
+ Object.defineProperty(response, 'url', { value: path });
61
+ return response;
20
62
  }
21
- const file = this.files[path];
22
- if (!file) {
23
- return new Response(path, {
24
- status: 400,
25
- statusText: 'NOT FOUND'
26
- });
63
+ /**
64
+ * List filenames in this filesystem
65
+ * @param dirname
66
+ * @returns
67
+ */
68
+ async readdir(dirname) {
69
+ const files = [];
70
+ for (const path in this.files) {
71
+ files.push(path);
72
+ }
73
+ // TODO filter by dirname
74
+ return files;
27
75
  }
28
- const headers = new Headers(options === null || options === void 0 ? void 0 : options.headers);
29
- const range = headers.get('Range');
30
- const bytes = range && /bytes=($1)-($2)/.exec(range);
31
- if (bytes) {
32
- const start = parseInt(bytes[1]);
33
- const end = parseInt(bytes[2]);
34
- const data = await file.slice(start, end).arrayBuffer();
35
- const response = new Response(data);
36
- Object.defineProperty(response, 'url', {
37
- value: path
38
- });
39
- return response;
76
+ /**
77
+ * Return information (size) about files in this file system
78
+ */
79
+ async stat(path, options) {
80
+ const file = this.files[path];
81
+ if (!file) {
82
+ throw new Error(path);
83
+ }
84
+ return { size: file.size };
40
85
  }
41
- const response = new Response(file);
42
- Object.defineProperty(response, 'url', {
43
- value: path
44
- });
45
- return response;
46
- }
47
- async readdir(dirname) {
48
- const files = [];
49
- for (const path in this.files) {
50
- files.push(path);
86
+ /**
87
+ * Just removes the file from the list
88
+ */
89
+ async unlink(path) {
90
+ delete this.files[path];
91
+ delete this.lowerCaseFiles[path];
92
+ this.usedFiles[path] = true;
51
93
  }
52
- return files;
53
- }
54
- async stat(path, options) {
55
- const file = this.files[path];
56
- if (!file) {
57
- throw new Error(path);
94
+ // implements IRandomAccessFileSystem
95
+ // RANDOM ACCESS
96
+ async openReadableFile(pathname, flags) {
97
+ return new BlobFile(this.files[pathname]);
58
98
  }
59
- return {
60
- size: file.size
61
- };
62
- }
63
- async unlink(path) {
64
- delete this.files[path];
65
- delete this.lowerCaseFiles[path];
66
- this.usedFiles[path] = true;
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;
99
+ // PRIVATE
100
+ // Supports case independent paths, and file usage tracking
101
+ _getFile(path, used) {
102
+ // Prefer case match, but fall back to case independent.
103
+ const file = this.files[path] || this.lowerCaseFiles[path];
104
+ if (file && used) {
105
+ this.usedFiles[path] = true;
106
+ }
107
+ return file;
75
108
  }
76
- return file;
77
- }
78
109
  }
79
- //# sourceMappingURL=browser-filesystem.js.map
110
+ /*
111
+ * Read a range into a buffer
112
+ * @todo - handle position memory
113
+ * @param buffer is the buffer that the data (read from the fd) will be written to.
114
+ * @param offset is the offset in the buffer to start writing at.
115
+ * @param length is an integer specifying the number of bytes to read.
116
+ * @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.
117
+ *
118
+ async read(
119
+ fd: any,
120
+ buffer: ArrayBuffer,
121
+ offset: number = 0,
122
+ length: number = buffer.byteLength,
123
+ position: number | null = null
124
+ ): Promise<{bytesRead: number; buffer: ArrayBuffer}> {
125
+ const file = fd as File;
126
+ const startPosition = 0; // position
127
+ const arrayBuffer = await file.slice(startPosition, startPosition + length).arrayBuffer();
128
+ // copy into target buffer
129
+ return {bytesRead: length, buffer: arrayBuffer};
130
+ }
131
+
132
+ async close(fd: number): Promise<void> {
133
+ // NO OP
134
+ }
135
+
136
+ // fstat(fd: number): Promise<object>; // Stat
137
+ */
@@ -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
- if (file instanceof Blob) {
3
- const slice = file.slice(start, start + length);
4
- return await slice.arrayBuffer();
5
- }
6
- return await file.read(start, start + length);
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";
2
- const version = typeof "4.2.0-alpha.4" !== 'undefined' ? "4.2.0-alpha.4" : '';
5
+ // @ts-ignore TS2304: Cannot find name '__VERSION__'.
6
+ const version = typeof "4.2.0-alpha.5" !== 'undefined' ? "4.2.0-alpha.5" : '';
7
+ // @ts-ignore
3
8
  if (!globalThis.loaders) {
4
- log.log(1, `loaders.gl ${version}`)();
5
- globalThis.loaders = Object.assign(globalThis.loaders || {}, {
6
- VERSION: version,
7
- log
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
- if (!response.ok) {
3
- let errorMessage = `fetch failed ${response.status} ${response.statusText}`;
4
- try {
5
- const text = await response.text();
6
- if (text) {
7
- errorMessage += `: ${getErrorText(text)}`;
8
- }
9
- } catch (error) {}
10
- throw new Error(errorMessage);
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
- if (!response.ok) {
15
- throw new Error(`fetch failed ${response.status}`);
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
- const matches = /<pre>(.*)<\/pre>/.exec(text);
20
- return matches ? matches[1] : ` ${text.slice(0, 10)}...`;
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
- if (loader.text && typeof data === 'string') {
8
- return data;
9
- }
10
- if (isBuffer(data)) {
11
- data = data.buffer;
12
- }
13
- if (data instanceof ArrayBuffer) {
14
- const arrayBuffer = data;
15
- if (loader.text && !loader.binary) {
16
- const textDecoder = new TextDecoder('utf8');
17
- return textDecoder.decode(arrayBuffer);
18
- }
19
- return arrayBuffer;
20
- }
21
- if (ArrayBuffer.isView(data)) {
22
- if (loader.text && !loader.binary) {
23
- const textDecoder = new TextDecoder('utf8');
24
- return textDecoder.decode(data);
25
- }
26
- let arrayBuffer = data.buffer;
27
- const byteLength = data.byteLength || data.length;
28
- if (data.byteOffset !== 0 || byteLength !== arrayBuffer.byteLength) {
29
- arrayBuffer = arrayBuffer.slice(data.byteOffset, data.byteOffset + byteLength);
30
- }
31
- return arrayBuffer;
32
- }
33
- throw new Error(ERR_DATA);
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
- const isArrayBuffer = data instanceof ArrayBuffer || ArrayBuffer.isView(data);
37
- if (typeof data === 'string' || isArrayBuffer) {
38
- return getArrayBufferOrStringFromDataSync(data, loader, options);
39
- }
40
- if (isBlob(data)) {
41
- data = await makeResponse(data);
42
- }
43
- if (isResponse(data)) {
44
- const response = data;
45
- await checkResponse(response);
46
- return loader.binary ? await response.arrayBuffer() : await response.text();
47
- }
48
- if (isReadableStream(data)) {
49
- data = makeIterator(data, options);
50
- }
51
- if (isIterable(data) || isAsyncIterable(data)) {
52
- return concatenateArrayBuffersAsync(data);
53
- }
54
- throw new Error(ERR_DATA);
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
- if (isIterator(data)) {
58
- return data;
59
- }
60
- if (isResponse(data)) {
61
- const response = data;
62
- await checkResponse(response);
63
- const body = await response.body;
64
- return makeIterator(body, options);
65
- }
66
- if (isBlob(data) || isReadableStream(data)) {
67
- return makeIterator(data, options);
68
- }
69
- if (isAsyncIterable(data)) {
70
- return data;
71
- }
72
- return getIterableFromData(data);
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
- if (isReadableStream(data)) {
76
- return data;
77
- }
78
- if (isResponse(data)) {
79
- return data.body;
80
- }
81
- const response = await makeResponse(data);
82
- return response.body;
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
- if (ArrayBuffer.isView(data)) {
86
- return function* oneChunk() {
87
- yield data.buffer;
88
- }();
89
- }
90
- if (data instanceof ArrayBuffer) {
91
- return function* oneChunk() {
92
- yield data;
93
- }();
94
- }
95
- if (isIterator(data)) {
96
- return data;
97
- }
98
- if (isIterable(data)) {
99
- return data[Symbol.iterator]();
100
- }
101
- throw new Error(ERR_DATA);
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
- const globalOptions = getGlobalLoaderOptions();
6
- const loaderOptions = options || globalOptions;
7
- if (typeof loaderOptions.fetch === 'function') {
8
- return loaderOptions.fetch;
9
- }
10
- if (isObject(loaderOptions.fetch)) {
11
- return url => fetchFile(url, loaderOptions.fetch);
12
- }
13
- if (context !== null && context !== void 0 && context.fetch) {
14
- return context === null || context === void 0 ? void 0 : context.fetch;
15
- }
16
- return fetchFile;
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