@loaders.gl/loader-utils 4.0.0-alpha.21 → 4.0.0-alpha.23

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 (108) hide show
  1. package/dist/es5/index.js +49 -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 +104 -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.js +2 -2
  16. package/dist/es5/lib/filesystems/node-filesystem.js.map +1 -1
  17. package/dist/es5/lib/option-utils/merge-loader-options.js.map +1 -1
  18. package/dist/es5/lib/worker-loader-utils/create-loader-worker.js +2 -2
  19. package/dist/es5/lib/worker-loader-utils/create-loader-worker.js.map +1 -1
  20. package/dist/es5/lib/worker-loader-utils/encode-with-worker.js.map +1 -1
  21. package/dist/es5/lib/worker-loader-utils/parse-with-worker.js.map +1 -1
  22. package/dist/es5/loader-types.js +58 -0
  23. package/dist/es5/loader-types.js.map +1 -0
  24. package/dist/es5/types.js.map +1 -1
  25. package/dist/es5/writer-types.js +2 -0
  26. package/dist/es5/writer-types.js.map +1 -0
  27. package/dist/esm/index.js +6 -1
  28. package/dist/esm/index.js.map +1 -1
  29. package/dist/esm/json-loader.js +1 -1
  30. package/dist/esm/json-loader.js.map +1 -1
  31. package/dist/esm/lib/file-provider/data-view-file.js +33 -0
  32. package/dist/esm/lib/file-provider/data-view-file.js.map +1 -0
  33. package/dist/esm/lib/file-provider/file-handle-file.js +59 -0
  34. package/dist/esm/lib/file-provider/file-handle-file.js.map +1 -0
  35. package/dist/esm/lib/file-provider/file-handle.js +37 -0
  36. package/dist/esm/lib/file-provider/file-handle.js.map +1 -0
  37. package/dist/esm/lib/file-provider/file-provider.js +4 -0
  38. package/dist/esm/lib/file-provider/file-provider.js.map +1 -0
  39. package/dist/esm/lib/filesystems/filesystem.js +2 -0
  40. package/dist/esm/lib/filesystems/filesystem.js.map +1 -0
  41. package/dist/esm/lib/filesystems/node-filesystem.js +1 -1
  42. package/dist/esm/lib/filesystems/node-filesystem.js.map +1 -1
  43. package/dist/esm/lib/option-utils/merge-loader-options.js.map +1 -1
  44. package/dist/esm/lib/worker-loader-utils/create-loader-worker.js +2 -2
  45. package/dist/esm/lib/worker-loader-utils/create-loader-worker.js.map +1 -1
  46. package/dist/esm/lib/worker-loader-utils/encode-with-worker.js.map +1 -1
  47. package/dist/esm/lib/worker-loader-utils/parse-with-worker.js.map +1 -1
  48. package/dist/esm/loader-types.js +16 -0
  49. package/dist/esm/loader-types.js.map +1 -0
  50. package/dist/esm/types.js.map +1 -1
  51. package/dist/esm/writer-types.js +2 -0
  52. package/dist/esm/writer-types.js.map +1 -0
  53. package/dist/index.d.ts +11 -2
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +17 -4
  56. package/dist/json-loader.d.ts +1 -1
  57. package/dist/json-loader.d.ts.map +1 -1
  58. package/dist/lib/file-provider/data-view-file.d.ts +37 -0
  59. package/dist/lib/file-provider/data-view-file.d.ts.map +1 -0
  60. package/dist/lib/file-provider/data-view-file.js +63 -0
  61. package/dist/lib/file-provider/file-handle-file.d.ts +53 -0
  62. package/dist/lib/file-provider/file-handle-file.d.ts.map +1 -0
  63. package/dist/lib/file-provider/file-handle-file.js +92 -0
  64. package/dist/lib/file-provider/file-handle.d.ts +40 -0
  65. package/dist/lib/file-provider/file-handle.d.ts.map +1 -0
  66. package/dist/lib/file-provider/file-handle.js +55 -0
  67. package/dist/lib/file-provider/file-provider.d.ts +45 -0
  68. package/dist/lib/file-provider/file-provider.d.ts.map +1 -0
  69. package/dist/lib/file-provider/file-provider.js +13 -0
  70. package/dist/lib/filesystems/filesystem.d.ts +81 -0
  71. package/dist/lib/filesystems/filesystem.d.ts.map +1 -0
  72. package/dist/lib/filesystems/filesystem.js +37 -0
  73. package/dist/lib/filesystems/node-filesystem.d.ts +2 -2
  74. package/dist/lib/filesystems/node-filesystem.d.ts.map +1 -1
  75. package/dist/lib/filesystems/node-filesystem.js +3 -2
  76. package/dist/lib/option-utils/merge-loader-options.d.ts +1 -1
  77. package/dist/lib/option-utils/merge-loader-options.d.ts.map +1 -1
  78. package/dist/lib/worker-loader-utils/create-loader-worker.d.ts +1 -1
  79. package/dist/lib/worker-loader-utils/create-loader-worker.d.ts.map +1 -1
  80. package/dist/lib/worker-loader-utils/create-loader-worker.js +3 -2
  81. package/dist/lib/worker-loader-utils/encode-with-worker.d.ts +1 -1
  82. package/dist/lib/worker-loader-utils/encode-with-worker.d.ts.map +1 -1
  83. package/dist/lib/worker-loader-utils/parse-with-worker.d.ts +1 -1
  84. package/dist/lib/worker-loader-utils/parse-with-worker.d.ts.map +1 -1
  85. package/dist/loader-types.d.ts +208 -0
  86. package/dist/loader-types.d.ts.map +1 -0
  87. package/dist/loader-types.js +36 -0
  88. package/dist/types.d.ts +0 -259
  89. package/dist/types.d.ts.map +1 -1
  90. package/dist/writer-types.d.ts +36 -0
  91. package/dist/writer-types.d.ts.map +1 -0
  92. package/dist/writer-types.js +3 -0
  93. package/package.json +3 -3
  94. package/src/index.ts +31 -16
  95. package/src/json-loader.ts +1 -1
  96. package/src/lib/file-provider/data-view-file.ts +72 -0
  97. package/src/lib/file-provider/file-handle-file.ts +116 -0
  98. package/src/lib/file-provider/file-handle.ts +74 -0
  99. package/src/lib/file-provider/file-provider.ts +56 -0
  100. package/src/lib/filesystems/filesystem.ts +87 -0
  101. package/src/lib/filesystems/node-filesystem.ts +3 -3
  102. package/src/lib/option-utils/merge-loader-options.ts +1 -1
  103. package/src/lib/worker-loader-utils/create-loader-worker.ts +20 -4
  104. package/src/lib/worker-loader-utils/encode-with-worker.ts +1 -1
  105. package/src/lib/worker-loader-utils/parse-with-worker.ts +1 -1
  106. package/src/loader-types.ts +366 -0
  107. package/src/types.ts +0 -366
  108. package/src/writer-types.ts +61 -0
