browser-model-manager 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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/PROVENANCE.json +16 -0
- package/README.md +607 -2
- package/THIRD_PARTY_NOTICES +4 -0
- package/dist/acquisition/grants.d.ts +31 -0
- package/dist/acquisition/grants.js +75 -0
- package/dist/acquisition/index.d.ts +4 -0
- package/dist/acquisition/index.js +6 -0
- package/dist/acquisition/output.d.ts +12 -0
- package/dist/acquisition/output.js +19 -0
- package/dist/acquisition/phases.d.ts +3 -0
- package/dist/acquisition/phases.js +64 -0
- package/dist/acquisition/progress.d.ts +29 -0
- package/dist/acquisition/progress.js +124 -0
- package/dist/acquisition/slots.d.ts +35 -0
- package/dist/acquisition/slots.js +296 -0
- package/dist/cache/index.d.ts +3 -0
- package/dist/cache/index.js +4 -0
- package/dist/cache/model-cache.d.ts +152 -0
- package/dist/cache/model-cache.js +1585 -0
- package/dist/cache/sha256.d.ts +11 -0
- package/dist/cache/sha256.js +97 -0
- package/dist/cache/shard-store.d.ts +89 -0
- package/dist/cache/shard-store.js +204 -0
- package/dist/contracts/budgets.d.ts +41 -0
- package/dist/contracts/budgets.js +48 -0
- package/dist/contracts/errors.d.ts +43 -0
- package/dist/contracts/errors.js +2 -0
- package/dist/contracts/grant.d.ts +47 -0
- package/dist/contracts/grant.js +44 -0
- package/dist/contracts/index.d.ts +6 -0
- package/dist/contracts/index.js +7 -0
- package/dist/contracts/leases.d.ts +20 -0
- package/dist/contracts/leases.js +14 -0
- package/dist/contracts/lifecycle.d.ts +88 -0
- package/dist/contracts/lifecycle.js +12 -0
- package/dist/contracts/manifest.d.ts +95 -0
- package/dist/contracts/manifest.js +174 -0
- package/dist/handles/cross-tab.d.ts +16 -0
- package/dist/handles/cross-tab.js +29 -0
- package/dist/handles/handle.d.ts +24 -0
- package/dist/handles/handle.js +93 -0
- package/dist/handles/index.d.ts +3 -0
- package/dist/handles/index.js +4 -0
- package/dist/handles/leases.d.ts +45 -0
- package/dist/handles/leases.js +103 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +7 -0
- package/package.json +43 -4
- package/schema/model-manifest.v1.schema.json +215 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
browser-model-manager bundles no third-party code. Its emitted JavaScript imports only
|
|
2
|
+
stylegan-js from the same repository (for types and contracts). The model and runtime bytes it acquires
|
|
3
|
+
(for example ONNX Runtime Web, MIT, Microsoft, and model weights) are not part of this package; their terms are
|
|
4
|
+
recorded per model in the docs and travel with those bytes.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AcquisitionGrant, GrantAsset, GrantOwnerId } from '../contracts/grant.ts';
|
|
2
|
+
export type GrantRefusal =
|
|
3
|
+
/** No grant was supplied: nothing is fetched. */
|
|
4
|
+
'no-grant'
|
|
5
|
+
/** The owner is not one the grant names. */
|
|
6
|
+
| 'owner-not-in-grant'
|
|
7
|
+
/** The digest or its size is not in the grant. */
|
|
8
|
+
| 'out-of-scope'
|
|
9
|
+
/** The grant's remaining bytes cannot cover the transfer: a revised grant is needed. */
|
|
10
|
+
| 'revision-required'
|
|
11
|
+
/** Every owner of the shared fetch has left. */
|
|
12
|
+
| 'no-owner';
|
|
13
|
+
/** Thrown before any request is made; `refusal` says why. */
|
|
14
|
+
export declare class GrantRefused extends Error {
|
|
15
|
+
readonly refusal: GrantRefusal;
|
|
16
|
+
readonly sha256: string;
|
|
17
|
+
constructor(refusal: GrantRefusal, sha256: string);
|
|
18
|
+
}
|
|
19
|
+
/** Bytes this grant may still transfer (approved, minus received, minus reserved by attempts in flight). */
|
|
20
|
+
export declare function grantRemaining(grant: AcquisitionGrant): number;
|
|
21
|
+
/** The asset of `sha256` with exactly `size` bytes in the grant, or a refusal. */
|
|
22
|
+
export declare function grantAsset(grant: AcquisitionGrant | undefined, owner: GrantOwnerId | undefined, sha256: string, size: number): GrantAsset;
|
|
23
|
+
/** One transfer attempt's claim on a grant: reserved up front, settled at the bytes actually received. */
|
|
24
|
+
export interface GrantCharge {
|
|
25
|
+
/** Counts bytes as they arrive; throws once the attempt would pass what it reserved. */
|
|
26
|
+
received(bytes: number): void;
|
|
27
|
+
/** Ends the attempt: received bytes stay spent, the unreceived part of the reservation is returned. */
|
|
28
|
+
settle(): void;
|
|
29
|
+
}
|
|
30
|
+
/** Reserves `bytes` (one chunk or asset) for an attempt, or refuses with `revision-required`. */
|
|
31
|
+
export declare function chargeGrant(grant: AcquisitionGrant, sha256: string, bytes: number): GrantCharge;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Grant enforcement (fm-fr9s): the manager transfers bytes only under an AcquisitionGrant the app supplies, only
|
|
2
|
+
// for digests inside it, and never more bytes than it approved. Every byte received counts against the grant,
|
|
3
|
+
// whichever origin sent it and whichever attempt it belonged to (a retry, an alternate origin, a corrupt chunk's
|
|
4
|
+
// refetch), so what the person approved is a bound on what crosses the network. A grant's remaining bytes only
|
|
5
|
+
// go down: nothing the manager does (retry, repair, revalidation) adds to them. More scope is a new grant object,
|
|
6
|
+
// which only the app can make, after a revised decision.
|
|
7
|
+
//
|
|
8
|
+
// A grant's ledger lives in the JS realm that spends it (a window or one worker): a grant posted to two workers
|
|
9
|
+
// arrives as two copies, each with the full remaining bytes. The app therefore gives each worker its own grant for
|
|
10
|
+
// the share it may spend, so the grants it hands out never add up to more than the person approved.
|
|
11
|
+
/** Thrown before any request is made; `refusal` says why. */
|
|
12
|
+
export class GrantRefused extends Error {
|
|
13
|
+
refusal;
|
|
14
|
+
sha256;
|
|
15
|
+
constructor(refusal, sha256) {
|
|
16
|
+
super(refusal === 'revision-required'
|
|
17
|
+
? 'This download needs more data than was approved. Approve the download again to continue.'
|
|
18
|
+
: `Model file ${sha256.slice(0, 12)} is not covered by an approved download (${refusal}).`);
|
|
19
|
+
this.name = 'GrantRefused';
|
|
20
|
+
this.refusal = refusal;
|
|
21
|
+
this.sha256 = sha256;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const ledgers = new WeakMap();
|
|
25
|
+
const ledgerOf = (grant) => {
|
|
26
|
+
let l = ledgers.get(grant);
|
|
27
|
+
if (!l) {
|
|
28
|
+
if (!Number.isSafeInteger(grant.remainingBytes) || grant.remainingBytes < 0)
|
|
29
|
+
throw new TypeError('A grant needs a non-negative integer remainingBytes');
|
|
30
|
+
l = { remaining: grant.remainingBytes, reserved: 0 };
|
|
31
|
+
ledgers.set(grant, l);
|
|
32
|
+
}
|
|
33
|
+
return l;
|
|
34
|
+
};
|
|
35
|
+
/** Bytes this grant may still transfer (approved, minus received, minus reserved by attempts in flight). */
|
|
36
|
+
export function grantRemaining(grant) {
|
|
37
|
+
const l = ledgerOf(grant);
|
|
38
|
+
return l.remaining - l.reserved;
|
|
39
|
+
}
|
|
40
|
+
/** The asset of `sha256` with exactly `size` bytes in the grant, or a refusal. */
|
|
41
|
+
export function grantAsset(grant, owner, sha256, size) {
|
|
42
|
+
if (!grant)
|
|
43
|
+
throw new GrantRefused('no-grant', sha256);
|
|
44
|
+
if (!owner || !grant.owners.includes(owner))
|
|
45
|
+
throw new GrantRefused('owner-not-in-grant', sha256);
|
|
46
|
+
const asset = grant.assets.find((a) => a.sha256 === sha256);
|
|
47
|
+
if (!asset || asset.bytes !== size)
|
|
48
|
+
throw new GrantRefused('out-of-scope', sha256);
|
|
49
|
+
return asset;
|
|
50
|
+
}
|
|
51
|
+
/** Reserves `bytes` (one chunk or asset) for an attempt, or refuses with `revision-required`. */
|
|
52
|
+
export function chargeGrant(grant, sha256, bytes) {
|
|
53
|
+
const l = ledgerOf(grant);
|
|
54
|
+
if (l.remaining - l.reserved < bytes)
|
|
55
|
+
throw new GrantRefused('revision-required', sha256);
|
|
56
|
+
l.reserved += bytes;
|
|
57
|
+
let got = 0;
|
|
58
|
+
let open = true;
|
|
59
|
+
return {
|
|
60
|
+
received(n) {
|
|
61
|
+
if (!open)
|
|
62
|
+
return;
|
|
63
|
+
if (got + n > bytes)
|
|
64
|
+
throw new Error('Asset exceeds declared size');
|
|
65
|
+
got += n;
|
|
66
|
+
},
|
|
67
|
+
settle() {
|
|
68
|
+
if (!open)
|
|
69
|
+
return;
|
|
70
|
+
open = false;
|
|
71
|
+
l.reserved -= bytes;
|
|
72
|
+
l.remaining -= got;
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Acquisition primitives (fm-fr9s): grant enforcement, the aggregate slot budget with priority classes, and the
|
|
2
|
+
// candidate's whole-acquisition progress. Consent and priorities are app policy; these only enforce them.
|
|
3
|
+
export * from './grants.js';
|
|
4
|
+
export * from './slots.js';
|
|
5
|
+
export * from './progress.js';
|
|
6
|
+
export * from './output.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type ActionSource<O, M> = {
|
|
2
|
+
readonly from: 'output-cache';
|
|
3
|
+
readonly output: O;
|
|
4
|
+
} | {
|
|
5
|
+
readonly from: 'model';
|
|
6
|
+
readonly model: M;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Looks the action up in the app's output cache first; acquires (and so may download) only on a miss. A lookup
|
|
10
|
+
* that fails is a miss, never a reason to skip the grant.
|
|
11
|
+
*/
|
|
12
|
+
export declare function satisfyAction<O, M>(lookup: () => Promise<O | null | undefined>, acquire: () => Promise<M>): Promise<ActionSource<O, M>>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Output-cache first (fm-fr9s): an action whose result the app already holds (a prepared names-picker face, a
|
|
2
|
+
// cached still) is satisfied from that output and acquires nothing. Model-cached and output-cached stay distinct
|
|
3
|
+
// cases: only a miss in the output cache reaches the model cache, and only then under the action's grant.
|
|
4
|
+
/**
|
|
5
|
+
* Looks the action up in the app's output cache first; acquires (and so may download) only on a miss. A lookup
|
|
6
|
+
* that fails is a miss, never a reason to skip the grant.
|
|
7
|
+
*/
|
|
8
|
+
export async function satisfyAction(lookup, acquire) {
|
|
9
|
+
let output;
|
|
10
|
+
try {
|
|
11
|
+
output = await lookup();
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
output = null;
|
|
15
|
+
}
|
|
16
|
+
if (output !== null && output !== undefined)
|
|
17
|
+
return { from: 'output-cache', output };
|
|
18
|
+
return { from: 'model', model: await acquire() };
|
|
19
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const localTails = {
|
|
2
|
+
hash: Promise.resolve(),
|
|
3
|
+
write: Promise.resolve(),
|
|
4
|
+
};
|
|
5
|
+
const check = (signal) => {
|
|
6
|
+
if (signal?.aborted)
|
|
7
|
+
throw signal.reason ?? new DOMException('Asset acquisition cancelled', 'AbortError');
|
|
8
|
+
};
|
|
9
|
+
/** Opaque origins without Web Locks still serialize operations within their realm. */
|
|
10
|
+
async function localPhase(phase, run, signal) {
|
|
11
|
+
const previous = localTails[phase];
|
|
12
|
+
let release;
|
|
13
|
+
const done = new Promise((resolve) => {
|
|
14
|
+
release = resolve;
|
|
15
|
+
});
|
|
16
|
+
localTails[phase] = previous.then(() => done);
|
|
17
|
+
let abort;
|
|
18
|
+
try {
|
|
19
|
+
await (signal
|
|
20
|
+
? Promise.race([
|
|
21
|
+
previous,
|
|
22
|
+
new Promise((_resolve, reject) => {
|
|
23
|
+
abort = () => reject(signal.reason ?? new DOMException('Asset acquisition cancelled', 'AbortError'));
|
|
24
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
25
|
+
if (signal.aborted)
|
|
26
|
+
abort();
|
|
27
|
+
}),
|
|
28
|
+
])
|
|
29
|
+
: previous);
|
|
30
|
+
check(signal);
|
|
31
|
+
return await run();
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
if (abort)
|
|
35
|
+
signal.removeEventListener('abort', abort);
|
|
36
|
+
// A cancelled waiter stays behind its predecessor in the tail. Releasing it cannot let
|
|
37
|
+
// a later operation bypass the predecessor while that operation is still running.
|
|
38
|
+
release();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export async function acquisitionPhase(phase, run, signal) {
|
|
42
|
+
check(signal);
|
|
43
|
+
let entered = false;
|
|
44
|
+
try {
|
|
45
|
+
const locks = globalThis.navigator?.locks;
|
|
46
|
+
if (!locks)
|
|
47
|
+
return localPhase(phase, run, signal);
|
|
48
|
+
let value;
|
|
49
|
+
await locks.request(`facemorph-acquisition:phase-${phase}`, signal ? { signal } : {}, async () => {
|
|
50
|
+
check(signal);
|
|
51
|
+
entered = true;
|
|
52
|
+
value = await run();
|
|
53
|
+
});
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
// Only a denied lock API falls back. A SecurityError from the operation itself must not
|
|
58
|
+
// repeat a digest/write, and a cancelled queued request must never run through the fallback.
|
|
59
|
+
check(signal);
|
|
60
|
+
if (entered || error?.name !== 'SecurityError')
|
|
61
|
+
throw error;
|
|
62
|
+
return localPhase(phase, run, signal);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { CacheEvent } from '../cache/model-cache.ts';
|
|
2
|
+
export interface PlannedAsset {
|
|
3
|
+
readonly sha256: string;
|
|
4
|
+
readonly size: number;
|
|
5
|
+
readonly url: string;
|
|
6
|
+
}
|
|
7
|
+
export interface AcquisitionTotals {
|
|
8
|
+
readonly loaded: number;
|
|
9
|
+
readonly total: number;
|
|
10
|
+
readonly fetched: number;
|
|
11
|
+
readonly fetchedTotal: number;
|
|
12
|
+
}
|
|
13
|
+
export interface AcquisitionProgress {
|
|
14
|
+
planAsset(value: unknown): void;
|
|
15
|
+
probe(has: (asset: PlannedAsset) => Promise<boolean>): Promise<void>;
|
|
16
|
+
totals(): AcquisitionTotals;
|
|
17
|
+
progress(progressOnly: boolean): void;
|
|
18
|
+
cacheEvent(event: CacheEvent): void;
|
|
19
|
+
attach(fn: (totals: AcquisitionTotals, progressOnly: boolean) => void): void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Every asset descriptor inside a manifest fragment, in declaration order.
|
|
23
|
+
*
|
|
24
|
+
* The prefetch lane acquires exactly what the progress budget plans, so both read the manifest through this one
|
|
25
|
+
* walk. A second copy of these rules would drift, and the difference would show up as a bar that never reaches its
|
|
26
|
+
* total.
|
|
27
|
+
*/
|
|
28
|
+
export declare function collectAssets(value: unknown, depth?: number, found?: PlannedAsset[]): PlannedAsset[];
|
|
29
|
+
export declare function createAcquisitionBudget(): AcquisitionProgress;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Whole-acquisition progress, moved from the candidate's src/Next/browser/acquisition-budget.mjs (classic a42829b)
|
|
2
|
+
// without behaviour change (fm-fr9s); the comments are the candidate's. (The candidate called this an "acquisition
|
|
3
|
+
// budget"; it counts bytes for one progress bar and bounds nothing: the byte bounds are contracts/budgets.ts.)
|
|
4
|
+
//
|
|
5
|
+
// One acquisition budget for everything a route needs, so the progress bar crosses the download once instead of
|
|
6
|
+
// restarting at every asset (C-10). The inference worker feeds this the manifest assets it plans to fetch plus the
|
|
7
|
+
// model cache's byte-level status events, and reports a single {loaded,total} pair for the whole set.
|
|
8
|
+
//
|
|
9
|
+
// In-flight bytes are tracked per asset rather than in a single slot: two assets downloading concurrently (the
|
|
10
|
+
// cache allows bounded parallelism) each advance the bar instead of fighting over one reading, so `loaded` only ever
|
|
11
|
+
// increases within an asset and across them.
|
|
12
|
+
const isPlanned = (value) => {
|
|
13
|
+
const v = value;
|
|
14
|
+
return typeof v.sha256 === 'string' && Number.isSafeInteger(v.size) && typeof v.url === 'string';
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Every asset descriptor inside a manifest fragment, in declaration order.
|
|
18
|
+
*
|
|
19
|
+
* The prefetch lane acquires exactly what the progress budget plans, so both read the manifest through this one
|
|
20
|
+
* walk. A second copy of these rules would drift, and the difference would show up as a bar that never reaches its
|
|
21
|
+
* total.
|
|
22
|
+
*/
|
|
23
|
+
export function collectAssets(value, depth = 0, found = []) {
|
|
24
|
+
if (!value || typeof value !== 'object')
|
|
25
|
+
return found;
|
|
26
|
+
if (isPlanned(value)) {
|
|
27
|
+
found.push(value);
|
|
28
|
+
return found;
|
|
29
|
+
}
|
|
30
|
+
if (depth >= 3)
|
|
31
|
+
return found;
|
|
32
|
+
for (const item of Array.isArray(value) ? value : Object.values(value))
|
|
33
|
+
collectAssets(item, depth + 1, found);
|
|
34
|
+
return found;
|
|
35
|
+
}
|
|
36
|
+
export function createAcquisitionBudget() {
|
|
37
|
+
const planned = new Map();
|
|
38
|
+
const completed = new Map();
|
|
39
|
+
const inflight = new Map();
|
|
40
|
+
// Which assets are actually crossing the network. The bar covers the whole acquisition on purpose — a cache read
|
|
41
|
+
// still costs the visitor time — but the words next to it must not claim a download that is not happening. A warm
|
|
42
|
+
// device reported "Downloading model files… 183 MB of 203 MB" with every one of those bytes already on disk, which
|
|
43
|
+
// is how a cache that was working read as a gigabyte re-downloading on every visit (operator, 22 September).
|
|
44
|
+
const fetching = new Set();
|
|
45
|
+
// Presence answers from the cache, taken when an asset is planned. Without them an asset only joined the fetched
|
|
46
|
+
// set when the cache reached for the network, so the denominator grew asset by asset as the download went on
|
|
47
|
+
// ("40 MB of 60 MB" becoming "of 120 MB").
|
|
48
|
+
const probed = new Map();
|
|
49
|
+
const descriptors = new Map();
|
|
50
|
+
let emitProgress = null;
|
|
51
|
+
function planAsset(value) {
|
|
52
|
+
for (const asset of collectAssets(value)) {
|
|
53
|
+
planned.set(asset.sha256, asset.size);
|
|
54
|
+
descriptors.set(asset.sha256, asset);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function totals() {
|
|
58
|
+
let total = 0;
|
|
59
|
+
let loaded = 0;
|
|
60
|
+
let fetched = 0;
|
|
61
|
+
let fetchedTotal = 0;
|
|
62
|
+
for (const [sha256, size] of planned) {
|
|
63
|
+
const done = completed.get(sha256) ?? Math.min(inflight.get(sha256) ?? 0, size);
|
|
64
|
+
total += size;
|
|
65
|
+
loaded += done;
|
|
66
|
+
if (fetching.has(sha256) || probed.get(sha256) === false) {
|
|
67
|
+
fetchedTotal += size;
|
|
68
|
+
fetched += done;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { loaded, total, fetched, fetchedTotal };
|
|
72
|
+
}
|
|
73
|
+
// progressOnly marks a reading that belongs to the bar and not to the diagnostics ledger: at a megabyte a tick
|
|
74
|
+
// these would be hundreds of POSTs saying nothing the boundaries do not.
|
|
75
|
+
function progress(progressOnly) {
|
|
76
|
+
if (!emitProgress)
|
|
77
|
+
return;
|
|
78
|
+
emitProgress(totals(), progressOnly);
|
|
79
|
+
}
|
|
80
|
+
function cacheEvent(event) {
|
|
81
|
+
// The cache says 'missing' before it reaches for the network and 'downloading' as it does. Nothing else promotes
|
|
82
|
+
// an asset into the fetched set: a retained asset never becomes one, and a repair re-announces itself through the
|
|
83
|
+
// same two statuses.
|
|
84
|
+
if (event.status === 'missing' || event.status === 'downloading') {
|
|
85
|
+
fetching.add(event.sha256);
|
|
86
|
+
progress(true);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (event.status === 'progress') {
|
|
90
|
+
inflight.set(event.sha256, event.loaded);
|
|
91
|
+
progress(true);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (event.status === 'retained' || event.status === 'saved' || event.status === 'verified') {
|
|
95
|
+
completed.set(event.sha256, event.bytes ?? planned.get(event.sha256) ?? 0);
|
|
96
|
+
inflight.delete(event.sha256);
|
|
97
|
+
progress(true);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Ask the cache once about every planned asset it has not been asked about yet. */
|
|
101
|
+
async function probe(has) {
|
|
102
|
+
for (const [sha256, asset] of descriptors) {
|
|
103
|
+
if (probed.has(sha256) || completed.has(sha256))
|
|
104
|
+
continue;
|
|
105
|
+
probed.set(sha256, true);
|
|
106
|
+
try {
|
|
107
|
+
probed.set(sha256, (await has(asset)) === true);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
probed.delete(sha256);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
planAsset,
|
|
116
|
+
probe,
|
|
117
|
+
totals,
|
|
118
|
+
progress,
|
|
119
|
+
cacheEvent,
|
|
120
|
+
attach: (fn) => {
|
|
121
|
+
emitProgress = fn;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type AcquisitionMemoryClass } from '../contracts/budgets.ts';
|
|
2
|
+
import type { AcquisitionPriority } from '../contracts/grant.ts';
|
|
3
|
+
/** Highest first. */
|
|
4
|
+
export declare const PRIORITY_ORDER: readonly AcquisitionPriority[];
|
|
5
|
+
/** Optional tiers never wait in a lock queue: they take a free slot or wait their turn locally. */
|
|
6
|
+
export declare const queuesOnLocks: (p: AcquisitionPriority) => boolean;
|
|
7
|
+
/** The subset of the Web Locks API the slots use (navigator.locks in windows and workers). */
|
|
8
|
+
export interface SlotLocks {
|
|
9
|
+
request(name: string, options: {
|
|
10
|
+
readonly ifAvailable?: boolean;
|
|
11
|
+
readonly signal?: AbortSignal;
|
|
12
|
+
}, run: (lock: unknown) => Promise<void> | void): Promise<unknown>;
|
|
13
|
+
query?(): Promise<{
|
|
14
|
+
readonly held?: readonly {
|
|
15
|
+
readonly name?: string;
|
|
16
|
+
}[];
|
|
17
|
+
readonly pending?: readonly {
|
|
18
|
+
readonly name?: string;
|
|
19
|
+
}[];
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
22
|
+
export interface AcquisitionSlots {
|
|
23
|
+
readonly count: number;
|
|
24
|
+
/** Waits for a slot; resolves with its release. Rejects with the signal's reason when cancelled first. */
|
|
25
|
+
take(priority: AcquisitionPriority, signal: AbortSignal): Promise<() => void>;
|
|
26
|
+
}
|
|
27
|
+
/** Slots per memory class: the aggregate read-ahead budget in whole chunks. */
|
|
28
|
+
export declare function slotCount(memory: AcquisitionMemoryClass): number;
|
|
29
|
+
/** How long optional work waits between looks for a free slot. */
|
|
30
|
+
export declare const OPTIONAL_RETRY_MS = 100;
|
|
31
|
+
export declare function createAcquisitionSlots({ locks, count, name, }: {
|
|
32
|
+
readonly locks: SlotLocks;
|
|
33
|
+
readonly count: number;
|
|
34
|
+
readonly name?: string;
|
|
35
|
+
}): AcquisitionSlots;
|