@loaders.gl/zip 4.0.4 → 4.1.0-alpha.10

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 (68) hide show
  1. package/dist/dist.dev.js +434 -27
  2. package/dist/filesystems/zip-filesystem.d.ts.map +1 -1
  3. package/dist/filesystems/zip-filesystem.js.map +1 -1
  4. package/dist/hash-file-utility.d.ts +6 -0
  5. package/dist/hash-file-utility.d.ts.map +1 -1
  6. package/dist/hash-file-utility.js +22 -0
  7. package/dist/hash-file-utility.js.map +1 -1
  8. package/dist/index.cjs +462 -32
  9. package/dist/index.d.ts +4 -3
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +4 -3
  12. package/dist/index.js.map +1 -1
  13. package/dist/lib/tar/header.d.ts.map +1 -1
  14. package/dist/lib/tar/header.js.map +1 -1
  15. package/dist/lib/tar/tar.d.ts.map +1 -1
  16. package/dist/lib/tar/tar.js.map +1 -1
  17. package/dist/lib/tar/types.d.ts.map +1 -1
  18. package/dist/lib/tar/types.js.map +1 -1
  19. package/dist/lib/tar/utils.d.ts.map +1 -1
  20. package/dist/lib/tar/utils.js.map +1 -1
  21. package/dist/parse-zip/cd-file-header.d.ts +18 -0
  22. package/dist/parse-zip/cd-file-header.d.ts.map +1 -1
  23. package/dist/parse-zip/cd-file-header.js +101 -1
  24. package/dist/parse-zip/cd-file-header.js.map +1 -1
  25. package/dist/parse-zip/end-of-central-directory.d.ts +19 -0
  26. package/dist/parse-zip/end-of-central-directory.d.ts.map +1 -1
  27. package/dist/parse-zip/end-of-central-directory.js +41 -8
  28. package/dist/parse-zip/end-of-central-directory.js.map +1 -1
  29. package/dist/parse-zip/local-file-header.d.ts +16 -0
  30. package/dist/parse-zip/local-file-header.d.ts.map +1 -1
  31. package/dist/parse-zip/local-file-header.js +73 -1
  32. package/dist/parse-zip/local-file-header.js.map +1 -1
  33. package/dist/parse-zip/search-from-the-end.d.ts.map +1 -1
  34. package/dist/parse-zip/search-from-the-end.js.map +1 -1
  35. package/dist/parse-zip/zip-compozition.d.ts +8 -0
  36. package/dist/parse-zip/zip-compozition.d.ts.map +1 -0
  37. package/dist/parse-zip/zip-compozition.js +43 -0
  38. package/dist/parse-zip/zip-compozition.js.map +1 -0
  39. package/dist/parse-zip/zip64-info-generation.d.ts +24 -0
  40. package/dist/parse-zip/zip64-info-generation.d.ts.map +1 -0
  41. package/dist/parse-zip/zip64-info-generation.js +50 -0
  42. package/dist/parse-zip/zip64-info-generation.js.map +1 -0
  43. package/dist/tar-builder.d.ts.map +1 -1
  44. package/dist/tar-builder.js.map +1 -1
  45. package/dist/zip-loader.d.ts.map +1 -1
  46. package/dist/zip-loader.js +1 -1
  47. package/dist/zip-loader.js.map +1 -1
  48. package/dist/zip-writer.d.ts +2 -2
  49. package/dist/zip-writer.d.ts.map +1 -1
  50. package/dist/zip-writer.js +22 -7
  51. package/dist/zip-writer.js.map +1 -1
  52. package/package.json +7 -7
  53. package/src/filesystems/zip-filesystem.ts +2 -1
  54. package/src/hash-file-utility.ts +52 -2
  55. package/src/index.ts +8 -4
  56. package/src/lib/tar/header.ts +2 -1
  57. package/src/lib/tar/tar.ts +2 -1
  58. package/src/lib/tar/types.ts +2 -1
  59. package/src/lib/tar/utils.ts +2 -1
  60. package/src/parse-zip/cd-file-header.ts +185 -2
  61. package/src/parse-zip/end-of-central-directory.ts +99 -9
  62. package/src/parse-zip/local-file-header.ts +128 -2
  63. package/src/parse-zip/search-from-the-end.ts +2 -1
  64. package/src/parse-zip/zip-compozition.ts +113 -0
  65. package/src/parse-zip/zip64-info-generation.ts +106 -0
  66. package/src/tar-builder.ts +2 -1
  67. package/src/zip-loader.ts +2 -1
  68. package/src/zip-writer.ts +24 -10
