effect-durable-agent 0.1.0-alpha.9 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/assert-never-CoyUpoRB.js +18 -0
- package/dist/assert-never-CoyUpoRB.js.map +1 -0
- package/dist/{commands-Ce1ec6kH.d.ts → commands-BUquEZVP.d.ts} +31 -12
- package/dist/{compaction-BgDr5VJy.js → compaction-63iEDHHl.js} +63 -13
- package/dist/compaction-63iEDHHl.js.map +1 -0
- package/dist/{context-projection-n6Rdup0N.d.ts → context-projection-Bezx3drD.d.ts} +16 -40
- package/dist/{core-CYT6DBaS.d.ts → core-BcimKMIv.d.ts} +5 -3
- package/dist/delivery-2ZILvoRD.js +507 -0
- package/dist/delivery-2ZILvoRD.js.map +1 -0
- package/dist/delivery-D3MxW4wg.d.ts +3026 -0
- package/dist/domain/command-queues.d.ts +81 -2
- package/dist/domain/command-queues.js +1 -1
- package/dist/domain/command-queues.js.map +1 -1
- package/dist/domain/context-projection.d.ts +1 -1
- package/dist/domain/context-projection.js +1 -1
- package/dist/domain/context-projection.js.map +1 -1
- package/dist/domain/message-transcript.d.ts +36 -2
- package/dist/domain/message-transcript.js +10 -2
- package/dist/domain/message-transcript.js.map +1 -1
- package/dist/domain/model-usage.d.ts +1 -1
- package/dist/domain/model-usage.js +1 -1
- package/dist/domain/reduced-state-schema.d.ts +3 -992
- package/dist/domain/reduced-state-schema.js +126 -7
- package/dist/domain/reduced-state-schema.js.map +1 -1
- package/dist/domain/reduced-state.d.ts +2 -2
- package/dist/domain/reduced-state.js +936 -2
- package/dist/domain/reduced-state.js.map +1 -0
- package/dist/domain/run-scheduling.d.ts +2 -0
- package/dist/domain/run-scheduling.js +127 -0
- package/dist/domain/run-scheduling.js.map +1 -0
- package/dist/{durable-br2C1bWK.d.ts → durable-168Womqp.d.ts} +8535 -2859
- package/dist/{durable-BGFvX7Mh.js → durable-D13w1Qr3.js} +229 -15
- package/dist/durable-D13w1Qr3.js.map +1 -0
- package/dist/{envelope-DxQzo5rN.js → envelope-BCzKUq_z.js} +12 -3
- package/dist/envelope-BCzKUq_z.js.map +1 -0
- package/dist/{envelope-DkZyMIgp.d.ts → envelope-Che6KatU.d.ts} +70 -7
- package/dist/{event-factory-DTFdy0Cb.js → event-factory-_vF1bIJ7.js} +7 -3
- package/dist/event-factory-_vF1bIJ7.js.map +1 -0
- package/dist/{events-LZFNoESe.d.ts → events-DwTARLMv.d.ts} +139 -137
- package/dist/{id-generator-Ckl8_XWW.d.ts → id-generator-BKGoKjt-.d.ts} +3 -3
- package/dist/in-memory-CeODnZhE.js +150 -0
- package/dist/in-memory-CeODnZhE.js.map +1 -0
- package/dist/index.d.ts +8 -8
- package/dist/index.js +6 -6
- package/dist/interpreter-DKGJ1sSx.js +1452 -0
- package/dist/interpreter-DKGJ1sSx.js.map +1 -0
- package/dist/{keep-alive-Chl5SJxy.d.ts → keep-alive-_zFCUvt3.d.ts} +1 -2
- package/dist/{model-resolver-BPPJAsuh.d.ts → model-resolver-DeBDK8XO.d.ts} +11 -5
- package/dist/{model-usage-CdBEkTwU.d.ts → model-usage-0GeHwaaU.d.ts} +2 -3
- package/dist/records--54mZEBx.js +675 -0
- package/dist/records--54mZEBx.js.map +1 -0
- package/dist/reduced-state-B1cvy8N_.d.ts +51110 -0
- package/dist/reduced-state-schema-B98exWxQ.d.ts +2338 -0
- package/dist/{reducer-registry-aFjz_ZLT.d.ts → reducer-registry-DhGVzHBV.d.ts} +12 -6
- package/dist/run-scheduling-wakeup-Mjic4tb_.d.ts +24 -0
- package/dist/services/compaction.d.ts +2 -2
- package/dist/services/compaction.js +2 -2
- package/dist/services/id-generator.d.ts +1 -1
- package/dist/services/id-generator.js +6 -12
- package/dist/services/id-generator.js.map +1 -1
- package/dist/services/keep-alive.d.ts +1 -1
- package/dist/services/keep-alive.js.map +1 -1
- package/dist/services/model-call.d.ts +15 -0
- package/dist/services/model-call.js +15 -0
- package/dist/services/model-call.js.map +1 -0
- package/dist/services/model-resolver.d.ts +1 -1
- package/dist/services/model-resolver.js +1 -1
- package/dist/services/model-resolver.js.map +1 -1
- package/dist/services/prompt-projector.d.ts +2 -2
- package/dist/services/prompt-projector.js +49 -5
- package/dist/services/prompt-projector.js.map +1 -1
- package/dist/services/reducer-registry.d.ts +1 -1
- package/dist/services/reducer-registry.js +19 -6
- package/dist/services/reducer-registry.js.map +1 -1
- package/dist/services/run-scheduler.d.ts +2 -2
- package/dist/services/run-scheduler.js +25 -2
- package/dist/services/run-scheduler.js.map +1 -0
- package/dist/services/run-scheduling-wakeup.d.ts +2 -0
- package/dist/services/run-scheduling-wakeup.js +13 -0
- package/dist/services/run-scheduling-wakeup.js.map +1 -0
- package/dist/services/runtime-layer.d.ts +17 -33
- package/dist/services/runtime-layer.js +19 -14
- package/dist/services/runtime-layer.js.map +1 -1
- package/dist/services/runtime.d.ts +1 -1
- package/dist/services/runtime.js +31 -20
- package/dist/services/runtime.js.map +1 -1
- package/dist/services/session-event-observer.d.ts +1 -1
- package/dist/services/session-event-observer.js.map +1 -1
- package/dist/services/session-query.d.ts +1 -1
- package/dist/services/session-query.js +57 -10
- package/dist/services/session-query.js.map +1 -1
- package/dist/services/session-state.d.ts +1 -1
- package/dist/services/session-state.js +1 -1
- package/dist/services/session-store.d.ts +2 -2
- package/dist/services/session-store.js +127 -37
- package/dist/services/session-store.js.map +1 -1
- package/dist/services/sink-checkpoint-store.d.ts +1 -1
- package/dist/services/sink-checkpoint-store.js +2 -1
- package/dist/services/sink-checkpoint-store.js.map +1 -1
- package/dist/services/sink-registry.d.ts +2 -2
- package/dist/services/sink-registry.js +1 -1
- package/dist/services/span-names.d.ts +1 -1
- package/dist/services/span-names.js +6 -1
- package/dist/services/span-names.js.map +1 -1
- package/dist/services/tool-registry.d.ts +2 -2
- package/dist/services/tool-registry.js +12 -3
- package/dist/services/tool-registry.js.map +1 -1
- package/dist/services/tracing.d.ts +3 -3
- package/dist/services/tracing.js +2 -2
- package/dist/services/tracing.js.map +1 -1
- package/dist/{session-event-observer-DaFMKKNc.d.ts → session-event-observer-DlP9ITB2.d.ts} +3 -3
- package/dist/{session-state-Bkc6u-Wr.js → session-state-BehvUVyq.js} +659 -164
- package/dist/session-state-BehvUVyq.js.map +1 -0
- package/dist/session-store-BL6sfrnn.d.ts +3326 -0
- package/dist/{sink-checkpoint-store-M7qIxoSH.d.ts → sink-checkpoint-store-DvuHwvx_.d.ts} +3 -4
- package/dist/sink-registry-DskRcyOS.js +345 -0
- package/dist/sink-registry-DskRcyOS.js.map +1 -0
- package/dist/subscription-DZNWFDTV.d.ts +966 -0
- package/dist/subscription-DmO3BcdT.js +505 -0
- package/dist/subscription-DmO3BcdT.js.map +1 -0
- package/dist/testkit/layers.d.ts +30 -23
- package/dist/testkit/layers.js +23 -15
- package/dist/testkit/layers.js.map +1 -1
- package/dist/testkit/transcript.d.ts +226 -0
- package/dist/testkit/transcript.js +1475 -0
- package/dist/testkit/transcript.js.map +1 -0
- package/dist/{tool-registry-BJaVNtw4.d.ts → tool-registry-CHHUU9BN.d.ts} +44 -8
- package/dist/{tracing-BXHAkxkx.d.ts → tracing-BwjbF285.d.ts} +1 -2
- package/dist/transcript.d.ts +101 -0
- package/dist/transcript.js +107 -0
- package/dist/transcript.js.map +1 -0
- package/dist/types/commands.d.ts +2 -2
- package/dist/types/commands.js +8 -3
- package/dist/types/commands.js.map +1 -1
- package/dist/types/core.d.ts +2 -2
- package/dist/types/core.js +3 -1
- package/dist/types/core.js.map +1 -1
- package/dist/types/events/durable.d.ts +2 -2
- package/dist/types/events/durable.js +2 -2
- package/dist/types/events.d.ts +5 -5
- package/dist/types/events.js +3 -3
- package/dist/types/tracing.d.ts +1 -1
- package/dist/types/tracing.js.map +1 -1
- package/dist/websocket/events.d.ts +2 -0
- package/dist/websocket/events.js +2 -0
- package/dist/websocket.d.ts +2123 -2552
- package/dist/websocket.js +628 -442
- package/dist/websocket.js.map +1 -1
- package/dist/window-read-Brov9VdQ.d.ts +113 -0
- package/dist/window-read-DPFAA1hM.js +264 -0
- package/dist/window-read-DPFAA1hM.js.map +1 -0
- package/package.json +37 -13
- package/dist/command-queues-DdDB_aYM.d.ts +0 -462
- package/dist/compaction-BgDr5VJy.js.map +0 -1
- package/dist/compaction-ch8z6G9x.d.ts +0 -165
- package/dist/durable-BGFvX7Mh.js.map +0 -1
- package/dist/envelope-DxQzo5rN.js.map +0 -1
- package/dist/event-factory-DTFdy0Cb.js.map +0 -1
- package/dist/event-factory-Di1jwQvm.d.ts +0 -87
- package/dist/index-BVVgDSdq.d.ts +0 -1
- package/dist/message-transcript-DQNlzxle.d.ts +0 -32
- package/dist/prompt-projector-f4ElyXCE.d.ts +0 -39
- package/dist/reduced-state-DF0-6mbR.js +0 -921
- package/dist/reduced-state-DF0-6mbR.js.map +0 -1
- package/dist/run-scheduler-CAt7dF6C.js +0 -21
- package/dist/run-scheduler-CAt7dF6C.js.map +0 -1
- package/dist/run-scheduler-xkhZyto5.d.ts +0 -44
- package/dist/runtime-DDSYP9jS.d.ts +0 -63
- package/dist/session-query-B72HY_0L.d.ts +0 -45
- package/dist/session-state-Bkc6u-Wr.js.map +0 -1
- package/dist/session-state-C9YLSAso.d.ts +0 -862
- package/dist/session-store-Cpi_NwTR.d.ts +0 -206
- package/dist/sink-registry-Bl-vQES6.d.ts +0 -133
- package/dist/sink-registry-NDgeyzO0.js +0 -272
- package/dist/sink-registry-NDgeyzO0.js.map +0 -1
package/dist/websocket.js
CHANGED
|
@@ -1,498 +1,684 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import * as Effect from "effect/Effect";
|
|
1
|
+
import { SequenceNumber } from "./types/core.js";
|
|
2
|
+
import { E as TranscriptOrder, _ as TranscriptCursor } from "./records--54mZEBx.js";
|
|
3
|
+
import { c as transcriptCoverageOf, i as TranscriptUpdate, n as TranscriptRequestId, r as TranscriptSubscriptionBase, t as TranscriptCoverage } from "./subscription-DmO3BcdT.js";
|
|
4
|
+
import { A as FrameId, B as EDAWebSocketWireFlowControl, C as EDAWebSocketServerFrame, D as EDA_WS_CLOSE_PROTOCOL_ERROR, E as EDA_WS_CLOSE_LAGGED, F as laggedCloseReason, G as EDAWebSocketWirePongFrame, H as EDAWebSocketWireHelloFrame, I as EDAFrameworkWebSocketEvent, J as EDA_WEB_SOCKET_WIRE_PROTOCOL_VERSION, K as EDA_WEB_SOCKET_PING_MESSAGE, L as EDAWebSocketWireAckFrame, M as decodeEDAWebSocketClientMessage, N as defaultEDAWebSocketFlowControl, O as EDA_WS_CLOSE_SEND_FAILED, P as encodeEDAWebSocketServerFrame, R as EDAWebSocketWireClientFrame, S as EDAWebSocketPongFrame, T as EDA_WS_CLOSE_GOING_AWAY, U as EDAWebSocketWireLaggedFrame, V as EDAWebSocketWireHeartbeatFrame, W as EDAWebSocketWirePingFrame, X as makeEDAWebSocketWireProtocol, Y as edaFrameworkWebSocketWireProtocol, _ as EDAWebSocketFlowControl, a as checkpointWebSocketDeliveryState, b as EDAWebSocketLaggedFrame, c as onClientAck, d as onHostSuppressedFrame, f as restoreWebSocketDeliveryState, g as EDAWebSocketEventsFrame, h as EDAWebSocketErrorFrame, i as SubscriberLagReason, j as SubscriberId, k as EDA_WS_CLOSE_UNSUPPORTED_DATA, l as onDurableEvents, m as EDAWebSocketClientFrame, n as EDAWebSocketInFlightFrame, o as deliveryHelloFrame, p as EDAWebSocketAckFrame, q as EDA_WEB_SOCKET_PONG_MESSAGE, r as EDAWebSocketSuppressedRange, s as makeWebSocketDeliveryState, t as EDAWebSocketDeliveryCheckpoint, u as onEphemeralEvent, v as EDAWebSocketHeartbeatFrame, w as EDA_WEB_SOCKET_PROTOCOL_VERSION, x as EDAWebSocketPingFrame, y as EDAWebSocketHelloFrame, z as EDAWebSocketWireErrorFrame } from "./delivery-2ZILvoRD.js";
|
|
6
5
|
import * as Schema from "effect/Schema";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
6
|
+
import * as Exit from "effect/Exit";
|
|
7
|
+
//#region src/websocket/transcript.ts
|
|
8
|
+
const PositiveInt$1 = Schema.Int.check(Schema.isGreaterThanOrEqualTo(1));
|
|
9
|
+
const NonNegativeInt$1 = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
|
|
10
|
+
/**
|
|
11
|
+
* Transcript protocol major. A change of meaning that an older client would misread bumps it;
|
|
12
|
+
* a client that sees another major performs one bounded reload.
|
|
13
|
+
*/
|
|
14
|
+
const EDA_TRANSCRIPT_PROTOCOL_MAJOR = 1;
|
|
15
|
+
/** Additive wire revision within a major; older clients drop fields they do not know. */
|
|
16
|
+
const EDA_TRANSCRIPT_WIRE_VERSION = 1;
|
|
17
|
+
/**
|
|
18
|
+
* The small, stable envelope a client decodes before any version-specific schema, from the
|
|
19
|
+
* socket hello and from the host's SSR data. Its shape never changes; everything else may.
|
|
20
|
+
*/
|
|
21
|
+
const EDATranscriptNegotiation = Schema.Struct({
|
|
22
|
+
protocolMajor: PositiveInt$1,
|
|
23
|
+
wireVersion: PositiveInt$1,
|
|
24
|
+
/** Optional features this server speaks; clients ignore names they do not know. */
|
|
25
|
+
capabilities: Schema.Array(Schema.String)
|
|
23
26
|
});
|
|
27
|
+
/** What this EDA release speaks. Hosts embed it in SSR data next to the transcript window. */
|
|
28
|
+
const edaTranscriptNegotiation = {
|
|
29
|
+
protocolMajor: 1,
|
|
30
|
+
wireVersion: 1,
|
|
31
|
+
capabilities: [
|
|
32
|
+
"window",
|
|
33
|
+
"page",
|
|
34
|
+
"retain",
|
|
35
|
+
"chunked-updates",
|
|
36
|
+
"active-content"
|
|
37
|
+
]
|
|
38
|
+
};
|
|
39
|
+
/** Any value carrying the envelope under `negotiation`: a hello frame or SSR data. */
|
|
40
|
+
const NegotiationCarrier = Schema.Struct({ negotiation: EDATranscriptNegotiation });
|
|
41
|
+
const decodeNegotiationCarrier = Schema.decodeUnknownOption(NegotiationCarrier);
|
|
24
42
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
|
|
28
|
-
|
|
43
|
+
* Decode only the negotiation envelope from an untrusted hello frame or SSR payload and compare
|
|
44
|
+
* it with this client's major. Run before decoding anything version-specific.
|
|
45
|
+
*/
|
|
46
|
+
const negotiateEDATranscript = (carrier, localMajor = 1) => {
|
|
47
|
+
const decoded = decodeNegotiationCarrier(carrier);
|
|
48
|
+
if (decoded._tag === "None") return { _tag: "Undecodable" };
|
|
49
|
+
const remote = decoded.value.negotiation;
|
|
50
|
+
return remote.protocolMajor === localMajor ? {
|
|
51
|
+
_tag: "Compatible",
|
|
52
|
+
remote
|
|
53
|
+
} : {
|
|
54
|
+
_tag: "MajorMismatch",
|
|
55
|
+
remote
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
/** WebSocket close code: the server lost volatile transfer state; reconnect and resubscribe. */
|
|
59
|
+
const EDA_WS_CLOSE_TRANSCRIPT_RESUBSCRIBE = 4009;
|
|
60
|
+
/** First frame on a transcript socket: the event-socket hello plus the negotiation envelope. */
|
|
61
|
+
const EDATranscriptHelloFrame = Schema.Struct({
|
|
62
|
+
...EDAWebSocketWireHelloFrame.fields,
|
|
63
|
+
negotiation: EDATranscriptNegotiation
|
|
64
|
+
});
|
|
65
|
+
/** A transcript update that fits one frame. */
|
|
66
|
+
const EDATranscriptUpdateFrame = Schema.Struct({
|
|
67
|
+
_tag: Schema.Literal("transcript"),
|
|
68
|
+
frameId: PositiveInt$1,
|
|
69
|
+
update: TranscriptUpdate
|
|
70
|
+
});
|
|
71
|
+
/**
|
|
72
|
+
* One slice of an update too large for a frame. `data` slices concatenated in `index` order are
|
|
73
|
+
* the JSON of one `TranscriptUpdate`; the client applies it only after slice `count - 1` (the
|
|
74
|
+
* completion marker) and discards a transfer that is interrupted.
|
|
75
|
+
*/
|
|
76
|
+
const EDATranscriptChunkFrame = Schema.Struct({
|
|
77
|
+
_tag: Schema.Literal("transcript-chunk"),
|
|
78
|
+
frameId: PositiveInt$1,
|
|
79
|
+
updateSeq: PositiveInt$1,
|
|
80
|
+
index: NonNegativeInt$1,
|
|
81
|
+
count: PositiveInt$1,
|
|
82
|
+
data: Schema.String
|
|
83
|
+
}).check(Schema.makeFilter((chunk) => chunk.index < chunk.count || "chunk index out of range"));
|
|
84
|
+
/** Every server frame a transcript socket may carry. */
|
|
85
|
+
const EDATranscriptServerFrame = Schema.Union([
|
|
86
|
+
EDATranscriptHelloFrame,
|
|
87
|
+
EDATranscriptUpdateFrame,
|
|
88
|
+
EDATranscriptChunkFrame,
|
|
89
|
+
EDAWebSocketWirePongFrame,
|
|
90
|
+
EDAWebSocketWireLaggedFrame,
|
|
91
|
+
EDAWebSocketWireErrorFrame
|
|
92
|
+
]);
|
|
93
|
+
/**
|
|
94
|
+
* Start or restart the subscription. After SSR, `range` is what the page rendered, `tail` its
|
|
95
|
+
* `!hasNewer`, and `since` its `throughSeq`; a reconnecting client sends what it last applied.
|
|
96
|
+
* Without `range` the server replies with the latest `count` records.
|
|
29
97
|
*/
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const EDAWebSocketWireHelloFrame = Schema.Struct({
|
|
38
|
-
_tag: Schema.Literal("hello"),
|
|
39
|
-
protocolVersion: Schema.Literal(1),
|
|
40
|
-
subscriberId: Schema.NonEmptyString,
|
|
41
|
-
resumeSeq: NonNegativeInt,
|
|
42
|
-
flowControl: EDAWebSocketWireFlowControl
|
|
98
|
+
const EDATranscriptSubscribeFrame = Schema.Struct({
|
|
99
|
+
_tag: Schema.Literal("subscribe"),
|
|
100
|
+
requestId: Schema.optionalKey(TranscriptRequestId),
|
|
101
|
+
range: Schema.optionalKey(TranscriptCoverage),
|
|
102
|
+
tail: Schema.optionalKey(Schema.Boolean),
|
|
103
|
+
since: Schema.optionalKey(SequenceNumber),
|
|
104
|
+
count: Schema.optionalKey(PositiveInt$1)
|
|
43
105
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
106
|
+
/** Extend the coverage from its edge: `cursor` is the coverage's `from` (before) or `to` (after). */
|
|
107
|
+
const EDATranscriptPageFrame = Schema.Struct({
|
|
108
|
+
_tag: Schema.Literal("page"),
|
|
109
|
+
requestId: Schema.optionalKey(TranscriptRequestId),
|
|
110
|
+
direction: Schema.Literals(["before", "after"]),
|
|
111
|
+
cursor: TranscriptCursor,
|
|
112
|
+
count: PositiveInt$1
|
|
48
113
|
});
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
114
|
+
/** Narrow the coverage to a span inside it, so the server stops sending changes outside it. */
|
|
115
|
+
const EDATranscriptRetainFrame = Schema.Struct({
|
|
116
|
+
_tag: Schema.Literal("retain"),
|
|
117
|
+
requestId: Schema.optionalKey(TranscriptRequestId),
|
|
118
|
+
from: TranscriptCursor,
|
|
119
|
+
to: TranscriptCursor
|
|
53
120
|
});
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
121
|
+
/** A client request; each is answered by exactly one update, in arrival order. */
|
|
122
|
+
const EDATranscriptRequestFrame = Schema.Union([
|
|
123
|
+
EDATranscriptSubscribeFrame,
|
|
124
|
+
EDATranscriptPageFrame,
|
|
125
|
+
EDATranscriptRetainFrame
|
|
126
|
+
]);
|
|
127
|
+
/** Every client frame a transcript socket accepts. */
|
|
128
|
+
const EDATranscriptClientFrame = Schema.Union([
|
|
129
|
+
EDAWebSocketWireAckFrame,
|
|
130
|
+
EDAWebSocketWirePingFrame,
|
|
131
|
+
EDATranscriptSubscribeFrame,
|
|
132
|
+
EDATranscriptPageFrame,
|
|
133
|
+
EDATranscriptRetainFrame
|
|
134
|
+
]);
|
|
135
|
+
const encodeUpdate = Schema.encodeSync(TranscriptUpdate);
|
|
136
|
+
const encodeServerFrame = Schema.encodeSync(EDATranscriptServerFrame);
|
|
137
|
+
/** Serialize one update once; frames embed this text, chunks slice it. */
|
|
138
|
+
const encodeEDATranscriptUpdate = (update) => JSON.stringify(encodeUpdate(update));
|
|
139
|
+
/** Serialize a non-update server frame. */
|
|
140
|
+
const encodeEDATranscriptServerFrame = (frame) => JSON.stringify(encodeServerFrame(frame));
|
|
141
|
+
/** The single-frame form of an encoded update. */
|
|
142
|
+
const edaTranscriptUpdateFrameText = (frameId, encodedUpdate) => `{"_tag":"transcript","frameId":${frameId},"update":${encodedUpdate}}`;
|
|
143
|
+
/** One chunk frame of an encoded update. */
|
|
144
|
+
const edaTranscriptChunkFrameText = (input) => `{"_tag":"transcript-chunk","frameId":${input.frameId},"updateSeq":${input.updateSeq},"index":${input.index},"count":${input.count},"data":${JSON.stringify(input.data)}}`;
|
|
145
|
+
const textEncoder = new TextEncoder();
|
|
146
|
+
/** UTF-8 byte length of a string. */
|
|
147
|
+
const utf8ByteLength = (text) => textEncoder.encode(text).byteLength;
|
|
148
|
+
/** Bytes a chunk frame spends besides its data, with generous room for large numbers. */
|
|
149
|
+
const chunkFrameOverheadBytes = 160;
|
|
150
|
+
/** Control characters JSON writes as a two-byte escape (`\b`, `\t`, `\n`, `\f`, `\r`). */
|
|
151
|
+
const shortEscapes = /* @__PURE__ */ new Set([
|
|
152
|
+
8,
|
|
153
|
+
9,
|
|
154
|
+
10,
|
|
155
|
+
12,
|
|
156
|
+
13
|
|
157
|
+
]);
|
|
158
|
+
/** UTF-8 bytes one UTF-16 code unit costs inside a JSON string literal. */
|
|
159
|
+
const jsonStringUnitBytes = (code) => {
|
|
160
|
+
if (code === 34 || code === 92) return 2;
|
|
161
|
+
if (code < 32) return shortEscapes.has(code) ? 2 : 6;
|
|
162
|
+
if (code < 128) return 1;
|
|
163
|
+
if (code < 2048) return 2;
|
|
164
|
+
if (code >= 55296 && code <= 57343) return 6;
|
|
165
|
+
return 3;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Split an encoded update into chunk data that each fit a frame of `maxFrameBytes`, never
|
|
169
|
+
* splitting a surrogate pair.
|
|
170
|
+
*/
|
|
171
|
+
const splitEDATranscriptUpdate = (encodedUpdate, maxFrameBytes) => {
|
|
172
|
+
const budget = Math.max(16, maxFrameBytes - chunkFrameOverheadBytes);
|
|
173
|
+
const chunks = [];
|
|
174
|
+
let start = 0;
|
|
175
|
+
let bytes = 0;
|
|
176
|
+
for (let index = 0; index < encodedUpdate.length; index += 1) {
|
|
177
|
+
const code = encodedUpdate.charCodeAt(index);
|
|
178
|
+
const pair = code >= 55296 && code <= 56319 && index + 1 < encodedUpdate.length ? encodedUpdate.charCodeAt(index + 1) : void 0;
|
|
179
|
+
const isPair = pair !== void 0 && pair >= 56320 && pair <= 57343;
|
|
180
|
+
const cost = isPair ? 4 : jsonStringUnitBytes(code);
|
|
181
|
+
if (bytes + cost > budget && index > start) {
|
|
182
|
+
chunks.push(encodedUpdate.slice(start, index));
|
|
183
|
+
start = index;
|
|
184
|
+
bytes = 0;
|
|
185
|
+
}
|
|
186
|
+
bytes += cost;
|
|
187
|
+
if (isPair) index += 1;
|
|
188
|
+
}
|
|
189
|
+
if (start < encodedUpdate.length || chunks.length === 0) chunks.push(encodedUpdate.slice(start));
|
|
190
|
+
return chunks;
|
|
191
|
+
};
|
|
192
|
+
//#endregion
|
|
193
|
+
//#region src/websocket/transcript-client.ts
|
|
194
|
+
/** A client that has rendered nothing yet. */
|
|
195
|
+
const emptyEDATranscriptClientState = {
|
|
196
|
+
window: void 0,
|
|
197
|
+
lastFrameId: 0,
|
|
198
|
+
appliedUpdateSeq: 0,
|
|
199
|
+
assembling: void 0
|
|
200
|
+
};
|
|
201
|
+
/** Seed a client from the window its page was server-rendered with. */
|
|
202
|
+
const edaTranscriptClientFromWindow = (window) => ({
|
|
203
|
+
...emptyEDATranscriptClientState,
|
|
204
|
+
window: {
|
|
205
|
+
incarnation: window.incarnation,
|
|
206
|
+
version: window.version,
|
|
207
|
+
throughSeq: window.throughSeq,
|
|
208
|
+
coverage: transcriptCoverageOf(window),
|
|
209
|
+
hasOlder: window.hasOlder,
|
|
210
|
+
hasNewer: window.hasNewer,
|
|
211
|
+
readiness: window.readiness,
|
|
212
|
+
records: window.records,
|
|
213
|
+
dependencies: window.dependencies,
|
|
214
|
+
activeContent: window.activeContent ?? []
|
|
215
|
+
}
|
|
57
216
|
});
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
217
|
+
/**
|
|
218
|
+
* Prepare for a new socket: per-socket counters restart and any partial transfer is discarded.
|
|
219
|
+
* The applied window is kept; the next `subscribe` asks for changes since it.
|
|
220
|
+
*/
|
|
221
|
+
const edaTranscriptClientReconnected = (state) => ({
|
|
222
|
+
...emptyEDATranscriptClientState,
|
|
223
|
+
window: state.window
|
|
62
224
|
});
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
225
|
+
const decodeUpdateFrame = Schema.decodeUnknownExit(EDATranscriptUpdateFrame);
|
|
226
|
+
const decodeChunkFrame = Schema.decodeUnknownExit(EDATranscriptChunkFrame);
|
|
227
|
+
const decodeUpdate = Schema.decodeUnknownExit(TranscriptUpdate);
|
|
228
|
+
const peek = (value) => {
|
|
229
|
+
if (typeof value !== "object" || value === null) return {};
|
|
230
|
+
const tag = "_tag" in value && typeof value._tag === "string" ? value._tag : void 0;
|
|
231
|
+
const frameId = "frameId" in value && typeof value.frameId === "number" && Number.isInteger(value.frameId) ? value.frameId : void 0;
|
|
232
|
+
return {
|
|
233
|
+
...tag === void 0 ? {} : { tag },
|
|
234
|
+
...frameId === void 0 ? {} : { frameId }
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
/** Apply one server text message. Frames must arrive in frame-id order without gaps. */
|
|
238
|
+
const applyEDATranscriptServerMessage = (state, message) => {
|
|
239
|
+
let json;
|
|
240
|
+
try {
|
|
241
|
+
json = JSON.parse(message);
|
|
242
|
+
} catch {
|
|
243
|
+
return resubscribe(state, "malformed server message");
|
|
244
|
+
}
|
|
245
|
+
const { tag, frameId } = peek(json);
|
|
246
|
+
switch (tag) {
|
|
247
|
+
case "hello": return {
|
|
248
|
+
state,
|
|
249
|
+
outcome: {
|
|
250
|
+
_tag: "Hello",
|
|
251
|
+
negotiation: negotiateEDATranscript(json)
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
case "pong": return {
|
|
255
|
+
state,
|
|
256
|
+
outcome: { _tag: "Ignored" }
|
|
257
|
+
};
|
|
258
|
+
case "lagged":
|
|
259
|
+
case "error": return resubscribe(state, `server sent ${tag}`);
|
|
260
|
+
}
|
|
261
|
+
if (frameId === void 0) return {
|
|
262
|
+
state,
|
|
263
|
+
outcome: { _tag: "Ignored" }
|
|
264
|
+
};
|
|
265
|
+
if (frameId !== state.lastFrameId + 1) return resubscribe(state, "frame gap");
|
|
266
|
+
const received = {
|
|
267
|
+
...state,
|
|
268
|
+
lastFrameId: frameId
|
|
269
|
+
};
|
|
270
|
+
if (tag === "transcript") {
|
|
271
|
+
const decoded = decodeUpdateFrame(json);
|
|
272
|
+
if (Exit.isFailure(decoded)) return resubscribe(state, "undecodable update");
|
|
273
|
+
if (received.assembling !== void 0) return resubscribe(state, "update interrupted a transfer");
|
|
274
|
+
return applyUpdate(received, decoded.value.update);
|
|
275
|
+
}
|
|
276
|
+
if (tag === "transcript-chunk") {
|
|
277
|
+
const decoded = decodeChunkFrame(json);
|
|
278
|
+
if (Exit.isFailure(decoded)) return resubscribe(state, "undecodable chunk");
|
|
279
|
+
const chunk = decoded.value;
|
|
280
|
+
const assembling = received.assembling ?? {
|
|
281
|
+
updateSeq: chunk.updateSeq,
|
|
282
|
+
count: chunk.count,
|
|
283
|
+
data: []
|
|
284
|
+
};
|
|
285
|
+
if (assembling.updateSeq !== chunk.updateSeq || assembling.count !== chunk.count || assembling.data.length !== chunk.index) return resubscribe(state, "interrupted chunked transfer");
|
|
286
|
+
const data = [...assembling.data, chunk.data];
|
|
287
|
+
if (data.length < chunk.count) {
|
|
288
|
+
const next = {
|
|
289
|
+
...received,
|
|
290
|
+
assembling: {
|
|
291
|
+
...assembling,
|
|
292
|
+
data
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
return {
|
|
296
|
+
state: next,
|
|
297
|
+
outcome: { _tag: "Partial" },
|
|
298
|
+
ack: ackOf(next)
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
let updateJson;
|
|
302
|
+
try {
|
|
303
|
+
updateJson = JSON.parse(data.join(""));
|
|
304
|
+
} catch {
|
|
305
|
+
return resubscribe(state, "malformed chunked update");
|
|
306
|
+
}
|
|
307
|
+
const update = decodeUpdate(updateJson);
|
|
308
|
+
if (Exit.isFailure(update) || update.value.updateSeq !== chunk.updateSeq) return resubscribe(state, "undecodable chunked update");
|
|
309
|
+
return applyUpdate({
|
|
310
|
+
...received,
|
|
311
|
+
assembling: void 0
|
|
312
|
+
}, update.value);
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
state: received,
|
|
316
|
+
outcome: { _tag: "Ignored" },
|
|
317
|
+
ack: ackOf(received)
|
|
318
|
+
};
|
|
319
|
+
};
|
|
320
|
+
const resubscribe = (state, reason) => ({
|
|
321
|
+
state: {
|
|
322
|
+
...state,
|
|
323
|
+
assembling: void 0
|
|
324
|
+
},
|
|
325
|
+
outcome: {
|
|
326
|
+
_tag: "Resubscribe",
|
|
327
|
+
reason
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
const ackOf = (state) => ({
|
|
331
|
+
_tag: "ack",
|
|
332
|
+
frameId: state.lastFrameId,
|
|
333
|
+
durableThroughSeq: state.appliedUpdateSeq === 0 ? 0 : state.window?.throughSeq ?? 0,
|
|
334
|
+
appliedUpdateSeq: state.appliedUpdateSeq
|
|
335
|
+
});
|
|
336
|
+
const orderKeyOf = (record) => "position" in record && record.position !== void 0 ? {
|
|
337
|
+
...record.position,
|
|
338
|
+
recordId: record.id
|
|
339
|
+
} : void 0;
|
|
340
|
+
const covered = (coverage, record) => {
|
|
341
|
+
const key = orderKeyOf(record);
|
|
342
|
+
return coverage !== void 0 && key !== void 0 && TranscriptOrder(coverage.from, key) <= 0 && TranscriptOrder(key, coverage.to) <= 0;
|
|
343
|
+
};
|
|
344
|
+
const applyUpdate = (state, update) => {
|
|
345
|
+
if (update.updateSeq !== state.appliedUpdateSeq + 1) return resubscribe(state, "update gap");
|
|
346
|
+
const previous = state.window;
|
|
347
|
+
if (update.mode === "patch" && (previous === void 0 || previous.incarnation !== update.incarnation || previous.version !== update.version)) return resubscribe(state, "patch without its base");
|
|
348
|
+
const base = update.mode === "patch" ? previous : void 0;
|
|
349
|
+
const topLevel = new Map((base?.records ?? []).map((record) => [record.id, record]));
|
|
350
|
+
for (const record of update.records) topLevel.set(record.id, record);
|
|
351
|
+
const records = [...topLevel.values()].filter((record) => covered(update.coverage, record)).sort((left, right) => {
|
|
352
|
+
const leftKey = orderKeyOf(left);
|
|
353
|
+
const rightKey = orderKeyOf(right);
|
|
354
|
+
return leftKey === void 0 || rightKey === void 0 ? 0 : TranscriptOrder(leftKey, rightKey);
|
|
70
355
|
});
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
356
|
+
const dependencyMap = new Map((base?.dependencies ?? []).map((record) => [record.id, record]));
|
|
357
|
+
for (const record of update.dependencies) dependencyMap.set(record.id, record);
|
|
358
|
+
const dependencies = update.dependencyIds.flatMap((id) => {
|
|
359
|
+
const record = dependencyMap.get(id);
|
|
360
|
+
return record === void 0 ? [] : [record];
|
|
76
361
|
});
|
|
362
|
+
if (dependencies.length !== update.dependencyIds.length) return resubscribe(state, "patch referenced a dependency the client does not hold");
|
|
363
|
+
const window = {
|
|
364
|
+
incarnation: update.incarnation,
|
|
365
|
+
version: update.version,
|
|
366
|
+
throughSeq: update.throughSeq,
|
|
367
|
+
coverage: update.coverage,
|
|
368
|
+
hasOlder: update.hasOlder,
|
|
369
|
+
hasNewer: update.hasNewer,
|
|
370
|
+
readiness: update.readiness,
|
|
371
|
+
records,
|
|
372
|
+
dependencies,
|
|
373
|
+
activeContent: update.readiness._tag !== "Ready" ? [] : update.activeContent ?? base?.activeContent ?? []
|
|
374
|
+
};
|
|
375
|
+
const next = {
|
|
376
|
+
...state,
|
|
377
|
+
window,
|
|
378
|
+
appliedUpdateSeq: update.updateSeq,
|
|
379
|
+
assembling: void 0
|
|
380
|
+
};
|
|
77
381
|
return {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
382
|
+
state: next,
|
|
383
|
+
outcome: {
|
|
384
|
+
_tag: "Applied",
|
|
385
|
+
updateSeq: update.updateSeq,
|
|
386
|
+
...update.requestId === void 0 ? {} : { requestId: update.requestId }
|
|
387
|
+
},
|
|
388
|
+
ack: ackOf(next)
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
/**
|
|
392
|
+
* The request a client sends first on every socket: changes since the window it holds over the
|
|
393
|
+
* span it holds, or the latest `count` records when it holds none.
|
|
394
|
+
*/
|
|
395
|
+
const edaTranscriptSubscribeRequest = (state, options) => {
|
|
396
|
+
const window = state.window;
|
|
397
|
+
const extra = {
|
|
398
|
+
...options?.count === void 0 ? {} : { count: options.count },
|
|
399
|
+
...options?.requestId === void 0 ? {} : { requestId: options.requestId }
|
|
400
|
+
};
|
|
401
|
+
return window?.coverage === void 0 ? {
|
|
402
|
+
_tag: "subscribe",
|
|
403
|
+
...extra
|
|
404
|
+
} : {
|
|
405
|
+
_tag: "subscribe",
|
|
406
|
+
range: window.coverage,
|
|
407
|
+
tail: !window.hasNewer,
|
|
408
|
+
since: window.throughSeq,
|
|
409
|
+
...extra
|
|
89
410
|
};
|
|
90
411
|
};
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
const encodeUnknownDomainServerFrame = Schema.encodeUnknownSync(domain.serverFrame);
|
|
96
|
-
const encodeServerFrame = (frame) => JSON.stringify(encodeDomainServerFrame(frame));
|
|
412
|
+
/** Page from the held coverage's edge, or `undefined` when there is nothing to page from. */
|
|
413
|
+
const edaTranscriptPageRequest = (state, direction, count, requestId) => {
|
|
414
|
+
const coverage = state.window?.coverage;
|
|
415
|
+
if (coverage === void 0) return void 0;
|
|
97
416
|
return {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
serverFrame: Schema.toEncoded(domain.serverFrame),
|
|
104
|
-
clientFrame: EDAWebSocketWireClientFrame
|
|
105
|
-
},
|
|
106
|
-
encodeServerFrame,
|
|
107
|
-
host: { encodeServerFrame: (frame) => JSON.stringify(encodeUnknownDomainServerFrame(frame)) }
|
|
417
|
+
_tag: "page",
|
|
418
|
+
direction,
|
|
419
|
+
cursor: direction === "before" ? coverage.from : coverage.to,
|
|
420
|
+
count,
|
|
421
|
+
...requestId === void 0 ? {} : { requestId }
|
|
108
422
|
};
|
|
109
423
|
};
|
|
110
|
-
/** Strict framework-only protocol used when an app registers no custom events. */
|
|
111
|
-
const edaFrameworkWebSocketWireProtocol = makeEDAWebSocketWireProtocolFromEventSchema(EDAFrameworkWebSocketEvent);
|
|
112
424
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
* Framework durable and ephemeral events are included automatically. Future EDA
|
|
116
|
-
* apps register only their custom events. Servers use the returned domain schemas
|
|
117
|
-
* and typed encoder; external consumers decode with the wire schemas. Pass the
|
|
118
|
-
* returned `host` adapter to the selected host package.
|
|
425
|
+
* Keep only the top-level records from `fromIndex` to `toIndex` (inclusive) of the held window;
|
|
426
|
+
* the server confirms with an update whose coverage is the narrowed span.
|
|
119
427
|
*/
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
maxFrameBytes: 256 * 1024,
|
|
146
|
-
maxFrameEvents: 1,
|
|
147
|
-
maxInFlightFrames: 16,
|
|
148
|
-
pingIntervalMs: 3e4,
|
|
149
|
-
subscriberBufferCapacityEvents: 2048
|
|
428
|
+
const edaTranscriptRetainRequest = (state, fromIndex, toIndex, requestId) => {
|
|
429
|
+
const window = state.window;
|
|
430
|
+
const from = window?.records[fromIndex];
|
|
431
|
+
const to = window?.records[toIndex];
|
|
432
|
+
const fromKey = from === void 0 ? void 0 : orderKeyOf(from);
|
|
433
|
+
const toKey = to === void 0 ? void 0 : orderKeyOf(to);
|
|
434
|
+
if (window === void 0 || fromKey === void 0 || toKey === void 0) return void 0;
|
|
435
|
+
const scope = {
|
|
436
|
+
incarnation: window.incarnation,
|
|
437
|
+
version: window.version
|
|
438
|
+
};
|
|
439
|
+
return {
|
|
440
|
+
_tag: "retain",
|
|
441
|
+
from: {
|
|
442
|
+
...scope,
|
|
443
|
+
...fromKey,
|
|
444
|
+
anchorSeq: SequenceNumber.make(fromKey.anchorSeq)
|
|
445
|
+
},
|
|
446
|
+
to: {
|
|
447
|
+
...scope,
|
|
448
|
+
...toKey,
|
|
449
|
+
anchorSeq: SequenceNumber.make(toKey.anchorSeq)
|
|
450
|
+
},
|
|
451
|
+
...requestId === void 0 ? {} : { requestId }
|
|
452
|
+
};
|
|
150
453
|
};
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
events: Schema.NonEmptyArray(PositionedEvent),
|
|
162
|
-
durableThroughSeq: SequenceNumber
|
|
454
|
+
//#endregion
|
|
455
|
+
//#region src/websocket/transcript-delivery.ts
|
|
456
|
+
const PositiveInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(1));
|
|
457
|
+
const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
|
|
458
|
+
/** A chunked update whose frames are not all sent yet; its chunk data is volatile. */
|
|
459
|
+
const EDATranscriptTransfer = Schema.Struct({
|
|
460
|
+
updateSeq: PositiveInt,
|
|
461
|
+
throughSeq: SequenceNumber,
|
|
462
|
+
count: PositiveInt,
|
|
463
|
+
nextIndex: NonNegativeInt
|
|
163
464
|
});
|
|
164
465
|
/**
|
|
165
|
-
*
|
|
466
|
+
* Everything a transcript socket needs to resume after hibernation, persisted in its attachment
|
|
467
|
+
* before every send and after every applied ACK.
|
|
166
468
|
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
469
|
+
* Transport receipts (`inFlight`, `lastAckedFrameId`) bound frames in flight exactly as on an
|
|
470
|
+
* event socket. The applied cut (`appliedUpdateSeq`, `appliedThroughSeq`) is the last update the
|
|
471
|
+
* client applied completely. `subscription` is the base the next update is planned against, and
|
|
472
|
+
* `requests` are client requests not yet answered. A persisted `transfer` means chunk data was
|
|
473
|
+
* staged in memory; a socket restored with one cannot finish it and is closed.
|
|
170
474
|
*/
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
const EDAWebSocketErrorFrame = EDAWebSocketWireErrorFrame;
|
|
187
|
-
/** Server-to-client application frame union. */
|
|
188
|
-
const EDAWebSocketServerFrame = Schema.Union([
|
|
189
|
-
EDAWebSocketHelloFrame,
|
|
190
|
-
EDAWebSocketEventsFrame,
|
|
191
|
-
EDAWebSocketHeartbeatFrame,
|
|
192
|
-
EDAWebSocketPongFrame,
|
|
193
|
-
EDAWebSocketLaggedFrame,
|
|
194
|
-
EDAWebSocketErrorFrame
|
|
195
|
-
]);
|
|
196
|
-
/** Client acknowledgement after applying a server frame. */
|
|
197
|
-
const EDAWebSocketAckFrame = Schema.Struct({
|
|
198
|
-
...EDAWebSocketWireAckFrame.fields,
|
|
199
|
-
frameId: FrameId,
|
|
200
|
-
durableThroughSeq: SequenceNumber
|
|
201
|
-
});
|
|
202
|
-
/** Client-to-server application frame union. */
|
|
203
|
-
const EDAWebSocketClientFrame = Schema.Union([EDAWebSocketAckFrame, EDAWebSocketPingFrame]);
|
|
204
|
-
/** Serialize a server frame for a text WebSocket message. */
|
|
205
|
-
const encodeEDAWebSocketServerFrame = (frame, protocol) => (protocol ?? edaFrameworkWebSocketWireProtocol.host).encodeServerFrame(frame);
|
|
206
|
-
/** Decode an inbound text WebSocket message at the host boundary. */
|
|
207
|
-
const decodeEDAWebSocketClientMessage = (input) => Effect.try({
|
|
208
|
-
try: () => JSON.parse(input),
|
|
209
|
-
catch: (error) => error
|
|
210
|
-
}).pipe(Effect.flatMap((json) => Schema.decodeUnknownEffect(EDAWebSocketWireClientFrame)(json)), Effect.flatMap((frame) => Schema.decodeUnknownEffect(EDAWebSocketClientFrame)(frame)));
|
|
211
|
-
/** Short close reason that fits the WebSocket protocol reason limit. */
|
|
212
|
-
const laggedCloseReason = (seq) => `lag;seq=${seq}`;
|
|
213
|
-
//#endregion
|
|
214
|
-
//#region src/websocket/delivery.ts
|
|
215
|
-
/** Slow subscriber reasons that intentionally close only the lagging socket. */
|
|
216
|
-
const SubscriberLagReason = Schema.Literals(["buffer-overflow", "ack-timeout"]);
|
|
217
|
-
/** One sent-but-unacknowledged events frame. */
|
|
218
|
-
const EDAWebSocketInFlightFrame = Schema.Struct({
|
|
219
|
-
frameId: FrameId,
|
|
220
|
-
durableThroughSeq: SequenceNumber,
|
|
221
|
-
sentAtMs: Schema.Number
|
|
222
|
-
});
|
|
223
|
-
/** Compact consecutive host-suppressed receipts blocked by a visible ACK. */
|
|
224
|
-
const EDAWebSocketSuppressedRange = Schema.Struct({
|
|
225
|
-
fromFrameId: FrameId,
|
|
226
|
-
throughFrameId: FrameId,
|
|
227
|
-
durableThroughSeq: SequenceNumber
|
|
228
|
-
});
|
|
229
|
-
/** Complete delivery bookkeeping persisted in the WebSocket attachment. */
|
|
230
|
-
const EDAWebSocketDeliveryCheckpoint = Schema.Struct({
|
|
231
|
-
lastAckedSeq: SequenceNumber,
|
|
232
|
-
lastAckedFrameId: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
233
|
-
lastSentFrameId: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
234
|
-
nextFrameId: Schema.Int.check(Schema.isGreaterThanOrEqualTo(1)),
|
|
235
|
-
sentDurableThroughSeq: SequenceNumber,
|
|
236
|
-
inFlight: Schema.Array(EDAWebSocketInFlightFrame),
|
|
237
|
-
/** Host-projected receipts waiting behind an earlier client-visible ACK. */
|
|
238
|
-
suppressed: Schema.optionalKey(Schema.Array(EDAWebSocketSuppressedRange))
|
|
475
|
+
const EDATranscriptDeliveryCheckpoint = Schema.Struct({
|
|
476
|
+
lastAckedFrameId: NonNegativeInt,
|
|
477
|
+
nextFrameId: PositiveInt,
|
|
478
|
+
inFlight: Schema.Array(Schema.Struct({
|
|
479
|
+
frameId: PositiveInt,
|
|
480
|
+
sentAtMs: Schema.Number
|
|
481
|
+
})),
|
|
482
|
+
nextUpdateSeq: PositiveInt,
|
|
483
|
+
completedUpdateSeq: NonNegativeInt,
|
|
484
|
+
completedThroughSeq: SequenceNumber,
|
|
485
|
+
appliedUpdateSeq: NonNegativeInt,
|
|
486
|
+
appliedThroughSeq: SequenceNumber,
|
|
487
|
+
transfer: Schema.optionalKey(EDATranscriptTransfer),
|
|
488
|
+
subscription: Schema.optionalKey(TranscriptSubscriptionBase),
|
|
489
|
+
requests: Schema.Array(EDATranscriptRequestFrame)
|
|
239
490
|
});
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
491
|
+
const defaultEDATranscriptDeliveryLimits = {
|
|
492
|
+
maxStagedBytes: 8388608,
|
|
493
|
+
maxPendingRequests: 4
|
|
494
|
+
};
|
|
495
|
+
/** Delivery state for a newly accepted transcript socket. */
|
|
496
|
+
const makeTranscriptDeliveryState = (policy) => ({
|
|
497
|
+
policy,
|
|
245
498
|
lastAckedFrameId: 0,
|
|
246
|
-
lastSentFrameId: 0,
|
|
247
499
|
nextFrameId: 1,
|
|
248
|
-
sentDurableThroughSeq: input.resumeSeq,
|
|
249
500
|
inFlight: [],
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
...input.checkpoint,
|
|
258
|
-
pendingEphemeral: [],
|
|
259
|
-
suppressed: input.checkpoint.suppressed ?? []
|
|
501
|
+
nextUpdateSeq: 1,
|
|
502
|
+
completedUpdateSeq: 0,
|
|
503
|
+
completedThroughSeq: SequenceNumber.make(0),
|
|
504
|
+
appliedUpdateSeq: 0,
|
|
505
|
+
appliedThroughSeq: SequenceNumber.make(0),
|
|
506
|
+
requests: [],
|
|
507
|
+
staged: []
|
|
260
508
|
});
|
|
261
|
-
/**
|
|
262
|
-
|
|
263
|
-
|
|
509
|
+
/**
|
|
510
|
+
* Restore a hibernated transcript socket. A socket that was mid-transfer lost its staged chunks
|
|
511
|
+
* with the isolate and cannot resume: the caller closes it and the client resubscribes.
|
|
512
|
+
*/
|
|
513
|
+
const restoreTranscriptDeliveryState = (policy, checkpoint) => checkpoint.transfer === void 0 ? {
|
|
514
|
+
_tag: "Restored",
|
|
515
|
+
state: {
|
|
516
|
+
...checkpoint,
|
|
517
|
+
policy,
|
|
518
|
+
staged: []
|
|
519
|
+
}
|
|
520
|
+
} : { _tag: "TransferLost" };
|
|
521
|
+
/** The attachment-safe part of the state. */
|
|
522
|
+
const checkpointTranscriptDelivery = (state) => ({
|
|
264
523
|
lastAckedFrameId: state.lastAckedFrameId,
|
|
265
|
-
lastSentFrameId: state.lastSentFrameId,
|
|
266
524
|
nextFrameId: state.nextFrameId,
|
|
267
|
-
sentDurableThroughSeq: state.sentDurableThroughSeq,
|
|
268
525
|
inFlight: state.inFlight,
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
flowControl: state.policy
|
|
526
|
+
nextUpdateSeq: state.nextUpdateSeq,
|
|
527
|
+
completedUpdateSeq: state.completedUpdateSeq,
|
|
528
|
+
completedThroughSeq: state.completedThroughSeq,
|
|
529
|
+
appliedUpdateSeq: state.appliedUpdateSeq,
|
|
530
|
+
appliedThroughSeq: state.appliedThroughSeq,
|
|
531
|
+
...state.transfer === void 0 ? {} : { transfer: state.transfer },
|
|
532
|
+
...state.subscription === void 0 ? {} : { subscription: state.subscription },
|
|
533
|
+
requests: state.requests
|
|
278
534
|
});
|
|
279
|
-
/**
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
let next = state;
|
|
284
|
-
const frames = [];
|
|
285
|
-
let batch = [];
|
|
286
|
-
const flushBatch = () => {
|
|
287
|
-
if (batch.length === 0) return;
|
|
288
|
-
const sent = sendEventsFrame(next, batch, nowMs);
|
|
289
|
-
next = sent.state;
|
|
290
|
-
frames.push(sent.frame);
|
|
291
|
-
batch = [];
|
|
292
|
-
};
|
|
293
|
-
for (const event of events) {
|
|
294
|
-
if (event.event.durability === "durable" && event.position.seq <= next.sentDurableThroughSeq) continue;
|
|
295
|
-
if (batch.length === 0 && windowCapacity(next) <= 0) break;
|
|
296
|
-
batch.push(event);
|
|
297
|
-
if (batch.length >= next.policy.maxFrameEvents) flushBatch();
|
|
298
|
-
}
|
|
299
|
-
flushBatch();
|
|
300
|
-
return sentResult(next, frames, windowCapacity(next) > 0 && next.sentDurableThroughSeq < head ? next.sentDurableThroughSeq : void 0);
|
|
301
|
-
};
|
|
302
|
-
/** Deliver one live ephemeral event, buffering only while this isolate is active. */
|
|
303
|
-
const onEphemeralEvent = (state, event, nowMs) => {
|
|
304
|
-
const stale = staleAckResult(state, nowMs);
|
|
305
|
-
if (stale !== void 0) return stale;
|
|
306
|
-
if (windowCapacity(state) > 0 && state.pendingEphemeral.length === 0) {
|
|
307
|
-
const sent = sendEventsFrame(state, [event], nowMs);
|
|
308
|
-
return sentResult(sent.state, [sent.frame]);
|
|
309
|
-
}
|
|
310
|
-
const pendingEphemeral = [...state.pendingEphemeral, event];
|
|
311
|
-
if (pendingEphemeral.length > state.policy.subscriberBufferCapacityEvents) return laggedResult(state, "buffer-overflow");
|
|
312
|
-
return {
|
|
313
|
-
state: {
|
|
314
|
-
...state,
|
|
315
|
-
pendingEphemeral
|
|
316
|
-
},
|
|
317
|
-
actions: []
|
|
318
|
-
};
|
|
319
|
-
};
|
|
320
|
-
/** Apply one client ACK, reopen the window, and drain buffered ephemeral events. */
|
|
321
|
-
const onClientAck = (state, ack, nowMs) => {
|
|
322
|
-
const validated = applyAck(state, ack);
|
|
323
|
-
if (validated._tag === "close") return validated.result;
|
|
324
|
-
if (validated._tag === "ignore") return {
|
|
325
|
-
state,
|
|
326
|
-
actions: []
|
|
327
|
-
};
|
|
328
|
-
return reopenDeliveryWindow(validated.state, nowMs);
|
|
329
|
-
};
|
|
535
|
+
/** Frames that may be sent before the client's next receipt. */
|
|
536
|
+
const transcriptDeliveryCapacity = (state) => state.policy.maxInFlightFrames - state.inFlight.length;
|
|
537
|
+
/** Lazily enforced ACK deadline: no resident timer, checked on each transition. */
|
|
538
|
+
const transcriptAckTimedOut = (state, nowMs) => state.inFlight.some((frame) => frame.sentAtMs + state.policy.ackTimeoutMs <= nowMs);
|
|
330
539
|
/**
|
|
331
|
-
*
|
|
332
|
-
*
|
|
540
|
+
* Start sending one planned update, captured from its own snapshot.
|
|
541
|
+
*
|
|
542
|
+
* `subscription` becomes the base for the next update immediately: nothing else is sent until
|
|
543
|
+
* this update's last frame is, so the client can only apply later updates on top of it. An
|
|
544
|
+
* update within the frame limit is one frame; a larger one is chunked and as many chunks as
|
|
545
|
+
* flow control allows are sent now, the rest staged.
|
|
333
546
|
*/
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
if (
|
|
337
|
-
|
|
547
|
+
const startTranscriptUpdate = (state, input, limits, nowMs) => {
|
|
548
|
+
if (state.transfer !== void 0) throw new Error("A transcript update cannot start while another is being transferred");
|
|
549
|
+
if (input.update.updateSeq !== state.nextUpdateSeq) throw new Error("A transcript update must carry the socket's next update seq");
|
|
550
|
+
const updateSeq = input.update.updateSeq;
|
|
551
|
+
const advanced = {
|
|
338
552
|
...state,
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
}
|
|
553
|
+
nextUpdateSeq: updateSeq + 1,
|
|
554
|
+
subscription: input.subscription
|
|
555
|
+
};
|
|
556
|
+
const single = edaTranscriptUpdateFrameText(state.nextFrameId, input.encodedUpdate);
|
|
557
|
+
if (utf8ByteLength(single) <= state.policy.maxFrameBytes) return {
|
|
558
|
+
_tag: "Sending",
|
|
559
|
+
...sendFrames({
|
|
560
|
+
...advanced,
|
|
561
|
+
completedUpdateSeq: updateSeq,
|
|
562
|
+
completedThroughSeq: input.update.throughSeq
|
|
563
|
+
}, [single], nowMs)
|
|
564
|
+
};
|
|
565
|
+
const bytes = utf8ByteLength(input.encodedUpdate);
|
|
566
|
+
if (bytes > limits.maxStagedBytes) return {
|
|
567
|
+
_tag: "TooLarge",
|
|
568
|
+
bytes
|
|
569
|
+
};
|
|
570
|
+
const chunks = splitEDATranscriptUpdate(input.encodedUpdate, state.policy.maxFrameBytes);
|
|
571
|
+
return {
|
|
572
|
+
_tag: "Sending",
|
|
573
|
+
...pumpTranscriptTransfer({
|
|
574
|
+
...advanced,
|
|
575
|
+
transfer: {
|
|
576
|
+
updateSeq,
|
|
577
|
+
throughSeq: input.update.throughSeq,
|
|
578
|
+
count: chunks.length,
|
|
579
|
+
nextIndex: 0
|
|
580
|
+
},
|
|
581
|
+
staged: chunks
|
|
582
|
+
}, nowMs)
|
|
583
|
+
};
|
|
342
584
|
};
|
|
343
|
-
|
|
585
|
+
/** Send staged chunks of the transfer in progress while flow control allows. */
|
|
586
|
+
const pumpTranscriptTransfer = (state, nowMs) => {
|
|
344
587
|
let next = state;
|
|
345
588
|
const frames = [];
|
|
346
|
-
while (next.
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
|
|
589
|
+
while (next.transfer !== void 0 && transcriptDeliveryCapacity(next) > 0) {
|
|
590
|
+
const transfer = next.transfer;
|
|
591
|
+
const [data, ...rest] = next.staged;
|
|
592
|
+
if (data === void 0) break;
|
|
593
|
+
const text = edaTranscriptChunkFrameText({
|
|
594
|
+
frameId: next.nextFrameId,
|
|
595
|
+
updateSeq: transfer.updateSeq,
|
|
596
|
+
index: transfer.nextIndex,
|
|
597
|
+
count: transfer.count,
|
|
598
|
+
data
|
|
599
|
+
});
|
|
600
|
+
const nextIndex = transfer.nextIndex + 1;
|
|
601
|
+
const done = nextIndex === transfer.count;
|
|
602
|
+
const { transfer: _sent, ...withoutTransfer } = next;
|
|
603
|
+
const sent = sendFrames(done ? {
|
|
604
|
+
...withoutTransfer,
|
|
605
|
+
staged: [],
|
|
606
|
+
completedUpdateSeq: transfer.updateSeq,
|
|
607
|
+
completedThroughSeq: transfer.throughSeq
|
|
608
|
+
} : {
|
|
350
609
|
...next,
|
|
351
|
-
|
|
352
|
-
|
|
610
|
+
staged: rest,
|
|
611
|
+
transfer: {
|
|
612
|
+
...transfer,
|
|
613
|
+
nextIndex
|
|
614
|
+
}
|
|
615
|
+
}, [text], nowMs);
|
|
353
616
|
next = sent.state;
|
|
354
|
-
frames.push(sent.
|
|
617
|
+
frames.push(...sent.frames);
|
|
355
618
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
619
|
+
return {
|
|
620
|
+
state: next,
|
|
621
|
+
frames
|
|
622
|
+
};
|
|
359
623
|
};
|
|
360
|
-
const
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
if (acknowledgedVisible.length === 0) return protocolClose(state, "ACK referenced no in-flight frames");
|
|
366
|
-
if (state.suppressed.some((range) => range.fromFrameId <= ack.frameId && ack.frameId <= range.throughFrameId)) return protocolClose(state, "ACK referenced a host-suppressed frame");
|
|
367
|
-
const acknowledgedSuppressed = state.suppressed.filter((range) => range.throughFrameId <= ack.frameId);
|
|
368
|
-
const acknowledged = [...acknowledgedVisible, ...acknowledgedSuppressed];
|
|
369
|
-
const allowedDurableSeq = SequenceNumber.make(acknowledged.reduce((max, receipt) => Math.max(max, receipt.durableThroughSeq), state.lastAckedSeq));
|
|
370
|
-
if (ack.durableThroughSeq > allowedDurableSeq) return protocolClose(state, "ACK durable seq exceeded sent frame boundary");
|
|
624
|
+
const sendFrames = (state, texts, nowMs) => {
|
|
625
|
+
const inFlight = texts.map((_, index) => ({
|
|
626
|
+
frameId: state.nextFrameId + index,
|
|
627
|
+
sentAtMs: nowMs
|
|
628
|
+
}));
|
|
371
629
|
return {
|
|
372
|
-
|
|
373
|
-
state: advanceSuppressedReceipts({
|
|
630
|
+
state: {
|
|
374
631
|
...state,
|
|
375
|
-
inFlight: state.inFlight
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
})
|
|
632
|
+
inFlight: [...state.inFlight, ...inFlight],
|
|
633
|
+
nextFrameId: state.nextFrameId + texts.length
|
|
634
|
+
},
|
|
635
|
+
frames: texts
|
|
380
636
|
};
|
|
381
637
|
};
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
638
|
+
/**
|
|
639
|
+
* Apply one ACK. The receipt frees flow-control capacity cumulatively; the applied cut may only
|
|
640
|
+
* name an update whose frames were all sent, and never moves backwards.
|
|
641
|
+
*/
|
|
642
|
+
const onTranscriptAck = (state, ack) => {
|
|
643
|
+
const appliedUpdateSeq = ack.appliedUpdateSeq ?? state.appliedUpdateSeq;
|
|
644
|
+
const advancesApplied = appliedUpdateSeq > state.appliedUpdateSeq;
|
|
645
|
+
if (ack.frameId <= state.lastAckedFrameId) return advancesApplied ? {
|
|
646
|
+
_tag: "ProtocolError",
|
|
647
|
+
message: "Duplicate ACK attempted to advance the applied cut"
|
|
648
|
+
} : { _tag: "Ignored" };
|
|
649
|
+
if (ack.frameId >= state.nextFrameId) return {
|
|
650
|
+
_tag: "ProtocolError",
|
|
651
|
+
message: "ACK referenced an unsent frame"
|
|
652
|
+
};
|
|
653
|
+
if (appliedUpdateSeq < state.appliedUpdateSeq) return {
|
|
654
|
+
_tag: "ProtocolError",
|
|
655
|
+
message: "ACK applied cut moved backwards"
|
|
656
|
+
};
|
|
657
|
+
if (appliedUpdateSeq > state.completedUpdateSeq) return {
|
|
658
|
+
_tag: "ProtocolError",
|
|
659
|
+
message: "ACK applied an incomplete update"
|
|
660
|
+
};
|
|
661
|
+
if (advancesApplied && appliedUpdateSeq === state.completedUpdateSeq && ack.durableThroughSeq !== state.completedThroughSeq) return {
|
|
662
|
+
_tag: "ProtocolError",
|
|
663
|
+
message: "ACK applied cut names another update's seq"
|
|
397
664
|
};
|
|
398
|
-
};
|
|
399
|
-
const insertSuppressedReceipt = (ranges, receipt) => {
|
|
400
|
-
const inserted = [...ranges, EDAWebSocketSuppressedRange.make({
|
|
401
|
-
fromFrameId: receipt.frameId,
|
|
402
|
-
throughFrameId: receipt.frameId,
|
|
403
|
-
durableThroughSeq: receipt.durableThroughSeq
|
|
404
|
-
})].sort((left, right) => left.fromFrameId - right.fromFrameId);
|
|
405
|
-
const compacted = [];
|
|
406
|
-
for (const range of inserted) {
|
|
407
|
-
const previous = compacted.at(-1);
|
|
408
|
-
if (previous === void 0 || range.fromFrameId > previous.throughFrameId + 1) {
|
|
409
|
-
compacted.push(range);
|
|
410
|
-
continue;
|
|
411
|
-
}
|
|
412
|
-
compacted[compacted.length - 1] = EDAWebSocketSuppressedRange.make({
|
|
413
|
-
fromFrameId: previous.fromFrameId,
|
|
414
|
-
throughFrameId: FrameId.make(Math.max(previous.throughFrameId, range.throughFrameId)),
|
|
415
|
-
durableThroughSeq: SequenceNumber.make(Math.max(previous.durableThroughSeq, range.durableThroughSeq))
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
return compacted;
|
|
419
|
-
};
|
|
420
|
-
const windowCapacity = (state) => state.policy.maxInFlightFrames - state.inFlight.length;
|
|
421
|
-
const sendEventsFrame = (state, events, nowMs) => {
|
|
422
|
-
const [first, ...rest] = events;
|
|
423
|
-
if (first === void 0) throw new Error("Cannot send an empty events frame");
|
|
424
|
-
const frameId = FrameId.make(state.nextFrameId);
|
|
425
|
-
const durableThroughSeq = SequenceNumber.make(events.reduce((max, event) => event.event.durability === "durable" ? Math.max(max, event.position.seq) : max, state.sentDurableThroughSeq));
|
|
426
|
-
const frame = EDAWebSocketEventsFrame.make({
|
|
427
|
-
_tag: "events",
|
|
428
|
-
frameId,
|
|
429
|
-
events: [first, ...rest],
|
|
430
|
-
durableThroughSeq
|
|
431
|
-
});
|
|
432
665
|
return {
|
|
666
|
+
_tag: "Applied",
|
|
433
667
|
state: {
|
|
434
668
|
...state,
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
lastSentFrameId: frameId,
|
|
441
|
-
nextFrameId: frameId + 1,
|
|
442
|
-
sentDurableThroughSeq: durableThroughSeq
|
|
443
|
-
},
|
|
444
|
-
frame
|
|
669
|
+
lastAckedFrameId: ack.frameId,
|
|
670
|
+
inFlight: state.inFlight.filter((frame) => frame.frameId > ack.frameId),
|
|
671
|
+
appliedUpdateSeq,
|
|
672
|
+
appliedThroughSeq: advancesApplied ? SequenceNumber.make(ack.durableThroughSeq) : state.appliedThroughSeq
|
|
673
|
+
}
|
|
445
674
|
};
|
|
446
675
|
};
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
_tag: "Send",
|
|
453
|
-
frames
|
|
454
|
-
}] : [],
|
|
455
|
-
...catchUpAfterSeq === void 0 ? [] : [{
|
|
456
|
-
_tag: "ReadEventPage",
|
|
457
|
-
afterSeq: catchUpAfterSeq
|
|
458
|
-
}]
|
|
459
|
-
]
|
|
460
|
-
});
|
|
461
|
-
/** Lazily enforce the ACK deadline: no resident timer, checked on each transition. */
|
|
462
|
-
const staleAckResult = (state, nowMs) => state.inFlight.some((frame) => frame.sentAtMs + state.policy.ackTimeoutMs <= nowMs) ? laggedResult(state, "ack-timeout") : void 0;
|
|
463
|
-
const laggedResult = (state, reason) => ({
|
|
464
|
-
state,
|
|
465
|
-
actions: [{
|
|
466
|
-
_tag: "Send",
|
|
467
|
-
frames: [EDAWebSocketLaggedFrame.make({
|
|
468
|
-
_tag: "lagged",
|
|
469
|
-
resumeSeq: state.lastAckedSeq,
|
|
470
|
-
reason
|
|
471
|
-
})]
|
|
472
|
-
}, {
|
|
473
|
-
_tag: "Close",
|
|
474
|
-
code: EDA_WS_CLOSE_LAGGED,
|
|
475
|
-
reason: laggedCloseReason(state.lastAckedSeq)
|
|
476
|
-
}]
|
|
477
|
-
});
|
|
478
|
-
const protocolClose = (state, message) => ({
|
|
479
|
-
_tag: "close",
|
|
480
|
-
result: {
|
|
481
|
-
state,
|
|
482
|
-
actions: [{
|
|
483
|
-
_tag: "Send",
|
|
484
|
-
frames: [EDAWebSocketErrorFrame.make({
|
|
485
|
-
_tag: "error",
|
|
486
|
-
message: message.slice(0, 120)
|
|
487
|
-
})]
|
|
488
|
-
}, {
|
|
489
|
-
_tag: "Close",
|
|
490
|
-
code: EDA_WS_CLOSE_PROTOCOL_ERROR,
|
|
491
|
-
reason: "protocol"
|
|
492
|
-
}]
|
|
493
|
-
}
|
|
494
|
-
});
|
|
676
|
+
/** Queue one client request, or report that the client exceeded its request bound. */
|
|
677
|
+
const enqueueTranscriptRequest = (state, request, limits) => state.requests.length >= limits.maxPendingRequests ? void 0 : {
|
|
678
|
+
...state,
|
|
679
|
+
requests: [...state.requests, request]
|
|
680
|
+
};
|
|
495
681
|
//#endregion
|
|
496
|
-
export { EDAFrameworkWebSocketEvent, EDAWebSocketAckFrame, EDAWebSocketClientFrame, EDAWebSocketDeliveryCheckpoint, EDAWebSocketErrorFrame, EDAWebSocketEventsFrame, EDAWebSocketFlowControl, EDAWebSocketHeartbeatFrame, EDAWebSocketHelloFrame, EDAWebSocketInFlightFrame, EDAWebSocketLaggedFrame, EDAWebSocketPingFrame, EDAWebSocketPongFrame, EDAWebSocketServerFrame, EDAWebSocketSuppressedRange, EDAWebSocketWireAckFrame, EDAWebSocketWireClientFrame, EDAWebSocketWireErrorFrame, EDAWebSocketWireFlowControl, EDAWebSocketWireHeartbeatFrame, EDAWebSocketWireHelloFrame, EDAWebSocketWireLaggedFrame, EDAWebSocketWirePingFrame, EDAWebSocketWirePongFrame, EDA_WEB_SOCKET_PING_MESSAGE, EDA_WEB_SOCKET_PONG_MESSAGE, EDA_WEB_SOCKET_PROTOCOL_VERSION, EDA_WEB_SOCKET_WIRE_PROTOCOL_VERSION, EDA_WS_CLOSE_GOING_AWAY, EDA_WS_CLOSE_LAGGED, EDA_WS_CLOSE_PROTOCOL_ERROR, EDA_WS_CLOSE_SEND_FAILED, EDA_WS_CLOSE_UNSUPPORTED_DATA, FrameId, SubscriberId, SubscriberLagReason, checkpointWebSocketDeliveryState, decodeEDAWebSocketClientMessage, defaultEDAWebSocketFlowControl, deliveryHelloFrame, edaFrameworkWebSocketWireProtocol, encodeEDAWebSocketServerFrame, laggedCloseReason, makeEDAWebSocketWireProtocol, makeWebSocketDeliveryState, onClientAck, onDurableEvents, onEphemeralEvent, onHostSuppressedFrame, restoreWebSocketDeliveryState };
|
|
682
|
+
export { EDAFrameworkWebSocketEvent, EDATranscriptChunkFrame, EDATranscriptClientFrame, EDATranscriptDeliveryCheckpoint, EDATranscriptHelloFrame, EDATranscriptNegotiation, EDATranscriptPageFrame, EDATranscriptRequestFrame, EDATranscriptRetainFrame, EDATranscriptServerFrame, EDATranscriptSubscribeFrame, EDATranscriptTransfer, EDATranscriptUpdateFrame, EDAWebSocketAckFrame, EDAWebSocketClientFrame, EDAWebSocketDeliveryCheckpoint, EDAWebSocketErrorFrame, EDAWebSocketEventsFrame, EDAWebSocketFlowControl, EDAWebSocketHeartbeatFrame, EDAWebSocketHelloFrame, EDAWebSocketInFlightFrame, EDAWebSocketLaggedFrame, EDAWebSocketPingFrame, EDAWebSocketPongFrame, EDAWebSocketServerFrame, EDAWebSocketSuppressedRange, EDAWebSocketWireAckFrame, EDAWebSocketWireClientFrame, EDAWebSocketWireErrorFrame, EDAWebSocketWireFlowControl, EDAWebSocketWireHeartbeatFrame, EDAWebSocketWireHelloFrame, EDAWebSocketWireLaggedFrame, EDAWebSocketWirePingFrame, EDAWebSocketWirePongFrame, EDA_TRANSCRIPT_PROTOCOL_MAJOR, EDA_TRANSCRIPT_WIRE_VERSION, EDA_WEB_SOCKET_PING_MESSAGE, EDA_WEB_SOCKET_PONG_MESSAGE, EDA_WEB_SOCKET_PROTOCOL_VERSION, EDA_WEB_SOCKET_WIRE_PROTOCOL_VERSION, EDA_WS_CLOSE_GOING_AWAY, EDA_WS_CLOSE_LAGGED, EDA_WS_CLOSE_PROTOCOL_ERROR, EDA_WS_CLOSE_SEND_FAILED, EDA_WS_CLOSE_TRANSCRIPT_RESUBSCRIBE, EDA_WS_CLOSE_UNSUPPORTED_DATA, FrameId, SubscriberId, SubscriberLagReason, applyEDATranscriptServerMessage, checkpointTranscriptDelivery, checkpointWebSocketDeliveryState, decodeEDAWebSocketClientMessage, defaultEDATranscriptDeliveryLimits, defaultEDAWebSocketFlowControl, deliveryHelloFrame, edaFrameworkWebSocketWireProtocol, edaTranscriptChunkFrameText, edaTranscriptClientFromWindow, edaTranscriptClientReconnected, edaTranscriptNegotiation, edaTranscriptPageRequest, edaTranscriptRetainRequest, edaTranscriptSubscribeRequest, edaTranscriptUpdateFrameText, emptyEDATranscriptClientState, encodeEDATranscriptServerFrame, encodeEDATranscriptUpdate, encodeEDAWebSocketServerFrame, enqueueTranscriptRequest, laggedCloseReason, makeEDAWebSocketWireProtocol, makeTranscriptDeliveryState, makeWebSocketDeliveryState, negotiateEDATranscript, onClientAck, onDurableEvents, onEphemeralEvent, onHostSuppressedFrame, onTranscriptAck, pumpTranscriptTransfer, restoreTranscriptDeliveryState, restoreWebSocketDeliveryState, splitEDATranscriptUpdate, startTranscriptUpdate, transcriptAckTimedOut, transcriptDeliveryCapacity, utf8ByteLength };
|
|
497
683
|
|
|
498
684
|
//# sourceMappingURL=websocket.js.map
|