@loaders.gl/loader-utils 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.
Files changed (144) hide show
  1. package/dist/index.cjs +66 -84
  2. package/dist/index.cjs.map +7 -0
  3. package/dist/index.d.ts +46 -46
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +18 -1
  6. package/dist/json-loader.d.ts +1 -1
  7. package/dist/json-loader.d.ts.map +1 -1
  8. package/dist/json-loader.js +19 -13
  9. package/dist/lib/binary-utils/array-buffer-utils.js +75 -46
  10. package/dist/lib/binary-utils/dataview-copy-utils.d.ts +1 -1
  11. package/dist/lib/binary-utils/dataview-copy-utils.d.ts.map +1 -1
  12. package/dist/lib/binary-utils/dataview-copy-utils.js +75 -34
  13. package/dist/lib/binary-utils/get-first-characters.js +39 -21
  14. package/dist/lib/binary-utils/memory-conversion-utils.js +38 -23
  15. package/dist/lib/binary-utils/memory-copy-utils.js +49 -20
  16. package/dist/lib/env-utils/assert.js +7 -4
  17. package/dist/lib/env-utils/globals.js +15 -7
  18. package/dist/lib/file-provider/data-view-file.d.ts +1 -1
  19. package/dist/lib/file-provider/data-view-file.d.ts.map +1 -1
  20. package/dist/lib/file-provider/data-view-file.js +58 -29
  21. package/dist/lib/file-provider/file-handle-file.d.ts +1 -1
  22. package/dist/lib/file-provider/file-handle-file.d.ts.map +1 -1
  23. package/dist/lib/file-provider/file-handle-file.js +96 -58
  24. package/dist/lib/file-provider/file-provider.js +8 -3
  25. package/dist/lib/files/blob-file.d.ts +1 -1
  26. package/dist/lib/files/blob-file.d.ts.map +1 -1
  27. package/dist/lib/files/blob-file.js +24 -23
  28. package/dist/lib/files/file.js +3 -1
  29. package/dist/lib/files/http-file.d.ts +1 -1
  30. package/dist/lib/files/http-file.d.ts.map +1 -1
  31. package/dist/lib/files/http-file.js +91 -71
  32. package/dist/lib/files/node-file-facade.d.ts +1 -1
  33. package/dist/lib/files/node-file-facade.d.ts.map +1 -1
  34. package/dist/lib/files/node-file-facade.js +37 -29
  35. package/dist/lib/files/sources.js +150 -1
  36. package/dist/lib/filesystems/filesystem.d.ts +1 -1
  37. package/dist/lib/filesystems/filesystem.d.ts.map +1 -1
  38. package/dist/lib/filesystems/filesystem.js +3 -1
  39. package/dist/lib/filesystems/node-filesystem-facade.d.ts +3 -3
  40. package/dist/lib/filesystems/node-filesystem-facade.d.ts.map +1 -1
  41. package/dist/lib/filesystems/node-filesystem-facade.js +41 -31
  42. package/dist/lib/iterators/async-iteration.js +39 -24
  43. package/dist/lib/iterators/text-iterators.js +46 -39
  44. package/dist/lib/node/buffer.browser.js +15 -3
  45. package/dist/lib/node/buffer.js +30 -16
  46. package/dist/lib/node/fs.browser.js +0 -1
  47. package/dist/lib/node/promisify.js +10 -4
  48. package/dist/lib/node/stream.browser.js +0 -1
  49. package/dist/lib/node/stream.js +4 -1
  50. package/dist/lib/option-utils/merge-loader-options.d.ts +1 -1
  51. package/dist/lib/option-utils/merge-loader-options.d.ts.map +1 -1
  52. package/dist/lib/option-utils/merge-loader-options.js +25 -17
  53. package/dist/lib/parser-utils/parse-json.js +9 -6
  54. package/dist/lib/path-utils/file-aliases.js +29 -13
  55. package/dist/lib/path-utils/get-cwd.js +6 -7
  56. package/dist/lib/path-utils/path.js +149 -105
  57. package/dist/lib/request-utils/request-scheduler.d.ts +2 -1
  58. package/dist/lib/request-utils/request-scheduler.d.ts.map +1 -1
  59. package/dist/lib/request-utils/request-scheduler.js +130 -102
  60. package/dist/lib/sources/data-source.js +48 -38
  61. package/dist/lib/sources/image-source.d.ts +3 -3
  62. package/dist/lib/sources/image-source.d.ts.map +1 -1
  63. package/dist/lib/sources/image-source.js +11 -3
  64. package/dist/lib/sources/image-tile-source.d.ts +3 -3
  65. package/dist/lib/sources/image-tile-source.d.ts.map +1 -1
  66. package/dist/lib/sources/image-tile-source.js +3 -1
  67. package/dist/lib/sources/tile-source-adapter.d.ts +2 -2
  68. package/dist/lib/sources/tile-source-adapter.d.ts.map +1 -1
  69. package/dist/lib/sources/tile-source-adapter.js +43 -36
  70. package/dist/lib/sources/tile-source.js +3 -1
  71. package/dist/lib/sources/utils/image-type.js +0 -1
  72. package/dist/lib/sources/utils/utils.js +31 -17
  73. package/dist/lib/sources/vector-tile-source.d.ts +2 -2
  74. package/dist/lib/sources/vector-tile-source.d.ts.map +1 -1
  75. package/dist/lib/sources/vector-tile-source.js +3 -1
  76. package/dist/lib/worker-loader-utils/create-loader-worker.d.ts +1 -1
  77. package/dist/lib/worker-loader-utils/create-loader-worker.d.ts.map +1 -1
  78. package/dist/lib/worker-loader-utils/create-loader-worker.js +87 -87
  79. package/dist/lib/worker-loader-utils/encode-with-worker.d.ts +1 -1
  80. package/dist/lib/worker-loader-utils/encode-with-worker.d.ts.map +1 -1
  81. package/dist/lib/worker-loader-utils/encode-with-worker.js +13 -8
  82. package/dist/lib/worker-loader-utils/parse-with-worker.d.ts +1 -1
  83. package/dist/lib/worker-loader-utils/parse-with-worker.d.ts.map +1 -1
  84. package/dist/lib/worker-loader-utils/parse-with-worker.js +68 -55
  85. package/dist/loader-types.d.ts +2 -2
  86. package/dist/loader-types.d.ts.map +1 -1
  87. package/dist/loader-types.js +26 -10
  88. package/dist/service-types.d.ts +1 -1
  89. package/dist/service-types.d.ts.map +1 -1
  90. package/dist/service-types.js +3 -1
  91. package/dist/types.js +1 -1
  92. package/dist/workers/json-worker.js +0 -1
  93. package/dist/writer-types.js +3 -1
  94. package/package.json +6 -4
  95. package/src/lib/request-utils/request-scheduler.ts +17 -8
  96. package/dist/index.js.map +0 -1
  97. package/dist/json-loader.js.map +0 -1
  98. package/dist/lib/binary-utils/array-buffer-utils.js.map +0 -1
  99. package/dist/lib/binary-utils/dataview-copy-utils.js.map +0 -1
  100. package/dist/lib/binary-utils/get-first-characters.js.map +0 -1
  101. package/dist/lib/binary-utils/memory-conversion-utils.js.map +0 -1
  102. package/dist/lib/binary-utils/memory-copy-utils.js.map +0 -1
  103. package/dist/lib/env-utils/assert.js.map +0 -1
  104. package/dist/lib/env-utils/globals.js.map +0 -1
  105. package/dist/lib/file-provider/data-view-file.js.map +0 -1
  106. package/dist/lib/file-provider/file-handle-file.js.map +0 -1
  107. package/dist/lib/file-provider/file-provider.js.map +0 -1
  108. package/dist/lib/files/blob-file.js.map +0 -1
  109. package/dist/lib/files/file.js.map +0 -1
  110. package/dist/lib/files/http-file.js.map +0 -1
  111. package/dist/lib/files/node-file-facade.js.map +0 -1
  112. package/dist/lib/files/sources.js.map +0 -1
  113. package/dist/lib/filesystems/filesystem.js.map +0 -1
  114. package/dist/lib/filesystems/node-filesystem-facade.js.map +0 -1
  115. package/dist/lib/iterators/async-iteration.js.map +0 -1
  116. package/dist/lib/iterators/text-iterators.js.map +0 -1
  117. package/dist/lib/node/buffer.browser.js.map +0 -1
  118. package/dist/lib/node/buffer.js.map +0 -1
  119. package/dist/lib/node/fs.browser.js.map +0 -1
  120. package/dist/lib/node/promisify.js.map +0 -1
  121. package/dist/lib/node/stream.browser.js.map +0 -1
  122. package/dist/lib/node/stream.js.map +0 -1
  123. package/dist/lib/option-utils/merge-loader-options.js.map +0 -1
  124. package/dist/lib/parser-utils/parse-json.js.map +0 -1
  125. package/dist/lib/path-utils/file-aliases.js.map +0 -1
  126. package/dist/lib/path-utils/get-cwd.js.map +0 -1
  127. package/dist/lib/path-utils/path.js.map +0 -1
  128. package/dist/lib/request-utils/request-scheduler.js.map +0 -1
  129. package/dist/lib/sources/data-source.js.map +0 -1
  130. package/dist/lib/sources/image-source.js.map +0 -1
  131. package/dist/lib/sources/image-tile-source.js.map +0 -1
  132. package/dist/lib/sources/tile-source-adapter.js.map +0 -1
  133. package/dist/lib/sources/tile-source.js.map +0 -1
  134. package/dist/lib/sources/utils/image-type.js.map +0 -1
  135. package/dist/lib/sources/utils/utils.js.map +0 -1
  136. package/dist/lib/sources/vector-tile-source.js.map +0 -1
  137. package/dist/lib/worker-loader-utils/create-loader-worker.js.map +0 -1
  138. package/dist/lib/worker-loader-utils/encode-with-worker.js.map +0 -1
  139. package/dist/lib/worker-loader-utils/parse-with-worker.js.map +0 -1
  140. package/dist/loader-types.js.map +0 -1
  141. package/dist/service-types.js.map +0 -1
  142. package/dist/types.js.map +0 -1
  143. package/dist/workers/json-worker.js.map +0 -1
  144. package/dist/writer-types.js.map +0 -1