@@ -0,0 +1,106 @@
1
+ import {concatenateArrayBuffers} from '@loaders.gl/loader-utils';
2
+
3
+ export const signature = new Uint8Array([0x01, 0x00]);
4
+
5
+ /** info that can be placed into zip64 field, doc: https://en.wikipedia.org/wiki/ZIP_(file_format)#ZIP64 */
6
+ type Zip64Options = {
7
+ /** Original uncompressed file size and Size of compressed data */
8
+ size?: number;
9
+ /** Offset of local header record */
10
+ offset?: number;
11
+ };
12
+
13
+ /**
14
+ * creates zip64 extra field
15
+ * @param options info that can be placed into zip64 field
16
+ * @returns buffer with field
17
+ */
18
+ export function createZip64Info(options: Zip64Options): ArrayBuffer {
19
+ const optionsToUse = {
20
+ ...options,
21
+ zip64Length: (options.offset ? 1 : 0) * 8 + (options.size ? 1 : 0) * 16
22
+ };
23
+
24
+ const arraysToConcat: ArrayBuffer[] = [];
25
+
26
+ for (const field of ZIP64_FIELDS) {
27
+ if (!optionsToUse[field.name ?? ''] && !field.default) {
28
+ continue;
29
+ }
30
+ const newValue = new DataView(new ArrayBuffer(field.size));
31
+ NUMBER_SETTERS[field.size](newValue, 0, optionsToUse[field.name ?? ''] ?? field.default);
32
+ arraysToConcat.push(newValue.buffer);
33
+ }
34
+
35
+ return concatenateArrayBuffers(...arraysToConcat);
36
+ }
37
+
38
+ /**
39
+ * Function to write values into buffer
40
+ * @param header buffer where to write a value
41
+ * @param offset offset of the writing start
42
+ * @param value value to be written
43
+ */
44
+ type NumberSetter = (header: DataView, offset: number, value: number | bigint) => void;
45
+
46
+ /**
47
+ * Writes values into buffer according to the bytes amount
48
+ * @param header header where to write the data
49
+ * @param fieldSize size of the field in bytes
50
+ * @param fieldOffset offset of the field
51
+ * @param value value to be written
52
+ */
53
+ export function setFieldToNumber(
54
+ header: DataView,
55
+ fieldSize: number,
56
+ fieldOffset: number | bigint,
57
+ value: number | bigint
58
+ ): void {
59
+ NUMBER_SETTERS[fieldSize](header, Number(fieldOffset), value);
60
+ }
61
+
62
+ /** functions to write values into buffer according to the bytes amount */
63
+ const NUMBER_SETTERS: {[key: number]: NumberSetter} = {
64
+ 2: (header, offset, value) => {
65
+ header.setUint16(offset, Number(value), true);
66
+ },
67
+ 4: (header, offset, value) => {
68
+ header.setUint32(offset, Number(value), true);
69
+ },
70
+ 8: (header, offset, value) => {
71
+ header.setBigUint64(offset, BigInt(value), true);
72
+ }
73
+ };
74
+
75
+ /** zip64 info fields description, we need it as a pattern to build a zip64 info */
76
+ const ZIP64_FIELDS = [
77
+ // Header ID 0x0001
78
+ {
79
+ size: 2,
80
+ default: new DataView(signature.buffer).getUint16(0, true)
81
+ },
82
+
83
+ // Size of the extra field chunk (8, 16, 24 or 28)
84
+ {
85
+ size: 2,
86
+ name: 'zip64Length'
87
+ },
88
+
89
+ // Original uncompressed file size
90
+ {
91
+ size: 8,
92
+ name: 'size'
93
+ },
94
+
95
+ // Size of compressed data
96
+ {
97
+ size: 8,
98
+ name: 'size'
99
+ },
100
+
101
+ // Offset of local header record
102
+ {
103
+ size: 8,
104
+ name: 'offset'
105
+ }
106
+ ];
@@ -1,4 +1,5 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
5
  import Tar from './lib/tar/tar';
