@loaders.gl/archives 0.0.0-stage → 5.0.0-alpha.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/dist.dev.js +5101 -0
- package/dist/dist.min.js +9 -0
- package/dist/index.cjs +2982 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/tar/index.cjs +576 -0
- package/dist/tar/index.cjs.map +6 -0
- package/dist/tar/index.d.ts +8 -0
- package/dist/tar/index.d.ts.map +1 -0
- package/dist/tar/index.js +9 -0
- package/dist/tar/index.js.map +1 -0
- package/dist/tar/lib/header.d.ts +9 -0
- package/dist/tar/lib/header.d.ts.map +1 -0
- package/dist/tar/lib/header.js +76 -0
- package/dist/tar/lib/header.js.map +1 -0
- package/dist/tar/lib/tar.d.ts +29 -0
- package/dist/tar/lib/tar.d.ts.map +1 -0
- package/dist/tar/lib/tar.js +136 -0
- package/dist/tar/lib/tar.js.map +1 -0
- package/dist/tar/lib/types.d.ts +48 -0
- package/dist/tar/lib/types.d.ts.map +1 -0
- package/dist/tar/lib/types.js +5 -0
- package/dist/tar/lib/types.js.map +1 -0
- package/dist/tar/lib/utils.d.ts +23 -0
- package/dist/tar/lib/utils.d.ts.map +1 -0
- package/dist/tar/lib/utils.js +52 -0
- package/dist/tar/lib/utils.js.map +1 -0
- package/dist/tar/tar-archive.d.ts +4 -0
- package/dist/tar/tar-archive.d.ts.map +1 -0
- package/dist/tar/tar-archive.js +97 -0
- package/dist/tar/tar-archive.js.map +1 -0
- package/dist/tar/tar-builder.d.ts +28 -0
- package/dist/tar/tar-builder.d.ts.map +1 -0
- package/dist/tar/tar-builder.js +38 -0
- package/dist/tar/tar-builder.js.map +1 -0
- package/dist/tar/tar-format.d.ts +12 -0
- package/dist/tar/tar-format.d.ts.map +1 -0
- package/dist/tar/tar-format.js +15 -0
- package/dist/tar/tar-format.js.map +1 -0
- package/dist/tar/tar-loader-types.d.ts +15 -0
- package/dist/tar/tar-loader-types.d.ts.map +1 -0
- package/dist/tar/tar-loader-types.js +20 -0
- package/dist/tar/tar-loader-types.js.map +1 -0
- package/dist/tar/tar-loader.cjs +285 -0
- package/dist/tar/tar-loader.cjs.map +6 -0
- package/dist/tar/tar-loader.d.ts +3 -0
- package/dist/tar/tar-loader.d.ts.map +1 -0
- package/dist/tar/tar-loader.js +12 -0
- package/dist/tar/tar-loader.js.map +1 -0
- package/dist/tar/tar-writer.cjs +114 -0
- package/dist/tar/tar-writer.cjs.map +6 -0
- package/dist/tar/tar-writer.d.ts +20 -0
- package/dist/tar/tar-writer.d.ts.map +1 -0
- package/dist/tar/tar-writer.js +94 -0
- package/dist/tar/tar-writer.js.map +1 -0
- package/dist/zip/filesystems/IndexedArchive.d.ts +40 -0
- package/dist/zip/filesystems/IndexedArchive.d.ts.map +1 -0
- package/dist/zip/filesystems/IndexedArchive.js +99 -0
- package/dist/zip/filesystems/IndexedArchive.js.map +1 -0
- package/dist/zip/filesystems/zip-filesystem.d.ts +53 -0
- package/dist/zip/filesystems/zip-filesystem.d.ts.map +1 -0
- package/dist/zip/filesystems/zip-filesystem.js +231 -0
- package/dist/zip/filesystems/zip-filesystem.js.map +1 -0
- package/dist/zip/hash-file-utility.d.ts +20 -0
- package/dist/zip/hash-file-utility.d.ts.map +1 -0
- package/dist/zip/hash-file-utility.js +83 -0
- package/dist/zip/hash-file-utility.js.map +1 -0
- package/dist/zip/index.cjs +4614 -0
- package/dist/zip/index.cjs.map +6 -0
- package/dist/zip/index.d.ts +18 -0
- package/dist/zip/index.d.ts.map +1 -0
- package/dist/zip/index.js +14 -0
- package/dist/zip/index.js.map +1 -0
- package/dist/zip/indexed-zip-writer.cjs +1211 -0
- package/dist/zip/indexed-zip-writer.cjs.map +6 -0
- package/dist/zip/indexed-zip-writer.d.ts +46 -0
- package/dist/zip/indexed-zip-writer.d.ts.map +1 -0
- package/dist/zip/indexed-zip-writer.js +229 -0
- package/dist/zip/indexed-zip-writer.js.map +1 -0
- package/dist/zip/lib/zip-composition.d.ts +37 -0
- package/dist/zip/lib/zip-composition.d.ts.map +1 -0
- package/dist/zip/lib/zip-composition.js +157 -0
- package/dist/zip/lib/zip-composition.js.map +1 -0
- package/dist/zip/lib/zip-entry-metadata.d.ts +34 -0
- package/dist/zip/lib/zip-entry-metadata.d.ts.map +1 -0
- package/dist/zip/lib/zip-entry-metadata.js +132 -0
- package/dist/zip/lib/zip-entry-metadata.js.map +1 -0
- package/dist/zip/lib/zip-serialization.d.ts +36 -0
- package/dist/zip/lib/zip-serialization.d.ts.map +1 -0
- package/dist/zip/lib/zip-serialization.js +148 -0
- package/dist/zip/lib/zip-serialization.js.map +1 -0
- package/dist/zip/lib/zip64-extra-field.d.ts +20 -0
- package/dist/zip/lib/zip64-extra-field.d.ts.map +1 -0
- package/dist/zip/lib/zip64-extra-field.js +53 -0
- package/dist/zip/lib/zip64-extra-field.js.map +1 -0
- package/dist/zip/zip-archive-types.d.ts +56 -0
- package/dist/zip/zip-archive-types.d.ts.map +1 -0
- package/dist/zip/zip-archive-types.js +5 -0
- package/dist/zip/zip-archive-types.js.map +1 -0
- package/dist/zip/zip-archive.d.ts +101 -0
- package/dist/zip/zip-archive.d.ts.map +1 -0
- package/dist/zip/zip-archive.js +593 -0
- package/dist/zip/zip-archive.js.map +1 -0
- package/dist/zip/zip-encode-types.d.ts +41 -0
- package/dist/zip/zip-encode-types.d.ts.map +1 -0
- package/dist/zip/zip-encode-types.js +5 -0
- package/dist/zip/zip-encode-types.js.map +1 -0
- package/dist/zip/zip-encode.cjs +1154 -0
- package/dist/zip/zip-encode.cjs.map +6 -0
- package/dist/zip/zip-encode.d.ts +15 -0
- package/dist/zip/zip-encode.d.ts.map +1 -0
- package/dist/zip/zip-encode.js +312 -0
- package/dist/zip/zip-encode.js.map +1 -0
- package/dist/zip/zip-format.d.ts +14 -0
- package/dist/zip/zip-format.d.ts.map +1 -0
- package/dist/zip/zip-format.js +17 -0
- package/dist/zip/zip-format.js.map +1 -0
- package/dist/zip/zip-loader-types.d.ts +15 -0
- package/dist/zip/zip-loader-types.d.ts.map +1 -0
- package/dist/zip/zip-loader-types.js +20 -0
- package/dist/zip/zip-loader-types.js.map +1 -0
- package/dist/zip/zip-loader.cjs +2681 -0
- package/dist/zip/zip-loader.cjs.map +6 -0
- package/dist/zip/zip-loader.d.ts +3 -0
- package/dist/zip/zip-loader.d.ts.map +1 -0
- package/dist/zip/zip-loader.js +38 -0
- package/dist/zip/zip-loader.js.map +1 -0
- package/dist/zip/zip-writer.cjs +1208 -0
- package/dist/zip/zip-writer.cjs.map +6 -0
- package/dist/zip/zip-writer.d.ts +40 -0
- package/dist/zip/zip-writer.d.ts.map +1 -0
- package/dist/zip/zip-writer.js +48 -0
- package/dist/zip/zip-writer.js.map +1 -0
- package/package.json +88 -4
- package/src/index.ts +14 -0
- package/src/tar/index.ts +11 -0
- package/src/tar/lib/header.ts +82 -0
- package/src/tar/lib/tar.ts +160 -0
- package/src/tar/lib/types.ts +51 -0
- package/src/tar/lib/utils.ts +55 -0
- package/src/tar/tar-archive.ts +98 -0
- package/src/tar/tar-builder.ts +47 -0
- package/src/tar/tar-format.ts +17 -0
- package/src/tar/tar-loader-types.ts +36 -0
- package/src/tar/tar-loader.ts +17 -0
- package/src/tar/tar-writer.ts +102 -0
- package/src/zip/filesystems/IndexedArchive.ts +110 -0
- package/src/zip/filesystems/zip-filesystem.ts +244 -0
- package/src/zip/hash-file-utility.ts +109 -0
- package/src/zip/index.ts +38 -0
- package/src/zip/indexed-zip-writer.ts +345 -0
- package/src/zip/lib/zip-composition.ts +243 -0
- package/src/zip/lib/zip-entry-metadata.ts +157 -0
- package/src/zip/lib/zip-serialization.ts +206 -0
- package/src/zip/lib/zip64-extra-field.ts +84 -0
- package/src/zip/zip-archive-types.ts +64 -0
- package/src/zip/zip-archive.ts +733 -0
- package/src/zip/zip-encode-types.ts +53 -0
- package/src/zip/zip-encode.ts +370 -0
- package/src/zip/zip-format.ts +19 -0
- package/src/zip/zip-loader-types.ts +36 -0
- package/src/zip/zip-loader.ts +43 -0
- package/src/zip/zip-writer.ts +73 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {TarFileMap, TarLoaderOptions} from './tar-loader-types';
|
|
6
|
+
|
|
7
|
+
const BLOCK_SIZE = 512;
|
|
8
|
+
const decoder = new TextDecoder();
|
|
9
|
+
|
|
10
|
+
/** Reads a null-terminated UTF-8 field from a TAR header. */
|
|
11
|
+
function readString(bytes: Uint8Array): string {
|
|
12
|
+
const end = bytes.indexOf(0);
|
|
13
|
+
return decoder.decode(end === -1 ? bytes : bytes.subarray(0, end));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Reads an unsigned octal field, rejecting malformed and unsafe values. */
|
|
17
|
+
function readOctal(bytes: Uint8Array): number {
|
|
18
|
+
const value = readString(bytes).trim();
|
|
19
|
+
if (!value || !/^[0-7]+$/.test(value)) throw new Error('Invalid TAR numeric field');
|
|
20
|
+
const number = Number.parseInt(value, 8);
|
|
21
|
+
if (!Number.isSafeInteger(number)) throw new RangeError('TAR numeric field exceeds safe range');
|
|
22
|
+
return number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Normalizes harmless current-directory components and rejects paths outside the archive root. */
|
|
26
|
+
function normalizePath(path: string, directory: boolean): string | null {
|
|
27
|
+
if (
|
|
28
|
+
!path ||
|
|
29
|
+
path.startsWith('/') ||
|
|
30
|
+
path.includes('\\') ||
|
|
31
|
+
path.includes('\0') ||
|
|
32
|
+
/^[A-Za-z]:/.test(path)
|
|
33
|
+
) {
|
|
34
|
+
throw new Error(`Unsafe TAR path: ${path}`);
|
|
35
|
+
}
|
|
36
|
+
const components = path.split('/').filter(part => part && part !== '.');
|
|
37
|
+
if (components.includes('..')) throw new Error(`Unsafe TAR path: ${path}`);
|
|
38
|
+
if (/^[A-Za-z]:/.test(components[0] ?? '')) throw new Error(`Unsafe TAR path: ${path}`);
|
|
39
|
+
if (!components.length) {
|
|
40
|
+
if (directory) return null;
|
|
41
|
+
throw new Error(`Unsafe TAR path: ${path}`);
|
|
42
|
+
}
|
|
43
|
+
return components.join('/');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Decode regular files in a TAR archive, validating headers and allocation limits. */
|
|
47
|
+
export function readTarArchive(
|
|
48
|
+
input: ArrayBuffer | ArrayBufferView,
|
|
49
|
+
options: TarLoaderOptions = {}
|
|
50
|
+
): TarFileMap {
|
|
51
|
+
const bytes =
|
|
52
|
+
input instanceof ArrayBuffer
|
|
53
|
+
? new Uint8Array(input)
|
|
54
|
+
: new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
55
|
+
const maxEntries = options.tar?.maxEntries ?? 100000;
|
|
56
|
+
const maxBytes = options.tar?.maxTotalDecodedBytes ?? 64 * 1024 * 1024;
|
|
57
|
+
if (
|
|
58
|
+
!Number.isSafeInteger(maxEntries) ||
|
|
59
|
+
maxEntries < 0 ||
|
|
60
|
+
!Number.isSafeInteger(maxBytes) ||
|
|
61
|
+
maxBytes < 0
|
|
62
|
+
) {
|
|
63
|
+
throw new RangeError('Invalid TAR limits');
|
|
64
|
+
}
|
|
65
|
+
const files: TarFileMap = Object.create(null);
|
|
66
|
+
let offset = 0;
|
|
67
|
+
let entries = 0;
|
|
68
|
+
let totalBytes = 0;
|
|
69
|
+
while (offset + BLOCK_SIZE <= bytes.length) {
|
|
70
|
+
const header = bytes.subarray(offset, offset + BLOCK_SIZE);
|
|
71
|
+
if (header.every(byte => byte === 0)) return files;
|
|
72
|
+
const storedChecksum = readOctal(header.subarray(148, 156));
|
|
73
|
+
let checksum = 0;
|
|
74
|
+
for (let index = 0; index < BLOCK_SIZE; index++) {
|
|
75
|
+
checksum += index >= 148 && index < 156 ? 32 : header[index];
|
|
76
|
+
}
|
|
77
|
+
if (checksum !== storedChecksum) throw new Error('Invalid TAR header checksum');
|
|
78
|
+
if (++entries > maxEntries) throw new RangeError('TAR entry limit exceeded');
|
|
79
|
+
const name = readString(header.subarray(0, 100));
|
|
80
|
+
const prefix = readString(header.subarray(345, 500));
|
|
81
|
+
const type = header[156];
|
|
82
|
+
const path = normalizePath(prefix ? `${prefix}/${name}` : name, type === 53);
|
|
83
|
+
const size = readOctal(header.subarray(124, 136));
|
|
84
|
+
const dataOffset = offset + BLOCK_SIZE;
|
|
85
|
+
if (size > bytes.length - dataOffset) throw new Error('Truncated TAR entry');
|
|
86
|
+
if (type === 0 || type === 48) {
|
|
87
|
+
totalBytes += size;
|
|
88
|
+
if (totalBytes > maxBytes) throw new RangeError('TAR decoded-byte limit exceeded');
|
|
89
|
+
if (path === null) throw new Error('Invalid TAR file path');
|
|
90
|
+
if (Object.hasOwn(files, path)) throw new Error(`Duplicate TAR path: ${path}`);
|
|
91
|
+
files[path] = bytes.slice(dataOffset, dataOffset + size).buffer;
|
|
92
|
+
} else if (type !== 53) {
|
|
93
|
+
throw new Error(`Unsupported TAR entry type: ${String.fromCharCode(type)}`);
|
|
94
|
+
}
|
|
95
|
+
offset = dataOffset + Math.ceil(size / BLOCK_SIZE) * BLOCK_SIZE;
|
|
96
|
+
}
|
|
97
|
+
throw new Error('Truncated TAR archive');
|
|
98
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import Tar from './lib/tar';
|
|
6
|
+
|
|
7
|
+
const TAR_BUILDER_OPTIONS = {
|
|
8
|
+
recordsPerBlock: 20
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type TarBuilderOptions = {
|
|
12
|
+
recordsPerBlock?: number;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Build a tar file by adding files
|
|
17
|
+
*/
|
|
18
|
+
export class TarBuilder {
|
|
19
|
+
static get properties() {
|
|
20
|
+
return {
|
|
21
|
+
id: 'tar',
|
|
22
|
+
name: 'TAR',
|
|
23
|
+
extensions: ['tar'],
|
|
24
|
+
mimeTypes: ['application/x-tar'],
|
|
25
|
+
builder: TarBuilder,
|
|
26
|
+
options: TAR_BUILDER_OPTIONS
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
options: TarBuilderOptions;
|
|
31
|
+
tape: Tar;
|
|
32
|
+
count: number = 0;
|
|
33
|
+
|
|
34
|
+
constructor(options?: Partial<TarBuilderOptions>) {
|
|
35
|
+
this.options = {...TAR_BUILDER_OPTIONS, ...options};
|
|
36
|
+
this.tape = new Tar(this.options.recordsPerBlock);
|
|
37
|
+
}
|
|
38
|
+
/** Adds a file to the archive. */
|
|
39
|
+
addFile(filename: string, buffer: ArrayBuffer) {
|
|
40
|
+
this.tape.append(filename, new Uint8Array(buffer));
|
|
41
|
+
this.count++;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async build(): Promise<ArrayBuffer> {
|
|
45
|
+
return new Response(this.tape.save()).arrayBuffer();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {Format} from '@loaders.gl/core/loader-utils';
|
|
6
|
+
|
|
7
|
+
/** Uncompressed TAR archive format. */
|
|
8
|
+
export const TarFormat = {
|
|
9
|
+
name: 'TAR Archive',
|
|
10
|
+
id: 'tar',
|
|
11
|
+
module: 'archives',
|
|
12
|
+
format: 'tar',
|
|
13
|
+
extensions: ['tar'],
|
|
14
|
+
mimeTypes: ['application/x-tar'],
|
|
15
|
+
category: 'archive',
|
|
16
|
+
binary: true
|
|
17
|
+
} as const satisfies Format;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {Loader, LoaderOptions} from '@loaders.gl/core/loader-utils';
|
|
6
|
+
import {TarFormat} from './tar-format';
|
|
7
|
+
|
|
8
|
+
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
|
|
9
|
+
const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
|
|
10
|
+
|
|
11
|
+
/** Decoded regular files keyed by archive path. */
|
|
12
|
+
export type TarFileMap = Record<string, ArrayBuffer>;
|
|
13
|
+
|
|
14
|
+
/** Limits for uncompressed TAR archives. */
|
|
15
|
+
export type TarLoaderOptions = LoaderOptions & {
|
|
16
|
+
tar?: {
|
|
17
|
+
/** Maximum number of entries; defaults to 100000. */
|
|
18
|
+
maxEntries?: number;
|
|
19
|
+
/** Maximum total retained file bytes; defaults to 64 MiB. */
|
|
20
|
+
maxTotalDecodedBytes?: number;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** Metadata-only TAR loader. */
|
|
25
|
+
export const TarLoader = {
|
|
26
|
+
...TarFormat,
|
|
27
|
+
version: VERSION,
|
|
28
|
+
dataType: null as unknown as TarFileMap,
|
|
29
|
+
batchType: null as unknown as never,
|
|
30
|
+
options: {tar: {maxEntries: 100000, maxTotalDecodedBytes: 64 * 1024 * 1024}},
|
|
31
|
+
/** Loads the parser through the installed package subpath. */
|
|
32
|
+
async preload() {
|
|
33
|
+
const {TarLoaderWithParser} = await import('@loaders.gl/archives/tar/tar-loader');
|
|
34
|
+
return TarLoaderWithParser;
|
|
35
|
+
}
|
|
36
|
+
} as const satisfies Loader<TarFileMap, never, TarLoaderOptions>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {LoaderWithParser} from '@loaders.gl/core/loader-utils';
|
|
6
|
+
import {TarLoader} from './tar-loader-types';
|
|
7
|
+
import type {TarFileMap, TarLoaderOptions} from './tar-loader-types';
|
|
8
|
+
import {readTarArchive} from './tar-archive';
|
|
9
|
+
|
|
10
|
+
const {preload: _preload, ...metadata} = TarLoader;
|
|
11
|
+
|
|
12
|
+
/** Parser-bearing TAR loader. */
|
|
13
|
+
export const TarLoaderWithParser = {
|
|
14
|
+
...metadata,
|
|
15
|
+
parse: async (input: ArrayBuffer, options?: TarLoaderOptions): Promise<TarFileMap> =>
|
|
16
|
+
readTarArchive(input, options)
|
|
17
|
+
} as const satisfies LoaderWithParser<TarFileMap, never, TarLoaderOptions>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {WriterWithEncoder, WriterOptions} from '@loaders.gl/core/loader-utils';
|
|
6
|
+
import {TarFormat} from './tar-format';
|
|
7
|
+
|
|
8
|
+
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
|
|
9
|
+
const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
|
|
10
|
+
const BLOCK_SIZE = 512;
|
|
11
|
+
const encoder = new TextEncoder();
|
|
12
|
+
|
|
13
|
+
/** Named files to encode as an uncompressed TAR archive. A trailing slash denotes a directory. */
|
|
14
|
+
export type TarWriterFiles = Readonly<Record<string, string | ArrayBuffer | Uint8Array>>;
|
|
15
|
+
|
|
16
|
+
/** Encodes an unsigned octal value into a fixed-width TAR header field. */
|
|
17
|
+
function writeOctal(header: Uint8Array, offset: number, width: number, value: number): void {
|
|
18
|
+
const digits = value.toString(8);
|
|
19
|
+
if (!Number.isSafeInteger(value) || value < 0 || digits.length > width - 1) {
|
|
20
|
+
throw new RangeError('TAR numeric field exceeds header capacity');
|
|
21
|
+
}
|
|
22
|
+
header.set(encoder.encode(digits.padStart(width - 1, '0')), offset);
|
|
23
|
+
header[offset + width - 1] = 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Splits a UTF-8 path at a slash for the ustar name and prefix fields. */
|
|
27
|
+
function splitPath(path: string): {name: Uint8Array; prefix: Uint8Array} {
|
|
28
|
+
const components = path.split('/').filter(part => part && part !== '.');
|
|
29
|
+
if (
|
|
30
|
+
!path ||
|
|
31
|
+
path.startsWith('/') ||
|
|
32
|
+
path.includes('\\') ||
|
|
33
|
+
path.includes('\0') ||
|
|
34
|
+
/^[A-Za-z]:/.test(components[0] ?? '') ||
|
|
35
|
+
components.includes('..')
|
|
36
|
+
) {
|
|
37
|
+
throw new Error(`Unsafe TAR path: ${path}`);
|
|
38
|
+
}
|
|
39
|
+
const bytes = encoder.encode(path);
|
|
40
|
+
if (bytes.length <= 100) return {name: bytes, prefix: new Uint8Array(0)};
|
|
41
|
+
for (let index = path.lastIndexOf('/'); index > 0; index = path.lastIndexOf('/', index - 1)) {
|
|
42
|
+
const prefix = encoder.encode(path.slice(0, index));
|
|
43
|
+
const name = encoder.encode(path.slice(index + 1));
|
|
44
|
+
if (prefix.length <= 155 && name.length > 0 && name.length <= 100) return {name, prefix};
|
|
45
|
+
}
|
|
46
|
+
throw new RangeError(`TAR path exceeds ustar header capacity: ${path}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Encodes named files with UTF-8 ustar names and deterministic metadata. */
|
|
50
|
+
export async function encodeTar(files: TarWriterFiles): Promise<ArrayBuffer> {
|
|
51
|
+
const entries = Object.keys(files)
|
|
52
|
+
.sort()
|
|
53
|
+
.map(path => [path, files[path]] as const);
|
|
54
|
+
const prepared = entries.map(([path, value]) => {
|
|
55
|
+
const directory = path.endsWith('/');
|
|
56
|
+
const bytes =
|
|
57
|
+
typeof value === 'string'
|
|
58
|
+
? encoder.encode(value)
|
|
59
|
+
: value instanceof Uint8Array
|
|
60
|
+
? value
|
|
61
|
+
: new Uint8Array(value);
|
|
62
|
+
if (directory && bytes.length) throw new Error(`TAR directory has file data: ${path}`);
|
|
63
|
+
return {path, directory, bytes, ...splitPath(path)};
|
|
64
|
+
});
|
|
65
|
+
let length = BLOCK_SIZE * 2;
|
|
66
|
+
for (const entry of prepared) {
|
|
67
|
+
length += BLOCK_SIZE + Math.ceil(entry.bytes.length / BLOCK_SIZE) * BLOCK_SIZE;
|
|
68
|
+
if (!Number.isSafeInteger(length)) throw new RangeError('TAR archive exceeds safe size');
|
|
69
|
+
}
|
|
70
|
+
const archive = new Uint8Array(length);
|
|
71
|
+
let offset = 0;
|
|
72
|
+
for (const entry of prepared) {
|
|
73
|
+
const header = archive.subarray(offset, offset + BLOCK_SIZE);
|
|
74
|
+
header.set(entry.name, 0);
|
|
75
|
+
writeOctal(header, 100, 8, entry.directory ? 0o755 : 0o644);
|
|
76
|
+
writeOctal(header, 108, 8, 0);
|
|
77
|
+
writeOctal(header, 116, 8, 0);
|
|
78
|
+
writeOctal(header, 124, 12, entry.bytes.length);
|
|
79
|
+
writeOctal(header, 136, 12, 0);
|
|
80
|
+
header.fill(32, 148, 156);
|
|
81
|
+
header[156] = entry.directory ? 53 : 48;
|
|
82
|
+
header.set(encoder.encode('ustar\0'), 257);
|
|
83
|
+
header.set(encoder.encode('00'), 263);
|
|
84
|
+
header.set(entry.prefix, 345);
|
|
85
|
+
let checksum = 0;
|
|
86
|
+
for (const byte of header) checksum += byte;
|
|
87
|
+
header.set(encoder.encode(checksum.toString(8).padStart(6, '0') + '\0 '), 148);
|
|
88
|
+
offset += BLOCK_SIZE;
|
|
89
|
+
archive.set(entry.bytes, offset);
|
|
90
|
+
offset += Math.ceil(entry.bytes.length / BLOCK_SIZE) * BLOCK_SIZE;
|
|
91
|
+
}
|
|
92
|
+
return archive.buffer;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Buffered UTF-8 ustar writer. */
|
|
96
|
+
export const TarWriter = {
|
|
97
|
+
...TarFormat,
|
|
98
|
+
version: VERSION,
|
|
99
|
+
options: {},
|
|
100
|
+
encode: async (files: TarWriterFiles, _options?: WriterOptions): Promise<ArrayBuffer> =>
|
|
101
|
+
encodeTar(files)
|
|
102
|
+
} as const satisfies WriterWithEncoder<TarWriterFiles, never, WriterOptions>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {ReadableFile} from '@loaders.gl/core/loader-utils';
|
|
6
|
+
import {MD5Hash} from '@loaders.gl/crypto';
|
|
7
|
+
import {openZipArchive, type ZipArchive} from '../zip-archive';
|
|
8
|
+
import {parseHashTable} from '../hash-file-utility';
|
|
9
|
+
import type {ZipArchiveOptions} from '../zip-archive-types';
|
|
10
|
+
|
|
11
|
+
/** Base transport for formats with a final MD5 path/local-offset index. */
|
|
12
|
+
export abstract class IndexedArchive {
|
|
13
|
+
/** Owned random-access archive source. */
|
|
14
|
+
readonly file: ReadableFile;
|
|
15
|
+
/** Optional resource URL prefix. */
|
|
16
|
+
readonly fileName?: string;
|
|
17
|
+
/** Format-provided path hashes, or a directory-derived fallback. */
|
|
18
|
+
protected hashTable?: Record<string, bigint>;
|
|
19
|
+
/** Cancellation for metadata initialization and indexed reads. */
|
|
20
|
+
private readonly controller = new AbortController();
|
|
21
|
+
/** Shared lazy ZIP reader and directory cache. */
|
|
22
|
+
private readerPromise?: Promise<ZipArchive>;
|
|
23
|
+
/** Idempotent release even when no reader has been opened. */
|
|
24
|
+
private closePromise?: Promise<void>;
|
|
25
|
+
|
|
26
|
+
/** Captures the source and an optional already-loaded format index. */
|
|
27
|
+
constructor(
|
|
28
|
+
file: ReadableFile,
|
|
29
|
+
hashTable?: Record<string, bigint>,
|
|
30
|
+
fileName?: string,
|
|
31
|
+
/** Independent reader budgets captured for lazy initialization. */
|
|
32
|
+
private readonly options: ZipArchiveOptions = {}
|
|
33
|
+
) {
|
|
34
|
+
this.file = file;
|
|
35
|
+
this.hashTable = hashTable;
|
|
36
|
+
this.fileName = fileName;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Reads a format resource; adapters retain aliases and content-level decoding. */
|
|
40
|
+
abstract getFile(path: string, mode?: string): Promise<ArrayBuffer>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Loads a final index when present, otherwise hashes validated directory entries.
|
|
44
|
+
* The format supplies the reserved name and fallback path-case policy. Returns true for an index.
|
|
45
|
+
*/
|
|
46
|
+
async initializeIndex(indexPath: string, lowercasePaths = false): Promise<boolean> {
|
|
47
|
+
const reader = await this.getZipArchive();
|
|
48
|
+
const last = await reader.getLastEntry();
|
|
49
|
+
if (last?.path === indexPath) {
|
|
50
|
+
this.hashTable = parseHashTable(await reader.read(last));
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
const hash = new MD5Hash();
|
|
54
|
+
const table: Record<string, bigint> = Object.create(null);
|
|
55
|
+
for await (const entry of reader.entries()) {
|
|
56
|
+
const path = lowercasePaths ? entry.path.toLowerCase() : entry.path;
|
|
57
|
+
const digest = await hash.hash(new TextEncoder().encode(path).buffer, 'hex');
|
|
58
|
+
if (digest in table) throw new Error('Duplicate ZIP index path hash');
|
|
59
|
+
table[digest] = entry.localHeaderOffset;
|
|
60
|
+
}
|
|
61
|
+
this.hashTable = table;
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Closes the reader/transport exactly once and rejects subsequent resource reads. */
|
|
66
|
+
async close(): Promise<void> {
|
|
67
|
+
this.controller.abort(new Error('ZIP archive is closed'));
|
|
68
|
+
this.closePromise ||= this.readerPromise
|
|
69
|
+
? this.readerPromise.then(
|
|
70
|
+
reader => reader.close(),
|
|
71
|
+
() => {}
|
|
72
|
+
)
|
|
73
|
+
: this.file.close();
|
|
74
|
+
await this.closePromise;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Reads and verifies a complete ZIP entry, retaining direct indexed local-header access. */
|
|
78
|
+
protected async readZipEntry(path: string): Promise<ArrayBuffer | undefined> {
|
|
79
|
+
const reader = await this.getZipArchive();
|
|
80
|
+
let entry;
|
|
81
|
+
if (this.hashTable) {
|
|
82
|
+
const digest = await new MD5Hash().hash(new TextEncoder().encode(path).buffer, 'hex');
|
|
83
|
+
const offset = this.hashTable[digest];
|
|
84
|
+
if (offset === undefined) return undefined;
|
|
85
|
+
entry = await reader.getEntryAt(offset);
|
|
86
|
+
if (entry.path.toLowerCase() !== path.toLowerCase())
|
|
87
|
+
throw new Error('ZIP index path mismatch');
|
|
88
|
+
} else entry = await reader.getEntry(path);
|
|
89
|
+
return entry ? await reader.read(entry) : undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Compatibility helper for adapters with no path index. */
|
|
93
|
+
protected async getFileWithoutHash(path: string): Promise<ArrayBuffer> {
|
|
94
|
+
const data = await this.readZipEntry(path);
|
|
95
|
+
if (!data) throw new Error(`No such file in the archive: ${path}`);
|
|
96
|
+
return data;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Initializes container metadata once without reading unrelated payloads. */
|
|
100
|
+
async getZipArchive(): Promise<ZipArchive> {
|
|
101
|
+
if (this.closePromise) throw new Error('ZIP archive is closed');
|
|
102
|
+
this.readerPromise ||= openZipArchive(this.file, {
|
|
103
|
+
...this.options,
|
|
104
|
+
signal: this.options.signal
|
|
105
|
+
? AbortSignal.any([this.controller.signal, this.options.signal])
|
|
106
|
+
: this.controller.signal
|
|
107
|
+
});
|
|
108
|
+
return await this.readerPromise;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {isBrowser, BlobFile, NodeFile} from '@loaders.gl/core/loader-utils';
|
|
6
|
+
import type {FileSystem, ReadableFile} from '@loaders.gl/core/loader-utils';
|
|
7
|
+
import {IndexedArchive} from './IndexedArchive';
|
|
8
|
+
import {openZipArchive, type ZipArchive} from '../zip-archive';
|
|
9
|
+
import type {ZipArchiveInput, ZipArchiveOptions, ZipEntry} from '../zip-archive-types';
|
|
10
|
+
|
|
11
|
+
/** Validated entry metadata with a FileSystem-compatible numeric size. */
|
|
12
|
+
export type ZipFileStat = Omit<ZipEntry, 'size'> & {
|
|
13
|
+
/** Decoded length as a safe integer for FileSystem callers. */
|
|
14
|
+
readonly size: number;
|
|
15
|
+
/** Exact decoded length, including ZIP64 values. */
|
|
16
|
+
readonly uncompressedSize: bigint;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** An owned ZIP filesystem with cached lookup, confined resources and STORE byte ranges. */
|
|
20
|
+
export class ZipFileSystem implements FileSystem {
|
|
21
|
+
/** Owned random-access transport. */
|
|
22
|
+
readonly file: ReadableFile;
|
|
23
|
+
/** Optional archive name used in response URLs. */
|
|
24
|
+
readonly fileName?: string;
|
|
25
|
+
/** Optional format adapter retaining resource aliases and content-level decoding. */
|
|
26
|
+
readonly archive: IndexedArchive | null;
|
|
27
|
+
/** Shared reader initialization, performed once when first needed. */
|
|
28
|
+
private readerPromise?: Promise<ZipArchive>;
|
|
29
|
+
/** Cancellation for opening and all reads during the filesystem lifetime. */
|
|
30
|
+
private readonly controller = new AbortController();
|
|
31
|
+
/** Caller cancellation combined with filesystem destruction. */
|
|
32
|
+
private readonly signal: AbortSignal;
|
|
33
|
+
/** Idempotent transport release. */
|
|
34
|
+
private destroyPromise?: Promise<void>;
|
|
35
|
+
|
|
36
|
+
/** Opens portable bytes, a random-access file, a format adapter, or a Node file path lazily. */
|
|
37
|
+
constructor(
|
|
38
|
+
file: ZipArchiveInput | IndexedArchive | string,
|
|
39
|
+
/** Shared reader bounds captured for lazy initialization. */
|
|
40
|
+
private readonly options: ZipArchiveOptions = {}
|
|
41
|
+
) {
|
|
42
|
+
this.signal = options.signal
|
|
43
|
+
? AbortSignal.any([this.controller.signal, options.signal])
|
|
44
|
+
: this.controller.signal;
|
|
45
|
+
this.archive = file instanceof IndexedArchive ? file : null;
|
|
46
|
+
if (typeof file === 'string') {
|
|
47
|
+
if (isBrowser)
|
|
48
|
+
throw new Error('ZipFileSystem cannot open file paths in browser environments');
|
|
49
|
+
this.fileName = file;
|
|
50
|
+
this.file = new NodeFile(file);
|
|
51
|
+
} else if (file instanceof IndexedArchive) {
|
|
52
|
+
this.file = file.file;
|
|
53
|
+
this.fileName = file.fileName;
|
|
54
|
+
} else if (file instanceof Blob || file instanceof ArrayBuffer) {
|
|
55
|
+
this.file = new BlobFile(file);
|
|
56
|
+
} else if (ArrayBuffer.isView(file)) {
|
|
57
|
+
this.file = new BlobFile(
|
|
58
|
+
Uint8Array.from(new Uint8Array(file.buffer, file.byteOffset, file.byteLength)).buffer
|
|
59
|
+
);
|
|
60
|
+
} else {
|
|
61
|
+
this.file = file;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Cancels pending reads and closes the owned file once; later operations reject. */
|
|
66
|
+
destroy(): Promise<void> {
|
|
67
|
+
this.controller.abort(new Error('ZIP filesystem is closed'));
|
|
68
|
+
this.destroyPromise ||= this.archive
|
|
69
|
+
? this.archive.close()
|
|
70
|
+
: this.readerPromise
|
|
71
|
+
? this.readerPromise.then(
|
|
72
|
+
reader => reader.close(),
|
|
73
|
+
() => {}
|
|
74
|
+
)
|
|
75
|
+
: this.file.close();
|
|
76
|
+
return this.destroyPromise;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Lists validated archive paths in central-directory order, including directory entries. */
|
|
80
|
+
async readdir(): Promise<string[]> {
|
|
81
|
+
const reader = await this.getReader();
|
|
82
|
+
const paths: string[] = [];
|
|
83
|
+
for await (const entry of reader.entries()) paths.push(entry.path);
|
|
84
|
+
return paths;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Returns validated metadata; rejects sizes that cannot be represented safely as a number. */
|
|
88
|
+
async stat(filename: string): Promise<ZipFileStat> {
|
|
89
|
+
const entry = await this.getEntry(filename);
|
|
90
|
+
if (entry.size > BigInt(Number.MAX_SAFE_INTEGER))
|
|
91
|
+
throw new RangeError('ZIP FileSystem size is not safely representable');
|
|
92
|
+
return {...entry, size: Number(entry.size), uncompressedSize: entry.size};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Fetches a confined resource. Complete streams verify CRC when consumed through completion.
|
|
97
|
+
* Single STORE ranges return 206 or 416; compressed and format-decoded ranges reject.
|
|
98
|
+
*/
|
|
99
|
+
async fetch(filename: string, options: RequestInit = {}): Promise<Response> {
|
|
100
|
+
this.signal.throwIfAborted();
|
|
101
|
+
options.signal?.throwIfAborted();
|
|
102
|
+
if (options.method && options.method.toUpperCase() !== 'GET')
|
|
103
|
+
throw new Error('ZIP filesystem only supports GET');
|
|
104
|
+
let path: string;
|
|
105
|
+
const range = new Headers(options.headers).get('range');
|
|
106
|
+
let response: Response;
|
|
107
|
+
if (this.archive) {
|
|
108
|
+
path = this.resolveResourcePath(filename);
|
|
109
|
+
if (range !== null)
|
|
110
|
+
throw new Error('ZIP ranges are unavailable through format resource adapters');
|
|
111
|
+
response = new Response(await this.archive.getFile(path, 'http'));
|
|
112
|
+
this.signal.throwIfAborted();
|
|
113
|
+
options.signal?.throwIfAborted();
|
|
114
|
+
} else {
|
|
115
|
+
const reader = await this.getReader();
|
|
116
|
+
options.signal?.throwIfAborted();
|
|
117
|
+
const entry = await this.getEntry(filename);
|
|
118
|
+
options.signal?.throwIfAborted();
|
|
119
|
+
path = entry.path;
|
|
120
|
+
const headers = new Headers({
|
|
121
|
+
'Accept-Ranges': entry.compression === 'store' ? 'bytes' : 'none',
|
|
122
|
+
'Content-Length': String(entry.size)
|
|
123
|
+
});
|
|
124
|
+
if (range !== null) {
|
|
125
|
+
if (entry.compression !== 'store') throw new Error('ZIP ranges require STORE entries');
|
|
126
|
+
const bounds = parseByteRange(range, entry.size);
|
|
127
|
+
if (!bounds) {
|
|
128
|
+
headers.set('Content-Range', `bytes */${entry.size}`);
|
|
129
|
+
headers.set('Content-Length', '0');
|
|
130
|
+
response = new Response(null, {status: 416, headers});
|
|
131
|
+
} else {
|
|
132
|
+
const bytes = await reader.readRange(entry, {
|
|
133
|
+
...bounds,
|
|
134
|
+
signal: options.signal || undefined
|
|
135
|
+
});
|
|
136
|
+
headers.set('Content-Range', `bytes ${bounds.start}-${bounds.end - 1n}/${entry.size}`);
|
|
137
|
+
headers.set('Content-Length', String(bounds.end - bounds.start));
|
|
138
|
+
response = new Response(bytes, {status: 206, headers});
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
const iterator = reader
|
|
142
|
+
.readInBatches(entry, {signal: options.signal || undefined})
|
|
143
|
+
[Symbol.asyncIterator]();
|
|
144
|
+
const stream = new ReadableStream<Uint8Array<ArrayBuffer>>(
|
|
145
|
+
{
|
|
146
|
+
async pull(controller) {
|
|
147
|
+
try {
|
|
148
|
+
const chunk = await iterator.next();
|
|
149
|
+
if (chunk.done) controller.close();
|
|
150
|
+
else controller.enqueue(chunk.value);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
controller.error(error);
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
async cancel() {
|
|
156
|
+
await iterator.return?.();
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
{highWaterMark: 0}
|
|
160
|
+
);
|
|
161
|
+
response = new Response(stream, {headers});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
Object.defineProperty(response, 'url', {
|
|
165
|
+
value: this.getResourceUrl(path)
|
|
166
|
+
});
|
|
167
|
+
return response;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Resolves resources once, allowing parents inside the archive but rejecting root escapes. */
|
|
171
|
+
private resolveResourcePath(filename: string, decodeUri = false): string {
|
|
172
|
+
const archivePath = filename.startsWith('./') ? filename.slice(2) : filename;
|
|
173
|
+
const archiveName = this.fileName?.replace(/^\.\//, '');
|
|
174
|
+
if (archiveName && (archivePath === archiveName || archivePath.startsWith(`${archiveName}/`))) {
|
|
175
|
+
filename = archivePath.slice(archiveName.length).replace(/^\//, '');
|
|
176
|
+
decodeUri = true;
|
|
177
|
+
}
|
|
178
|
+
decodeUri ||= filename.startsWith('/') || filename.startsWith('./');
|
|
179
|
+
if (/^[a-z][a-z\d+.-]*:/i.test(filename) || filename.startsWith('//'))
|
|
180
|
+
throw new Error('ZIP resource must stay inside the archive');
|
|
181
|
+
const decoded = decodeUri ? decodeURIComponent(filename.split(/[?#]/, 1)[0]) : filename;
|
|
182
|
+
if (
|
|
183
|
+
decoded.includes('\\') ||
|
|
184
|
+
decoded.includes('\0') ||
|
|
185
|
+
decoded.startsWith('//') ||
|
|
186
|
+
/^[a-z][a-z\d+.-]*:/i.test(decoded)
|
|
187
|
+
)
|
|
188
|
+
throw new Error('ZIP resource must stay inside the archive');
|
|
189
|
+
const segments: string[] = [];
|
|
190
|
+
for (const segment of decoded.split('/')) {
|
|
191
|
+
if (!segment || segment === '.') continue;
|
|
192
|
+
if (segment === '..') {
|
|
193
|
+
if (!segments.length) throw new Error('ZIP resource escapes the archive');
|
|
194
|
+
segments.pop();
|
|
195
|
+
} else segments.push(segment);
|
|
196
|
+
}
|
|
197
|
+
return segments.join('/') + (decoded.endsWith('/') && segments.length ? '/' : '');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Creates a qualified URL that cannot be confused with a canonical literal archive path. */
|
|
201
|
+
private getResourceUrl(path: string): string {
|
|
202
|
+
const prefix =
|
|
203
|
+
this.fileName && !/^(?:\/|[a-z][a-z\d+.-]*:)/i.test(this.fileName)
|
|
204
|
+
? `./${this.fileName.replace(/^\.\//, '')}`
|
|
205
|
+
: this.fileName || '';
|
|
206
|
+
return `${prefix}/${path.split('/').map(encodeURIComponent).join('/')}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Shares the directory cache and lifetime with every filesystem lookup. */
|
|
210
|
+
private async getReader(): Promise<ZipArchive> {
|
|
211
|
+
this.signal.throwIfAborted();
|
|
212
|
+
if (this.archive) return await this.archive.getZipArchive();
|
|
213
|
+
this.readerPromise ||= openZipArchive(this.file, {
|
|
214
|
+
...this.options,
|
|
215
|
+
signal: this.signal
|
|
216
|
+
});
|
|
217
|
+
return await this.readerPromise;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Resolves one validated entry and reports absent resources explicitly. */
|
|
221
|
+
private async getEntry(path: string): Promise<ZipEntry> {
|
|
222
|
+
const reader = await this.getReader();
|
|
223
|
+
const entry =
|
|
224
|
+
(await reader.getEntry(path)) ||
|
|
225
|
+
(await reader.getEntry(this.resolveResourcePath(path, true)));
|
|
226
|
+
if (!entry) throw new Error(`File has not been found in the zip archive: ${path}`);
|
|
227
|
+
return entry;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Parses one inclusive HTTP byte range into an exclusive bounded STORE slice. */
|
|
232
|
+
function parseByteRange(value: string, size: bigint): {start: bigint; end: bigint} | undefined {
|
|
233
|
+
const match = /^bytes=(\d*)-(\d*)$/i.exec(value.trim());
|
|
234
|
+
if (!match || (!match[1] && !match[2]) || size === 0n) return undefined;
|
|
235
|
+
if (!match[1]) {
|
|
236
|
+
const suffix = BigInt(match[2]);
|
|
237
|
+
if (!suffix) return undefined;
|
|
238
|
+
return {start: suffix < size ? size - suffix : 0n, end: size};
|
|
239
|
+
}
|
|
240
|
+
const start = BigInt(match[1]);
|
|
241
|
+
const last = match[2] ? BigInt(match[2]) : size - 1n;
|
|
242
|
+
if (start >= size || last < start) return undefined;
|
|
243
|
+
return {start, end: last < size ? last + 1n : size};
|
|
244
|
+
}
|