@loaders.gl/zip 4.0.0-alpha.19 → 4.0.0-alpha.21

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 (92) hide show
  1. package/dist/dist.min.js +638 -1
  2. package/dist/es5/file-provider/data-view-file.js +146 -0
  3. package/dist/es5/file-provider/data-view-file.js.map +1 -0
  4. package/dist/es5/file-provider/file-handle-file.js +234 -0
  5. package/dist/es5/file-provider/file-handle-file.js.map +1 -0
  6. package/dist/es5/file-provider/file-handle.js +101 -0
  7. package/dist/es5/file-provider/file-handle.js.map +1 -0
  8. package/dist/es5/file-provider/file-provider.js +11 -0
  9. package/dist/es5/file-provider/file-provider.js.map +1 -0
  10. package/dist/es5/filesystems/zip-filesystem.js +340 -0
  11. package/dist/es5/filesystems/zip-filesystem.js.map +1 -0
  12. package/dist/es5/hash-file-utility.js +130 -0
  13. package/dist/es5/hash-file-utility.js.map +1 -0
  14. package/dist/es5/index.js +85 -0
  15. package/dist/es5/index.js.map +1 -1
  16. package/dist/es5/parse-zip/cd-file-header.js +163 -0
  17. package/dist/es5/parse-zip/cd-file-header.js.map +1 -0
  18. package/dist/es5/parse-zip/end-of-central-directory.js +98 -0
  19. package/dist/es5/parse-zip/end-of-central-directory.js.map +1 -0
  20. package/dist/es5/parse-zip/local-file-header.js +117 -0
  21. package/dist/es5/parse-zip/local-file-header.js.map +1 -0
  22. package/dist/es5/parse-zip/search-from-the-end.js +69 -0
  23. package/dist/es5/parse-zip/search-from-the-end.js.map +1 -0
  24. package/dist/es5/zip-loader.js +1 -1
  25. package/dist/esm/file-provider/data-view-file.js +33 -0
  26. package/dist/esm/file-provider/data-view-file.js.map +1 -0
  27. package/dist/esm/file-provider/file-handle-file.js +57 -0
  28. package/dist/esm/file-provider/file-handle-file.js.map +1 -0
  29. package/dist/esm/file-provider/file-handle.js +37 -0
  30. package/dist/esm/file-provider/file-handle.js.map +1 -0
  31. package/dist/esm/file-provider/file-provider.js +4 -0
  32. package/dist/esm/file-provider/file-provider.js.map +1 -0
  33. package/dist/esm/filesystems/zip-filesystem.js +86 -0
  34. package/dist/esm/filesystems/zip-filesystem.js.map +1 -0
  35. package/dist/esm/hash-file-utility.js +55 -0
  36. package/dist/esm/hash-file-utility.js.map +1 -0
  37. package/dist/esm/index.js +7 -0
  38. package/dist/esm/index.js.map +1 -1
  39. package/dist/esm/parse-zip/cd-file-header.js +54 -0
  40. package/dist/esm/parse-zip/cd-file-header.js.map +1 -0
  41. package/dist/esm/parse-zip/end-of-central-directory.js +31 -0
  42. package/dist/esm/parse-zip/end-of-central-directory.js.map +1 -0
  43. package/dist/esm/parse-zip/local-file-header.js +41 -0
  44. package/dist/esm/parse-zip/local-file-header.js.map +1 -0
  45. package/dist/esm/parse-zip/search-from-the-end.js +16 -0
  46. package/dist/esm/parse-zip/search-from-the-end.js.map +1 -0
  47. package/dist/esm/zip-loader.js +1 -1
  48. package/dist/file-provider/data-view-file.d.ts +37 -0
  49. package/dist/file-provider/data-view-file.d.ts.map +1 -0
  50. package/dist/file-provider/data-view-file.js +63 -0
  51. package/dist/file-provider/file-handle-file.d.ts +53 -0
  52. package/dist/file-provider/file-handle-file.d.ts.map +1 -0
  53. package/dist/file-provider/file-handle-file.js +90 -0
  54. package/dist/file-provider/file-handle.d.ts +40 -0
  55. package/dist/file-provider/file-handle.d.ts.map +1 -0
  56. package/dist/file-provider/file-handle.js +57 -0
  57. package/dist/file-provider/file-provider.d.ts +45 -0
  58. package/dist/file-provider/file-provider.d.ts.map +1 -0
  59. package/dist/file-provider/file-provider.js +13 -0
  60. package/dist/filesystems/zip-filesystem.d.ts +44 -0
  61. package/dist/filesystems/zip-filesystem.d.ts.map +1 -0
  62. package/dist/filesystems/zip-filesystem.js +119 -0
  63. package/dist/hash-file-utility.d.ts +35 -0
  64. package/dist/hash-file-utility.d.ts.map +1 -0
  65. package/dist/hash-file-utility.js +88 -0
  66. package/dist/index.d.ts +9 -0
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +21 -1
  69. package/dist/parse-zip/cd-file-header.d.ts +36 -0
  70. package/dist/parse-zip/cd-file-header.d.ts.map +1 -0
  71. package/dist/parse-zip/cd-file-header.js +68 -0
  72. package/dist/parse-zip/end-of-central-directory.d.ts +18 -0
  73. package/dist/parse-zip/end-of-central-directory.d.ts.map +1 -0
  74. package/dist/parse-zip/end-of-central-directory.js +40 -0
  75. package/dist/parse-zip/local-file-header.d.ts +29 -0
  76. package/dist/parse-zip/local-file-header.d.ts.map +1 -0
  77. package/dist/parse-zip/local-file-header.js +55 -0
  78. package/dist/parse-zip/search-from-the-end.d.ts +11 -0
  79. package/dist/parse-zip/search-from-the-end.d.ts.map +1 -0
  80. package/dist/parse-zip/search-from-the-end.js +31 -0
  81. package/package.json +9 -2
  82. package/src/file-provider/data-view-file.ts +72 -0
  83. package/src/file-provider/file-handle-file.ts +114 -0
  84. package/src/file-provider/file-handle.ts +73 -0
  85. package/src/file-provider/file-provider.ts +56 -0
  86. package/src/filesystems/zip-filesystem.ts +132 -0
  87. package/src/hash-file-utility.ts +101 -0
  88. package/src/index.ts +19 -0
  89. package/src/parse-zip/cd-file-header.ts +114 -0
  90. package/src/parse-zip/end-of-central-directory.ts +71 -0
  91. package/src/parse-zip/local-file-header.ts +91 -0
  92. package/src/parse-zip/search-from-the-end.ts +38 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-handle-file.d.ts","sourceRoot":"","sources":["../../src/file-provider/file-handle-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,iBAAiB,CAAC;AAG7C;;GAEG;AACH,qBAAa,cAAe,YAAW,YAAY;IACjD;;;OAGG;WACU,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAKxD;;OAEG;IACH,OAAO,CAAC,cAAc,CAAa;IAEnC;;OAEG;IACH,OAAO,CAAC,IAAI,CAAS;IAErB,OAAO;IAKP,iBAAiB;IACX,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B;;;OAGG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAU/C;;;OAGG;IACG,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAUhD;;;OAGG;IACG,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAUhD;;;OAGG;IACG,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAUnD;;;;OAIG;IACG,KAAK,CAAC,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAU1E;;OAEG;IACH,IAAI,MAAM,IAAI,MAAM,CAEnB;CACF"}
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileHandleFile = void 0;
4
+ const file_handle_1 = require("./file-handle");
5
+ /**
6
+ * Provides file data using node fs library
7
+ */
8
+ class FileHandleFile {
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) {
14
+ const fileDescriptor = await file_handle_1.FileHandle.open(path);
15
+ return new FileHandleFile(fileDescriptor, fileDescriptor.stat.size);
16
+ }
17
+ constructor(fileDescriptor, size) {
18
+ this.fileDescriptor = fileDescriptor;
19
+ this.size = size;
20
+ }
21
+ /** Close file */
22
+ async destroy() {
23
+ await this.fileDescriptor.close();
24
+ }
25
+ /**
26
+ * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
27
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
28
+ */
29
+ async getUint8(offset) {
30
+ const val = new Uint8Array((await this.fileDescriptor.read(Buffer.alloc(1), 0, 1, offset)).buffer.buffer).at(0);
31
+ if (val === undefined) {
32
+ throw new Error('something went wrong');
33
+ }
34
+ return val;
35
+ }
36
+ /**
37
+ * Gets an unsigned 16-bit integer at the specified byte offset from the start of the file.
38
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
39
+ */
40
+ async getUint16(offset) {
41
+ const val = new Uint16Array((await this.fileDescriptor.read(Buffer.alloc(2), 0, 2, offset)).buffer.buffer).at(0);
42
+ if (val === undefined) {
43
+ throw new Error('something went wrong');
44
+ }
45
+ return val;
46
+ }
47
+ /**
48
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
49
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
50
+ */
51
+ async getUint32(offset) {
52
+ const val = new Uint32Array((await this.fileDescriptor.read(Buffer.alloc(4), 0, 4, offset)).buffer.buffer).at(0);
53
+ if (val === undefined) {
54
+ throw new Error('something went wrong');
55
+ }
56
+ return val;
57
+ }
58
+ /**
59
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
60
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
61
+ */
62
+ async getBigUint64(offset) {
63
+ const val = new BigInt64Array((await this.fileDescriptor.read(Buffer.alloc(8), 0, 8, offset)).buffer.buffer).at(0);
64
+ if (val === undefined) {
65
+ throw new Error('something went wrong');
66
+ }
67
+ return val;
68
+ }
69
+ /**
70
+ * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
71
+ * @param startOffsset The offset, in byte, from the start of the file where to start reading the data.
72
+ * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
73
+ */
74
+ async slice(startOffsset, endOffset) {
75
+ const bigLength = endOffset - startOffsset;
76
+ if (bigLength > Number.MAX_SAFE_INTEGER) {
77
+ throw new Error('too big slice');
78
+ }
79
+ const length = Number(bigLength);
80
+ return (await this.fileDescriptor.read(Buffer.alloc(length), 0, length, startOffsset)).buffer
81
+ .buffer;
82
+ }
83
+ /**
84
+ * the length (in bytes) of the data.
85
+ */
86
+ get length() {
87
+ return this.size;
88
+ }
89
+ }
90
+ exports.FileHandleFile = FileHandleFile;
@@ -0,0 +1,40 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { BigIntStats } from 'fs';
4
+ /** file reading result */
5
+ export type FileReadResult = {
6
+ /** amount of the bytes read */
7
+ bytesRead: number;
8
+ /** the buffer filled with data from file*/
9
+ buffer: Buffer;
10
+ };
11
+ /** Object handling file info */
12
+ export declare class FileHandle {
13
+ private fileDescriptor;
14
+ private stats;
15
+ private constructor();
16
+ /**
17
+ * Opens a `FileHandle`.
18
+ *
19
+ * @param path path to the file
20
+ * @return Fulfills with a {FileHandle} object.
21
+ */
22
+ static open: (path: string) => Promise<FileHandle>;
23
+ /** Close file */
24
+ close(): Promise<void>;
25
+ /**
26
+ * Reads data from the file and stores that in the given buffer.
27
+ *
28
+ * If the file is not modified concurrently, the end-of-file is reached when the
29
+ * number of bytes read is zero.
30
+ * @param buffer A buffer that will be filled with the file data read.
31
+ * @param offset The location in the buffer at which to start filling.
32
+ * @param length The number of bytes to read.
33
+ * @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
34
+ * integer, the current file position will remain unchanged.
35
+ * @return Fulfills upon success with a FileReadResult object
36
+ */
37
+ read: (buffer: Buffer, offset: number, length: number, position: number | bigint) => Promise<FileReadResult>;
38
+ get stat(): BigIntStats;
39
+ }
40
+ //# sourceMappingURL=file-handle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-handle.d.ts","sourceRoot":"","sources":["../../src/file-provider/file-handle.ts"],"names":[],"mappings":";;AAAA,OAAO,EAA0B,WAAW,EAAC,MAAM,IAAI,CAAC;AAExD,0BAA0B;AAC1B,MAAM,MAAM,cAAc,GAAG;IAC3B,+BAA+B;IAC/B,SAAS,EAAE,MAAM,CAAC;IAClB,2CAA2C;IAC3C,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,gCAAgC;AAChC,qBAAa,UAAU;IACrB,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO;IAIP;;;;;OAKG;IAEH,MAAM,CAAC,IAAI,SAAgB,MAAM,KAAG,QAAQ,UAAU,CAAC,CAUrD;IAEF,iBAAiB;IACX,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAM5B;;;;;;;;;;;OAWG;IACH,IAAI,WACM,MAAM,UACN,MAAM,UACN,MAAM,YACJ,MAAM,GAAG,MAAM,KACxB,QAAQ,cAAc,CAAC,CAMxB;IAEF,IAAI,IAAI,IAAI,WAAW,CAEtB;CACF"}
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.FileHandle = void 0;
5
+ const fs_1 = require("fs");
6
+ /** Object handling file info */
7
+ class FileHandle {
8
+ constructor(fileDescriptor, stats) {
9
+ /**
10
+ * Reads data from the file and stores that in the given buffer.
11
+ *
12
+ * If the file is not modified concurrently, the end-of-file is reached when the
13
+ * number of bytes read is zero.
14
+ * @param buffer A buffer that will be filled with the file data read.
15
+ * @param offset The location in the buffer at which to start filling.
16
+ * @param length The number of bytes to read.
17
+ * @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
18
+ * integer, the current file position will remain unchanged.
19
+ * @return Fulfills upon success with a FileReadResult object
20
+ */
21
+ this.read = (buffer, offset, length, position) => {
22
+ return new Promise((s) => {
23
+ (0, fs_1.read)(this.fileDescriptor, buffer, offset, length, position, (_err, bytesRead, buffer) => s({ bytesRead, buffer }));
24
+ });
25
+ };
26
+ this.fileDescriptor = fileDescriptor;
27
+ this.stats = stats;
28
+ }
29
+ /** Close file */
30
+ async close() {
31
+ return new Promise((resolve) => {
32
+ (0, fs_1.close)(this.fileDescriptor, (_err) => resolve());
33
+ });
34
+ }
35
+ get stat() {
36
+ return this.stats;
37
+ }
38
+ }
39
+ exports.FileHandle = FileHandle;
40
+ _a = FileHandle;
41
+ /**
42
+ * Opens a `FileHandle`.
43
+ *
44
+ * @param path path to the file
45
+ * @return Fulfills with a {FileHandle} object.
46
+ */
47
+ FileHandle.open = async (path) => {
48
+ const [fd, stats] = await Promise.all([
49
+ new Promise((s) => {
50
+ (0, fs_1.open)(path, undefined, undefined, (_err, fd) => s(fd));
51
+ }),
52
+ new Promise((s) => {
53
+ (0, fs_1.stat)(path, { bigint: true }, (_err, stats) => s(stats));
54
+ })
55
+ ]);
56
+ return new FileHandle(fd, stats);
57
+ };
@@ -0,0 +1,45 @@
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
+ * Gets an unsigned 16-bit integer at the specified byte offset from the start of the file.
16
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
17
+ */
18
+ getUint16(offset: bigint): Promise<number>;
19
+ /**
20
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
21
+ * @param offset The offset, in bytes, from the file of the view where to read the data.
22
+ */
23
+ getUint32(offset: bigint): Promise<number>;
24
+ /**
25
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
26
+ * @param offset The offset, in byte, from the file of the view where to read the data.
27
+ */
28
+ getBigUint64(offset: bigint): Promise<bigint>;
29
+ /**
30
+ * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
31
+ * @param startOffset The offset, in bytes, from the start of the file where to start reading the data.
32
+ * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
33
+ */
34
+ slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer>;
35
+ /**
36
+ * the length (in bytes) of the data.
37
+ */
38
+ length: bigint;
39
+ }
40
+ /**
41
+ * Check is the object has FileProvider members
42
+ * @param fileProvider - tested object
43
+ */
44
+ export declare const isFileProvider: (fileProvider: unknown) => bigint;
45
+ //# sourceMappingURL=file-provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-provider.d.ts","sourceRoot":"","sources":["../../src/file-provider/file-provider.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE1C;;;OAGG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE3C;;;OAGG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE3C;;;OAGG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAE9C;;;;OAIG;IACH,KAAK,CAAC,WAAW,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IAEpE;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,iBAAkB,OAAO,WAMnD,CAAC"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isFileProvider = void 0;
4
+ /**
5
+ * Check is the object has FileProvider members
6
+ * @param fileProvider - tested object
7
+ */
8
+ const isFileProvider = (fileProvider) => {
9
+ return (fileProvider?.getUint8 &&
10
+ fileProvider?.slice &&
11
+ fileProvider?.length);
12
+ };
13
+ exports.isFileProvider = isFileProvider;
@@ -0,0 +1,44 @@
1
+ import { FileSystem } from '@loaders.gl/core';
2
+ import { FileProvider } from '../file-provider/file-provider';
3
+ import { ZipCDFileHeader } from '../parse-zip/cd-file-header';
4
+ /**
5
+ * FileSystem adapter for a ZIP file
6
+ * Holds FileProvider object that provides random access to archived files
7
+ */
8
+ export declare class ZipFileSystem implements FileSystem {
9
+ /** FileProvider instance promise */
10
+ private fileProvider;
11
+ /**
12
+ * Constructor
13
+ * @param file - instance of FileProvider or file path string
14
+ */
15
+ constructor(file: FileProvider | string);
16
+ /** Clean up resources */
17
+ destroy(): Promise<void>;
18
+ /**
19
+ * Get file names list from zip archive
20
+ * @returns array of file names
21
+ */
22
+ readdir(): Promise<string[]>;
23
+ /**
24
+ * Get file metadata
25
+ * @param filename - name of a file
26
+ * @returns central directory data
27
+ */
28
+ stat(filename: string): Promise<ZipCDFileHeader & {
29
+ size: number;
30
+ }>;
31
+ /**
32
+ * Implementation of fetch against this file system
33
+ * @param filename - name of a file
34
+ * @returns - Response with file data
35
+ */
36
+ fetch(filename: string): Promise<Response>;
37
+ /**
38
+ * Get central directory file header
39
+ * @param filename - name of a file
40
+ * @returns central directory file header
41
+ */
42
+ private getCDFileHeader;
43
+ }
44
+ //# sourceMappingURL=zip-filesystem.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zip-filesystem.d.ts","sourceRoot":"","sources":["../../src/filesystems/zip-filesystem.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAY,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAC,YAAY,EAAiB,MAAM,gCAAgC,CAAC;AAE5E,OAAO,EAAC,eAAe,EAA2B,MAAM,6BAA6B,CAAC;AAUtF;;;GAGG;AACH,qBAAa,aAAc,YAAW,UAAU;IAC9C,oCAAoC;IACpC,OAAO,CAAC,YAAY,CAAuD;IAE3E;;;OAGG;gBACS,IAAI,EAAE,YAAY,GAAG,MAAM;IAavC,yBAAyB;IACnB,OAAO;IAOb;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAalC;;;;OAIG;IACG,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG;QAAC,IAAI,EAAE,MAAM,CAAA;KAAC,CAAC;IAKvE;;;;OAIG;IACG,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IA8BhD;;;;OAIG;YACW,eAAe;CAkB9B"}
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ZipFileSystem = void 0;
4
+ const core_1 = require("@loaders.gl/core");
5
+ const file_provider_1 = require("../file-provider/file-provider");
6
+ const file_handle_file_1 = require("../file-provider/file-handle-file");
7
+ const cd_file_header_1 = require("../parse-zip/cd-file-header");
8
+ const local_file_header_1 = require("../parse-zip/local-file-header");
9
+ /** Handling different compression types in zip */
10
+ const COMPRESSION_METHODS = {
11
+ /** No compression */
12
+ 0: async (compressedFile) => compressedFile
13
+ };
14
+ /**
15
+ * FileSystem adapter for a ZIP file
16
+ * Holds FileProvider object that provides random access to archived files
17
+ */
18
+ class ZipFileSystem {
19
+ /**
20
+ * Constructor
21
+ * @param file - instance of FileProvider or file path string
22
+ */
23
+ constructor(file) {
24
+ /** FileProvider instance promise */
25
+ this.fileProvider = Promise.resolve(null);
26
+ // Try to open file in NodeJS
27
+ if (typeof file === 'string') {
28
+ if (!core_1.isBrowser) {
29
+ this.fileProvider = file_handle_file_1.FileHandleFile.from(file);
30
+ }
31
+ else {
32
+ throw new Error('Cannot open file for random access in a WEB browser');
33
+ }
34
+ }
35
+ else if ((0, file_provider_1.isFileProvider)(file)) {
36
+ this.fileProvider = Promise.resolve(file);
37
+ }
38
+ }
39
+ /** Clean up resources */
40
+ async destroy() {
41
+ const fileProvider = await this.fileProvider;
42
+ if (fileProvider) {
43
+ fileProvider.destroy();
44
+ }
45
+ }
46
+ /**
47
+ * Get file names list from zip archive
48
+ * @returns array of file names
49
+ */
50
+ async readdir() {
51
+ const fileProvider = await this.fileProvider;
52
+ if (!fileProvider) {
53
+ throw new Error('No data detected in the zip archive');
54
+ }
55
+ const fileNames = [];
56
+ const zipCDIterator = (0, cd_file_header_1.zipCDFileHeaderGenerator)(fileProvider);
57
+ for await (const cdHeader of zipCDIterator) {
58
+ fileNames.push(cdHeader.fileName);
59
+ }
60
+ return fileNames;
61
+ }
62
+ /**
63
+ * Get file metadata
64
+ * @param filename - name of a file
65
+ * @returns central directory data
66
+ */
67
+ async stat(filename) {
68
+ const cdFileHeader = await this.getCDFileHeader(filename);
69
+ return { ...cdFileHeader, size: Number(cdFileHeader.uncompressedSize) };
70
+ }
71
+ /**
72
+ * Implementation of fetch against this file system
73
+ * @param filename - name of a file
74
+ * @returns - Response with file data
75
+ */
76
+ async fetch(filename) {
77
+ const fileProvider = await this.fileProvider;
78
+ if (!fileProvider) {
79
+ throw new Error('No data detected in the zip archive');
80
+ }
81
+ const cdFileHeader = await this.getCDFileHeader(filename);
82
+ const localFileHeader = await (0, local_file_header_1.parseZipLocalFileHeader)(cdFileHeader.localHeaderOffset, fileProvider);
83
+ if (!localFileHeader) {
84
+ throw new Error('Local file header has not been found in the zip archive`');
85
+ }
86
+ const compressionHandler = COMPRESSION_METHODS[localFileHeader.compressionMethod.toString()];
87
+ if (!compressionHandler) {
88
+ throw Error('Only Deflation compression is supported');
89
+ }
90
+ const compressedFile = await fileProvider.slice(localFileHeader.fileDataOffset, localFileHeader.fileDataOffset + localFileHeader.compressedSize);
91
+ const uncompressedFile = await compressionHandler(compressedFile);
92
+ const response = new Response(uncompressedFile);
93
+ return response;
94
+ }
95
+ /**
96
+ * Get central directory file header
97
+ * @param filename - name of a file
98
+ * @returns central directory file header
99
+ */
100
+ async getCDFileHeader(filename) {
101
+ const fileProvider = await this.fileProvider;
102
+ if (!fileProvider) {
103
+ throw new Error('No data detected in the zip archive');
104
+ }
105
+ const zipCDIterator = (0, cd_file_header_1.zipCDFileHeaderGenerator)(fileProvider);
106
+ let result = null;
107
+ for await (const cdHeader of zipCDIterator) {
108
+ if (cdHeader.fileName === filename) {
109
+ result = cdHeader;
110
+ break;
111
+ }
112
+ }
113
+ if (!result) {
114
+ throw new Error('File has not been found in the zip archive');
115
+ }
116
+ return result;
117
+ }
118
+ }
119
+ exports.ZipFileSystem = ZipFileSystem;
@@ -0,0 +1,35 @@
1
+ /// <reference types="node" />
2
+ import { FileProvider } from './file-provider/file-provider';
3
+ /** Element of hash array */
4
+ export type HashElement = {
5
+ /** File name hash */
6
+ hash: Buffer;
7
+ /** File offset in the archive */
8
+ offset: bigint;
9
+ };
10
+ /**
11
+ * Comparing md5 hashes according to https://github.com/Esri/i3s-spec/blob/master/docs/2.0/slpk_hashtable.pcsl.md step 5
12
+ * @param hash1 hash to compare
13
+ * @param hash2 hash to compare
14
+ * @returns -1 if hash1 < hash2, 0 of hash1 === hash2, 1 if hash1 > hash2
15
+ */
16
+ export declare const compareHashes: (hash1: Buffer, hash2: Buffer) => number;
17
+ /**
18
+ * Reads hash file from buffer and returns it in ready-to-use form
19
+ * @param hashFile - bufer containing hash file
20
+ * @returns Array containing file info
21
+ */
22
+ export declare const parseHashFile: (hashFile: ArrayBuffer) => HashElement[];
23
+ /**
24
+ * Binary search in the hash info
25
+ * @param hashToSearch hash that we need to find
26
+ * @returns required hash element or undefined if not found
27
+ */
28
+ export declare const findBin: (hashToSearch: Buffer, hashArray: HashElement[]) => HashElement | undefined;
29
+ /**
30
+ * generates hash info from central directory
31
+ * @param fileProvider - provider of the archive
32
+ * @returns ready to use hash info
33
+ */
34
+ export declare const generateHashInfo: (fileProvider: FileProvider) => Promise<HashElement[]>;
35
+ //# sourceMappingURL=hash-file-utility.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash-file-utility.d.ts","sourceRoot":"","sources":["../src/hash-file-utility.ts"],"names":[],"mappings":";AAEA,OAAO,EAAC,YAAY,EAAC,MAAM,+BAA+B,CAAC;AAE3D,4BAA4B;AAC5B,MAAM,MAAM,WAAW,GAAG;IACxB,qBAAqB;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,iCAAiC;IACjC,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,UAAW,MAAM,SAAS,MAAM,KAAG,MAY5D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,aAAc,WAAW,KAAG,WAAW,EAmBhE,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,OAAO,iBACJ,MAAM,aACT,WAAW,EAAE,KACvB,WAAW,GAAG,SAgBhB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,iBAAwB,YAAY,KAAG,QAAQ,WAAW,EAAE,CAWxF,CAAC"}
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateHashInfo = exports.findBin = exports.parseHashFile = exports.compareHashes = void 0;
7
+ const md5_1 = __importDefault(require("md5"));
8
+ const cd_file_header_1 = require("./parse-zip/cd-file-header");
9
+ /**
10
+ * Comparing md5 hashes according to https://github.com/Esri/i3s-spec/blob/master/docs/2.0/slpk_hashtable.pcsl.md step 5
11
+ * @param hash1 hash to compare
12
+ * @param hash2 hash to compare
13
+ * @returns -1 if hash1 < hash2, 0 of hash1 === hash2, 1 if hash1 > hash2
14
+ */
15
+ const compareHashes = (hash1, hash2) => {
16
+ const h1 = new BigUint64Array(hash1.buffer, hash1.byteOffset, 2);
17
+ const h2 = new BigUint64Array(hash2.buffer, hash2.byteOffset, 2);
18
+ const diff = h1[0] === h2[0] ? h1[1] - h2[1] : h1[0] - h2[0];
19
+ if (diff < 0n) {
20
+ return -1;
21
+ }
22
+ else if (diff === 0n) {
23
+ return 0;
24
+ }
25
+ return 1;
26
+ };
27
+ exports.compareHashes = compareHashes;
28
+ /**
29
+ * Reads hash file from buffer and returns it in ready-to-use form
30
+ * @param hashFile - bufer containing hash file
31
+ * @returns Array containing file info
32
+ */
33
+ const parseHashFile = (hashFile) => {
34
+ const hashFileBuffer = Buffer.from(hashFile);
35
+ const hashArray = [];
36
+ for (let i = 0; i < hashFileBuffer.buffer.byteLength; i = i + 24) {
37
+ const offsetBuffer = new DataView(hashFileBuffer.buffer.slice(hashFileBuffer.byteOffset + i + 16, hashFileBuffer.byteOffset + i + 24));
38
+ const offset = offsetBuffer.getBigUint64(offsetBuffer.byteOffset, true);
39
+ hashArray.push({
40
+ hash: Buffer.from(hashFileBuffer.subarray(hashFileBuffer.byteOffset + i, hashFileBuffer.byteOffset + i + 16)),
41
+ offset
42
+ });
43
+ }
44
+ return hashArray;
45
+ };
46
+ exports.parseHashFile = parseHashFile;
47
+ /**
48
+ * Binary search in the hash info
49
+ * @param hashToSearch hash that we need to find
50
+ * @returns required hash element or undefined if not found
51
+ */
52
+ const findBin = (hashToSearch, hashArray) => {
53
+ let lowerBorder = 0;
54
+ let upperBorder = hashArray.length;
55
+ while (upperBorder - lowerBorder > 1) {
56
+ const middle = lowerBorder + Math.floor((upperBorder - lowerBorder) / 2);
57
+ const value = (0, exports.compareHashes)(hashArray[middle].hash, hashToSearch);
58
+ if (value === 0) {
59
+ return hashArray[middle];
60
+ }
61
+ else if (value < 0) {
62
+ lowerBorder = middle;
63
+ }
64
+ else {
65
+ upperBorder = middle;
66
+ }
67
+ }
68
+ return undefined;
69
+ };
70
+ exports.findBin = findBin;
71
+ /**
72
+ * generates hash info from central directory
73
+ * @param fileProvider - provider of the archive
74
+ * @returns ready to use hash info
75
+ */
76
+ const generateHashInfo = async (fileProvider) => {
77
+ const zipCDIterator = (0, cd_file_header_1.zipCDFileHeaderGenerator)(fileProvider);
78
+ const hashInfo = [];
79
+ for await (const cdHeader of zipCDIterator) {
80
+ hashInfo.push({
81
+ hash: Buffer.from((0, md5_1.default)(cdHeader.fileName.split('\\').join('/').toLocaleLowerCase()), 'hex'),
82
+ offset: cdHeader.localHeaderOffset
83
+ });
84
+ }
85
+ hashInfo.sort((a, b) => (0, exports.compareHashes)(a.hash, b.hash));
86
+ return hashInfo;
87
+ };
88
+ exports.generateHashInfo = generateHashInfo;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,13 @@
1
1
  export { ZipLoader } from './zip-loader';
