@pwfabric/pack 0.0.0-stage → 1.0.0-rc.0
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/COMPAT.md +136 -0
- package/LICENSE +202 -0
- package/README.md +34 -2
- package/dist/archive.d.ts +57 -0
- package/dist/archive.d.ts.map +1 -0
- package/dist/archive.js +151 -0
- package/dist/archive.js.map +1 -0
- package/dist/content.d.ts +35 -0
- package/dist/content.d.ts.map +1 -0
- package/dist/content.js +49 -0
- package/dist/content.js.map +1 -0
- package/dist/folder.d.ts +28 -0
- package/dist/folder.d.ts.map +1 -0
- package/dist/folder.js +85 -0
- package/dist/folder.js.map +1 -0
- package/dist/hash.d.ts +22 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +41 -0
- package/dist/hash.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/manifest.d.ts +54 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/manifest.js +94 -0
- package/dist/manifest.js.map +1 -0
- package/dist/media.d.ts +78 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +135 -0
- package/dist/media.js.map +1 -0
- package/dist/paths.d.ts +45 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +126 -0
- package/dist/paths.js.map +1 -0
- package/dist/tar.d.ts +40 -0
- package/dist/tar.d.ts.map +1 -0
- package/dist/tar.js +277 -0
- package/dist/tar.js.map +1 -0
- package/dist/types.d.ts +60 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +18 -0
- package/dist/types.js.map +1 -0
- package/dist/validate.d.ts +64 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +267 -0
- package/dist/validate.js.map +1 -0
- package/package.json +54 -3
package/dist/media.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Media: an app's media declarations and the bytes they name.
|
|
3
|
+
*
|
|
4
|
+
* app/media/<key>.json one declaration: which image or file, which page slots
|
|
5
|
+
* media/<name> the bytes (images, video, fonts)
|
|
6
|
+
* media/provenance.json where each file under media/ came from, and its hash
|
|
7
|
+
*
|
|
8
|
+
* {@link mediaDeclarations} is the one reader of that layout, so a host that
|
|
9
|
+
* ingests a pack, a generator that writes one and the CLI that serves one do
|
|
10
|
+
* not each re-implement it.
|
|
11
|
+
*
|
|
12
|
+
* @module @pwfabric/pack/media
|
|
13
|
+
*/
|
|
14
|
+
import { safeParseMediaDeclaration, safeParseMediaProvenance, } from '@pwfabric/contracts';
|
|
15
|
+
import { sortedPaths } from './paths.js';
|
|
16
|
+
import { stripBom } from './manifest.js';
|
|
17
|
+
/** The provenance record of the media a pack carries. */
|
|
18
|
+
export const MEDIA_PROVENANCE_FILE = 'media/provenance.json';
|
|
19
|
+
/** Where media bytes live. */
|
|
20
|
+
export const MEDIA_DIR = 'media/';
|
|
21
|
+
/** Where media declarations live: `app/media/<key>.json`. */
|
|
22
|
+
export const MEDIA_DECLARATIONS_DIR = 'app/media/';
|
|
23
|
+
/** A folder no pack may carry: declarations and bytes live in `app/media/` and `media/`. */
|
|
24
|
+
export const LEGACY_ASSETS_DIR = 'app/assets/';
|
|
25
|
+
/** Read and check `media/provenance.json`. */
|
|
26
|
+
export function readMediaProvenance(pack) {
|
|
27
|
+
const bytes = pack.files.get(MEDIA_PROVENANCE_FILE);
|
|
28
|
+
if (!bytes)
|
|
29
|
+
return { state: 'absent' };
|
|
30
|
+
let value;
|
|
31
|
+
try {
|
|
32
|
+
value = JSON.parse(stripBom(new TextDecoder().decode(bytes)));
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
return { state: 'invalid', issues: [`is not JSON: ${e.message}`] };
|
|
36
|
+
}
|
|
37
|
+
const result = safeParseMediaProvenance(value);
|
|
38
|
+
return result.ok ? { state: 'valid', value: result.value } : { state: 'invalid', issues: result.issues };
|
|
39
|
+
}
|
|
40
|
+
/** Files directly in `dir` ending in `.json`, as `[key, path]`. */
|
|
41
|
+
function declarationFiles(paths, dir) {
|
|
42
|
+
const out = [];
|
|
43
|
+
for (const path of paths) {
|
|
44
|
+
if (!path.startsWith(dir))
|
|
45
|
+
continue;
|
|
46
|
+
const rest = path.slice(dir.length);
|
|
47
|
+
if (rest.includes('/') || !rest.endsWith('.json') || rest === '.json')
|
|
48
|
+
continue;
|
|
49
|
+
out.push([rest.slice(0, -'.json'.length), path]);
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The pack's media declarations (`app/media/<key>.json`), each with the bytes
|
|
55
|
+
* its `file` names (a pack path, `media/<name>`) and that file's provenance
|
|
56
|
+
* entry.
|
|
57
|
+
*
|
|
58
|
+
* Findings (also reported by `validatePack`, all errors unless noted):
|
|
59
|
+
* - `LEGACY_ASSETS_FOLDER` — a file under `app/assets/`. Nothing there is
|
|
60
|
+
* read: declarations belong in `app/media/`, bytes in `media/`.
|
|
61
|
+
* - `MEDIA_DECLARATION_INVALID` — a declaration that parses as JSON but
|
|
62
|
+
* breaks its schema; it is left out. (Not JSON at all is `JSON_INVALID`,
|
|
63
|
+
* reported by `validatePack`.)
|
|
64
|
+
* - `MEDIA_FILE_MISSING` — `file` names bytes the pack does not carry.
|
|
65
|
+
* - `MEDIA_FILE_UNRECORDED` — `file`'s bytes have no entry in
|
|
66
|
+
* `media/provenance.json`. Not reported when the record is itself invalid
|
|
67
|
+
* (`MEDIA_PROVENANCE_INVALID` covers it).
|
|
68
|
+
* - `APP_UNEXPECTED_FILE` (warning) — a file under `app/media/` that is not
|
|
69
|
+
* `<key>.json`.
|
|
70
|
+
*
|
|
71
|
+
* Never throws for content problems.
|
|
72
|
+
*/
|
|
73
|
+
export function mediaDeclarations(pack) {
|
|
74
|
+
const issues = [];
|
|
75
|
+
const add = (severity, code, message, path) => {
|
|
76
|
+
issues.push({ severity, code, message, path });
|
|
77
|
+
};
|
|
78
|
+
const paths = sortedPaths(pack.files.keys());
|
|
79
|
+
for (const path of paths) {
|
|
80
|
+
if (path.startsWith(LEGACY_ASSETS_DIR)) {
|
|
81
|
+
add('error', 'LEGACY_ASSETS_FOLDER', `${path} is under app/assets/, which is not read: declarations go in app/media/<key>.json, bytes in media/`, path);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const decls = declarationFiles(paths, MEDIA_DECLARATIONS_DIR);
|
|
85
|
+
const keyed = new Set(decls.map(([, p]) => p));
|
|
86
|
+
for (const path of paths) {
|
|
87
|
+
if (path.startsWith(MEDIA_DECLARATIONS_DIR) && !keyed.has(path)) {
|
|
88
|
+
add('warning', 'APP_UNEXPECTED_FILE', `${path} is not an app/media/<key>.json; media bytes belong in media/`, path);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const provenanceRead = readMediaProvenance(pack);
|
|
92
|
+
const provenance = provenanceRead.state === 'valid' ? provenanceRead.value : null;
|
|
93
|
+
const provenanceByPath = new Map((provenance?.items ?? []).map((item) => [item.path, item]));
|
|
94
|
+
const declarations = [];
|
|
95
|
+
for (const [key, path] of decls) {
|
|
96
|
+
let raw;
|
|
97
|
+
try {
|
|
98
|
+
raw = JSON.parse(stripBom(new TextDecoder().decode(pack.files.get(path))));
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
continue; // JSON_INVALID, reported by validatePack for every file under app/
|
|
102
|
+
}
|
|
103
|
+
const parsed = safeParseMediaDeclaration(raw);
|
|
104
|
+
if (!parsed.ok) {
|
|
105
|
+
for (const issue of parsed.issues)
|
|
106
|
+
add('error', 'MEDIA_DECLARATION_INVALID', `${path} ${issue}`, path);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const declaration = parsed.value;
|
|
110
|
+
if (declaration.file === undefined) {
|
|
111
|
+
declarations.push({ key, path, declaration });
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const filePath = declaration.file;
|
|
115
|
+
if (!pack.files.has(filePath)) {
|
|
116
|
+
add('error', 'MEDIA_FILE_MISSING', `${path} names file ${filePath}, which the pack does not carry`, path);
|
|
117
|
+
declarations.push({ key, path, declaration });
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const entry = provenanceByPath.get(filePath);
|
|
121
|
+
if (!entry && provenanceRead.state !== 'invalid') {
|
|
122
|
+
add('error', 'MEDIA_FILE_UNRECORDED', `${filePath}, named by ${path}, has no entry in media/provenance.json`, filePath);
|
|
123
|
+
}
|
|
124
|
+
declarations.push({
|
|
125
|
+
key,
|
|
126
|
+
path,
|
|
127
|
+
declaration,
|
|
128
|
+
filePath,
|
|
129
|
+
bytes: pack.files.get(filePath),
|
|
130
|
+
...(entry ? { provenance: entry } : {}),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return { declarations, provenance, issues };
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=media.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media.js","sourceRoot":"","sources":["../src/media.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EACL,yBAAyB,EACzB,wBAAwB,GAIzB,MAAM,qBAAqB,CAAA;AAE5B,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC,yDAAyD;AACzD,MAAM,CAAC,MAAM,qBAAqB,GAAG,uBAAuB,CAAA;AAC5D,8BAA8B;AAC9B,MAAM,CAAC,MAAM,SAAS,GAAG,QAAQ,CAAA;AACjC,6DAA6D;AAC7D,MAAM,CAAC,MAAM,sBAAsB,GAAG,YAAY,CAAA;AAClD,4FAA4F;AAC5F,MAAM,CAAC,MAAM,iBAAiB,GAAG,aAAa,CAAA;AA+B9C,8CAA8C;AAC9C,MAAM,UAAU,mBAAmB,CAAC,IAAgB;IAClD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK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AW,CAAC,IAAI,CAAA;QACjC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC9B,GAAG,CAAC,OAAO,EAAE,oBAAoB,EAAE,GAAG,IAAI,eAAe,QAAQ,iCAAiC,EAAE,IAAI,CAAC,CAAA;YACzG,YAAY,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAA;YAC7C,SAAQ;QACV,CAAC;QACD,MAAM,KAAK,GAAG,gBAAgB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;QAC5C,IAAI,CAAC,KAAK,IAAI,cAAc,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YACjD,GAAG,CAAC,OAAO,EAAE,uBAAuB,EAAE,GAAG,QAAQ,cAAc,IAAI,yCAAyC,EAAE,QAAQ,CAAC,CAAA;QACzH,CAAC;QACD,YAAY,CAAC,IAAI,CAAC;YAChB,GAAG;YACH,IAAI;YACJ,WAAW;YACX,QAAQ;YACR,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAe;YAC7C,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACxC,CAAC,CAAA;IACJ,CAAC;IAED,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,CAAA;AAC7C,CAAC"}
|
package/dist/paths.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path rules shared by the folder reader, the archive reader and writer, and
|
|
3
|
+
* validation. One definition of "safe", one ordering.
|
|
4
|
+
*
|
|
5
|
+
* @module @pwfabric/pack/paths
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Why a pack path is unsafe, or `null` when it is safe.
|
|
9
|
+
*
|
|
10
|
+
* Safe means: non-empty, relative, `/`-separated, no empty / `.` / `..`
|
|
11
|
+
* segment, no backslash, no NUL, no drive letter. Segment-based, so
|
|
12
|
+
* `a/../b`, a bare `..` and `..\x` all fail alike.
|
|
13
|
+
*/
|
|
14
|
+
export declare function unsafePathReason(path: string): string | null;
|
|
15
|
+
/** True when {@link unsafePathReason} finds nothing. */
|
|
16
|
+
export declare function isSafePackPath(path: string): boolean;
|
|
17
|
+
/** Byte-wise comparison of the UTF-8 encodings — stable across locales and platforms. */
|
|
18
|
+
export declare function compareBytes(a: string, b: string): number;
|
|
19
|
+
/** The pack's paths in byte order. */
|
|
20
|
+
export declare function sortedPaths(paths: Iterable<string>): string[];
|
|
21
|
+
/**
|
|
22
|
+
* The first file/directory collision in a set of file paths: a path that is a
|
|
23
|
+
* file and also a directory of another path (`atoms/x` and `atoms/x/y`).
|
|
24
|
+
* Such a set cannot exist on any file system — extracting it clobbers one
|
|
25
|
+
* side or fails. `null` when there is none. Checked in byte order, so the
|
|
26
|
+
* answer is the same whatever order the paths come in.
|
|
27
|
+
*/
|
|
28
|
+
export declare function findPathConflict(paths: Iterable<string>): {
|
|
29
|
+
readonly file: string;
|
|
30
|
+
readonly inside: string;
|
|
31
|
+
} | null;
|
|
32
|
+
/** The key two paths share when a case-insensitive, normalisation-insensitive file system treats them as one. */
|
|
33
|
+
export declare function foldPath(path: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Groups of distinct paths that collide on a case-insensitive file system
|
|
36
|
+
* (paths themselves are case-sensitive, so the pack is valid, but extracting
|
|
37
|
+
* it there overwrites one file with another). Two kinds: two files whose
|
|
38
|
+
* folded paths are equal (`Hero.json` / `hero.json`, or NFC / NFD spellings),
|
|
39
|
+
* and a file whose folded path equals the folded path of another file's
|
|
40
|
+
* directory (`Atoms` / `atoms/x`). Exact file/directory collisions are
|
|
41
|
+
* {@link findPathConflict}'s and are not repeated here. Each group is sorted
|
|
42
|
+
* in byte order; groups are sorted by their first member.
|
|
43
|
+
*/
|
|
44
|
+
export declare function findCaseConflicts(paths: Iterable<string>): string[][];
|
|
45
|
+
//# sourceMappingURL=paths.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAY5D;AAED,wDAAwD;AACxD,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED,yFAAyF;AACzF,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAUzD;AAED,sCAAsC;AACtC,wBAAgB,WAAW,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,MAAM,EAAE,CAE7D;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAWnH;AAED,iHAAiH;AACjH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,MAAM,EAAE,EAAE,CA4BrE"}
|
package/dist/paths.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path rules shared by the folder reader, the archive reader and writer, and
|
|
3
|
+
* validation. One definition of "safe", one ordering.
|
|
4
|
+
*
|
|
5
|
+
* @module @pwfabric/pack/paths
|
|
6
|
+
*/
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
/**
|
|
9
|
+
* Why a pack path is unsafe, or `null` when it is safe.
|
|
10
|
+
*
|
|
11
|
+
* Safe means: non-empty, relative, `/`-separated, no empty / `.` / `..`
|
|
12
|
+
* segment, no backslash, no NUL, no drive letter. Segment-based, so
|
|
13
|
+
* `a/../b`, a bare `..` and `..\x` all fail alike.
|
|
14
|
+
*/
|
|
15
|
+
export function unsafePathReason(path) {
|
|
16
|
+
if (path.length === 0)
|
|
17
|
+
return 'empty path';
|
|
18
|
+
if (path.includes('\0'))
|
|
19
|
+
return 'contains a NUL byte';
|
|
20
|
+
if (path.includes('\\'))
|
|
21
|
+
return 'contains a backslash';
|
|
22
|
+
if (path.startsWith('/'))
|
|
23
|
+
return 'absolute path';
|
|
24
|
+
if (/^[A-Za-z]:/.test(path))
|
|
25
|
+
return 'drive-qualified path';
|
|
26
|
+
for (const seg of path.split('/')) {
|
|
27
|
+
if (seg === '..')
|
|
28
|
+
return 'parent-directory segment';
|
|
29
|
+
if (seg === '.')
|
|
30
|
+
return 'current-directory segment';
|
|
31
|
+
if (seg.length === 0)
|
|
32
|
+
return 'empty segment';
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
/** True when {@link unsafePathReason} finds nothing. */
|
|
37
|
+
export function isSafePackPath(path) {
|
|
38
|
+
return unsafePathReason(path) === null;
|
|
39
|
+
}
|
|
40
|
+
/** Byte-wise comparison of the UTF-8 encodings — stable across locales and platforms. */
|
|
41
|
+
export function compareBytes(a, b) {
|
|
42
|
+
if (a === b)
|
|
43
|
+
return 0;
|
|
44
|
+
const ba = encoder.encode(a);
|
|
45
|
+
const bb = encoder.encode(b);
|
|
46
|
+
const n = Math.min(ba.length, bb.length);
|
|
47
|
+
for (let i = 0; i < n; i++) {
|
|
48
|
+
const d = ba[i] - bb[i];
|
|
49
|
+
if (d !== 0)
|
|
50
|
+
return d;
|
|
51
|
+
}
|
|
52
|
+
return ba.length - bb.length;
|
|
53
|
+
}
|
|
54
|
+
/** The pack's paths in byte order. */
|
|
55
|
+
export function sortedPaths(paths) {
|
|
56
|
+
return [...paths].sort(compareBytes);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The first file/directory collision in a set of file paths: a path that is a
|
|
60
|
+
* file and also a directory of another path (`atoms/x` and `atoms/x/y`).
|
|
61
|
+
* Such a set cannot exist on any file system — extracting it clobbers one
|
|
62
|
+
* side or fails. `null` when there is none. Checked in byte order, so the
|
|
63
|
+
* answer is the same whatever order the paths come in.
|
|
64
|
+
*/
|
|
65
|
+
export function findPathConflict(paths) {
|
|
66
|
+
const all = new Set(paths);
|
|
67
|
+
for (const path of sortedPaths(all)) {
|
|
68
|
+
let i = path.indexOf('/');
|
|
69
|
+
while (i !== -1) {
|
|
70
|
+
const dir = path.slice(0, i);
|
|
71
|
+
if (all.has(dir))
|
|
72
|
+
return { file: dir, inside: path };
|
|
73
|
+
i = path.indexOf('/', i + 1);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
/** The key two paths share when a case-insensitive, normalisation-insensitive file system treats them as one. */
|
|
79
|
+
export function foldPath(path) {
|
|
80
|
+
return path.normalize('NFC').toLowerCase();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Groups of distinct paths that collide on a case-insensitive file system
|
|
84
|
+
* (paths themselves are case-sensitive, so the pack is valid, but extracting
|
|
85
|
+
* it there overwrites one file with another). Two kinds: two files whose
|
|
86
|
+
* folded paths are equal (`Hero.json` / `hero.json`, or NFC / NFD spellings),
|
|
87
|
+
* and a file whose folded path equals the folded path of another file's
|
|
88
|
+
* directory (`Atoms` / `atoms/x`). Exact file/directory collisions are
|
|
89
|
+
* {@link findPathConflict}'s and are not repeated here. Each group is sorted
|
|
90
|
+
* in byte order; groups are sorted by their first member.
|
|
91
|
+
*/
|
|
92
|
+
export function findCaseConflicts(paths) {
|
|
93
|
+
const sorted = sortedPaths(new Set(paths));
|
|
94
|
+
const files = new Map();
|
|
95
|
+
for (const path of sorted) {
|
|
96
|
+
const key = foldPath(path);
|
|
97
|
+
const group = files.get(key);
|
|
98
|
+
if (group)
|
|
99
|
+
group.push(path);
|
|
100
|
+
else
|
|
101
|
+
files.set(key, [path]);
|
|
102
|
+
}
|
|
103
|
+
const dirs = new Map();
|
|
104
|
+
for (const path of sorted) {
|
|
105
|
+
let i = path.indexOf('/');
|
|
106
|
+
while (i !== -1) {
|
|
107
|
+
const dir = path.slice(0, i);
|
|
108
|
+
const key = foldPath(dir);
|
|
109
|
+
const set = dirs.get(key) ?? new Set();
|
|
110
|
+
set.add(dir);
|
|
111
|
+
dirs.set(key, set);
|
|
112
|
+
i = path.indexOf('/', i + 1);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const out = [];
|
|
116
|
+
for (const [key, group] of files) {
|
|
117
|
+
const members = new Set(group);
|
|
118
|
+
for (const dir of dirs.get(key) ?? [])
|
|
119
|
+
if (!members.has(dir))
|
|
120
|
+
members.add(`${dir}/`);
|
|
121
|
+
if (members.size > 1)
|
|
122
|
+
out.push(sortedPaths(members));
|
|
123
|
+
}
|
|
124
|
+
return out.sort((a, b) => compareBytes(a[0], b[0]));
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=paths.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paths.js","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;AAEjC;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,YAAY,CAAA;IAC1C,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,qBAAqB,CAAA;IACrD,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,sBAAsB,CAAA;IACtD,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,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AC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAA;IACtD,CAAC;IACD,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAW,EAAE,CAAC,CAAC,CAAC,CAAW,CAAC,CAAC,CAAA;AACzE,CAAC"}
|
package/dist/tar.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Mode written for every regular file. Executable bits are not carried. */
|
|
2
|
+
export declare const FILE_MODE = 420;
|
|
3
|
+
/** Mode written for every directory entry. */
|
|
4
|
+
export declare const DIR_MODE = 493;
|
|
5
|
+
/** mtime written into every header (seconds since the epoch). */
|
|
6
|
+
export declare const FIXED_MTIME = 0;
|
|
7
|
+
/**
|
|
8
|
+
* Split a path into ustar `prefix` (≤155 bytes) and `name` (≤100 bytes) at a
|
|
9
|
+
* `/`. The longest prefix that fits is chosen. `null` when no split fits.
|
|
10
|
+
*/
|
|
11
|
+
export declare function splitUstarPath(path: string): {
|
|
12
|
+
prefix: string;
|
|
13
|
+
name: string;
|
|
14
|
+
} | null;
|
|
15
|
+
/**
|
|
16
|
+
* The tar of a file map. Every directory implied by a file path gets its own
|
|
17
|
+
* entry (`<dir>/`); all names are emitted in byte order, so a parent always
|
|
18
|
+
* precedes its children. Callers pass already-validated paths.
|
|
19
|
+
*/
|
|
20
|
+
export declare function writeTar(files: ReadonlyMap<string, Uint8Array>): Uint8Array;
|
|
21
|
+
/** What a tar member is. */
|
|
22
|
+
export type TarEntryType = 'file' | 'dir' | 'symlink' | 'hardlink' | 'other';
|
|
23
|
+
export interface TarEntry {
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly type: TarEntryType;
|
|
26
|
+
readonly data: Uint8Array;
|
|
27
|
+
}
|
|
28
|
+
export interface TarReadLimits {
|
|
29
|
+
readonly maxEntries: number;
|
|
30
|
+
readonly maxEntryBytes: number;
|
|
31
|
+
readonly maxTotalBytes: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Every member of an uncompressed tar, in archive order. Names are returned
|
|
35
|
+
* as stored (ustar prefix joined, PAX `path` and GNU long names applied);
|
|
36
|
+
* normalisation and safety are the caller's job. Throws {@link PackError}
|
|
37
|
+
* for a malformed stream or a crossed limit.
|
|
38
|
+
*/
|
|
39
|
+
export declare function readTar(tar: Uint8Array, limits: TarReadLimits): TarEntry[];
|
|
40
|
+
//# sourceMappingURL=tar.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tar.d.ts","sourceRoot":"","sources":["../src/tar.ts"],"names":[],"mappings":"AAiBA,4EAA4E;AAC5E,eAAO,MAAM,SAAS,MAAQ,CAAA;AAC9B,8CAA8C;AAC9C,eAAO,MAAM,QAAQ,MAAQ,CAAA;AAC7B,iEAAiE;AACjE,eAAO,MAAM,WAAW,IAAI,CAAA;AAmB5B;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CASpF;AAqED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,UAAU,CAmB3E;AAMD,4BAA4B;AAC5B,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,KAAK,GAAG,SAAS,GAAG,UAAU,GAAG,OAAO,CAAA;AAE5E,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAA;IAC3B,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;CAC1B;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAA;IAC9B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAA;CAC/B;AA+ED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,aAAa,GAAG,QAAQ,EAAE,CAgD1E"}
|
package/dist/tar.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A minimal, deterministic ustar writer and a defensive tar reader.
|
|
3
|
+
*
|
|
4
|
+
* Only what a pwpack needs: regular files and directories on the way out;
|
|
5
|
+
* regular files, directories, PAX `path` records and GNU long names on the
|
|
6
|
+
* way in, with links, devices and FIFOs surfaced so the caller can refuse
|
|
7
|
+
* them. Not a general-purpose tar implementation.
|
|
8
|
+
*
|
|
9
|
+
* @module @pwfabric/pack/tar
|
|
10
|
+
*/
|
|
11
|
+
import { PackError } from './types.js';
|
|
12
|
+
import { compareBytes } from './paths.js';
|
|
13
|
+
const BLOCK = 512;
|
|
14
|
+
const encoder = new TextEncoder();
|
|
15
|
+
const decoder = new TextDecoder();
|
|
16
|
+
/** Mode written for every regular file. Executable bits are not carried. */
|
|
17
|
+
export const FILE_MODE = 0o644;
|
|
18
|
+
/** Mode written for every directory entry. */
|
|
19
|
+
export const DIR_MODE = 0o755;
|
|
20
|
+
/** mtime written into every header (seconds since the epoch). */
|
|
21
|
+
export const FIXED_MTIME = 0;
|
|
22
|
+
// ============================================================
|
|
23
|
+
// WRITE
|
|
24
|
+
// ============================================================
|
|
25
|
+
function putString(buf, value, offset, length) {
|
|
26
|
+
buf.set(encoder.encode(value).subarray(0, length), offset);
|
|
27
|
+
}
|
|
28
|
+
/** `width - 1` octal digits followed by NUL. */
|
|
29
|
+
function putOctal(buf, value, offset, width) {
|
|
30
|
+
putString(buf, value.toString(8).padStart(width - 1, '0') + '\0', offset, width);
|
|
31
|
+
}
|
|
32
|
+
function byteLength(s) {
|
|
33
|
+
return encoder.encode(s).length;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Split a path into ustar `prefix` (≤155 bytes) and `name` (≤100 bytes) at a
|
|
37
|
+
* `/`. The longest prefix that fits is chosen. `null` when no split fits.
|
|
38
|
+
*/
|
|
39
|
+
export function splitUstarPath(path) {
|
|
40
|
+
if (byteLength(path) <= 100)
|
|
41
|
+
return { prefix: '', name: path };
|
|
42
|
+
for (let i = path.length - 1; i > 0; i--) {
|
|
43
|
+
if (path[i] !== '/')
|
|
44
|
+
continue;
|
|
45
|
+
const prefix = path.slice(0, i);
|
|
46
|
+
const name = path.slice(i + 1);
|
|
47
|
+
if (name.length > 0 && byteLength(prefix) <= 155 && byteLength(name) <= 100)
|
|
48
|
+
return { prefix, name };
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
/** The first `max` bytes of `s`, cut at a character boundary. */
|
|
53
|
+
function truncateUtf8(s, max) {
|
|
54
|
+
let out = '';
|
|
55
|
+
let used = 0;
|
|
56
|
+
for (const ch of s) {
|
|
57
|
+
const n = byteLength(ch);
|
|
58
|
+
if (used + n > max)
|
|
59
|
+
break;
|
|
60
|
+
out += ch;
|
|
61
|
+
used += n;
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
function header(name, prefix, size, mode, typeflag) {
|
|
66
|
+
const h = new Uint8Array(BLOCK);
|
|
67
|
+
putString(h, name, 0, 100);
|
|
68
|
+
putOctal(h, mode, 100, 8);
|
|
69
|
+
putOctal(h, 0, 108, 8); // uid
|
|
70
|
+
putOctal(h, 0, 116, 8); // gid
|
|
71
|
+
putOctal(h, size, 124, 12);
|
|
72
|
+
putOctal(h, FIXED_MTIME, 136, 12);
|
|
73
|
+
putString(h, ' ', 148, 8); // checksum placeholder
|
|
74
|
+
putString(h, typeflag, 156, 1);
|
|
75
|
+
// linkname (157..256) stays zero.
|
|
76
|
+
putString(h, 'ustar\0', 257, 6);
|
|
77
|
+
putString(h, '00', 263, 2);
|
|
78
|
+
// uname (265..296) and gname (297..328) stay empty.
|
|
79
|
+
putOctal(h, 0, 329, 8); // devmajor
|
|
80
|
+
putOctal(h, 0, 337, 8); // devminor
|
|
81
|
+
if (prefix)
|
|
82
|
+
putString(h, prefix, 345, 155);
|
|
83
|
+
let sum = 0;
|
|
84
|
+
for (const b of h)
|
|
85
|
+
sum += b;
|
|
86
|
+
putString(h, sum.toString(8).padStart(6, '0') + '\0 ', 148, 8);
|
|
87
|
+
return h;
|
|
88
|
+
}
|
|
89
|
+
function padding(size) {
|
|
90
|
+
const rem = size % BLOCK;
|
|
91
|
+
return new Uint8Array(rem === 0 ? 0 : BLOCK - rem);
|
|
92
|
+
}
|
|
93
|
+
/** A PAX `x` header + data blocks carrying `path=<path>` for the entry that follows. */
|
|
94
|
+
function paxPathBlocks(path) {
|
|
95
|
+
const record = ` path=${path}\n`;
|
|
96
|
+
// The length prefix counts itself; iterate until it is stable.
|
|
97
|
+
let len = byteLength(record) + 1;
|
|
98
|
+
while (byteLength(`${len}${record}`) !== len)
|
|
99
|
+
len = byteLength(`${len}${record}`);
|
|
100
|
+
const data = encoder.encode(`${len}${record}`);
|
|
101
|
+
return [header('PaxHeader', '', data.length, FILE_MODE, 'x'), data, padding(data.length)];
|
|
102
|
+
}
|
|
103
|
+
function entryBlocks(path, data) {
|
|
104
|
+
const size = data?.length ?? 0;
|
|
105
|
+
const mode = data ? FILE_MODE : DIR_MODE;
|
|
106
|
+
const typeflag = data ? '0' : '5';
|
|
107
|
+
const split = splitUstarPath(path);
|
|
108
|
+
const out = [];
|
|
109
|
+
if (split) {
|
|
110
|
+
out.push(header(split.name, split.prefix, size, mode, typeflag));
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
out.push(...paxPathBlocks(path));
|
|
114
|
+
out.push(header(truncateUtf8(path, 100), '', size, mode, typeflag));
|
|
115
|
+
}
|
|
116
|
+
if (data)
|
|
117
|
+
out.push(data, padding(size));
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The tar of a file map. Every directory implied by a file path gets its own
|
|
122
|
+
* entry (`<dir>/`); all names are emitted in byte order, so a parent always
|
|
123
|
+
* precedes its children. Callers pass already-validated paths.
|
|
124
|
+
*/
|
|
125
|
+
export function writeTar(files) {
|
|
126
|
+
const names = new Map();
|
|
127
|
+
for (const [path, data] of files) {
|
|
128
|
+
const parts = path.split('/');
|
|
129
|
+
for (let i = 1; i < parts.length; i++)
|
|
130
|
+
names.set(`${parts.slice(0, i).join('/')}/`, null);
|
|
131
|
+
names.set(path, data);
|
|
132
|
+
}
|
|
133
|
+
const chunks = [];
|
|
134
|
+
for (const name of [...names.keys()].sort(compareBytes))
|
|
135
|
+
chunks.push(...entryBlocks(name, names.get(name) ?? null));
|
|
136
|
+
chunks.push(new Uint8Array(BLOCK * 2));
|
|
137
|
+
let total = 0;
|
|
138
|
+
for (const c of chunks)
|
|
139
|
+
total += c.length;
|
|
140
|
+
const out = new Uint8Array(total);
|
|
141
|
+
let at = 0;
|
|
142
|
+
for (const c of chunks) {
|
|
143
|
+
out.set(c, at);
|
|
144
|
+
at += c.length;
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
function cString(bytes) {
|
|
149
|
+
const end = bytes.indexOf(0);
|
|
150
|
+
return decoder.decode(end === -1 ? bytes : bytes.subarray(0, end));
|
|
151
|
+
}
|
|
152
|
+
function readOctal(h, offset, width, what) {
|
|
153
|
+
const field = h.subarray(offset, offset + width);
|
|
154
|
+
// Base-256 (high bit set) is how tar spells numbers past the octal range — far past any limit here.
|
|
155
|
+
if ((field[0] & 0x80) !== 0) {
|
|
156
|
+
throw new PackError('ENTRY_TOO_LARGE', `tar ${what} uses base-256 encoding (a value beyond the octal range)`);
|
|
157
|
+
}
|
|
158
|
+
const text = cString(field).trim();
|
|
159
|
+
if (text === '')
|
|
160
|
+
return 0;
|
|
161
|
+
if (!/^[0-7]+$/.test(text))
|
|
162
|
+
throw new PackError('TAR_INVALID', `tar ${what} is not an octal number: "${text}"`);
|
|
163
|
+
return parseInt(text, 8);
|
|
164
|
+
}
|
|
165
|
+
function verifyChecksum(h) {
|
|
166
|
+
const stored = readOctal(h, 148, 8, 'checksum');
|
|
167
|
+
let unsigned = 0;
|
|
168
|
+
let signed = 0;
|
|
169
|
+
for (let i = 0; i < BLOCK; i++) {
|
|
170
|
+
const b = i >= 148 && i < 156 ? 0x20 : h[i];
|
|
171
|
+
unsigned += b;
|
|
172
|
+
signed += b > 127 ? b - 256 : b;
|
|
173
|
+
}
|
|
174
|
+
if (stored !== unsigned && stored !== signed) {
|
|
175
|
+
throw new PackError('TAR_INVALID', 'tar header checksum does not match');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** The `path` record of a PAX extended header, if it has one. Lengths count bytes. */
|
|
179
|
+
function paxPath(data) {
|
|
180
|
+
let at = 0;
|
|
181
|
+
let path = null;
|
|
182
|
+
while (at < data.length) {
|
|
183
|
+
const space = data.indexOf(0x20, at);
|
|
184
|
+
if (space === -1)
|
|
185
|
+
break;
|
|
186
|
+
const len = Number(decoder.decode(data.subarray(at, space)));
|
|
187
|
+
if (!Number.isInteger(len) || len <= space - at + 1 || at + len > data.length) {
|
|
188
|
+
throw new PackError('TAR_INVALID', 'malformed PAX record');
|
|
189
|
+
}
|
|
190
|
+
const body = decoder.decode(data.subarray(space + 1, at + len)).replace(/\n$/, '');
|
|
191
|
+
const eq = body.indexOf('=');
|
|
192
|
+
if (eq > 0 && body.slice(0, eq) === 'path')
|
|
193
|
+
path = body.slice(eq + 1);
|
|
194
|
+
at += len;
|
|
195
|
+
}
|
|
196
|
+
return path;
|
|
197
|
+
}
|
|
198
|
+
function typeOf(flag) {
|
|
199
|
+
switch (String.fromCharCode(flag)) {
|
|
200
|
+
case '\0':
|
|
201
|
+
case '0':
|
|
202
|
+
case '7':
|
|
203
|
+
return 'file';
|
|
204
|
+
case '5':
|
|
205
|
+
return 'dir';
|
|
206
|
+
case '1':
|
|
207
|
+
return 'hardlink';
|
|
208
|
+
case '2':
|
|
209
|
+
return 'symlink';
|
|
210
|
+
case 'x':
|
|
211
|
+
return 'pax';
|
|
212
|
+
case 'g':
|
|
213
|
+
return 'pax-global';
|
|
214
|
+
case 'L':
|
|
215
|
+
return 'gnu-longname';
|
|
216
|
+
case 'K':
|
|
217
|
+
return 'gnu-longlink';
|
|
218
|
+
default:
|
|
219
|
+
return 'other';
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Every member of an uncompressed tar, in archive order. Names are returned
|
|
224
|
+
* as stored (ustar prefix joined, PAX `path` and GNU long names applied);
|
|
225
|
+
* normalisation and safety are the caller's job. Throws {@link PackError}
|
|
226
|
+
* for a malformed stream or a crossed limit.
|
|
227
|
+
*/
|
|
228
|
+
export function readTar(tar, limits) {
|
|
229
|
+
const out = [];
|
|
230
|
+
let at = 0;
|
|
231
|
+
let total = 0;
|
|
232
|
+
let nextName = null;
|
|
233
|
+
while (at + BLOCK <= tar.length) {
|
|
234
|
+
const h = tar.subarray(at, at + BLOCK);
|
|
235
|
+
if (h.every((b) => b === 0))
|
|
236
|
+
break;
|
|
237
|
+
verifyChecksum(h);
|
|
238
|
+
const size = readOctal(h, 124, 12, 'size');
|
|
239
|
+
const kind = typeOf(h[156]);
|
|
240
|
+
at += BLOCK;
|
|
241
|
+
if (at + size > tar.length)
|
|
242
|
+
throw new PackError('TAR_INVALID', 'tar entry is truncated');
|
|
243
|
+
const data = tar.subarray(at, at + size);
|
|
244
|
+
at += Math.ceil(size / BLOCK) * BLOCK;
|
|
245
|
+
if (kind === 'pax') {
|
|
246
|
+
nextName = paxPath(data) ?? nextName;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (kind === 'gnu-longname') {
|
|
250
|
+
nextName = cString(data);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (kind === 'pax-global' || kind === 'gnu-longlink')
|
|
254
|
+
continue;
|
|
255
|
+
// POSIX ustar ("ustar\0") carries a prefix field; the old GNU magic ("ustar ") uses those bytes for other things.
|
|
256
|
+
const posix = decoder.decode(h.subarray(257, 263)) === 'ustar\0';
|
|
257
|
+
const prefix = posix ? cString(h.subarray(345, 500)) : '';
|
|
258
|
+
const short = cString(h.subarray(0, 100));
|
|
259
|
+
const name = nextName ?? (prefix ? `${prefix}/${short}` : short);
|
|
260
|
+
nextName = null;
|
|
261
|
+
if (out.length >= limits.maxEntries) {
|
|
262
|
+
throw new PackError('TOO_MANY_ENTRIES', `archive has more than ${limits.maxEntries} entries`);
|
|
263
|
+
}
|
|
264
|
+
if (kind === 'file') {
|
|
265
|
+
if (size > limits.maxEntryBytes) {
|
|
266
|
+
throw new PackError('ENTRY_TOO_LARGE', `${name} is ${size} bytes (limit ${limits.maxEntryBytes})`, name);
|
|
267
|
+
}
|
|
268
|
+
total += size;
|
|
269
|
+
if (total > limits.maxTotalBytes) {
|
|
270
|
+
throw new PackError('PACK_TOO_LARGE', `archive files exceed ${limits.maxTotalBytes} bytes in total`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
out.push({ name, type: kind, data: kind === 'file' ? data : new Uint8Array(0) });
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
//# sourceMappingURL=tar.js.map
|