@loaders.gl/loader-utils 4.0.0-alpha.9 → 4.0.0-beta.1

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 (221) hide show
  1. package/dist/es5/index.js +63 -2
  2. package/dist/es5/index.js.map +1 -1
  3. package/dist/es5/json-loader.js +1 -1
  4. package/dist/es5/json-loader.js.map +1 -1
  5. package/dist/es5/lib/file-provider/data-view-file.js +146 -0
  6. package/dist/es5/lib/file-provider/data-view-file.js.map +1 -0
  7. package/dist/es5/lib/file-provider/file-handle-file.js +236 -0
  8. package/dist/es5/lib/file-provider/file-handle-file.js.map +1 -0
  9. package/dist/es5/lib/file-provider/file-handle.js +98 -0
  10. package/dist/es5/lib/file-provider/file-handle.js.map +1 -0
  11. package/dist/es5/lib/file-provider/file-provider.js +11 -0
  12. package/dist/es5/lib/file-provider/file-provider.js.map +1 -0
  13. package/dist/es5/lib/filesystems/filesystem.js +2 -0
  14. package/dist/es5/lib/filesystems/filesystem.js.map +1 -0
  15. package/dist/es5/lib/filesystems/node-filesystem.browser.js +172 -0
  16. package/dist/es5/lib/filesystems/node-filesystem.browser.js.map +1 -0
  17. package/dist/es5/lib/filesystems/node-filesystem.js +2 -2
  18. package/dist/es5/lib/filesystems/node-filesystem.js.map +1 -1
  19. package/dist/es5/lib/node/fs.browser.js +9 -0
  20. package/dist/es5/lib/node/fs.browser.js.map +1 -0
  21. package/dist/es5/lib/node/fs.js +28 -34
  22. package/dist/es5/lib/node/fs.js.map +1 -1
  23. package/dist/es5/lib/node/stream.browser.js +9 -0
  24. package/dist/es5/lib/node/stream.browser.js.map +1 -0
  25. package/dist/es5/lib/node/stream.js +6 -7
  26. package/dist/es5/lib/node/stream.js.map +1 -1
  27. package/dist/es5/lib/option-utils/merge-loader-options.js +1 -1
  28. package/dist/es5/lib/option-utils/merge-loader-options.js.map +1 -1
  29. package/dist/es5/lib/sources/data-source.js +67 -0
  30. package/dist/es5/lib/sources/data-source.js.map +1 -0
  31. package/dist/es5/lib/sources/image-source.js +31 -0
  32. package/dist/es5/lib/sources/image-source.js.map +1 -0
  33. package/dist/es5/lib/sources/image-tile-source.js +2 -0
  34. package/dist/es5/lib/sources/image-tile-source.js.map +1 -0
  35. package/dist/es5/lib/sources/tile-source.js +2 -0
  36. package/dist/es5/lib/sources/tile-source.js.map +1 -0
  37. package/dist/es5/lib/sources/utils/image-type.js +2 -0
  38. package/dist/es5/lib/sources/utils/image-type.js.map +1 -0
  39. package/dist/es5/lib/sources/utils/utils.js +36 -0
  40. package/dist/es5/lib/sources/utils/utils.js.map +1 -0
  41. package/dist/es5/lib/sources/vector-tile-source.js +2 -0
  42. package/dist/es5/lib/sources/vector-tile-source.js.map +1 -0
  43. package/dist/es5/lib/worker-loader-utils/create-loader-worker.js +2 -2
  44. package/dist/es5/lib/worker-loader-utils/create-loader-worker.js.map +1 -1
  45. package/dist/es5/lib/worker-loader-utils/encode-with-worker.js.map +1 -1
  46. package/dist/es5/lib/worker-loader-utils/parse-with-worker.js.map +1 -1
  47. package/dist/es5/loader-types.js +58 -0
  48. package/dist/es5/loader-types.js.map +1 -0
  49. package/dist/es5/service-types.js +2 -0
  50. package/dist/es5/service-types.js.map +1 -0
  51. package/dist/es5/types.js.map +1 -1
  52. package/dist/es5/writer-types.js +2 -0
  53. package/dist/es5/writer-types.js.map +1 -0
  54. package/dist/esm/index.js +8 -1
  55. package/dist/esm/index.js.map +1 -1
  56. package/dist/esm/json-loader.js +1 -1
  57. package/dist/esm/json-loader.js.map +1 -1
  58. package/dist/esm/lib/file-provider/data-view-file.js +33 -0
  59. package/dist/esm/lib/file-provider/data-view-file.js.map +1 -0
  60. package/dist/esm/lib/file-provider/file-handle-file.js +59 -0
  61. package/dist/esm/lib/file-provider/file-handle-file.js.map +1 -0
  62. package/dist/esm/lib/file-provider/file-handle.js +33 -0
  63. package/dist/esm/lib/file-provider/file-handle.js.map +1 -0
  64. package/dist/esm/lib/file-provider/file-provider.js +4 -0
  65. package/dist/esm/lib/file-provider/file-provider.js.map +1 -0
  66. package/dist/esm/lib/filesystems/filesystem.js +2 -0
  67. package/dist/esm/lib/filesystems/filesystem.js.map +1 -0
  68. package/dist/esm/lib/filesystems/node-filesystem.browser.js +42 -0
  69. package/dist/esm/lib/filesystems/node-filesystem.browser.js.map +1 -0
  70. package/dist/esm/lib/filesystems/node-filesystem.js +1 -1
  71. package/dist/esm/lib/filesystems/node-filesystem.js.map +1 -1
  72. package/dist/esm/lib/node/fs.browser.js +2 -0
  73. package/dist/esm/lib/node/fs.browser.js.map +1 -0
  74. package/dist/esm/lib/node/fs.js +16 -27
  75. package/dist/esm/lib/node/fs.js.map +1 -1
  76. package/dist/esm/lib/node/stream.browser.js +2 -0
  77. package/dist/esm/lib/node/stream.browser.js.map +1 -0
  78. package/dist/esm/lib/node/stream.js +2 -5
  79. package/dist/esm/lib/node/stream.js.map +1 -1
  80. package/dist/esm/lib/option-utils/merge-loader-options.js +1 -1
  81. package/dist/esm/lib/option-utils/merge-loader-options.js.map +1 -1
  82. package/dist/esm/lib/sources/data-source.js +43 -0
  83. package/dist/esm/lib/sources/data-source.js.map +1 -0
  84. package/dist/esm/lib/sources/image-source.js +6 -0
  85. package/dist/esm/lib/sources/image-source.js.map +1 -0
  86. package/dist/esm/lib/sources/image-tile-source.js +2 -0
  87. package/dist/esm/lib/sources/image-tile-source.js.map +1 -0
  88. package/dist/esm/lib/sources/tile-source.js +2 -0
  89. package/dist/esm/lib/sources/tile-source.js.map +1 -0
  90. package/dist/esm/lib/sources/utils/image-type.js +2 -0
  91. package/dist/esm/lib/sources/utils/image-type.js.map +1 -0
  92. package/dist/esm/lib/sources/utils/utils.js +21 -0
  93. package/dist/esm/lib/sources/utils/utils.js.map +1 -0
  94. package/dist/esm/lib/sources/vector-tile-source.js +2 -0
  95. package/dist/esm/lib/sources/vector-tile-source.js.map +1 -0
  96. package/dist/esm/lib/worker-loader-utils/create-loader-worker.js +2 -2
  97. package/dist/esm/lib/worker-loader-utils/create-loader-worker.js.map +1 -1
  98. package/dist/esm/lib/worker-loader-utils/encode-with-worker.js.map +1 -1
  99. package/dist/esm/lib/worker-loader-utils/parse-with-worker.js.map +1 -1
  100. package/dist/esm/loader-types.js +16 -0
  101. package/dist/esm/loader-types.js.map +1 -0
  102. package/dist/esm/service-types.js +2 -0
  103. package/dist/esm/service-types.js.map +1 -0
  104. package/dist/esm/types.js.map +1 -1
  105. package/dist/esm/writer-types.js +2 -0
  106. package/dist/esm/writer-types.js.map +1 -0
  107. package/dist/index.d.ts +22 -2
  108. package/dist/index.d.ts.map +1 -1
  109. package/dist/json-loader.d.ts +1 -1
  110. package/dist/json-loader.d.ts.map +1 -1
  111. package/dist/lib/file-provider/data-view-file.d.ts +37 -0
  112. package/dist/lib/file-provider/data-view-file.d.ts.map +1 -0
  113. package/dist/lib/file-provider/file-handle-file.d.ts +53 -0
  114. package/dist/lib/file-provider/file-handle-file.d.ts.map +1 -0
  115. package/dist/lib/file-provider/file-handle.d.ts +40 -0
  116. package/dist/lib/file-provider/file-handle.d.ts.map +1 -0
  117. package/dist/lib/file-provider/file-provider.d.ts +45 -0
  118. package/dist/lib/file-provider/file-provider.d.ts.map +1 -0
  119. package/dist/lib/filesystems/filesystem.d.ts +80 -0
  120. package/dist/lib/filesystems/filesystem.d.ts.map +1 -0
  121. package/dist/lib/filesystems/node-filesystem.browser.d.ts +39 -0
  122. package/dist/lib/filesystems/node-filesystem.browser.d.ts.map +1 -0
  123. package/dist/lib/filesystems/node-filesystem.d.ts +3 -3
  124. package/dist/lib/filesystems/node-filesystem.d.ts.map +1 -1
  125. package/dist/lib/node/fs.browser.d.ts +2 -0
  126. package/dist/lib/node/fs.browser.d.ts.map +1 -0
  127. package/dist/lib/node/fs.d.ts +19 -14
  128. package/dist/lib/node/fs.d.ts.map +1 -1
  129. package/dist/lib/node/stream.browser.d.ts +2 -0
  130. package/dist/lib/node/stream.browser.d.ts.map +1 -0
  131. package/dist/lib/node/stream.d.ts +4 -1
  132. package/dist/lib/node/stream.d.ts.map +1 -1
  133. package/dist/lib/option-utils/merge-loader-options.d.ts +1 -1
  134. package/dist/lib/option-utils/merge-loader-options.d.ts.map +1 -1
  135. package/dist/lib/sources/data-source.d.ts +33 -0
  136. package/dist/lib/sources/data-source.d.ts.map +1 -0
  137. package/dist/lib/sources/image-source.d.ts +81 -0
  138. package/dist/lib/sources/image-source.d.ts.map +1 -0
  139. package/dist/lib/sources/image-tile-source.d.ts +11 -0
  140. package/dist/lib/sources/image-tile-source.d.ts.map +1 -0
  141. package/dist/lib/sources/tile-source.d.ts +105 -0
  142. package/dist/lib/sources/tile-source.d.ts.map +1 -0
  143. package/dist/lib/sources/utils/image-type.d.ts +10 -0
  144. package/dist/lib/sources/utils/image-type.d.ts.map +1 -0
  145. package/dist/lib/sources/utils/utils.d.ts +13 -0
  146. package/dist/lib/sources/utils/utils.d.ts.map +1 -0
  147. package/dist/lib/sources/vector-tile-source.d.ts +10 -0
  148. package/dist/lib/sources/vector-tile-source.d.ts.map +1 -0
  149. package/dist/lib/worker-loader-utils/create-loader-worker.d.ts +1 -1
  150. package/dist/lib/worker-loader-utils/create-loader-worker.d.ts.map +1 -1
  151. package/dist/lib/worker-loader-utils/encode-with-worker.d.ts +1 -1
  152. package/dist/lib/worker-loader-utils/encode-with-worker.d.ts.map +1 -1
  153. package/dist/lib/worker-loader-utils/parse-with-worker.d.ts +2 -2
  154. package/dist/lib/worker-loader-utils/parse-with-worker.d.ts.map +1 -1
  155. package/dist/loader-types.d.ts +209 -0
  156. package/dist/loader-types.d.ts.map +1 -0
  157. package/dist/service-types.d.ts +10 -0
  158. package/dist/service-types.d.ts.map +1 -0
  159. package/dist/types.d.ts +1 -240
  160. package/dist/types.d.ts.map +1 -1
  161. package/dist/writer-types.d.ts +34 -0
  162. package/dist/writer-types.d.ts.map +1 -0
  163. package/package.json +9 -5
  164. package/src/index.ts +57 -17
  165. package/src/json-loader.ts +1 -1
  166. package/src/lib/file-provider/data-view-file.ts +72 -0
  167. package/src/lib/file-provider/file-handle-file.ts +116 -0
  168. package/src/lib/file-provider/file-handle.ts +79 -0
  169. package/src/lib/file-provider/file-provider.ts +56 -0
  170. package/src/lib/filesystems/filesystem.ts +87 -0
  171. package/src/lib/filesystems/node-filesystem.browser.ts +61 -0
  172. package/src/lib/filesystems/node-filesystem.ts +4 -4
  173. package/src/lib/node/fs.browser.ts +1 -0
  174. package/src/lib/node/fs.ts +20 -50
  175. package/src/lib/node/stream.browser.ts +1 -0
  176. package/src/lib/node/stream.ts +3 -11
  177. package/src/lib/option-utils/merge-loader-options.ts +2 -2
  178. package/src/lib/sources/data-source.ts +74 -0
  179. package/src/lib/sources/image-source.ts +95 -0
  180. package/src/lib/sources/image-tile-source.ts +14 -0
  181. package/src/lib/sources/tile-source.ts +101 -0
  182. package/src/lib/sources/utils/image-type.ts +10 -0
  183. package/src/lib/sources/utils/utils.ts +42 -0
  184. package/src/lib/sources/vector-tile-source.ts +13 -0
  185. package/src/lib/worker-loader-utils/create-loader-worker.ts +20 -4
  186. package/src/lib/worker-loader-utils/encode-with-worker.ts +1 -1
  187. package/src/lib/worker-loader-utils/parse-with-worker.ts +2 -2
  188. package/src/loader-types.ts +367 -0
  189. package/src/service-types.ts +12 -0
  190. package/src/types.ts +2 -351
  191. package/src/writer-types.ts +56 -0
  192. package/dist/index.js +0 -122
  193. package/dist/json-loader.js +0 -27
  194. package/dist/lib/binary-utils/array-buffer-utils.js +0 -84
  195. package/dist/lib/binary-utils/dataview-copy-utils.js +0 -97
  196. package/dist/lib/binary-utils/get-first-characters.js +0 -45
  197. package/dist/lib/binary-utils/memory-conversion-utils.js +0 -73
  198. package/dist/lib/binary-utils/memory-copy-utils.js +0 -61
  199. package/dist/lib/env-utils/assert.js +0 -13
  200. package/dist/lib/env-utils/globals.js +0 -30
  201. package/dist/lib/filesystems/node-filesystem.js +0 -73
  202. package/dist/lib/filesystems/readable-file.js +0 -25
  203. package/dist/lib/filesystems/writable-file.js +0 -48
  204. package/dist/lib/iterators/async-iteration.js +0 -53
  205. package/dist/lib/iterators/text-iterators.js +0 -61
  206. package/dist/lib/node/buffer.browser.js +0 -22
  207. package/dist/lib/node/buffer.js +0 -36
  208. package/dist/lib/node/fs.js +0 -49
  209. package/dist/lib/node/promisify.js +0 -22
  210. package/dist/lib/node/stream.js +0 -17
  211. package/dist/lib/option-utils/merge-loader-options.js +0 -27
  212. package/dist/lib/parser-utils/parse-json.js +0 -16
  213. package/dist/lib/path-utils/file-aliases.js +0 -47
  214. package/dist/lib/path-utils/get-cwd.js +0 -12
  215. package/dist/lib/path-utils/path.js +0 -178
  216. package/dist/lib/request-utils/request-scheduler.js +0 -142
  217. package/dist/lib/worker-loader-utils/create-loader-worker.js +0 -98
  218. package/dist/lib/worker-loader-utils/encode-with-worker.js +0 -21
  219. package/dist/lib/worker-loader-utils/parse-with-worker.js +0 -81
  220. package/dist/types.js +0 -3
  221. package/dist/workers/json-worker.js +0 -5