2
2
  export { ZipWriter } from './zip-writer';
3
3
  export { default as TarBuilder } from './tar-builder';
4
+ export type { FileProvider } from './file-provider/file-provider';
5
+ export { FileHandleFile } from './file-provider/file-handle-file';
6
+ export { DataViewFile } from './file-provider/data-view-file';
7
+ export { parseZipCDFileHeader, zipCDFileHeaderGenerator, signature as cdSignature } from './parse-zip/cd-file-header';
8
+ export { parseZipLocalFileHeader, signature as localHeaderSignature } from './parse-zip/local-file-header';
9
+ export { parseEoCDRecord } from './parse-zip/end-of-central-directory';
10
+ export { searchFromTheEnd } from './parse-zip/search-from-the-end';
11
+ export type { HashElement } from './hash-file-utility';
12
+ export { compareHashes, parseHashFile, findBin, generateHashInfo } from './hash-file-utility';
4
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,cAAc,CAAC;AACvC,OAAO,EAAC,SAAS,EAAC,MAAM,cAAc,CAAC;AACvC,OAAO,EAAC,OAAO,IAAI,UAAU,EAAC,MAAM,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,cAAc,CAAC;AACvC,OAAO,EAAC,SAAS,EAAC,MAAM,cAAc,CAAC;AACvC,OAAO,EAAC,OAAO,IAAI,UAAU,EAAC,MAAM,eAAe,CAAC;AAEpD,YAAY,EAAC,YAAY,EAAC,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAC,cAAc,EAAC,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAC,YAAY,EAAC,MAAM,gCAAgC,CAAC;AAE5D,OAAO,EACL,oBAAoB,EACpB,wBAAwB,EACxB,SAAS,IAAI,WAAW,EACzB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,uBAAuB,EACvB,SAAS,IAAI,oBAAoB,EAClC,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAC,eAAe,EAAC,MAAM,sCAAsC,CAAC;AACrE,OAAO,EAAC,gBAAgB,EAAC,MAAM,iCAAiC,CAAC;AAEjE,YAAY,EAAC,WAAW,EAAC,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAC,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,gBAAgB,EAAC,MAAM,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -3,10 +3,30 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.TarBuilder = exports.ZipWriter = exports.ZipLoader = void 0;
6
+ exports.generateHashInfo = exports.findBin = exports.parseHashFile = exports.compareHashes = exports.searchFromTheEnd = exports.parseEoCDRecord = exports.localHeaderSignature = exports.parseZipLocalFileHeader = exports.cdSignature = exports.zipCDFileHeaderGenerator = exports.parseZipCDFileHeader = exports.DataViewFile = exports.FileHandleFile = exports.TarBuilder = exports.ZipWriter = exports.ZipLoader = void 0;
7
7
  var zip_loader_1 = require("./zip-loader");