package/src/zip-loader.ts CHANGED
@@ -1,4 +1,5 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
5
  import type {LoaderWithParser, LoaderOptions} from '@loaders.gl/loader-utils';
package/src/zip-writer.ts CHANGED
@@ -1,9 +1,13 @@
1
- // loaders.gl, MIT license
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
2
3
  // Copyright (c) vis.gl contributors
3
4
 
4
- import type {Writer, WriterOptions} from '@loaders.gl/loader-utils';
5
+ import type {WriterWithEncoder, WriterOptions} from '@loaders.gl/loader-utils';
5
6
  import JSZip, {JSZipGeneratorOptions} from 'jszip';
6
7
 
8
+ // @ts-ignore TS2304: Cannot find name '__VERSION__'.
9
+ const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
10
+
7
11
  export type ZipWriterOptions = WriterOptions & {
8
12
  zip?: {
9
13
  onUpdate?: (metadata: {percent: number}) => void;
@@ -15,19 +19,27 @@ export type ZipWriterOptions = WriterOptions & {
15
19
  /**
16
20
  * Zip exporter
17
21
  */
18
- export const ZipWriter: Writer<FileReaderEventMap, never, ZipWriterOptions> = {
22
+ export const ZipWriter: WriterWithEncoder<Record<string, ArrayBuffer>, never, ZipWriterOptions> = {
19
23
  name: 'Zip Archive',
24
+ id: 'zip',
25
+ module: 'zip',
26
+ version: VERSION,
20
27
  extensions: ['zip'],
21
28
  category: 'archive',
22
29
  mimeTypes: ['application/zip'],
23
- // @ts-ignore
30
+ options: {
31
+ zip: {
32
+ onUpdate: () => {}
33
+ },
34
+ jszip: {}
35
+ },
24
36
  encode: encodeZipAsync
25
37
  };
26
38
 
27
39
  async function encodeZipAsync(
28
40
  fileMap: Record<string, ArrayBuffer>,
29
41
  options: ZipWriterOptions = {}
30
- ) {
42
+ ): Promise<ArrayBuffer> {
31
43
  const jsZip = new JSZip();
32
44
  // add files to the zip
33
45
  for (const subFileName in fileMap) {
@@ -38,14 +50,16 @@ async function encodeZipAsync(
38
50
  jsZip.file(subFileName, subFileData, options?.jszip || {});
39
51
  }
40
52
 
41
- // always generate the full zip as an arraybuffer
42
- const jszipOptions: JSZipGeneratorOptions = {...options?.jszip, type: 'arraybuffer'};
43
- const {onUpdate = () => {}} = options;
53
+ const zipOptions = {...ZipWriter.options.zip, ...options?.zip};
54
+ const jszipOptions: JSZipGeneratorOptions = {...ZipWriter.options?.jszip, ...options.jszip};
44
55
 
45
56
  try {
46
- return await jsZip.generateAsync(jszipOptions, onUpdate);
57
+ return await jsZip.generateAsync(
58
+ {...jszipOptions, type: 'arraybuffer'}, // generate an arraybuffer
59
+ zipOptions.onUpdate
60
+ );
47
61
  } catch (error) {
48
- options.log.error(`Unable to write zip archive: ${error}`);
62
+ options.log.error(`Unable to encode zip archive: ${error}`);
49
63
  throw error;
50
64
  }
51
65
  }