@loaders.gl/archives 0.0.0-stage → 5.0.0-alpha.13

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 (167) hide show
  1. package/README.md +4 -2
  2. package/dist/dist.dev.js +5101 -0
  3. package/dist/dist.min.js +9 -0
  4. package/dist/index.cjs +2982 -0
  5. package/dist/index.cjs.map +6 -0
  6. package/dist/index.d.ts +11 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +11 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/tar/index.cjs +576 -0
  11. package/dist/tar/index.cjs.map +6 -0
  12. package/dist/tar/index.d.ts +8 -0
  13. package/dist/tar/index.d.ts.map +1 -0
  14. package/dist/tar/index.js +9 -0
  15. package/dist/tar/index.js.map +1 -0
  16. package/dist/tar/lib/header.d.ts +9 -0
  17. package/dist/tar/lib/header.d.ts.map +1 -0
  18. package/dist/tar/lib/header.js +76 -0
  19. package/dist/tar/lib/header.js.map +1 -0
  20. package/dist/tar/lib/tar.d.ts +29 -0
  21. package/dist/tar/lib/tar.d.ts.map +1 -0
  22. package/dist/tar/lib/tar.js +136 -0
  23. package/dist/tar/lib/tar.js.map +1 -0
  24. package/dist/tar/lib/types.d.ts +48 -0
  25. package/dist/tar/lib/types.d.ts.map +1 -0
  26. package/dist/tar/lib/types.js +5 -0
  27. package/dist/tar/lib/types.js.map +1 -0
  28. package/dist/tar/lib/utils.d.ts +23 -0
  29. package/dist/tar/lib/utils.d.ts.map +1 -0
  30. package/dist/tar/lib/utils.js +52 -0
  31. package/dist/tar/lib/utils.js.map +1 -0
  32. package/dist/tar/tar-archive.d.ts +4 -0
  33. package/dist/tar/tar-archive.d.ts.map +1 -0
  34. package/dist/tar/tar-archive.js +97 -0
  35. package/dist/tar/tar-archive.js.map +1 -0
  36. package/dist/tar/tar-builder.d.ts +28 -0
  37. package/dist/tar/tar-builder.d.ts.map +1 -0
  38. package/dist/tar/tar-builder.js +38 -0
  39. package/dist/tar/tar-builder.js.map +1 -0
  40. package/dist/tar/tar-format.d.ts +12 -0
  41. package/dist/tar/tar-format.d.ts.map +1 -0
  42. package/dist/tar/tar-format.js +15 -0
  43. package/dist/tar/tar-format.js.map +1 -0
  44. package/dist/tar/tar-loader-types.d.ts +15 -0
  45. package/dist/tar/tar-loader-types.d.ts.map +1 -0
  46. package/dist/tar/tar-loader-types.js +20 -0
  47. package/dist/tar/tar-loader-types.js.map +1 -0
  48. package/dist/tar/tar-loader.cjs +285 -0
  49. package/dist/tar/tar-loader.cjs.map +6 -0
  50. package/dist/tar/tar-loader.d.ts +3 -0
  51. package/dist/tar/tar-loader.d.ts.map +1 -0
  52. package/dist/tar/tar-loader.js +12 -0
  53. package/dist/tar/tar-loader.js.map +1 -0
  54. package/dist/tar/tar-writer.cjs +114 -0
  55. package/dist/tar/tar-writer.cjs.map +6 -0
  56. package/dist/tar/tar-writer.d.ts +20 -0
  57. package/dist/tar/tar-writer.d.ts.map +1 -0
  58. package/dist/tar/tar-writer.js +94 -0
  59. package/dist/tar/tar-writer.js.map +1 -0
  60. package/dist/zip/filesystems/IndexedArchive.d.ts +40 -0
  61. package/dist/zip/filesystems/IndexedArchive.d.ts.map +1 -0
  62. package/dist/zip/filesystems/IndexedArchive.js +99 -0
  63. package/dist/zip/filesystems/IndexedArchive.js.map +1 -0
  64. package/dist/zip/filesystems/zip-filesystem.d.ts +53 -0
  65. package/dist/zip/filesystems/zip-filesystem.d.ts.map +1 -0
  66. package/dist/zip/filesystems/zip-filesystem.js +231 -0
  67. package/dist/zip/filesystems/zip-filesystem.js.map +1 -0
  68. package/dist/zip/hash-file-utility.d.ts +20 -0
  69. package/dist/zip/hash-file-utility.d.ts.map +1 -0
  70. package/dist/zip/hash-file-utility.js +83 -0
  71. package/dist/zip/hash-file-utility.js.map +1 -0
  72. package/dist/zip/index.cjs +4614 -0
  73. package/dist/zip/index.cjs.map +6 -0
  74. package/dist/zip/index.d.ts +18 -0
  75. package/dist/zip/index.d.ts.map +1 -0
  76. package/dist/zip/index.js +14 -0
  77. package/dist/zip/index.js.map +1 -0
  78. package/dist/zip/indexed-zip-writer.cjs +1211 -0
  79. package/dist/zip/indexed-zip-writer.cjs.map +6 -0
  80. package/dist/zip/indexed-zip-writer.d.ts +46 -0
  81. package/dist/zip/indexed-zip-writer.d.ts.map +1 -0
  82. package/dist/zip/indexed-zip-writer.js +229 -0
  83. package/dist/zip/indexed-zip-writer.js.map +1 -0
  84. package/dist/zip/lib/zip-composition.d.ts +37 -0
  85. package/dist/zip/lib/zip-composition.d.ts.map +1 -0
  86. package/dist/zip/lib/zip-composition.js +157 -0
  87. package/dist/zip/lib/zip-composition.js.map +1 -0
  88. package/dist/zip/lib/zip-entry-metadata.d.ts +34 -0
  89. package/dist/zip/lib/zip-entry-metadata.d.ts.map +1 -0
  90. package/dist/zip/lib/zip-entry-metadata.js +132 -0
  91. package/dist/zip/lib/zip-entry-metadata.js.map +1 -0
  92. package/dist/zip/lib/zip-serialization.d.ts +36 -0
  93. package/dist/zip/lib/zip-serialization.d.ts.map +1 -0
  94. package/dist/zip/lib/zip-serialization.js +148 -0
  95. package/dist/zip/lib/zip-serialization.js.map +1 -0
  96. package/dist/zip/lib/zip64-extra-field.d.ts +20 -0
  97. package/dist/zip/lib/zip64-extra-field.d.ts.map +1 -0
  98. package/dist/zip/lib/zip64-extra-field.js +53 -0
  99. package/dist/zip/lib/zip64-extra-field.js.map +1 -0
  100. package/dist/zip/zip-archive-types.d.ts +56 -0
  101. package/dist/zip/zip-archive-types.d.ts.map +1 -0
  102. package/dist/zip/zip-archive-types.js +5 -0
  103. package/dist/zip/zip-archive-types.js.map +1 -0
  104. package/dist/zip/zip-archive.d.ts +101 -0
  105. package/dist/zip/zip-archive.d.ts.map +1 -0
  106. package/dist/zip/zip-archive.js +593 -0
  107. package/dist/zip/zip-archive.js.map +1 -0
  108. package/dist/zip/zip-encode-types.d.ts +41 -0
  109. package/dist/zip/zip-encode-types.d.ts.map +1 -0
  110. package/dist/zip/zip-encode-types.js +5 -0
  111. package/dist/zip/zip-encode-types.js.map +1 -0
  112. package/dist/zip/zip-encode.cjs +1154 -0
  113. package/dist/zip/zip-encode.cjs.map +6 -0
  114. package/dist/zip/zip-encode.d.ts +15 -0
  115. package/dist/zip/zip-encode.d.ts.map +1 -0
  116. package/dist/zip/zip-encode.js +312 -0
  117. package/dist/zip/zip-encode.js.map +1 -0
  118. package/dist/zip/zip-format.d.ts +14 -0
  119. package/dist/zip/zip-format.d.ts.map +1 -0
  120. package/dist/zip/zip-format.js +17 -0
  121. package/dist/zip/zip-format.js.map +1 -0
  122. package/dist/zip/zip-loader-types.d.ts +15 -0
  123. package/dist/zip/zip-loader-types.d.ts.map +1 -0
  124. package/dist/zip/zip-loader-types.js +20 -0
  125. package/dist/zip/zip-loader-types.js.map +1 -0
  126. package/dist/zip/zip-loader.cjs +2681 -0
  127. package/dist/zip/zip-loader.cjs.map +6 -0
  128. package/dist/zip/zip-loader.d.ts +3 -0
  129. package/dist/zip/zip-loader.d.ts.map +1 -0
  130. package/dist/zip/zip-loader.js +38 -0
  131. package/dist/zip/zip-loader.js.map +1 -0
  132. package/dist/zip/zip-writer.cjs +1208 -0
  133. package/dist/zip/zip-writer.cjs.map +6 -0
  134. package/dist/zip/zip-writer.d.ts +40 -0
  135. package/dist/zip/zip-writer.d.ts.map +1 -0
  136. package/dist/zip/zip-writer.js +48 -0
  137. package/dist/zip/zip-writer.js.map +1 -0
  138. package/package.json +88 -4
  139. package/src/index.ts +14 -0
  140. package/src/tar/index.ts +11 -0
  141. package/src/tar/lib/header.ts +82 -0
  142. package/src/tar/lib/tar.ts +160 -0
  143. package/src/tar/lib/types.ts +51 -0
  144. package/src/tar/lib/utils.ts +55 -0
  145. package/src/tar/tar-archive.ts +98 -0
  146. package/src/tar/tar-builder.ts +47 -0
  147. package/src/tar/tar-format.ts +17 -0
  148. package/src/tar/tar-loader-types.ts +36 -0
  149. package/src/tar/tar-loader.ts +17 -0
  150. package/src/tar/tar-writer.ts +102 -0
  151. package/src/zip/filesystems/IndexedArchive.ts +110 -0
  152. package/src/zip/filesystems/zip-filesystem.ts +244 -0
  153. package/src/zip/hash-file-utility.ts +109 -0
  154. package/src/zip/index.ts +38 -0
  155. package/src/zip/indexed-zip-writer.ts +345 -0
  156. package/src/zip/lib/zip-composition.ts +243 -0
  157. package/src/zip/lib/zip-entry-metadata.ts +157 -0
  158. package/src/zip/lib/zip-serialization.ts +206 -0
  159. package/src/zip/lib/zip64-extra-field.ts +84 -0
  160. package/src/zip/zip-archive-types.ts +64 -0
  161. package/src/zip/zip-archive.ts +733 -0
  162. package/src/zip/zip-encode-types.ts +53 -0
  163. package/src/zip/zip-encode.ts +370 -0
  164. package/src/zip/zip-format.ts +19 -0
  165. package/src/zip/zip-loader-types.ts +36 -0
  166. package/src/zip/zip-loader.ts +43 -0
  167. package/src/zip/zip-writer.ts +73 -0
