@smthrs/sync 0.0.0-stage → 1.0.0-rc.3
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/CHANGELOG.md +208 -0
- package/LICENSE +21 -0
- package/README.md +208 -2
- package/dist/cjs/BranchCommands.d.ts +228 -0
- package/dist/cjs/BranchCommands.d.ts.map +1 -0
- package/dist/cjs/BranchCommands.js +316 -0
- package/dist/cjs/BranchCommands.js.map +7 -0
- package/dist/cjs/BranchIds.d.ts +65 -0
- package/dist/cjs/BranchIds.d.ts.map +1 -0
- package/dist/cjs/BranchIds.js +53 -0
- package/dist/cjs/BranchIds.js.map +7 -0
- package/dist/cjs/BranchPresence.d.ts +195 -0
- package/dist/cjs/BranchPresence.d.ts.map +1 -0
- package/dist/cjs/BranchPresence.js +276 -0
- package/dist/cjs/BranchPresence.js.map +7 -0
- package/dist/cjs/BranchProjection.d.ts +179 -0
- package/dist/cjs/BranchProjection.d.ts.map +1 -0
- package/dist/cjs/BranchProjection.js +197 -0
- package/dist/cjs/BranchProjection.js.map +7 -0
- package/dist/cjs/BranchProtocol.d.ts +415 -0
- package/dist/cjs/BranchProtocol.d.ts.map +1 -0
- package/dist/cjs/BranchProtocol.js +172 -0
- package/dist/cjs/BranchProtocol.js.map +7 -0
- package/dist/cjs/BranchRpcs.d.ts +177 -0
- package/dist/cjs/BranchRpcs.d.ts.map +1 -0
- package/dist/cjs/BranchRpcs.js +85 -0
- package/dist/cjs/BranchRpcs.js.map +7 -0
- package/dist/cjs/BranchServer.d.ts +55 -0
- package/dist/cjs/BranchServer.d.ts.map +1 -0
- package/dist/cjs/BranchServer.js +147 -0
- package/dist/cjs/BranchServer.js.map +7 -0
- package/dist/cjs/BranchShare.d.ts +159 -0
- package/dist/cjs/BranchShare.d.ts.map +1 -0
- package/dist/cjs/BranchShare.js +147 -0
- package/dist/cjs/BranchShare.js.map +7 -0
- package/dist/cjs/RunCatalog.d.ts +207 -0
- package/dist/cjs/RunCatalog.d.ts.map +1 -0
- package/dist/cjs/RunCatalog.js +122 -0
- package/dist/cjs/RunCatalog.js.map +7 -0
- package/dist/cjs/SyncAuth.d.ts +70 -0
- package/dist/cjs/SyncAuth.d.ts.map +1 -0
- package/dist/cjs/SyncAuth.js +109 -0
- package/dist/cjs/SyncAuth.js.map +7 -0
- package/dist/cjs/SyncClient.d.ts +274 -0
- package/dist/cjs/SyncClient.d.ts.map +1 -0
- package/dist/cjs/SyncClient.js +474 -0
- package/dist/cjs/SyncClient.js.map +7 -0
- package/dist/cjs/SyncError.d.ts +107 -0
- package/dist/cjs/SyncError.d.ts.map +1 -0
- package/dist/cjs/SyncError.js +110 -0
- package/dist/cjs/SyncError.js.map +7 -0
- package/dist/cjs/SyncPrincipal.d.ts +100 -0
- package/dist/cjs/SyncPrincipal.d.ts.map +1 -0
- package/dist/cjs/SyncPrincipal.js +48 -0
- package/dist/cjs/SyncPrincipal.js.map +7 -0
- package/dist/cjs/SyncProtocol.d.ts +592 -0
- package/dist/cjs/SyncProtocol.d.ts.map +1 -0
- package/dist/cjs/SyncProtocol.js +165 -0
- package/dist/cjs/SyncProtocol.js.map +7 -0
- package/dist/cjs/SyncRpcs.d.ts +92 -0
- package/dist/cjs/SyncRpcs.d.ts.map +1 -0
- package/dist/cjs/SyncRpcs.js +35 -0
- package/dist/cjs/SyncRpcs.js.map +7 -0
- package/dist/cjs/SyncServer.d.ts +184 -0
- package/dist/cjs/SyncServer.d.ts.map +1 -0
- package/dist/cjs/SyncServer.js +513 -0
- package/dist/cjs/SyncServer.js.map +7 -0
- package/dist/cjs/WorkspaceShare.d.ts +174 -0
- package/dist/cjs/WorkspaceShare.d.ts.map +1 -0
- package/dist/cjs/WorkspaceShare.js +149 -0
- package/dist/cjs/WorkspaceShare.js.map +7 -0
- package/dist/cjs/index.d.ts +91 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +67 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/internal/Admission.d.ts +43 -0
- package/dist/cjs/internal/Admission.d.ts.map +1 -0
- package/dist/cjs/internal/Admission.js +90 -0
- package/dist/cjs/internal/Admission.js.map +7 -0
- package/dist/cjs/internal/CauseText.d.ts +55 -0
- package/dist/cjs/internal/CauseText.d.ts.map +1 -0
- package/dist/cjs/internal/CauseText.js +38 -0
- package/dist/cjs/internal/CauseText.js.map +7 -0
- package/dist/cjs/internal/JournalErrorCode.d.ts +23 -0
- package/dist/cjs/internal/JournalErrorCode.d.ts.map +1 -0
- package/dist/cjs/internal/JournalErrorCode.js +34 -0
- package/dist/cjs/internal/JournalErrorCode.js.map +7 -0
- package/dist/cjs/internal/PolicyOptions.d.ts +57 -0
- package/dist/cjs/internal/PolicyOptions.d.ts.map +1 -0
- package/dist/cjs/internal/PolicyOptions.js +62 -0
- package/dist/cjs/internal/PolicyOptions.js.map +7 -0
- package/dist/cjs/internal/ProtocolVersion.d.ts +17 -0
- package/dist/cjs/internal/ProtocolVersion.d.ts.map +1 -0
- package/dist/cjs/internal/ProtocolVersion.js +44 -0
- package/dist/cjs/internal/ProtocolVersion.js.map +7 -0
- package/dist/cjs/internal/ShareSigner.d.ts +173 -0
- package/dist/cjs/internal/ShareSigner.d.ts.map +1 -0
- package/dist/cjs/internal/ShareSigner.js +150 -0
- package/dist/cjs/internal/ShareSigner.js.map +7 -0
- package/dist/cjs/internal/SnapshotBoundary.d.ts +17 -0
- package/dist/cjs/internal/SnapshotBoundary.d.ts.map +1 -0
- package/dist/cjs/internal/SnapshotBoundary.js +97 -0
- package/dist/cjs/internal/SnapshotBoundary.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/test/TestSocket.d.ts +55 -0
- package/dist/cjs/test/TestSocket.d.ts.map +1 -0
- package/dist/cjs/test/TestSocket.js +145 -0
- package/dist/cjs/test/TestSocket.js.map +7 -0
- package/dist/cjs/test/TestSync.d.ts +54 -0
- package/dist/cjs/test/TestSync.d.ts.map +1 -0
- package/dist/cjs/test/TestSync.js +96 -0
- package/dist/cjs/test/TestSync.js.map +7 -0
- package/dist/esm/BranchCommands.d.ts +228 -0
- package/dist/esm/BranchCommands.d.ts.map +1 -0
- package/dist/esm/BranchCommands.js +496 -0
- package/dist/esm/BranchCommands.js.map +1 -0
- package/dist/esm/BranchIds.d.ts +65 -0
- package/dist/esm/BranchIds.d.ts.map +1 -0
- package/dist/esm/BranchIds.js +61 -0
- package/dist/esm/BranchIds.js.map +1 -0
- package/dist/esm/BranchPresence.d.ts +195 -0
- package/dist/esm/BranchPresence.d.ts.map +1 -0
- package/dist/esm/BranchPresence.js +361 -0
- package/dist/esm/BranchPresence.js.map +1 -0
- package/dist/esm/BranchProjection.d.ts +179 -0
- package/dist/esm/BranchProjection.d.ts.map +1 -0
- package/dist/esm/BranchProjection.js +269 -0
- package/dist/esm/BranchProjection.js.map +1 -0
- package/dist/esm/BranchProtocol.d.ts +415 -0
- package/dist/esm/BranchProtocol.d.ts.map +1 -0
- package/dist/esm/BranchProtocol.js +323 -0
- package/dist/esm/BranchProtocol.js.map +1 -0
- package/dist/esm/BranchRpcs.d.ts +177 -0
- package/dist/esm/BranchRpcs.d.ts.map +1 -0
- package/dist/esm/BranchRpcs.js +103 -0
- package/dist/esm/BranchRpcs.js.map +1 -0
- package/dist/esm/BranchServer.d.ts +55 -0
- package/dist/esm/BranchServer.d.ts.map +1 -0
- package/dist/esm/BranchServer.js +168 -0
- package/dist/esm/BranchServer.js.map +1 -0
- package/dist/esm/BranchShare.d.ts +159 -0
- package/dist/esm/BranchShare.d.ts.map +1 -0
- package/dist/esm/BranchShare.js +213 -0
- package/dist/esm/BranchShare.js.map +1 -0
- package/dist/esm/RunCatalog.d.ts +207 -0
- package/dist/esm/RunCatalog.d.ts.map +1 -0
- package/dist/esm/RunCatalog.js +196 -0
- package/dist/esm/RunCatalog.js.map +1 -0
- package/dist/esm/SyncAuth.d.ts +70 -0
- package/dist/esm/SyncAuth.d.ts.map +1 -0
- package/dist/esm/SyncAuth.js +119 -0
- package/dist/esm/SyncAuth.js.map +1 -0
- package/dist/esm/SyncClient.d.ts +274 -0
- package/dist/esm/SyncClient.d.ts.map +1 -0
- package/dist/esm/SyncClient.js +586 -0
- package/dist/esm/SyncClient.js.map +1 -0
- package/dist/esm/SyncError.d.ts +107 -0
- package/dist/esm/SyncError.d.ts.map +1 -0
- package/dist/esm/SyncError.js +133 -0
- package/dist/esm/SyncError.js.map +1 -0
- package/dist/esm/SyncPrincipal.d.ts +100 -0
- package/dist/esm/SyncPrincipal.d.ts.map +1 -0
- package/dist/esm/SyncPrincipal.js +58 -0
- package/dist/esm/SyncPrincipal.js.map +1 -0
- package/dist/esm/SyncProtocol.d.ts +592 -0
- package/dist/esm/SyncProtocol.d.ts.map +1 -0
- package/dist/esm/SyncProtocol.js +434 -0
- package/dist/esm/SyncProtocol.js.map +1 -0
- package/dist/esm/SyncRpcs.d.ts +92 -0
- package/dist/esm/SyncRpcs.d.ts.map +1 -0
- package/dist/esm/SyncRpcs.js +35 -0
- package/dist/esm/SyncRpcs.js.map +1 -0
- package/dist/esm/SyncServer.d.ts +184 -0
- package/dist/esm/SyncServer.d.ts.map +1 -0
- package/dist/esm/SyncServer.js +897 -0
- package/dist/esm/SyncServer.js.map +1 -0
- package/dist/esm/WorkspaceShare.d.ts +174 -0
- package/dist/esm/WorkspaceShare.d.ts.map +1 -0
- package/dist/esm/WorkspaceShare.js +208 -0
- package/dist/esm/WorkspaceShare.js.map +1 -0
- package/dist/esm/index.d.ts +91 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +91 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internal/Admission.d.ts +43 -0
- package/dist/esm/internal/Admission.d.ts.map +1 -0
- package/dist/esm/internal/Admission.js +80 -0
- package/dist/esm/internal/Admission.js.map +1 -0
- package/dist/esm/internal/CauseText.d.ts +55 -0
- package/dist/esm/internal/CauseText.d.ts.map +1 -0
- package/dist/esm/internal/CauseText.js +67 -0
- package/dist/esm/internal/CauseText.js.map +1 -0
- package/dist/esm/internal/JournalErrorCode.d.ts +23 -0
- package/dist/esm/internal/JournalErrorCode.d.ts.map +1 -0
- package/dist/esm/internal/JournalErrorCode.js +44 -0
- package/dist/esm/internal/JournalErrorCode.js.map +1 -0
- package/dist/esm/internal/PolicyOptions.d.ts +57 -0
- package/dist/esm/internal/PolicyOptions.d.ts.map +1 -0
- package/dist/esm/internal/PolicyOptions.js +70 -0
- package/dist/esm/internal/PolicyOptions.js.map +1 -0
- package/dist/esm/internal/ProtocolVersion.d.ts +17 -0
- package/dist/esm/internal/ProtocolVersion.d.ts.map +1 -0
- package/dist/esm/internal/ProtocolVersion.js +24 -0
- package/dist/esm/internal/ProtocolVersion.js.map +1 -0
- package/dist/esm/internal/ShareSigner.d.ts +173 -0
- package/dist/esm/internal/ShareSigner.d.ts.map +1 -0
- package/dist/esm/internal/ShareSigner.js +219 -0
- package/dist/esm/internal/ShareSigner.js.map +1 -0
- package/dist/esm/internal/SnapshotBoundary.d.ts +17 -0
- package/dist/esm/internal/SnapshotBoundary.d.ts.map +1 -0
- package/dist/esm/internal/SnapshotBoundary.js +68 -0
- package/dist/esm/internal/SnapshotBoundary.js.map +1 -0
- package/dist/esm/test/TestSocket.d.ts +55 -0
- package/dist/esm/test/TestSocket.d.ts.map +1 -0
- package/dist/esm/test/TestSocket.js +128 -0
- package/dist/esm/test/TestSocket.js.map +1 -0
- package/dist/esm/test/TestSync.d.ts +54 -0
- package/dist/esm/test/TestSync.d.ts.map +1 -0
- package/dist/esm/test/TestSync.js +76 -0
- package/dist/esm/test/TestSync.js.map +1 -0
- package/docs/README.md +156 -0
- package/docs/api.md +328 -0
- package/docs/concepts/authorization.md +117 -0
- package/docs/concepts/branches.md +195 -0
- package/docs/concepts/compaction.md +144 -0
- package/docs/concepts/replay-then-follow.md +129 -0
- package/docs/concepts/scopes-and-cursors.md +135 -0
- package/docs/guides/authorize-a-connection.md +174 -0
- package/docs/guides/follow-a-run.md +174 -0
- package/docs/guides/handle-a-compacted-run.md +158 -0
- package/docs/guides/list-workspace-runs.md +120 -0
- package/docs/guides/run-a-long-soak.md +80 -0
- package/docs/guides/serve-the-read-path.md +146 -0
- package/docs/guides/test-a-follower.md +273 -0
- package/docs/installation.md +92 -0
- package/docs/protocol.md +183 -0
- package/docs/quickstart.md +159 -0
- package/docs/troubleshooting.md +234 -0
- package/package.json +304 -3
- package/src/BranchCommands.ts +684 -0
- package/src/BranchIds.ts +76 -0
- package/src/BranchPresence.ts +512 -0
- package/src/BranchProjection.ts +308 -0
- package/src/BranchProtocol.ts +411 -0
- package/src/BranchRpcs.ts +163 -0
- package/src/BranchServer.ts +202 -0
- package/src/BranchShare.ts +297 -0
- package/src/RunCatalog.ts +308 -0
- package/src/SyncAuth.ts +146 -0
- package/src/SyncClient.ts +902 -0
- package/src/SyncError.ts +147 -0
- package/src/SyncPrincipal.ts +115 -0
- package/src/SyncProtocol.ts +594 -0
- package/src/SyncRpcs.ts +40 -0
- package/src/SyncServer.ts +1184 -0
- package/src/WorkspaceShare.ts +292 -0
- package/src/index.ts +107 -0
- package/src/internal/Admission.ts +119 -0
- package/src/internal/CauseText.ts +74 -0
- package/src/internal/JournalErrorCode.ts +46 -0
- package/src/internal/PolicyOptions.ts +97 -0
- package/src/internal/ProtocolVersion.ts +28 -0
- package/src/internal/ShareSigner.ts +300 -0
- package/src/internal/SnapshotBoundary.ts +91 -0
- package/src/test/TestSocket.ts +192 -0
- package/src/test/TestSync.ts +113 -0
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-safe client for replaying and following journal entries.
|
|
3
|
+
*
|
|
4
|
+
* @since 0.1.0
|
|
5
|
+
*/
|
|
6
|
+
import * as Cause from "effect/Cause";
|
|
7
|
+
import * as Context from "effect/Context";
|
|
8
|
+
import * as Effect from "effect/Effect";
|
|
9
|
+
import * as Layer from "effect/Layer";
|
|
10
|
+
import * as Ref from "effect/Ref";
|
|
11
|
+
import * as Schedule from "effect/Schedule";
|
|
12
|
+
import * as Schema from "effect/Schema";
|
|
13
|
+
import * as Stream from "effect/Stream";
|
|
14
|
+
import * as RpcClient from "effect/unstable/rpc/RpcClient";
|
|
15
|
+
import * as RpcClientError from "effect/unstable/rpc/RpcClientError";
|
|
16
|
+
import * as Admission from "./internal/Admission.js";
|
|
17
|
+
import { boundedText, causeCode, causeText } from "./internal/CauseText.js";
|
|
18
|
+
import { boundedInt, positiveInt } from "./internal/PolicyOptions.js";
|
|
19
|
+
import * as SnapshotBoundary from "./internal/SnapshotBoundary.js";
|
|
20
|
+
import { SyncError, SyncGapError } from "./SyncError.js";
|
|
21
|
+
import { covers, defaultMaxFrameBytes, duplicateCursorRunId, encodedByteLength, Frame, maxReadLimit, maxSubscribeCredit, protocolVersion, ReadResponse, RequestEnvelope, RunCursor } from "./SyncProtocol.js";
|
|
22
|
+
import { SyncRpcs } from "./SyncRpcs.js";
|
|
23
|
+
/**
|
|
24
|
+
* Frames one subscription round may carry before the follow replenishes it.
|
|
25
|
+
*
|
|
26
|
+
* A subscription's `credit` is a hard frame limit the server enforces with
|
|
27
|
+
* `Stream.take`, so the number the client sends is the size of its window, not
|
|
28
|
+
* a hint. At `credit: 1` the window closed after every single entry and the
|
|
29
|
+
* follow resubscribed: following a run that emits a hundred entries cost a
|
|
30
|
+
* hundred subscribe round trips, each of them a fresh cursor snapshot and a
|
|
31
|
+
* fresh server-side fan-out. Replenishing 256 frames at a time makes the
|
|
32
|
+
* round-trip cost proportional to the window rather than to the traffic, and
|
|
33
|
+
* keeps it bounded — an unbounded window would hand the server one
|
|
34
|
+
* never-closing subscription per follower.
|
|
35
|
+
*
|
|
36
|
+
* @category constants
|
|
37
|
+
* @since 0.1.0
|
|
38
|
+
*/
|
|
39
|
+
export const defaultCredit = 256;
|
|
40
|
+
/**
|
|
41
|
+
* Entries one catch-up page asks for.
|
|
42
|
+
*
|
|
43
|
+
* Bootstrap is the replay half of replay-then-follow, so the page size is the
|
|
44
|
+
* memory one catch-up costs and the granularity at which its cursor advances.
|
|
45
|
+
* It matches {@link defaultCredit} and stays under
|
|
46
|
+
* `SyncProtocol.maxReadLimit`, which is the ceiling the server enforces.
|
|
47
|
+
*
|
|
48
|
+
* @category constants
|
|
49
|
+
* @since 1.0.0-rc.0
|
|
50
|
+
*/
|
|
51
|
+
export const defaultBootstrapLimit = 256;
|
|
52
|
+
const defaults = {
|
|
53
|
+
maxFrameBytes: defaultMaxFrameBytes,
|
|
54
|
+
bootstrapLimit: defaultBootstrapLimit
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* The browser-safe journal replication client.
|
|
58
|
+
*
|
|
59
|
+
* @category services
|
|
60
|
+
* @since 1.0.0-rc.0
|
|
61
|
+
*/
|
|
62
|
+
export class SyncClient extends Context.Service()("@smthrs/sync/SyncClient") {
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The client tag's former name, kept for one release.
|
|
66
|
+
*
|
|
67
|
+
* @deprecated Use {@link SyncClient}.
|
|
68
|
+
* @category services
|
|
69
|
+
* @since 0.1.0
|
|
70
|
+
*/
|
|
71
|
+
export const Sync = SyncClient;
|
|
72
|
+
/**
|
|
73
|
+
* Constructs a closed sync stub, optionally overriding individual operations.
|
|
74
|
+
*
|
|
75
|
+
* @category constructors
|
|
76
|
+
* @since 0.1.0
|
|
77
|
+
*/
|
|
78
|
+
export const makeNoop = (overrides = {}) => SyncClient.of({
|
|
79
|
+
snapshot: () => Effect.fail(new SyncError({ code: "closed", message: "Sync client is closed" })),
|
|
80
|
+
subscribe: () => Stream.fail(new SyncError({ code: "closed", message: "Sync client is closed" })),
|
|
81
|
+
progress: Effect.succeed({ delivered: [], applied: [] }),
|
|
82
|
+
...overrides
|
|
83
|
+
});
|
|
84
|
+
/**
|
|
85
|
+
* Provides a closed sync stub.
|
|
86
|
+
*
|
|
87
|
+
* @category layers
|
|
88
|
+
* @since 0.1.0
|
|
89
|
+
*/
|
|
90
|
+
export const layerNoop = (overrides = {}) => Layer.succeed(SyncClient, makeNoop(overrides));
|
|
91
|
+
const transportError = (cause) => cause instanceof SyncError
|
|
92
|
+
? cause
|
|
93
|
+
: cause instanceof RpcClientError.RpcClientError && cause.reason._tag === "RpcClientDefect"
|
|
94
|
+
? new SyncError({ code: "protocol_violation", message: "Sync RPC protocol failed", cause: causeCode(cause.reason) })
|
|
95
|
+
: new SyncError({
|
|
96
|
+
code: "transport_failed",
|
|
97
|
+
// Bounded like `cause`. `message` is `Schema.String` and it is the
|
|
98
|
+
// declared error schema of every RPC, so a host's own sentence must not
|
|
99
|
+
// be able to be the largest thing this error carries.
|
|
100
|
+
message: cause instanceof Error ? boundedText(cause.message) : "Sync RPC transport failed",
|
|
101
|
+
cause: causeText(cause)
|
|
102
|
+
});
|
|
103
|
+
/** Effect RPC decodes malformed peer values with orDie. Restore the sync
|
|
104
|
+
* boundary's typed refusal without swallowing unrelated defects or interruption.
|
|
105
|
+
*/
|
|
106
|
+
const rpcCause = (cause) => Cause.fromReasons(Cause.map(cause, transportError).reasons.map((reason) => Cause.isDieReason(reason) && Schema.isSchemaError(reason.defect)
|
|
107
|
+
? Cause.makeFailReason(new SyncError({ code: "decode_failed", message: "Invalid sync RPC response", cause: "SchemaError" }))
|
|
108
|
+
.annotate(Context.makeUnsafe(reason.annotations))
|
|
109
|
+
: reason));
|
|
110
|
+
const cursorMap = (cursors) => new Map(cursors.map((cursor) => [cursor.runId, { ...cursor }]));
|
|
111
|
+
const canonicalCursors = (cursors) => Array.from(cursors.values(), (cursor) => ({ ...cursor })).sort((left, right) =>
|
|
112
|
+
// A Map has one value per key, so this comparator never receives equal
|
|
113
|
+
// run IDs. `cursorMap` and the acknowledged cursor state both establish
|
|
114
|
+
// that invariant before reaching this boundary.
|
|
115
|
+
left.runId > right.runId ? 1 : -1);
|
|
116
|
+
/**
|
|
117
|
+
* Advances the shared acknowledged view monotonically. A lagging subscription
|
|
118
|
+
* commits what it just consumed, but it must never move the shared cursor
|
|
119
|
+
* backward past another subscription's progress.
|
|
120
|
+
*/
|
|
121
|
+
const advance = (cursors, runId, throughSeq, generation) => {
|
|
122
|
+
const current = cursors.get(runId);
|
|
123
|
+
if (current !== undefined && current.afterSeq >= throughSeq)
|
|
124
|
+
return cursors;
|
|
125
|
+
const next = new Map(cursors);
|
|
126
|
+
next.set(runId, { runId, afterSeq: throughSeq, generation });
|
|
127
|
+
return next;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Live-follow reconnect policy: exponential backoff capped at five seconds,
|
|
131
|
+
* the same shape as `RpcClient`'s own transport retry policy, and only for
|
|
132
|
+
* transport failures — gaps, authorization refusals, server closes, and the
|
|
133
|
+
* consumer's own callback failures propagate to the consumer.
|
|
134
|
+
*/
|
|
135
|
+
const reconnectPolicy = Schedule.min([
|
|
136
|
+
Schedule.exponential(500, 1.5),
|
|
137
|
+
Schedule.spaced(5000)
|
|
138
|
+
]).pipe(Schedule.while(({ input }) => isTransportCause(input)));
|
|
139
|
+
const isTransportCause = (cause) => {
|
|
140
|
+
const reason = cause.reasons[0];
|
|
141
|
+
return cause.reasons.length === 1 && Cause.isFailReason(reason) &&
|
|
142
|
+
SyncError.is(reason.error) && reason.error.code === "transport_failed";
|
|
143
|
+
};
|
|
144
|
+
const lineageChanged = (runId) => new SyncError({
|
|
145
|
+
code: "lineage_changed",
|
|
146
|
+
message: `Run ${runId} was rewound; rebuild from its archive boundary with a fresh sync client`
|
|
147
|
+
});
|
|
148
|
+
const entriesByteLength = (entries) => entries.reduce((bytes, entry) => bytes + encodedByteLength(entry), 0);
|
|
149
|
+
const oversized = (bytes, maxFrameBytes) => new SyncError({
|
|
150
|
+
code: "frame_too_large",
|
|
151
|
+
message: `Encoded entries of ${bytes} bytes exceed the ${maxFrameBytes}-byte frame ceiling`
|
|
152
|
+
});
|
|
153
|
+
/**
|
|
154
|
+
* The first internal inconsistency of a server frame, or `undefined` for a
|
|
155
|
+
* consistent one.
|
|
156
|
+
*
|
|
157
|
+
* A schema-valid frame can still contradict itself: carry another run's
|
|
158
|
+
* entry, repeat or reorder sequences, or carry an entry outside its own
|
|
159
|
+
* declared covered interval. Applying one would corrupt the client's cursor
|
|
160
|
+
* bookkeeping, so an inconsistent frame is refused as a protocol violation
|
|
161
|
+
* before any entry is admitted or any cursor moves.
|
|
162
|
+
*/
|
|
163
|
+
const frameViolation = (frame) => {
|
|
164
|
+
let previous = -1;
|
|
165
|
+
for (const entry of frame.entries) {
|
|
166
|
+
if (entry.runId !== frame.runId) {
|
|
167
|
+
return new SyncError({
|
|
168
|
+
code: "protocol_violation",
|
|
169
|
+
cause: "foreign_run",
|
|
170
|
+
message: `Frame for run ${frame.runId} carried an entry for run ${entry.runId}`
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
if (entry.seq <= previous) {
|
|
174
|
+
return new SyncError({
|
|
175
|
+
code: "protocol_violation",
|
|
176
|
+
cause: "non_monotonic_sequence",
|
|
177
|
+
message: `Frame entries must ascend strictly: sequence ${entry.seq} arrived after ${previous}`
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (entry.seq < frame.fromSeq || frame.toSeq < entry.seq) {
|
|
181
|
+
return new SyncError({
|
|
182
|
+
code: "protocol_violation",
|
|
183
|
+
cause: "invalid_frame_interval",
|
|
184
|
+
message: `Entry ${entry.seq} lies outside the frame's covered interval ${frame.fromSeq}..${frame.toSeq}`
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
previous = entry.seq;
|
|
188
|
+
}
|
|
189
|
+
return undefined;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* The first semantic inconsistency of a catch-up page, or `undefined` for a
|
|
193
|
+
* consistent one.
|
|
194
|
+
*
|
|
195
|
+
* Every entry must belong to the requested scope, ascend strictly within its
|
|
196
|
+
* run, and lie above the requested cursor. Every entry-bearing run must have
|
|
197
|
+
* exactly one response cursor with an explicit generation matching the request.
|
|
198
|
+
*
|
|
199
|
+
* Strictly-above-the-cursor is also what makes an incomplete page CONVERGE: a
|
|
200
|
+
* page that carries entries but moves no cursor would be re-read forever.
|
|
201
|
+
*/
|
|
202
|
+
const pageViolation = (scope, requested, response) => {
|
|
203
|
+
const violation = (message) => new SyncError({ code: "protocol_violation", message, cause: "invalid_read_page" });
|
|
204
|
+
const generations = new Set();
|
|
205
|
+
for (const cursor of response.cursors) {
|
|
206
|
+
generations.add(cursor.runId);
|
|
207
|
+
if (cursor.generation === undefined)
|
|
208
|
+
return violation("Read response lacks a generation");
|
|
209
|
+
const previous = requested.get(cursor.runId);
|
|
210
|
+
if (previous !== undefined && (previous.generation ?? 0) !== cursor.generation) {
|
|
211
|
+
return lineageChanged(cursor.runId);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const highest = new Map();
|
|
215
|
+
for (const entry of response.entries) {
|
|
216
|
+
if (!covers(scope, entry.runId)) {
|
|
217
|
+
return violation(`Read page carried an entry for run ${entry.runId}, which the request's scope excludes`);
|
|
218
|
+
}
|
|
219
|
+
const floor = requested.get(entry.runId)?.afterSeq;
|
|
220
|
+
if (floor !== undefined && entry.seq <= floor) {
|
|
221
|
+
return violation(`Read page carried sequence ${entry.seq} for run ${entry.runId}, at or below the requested cursor ${floor}`);
|
|
222
|
+
}
|
|
223
|
+
const previous = highest.get(entry.runId);
|
|
224
|
+
if (previous !== undefined && entry.seq <= previous) {
|
|
225
|
+
return violation(`Read page entries for run ${entry.runId} must ascend strictly: sequence ${entry.seq} arrived after ${previous}`);
|
|
226
|
+
}
|
|
227
|
+
highest.set(entry.runId, entry.seq);
|
|
228
|
+
}
|
|
229
|
+
for (const runId of highest.keys()) {
|
|
230
|
+
if (!generations.has(runId))
|
|
231
|
+
return violation(`Read page lacks a generation cursor for run ${runId}`);
|
|
232
|
+
}
|
|
233
|
+
const duplicate = duplicateCursorRunId(response.cursors);
|
|
234
|
+
return duplicate === undefined ? undefined : violation(`Read page echoed run ${duplicate} more than once`);
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* Projects a Sync RPC client into the local replication service.
|
|
238
|
+
*
|
|
239
|
+
* Delivered and applied cursors are separate per-service `Ref` maps: concurrent
|
|
240
|
+
* subscriptions for one materialization share them, and each commit advances
|
|
241
|
+
* its matching map monotonically. A
|
|
242
|
+
* cursor advances in the same stream pull that admits each entry to the
|
|
243
|
+
* consumer, or — when the caller supplies `SubscribeOptions.apply` — only
|
|
244
|
+
* after that callback has applied the entry. Interrupted partial pages
|
|
245
|
+
* therefore retain the last admitted entry rather than incorrectly
|
|
246
|
+
* acknowledging the server's whole page.
|
|
247
|
+
*
|
|
248
|
+
* A frame or bootstrap page whose encoded entries exceed `maxFrameBytes` is
|
|
249
|
+
* refused with `frame_too_large`. Out-of-scope entries, repeated or reordered
|
|
250
|
+
* sequences, and missing response generations fail with `protocol_violation`
|
|
251
|
+
* before delivery. Each entry-bearing run in a Read page must have exactly one
|
|
252
|
+
* explicit generation cursor, and every entry must be above the requested
|
|
253
|
+
* cursor. Live frames instead drop already-covered entries and deliver only
|
|
254
|
+
* the suffix above the cursor. An incomplete bootstrap page with no entries
|
|
255
|
+
* or a completed subscription window with no frames fails with
|
|
256
|
+
* `protocol_violation` instead of reopening immediately.
|
|
257
|
+
*
|
|
258
|
+
* A live follow that loses its transport reconnects under
|
|
259
|
+
* {@link reconnectPolicy}, resuming from the acknowledged cursors; the failure
|
|
260
|
+
* cause is logged before the retry folds it. The follow spends a credit window
|
|
261
|
+
* of {@link defaultCredit} frames per subscription round and replenishes it by
|
|
262
|
+
* resubscribing from the cursors it has acknowledged. Already-covered entries
|
|
263
|
+
* are not delivered again; the round-trip cost scales with credit windows.
|
|
264
|
+
*
|
|
265
|
+
* A `compacted` refusal fails closed unless `SubscribeOptions.onResync`
|
|
266
|
+
* restores the missing prefix and returns its exact cursor. Snapshot fetching
|
|
267
|
+
* never applies state or advances cursors. Only a validated receipt for the requested run at or
|
|
268
|
+
* above its compaction floor advances the cursor before reading the suffix.
|
|
269
|
+
*
|
|
270
|
+
* A subscription's effective cursor per run is the LATER of the caller's and
|
|
271
|
+
* the matching progress map: a caller that restored its own progress is never
|
|
272
|
+
* regressed, and a caller asking for history this client already acknowledged
|
|
273
|
+
* is fast-forwarded. Rebuild a projection from an earlier position with a
|
|
274
|
+
* fresh client, whose acknowledged map is empty.
|
|
275
|
+
*
|
|
276
|
+
* This constructor carries the default policy, which is valid by
|
|
277
|
+
* construction and so cannot fail. {@link makeWith} takes an explicit
|
|
278
|
+
* {@link Options} and validates it.
|
|
279
|
+
*
|
|
280
|
+
* @category constructors
|
|
281
|
+
* @since 0.1.0
|
|
282
|
+
*/
|
|
283
|
+
export const make = ({ client }) => service(client, defaults);
|
|
284
|
+
/**
|
|
285
|
+
* Constructs the sync client under an explicit policy.
|
|
286
|
+
*
|
|
287
|
+
* Every option is validated here, once, so a client either exists under a
|
|
288
|
+
* policy it can serve or never exists at all. Validating on each `subscribe`
|
|
289
|
+
* and each `snapshot` instead accepted a `maxFrameBytes` of `NaN` at
|
|
290
|
+
* construction and then refused every operation under it, which reported a
|
|
291
|
+
* composition's mistake as a run-time failure of the follow. This mirrors
|
|
292
|
+
* `SyncServer.makeLiveWith` and `BranchCommands.makeLiveWith`.
|
|
293
|
+
*
|
|
294
|
+
* `SubscribeOptions.credit` is a REQUEST count rather than a client policy, so
|
|
295
|
+
* it is still checked where the request is built.
|
|
296
|
+
*
|
|
297
|
+
* @category constructors
|
|
298
|
+
* @since 1.0.0-rc.0
|
|
299
|
+
*/
|
|
300
|
+
export const makeWith = ({ bootstrapLimit, client, maxFrameBytes }) => Effect.flatMap(Effect.all({
|
|
301
|
+
maxFrameBytes: positiveInt("SyncClient.Options.maxFrameBytes", maxFrameBytes, defaults.maxFrameBytes),
|
|
302
|
+
bootstrapLimit: boundedInt("SyncClient.Options.bootstrapLimit", bootstrapLimit, defaults.bootstrapLimit, maxReadLimit)
|
|
303
|
+
}), (policy) => service(client, policy));
|
|
304
|
+
const service = (client, { bootstrapLimit, maxFrameBytes }) => Effect.sync(() => {
|
|
305
|
+
const delivered = Ref.makeUnsafe(new Map());
|
|
306
|
+
const applied = Ref.makeUnsafe(new Map());
|
|
307
|
+
const progress = Effect.gen(function* () {
|
|
308
|
+
return {
|
|
309
|
+
delivered: canonicalCursors(yield* Ref.get(delivered)),
|
|
310
|
+
applied: canonicalCursors(yield* Ref.get(applied))
|
|
311
|
+
};
|
|
312
|
+
});
|
|
313
|
+
const snapshot = (input) => Effect.gen(function* () {
|
|
314
|
+
const request = yield* SnapshotBoundary.request(input);
|
|
315
|
+
const supplied = yield* client["Sync.Snapshot"]({ ...request }).pipe(Effect.catchCause((cause) => Effect.failCause(rpcCause(cause))));
|
|
316
|
+
return yield* SnapshotBoundary.response(request, supplied, maxFrameBytes);
|
|
317
|
+
});
|
|
318
|
+
const subscribe = (input) => Stream.unwrap(Effect.gen(function* () {
|
|
319
|
+
const options = {
|
|
320
|
+
...input,
|
|
321
|
+
...yield* Admission.decode(RequestEnvelope, input, "invalid_request")
|
|
322
|
+
};
|
|
323
|
+
// The client's own policy was checked once, by the constructor. What
|
|
324
|
+
// is left is the count this REQUEST carries: the declared type says
|
|
325
|
+
// `number`, and `NaN` disabled each comparison it appears in rather
|
|
326
|
+
// than tightening it.
|
|
327
|
+
const credit = yield* boundedInt("SyncClient.SubscribeOptions.credit", options.credit, defaultCredit, maxSubscribeCredit);
|
|
328
|
+
const cursor = cursorMap(options.cursors);
|
|
329
|
+
// A previous delivery-only subscriber cannot skip work for an applying subscriber.
|
|
330
|
+
// Rebuilding an independent projection requires a fresh client.
|
|
331
|
+
for (const [runId, position] of yield* Ref.get(options.apply === undefined ? delivered : applied)) {
|
|
332
|
+
const supplied = cursor.get(runId);
|
|
333
|
+
if (supplied !== undefined && (supplied.generation ?? 0) !== position.generation) {
|
|
334
|
+
return yield* Effect.fail(lineageChanged(runId));
|
|
335
|
+
}
|
|
336
|
+
if (supplied === undefined || supplied.afterSeq < position.afterSeq)
|
|
337
|
+
cursor.set(runId, position);
|
|
338
|
+
}
|
|
339
|
+
const snapshot = () => canonicalCursors(cursor);
|
|
340
|
+
const commit = (runId, throughSeq, generation, wasApplied) => Effect.gen(function* () {
|
|
341
|
+
// Delivery and application have distinct cursors, but both are bound
|
|
342
|
+
// to the same journal generation and advance without an interrupt gap.
|
|
343
|
+
for (const view of wasApplied ? [delivered, applied] : [delivered]) {
|
|
344
|
+
const previous = (yield* Ref.get(view)).get(runId);
|
|
345
|
+
if (previous !== undefined && previous.generation !== generation) {
|
|
346
|
+
return yield* Effect.fail(lineageChanged(runId));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
yield* Ref.update(delivered, (shared) => advance(shared, runId, throughSeq, generation));
|
|
350
|
+
if (wasApplied)
|
|
351
|
+
yield* Ref.update(applied, (shared) => advance(shared, runId, throughSeq, generation));
|
|
352
|
+
cursor.set(runId, { runId, afterSeq: throughSeq, generation });
|
|
353
|
+
}).pipe(Effect.uninterruptible);
|
|
354
|
+
const applyEntry = options.apply;
|
|
355
|
+
/**
|
|
356
|
+
* Hands one entry to the consumer and moves its cursor.
|
|
357
|
+
*
|
|
358
|
+
* With an `apply` callback the cursor moves only after the callback
|
|
359
|
+
* succeeds, so a cursor names what the consumer materialized. Without
|
|
360
|
+
* one it moves as the entry is admitted, which is the weaker promise
|
|
361
|
+
* this client has always made and the one `RunCursor`'s JSDoc now
|
|
362
|
+
* states.
|
|
363
|
+
*
|
|
364
|
+
* The position committed is SNAPSHOTTED before the callback runs.
|
|
365
|
+
* `Entry` is a `Schema.Class`, and instances are not frozen, so
|
|
366
|
+
* reading `entry.seq` again afterwards let the consumer's own callback
|
|
367
|
+
* choose the cursor it was being handed an entry for.
|
|
368
|
+
*/
|
|
369
|
+
const deliver = (runId, entry, generation) => {
|
|
370
|
+
const through = entry.seq;
|
|
371
|
+
return Stream.concat(Stream.drain(Stream.fromEffect(applyEntry === undefined
|
|
372
|
+
? commit(runId, through, generation, false)
|
|
373
|
+
: Effect.uninterruptibleMask((restore) => Effect.andThen(restore(applyEntry(entry)), commit(runId, through, generation, true))))), Stream.succeed(entry));
|
|
374
|
+
};
|
|
375
|
+
const batch = (frame) => {
|
|
376
|
+
if (frame.generation === undefined) {
|
|
377
|
+
return Stream.fail(new SyncError({ code: "protocol_violation", message: "Entries frame lacks a generation" }));
|
|
378
|
+
}
|
|
379
|
+
if (!covers(options.scope, frame.runId)) {
|
|
380
|
+
return Stream.fail(new SyncError({
|
|
381
|
+
code: "protocol_violation",
|
|
382
|
+
message: "Live frame is outside the subscription scope",
|
|
383
|
+
cause: "foreign_run"
|
|
384
|
+
}));
|
|
385
|
+
}
|
|
386
|
+
const frameBytes = entriesByteLength(frame.entries);
|
|
387
|
+
if (frameBytes > maxFrameBytes)
|
|
388
|
+
return Stream.fail(oversized(frameBytes, maxFrameBytes));
|
|
389
|
+
const violation = frameViolation(frame);
|
|
390
|
+
if (violation !== undefined)
|
|
391
|
+
return Stream.fail(violation);
|
|
392
|
+
const position = cursor.get(frame.runId);
|
|
393
|
+
const generation = frame.generation;
|
|
394
|
+
if (position !== undefined && (position.generation ?? 0) !== generation) {
|
|
395
|
+
return Stream.fail(lineageChanged(frame.runId));
|
|
396
|
+
}
|
|
397
|
+
const afterSeq = position?.afterSeq ?? -1;
|
|
398
|
+
// `fromSeq` is the start of the server-declared covered interval. It is
|
|
399
|
+
// intentionally compared to the cursor, not to the first entry's seq:
|
|
400
|
+
// dropped journal admissions legitimately leave holes inside that range.
|
|
401
|
+
if (frame.fromSeq > afterSeq + 1) {
|
|
402
|
+
return Stream.fail(new SyncGapError({
|
|
403
|
+
runId: frame.runId,
|
|
404
|
+
expectedFrom: (afterSeq + 1),
|
|
405
|
+
receivedFrom: frame.fromSeq
|
|
406
|
+
}));
|
|
407
|
+
}
|
|
408
|
+
if (frame.toSeq <= afterSeq)
|
|
409
|
+
return Stream.empty;
|
|
410
|
+
const entries = frame.entries.filter((entry) => entry.seq > afterSeq);
|
|
411
|
+
return Stream.flatMap(Stream.fromIterable(entries), (entry) => deliver(frame.runId, entry, generation));
|
|
412
|
+
};
|
|
413
|
+
const livePage = (onFrame) => Stream.unwrap(Effect.sync(() => client["Sync.Subscribe"]({
|
|
414
|
+
protocolVersion,
|
|
415
|
+
scope: options.scope,
|
|
416
|
+
cursors: snapshot(),
|
|
417
|
+
credit,
|
|
418
|
+
...(options.capability === undefined ? {} : { capability: options.capability })
|
|
419
|
+
}))).pipe(Stream.catchCause((cause) => Stream.failCause(rpcCause(cause))), Stream.mapEffect((frame) => Admission.decode(Frame, frame)), Stream.mapEffect((frame) => frame._tag === "Entries"
|
|
420
|
+
? Effect.map(Admission.records(frame.entries), (entries) => ({ ...frame, entries }))
|
|
421
|
+
: Effect.succeed(frame)), Stream.flatMap((frame) => {
|
|
422
|
+
onFrame();
|
|
423
|
+
switch (frame._tag) {
|
|
424
|
+
case "Entries":
|
|
425
|
+
return batch(frame);
|
|
426
|
+
case "Closed":
|
|
427
|
+
return Stream.fail(new SyncError({ code: "closed", message: "Sync subscription closed" }));
|
|
428
|
+
default:
|
|
429
|
+
return Stream.empty;
|
|
430
|
+
}
|
|
431
|
+
}));
|
|
432
|
+
const live = () => Stream.unwrap(Effect.sync(() => {
|
|
433
|
+
let receivedFrame = false;
|
|
434
|
+
return Stream.concat(livePage(() => {
|
|
435
|
+
receivedFrame = true;
|
|
436
|
+
}), Stream.unwrap(Effect.sync(() => receivedFrame ? live() : Stream.fail(new SyncError({
|
|
437
|
+
code: "protocol_violation",
|
|
438
|
+
message: "Subscription window closed without frames"
|
|
439
|
+
})))));
|
|
440
|
+
}));
|
|
441
|
+
// The retry re-runs the whole follow from the acknowledged cursors,
|
|
442
|
+
// and its schedule resets once entries flow again, so a long-lived
|
|
443
|
+
// subscription pays the backoff only across consecutive failures.
|
|
444
|
+
const follow = () => live().pipe(Stream.tapCause((cause) => isTransportCause(cause)
|
|
445
|
+
? Effect.logWarning("sync live follow transport failed; reconnecting with backoff", cause)
|
|
446
|
+
: Effect.void),
|
|
447
|
+
// Schedule the complete cause so retry refusal cannot discard an
|
|
448
|
+
// accompanying defect, interruption, or tracing annotation.
|
|
449
|
+
Stream.catchCause((cause) => Stream.fail(cause)), Stream.retry(reconnectPolicy), Stream.catch((cause) => Stream.failCause(cause)));
|
|
450
|
+
const bootstrap = () => Stream.unwrap(
|
|
451
|
+
// SUSPENDED, exactly as `livePage` is. Building the payload where
|
|
452
|
+
// the recursive call is written snapshotted the cursors as page N
|
|
453
|
+
// was mapped, before a single one of its entries had reached
|
|
454
|
+
// `commit` — so read N+1 carried read N's request cursors and the
|
|
455
|
+
// server correctly re-served the page it had just served. Every
|
|
456
|
+
// catch-up page past the first arrived twice.
|
|
457
|
+
Effect.suspend(() => {
|
|
458
|
+
const requested = snapshot();
|
|
459
|
+
return client["Sync.Read"]({
|
|
460
|
+
protocolVersion,
|
|
461
|
+
scope: options.scope,
|
|
462
|
+
cursors: requested,
|
|
463
|
+
limit: bootstrapLimit,
|
|
464
|
+
...(options.capability === undefined ? {} : { capability: options.capability })
|
|
465
|
+
}).pipe(Effect.catchCause((cause) => Effect.failCause(rpcCause(cause))), Effect.flatMap((response) => Admission.decode(ReadResponse, response)), Effect.flatMap((response) => Effect.map(Admission.records(response.entries), (entries) => ({ ...response, entries }))), Effect.map((response) => {
|
|
466
|
+
const pageBytes = entriesByteLength(response.entries);
|
|
467
|
+
if (pageBytes > maxFrameBytes)
|
|
468
|
+
return Stream.fail(oversized(pageBytes, maxFrameBytes));
|
|
469
|
+
const violation = pageViolation(options.scope, cursorMap(requested), response);
|
|
470
|
+
if (violation !== undefined)
|
|
471
|
+
return Stream.fail(violation);
|
|
472
|
+
const generations = new Map(response.cursors.map((cursor) => [cursor.runId, cursor.generation]));
|
|
473
|
+
const page = Stream.flatMap(Stream.fromIterable(response.entries), (entry) => deliver(entry.runId, entry, generations.get(entry.runId)));
|
|
474
|
+
if (response.done)
|
|
475
|
+
return Stream.concat(page, follow());
|
|
476
|
+
if (response.entries.length === 0) {
|
|
477
|
+
// An incomplete page with no entries can never converge:
|
|
478
|
+
// the next read would carry the same cursors and receive
|
|
479
|
+
// the same page. Fail typed instead of spinning.
|
|
480
|
+
return Stream.fail(new SyncError({
|
|
481
|
+
code: "protocol_violation",
|
|
482
|
+
message: "Bootstrap read reported more durable entries but returned an empty page"
|
|
483
|
+
}));
|
|
484
|
+
}
|
|
485
|
+
return Stream.concat(page, bootstrap());
|
|
486
|
+
}));
|
|
487
|
+
}));
|
|
488
|
+
/**
|
|
489
|
+
* The checkpoint one failure says to resume from, or `undefined` when
|
|
490
|
+
* the failure is not a recoverable compaction.
|
|
491
|
+
*
|
|
492
|
+
* A compaction whose checkpoint is at or below what this subscription
|
|
493
|
+
* already covers cannot move it forward, so retrying would re-read the
|
|
494
|
+
* same refusal forever. That is the same non-convergence rule the
|
|
495
|
+
* empty incomplete bootstrap page is refused under, and it is why a
|
|
496
|
+
* `compacted` failure carrying no {@link Resync} stays a failure
|
|
497
|
+
* rather than becoming a silent stall.
|
|
498
|
+
*/
|
|
499
|
+
const resyncTarget = (failure) => {
|
|
500
|
+
if (!SyncError.is(failure) || failure.code !== "compacted" || failure.resync === undefined) {
|
|
501
|
+
return undefined;
|
|
502
|
+
}
|
|
503
|
+
const covered = cursor.get(failure.resync.runId)?.afterSeq ?? -1;
|
|
504
|
+
return covered >= failure.resync.checkpointSeq ? undefined : failure.resync;
|
|
505
|
+
};
|
|
506
|
+
const onResync = options.onResync;
|
|
507
|
+
const resynced = () => Stream.catchCause(bootstrap(), (cause) => {
|
|
508
|
+
const reason = cause.reasons[0];
|
|
509
|
+
if (cause.reasons.length !== 1 || !Cause.isFailReason(reason))
|
|
510
|
+
return Stream.failCause(cause);
|
|
511
|
+
const failure = reason.error;
|
|
512
|
+
const resync = resyncTarget(failure);
|
|
513
|
+
if (resync === undefined)
|
|
514
|
+
return Stream.failCause(cause);
|
|
515
|
+
if (!covers(options.scope, resync.runId)) {
|
|
516
|
+
return Stream.fail(new SyncError({
|
|
517
|
+
code: "protocol_violation",
|
|
518
|
+
message: "Compaction target is outside the subscription scope"
|
|
519
|
+
}));
|
|
520
|
+
}
|
|
521
|
+
if (onResync === undefined)
|
|
522
|
+
return Stream.failCause(cause);
|
|
523
|
+
// Snapshotted before the hook runs, for the reason `deliver`
|
|
524
|
+
// snapshots: the hook is handed the value the commit is read from,
|
|
525
|
+
// and re-reading it afterwards let a consumer move its own cursor
|
|
526
|
+
// arbitrarily far forward from inside the callback.
|
|
527
|
+
const runId = resync.runId;
|
|
528
|
+
const checkpointSeq = resync.checkpointSeq;
|
|
529
|
+
const refusedGeneration = resync.generation;
|
|
530
|
+
return Stream.unwrap(Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
531
|
+
// Application remains interruptible. Once it returns success,
|
|
532
|
+
// validation and the in-memory commit finish without an async
|
|
533
|
+
// cancellation gap. Durable state/cursor atomicity belongs to
|
|
534
|
+
// the consumer's own transaction, not to this mask.
|
|
535
|
+
const receipt = yield* restore(onResync(resync));
|
|
536
|
+
const restoredCursor = () => new SyncError({
|
|
537
|
+
code: "invalid_request",
|
|
538
|
+
message: "Recovery must return the restored run cursor at or above its compaction floor"
|
|
539
|
+
});
|
|
540
|
+
// A receipt that omits its generation restored the floor's
|
|
541
|
+
// history, which the refusal names. The previous cursor's
|
|
542
|
+
// generation is no substitute: a fresh client has none,
|
|
543
|
+
// and a rewind since then has replaced it.
|
|
544
|
+
const captured = yield* Effect.try({
|
|
545
|
+
try: () => ({
|
|
546
|
+
runId: receipt.runId,
|
|
547
|
+
afterSeq: receipt.afterSeq,
|
|
548
|
+
generation: receipt.generation ?? refusedGeneration
|
|
549
|
+
}),
|
|
550
|
+
catch: restoredCursor
|
|
551
|
+
});
|
|
552
|
+
if (!Schema.is(RunCursor)(captured) || captured.runId !== runId || captured.afterSeq < checkpointSeq) {
|
|
553
|
+
return yield* Effect.fail(restoredCursor());
|
|
554
|
+
}
|
|
555
|
+
const applied = captured;
|
|
556
|
+
if (applied.generation === undefined) {
|
|
557
|
+
return yield* Effect.fail(new SyncError({
|
|
558
|
+
code: "protocol_violation",
|
|
559
|
+
message: "Compaction refusal names no generation for a receipt that omits one"
|
|
560
|
+
}));
|
|
561
|
+
}
|
|
562
|
+
yield* commit(applied.runId, applied.afterSeq, applied.generation, true);
|
|
563
|
+
return resynced();
|
|
564
|
+
})));
|
|
565
|
+
});
|
|
566
|
+
return resynced();
|
|
567
|
+
}));
|
|
568
|
+
return SyncClient.of({ snapshot, subscribe, progress });
|
|
569
|
+
});
|
|
570
|
+
/**
|
|
571
|
+
* Provides the sync client from the active RPC protocol.
|
|
572
|
+
*
|
|
573
|
+
* @category layers
|
|
574
|
+
* @since 0.1.0
|
|
575
|
+
*/
|
|
576
|
+
export const layer = Layer.effect(SyncClient, Effect.flatMap(RpcClient.make(SyncRpcs), (client) => make({ client })));
|
|
577
|
+
/**
|
|
578
|
+
* Provides the sync client under an explicit policy. Fails with
|
|
579
|
+
* `invalid_request` when an option is not a positive safe integer, so a bad
|
|
580
|
+
* policy fails the composition rather than every operation under it.
|
|
581
|
+
*
|
|
582
|
+
* @category layers
|
|
583
|
+
* @since 1.0.0-rc.0
|
|
584
|
+
*/
|
|
585
|
+
export const layerWith = (options) => Layer.effect(SyncClient, Effect.flatMap(RpcClient.make(SyncRpcs), (client) => makeWith({ ...options, client })));
|
|
586
|
+
//# sourceMappingURL=SyncClient.js.map
|