@@ -1,2 +1,151 @@
1
+ "use strict";
2
+ /*
3
+ import {fetchFile} from '@loaders.gl/core';
1
4
 
2
- //# sourceMappingURL=sources.js.map
5
+ import {Source as PMTilesSource, RangeResponse} from 'pmtiles';
6
+
7
+ /** @note "source" here is a PMTiles library type, referring to *
8
+ export function makeSource(data: string | Blob, fetch?) {
9
+ if (typeof data === 'string') {
10
+ return new FetchSource(data, fetch);
11
+ }
12
+ if (data instanceof Blob) {
13
+ const url = '';
14
+ return new BlobSource(data, url);
15
+ }
16
+ }
17
+
18
+ export class BlobSource implements PMTilesSource {
19
+ blob: Blob;
20
+ key: string;
21
+
22
+ constructor(blob: Blob, key: string) {
23
+ this.blob = blob;
24
+ this.key = key;
25
+ }
26
+
27
+ // TODO - how is this used?
28
+ getKey() {
29
+ return this.blob.url || '';
30
+ }
31
+
32
+ async getBytes(offset: number, length: number, signal?: AbortSignal): Promise<RangeResponse> {
33
+ const data = await this.blob.arrayBuffer();
34
+ return {
35
+ data
36
+ // etag: response.headers.get('ETag') || undefined,
37
+ // cacheControl: response.headers.get('Cache-Control') || undefined,
38
+ // expires: response.headers.get('Expires') || undefined
39
+ };
40
+ }
41
+ }
42
+
43
+ export class FetchSource implements PMTilesSource {
44
+ url: string;
45
+ fetch;
46
+
47
+ constructor(url: string, fetch = fetchFile) {
48
+ this.url = url;
49
+ this.fetch = fetch;
50
+ }
51
+
52
+ // TODO - how is this used?
53
+ getKey() {
54
+ return this.url;
55
+ }
56
+
57
+ async getBytes(offset: number, length: number, signal?: AbortSignal): Promise<RangeResponse> {
58
+ let controller;
59
+ if (!signal) {
60
+ // ToDO why is it so important to abort in case 200?
61
+ // TODO check this works or assert 206
62
+ controller = new AbortController();
63
+ signal = controller.signal;
64
+ }
65
+
66
+ let response = await fetch(this.url, {
67
+ signal,
68
+ headers: {Range: `bytes=${offset}-${offset + length - 1}`}
69
+ });
70
+
71
+ switch (response.status) {
72
+ case 206: // Partial Content success
73
+ // This is the expected success code for a range request
74
+ break;
75
+
76
+ case 200:
77
+ // some well-behaved backends, e.g. DigitalOcean CDN, respond with 200 instead of 206
78
+ // but we also need to detect no support for Byte Serving which is returning the whole file
79
+ const content_length = response.headers.get('Content-Length');
80
+ if (!content_length || Number(content_length) > length) {
81
+ if (controller) {
82
+ controller.abort();
83
+ }
84
+ throw Error(
85
+ 'content-length header missing or exceeding request. Server must support HTTP Byte Serving.'
86
+ );
87
+ }
88
+
89
+ case 416: // "Range Not Satisfiable"
90
+ // some HTTP servers don't accept ranges beyond the end of the resource.
91
+ // Retry with the exact length
92
+ // TODO: can return 416 with offset > 0 if content changed, which will have a blank etag.
93
+ // See https://github.com/protomaps/PMTiles/issues/90
94
+ if (offset === 0) {
95
+ const content_range = response.headers.get('Content-Range');
96
+ if (!content_range || !content_range.startsWith('bytes *')) {
97
+ throw Error('Missing content-length on 416 response');
98
+ }
99
+ const actual_length = Number(content_range.substr(8));
100
+ response = await fetch(this.url, {
101
+ signal,
102
+ headers: {Range: `bytes=0-${actual_length - 1}`}
103
+ });
104
+ }
105
+ break;
106
+
107
+ default:
108
+ if (response.status >= 300) {
109
+ throw Error(`Bad response code: ${response.status}`);
110
+ }
111
+ }
112
+
113
+ const data = await response.arrayBuffer();
114
+ return {
115
+ data,
116
+ etag: response.headers.get('ETag') || undefined,
117
+ cacheControl: response.headers.get('Cache-Control') || undefined,
118
+ expires: response.headers.get('Expires') || undefined
119
+ };
120
+ }
121
+ }
122
+
123
+ /*
124
+ class TestNodeFileSource implements Source {
125
+ buffer: ArrayBuffer;
126
+ path: string;
127
+ key: string;
128
+ etag?: string;
129
+
130
+ constructor(path: string, key: string) {
131
+ this.path = path;
132
+ this.buffer = fs.readFileSync(path);
133
+ this.key = key;
134
+ }
135
+
136
+ getKey() {
137
+ return this.key;
138
+ }
139
+
140
+ replaceData(path: string) {
141
+ this.path = path;
142
+ this.buffer = fs.readFileSync(path);
143
+ }
144
+
145
+ async getBytes(offset: number, length: number): Promise<RangeResponse> {
146
+ const slice = new Uint8Array(this.buffer.slice(offset, offset + length))
147
+ .buffer;
148
+ return { data: slice, etag: this.etag };
149
+ }
150
+ }
151
+ */
@@ -1,4 +1,4 @@
1
- import { ReadableFile, WritableFile } from '../files/file';
1
+ import { ReadableFile, WritableFile } from "../files/file.js";
2
2
  /**
3
3
  * A FileSystem interface can encapsulate various file sources,
4
4
  * a FileList, a Node.js filesystem, a ZipFile, a GoogleDrive etc.
@@ -1 +1 @@
1
- {"version":3,"file":"filesystem.d.ts","sourceRoot":"","sources":["../../../src/lib/filesystems/filesystem.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,YAAY,EAAE,YAAY,EAAC,MAAM,eAAe,CAAC;AAEzD;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,mDAAmD;IACnD,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAC,SAAS,CAAC,EAAE,OAAO,CAAA;KAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAE9E,6DAA6D;IAC7D,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAC,CAAC,CAAC;IAElE,0CAA0C;IAC1C,MAAM,CAAC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAErC,yEAAyE;IACzE,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC/D;AAED;;GAEG;AACH,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AQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAE3B,8BAA8B;IAC9B,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAE3B,2BAA2B;IAC3B,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IAEnE,2BAA2B;IAC3B,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,GAAG,GAAG,IAAI,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CAC1F"}
@@ -1,2 +1,4 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  export {};
2
- //# sourceMappingURL=filesystem.js.map
@@ -1,6 +1,6 @@
1
- import { Stat } from '../files/file';
2
- import { NodeFileFacade as NodeFile } from '../files/node-file-facade';
3
- import { RandomAccessFileSystem } from './filesystem';
1
+ import { Stat } from "../files/file.js";
2
+ import { NodeFileFacade as NodeFile } from "../files/node-file-facade.js";
3
+ import { RandomAccessFileSystem } from "./filesystem.js";
4
4
  /**
5
5
  * FileSystem pass-through for Node.js
6
6
  * Compatible with BrowserFileSystem.
@@ -1 +1 @@
1
- {"version":3,"file":"node-filesystem-facade.d.ts","sourceRoot":"","sources":["../../../src/lib/filesystems/node-filesystem-facade.ts"],"names":[],"mappings":"AAKA,OAAO,EAAC,IAAI,EAAC,MAAM,eAAe,CAAC;AACnC,OAAO,EAAC,cAAc,IAAI,QAAQ,EAAC,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAC,sBAAsB,EAAC,MAAM,cAAc,CAAC;AAIpD;;;;;GAKG;AACH,qBAAa,oBAAqB,YAAW,sBAAsB;gBAErD,OAAO,EAAE;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAC;IAgBzC,QAAQ,CAAC,QAAQ,QAAQ;IACzB,QAA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O,SAAM,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAIvD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAI/C,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAInC,KAAK,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC;CAGzE"}
@@ -1,37 +1,47 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { isBrowser } from "../env-utils/globals.js";
2
5
  const NOT_IMPLEMENTED = new Error('Not implemented');
6
+ /**
7
+ * FileSystem pass-through for Node.js
8
+ * Compatible with BrowserFileSystem.
9
+ * @note Dummy implementation, not used (constructor returns a real NodeFileSystem instance)
10
+ * @param options
11
+ */
3
12
  export class NodeFileSystemFacade {
4
- constructor(options) {
5
- var _globalThis$loaders;
6
- this.readable = true;
7
- this.writable = true;
8
- if ((_globalThis$loaders = globalThis.loaders) !== null && _globalThis$loaders !== void 0 && _globalThis$loaders.NodeFileSystem) {
9
- return new globalThis.loaders.NodeFileSystem(options);
13
+ // implements FileSystem
14
+ constructor(options) {
15
+ // DUMMY IMPLEMENTATION, not used (constructor returns a real NodeFileSystem instance)
16
+ // implements RandomAccessReadFileSystem
17
+ this.readable = true;
18
+ this.writable = true;
19
+ if (globalThis.loaders?.NodeFileSystem) {
20
+ return new globalThis.loaders.NodeFileSystem(options);
21
+ }
22
+ if (isBrowser) {
23
+ throw new Error('Can\'t instantiate NodeFileSystem in browser.');
24
+ }
25
+ throw new Error('Can\'t instantiate NodeFileSystem. Make sure to import @loaders.gl/polyfills first.');
10
26
  }
11
- if (isBrowser) {
12
- throw new Error('Can\'t instantiate NodeFileSystem in browser.');
27
+ async openReadableFile(path, flags) {
28
+ throw NOT_IMPLEMENTED;
29
+ }
30
+ // implements RandomAccessWriteFileSystem
31
+ async openWritableFile(path, flags, mode) {
32
+ throw NOT_IMPLEMENTED;
33
+ }
34
+ // Implements file system
35
+ async readdir(dirname = '.', options) {
36
+ throw NOT_IMPLEMENTED;
37
+ }
38
+ async stat(path, options) {
39
+ throw NOT_IMPLEMENTED;
40
+ }
41
+ async unlink(path) {
42
+ throw NOT_IMPLEMENTED;
43
+ }
44
+ async fetch(path, options) {
45
+ throw NOT_IMPLEMENTED;
13
46
  }
14
- throw new Error('Can\'t instantiate NodeFileSystem. Make sure to import @loaders.gl/polyfills first.');
15
- }
16
- async openReadableFile(path, flags) {
17
- throw NOT_IMPLEMENTED;
18
- }
19
- async openWritableFile(path, flags, mode) {
20
- throw NOT_IMPLEMENTED;
21
- }
22
- async readdir() {
23
- let dirname = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '.';
24
- let options = arguments.length > 1 ? arguments[1] : undefined;
25
- throw NOT_IMPLEMENTED;
26
- }
27
- async stat(path, options) {
28
- throw NOT_IMPLEMENTED;
29
- }
30
- async unlink(path) {
31
- throw NOT_IMPLEMENTED;
32
- }
33
- async fetch(path, options) {
34
- throw NOT_IMPLEMENTED;
35
- }
36
47
  }
37
- //# sourceMappingURL=node-filesystem-facade.js.map
@@ -1,32 +1,47 @@
1
1
  import { concatenateArrayBuffers } from "../binary-utils/array-buffer-utils.js";
2
+ // GENERAL UTILITIES
3
+ /**
4
+ * Iterate over async iterator, without resetting iterator if end is not reached
5
+ * - forEach intentionally does not reset iterator if exiting loop prematurely
6
+ * so that iteration can continue in a second loop
7
+ * - It is recommended to use a standard for-await as last loop to ensure
8
+ * iterator gets properly reset
9
+ *
10
+ * TODO - optimize using sync iteration if argument is an Iterable?
11
+ *
12
+ * @param iterator
13
+ * @param visitor
14
+ */
2
15
  export async function forEach(iterator, visitor) {
3
- while (true) {
4
- const {
5
- done,
6
- value
7
- } = await iterator.next();
8
- if (done) {
9
- iterator.return();
10
- return;
16
+ // eslint-disable-next-line
17
+ while (true) {
18
+ const { done, value } = await iterator.next();
19
+ if (done) {
20
+ iterator.return();
21
+ return;
22
+ }
23
+ const cancel = visitor(value);
24
+ if (cancel) {
25
+ return;
26
+ }
11
27
  }
12
- const cancel = visitor(value);
13
- if (cancel) {
14
- return;
15
- }
16
- }
17
28
  }
29
+ // Breaking big data into iterable chunks, concatenating iterable chunks into big data objects
30
+ /**
31
+ * Concatenates all data chunks yielded by an (async) iterator
32
+ * This function can e.g. be used to enable atomic parsers to work on (async) iterator inputs
33
+ */
18
34
  export async function concatenateArrayBuffersAsync(asyncIterator) {
19
- const arrayBuffers = [];
20
- for await (const chunk of asyncIterator) {
21
- arrayBuffers.push(chunk);
22
- }
23
- return concatenateArrayBuffers(...arrayBuffers);
35
+ const arrayBuffers = [];
36
+ for await (const chunk of asyncIterator) {
37
+ arrayBuffers.push(chunk);
38
+ }
39
+ return concatenateArrayBuffers(...arrayBuffers);
24
40
  }
25
41
  export async function concatenateStringsAsync(asyncIterator) {
26
- const strings = [];
27
- for await (const chunk of asyncIterator) {
28
- strings.push(chunk);
29
- }
30
- return strings.join('');
42
+ const strings = [];
43
+ for await (const chunk of asyncIterator) {
44
+ strings.push(chunk);
45
+ }
46
+ return strings.join('');
31
47
  }
32
- //# sourceMappingURL=async-iteration.js.map
@@ -1,47 +1,54 @@
1
- export function makeTextDecoderIterator(arrayBufferIterator) {
2
- try {
3
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
4
- return async function* () {
5
- const textDecoder = new TextDecoder(undefined, options);
6
- for await (const arrayBuffer of arrayBufferIterator) {
7
- yield typeof arrayBuffer === 'string' ? arrayBuffer : textDecoder.decode(arrayBuffer, {
8
- stream: true
9
- });
10
- }
11
- }();
12
- } catch (e) {
13
- return Promise.reject(e);
14
- }
1
+ // TextDecoder iterators
2
+ // TextDecoder will keep any partial undecoded bytes between calls to `decode`
3
+ export async function* makeTextDecoderIterator(arrayBufferIterator, options = {}) {
4
+ const textDecoder = new TextDecoder(undefined, options);
5
+ for await (const arrayBuffer of arrayBufferIterator) {
6
+ yield typeof arrayBuffer === 'string'
7
+ ? arrayBuffer
8
+ : textDecoder.decode(arrayBuffer, { stream: true });
9
+ }
15
10
  }
11
+ // TextEncoder iterator
12
+ // TODO - this is not useful unless min chunk size is given
13
+ // TextEncoder will keep any partial undecoded bytes between calls to `encode`
14
+ // If iterator does not yield strings, assume arrayBuffer and return unencoded
16
15
  export async function* makeTextEncoderIterator(textIterator) {
17
- const textEncoder = new TextEncoder();
18
- for await (const text of textIterator) {
19
- yield typeof text === 'string' ? textEncoder.encode(text) : text;
20
- }
16
+ const textEncoder = new TextEncoder();
17
+ for await (const text of textIterator) {
18
+ yield typeof text === 'string' ? textEncoder.encode(text) : text;
19
+ }
21
20
  }
21
+ /**
22
+ * @param textIterator async iterable yielding strings
23
+ * @returns an async iterable over lines
24
+ * See http://2ality.com/2018/04/async-iter-nodejs.html
25
+ */
22
26
  export async function* makeLineIterator(textIterator) {
23
- let previous = '';
24
- for await (const textChunk of textIterator) {
25
- previous += textChunk;
26
- let eolIndex;
27
- while ((eolIndex = previous.indexOf('\n')) >= 0) {
28
- const line = previous.slice(0, eolIndex + 1);
29
- previous = previous.slice(eolIndex + 1);
30
- yield line;
27
+ let previous = '';
28
+ for await (const textChunk of textIterator) {
29
+ previous += textChunk;
30
+ let eolIndex;
31
+ while ((eolIndex = previous.indexOf('\n')) >= 0) {
32
+ // line includes the EOL
33
+ const line = previous.slice(0, eolIndex + 1);
34
+ previous = previous.slice(eolIndex + 1);
35
+ yield line;
36
+ }
37
+ }
38
+ if (previous.length > 0) {
39
+ yield previous;
31
40
  }
32
- }
33
- if (previous.length > 0) {
34
- yield previous;
35
- }
36
41
  }
42
+ /**
43
+ * @param lineIterator async iterable yielding lines
44
+ * @returns async iterable yielding numbered lines
45
+ *
46
+ * See http://2ality.com/2018/04/async-iter-nodejs.html
47
+ */
37
48
  export async function* makeNumberedLineIterator(lineIterator) {
38
- let counter = 1;
39
- for await (const line of lineIterator) {
40
- yield {
41
- counter,
42
- line
43
- };
44
- counter++;
45
- }
49
+ let counter = 1;
50
+ for await (const line of lineIterator) {
51
+ yield { counter, line };
52
+ counter++;
53
+ }
46
54
  }
47
- //# sourceMappingURL=text-iterators.js.map
@@ -1,7 +1,19 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // Isolates Buffer references to ensure they are only bundled under Node.js (avoids big webpack polyfill)
5
+ // this file is selected by the package.json "browser" field).
6
+ /**
7
+ * Convert Buffer to ArrayBuffer
8
+ * Converts Node.js `Buffer` to `ArrayBuffer` (without triggering bundler to include Buffer polyfill on browser)
9
+ * @todo better data type
10
+ */
1
11
  export function toArrayBuffer(buffer) {
2
- return buffer;
12
+ return buffer;
3
13
  }
14
+ /**
15
+ * Convert (copy) ArrayBuffer to Buffer
16
+ */
4
17
  export function toBuffer(binaryData) {
5
- throw new Error('Buffer not supported in browser');
18
+ throw new Error('Buffer not supported in browser');
6
19
  }
7
- //# sourceMappingURL=buffer.browser.js.map
@@ -1,20 +1,34 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // Isolates Buffer references to ensure they are only bundled under Node.js (avoids big webpack polyfill)
5
+ // this file is selected by the package.json "browser" field).
6
+ /**
7
+ * Convert Buffer to ArrayBuffer
8
+ * Converts Node.js `Buffer` to `ArrayBuffer` (without triggering bundler to include Buffer polyfill on browser)
9
+ * @todo better data type
10
+ */
1
11
  export function toArrayBuffer(buffer) {
2
- if (Buffer.isBuffer(buffer)) {
3
- const typedArray = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length);
4
- return typedArray.slice().buffer;
5
- }
6
- return buffer;
12
+ // TODO - per docs we should just be able to call buffer.buffer, but there are issues
13
+ if (Buffer.isBuffer(buffer)) {
14
+ const typedArray = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length);
15
+ return typedArray.slice().buffer;
16
+ }
17
+ return buffer;
7
18
  }
19
+ /**
20
+ * Convert (copy) ArrayBuffer to Buffer
21
+ */
8
22
  export function toBuffer(binaryData) {
9
- if (Buffer.isBuffer(binaryData)) {
10
- return binaryData;
11
- }
12
- if (ArrayBuffer.isView(binaryData)) {
13
- binaryData = binaryData.buffer;
14
- }
15
- if (typeof Buffer !== 'undefined' && binaryData instanceof ArrayBuffer) {
16
- return Buffer.from(binaryData);
17
- }
18
- throw new Error('toBuffer');
23
+ if (Buffer.isBuffer(binaryData)) {
24
+ return binaryData;
25
+ }
26
+ if (ArrayBuffer.isView(binaryData)) {
27
+ binaryData = binaryData.buffer;
28
+ }
29
+ // TODO - move to loaders.gl/polyfills
30
+ if (typeof Buffer !== 'undefined' && binaryData instanceof ArrayBuffer) {
31
+ return Buffer.from(binaryData);
32
+ }
33
+ throw new Error('toBuffer');
19
34
  }
20
- //# sourceMappingURL=buffer.js.map
@@ -1,2 +1 @@
1
1
  export const isSupported = false;
2
- //# sourceMappingURL=fs.browser.js.map
@@ -1,10 +1,16 @@
1
+ // @loaders.gl, MIT license
2
+ /**
3
+ * Typesafe promisify implementation
4
+ * @link https://dev.to/_gdelgado/implement-a-type-safe-version-of-node-s-promisify-in-7-lines-of-code-in-typescript-2j34
5
+ * @param fn
6
+ * @returns
7
+ */
1
8
  export function promisify1(fn) {
2
- return args => new Promise((resolve, reject) => fn(args, (error, callbackArgs) => error ? reject(error) : resolve(callbackArgs)));
9
+ return (args) => new Promise((resolve, reject) => fn(args, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))));
3
10
  }
4
11
  export function promisify2(fn) {
5
- return (arg1, arg2) => new Promise((resolve, reject) => fn(arg1, arg2, (error, callbackArgs) => error ? reject(error) : resolve(callbackArgs)));
12
+ return (arg1, arg2) => new Promise((resolve, reject) => fn(arg1, arg2, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))));
6
13
  }