8
8
  Object.defineProperty(exports, "ZipLoader", { enumerable: true, get: function () { return zip_loader_1.ZipLoader; } });
9
9
  var zip_writer_1 = require("./zip-writer");
10
10
  Object.defineProperty(exports, "ZipWriter", { enumerable: true, get: function () { return zip_writer_1.ZipWriter; } });
11
11
  var tar_builder_1 = require("./tar-builder");
12
12
  Object.defineProperty(exports, "TarBuilder", { enumerable: true, get: function () { return __importDefault(tar_builder_1).default; } });
13
+ var file_handle_file_1 = require("./file-provider/file-handle-file");
14
+ Object.defineProperty(exports, "FileHandleFile", { enumerable: true, get: function () { return file_handle_file_1.FileHandleFile; } });
15
+ var data_view_file_1 = require("./file-provider/data-view-file");
16
+ Object.defineProperty(exports, "DataViewFile", { enumerable: true, get: function () { return data_view_file_1.DataViewFile; } });
17
+ var cd_file_header_1 = require("./parse-zip/cd-file-header");
18
+ Object.defineProperty(exports, "parseZipCDFileHeader", { enumerable: true, get: function () { return cd_file_header_1.parseZipCDFileHeader; } });
19
+ Object.defineProperty(exports, "zipCDFileHeaderGenerator", { enumerable: true, get: function () { return cd_file_header_1.zipCDFileHeaderGenerator; } });
20
+ Object.defineProperty(exports, "cdSignature", { enumerable: true, get: function () { return cd_file_header_1.signature; } });
21
+ var local_file_header_1 = require("./parse-zip/local-file-header");
22
+ Object.defineProperty(exports, "parseZipLocalFileHeader", { enumerable: true, get: function () { return local_file_header_1.parseZipLocalFileHeader; } });
23
+ Object.defineProperty(exports, "localHeaderSignature", { enumerable: true, get: function () { return local_file_header_1.signature; } });
24
+ var end_of_central_directory_1 = require("./parse-zip/end-of-central-directory");
25
+ Object.defineProperty(exports, "parseEoCDRecord", { enumerable: true, get: function () { return end_of_central_directory_1.parseEoCDRecord; } });
26
+ var search_from_the_end_1 = require("./parse-zip/search-from-the-end");
27
+ Object.defineProperty(exports, "searchFromTheEnd", { enumerable: true, get: function () { return search_from_the_end_1.searchFromTheEnd; } });
28
+ var hash_file_utility_1 = require("./hash-file-utility");
29
+ Object.defineProperty(exports, "compareHashes", { enumerable: true, get: function () { return hash_file_utility_1.compareHashes; } });
30
+ Object.defineProperty(exports, "parseHashFile", { enumerable: true, get: function () { return hash_file_utility_1.parseHashFile; } });
31
+ Object.defineProperty(exports, "findBin", { enumerable: true, get: function () { return hash_file_utility_1.findBin; } });
32
+ Object.defineProperty(exports, "generateHashInfo", { enumerable: true, get: function () { return hash_file_utility_1.generateHashInfo; } });
@@ -0,0 +1,36 @@
1
+ import { FileProvider } from '../file-provider/file-provider';
2
+ import { ZipSignature } from './search-from-the-end';
3
+ /**
4
+ * zip central directory file header info
5
+ * according to https://en.wikipedia.org/wiki/ZIP_(file_format)
6
+ */
7
+ export type ZipCDFileHeader = {
8
+ /** Compressed size */
9
+ compressedSize: bigint;
10
+ /** Uncompressed size */
11
+ uncompressedSize: bigint;
12
+ /** Extra field size */
13
+ extraFieldLength: number;
14
+ /** File name length */
15
+ fileNameLength: number;
16
+ /** File name */
17
+ fileName: string;
18
+ /** Extra field offset */
19
+ extraOffset: bigint;
20
+ /** Relative offset of local file header */
21
+ localHeaderOffset: bigint;
22
+ };
23
+ export declare const signature: ZipSignature;
24
+ /**
25
+ * Parses central directory file header of zip file
26
+ * @param headerOffset - offset in the archive where header starts
27
+ * @param buffer - buffer containing whole array
28
+ * @returns Info from the header
29
+ */
30
+ export declare const parseZipCDFileHeader: (headerOffset: bigint, buffer: FileProvider) => Promise<ZipCDFileHeader | null>;
31
+ /**
32
+ * Create iterator over files of zip archive
33
+ * @param fileProvider - file provider that provider random access to the file
34
+ */
35
+ export declare function zipCDFileHeaderGenerator(fileProvider: FileProvider): AsyncGenerator<ZipCDFileHeader, void, unknown>;
36
+ //# sourceMappingURL=cd-file-header.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cd-file-header.d.ts","sourceRoot":"","sources":["../../src/parse-zip/cd-file-header.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,gCAAgC,CAAC;AAE5D,OAAO,EAAC,YAAY,EAAC,MAAM,uBAAuB,CAAC;AAEnD;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,sBAAsB;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,wBAAwB;IACxB,gBAAgB,EAAE,MAAM,CAAC;IACzB,uBAAuB;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,uBAAuB;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,yBAAyB;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,2CAA2C;IAC3C,iBAAiB,EAAE,MAAM,CAAC;CAC3B,CAAC;AAUF,eAAO,MAAM,SAAS,EAAE,YAAuC,CAAC;AAEhE;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,iBACjB,MAAM,UACZ,YAAY,KACnB,QAAQ,eAAe,GAAG,IAAI,CAqDhC,CAAC;AAEF;;;GAGG;AACH,wBAAuB,wBAAwB,CAAC,YAAY,EAAE,YAAY,kDAUzE"}