@waffle-os/runtime 0.0.154 → 0.0.156
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/core/filesystem.ts +22 -0
- package/core/wasi.ts +78 -5
- package/dist/core/filesystem.js +1 -0
- package/dist/core/wasi.js +71 -6
- package/dist/fs/backend.js +22 -1
- package/dist/fs/client.js +11 -3
- package/dist/fs/cold-read-cache.js +23 -0
- package/dist/fs/guest-watch.js +267 -94
- package/dist/fs/indexeddb-chunks.js +88 -11
- package/dist/fs/indexeddb.js +78 -14
- package/dist/fs/memory.js +17 -1
- package/dist/fs/multitab-backend.js +198 -64
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +19 -1
- package/dist/fs/service.js +30 -1
- package/dist/fs/shared.js +4 -0
- package/dist/fs/storage.js +107 -8
- package/dist/fs/watch-service.js +97 -2
- package/dist/fs/watch-subscriptions.js +121 -8
- package/dist/proc/intrinsics/core.js +5 -0
- package/dist/proc/intrinsics.js +1 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +27 -1
- package/fs/client.ts +11 -0
- package/fs/cold-read-cache.ts +38 -0
- package/fs/guest-watch.ts +298 -102
- package/fs/indexeddb-chunks.ts +103 -9
- package/fs/indexeddb.ts +98 -11
- package/fs/memory.ts +18 -0
- package/fs/multitab-backend.ts +273 -72
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +38 -1
- package/fs/service.ts +28 -0
- package/fs/shared.ts +4 -0
- package/fs/storage.ts +128 -10
- package/fs/watch-service.ts +110 -3
- package/fs/watch-subscriptions.ts +115 -7
- package/package.json +1 -1
- package/proc/intrinsics/core.ts +7 -0
- package/proc/intrinsics.ts +1 -0
- package/testing/indexeddb-storage-browser.ts +129 -0
- package/tooling/program/program-transformer.json +1 -1
package/fs/client.ts
CHANGED
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
isFileTime,
|
|
42
42
|
permissionsFor,
|
|
43
43
|
type FileTime,
|
|
44
|
+
type ProbeKind,
|
|
44
45
|
type WatchFileSystem,
|
|
45
46
|
} from "../core/filesystem.js";
|
|
46
47
|
import {
|
|
@@ -210,6 +211,7 @@ const PLAIN_REPLIES = new SetConstructor([
|
|
|
210
211
|
"setMode",
|
|
211
212
|
"read",
|
|
212
213
|
"readlink",
|
|
214
|
+
"probe",
|
|
213
215
|
]);
|
|
214
216
|
export class FilesystemCompletion {
|
|
215
217
|
static {
|
|
@@ -501,6 +503,7 @@ function clientWatches(
|
|
|
501
503
|
cells: () => Int32Array,
|
|
502
504
|
revoked: () => boolean,
|
|
503
505
|
remote: () => boolean,
|
|
506
|
+
canDrain: () => boolean,
|
|
504
507
|
call: (operation: string, args: unknown[]) => any,
|
|
505
508
|
): WatchFileSystem {
|
|
506
509
|
return {
|
|
@@ -513,8 +516,12 @@ function clientWatches(
|
|
|
513
516
|
get remote() {
|
|
514
517
|
return remote();
|
|
515
518
|
},
|
|
519
|
+
get canDrain() {
|
|
520
|
+
return canDrain();
|
|
521
|
+
},
|
|
516
522
|
open: (handle, recursive) => call("watchOpen", [handle, recursive]),
|
|
517
523
|
read: (id) => call("watchRead", [id]),
|
|
524
|
+
drain: () => call("watchDrain", []),
|
|
518
525
|
close: (id) => {
|
|
519
526
|
call("watchClose", [id]);
|
|
520
527
|
},
|
|
@@ -648,6 +655,7 @@ export class FilesystemClient implements FileSystem {
|
|
|
648
655
|
() => this.shared.watch.cells,
|
|
649
656
|
this.revoked,
|
|
650
657
|
() => this.shared.isDirectDisabled(),
|
|
658
|
+
() => !this.shared.isWatchDrainDisabled(),
|
|
651
659
|
(operation, args) => this.call(operation, args),
|
|
652
660
|
);
|
|
653
661
|
connection.port.onmessage = ({ data }) => {
|
|
@@ -2606,6 +2614,9 @@ export class FilesystemClient implements FileSystem {
|
|
|
2606
2614
|
return errno(error);
|
|
2607
2615
|
}
|
|
2608
2616
|
}
|
|
2617
|
+
probe(directory: FileHandle, paths: readonly string[], kind: ProbeKind, rooted = false): number {
|
|
2618
|
+
return this.call("probe", [directory, paths, kind, rooted]);
|
|
2619
|
+
}
|
|
2609
2620
|
readlink(handle: FileHandle): string {
|
|
2610
2621
|
this.retain(handle);
|
|
2611
2622
|
try {
|
package/fs/cold-read-cache.ts
CHANGED
|
@@ -5,6 +5,12 @@ const COLD_READ_CACHE_BYTES = 32 * 1024 * 1024;
|
|
|
5
5
|
/** Charged per page on top of its bytes: an estimate of its map entry, key string and typed
|
|
6
6
|
* array and buffer headers. It keeps many tiny files from exceeding the bound in real heap. */
|
|
7
7
|
export const COLD_PAGE_OVERHEAD = 256;
|
|
8
|
+
/** Pages one storage read fetches for a sequential cold scan: 1 MiB, so a 13 MB binary costs
|
|
9
|
+
* 14 storage reads instead of 210, while its transport still moves one page per request. */
|
|
10
|
+
const READAHEAD_PAGES = 16;
|
|
11
|
+
export const READAHEAD_BYTES = READAHEAD_PAGES * PAGE;
|
|
12
|
+
/** Payloads whose last read end is remembered for sequential detection. */
|
|
13
|
+
const STREAMS = 64;
|
|
8
14
|
|
|
9
15
|
/** Verified bytes of cold payloads, kept by whole aligned page so a file read again while it
|
|
10
16
|
* stays unmodified skips storage and its checksums. A payload key names one immutable stored
|
|
@@ -13,8 +19,39 @@ export const COLD_PAGE_OVERHEAD = 256;
|
|
|
13
19
|
* cache owns private copies, so a caller may transfer or modify what it receives. */
|
|
14
20
|
export class ColdReadCache {
|
|
15
21
|
private readonly pages = new Map<string, Uint8Array>();
|
|
22
|
+
/** Where the last read of each recent payload ended. */
|
|
23
|
+
private readonly ends = new Map<string, number>();
|
|
16
24
|
private bytes = 0;
|
|
17
25
|
constructor(private readonly limit = COLD_READ_CACHE_BYTES) {}
|
|
26
|
+
/** Aligned storage range for a miss at `[offset, offset + count)`. A read that starts exactly
|
|
27
|
+
* where the previous read of this payload ended is a sequential scan, so it fetches a
|
|
28
|
+
* page-aligned readahead window (as Linux page-cache readahead does) up to `READAHEAD_BYTES`
|
|
29
|
+
* and half the cache budget; any other read fetches only what it needs. Aligning the start to a
|
|
30
|
+
* page boundary lets small sequential reads populate their current page too instead of
|
|
31
|
+
* repeatedly missing and refetching the window. */
|
|
32
|
+
readWindow(
|
|
33
|
+
key: string,
|
|
34
|
+
offset: number,
|
|
35
|
+
count: number,
|
|
36
|
+
size: number,
|
|
37
|
+
): { offset: number; length: number } {
|
|
38
|
+
if (this.ends.get(key) !== offset) return { offset, length: count };
|
|
39
|
+
const windowPages = Math.min(
|
|
40
|
+
READAHEAD_PAGES,
|
|
41
|
+
Math.floor(this.limit / 2 / (PAGE + COLD_PAGE_OVERHEAD)),
|
|
42
|
+
);
|
|
43
|
+
if (windowPages < 1) return { offset, length: count };
|
|
44
|
+
const start = Math.floor(offset / PAGE) * PAGE;
|
|
45
|
+
const end = Math.min(size, start + windowPages * PAGE);
|
|
46
|
+
if (end < offset + count) return { offset, length: count };
|
|
47
|
+
return { offset: start, length: end - start };
|
|
48
|
+
}
|
|
49
|
+
/** Record where a read of this payload ended, hit or miss. */
|
|
50
|
+
noteRead(key: string, end: number): void {
|
|
51
|
+
this.ends.delete(key);
|
|
52
|
+
if (this.ends.size >= STREAMS) this.ends.delete(this.ends.keys().next().value!);
|
|
53
|
+
this.ends.set(key, end);
|
|
54
|
+
}
|
|
18
55
|
/** A copy of the range, or undefined unless every page it touches is cached. */
|
|
19
56
|
read(key: string, offset: number, length: number): Uint8Array | undefined {
|
|
20
57
|
const first = Math.floor(offset / PAGE);
|
|
@@ -54,6 +91,7 @@ export class ColdReadCache {
|
|
|
54
91
|
}
|
|
55
92
|
clear(): void {
|
|
56
93
|
this.pages.clear();
|
|
94
|
+
this.ends.clear();
|
|
57
95
|
this.bytes = 0;
|
|
58
96
|
}
|
|
59
97
|
private remove(name: string): void {
|
package/fs/guest-watch.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
FilesystemError,
|
|
3
|
+
type FileHandle,
|
|
4
|
+
type WatchDrainResult,
|
|
5
|
+
type WatchFileSystem,
|
|
6
|
+
} from "../core/filesystem.js";
|
|
7
|
+
import type { WatchBatch } from "./watch.js";
|
|
3
8
|
import {
|
|
4
9
|
arrayAppend,
|
|
5
10
|
atomicsLoad,
|
|
6
11
|
MapConstructor,
|
|
7
12
|
mapDelete,
|
|
13
|
+
mapFirst,
|
|
8
14
|
mapForEach,
|
|
9
15
|
mapGet,
|
|
10
16
|
mapSet,
|
|
@@ -15,6 +21,7 @@ import {
|
|
|
15
21
|
const EAGAIN = 6;
|
|
16
22
|
const EBADF = 8;
|
|
17
23
|
const ECANCELED = 11;
|
|
24
|
+
const ENOTSUP = 58;
|
|
18
25
|
const CHANGE = 1;
|
|
19
26
|
const RENAME = 2;
|
|
20
27
|
const OVERFLOW = 3;
|
|
@@ -33,179 +40,368 @@ export interface GuestWatchReadiness {
|
|
|
33
40
|
}
|
|
34
41
|
|
|
35
42
|
export type GuestWatchRead =
|
|
36
|
-
/** Whole records in order; no records means end of stream. */
|
|
37
43
|
| { readonly kind: "records"; readonly records: readonly Uint8Array[]; readonly length: number }
|
|
38
|
-
/** The first pending record needs `needed` bytes. Nothing was consumed. */
|
|
39
44
|
| { readonly kind: "short"; readonly needed: number }
|
|
40
45
|
| { readonly kind: "error"; readonly errno: number };
|
|
41
46
|
|
|
47
|
+
export type GuestWatchNext =
|
|
48
|
+
| {
|
|
49
|
+
readonly kind: "records";
|
|
50
|
+
readonly id: number;
|
|
51
|
+
readonly records: readonly Uint8Array[];
|
|
52
|
+
readonly length: number;
|
|
53
|
+
}
|
|
54
|
+
| { readonly kind: "short"; readonly id: number; readonly needed: number }
|
|
55
|
+
| { readonly kind: "watch_error"; readonly id: number; readonly errno: number }
|
|
56
|
+
| { readonly kind: "error"; readonly errno: number };
|
|
57
|
+
|
|
42
58
|
interface Watch {
|
|
59
|
+
readonly id: number;
|
|
43
60
|
readonly service: number;
|
|
44
|
-
/** cells[3] before this watch's latest service read, or before its open RPC. */
|
|
45
61
|
observed: number;
|
|
46
|
-
/** Encoded records of at most one service batch, consumed from `head`. */
|
|
47
62
|
queue: Uint8Array[];
|
|
48
63
|
head: number;
|
|
49
64
|
ended: boolean;
|
|
65
|
+
errno: number;
|
|
50
66
|
}
|
|
51
67
|
|
|
52
68
|
const END: GuestWatchRead = { kind: "records", records: [], length: 0 };
|
|
53
69
|
|
|
54
|
-
function failure(errno: number):
|
|
70
|
+
function failure(errno: number): { readonly kind: "error"; readonly errno: number } {
|
|
55
71
|
return { kind: "error", errno };
|
|
56
72
|
}
|
|
57
73
|
|
|
74
|
+
function watchError(id: number, errno: number): GuestWatchNext {
|
|
75
|
+
return { kind: "watch_error", id, errno };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function nextOf(id: number, result: GuestWatchRead): GuestWatchNext {
|
|
79
|
+
return result.kind === "error" ? watchError(id, result.errno) : { ...result, id };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function rotate(map: Map<number, Watch>, watch: Watch): void {
|
|
83
|
+
mapDelete(map, watch.id);
|
|
84
|
+
mapSet(map, watch.id, watch);
|
|
85
|
+
}
|
|
86
|
+
|
|
58
87
|
/** Process-local watch ids over the opener's filesystem session. Ids are never reused. */
|
|
59
88
|
export class GuestWatchTable implements GuestWatchReadiness {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
89
|
+
#fs: WatchFileSystem;
|
|
90
|
+
#watches!: Map<number, Watch>;
|
|
91
|
+
#byService!: Map<number, Watch>;
|
|
92
|
+
#buffered!: Map<number, Watch>;
|
|
93
|
+
#errored!: Map<number, Watch>;
|
|
94
|
+
#ended!: Map<number, Watch>;
|
|
95
|
+
#nextId = 1;
|
|
96
|
+
#revocationNotified = false;
|
|
97
|
+
#activeCount = 0;
|
|
98
|
+
#observedGen = 0;
|
|
99
|
+
#observedCount = 0;
|
|
100
|
+
#tableObserved: number | undefined;
|
|
101
|
+
#drainMore = false;
|
|
102
|
+
#noDrain = false;
|
|
103
|
+
#flip = false;
|
|
63
104
|
|
|
64
|
-
constructor(
|
|
105
|
+
constructor(fs: WatchFileSystem) {
|
|
106
|
+
this.#fs = fs;
|
|
107
|
+
this.drop();
|
|
108
|
+
}
|
|
65
109
|
|
|
66
110
|
get word(): Int32Array {
|
|
67
|
-
return this
|
|
111
|
+
return this.#fs.cells;
|
|
68
112
|
}
|
|
69
113
|
|
|
70
114
|
get active(): boolean {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return
|
|
115
|
+
return this.#activeCount > 0;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
#useDrain(): boolean {
|
|
119
|
+
if (typeof this.#fs.drain !== "function" || this.#noDrain) return false;
|
|
120
|
+
if (this.#fs.canDrain === false) {
|
|
121
|
+
this.#tableObserved = undefined;
|
|
122
|
+
this.#drainMore = false;
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#allObserved(generation: number): boolean {
|
|
129
|
+
return (
|
|
130
|
+
(this.#observedCount === this.#activeCount && this.#observedGen === generation) ||
|
|
131
|
+
(this.#useDrain() && !this.#drainMore && this.#tableObserved === generation)
|
|
132
|
+
);
|
|
76
133
|
}
|
|
77
134
|
|
|
78
135
|
pending(): boolean {
|
|
79
|
-
if (this
|
|
80
|
-
|
|
136
|
+
if (this.#fs.revoked)
|
|
137
|
+
return !!this.#fs.remote && !this.#revocationNotified && this.#activeCount > 0;
|
|
138
|
+
return (
|
|
139
|
+
this.#activeCount > 0 &&
|
|
140
|
+
(mapSize(this.#buffered) > 0 ||
|
|
141
|
+
mapSize(this.#errored) > 0 ||
|
|
142
|
+
!this.#allObserved(atomicsLoad(this.#fs.cells, 3)))
|
|
143
|
+
);
|
|
81
144
|
}
|
|
82
145
|
|
|
83
146
|
/** A read may return records, end or an error; never consumes watch state. */
|
|
84
147
|
ready(): boolean {
|
|
85
|
-
if (!mapSize(this
|
|
86
|
-
return
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
148
|
+
if (!mapSize(this.#watches)) return false;
|
|
149
|
+
return (
|
|
150
|
+
this.#fs.revoked ||
|
|
151
|
+
mapSize(this.#buffered) > 0 ||
|
|
152
|
+
mapSize(this.#errored) > 0 ||
|
|
153
|
+
mapSize(this.#ended) > 0 ||
|
|
154
|
+
(this.#activeCount > 0 && !this.#allObserved(atomicsLoad(this.#fs.cells, 3)))
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
#setObserved(watch: Watch, generation: number): void {
|
|
159
|
+
if (watch.observed === generation) return;
|
|
160
|
+
if (!watch.ended) {
|
|
161
|
+
if (watch.observed === this.#observedGen) this.#observedCount--;
|
|
162
|
+
if (generation !== this.#observedGen) {
|
|
163
|
+
this.#observedGen = generation;
|
|
164
|
+
this.#observedCount = 1;
|
|
165
|
+
} else this.#observedCount++;
|
|
166
|
+
}
|
|
167
|
+
watch.observed = generation;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
#markEnded(watch: Watch): void {
|
|
171
|
+
if (watch.ended) return;
|
|
172
|
+
watch.ended = true;
|
|
173
|
+
this.#activeCount--;
|
|
174
|
+
if (watch.observed === this.#observedGen) this.#observedCount--;
|
|
175
|
+
watch.errno = 0;
|
|
176
|
+
mapDelete(this.#buffered, watch.id);
|
|
177
|
+
mapDelete(this.#errored, watch.id);
|
|
178
|
+
mapSet(this.#ended, watch.id, watch);
|
|
101
179
|
}
|
|
102
180
|
|
|
103
181
|
/** Throws FilesystemError from the service. */
|
|
104
182
|
open(handle: FileHandle, recursive: boolean): number {
|
|
105
|
-
if (this
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
mapSet(this
|
|
183
|
+
if (this.#nextId > LAST_ID) throw new FilesystemError(EAGAIN, "watch ids exhausted");
|
|
184
|
+
const observed = atomicsLoad(this.#fs.cells, 3);
|
|
185
|
+
const service = this.#fs.open(handle, recursive);
|
|
186
|
+
const id = this.#nextId++;
|
|
187
|
+
const watch: Watch = { id, service, observed: NaN, queue: [], head: 0, ended: false, errno: 0 };
|
|
188
|
+
mapSet(this.#watches, id, watch);
|
|
189
|
+
mapSet(this.#byService, service, watch);
|
|
190
|
+
this.#activeCount++;
|
|
191
|
+
this.#setObserved(watch, observed);
|
|
112
192
|
return id;
|
|
113
193
|
}
|
|
114
194
|
|
|
115
|
-
/** Throws FilesystemError when the service read fails. */
|
|
195
|
+
/** Scoped to `id`; never drains other watches. Throws FilesystemError when the service read fails. */
|
|
116
196
|
read(id: number, capacity: number): GuestWatchRead {
|
|
117
|
-
const watch = mapGet(this
|
|
197
|
+
const watch = mapGet(this.#watches, id);
|
|
118
198
|
if (!watch) return failure(EBADF);
|
|
119
199
|
if (watch.ended) return END;
|
|
120
|
-
if (watch.head
|
|
121
|
-
|
|
122
|
-
|
|
200
|
+
if (watch.head < watch.queue.length) return this.#takeFrom(watch, capacity);
|
|
201
|
+
const force = watch.errno !== 0;
|
|
202
|
+
if (force) {
|
|
203
|
+
watch.errno = 0;
|
|
204
|
+
mapDelete(this.#errored, id);
|
|
123
205
|
}
|
|
124
|
-
return
|
|
206
|
+
return this.#refillSingle(watch, force) ?? this.#takeFrom(watch, capacity);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Read the next ready watch without scanning idle ones. Returns EAGAIN when nothing is ready. */
|
|
210
|
+
readNext(capacity: number): GuestWatchNext {
|
|
211
|
+
if (!mapSize(this.#watches)) return failure(EAGAIN);
|
|
212
|
+
if (this.#fs.revoked) {
|
|
213
|
+
if (this.#fs.remote) this.#revocationNotified = true;
|
|
214
|
+
const first = mapFirst(this.#watches)!;
|
|
215
|
+
rotate(this.#watches, first);
|
|
216
|
+
return watchError(first.id, ECANCELED);
|
|
217
|
+
}
|
|
218
|
+
if (
|
|
219
|
+
this.#useDrain() &&
|
|
220
|
+
(mapSize(this.#buffered) || mapSize(this.#errored) || !this.#drainService())
|
|
221
|
+
)
|
|
222
|
+
return this.#takeNextReady(capacity);
|
|
223
|
+
return this.#readNextFallback(capacity);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
#takeNextReady(capacity: number): GuestWatchNext {
|
|
227
|
+
const buffered = mapFirst(this.#buffered);
|
|
228
|
+
if (buffered) {
|
|
229
|
+
const read = this.#takeFrom(buffered, capacity);
|
|
230
|
+
if (buffered.head < buffered.queue.length) rotate(this.#buffered, buffered);
|
|
231
|
+
return nextOf(buffered.id, read);
|
|
232
|
+
}
|
|
233
|
+
const ended = mapFirst(this.#ended);
|
|
234
|
+
const errored = mapFirst(this.#errored);
|
|
235
|
+
const pick = this.#flip ? (ended ?? errored) : (errored ?? ended);
|
|
236
|
+
if (!pick) return failure(EAGAIN);
|
|
237
|
+
this.#flip = !pick.ended;
|
|
238
|
+
if (pick.ended) {
|
|
239
|
+
rotate(this.#ended, pick);
|
|
240
|
+
return nextOf(pick.id, END);
|
|
241
|
+
}
|
|
242
|
+
const errno = pick.errno;
|
|
243
|
+
pick.errno = 0;
|
|
244
|
+
if (pick.observed === this.#observedGen) this.#observedCount--;
|
|
245
|
+
pick.observed = NaN;
|
|
246
|
+
this.#drainMore = true;
|
|
247
|
+
mapDelete(this.#errored, pick.id);
|
|
248
|
+
return watchError(pick.id, errno);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
#readNextFallback(capacity: number): GuestWatchNext {
|
|
252
|
+
let result: GuestWatchNext | undefined;
|
|
253
|
+
let reported: Watch | undefined;
|
|
254
|
+
mapForEach(this.#watches, (watch) => {
|
|
255
|
+
if (result) return;
|
|
256
|
+
try {
|
|
257
|
+
const read = this.read(watch.id, capacity);
|
|
258
|
+
if (read.kind !== "error" || read.errno !== EAGAIN) {
|
|
259
|
+
result = nextOf(watch.id, read);
|
|
260
|
+
reported = watch;
|
|
261
|
+
}
|
|
262
|
+
} catch (error) {
|
|
263
|
+
if (!(error instanceof FilesystemError)) throw error;
|
|
264
|
+
result = watchError(watch.id, error.errno);
|
|
265
|
+
reported = watch;
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
if (reported) rotate(this.#watches, reported);
|
|
269
|
+
return result ?? failure(EAGAIN);
|
|
125
270
|
}
|
|
126
271
|
|
|
127
272
|
/** Throws FilesystemError when the service close fails; the id then stays open for a retry. */
|
|
128
273
|
close(id: number): number {
|
|
129
|
-
const watch = mapGet(this
|
|
274
|
+
const watch = mapGet(this.#watches, id);
|
|
130
275
|
if (!watch) return EBADF;
|
|
131
|
-
if (!this
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
276
|
+
if (!this.#fs.revoked) {
|
|
277
|
+
try {
|
|
278
|
+
this.#fs.close(watch.service);
|
|
279
|
+
} catch (error) {
|
|
280
|
+
if (!(error instanceof FilesystemError) || error.errno !== ECANCELED) throw error;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (!watch.ended) {
|
|
284
|
+
this.#activeCount--;
|
|
285
|
+
if (watch.observed === this.#observedGen) this.#observedCount--;
|
|
286
|
+
}
|
|
287
|
+
mapDelete(this.#buffered, id);
|
|
288
|
+
mapDelete(this.#errored, id);
|
|
289
|
+
mapDelete(this.#ended, id);
|
|
290
|
+
mapDelete(this.#watches, id);
|
|
291
|
+
mapDelete(this.#byService, watch.service);
|
|
292
|
+
if (!mapSize(this.#watches)) {
|
|
293
|
+
this.#tableObserved = undefined;
|
|
294
|
+
this.#drainMore = false;
|
|
142
295
|
}
|
|
296
|
+
return 0;
|
|
143
297
|
}
|
|
144
298
|
|
|
145
299
|
/** Forget every id without service calls; the session's close releases subscriptions. */
|
|
146
300
|
drop(): void {
|
|
147
|
-
this
|
|
301
|
+
this.#watches = new MapConstructor();
|
|
302
|
+
this.#byService = new MapConstructor();
|
|
303
|
+
this.#buffered = new MapConstructor();
|
|
304
|
+
this.#errored = new MapConstructor();
|
|
305
|
+
this.#ended = new MapConstructor();
|
|
306
|
+
this.#activeCount = this.#observedCount = 0;
|
|
307
|
+
this.#tableObserved = undefined;
|
|
308
|
+
this.#drainMore = false;
|
|
148
309
|
}
|
|
149
310
|
|
|
150
|
-
|
|
151
|
-
if (
|
|
152
|
-
|
|
153
|
-
return
|
|
311
|
+
#applyBatch(watch: Watch, batch: WatchBatch, generation: number): boolean {
|
|
312
|
+
if (batch.kind === "closed") {
|
|
313
|
+
this.#markEnded(watch);
|
|
314
|
+
return true;
|
|
154
315
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
316
|
+
this.#setObserved(watch, generation);
|
|
317
|
+
watch.queue = encodeBatch(batch);
|
|
318
|
+
watch.head = 0;
|
|
319
|
+
if (watch.queue.length) mapSet(this.#buffered, watch.id, watch);
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Drain ready watches in one service RPC. Returns true if drain is unsupported. */
|
|
324
|
+
#drainService(): boolean {
|
|
325
|
+
const generation = atomicsLoad(this.#fs.cells, 3);
|
|
326
|
+
if (!this.#drainMore && this.#allObserved(generation)) return false;
|
|
327
|
+
const previous = this.#tableObserved;
|
|
328
|
+
this.#tableObserved = generation;
|
|
329
|
+
let drained: WatchDrainResult;
|
|
162
330
|
try {
|
|
163
|
-
|
|
331
|
+
drained = this.#fs.drain!();
|
|
164
332
|
} catch (error) {
|
|
165
|
-
|
|
333
|
+
if (error instanceof FilesystemError && error.errno === ENOTSUP) {
|
|
334
|
+
this.#tableObserved = undefined;
|
|
335
|
+
this.#drainMore = false;
|
|
336
|
+
if (this.#fs.canDrain === undefined) this.#noDrain = true;
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
this.#tableObserved = previous;
|
|
166
340
|
throw error;
|
|
167
341
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
342
|
+
this.#drainMore = drained.more;
|
|
343
|
+
for (let index = 0; index < drained.entries.length; index++) {
|
|
344
|
+
const entry = drained.entries[index]!;
|
|
345
|
+
const watch = mapGet(this.#byService, entry.id);
|
|
346
|
+
if (!watch || watch.ended) continue;
|
|
347
|
+
if ("errno" in entry) {
|
|
348
|
+
watch.errno = entry.errno;
|
|
349
|
+
mapSet(this.#errored, watch.id, watch);
|
|
350
|
+
} else {
|
|
351
|
+
this.#applyBatch(watch, entry.batch, generation);
|
|
352
|
+
}
|
|
171
353
|
}
|
|
172
|
-
|
|
173
|
-
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
#refillSingle(watch: Watch, force: boolean): GuestWatchRead | undefined {
|
|
358
|
+
if (this.#fs.revoked) {
|
|
359
|
+
if (this.#fs.remote) this.#revocationNotified = true;
|
|
360
|
+
return failure(ECANCELED);
|
|
361
|
+
}
|
|
362
|
+
const generation = atomicsLoad(this.#fs.cells, 3);
|
|
363
|
+
if (
|
|
364
|
+
!force &&
|
|
365
|
+
!this.#fs.remote &&
|
|
366
|
+
(generation === watch.observed ||
|
|
367
|
+
(this.#useDrain() && !this.#drainMore && generation === this.#tableObserved))
|
|
368
|
+
)
|
|
369
|
+
return failure(EAGAIN);
|
|
370
|
+
if (this.#applyBatch(watch, this.#fs.read(watch.service), generation)) return END;
|
|
174
371
|
if (!watch.queue.length) return failure(EAGAIN);
|
|
175
372
|
}
|
|
176
|
-
}
|
|
177
373
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
374
|
+
#takeFrom(watch: Watch, capacity: number): GuestWatchRead {
|
|
375
|
+
const records: Uint8Array[] = [];
|
|
376
|
+
let length = 0;
|
|
377
|
+
while (watch.head < watch.queue.length) {
|
|
378
|
+
const record = watch.queue[watch.head]!;
|
|
379
|
+
if (length + record.length > capacity) break;
|
|
380
|
+
arrayAppend(records, record);
|
|
381
|
+
length += record.length;
|
|
382
|
+
watch.head++;
|
|
383
|
+
if (record[0] === OVERFLOW) this.#markEnded(watch);
|
|
384
|
+
}
|
|
385
|
+
if (!length) return { kind: "short", needed: watch.queue[watch.head]!.length };
|
|
386
|
+
if (watch.head === watch.queue.length) {
|
|
387
|
+
watch.queue = [];
|
|
388
|
+
watch.head = 0;
|
|
389
|
+
mapDelete(this.#buffered, watch.id);
|
|
390
|
+
}
|
|
391
|
+
return { kind: "records", records, length };
|
|
193
392
|
}
|
|
194
|
-
return { kind: "records", records, length };
|
|
195
393
|
}
|
|
196
394
|
|
|
197
395
|
function encodeBatch(batch: Exclude<WatchBatch, { kind: "closed" }>): Uint8Array[] {
|
|
198
396
|
if (batch.kind === "overflow") return [encodeRecord(OVERFLOW, null)];
|
|
199
397
|
const records: Uint8Array[] = [];
|
|
200
|
-
for (let index = 0; index < batch.events.length; index++)
|
|
201
|
-
|
|
398
|
+
for (let index = 0; index < batch.events.length; index++) {
|
|
399
|
+
const event = batch.events[index]!;
|
|
400
|
+
arrayAppend(records, encodeRecord(event.type === "change" ? CHANGE : RENAME, event.name));
|
|
401
|
+
}
|
|
202
402
|
return records;
|
|
203
403
|
}
|
|
204
404
|
|
|
205
|
-
function encodeEvent(event: WatchEvent): Uint8Array {
|
|
206
|
-
return encodeRecord(event.type === "change" ? CHANGE : RENAME, event.name);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
405
|
/** u8 type, three zero bytes, u32le name length or NULL_NAME, UTF-8 name padded to 4. */
|
|
210
406
|
function encodeRecord(type: number, name: string | null): Uint8Array {
|
|
211
407
|
const bytes = name === null ? undefined : encoder.encode(name);
|