@@ -0,0 +1,243 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {
6
+ concatenateArrayBuffers,
7
+ path,
8
+ NodeFilesystem,
9
+ NodeFile,
10
+ toArrayBuffer
11
+ } from '@loaders.gl/core/loader-utils';
12
+ import {encodeUtf8Text, validateZipPath} from './zip-entry-metadata';
13
+ import {CRC32Hash} from '@loaders.gl/crypto';
14
+ import type {ZipArchiveOptions} from '../zip-archive-types';
15
+ import type {ZipEncodeOptions} from '../zip-encode-types';
16
+
17
+ import {ZipArchive} from '../zip-archive';
18
+ import {
19
+ encodeZipLocalHeader,
20
+ encodeZipCentralHeader,
21
+ encodeZipEndRecords
22
+ } from './zip-serialization';
23
+
24
+ /** Bounds for the retained Node directory-composition API. */
25
+ export type ZipCompositionOptions = Pick<
26
+ ZipEncodeOptions,
27
+ 'maxArchiveBytes' | 'maxEntries' | 'maxDirectoryBytes' | 'signal'
28
+ >;
29
+
30
+ /** Captured composition limits and the running central-directory byte count. */
31
+ interface ZipCompositionState {
32
+ /** Maximum final output bytes. */
33
+ readonly maxArchiveBytes: bigint;
34
+ /** Maximum central-directory bytes. */
35
+ readonly maxDirectoryBytes: number;
36
+ /** Maximum entry records. */
37
+ readonly maxEntries: number;
38
+ /** Cooperative cancellation between file reads and writes. */
39
+ readonly signal?: AbortSignal;
40
+ /** Bytes retained in central headers so far. */
41
+ directoryBytes: number;
42
+ }
43
+ /** Creates populated STORE headers through the common ZIP32/ZIP64 serializer. */
44
+ async function generateFileHeaders(
45
+ fileName: string,
46
+ file: ArrayBuffer,
47
+ offset: bigint
48
+ ): Promise<[Uint8Array, Uint8Array]> {
49
+ validateZipPath(fileName);
50
+ const header = {
51
+ nameBytes: encodeUtf8Text(fileName),
52
+ date: new Date('1980-01-01T00:00:00Z'),
53
+ flags: 0x800,
54
+ method: 0 as const,
55
+ crc32: Number.parseInt(await new CRC32Hash().hash(file, 'hex'), 16),
56
+ size: BigInt(file.byteLength),
57
+ compressedSize: BigInt(file.byteLength),
58
+ localHeaderOffset: offset,
59
+ zip64: offset >= 0xffffffffn || file.byteLength >= 0xffffffff
60
+ };
61
+ return [
62
+ new Uint8Array(concatenateArrayBuffers(encodeZipLocalHeader(header), file)),
63
+ encodeZipCentralHeader(header)
64
+ ];
65
+ }
66
+
67
+ /** Appends one STORE entry, validating the existing archive before mutation and closing on completion. */
68
+ export async function addOneFile(
69
+ zipUrl: string,
70
+ file: ArrayBuffer | ArrayBufferView,
71
+ fileName: string,
72
+ options: ZipArchiveOptions = {}
73
+ ): Promise<void> {
74
+ await ZipArchive.appendEntry(new NodeFile(zipUrl, 'a+'), fileName, file, options);
75
+ }
76
+
77
+ /**
78
+ * creates zip archive with no compression
79
+ * @note This is a node specific function that works on files
80
+ * @param inputPath path where files for the achive are stored
81
+ * @param outputPath path where zip archive will be placed
82
+ */
83
+ export async function createZip(
84
+ inputPath: string,
85
+ outputPath: string,
86
+ createAdditionalData?: (
87
+ fileList: {fileName: string; localHeaderOffset: bigint}[]
88
+ ) => Promise<{path: string; file: ArrayBuffer}>,
89
+ options: ZipCompositionOptions = {}
90
+ ) {
91
+ const state: ZipCompositionState = {
92
+ maxArchiveBytes: options.maxArchiveBytes ?? 1024n ** 4n,
93
+ maxEntries: options.maxEntries ?? 100000,
94
+ maxDirectoryBytes: options.maxDirectoryBytes ?? 32 * 1024 * 1024,
95
+ signal: options.signal,
96
+ directoryBytes: 0
97
+ };
98
+ if (
99
+ typeof state.maxArchiveBytes !== 'bigint' ||
100
+ state.maxArchiveBytes < 0n ||
101
+ !Number.isSafeInteger(state.maxEntries) ||
102
+ state.maxEntries < 1 ||
103
+ !Number.isSafeInteger(state.maxDirectoryBytes) ||
104
+ state.maxDirectoryBytes < 1
105
+ )
106
+ throw new RangeError('Invalid ZIP composition limits');
107
+ if (state.maxArchiveBytes < 22n) throw new RangeError('ZIP composition archive limit exceeded');
108
+ state.signal?.throwIfAborted();
109
+ const fileIterator = getFileIterator(inputPath);
110
+
111
+ const resFile = new NodeFile(outputPath, 'w');
112
+ const fileList: {fileName: string; localHeaderOffset: bigint}[] = [];
113
+
114
+ const cdArray: ArrayBuffer[] = [];
115
+ try {
116
+ for await (const file of fileIterator) {
117
+ await addFile(file, resFile, cdArray, state, fileList);
118
+ }
119
+ if (createAdditionalData) {
120
+ const additionaldata = await createAdditionalData(fileList);
121
+ await addFile(additionaldata, resFile, cdArray, state);
122
+ }
123
+ state.signal?.throwIfAborted();
124
+ const cdOffset = (await resFile.stat()).bigsize;
125
+ const cd = concatenateArrayBuffers(...cdArray);
126
+ await resFile.append(new Uint8Array(cd));
127
+ const eoCDStart = (await resFile.stat()).bigsize;
128
+ await resFile.append(
129
+ new Uint8Array(
130
+ encodeZipEndRecords(
131
+ BigInt(cdArray.length),
132
+ BigInt(cd.byteLength),
133
+ cdOffset,
134
+ eoCDStart > 0x7fffffffn || cdArray.length >= 0xffff
135
+ )
136
+ )
137
+ );
138
+ } finally {
139
+ await resFile.close();
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Adds file to zip parts
145
+ * @param file file to add
146
+ * @param resFile zip file body
147
+ * @param cdArray zip file central directory
148
+ * @param fileList list of file offsets
149
+ */
150
+ async function addFile(
151
+ file: {path: string; file: ArrayBuffer},
152
+ resFile: NodeFile,
153
+ cdArray: ArrayBuffer[],
154
+ state: ZipCompositionState,
155
+ fileList?: {fileName: string; localHeaderOffset: bigint}[]
156
+ ) {
157
+ state.signal?.throwIfAborted();
158
+ const size = (await resFile.stat()).bigsize;
159
+ fileList?.push({fileName: file.path, localHeaderOffset: size});
160
+ const [localPart, cdHeaderPart] = await generateFileHeaders(file.path, file.file, size);
161
+ const directoryBytes = state.directoryBytes + cdHeaderPart.byteLength;
162
+ const directoryOffset = size + BigInt(localPart.byteLength);
163
+ const count = cdArray.length + 1;
164
+ const footer = encodeZipEndRecords(
165
+ BigInt(count),
166
+ BigInt(directoryBytes),
167
+ directoryOffset,
168
+ directoryOffset + BigInt(directoryBytes) > 0x7fffffffn || count >= 0xffff
169
+ );
170
+ if (
171
+ count > state.maxEntries ||
172
+ directoryBytes > state.maxDirectoryBytes ||
173
+ directoryOffset + BigInt(directoryBytes + footer.length) > state.maxArchiveBytes
174
+ )
175
+ throw new RangeError('ZIP composition metadata/archive limit exceeded');
176
+ state.signal?.throwIfAborted();
177
+ await resFile.append(localPart);
178
+ state.directoryBytes = directoryBytes;
179
+ cdArray.push(toArrayBuffer(cdHeaderPart));
180
+ }
181
+
182
+ /**
183
+ * creates iterator providing buffer with file content and path to every file in the input folder
184
+ * @param inputPath path to the input folder
185
+ * @returns iterator
186
+ */
187
+ export function getFileIterator(
188
+ inputPath: string
189
+ ): AsyncIterable<{path: string; file: ArrayBuffer}> {
190
+ async function* iterable() {
191
+ const fileList = await getAllFiles(inputPath);
192
+ for (const filePath of fileList) {
193
+ const provider = new NodeFile(path.join(inputPath, filePath), 'r');
194
+ try {
195
+ const {bigsize} = await provider.stat();
196
+ const file = await provider.read(0, Number(bigsize));
197
+ yield {path: filePath, file};
198
+ } finally {
199
+ await provider.close();
200
+ }
201
+ }
202
+ }
203
+ return iterable();
204
+ }
205
+
206
+ /**
207
+ * creates a list of relative paths to all files in the provided folder
208
+ * @param basePath path of the root folder
209
+ * @param subfolder relative path from the root folder.
210
+ * @returns list of paths
211
+ */
212
+ export async function getAllFiles(
213
+ basePath: string,
214
+ subfolder: string = '',
215
+ fsPassed?: NodeFilesystem
216
+ ): Promise<string[]> {
217
+ const fs = fsPassed ? fsPassed : new NodeFilesystem({});
218
+ const files = await fs.readdir(pathJoin(basePath, subfolder));
219
+
220
+ const arrayOfFiles: string[] = [];
221
+
222
+ for (const file of files) {
223
+ const fullPath = pathJoin(basePath, subfolder, file);
224
+ if ((await fs.stat(fullPath)).isDirectory) {
225
+ const files = await getAllFiles(basePath, pathJoin(subfolder, file));
226
+ arrayOfFiles.push(...files);
227
+ } else {
228
+ arrayOfFiles.push(pathJoin(subfolder, file));
229
+ }
230
+ }
231
+
232
+ return arrayOfFiles;
233
+ }
234
+
235
+ /**
236
+ * removes empty parts from path array and joins it
237
+ * @param paths paths to join
238
+ * @returns joined path
239
+ */
240
+ function pathJoin(...paths: string[]): string {
241
+ const resPaths: string[] = paths.filter(val => val.length);
242
+ return path.join(...resPaths);
243
+ }
@@ -0,0 +1,157 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {CRC32Hash} from '@loaders.gl/crypto';
6
+ import {parseZip64ExtraField} from './zip64-extra-field';
7
+ import type {Zip64ExtraFieldDescription} from './zip64-extra-field';
8
+
9
+ /** DOS CP437 characters for bytes 128–255; ASCII/control bytes retain their code points. */
10
+ const CP437_HIGH =
11
+ 'ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ';
12
+
13
+ /** Header fields required by the common ZIP container reader. */
14
+ export interface ZipHeaderMetadata {
15
+ /** Decoded canonical relative path. */
16
+ readonly path: string;
17
+ /** Original filename bytes, used for local/central consistency. */
18
+ readonly nameBytes: Uint8Array;
19
+ /** General-purpose flags. */
20
+ readonly flags: number;
21
+ /** Compression method number. */
22
+ readonly method: number;
23
+ /** Expected payload CRC-32. */
24
+ readonly crc32: number;
25
+ /** Compressed length. */
26
+ readonly compressedSize: bigint;
27
+ /** Decoded length. */
28
+ readonly size: bigint;
29
+ /** Central local-header pointer; zero for a local header. */
30
+ readonly localHeaderOffset: bigint;
31
+ /** Decoded central comment. */
32
+ readonly comment: string;
33
+ /** Whether local size fields use ZIP64. */
34
+ readonly zip64: boolean;
35
+ }
36
+
37
+ /** Decodes common ZIP headers using shared ZIP64 extra-field parsing. */
38
+ export function decodeZipHeader(bytes: Uint8Array, central: boolean): ZipHeaderMetadata {
39
+ const header = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
40
+ const fixedBytes = central ? 46 : 30;
41
+ if (
42
+ bytes.byteLength < fixedBytes ||
43
+ header.getUint32(0, true) !== (central ? 0x02014b50 : 0x04034b50)
44
+ )
45
+ throw new Error('Invalid ZIP archive: missing entry header');
46
+ const nameLength = header.getUint16(central ? 28 : 26, true);
47
+ const extraLength = header.getUint16(central ? 30 : 28, true);
48
+ const commentLength = central ? header.getUint16(32, true) : 0;
49
+ if (bytes.byteLength !== fixedBytes + nameLength + extraLength + commentLength)
50
+ throw new Error('Invalid ZIP archive: truncated entry metadata');
51
+ const flags = header.getUint16(central ? 8 : 6, true);
52
+ const method = header.getUint16(central ? 10 : 8, true);
53
+ if (flags & (1 | 0x40 | 0x2000)) throw new Error('Unsupported ZIP encryption');
54
+ if (flags & ~(8 | 0x800 | (method === 8 ? 6 : 0))) throw new Error('Unsupported ZIP flags');
55
+ if (method !== 0 && method !== 8)
56
+ throw new Error(`Unsupported ZIP compression method: ${method}`);
57
+ let compressedSize = BigInt(header.getUint32(central ? 20 : 18, true));
58
+ let size = BigInt(header.getUint32(central ? 24 : 22, true));
59
+ let localHeaderOffset = central ? BigInt(header.getUint32(42, true)) : 0n;
60
+ let disk = central ? BigInt(header.getUint16(34, true)) : 0n;
61
+ let zip64 = compressedSize === 0xffffffffn || size === 0xffffffffn;
62
+ const fields: Zip64ExtraFieldDescription<
63
+ 'size' | 'compressedSize' | 'localHeaderOffset' | 'disk'
64
+ >[] = [];
65
+ if (central) {
66
+ if (size === 0xffffffffn) fields.push({name: 'size', byteLength: 8});
67
+ if (compressedSize === 0xffffffffn) fields.push({name: 'compressedSize', byteLength: 8});
68
+ if (localHeaderOffset === 0xffffffffn) fields.push({name: 'localHeaderOffset', byteLength: 8});
69
+ if (disk === 0xffffn) fields.push({name: 'disk', byteLength: 4});
70
+ } else if (zip64)
71
+ fields.push({name: 'size', byteLength: 8}, {name: 'compressedSize', byteLength: 8});
72
+ const extraBytes = bytes.subarray(fixedBytes + nameLength, fixedBytes + nameLength + extraLength);
73
+ const extra = new DataView(extraBytes.buffer, extraBytes.byteOffset, extraBytes.byteLength);
74
+ const extraValues = parseZip64ExtraField(extra, fields);
75
+ if (size === 0xffffffffn) size = extraValues.size!;
76
+ if (compressedSize === 0xffffffffn) compressedSize = extraValues.compressedSize!;
77
+ if (localHeaderOffset === 0xffffffffn) localHeaderOffset = extraValues.localHeaderOffset!;
78
+ if (disk === 0xffffn) disk = extraValues.disk!;
79
+ if (disk !== 0n) throw new Error('Unsupported split ZIP archive');
80
+ const nameBytes = bytes.subarray(fixedBytes, fixedBytes + nameLength);
81
+ let path = decodeZipText(nameBytes, Boolean(flags & 0x800));
82
+ const commentBytes = bytes.subarray(fixedBytes + nameLength + extraLength);
83
+ let comment = decodeZipText(commentBytes, Boolean(flags & 0x800));
84
+ for (let offset = 0; offset < extra.byteLength; ) {
85
+ if (offset + 4 > extra.byteLength)
86
+ throw new Error('Invalid ZIP archive: truncated extra field');
87
+ const identifier = extra.getUint16(offset, true);
88
+ const length = extra.getUint16(offset + 2, true);
89
+ if (offset + 4 + length > extra.byteLength)
90
+ throw new Error('Invalid ZIP archive: truncated extra field');
91
+ // Nonseekable Python ZIP64 output can retain zero legacy sizes and indicate the
92
+ // descriptor width only through its local ZIP64 extra record.
93
+ if (!central && flags & 8 && identifier === 1) {
94
+ if (length >= 16) zip64 = true;
95
+ }
96
+ if (identifier === 0x7075 || identifier === 0x6375) {
97
+ const original = identifier === 0x7075 ? nameBytes : commentBytes;
98
+ if (length < 5) throw new Error('Invalid ZIP archive: truncated Unicode extra field');
99
+ if (
100
+ extra.getUint8(offset + 4) === 1 &&
101
+ extra.getUint32(offset + 5, true) === calculateZipChecksum(original)
102
+ ) {
103
+ const text = decodeZipText(extraBytes.subarray(offset + 9, offset + 4 + length), true);
104
+ if (identifier === 0x7075) path = text;
105
+ else comment = text;
106
+ }
107
+ }
108
+ offset += 4 + length;
109
+ }
110
+ validateZipPath(path);
111
+ return {
112
+ path,
113
+ nameBytes,
114
+ flags,
115
+ method,
116
+ crc32: header.getUint32(central ? 16 : 14, true),
117
+ compressedSize,
118
+ size,
119
+ localHeaderOffset,
120
+ comment,
121
+ zip64
122
+ };
123
+ }
124
+
125
+ /** Decodes UTF-8 strictly or the ZIP legacy CP437 character set. */
126
+ export function decodeZipText(bytes: Uint8Array, utf8: boolean): string {
127
+ if (utf8) return new TextDecoder('utf-8', {fatal: true, ignoreBOM: true}).decode(bytes);
128
+ return Array.from(bytes, value =>
129
+ value < 128 ? String.fromCharCode(value) : CP437_HIGH[value - 128]
130
+ ).join('');
131
+ }
132
+
133
+ /** Validates archive-relative paths without silently rewriting their identity. */
134
+ export function validateZipPath(path: string): void {
135
+ const segments = path.endsWith('/') ? path.slice(0, -1).split('/') : path.split('/');
136
+ if (
137
+ !path ||
138
+ path.includes('\\') ||
139
+ path.includes('\0') ||
140
+ /^[A-Za-z]:/.test(path) ||
141
+ segments.some(segment => !segment || segment === '.' || segment === '..')
142
+ )
143
+ throw new Error('Invalid ZIP archive: noncanonical relative path');
144
+ }
145
+
146
+ /** Uses the shared CRC implementation for small filename/metadata checks. */
147
+ export function calculateZipChecksum(bytes: Uint8Array): number {
148
+ return Number.parseInt(new CRC32Hash().hashSync(bytes.slice().buffer, 'hex'), 16);
149
+ }
150
+
151
+ /** Rejects unpaired UTF-16 surrogates instead of silently replacing names/comments during encoding. */
152
+ export function encodeUtf8Text(value: string): Uint8Array<ArrayBuffer> {
153
+ const bytes = new TextEncoder().encode(value);
154
+ if (new TextDecoder('utf-8', {ignoreBOM: true}).decode(bytes) !== value)
155
+ throw new TypeError('ZIP names and entry comments must be well-formed Unicode');
156
+ return bytes;
157
+ }
@@ -0,0 +1,206 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /** Header metadata shared by general and indexed archive writers. */
6
+ export interface ZipHeaderEntry {
7
+ /** Encoded filename, at most 65535 bytes. */
8
+ readonly nameBytes: Uint8Array;
9
+ /** Encoded entry comment. */
10
+ readonly commentBytes?: Uint8Array;
11
+ /** UTC modification date. */
12
+ readonly date: Date;
13
+ /** STORE or DEFLATE method number. */
14
+ readonly method: 0 | 8;
15
+ /** General-purpose ZIP flags. */
16
+ readonly flags: number;
17
+ /** CRC-32 of decoded bytes. */
18
+ readonly crc32: number;
19
+ /** Decoded byte length. */
20
+ readonly size: bigint;
21
+ /** Encoded payload byte length. */
22
+ readonly compressedSize: bigint;
23
+ /** Absolute local-header offset. */
24
+ readonly localHeaderOffset: bigint;
25
+ /** Use ZIP64 size/offset fields. */
26
+ readonly zip64: boolean;
27
+ /** Set the DOS directory attribute. */
28
+ readonly directory?: boolean;
29
+ }
30
+
31
+ /** Largest ZIP32 size/offset field; reserved for ZIP64 sentinels by this writer. */
32
+ const ZIP32_SENTINEL = 0xffffffffn;
33
+
34
+ /** Serializes an independent local header; descriptors carry unknown final sizes. */
35
+ export function encodeZipLocalHeader(entry: ZipHeaderEntry): Uint8Array<ArrayBuffer> {
36
+ validateHeaderEntry(entry);
37
+ const bytes = new Uint8Array(30 + entry.nameBytes.length + (entry.zip64 ? 20 : 0));
38
+ const view = new DataView(bytes.buffer);
39
+ view.setUint32(0, 0x04034b50, true);
40
+ view.setUint16(4, getZipVersion(entry), true);
41
+ view.setUint16(6, entry.flags, true);
42
+ view.setUint16(8, entry.method, true);
43
+ view.setUint32(10, encodeZipTimestamp(entry.date), true);
44
+ const descriptor = Boolean(entry.flags & 8);
45
+ view.setUint32(14, descriptor ? 0 : entry.crc32, true);
46
+ view.setUint32(
47
+ 18,
48
+ entry.zip64 ? Number(ZIP32_SENTINEL) : descriptor ? 0 : Number(entry.compressedSize),
49
+ true
50
+ );
51
+ view.setUint32(
52
+ 22,
53
+ entry.zip64 ? Number(ZIP32_SENTINEL) : descriptor ? 0 : Number(entry.size),
54
+ true
55
+ );
56
+ view.setUint16(26, entry.nameBytes.length, true);
57
+ view.setUint16(28, entry.zip64 ? 20 : 0, true);
58
+ bytes.set(entry.nameBytes, 30);
59
+ if (entry.zip64) {
60
+ const extraOffset = 30 + entry.nameBytes.length;
61
+ view.setUint16(extraOffset, 1, true);
62
+ view.setUint16(extraOffset + 2, 16, true);
63
+ view.setBigUint64(extraOffset + 4, descriptor ? 0n : entry.size, true);
64
+ view.setBigUint64(extraOffset + 12, descriptor ? 0n : entry.compressedSize, true);
65
+ }
66
+ return bytes;
67
+ }
68
+
69
+ /** Serializes a central record with final sizes, CRC and local-header offset. */
70
+ export function encodeZipCentralHeader(entry: ZipHeaderEntry): Uint8Array<ArrayBuffer> {
71
+ validateHeaderEntry(entry);
72
+ const comment = entry.commentBytes ?? new Uint8Array();
73
+ const extraBytes = entry.zip64 ? 28 : 0;
74
+ const bytes = new Uint8Array(46 + entry.nameBytes.length + extraBytes + comment.length);
75
+ const view = new DataView(bytes.buffer);
76
+ view.setUint32(0, 0x02014b50, true);
77
+ view.setUint16(4, entry.zip64 ? 45 : 20, true);
78
+ view.setUint16(6, getZipVersion(entry), true);
79
+ view.setUint16(8, entry.flags, true);
80
+ view.setUint16(10, entry.method, true);
81
+ view.setUint32(12, encodeZipTimestamp(entry.date), true);
82
+ view.setUint32(16, entry.crc32, true);
83
+ view.setUint32(20, entry.zip64 ? Number(ZIP32_SENTINEL) : Number(entry.compressedSize), true);
84
+ view.setUint32(24, entry.zip64 ? Number(ZIP32_SENTINEL) : Number(entry.size), true);
85
+ view.setUint16(28, entry.nameBytes.length, true);
86
+ view.setUint16(30, extraBytes, true);
87
+ view.setUint16(32, comment.length, true);
88
+ view.setUint32(38, entry.directory ? 16 : 0, true);
89
+ view.setUint32(42, entry.zip64 ? Number(ZIP32_SENTINEL) : Number(entry.localHeaderOffset), true);
90
+ bytes.set(entry.nameBytes, 46);
91
+ if (entry.zip64) {
92
+ const extraOffset = 46 + entry.nameBytes.length;
93
+ view.setUint16(extraOffset, 1, true);
94
+ view.setUint16(extraOffset + 2, 24, true);
95
+ view.setBigUint64(extraOffset + 4, entry.size, true);
96
+ view.setBigUint64(extraOffset + 12, entry.compressedSize, true);
97
+ view.setBigUint64(extraOffset + 20, entry.localHeaderOffset, true);
98
+ }
99
+ bytes.set(comment, 46 + entry.nameBytes.length + extraBytes);
100
+ return bytes;
101
+ }
102
+
103
+ /** Serializes a signed descriptor matching the local header's ZIP32/ZIP64 layout. */
104
+ export function encodeZipDescriptor(entry: ZipHeaderEntry): Uint8Array<ArrayBuffer> {
105
+ validateHeaderEntry(entry);
106
+ const bytes = new Uint8Array(entry.zip64 ? 24 : 16);
107
+ const view = new DataView(bytes.buffer);
108
+ view.setUint32(0, 0x08074b50, true);
109
+ view.setUint32(4, entry.crc32, true);
110
+ if (entry.zip64) {
111
+ view.setBigUint64(8, entry.compressedSize, true);
112
+ view.setBigUint64(16, entry.size, true);
113
+ } else {
114
+ view.setUint32(8, Number(entry.compressedSize), true);
115
+ view.setUint32(12, Number(entry.size), true);
116
+ }
117
+ return bytes;
118
+ }
119
+
120
+ /** Serializes single-disk end records with bigint positions and an encoded archive comment. */
121
+ export function encodeZipEndRecords(
122
+ count: bigint,
123
+ directorySize: bigint,
124
+ directoryOffset: bigint,
125
+ zip64: boolean,
126
+ comment = new Uint8Array()
127
+ ): Uint8Array<ArrayBuffer> {
128
+ for (const value of [count, directorySize, directoryOffset, directoryOffset + directorySize])
129
+ validateUint64(value);
130
+ if (comment.length > 0xffff) throw new RangeError('ZIP archive comment exceeds 65535 bytes');
131
+ if (
132
+ !zip64 &&
133
+ (count > 0xffffn || directorySize >= ZIP32_SENTINEL || directoryOffset >= ZIP32_SENTINEL)
134
+ )
135
+ throw new RangeError('ZIP32 end record capacity exceeded');
136
+ const bytes = new Uint8Array((zip64 ? 98 : 22) + comment.length);
137
+ const view = new DataView(bytes.buffer);
138
+ if (zip64) {
139
+ view.setUint32(0, 0x06064b50, true);
140
+ view.setBigUint64(4, 44n, true);
141
+ view.setUint16(12, 45, true);
142
+ view.setUint16(14, 45, true);
143
+ view.setBigUint64(24, count, true);
144
+ view.setBigUint64(32, count, true);
145
+ view.setBigUint64(40, directorySize, true);
146
+ view.setBigUint64(48, directoryOffset, true);
147
+ view.setUint32(56, 0x07064b50, true);
148
+ view.setBigUint64(64, directoryOffset + directorySize, true);
149
+ view.setUint32(72, 1, true);
150
+ }
151
+ const endOffset = zip64 ? 76 : 0;
152
+ view.setUint32(endOffset, 0x06054b50, true);
153
+ view.setUint16(endOffset + 8, zip64 ? 0xffff : Number(count), true);
154
+ view.setUint16(endOffset + 10, zip64 ? 0xffff : Number(count), true);
155
+ view.setUint32(endOffset + 12, zip64 ? Number(ZIP32_SENTINEL) : Number(directorySize), true);
156
+ view.setUint32(endOffset + 16, zip64 ? Number(ZIP32_SENTINEL) : Number(directoryOffset), true);
157
+ view.setUint16(endOffset + 20, comment.length, true);
158
+ bytes.set(comment, endOffset + 22);
159
+ return bytes;
160
+ }
161
+
162
+ /** Encodes the UTC DOS time/date pair without depending on the host timezone. */
163
+ export function encodeZipTimestamp(date: Date): number {
164
+ if (
165
+ !(date instanceof Date) ||
166
+ !Number.isFinite(date.getTime()) ||
167
+ date.getUTCFullYear() < 1980 ||
168
+ date.getUTCFullYear() > 2107
169
+ )
170
+ throw new RangeError('ZIP timestamp must be a valid UTC date in 1980–2107');
171
+ const time =
172
+ (date.getUTCHours() << 11) | (date.getUTCMinutes() << 5) | Math.floor(date.getUTCSeconds() / 2);
173
+ const day =
174
+ ((date.getUTCFullYear() - 1980) << 9) | ((date.getUTCMonth() + 1) << 5) | date.getUTCDate();
175
+ return ((day << 16) | time) >>> 0;
176
+ }
177
+
178
+ /** Selects the required extractor version for the encoded fields and method. */
179
+ function getZipVersion(entry: ZipHeaderEntry): number {
180
+ return entry.zip64 ? 45 : entry.method === 8 || entry.flags & 8 ? 20 : 10;
181
+ }
182
+
183
+ /** Rejects overflow before DataView's setters can silently truncate values. */
184
+ function validateHeaderEntry(entry: ZipHeaderEntry): void {
185
+ if (
186
+ !entry.nameBytes.length ||
187
+ entry.nameBytes.length > 0xffff ||
188
+ (entry.commentBytes?.length ?? 0) > 0xffff
189
+ )
190
+ throw new RangeError('ZIP name/comment exceeds header capacity');
191
+ for (const value of [entry.size, entry.compressedSize, entry.localHeaderOffset])
192
+ validateUint64(value);
193
+ if (
194
+ !entry.zip64 &&
195
+ [entry.size, entry.compressedSize, entry.localHeaderOffset].some(
196
+ value => value >= ZIP32_SENTINEL
197
+ )
198
+ )
199
+ throw new RangeError('ZIP32 entry capacity exceeded');
200
+ }
201
+
202
+ /** Validates an unsigned ZIP64 integer without converting through Number. */
203
+ function validateUint64(value: bigint): void {
204
+ if (typeof value !== 'bigint' || value < 0n || value > 0xffffffffffffffffn)
205
+ throw new RangeError('ZIP64 unsigned integer capacity exceeded');
206
+ }
@@ -0,0 +1,84 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /** ZIP64 extra-field header identifier. */
6
+ const ZIP64_EXTRA_FIELD_ID = 0x0001;
7
+
8
+ /** Sentinel indicating that a 16-bit ZIP header value is stored in ZIP64 data. */
9
+ export const ZIP64_UINT16_SENTINEL = 0xffffn;
10
+
11
+ /** Sentinel indicating that a 32-bit ZIP header value is stored in ZIP64 data. */
12
+ export const ZIP64_UINT32_SENTINEL = 0xffffffffn;
13
+
14
+ /** Description of a value stored in a ZIP64 extended information extra field. */
15
+ export type Zip64ExtraFieldDescription<FieldName extends string> = {
16
+ /** Name used for the decoded value. */
17
+ name: FieldName;
18
+ /** Encoded value width in bytes. */
19
+ byteLength: 4 | 8;
20
+ };
21
+
22
+ /**
23
+ * Finds and decodes the ZIP64 record in a sequence of ZIP extra-field records.
24
+ * @param extraField complete extra-field data from a local or central-directory header
25
+ * @param expectedFields ZIP64 values required by sentinel fields in the legacy header
26
+ * @returns decoded ZIP64 values keyed by the supplied field names
27
+ * @throws If required ZIP64 data is missing, truncated, or has an unexpected size
28
+ */
29
+ export function parseZip64ExtraField<FieldName extends string>(
30
+ extraField: DataView,
31
+ expectedFields: readonly Zip64ExtraFieldDescription<FieldName>[]
32
+ ): Partial<Record<FieldName, bigint>> {
33
+ const values: Partial<Record<FieldName, bigint>> = {};
34
+ if (expectedFields.length === 0) {
35
+ return values;
36
+ }
37
+
38
+ const expectedPayloadLength = expectedFields.reduce(
39
+ (totalByteLength, field) => totalByteLength + field.byteLength,
40
+ 0
41
+ );
42
+ let recordOffset = 0;
43
+
44
+ while (recordOffset < extraField.byteLength) {
45
+ if (recordOffset + 4 > extraField.byteLength) {
46
+ throw new Error(
47
+ 'Invalid ZIP archive: truncated extra-field record header while reading ZIP64 data'
48
+ );
49
+ }
50
+
51
+ const headerId = extraField.getUint16(recordOffset, true);
52
+ const payloadLength = extraField.getUint16(recordOffset + 2, true);
53
+ const payloadOffset = recordOffset + 4;
54
+ const nextRecordOffset = payloadOffset + payloadLength;
55
+
56
+ if (nextRecordOffset > extraField.byteLength) {
57
+ throw new Error(
58
+ 'Invalid ZIP archive: truncated extra-field record payload while reading ZIP64 data'
59
+ );
60
+ }
61
+
62
+ if (headerId === ZIP64_EXTRA_FIELD_ID) {
63
+ if (payloadLength !== expectedPayloadLength) {
64
+ throw new Error(
65
+ 'Invalid ZIP archive: ZIP64 extended information has an unexpected payload size'
66
+ );
67
+ }
68
+
69
+ let fieldOffset = payloadOffset;
70
+ for (const field of expectedFields) {
71
+ values[field.name] =
72
+ field.byteLength === 8
73
+ ? extraField.getBigUint64(fieldOffset, true)
74
+ : BigInt(extraField.getUint32(fieldOffset, true));
75
+ fieldOffset += field.byteLength;
76
+ }
77
+ return values;
78
+ }
79
+
80
+ recordOffset = nextRecordOffset;
81
+ }
82
+
83
+ throw new Error('Invalid ZIP archive: required ZIP64 extended information is missing');
84
+ }