package/src/index.ts CHANGED
@@ -1,32 +1,41 @@
1
+ // loaders.gl, MIT license
2
+
1
3
  // TYPES
4
+
2
5
  export type {
3
- // loaders
4
- Loader,
5
- LoaderWithParser,
6
- LoaderContext,
7
- LoaderOptions,
8
- LoaderOptionsType,
9
- LoaderReturnType,
10
- LoaderBatchType,
11
- // writers
12
- Writer,
13
- WriterOptions,
14
- WriterOptionsType,
15
6
  // misc
16
7
  DataType,
17
8
  SyncDataType,
18
9
  BatchableDataType,
19
- IFileSystem,
20
- IRandomAccessReadFileSystem,
21
10
  // numeric array types
22
11
  TypedArray,
23
12
  BigTypedArray,
24
13
  TypedArrayConstructor,
25
14
  BigTypedArrayConstructor,
26
15
  NumberArray,
27
- NumericArray
16
+ NumericArray,
17
+ // fetch
18
+ FetchLike
28
19
  } from './types';
29
20
 
21
+ // loaders
22
+
23
+ export type {
24
+ Loader,
25
+ LoaderWithParser,
26
+ LoaderContext,
27
+ LoaderOptions,
28
+ LoaderOptionsType,
29
+ LoaderReturnType,
30
+ LoaderBatchType
31
+ } from './loader-types';
32
+
33
+ export {parseFromContext, parseSyncFromContext, parseInBatchesFromContext} from './loader-types';
34
+
35
+ // writers
36
+
37
+ export type {Writer, WriterOptions, WriterOptionsType} from './writer-types';
38
+
30
39
  // GENERAL UTILS