package/src/index.ts CHANGED
@@ -1,23 +1,12 @@
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,
@@ -29,6 +18,24 @@ export type {
29
18
  FetchLike
30
19
  } from './types';
31
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
+
32
39
  // GENERAL UTILS
33
40
  export {assert} from './lib/env-utils/assert';
34
41
  export {
@@ -112,10 +119,18 @@ import * as stream from './lib/node/stream';
112
119
  export {stream};
113
120
 
114
121
  // EXPERIMENTAL
122
+ export type {FileSystem, RandomAccessReadFileSystem} from './lib/filesystems/filesystem';
123
+ export {NodeFileSystem as _NodeFileSystem} from './lib/filesystems/node-filesystem';
124
+
125
+ export type {FileProvider} from './lib/file-provider/file-provider';
126
+ export {isFileProvider} from './lib/file-provider/file-provider';
127
+
128
+ export {FileHandle} from './lib/file-provider/file-handle';
129
+ export {FileHandleFile} from './lib/file-provider/file-handle-file';
130
+ export {DataViewFile} from './lib/file-provider/data-view-file';
131
+
115
132
  export type {ReadableFile} from './lib/filesystems/readable-file';
116
133
  export {makeReadableFile} from './lib/filesystems/readable-file';
117
134
 
118
135
  export type {WritableFile} from './lib/filesystems/writable-file';
119
136
  export {makeWritableFile} from './lib/filesystems/writable-file';
120
-
121
- export {default as _NodeFileSystem} from './lib/filesystems/node-filesystem';
@@ -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,74 @@
1
+ import {read, open, close, stat, BigIntStats} from '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: BigIntStats;
15
+
16
+ private constructor(fileDescriptor: number, stats: 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
+ open(path, undefined, undefined, (_err, fd) => (_err ? reject(_err) : resolve(fd)));
31
+ }),
32
+ new Promise<BigIntStats>((resolve, reject) => {
33
+ stat(path, {bigint: true}, (_err, stats) => (_err ? reject(_err) : resolve(stats)));
34
+ })
35
+ ]);
36
+ return new FileHandle(fd, stats);
37
+ }
38
+
39
+ /** Close file */
40
+ async close(): Promise<void> {
41
+ return new Promise<void>((resolve) => {
42
+ close(this.fileDescriptor, (_err) => resolve());
43
+ });
44
+ }
45
+
46
+ /**
47
+ * Reads data from the file and stores that in the given buffer.
48
+ *
49
+ * If the file is not modified concurrently, the end-of-file is reached when the
50
+ * number of bytes read is zero.
51
+ * @param buffer A buffer that will be filled with the file data read.
52
+ * @param offset The location in the buffer at which to start filling.
53
+ * @param length The number of bytes to read.
54
+ * @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
55
+ * integer, the current file position will remain unchanged.
56
+ * @return Fulfills upon success with a FileReadResult object
57
+ */
58
+ read = (
59
+ buffer: Buffer,
60
+ offset: number,
61
+ length: number,
62
+ position: number | bigint
63
+ ): Promise<FileReadResult> => {
64
+ return new Promise((s) => {
65
+ read(this.fileDescriptor, buffer, offset, length, position, (_err, bytesRead, buffer) =>
66
+ s({bytesRead, buffer})
67
+ );
68
+ });
69
+ };
70
+
71
+ get stat(): BigIntStats {
72
+ return this.stats;
73
+ }
74
+ }
@@ -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: Buffer}>;
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
+ */
@@ -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
  }
