@isoloop/bus 0.1.1 → 0.2.0-next.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/dist/bus-worker.js +416 -6
- package/package.json +4 -4
package/dist/bus-worker.js
CHANGED
|
@@ -64,7 +64,9 @@ const isIsolateToBus = envelopeGuard({
|
|
|
64
64
|
unsubscribe: true,
|
|
65
65
|
commits: true,
|
|
66
66
|
request: true,
|
|
67
|
-
reply: true
|
|
67
|
+
reply: true,
|
|
68
|
+
watch: true,
|
|
69
|
+
unwatch: true
|
|
68
70
|
});
|
|
69
71
|
/** Longest preview of a record's value, in characters (ADR 0020, decision 3). */
|
|
70
72
|
const PREVIEW_CHARS = 2048;
|
|
@@ -758,6 +760,342 @@ function estimateSize(value, limit = Infinity) {
|
|
|
758
760
|
}
|
|
759
761
|
return size;
|
|
760
762
|
}
|
|
763
|
+
/** How long a topic's snapshot map outlives its last watch (ADR 0023, point 19). */
|
|
764
|
+
const SNAPSHOT_GRACE_MS = 5e3;
|
|
765
|
+
/** Keys an unfiltered watch may hold before dev mode warns (ADR 0023, point 12). */
|
|
766
|
+
const WIDE_WATCH_KEYS = 1e3;
|
|
767
|
+
/** Records read from the log per step while a view is built. */
|
|
768
|
+
const SCAN_BATCH = 1024;
|
|
769
|
+
/**
|
|
770
|
+
* Watches in the bus worker (ADR 0023, points 13 to 20): the current value
|
|
771
|
+
* per key, sent as `view`s built from the log when they go out, one in
|
|
772
|
+
* flight per watch. An append only notes news for idle watches.
|
|
773
|
+
*
|
|
774
|
+
* @internal
|
|
775
|
+
*/
|
|
776
|
+
var WatchBook = class {
|
|
777
|
+
#engine;
|
|
778
|
+
#seeding;
|
|
779
|
+
#timers;
|
|
780
|
+
#dev;
|
|
781
|
+
#topics = /* @__PURE__ */ new Map();
|
|
782
|
+
#byWatcher = /* @__PURE__ */ new Map();
|
|
783
|
+
/**
|
|
784
|
+
* Per topic, kept after its last watch leaves: the counters behind
|
|
785
|
+
* `WatchStats`. `records` holds only the watches that ended; a live watch's
|
|
786
|
+
* share is read from the head at sample time, so an append counts nothing.
|
|
787
|
+
*/
|
|
788
|
+
#counts = /* @__PURE__ */ new Map();
|
|
789
|
+
constructor(opts) {
|
|
790
|
+
this.#engine = opts.engine;
|
|
791
|
+
this.#seeding = opts.seeding;
|
|
792
|
+
this.#timers = opts.timers;
|
|
793
|
+
this.#dev = opts.dev;
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Starts or replaces `watcher`'s watch on `topic`. With `staleHave`, the
|
|
797
|
+
* versions in `have` came from a bus that lost the topic in a restart, so
|
|
798
|
+
* every key the client holds is sent again or cleared.
|
|
799
|
+
*/
|
|
800
|
+
watch(watcher, topic, keys, have, staleHave) {
|
|
801
|
+
const old = this.#byWatcher.get(watcher)?.get(topic);
|
|
802
|
+
if (old) this.#remove(old, false);
|
|
803
|
+
this.#count(topic);
|
|
804
|
+
let tw = this.#topics.get(topic);
|
|
805
|
+
if (!tw) {
|
|
806
|
+
tw = {
|
|
807
|
+
watches: /* @__PURE__ */ new Set(),
|
|
808
|
+
snap: void 0,
|
|
809
|
+
gen: 0,
|
|
810
|
+
tail: this.#engine.tail(topic)
|
|
811
|
+
};
|
|
812
|
+
this.#topics.set(topic, tw);
|
|
813
|
+
}
|
|
814
|
+
const w = {
|
|
815
|
+
watcher,
|
|
816
|
+
topic,
|
|
817
|
+
keys: keys ? new Set(keys) : void 0,
|
|
818
|
+
sent: new Map((have ?? []).map(([k, v]) => [k, staleHave ? -1 : v])),
|
|
819
|
+
cursor: void 0,
|
|
820
|
+
checkedTail: -1,
|
|
821
|
+
vseq: 0,
|
|
822
|
+
inFlight: void 0,
|
|
823
|
+
news: false,
|
|
824
|
+
scheduled: false,
|
|
825
|
+
building: void 0,
|
|
826
|
+
paging: false,
|
|
827
|
+
warned: false,
|
|
828
|
+
startHead: this.#engine.head(topic)
|
|
829
|
+
};
|
|
830
|
+
tw.watches.add(w);
|
|
831
|
+
tw.gen++;
|
|
832
|
+
let mine = this.#byWatcher.get(watcher);
|
|
833
|
+
if (!mine) this.#byWatcher.set(watcher, mine = /* @__PURE__ */ new Map());
|
|
834
|
+
mine.set(topic, w);
|
|
835
|
+
this.#schedule(w);
|
|
836
|
+
}
|
|
837
|
+
/** Ends `watcher`'s watch on `topic`, if it has one. */
|
|
838
|
+
unwatch(watcher, topic) {
|
|
839
|
+
const w = this.#byWatcher.get(watcher)?.get(topic);
|
|
840
|
+
if (w) this.#remove(w, true);
|
|
841
|
+
}
|
|
842
|
+
/** Ends every watch of a connection going away, and returns their topics. */
|
|
843
|
+
drop(watcher) {
|
|
844
|
+
const mine = this.#byWatcher.get(watcher);
|
|
845
|
+
if (!mine) return [];
|
|
846
|
+
const topics = [...mine.keys()];
|
|
847
|
+
for (const w of mine.values()) this.#remove(w, true);
|
|
848
|
+
this.#byWatcher.delete(watcher);
|
|
849
|
+
return topics;
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* A `seen` from the client's `commits` (point 21): the view numbered
|
|
853
|
+
* `vseq` is settled, so the next one may go out. A `seen` for a watch that
|
|
854
|
+
* has ended, or for a view not sent on this watch, is a race with an
|
|
855
|
+
* `unwatch` or a new `watch`, and is ignored.
|
|
856
|
+
*/
|
|
857
|
+
seen(watcher, topic, vseq) {
|
|
858
|
+
if (typeof vseq !== "number" || !Number.isInteger(vseq) || vseq < 1) return {
|
|
859
|
+
ok: false,
|
|
860
|
+
error: {
|
|
861
|
+
code: "invalid",
|
|
862
|
+
message: "vseq must be a positive integer"
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
const w = this.#byWatcher.get(watcher)?.get(topic);
|
|
866
|
+
if (!w || vseq > w.vseq || w.inFlight === void 0 || vseq < w.inFlight) return {
|
|
867
|
+
ok: true,
|
|
868
|
+
value: null
|
|
869
|
+
};
|
|
870
|
+
w.inFlight = void 0;
|
|
871
|
+
if (w.news) this.#schedule(w);
|
|
872
|
+
return {
|
|
873
|
+
ok: true,
|
|
874
|
+
value: null
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
/** A record landed on `topic`: idle watches build a view, busy ones note news (point 14). */
|
|
878
|
+
appended(topic) {
|
|
879
|
+
const tw = this.#topics.get(topic);
|
|
880
|
+
if (!tw) return;
|
|
881
|
+
for (const w of tw.watches) this.#note(w);
|
|
882
|
+
}
|
|
883
|
+
/** The figures of every topic watched now or since the bus worker started. */
|
|
884
|
+
stats() {
|
|
885
|
+
const out = [];
|
|
886
|
+
for (const [topic, c] of this.#counts) {
|
|
887
|
+
const keys = /* @__PURE__ */ new Set();
|
|
888
|
+
let watches = 0;
|
|
889
|
+
let records = c.records;
|
|
890
|
+
const head = this.#engine.head(topic);
|
|
891
|
+
for (const w of this.#topics.get(topic)?.watches ?? []) {
|
|
892
|
+
watches++;
|
|
893
|
+
records += head - w.startHead;
|
|
894
|
+
for (const k of w.sent.keys()) keys.add(k);
|
|
895
|
+
}
|
|
896
|
+
out.push({
|
|
897
|
+
topic,
|
|
898
|
+
watches,
|
|
899
|
+
liveKeys: keys.size,
|
|
900
|
+
records,
|
|
901
|
+
values: c.values
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
return out;
|
|
905
|
+
}
|
|
906
|
+
#count(topic) {
|
|
907
|
+
let c = this.#counts.get(topic);
|
|
908
|
+
if (!c) {
|
|
909
|
+
c = {
|
|
910
|
+
records: 0,
|
|
911
|
+
values: 0
|
|
912
|
+
};
|
|
913
|
+
this.#counts.set(topic, c);
|
|
914
|
+
}
|
|
915
|
+
return c;
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* The retention pass ran on `topic`. When it moved the tail, each watch
|
|
919
|
+
* looks for keys that fell out, so a clear goes out with no new record
|
|
920
|
+
* (point 16).
|
|
921
|
+
*/
|
|
922
|
+
retained(topic) {
|
|
923
|
+
const tw = this.#topics.get(topic);
|
|
924
|
+
if (!tw) return;
|
|
925
|
+
const tail = this.#engine.tail(topic);
|
|
926
|
+
if (tail === tw.tail) return;
|
|
927
|
+
tw.tail = tail;
|
|
928
|
+
for (const w of tw.watches) this.#note(w);
|
|
929
|
+
}
|
|
930
|
+
/** A seed ended: the topic's watches send their snapshots. */
|
|
931
|
+
opened(topic) {
|
|
932
|
+
const tw = this.#topics.get(topic);
|
|
933
|
+
if (!tw) return;
|
|
934
|
+
for (const w of tw.watches) this.#schedule(w);
|
|
935
|
+
}
|
|
936
|
+
#note(w) {
|
|
937
|
+
if (w.inFlight !== void 0 || w.building) w.news = true;
|
|
938
|
+
else this.#schedule(w);
|
|
939
|
+
}
|
|
940
|
+
#remove(w, graceful) {
|
|
941
|
+
const mine = this.#byWatcher.get(w.watcher);
|
|
942
|
+
if (mine?.get(w.topic) === w) mine.delete(w.topic);
|
|
943
|
+
const tw = this.#topics.get(w.topic);
|
|
944
|
+
if (!tw) return;
|
|
945
|
+
if (tw.watches.delete(w)) this.#count(w.topic).records += this.#engine.head(w.topic) - w.startHead;
|
|
946
|
+
if (!graceful || tw.watches.size > 0) return;
|
|
947
|
+
const gen = ++tw.gen;
|
|
948
|
+
this.#timers.set(() => {
|
|
949
|
+
if (tw.gen === gen && tw.watches.size === 0) this.#topics.delete(w.topic);
|
|
950
|
+
}, SNAPSHOT_GRACE_MS);
|
|
951
|
+
}
|
|
952
|
+
#current(w) {
|
|
953
|
+
return this.#byWatcher.get(w.watcher)?.get(w.topic) === w;
|
|
954
|
+
}
|
|
955
|
+
/** Queues one view build per watch per microtask, so records of one task go out together. */
|
|
956
|
+
#schedule(w) {
|
|
957
|
+
if (w.scheduled) return;
|
|
958
|
+
w.scheduled = true;
|
|
959
|
+
queueMicrotask(() => {
|
|
960
|
+
w.scheduled = false;
|
|
961
|
+
if (this.#current(w)) this.#run(w);
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
/** Reads evicted records back from disk, then goes on with the view. */
|
|
965
|
+
#pageIn(w, offset) {
|
|
966
|
+
w.paging = true;
|
|
967
|
+
this.#engine.page(w.topic, offset).then(() => {
|
|
968
|
+
w.paging = false;
|
|
969
|
+
this.#schedule(w);
|
|
970
|
+
}, (e) => {
|
|
971
|
+
w.paging = false;
|
|
972
|
+
console.warn(`[isoloop] reading ${w.topic}@${offset} back from disk failed`, e);
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
#run(w) {
|
|
976
|
+
if (w.inFlight !== void 0) {
|
|
977
|
+
w.news = true;
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
if (w.paging) return;
|
|
981
|
+
if (this.#seeding(w.topic)) return;
|
|
982
|
+
const tw = this.#topics.get(w.topic);
|
|
983
|
+
if (!tw) return;
|
|
984
|
+
const engine = this.#engine;
|
|
985
|
+
const tail = engine.tail(w.topic);
|
|
986
|
+
let b = w.building;
|
|
987
|
+
if (!b) {
|
|
988
|
+
if (w.cursor !== void 0) b = {
|
|
989
|
+
at: Math.max(w.cursor, tail),
|
|
990
|
+
newest: /* @__PURE__ */ new Map(),
|
|
991
|
+
base: void 0,
|
|
992
|
+
i: 0,
|
|
993
|
+
all: false
|
|
994
|
+
};
|
|
995
|
+
else if (tw.snap) {
|
|
996
|
+
const base = [];
|
|
997
|
+
for (const [k, offset] of tw.snap.keys) if (offset >= tail && (!w.keys || w.keys.has(k))) base.push([k, offset]);
|
|
998
|
+
b = {
|
|
999
|
+
at: Math.max(tw.snap.upTo, tail),
|
|
1000
|
+
newest: /* @__PURE__ */ new Map(),
|
|
1001
|
+
base,
|
|
1002
|
+
i: 0,
|
|
1003
|
+
all: false
|
|
1004
|
+
};
|
|
1005
|
+
} else b = {
|
|
1006
|
+
at: tail,
|
|
1007
|
+
newest: /* @__PURE__ */ new Map(),
|
|
1008
|
+
base: void 0,
|
|
1009
|
+
i: 0,
|
|
1010
|
+
all: true
|
|
1011
|
+
};
|
|
1012
|
+
w.building = b;
|
|
1013
|
+
}
|
|
1014
|
+
w.news = false;
|
|
1015
|
+
if (b.base) for (; b.i < b.base.length; b.i++) {
|
|
1016
|
+
const entry = b.base[b.i];
|
|
1017
|
+
if (!entry) continue;
|
|
1018
|
+
const [k, offset] = entry;
|
|
1019
|
+
if (!engine.readable(w.topic, offset)) return this.#pageIn(w, offset);
|
|
1020
|
+
const rec = engine.read(w.topic, offset, 1)[0];
|
|
1021
|
+
if (rec?.offset === offset) b.newest.set(k, rec);
|
|
1022
|
+
}
|
|
1023
|
+
const head = engine.head(w.topic);
|
|
1024
|
+
while (b.at < head) {
|
|
1025
|
+
if (!engine.readable(w.topic, b.at)) return this.#pageIn(w, b.at);
|
|
1026
|
+
const records = engine.read(w.topic, b.at, SCAN_BATCH);
|
|
1027
|
+
const last = records.at(-1);
|
|
1028
|
+
if (!last) {
|
|
1029
|
+
b.at = head;
|
|
1030
|
+
break;
|
|
1031
|
+
}
|
|
1032
|
+
for (const r of records) {
|
|
1033
|
+
const k = r.key ?? null;
|
|
1034
|
+
if (b.all || !w.keys || w.keys.has(k)) b.newest.set(k, r);
|
|
1035
|
+
}
|
|
1036
|
+
b.at = last.offset + 1;
|
|
1037
|
+
}
|
|
1038
|
+
w.building = void 0;
|
|
1039
|
+
if (b.all) {
|
|
1040
|
+
const keys = /* @__PURE__ */ new Map();
|
|
1041
|
+
for (const [k, r] of b.newest) keys.set(k, r.offset);
|
|
1042
|
+
tw.snap = {
|
|
1043
|
+
keys,
|
|
1044
|
+
upTo: b.at
|
|
1045
|
+
};
|
|
1046
|
+
}
|
|
1047
|
+
this.#send(w, b, engine.tail(w.topic));
|
|
1048
|
+
}
|
|
1049
|
+
/** Sends what changed since the last view: values that differ from `sent`, and clears. */
|
|
1050
|
+
#send(w, b, tail) {
|
|
1051
|
+
const snapshot = w.cursor === void 0;
|
|
1052
|
+
const values = [];
|
|
1053
|
+
for (const [k, r] of b.newest) {
|
|
1054
|
+
if (w.keys && !w.keys.has(k)) continue;
|
|
1055
|
+
if (w.sent.get(k) !== r.offset) values.push({
|
|
1056
|
+
key: k,
|
|
1057
|
+
version: r.offset,
|
|
1058
|
+
value: r.value
|
|
1059
|
+
});
|
|
1060
|
+
}
|
|
1061
|
+
const cleared = [];
|
|
1062
|
+
if (snapshot) {
|
|
1063
|
+
for (const k of w.sent.keys()) if (w.keys && !w.keys.has(k)) w.sent.delete(k);
|
|
1064
|
+
else if (!b.newest.has(k)) cleared.push(k);
|
|
1065
|
+
} else if (tail !== w.checkedTail) {
|
|
1066
|
+
for (const [k, v] of w.sent) if (v < tail && !b.newest.has(k)) cleared.push(k);
|
|
1067
|
+
}
|
|
1068
|
+
w.checkedTail = tail;
|
|
1069
|
+
w.cursor = b.at;
|
|
1070
|
+
for (const k of cleared) w.sent.delete(k);
|
|
1071
|
+
for (const v of values) w.sent.set(v.key, v.version);
|
|
1072
|
+
if (this.#dev && !w.keys && !w.warned && w.sent.size > 1e3) {
|
|
1073
|
+
w.warned = true;
|
|
1074
|
+
console.warn(`[isoloop] isolate "${w.watcher.isolateId}" watches every key of "${w.topic}" and holds ${w.sent.size} of them, over ${WIDE_WATCH_KEYS}; pass keys to watch only the ones it shows.`);
|
|
1075
|
+
}
|
|
1076
|
+
if (!snapshot && values.length === 0 && cleared.length === 0) return;
|
|
1077
|
+
if (!snapshot) this.#count(w.topic).values += values.length;
|
|
1078
|
+
const size = snapshot ? 512 : Math.max(values.length, 1);
|
|
1079
|
+
let i = 0;
|
|
1080
|
+
do {
|
|
1081
|
+
const part = values.slice(i, i + size);
|
|
1082
|
+
const p = {
|
|
1083
|
+
topic: w.topic,
|
|
1084
|
+
vseq: ++w.vseq,
|
|
1085
|
+
values: part
|
|
1086
|
+
};
|
|
1087
|
+
if (i === 0 && cleared.length > 0) p.cleared = cleared;
|
|
1088
|
+
if (i + size < values.length) p.more = true;
|
|
1089
|
+
w.watcher.port.post({
|
|
1090
|
+
v: 1,
|
|
1091
|
+
t: "view",
|
|
1092
|
+
p
|
|
1093
|
+
});
|
|
1094
|
+
i += size;
|
|
1095
|
+
} while (i < values.length);
|
|
1096
|
+
w.inFlight = w.vseq;
|
|
1097
|
+
}
|
|
1098
|
+
};
|
|
761
1099
|
/**
|
|
762
1100
|
* Seed progress, kept as committed offsets of reserved consumers on the
|
|
763
1101
|
* seeding topic. Offsets are written after the records of the same group
|
|
@@ -936,6 +1274,8 @@ const hasTopic = (p) => typeof p === "object" && p !== null && "topic" in p && t
|
|
|
936
1274
|
const isOffset = (x) => typeof x === "number" && Number.isInteger(x) && x >= 0;
|
|
937
1275
|
/** A commit's `seq`: a non-negative integer, 0 before any delivery is settled whole. */
|
|
938
1276
|
const isSeq = isOffset;
|
|
1277
|
+
const isWatchKey = (x) => x === null || typeof x === "string";
|
|
1278
|
+
const isHave = (x) => Array.isArray(x) && x.length === 2 && isWatchKey(x[0]) && isOffset(x[1]);
|
|
939
1279
|
/** FNV-1a: a small, stable string hash for key → group member. */
|
|
940
1280
|
function hash(s) {
|
|
941
1281
|
let h = 2166136261;
|
|
@@ -1004,6 +1344,8 @@ var Broker = class {
|
|
|
1004
1344
|
#dropped = 0;
|
|
1005
1345
|
/** Inspection taps, with the topic patterns each one matches. */
|
|
1006
1346
|
#taps = /* @__PURE__ */ new Map();
|
|
1347
|
+
/** Watches: the current value per key, read from the log (ADR 0023). */
|
|
1348
|
+
#watches;
|
|
1007
1349
|
constructor(opts) {
|
|
1008
1350
|
this.#engine = opts.engine;
|
|
1009
1351
|
this.#now = opts.now ?? Date.now;
|
|
@@ -1014,6 +1356,12 @@ var Broker = class {
|
|
|
1014
1356
|
this.#durable = opts.durable ?? ((topic) => this.#engine.durable(topic));
|
|
1015
1357
|
this.#onWake = opts.onWake ?? (() => {});
|
|
1016
1358
|
this.#onSeed = opts.onSeed ?? (() => {});
|
|
1359
|
+
this.#watches = new WatchBook({
|
|
1360
|
+
engine: this.#engine,
|
|
1361
|
+
seeding: (topic) => this.#topics.get(topic)?.seeding !== void 0,
|
|
1362
|
+
timers: this.#timers,
|
|
1363
|
+
dev: this.#dev
|
|
1364
|
+
});
|
|
1017
1365
|
for (const def of [...opts.systemTopics ?? SYSTEM_TOPICS, ...opts.contracts.topics]) {
|
|
1018
1366
|
this.#engine.createTopic(topicConfig(def));
|
|
1019
1367
|
this.#topics.set(def.name, {
|
|
@@ -1088,7 +1436,8 @@ var Broker = class {
|
|
|
1088
1436
|
retained: this.#engine.retained(topic)
|
|
1089
1437
|
})),
|
|
1090
1438
|
subscriptions,
|
|
1091
|
-
rejected: { ...this.#rejected }
|
|
1439
|
+
rejected: { ...this.#rejected },
|
|
1440
|
+
watches: this.#watches.stats()
|
|
1092
1441
|
};
|
|
1093
1442
|
}
|
|
1094
1443
|
/**
|
|
@@ -1254,6 +1603,7 @@ var Broker = class {
|
|
|
1254
1603
|
const held = topic.seeding ?? [];
|
|
1255
1604
|
topic.seeding = void 0;
|
|
1256
1605
|
this.#drain(name, topic);
|
|
1606
|
+
this.#watches.opened(name);
|
|
1257
1607
|
for (const h of held) {
|
|
1258
1608
|
if (this.#conns.get(h.conn.isolateId) !== h.conn) continue;
|
|
1259
1609
|
h.reply(this.#subscribe(h.conn, name, h.from, h.group));
|
|
@@ -1262,7 +1612,10 @@ var Broker = class {
|
|
|
1262
1612
|
}
|
|
1263
1613
|
/** Retention pass over every topic (`time` and `compacted`); the bus worker runs it every second. */
|
|
1264
1614
|
compact() {
|
|
1265
|
-
for (const name of this.#topics.keys())
|
|
1615
|
+
for (const name of this.#topics.keys()) {
|
|
1616
|
+
this.#engine.compact(name);
|
|
1617
|
+
this.#watches.retained(name);
|
|
1618
|
+
}
|
|
1266
1619
|
}
|
|
1267
1620
|
/**
|
|
1268
1621
|
* Connects an isolate. A second attach for the same id replaces the first.
|
|
@@ -1277,7 +1630,8 @@ var Broker = class {
|
|
|
1277
1630
|
port,
|
|
1278
1631
|
subs: /* @__PURE__ */ new Map(),
|
|
1279
1632
|
warned: /* @__PURE__ */ new Set(),
|
|
1280
|
-
reset: opts.reattach ? /* @__PURE__ */ new Set() : void 0
|
|
1633
|
+
reset: opts.reattach ? /* @__PURE__ */ new Set() : void 0,
|
|
1634
|
+
rewatch: opts.reattach ? /* @__PURE__ */ new Set() : void 0
|
|
1281
1635
|
};
|
|
1282
1636
|
this.#conns.set(isolateId, conn);
|
|
1283
1637
|
port.onMessage((msg) => {
|
|
@@ -1300,6 +1654,7 @@ var Broker = class {
|
|
|
1300
1654
|
this.#leave(sub);
|
|
1301
1655
|
}
|
|
1302
1656
|
conn.subs.clear();
|
|
1657
|
+
for (const topic of this.#watches.drop(conn)) if (opts.wake) this.#sleep(topic, isolateId);
|
|
1303
1658
|
if (!keepOffsets) for (const name of this.#topics.keys()) this.#forgotten.add(`${name}\u0000${isolateId}`);
|
|
1304
1659
|
conn.port.close();
|
|
1305
1660
|
}
|
|
@@ -1341,6 +1696,7 @@ var Broker = class {
|
|
|
1341
1696
|
subs: /* @__PURE__ */ new Map(),
|
|
1342
1697
|
warned: /* @__PURE__ */ new Set(),
|
|
1343
1698
|
reset: void 0,
|
|
1699
|
+
rewatch: void 0,
|
|
1344
1700
|
host: true
|
|
1345
1701
|
},
|
|
1346
1702
|
id: opts.id,
|
|
@@ -1439,6 +1795,16 @@ var Broker = class {
|
|
|
1439
1795
|
});
|
|
1440
1796
|
return;
|
|
1441
1797
|
}
|
|
1798
|
+
case "watch":
|
|
1799
|
+
reply(this.#watch(conn, msg.p.topic, msg.p.keys, msg.p.have));
|
|
1800
|
+
return;
|
|
1801
|
+
case "unwatch":
|
|
1802
|
+
this.#watches.unwatch(conn, msg.p.topic);
|
|
1803
|
+
reply({
|
|
1804
|
+
ok: true,
|
|
1805
|
+
value: null
|
|
1806
|
+
});
|
|
1807
|
+
return;
|
|
1442
1808
|
case "commits": {
|
|
1443
1809
|
const offsets = msg.p.offsets;
|
|
1444
1810
|
if (!Array.isArray(offsets)) {
|
|
@@ -1453,6 +1819,13 @@ var Broker = class {
|
|
|
1453
1819
|
const r = typeof e === "object" && e !== null && "topic" in e && typeof e.topic === "string" ? this.#commit(conn, e.topic, "offset" in e ? e.offset : void 0, "seq" in e ? e.seq : void 0, "partial" in e ? e.partial : void 0) : fail("invalid", "each commit needs a topic, an offset and a seq");
|
|
1454
1820
|
if (!r.ok && first.ok) first = r;
|
|
1455
1821
|
}
|
|
1822
|
+
const seen = msg.p.seen;
|
|
1823
|
+
if (seen !== void 0 && !Array.isArray(seen)) {
|
|
1824
|
+
if (first.ok) first = fail("invalid", "seen must be an array");
|
|
1825
|
+
} else for (const e of seen ?? []) {
|
|
1826
|
+
const r = typeof e === "object" && e !== null && "topic" in e && typeof e.topic === "string" ? this.#watches.seen(conn, e.topic, "vseq" in e ? e.vseq : void 0) : fail("invalid", "each seen needs a topic and a vseq");
|
|
1827
|
+
if (!r.ok && first.ok) first = r;
|
|
1828
|
+
}
|
|
1456
1829
|
reply(first);
|
|
1457
1830
|
return;
|
|
1458
1831
|
}
|
|
@@ -1577,6 +1950,7 @@ var Broker = class {
|
|
|
1577
1950
|
});
|
|
1578
1951
|
this.#dropEvicted(name);
|
|
1579
1952
|
this.#fanOut(name);
|
|
1953
|
+
this.#watches.appended(name);
|
|
1580
1954
|
this.#wake(name);
|
|
1581
1955
|
this.#follow(name, {
|
|
1582
1956
|
...rec,
|
|
@@ -1769,11 +2143,33 @@ var Broker = class {
|
|
|
1769
2143
|
p: r
|
|
1770
2144
|
});
|
|
1771
2145
|
}
|
|
2146
|
+
/**
|
|
2147
|
+
* Starts a watch: refused without the subscribe grant, as `subscribe` is
|
|
2148
|
+
* (point 10). On a connection reattached after a bus restart, the first
|
|
2149
|
+
* watch of a memory topic treats `have` as naming records the old bus
|
|
2150
|
+
* lost, so the snapshot sends every key again (point 23).
|
|
2151
|
+
*/
|
|
2152
|
+
#watch(conn, name, keys, have) {
|
|
2153
|
+
if (!isSystemTopic(name) && !matchAny(conn.grants.subscribe, name)) return fail("forbidden", `no subscribe grant for ${name}`);
|
|
2154
|
+
if (!this.#topics.has(name)) return fail("unknown_topic", `unknown topic ${name}`);
|
|
2155
|
+
if (keys !== void 0 && !(Array.isArray(keys) && keys.every(isWatchKey))) return fail("invalid", "keys must be an array of strings or null");
|
|
2156
|
+
if (have !== void 0 && !(Array.isArray(have) && have.every(isHave))) return fail("invalid", "have must be an array of [key, version] pairs");
|
|
2157
|
+
let stale = false;
|
|
2158
|
+
if (conn.rewatch && !conn.rewatch.has(name) && !this.#durable(name)) {
|
|
2159
|
+
conn.rewatch.add(name);
|
|
2160
|
+
stale = true;
|
|
2161
|
+
}
|
|
2162
|
+
this.#watches.watch(conn, name, keys, have, stale);
|
|
2163
|
+
return {
|
|
2164
|
+
ok: true,
|
|
2165
|
+
value: null
|
|
2166
|
+
};
|
|
2167
|
+
}
|
|
1772
2168
|
#warnRepeatSubscribe(conn, topic) {
|
|
1773
2169
|
if (conn.warned.has(topic)) return;
|
|
1774
2170
|
if (!(this.#topics.get(topic)?.seeding?.some((h) => h.conn === conn) ?? false) && !conn.subs.has(topic)) return;
|
|
1775
2171
|
conn.warned.add(topic);
|
|
1776
|
-
console.warn(`[isoloop] isolate "${conn.isolateId}" subscribed to "${topic}" again; the newer subscription replaced the older one (a second
|
|
2172
|
+
console.warn(`[isoloop] isolate "${conn.isolateId}" subscribed to "${topic}" again; the newer subscription replaced the older one (a second bus.subscribe on this topic, or one beside a useTopic).`);
|
|
1777
2173
|
}
|
|
1778
2174
|
#subscribe(conn, name, from, group) {
|
|
1779
2175
|
if (!isStartPosition(from)) return fail("invalid", "from must be latest, earliest or committed");
|
|
@@ -3699,6 +4095,18 @@ function contractsCode(e) {
|
|
|
3699
4095
|
const code = e.code;
|
|
3700
4096
|
return typeof code === "string" && CONTRACT_CODES.has(code) ? code : void 0;
|
|
3701
4097
|
}
|
|
4098
|
+
/** A topic row's watcher figures (ADR 0023, point 25), once it has had a watch. */
|
|
4099
|
+
function watchRow(w) {
|
|
4100
|
+
if (!w) return {};
|
|
4101
|
+
const { watches, liveKeys, records, values } = w;
|
|
4102
|
+
return { watch: {
|
|
4103
|
+
watches,
|
|
4104
|
+
liveKeys,
|
|
4105
|
+
transport: "messages",
|
|
4106
|
+
records,
|
|
4107
|
+
values
|
|
4108
|
+
} };
|
|
4109
|
+
}
|
|
3702
4110
|
/** Topic configs as a canonical string, so key order doesn't matter. */
|
|
3703
4111
|
function canonical(cfg) {
|
|
3704
4112
|
return JSON.stringify(cfg, (_key, v) => {
|
|
@@ -4059,13 +4467,15 @@ function serve(scope, opts = {}) {
|
|
|
4059
4467
|
const t = now();
|
|
4060
4468
|
const secs = Math.max(.001, (t - at) / 1e3);
|
|
4061
4469
|
const before = new Map(prev.topics.map((x) => [x.topic, x]));
|
|
4470
|
+
const watched = new Map(stats.watches.map((x) => [x.topic, x]));
|
|
4062
4471
|
const metrics = {
|
|
4063
4472
|
topics: stats.topics.map((x) => ({
|
|
4064
4473
|
topic: x.topic,
|
|
4065
4474
|
recordsPerSec: (x.records - (before.get(x.topic)?.records ?? 0)) / secs,
|
|
4066
4475
|
bytesPerSec: (x.bytes - (before.get(x.topic)?.bytes ?? 0)) / secs,
|
|
4067
4476
|
head: x.head,
|
|
4068
|
-
retained: x.retained
|
|
4477
|
+
retained: x.retained,
|
|
4478
|
+
...watchRow(watched.get(x.topic))
|
|
4069
4479
|
})),
|
|
4070
4480
|
subscriptions: stats.subscriptions,
|
|
4071
4481
|
rejected: stats.rejected,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isoloop/bus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-next.0",
|
|
4
4
|
"description": "The Isoloop message bus: typed topics in a worker, with retention, replay and durable history.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"isoloop",
|
|
@@ -41,11 +41,11 @@
|
|
|
41
41
|
}
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@isoloop/codec": "0.
|
|
45
|
-
"@isoloop/protocol": "0.
|
|
44
|
+
"@isoloop/codec": "0.2.0-next.0",
|
|
45
|
+
"@isoloop/protocol": "0.2.0-next.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"fast-check": "4.10.
|
|
48
|
+
"fast-check": "4.10.3",
|
|
49
49
|
"typescript": "7.0.2",
|
|
50
50
|
"valibot": "1.5.0",
|
|
51
51
|
"zod": "4.6.7"
|