@loaders.gl/loader-utils 4.2.0-alpha.3 → 4.2.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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 +20 -14
  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,61 +1,99 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { NodeFileFacade as NodeFile } from "../files/node-file-facade.js";
5
+ /**
6
+ * Provides file data using node fs library
7
+ * @deprecated - will be replaced with ReadableFile
8
+ */
2
9
  export class FileHandleFile {
3
- constructor(path) {
4
- let append = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
5
- this.file = void 0;
6
- this.file = new NodeFile(path, append ? 'a+' : 'r');
7
- }
8
- async truncate(length) {
9
- await this.file.truncate(length);
10
- }
11
- async append(buffer) {
12
- await this.file.append(buffer);
13
- }
14
- async destroy() {
15
- await this.file.close();
16
- }
17
- async getUint8(offset) {
18
- const arrayBuffer = await this.file.read(offset, 1);
19
- const val = new Uint8Array(arrayBuffer).at(0);
20
- if (val === undefined) {
21
- throw new Error('something went wrong');
22
- }
23
- return val;
24
- }
25
- async getUint16(offset) {
26
- const arrayBuffer = await this.file.read(offset, 2);
27
- const val = new Uint16Array(arrayBuffer).at(0);
28
- if (val === undefined) {
29
- throw new Error('something went wrong');
30
- }
31
- return val;
32
- }
33
- async getUint32(offset) {
34
- const arrayBuffer = await this.file.read(offset, 4);
35
- const val = new Uint32Array(arrayBuffer).at(0);
36
- if (val === undefined) {
37
- throw new Error('something went wrong');
38
- }
39
- return val;
40
- }
41
- async getBigUint64(offset) {
42
- const arrayBuffer = await this.file.read(offset, 8);
43
- const val = new BigInt64Array(arrayBuffer).at(0);
44
- if (val === undefined) {
45
- throw new Error('something went wrong');
46
- }
47
- return val;
48
- }
49
- async slice(startOffset, endOffset) {
50
- const bigLength = endOffset - startOffset;
51
- if (bigLength > Number.MAX_SAFE_INTEGER) {
52
- throw new Error('too big slice');
53
- }
54
- const length = Number(bigLength);
55
- return await this.file.read(startOffset, length);
56
- }
57
- get length() {
58
- return this.file.bigsize;
59
- }
10
+ /** Create a new FileHandleFile */
11
+ constructor(path, append = false) {
12
+ this.file = new NodeFile(path, append ? 'a+' : 'r');
13
+ }
14
+ /**
15
+ * Truncates the file descriptor.
16
+ * @param length desired file lenght
17
+ */
18
+ async truncate(length) {
19
+ await this.file.truncate(length);
20
+ }
21
+ /**
22
+ * Append data to a file.
23
+ * @param buffer data to append
24
+ */
25
+ async append(buffer) {
26
+ await this.file.append(buffer);
27
+ }
28
+ /** Close file */
29
+ async destroy() {
30
+ await this.file.close();
31
+ }
32
+ /**
33
+ * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
34
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
35
+ */
36
+ async getUint8(offset) {
37
+ const arrayBuffer = await this.file.read(offset, 1);
38
+ const val = new Uint8Array(arrayBuffer).at(0);
39
+ if (val === undefined) {
40
+ throw new Error('something went wrong');
41
+ }
42
+ return val;
43
+ }
44
+ /**
45
+ * Gets an unsigned 16-bit integer at the specified byte offset from the start of the file.
46
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
47
+ */
48
+ async getUint16(offset) {
49
+ const arrayBuffer = await this.file.read(offset, 2);
50
+ const val = new Uint16Array(arrayBuffer).at(0);
51
+ if (val === undefined) {
52
+ throw new Error('something went wrong');
53
+ }
54
+ return val;
55
+ }
56
+ /**
57
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
58
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
59
+ */
60
+ async getUint32(offset) {
61
+ const arrayBuffer = await this.file.read(offset, 4);
62
+ const val = new Uint32Array(arrayBuffer).at(0);
63
+ if (val === undefined) {
64
+ throw new Error('something went wrong');
65
+ }
66
+ return val;
67
+ }
68
+ /**
69
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
70
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
71
+ */
72
+ async getBigUint64(offset) {
73
+ const arrayBuffer = await this.file.read(offset, 8);
74
+ const val = new BigInt64Array(arrayBuffer).at(0);
75
+ if (val === undefined) {
76
+ throw new Error('something went wrong');
77
+ }
78
+ return val;
79
+ }
80
+ /**
81
+ * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
82
+ * @param startOffset The offset, in byte, from the start of the file where to start reading the data.
83
+ * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
84
+ */
85
+ async slice(startOffset, endOffset) {
86
+ const bigLength = endOffset - startOffset;
87
+ if (bigLength > Number.MAX_SAFE_INTEGER) {
88
+ throw new Error('too big slice');
89
+ }
90
+ const length = Number(bigLength);
91
+ return await this.file.read(startOffset, length);
92
+ }
93
+ /**
94
+ * the length (in bytes) of the data.
95
+ */
96
+ get length() {
97
+ return this.file.bigsize;
98
+ }
60
99
  }
