@loaders.gl/zip 4.0.0-alpha.18 → 4.0.0-alpha.20

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.min.js +263 -1
  2. package/dist/es5/hash-file-utility.js +50 -0
  3. package/dist/es5/hash-file-utility.js.map +1 -0
  4. package/dist/es5/index.js +66 -0
  5. package/dist/es5/index.js.map +1 -1
  6. package/dist/es5/parse-zip/cd-file-header.js +121 -0
  7. package/dist/es5/parse-zip/cd-file-header.js.map +1 -0
  8. package/dist/es5/parse-zip/data-view-file.js +129 -0
  9. package/dist/es5/parse-zip/data-view-file.js.map +1 -0
  10. package/dist/es5/parse-zip/end-of-central-directory.js +100 -0
  11. package/dist/es5/parse-zip/end-of-central-directory.js.map +1 -0
  12. package/dist/es5/parse-zip/file-provider.js +2 -0
  13. package/dist/es5/parse-zip/file-provider.js.map +1 -0
  14. package/dist/es5/parse-zip/local-file-header.js +113 -0
  15. package/dist/es5/parse-zip/local-file-header.js.map +1 -0
  16. package/dist/es5/parse-zip/search-from-the-end.js +69 -0
  17. package/dist/es5/parse-zip/search-from-the-end.js.map +1 -0
  18. package/dist/es5/zip-loader.js +1 -1
  19. package/dist/esm/hash-file-utility.js +41 -0
  20. package/dist/esm/hash-file-utility.js.map +1 -0
  21. package/dist/esm/index.js +6 -0
  22. package/dist/esm/index.js.map +1 -1
  23. package/dist/esm/parse-zip/cd-file-header.js +45 -0
  24. package/dist/esm/parse-zip/cd-file-header.js.map +1 -0
  25. package/dist/esm/parse-zip/data-view-file.js +32 -0
  26. package/dist/esm/parse-zip/data-view-file.js.map +1 -0
  27. package/dist/esm/parse-zip/end-of-central-directory.js +33 -0
  28. package/dist/esm/parse-zip/end-of-central-directory.js.map +1 -0
  29. package/dist/esm/parse-zip/file-provider.js +2 -0
  30. package/dist/esm/parse-zip/file-provider.js.map +1 -0
  31. package/dist/esm/parse-zip/local-file-header.js +40 -0
  32. package/dist/esm/parse-zip/local-file-header.js.map +1 -0
  33. package/dist/esm/parse-zip/search-from-the-end.js +16 -0
  34. package/dist/esm/parse-zip/search-from-the-end.js.map +1 -0
  35. package/dist/esm/zip-loader.js +1 -1
  36. package/dist/hash-file-utility.d.ts +28 -0
  37. package/dist/hash-file-utility.d.ts.map +1 -0
  38. package/dist/hash-file-utility.js +65 -0
  39. package/dist/index.d.ts +8 -0
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +17 -1
  42. package/dist/parse-zip/cd-file-header.d.ts +31 -0
  43. package/dist/parse-zip/cd-file-header.d.ts.map +1 -0
  44. package/dist/parse-zip/cd-file-header.js +55 -0
  45. package/dist/parse-zip/data-view-file.d.ts +36 -0
  46. package/dist/parse-zip/data-view-file.d.ts.map +1 -0
  47. package/dist/parse-zip/data-view-file.js +61 -0
  48. package/dist/parse-zip/end-of-central-directory.d.ts +18 -0
  49. package/dist/parse-zip/end-of-central-directory.d.ts.map +1 -0
  50. package/dist/parse-zip/end-of-central-directory.js +41 -0
  51. package/dist/parse-zip/file-provider.d.ts +36 -0
  52. package/dist/parse-zip/file-provider.d.ts.map +1 -0
  53. package/dist/parse-zip/file-provider.js +2 -0
  54. package/dist/parse-zip/local-file-header.d.ts +27 -0
  55. package/dist/parse-zip/local-file-header.d.ts.map +1 -0
  56. package/dist/parse-zip/local-file-header.js +53 -0
  57. package/dist/parse-zip/search-from-the-end.d.ts +11 -0
  58. package/dist/parse-zip/search-from-the-end.d.ts.map +1 -0
  59. package/dist/parse-zip/search-from-the-end.js +31 -0
  60. package/package.json +2 -2
  61. package/src/hash-file-utility.ts +79 -0
  62. package/src/index.ts +13 -0
  63. package/src/parse-zip/cd-file-header.ts +106 -0
  64. package/src/parse-zip/data-view-file.ts +69 -0
  65. package/src/parse-zip/end-of-central-directory.ts +78 -0
  66. package/src/parse-zip/file-provider.ts +40 -0
  67. package/src/parse-zip/local-file-header.ts +91 -0
  68. package/src/parse-zip/search-from-the-end.ts +38 -0
