@waffle-os/runtime 0.0.153 → 0.0.155
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/browser/net/helper.ts +114 -19
- package/browser/net/ring.ts +18 -6
- package/browser/net/runtime.ts +2 -2
- package/browser/notification-relay.ts +105 -18
- package/browser/notification-wait.ts +45 -4
- package/browser/notification-worker.ts +169 -76
- package/browser/preview.ts +15 -2
- package/core/filesystem.ts +18 -0
- package/core/wasi.ts +75 -4
- package/dist/browser/net/helper.js +83 -18
- package/dist/browser/net/ring.js +6 -5
- package/dist/browser/net/runtime.js +2 -2
- package/dist/browser/notification-relay.js +78 -15
- package/dist/browser/notification-wait.js +37 -5
- package/dist/browser/notification-worker.js +146 -67
- package/dist/core/wasi.js +62 -4
- package/dist/fs/backend.js +7 -0
- package/dist/fs/client.js +75 -17
- package/dist/fs/dirty-admission.js +15 -8
- package/dist/fs/guest-watch.js +267 -94
- package/dist/fs/indexeddb-chunks.js +88 -11
- package/dist/fs/indexeddb.js +366 -110
- package/dist/fs/memory.js +106 -20
- package/dist/fs/multitab-backend.js +291 -74
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +70 -16
- package/dist/fs/protocol.js +15 -7
- package/dist/fs/publication-attachment.js +9 -7
- package/dist/fs/service.js +62 -9
- package/dist/fs/shared.js +100 -6
- 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/command-stdio.js +1 -1
- 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 +17 -0
- package/fs/client.ts +99 -16
- package/fs/dirty-admission.ts +22 -7
- package/fs/guest-watch.ts +299 -103
- package/fs/indexeddb-chunks.ts +103 -9
- package/fs/indexeddb.ts +477 -121
- package/fs/memory.ts +120 -19
- package/fs/multitab-backend.ts +440 -95
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +88 -16
- package/fs/protocol.ts +26 -7
- package/fs/publication-attachment.ts +9 -7
- package/fs/service.ts +62 -7
- package/fs/shared.ts +110 -8
- 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/command-stdio.ts +1 -1
- package/proc/intrinsics/core.ts +7 -0
- package/proc/intrinsics.ts +1 -0
- package/testing/idle-workspace-cancellation-browser.ts +22 -13
- package/testing/indexeddb-storage-browser.ts +564 -1
- package/tooling/program/program-transformer.json +1 -1
package/dist/fs/client.js
CHANGED
|
@@ -87,6 +87,8 @@ import {
|
|
|
87
87
|
reapFilesystem,
|
|
88
88
|
unpack,
|
|
89
89
|
wake,
|
|
90
|
+
wakeTransport,
|
|
91
|
+
SIGNAL_TRANSPORT,
|
|
90
92
|
releaseFilesystemReservation
|
|
91
93
|
} from "./protocol.js";
|
|
92
94
|
import {
|
|
@@ -141,6 +143,18 @@ function transferList(payload) {
|
|
|
141
143
|
return { __proto__: null, [symbolIterator]: () => iterator };
|
|
142
144
|
}
|
|
143
145
|
let waitForObservedResolve;
|
|
146
|
+
const PLAIN_REPLIES = new SetConstructor([
|
|
147
|
+
"stat",
|
|
148
|
+
"create",
|
|
149
|
+
"write",
|
|
150
|
+
"append",
|
|
151
|
+
"grow",
|
|
152
|
+
"resize",
|
|
153
|
+
"setTimes",
|
|
154
|
+
"setMode",
|
|
155
|
+
"read",
|
|
156
|
+
"readlink"
|
|
157
|
+
]);
|
|
144
158
|
|
|
145
159
|
export class FilesystemCompletion {
|
|
146
160
|
id;
|
|
@@ -158,6 +172,7 @@ export class FilesystemCompletion {
|
|
|
158
172
|
loading;
|
|
159
173
|
consumed = false;
|
|
160
174
|
released = false;
|
|
175
|
+
retained = true;
|
|
161
176
|
constructor(id, buffer, client, publication) {
|
|
162
177
|
this.id = id;
|
|
163
178
|
this.buffer = buffer;
|
|
@@ -220,7 +235,8 @@ export class FilesystemCompletion {
|
|
|
220
235
|
if (!this.consumed)
|
|
221
236
|
this.cancel();
|
|
222
237
|
else {
|
|
223
|
-
this.
|
|
238
|
+
if (this.retained || atomicsLoad(this.cells, 0) !== State.Complete)
|
|
239
|
+
this.client.connection.port.postMessage({ discard: this.id });
|
|
224
240
|
this.notify();
|
|
225
241
|
}
|
|
226
242
|
mapDelete(this.client.completions, this.id);
|
|
@@ -235,6 +251,7 @@ export class FilesystemCompletion {
|
|
|
235
251
|
}
|
|
236
252
|
arrayAppend(this.backendWaiters, callback);
|
|
237
253
|
this.client.watchRevocation();
|
|
254
|
+
this.client.watchBackendSettlement();
|
|
238
255
|
if (this.backendSettled)
|
|
239
256
|
this.notify();
|
|
240
257
|
}
|
|
@@ -412,7 +429,7 @@ function preparationFailed(completion) {
|
|
|
412
429
|
const state = completion?.cells[0];
|
|
413
430
|
return state === State.Failed || state === State.Cancelled;
|
|
414
431
|
}
|
|
415
|
-
function clientWatches(cells, revoked, remote, call) {
|
|
432
|
+
function clientWatches(cells, revoked, remote, canDrain, call) {
|
|
416
433
|
return {
|
|
417
434
|
get cells() {
|
|
418
435
|
return cells();
|
|
@@ -423,8 +440,12 @@ function clientWatches(cells, revoked, remote, call) {
|
|
|
423
440
|
get remote() {
|
|
424
441
|
return remote();
|
|
425
442
|
},
|
|
443
|
+
get canDrain() {
|
|
444
|
+
return canDrain();
|
|
445
|
+
},
|
|
426
446
|
open: (handle, recursive) => call("watchOpen", [handle, recursive]),
|
|
427
447
|
read: (id) => call("watchRead", [id]),
|
|
448
|
+
drain: () => call("watchDrain", []),
|
|
428
449
|
close: (id) => {
|
|
429
450
|
call("watchClose", [id]);
|
|
430
451
|
}
|
|
@@ -473,6 +494,7 @@ export class FilesystemClient {
|
|
|
473
494
|
capabilities;
|
|
474
495
|
epoch;
|
|
475
496
|
signal;
|
|
497
|
+
transportSignal;
|
|
476
498
|
revoked;
|
|
477
499
|
section;
|
|
478
500
|
shared;
|
|
@@ -489,14 +511,15 @@ export class FilesystemClient {
|
|
|
489
511
|
this.requests = new FilesystemRequestAllocator(connection.requests);
|
|
490
512
|
this.capabilities = new Int32ArrayConstructor(connection.capabilities, 0, CAPABILITY_LENGTH);
|
|
491
513
|
this.epoch = new Int32ArrayConstructor(connection.epoch);
|
|
492
|
-
this.signal = new Int32ArrayConstructor(connection.signal);
|
|
514
|
+
this.signal = new Int32ArrayConstructor(connection.signal, 0, 1);
|
|
515
|
+
this.transportSignal = new Int32ArrayConstructor(connection.signal, SIGNAL_TRANSPORT * 4, 1);
|
|
493
516
|
const revoked = sharedRevoked(connection);
|
|
494
517
|
this.revoked = () => this.disposed || revoked() || !!this.transportError();
|
|
495
518
|
if (connection.residentPublication)
|
|
496
519
|
this.resident = new ResidentAttachment(connection.residentPublication, this.revoked);
|
|
497
520
|
this.section = new FilesystemTransportSection(connection, this.requests.memory, this.revoked);
|
|
498
521
|
this.shared = new SharedFilesystemState(connection.shared, connection.maxBytes, connection.owner, connection.segments, this.section, connection.publication?.buffers);
|
|
499
|
-
this.watches = clientWatches(() => this.shared.watch.cells, this.revoked, () => this.shared.isDirectDisabled(), (operation, args) => this.call(operation, args));
|
|
522
|
+
this.watches = clientWatches(() => this.shared.watch.cells, this.revoked, () => this.shared.isDirectDisabled(), () => !this.shared.isWatchDrainDisabled(), (operation, args) => this.call(operation, args));
|
|
500
523
|
connection.port.onmessage = ({ data }) => {
|
|
501
524
|
if (data.segment)
|
|
502
525
|
this.attachSegment(data.segment, data.buffer);
|
|
@@ -505,7 +528,7 @@ export class FilesystemClient {
|
|
|
505
528
|
};
|
|
506
529
|
connection.port.onmessageerror = () => {
|
|
507
530
|
closeRequestMemory(this.requests.memory);
|
|
508
|
-
|
|
531
|
+
wakeTransport(connection);
|
|
509
532
|
mapForEach(this.completions, (completion) => completion.notify());
|
|
510
533
|
connection.port.postMessage({ transportClosed: true });
|
|
511
534
|
connection.port.close();
|
|
@@ -639,26 +662,49 @@ export class FilesystemClient {
|
|
|
639
662
|
this.watchingRevocation = true;
|
|
640
663
|
this.monitorRevocation();
|
|
641
664
|
}
|
|
665
|
+
watchBackendSettlement() {
|
|
666
|
+
this.backendWaiterVersion++;
|
|
667
|
+
if (this.revocationWord === this.transportSignal)
|
|
668
|
+
atomicsNotify(this.transportSignal, 0);
|
|
669
|
+
}
|
|
670
|
+
revocationWord;
|
|
671
|
+
backendWaiterVersion = 0;
|
|
672
|
+
unsettledBackendWaiter() {
|
|
673
|
+
let found = false;
|
|
674
|
+
mapForEach(this.completions, (completion) => {
|
|
675
|
+
if (completion.hasBackendWaiters && !completion.backendSettled)
|
|
676
|
+
found = true;
|
|
677
|
+
});
|
|
678
|
+
return found;
|
|
679
|
+
}
|
|
680
|
+
revocationSignal() {
|
|
681
|
+
return this.connection.publication || this.unsettledBackendWaiter() ? this.signal : this.transportSignal;
|
|
682
|
+
}
|
|
642
683
|
monitorRevocation() {
|
|
643
|
-
const signal = this.signal;
|
|
644
684
|
const stop = () => {
|
|
685
|
+
this.revocationWord = undefined;
|
|
645
686
|
this.watchingRevocation = false;
|
|
646
687
|
};
|
|
647
688
|
const step = () => {
|
|
689
|
+
this.revocationWord = undefined;
|
|
648
690
|
try {
|
|
649
691
|
while (mapSize(this.completions)) {
|
|
692
|
+
const version = this.backendWaiterVersion;
|
|
693
|
+
const signal = this.revocationSignal();
|
|
650
694
|
const observed = atomicsLoad(signal, 0);
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
695
|
+
let waiting = true;
|
|
696
|
+
if (this.revoked())
|
|
697
|
+
waiting = this.sweepRevokedCompletions();
|
|
698
|
+
else
|
|
655
699
|
this.notifySettledBackendWaiters();
|
|
656
|
-
|
|
657
|
-
|
|
700
|
+
if (version !== this.backendWaiterVersion)
|
|
701
|
+
continue;
|
|
702
|
+
if (!waiting || !mapSize(this.completions))
|
|
658
703
|
break;
|
|
659
|
-
const
|
|
660
|
-
if (
|
|
661
|
-
|
|
704
|
+
const parked = atomicsWaitAsync(signal, 0, observed);
|
|
705
|
+
if (parked.async) {
|
|
706
|
+
this.revocationWord = signal;
|
|
707
|
+
afterNativePromise(parked.value, step, stop);
|
|
662
708
|
return;
|
|
663
709
|
}
|
|
664
710
|
}
|
|
@@ -805,14 +851,19 @@ export class FilesystemClient {
|
|
|
805
851
|
throw new FilesystemError(6);
|
|
806
852
|
}
|
|
807
853
|
submitPrepared(operation, args, data, capacity, publication) {
|
|
808
|
-
const input = pack({ operation, args, publication });
|
|
854
|
+
const input = pack(publication ? { operation, args, publication } : { operation, args });
|
|
809
855
|
this.section.enter();
|
|
810
856
|
try {
|
|
811
|
-
|
|
857
|
+
const completion = this.postRequest(input, data, capacity, publication);
|
|
858
|
+
completion.retained = this.retainsReply(operation);
|
|
859
|
+
return completion;
|
|
812
860
|
} finally {
|
|
813
861
|
this.section.exit();
|
|
814
862
|
}
|
|
815
863
|
}
|
|
864
|
+
retainsReply(operation) {
|
|
865
|
+
return !setHas(PLAIN_REPLIES, operation) || this.connection.residentPublication !== undefined || this.shared.publication !== undefined;
|
|
866
|
+
}
|
|
816
867
|
postRequest(input, data, capacity, publication) {
|
|
817
868
|
const inputLength = typedArrayLength(input);
|
|
818
869
|
const total = inputLength + typedArrayLength(data);
|
|
@@ -2047,6 +2098,13 @@ export class FilesystemClient {
|
|
|
2047
2098
|
return errno(error);
|
|
2048
2099
|
}
|
|
2049
2100
|
}
|
|
2101
|
+
realpath(directory, path, rooted = false) {
|
|
2102
|
+
try {
|
|
2103
|
+
return this.call("realpath", [directory, path, rooted]);
|
|
2104
|
+
} catch (error) {
|
|
2105
|
+
return errno(error);
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2050
2108
|
readlink(handle) {
|
|
2051
2109
|
this.retain(handle);
|
|
2052
2110
|
try {
|
|
@@ -127,15 +127,22 @@ export class DirtyAdmission {
|
|
|
127
127
|
get revision() {
|
|
128
128
|
return this.values[REVISION];
|
|
129
129
|
}
|
|
130
|
-
|
|
131
|
-
if (!this.enabled ||
|
|
132
|
-
return
|
|
133
|
-
this.values[DIRTY_BYTES] = 0;
|
|
134
|
-
this.values[DIRTY_METADATA] = 0;
|
|
130
|
+
releaseSlot(bytes, metadata) {
|
|
131
|
+
if (!this.enabled || !bytes && !metadata)
|
|
132
|
+
return;
|
|
133
|
+
this.values[DIRTY_BYTES] = Math.max(0, this.values[DIRTY_BYTES] - bytes);
|
|
134
|
+
this.values[DIRTY_METADATA] = Math.max(0, this.values[DIRTY_METADATA] - metadata);
|
|
135
|
+
this.values[REVISION]++;
|
|
136
|
+
this.notify();
|
|
137
|
+
}
|
|
138
|
+
reconcile(namespace, dirtyNamespace, dirtyBytes, dirtyMetadata) {
|
|
139
|
+
if (!this.enabled)
|
|
140
|
+
return;
|
|
141
|
+
this.values[DIRTY_BYTES] = dirtyBytes;
|
|
142
|
+
this.values[DIRTY_METADATA] = dirtyMetadata;
|
|
135
143
|
this.values[NAMESPACE] = namespace;
|
|
136
|
-
this.values[BASE_NAMESPACE] = namespace;
|
|
144
|
+
this.values[BASE_NAMESPACE] = namespace - dirtyNamespace;
|
|
137
145
|
this.notify();
|
|
138
|
-
return true;
|
|
139
146
|
}
|
|
140
147
|
notify() {
|
|
141
148
|
Atomics.add(this.signals, 0, 1);
|
|
@@ -162,7 +169,7 @@ export class DirtyAdmission {
|
|
|
162
169
|
}
|
|
163
170
|
}
|
|
164
171
|
get hasDebt() {
|
|
165
|
-
return this.values[DIRTY_BYTES] > 0 || this.values[DIRTY_METADATA] > 0;
|
|
172
|
+
return this.values[DIRTY_BYTES] > 0 || this.values[DIRTY_METADATA] > 0 || this.values[NAMESPACE] > this.values[BASE_NAMESPACE];
|
|
166
173
|
}
|
|
167
174
|
capacityReleased() {
|
|
168
175
|
if (this.enabled)
|
package/dist/fs/guest-watch.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
FilesystemError
|
|
3
|
+
} from "../core/filesystem.js";
|
|
2
4
|
import {
|
|
3
5
|
arrayAppend,
|
|
4
6
|
atomicsLoad,
|
|
5
7
|
MapConstructor,
|
|
6
8
|
mapDelete,
|
|
9
|
+
mapFirst,
|
|
7
10
|
mapForEach,
|
|
8
11
|
mapGet,
|
|
9
12
|
mapSet,
|
|
@@ -12,6 +15,7 @@ import {
|
|
|
12
15
|
const EAGAIN = 6;
|
|
13
16
|
const EBADF = 8;
|
|
14
17
|
const ECANCELED = 11;
|
|
18
|
+
const ENOTSUP = 58;
|
|
15
19
|
const CHANGE = 1;
|
|
16
20
|
const RENAME = 2;
|
|
17
21
|
const OVERFLOW = 3;
|
|
@@ -22,147 +26,316 @@ const END = { kind: "records", records: [], length: 0 };
|
|
|
22
26
|
function failure(errno) {
|
|
23
27
|
return { kind: "error", errno };
|
|
24
28
|
}
|
|
29
|
+
function watchError(id, errno) {
|
|
30
|
+
return { kind: "watch_error", id, errno };
|
|
31
|
+
}
|
|
32
|
+
function nextOf(id, result) {
|
|
33
|
+
return result.kind === "error" ? watchError(id, result.errno) : { ...result, id };
|
|
34
|
+
}
|
|
35
|
+
function rotate(map, watch) {
|
|
36
|
+
mapDelete(map, watch.id);
|
|
37
|
+
mapSet(map, watch.id, watch);
|
|
38
|
+
}
|
|
25
39
|
|
|
26
40
|
export class GuestWatchTable {
|
|
27
|
-
fs;
|
|
28
|
-
watches
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
#fs;
|
|
42
|
+
#watches;
|
|
43
|
+
#byService;
|
|
44
|
+
#buffered;
|
|
45
|
+
#errored;
|
|
46
|
+
#ended;
|
|
47
|
+
#nextId = 1;
|
|
48
|
+
#revocationNotified = false;
|
|
49
|
+
#activeCount = 0;
|
|
50
|
+
#observedGen = 0;
|
|
51
|
+
#observedCount = 0;
|
|
52
|
+
#tableObserved;
|
|
53
|
+
#drainMore = false;
|
|
54
|
+
#noDrain = false;
|
|
55
|
+
#flip = false;
|
|
31
56
|
constructor(fs) {
|
|
32
|
-
this
|
|
57
|
+
this.#fs = fs;
|
|
58
|
+
this.drop();
|
|
33
59
|
}
|
|
34
60
|
get word() {
|
|
35
|
-
return this
|
|
61
|
+
return this.#fs.cells;
|
|
36
62
|
}
|
|
37
63
|
get active() {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
64
|
+
return this.#activeCount > 0;
|
|
65
|
+
}
|
|
66
|
+
#useDrain() {
|
|
67
|
+
if (typeof this.#fs.drain !== "function" || this.#noDrain)
|
|
68
|
+
return false;
|
|
69
|
+
if (this.#fs.canDrain === false) {
|
|
70
|
+
this.#tableObserved = undefined;
|
|
71
|
+
this.#drainMore = false;
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
#allObserved(generation) {
|
|
77
|
+
return this.#observedCount === this.#activeCount && this.#observedGen === generation || this.#useDrain() && !this.#drainMore && this.#tableObserved === generation;
|
|
44
78
|
}
|
|
45
79
|
pending() {
|
|
46
|
-
if (this
|
|
47
|
-
return !!this
|
|
48
|
-
return this
|
|
80
|
+
if (this.#fs.revoked)
|
|
81
|
+
return !!this.#fs.remote && !this.#revocationNotified && this.#activeCount > 0;
|
|
82
|
+
return this.#activeCount > 0 && (mapSize(this.#buffered) > 0 || mapSize(this.#errored) > 0 || !this.#allObserved(atomicsLoad(this.#fs.cells, 3)));
|
|
49
83
|
}
|
|
50
84
|
ready() {
|
|
51
|
-
if (!mapSize(this
|
|
85
|
+
if (!mapSize(this.#watches))
|
|
52
86
|
return false;
|
|
53
|
-
return this
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (watch.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
87
|
+
return this.#fs.revoked || mapSize(this.#buffered) > 0 || mapSize(this.#errored) > 0 || mapSize(this.#ended) > 0 || this.#activeCount > 0 && !this.#allObserved(atomicsLoad(this.#fs.cells, 3));
|
|
88
|
+
}
|
|
89
|
+
#setObserved(watch, generation) {
|
|
90
|
+
if (watch.observed === generation)
|
|
91
|
+
return;
|
|
92
|
+
if (!watch.ended) {
|
|
93
|
+
if (watch.observed === this.#observedGen)
|
|
94
|
+
this.#observedCount--;
|
|
95
|
+
if (generation !== this.#observedGen) {
|
|
96
|
+
this.#observedGen = generation;
|
|
97
|
+
this.#observedCount = 1;
|
|
98
|
+
} else
|
|
99
|
+
this.#observedCount++;
|
|
100
|
+
}
|
|
101
|
+
watch.observed = generation;
|
|
102
|
+
}
|
|
103
|
+
#markEnded(watch) {
|
|
104
|
+
if (watch.ended)
|
|
105
|
+
return;
|
|
106
|
+
watch.ended = true;
|
|
107
|
+
this.#activeCount--;
|
|
108
|
+
if (watch.observed === this.#observedGen)
|
|
109
|
+
this.#observedCount--;
|
|
110
|
+
watch.errno = 0;
|
|
111
|
+
mapDelete(this.#buffered, watch.id);
|
|
112
|
+
mapDelete(this.#errored, watch.id);
|
|
113
|
+
mapSet(this.#ended, watch.id, watch);
|
|
63
114
|
}
|
|
64
115
|
open(handle, recursive) {
|
|
65
|
-
if (this
|
|
116
|
+
if (this.#nextId > LAST_ID)
|
|
66
117
|
throw new FilesystemError(EAGAIN, "watch ids exhausted");
|
|
67
|
-
const observed = atomicsLoad(this
|
|
68
|
-
const service = this
|
|
69
|
-
const id = this
|
|
70
|
-
|
|
118
|
+
const observed = atomicsLoad(this.#fs.cells, 3);
|
|
119
|
+
const service = this.#fs.open(handle, recursive);
|
|
120
|
+
const id = this.#nextId++;
|
|
121
|
+
const watch = { id, service, observed: NaN, queue: [], head: 0, ended: false, errno: 0 };
|
|
122
|
+
mapSet(this.#watches, id, watch);
|
|
123
|
+
mapSet(this.#byService, service, watch);
|
|
124
|
+
this.#activeCount++;
|
|
125
|
+
this.#setObserved(watch, observed);
|
|
71
126
|
return id;
|
|
72
127
|
}
|
|
73
128
|
read(id, capacity) {
|
|
74
|
-
const watch = mapGet(this
|
|
129
|
+
const watch = mapGet(this.#watches, id);
|
|
75
130
|
if (!watch)
|
|
76
131
|
return failure(EBADF);
|
|
77
132
|
if (watch.ended)
|
|
78
133
|
return END;
|
|
79
|
-
if (watch.head
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
134
|
+
if (watch.head < watch.queue.length)
|
|
135
|
+
return this.#takeFrom(watch, capacity);
|
|
136
|
+
const force = watch.errno !== 0;
|
|
137
|
+
if (force) {
|
|
138
|
+
watch.errno = 0;
|
|
139
|
+
mapDelete(this.#errored, id);
|
|
140
|
+
}
|
|
141
|
+
return this.#refillSingle(watch, force) ?? this.#takeFrom(watch, capacity);
|
|
142
|
+
}
|
|
143
|
+
readNext(capacity) {
|
|
144
|
+
if (!mapSize(this.#watches))
|
|
145
|
+
return failure(EAGAIN);
|
|
146
|
+
if (this.#fs.revoked) {
|
|
147
|
+
if (this.#fs.remote)
|
|
148
|
+
this.#revocationNotified = true;
|
|
149
|
+
const first = mapFirst(this.#watches);
|
|
150
|
+
rotate(this.#watches, first);
|
|
151
|
+
return watchError(first.id, ECANCELED);
|
|
152
|
+
}
|
|
153
|
+
if (this.#useDrain() && (mapSize(this.#buffered) || mapSize(this.#errored) || !this.#drainService()))
|
|
154
|
+
return this.#takeNextReady(capacity);
|
|
155
|
+
return this.#readNextFallback(capacity);
|
|
156
|
+
}
|
|
157
|
+
#takeNextReady(capacity) {
|
|
158
|
+
const buffered = mapFirst(this.#buffered);
|
|
159
|
+
if (buffered) {
|
|
160
|
+
const read = this.#takeFrom(buffered, capacity);
|
|
161
|
+
if (buffered.head < buffered.queue.length)
|
|
162
|
+
rotate(this.#buffered, buffered);
|
|
163
|
+
return nextOf(buffered.id, read);
|
|
83
164
|
}
|
|
84
|
-
|
|
165
|
+
const ended = mapFirst(this.#ended);
|
|
166
|
+
const errored = mapFirst(this.#errored);
|
|
167
|
+
const pick = this.#flip ? ended ?? errored : errored ?? ended;
|
|
168
|
+
if (!pick)
|
|
169
|
+
return failure(EAGAIN);
|
|
170
|
+
this.#flip = !pick.ended;
|
|
171
|
+
if (pick.ended) {
|
|
172
|
+
rotate(this.#ended, pick);
|
|
173
|
+
return nextOf(pick.id, END);
|
|
174
|
+
}
|
|
175
|
+
const errno = pick.errno;
|
|
176
|
+
pick.errno = 0;
|
|
177
|
+
if (pick.observed === this.#observedGen)
|
|
178
|
+
this.#observedCount--;
|
|
179
|
+
pick.observed = NaN;
|
|
180
|
+
this.#drainMore = true;
|
|
181
|
+
mapDelete(this.#errored, pick.id);
|
|
182
|
+
return watchError(pick.id, errno);
|
|
183
|
+
}
|
|
184
|
+
#readNextFallback(capacity) {
|
|
185
|
+
let result;
|
|
186
|
+
let reported;
|
|
187
|
+
mapForEach(this.#watches, (watch) => {
|
|
188
|
+
if (result)
|
|
189
|
+
return;
|
|
190
|
+
try {
|
|
191
|
+
const read = this.read(watch.id, capacity);
|
|
192
|
+
if (read.kind !== "error" || read.errno !== EAGAIN) {
|
|
193
|
+
result = nextOf(watch.id, read);
|
|
194
|
+
reported = watch;
|
|
195
|
+
}
|
|
196
|
+
} catch (error) {
|
|
197
|
+
if (!(error instanceof FilesystemError))
|
|
198
|
+
throw error;
|
|
199
|
+
result = watchError(watch.id, error.errno);
|
|
200
|
+
reported = watch;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
if (reported)
|
|
204
|
+
rotate(this.#watches, reported);
|
|
205
|
+
return result ?? failure(EAGAIN);
|
|
85
206
|
}
|
|
86
207
|
close(id) {
|
|
87
|
-
const watch = mapGet(this
|
|
208
|
+
const watch = mapGet(this.#watches, id);
|
|
88
209
|
if (!watch)
|
|
89
210
|
return EBADF;
|
|
90
|
-
if (!this
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
211
|
+
if (!this.#fs.revoked) {
|
|
212
|
+
try {
|
|
213
|
+
this.#fs.close(watch.service);
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (!(error instanceof FilesystemError) || error.errno !== ECANCELED)
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (!watch.ended) {
|
|
220
|
+
this.#activeCount--;
|
|
221
|
+
if (watch.observed === this.#observedGen)
|
|
222
|
+
this.#observedCount--;
|
|
101
223
|
}
|
|
224
|
+
mapDelete(this.#buffered, id);
|
|
225
|
+
mapDelete(this.#errored, id);
|
|
226
|
+
mapDelete(this.#ended, id);
|
|
227
|
+
mapDelete(this.#watches, id);
|
|
228
|
+
mapDelete(this.#byService, watch.service);
|
|
229
|
+
if (!mapSize(this.#watches)) {
|
|
230
|
+
this.#tableObserved = undefined;
|
|
231
|
+
this.#drainMore = false;
|
|
232
|
+
}
|
|
233
|
+
return 0;
|
|
102
234
|
}
|
|
103
235
|
drop() {
|
|
104
|
-
this
|
|
236
|
+
this.#watches = new MapConstructor;
|
|
237
|
+
this.#byService = new MapConstructor;
|
|
238
|
+
this.#buffered = new MapConstructor;
|
|
239
|
+
this.#errored = new MapConstructor;
|
|
240
|
+
this.#ended = new MapConstructor;
|
|
241
|
+
this.#activeCount = this.#observedCount = 0;
|
|
242
|
+
this.#tableObserved = undefined;
|
|
243
|
+
this.#drainMore = false;
|
|
105
244
|
}
|
|
106
|
-
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return failure(ECANCELED);
|
|
245
|
+
#applyBatch(watch, batch, generation) {
|
|
246
|
+
if (batch.kind === "closed") {
|
|
247
|
+
this.#markEnded(watch);
|
|
248
|
+
return true;
|
|
111
249
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
250
|
+
this.#setObserved(watch, generation);
|
|
251
|
+
watch.queue = encodeBatch(batch);
|
|
252
|
+
watch.head = 0;
|
|
253
|
+
if (watch.queue.length)
|
|
254
|
+
mapSet(this.#buffered, watch.id, watch);
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
#drainService() {
|
|
258
|
+
const generation = atomicsLoad(this.#fs.cells, 3);
|
|
259
|
+
if (!this.#drainMore && this.#allObserved(generation))
|
|
260
|
+
return false;
|
|
261
|
+
const previous = this.#tableObserved;
|
|
262
|
+
this.#tableObserved = generation;
|
|
263
|
+
let drained;
|
|
118
264
|
try {
|
|
119
|
-
|
|
265
|
+
drained = this.#fs.drain();
|
|
120
266
|
} catch (error) {
|
|
121
|
-
|
|
267
|
+
if (error instanceof FilesystemError && error.errno === ENOTSUP) {
|
|
268
|
+
this.#tableObserved = undefined;
|
|
269
|
+
this.#drainMore = false;
|
|
270
|
+
if (this.#fs.canDrain === undefined)
|
|
271
|
+
this.#noDrain = true;
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
this.#tableObserved = previous;
|
|
122
275
|
throw error;
|
|
123
276
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
277
|
+
this.#drainMore = drained.more;
|
|
278
|
+
for (let index = 0;index < drained.entries.length; index++) {
|
|
279
|
+
const entry = drained.entries[index];
|
|
280
|
+
const watch = mapGet(this.#byService, entry.id);
|
|
281
|
+
if (!watch || watch.ended)
|
|
282
|
+
continue;
|
|
283
|
+
if ("errno" in entry) {
|
|
284
|
+
watch.errno = entry.errno;
|
|
285
|
+
mapSet(this.#errored, watch.id, watch);
|
|
286
|
+
} else {
|
|
287
|
+
this.#applyBatch(watch, entry.batch, generation);
|
|
288
|
+
}
|
|
127
289
|
}
|
|
128
|
-
|
|
129
|
-
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
#refillSingle(watch, force) {
|
|
293
|
+
if (this.#fs.revoked) {
|
|
294
|
+
if (this.#fs.remote)
|
|
295
|
+
this.#revocationNotified = true;
|
|
296
|
+
return failure(ECANCELED);
|
|
297
|
+
}
|
|
298
|
+
const generation = atomicsLoad(this.#fs.cells, 3);
|
|
299
|
+
if (!force && !this.#fs.remote && (generation === watch.observed || this.#useDrain() && !this.#drainMore && generation === this.#tableObserved))
|
|
300
|
+
return failure(EAGAIN);
|
|
301
|
+
if (this.#applyBatch(watch, this.#fs.read(watch.service), generation))
|
|
302
|
+
return END;
|
|
130
303
|
if (!watch.queue.length)
|
|
131
304
|
return failure(EAGAIN);
|
|
132
305
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
306
|
+
#takeFrom(watch, capacity) {
|
|
307
|
+
const records = [];
|
|
308
|
+
let length = 0;
|
|
309
|
+
while (watch.head < watch.queue.length) {
|
|
310
|
+
const record = watch.queue[watch.head];
|
|
311
|
+
if (length + record.length > capacity)
|
|
312
|
+
break;
|
|
313
|
+
arrayAppend(records, record);
|
|
314
|
+
length += record.length;
|
|
315
|
+
watch.head++;
|
|
316
|
+
if (record[0] === OVERFLOW)
|
|
317
|
+
this.#markEnded(watch);
|
|
318
|
+
}
|
|
319
|
+
if (!length)
|
|
320
|
+
return { kind: "short", needed: watch.queue[watch.head].length };
|
|
321
|
+
if (watch.head === watch.queue.length) {
|
|
322
|
+
watch.queue = [];
|
|
323
|
+
watch.head = 0;
|
|
324
|
+
mapDelete(this.#buffered, watch.id);
|
|
325
|
+
}
|
|
326
|
+
return { kind: "records", records, length };
|
|
152
327
|
}
|
|
153
|
-
return { kind: "records", records, length };
|
|
154
328
|
}
|
|
155
329
|
function encodeBatch(batch) {
|
|
156
330
|
if (batch.kind === "overflow")
|
|
157
331
|
return [encodeRecord(OVERFLOW, null)];
|
|
158
332
|
const records = [];
|
|
159
|
-
for (let index = 0;index < batch.events.length; index++)
|
|
160
|
-
|
|
333
|
+
for (let index = 0;index < batch.events.length; index++) {
|
|
334
|
+
const event = batch.events[index];
|
|
335
|
+
arrayAppend(records, encodeRecord(event.type === "change" ? CHANGE : RENAME, event.name));
|
|
336
|
+
}
|
|
161
337
|
return records;
|
|
162
338
|
}
|
|
163
|
-
function encodeEvent(event) {
|
|
164
|
-
return encodeRecord(event.type === "change" ? CHANGE : RENAME, event.name);
|
|
165
|
-
}
|
|
166
339
|
function encodeRecord(type, name) {
|
|
167
340
|
const bytes = name === null ? undefined : encoder.encode(name);
|
|
168
341
|
const size = bytes ? bytes.length : 0;
|