7
14
  export function promisify3(fn) {
8
- return (arg1, arg2, arg3) => new Promise((resolve, reject) => fn(arg1, arg2, arg3, (error, callbackArgs) => error ? reject(error) : resolve(callbackArgs)));
15
+ return (arg1, arg2, arg3) => new Promise((resolve, reject) => fn(arg1, arg2, arg3, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))));
9
16
  }
10
- //# sourceMappingURL=promisify.js.map
@@ -1,2 +1 @@
1
1
  export const isSupported = false;
2
- //# sourceMappingURL=stream.browser.js.map
@@ -1,4 +1,7 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import * as stream from 'stream';
5
+ /** Wrapper for Node.js stream method */
2
6
  export const Transform = stream.Transform;
3
7
  export const isSupported = Boolean(stream);
4
- //# sourceMappingURL=stream.js.map
@@ -1,4 +1,4 @@
1
- import { LoaderOptions } from '../../loader-types';
1
+ import { LoaderOptions } from "../../loader-types.js";
2
2
  /**
3
3
  *
4
4
  * @param baseOptions Can be undefined, in which case a fresh options object will be minted
@@ -1 +1 @@
1
- {"version":3,"file":"merge-loader-options.d.ts","sourceRoot":"","sources":["../../../src/lib/option-utils/merge-loader-options.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,aAAa,EAAC,MAAM,oBAAoB,CAAC;AAEjD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,SAAS,aAAa,EAC9D,WAAW,EAAE,OAAO,GAAG,SAAS,EAChC,UAAU,EAAE,OAAO,GAClB,OAAO,CAET"}
1
+ {"version":3,"file":"merge-loader-options.d.ts","sourceRoot":"","sources":["../../../src/lib/option-utils/merge-loader-options.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,aAAa,EAAC,8BAA2B;AAEjD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,SAAS,aAAa,EAC9D,WAAW,EAAE,OAAO,GAAG,SAAS,EAChC,UAAU,EAAE,OAAO,GAClB,OAAO,CAET"}
@@ -1,21 +1,29 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ /**
5
+ *
6
+ * @param baseOptions Can be undefined, in which case a fresh options object will be minted
7
+ * @param newOptions
8
+ * @returns
9
+ */
1
10
  export function mergeLoaderOptions(baseOptions, newOptions) {
2
- return mergeOptionsRecursively(baseOptions || {}, newOptions);
11
+ return mergeOptionsRecursively(baseOptions || {}, newOptions);
3
12
  }