@@ -0,0 +1,32 @@
1
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
+ const toNumber = bigint => {
3
+ if (bigint > Number.MAX_SAFE_INTEGER) {
4
+ throw new Error('Offset is out of bounds');
5
+ }
6
+ return Number(bigint);
7
+ };
8
+ export class DataViewFile {
9
+ constructor(file) {
10
+ _defineProperty(this, "file", void 0);
11
+ this.file = file;
12
+ }
13
+ async getUint8(offset) {
14
+ return this.file.getUint8(toNumber(offset));
15
+ }
16
+ async getUint16(offset) {
17
+ return this.file.getUint16(toNumber(offset), true);
18
+ }
19
+ async getUint32(offset) {
20
+ return this.file.getUint32(toNumber(offset), true);
21
+ }
22
+ async getBigUint64(offset) {
23
+ return this.file.getBigUint64(toNumber(offset), true);
24
+ }
25
+ async slice(startOffset, endOffset) {
26
+ return this.file.buffer.slice(toNumber(startOffset), toNumber(endOffset));
27
+ }
28
+ get length() {
29
+ return BigInt(this.file.byteLength);
30
+ }
31
+ }
32
+ //# sourceMappingURL=data-view-file.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data-view-file.js","names":["toNumber","bigint","Number","MAX_SAFE_INTEGER","Error","DataViewFile","constructor","file","_defineProperty","getUint8","offset","getUint16","getUint32","getBigUint64","slice","startOffset","endOffset","buffer","length","BigInt","byteLength"],"sources":["../../../src/parse-zip/data-view-file.ts"],"sourcesContent":["import {FileProvider} from './file-provider';\n\n/**\n * Checks if bigint can be converted to number and convert it if possible\n * @param bigint bigint to be converted\n * @returns number\n */\nconst toNumber = (bigint: bigint) => {\n if (bigint > Number.MAX_SAFE_INTEGER) {\n throw new Error('Offset is out of bounds');\n }\n return Number(bigint);\n};\n\n/** Provides file data using DataView */\nexport class DataViewFile implements FileProvider {\n /** The DataView from which data is provided */\n private file: DataView;\n\n constructor(file: DataView) {\n this.file = file;\n }\n\n /**\n * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.\n * @param offset The offset, in bytes, from the start of the file where to read the data.\n */\n async getUint8(offset: bigint): Promise<number> {\n return this.file.getUint8(toNumber(offset));\n }\n\n /**\n * Gets an unsigned 16-bit intege at the specified byte offset from the start of the file.\n * @param offset The offset, in bytes, from the start of the file where to read the data.\n */\n async getUint16(offset: bigint): Promise<number> {\n return this.file.getUint16(toNumber(offset), true);\n }\n\n /**\n * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.\n * @param offset The offset, in bytes, from the start of the file where to read the data.\n */\n async getUint32(offset: bigint): Promise<number> {\n return this.file.getUint32(toNumber(offset), true);\n }\n\n /**\n * Gets an unsigned 64-bit integer at the specified byte offset from the start of the file.\n * @param offset The offset, in bytes, from the start of the file where to read the data.\n */\n async getBigUint64(offset: bigint): Promise<bigint> {\n return this.file.getBigUint64(toNumber(offset), true);\n }\n\n /**\n * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.\n * @param startOffset The offset, in bytes, from the start of the file where to start reading the data.\n * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.\n */\n async slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer> {\n return this.file.buffer.slice(toNumber(startOffset), toNumber(endOffset));\n }\n\n /** the length (in bytes) of the data. */\n get length() {\n return BigInt(this.file.byteLength);\n }\n}\n"],"mappings":";AAOA,MAAMA,QAAQ,GAAIC,MAAc,IAAK;EACnC,IAAIA,MAAM,GAAGC,MAAM,CAACC,gBAAgB,EAAE;IACpC,MAAM,IAAIC,KAAK,CAAC,yBAAyB,CAAC;EAC5C;EACA,OAAOF,MAAM,CAACD,MAAM,CAAC;AACvB,CAAC;AAGD,OAAO,MAAMI,YAAY,CAAyB;EAIhDC,WAAWA,CAACC,IAAc,EAAE;IAAAC,eAAA;IAC1B,IAAI,CAACD,IAAI,GAAGA,IAAI;EAClB;EAMA,MAAME,QAAQA,CAACC,MAAc,EAAmB;IAC9C,OAAO,IAAI,CAACH,IAAI,CAACE,QAAQ,CAACT,QAAQ,CAACU,MAAM,CAAC,CAAC;EAC7C;EAMA,MAAMC,SAASA,CAACD,MAAc,EAAmB;IAC/C,OAAO,IAAI,CAACH,IAAI,CAACI,SAAS,CAACX,QAAQ,CAACU,MAAM,CAAC,EAAE,IAAI,CAAC;EACpD;EAMA,MAAME,SAASA,CAACF,MAAc,EAAmB;IAC/C,OAAO,IAAI,CAACH,IAAI,CAACK,SAAS,CAACZ,QAAQ,CAACU,MAAM,CAAC,EAAE,IAAI,CAAC;EACpD;EAMA,MAAMG,YAAYA,CAACH,MAAc,EAAmB;IAClD,OAAO,IAAI,CAACH,IAAI,CAACM,YAAY,CAACb,QAAQ,CAACU,MAAM,CAAC,EAAE,IAAI,CAAC;EACvD;EAOA,MAAMI,KAAKA,CAACC,WAAmB,EAAEC,SAAiB,EAAwB;IACxE,OAAO,IAAI,CAACT,IAAI,CAACU,MAAM,CAACH,KAAK,CAACd,QAAQ,CAACe,WAAW,CAAC,EAAEf,QAAQ,CAACgB,SAAS,CAAC,CAAC;EAC3E;EAGA,IAAIE,MAAMA,CAAA,EAAG;IACX,OAAOC,MAAM,CAAC,IAAI,CAACZ,IAAI,CAACa,UAAU,CAAC;EACrC;AACF"}
@@ -0,0 +1,33 @@
1
+ import { searchFromTheEnd } from './search-from-the-end';
2
+ const eoCDSignature = [0x50, 0x4b, 0x05, 0x06];
3
+ const zip64EoCDLocatorSignature = Buffer.from([0x50, 0x4b, 0x06, 0x07]);
4
+ const zip64EoCDSignature = Buffer.from([0x50, 0x4b, 0x06, 0x06]);
5
+ const offsets = {
6
+ CD_RECORDS_NUMBER_OFFSET: 8n,
7
+ CD_START_OFFSET_OFFSET: 16n,
8
+ ZIP64_EOCD_START_OFFSET_OFFSET: 8n,
9
+ ZIP64_CD_RECORDS_NUMBER_OFFSET: 24n,
10
+ ZIP64_CD_START_OFFSET_OFFSET: 48n
11
+ };
12
+ export const parseEoCDRecord = async fileProvider => {
13
+ const zipEoCDOffset = await searchFromTheEnd(fileProvider, eoCDSignature);
14
+ let cdRecordsNumber = BigInt(await fileProvider.getUint16(zipEoCDOffset + offsets.CD_RECORDS_NUMBER_OFFSET));
15
+ let cdStartOffset = BigInt(await fileProvider.getUint32(zipEoCDOffset + offsets.CD_START_OFFSET_OFFSET));
16
+ if (cdStartOffset === BigInt(0xffffffff) || cdRecordsNumber === BigInt(0xffffffff)) {
17
+ const zip64EoCDLocatorOffset = zipEoCDOffset - 20n;
18
+ if (Buffer.from(await fileProvider.slice(zip64EoCDLocatorOffset, zip64EoCDLocatorOffset + 4n)).compare(zip64EoCDLocatorSignature) !== 0) {
19
+ throw new Error('zip64 EoCD locator not found');
20
+ }
21
+ const zip64EoCDOffset = await fileProvider.getBigUint64(zip64EoCDLocatorOffset + offsets.ZIP64_EOCD_START_OFFSET_OFFSET);
22
+ if (Buffer.from(await fileProvider.slice(zip64EoCDOffset, zip64EoCDOffset + 4n)).compare(zip64EoCDSignature) !== 0) {
23
+ throw new Error('zip64 EoCD not found');
24
+ }
25
+ cdRecordsNumber = await fileProvider.getBigUint64(zip64EoCDOffset + offsets.ZIP64_CD_RECORDS_NUMBER_OFFSET);
26
+ cdStartOffset = await fileProvider.getBigUint64(zip64EoCDOffset + offsets.ZIP64_CD_START_OFFSET_OFFSET);
27
+ }
28
+ return {
29
+ cdRecordsNumber,
30
+ cdStartOffset
31
+ };
32
+ };
33
+ //# sourceMappingURL=end-of-central-directory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"end-of-central-directory.js","names":["searchFromTheEnd","eoCDSignature","zip64EoCDLocatorSignature","Buffer","from","zip64EoCDSignature","offsets","CD_RECORDS_NUMBER_OFFSET","CD_START_OFFSET_OFFSET","ZIP64_EOCD_START_OFFSET_OFFSET","ZIP64_CD_RECORDS_NUMBER_OFFSET","ZIP64_CD_START_OFFSET_OFFSET","parseEoCDRecord","fileProvider","zipEoCDOffset","cdRecordsNumber","BigInt","getUint16","cdStartOffset","getUint32","zip64EoCDLocatorOffset","slice","compare","Error","zip64EoCDOffset","getBigUint64"],"sources":["../../../src/parse-zip/end-of-central-directory.ts"],"sourcesContent":["import {FileProvider} from './file-provider';\nimport {ZipSignature, searchFromTheEnd} from './search-from-the-end';\n\n/**\n * End of central directory info\n * according to https://en.wikipedia.org/wiki/ZIP_(file_format)\n */\nexport type ZipEoCDRecord = {\n /** Relative offset of local file header */\n cdStartOffset: bigint;\n /** Relative offset of local file header */\n cdRecordsNumber: bigint;\n};\n\nconst eoCDSignature: ZipSignature = [0x50, 0x4b, 0x05, 0x06];\nconst zip64EoCDLocatorSignature = Buffer.from([0x50, 0x4b, 0x06, 0x07]);\nconst zip64EoCDSignature = Buffer.from([0x50, 0x4b, 0x06, 0x06]);\n\nconst offsets = {\n CD_RECORDS_NUMBER_OFFSET: 8n,\n CD_START_OFFSET_OFFSET: 16n,\n\n ZIP64_EOCD_START_OFFSET_OFFSET: 8n,\n\n ZIP64_CD_RECORDS_NUMBER_OFFSET: 24n,\n ZIP64_CD_START_OFFSET_OFFSET: 48n\n};\n\n/**\n * Parses end of central directory record of zip file\n * @param fileProvider - FileProvider instance\n * @returns Info from the header\n */\nexport const parseEoCDRecord = async (fileProvider: FileProvider): Promise<ZipEoCDRecord> => {\n const zipEoCDOffset = await searchFromTheEnd(fileProvider, eoCDSignature);\n\n let cdRecordsNumber = BigInt(\n await fileProvider.getUint16(zipEoCDOffset + offsets.CD_RECORDS_NUMBER_OFFSET)\n );\n let cdStartOffset = BigInt(\n await fileProvider.getUint32(zipEoCDOffset + offsets.CD_START_OFFSET_OFFSET)\n );\n\n if (cdStartOffset === BigInt(0xffffffff) || cdRecordsNumber === BigInt(0xffffffff)) {\n const zip64EoCDLocatorOffset = zipEoCDOffset - 20n;\n\n if (\n Buffer.from(\n await fileProvider.slice(zip64EoCDLocatorOffset, zip64EoCDLocatorOffset + 4n)\n ).compare(zip64EoCDLocatorSignature) !== 0\n ) {\n throw new Error('zip64 EoCD locator not found');\n }\n const zip64EoCDOffset = await fileProvider.getBigUint64(\n zip64EoCDLocatorOffset + offsets.ZIP64_EOCD_START_OFFSET_OFFSET\n );\n\n if (\n Buffer.from(await fileProvider.slice(zip64EoCDOffset, zip64EoCDOffset + 4n)).compare(\n zip64EoCDSignature\n ) !== 0\n ) {\n throw new Error('zip64 EoCD not found');\n }\n\n cdRecordsNumber = await fileProvider.getBigUint64(\n zip64EoCDOffset + offsets.ZIP64_CD_RECORDS_NUMBER_OFFSET\n );\n cdStartOffset = await fileProvider.getBigUint64(\n zip64EoCDOffset + offsets.ZIP64_CD_START_OFFSET_OFFSET\n );\n }\n\n return {\n cdRecordsNumber,\n cdStartOffset\n };\n};\n"],"mappings":"AACA,SAAsBA,gBAAgB,QAAO,uBAAuB;AAapE,MAAMC,aAA2B,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC;AAC5D,MAAMC,yBAAyB,GAAGC,MAAM,CAACC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AACvE,MAAMC,kBAAkB,GAAGF,MAAM,CAACC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAEhE,MAAME,OAAO,GAAG;EACdC,wBAAwB,EAAE,EAAE;EAC5BC,sBAAsB,EAAE,GAAG;EAE3BC,8BAA8B,EAAE,EAAE;EAElCC,8BAA8B,EAAE,GAAG;EACnCC,4BAA4B,EAAE;AAChC,CAAC;AAOD,OAAO,MAAMC,eAAe,GAAG,MAAOC,YAA0B,IAA6B;EAC3F,MAAMC,aAAa,GAAG,MAAMd,gBAAgB,CAACa,YAAY,EAAEZ,aAAa,CAAC;EAEzE,IAAIc,eAAe,GAAGC,MAAM,CAC1B,MAAMH,YAAY,CAACI,SAAS,CAACH,aAAa,GAAGR,OAAO,CAACC,wBAAwB,CAC/E,CAAC;EACD,IAAIW,aAAa,GAAGF,MAAM,CACxB,MAAMH,YAAY,CAACM,SAAS,CAACL,aAAa,GAAGR,OAAO,CAACE,sBAAsB,CAC7E,CAAC;EAED,IAAIU,aAAa,KAAKF,MAAM,CAAC,UAAU,CAAC,IAAID,eAAe,KAAKC,MAAM,CAAC,UAAU,CAAC,EAAE;IAClF,MAAMI,sBAAsB,GAAGN,aAAa,GAAG,GAAG;IAElD,IACEX,MAAM,CAACC,IAAI,CACT,MAAMS,YAAY,CAACQ,KAAK,CAACD,sBAAsB,EAAEA,sBAAsB,GAAG,EAAE,CAC9E,CAAC,CAACE,OAAO,CAACpB,yBAAyB,CAAC,KAAK,CAAC,EAC1C;MACA,MAAM,IAAIqB,KAAK,CAAC,8BAA8B,CAAC;IACjD;IACA,MAAMC,eAAe,GAAG,MAAMX,YAAY,CAACY,YAAY,CACrDL,sBAAsB,GAAGd,OAAO,CAACG,8BACnC,CAAC;IAED,IACEN,MAAM,CAACC,IAAI,CAAC,MAAMS,YAAY,CAACQ,KAAK,CAACG,eAAe,EAAEA,eAAe,GAAG,EAAE,CAAC,CAAC,CAACF,OAAO,CAClFjB,kBACF,CAAC,KAAK,CAAC,EACP;MACA,MAAM,IAAIkB,KAAK,CAAC,sBAAsB,CAAC;IACzC;IAEAR,eAAe,GAAG,MAAMF,YAAY,CAACY,YAAY,CAC/CD,eAAe,GAAGlB,OAAO,CAACI,8BAC5B,CAAC;IACDQ,aAAa,GAAG,MAAML,YAAY,CAACY,YAAY,CAC7CD,eAAe,GAAGlB,OAAO,CAACK,4BAC5B,CAAC;EACH;EAEA,OAAO;IACLI,eAAe;IACfG;EACF,CAAC;AACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=file-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-provider.js","names":[],"sources":["../../../src/parse-zip/file-provider.ts"],"sourcesContent":["/**\n * Interface for providing file data\n */\nexport interface FileProvider {\n /**\n * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.\n * @param offset The offset, in bytes, from the start of the file where to read the data.\n */\n getUint8(offset: bigint): Promise<number>;\n\n /**\n * Gets an unsigned 16-bit integer at the specified byte offset from the start of the file.\n * @param offset The offset, in bytes, from the start of the file where to read the data.\n */\n getUint16(offset: bigint): Promise<number>;\n\n /**\n * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.\n * @param offset The offset, in bytes, from the file of the view where to read the data.\n */\n getUint32(offset: bigint): Promise<number>;\n\n /**\n * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.\n * @param offset The offset, in byte, from the file of the view where to read the data.\n */\n getBigUint64(offset: bigint): Promise<bigint>;\n\n /**\n * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.\n * @param startOffset The offset, in bytes, from the start of the file where to start reading the data.\n * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.\n */\n slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer>;\n\n /**\n * the length (in bytes) of the data.\n */\n length: bigint;\n}\n"],"mappings":""}
@@ -0,0 +1,40 @@
1
+ const offsets = {
2
+ COMPRESSED_SIZE_OFFSET: 18n,
3
+ UNCOMPRESSED_SIZE_OFFSET: 22n,
4
+ FILE_NAME_LENGTH_OFFSET: 26n,
5
+ EXTRA_FIELD_LENGTH_OFFSET: 28n,
6
+ FILE_NAME_OFFSET: 30n
7
+ };
8
+ export const signature = Buffer.from([0x50, 0x4b, 0x03, 0x04]);
9
+ export const parseZipLocalFileHeader = async (headerOffset, buffer) => {
10
+ if (Buffer.from(await buffer.slice(headerOffset, headerOffset + 4n)).compare(signature) !== 0) {
11
+ return null;
12
+ }
13
+ const fileNameLength = await buffer.getUint16(headerOffset + offsets.FILE_NAME_LENGTH_OFFSET);
14
+ const fileName = new TextDecoder().decode(await buffer.slice(headerOffset + offsets.FILE_NAME_OFFSET, headerOffset + offsets.FILE_NAME_OFFSET + BigInt(fileNameLength))).split('\\').join('/');
15
+ const extraFieldLength = await buffer.getUint16(headerOffset + offsets.EXTRA_FIELD_LENGTH_OFFSET);
16
+ let fileDataOffset = headerOffset + offsets.FILE_NAME_OFFSET + BigInt(fileNameLength + extraFieldLength);
17
+ let compressedSize = BigInt(await buffer.getUint32(headerOffset + offsets.COMPRESSED_SIZE_OFFSET));
18
+ let uncompressedSize = BigInt(await buffer.getUint32(headerOffset + offsets.UNCOMPRESSED_SIZE_OFFSET));
19
+ const extraOffset = headerOffset + offsets.FILE_NAME_OFFSET + BigInt(fileNameLength);
20
+ let offsetInZip64Data = 4n;
21
+ if (uncompressedSize === BigInt(0xffffffff)) {
22
+ uncompressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
23
+ offsetInZip64Data += 8n;
24
+ }
25
+ if (compressedSize === BigInt(0xffffffff)) {
26
+ compressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
27
+ offsetInZip64Data += 8n;
28
+ }
29
+ if (fileDataOffset === BigInt(0xffffffff)) {
30
+ fileDataOffset = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
31
+ }
32
+ return {
33
+ fileNameLength,
34
+ fileName,
35
+ extraFieldLength,
36
+ fileDataOffset,
37
+ compressedSize
38
+ };
39
+ };
40
+ //# sourceMappingURL=local-file-header.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local-file-header.js","names":["offsets","COMPRESSED_SIZE_OFFSET","UNCOMPRESSED_SIZE_OFFSET","FILE_NAME_LENGTH_OFFSET","EXTRA_FIELD_LENGTH_OFFSET","FILE_NAME_OFFSET","signature","Buffer","from","parseZipLocalFileHeader","headerOffset","buffer","slice","compare","fileNameLength","getUint16","fileName","TextDecoder","decode","BigInt","split","join","extraFieldLength","fileDataOffset","compressedSize","getUint32","uncompressedSize","extraOffset","offsetInZip64Data","getBigUint64"],"sources":["../../../src/parse-zip/local-file-header.ts"],"sourcesContent":["import {FileProvider} from './file-provider';\n\n/**\n * zip local file header info\n * according to https://en.wikipedia.org/wiki/ZIP_(file_format)\n */\nexport type ZipLocalFileHeader = {\n /** File name length */\n fileNameLength: number;\n /** File name */\n fileName: string;\n /** Extra field length */\n extraFieldLength: number;\n /** Offset of the file data */\n fileDataOffset: bigint;\n /** Compressed size */\n compressedSize: bigint;\n};\n\nconst offsets = {\n COMPRESSED_SIZE_OFFSET: 18n,\n UNCOMPRESSED_SIZE_OFFSET: 22n,\n FILE_NAME_LENGTH_OFFSET: 26n,\n EXTRA_FIELD_LENGTH_OFFSET: 28n,\n FILE_NAME_OFFSET: 30n\n};\n\nexport const signature = Buffer.from([0x50, 0x4b, 0x03, 0x04]);\n\n/**\n * Parses local file header of zip file\n * @param headerOffset - offset in the archive where header starts\n * @param buffer - buffer containing whole array\n * @returns Info from the header\n */\nexport const parseZipLocalFileHeader = async (\n headerOffset: bigint,\n buffer: FileProvider\n): Promise<ZipLocalFileHeader | null> => {\n if (Buffer.from(await buffer.slice(headerOffset, headerOffset + 4n)).compare(signature) !== 0) {\n return null;\n }\n\n const fileNameLength = await buffer.getUint16(headerOffset + offsets.FILE_NAME_LENGTH_OFFSET);\n\n const fileName = new TextDecoder()\n .decode(\n await buffer.slice(\n headerOffset + offsets.FILE_NAME_OFFSET,\n headerOffset + offsets.FILE_NAME_OFFSET + BigInt(fileNameLength)\n )\n )\n .split('\\\\')\n .join('/');\n const extraFieldLength = await buffer.getUint16(headerOffset + offsets.EXTRA_FIELD_LENGTH_OFFSET);\n\n let fileDataOffset =\n headerOffset + offsets.FILE_NAME_OFFSET + BigInt(fileNameLength + extraFieldLength);\n\n let compressedSize = BigInt(\n await buffer.getUint32(headerOffset + offsets.COMPRESSED_SIZE_OFFSET)\n ); // add zip 64 logic\n\n let uncompressedSize = BigInt(\n await buffer.getUint32(headerOffset + offsets.UNCOMPRESSED_SIZE_OFFSET)\n ); // add zip 64 logic\n\n const extraOffset = headerOffset + offsets.FILE_NAME_OFFSET + BigInt(fileNameLength);\n\n let offsetInZip64Data = 4n;\n // looking for info that might be also be in zip64 extra field\n if (uncompressedSize === BigInt(0xffffffff)) {\n uncompressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);\n offsetInZip64Data += 8n;\n }\n if (compressedSize === BigInt(0xffffffff)) {\n compressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);\n offsetInZip64Data += 8n;\n }\n if (fileDataOffset === BigInt(0xffffffff)) {\n fileDataOffset = await buffer.getBigUint64(extraOffset + offsetInZip64Data); // setting it to the one from zip64\n }\n\n return {\n fileNameLength,\n fileName,\n extraFieldLength,\n fileDataOffset,\n compressedSize\n };\n};\n"],"mappings":"AAmBA,MAAMA,OAAO,GAAG;EACdC,sBAAsB,EAAE,GAAG;EAC3BC,wBAAwB,EAAE,GAAG;EAC7BC,uBAAuB,EAAE,GAAG;EAC5BC,yBAAyB,EAAE,GAAG;EAC9BC,gBAAgB,EAAE;AACpB,CAAC;AAED,OAAO,MAAMC,SAAS,GAAGC,MAAM,CAACC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAQ9D,OAAO,MAAMC,uBAAuB,GAAG,MAAAA,CACrCC,YAAoB,EACpBC,MAAoB,KACmB;EACvC,IAAIJ,MAAM,CAACC,IAAI,CAAC,MAAMG,MAAM,CAACC,KAAK,CAACF,YAAY,EAAEA,YAAY,GAAG,EAAE,CAAC,CAAC,CAACG,OAAO,CAACP,SAAS,CAAC,KAAK,CAAC,EAAE;IAC7F,OAAO,IAAI;EACb;EAEA,MAAMQ,cAAc,GAAG,MAAMH,MAAM,CAACI,SAAS,CAACL,YAAY,GAAGV,OAAO,CAACG,uBAAuB,CAAC;EAE7F,MAAMa,QAAQ,GAAG,IAAIC,WAAW,CAAC,CAAC,CAC/BC,MAAM,CACL,MAAMP,MAAM,CAACC,KAAK,CAChBF,YAAY,GAAGV,OAAO,CAACK,gBAAgB,EACvCK,YAAY,GAAGV,OAAO,CAACK,gBAAgB,GAAGc,MAAM,CAACL,cAAc,CACjE,CACF,CAAC,CACAM,KAAK,CAAC,IAAI,CAAC,CACXC,IAAI,CAAC,GAAG,CAAC;EACZ,MAAMC,gBAAgB,GAAG,MAAMX,MAAM,CAACI,SAAS,CAACL,YAAY,GAAGV,OAAO,CAACI,yBAAyB,CAAC;EAEjG,IAAImB,cAAc,GAChBb,YAAY,GAAGV,OAAO,CAACK,gBAAgB,GAAGc,MAAM,CAACL,cAAc,GAAGQ,gBAAgB,CAAC;EAErF,IAAIE,cAAc,GAAGL,MAAM,CACzB,MAAMR,MAAM,CAACc,SAAS,CAACf,YAAY,GAAGV,OAAO,CAACC,sBAAsB,CACtE,CAAC;EAED,IAAIyB,gBAAgB,GAAGP,MAAM,CAC3B,MAAMR,MAAM,CAACc,SAAS,CAACf,YAAY,GAAGV,OAAO,CAACE,wBAAwB,CACxE,CAAC;EAED,MAAMyB,WAAW,GAAGjB,YAAY,GAAGV,OAAO,CAACK,gBAAgB,GAAGc,MAAM,CAACL,cAAc,CAAC;EAEpF,IAAIc,iBAAiB,GAAG,EAAE;EAE1B,IAAIF,gBAAgB,KAAKP,MAAM,CAAC,UAAU,CAAC,EAAE;IAC3CO,gBAAgB,GAAG,MAAMf,MAAM,CAACkB,YAAY,CAACF,WAAW,GAAGC,iBAAiB,CAAC;IAC7EA,iBAAiB,IAAI,EAAE;EACzB;EACA,IAAIJ,cAAc,KAAKL,MAAM,CAAC,UAAU,CAAC,EAAE;IACzCK,cAAc,GAAG,MAAMb,MAAM,CAACkB,YAAY,CAACF,WAAW,GAAGC,iBAAiB,CAAC;IAC3EA,iBAAiB,IAAI,EAAE;EACzB;EACA,IAAIL,cAAc,KAAKJ,MAAM,CAAC,UAAU,CAAC,EAAE;IACzCI,cAAc,GAAG,MAAMZ,MAAM,CAACkB,YAAY,CAACF,WAAW,GAAGC,iBAAiB,CAAC;EAC7E;EAEA,OAAO;IACLd,cAAc;IACdE,QAAQ;IACRM,gBAAgB;IAChBC,cAAc;IACdC;EACF,CAAC;AACH,CAAC"}
@@ -0,0 +1,16 @@
1
+ export const searchFromTheEnd = async (file, target) => {
2
+ const searchWindow = [await file.getUint8(file.length - 1n), await file.getUint8(file.length - 2n), await file.getUint8(file.length - 3n), undefined];
3
+ let targetOffset = 0n;
4
+ for (let i = file.length - 4n; i > -1; i--) {
5
+ searchWindow[3] = searchWindow[2];
6
+ searchWindow[2] = searchWindow[1];
7
+ searchWindow[1] = searchWindow[0];
8
+ searchWindow[0] = await file.getUint8(i);
9
+ if (searchWindow.every((val, index) => val === target[index])) {
10
+ targetOffset = i;
11
+ break;
12
+ }
13
+ }
14
+ return targetOffset;
15
+ };
16
+ //# sourceMappingURL=search-from-the-end.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"search-from-the-end.js","names":["searchFromTheEnd","file","target","searchWindow","getUint8","length","undefined","targetOffset","i","every","val","index"],"sources":["../../../src/parse-zip/search-from-the-end.ts"],"sourcesContent":["import {FileProvider} from './file-provider';\n\n/** Description of zip signature type */\nexport type ZipSignature = [number, number, number, number];\n\n/**\n * looking for the last occurrence of the provided\n * @param file\n * @param target\n * @returns\n */\nexport const searchFromTheEnd = async (\n file: FileProvider,\n target: ZipSignature\n): Promise<bigint> => {\n const searchWindow = [\n await file.getUint8(file.length - 1n),\n await file.getUint8(file.length - 2n),\n await file.getUint8(file.length - 3n),\n undefined\n ];\n\n let targetOffset = 0n;\n\n // looking for the last record in the central directory\n for (let i = file.length - 4n; i > -1; i--) {\n searchWindow[3] = searchWindow[2];\n searchWindow[2] = searchWindow[1];\n searchWindow[1] = searchWindow[0];\n searchWindow[0] = await file.getUint8(i);\n if (searchWindow.every((val, index) => val === target[index])) {\n targetOffset = i;\n break;\n }\n }\n\n return targetOffset;\n};\n"],"mappings":"AAWA,OAAO,MAAMA,gBAAgB,GAAG,MAAAA,CAC9BC,IAAkB,EAClBC,MAAoB,KACA;EACpB,MAAMC,YAAY,GAAG,CACnB,MAAMF,IAAI,CAACG,QAAQ,CAACH,IAAI,CAACI,MAAM,GAAG,EAAE,CAAC,EACrC,MAAMJ,IAAI,CAACG,QAAQ,CAACH,IAAI,CAACI,MAAM,GAAG,EAAE,CAAC,EACrC,MAAMJ,IAAI,CAACG,QAAQ,CAACH,IAAI,CAACI,MAAM,GAAG,EAAE,CAAC,EACrCC,SAAS,CACV;EAED,IAAIC,YAAY,GAAG,EAAE;EAGrB,KAAK,IAAIC,CAAC,GAAGP,IAAI,CAACI,MAAM,GAAG,EAAE,EAAEG,CAAC,GAAG,CAAC,CAAC,EAAEA,CAAC,EAAE,EAAE;IAC1CL,YAAY,CAAC,CAAC,CAAC,GAAGA,YAAY,CAAC,CAAC,CAAC;IACjCA,YAAY,CAAC,CAAC,CAAC,GAAGA,YAAY,CAAC,CAAC,CAAC;IACjCA,YAAY,CAAC,CAAC,CAAC,GAAGA,YAAY,CAAC,CAAC,CAAC;IACjCA,YAAY,CAAC,CAAC,CAAC,GAAG,MAAMF,IAAI,CAACG,QAAQ,CAACI,CAAC,CAAC;IACxC,IAAIL,YAAY,CAACM,KAAK,CAAC,CAACC,GAAG,EAAEC,KAAK,KAAKD,GAAG,KAAKR,MAAM,CAACS,KAAK,CAAC,CAAC,EAAE;MAC7DJ,YAAY,GAAGC,CAAC;MAChB;IACF;EACF;EAEA,OAAOD,YAAY;AACrB,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import JSZip from 'jszip';
2
- const VERSION = typeof "4.0.0-alpha.18" !== 'undefined' ? "4.0.0-alpha.18" : 'latest';
2
+ const VERSION = typeof "4.0.0-alpha.20" !== 'undefined' ? "4.0.0-alpha.20" : 'latest';
3
3
  export const ZipLoader = {
4
4
  id: 'zip',
5
5
  module: 'zip',
@@ -0,0 +1,28 @@
1
+ /// <reference types="node" />
2
+ /** Element of hash array */
3
+ export type HashElement = {
4
+ /** File name hash */
5
+ hash: Buffer;
6
+ /** File offset in the archive */
7
+ offset: bigint;
8
+ };
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
+ export declare const compareHashes: (hash1: Buffer, hash2: Buffer) => number;
16
+ /**
17
+ * Reads hash file from buffer and returns it in ready-to-use form
18
+ * @param hashFile - bufer containing hash file
19
+ * @returns Array containing file info
20
+ */
21
+ export declare const parseHashFile: (hashFile: ArrayBuffer) => HashElement[];
22
+ /**
23
+ * Binary search in the hash info
24
+ * @param hashToSearch hash that we need to find
25
+ * @returns required hash element or undefined if not found
26
+ */
27
+ export declare const findBin: (hashToSearch: Buffer, hashArray: HashElement[]) => HashElement | undefined;
28
+ //# 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":";AAAA,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"}
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.findBin = exports.parseHashFile = exports.compareHashes = void 0;
4
+ /**
5
+ * Comparing md5 hashes according to https://github.com/Esri/i3s-spec/blob/master/docs/2.0/slpk_hashtable.pcsl.md step 5
6
+ * @param hash1 hash to compare
7
+ * @param hash2 hash to compare
8
+ * @returns -1 if hash1 < hash2, 0 of hash1 === hash2, 1 if hash1 > hash2
9
+ */
10
+ const compareHashes = (hash1, hash2) => {
11
+ const h1 = new BigUint64Array(hash1.buffer, hash1.byteOffset, 2);
12
+ const h2 = new BigUint64Array(hash2.buffer, hash2.byteOffset, 2);
13
+ const diff = h1[0] === h2[0] ? h1[1] - h2[1] : h1[0] - h2[0];
14
+ if (diff < 0n) {
15
+ return -1;
16
+ }
17
+ else if (diff === 0n) {
18
+ return 0;
19
+ }
20
+ return 1;
21
+ };
22
+ exports.compareHashes = compareHashes;
23
+ /**
24
+ * Reads hash file from buffer and returns it in ready-to-use form
25
+ * @param hashFile - bufer containing hash file
26
+ * @returns Array containing file info
27
+ */
28
+ const parseHashFile = (hashFile) => {
29
+ const hashFileBuffer = Buffer.from(hashFile);
30
+ const hashArray = [];
31
+ for (let i = 0; i < hashFileBuffer.buffer.byteLength; i = i + 24) {
32
+ const offsetBuffer = new DataView(hashFileBuffer.buffer.slice(hashFileBuffer.byteOffset + i + 16, hashFileBuffer.byteOffset + i + 24));
33
+ const offset = offsetBuffer.getBigUint64(offsetBuffer.byteOffset, true);
34
+ hashArray.push({
35
+ hash: Buffer.from(hashFileBuffer.subarray(hashFileBuffer.byteOffset + i, hashFileBuffer.byteOffset + i + 16)),
36
+ offset
37
+ });
38
+ }
39
+ return hashArray;
40
+ };
41
+ exports.parseHashFile = parseHashFile;
42
+ /**
43
+ * Binary search in the hash info
44
+ * @param hashToSearch hash that we need to find
45
+ * @returns required hash element or undefined if not found
46
+ */
47
+ const findBin = (hashToSearch, hashArray) => {
48
+ let lowerBorder = 0;
49
+ let upperBorder = hashArray.length;
50
+ while (upperBorder - lowerBorder > 1) {
51
+ const middle = lowerBorder + Math.floor((upperBorder - lowerBorder) / 2);
52
+ const value = (0, exports.compareHashes)(hashArray[middle].hash, hashToSearch);
53
+ if (value === 0) {
54
+ return hashArray[middle];
55
+ }
56
+ else if (value < 0) {
57
+ lowerBorder = middle;
58
+ }
59
+ else {
60
+ upperBorder = middle;
61
+ }
62
+ }
63
+ return undefined;
64
+ };
65
+ exports.findBin = findBin;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,12 @@
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 './parse-zip/file-provider';
5
+ export { parseZipCDFileHeader, signature as cdSignature } from './parse-zip/cd-file-header';
6
+ export { parseZipLocalFileHeader, signature as localHeaderSignature } from './parse-zip/local-file-header';
7
+ export { parseEoCDRecord } from './parse-zip/end-of-central-directory';
8
+ export { searchFromTheEnd } from './parse-zip/search-from-the-end';
9
+ export { DataViewFile } from './parse-zip/data-view-file';
10
+ export type { HashElement } from './hash-file-utility';
11
+ export { compareHashes, parseHashFile, findBin } from './hash-file-utility';
4
12
  //# 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,2BAA2B,CAAC;AAC5D,OAAO,EAAC,oBAAoB,EAAE,SAAS,IAAI,WAAW,EAAC,MAAM,4BAA4B,CAAC;AAC1F,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;AACjE,OAAO,EAAC,YAAY,EAAC,MAAM,4BAA4B,CAAC;AAExD,YAAY,EAAC,WAAW,EAAC,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAC,aAAa,EAAE,aAAa,EAAE,OAAO,EAAC,MAAM,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -3,10 +3,26 @@ 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.findBin = exports.parseHashFile = exports.compareHashes = exports.DataViewFile = exports.searchFromTheEnd = exports.parseEoCDRecord = exports.localHeaderSignature = exports.parseZipLocalFileHeader = exports.cdSignature = exports.parseZipCDFileHeader = 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 cd_file_header_1 = require("./parse-zip/cd-file-header");
14
+ Object.defineProperty(exports, "parseZipCDFileHeader", { enumerable: true, get: function () { return cd_file_header_1.parseZipCDFileHeader; } });
15
+ Object.defineProperty(exports, "cdSignature", { enumerable: true, get: function () { return cd_file_header_1.signature; } });
16
+ var local_file_header_1 = require("./parse-zip/local-file-header");
17
+ Object.defineProperty(exports, "parseZipLocalFileHeader", { enumerable: true, get: function () { return local_file_header_1.parseZipLocalFileHeader; } });
18
+ Object.defineProperty(exports, "localHeaderSignature", { enumerable: true, get: function () { return local_file_header_1.signature; } });
19
+ var end_of_central_directory_1 = require("./parse-zip/end-of-central-directory");
20
+ Object.defineProperty(exports, "parseEoCDRecord", { enumerable: true, get: function () { return end_of_central_directory_1.parseEoCDRecord; } });
21
+ var search_from_the_end_1 = require("./parse-zip/search-from-the-end");
22
+ Object.defineProperty(exports, "searchFromTheEnd", { enumerable: true, get: function () { return search_from_the_end_1.searchFromTheEnd; } });
23
+ var data_view_file_1 = require("./parse-zip/data-view-file");
24
+ Object.defineProperty(exports, "DataViewFile", { enumerable: true, get: function () { return data_view_file_1.DataViewFile; } });
25
+ var hash_file_utility_1 = require("./hash-file-utility");
26
+ Object.defineProperty(exports, "compareHashes", { enumerable: true, get: function () { return hash_file_utility_1.compareHashes; } });
27
+ Object.defineProperty(exports, "parseHashFile", { enumerable: true, get: function () { return hash_file_utility_1.parseHashFile; } });
28
+ Object.defineProperty(exports, "findBin", { enumerable: true, get: function () { return hash_file_utility_1.findBin; } });
@@ -0,0 +1,31 @@
1
+ import { FileProvider } from './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
+ //# 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,iBAAiB,CAAC;AAC7C,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;AAWF,eAAO,MAAM,SAAS,EAAE,YAAuC,CAAC;AAEhE;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,iBACjB,MAAM,UACZ,YAAY,KACnB,QAAQ,eAAe,GAAG,IAAI,CA6DhC,CAAC"}
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseZipCDFileHeader = exports.signature = void 0;
4
+ const offsets = {
5
+ CD_COMPRESSED_SIZE_OFFSET: 20n,
6
+ CD_UNCOMPRESSED_SIZE_OFFSET: 24n,
7
+ CD_FILE_NAME_LENGTH_OFFSET: 28n,
8
+ CD_EXTRA_FIELD_LENGTH_OFFSET: 30n,
9
+ CD_LOCAL_HEADER_OFFSET_OFFSET: 42n,
10
+ CD_FILE_NAME_OFFSET: 46n
11
+ };
12
+ exports.signature = [0x50, 0x4b, 0x01, 0x02];
13
+ /**
14
+ * Parses central directory file header of zip file
15
+ * @param headerOffset - offset in the archive where header starts
16
+ * @param buffer - buffer containing whole array
17
+ * @returns Info from the header
18
+ */
19
+ const parseZipCDFileHeader = async (headerOffset, buffer) => {
20
+ if (Buffer.from(await buffer.slice(headerOffset, headerOffset + 4n)).compare(Buffer.from(exports.signature)) !== 0) {
21
+ return null;
22
+ }
23
+ let compressedSize = BigInt(await buffer.getUint32(headerOffset + offsets.CD_COMPRESSED_SIZE_OFFSET));
24
+ let uncompressedSize = BigInt(await buffer.getUint32(headerOffset + offsets.CD_UNCOMPRESSED_SIZE_OFFSET));
25
+ const extraFieldLength = await buffer.getUint16(headerOffset + offsets.CD_EXTRA_FIELD_LENGTH_OFFSET);
26
+ const fileNameLength = await buffer.getUint16(headerOffset + offsets.CD_FILE_NAME_LENGTH_OFFSET);
27
+ const fileName = new TextDecoder().decode(await buffer.slice(headerOffset + offsets.CD_FILE_NAME_OFFSET, headerOffset + offsets.CD_FILE_NAME_OFFSET + BigInt(fileNameLength)));
28
+ const extraOffset = headerOffset + offsets.CD_FILE_NAME_OFFSET + BigInt(fileNameLength);
29
+ const oldFormatOffset = await buffer.getUint32(headerOffset + offsets.CD_LOCAL_HEADER_OFFSET_OFFSET);
30
+ let fileDataOffset = BigInt(oldFormatOffset);
31
+ let offsetInZip64Data = 4n;
32
+ // looking for info that might be also be in zip64 extra field
33
+ if (uncompressedSize === BigInt(0xffffffff)) {
34
+ uncompressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
35
+ offsetInZip64Data += 8n;
36
+ }
37
+ if (compressedSize === BigInt(0xffffffff)) {
38
+ compressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
39
+ offsetInZip64Data += 8n;
40
+ }
41
+ if (fileDataOffset === BigInt(0xffffffff)) {
42
+ fileDataOffset = await buffer.getBigUint64(extraOffset + offsetInZip64Data); // setting it to the one from zip64
43
+ }
44
+ const localHeaderOffset = fileDataOffset;
45
+ return {
46
+ compressedSize,
47
+ uncompressedSize,
48
+ extraFieldLength,
49
+ fileNameLength,
50
+ fileName,
51
+ extraOffset,
52
+ localHeaderOffset
53
+ };
54
+ };
55
+ exports.parseZipCDFileHeader = parseZipCDFileHeader;
@@ -0,0 +1,36 @@
1
+ import { FileProvider } from './file-provider';
2
+ /** Provides file data using DataView */
3
+ export declare class DataViewFile implements FileProvider {
4
+ /** The DataView from which data is provided */
5
+ private file;
6
+ constructor(file: DataView);
7
+ /**
8
+ * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
9
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
10
+ */
11
+ getUint8(offset: bigint): Promise<number>;
12
+ /**
13
+ * Gets an unsigned 16-bit intege at the specified byte offset from the start of the file.
14
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
15
+ */
16
+ getUint16(offset: bigint): Promise<number>;
17
+ /**
18
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
19
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
20
+ */
21
+ getUint32(offset: bigint): Promise<number>;
22
+ /**
23
+ * Gets an unsigned 64-bit integer at the specified byte offset from the start of the file.
24
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
25
+ */
26
+ getBigUint64(offset: bigint): Promise<bigint>;
27
+ /**
28
+ * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
29
+ * @param startOffset The offset, in bytes, from the start of the file where to start reading the data.
30
+ * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
31
+ */
32
+ slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer>;
33
+ /** the length (in bytes) of the data. */
34
+ get length(): bigint;
35
+ }
36
+ //# sourceMappingURL=data-view-file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data-view-file.d.ts","sourceRoot":"","sources":["../../src/parse-zip/data-view-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,iBAAiB,CAAC;AAc7C,wCAAwC;AACxC,qBAAa,YAAa,YAAW,YAAY;IAC/C,+CAA+C;IAC/C,OAAO,CAAC,IAAI,CAAW;gBAEX,IAAI,EAAE,QAAQ;IAI1B;;;OAGG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAI/C;;;OAGG;IACG,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIhD;;;OAGG;IACG,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIhD;;;OAGG;IACG,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAInD;;;;OAIG;IACG,KAAK,CAAC,WAAW,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAIzE,yCAAyC;IACzC,IAAI,MAAM,WAET;CACF"}
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataViewFile = void 0;
4
+ /**
5
+ * Checks if bigint can be converted to number and convert it if possible
6
+ * @param bigint bigint to be converted
7
+ * @returns number
8
+ */
9
+ const toNumber = (bigint) => {
10
+ if (bigint > Number.MAX_SAFE_INTEGER) {
11
+ throw new Error('Offset is out of bounds');
12
+ }
13
+ return Number(bigint);
14
+ };
15
+ /** Provides file data using DataView */
16
+ class DataViewFile {
17
+ constructor(file) {
18
+ this.file = file;
19
+ }
20
+ /**
21
+ * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
22
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
23
+ */
24
+ async getUint8(offset) {
25
+ return this.file.getUint8(toNumber(offset));
26
+ }
27
+ /**
28
+ * Gets an unsigned 16-bit intege 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 getUint16(offset) {
32
+ return this.file.getUint16(toNumber(offset), true);
33
+ }
34
+ /**
35
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
36
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
37
+ */
38
+ async getUint32(offset) {
39
+ return this.file.getUint32(toNumber(offset), true);
40
+ }
41
+ /**
42
+ * Gets an unsigned 64-bit integer at the specified byte offset from the start of the file.
43
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
44
+ */
45
+ async getBigUint64(offset) {
46
+ return this.file.getBigUint64(toNumber(offset), true);
47
+ }
48
+ /**
49
+ * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
50
+ * @param startOffset The offset, in bytes, from the start of the file where to start reading the data.
51
+ * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
52
+ */
53
+ async slice(startOffset, endOffset) {
54
+ return this.file.buffer.slice(toNumber(startOffset), toNumber(endOffset));
55
+ }
56
+ /** the length (in bytes) of the data. */
57
+ get length() {
58
+ return BigInt(this.file.byteLength);
59
+ }
60
+ }
61
+ exports.DataViewFile = DataViewFile;
@@ -0,0 +1,18 @@
1
+ import { FileProvider } from './file-provider';
2
+ /**
3
+ * End of central directory info
4
+ * according to https://en.wikipedia.org/wiki/ZIP_(file_format)
5
+ */
6
+ export type ZipEoCDRecord = {
7
+ /** Relative offset of local file header */
8
+ cdStartOffset: bigint;
9
+ /** Relative offset of local file header */
10
+ cdRecordsNumber: bigint;
11
+ };
12
+ /**
13
+ * Parses end of central directory record of zip file
14
+ * @param fileProvider - FileProvider instance
15
+ * @returns Info from the header
16
+ */
17
+ export declare const parseEoCDRecord: (fileProvider: FileProvider) => Promise<ZipEoCDRecord>;
18
+ //# sourceMappingURL=end-of-central-directory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"end-of-central-directory.d.ts","sourceRoot":"","sources":["../../src/parse-zip/end-of-central-directory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,iBAAiB,CAAC;AAG7C;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,2CAA2C;IAC3C,aAAa,EAAE,MAAM,CAAC;IACtB,2CAA2C;IAC3C,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAgBF;;;;GAIG;AACH,eAAO,MAAM,eAAe,iBAAwB,YAAY,KAAG,QAAQ,aAAa,CA4CvF,CAAC"}
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseEoCDRecord = void 0;
4
+ const search_from_the_end_1 = require("./search-from-the-end");
5
+ const eoCDSignature = [0x50, 0x4b, 0x05, 0x06];
6
+ const zip64EoCDLocatorSignature = Buffer.from([0x50, 0x4b, 0x06, 0x07]);
7
+ const zip64EoCDSignature = Buffer.from([0x50, 0x4b, 0x06, 0x06]);
8
+ const offsets = {
9
+ CD_RECORDS_NUMBER_OFFSET: 8n,
10
+ CD_START_OFFSET_OFFSET: 16n,
11
+ ZIP64_EOCD_START_OFFSET_OFFSET: 8n,
12
+ ZIP64_CD_RECORDS_NUMBER_OFFSET: 24n,
13
+ ZIP64_CD_START_OFFSET_OFFSET: 48n
14
+ };
15
+ /**
16
+ * Parses end of central directory record of zip file
17
+ * @param fileProvider - FileProvider instance
18
+ * @returns Info from the header
19
+ */
20
+ const parseEoCDRecord = async (fileProvider) => {
21
+ const zipEoCDOffset = await (0, search_from_the_end_1.searchFromTheEnd)(fileProvider, eoCDSignature);
22
+ let cdRecordsNumber = BigInt(await fileProvider.getUint16(zipEoCDOffset + offsets.CD_RECORDS_NUMBER_OFFSET));
23
+ let cdStartOffset = BigInt(await fileProvider.getUint32(zipEoCDOffset + offsets.CD_START_OFFSET_OFFSET));
24
+ if (cdStartOffset === BigInt(0xffffffff) || cdRecordsNumber === BigInt(0xffffffff)) {
25
+ const zip64EoCDLocatorOffset = zipEoCDOffset - 20n;
26
+ if (Buffer.from(await fileProvider.slice(zip64EoCDLocatorOffset, zip64EoCDLocatorOffset + 4n)).compare(zip64EoCDLocatorSignature) !== 0) {
27
+ throw new Error('zip64 EoCD locator not found');
28
+ }
29
+ const zip64EoCDOffset = await fileProvider.getBigUint64(zip64EoCDLocatorOffset + offsets.ZIP64_EOCD_START_OFFSET_OFFSET);
30
+ if (Buffer.from(await fileProvider.slice(zip64EoCDOffset, zip64EoCDOffset + 4n)).compare(zip64EoCDSignature) !== 0) {
31
+ throw new Error('zip64 EoCD not found');
32
+ }
33
+ cdRecordsNumber = await fileProvider.getBigUint64(zip64EoCDOffset + offsets.ZIP64_CD_RECORDS_NUMBER_OFFSET);
34
+ cdStartOffset = await fileProvider.getBigUint64(zip64EoCDOffset + offsets.ZIP64_CD_START_OFFSET_OFFSET);
35
+ }
36
+ return {
37
+ cdRecordsNumber,
38
+ cdStartOffset
39
+ };
40
+ };
41
+ exports.parseEoCDRecord = parseEoCDRecord;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Interface for providing file data
3
+ */
4
+ export interface FileProvider {
5
+ /**
6
+ * Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
7
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
8
+ */
9
+ getUint8(offset: bigint): Promise<number>;
10
+ /**
11
+ * Gets an unsigned 16-bit integer at the specified byte offset from the start of the file.
12
+ * @param offset The offset, in bytes, from the start of the file where to read the data.
13
+ */
14
+ getUint16(offset: bigint): Promise<number>;
15
+ /**
16
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
17
+ * @param offset The offset, in bytes, from the file of the view where to read the data.
18
+ */
19
+ getUint32(offset: bigint): Promise<number>;
20
+ /**
21
+ * Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
22
+ * @param offset The offset, in byte, from the file of the view where to read the data.
23
+ */
24
+ getBigUint64(offset: bigint): Promise<bigint>;
25
+ /**
26
+ * returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
27
+ * @param startOffset The offset, in bytes, from the start of the file where to start reading the data.
28
+ * @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
29
+ */
30
+ slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer>;
31
+ /**
32
+ * the length (in bytes) of the data.
33
+ */
34
+ length: bigint;
35
+ }
36
+ //# sourceMappingURL=file-provider.d.ts.map