31
40
  export {assert} from './lib/env-utils/assert';
32
41
  export {
@@ -109,11 +118,42 @@ export {fs};
109
118
  import * as stream from './lib/node/stream';
110
119
  export {stream};
111
120
 
112
- // EXPERIMENTAL
121
+ // EXPERIMENTAL: FILE SYSTEMS
122
+
123
+ export type {FileSystem, RandomAccessReadFileSystem} from './lib/filesystems/filesystem';
124
+ export {NodeFileSystem as _NodeFileSystem} from './lib/filesystems/node-filesystem';
125
+
126
+ export type {FileProvider} from './lib/file-provider/file-provider';
127
+ export {isFileProvider} from './lib/file-provider/file-provider';
128
+
129
+ export {FileHandle} from './lib/file-provider/file-handle';
130
+ export {FileHandleFile} from './lib/file-provider/file-handle-file';
131
+ export {DataViewFile} from './lib/file-provider/data-view-file';
132
+
113
133
  export type {ReadableFile} from './lib/filesystems/readable-file';
114
134
  export {makeReadableFile} from './lib/filesystems/readable-file';
115
135
 
116
136
  export type {WritableFile} from './lib/filesystems/writable-file';
117
137
  export {makeWritableFile} from './lib/filesystems/writable-file';
118
138
 
119
- export {default as _NodeFileSystem} from './lib/filesystems/node-filesystem';
139
+ // EXPERIMENTAL: DATA SOURCES
140
+ export type {Service} from './service-types';
141
+
142
+ export type {DataSourceProps} from './lib/sources/data-source';
143
+ export {DataSource} from './lib/sources/data-source';
144
+
145
+ export type {ImageType} from './lib/sources/utils/image-type';
146
+ export type {ImageSourceProps, ImageSourceMetadata} from './lib/sources/image-source';
147
+ export type {GetImageParameters} from './lib/sources/image-source';
148
+ export {ImageSource} from './lib/sources/image-source';
149
+
150
+ export type {
151
+ TileSourceProps,
152
+ TileSourceMetadata,
153
+ GetTileParameters
154
+ } from './lib/sources/tile-source';
155
+ export type {TileSource} from './lib/sources/tile-source';
156
+
157
+ export type {ImageTileSource} from './lib/sources/image-tile-source';
158
+
159
+ export type {VectorTileSource} from './lib/sources/vector-tile-source';
@@ -1,4 +1,4 @@
1
- import type {LoaderWithParser, LoaderOptions} from './types';
1
+ import type {LoaderWithParser, LoaderOptions} from './loader-types';
2
2
  import type {Table, TableBatch} from '@loaders.gl/schema';
3
3
 
4
4
  // __VERSION__ is injected by babel-plugin-version-inline
@@ -0,0 +1,72 @@
1
+ import {FileProvider} from './file-provider';
2
+
3
+ /**
4
+ * Checks if bigint can be converted to number and convert it if possible
5
+ * @param bigint bigint to be converted
6
+ * @returns number
7
+ */
8
+ const toNumber = (bigint: bigint) => {
9
+ if (bigint > Number.MAX_SAFE_INTEGER) {
10
+ throw new Error('Offset is out of bounds');
11
+ }
12
+ return Number(bigint);
13
+ };
14
+
15
+ /** Provides file data using DataView */
16
+ export class DataViewFile implements FileProvider {
17
+ /** The DataView from which data is provided */
18
+ private file: DataView;
19
+
20
+ constructor(file: DataView) {
21
+ this.file = file;
22
+ }
23
+
24
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
25
+ async destroy(): Promise<void> {}
26
+
27
+ /**
28
+ * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
29
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
30
+ */
31
+ async getUint8(offset: bigint): Promise<number> {
32
+ return this.file.getUint8(toNumber(offset));
33
+ }
34
+
35
+ /**
36
+ * Gets an unsigned 16-bit intege at the specified byte offset from the start of the file.
37
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
38
+ */
39
+ async getUint16(offset: bigint): Promise<number> {
40
+ return this.file.getUint16(toNumber(offset), true);
41
+ }
42
+
43
+ /**
44
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
45
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
46
+ */
47
+ async getUint32(offset: bigint): Promise<number> {
48
+ return this.file.getUint32(toNumber(offset), true);
49
+ }
50
+
51
+ /**
52
+ * Gets an unsigned 64-bit integer at the specified byte offset from the start of the file.
53
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
54
+ */
55
+ async getBigUint64(offset: bigint): Promise<bigint> {
56
+ return this.file.getBigUint64(toNumber(offset), true);
57
+ }
58
+
59
+ /**
60
+ * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
61
+ * @param startOffset The offset, in bytes, from the start of the file where to start reading the data.
62
+ * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
63
+ */
64
+ async slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer> {
65
+ return this.file.buffer.slice(toNumber(startOffset), toNumber(endOffset));
66
+ }
67
+
68
+ /** the length (in bytes) of the data. */
69
+ get length() {
70
+ return BigInt(this.file.byteLength);
71
+ }
72
+ }
@@ -0,0 +1,116 @@
1
+ import {FileProvider} from './file-provider';
2
+ import {FileHandle} from './file-handle';
3
+ import {resolvePath} from '../path-utils/file-aliases';
4
+
5
+ /**
6
+ * Provides file data using node fs library
7
+ */
8
+ export class FileHandleFile implements FileProvider {
9
+ /**
10
+ * Returns a new copy of FileHandleFile
11
+ * @param path The path to the file in file system
12
+ */
13
+ static async from(path: string): Promise<FileHandleFile> {
14
+ path = resolvePath(path);
15
+ const fileDescriptor = await FileHandle.open(path);
16
+ return new FileHandleFile(fileDescriptor, fileDescriptor.stat.size);
17
+ }
18
+
19
+ /**
20
+ * The FileHandle from which data is provided
21
+ */
22
+ private fileDescriptor: FileHandle;
23
+
24
+ /**
25
+ * The file length in bytes
26
+ */
27
+ private size: bigint;
28
+
29
+ private constructor(fileDescriptor: FileHandle, size: bigint) {
30
+ this.fileDescriptor = fileDescriptor;
31
+ this.size = size;
32
+ }
33
+
34
+ /** Close file */
35
+ async destroy(): Promise<void> {
36
+ await this.fileDescriptor.close();
37
+ }
38
+
39
+ /**
40
+ * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
41
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
42
+ */
43
+ async getUint8(offset: bigint): Promise<number> {
44
+ const val = new Uint8Array(
45
+ (await this.fileDescriptor.read(Buffer.alloc(1), 0, 1, offset)).buffer.buffer
46
+ ).at(0);
47
+ if (val === undefined) {
48
+ throw new Error('something went wrong');
49
+ }
50
+ return val;
51
+ }
52
+
53
+ /**
54
+ * Gets an unsigned 16-bit integer at the specified byte offset from the start of the file.
55
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
56
+ */
57
+ async getUint16(offset: bigint): Promise<number> {
58
+ const val = new Uint16Array(
59
+ (await this.fileDescriptor.read(Buffer.alloc(2), 0, 2, offset)).buffer.buffer
60
+ ).at(0);
61
+ if (val === undefined) {
62
+ throw new Error('something went wrong');
63
+ }
64
+ return val;
65
+ }
66
+
67
+ /**
68
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
69
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
70
+ */
71
+ async getUint32(offset: bigint): Promise<number> {
72
+ const val = new Uint32Array(
73
+ (await this.fileDescriptor.read(Buffer.alloc(4), 0, 4, offset)).buffer.buffer
74
+ ).at(0);
75
+ if (val === undefined) {
76
+ throw new Error('something went wrong');
77
+ }
78
+ return val;
79
+ }
80
+
81
+ /**
82
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
83
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
84
+ */
85
+ async getBigUint64(offset: bigint): Promise<bigint> {
86
+ const val = new BigInt64Array(
87
+ (await this.fileDescriptor.read(Buffer.alloc(8), 0, 8, offset)).buffer.buffer
88
+ ).at(0);
89
+ if (val === undefined) {
90
+ throw new Error('something went wrong');
91
+ }
92
+ return val;
93
+ }
94
+
95
+ /**
96
+ * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
97
+ * @param startOffsset The offset, in byte, from the start of the file where to start reading the data.
98
+ * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
99
+ */
100
+ async slice(startOffsset: bigint, endOffset: bigint): Promise<ArrayBuffer> {
101
+ const bigLength = endOffset - startOffsset;
102
+ if (bigLength > Number.MAX_SAFE_INTEGER) {
103
+ throw new Error('too big slice');
104
+ }
105
+ const length = Number(bigLength);
106
+ return (await this.fileDescriptor.read(Buffer.alloc(length), 0, length, startOffsset)).buffer
107
+ .buffer;
108
+ }
109
+
110
+ /**
111
+ * the length (in bytes) of the data.
112
+ */
113
+ get length(): bigint {
114
+ return this.size;
115
+ }
116
+ }
@@ -0,0 +1,79 @@
1
+ import * as fs from '../node/fs';
2
+
3
+ /** file reading result */
4
+ export type FileReadResult = {
5
+ /** amount of the bytes read */
6
+ bytesRead: number;
7
+ /** the buffer filled with data from file*/
8
+ buffer: Buffer;
9
+ };
10
+
11
+ /** Object handling file info */
12
+ export class FileHandle {
13
+ private fileDescriptor: number;
14
+ private stats: fs.BigIntStats;
15
+
16
+ private constructor(fileDescriptor: number, stats: fs.BigIntStats) {
17
+ this.fileDescriptor = fileDescriptor;
18
+ this.stats = stats;
19
+ }
20
+ /**
21
+ * Opens a `FileHandle`.
22
+ *
23
+ * @param path path to the file
24
+ * @return Fulfills with a {FileHandle} object.
25
+ */
26
+
27
+ static async open(path: string): Promise<FileHandle> {
28
+ const [fd, stats] = await Promise.all([
29
+ new Promise<number>((resolve, reject) => {
30
+ fs.open(path, undefined, undefined, (_err, fd) => (_err ? reject(_err) : resolve(fd)));
31
+ }),
32
+ fs.stat(path, {bigint: true})
33
+ // new Promise<fs.BigIntStats>((resolve, reject) => {
34
+ // console.error(fs.stat)
35
+ // fs.stat(path, {bigint: true}, (_err, stats) => (_err ? reject(_err) : resolve(stats)));
36
+ // console.error(fs.open)
37
+ // })
38
+ ]);
39
+ return new FileHandle(fd, stats);
40
+ }
41
+
42
+ /** Close file */
43
+ async close(): Promise<void> {
44
+ return fs.close(this.fileDescriptor);
45
+ // return new Promise<void>((resolve) => {
46
+ // // @ts-expect-error
47
+ // fs.close(this.fileDescriptor, (_err) => resolve());
48
+ // });
49
+ }
50
+
51
+ /**
52
+ * Reads data from the file and stores that in the given buffer.
53
+ *
54
+ * If the file is not modified concurrently, the end-of-file is reached when the
55
+ * number of bytes read is zero.
56
+ * @param buffer A buffer that will be filled with the file data read.
57
+ * @param offset The location in the buffer at which to start filling.
58
+ * @param length The number of bytes to read.
59
+ * @param position The location where to begin reading data from the file. If `null`, data will be read from the current file position, and the position will be updated. If `position` is an
60
+ * integer, the current file position will remain unchanged.
61
+ * @return Fulfills upon success with a FileReadResult object
62
+ */
63
+ read = (
64
+ buffer: Buffer,
65
+ offset: number,
66
+ length: number,
67
+ position: number | bigint
68
+ ): Promise<FileReadResult> => {
69
+ return new Promise((s) => {
70
+ fs.read(this.fileDescriptor, buffer, offset, length, position, (_err, bytesRead, buffer) =>
71
+ s({bytesRead, buffer})
72
+ );
73
+ });
74
+ };
75
+
76
+ get stat(): fs.BigIntStats {
77
+ return this.stats;
78
+ }
79
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Interface for providing file data
3
+ */
4
+ export interface FileProvider {
5
+ /**
6
+ * Cleanup class data
7
+ */
8
+ destroy(): Promise<void>;
9
+ /**
10
+ * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
11
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
12
+ */
13
+ getUint8(offset: bigint): Promise<number>;
14
+
15
+ /**
16
+ * Gets an unsigned 16-bit integer at the specified byte offset from the start of the file.
17
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
18
+ */
19
+ getUint16(offset: bigint): Promise<number>;
20
+
21
+ /**
22
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
23
+ * @param offset The offset, in bytes, from the file of the view where to read the data.
24
+ */
25
+ getUint32(offset: bigint): Promise<number>;
26
+
27
+ /**
28
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
29
+ * @param offset The offset, in byte, from the file of the view where to read the data.
30
+ */
31
+ getBigUint64(offset: bigint): Promise<bigint>;
32
+
33
+ /**
34
+ * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
35
+ * @param startOffset The offset, in bytes, from the start of the file where to start reading the data.
36
+ * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
37
+ */
38
+ slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer>;
39
+
40
+ /**
41
+ * the length (in bytes) of the data.
42
+ */
43
+ length: bigint;
44
+ }
45
+
46
+ /**
47
+ * Check is the object has FileProvider members
48
+ * @param fileProvider - tested object
49
+ */
50
+ export const isFileProvider = (fileProvider: unknown) => {
51
+ return (
52
+ (fileProvider as FileProvider)?.getUint8 &&
53
+ (fileProvider as FileProvider)?.slice &&
54
+ (fileProvider as FileProvider)?.length
55
+ );
56
+ };
@@ -0,0 +1,87 @@
1
+ // loaders.gl, MIT license
2
+
3
+ export type ReadOptions = {};
4
+
5
+ export type Stat = {
6
+ size: number;
7
+ isDirectory: () => boolean;
8
+ };
9
+
10
+ /**
11
+ * A FileSystem interface can encapsulate various file sources,
12
+ * a FileList, a ZipFile, a GoogleDrive etc.
13
+ */
14
+ export interface FileSystem {
15
+ /**
16
+ * Return a list of file names
17
+ * @param dirname directory name. file system root directory if omitted
18
+ */
19
+ readdir(dirname?: string, options?: {recursive?: boolean}): Promise<string[]>;
20
+
21
+ /**
22
+ * Gets information from a local file from the filesystem
23
+ * @param filename file name to stat
24
+ * @param options currently unused
25
+ * @throws if filename is not in local filesystem
26
+ */
27
+ stat(filename: string, options?: object): Promise<{size: number}>;
28
+
29
+ /**
30
+ * Fetches a local file from the filesystem (or a URL)
31
+ * @param filename
32
+ * @param options
33
+ */
34
+ fetch(filename: RequestInfo, options?: RequestInit): Promise<Response>;
35
+ }
36
+
37
+ /**
38
+ * A random access file system
39
+ */
40
+ export interface RandomAccessReadFileSystem extends FileSystem {
41
+ open(path: string, flags: unknown, mode?: unknown): Promise<any>;
42
+ close(fd: unknown): Promise<void>;
43
+ fstat(fd: unknown): Promise<Stat>;
44
+ read(fd: any, options?: ReadOptions): Promise<{bytesRead: number; buffer: Uint8Array}>;
45
+ // read(
46
+ // fd: any,
47
+ // buffer: ArrayBuffer | ArrayBufferView,
48
+ // offset?: number,
49
+ // length?: number,
50
+ // position?: number
51
+ // ): Promise<{bytesRead: number; buffer: ArrayBuffer}>;
52
+ }
53
+
54
+ /**
55
+ * A FileSystem interface can encapsulate a FileList, a ZipFile, a GoogleDrive etc.
56
+ *
57
+ export interface IFileSystem {
58
+ /**
59
+ * Return a list of file names
60
+ * @param dirname directory name. file system root directory if omitted
61
+ *
62
+ readdir(dirname?: string, options?: {recursive?: boolean}): Promise<string[]>;
63
+
64
+ /**
65
+ * Gets information from a local file from the filesystem
66
+ * @param filename file name to stat
67
+ * @param options currently unused
68
+ * @throws if filename is not in local filesystem
69
+ *
70
+ stat(filename: string, options?: object): Promise<{size: number}>;
71
+
72
+ /**
73
+ * Fetches a local file from the filesystem (or a URL)
74
+ * @param filename
75
+ * @param options
76
+ *
77
+ fetch(filename: string, options?: object): Promise<Response>;
78
+ }
79
+
80
+ type ReadOptions = {buffer?: ArrayBuffer; offset?: number; length?: number; position?: number};
81
+ export interface IRandomAccessReadFileSystem extends IFileSystem {
82
+ open(path: string, flags: string | number, mode?: any): Promise<any>;
83
+ close(fd: any): Promise<void>;
84
+ fstat(fd: any): Promise<object>;
85
+ read(fd: any, options?: ReadOptions): Promise<{bytesRead: number; buffer: Buffer}>;
86
+ }
87
+ */
@@ -0,0 +1,61 @@
1
+ import * as fs from '../node/fs';
2
+ import {FileSystem, RandomAccessReadFileSystem} from './filesystem';
3
+ // import {fetchFile} from "../fetch/fetch-file"
4
+ // import {selectLoader} from "../api/select-loader";
5
+
6
+ type Stat = {
7
+ size: number;
8
+ isDirectory: () => boolean;
9
+ info?: fs.Stats;
10
+ };
11
+
12
+ type ReadOptions = {
13
+ buffer?: Buffer;
14
+ offset?: number;
15
+ length?: number;
16
+ position?: number;
17
+ };
18
+
19
+ /**
20
+ * FileSystem pass-through for Node.js
21
+ * Compatible with BrowserFileSystem.
22
+ * @param options
23
+ */
24
+ export class NodeFileSystem implements FileSystem, RandomAccessReadFileSystem {
25
+ // implements FileSystem
26
+ constructor(options: {[key: string]: any}) {
27
+ throw new Error('Can\'t instantiate NodeFileSystem in browser');
28
+ }
29
+
30
+ async readdir(dirname = '.', options?: {}): Promise<any[]> {
31
+ return [];
32
+ }
33
+
34
+ async stat(path: string, options?: {}): Promise<Stat> {
35
+ return {size: 0, isDirectory: () => false};
36
+ }
37
+
38
+ async fetch(path: string, options: {[key: string]: any}) {
39
+ return globalThis.fetch(path, options);
40
+ }
41
+
42
+ // implements IRandomAccessFileSystem
43
+ async open(path: string, flags: string | number, mode?: any): Promise<number> {
44
+ return 0;
45
+ }
46
+
47
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
48
+ async close(fd: number): Promise<void> {}
49
+
50
+ async fstat(fd: number): Promise<Stat> {
51
+ return {size: 0, isDirectory: () => false};
52
+ }
53
+
54
+ async read(
55
+ fd: number,
56
+ // @ts-ignore Possibly null
57
+ {buffer = null, offset = 0, length = buffer.byteLength, position = null}: ReadOptions
58
+ ): Promise<{bytesRead: number; buffer: Uint8Array}> {
59
+ return {bytesRead: 0, buffer: new Uint8Array(0)};
60
+ }
61
+ }
@@ -1,5 +1,5 @@
1
1
  import * as fs from '../node/fs';
2
- import {IFileSystem, IRandomAccessReadFileSystem} from '../../types';
2
+ import {FileSystem, RandomAccessReadFileSystem} from './filesystem';
3
3
  // import {fetchFile} from "../fetch/fetch-file"
4
4
  // import {selectLoader} from "../api/select-loader";
5
5
 
@@ -21,8 +21,8 @@ type ReadOptions = {
21
21
  * Compatible with BrowserFileSystem.
22
22
  * @param options
23
23
  */
24
- export default class NodeFileSystem implements IFileSystem, IRandomAccessReadFileSystem {
25
- // implements IFileSystem
24
+ export class NodeFileSystem implements FileSystem, RandomAccessReadFileSystem {
25
+ // implements FileSystem
26
26
  constructor(options: {[key: string]: any}) {
27
27
  this.fetch = options._fetch;
28
28
  }
@@ -61,7 +61,7 @@ export default class NodeFileSystem implements IFileSystem, IRandomAccessReadFil
61
61
  fd: number,
62
62
  // @ts-ignore Possibly null
63
63
  {buffer = null, offset = 0, length = buffer.byteLength, position = null}: ReadOptions
64
- ): Promise<{bytesRead: number; buffer: Buffer}> {
64
+ ): Promise<{bytesRead: number; buffer: Uint8Array}> {
65
65
  let totalBytesRead = 0;
66
66
  // Read in loop until we get required number of bytes
67
67
  while (totalBytesRead < length) {
@@ -0,0 +1 @@
1
+ export const isSupported = false;