4
- function mergeOptionsRecursively(baseOptions, newOptions) {
5
- let level = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
6
- if (level > 3) {
7
- return newOptions;
8
- }
9
- const options = {
10
- ...baseOptions
11
- };
12
- for (const [key, newValue] of Object.entries(newOptions)) {
13
- if (newValue && typeof newValue === 'object' && !Array.isArray(newValue)) {
14
- options[key] = mergeOptionsRecursively(options[key] || {}, newOptions[key], level + 1);
15
- } else {
16
- options[key] = newOptions[key];
13
+ function mergeOptionsRecursively(baseOptions, newOptions, level = 0) {
14
+ // Sanity check (jest test runner overwrites the console object which can lead to infinite recursion)
15
+ if (level > 3) {
16
+ return newOptions;
17
17
  }
18
- }
19
- return options;
18
+ const options = { ...baseOptions };
19
+ for (const [key, newValue] of Object.entries(newOptions)) {
20
+ if (newValue && typeof newValue === 'object' && !Array.isArray(newValue)) {
21
+ options[key] = mergeOptionsRecursively(options[key] || {}, newOptions[key], level + 1);
22
+ // Object.assign(options[key] as object, newOptions[key]);
23
+ }
24
+ else {
25
+ options[key] = newOptions[key];
26
+ }
27
+ }
28
+ return options;
20
29
  }
21
- //# sourceMappingURL=merge-loader-options.js.map
@@ -1,9 +1,12 @@
1
1
  import { getFirstCharacters } from "../binary-utils/get-first-characters.js";
2
+ /**
3
+ * Minimal JSON parser that throws more meaningful error messages
4
+ */
2
5
  export function parseJSON(string) {
3
- try {
4
- return JSON.parse(string);
5
- } catch (_) {
6
- throw new Error(`Failed to parse JSON from data starting with "${getFirstCharacters(string)}"`);
7
- }
6
+ try {
7
+ return JSON.parse(string);
8
+ }
9
+ catch (_) {
10
+ throw new Error(`Failed to parse JSON from data starting with "${getFirstCharacters(string)}"`);
11
+ }
8
12
  }
9
- //# sourceMappingURL=parse-json.js.map