61
- //# sourceMappingURL=file-handle-file.js.map
@@ -1,4 +1,9 @@
1
- export const isFileProvider = fileProvider => {
2
- return (fileProvider === null || fileProvider === void 0 ? void 0 : fileProvider.getUint8) && (fileProvider === null || fileProvider === void 0 ? void 0 : fileProvider.slice) && (fileProvider === null || fileProvider === void 0 ? void 0 : fileProvider.length);
1
+ /**
2
+ * Check is the object has FileProvider members
3
+ * @param fileProvider - tested object
4
+ */
5
+ export const isFileProvider = (fileProvider) => {
6
+ return (fileProvider?.getUint8 &&
7
+ fileProvider?.slice &&
8
+ fileProvider?.length);
3
9
  };
4
- //# sourceMappingURL=file-provider.js.map
@@ -1,4 +1,4 @@
1
- import { ReadableFile } from './file';
1
+ import { ReadableFile } from "./file.js";
2
2
  /**
3
3
  * BlobFile provides a "file like interface" to the data in a Blob or File object
4
4
  */
@@ -1 +1 @@
1
- {"version":3,"file":"blob-file.d.ts","sourceRoot":"","sources":["../../../src/lib/files/blob-file.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,YAAY,EAAC,MAAM,QAAQ,CAAC;AAEpC;;GAEG;AACH,qBAAa,QAAS,YAAW,YAAY;IAC3C,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;gBAET,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,WAAW;IAOrC,KAAK;IAEL,IAAI;;;;;IAQJ,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;CAIhE"}
1
+ {"version":3,"file":"blob-file.d.ts","sourceRoot":"","sources":["../../../src/lib/files/blob-file.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,YAAY,EAAC,kBAAe;AAEpC;;GAEG;AACH,qBAAa,QAAS,YAAW,YAAY;IAC3C,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;gBAET,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,WAAW;IAOrC,KAAK;IAEL,IAAI;;;;;IAQJ,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;CAIhE"}
@@ -1,25 +1,26 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ /**
5
+ * BlobFile provides a "file like interface" to the data in a Blob or File object
6
+ */
1
7
  export class BlobFile {
2
- constructor(blob) {
3
- this.handle = void 0;
4
- this.size = void 0;
5
- this.bigsize = void 0;
6
- this.url = void 0;
7
- this.handle = blob instanceof ArrayBuffer ? new Blob([blob]) : blob;
8
- this.size = blob instanceof ArrayBuffer ? blob.byteLength : blob.size;
9
- this.bigsize = BigInt(this.size);
10
- this.url = blob instanceof File ? blob.name : '';
11
- }
12
- async close() {}
13
- async stat() {
14
- return {
15
- size: this.handle.size,
16
- bigsize: BigInt(this.handle.size),
17
- isDirectory: false
18
- };
19
- }
20
- async read(start, length) {
21
- const arrayBuffer = await this.handle.slice(start, start + length).arrayBuffer();
22
- return arrayBuffer;
23
- }
8
+ constructor(blob) {
9
+ this.handle = blob instanceof ArrayBuffer ? new Blob([blob]) : blob;
10
+ this.size = blob instanceof ArrayBuffer ? blob.byteLength : blob.size;
11
+ this.bigsize = BigInt(this.size);
12
+ this.url = blob instanceof File ? blob.name : '';
13
+ }
14
+ async close() { }
15
+ async stat() {
16
+ return {
17
+ size: this.handle.size,
18
+ bigsize: BigInt(this.handle.size),
19
+ isDirectory: false
20
+ };
21
+ }
22
+ async read(start, length) {
23
+ const arrayBuffer = await this.handle.slice(start, start + length).arrayBuffer();
24
+ return arrayBuffer;
25
+ }
24
26
  }
25
- //# sourceMappingURL=blob-file.js.map
@@ -1,2 +1,4 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  export {};
2
- //# sourceMappingURL=file.js.map
@@ -1,4 +1,4 @@
1
- import { ReadableFile, Stat } from './file';
1
+ import { ReadableFile, Stat } from "./file.js";
2
2
  export declare class HttpFile implements ReadableFile {
3
3
  readonly handle: string;
4
4
  readonly size: number;
@@ -1 +1 @@
1
- {"version":3,"file":"http-file.d.ts","sourceRoot":"","sources":["../../../src/lib/files/http-file.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,YAAY,EAAE,IAAI,EAAC,MAAM,QAAQ,CAAC;AAE1C,qBAAa,QAAS,YAAW,YAAY;IAC3C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAK;IAC1B,QA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yright (c) vis.gl contributors
1
4
  export class HttpFile {
2
- constructor(url) {
3
- this.handle = void 0;
4
- this.size = 0;
5
- this.bigsize = 0n;
6
- this.url = void 0;
7
- this.handle = url;
8
- this.url = url;
9
- }
10
- async close() {}
11
- async stat() {
12
- const response = await fetch(this.handle, {
13
- method: 'HEAD'
14
- });
15
- if (!response.ok) {
16
- throw new Error(`Failed to fetch HEAD ${this.handle}`);
5
+ constructor(url) {
6
+ this.size = 0;
7
+ this.bigsize = 0n;
8
+ this.handle = url;
9
+ this.url = url;
17
10
  }
18
- const size = parseInt(response.headers.get('Content-Length') || '0');
19
- return {
20
- size,
21
- bigsize: BigInt(size),
22
- isDirectory: false
23
- };
24
- }
25
- async read(offset, length) {
26
- const response = await this.fetchRange(offset, length);
27
- const arrayBuffer = await response.arrayBuffer();
28
- return arrayBuffer;
29
- }
30
- async fetchRange(offset, length, signal) {
31
- const nOffset = Number(offset);
32
- const nLength = Number(length);
33
- let controller;
34
- if (!signal) {
35
- controller = new AbortController();
36
- signal = controller.signal;
11
+ async close() { }
12
+ async stat() {
13
+ const response = await fetch(this.handle, { method: 'HEAD' });
14
+ if (!response.ok) {
15
+ throw new Error(`Failed to fetch HEAD ${this.handle}`);
16
+ }
17
+ const size = parseInt(response.headers.get('Content-Length') || '0');
18
+ return {
19
+ size,
20
+ bigsize: BigInt(size),
21
+ isDirectory: false
22
+ };
23
+ }
24
+ async read(offset, length) {
25
+ const response = await this.fetchRange(offset, length);
26
+ const arrayBuffer = await response.arrayBuffer();
27
+ return arrayBuffer;
37
28
  }
38
- const url = this.handle;
39
- let response = await fetch(url, {
40
- signal,
41
- headers: {
42
- Range: `bytes=${nOffset}-${nOffset + nLength - 1}`
43
- }
44
- });
45
- switch (response.status) {
46
- case 206:
47
- break;
48
- case 200:
49
- const contentLength = response.headers.get('Content-Length');
50
- if (!contentLength || Number(contentLength) > length) {
51
- if (controller) {
52
- controller.abort();
53
- }
54
- throw Error('content-length header missing or exceeding request. Server must support HTTP Byte Serving.');
29
+ /**
30
+ *
31
+ * @param offset
32
+ * @param length
33
+ * @param signal
34
+ * @returns
35
+ * @see https://github.com/protomaps/PMTiles
36
+ */
37
+ // eslint-disable-next-line complexity
38
+ async fetchRange(offset, length, signal) {
39
+ const nOffset = Number(offset);
40
+ const nLength = Number(length);
41
+ let controller;
42
+ if (!signal) {
43
+ // ToDO why is it so important to abort in case 200?
44
+ // TODO check this works or assert 206
45
+ controller = new AbortController();
46
+ signal = controller.signal;
55
47
  }
56
- case 416:
57
- if (offset === 0) {
58
- const contentRange = response.headers.get('Content-Range');
59
- if (!contentRange || !contentRange.startsWith('bytes *')) {
60
- throw Error('Missing content-length on 416 response');
61
- }
62
- const actualLength = Number(contentRange.substr(8));
63
- response = await fetch(this.url, {
48
+ const url = this.handle;
49
+ let response = await fetch(url, {
64
50
  signal,
65
- headers: {
66
- Range: `bytes=0-${actualLength - 1}`
67
- }
68
- });
69
- }
70
- break;
71
- default:
72
- if (response.status >= 300) {
73
- throw Error(`Bad response code: ${response.status}`);
51
+ headers: { Range: `bytes=${nOffset}-${nOffset + nLength - 1}` }
52
+ });
53
+ switch (response.status) {
54
+ case 206: // Partial Content success
55
+ // This is the expected success code for a range request
56
+ break;
57
+ case 200:
58
+ // some well-behaved backends, e.g. DigitalOcean CDN, respond with 200 instead of 206
59
+ // but we also need to detect no support for Byte Serving which is returning the whole file
60
+ const contentLength = response.headers.get('Content-Length');
61
+ if (!contentLength || Number(contentLength) > length) {
62
+ if (controller) {
63
+ controller.abort();
64
+ }
65
+ throw Error('content-length header missing or exceeding request. Server must support HTTP Byte Serving.');
66
+ }
67
+ // @eslint-disable-next-line no-fallthrough
68
+ case 416: // "Range Not Satisfiable"
69
+ // some HTTP servers don't accept ranges beyond the end of the resource.
70
+ // Retry with the exact length
71
+ // TODO: can return 416 with offset > 0 if content changed, which will have a blank etag.
72
+ // See https://github.com/protomaps/PMTiles/issues/90
73
+ if (offset === 0) {
74
+ const contentRange = response.headers.get('Content-Range');
75
+ if (!contentRange || !contentRange.startsWith('bytes *')) {
76
+ throw Error('Missing content-length on 416 response');
77
+ }
78
+ const actualLength = Number(contentRange.substr(8));
79
+ response = await fetch(this.url, {
80
+ signal,
81
+ headers: { Range: `bytes=0-${actualLength - 1}` }
82
+ });
83
+ }
84
+ break;
85
+ default:
86
+ if (response.status >= 300) {
87
+ throw Error(`Bad response code: ${response.status}`);
88
+ }
74
89
  }
90
+ return response;
91
+ // const data = await response.arrayBuffer();
92
+ // return {
93
+ // data,
94
+ // etag: response.headers.get('ETag') || undefined,
95
+ // cacheControl: response.headers.get('Cache-Control') || undefined,
96
+ // expires: response.headers.get('Expires') || undefined
97
+ // };
75
98
  }
76
- return response;
77
- }
78
99
  }
79
- //# sourceMappingURL=http-file.js.map
@@ -1,4 +1,4 @@
1
- import { ReadableFile, WritableFile, Stat } from './file';
1
+ import { ReadableFile, WritableFile, Stat } from "./file.js";
2
2
  /** This class is a facade that gets replaced with an actual NodeFile instance */
3
3
  export declare class NodeFileFacade implements ReadableFile, WritableFile {
4
4
  handle: unknown;
@@ -1 +1 @@
1
- {"version":3,"file":"node-file-facade.d.ts","sourceRoot":"","sources":["../../../src/lib/files/node-file-facade.ts"],"names":[],"mappings":"AAKA,OAAO,EAAC,YAAY,EAAE,YAAY,EAAE,IAAI,EAAC,MAAM,QAAQ,CAAC;AAIxD,kFAAkF;AAClF,qBAAa,cAAe,YAAW,YAAY,EAAE,YAAY;IAC/D,MAAM,EAAE,OAAO,CAAC;IAChB,IAAI,EAAE,MAAM,CAAK;IACjB,OAAO,EAAE,MAAM,CAAM;IACrB,GAAG,EAAE,MAAM,CAAM;gBAEL,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,EAAE,IAAI,CAAC,EAAE,MAAM;IAUvE,gBAAgB;IACV,IAAI,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,G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OAAO,CAAC,MAAM,CAAC;IAG1G,iCAAiC;IAC3B,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3B,iEAAiE;IAC3D,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI7C,yDAAyD;IACnD,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAG7C,qBAAqB;IACf,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAC7B"}
@@ -1,35 +1,43 @@
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
+ /** This class is a facade that gets replaced with an actual NodeFile instance */
3
7
  export class NodeFileFacade {
4
- constructor(url, flags, mode) {
5
- var _globalThis$loaders;
6
- this.handle = void 0;
7
- this.size = 0;
8
- this.bigsize = 0n;
9
- this.url = '';
10
- if ((_globalThis$loaders = globalThis.loaders) !== null && _globalThis$loaders !== void 0 && _globalThis$loaders.NodeFile) {
11
- return new globalThis.loaders.NodeFile(url, flags, mode);
8
+ constructor(url, flags, mode) {
9
+ this.size = 0;
10
+ this.bigsize = 0n;
11
+ this.url = '';
12
+ // Return the actual implementation instance
13
+ if (globalThis.loaders?.NodeFile) {
14
+ return new globalThis.loaders.NodeFile(url, flags, mode);
15
+ }
16
+ if (isBrowser) {
17
+ throw new Error('Can\'t instantiate NodeFile in browser.');
18
+ }
19
+ throw new Error('Can\'t instantiate NodeFile. Make sure to import @loaders.gl/polyfills first.');
12
20
  }
13
- if (isBrowser) {
14
- throw new Error('Can\'t instantiate NodeFile in browser.');
21
+ /** Read data */
22
+ async read(start, end) {
23
+ throw NOT_IMPLEMENTED;
15
24
  }
16
- throw new Error('Can\'t instantiate NodeFile. Make sure to import @loaders.gl/polyfills first.');
17
- }
18
- async read(start, end) {
19
- throw NOT_IMPLEMENTED;
20
- }
21
- async write(arrayBuffer, offset, length) {
22
- throw NOT_IMPLEMENTED;
23
- }
24
- async stat() {
25
- throw NOT_IMPLEMENTED;
26
- }
27
- async truncate(length) {
28
- throw NOT_IMPLEMENTED;
29
- }
30
- async append(data) {
31
- throw NOT_IMPLEMENTED;
32
- }
33
- async close() {}
25
+ /** Write to file. The number of bytes written will be returned */
26
+ async write(arrayBuffer, offset, length) {
27
+ throw NOT_IMPLEMENTED;
28
+ }
29
+ /** Get information about file */
30
+ async stat() {
31
+ throw NOT_IMPLEMENTED;
32
+ }
33
+ /** Truncates the file descriptor. Only available on NodeFile. */
34
+ async truncate(length) {
35
+ throw NOT_IMPLEMENTED;
36
+ }
37
+ /** Append data to a file. Only available on NodeFile. */
38
+ async append(data) {
39
+ throw NOT_IMPLEMENTED;
40
+ }
41
+ /** Close the file */
42
+ async close() { }
34
43
  }
35
- //# sourceMappingURL=node-file-facade.js.map