@loaders.gl/zip 4.0.0-alpha.19 → 4.0.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dist.min.js +638 -1
- package/dist/es5/file-provider/data-view-file.js +146 -0
- package/dist/es5/file-provider/data-view-file.js.map +1 -0
- package/dist/es5/file-provider/file-handle-file.js +234 -0
- package/dist/es5/file-provider/file-handle-file.js.map +1 -0
- package/dist/es5/file-provider/file-handle.js +101 -0
- package/dist/es5/file-provider/file-handle.js.map +1 -0
- package/dist/es5/file-provider/file-provider.js +11 -0
- package/dist/es5/file-provider/file-provider.js.map +1 -0
- package/dist/es5/filesystems/zip-filesystem.js +340 -0
- package/dist/es5/filesystems/zip-filesystem.js.map +1 -0
- package/dist/es5/hash-file-utility.js +130 -0
- package/dist/es5/hash-file-utility.js.map +1 -0
- package/dist/es5/index.js +85 -0
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/parse-zip/cd-file-header.js +163 -0
- package/dist/es5/parse-zip/cd-file-header.js.map +1 -0
- package/dist/es5/parse-zip/end-of-central-directory.js +98 -0
- package/dist/es5/parse-zip/end-of-central-directory.js.map +1 -0
- package/dist/es5/parse-zip/local-file-header.js +117 -0
- package/dist/es5/parse-zip/local-file-header.js.map +1 -0
- package/dist/es5/parse-zip/search-from-the-end.js +69 -0
- package/dist/es5/parse-zip/search-from-the-end.js.map +1 -0
- package/dist/es5/zip-loader.js +1 -1
- package/dist/esm/file-provider/data-view-file.js +33 -0
- package/dist/esm/file-provider/data-view-file.js.map +1 -0
- package/dist/esm/file-provider/file-handle-file.js +57 -0
- package/dist/esm/file-provider/file-handle-file.js.map +1 -0
- package/dist/esm/file-provider/file-handle.js +37 -0
- package/dist/esm/file-provider/file-handle.js.map +1 -0
- package/dist/esm/file-provider/file-provider.js +4 -0
- package/dist/esm/file-provider/file-provider.js.map +1 -0
- package/dist/esm/filesystems/zip-filesystem.js +86 -0
- package/dist/esm/filesystems/zip-filesystem.js.map +1 -0
- package/dist/esm/hash-file-utility.js +55 -0
- package/dist/esm/hash-file-utility.js.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/parse-zip/cd-file-header.js +54 -0
- package/dist/esm/parse-zip/cd-file-header.js.map +1 -0
- package/dist/esm/parse-zip/end-of-central-directory.js +31 -0
- package/dist/esm/parse-zip/end-of-central-directory.js.map +1 -0
- package/dist/esm/parse-zip/local-file-header.js +41 -0
- package/dist/esm/parse-zip/local-file-header.js.map +1 -0
- package/dist/esm/parse-zip/search-from-the-end.js +16 -0
- package/dist/esm/parse-zip/search-from-the-end.js.map +1 -0
- package/dist/esm/zip-loader.js +1 -1
- package/dist/file-provider/data-view-file.d.ts +37 -0
- package/dist/file-provider/data-view-file.d.ts.map +1 -0
- package/dist/file-provider/data-view-file.js +63 -0
- package/dist/file-provider/file-handle-file.d.ts +53 -0
- package/dist/file-provider/file-handle-file.d.ts.map +1 -0
- package/dist/file-provider/file-handle-file.js +90 -0
- package/dist/file-provider/file-handle.d.ts +40 -0
- package/dist/file-provider/file-handle.d.ts.map +1 -0
- package/dist/file-provider/file-handle.js +57 -0
- package/dist/file-provider/file-provider.d.ts +45 -0
- package/dist/file-provider/file-provider.d.ts.map +1 -0
- package/dist/file-provider/file-provider.js +13 -0
- package/dist/filesystems/zip-filesystem.d.ts +44 -0
- package/dist/filesystems/zip-filesystem.d.ts.map +1 -0
- package/dist/filesystems/zip-filesystem.js +119 -0
- package/dist/hash-file-utility.d.ts +35 -0
- package/dist/hash-file-utility.d.ts.map +1 -0
- package/dist/hash-file-utility.js +88 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -1
- package/dist/parse-zip/cd-file-header.d.ts +36 -0
- package/dist/parse-zip/cd-file-header.d.ts.map +1 -0
- package/dist/parse-zip/cd-file-header.js +68 -0
- package/dist/parse-zip/end-of-central-directory.d.ts +18 -0
- package/dist/parse-zip/end-of-central-directory.d.ts.map +1 -0
- package/dist/parse-zip/end-of-central-directory.js +40 -0
- package/dist/parse-zip/local-file-header.d.ts +29 -0
- package/dist/parse-zip/local-file-header.d.ts.map +1 -0
- package/dist/parse-zip/local-file-header.js +55 -0
- package/dist/parse-zip/search-from-the-end.d.ts +11 -0
- package/dist/parse-zip/search-from-the-end.d.ts.map +1 -0
- package/dist/parse-zip/search-from-the-end.js +31 -0
- package/package.json +9 -2
- package/src/file-provider/data-view-file.ts +72 -0
- package/src/file-provider/file-handle-file.ts +114 -0
- package/src/file-provider/file-handle.ts +73 -0
- package/src/file-provider/file-provider.ts +56 -0
- package/src/filesystems/zip-filesystem.ts +132 -0
- package/src/hash-file-utility.ts +101 -0
- package/src/index.ts +19 -0
- package/src/parse-zip/cd-file-header.ts +114 -0
- package/src/parse-zip/end-of-central-directory.ts +71 -0
- package/src/parse-zip/local-file-header.ts +91 -0
- package/src/parse-zip/search-from-the-end.ts +38 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zipCDFileHeaderGenerator = exports.parseZipCDFileHeader = exports.signature = void 0;
|
|
4
|
+
const end_of_central_directory_1 = require("./end-of-central-directory");
|
|
5
|
+
// offsets accroding to https://en.wikipedia.org/wiki/ZIP_(file_format)
|
|
6
|
+
const CD_COMPRESSED_SIZE_OFFSET = 20n;
|
|
7
|
+
const CD_UNCOMPRESSED_SIZE_OFFSET = 24n;
|
|
8
|
+
const CD_FILE_NAME_LENGTH_OFFSET = 28n;
|
|
9
|
+
const CD_EXTRA_FIELD_LENGTH_OFFSET = 30n;
|
|
10
|
+
const CD_LOCAL_HEADER_OFFSET_OFFSET = 42n;
|
|
11
|
+
const CD_FILE_NAME_OFFSET = 46n;
|
|
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 + CD_COMPRESSED_SIZE_OFFSET));
|
|
24
|
+
let uncompressedSize = BigInt(await buffer.getUint32(headerOffset + CD_UNCOMPRESSED_SIZE_OFFSET));
|
|
25
|
+
const extraFieldLength = await buffer.getUint16(headerOffset + CD_EXTRA_FIELD_LENGTH_OFFSET);
|
|
26
|
+
const fileNameLength = await buffer.getUint16(headerOffset + CD_FILE_NAME_LENGTH_OFFSET);
|
|
27
|
+
const fileName = new TextDecoder().decode(await buffer.slice(headerOffset + CD_FILE_NAME_OFFSET, headerOffset + CD_FILE_NAME_OFFSET + BigInt(fileNameLength)));
|
|
28
|
+
const extraOffset = headerOffset + CD_FILE_NAME_OFFSET + BigInt(fileNameLength);
|
|
29
|
+
const oldFormatOffset = await buffer.getUint32(headerOffset + 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;
|
|
56
|
+
/**
|
|
57
|
+
* Create iterator over files of zip archive
|
|
58
|
+
* @param fileProvider - file provider that provider random access to the file
|
|
59
|
+
*/
|
|
60
|
+
async function* zipCDFileHeaderGenerator(fileProvider) {
|
|
61
|
+
const { cdStartOffset } = await (0, end_of_central_directory_1.parseEoCDRecord)(fileProvider);
|
|
62
|
+
let cdHeader = await (0, exports.parseZipCDFileHeader)(cdStartOffset, fileProvider);
|
|
63
|
+
while (cdHeader) {
|
|
64
|
+
yield cdHeader;
|
|
65
|
+
cdHeader = await (0, exports.parseZipCDFileHeader)(cdHeader.extraOffset + BigInt(cdHeader.extraFieldLength), fileProvider);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.zipCDFileHeaderGenerator = zipCDFileHeaderGenerator;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FileProvider } from '../file-provider/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,gCAAgC,CAAC;AAG5D;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,2CAA2C;IAC3C,aAAa,EAAE,MAAM,CAAC;IACtB,2CAA2C;IAC3C,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAaF;;;;GAIG;AACH,eAAO,MAAM,eAAe,iBAAwB,YAAY,KAAG,QAAQ,aAAa,CAwCvF,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
// offsets accroding to https://en.wikipedia.org/wiki/ZIP_(file_format)
|
|
9
|
+
const CD_RECORDS_NUMBER_OFFSET = 8n;
|
|
10
|
+
const CD_START_OFFSET_OFFSET = 16n;
|
|
11
|
+
const ZIP64_EOCD_START_OFFSET_OFFSET = 8n;
|
|
12
|
+
const ZIP64_CD_RECORDS_NUMBER_OFFSET = 24n;
|
|
13
|
+
const ZIP64_CD_START_OFFSET_OFFSET = 48n;
|
|
14
|
+
/**
|
|
15
|
+
* Parses end of central directory record of zip file
|
|
16
|
+
* @param fileProvider - FileProvider instance
|
|
17
|
+
* @returns Info from the header
|
|
18
|
+
*/
|
|
19
|
+
const parseEoCDRecord = async (fileProvider) => {
|
|
20
|
+
const zipEoCDOffset = await (0, search_from_the_end_1.searchFromTheEnd)(fileProvider, eoCDSignature);
|
|
21
|
+
let cdRecordsNumber = BigInt(await fileProvider.getUint16(zipEoCDOffset + CD_RECORDS_NUMBER_OFFSET));
|
|
22
|
+
let cdStartOffset = BigInt(await fileProvider.getUint32(zipEoCDOffset + CD_START_OFFSET_OFFSET));
|
|
23
|
+
if (cdStartOffset === BigInt(0xffffffff) || cdRecordsNumber === BigInt(0xffffffff)) {
|
|
24
|
+
const zip64EoCDLocatorOffset = zipEoCDOffset - 20n;
|
|
25
|
+
if (Buffer.from(await fileProvider.slice(zip64EoCDLocatorOffset, zip64EoCDLocatorOffset + 4n)).compare(zip64EoCDLocatorSignature) !== 0) {
|
|
26
|
+
throw new Error('zip64 EoCD locator not found');
|
|
27
|
+
}
|
|
28
|
+
const zip64EoCDOffset = await fileProvider.getBigUint64(zip64EoCDLocatorOffset + ZIP64_EOCD_START_OFFSET_OFFSET);
|
|
29
|
+
if (Buffer.from(await fileProvider.slice(zip64EoCDOffset, zip64EoCDOffset + 4n)).compare(zip64EoCDSignature) !== 0) {
|
|
30
|
+
throw new Error('zip64 EoCD not found');
|
|
31
|
+
}
|
|
32
|
+
cdRecordsNumber = await fileProvider.getBigUint64(zip64EoCDOffset + ZIP64_CD_RECORDS_NUMBER_OFFSET);
|
|
33
|
+
cdStartOffset = await fileProvider.getBigUint64(zip64EoCDOffset + ZIP64_CD_START_OFFSET_OFFSET);
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
cdRecordsNumber,
|
|
37
|
+
cdStartOffset
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
exports.parseEoCDRecord = parseEoCDRecord;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { FileProvider } from '../file-provider/file-provider';
|
|
3
|
+
/**
|
|
4
|
+
* zip local file header info
|
|
5
|
+
* according to https://en.wikipedia.org/wiki/ZIP_(file_format)
|
|
6
|
+
*/
|
|
7
|
+
export type ZipLocalFileHeader = {
|
|
8
|
+
/** File name length */
|
|
9
|
+
fileNameLength: number;
|
|
10
|
+
/** File name */
|
|
11
|
+
fileName: string;
|
|
12
|
+
/** Extra field length */
|
|
13
|
+
extraFieldLength: number;
|
|
14
|
+
/** Offset of the file data */
|
|
15
|
+
fileDataOffset: bigint;
|
|
16
|
+
/** Compressed size */
|
|
17
|
+
compressedSize: bigint;
|
|
18
|
+
/** Compression method */
|
|
19
|
+
compressionMethod: number;
|
|
20
|
+
};
|
|
21
|
+
export declare const signature: Buffer;
|
|
22
|
+
/**
|
|
23
|
+
* Parses local file header of zip file
|
|
24
|
+
* @param headerOffset - offset in the archive where header starts
|
|
25
|
+
* @param buffer - buffer containing whole array
|
|
26
|
+
* @returns Info from the header
|
|
27
|
+
*/
|
|
28
|
+
export declare const parseZipLocalFileHeader: (headerOffset: bigint, buffer: FileProvider) => Promise<ZipLocalFileHeader | null>;
|
|
29
|
+
//# sourceMappingURL=local-file-header.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-file-header.d.ts","sourceRoot":"","sources":["../../src/parse-zip/local-file-header.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,gCAAgC,CAAC;AAE5D;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,uBAAuB;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,yBAAyB;IACzB,gBAAgB,EAAE,MAAM,CAAC;IACzB,8BAA8B;IAC9B,cAAc,EAAE,MAAM,CAAC;IACvB,sBAAsB;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,yBAAyB;IACzB,iBAAiB,EAAE,MAAM,CAAC;CAC3B,CAAC;AAUF,eAAO,MAAM,SAAS,QAAwC,CAAC;AAE/D;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,iBACpB,MAAM,UACZ,YAAY,KACnB,QAAQ,kBAAkB,GAAG,IAAI,CAkDnC,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseZipLocalFileHeader = exports.signature = void 0;
|
|
4
|
+
// offsets accroding to https://en.wikipedia.org/wiki/ZIP_(file_format)
|
|
5
|
+
const COMPRESSION_METHOD_OFFSET = 8n;
|
|
6
|
+
const COMPRESSED_SIZE_OFFSET = 18n;
|
|
7
|
+
const UNCOMPRESSED_SIZE_OFFSET = 22n;
|
|
8
|
+
const FILE_NAME_LENGTH_OFFSET = 26n;
|
|
9
|
+
const EXTRA_FIELD_LENGTH_OFFSET = 28n;
|
|
10
|
+
const FILE_NAME_OFFSET = 30n;
|
|
11
|
+
exports.signature = Buffer.from([0x50, 0x4b, 0x03, 0x04]);
|
|
12
|
+
/**
|
|
13
|
+
* Parses local file header of zip file
|
|
14
|
+
* @param headerOffset - offset in the archive where header starts
|
|
15
|
+
* @param buffer - buffer containing whole array
|
|
16
|
+
* @returns Info from the header
|
|
17
|
+
*/
|
|
18
|
+
const parseZipLocalFileHeader = async (headerOffset, buffer) => {
|
|
19
|
+
if (Buffer.from(await buffer.slice(headerOffset, headerOffset + 4n)).compare(exports.signature) !== 0) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
const fileNameLength = await buffer.getUint16(headerOffset + FILE_NAME_LENGTH_OFFSET);
|
|
23
|
+
const fileName = new TextDecoder()
|
|
24
|
+
.decode(await buffer.slice(headerOffset + FILE_NAME_OFFSET, headerOffset + FILE_NAME_OFFSET + BigInt(fileNameLength)))
|
|
25
|
+
.split('\\')
|
|
26
|
+
.join('/');
|
|
27
|
+
const extraFieldLength = await buffer.getUint16(headerOffset + EXTRA_FIELD_LENGTH_OFFSET);
|
|
28
|
+
let fileDataOffset = headerOffset + FILE_NAME_OFFSET + BigInt(fileNameLength + extraFieldLength);
|
|
29
|
+
const compressionMethod = await buffer.getUint16(headerOffset + COMPRESSION_METHOD_OFFSET);
|
|
30
|
+
let compressedSize = BigInt(await buffer.getUint32(headerOffset + COMPRESSED_SIZE_OFFSET)); // add zip 64 logic
|
|
31
|
+
let uncompressedSize = BigInt(await buffer.getUint32(headerOffset + UNCOMPRESSED_SIZE_OFFSET)); // add zip 64 logic
|
|
32
|
+
const extraOffset = headerOffset + FILE_NAME_OFFSET + BigInt(fileNameLength);
|
|
33
|
+
let offsetInZip64Data = 4n;
|
|
34
|
+
// looking for info that might be also be in zip64 extra field
|
|
35
|
+
if (uncompressedSize === BigInt(0xffffffff)) {
|
|
36
|
+
uncompressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
|
|
37
|
+
offsetInZip64Data += 8n;
|
|
38
|
+
}
|
|
39
|
+
if (compressedSize === BigInt(0xffffffff)) {
|
|
40
|
+
compressedSize = await buffer.getBigUint64(extraOffset + offsetInZip64Data);
|
|
41
|
+
offsetInZip64Data += 8n;
|
|
42
|
+
}
|
|
43
|
+
if (fileDataOffset === BigInt(0xffffffff)) {
|
|
44
|
+
fileDataOffset = await buffer.getBigUint64(extraOffset + offsetInZip64Data); // setting it to the one from zip64
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
fileNameLength,
|
|
48
|
+
fileName,
|
|
49
|
+
extraFieldLength,
|
|
50
|
+
fileDataOffset,
|
|
51
|
+
compressedSize,
|
|
52
|
+
compressionMethod
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
exports.parseZipLocalFileHeader = parseZipLocalFileHeader;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FileProvider } from '../file-provider/file-provider';
|
|
2
|
+
/** Description of zip signature type */
|
|
3
|
+
export type ZipSignature = [number, number, number, number];
|
|
4
|
+
/**
|
|
5
|
+
* looking for the last occurrence of the provided
|
|
6
|
+
* @param file
|
|
7
|
+
* @param target
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare const searchFromTheEnd: (file: FileProvider, target: ZipSignature) => Promise<bigint>;
|
|
11
|
+
//# sourceMappingURL=search-from-the-end.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"search-from-the-end.d.ts","sourceRoot":"","sources":["../../src/parse-zip/search-from-the-end.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,gCAAgC,CAAC;AAE5D,wCAAwC;AACxC,MAAM,MAAM,YAAY,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAE5D;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,SACrB,YAAY,2BAEjB,QAAQ,MAAM,CAuBhB,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.searchFromTheEnd = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* looking for the last occurrence of the provided
|
|
6
|
+
* @param file
|
|
7
|
+
* @param target
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
const searchFromTheEnd = async (file, target) => {
|
|
11
|
+
const searchWindow = [
|
|
12
|
+
await file.getUint8(file.length - 1n),
|
|
13
|
+
await file.getUint8(file.length - 2n),
|
|
14
|
+
await file.getUint8(file.length - 3n),
|
|
15
|
+
undefined
|
|
16
|
+
];
|
|
17
|
+
let targetOffset = 0n;
|
|
18
|
+
// looking for the last record in the central directory
|
|
19
|
+
for (let i = file.length - 4n; i > -1; i--) {
|
|
20
|
+
searchWindow[3] = searchWindow[2];
|
|
21
|
+
searchWindow[2] = searchWindow[1];
|
|
22
|
+
searchWindow[1] = searchWindow[0];
|
|
23
|
+
searchWindow[0] = await file.getUint8(i);
|
|
24
|
+
if (searchWindow.every((val, index) => val === target[index])) {
|
|
25
|
+
targetOffset = i;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return targetOffset;
|
|
30
|
+
};
|
|
31
|
+
exports.searchFromTheEnd = searchFromTheEnd;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loaders.gl/zip",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.21",
|
|
4
4
|
"description": "Zip Archive Loader",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -25,12 +25,19 @@
|
|
|
25
25
|
"dist",
|
|
26
26
|
"README.md"
|
|
27
27
|
],
|
|
28
|
+
"browser": {
|
|
29
|
+
"fs": false
|
|
30
|
+
},
|
|
28
31
|
"scripts": {
|
|
29
32
|
"pre-build": "npm run build-bundle",
|
|
30
33
|
"build-bundle": "esbuild src/bundle.ts --bundle --outfile=dist/dist.min.js"
|
|
31
34
|
},
|
|
32
35
|
"dependencies": {
|
|
36
|
+
"@loaders.gl/compression": "4.0.0-alpha.21",
|
|
33
37
|
"jszip": "^3.1.5"
|
|
34
38
|
},
|
|
35
|
-
"
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@loaders.gl/core": "^4.0.0"
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "df5d670b136192b26941396e944f9c46be788e83"
|
|
36
43
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {FileProvider} from './file-provider';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if bigint can be converted to number and convert it if possible
|
|
5
|
+
* @param bigint bigint to be converted
|
|
6
|
+
* @returns number
|
|
7
|
+
*/
|
|
8
|
+
const toNumber = (bigint: bigint) => {
|
|
9
|
+
if (bigint > Number.MAX_SAFE_INTEGER) {
|
|
10
|
+
throw new Error('Offset is out of bounds');
|
|
11
|
+
}
|
|
12
|
+
return Number(bigint);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** Provides file data using DataView */
|
|
16
|
+
export class DataViewFile implements FileProvider {
|
|
17
|
+
/** The DataView from which data is provided */
|
|
18
|
+
private file: DataView;
|
|
19
|
+
|
|
20
|
+
constructor(file: DataView) {
|
|
21
|
+
this.file = file;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
25
|
+
async destroy(): Promise<void> {}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
|
|
29
|
+
* @param offset The offset, in bytes, from the start of the file where to read the data.
|
|
30
|
+
*/
|
|
31
|
+
async getUint8(offset: bigint): Promise<number> {
|
|
32
|
+
return this.file.getUint8(toNumber(offset));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Gets an unsigned 16-bit intege at the specified byte offset from the start of the file.
|
|
37
|
+
* @param offset The offset, in bytes, from the start of the file where to read the data.
|
|
38
|
+
*/
|
|
39
|
+
async getUint16(offset: bigint): Promise<number> {
|
|
40
|
+
return this.file.getUint16(toNumber(offset), true);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
|
|
45
|
+
* @param offset The offset, in bytes, from the start of the file where to read the data.
|
|
46
|
+
*/
|
|
47
|
+
async getUint32(offset: bigint): Promise<number> {
|
|
48
|
+
return this.file.getUint32(toNumber(offset), true);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Gets an unsigned 64-bit integer at the specified byte offset from the start of the file.
|
|
53
|
+
* @param offset The offset, in bytes, from the start of the file where to read the data.
|
|
54
|
+
*/
|
|
55
|
+
async getBigUint64(offset: bigint): Promise<bigint> {
|
|
56
|
+
return this.file.getBigUint64(toNumber(offset), true);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
|
|
61
|
+
* @param startOffset The offset, in bytes, from the start of the file where to start reading the data.
|
|
62
|
+
* @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
|
|
63
|
+
*/
|
|
64
|
+
async slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer> {
|
|
65
|
+
return this.file.buffer.slice(toNumber(startOffset), toNumber(endOffset));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** the length (in bytes) of the data. */
|
|
69
|
+
get length() {
|
|
70
|
+
return BigInt(this.file.byteLength);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {FileProvider} from '@loaders.gl/zip';
|
|
2
|
+
import {FileHandle} from './file-handle';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Provides file data using node fs library
|
|
6
|
+
*/
|
|
7
|
+
export class FileHandleFile implements FileProvider {
|
|
8
|
+
/**
|
|
9
|
+
* Returns a new copy of FileHandleFile
|
|
10
|
+
* @param path The path to the file in file system
|
|
11
|
+
*/
|
|
12
|
+
static async from(path: string): Promise<FileHandleFile> {
|
|
13
|
+
const fileDescriptor = await FileHandle.open(path);
|
|
14
|
+
return new FileHandleFile(fileDescriptor, fileDescriptor.stat.size);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The FileHandle from which data is provided
|
|
19
|
+
*/
|
|
20
|
+
private fileDescriptor: FileHandle;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The file length in bytes
|
|
24
|
+
*/
|
|
25
|
+
private size: bigint;
|
|
26
|
+
|
|
27
|
+
private constructor(fileDescriptor: FileHandle, size: bigint) {
|
|
28
|
+
this.fileDescriptor = fileDescriptor;
|
|
29
|
+
this.size = size;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Close file */
|
|
33
|
+
async destroy(): Promise<void> {
|
|
34
|
+
await this.fileDescriptor.close();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
|
|
39
|
+
* @param offset The offset, in bytes, from the start of the file where to read the data.
|
|
40
|
+
*/
|
|
41
|
+
async getUint8(offset: bigint): Promise<number> {
|
|
42
|
+
const val = new Uint8Array(
|
|
43
|
+
(await this.fileDescriptor.read(Buffer.alloc(1), 0, 1, offset)).buffer.buffer
|
|
44
|
+
).at(0);
|
|
45
|
+
if (val === undefined) {
|
|
46
|
+
throw new Error('something went wrong');
|
|
47
|
+
}
|
|
48
|
+
return val;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Gets an unsigned 16-bit integer at the specified byte offset from the start of the file.
|
|
53
|
+
* @param offset The offset, in bytes, from the start of the file where to read the data.
|
|
54
|
+
*/
|
|
55
|
+
async getUint16(offset: bigint): Promise<number> {
|
|
56
|
+
const val = new Uint16Array(
|
|
57
|
+
(await this.fileDescriptor.read(Buffer.alloc(2), 0, 2, offset)).buffer.buffer
|
|
58
|
+
).at(0);
|
|
59
|
+
if (val === undefined) {
|
|
60
|
+
throw new Error('something went wrong');
|
|
61
|
+
}
|
|
62
|
+
return val;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
|
|
67
|
+
* @param offset The offset, in bytes, from the start of the file where to read the data.
|
|
68
|
+
*/
|
|
69
|
+
async getUint32(offset: bigint): Promise<number> {
|
|
70
|
+
const val = new Uint32Array(
|
|
71
|
+
(await this.fileDescriptor.read(Buffer.alloc(4), 0, 4, offset)).buffer.buffer
|
|
72
|
+
).at(0);
|
|
73
|
+
if (val === undefined) {
|
|
74
|
+
throw new Error('something went wrong');
|
|
75
|
+
}
|
|
76
|
+
return val;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
|
|
81
|
+
* @param offset The offset, in bytes, from the start of the file where to read the data.
|
|
82
|
+
*/
|
|
83
|
+
async getBigUint64(offset: bigint): Promise<bigint> {
|
|
84
|
+
const val = new BigInt64Array(
|
|
85
|
+
(await this.fileDescriptor.read(Buffer.alloc(8), 0, 8, offset)).buffer.buffer
|
|
86
|
+
).at(0);
|
|
87
|
+
if (val === undefined) {
|
|
88
|
+
throw new Error('something went wrong');
|
|
89
|
+
}
|
|
90
|
+
return val;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
|
|
95
|
+
* @param startOffsset The offset, in byte, from the start of the file where to start reading the data.
|
|
96
|
+
* @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
|
|
97
|
+
*/
|
|
98
|
+
async slice(startOffsset: bigint, endOffset: bigint): Promise<ArrayBuffer> {
|
|
99
|
+
const bigLength = endOffset - startOffsset;
|
|
100
|
+
if (bigLength > Number.MAX_SAFE_INTEGER) {
|
|
101
|
+
throw new Error('too big slice');
|
|
102
|
+
}
|
|
103
|
+
const length = Number(bigLength);
|
|
104
|
+
return (await this.fileDescriptor.read(Buffer.alloc(length), 0, length, startOffsset)).buffer
|
|
105
|
+
.buffer;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* the length (in bytes) of the data.
|
|
110
|
+
*/
|
|
111
|
+
get length(): bigint {
|
|
112
|
+
return this.size;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {read, open, close, stat, BigIntStats} from 'fs';
|
|
2
|
+
|
|
3
|
+
/** file reading result */
|
|
4
|
+
export type FileReadResult = {
|
|
5
|
+
/** amount of the bytes read */
|
|
6
|
+
bytesRead: number;
|
|
7
|
+
/** the buffer filled with data from file*/
|
|
8
|
+
buffer: Buffer;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/** Object handling file info */
|
|
12
|
+
export class FileHandle {
|
|
13
|
+
private fileDescriptor: number;
|
|
14
|
+
private stats: BigIntStats;
|
|
15
|
+
private constructor(fileDescriptor: number, stats: BigIntStats) {
|
|
16
|
+
this.fileDescriptor = fileDescriptor;
|
|
17
|
+
this.stats = stats;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Opens a `FileHandle`.
|
|
21
|
+
*
|
|
22
|
+
* @param path path to the file
|
|
23
|
+
* @return Fulfills with a {FileHandle} object.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
static open = async (path: string): Promise<FileHandle> => {
|
|
27
|
+
const [fd, stats] = await Promise.all([
|
|
28
|
+
new Promise<number>((s) => {
|
|
29
|
+
open(path, undefined, undefined, (_err, fd) => s(fd));
|
|
30
|
+
}),
|
|
31
|
+
new Promise<BigIntStats>((s) => {
|
|
32
|
+
stat(path, {bigint: true}, (_err, stats) => s(stats));
|
|
33
|
+
})
|
|
34
|
+
]);
|
|
35
|
+
return new FileHandle(fd, stats);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Close file */
|
|
39
|
+
async close(): Promise<void> {
|
|
40
|
+
return new Promise<void>((resolve) => {
|
|
41
|
+
close(this.fileDescriptor, (_err) => resolve());
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Reads data from the file and stores that in the given buffer.
|
|
47
|
+
*
|
|
48
|
+
* If the file is not modified concurrently, the end-of-file is reached when the
|
|
49
|
+
* number of bytes read is zero.
|
|
50
|
+
* @param buffer A buffer that will be filled with the file data read.
|
|
51
|
+
* @param offset The location in the buffer at which to start filling.
|
|
52
|
+
* @param length The number of bytes to read.
|
|
53
|
+
* @param position The location where to begin reading data from the file. If `null`, data will be read from the current file position, and the position will be updated. If `position` is an
|
|
54
|
+
* integer, the current file position will remain unchanged.
|
|
55
|
+
* @return Fulfills upon success with a FileReadResult object
|
|
56
|
+
*/
|
|
57
|
+
read = (
|
|
58
|
+
buffer: Buffer,
|
|
59
|
+
offset: number,
|
|
60
|
+
length: number,
|
|
61
|
+
position: number | bigint
|
|
62
|
+
): Promise<FileReadResult> => {
|
|
63
|
+
return new Promise((s) => {
|
|
64
|
+
read(this.fileDescriptor, buffer, offset, length, position, (_err, bytesRead, buffer) =>
|
|
65
|
+
s({bytesRead, buffer})
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
get stat(): BigIntStats {
|
|
71
|
+
return this.stats;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for providing file data
|
|
3
|
+
*/
|
|
4
|
+
export interface FileProvider {
|
|
5
|
+
/**
|
|
6
|
+
* Cleanup class data
|
|
7
|
+
*/
|
|
8
|
+
destroy(): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Gets an unsigned 8-bit integer at the specified byte offset from the start of the file.
|
|
11
|
+
* @param offset The offset, in bytes, from the start of the file where to read the data.
|
|
12
|
+
*/
|
|
13
|
+
getUint8(offset: bigint): Promise<number>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Gets an unsigned 16-bit integer at the specified byte offset from the start of the file.
|
|
17
|
+
* @param offset The offset, in bytes, from the start of the file where to read the data.
|
|
18
|
+
*/
|
|
19
|
+
getUint16(offset: bigint): Promise<number>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
|
|
23
|
+
* @param offset The offset, in bytes, from the file of the view where to read the data.
|
|
24
|
+
*/
|
|
25
|
+
getUint32(offset: bigint): Promise<number>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Gets an unsigned 32-bit integer at the specified byte offset from the start of the file.
|
|
29
|
+
* @param offset The offset, in byte, from the file of the view where to read the data.
|
|
30
|
+
*/
|
|
31
|
+
getBigUint64(offset: bigint): Promise<bigint>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* returns an ArrayBuffer whose contents are a copy of this file bytes from startOffset, inclusive, up to endOffset, exclusive.
|
|
35
|
+
* @param startOffset The offset, in bytes, from the start of the file where to start reading the data.
|
|
36
|
+
* @param endOffset The offset, in bytes, from the start of the file where to end reading the data.
|
|
37
|
+
*/
|
|
38
|
+
slice(startOffset: bigint, endOffset: bigint): Promise<ArrayBuffer>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* the length (in bytes) of the data.
|
|
42
|
+
*/
|
|
43
|
+
length: bigint;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Check is the object has FileProvider members
|
|
48
|
+
* @param fileProvider - tested object
|
|
49
|
+
*/
|
|
50
|
+
export const isFileProvider = (fileProvider: unknown) => {
|
|
51
|
+
return (
|
|
52
|
+
(fileProvider as FileProvider)?.getUint8 &&
|
|
53
|
+
(fileProvider as FileProvider)?.slice &&
|
|
54
|
+
(fileProvider as FileProvider)?.length
|
|
55
|
+
);
|
|
56
|
+
};
|