@@ -1,6 +1,6 @@
1
1
  // loaders.gl, MIT license
2
2
 
3
- import {LoaderOptions} from '../../types';
3
+ import {LoaderOptions} from '../../loader-types';
4
4
 
5
5
  /**
6
6
  *
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-restricted-globals */
2
- import type {LoaderWithParser} from '../../types';
2
+ import type {LoaderWithParser, LoaderOptions, LoaderContext} from '../../loader-types';
3
3
  import {WorkerBody} from '@loaders.gl/worker-utils';
4
4
  // import {validateLoaderVersion} from './validate-loader-version';
5
5
 
@@ -27,9 +27,10 @@ export function createLoaderWorker(loader: LoaderWithParser) {
27
27
  loader,
28
28
  arrayBuffer: input,
29
29
  options,
30
+ // @ts-expect-error fetch missing
30
31
  context: {
31
32
  ...context,
32
- parse: parseOnMainThread
33
+ _parse: parseOnMainThread
33
34
  }
34
35
  });
35
36
  WorkerBody.postMessage('done', {result});
@@ -43,7 +44,12 @@ export function createLoaderWorker(loader: LoaderWithParser) {
43
44
  };
44
45
  }
45
46
 
46
- function parseOnMainThread(arrayBuffer: ArrayBuffer, options: {[key: string]: any}): Promise<void> {
47
+ function parseOnMainThread(
48
+ arrayBuffer: ArrayBuffer,
49
+ loader: any,
50
+ options?: LoaderOptions,
51
+ context?: LoaderContext
52
+ ): Promise<void> {
47
53
  return new Promise((resolve, reject) => {
48
54
  const id = requestId++;
49
55
 
@@ -83,7 +89,17 @@ function parseOnMainThread(arrayBuffer: ArrayBuffer, options: {[key: string]: an
83
89
  // TODO - Why not support async loader.parse* funcs here?
84
90
  // TODO - Why not reuse a common function instead of reimplementing loader.parse* selection logic? Keeping loader small?
85
91
  // TODO - Lack of appropriate parser functions can be detected when we create worker, no need to wait until parse
86
- async function parseData({loader, arrayBuffer, options, context}) {
92
+ async function parseData({
93
+ loader,
94
+ arrayBuffer,
95
+ options,
96
+ context
97
+ }: {
98
+ loader: LoaderWithParser;
99
+ arrayBuffer: ArrayBuffer;
100
+ options: LoaderOptions;
101
+ context: LoaderContext;
102
+ }) {
87
103
  let data;
88
104
  let parser;
89
105
  if (loader.parseSync || loader.parse) {
@@ -1,5 +1,5 @@
1
1
  import {WorkerFarm} from '@loaders.gl/worker-utils';
2
- import {Writer, WriterOptions} from '../../types';
2
+ import {Writer, WriterOptions} from '../../writer-types';
3
3
  import {isBrowser} from '../env-utils/globals';
4
4
 
5
5
  /**
@@ -4,7 +4,7 @@ import {
4
4
  WorkerMessagePayload,
5
5
  isBrowser
6
6
  } from '@loaders.gl/worker-utils';
7
- import type {Loader, LoaderOptions, LoaderContext} from '../../types';
7
+ import type {Loader, LoaderOptions, LoaderContext} from '../../loader-types';
8
8
  import {WorkerFarm, getWorkerURL} from '@loaders.gl/worker-utils';
9
9
 
10
10
  /**