@stigmer/artifact-fetch 0.0.0-stage → 0.1.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/LICENSE +194 -0
- package/README.md +19 -2
- package/digest.d.ts +38 -0
- package/digest.d.ts.map +1 -0
- package/digest.js +67 -0
- package/digest.js.map +1 -0
- package/download.d.ts +35 -0
- package/download.d.ts.map +1 -0
- package/download.js +63 -0
- package/download.js.map +1 -0
- package/errors.d.ts +14 -0
- package/errors.d.ts.map +1 -0
- package/errors.js +18 -0
- package/errors.js.map +1 -0
- package/index.d.ts +6 -0
- package/index.d.ts.map +1 -0
- package/index.js +6 -0
- package/index.js.map +1 -0
- package/package.json +34 -4
- package/retry.d.ts +35 -0
- package/retry.d.ts.map +1 -0
- package/retry.js +74 -0
- package/retry.js.map +1 -0
- package/src/digest.ts +76 -0
- package/src/download.ts +80 -0
- package/src/errors.ts +18 -0
- package/src/index.ts +23 -0
- package/src/retry.ts +88 -0
- package/src/tar.ts +290 -0
- package/src/testing.ts +82 -0
- package/tar.d.ts +79 -0
- package/tar.d.ts.map +1 -0
- package/tar.js +251 -0
- package/tar.js.map +1 -0
- package/testing.d.ts +31 -0
- package/testing.d.ts.map +1 -0
- package/testing.js +65 -0
- package/testing.js.map +1 -0
package/src/testing.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte-level tar fixture builder for this library's consumers' tests
|
|
3
|
+
* (exported as `@stigmer/artifact-fetch/testing`). The lib's own suite and
|
|
4
|
+
* the runner's engine-fetch suite craft archives with it, so a fixture that
|
|
5
|
+
* breaks a rule (a link, a `..` entry, a corrupt header) is spelled one way.
|
|
6
|
+
*
|
|
7
|
+
* Emits real ustar headers with a valid checksum unless an entry asks
|
|
8
|
+
* otherwise, the zero blocks that end an archive, and, through
|
|
9
|
+
* {@link buildPackageTarball}, an npm-shaped `.tgz` whose entries sit under
|
|
10
|
+
* `package/`.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { createHash } from "node:crypto";
|
|
14
|
+
import { gzipSync } from "node:zlib";
|
|
15
|
+
|
|
16
|
+
export interface TarFixtureEntry {
|
|
17
|
+
/** The path as the archive spells it. Names over 100 bytes are split into the ustar prefix. */
|
|
18
|
+
readonly name: string;
|
|
19
|
+
readonly content?: string | Uint8Array;
|
|
20
|
+
/** The type flag; `"0"` (a regular file) by default. */
|
|
21
|
+
readonly typeFlag?: string;
|
|
22
|
+
/** Permission bits; 0o644 by default. */
|
|
23
|
+
readonly mode?: number;
|
|
24
|
+
/** The link target of a symlink or hard link. */
|
|
25
|
+
readonly linkName?: string;
|
|
26
|
+
/** Write a checksum that does not match the header. */
|
|
27
|
+
readonly corruptChecksum?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const BLOCK = 512;
|
|
31
|
+
|
|
32
|
+
/** An uncompressed tar archive of `entries`, ended by two zero blocks. */
|
|
33
|
+
export function buildTar(entries: readonly TarFixtureEntry[]): Buffer {
|
|
34
|
+
const parts: Buffer[] = [];
|
|
35
|
+
for (const entry of entries) {
|
|
36
|
+
const content = typeof entry.content === "string" ? Buffer.from(entry.content) : Buffer.from(entry.content ?? new Uint8Array(0));
|
|
37
|
+
parts.push(header(entry, content.length), content, Buffer.alloc((BLOCK - (content.length % BLOCK)) % BLOCK));
|
|
38
|
+
}
|
|
39
|
+
parts.push(Buffer.alloc(BLOCK * 2));
|
|
40
|
+
return Buffer.concat(parts);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function header(entry: TarFixtureEntry, size: number): Buffer {
|
|
44
|
+
const block = Buffer.alloc(BLOCK);
|
|
45
|
+
let name = entry.name;
|
|
46
|
+
let prefix = "";
|
|
47
|
+
if (Buffer.byteLength(name) > 100) {
|
|
48
|
+
const cut = name.lastIndexOf("/", name.length - 1);
|
|
49
|
+
prefix = name.slice(0, cut);
|
|
50
|
+
name = name.slice(cut + 1);
|
|
51
|
+
}
|
|
52
|
+
block.write(name, 0, "utf8");
|
|
53
|
+
block.write(octal(entry.mode ?? 0o644, 8), 100, "ascii");
|
|
54
|
+
block.write(octal(0, 8), 108, "ascii");
|
|
55
|
+
block.write(octal(0, 8), 116, "ascii");
|
|
56
|
+
block.write(octal(size, 12), 124, "ascii");
|
|
57
|
+
block.write(octal(0, 12), 136, "ascii");
|
|
58
|
+
block.write(entry.typeFlag ?? "0", 156, "ascii");
|
|
59
|
+
if (entry.linkName !== undefined) block.write(entry.linkName, 157, "utf8");
|
|
60
|
+
block.write("ustar\0", 257, "ascii");
|
|
61
|
+
block.write("00", 263, "ascii");
|
|
62
|
+
if (prefix !== "") block.write(prefix, 345, "utf8");
|
|
63
|
+
block.fill(0x20, 148, 156);
|
|
64
|
+
let sum = 0;
|
|
65
|
+
for (const byte of block) sum += byte;
|
|
66
|
+
block.write(octal(entry.corruptChecksum === true ? sum + 1 : sum, 7) + "\0", 148, "ascii");
|
|
67
|
+
return block;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function octal(value: number, width: number): string {
|
|
71
|
+
return value.toString(8).padStart(width - 1, "0") + "\0";
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** An npm-shaped package tarball: every file under `package/`, gzipped. */
|
|
75
|
+
export function buildPackageTarball(files: Readonly<Record<string, string | Uint8Array>>, modes: Readonly<Record<string, number>> = {}): Buffer {
|
|
76
|
+
return gzipSync(buildTar(Object.entries(files).map(([path, content]) => ({ name: `package/${path}`, content, mode: modes[path] ?? 0o644 }))));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The Subresource Integrity string npm records for `bytes`. */
|
|
80
|
+
export function sriSha512(bytes: Uint8Array): string {
|
|
81
|
+
return `sha512-${createHash("sha512").update(bytes).digest("base64")}`;
|
|
82
|
+
}
|
package/tar.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One minimal POSIX/ustar reader, two uses.
|
|
3
|
+
*
|
|
4
|
+
* {@link parseTarHeader} reads a 512-byte header block: the name (with the
|
|
5
|
+
* ustar prefix), the octal mode and size, the type flag, and whether the
|
|
6
|
+
* header's checksum holds. Everything else here walks archives with it:
|
|
7
|
+
*
|
|
8
|
+
* - {@link extractTarEntry} returns one regular file by basename from an
|
|
9
|
+
* uncompressed archive in memory (a release's single binary, such as the
|
|
10
|
+
* Temporal CLI);
|
|
11
|
+
* - {@link readTar} walks an archive as it streams, entry by entry, and
|
|
12
|
+
* {@link extractPackageTarball} uses it to unpack a whole npm package from
|
|
13
|
+
* a `.tgz` on disk under strict rules: every entry a regular file under the
|
|
14
|
+
* prefix npm packs with (`package/`), no absolute path, no `.` or `..`
|
|
15
|
+
* segment, no duplicate, a valid header checksum. Any other entry refuses
|
|
16
|
+
* the archive, links and pax headers included, so a package can never
|
|
17
|
+
* write outside the directory it is unpacked into. npm packs regular files
|
|
18
|
+
* only, so a published package passes.
|
|
19
|
+
*
|
|
20
|
+
* No native `tar` and no dependency: the CLI's base install stays lean, and
|
|
21
|
+
* the runner bundles this into its single file.
|
|
22
|
+
*/
|
|
23
|
+
/** A tar block: headers and data are padded to it. */
|
|
24
|
+
export declare const TAR_BLOCK = 512;
|
|
25
|
+
/** What an entry is, from its type flag. */
|
|
26
|
+
export type TarEntryKind = "file" | "directory" | "symlink" | "hardlink" | "pax" | "other";
|
|
27
|
+
/** One header block, read. */
|
|
28
|
+
export interface TarHeader {
|
|
29
|
+
/** The entry's path: the ustar prefix and name joined, as the archive spells it. */
|
|
30
|
+
readonly name: string;
|
|
31
|
+
/** Permission bits from the header (octal), 0 when unreadable. */
|
|
32
|
+
readonly mode: number;
|
|
33
|
+
/** The data's length in bytes; `NaN` when the size field is not octal. */
|
|
34
|
+
readonly size: number;
|
|
35
|
+
readonly kind: TarEntryKind;
|
|
36
|
+
/** The raw type flag character. */
|
|
37
|
+
readonly typeFlag: string;
|
|
38
|
+
/** Whether the header's stored checksum equals its computed one. */
|
|
39
|
+
readonly checksumValid: boolean;
|
|
40
|
+
}
|
|
41
|
+
/** The header in `block`, or `null` for a zero block, which marks the end of an archive. */
|
|
42
|
+
export declare function parseTarHeader(block: Uint8Array): TarHeader | null;
|
|
43
|
+
/**
|
|
44
|
+
* Extract a single regular-file entry whose basename matches `name` from an
|
|
45
|
+
* (uncompressed) tar buffer, or `null` when there is none.
|
|
46
|
+
*/
|
|
47
|
+
export declare function extractTarEntry(buf: Uint8Array, name: string): Uint8Array | null;
|
|
48
|
+
/** One entry of a streamed archive: its header, and its data, readable once before the walk moves on. */
|
|
49
|
+
export interface TarEntry {
|
|
50
|
+
readonly header: TarHeader;
|
|
51
|
+
/** The entry's bytes in order; an entry whose data is not read is skipped. */
|
|
52
|
+
data(): AsyncGenerator<Uint8Array>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The entries of an uncompressed tar stream, in order. Each entry's data must
|
|
56
|
+
* be read (or left) before the next entry is requested. Ends at the archive's
|
|
57
|
+
* zero block, draining what follows; throws on a stream that ends inside a
|
|
58
|
+
* header or an entry's data, and on a size field that is not octal.
|
|
59
|
+
*/
|
|
60
|
+
export declare function readTar(source: AsyncIterable<Uint8Array>): AsyncGenerator<TarEntry>;
|
|
61
|
+
export interface ExtractPackageOptions {
|
|
62
|
+
/** The prefix every entry must carry, stripped on the way out; npm packs under `package/`. */
|
|
63
|
+
readonly prefix: string;
|
|
64
|
+
/** The archive's name in errors. */
|
|
65
|
+
readonly label: string;
|
|
66
|
+
/** Stops the extraction; the signal's reason is thrown. */
|
|
67
|
+
readonly signal?: AbortSignal;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Unpack the gzipped tar at `archivePath` into `destDir` under the rules in
|
|
71
|
+
* the module header; resolves with the number of files written. `destDir`
|
|
72
|
+
* should be new and empty: a file that already exists there refuses the
|
|
73
|
+
* archive rather than be replaced. Each file keeps its archived permission
|
|
74
|
+
* bits, less set-id, sticky and group or other write.
|
|
75
|
+
*/
|
|
76
|
+
export declare function extractPackageTarball(archivePath: string, destDir: string, options: ExtractPackageOptions): Promise<{
|
|
77
|
+
readonly files: number;
|
|
78
|
+
}>;
|
|
79
|
+
//# sourceMappingURL=tar.d.ts.map
|
package/tar.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tar.d.ts","sourceRoot":"","sources":["src/tar.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AASH,sDAAsD;AACtD,eAAO,MAAM,SAAS,MAAM,CAAC;AAE7B,4CAA4C;AAC5C,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,WAAW,GAAG,SAAS,GAAG,UAAU,GAAG,KAAK,GAAG,OAAO,CAAC;AAE3F,8BAA8B;AAC9B,MAAM,WAAW,SAAS;IACxB,oFAAoF;IACpF,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,kEAAkE;IAClE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0EAA0E;IAC1E,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,mCAAmC;IACnC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,oEAAoE;IACpE,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;CACjC;AAED,4FAA4F;AAC5F,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,GAAG,IAAI,CAclE;AA8CD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAWhF;AAOD,yGAAyG;AACzG,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,8EAA8E;IAC9E,IAAI,IAAI,cAAc,CAAC,UAAU,CAAC,CAAC;CACpC;AAED;;;;;GAKG;AACH,wBAAuB,OAAO,CAAC,MAAM,EAAE,aAAa,CAAC,UAAU,CAAC,GAAG,cAAc,CAAC,QAAQ,CAAC,CA2B1F;AA0DD,MAAM,WAAW,qBAAqB;IACpC,8FAA8F;IAC9F,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,oCAAoC;IACpC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,2DAA2D;IAC3D,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC/B;AAED;;;;;;GAMG;AACH,wBAAsB,qBAAqB,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,CA8BrJ"}
|
package/tar.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One minimal POSIX/ustar reader, two uses.
|
|
3
|
+
*
|
|
4
|
+
* {@link parseTarHeader} reads a 512-byte header block: the name (with the
|
|
5
|
+
* ustar prefix), the octal mode and size, the type flag, and whether the
|
|
6
|
+
* header's checksum holds. Everything else here walks archives with it:
|
|
7
|
+
*
|
|
8
|
+
* - {@link extractTarEntry} returns one regular file by basename from an
|
|
9
|
+
* uncompressed archive in memory (a release's single binary, such as the
|
|
10
|
+
* Temporal CLI);
|
|
11
|
+
* - {@link readTar} walks an archive as it streams, entry by entry, and
|
|
12
|
+
* {@link extractPackageTarball} uses it to unpack a whole npm package from
|
|
13
|
+
* a `.tgz` on disk under strict rules: every entry a regular file under the
|
|
14
|
+
* prefix npm packs with (`package/`), no absolute path, no `.` or `..`
|
|
15
|
+
* segment, no duplicate, a valid header checksum. Any other entry refuses
|
|
16
|
+
* the archive, links and pax headers included, so a package can never
|
|
17
|
+
* write outside the directory it is unpacked into. npm packs regular files
|
|
18
|
+
* only, so a published package passes.
|
|
19
|
+
*
|
|
20
|
+
* No native `tar` and no dependency: the CLI's base install stays lean, and
|
|
21
|
+
* the runner bundles this into its single file.
|
|
22
|
+
*/
|
|
23
|
+
import { createReadStream } from "node:fs";
|
|
24
|
+
import { mkdir, open } from "node:fs/promises";
|
|
25
|
+
import { dirname, isAbsolute, join, sep } from "node:path";
|
|
26
|
+
import { pipeline } from "node:stream/promises";
|
|
27
|
+
import { createGunzip } from "node:zlib";
|
|
28
|
+
import { ArtifactFetchError } from "./errors.js";
|
|
29
|
+
/** A tar block: headers and data are padded to it. */
|
|
30
|
+
export const TAR_BLOCK = 512;
|
|
31
|
+
/** The header in `block`, or `null` for a zero block, which marks the end of an archive. */
|
|
32
|
+
export function parseTarHeader(block) {
|
|
33
|
+
if (block.every((byte) => byte === 0))
|
|
34
|
+
return null;
|
|
35
|
+
const name = readCString(block, 0, 100);
|
|
36
|
+
const magic = readCString(block, 257, 6);
|
|
37
|
+
const prefix = magic.startsWith("ustar") ? readCString(block, 345, 155) : "";
|
|
38
|
+
const typeFlag = String.fromCharCode(block[156] ?? 0);
|
|
39
|
+
return {
|
|
40
|
+
name: prefix === "" ? name : `${prefix}/${name}`,
|
|
41
|
+
mode: readOctal(block, 100, 8) || 0,
|
|
42
|
+
size: readOctal(block, 124, 12),
|
|
43
|
+
kind: kindOf(typeFlag),
|
|
44
|
+
typeFlag,
|
|
45
|
+
checksumValid: readOctal(block, 148, 8) === headerChecksum(block),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function kindOf(typeFlag) {
|
|
49
|
+
switch (typeFlag) {
|
|
50
|
+
case "0":
|
|
51
|
+
case "\0":
|
|
52
|
+
case "7":
|
|
53
|
+
return "file";
|
|
54
|
+
case "5":
|
|
55
|
+
return "directory";
|
|
56
|
+
case "2":
|
|
57
|
+
return "symlink";
|
|
58
|
+
case "1":
|
|
59
|
+
return "hardlink";
|
|
60
|
+
case "x":
|
|
61
|
+
case "g":
|
|
62
|
+
return "pax";
|
|
63
|
+
default:
|
|
64
|
+
return "other";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// The unsigned sum of the header's bytes with the checksum field read as spaces.
|
|
68
|
+
function headerChecksum(block) {
|
|
69
|
+
let sum = 0;
|
|
70
|
+
for (let i = 0; i < TAR_BLOCK; i++)
|
|
71
|
+
sum += i >= 148 && i < 156 ? 0x20 : (block[i] ?? 0);
|
|
72
|
+
return sum;
|
|
73
|
+
}
|
|
74
|
+
function readOctal(block, start, length) {
|
|
75
|
+
const text = readCString(block, start, length).trim();
|
|
76
|
+
if (text === "")
|
|
77
|
+
return 0;
|
|
78
|
+
return /^[0-7]+$/.test(text) ? Number.parseInt(text, 8) : Number.NaN;
|
|
79
|
+
}
|
|
80
|
+
function readCString(block, start, length) {
|
|
81
|
+
let end = start;
|
|
82
|
+
const limit = start + length;
|
|
83
|
+
while (end < limit && block[end] !== 0)
|
|
84
|
+
end += 1;
|
|
85
|
+
return Buffer.from(block.subarray(start, end)).toString("utf8");
|
|
86
|
+
}
|
|
87
|
+
function padded(size) {
|
|
88
|
+
return Math.ceil(size / TAR_BLOCK) * TAR_BLOCK;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Extract a single regular-file entry whose basename matches `name` from an
|
|
92
|
+
* (uncompressed) tar buffer, or `null` when there is none.
|
|
93
|
+
*/
|
|
94
|
+
export function extractTarEntry(buf, name) {
|
|
95
|
+
let offset = 0;
|
|
96
|
+
while (offset + TAR_BLOCK <= buf.length) {
|
|
97
|
+
const header = parseTarHeader(buf.subarray(offset, offset + TAR_BLOCK));
|
|
98
|
+
if (header === null)
|
|
99
|
+
break;
|
|
100
|
+
const size = Number.isNaN(header.size) ? 0 : header.size;
|
|
101
|
+
const dataStart = offset + TAR_BLOCK;
|
|
102
|
+
if (header.kind === "file" && basename(header.name) === name)
|
|
103
|
+
return buf.subarray(dataStart, dataStart + size);
|
|
104
|
+
offset = dataStart + padded(size);
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
function basename(path) {
|
|
109
|
+
const parts = path.split("/");
|
|
110
|
+
return parts[parts.length - 1] ?? path;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The entries of an uncompressed tar stream, in order. Each entry's data must
|
|
114
|
+
* be read (or left) before the next entry is requested. Ends at the archive's
|
|
115
|
+
* zero block, draining what follows; throws on a stream that ends inside a
|
|
116
|
+
* header or an entry's data, and on a size field that is not octal.
|
|
117
|
+
*/
|
|
118
|
+
export async function* readTar(source) {
|
|
119
|
+
const reader = new ByteReader(source);
|
|
120
|
+
for (;;) {
|
|
121
|
+
const block = await reader.readExact(TAR_BLOCK);
|
|
122
|
+
if (block === null)
|
|
123
|
+
throw new Error("the archive ends without its end-of-archive block");
|
|
124
|
+
const header = parseTarHeader(block);
|
|
125
|
+
if (header === null) {
|
|
126
|
+
await reader.drain();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (Number.isNaN(header.size))
|
|
130
|
+
throw new Error(`entry ${header.name} has a size field that is not octal`);
|
|
131
|
+
let remaining = header.size;
|
|
132
|
+
yield {
|
|
133
|
+
header,
|
|
134
|
+
async *data() {
|
|
135
|
+
while (remaining > 0) {
|
|
136
|
+
const chunk = await reader.readUpTo(remaining);
|
|
137
|
+
if (chunk === null)
|
|
138
|
+
throw new Error(`the archive ends inside ${header.name}`);
|
|
139
|
+
remaining -= chunk.byteLength;
|
|
140
|
+
yield chunk;
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
if (!(await reader.skip(remaining + padded(header.size) - header.size))) {
|
|
145
|
+
throw new Error(`the archive ends inside ${header.name}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// Exact and bounded reads over an async byte stream, holding at most one
|
|
150
|
+
// chunk beyond what was asked.
|
|
151
|
+
class ByteReader {
|
|
152
|
+
iterator;
|
|
153
|
+
pending = new Uint8Array(0);
|
|
154
|
+
constructor(source) {
|
|
155
|
+
this.iterator = source[Symbol.asyncIterator]();
|
|
156
|
+
}
|
|
157
|
+
/** Exactly `n` bytes, or `null` when the stream ends first. */
|
|
158
|
+
async readExact(n) {
|
|
159
|
+
const parts = [];
|
|
160
|
+
let have = 0;
|
|
161
|
+
while (have < n) {
|
|
162
|
+
const chunk = await this.readUpTo(n - have);
|
|
163
|
+
if (chunk === null)
|
|
164
|
+
return null;
|
|
165
|
+
parts.push(chunk);
|
|
166
|
+
have += chunk.byteLength;
|
|
167
|
+
}
|
|
168
|
+
return parts.length === 1 ? parts[0] : Buffer.concat(parts);
|
|
169
|
+
}
|
|
170
|
+
/** Between 1 and `n` bytes, or `null` at the end of the stream. */
|
|
171
|
+
async readUpTo(n) {
|
|
172
|
+
while (this.pending.byteLength === 0) {
|
|
173
|
+
const next = await this.iterator.next();
|
|
174
|
+
if (next.done === true)
|
|
175
|
+
return null;
|
|
176
|
+
this.pending = next.value;
|
|
177
|
+
}
|
|
178
|
+
const take = this.pending.subarray(0, Math.min(n, this.pending.byteLength));
|
|
179
|
+
this.pending = this.pending.subarray(take.byteLength);
|
|
180
|
+
return take;
|
|
181
|
+
}
|
|
182
|
+
/** Discards `n` bytes; false when the stream ends first. */
|
|
183
|
+
async skip(n) {
|
|
184
|
+
let left = n;
|
|
185
|
+
while (left > 0) {
|
|
186
|
+
const chunk = await this.readUpTo(left);
|
|
187
|
+
if (chunk === null)
|
|
188
|
+
return false;
|
|
189
|
+
left -= chunk.byteLength;
|
|
190
|
+
}
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
/** Reads the stream to its end. */
|
|
194
|
+
async drain() {
|
|
195
|
+
this.pending = new Uint8Array(0);
|
|
196
|
+
for (;;) {
|
|
197
|
+
const next = await this.iterator.next();
|
|
198
|
+
if (next.done === true)
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Unpack the gzipped tar at `archivePath` into `destDir` under the rules in
|
|
205
|
+
* the module header; resolves with the number of files written. `destDir`
|
|
206
|
+
* should be new and empty: a file that already exists there refuses the
|
|
207
|
+
* archive rather than be replaced. Each file keeps its archived permission
|
|
208
|
+
* bits, less set-id, sticky and group or other write.
|
|
209
|
+
*/
|
|
210
|
+
export async function extractPackageTarball(archivePath, destDir, options) {
|
|
211
|
+
let files = 0;
|
|
212
|
+
const refuse = (reason) => {
|
|
213
|
+
throw new ArtifactFetchError(`${options.label} archive refused: ${reason}`, [`archive: ${archivePath}`]);
|
|
214
|
+
};
|
|
215
|
+
await pipeline(createReadStream(archivePath), createGunzip(), async (source) => {
|
|
216
|
+
for await (const entry of readTar(source)) {
|
|
217
|
+
const { header } = entry;
|
|
218
|
+
if (!header.checksumValid)
|
|
219
|
+
refuse(`entry ${JSON.stringify(header.name)} has a corrupt header`);
|
|
220
|
+
if (header.kind !== "file")
|
|
221
|
+
refuse(`entry ${JSON.stringify(header.name)} is a ${header.kind}, and only regular files are accepted`);
|
|
222
|
+
const target = join(destDir, packagePath(header.name, options.prefix, refuse));
|
|
223
|
+
await mkdir(dirname(target), { recursive: true });
|
|
224
|
+
const file = await open(target, "wx", 0o600).catch((err) => refuse(`entry ${JSON.stringify(header.name)} could not be created (${err instanceof Error ? err.message : String(err)})`));
|
|
225
|
+
try {
|
|
226
|
+
for await (const chunk of entry.data())
|
|
227
|
+
await file.write(chunk);
|
|
228
|
+
await file.chmod(header.mode & 0o755);
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
await file.close();
|
|
232
|
+
}
|
|
233
|
+
files += 1;
|
|
234
|
+
}
|
|
235
|
+
}, options.signal === undefined ? {} : { signal: options.signal });
|
|
236
|
+
return { files };
|
|
237
|
+
}
|
|
238
|
+
// The path an entry writes to, relative to the destination, or a refusal.
|
|
239
|
+
function packagePath(name, prefix, refuse) {
|
|
240
|
+
const quoted = JSON.stringify(name);
|
|
241
|
+
if (!name.startsWith(prefix))
|
|
242
|
+
refuse(`entry ${quoted} is not under ${prefix}`);
|
|
243
|
+
const rel = name.slice(prefix.length);
|
|
244
|
+
if (rel === "" || isAbsolute(rel) || rel.includes("\\") || rel.includes("\0"))
|
|
245
|
+
refuse(`entry ${quoted} is not a plain relative path`);
|
|
246
|
+
// With no absolute start and no empty, . or .. segment, the path cannot leave the package.
|
|
247
|
+
if (rel.split("/").some((segment) => segment === "" || segment === "." || segment === ".."))
|
|
248
|
+
refuse(`entry ${quoted} has an empty, . or .. segment`);
|
|
249
|
+
return rel.split("/").join(sep);
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=tar.js.map
|
package/tar.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tar.js","sourceRoot":"","sources":["src/tar.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD,sDAAsD;AACtD,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,CAAC;AAoB7B,4FAA4F;AAC5F,MAAM,UAAU,cAAc,CAAC,KAAiB;IAC9C,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IACnD,MAAM,IAAI,GAAG,WAAW,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC;IACxC,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;IACzC,MAAM,MAAM,GAAG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AAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACpC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC;QAAE,MAAM,CAAC,SAAS,MAAM,iBAAiB,MAAM,EAAE,CAAC,CAAC;IAC/E,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACtC,IAAI,GAAG,KAAK,EAAE,IAAI,UAAU,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,MAAM,CAAC,SAAS,MAAM,+BAA+B,CAAC,CAAC;IACtI,2FAA2F;IAC3F,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,KAAK,EAAE,IAAI,OAAO,KAAK,GAAG,IAAI,OAAO,KAAK,IAAI,CAAC;QAAE,MAAM,CAAC,SAAS,MAAM,gCAAgC,CAAC,CAAC;IACrJ,OAAO,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClC,CAAC"}
|
package/testing.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte-level tar fixture builder for this library's consumers' tests
|
|
3
|
+
* (exported as `@stigmer/artifact-fetch/testing`). The lib's own suite and
|
|
4
|
+
* the runner's engine-fetch suite craft archives with it, so a fixture that
|
|
5
|
+
* breaks a rule (a link, a `..` entry, a corrupt header) is spelled one way.
|
|
6
|
+
*
|
|
7
|
+
* Emits real ustar headers with a valid checksum unless an entry asks
|
|
8
|
+
* otherwise, the zero blocks that end an archive, and, through
|
|
9
|
+
* {@link buildPackageTarball}, an npm-shaped `.tgz` whose entries sit under
|
|
10
|
+
* `package/`.
|
|
11
|
+
*/
|
|
12
|
+
export interface TarFixtureEntry {
|
|
13
|
+
/** The path as the archive spells it. Names over 100 bytes are split into the ustar prefix. */
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly content?: string | Uint8Array;
|
|
16
|
+
/** The type flag; `"0"` (a regular file) by default. */
|
|
17
|
+
readonly typeFlag?: string;
|
|
18
|
+
/** Permission bits; 0o644 by default. */
|
|
19
|
+
readonly mode?: number;
|
|
20
|
+
/** The link target of a symlink or hard link. */
|
|
21
|
+
readonly linkName?: string;
|
|
22
|
+
/** Write a checksum that does not match the header. */
|
|
23
|
+
readonly corruptChecksum?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** An uncompressed tar archive of `entries`, ended by two zero blocks. */
|
|
26
|
+
export declare function buildTar(entries: readonly TarFixtureEntry[]): Buffer;
|
|
27
|
+
/** An npm-shaped package tarball: every file under `package/`, gzipped. */
|
|
28
|
+
export declare function buildPackageTarball(files: Readonly<Record<string, string | Uint8Array>>, modes?: Readonly<Record<string, number>>): Buffer;
|
|
29
|
+
/** The Subresource Integrity string npm records for `bytes`. */
|
|
30
|
+
export declare function sriSha512(bytes: Uint8Array): string;
|
|
31
|
+
//# sourceMappingURL=testing.d.ts.map
|
package/testing.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["src/testing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAKH,MAAM,WAAW,eAAe;IAC9B,+FAA+F;IAC/F,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,UAAU,CAAC;IACvC,wDAAwD;IACxD,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,yCAAyC;IACzC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,iDAAiD;IACjD,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,uDAAuD;IACvD,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;CACpC;AAID,0EAA0E;AAC1E,wBAAgB,QAAQ,CAAC,OAAO,EAAE,SAAS,eAAe,EAAE,GAAG,MAAM,CAQpE;AAiCD,2EAA2E;AAC3E,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CAAC,EAAE,KAAK,GAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM,GAAG,MAAM,CAE9I;AAED,gEAAgE;AAChE,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAEnD"}
|
package/testing.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte-level tar fixture builder for this library's consumers' tests
|
|
3
|
+
* (exported as `@stigmer/artifact-fetch/testing`). The lib's own suite and
|
|
4
|
+
* the runner's engine-fetch suite craft archives with it, so a fixture that
|
|
5
|
+
* breaks a rule (a link, a `..` entry, a corrupt header) is spelled one way.
|
|
6
|
+
*
|
|
7
|
+
* Emits real ustar headers with a valid checksum unless an entry asks
|
|
8
|
+
* otherwise, the zero blocks that end an archive, and, through
|
|
9
|
+
* {@link buildPackageTarball}, an npm-shaped `.tgz` whose entries sit under
|
|
10
|
+
* `package/`.
|
|
11
|
+
*/
|
|
12
|
+
import { createHash } from "node:crypto";
|
|
13
|
+
import { gzipSync } from "node:zlib";
|
|
14
|
+
const BLOCK = 512;
|
|
15
|
+
/** An uncompressed tar archive of `entries`, ended by two zero blocks. */
|
|
16
|
+
export function buildTar(entries) {
|
|
17
|
+
const parts = [];
|
|
18
|
+
for (const entry of entries) {
|
|
19
|
+
const content = typeof entry.content === "string" ? Buffer.from(entry.content) : Buffer.from(entry.content ?? new Uint8Array(0));
|
|
20
|
+
parts.push(header(entry, content.length), content, Buffer.alloc((BLOCK - (content.length % BLOCK)) % BLOCK));
|
|
21
|
+
}
|
|
22
|
+
parts.push(Buffer.alloc(BLOCK * 2));
|
|
23
|
+
return Buffer.concat(parts);
|
|
24
|
+
}
|
|
25
|
+
function header(entry, size) {
|
|
26
|
+
const block = Buffer.alloc(BLOCK);
|
|
27
|
+
let name = entry.name;
|
|
28
|
+
let prefix = "";
|
|
29
|
+
if (Buffer.byteLength(name) > 100) {
|
|
30
|
+
const cut = name.lastIndexOf("/", name.length - 1);
|
|
31
|
+
prefix = name.slice(0, cut);
|
|
32
|
+
name = name.slice(cut + 1);
|
|
33
|
+
}
|
|
34
|
+
block.write(name, 0, "utf8");
|
|
35
|
+
block.write(octal(entry.mode ?? 0o644, 8), 100, "ascii");
|
|
36
|
+
block.write(octal(0, 8), 108, "ascii");
|
|
37
|
+
block.write(octal(0, 8), 116, "ascii");
|
|
38
|
+
block.write(octal(size, 12), 124, "ascii");
|
|
39
|
+
block.write(octal(0, 12), 136, "ascii");
|
|
40
|
+
block.write(entry.typeFlag ?? "0", 156, "ascii");
|
|
41
|
+
if (entry.linkName !== undefined)
|
|
42
|
+
block.write(entry.linkName, 157, "utf8");
|
|
43
|
+
block.write("ustar\0", 257, "ascii");
|
|
44
|
+
block.write("00", 263, "ascii");
|
|
45
|
+
if (prefix !== "")
|
|
46
|
+
block.write(prefix, 345, "utf8");
|
|
47
|
+
block.fill(0x20, 148, 156);
|
|
48
|
+
let sum = 0;
|
|
49
|
+
for (const byte of block)
|
|
50
|
+
sum += byte;
|
|
51
|
+
block.write(octal(entry.corruptChecksum === true ? sum + 1 : sum, 7) + "\0", 148, "ascii");
|
|
52
|
+
return block;
|
|
53
|
+
}
|
|
54
|
+
function octal(value, width) {
|
|
55
|
+
return value.toString(8).padStart(width - 1, "0") + "\0";
|
|
56
|
+
}
|
|
57
|
+
/** An npm-shaped package tarball: every file under `package/`, gzipped. */
|
|
58
|
+
export function buildPackageTarball(files, modes = {}) {
|
|
59
|
+
return gzipSync(buildTar(Object.entries(files).map(([path, content]) => ({ name: `package/${path}`, content, mode: modes[path] ?? 0o644 }))));
|
|
60
|
+
}
|
|
61
|
+
/** The Subresource Integrity string npm records for `bytes`. */
|
|
62
|
+
export function sriSha512(bytes) {
|
|
63
|
+
return `sha512-${createHash("sha512").update(bytes).digest("base64")}`;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=testing.js.map
|
package/testing.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.js","sourceRoot":"","sources":["src/testing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAgBrC,MAAM,KAAK,GAAG,GAAG,CAAC;AAElB,0EAA0E;AAC1E,MAAM,UAAU,QAAQ,CAAC,OAAmC;IAC1D,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC5B,MAAM,OAAO,GAAG,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,IAAI,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;QACjI,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC;IAC/G,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;IACpC,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC;AAED,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