@jini-ai/platform 0.7.2 → 0.7.3-next.2
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/CHANGELOG.md +2 -2
- package/README.md +16 -4
- package/dist/asset-cache.d.ts +13 -3
- package/dist/asset-cache.js +163 -154
- package/dist/blob-storage.d.ts +3 -32
- package/dist/blob-storage.js +1 -9
- package/dist/child-environment.d.ts +0 -16
- package/dist/child-environment.js +0 -25
- package/dist/command.d.ts +6 -11
- package/dist/command.js +8 -54
- package/dist/download-lock.d.ts +4 -0
- package/dist/download-lock.js +82 -0
- package/dist/download-maintenance.d.ts +5 -0
- package/dist/download-maintenance.js +82 -0
- package/dist/download-state.d.ts +8 -0
- package/dist/download-state.js +126 -0
- package/dist/download-store.d.ts +22 -0
- package/dist/download-store.js +174 -0
- package/dist/download-target.d.ts +10 -0
- package/dist/download-target.js +102 -0
- package/dist/download-transfer.d.ts +12 -0
- package/dist/download-transfer.js +174 -0
- package/dist/download-types.d.ts +162 -0
- package/dist/download-types.js +31 -0
- package/dist/download.d.ts +1 -89
- package/dist/download.js +8 -738
- package/dist/fetch-with-timeout.d.ts +7 -32
- package/dist/fetch-with-timeout.js +1 -77
- package/dist/fs/app-data-root.d.ts +28 -0
- package/dist/fs/app-data-root.js +35 -0
- package/dist/fs/atomic-write.js +1 -1
- package/dist/fs/boot-identity.d.ts +1 -4
- package/dist/fs/directory-delete-guard.d.ts +93 -0
- package/dist/fs/directory-delete-guard.js +148 -0
- package/dist/fs/directory-registry.js +45 -35
- package/dist/fs/directory-relocation.d.ts +82 -0
- package/dist/fs/directory-relocation.js +99 -0
- package/dist/fs/directory-running-lock.d.ts +1 -1
- package/dist/fs/directory-running-lock.js +24 -21
- package/dist/fs/directory-sync.js +1 -1
- package/dist/fs/directory-user-config.js +30 -3
- package/dist/fs/directory-workspace.d.ts +34 -30
- package/dist/fs/directory-workspace.js +70 -29
- package/dist/fs/durable-json.d.ts +6 -15
- package/dist/fs/durable-json.js +38 -35
- package/dist/fs/errno.d.ts +12 -3
- package/dist/fs/errno.js +5 -3
- package/dist/fs/file-lock.d.ts +3 -87
- package/dist/fs/file-lock.js +16 -30
- package/dist/fs/guarded-reader.js +335 -315
- package/dist/fs/index.d.ts +5 -0
- package/dist/fs/index.js +5 -0
- package/dist/fs/recent-directories.d.ts +46 -0
- package/dist/fs/recent-directories.js +68 -0
- package/dist/fs/remove-directory-tree.d.ts +20 -0
- package/dist/fs/remove-directory-tree.js +28 -0
- package/dist/fs/user-data-directories.js +2 -2
- package/dist/fs/workspace-directory.js +2 -1
- package/dist/fs.d.ts +16 -4
- package/dist/fs.js +4 -4
- package/dist/home-expansion.d.ts +7 -2
- package/dist/home-expansion.js +3 -3
- package/dist/http/guarded/client.d.ts +1 -2
- package/dist/http/guarded/client.js +2 -1
- package/dist/http/guarded/index.d.ts +0 -3
- package/dist/http/guarded/node-ports.d.ts +1 -1
- package/dist/http/guarded/transport.fetch.d.ts +2 -3
- package/dist/http/guarded/transport.fetch.js +64 -58
- package/dist/http.d.ts +3 -1
- package/dist/http.js +2 -2
- package/dist/index.d.ts +10 -11
- package/dist/index.js +7 -6
- package/dist/mail/activity/activity.d.ts +136 -0
- package/dist/mail/activity/activity.js +172 -0
- package/dist/mail/activity/ports.d.ts +113 -0
- package/dist/mail/activity/suppressions.d.ts +82 -0
- package/dist/mail/activity/suppressions.js +119 -0
- package/dist/mail/activity/vocabulary.d.ts +10 -0
- package/dist/mail/activity/vocabulary.js +8 -0
- package/dist/mail/builtin-providers.d.ts +12 -6
- package/dist/mail/builtin-providers.js +14 -11
- package/dist/mail/delivery-wrapper.js +2 -1
- package/dist/mail/email-sender-registry.d.ts +1 -1
- package/dist/mail/email-sender-registry.js +1 -1
- package/dist/mail/email-template-operations.d.ts +186 -0
- package/dist/mail/email-template-operations.js +226 -0
- package/dist/mail/email-template.d.ts +120 -0
- package/dist/mail/email-template.js +243 -0
- package/dist/mail/index.d.ts +4 -0
- package/dist/mail/index.js +3 -0
- package/dist/mail/prepare-sender-email.d.ts +1 -10
- package/dist/mail/prepare-sender-email.js +1 -11
- package/dist/mail/resolve-mailer.d.ts +2 -1
- package/dist/mail/resolve-mailer.js +5 -2
- package/dist/mail/senders.d.ts +0 -1
- package/dist/mail/senders.js +0 -1
- package/dist/mail/smtp.d.ts +1 -31
- package/dist/net/address.d.ts +1 -8
- package/dist/net/address.js +1 -18
- package/dist/net/index.d.ts +1 -1
- package/dist/net/index.js +1 -1
- package/dist/net/local-http-probe.js +1 -1
- package/dist/net/local-http-request.js +1 -1
- package/dist/process-probes.d.ts +36 -0
- package/dist/process-probes.js +151 -0
- package/dist/process-top-footprint.d.ts +7 -0
- package/dist/process-top-footprint.js +30 -0
- package/dist/process.d.ts +29 -31
- package/dist/process.js +18 -196
- package/dist/proxy-env.d.ts +12 -3
- package/dist/proxy-env.js +9 -8
- package/dist/resource-paths.d.ts +15 -3
- package/dist/resource-paths.js +7 -5
- package/dist/sandbox-env.d.ts +26 -5
- package/dist/sandbox-env.js +7 -7
- package/dist/secrets/credential-sets/contracts.d.ts +0 -1
- package/dist/secrets/credential-sets/contracts.js +0 -1
- package/dist/secrets/credential-sets/index.d.ts +2 -3
- package/dist/secrets/credential-sets/index.js +1 -1
- package/dist/secrets/credential-sets/provider-matches.d.ts +1 -1
- package/dist/secrets/credential-sets/provider-matches.js +1 -1
- package/dist/secrets/credential-sets/vault-archive.d.ts +1 -1
- package/dist/secrets/credential-sets/vault-archive.js +14 -8
- package/dist/secrets/credential-sets/vault.aad.d.ts +1 -1
- package/dist/secrets/credential-sets/vault.meta.d.ts +1 -1
- package/dist/secrets/credential-sets/vault.meta.js +1 -1
- package/dist/secrets/credential-sets/vault.repo.memory.d.ts +1 -1
- package/dist/secrets/credential-sets/vault.repo.memory.js +1 -1
- package/dist/secrets/credential-sets/vault.reseal-refused.d.ts +23 -0
- package/dist/secrets/credential-sets/vault.reseal-refused.js +36 -0
- package/dist/secrets/credential-sets/vault.service.d.ts +25 -5
- package/dist/secrets/credential-sets/vault.service.js +42 -8
- package/dist/secrets/index.d.ts +0 -3
- package/dist/secrets/index.js +5 -0
- package/dist/secrets/keyring.env.d.ts +1 -1
- package/dist/secrets/legacy-owner.d.ts +61 -0
- package/dist/secrets/legacy-owner.js +196 -0
- package/dist/secrets/root-key/decide.d.ts +89 -0
- package/dist/secrets/root-key/decide.js +60 -0
- package/dist/secrets/sealer-contract.d.ts +19 -0
- package/dist/secrets/sealer-contract.js +37 -0
- package/dist/secrets/secret-sealer.aesgcm.d.ts +1 -1
- package/dist/secrets/testing.d.ts +1 -0
- package/dist/secrets/testing.js +1 -0
- package/dist/shell.d.ts +4 -1
- package/dist/shell.js +1 -1
- package/dist/system.d.ts +6 -0
- package/dist/system.js +9 -0
- package/dist/terminal.d.ts +11 -96
- package/dist/terminal.js +80 -56
- package/dist/web/agent-tools.d.ts +107 -0
- package/dist/web/agent-tools.js +49 -0
- package/dist/web/index.d.ts +1 -0
- package/dist/web/index.js +1 -0
- package/package.json +39 -13
- package/dist/http/guarded/ports.d.ts +0 -56
- package/dist/http/guarded/types.d.ts +0 -8
- package/dist/http/guarded/types.js +0 -2
- package/dist/mail/email-sender.d.ts +0 -109
- package/dist/mail/email-sender.js +0 -10
- package/dist/net/endpoint-policy.d.ts +0 -39
- package/dist/net/endpoint-policy.js +0 -218
- package/dist/secrets/credential-sets/aad.d.ts +0 -7
- package/dist/secrets/credential-sets/aad.js +0 -7
- package/dist/secrets/credential-sets/repo.memory.d.ts +0 -44
- package/dist/secrets/credential-sets/repo.memory.js +0 -80
- package/dist/secrets/credential-sets/vault.types.d.ts +0 -27
- package/dist/secrets/credential-sets/vault.types.js +0 -49
- /package/dist/{http/guarded → mail/activity}/ports.js +0 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isAbsolute, resolve } from "node:path";
|
|
3
|
+
import { pathContains } from "./fs.js";
|
|
4
|
+
import { ManagedDownloadError, MANAGED_DOWNLOAD_ERROR_CODES, STATE_DIR, PARTIAL_DIR, LOCK_DIR } from './download-types.js';
|
|
5
|
+
import { errorMessage } from './download-store.js';
|
|
6
|
+
export const activeTargets = new Map();
|
|
7
|
+
export function digest(value) {
|
|
8
|
+
return createHash("sha256").update(value).digest("hex");
|
|
9
|
+
}
|
|
10
|
+
function normalizeChecksum(input) {
|
|
11
|
+
if (input.algorithm !== "sha256" && input.algorithm !== "sha512") {
|
|
12
|
+
throw new ManagedDownloadError(MANAGED_DOWNLOAD_ERROR_CODES.INVALID_TARGET, `unsupported checksum algorithm: ${String(input.algorithm)}`);
|
|
13
|
+
}
|
|
14
|
+
const value = input.value.trim().toLowerCase();
|
|
15
|
+
const expectedLength = input.algorithm === "sha256" ? 64 : 128;
|
|
16
|
+
if (!new RegExp(`^[0-9a-f]{${expectedLength}}$`).test(value)) {
|
|
17
|
+
throw new ManagedDownloadError(MANAGED_DOWNLOAD_ERROR_CODES.INVALID_TARGET, `${input.algorithm} checksum must be ${expectedLength} hex characters`);
|
|
18
|
+
}
|
|
19
|
+
return { algorithm: input.algorithm, value };
|
|
20
|
+
}
|
|
21
|
+
const DOT_SEGMENTS = new Set([".", ".."]);
|
|
22
|
+
function normalizeSegment(value, label) {
|
|
23
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
24
|
+
throw new ManagedDownloadError(MANAGED_DOWNLOAD_ERROR_CODES.INVALID_TARGET, `${label} must be a non-empty string`);
|
|
25
|
+
}
|
|
26
|
+
// `:` blocks a Windows drive-relative segment (e.g. "C:foo") — win32's
|
|
27
|
+
// `isAbsolute`/`[\\/]` checks above don't reject it, but `path.win32.resolve`
|
|
28
|
+
// treats it as a real drive reference, which can escape `basePath` onto a
|
|
29
|
+
// different drive entirely.
|
|
30
|
+
if (DOT_SEGMENTS.has(value) || value.includes("\0") || /[:\\/]/.test(value) || isAbsolute(value)) {
|
|
31
|
+
throw new ManagedDownloadError(MANAGED_DOWNLOAD_ERROR_CODES.INVALID_TARGET, `${label} must be a safe single path segment`);
|
|
32
|
+
}
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
function normalizeUrl(value) {
|
|
36
|
+
try {
|
|
37
|
+
const url = new URL(value);
|
|
38
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
39
|
+
throw new Error("only http and https URLs are supported");
|
|
40
|
+
}
|
|
41
|
+
return url.toString();
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
throw new ManagedDownloadError(MANAGED_DOWNLOAD_ERROR_CODES.INVALID_TARGET, errorMessage(error));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export function normalizeBasePath(basePath) {
|
|
48
|
+
if (typeof basePath !== "string" || basePath.length === 0 || basePath.includes("\0")) {
|
|
49
|
+
throw new ManagedDownloadError(MANAGED_DOWNLOAD_ERROR_CODES.INVALID_TARGET, "basePath must be a non-empty path");
|
|
50
|
+
}
|
|
51
|
+
// `path.resolve()` always returns an absolute path by contract (resolving
|
|
52
|
+
// against `process.cwd()` when given a relative input), so this can never
|
|
53
|
+
// be false for any string input.
|
|
54
|
+
return resolve(basePath);
|
|
55
|
+
}
|
|
56
|
+
export function targetFromOptions(options) {
|
|
57
|
+
const basePath = normalizeBasePath(options.basePath);
|
|
58
|
+
const bucket = normalizeSegment(options.bucket, "bucket");
|
|
59
|
+
const fileName = normalizeSegment(options.fileName, "fileName");
|
|
60
|
+
const checksum = normalizeChecksum(options.payload.checksum);
|
|
61
|
+
const url = normalizeUrl(options.payload.url);
|
|
62
|
+
const urlDigest = digest(url);
|
|
63
|
+
const identityDigest = digest(`${url}\0${checksum.algorithm}\0${checksum.value}`);
|
|
64
|
+
const targetKey = digest(`${bucket}\0${fileName}`);
|
|
65
|
+
const finalPath = resolve(basePath, bucket, fileName);
|
|
66
|
+
const manifestPath = resolve(basePath, STATE_DIR, `${targetKey}.json`);
|
|
67
|
+
const partialPath = resolve(basePath, PARTIAL_DIR, `${targetKey}.partial`);
|
|
68
|
+
const lockPath = resolve(basePath, LOCK_DIR, `${targetKey}.lock`);
|
|
69
|
+
// Real, reachable guard — NOT dead code: `normalizeSegment` only rejects a
|
|
70
|
+
// segment that is *exactly* "." or "..", not one that merely starts with
|
|
71
|
+
// ".." (e.g. "..evil" is a syntactically ordinary filename, not a parent
|
|
72
|
+
// reference), so `pathContains`'s own naive `!rel.startsWith("..")` string
|
|
73
|
+
// check (see fs.ts) flags such a segment as escaping even though
|
|
74
|
+
// `path.resolve` never actually walked upward. Empirically verified this
|
|
75
|
+
// session: a 300k-segment fuzz found ~0.2% of `normalizeSegment`-valid
|
|
76
|
+
// segments (every one starting with literal "..") trip this exact guard —
|
|
77
|
+
// see archived provenance ledger's 2026-07-22 entry and this file's own test for a
|
|
78
|
+
// concrete example (bucket "..evil").
|
|
79
|
+
for (const path of [finalPath, manifestPath, partialPath, lockPath]) {
|
|
80
|
+
if (!pathContains({ root: basePath, target: path })) {
|
|
81
|
+
throw new ManagedDownloadError(MANAGED_DOWNLOAD_ERROR_CODES.INVALID_TARGET, "resolved managed download path escaped basePath");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return { basePath, bucket, checksum, fileName, finalPath, identityDigest, lockPath, manifestPath, partialPath, targetKey, url, urlDigest };
|
|
85
|
+
}
|
|
86
|
+
export function activeKey(target) {
|
|
87
|
+
return `${target.basePath}\0${target.targetKey}\0${target.identityDigest}`;
|
|
88
|
+
}
|
|
89
|
+
export function targetActiveKey(target) {
|
|
90
|
+
return `${target.basePath}\0${target.targetKey}`;
|
|
91
|
+
}
|
|
92
|
+
/** Resolve a target's paths for callers (inspect/remove) that have no payload; the placeholder never reaches the network. */
|
|
93
|
+
export function targetWithoutPayload(options) {
|
|
94
|
+
return targetFromOptions({
|
|
95
|
+
...options,
|
|
96
|
+
payload: {
|
|
97
|
+
checksum: { algorithm: "sha256", value: "0".repeat(64) },
|
|
98
|
+
url: "https://example.invalid/",
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=download-target.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ManagedDownloadProgress, type DownloadManifest, type NormalizedTarget, type DownloadAttemptResult } from './download-types.js';
|
|
2
|
+
export declare function downloadWithRetries({ target, manifest, options }: {
|
|
3
|
+
target: NormalizedTarget;
|
|
4
|
+
manifest: DownloadManifest | null;
|
|
5
|
+
options: {
|
|
6
|
+
emit: (progress: ManagedDownloadProgress) => void;
|
|
7
|
+
fetchImpl: typeof globalThis.fetch;
|
|
8
|
+
maxAttempts: number;
|
|
9
|
+
requestHeaders?: Record<string, string> | undefined;
|
|
10
|
+
};
|
|
11
|
+
}, _optional?: {}): Promise<DownloadAttemptResult>;
|
|
12
|
+
//# sourceMappingURL=download-transfer.d.ts.map
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { createWriteStream } from "node:fs";
|
|
2
|
+
import { rm } from "node:fs/promises";
|
|
3
|
+
import { Readable, Transform } from "node:stream";
|
|
4
|
+
import { pipeline } from "node:stream/promises";
|
|
5
|
+
import { ManagedDownloadError, MANAGED_DOWNLOAD_ERROR_CODES } from './download-types.js';
|
|
6
|
+
import { statFileSize, writeJson, createManifest, errorMessage } from './download-store.js';
|
|
7
|
+
async function emitExistingProgress(path, totalBytes, emit) {
|
|
8
|
+
// `totalBytes` is required (not `number | undefined`): its only real call
|
|
9
|
+
// site (`tryResumeDownload`) always resolves one, via a `??` chain (in
|
|
10
|
+
// `acceptedResumeTotalBytes`) whose
|
|
11
|
+
// last resort is arithmetic (`partialBytes + (contentLength(response) ??
|
|
12
|
+
// 0)`) — never `undefined`.
|
|
13
|
+
const existing = await statFileSize(path);
|
|
14
|
+
if (existing == null || existing <= 0)
|
|
15
|
+
return;
|
|
16
|
+
emit({ receivedBytes: existing, sessionReceivedBytes: 0, totalBytes });
|
|
17
|
+
}
|
|
18
|
+
function contentLength(response) {
|
|
19
|
+
const raw = response.headers.get("content-length");
|
|
20
|
+
if (raw == null)
|
|
21
|
+
return undefined;
|
|
22
|
+
const parsed = Number(raw);
|
|
23
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
|
|
24
|
+
}
|
|
25
|
+
function validatorsFromResponse(response) {
|
|
26
|
+
const etag = response.headers.get("etag") ?? undefined;
|
|
27
|
+
const lastModified = response.headers.get("last-modified") ?? undefined;
|
|
28
|
+
return etag == null && lastModified == null ? undefined : { ...(etag == null ? {} : { etag }), ...(lastModified == null ? {} : { lastModified }) };
|
|
29
|
+
}
|
|
30
|
+
function validatorsConflict(saved, response) {
|
|
31
|
+
if (saved == null)
|
|
32
|
+
return false;
|
|
33
|
+
const etag = response.headers.get("etag") ?? undefined;
|
|
34
|
+
const lastModified = response.headers.get("last-modified") ?? undefined;
|
|
35
|
+
if (saved.etag != null && etag != null && saved.etag !== etag)
|
|
36
|
+
return true;
|
|
37
|
+
if (saved.lastModified != null && lastModified != null && saved.lastModified !== lastModified)
|
|
38
|
+
return true;
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
function parseContentRange(value) {
|
|
42
|
+
if (value == null)
|
|
43
|
+
return null;
|
|
44
|
+
const match = /^bytes\s+(\d+)-(\d+)\/(\d+|\*)$/i.exec(value.trim());
|
|
45
|
+
// Every group in the pattern is mandatory, so a match always carries all three.
|
|
46
|
+
if (match == null)
|
|
47
|
+
return null;
|
|
48
|
+
// `\d+` always parses to an integer — except a digit run long enough to
|
|
49
|
+
// overflow to Infinity, which `Number.isInteger` rejects.
|
|
50
|
+
const start = Number(match[1]);
|
|
51
|
+
const end = Number(match[2]);
|
|
52
|
+
if (!Number.isInteger(start) || !Number.isInteger(end) || end < start)
|
|
53
|
+
return null;
|
|
54
|
+
if (match[3] === "*")
|
|
55
|
+
return { start, end };
|
|
56
|
+
const totalBytes = Number(match[3]);
|
|
57
|
+
if (!Number.isInteger(totalBytes) || totalBytes <= end)
|
|
58
|
+
return null;
|
|
59
|
+
return { start, end, totalBytes };
|
|
60
|
+
}
|
|
61
|
+
async function writeResponseBodyToPartial(response, target, options) {
|
|
62
|
+
if (response.body == null)
|
|
63
|
+
throw new Error("download response did not include a body");
|
|
64
|
+
let receivedBytes = options.startBytes;
|
|
65
|
+
let sessionReceivedBytes = 0;
|
|
66
|
+
const meter = new Transform({
|
|
67
|
+
transform(chunk, _encoding, callback) {
|
|
68
|
+
receivedBytes += chunk.byteLength;
|
|
69
|
+
sessionReceivedBytes += chunk.byteLength;
|
|
70
|
+
options.emit({
|
|
71
|
+
receivedBytes,
|
|
72
|
+
sessionReceivedBytes,
|
|
73
|
+
...(options.totalBytes == null ? {} : { totalBytes: options.totalBytes }),
|
|
74
|
+
});
|
|
75
|
+
callback(null, chunk);
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
await pipeline(Readable.fromWeb(response.body), meter, createWriteStream(target.partialPath, { flags: options.startBytes > 0 ? "a" : "w" }));
|
|
79
|
+
}
|
|
80
|
+
function resumeRequestHeaders(input, options) {
|
|
81
|
+
const etag = input.manifest.validators?.etag;
|
|
82
|
+
return {
|
|
83
|
+
...(options.requestHeaders ?? {}),
|
|
84
|
+
...(etag == null ? {} : { "If-Range": etag }),
|
|
85
|
+
Range: `bytes=${input.partialBytes}-`,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** The resumed total size, or `null` when the response cannot continue the saved partial. */
|
|
89
|
+
function acceptedResumeTotalBytes(input) {
|
|
90
|
+
const { manifest, partialBytes, response } = input;
|
|
91
|
+
if (response.status !== 206)
|
|
92
|
+
return null;
|
|
93
|
+
const range = parseContentRange(response.headers.get("content-range"));
|
|
94
|
+
if (range == null || range.start !== partialBytes || validatorsConflict(manifest.validators, response)) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
return range.totalBytes ?? manifest.totalBytes ?? partialBytes + (contentLength(response) ?? 0);
|
|
98
|
+
}
|
|
99
|
+
async function tryResumeDownload(input, options) {
|
|
100
|
+
const { emit, fetchImpl, manifest, target } = input;
|
|
101
|
+
const partialBytes = await statFileSize(target.partialPath);
|
|
102
|
+
if (partialBytes == null || partialBytes <= 0)
|
|
103
|
+
return "restart";
|
|
104
|
+
const response = await fetchImpl(target.url, {
|
|
105
|
+
headers: resumeRequestHeaders({ manifest, partialBytes }, options),
|
|
106
|
+
});
|
|
107
|
+
const totalBytes = acceptedResumeTotalBytes({ manifest, partialBytes, response });
|
|
108
|
+
if (totalBytes == null)
|
|
109
|
+
return "restart";
|
|
110
|
+
await emitExistingProgress(target.partialPath, totalBytes, emit);
|
|
111
|
+
await writeJson(target.manifestPath, {
|
|
112
|
+
...manifest,
|
|
113
|
+
state: "partial",
|
|
114
|
+
totalBytes,
|
|
115
|
+
updatedAt: new Date().toISOString(),
|
|
116
|
+
validators: manifest.validators ?? validatorsFromResponse(response),
|
|
117
|
+
});
|
|
118
|
+
await writeResponseBodyToPartial(response, target, { emit, startBytes: partialBytes, totalBytes });
|
|
119
|
+
return { resumed: true, totalBytes };
|
|
120
|
+
}
|
|
121
|
+
async function downloadFromZero(input, options) {
|
|
122
|
+
const { emit, fetchImpl, target } = input;
|
|
123
|
+
const { requestHeaders } = options;
|
|
124
|
+
await rm(target.partialPath, { force: true }).catch(() => undefined);
|
|
125
|
+
const response = await fetchImpl(target.url, {
|
|
126
|
+
...(requestHeaders == null ? {} : { headers: requestHeaders }),
|
|
127
|
+
});
|
|
128
|
+
if (!response.ok)
|
|
129
|
+
throw new Error(`download request returned HTTP ${response.status}`);
|
|
130
|
+
const totalBytes = contentLength(response);
|
|
131
|
+
const validators = validatorsFromResponse(response);
|
|
132
|
+
await writeJson(target.manifestPath, createManifest({ target, state: "partial", patch: { totalBytes, validators } }));
|
|
133
|
+
await writeResponseBodyToPartial(response, target, { emit, startBytes: 0, totalBytes });
|
|
134
|
+
return { resumed: false, totalBytes };
|
|
135
|
+
}
|
|
136
|
+
export async function downloadWithRetries({ target, manifest, options }, _optional = {}) {
|
|
137
|
+
let lastError;
|
|
138
|
+
let nextManifest = manifest;
|
|
139
|
+
const deps = { emit: options.emit, fetchImpl: options.fetchImpl };
|
|
140
|
+
for (let attempt = 1; attempt <= options.maxAttempts; attempt += 1) {
|
|
141
|
+
try {
|
|
142
|
+
const resume = nextManifest?.state === "partial"
|
|
143
|
+
? await tryResumeDownload({ ...deps, manifest: nextManifest, target }, options)
|
|
144
|
+
: null;
|
|
145
|
+
if (resume !== null && resume !== "restart")
|
|
146
|
+
return resume;
|
|
147
|
+
if (resume === "restart") {
|
|
148
|
+
await rm(target.partialPath, { force: true }).catch(() => undefined);
|
|
149
|
+
nextManifest = null;
|
|
150
|
+
}
|
|
151
|
+
return await downloadFromZero({ ...deps, target }, options);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
lastError = error;
|
|
155
|
+
nextManifest = await manifestAfterFailedAttempt({ manifest: nextManifest, target });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
throw new ManagedDownloadError(MANAGED_DOWNLOAD_ERROR_CODES.NETWORK_EXHAUSTED, `download failed after ${options.maxAttempts} attempts: ${errorMessage(lastError)}`);
|
|
159
|
+
}
|
|
160
|
+
/** After a failed attempt, mark any bytes already on disk as a resumable partial (best-effort persisted). */
|
|
161
|
+
async function manifestAfterFailedAttempt(input) {
|
|
162
|
+
const { manifest, target } = input;
|
|
163
|
+
const partialBytes = await statFileSize(target.partialPath);
|
|
164
|
+
if (partialBytes == null || partialBytes <= 0)
|
|
165
|
+
return manifest;
|
|
166
|
+
const next = {
|
|
167
|
+
...(manifest ?? createManifest({ target, state: "partial" })),
|
|
168
|
+
state: "partial",
|
|
169
|
+
updatedAt: new Date().toISOString(),
|
|
170
|
+
};
|
|
171
|
+
await writeJson(target.manifestPath, next).catch(() => undefined);
|
|
172
|
+
return next;
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=download-transfer.js.map
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
export declare const STORE_SENTINEL = ".jini-download-root.json";
|
|
2
|
+
export declare const STATE_DIR = ".state";
|
|
3
|
+
export declare const PARTIAL_DIR = ".partial";
|
|
4
|
+
export declare const LOCK_DIR = ".locks";
|
|
5
|
+
export declare const STORE_SCHEMA_VERSION = 1;
|
|
6
|
+
export declare const MANIFEST_SCHEMA_VERSION = 1;
|
|
7
|
+
export declare const STORE_KIND = "jini-managed-download-root";
|
|
8
|
+
export declare const MANIFEST_KIND = "jini-managed-download";
|
|
9
|
+
export type ManagedDownloadChecksum = {
|
|
10
|
+
algorithm: "sha256" | "sha512";
|
|
11
|
+
value: string;
|
|
12
|
+
};
|
|
13
|
+
export type ManagedDownloadPayload = {
|
|
14
|
+
checksum: ManagedDownloadChecksum;
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
url: string;
|
|
17
|
+
};
|
|
18
|
+
export type ManagedDownloadProgress = {
|
|
19
|
+
receivedBytes: number;
|
|
20
|
+
sessionReceivedBytes: number;
|
|
21
|
+
totalBytes?: number;
|
|
22
|
+
};
|
|
23
|
+
export type ManagedDownloadOptions = {
|
|
24
|
+
basePath: string;
|
|
25
|
+
bucket: string;
|
|
26
|
+
fileName: string;
|
|
27
|
+
fetch?: typeof globalThis.fetch;
|
|
28
|
+
maxAttempts?: number;
|
|
29
|
+
onProgress?: (progress: ManagedDownloadProgress) => void;
|
|
30
|
+
payload: ManagedDownloadPayload;
|
|
31
|
+
signal?: AbortSignal;
|
|
32
|
+
};
|
|
33
|
+
export type ManagedDownloadResult = {
|
|
34
|
+
bucket: string;
|
|
35
|
+
bytes: number;
|
|
36
|
+
checksum: ManagedDownloadChecksum;
|
|
37
|
+
fileName: string;
|
|
38
|
+
path: string;
|
|
39
|
+
reusedComplete: boolean;
|
|
40
|
+
resumed: boolean;
|
|
41
|
+
urlDigest: string;
|
|
42
|
+
};
|
|
43
|
+
export type DownloadCopyAndClearOptions = ManagedDownloadOptions & {
|
|
44
|
+
outputPath: string;
|
|
45
|
+
};
|
|
46
|
+
export type DownloadCopyAndClearResult = {
|
|
47
|
+
bytes: number;
|
|
48
|
+
cleanup: "deferred" | "removed";
|
|
49
|
+
cleanupWarning?: string;
|
|
50
|
+
outputPath: string;
|
|
51
|
+
reusedComplete: boolean;
|
|
52
|
+
resumed: boolean;
|
|
53
|
+
};
|
|
54
|
+
export type ManagedDownloadInspection = {
|
|
55
|
+
bucket: string;
|
|
56
|
+
complete: boolean;
|
|
57
|
+
fileName: string;
|
|
58
|
+
manifest: "complete" | "missing" | "partial" | "unreadable";
|
|
59
|
+
path: string;
|
|
60
|
+
};
|
|
61
|
+
export type RemoveManagedDownloadOptions = {
|
|
62
|
+
basePath: string;
|
|
63
|
+
bucket: string;
|
|
64
|
+
fileName: string;
|
|
65
|
+
};
|
|
66
|
+
export type RemoveManagedDownloadResult = {
|
|
67
|
+
removed: boolean;
|
|
68
|
+
};
|
|
69
|
+
export type PruneManagedDownloadsOptions = {
|
|
70
|
+
basePath: string;
|
|
71
|
+
now?: Date;
|
|
72
|
+
olderThanMs?: number;
|
|
73
|
+
};
|
|
74
|
+
export type PruneManagedDownloadsResult = {
|
|
75
|
+
removed: number;
|
|
76
|
+
warnings: string[];
|
|
77
|
+
};
|
|
78
|
+
export declare const MANAGED_DOWNLOAD_ERROR_CODES: Readonly<{
|
|
79
|
+
readonly ABORTED: "aborted";
|
|
80
|
+
readonly CHECKSUM_MISMATCH: "checksum-mismatch";
|
|
81
|
+
readonly INVALID_TARGET: "invalid-target";
|
|
82
|
+
readonly NETWORK_EXHAUSTED: "network-exhausted";
|
|
83
|
+
readonly OUTPUT_CONFLICT: "output-conflict";
|
|
84
|
+
readonly STORE_CORRUPT: "store-corrupt";
|
|
85
|
+
readonly STORE_NOT_OWNED: "store-not-owned";
|
|
86
|
+
readonly TARGET_CONFLICT: "target-conflict";
|
|
87
|
+
readonly TARGET_LOCKED: "target-locked";
|
|
88
|
+
}>;
|
|
89
|
+
export type ManagedDownloadErrorCode = (typeof MANAGED_DOWNLOAD_ERROR_CODES)[keyof typeof MANAGED_DOWNLOAD_ERROR_CODES];
|
|
90
|
+
export declare class ManagedDownloadError extends Error {
|
|
91
|
+
readonly code: ManagedDownloadErrorCode;
|
|
92
|
+
readonly details?: unknown;
|
|
93
|
+
constructor(code: ManagedDownloadErrorCode, message: string, details?: unknown);
|
|
94
|
+
}
|
|
95
|
+
export type StoreSentinel = {
|
|
96
|
+
createdAt: string;
|
|
97
|
+
kind: typeof STORE_KIND;
|
|
98
|
+
schemaVersion: typeof STORE_SCHEMA_VERSION;
|
|
99
|
+
};
|
|
100
|
+
export type DownloadManifest = {
|
|
101
|
+
bucket: string;
|
|
102
|
+
checksum: ManagedDownloadChecksum;
|
|
103
|
+
createdAt: string;
|
|
104
|
+
fileName: string;
|
|
105
|
+
identityDigest: string;
|
|
106
|
+
kind: typeof MANIFEST_KIND;
|
|
107
|
+
schemaVersion: typeof MANIFEST_SCHEMA_VERSION;
|
|
108
|
+
state: "complete" | "partial";
|
|
109
|
+
targetKey: string;
|
|
110
|
+
totalBytes?: number | undefined;
|
|
111
|
+
updatedAt: string;
|
|
112
|
+
urlDigest: string;
|
|
113
|
+
validators?: {
|
|
114
|
+
etag?: string | undefined;
|
|
115
|
+
lastModified?: string | undefined;
|
|
116
|
+
} | undefined;
|
|
117
|
+
};
|
|
118
|
+
export type NormalizedTarget = {
|
|
119
|
+
basePath: string;
|
|
120
|
+
bucket: string;
|
|
121
|
+
checksum: ManagedDownloadChecksum;
|
|
122
|
+
fileName: string;
|
|
123
|
+
finalPath: string;
|
|
124
|
+
identityDigest: string;
|
|
125
|
+
lockPath: string;
|
|
126
|
+
manifestPath: string;
|
|
127
|
+
partialPath: string;
|
|
128
|
+
targetKey: string;
|
|
129
|
+
url: string;
|
|
130
|
+
urlDigest: string;
|
|
131
|
+
};
|
|
132
|
+
export type ActiveTask = {
|
|
133
|
+
listeners: Set<(progress: ManagedDownloadProgress) => void>;
|
|
134
|
+
promise: Promise<ManagedDownloadResult>;
|
|
135
|
+
targetKey: string;
|
|
136
|
+
};
|
|
137
|
+
export type CopyLease = {
|
|
138
|
+
key: string;
|
|
139
|
+
};
|
|
140
|
+
export type CopyLeaseState = {
|
|
141
|
+
clearRequested: boolean;
|
|
142
|
+
count: number;
|
|
143
|
+
removeOptions: RemoveManagedDownloadOptions | null;
|
|
144
|
+
};
|
|
145
|
+
export type AcquiredLock = {
|
|
146
|
+
path: string;
|
|
147
|
+
};
|
|
148
|
+
export type DownloadLockFile = {
|
|
149
|
+
createdAt: string;
|
|
150
|
+
pid: number;
|
|
151
|
+
processStartedAt?: string;
|
|
152
|
+
};
|
|
153
|
+
export type DownloadAttemptResult = {
|
|
154
|
+
resumed: boolean;
|
|
155
|
+
totalBytes?: number | undefined;
|
|
156
|
+
};
|
|
157
|
+
export type ReusableState = {
|
|
158
|
+
manifest: DownloadManifest | null;
|
|
159
|
+
reset?: boolean;
|
|
160
|
+
result?: ManagedDownloadResult;
|
|
161
|
+
};
|
|
162
|
+
//# sourceMappingURL=download-types.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const STORE_SENTINEL = ".jini-download-root.json";
|
|
2
|
+
export const STATE_DIR = ".state";
|
|
3
|
+
export const PARTIAL_DIR = ".partial";
|
|
4
|
+
export const LOCK_DIR = ".locks";
|
|
5
|
+
export const STORE_SCHEMA_VERSION = 1;
|
|
6
|
+
export const MANIFEST_SCHEMA_VERSION = 1;
|
|
7
|
+
export const STORE_KIND = "jini-managed-download-root";
|
|
8
|
+
export const MANIFEST_KIND = "jini-managed-download";
|
|
9
|
+
export const MANAGED_DOWNLOAD_ERROR_CODES = Object.freeze({
|
|
10
|
+
ABORTED: "aborted",
|
|
11
|
+
CHECKSUM_MISMATCH: "checksum-mismatch",
|
|
12
|
+
INVALID_TARGET: "invalid-target",
|
|
13
|
+
NETWORK_EXHAUSTED: "network-exhausted",
|
|
14
|
+
OUTPUT_CONFLICT: "output-conflict",
|
|
15
|
+
STORE_CORRUPT: "store-corrupt",
|
|
16
|
+
STORE_NOT_OWNED: "store-not-owned",
|
|
17
|
+
TARGET_CONFLICT: "target-conflict",
|
|
18
|
+
TARGET_LOCKED: "target-locked",
|
|
19
|
+
});
|
|
20
|
+
export class ManagedDownloadError extends Error {
|
|
21
|
+
code;
|
|
22
|
+
details;
|
|
23
|
+
constructor(code, message, details) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.name = "ManagedDownloadError";
|
|
26
|
+
this.code = code;
|
|
27
|
+
if (details !== undefined)
|
|
28
|
+
this.details = details;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=download-types.js.map
|
package/dist/download.d.ts
CHANGED
|
@@ -1,92 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
algorithm: "sha256" | "sha512";
|
|
3
|
-
value: string;
|
|
4
|
-
};
|
|
5
|
-
export type ManagedDownloadPayload = {
|
|
6
|
-
checksum: ManagedDownloadChecksum;
|
|
7
|
-
headers?: Record<string, string>;
|
|
8
|
-
url: string;
|
|
9
|
-
};
|
|
10
|
-
export type ManagedDownloadProgress = {
|
|
11
|
-
receivedBytes: number;
|
|
12
|
-
sessionReceivedBytes: number;
|
|
13
|
-
totalBytes?: number;
|
|
14
|
-
};
|
|
15
|
-
export type ManagedDownloadOptions = {
|
|
16
|
-
basePath: string;
|
|
17
|
-
bucket: string;
|
|
18
|
-
fileName: string;
|
|
19
|
-
fetch?: typeof globalThis.fetch;
|
|
20
|
-
maxAttempts?: number;
|
|
21
|
-
onProgress?: (progress: ManagedDownloadProgress) => void;
|
|
22
|
-
payload: ManagedDownloadPayload;
|
|
23
|
-
signal?: AbortSignal;
|
|
24
|
-
};
|
|
25
|
-
export type ManagedDownloadResult = {
|
|
26
|
-
bucket: string;
|
|
27
|
-
bytes: number;
|
|
28
|
-
checksum: ManagedDownloadChecksum;
|
|
29
|
-
fileName: string;
|
|
30
|
-
path: string;
|
|
31
|
-
reusedComplete: boolean;
|
|
32
|
-
resumed: boolean;
|
|
33
|
-
urlDigest: string;
|
|
34
|
-
};
|
|
35
|
-
export type DownloadCopyAndClearOptions = ManagedDownloadOptions & {
|
|
36
|
-
outputPath: string;
|
|
37
|
-
};
|
|
38
|
-
export type DownloadCopyAndClearResult = {
|
|
39
|
-
bytes: number;
|
|
40
|
-
cleanup: "deferred" | "removed";
|
|
41
|
-
cleanupWarning?: string;
|
|
42
|
-
outputPath: string;
|
|
43
|
-
reusedComplete: boolean;
|
|
44
|
-
resumed: boolean;
|
|
45
|
-
};
|
|
46
|
-
export type ManagedDownloadInspection = {
|
|
47
|
-
bucket: string;
|
|
48
|
-
complete: boolean;
|
|
49
|
-
fileName: string;
|
|
50
|
-
manifest: "complete" | "missing" | "partial" | "unreadable";
|
|
51
|
-
path: string;
|
|
52
|
-
};
|
|
53
|
-
export type RemoveManagedDownloadOptions = {
|
|
54
|
-
basePath: string;
|
|
55
|
-
bucket: string;
|
|
56
|
-
fileName: string;
|
|
57
|
-
};
|
|
58
|
-
export type RemoveManagedDownloadResult = {
|
|
59
|
-
removed: boolean;
|
|
60
|
-
};
|
|
61
|
-
export type PruneManagedDownloadsOptions = {
|
|
62
|
-
basePath: string;
|
|
63
|
-
now?: Date;
|
|
64
|
-
olderThanMs?: number;
|
|
65
|
-
};
|
|
66
|
-
export type PruneManagedDownloadsResult = {
|
|
67
|
-
removed: number;
|
|
68
|
-
warnings: string[];
|
|
69
|
-
};
|
|
70
|
-
export declare const MANAGED_DOWNLOAD_ERROR_CODES: Readonly<{
|
|
71
|
-
readonly ABORTED: "aborted";
|
|
72
|
-
readonly CHECKSUM_MISMATCH: "checksum-mismatch";
|
|
73
|
-
readonly INVALID_TARGET: "invalid-target";
|
|
74
|
-
readonly NETWORK_EXHAUSTED: "network-exhausted";
|
|
75
|
-
readonly OUTPUT_CONFLICT: "output-conflict";
|
|
76
|
-
readonly STORE_CORRUPT: "store-corrupt";
|
|
77
|
-
readonly STORE_NOT_OWNED: "store-not-owned";
|
|
78
|
-
readonly TARGET_CONFLICT: "target-conflict";
|
|
79
|
-
readonly TARGET_LOCKED: "target-locked";
|
|
80
|
-
}>;
|
|
81
|
-
export type ManagedDownloadErrorCode = (typeof MANAGED_DOWNLOAD_ERROR_CODES)[keyof typeof MANAGED_DOWNLOAD_ERROR_CODES];
|
|
82
|
-
export declare class ManagedDownloadError extends Error {
|
|
83
|
-
readonly code: ManagedDownloadErrorCode;
|
|
84
|
-
readonly details?: unknown;
|
|
85
|
-
constructor(code: ManagedDownloadErrorCode, message: string, details?: unknown);
|
|
86
|
-
}
|
|
1
|
+
import { type ManagedDownloadOptions, type ManagedDownloadResult, type DownloadCopyAndClearOptions, type DownloadCopyAndClearResult } from './download-types.js';
|
|
87
2
|
export declare function managedDownload(options: ManagedDownloadOptions): Promise<ManagedDownloadResult>;
|
|
88
3
|
export declare function downloadCopyAndClear(options: DownloadCopyAndClearOptions): Promise<DownloadCopyAndClearResult>;
|
|
89
|
-
export declare function inspectManagedDownload(options: RemoveManagedDownloadOptions): Promise<ManagedDownloadInspection>;
|
|
90
|
-
export declare function removeManagedDownload(options: RemoveManagedDownloadOptions): Promise<RemoveManagedDownloadResult>;
|
|
91
|
-
export declare function pruneManagedDownloads(options: PruneManagedDownloadsOptions): Promise<PruneManagedDownloadsResult>;
|
|
92
4
|
//# sourceMappingURL=download.d.ts.map
|