@gmitch215/folio 0.0.0-stage → 1.0.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 +21 -0
- package/README.md +482 -2
- package/dist/bake.d.ts +174 -0
- package/dist/bake.js +701 -0
- package/dist/browser-worker.d.ts +9 -0
- package/dist/browser-worker.js +37 -0
- package/dist/browser.d.ts +68 -0
- package/dist/browser.js +274 -0
- package/dist/compiler.d.ts +43 -0
- package/dist/compiler.js +505 -0
- package/dist/coordinator.d.ts +27 -0
- package/dist/coordinator.js +189 -0
- package/dist/extract.d.ts +71 -0
- package/dist/extract.js +1176 -0
- package/dist/frames.d.ts +19 -0
- package/dist/frames.js +111 -0
- package/dist/guards.d.ts +12 -0
- package/dist/guards.js +202 -0
- package/dist/host.d.ts +60 -0
- package/dist/host.js +374 -0
- package/dist/import-order.d.ts +2 -0
- package/dist/import-order.js +114 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/managed.d.ts +86 -0
- package/dist/managed.js +398 -0
- package/dist/memory.d.ts +10 -0
- package/dist/memory.js +89 -0
- package/dist/node.d.ts +124 -0
- package/dist/node.js +831 -0
- package/dist/pages.d.ts +109 -0
- package/dist/pages.js +339 -0
- package/dist/remote.d.ts +86 -0
- package/dist/remote.js +255 -0
- package/dist/tasks.d.ts +107 -0
- package/dist/tasks.js +317 -0
- package/dist/transaction.d.ts +23 -0
- package/dist/transaction.js +93 -0
- package/dist/values.d.ts +3 -0
- package/dist/values.js +25 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +86 -0
- package/package.json +126 -5
package/dist/pages.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/** A WebAssembly page contains 64 KiB. */
|
|
2
|
+
export declare const pageBytes = 65536;
|
|
3
|
+
/** Every owner operation is scoped to one tenant, session and fresh directory incarnation. */
|
|
4
|
+
export interface PageScope {
|
|
5
|
+
tenant: string;
|
|
6
|
+
session: string;
|
|
7
|
+
incarnation: string;
|
|
8
|
+
}
|
|
9
|
+
/** Immutable owner version. A fence is never reused within an incarnation. */
|
|
10
|
+
export interface PageVersion {
|
|
11
|
+
page: number;
|
|
12
|
+
fence: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Transport boundary for immutable pages. Reads return independent copies. Staging is idempotent for
|
|
16
|
+
* identical versions and bytes; discarding fences that version against delayed stage requests.
|
|
17
|
+
* Implementations must authenticate the scope and enforce their own storage, request and deadline
|
|
18
|
+
* limits. A never-settling request retains its directory locks; expiry cannot cancel an RPC.
|
|
19
|
+
*/
|
|
20
|
+
export interface PageOwner {
|
|
21
|
+
readonly id: string;
|
|
22
|
+
read(scope: PageScope, version: PageVersion): Promise<Uint8Array>;
|
|
23
|
+
stage(scope: PageScope, version: PageVersion, bytes: Uint8Array): Promise<void>;
|
|
24
|
+
discard(scope: PageScope, version: PageVersion): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
/** Bounded, volatile reference owner. This class does not persist pages across process loss. */
|
|
27
|
+
export declare class MemoryPageOwner implements PageOwner {
|
|
28
|
+
#private;
|
|
29
|
+
readonly id: string;
|
|
30
|
+
constructor(id: string, scope: PageScope, maximumBytes: number);
|
|
31
|
+
/** Bytes retained by immutable versions, including unpublished staged versions. */
|
|
32
|
+
get bytes(): number;
|
|
33
|
+
read(scope: PageScope, version: PageVersion): Promise<Uint8Array>;
|
|
34
|
+
stage(scope: PageScope, version: PageVersion, bytes: Uint8Array): Promise<void>;
|
|
35
|
+
discard(scope: PageScope, version: PageVersion): Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
/** Bounds apply to retained metadata, resident pages and commit snapshots, not caller-owned copies. */
|
|
38
|
+
export interface PageDirectoryOptions {
|
|
39
|
+
/** Fresh incarnation for one trusted active directory; never reuse it after a restart. */
|
|
40
|
+
scope: PageScope;
|
|
41
|
+
owners: readonly PageOwner[];
|
|
42
|
+
initialPages?: number;
|
|
43
|
+
maximumPages?: number;
|
|
44
|
+
maximumLeases?: number;
|
|
45
|
+
maximumLeasePages?: number;
|
|
46
|
+
maximumResidentPages?: number;
|
|
47
|
+
maximumRetiredPages?: number;
|
|
48
|
+
leaseMilliseconds?: number;
|
|
49
|
+
clock?: () => number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Volatile manifest coordinator. Publication is atomic within this instance. Recovery requires a
|
|
53
|
+
* persisted manifest and decision log; this constructor only creates a fresh zero-filled arena.
|
|
54
|
+
* Exactly one trusted directory may issue fences for its scope. Restart with a new incarnation;
|
|
55
|
+
* reusing an old scope is unsupported and can collide with old versions and delayed requests.
|
|
56
|
+
*/
|
|
57
|
+
export declare class PageDirectory {
|
|
58
|
+
#private;
|
|
59
|
+
readonly scope: Readonly<PageScope>;
|
|
60
|
+
constructor(options: PageDirectoryOptions);
|
|
61
|
+
/** Directory metadata only; page contents live at owners or in active leases. */
|
|
62
|
+
get stats(): {
|
|
63
|
+
pages: number;
|
|
64
|
+
residentBytes: number;
|
|
65
|
+
leases: number;
|
|
66
|
+
retiredPages: number;
|
|
67
|
+
};
|
|
68
|
+
/** Declares new zero pages without allocating page payloads. Returns the previous page count. */
|
|
69
|
+
grow(pages: number): number;
|
|
70
|
+
/** Returns immutable manifest metadata, never a page view. */
|
|
71
|
+
inspect(page: number): {
|
|
72
|
+
zero: boolean;
|
|
73
|
+
owner: string;
|
|
74
|
+
fence: number;
|
|
75
|
+
};
|
|
76
|
+
/** Acquires every page or none. Reads share locks; writes exclude all other leases. */
|
|
77
|
+
acquire(request: {
|
|
78
|
+
read?: readonly number[];
|
|
79
|
+
write?: readonly number[];
|
|
80
|
+
}): Promise<ResidentPageLease>;
|
|
81
|
+
/** Releases local dirty pages without publishing them. */
|
|
82
|
+
abort(lease: ResidentPageLease): void;
|
|
83
|
+
/** Stages all dirty pages, validates the fence again, then publishes one manifest decision. */
|
|
84
|
+
commit(lease: ResidentPageLease): Promise<void>;
|
|
85
|
+
/** Retries idempotent garbage deletion; failed owners retain credits and remain visible here. */
|
|
86
|
+
collect(): Promise<{
|
|
87
|
+
discarded: number;
|
|
88
|
+
pending: number;
|
|
89
|
+
}>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Resident copies for one bounded region. Escaped views cannot be physically revoked; after release
|
|
93
|
+
* they are caller-owned and outside the directory's admission accounting. They never alias owners.
|
|
94
|
+
*/
|
|
95
|
+
export declare class ResidentPageLease {
|
|
96
|
+
#private;
|
|
97
|
+
readonly scope: Readonly<PageScope>;
|
|
98
|
+
readonly fence: number;
|
|
99
|
+
readonly expiresAt: number;
|
|
100
|
+
readonly pages: readonly number[];
|
|
101
|
+
readonly writable: readonly number[];
|
|
102
|
+
constructor(directory: PageDirectory, scope: PageScope, fence: number, expires: number, pages: number[], writes: Set<number>);
|
|
103
|
+
/** Returns the local region page. Read pages may be changed locally but are never written back. */
|
|
104
|
+
view(page: number): Uint8Array;
|
|
105
|
+
/** Publishes writable pages together, or rejects with the previous manifest intact. */
|
|
106
|
+
commit(): Promise<void>;
|
|
107
|
+
/** Drops resident dirty bytes without publication. */
|
|
108
|
+
abort(): void;
|
|
109
|
+
}
|
package/dist/pages.js
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/** A WebAssembly page contains 64 KiB. */
|
|
2
|
+
export const pageBytes = 65536;
|
|
3
|
+
function integer(value, minimum, maximum, label) {
|
|
4
|
+
if (!Number.isSafeInteger(value) || value < minimum || value > maximum)
|
|
5
|
+
throw new RangeError(`invalid ${label}`);
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
function identity(scope) {
|
|
9
|
+
for (const value of [scope.tenant, scope.session, scope.incarnation]) {
|
|
10
|
+
if (typeof value !== 'string' || !value.length || value.length > 128)
|
|
11
|
+
throw new TypeError('invalid page scope');
|
|
12
|
+
}
|
|
13
|
+
return Object.freeze({ ...scope });
|
|
14
|
+
}
|
|
15
|
+
function sameScope(a, b) {
|
|
16
|
+
return a.tenant === b.tenant && a.session === b.session && a.incarnation === b.incarnation;
|
|
17
|
+
}
|
|
18
|
+
function versionKey(version) {
|
|
19
|
+
integer(version.page, 0, 65535, 'page');
|
|
20
|
+
integer(version.fence, 1, Number.MAX_SAFE_INTEGER, 'page fence');
|
|
21
|
+
return `${version.page}/${version.fence}`;
|
|
22
|
+
}
|
|
23
|
+
/** Bounded, volatile reference owner. This class does not persist pages across process loss. */
|
|
24
|
+
export class MemoryPageOwner {
|
|
25
|
+
id;
|
|
26
|
+
#scope;
|
|
27
|
+
#maximum;
|
|
28
|
+
#versions = new Map();
|
|
29
|
+
#closed = new Map();
|
|
30
|
+
constructor(id, scope, maximumBytes) {
|
|
31
|
+
if (typeof id !== 'string' || !id.length || id.length > 128)
|
|
32
|
+
throw new TypeError('invalid page owner id');
|
|
33
|
+
this.id = id;
|
|
34
|
+
this.#scope = identity(scope);
|
|
35
|
+
this.#maximum = integer(maximumBytes, pageBytes, Number.MAX_SAFE_INTEGER, 'owner credit');
|
|
36
|
+
}
|
|
37
|
+
/** Bytes retained by immutable versions, including unpublished staged versions. */
|
|
38
|
+
get bytes() {
|
|
39
|
+
return this.#versions.size * pageBytes;
|
|
40
|
+
}
|
|
41
|
+
#check(scope, version) {
|
|
42
|
+
if (!sameScope(this.#scope, scope))
|
|
43
|
+
throw new Error('page owner scope mismatch');
|
|
44
|
+
return versionKey(version);
|
|
45
|
+
}
|
|
46
|
+
async read(scope, version) {
|
|
47
|
+
const bytes = this.#versions.get(this.#check(scope, version));
|
|
48
|
+
if (!bytes)
|
|
49
|
+
throw new Error('materialized page is missing');
|
|
50
|
+
return bytes.slice();
|
|
51
|
+
}
|
|
52
|
+
async stage(scope, version, bytes) {
|
|
53
|
+
const key = this.#check(scope, version);
|
|
54
|
+
if (bytes.length !== pageBytes)
|
|
55
|
+
throw new RangeError('invalid page payload size');
|
|
56
|
+
if (version.fence <= (this.#closed.get(version.page) ?? 0))
|
|
57
|
+
throw new Error('page version was fenced by discard');
|
|
58
|
+
const previous = this.#versions.get(key);
|
|
59
|
+
if (previous) {
|
|
60
|
+
if (bytes.some((value, index) => value !== previous[index]))
|
|
61
|
+
throw new Error('page version retry changed bytes');
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (this.bytes + pageBytes > this.#maximum)
|
|
65
|
+
throw new RangeError('page owner credit exhausted');
|
|
66
|
+
this.#versions.set(key, bytes.slice());
|
|
67
|
+
}
|
|
68
|
+
async discard(scope, version) {
|
|
69
|
+
const key = this.#check(scope, version);
|
|
70
|
+
this.#closed.set(version.page, Math.max(this.#closed.get(version.page) ?? 0, version.fence));
|
|
71
|
+
this.#versions.delete(key);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const residentState = new WeakMap();
|
|
75
|
+
/**
|
|
76
|
+
* Volatile manifest coordinator. Publication is atomic within this instance. Recovery requires a
|
|
77
|
+
* persisted manifest and decision log; this constructor only creates a fresh zero-filled arena.
|
|
78
|
+
* Exactly one trusted directory may issue fences for its scope. Restart with a new incarnation;
|
|
79
|
+
* reusing an old scope is unsupported and can collide with old versions and delayed requests.
|
|
80
|
+
*/
|
|
81
|
+
export class PageDirectory {
|
|
82
|
+
scope;
|
|
83
|
+
#owners;
|
|
84
|
+
#manifest;
|
|
85
|
+
#maximumPages;
|
|
86
|
+
#maximumLeases;
|
|
87
|
+
#maximumLeasePages;
|
|
88
|
+
#maximumResidentPages;
|
|
89
|
+
#maximumRetiredPages;
|
|
90
|
+
#milliseconds;
|
|
91
|
+
#clock;
|
|
92
|
+
#leases = new Map();
|
|
93
|
+
#locks = new Map();
|
|
94
|
+
#retired = new Map();
|
|
95
|
+
#fence = 0;
|
|
96
|
+
#resident = 0;
|
|
97
|
+
#retirementReservations = 0;
|
|
98
|
+
constructor(options) {
|
|
99
|
+
this.scope = identity(options.scope);
|
|
100
|
+
if (!options.owners.length ||
|
|
101
|
+
options.owners.length > 65536 ||
|
|
102
|
+
options.owners.some((owner) => typeof owner.id !== 'string' || !owner.id.length || owner.id.length > 128) ||
|
|
103
|
+
new Set(options.owners.map((owner) => owner.id)).size !== options.owners.length)
|
|
104
|
+
throw new TypeError('page owners must have distinct ids');
|
|
105
|
+
this.#owners = [...options.owners];
|
|
106
|
+
this.#maximumPages = integer(options.maximumPages ?? 65536, 0, 65536, 'maximum pages');
|
|
107
|
+
this.#maximumLeases = integer(options.maximumLeases ?? 64, 1, 65536, 'maximum leases');
|
|
108
|
+
this.#maximumLeasePages = integer(options.maximumLeasePages ?? 256, 1, 65536, 'lease pages');
|
|
109
|
+
this.#maximumResidentPages = integer(options.maximumResidentPages ?? 512, 1, 131072, 'resident pages');
|
|
110
|
+
this.#maximumRetiredPages = integer(options.maximumRetiredPages ?? 1024, 2, 131072, 'retired pages');
|
|
111
|
+
this.#milliseconds = integer(options.leaseMilliseconds ?? 30000, 1, Number.MAX_SAFE_INTEGER, 'lease duration');
|
|
112
|
+
this.#clock = options.clock ?? Date.now;
|
|
113
|
+
this.#manifest = Array.from({ length: integer(options.initialPages ?? 0, 0, this.#maximumPages, 'initial pages') }, () => null);
|
|
114
|
+
}
|
|
115
|
+
/** Directory metadata only; page contents live at owners or in active leases. */
|
|
116
|
+
get stats() {
|
|
117
|
+
this.#expire();
|
|
118
|
+
return {
|
|
119
|
+
pages: this.#manifest.length,
|
|
120
|
+
residentBytes: this.#resident * pageBytes,
|
|
121
|
+
leases: this.#leases.size,
|
|
122
|
+
retiredPages: this.#retired.size
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** Declares new zero pages without allocating page payloads. Returns the previous page count. */
|
|
126
|
+
grow(pages) {
|
|
127
|
+
integer(pages, 0, this.#maximumPages - this.#manifest.length, 'growth');
|
|
128
|
+
const previous = this.#manifest.length;
|
|
129
|
+
for (let i = 0; i < pages; i++)
|
|
130
|
+
this.#manifest.push(null);
|
|
131
|
+
return previous;
|
|
132
|
+
}
|
|
133
|
+
/** Returns immutable manifest metadata, never a page view. */
|
|
134
|
+
inspect(page) {
|
|
135
|
+
this.#page(page);
|
|
136
|
+
const ref = this.#manifest[page];
|
|
137
|
+
return { zero: ref === null, owner: this.#owner(page).id, fence: ref?.version.fence ?? 0 };
|
|
138
|
+
}
|
|
139
|
+
#page(page) {
|
|
140
|
+
integer(page, 0, this.#manifest.length - 1, 'page address');
|
|
141
|
+
}
|
|
142
|
+
#owner(page) {
|
|
143
|
+
return this.#owners[page % this.#owners.length];
|
|
144
|
+
}
|
|
145
|
+
#now() {
|
|
146
|
+
const now = this.#clock();
|
|
147
|
+
if (!Number.isFinite(now))
|
|
148
|
+
throw new Error('invalid page clock');
|
|
149
|
+
return now;
|
|
150
|
+
}
|
|
151
|
+
#release(record) {
|
|
152
|
+
if (!this.#leases.delete(record.lease))
|
|
153
|
+
return;
|
|
154
|
+
for (const page of record.pages) {
|
|
155
|
+
const locks = this.#locks.get(page);
|
|
156
|
+
locks.delete(record);
|
|
157
|
+
if (!locks.size)
|
|
158
|
+
this.#locks.delete(page);
|
|
159
|
+
}
|
|
160
|
+
this.#resident -= record.pages.length;
|
|
161
|
+
const state = residentState.get(record.lease);
|
|
162
|
+
state.released = true;
|
|
163
|
+
state.views.clear();
|
|
164
|
+
}
|
|
165
|
+
#expire() {
|
|
166
|
+
const now = this.#now();
|
|
167
|
+
for (const record of this.#leases.values()) {
|
|
168
|
+
if (!record.busy && record.expires <= now)
|
|
169
|
+
this.#release(record);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
#record(lease, busy = false) {
|
|
173
|
+
const record = this.#leases.get(lease);
|
|
174
|
+
if (!record || !sameScope(lease.scope, this.scope))
|
|
175
|
+
throw new Error('stale page lease');
|
|
176
|
+
if (!busy && record.busy)
|
|
177
|
+
throw new Error('page lease has an active operation');
|
|
178
|
+
if (record.expires <= this.#now())
|
|
179
|
+
throw new Error('page lease expired');
|
|
180
|
+
return record;
|
|
181
|
+
}
|
|
182
|
+
/** Acquires every page or none. Reads share locks; writes exclude all other leases. */
|
|
183
|
+
async acquire(request) {
|
|
184
|
+
this.#expire();
|
|
185
|
+
integer(request.read?.length ?? 0, 0, this.#maximumLeasePages, 'lease page count');
|
|
186
|
+
integer(request.write?.length ?? 0, 0, this.#maximumLeasePages, 'lease page count');
|
|
187
|
+
const writes = new Set(request.write ?? []), pages = [...new Set([...(request.read ?? []), ...writes])].sort((a, b) => a - b);
|
|
188
|
+
integer(pages.length, 1, this.#maximumLeasePages, 'lease page count');
|
|
189
|
+
for (const page of pages) {
|
|
190
|
+
this.#page(page);
|
|
191
|
+
for (const lock of this.#locks.get(page) ?? []) {
|
|
192
|
+
if (writes.has(page) || lock.writes.has(page))
|
|
193
|
+
throw new Error('page lease conflict');
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (this.#leases.size >= this.#maximumLeases ||
|
|
197
|
+
this.#resident + pages.length > this.#maximumResidentPages)
|
|
198
|
+
throw new RangeError('page lease credit exhausted');
|
|
199
|
+
if (this.#fence >= Number.MAX_SAFE_INTEGER)
|
|
200
|
+
throw new RangeError('page fence exhausted');
|
|
201
|
+
const fence = ++this.#fence, expires = this.#now() + this.#milliseconds;
|
|
202
|
+
const lease = new ResidentPageLease(this, this.scope, fence, expires, pages, writes);
|
|
203
|
+
const record = { lease, pages, writes, expires, busy: true };
|
|
204
|
+
this.#leases.set(lease, record);
|
|
205
|
+
this.#resident += pages.length;
|
|
206
|
+
for (const page of pages) {
|
|
207
|
+
const locks = this.#locks.get(page) ?? new Set();
|
|
208
|
+
locks.add(record);
|
|
209
|
+
this.#locks.set(page, locks);
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
for (const page of pages) {
|
|
213
|
+
const ref = this.#manifest[page];
|
|
214
|
+
const bytes = ref
|
|
215
|
+
? await ref.owner.read(this.scope, ref.version)
|
|
216
|
+
: new Uint8Array(pageBytes);
|
|
217
|
+
this.#record(lease, true);
|
|
218
|
+
if (bytes.length !== pageBytes)
|
|
219
|
+
throw new RangeError('invalid owner page payload');
|
|
220
|
+
residentState.get(lease).views.set(page, bytes.slice());
|
|
221
|
+
}
|
|
222
|
+
record.busy = false;
|
|
223
|
+
return lease;
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
this.#release(record);
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/** Releases local dirty pages without publishing them. */
|
|
231
|
+
abort(lease) {
|
|
232
|
+
const record = this.#leases.get(lease);
|
|
233
|
+
if (!record)
|
|
234
|
+
return;
|
|
235
|
+
if (record.busy)
|
|
236
|
+
throw new Error('page lease has an active operation');
|
|
237
|
+
this.#release(record);
|
|
238
|
+
}
|
|
239
|
+
/** Stages all dirty pages, validates the fence again, then publishes one manifest decision. */
|
|
240
|
+
async commit(lease) {
|
|
241
|
+
const record = this.#record(lease);
|
|
242
|
+
const writes = [...record.writes];
|
|
243
|
+
if (this.#resident + writes.length > this.#maximumResidentPages)
|
|
244
|
+
throw new RangeError('page snapshot credit exhausted');
|
|
245
|
+
if (this.#retired.size + this.#retirementReservations + 2 * writes.length >
|
|
246
|
+
this.#maximumRetiredPages)
|
|
247
|
+
throw new RangeError('page retirement credit exhausted');
|
|
248
|
+
const staged = writes.map((page) => ({
|
|
249
|
+
ref: { owner: this.#owner(page), version: Object.freeze({ page, fence: lease.fence }) },
|
|
250
|
+
bytes: lease.view(page).slice()
|
|
251
|
+
}));
|
|
252
|
+
record.busy = true;
|
|
253
|
+
this.#resident += writes.length;
|
|
254
|
+
this.#retirementReservations += 2 * writes.length;
|
|
255
|
+
let published = false;
|
|
256
|
+
try {
|
|
257
|
+
for (const { ref, bytes } of staged) {
|
|
258
|
+
await ref.owner.stage(this.scope, ref.version, bytes);
|
|
259
|
+
this.#record(lease, true);
|
|
260
|
+
}
|
|
261
|
+
this.#record(lease, true);
|
|
262
|
+
// no await between the final validation and the entire manifest publication
|
|
263
|
+
for (const { ref } of staged) {
|
|
264
|
+
const previous = this.#manifest[ref.version.page];
|
|
265
|
+
if (previous)
|
|
266
|
+
this.#retire(previous);
|
|
267
|
+
this.#manifest[ref.version.page] = ref;
|
|
268
|
+
}
|
|
269
|
+
published = true;
|
|
270
|
+
}
|
|
271
|
+
finally {
|
|
272
|
+
if (!published)
|
|
273
|
+
for (const { ref } of staged)
|
|
274
|
+
this.#retire(ref);
|
|
275
|
+
this.#resident -= writes.length;
|
|
276
|
+
this.#retirementReservations -= 2 * writes.length;
|
|
277
|
+
this.#release(record);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
#retire(ref) {
|
|
281
|
+
this.#retired.set(`${ref.owner.id}/${versionKey(ref.version)}`, ref);
|
|
282
|
+
}
|
|
283
|
+
/** Retries idempotent garbage deletion; failed owners retain credits and remain visible here. */
|
|
284
|
+
async collect() {
|
|
285
|
+
let discarded = 0;
|
|
286
|
+
for (const [key, ref] of [...this.#retired]) {
|
|
287
|
+
try {
|
|
288
|
+
await ref.owner.discard(this.scope, ref.version);
|
|
289
|
+
if (this.#retired.get(key) === ref)
|
|
290
|
+
this.#retired.delete(key);
|
|
291
|
+
discarded++;
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
// unavailable garbage stays charged until a later successful discard
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return { discarded, pending: this.#retired.size };
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Resident copies for one bounded region. Escaped views cannot be physically revoked; after release
|
|
302
|
+
* they are caller-owned and outside the directory's admission accounting. They never alias owners.
|
|
303
|
+
*/
|
|
304
|
+
export class ResidentPageLease {
|
|
305
|
+
scope;
|
|
306
|
+
fence;
|
|
307
|
+
expiresAt;
|
|
308
|
+
pages;
|
|
309
|
+
writable;
|
|
310
|
+
#directory;
|
|
311
|
+
constructor(directory, scope, fence, expires, pages, writes) {
|
|
312
|
+
this.#directory = directory;
|
|
313
|
+
this.scope = Object.freeze({ ...scope });
|
|
314
|
+
this.fence = fence;
|
|
315
|
+
this.expiresAt = expires;
|
|
316
|
+
this.pages = Object.freeze([...pages]);
|
|
317
|
+
this.writable = Object.freeze([...writes]);
|
|
318
|
+
residentState.set(this, { views: new Map(), released: false });
|
|
319
|
+
Object.freeze(this);
|
|
320
|
+
}
|
|
321
|
+
/** Returns the local region page. Read pages may be changed locally but are never written back. */
|
|
322
|
+
view(page) {
|
|
323
|
+
const state = residentState.get(this);
|
|
324
|
+
if (state.released)
|
|
325
|
+
throw new Error('page lease is released');
|
|
326
|
+
const bytes = state.views.get(page);
|
|
327
|
+
if (!bytes)
|
|
328
|
+
throw new RangeError('page is not resident in this lease');
|
|
329
|
+
return bytes;
|
|
330
|
+
}
|
|
331
|
+
/** Publishes writable pages together, or rejects with the previous manifest intact. */
|
|
332
|
+
commit() {
|
|
333
|
+
return this.#directory.commit(this);
|
|
334
|
+
}
|
|
335
|
+
/** Drops resident dirty bytes without publication. */
|
|
336
|
+
abort() {
|
|
337
|
+
this.#directory.abort(this);
|
|
338
|
+
}
|
|
339
|
+
}
|
package/dist/remote.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/** Portable transport values. References and executable objects do not cross this boundary. */
|
|
2
|
+
export declare function encodeValue(value: unknown, depth?: number): unknown;
|
|
3
|
+
/** Inverse of `encodeValue`; rejects anything that is not a transport value. */
|
|
4
|
+
export declare function decodeValue(value: unknown, depth?: number): any;
|
|
5
|
+
/** Lowercase hex text for a byte payload. */
|
|
6
|
+
export declare function encodeBytes(bytes: Uint8Array): string;
|
|
7
|
+
/** Inverse of `encodeBytes`; rejects odd lengths and non-hex text. */
|
|
8
|
+
export declare function decodeBytes(value: unknown): Uint8Array;
|
|
9
|
+
/** The host's execution report, decoded. Fields match `ExecutionReport`. */
|
|
10
|
+
export interface RemoteReport {
|
|
11
|
+
/** Return values; i64 results arrive as bigint and void as undefined. */
|
|
12
|
+
values: unknown[];
|
|
13
|
+
/** Workers that executed regions. */
|
|
14
|
+
workers: number;
|
|
15
|
+
/** Regions dispatched. */
|
|
16
|
+
regions: number;
|
|
17
|
+
/** Regions replayed after a conflict. */
|
|
18
|
+
replayed: number;
|
|
19
|
+
/** Regions that fell back to native execution at the host. */
|
|
20
|
+
fallbacks: number;
|
|
21
|
+
/** How the host ran the call. */
|
|
22
|
+
mode: 'serial' | 'speculative' | 'native-shared';
|
|
23
|
+
/** Set when control-flow extraction produced the regions. */
|
|
24
|
+
extraction?: 'control-flow';
|
|
25
|
+
/** Whether regions read the shared allocation or a copied snapshot. */
|
|
26
|
+
memoryMode?: 'shared' | 'snapshot';
|
|
27
|
+
/** Bytes copied for snapshot execution. */
|
|
28
|
+
snapshotBytes?: number;
|
|
29
|
+
/** Why the run stayed serial or left the fast path. */
|
|
30
|
+
reason?: string;
|
|
31
|
+
/** Present when the host profiles runs. */
|
|
32
|
+
profile?: unknown;
|
|
33
|
+
}
|
|
34
|
+
/** Connection options for `RemoteFolio.open`. */
|
|
35
|
+
export interface RemoteOptions {
|
|
36
|
+
/** A service binding's fetch, or an ordinary fetch implementation. */
|
|
37
|
+
fetch?: (input: Request) => Promise<Response>;
|
|
38
|
+
/** Sent with every request, usually `authorization`. */
|
|
39
|
+
headers?: HeadersInit;
|
|
40
|
+
/** Largest response body the client reads; defaults to 2 MiB. */
|
|
41
|
+
maximumResponseBytes?: number;
|
|
42
|
+
}
|
|
43
|
+
/** An error response from the host. */
|
|
44
|
+
export declare class RemoteError extends Error {
|
|
45
|
+
/** HTTP status of the response. */
|
|
46
|
+
readonly status: number;
|
|
47
|
+
/** The host's error category. */
|
|
48
|
+
readonly kind: string;
|
|
49
|
+
constructor(message: string, status: number, kind: string);
|
|
50
|
+
}
|
|
51
|
+
/** A failed response can be retried explicitly with the exact original request. */
|
|
52
|
+
export declare class UncertainDelivery extends Error {
|
|
53
|
+
/** Sequence number of the request whose response was lost. */
|
|
54
|
+
readonly sequence: number;
|
|
55
|
+
constructor(sequence: number, options?: ErrorOptions);
|
|
56
|
+
}
|
|
57
|
+
/** Connects to a registered artifact. Compilation and shared memory stay at the host. */
|
|
58
|
+
export declare class RemoteFolio {
|
|
59
|
+
#private;
|
|
60
|
+
/** Session details from the host: its capability, worker thread ids and sharing capabilities. */
|
|
61
|
+
readonly info: {
|
|
62
|
+
capability: string;
|
|
63
|
+
threadIds: number[];
|
|
64
|
+
capabilities: unknown;
|
|
65
|
+
};
|
|
66
|
+
private constructor();
|
|
67
|
+
/** Opens a session on a registered artifact; `workers` asks for a group width within host limits. */
|
|
68
|
+
static open(url: string | URL, artifact: string, options?: RemoteOptions & {
|
|
69
|
+
workers?: number;
|
|
70
|
+
}): Promise<RemoteFolio>;
|
|
71
|
+
/** Resends the exact uncertain request; the host returns its cached result without repeating effects. */
|
|
72
|
+
retryPending(): Promise<any>;
|
|
73
|
+
/** Runs one allowlisted export in order on the host. */
|
|
74
|
+
run(entry: string, args?: readonly unknown[], options?: {
|
|
75
|
+
parallel?: 'auto' | 'off' | 'speculate';
|
|
76
|
+
maximumWorkers?: number;
|
|
77
|
+
}): Promise<RemoteReport>;
|
|
78
|
+
/** Reads bytes from the host's memory, bounded by the host's read limit. */
|
|
79
|
+
read(offset: number, length: number, memory?: string): Promise<Uint8Array>;
|
|
80
|
+
/** Writes bytes into the host's memory, bounded by the host's write limit. */
|
|
81
|
+
write(offset: number, bytes: Uint8Array, memory?: string): Promise<void>;
|
|
82
|
+
/** Ends the session; the host joins its workers before releasing the slot. */
|
|
83
|
+
close(): Promise<void>;
|
|
84
|
+
/** `await using` support; equivalent to `close()`. */
|
|
85
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
86
|
+
}
|