@rindle/remote 0.1.0-rc.5
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/LICENSE +201 -0
- package/README.md +119 -0
- package/dist/backend.d.ts +31 -0
- package/dist/backend.d.ts.map +1 -0
- package/dist/backend.js +136 -0
- package/dist/backend.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/mutation-queue.d.ts +26 -0
- package/dist/mutation-queue.d.ts.map +1 -0
- package/dist/mutation-queue.js +62 -0
- package/dist/mutation-queue.js.map +1 -0
- package/dist/normalized.d.ts +50 -0
- package/dist/normalized.d.ts.map +1 -0
- package/dist/normalized.js +146 -0
- package/dist/normalized.js.map +1 -0
- package/dist/optimistic-source.d.ts +108 -0
- package/dist/optimistic-source.d.ts.map +1 -0
- package/dist/optimistic-source.js +317 -0
- package/dist/optimistic-source.js.map +1 -0
- package/dist/protocol.d.ts +134 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +178 -0
- package/dist/protocol.js.map +1 -0
- package/dist/remote-source.d.ts +31 -0
- package/dist/remote-source.d.ts.map +1 -0
- package/dist/remote-source.js +133 -0
- package/dist/remote-source.js.map +1 -0
- package/dist/subscribe.d.ts +26 -0
- package/dist/subscribe.d.ts.map +1 -0
- package/dist/subscribe.js +14 -0
- package/dist/subscribe.js.map +1 -0
- package/dist/transport.d.ts +53 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +105 -0
- package/dist/transport.js.map +1 -0
- package/package.json +39 -0
- package/src/backend.ts +163 -0
- package/src/index.ts +40 -0
- package/src/mutation-queue.ts +96 -0
- package/src/normalized.ts +188 -0
- package/src/optimistic-source.ts +374 -0
- package/src/protocol.ts +252 -0
- package/src/remote-source.ts +171 -0
- package/src/subscribe.ts +40 -0
- package/src/transport.ts +127 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# @rindle/remote — network backend for @rindle/client
|
|
2
|
+
|
|
3
|
+
A `Backend` that drives the **same** `Store` / `ArrayView` as the local backends
|
|
4
|
+
([`@rindle/wasm`](../wasm), [`@rindle/replica`](../replica)) — but over a network transport to a
|
|
5
|
+
server (e.g. [`@rindle/server`](../reference/server)). The core never sees the wire protocol: the
|
|
6
|
+
`RemoteBackend` owns the epoch/seq/gap handshake and hands only clean `hello`/`snapshot`/
|
|
7
|
+
`batch` changes up to the `ArrayView` (WASM-CLIENT-DESIGN.md §2.2). Re-exports `@rindle/client`.
|
|
8
|
+
|
|
9
|
+
## Quick start
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import {
|
|
13
|
+
createRemoteStore,
|
|
14
|
+
createSchema,
|
|
15
|
+
defineQuery,
|
|
16
|
+
newQueryBuilder,
|
|
17
|
+
number,
|
|
18
|
+
string,
|
|
19
|
+
table,
|
|
20
|
+
} from "@rindle/remote";
|
|
21
|
+
|
|
22
|
+
const issue = table("issue").columns({ id: number(), title: string() }).primaryKey("id");
|
|
23
|
+
const schema = createSchema({ tables: [issue] });
|
|
24
|
+
const q = newQueryBuilder(schema);
|
|
25
|
+
const issueQueries = {
|
|
26
|
+
issueByID: defineQuery("issueByID", (args: { id: number }) => q.issue.where.id(args.id)),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const store = createRemoteStore(schema, "ws://localhost:3000");
|
|
30
|
+
const view = store.materialize(issueQueries.issueByID({ id: 1 }));
|
|
31
|
+
view.subscribe(render); // fires `[]` immediately, then the server's snapshot
|
|
32
|
+
await store.write((tx) => tx.add("issue", { id: 1, title: "x" })); // sent up; batches stream back
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Reads are **eventually consistent**: `materialize()` returns immediately with a pending view
|
|
36
|
+
(`[]` / `null`) and fills in when the server's `hello` + snapshot arrive; writes are sent and
|
|
37
|
+
the resulting changes stream back (the §2.3 write asymmetry — same API, async timing). The
|
|
38
|
+
default transport uses the global `WebSocket` (Node 22+ and browsers — zero dependency); pass a
|
|
39
|
+
custom `Transport` to `createRemoteStore` for ws/sse/http of your choosing.
|
|
40
|
+
|
|
41
|
+
Remote subscriptions are named by default: the client sends only `{ name, args }`. The local query
|
|
42
|
+
function builds the client view AST; the embedded server resolves the same name in its own query
|
|
43
|
+
registry.
|
|
44
|
+
|
|
45
|
+
For daemon/serverless deployments, pass `resolveSubscribe`. It can call your API server to
|
|
46
|
+
authenticate the user, validate args, materialize the query on `rindle-server`, and return the
|
|
47
|
+
opaque lease the browser should present to the daemon:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
const store = createRemoteStore(schema, daemonTransport, {
|
|
51
|
+
resolveSubscribe: async ({ remote }) => {
|
|
52
|
+
const res = await fetch("/api/rindle/query-lease", {
|
|
53
|
+
method: "POST",
|
|
54
|
+
body: JSON.stringify(remote), // { name, args }
|
|
55
|
+
});
|
|
56
|
+
return { leaseToken: (await res.json()).leaseToken };
|
|
57
|
+
},
|
|
58
|
+
sendMutation: async (mutations) => {
|
|
59
|
+
await fetch("/api/rindle/write", { method: "POST", body: JSON.stringify({ mutations }) });
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## The protocol (for servers)
|
|
65
|
+
|
|
66
|
+
`@rindle/remote` also exports the wire contract a server frames with — a port of
|
|
67
|
+
`src/flat_protocol.rs` + `src/wire_schema.rs`:
|
|
68
|
+
|
|
69
|
+
- **`schemaFp(wireSchema)`** — the FNV-1a 64 content fingerprint (16-char hex), byte-for-byte
|
|
70
|
+
the engine's `schema_fp` (checked against Rust-dumped vectors in `test/`).
|
|
71
|
+
- **`COMPARATOR_VERSION`** — the comparator algorithm-contract version.
|
|
72
|
+
- **`Publisher`** (server) — stamps batches with `epoch`/`seq`/`schemaFp`; snapshot is seq 0,
|
|
73
|
+
increments seq 1, 2, …; an empty transaction emits no batch (so a gap = a lost batch).
|
|
74
|
+
- **`Subscriber`** (client) — validates a frame stream (comparator at `hello`; per batch: epoch,
|
|
75
|
+
fingerprint, strict in-order seq) and emits clean `ChangeEvent`s. Does **not** fold — the
|
|
76
|
+
core `ArrayView` does (the §2.2 split that lets one view serve both local and remote).
|
|
77
|
+
|
|
78
|
+
On a gap / drift the `RemoteBackend` re-hydrates internally: it re-subscribes, the server
|
|
79
|
+
re-registers under a **new epoch** and replies with a fresh snapshot, and the `ArrayView` resets
|
|
80
|
+
in place — the materialized view reference the caller holds survives.
|
|
81
|
+
|
|
82
|
+
Chunked snapshots (for very large hydrates) are a deferred follow-up; today the snapshot is one
|
|
83
|
+
seq-0 batch.
|
|
84
|
+
|
|
85
|
+
## The optimistic source
|
|
86
|
+
|
|
87
|
+
For the optimistic-writes path (OPTIMISTIC-WRITES-DESIGN.md §8), `RemoteOptimisticSource`
|
|
88
|
+
implements `@rindle/client`'s `OptimisticSource` over the same transport: the normalized
|
|
89
|
+
subscription stream with **`cv`-stamped** frames, plus `init` (the connection's stable
|
|
90
|
+
clientID), `pushMutation` (named-mutator envelopes up), and connection-level **progress
|
|
91
|
+
frames** `{cvMin, lmid, rejected}` down. Plug it into `@rindle/optimistic`'s
|
|
92
|
+
`createOptimisticStore` against a [`@rindle/server`](../reference/server) `createOptimisticServer`:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { createRemoteOptimisticSource } from "@rindle/remote";
|
|
96
|
+
import { createOptimisticStore } from "@rindle/optimistic";
|
|
97
|
+
|
|
98
|
+
const source = createRemoteOptimisticSource("ws://localhost:3000", clientID);
|
|
99
|
+
const { store, backend, mutate } = createOptimisticStore(schema, source, clientRegistry, { clientID });
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
In daemon/serverless mode, use the same `resolveSubscribe` hook for query leases and override
|
|
103
|
+
`pushMutation` so custom mutators go to your API server. The API server remains the authority and
|
|
104
|
+
forwards an approved transaction to `rindle-server`; progress frames still arrive on the daemon
|
|
105
|
+
stream:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
const source = createRemoteOptimisticSource(daemonTransport, clientID, {
|
|
109
|
+
resolveSubscribe: async ({ remote }) => {
|
|
110
|
+
const lease = await api.createQueryLease(remote);
|
|
111
|
+
return { leaseToken: lease.token };
|
|
112
|
+
},
|
|
113
|
+
pushMutation: (envelope) => api.runMutator(envelope),
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Validation is the ordinary normalized subscriber; on a gap it re-subscribes and the server's
|
|
118
|
+
fresh `cv`-stamped snapshot (released by its progress frame) re-hydrates — still-pending
|
|
119
|
+
mutations survive, re-invoked against the recovered base.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Store } from "@rindle/client";
|
|
2
|
+
import type { Backend, ChangeEvent, ColsMap, Mutation, QueryId, RemoteQuery, Schema } from "@rindle/client";
|
|
3
|
+
import { type RawMutationSender, type SubscribeResolver } from "./subscribe.ts";
|
|
4
|
+
import type { Transport } from "./transport.ts";
|
|
5
|
+
export interface RemoteBackendOptions {
|
|
6
|
+
/** Resolve the upstream subscribe target. Defaults to embedded-server `{name,args}`. */
|
|
7
|
+
resolveSubscribe?: SubscribeResolver;
|
|
8
|
+
/** Override raw authoritative writes, e.g. POST them to an app API server. */
|
|
9
|
+
sendMutation?: RawMutationSender;
|
|
10
|
+
}
|
|
11
|
+
export declare class RemoteBackend implements Backend {
|
|
12
|
+
private readonly transport;
|
|
13
|
+
private readonly resolveSubscribe;
|
|
14
|
+
private readonly sendMutation?;
|
|
15
|
+
private handler;
|
|
16
|
+
private readonly subs;
|
|
17
|
+
constructor(transport: Transport, opts?: RemoteBackendOptions);
|
|
18
|
+
registerQuery(qid: QueryId, _ast: unknown, remote?: RemoteQuery): void;
|
|
19
|
+
unregisterQuery(qid: QueryId): void;
|
|
20
|
+
/** Eventually-consistent: the mutation is sent; the resulting batches arrive async on the
|
|
21
|
+
* stream. The promise resolves once sent (the §2.3 write asymmetry, named not leaked). */
|
|
22
|
+
mutate(mutations: Mutation[]): Promise<void>;
|
|
23
|
+
onEvent(handler: (qid: QueryId, ev: ChangeEvent) => void): void;
|
|
24
|
+
private subscribe;
|
|
25
|
+
private onServerMsg;
|
|
26
|
+
private openSubscriber;
|
|
27
|
+
private applyBatch;
|
|
28
|
+
}
|
|
29
|
+
/** Convenience: a `Store` backed by a remote server, over a ws URL or a custom transport. */
|
|
30
|
+
export declare function createRemoteStore<S extends ColsMap>(schema: Schema<S>, urlOrTransport: string | Transport, opts?: RemoteBackendOptions): Store<S>;
|
|
31
|
+
//# sourceMappingURL=backend.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backend.d.ts","sourceRoot":"","sources":["../src/backend.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAI5G,OAAO,EAIL,KAAK,iBAAiB,EACtB,KAAK,iBAAiB,EACvB,MAAM,gBAAgB,CAAC;AAExB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAahD,MAAM,WAAW,oBAAoB;IACnC,wFAAwF;IACxF,gBAAgB,CAAC,EAAE,iBAAiB,CAAC;IACrC,8EAA8E;IAC9E,YAAY,CAAC,EAAE,iBAAiB,CAAC;CAClC;AAED,qBAAa,aAAc,YAAW,OAAO;IAC3C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;IACtC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAoB;IACrD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAoB;IAClD,OAAO,CAAC,OAAO,CAAqD;IACpE,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA8B;gBAEvC,SAAS,EAAE,SAAS,EAAE,IAAI,GAAE,oBAAyB;IAOjE,aAAa,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,IAAI;IAStE,eAAe,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI;IAKnC;+FAC2F;IAC3F,MAAM,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAM5C,OAAO,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,WAAW,KAAK,IAAI,GAAG,IAAI;IAM/D,OAAO,CAAC,SAAS;IAyBjB,OAAO,CAAC,WAAW;IAcnB,OAAO,CAAC,cAAc;IAetB,OAAO,CAAC,UAAU;CAcnB;AAED,6FAA6F;AAC7F,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,OAAO,EACjD,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EACjB,cAAc,EAAE,MAAM,GAAG,SAAS,EAClC,IAAI,GAAE,oBAAyB,GAC9B,KAAK,CAAC,CAAC,CAAC,CAGV"}
|
package/dist/backend.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// The RemoteBackend: a `@rindle/client` `Backend` over a network transport. It owns the
|
|
2
|
+
// epoch/seq/gap protocol (via {@link Subscriber}) and emits only CLEAN, in-order
|
|
3
|
+
// `hello`/`snapshot`/`batch` `ChangeEvent`s upward — so the same `Store`/`ArrayView` that
|
|
4
|
+
// drives the local backends drives this one, never seeing seq/epoch (WASM-CLIENT-DESIGN.md §2.2).
|
|
5
|
+
//
|
|
6
|
+
// On a gap (or epoch/schema drift) the backend re-hydrates INTERNALLY: it re-subscribes, the
|
|
7
|
+
// server re-registers the query under a NEW epoch and replies with a fresh hello + snapshot,
|
|
8
|
+
// and the `ArrayView` resets in place — the caller's materialized view reference survives.
|
|
9
|
+
import { Store } from "@rindle/client";
|
|
10
|
+
import { ProtocolError, Subscriber } from "./protocol.js";
|
|
11
|
+
import { defaultSubscribeTarget, isThenable, subscribeMessage, } from "./subscribe.js";
|
|
12
|
+
import { WsTransport } from "./transport.js";
|
|
13
|
+
export class RemoteBackend {
|
|
14
|
+
transport;
|
|
15
|
+
resolveSubscribe;
|
|
16
|
+
sendMutation;
|
|
17
|
+
handler = () => { };
|
|
18
|
+
subs = new Map();
|
|
19
|
+
constructor(transport, opts = {}) {
|
|
20
|
+
this.transport = transport;
|
|
21
|
+
this.resolveSubscribe = opts.resolveSubscribe ?? defaultSubscribeTarget;
|
|
22
|
+
this.sendMutation = opts.sendMutation;
|
|
23
|
+
this.transport.onMessage((msg) => this.onServerMsg(msg));
|
|
24
|
+
}
|
|
25
|
+
registerQuery(qid, _ast, remote) {
|
|
26
|
+
if (!remote) {
|
|
27
|
+
console.error(`[rindle-remote] flat query ${qid} has no named remote identity`);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.subs.set(qid, { remote, subscriber: null, epoch: 0, resubscribing: false, subscribeTicket: 0 });
|
|
31
|
+
this.subscribe(qid, remote);
|
|
32
|
+
}
|
|
33
|
+
unregisterQuery(qid) {
|
|
34
|
+
this.subs.delete(qid);
|
|
35
|
+
this.transport.send({ t: "unsubscribe", queryId: qid });
|
|
36
|
+
}
|
|
37
|
+
/** Eventually-consistent: the mutation is sent; the resulting batches arrive async on the
|
|
38
|
+
* stream. The promise resolves once sent (the §2.3 write asymmetry, named not leaked). */
|
|
39
|
+
mutate(mutations) {
|
|
40
|
+
if (this.sendMutation)
|
|
41
|
+
return Promise.resolve(this.sendMutation(mutations));
|
|
42
|
+
this.transport.send({ t: "mutate", mutations });
|
|
43
|
+
return Promise.resolve();
|
|
44
|
+
}
|
|
45
|
+
onEvent(handler) {
|
|
46
|
+
this.handler = handler;
|
|
47
|
+
}
|
|
48
|
+
// --- internals ---------------------------------------------------------------
|
|
49
|
+
subscribe(qid, remote) {
|
|
50
|
+
const s = this.subs.get(qid);
|
|
51
|
+
if (!s)
|
|
52
|
+
return;
|
|
53
|
+
const request = { queryId: qid, remote, mode: "flat" };
|
|
54
|
+
const ticket = ++s.subscribeTicket;
|
|
55
|
+
const send = (target) => {
|
|
56
|
+
const cur = this.subs.get(qid);
|
|
57
|
+
if (cur !== s || cur.subscribeTicket !== ticket)
|
|
58
|
+
return;
|
|
59
|
+
this.transport.send(subscribeMessage(request, target));
|
|
60
|
+
};
|
|
61
|
+
const fail = (err) => {
|
|
62
|
+
const cur = this.subs.get(qid);
|
|
63
|
+
if (cur !== s || cur.subscribeTicket !== ticket)
|
|
64
|
+
return;
|
|
65
|
+
s.resubscribing = false;
|
|
66
|
+
console.error(`[rindle-remote] query ${qid} subscribe resolution failed: ${String(err?.message ?? err)}`);
|
|
67
|
+
};
|
|
68
|
+
try {
|
|
69
|
+
const target = this.resolveSubscribe(request);
|
|
70
|
+
if (isThenable(target))
|
|
71
|
+
void target.then(send, fail);
|
|
72
|
+
else
|
|
73
|
+
send(target);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
fail(err);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
onServerMsg(msg) {
|
|
80
|
+
if (msg.t === "queryError") {
|
|
81
|
+
this.subs.delete(msg.queryId);
|
|
82
|
+
console.error(`[rindle-remote] query ${msg.queryId} subscription rejected: ${msg.message}`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
// This backend is flat-only; it ignores normalized frames (`nhello`/`nbatch`).
|
|
86
|
+
if (msg.t !== "hello" && msg.t !== "batch")
|
|
87
|
+
return;
|
|
88
|
+
const s = this.subs.get(msg.queryId);
|
|
89
|
+
if (!s)
|
|
90
|
+
return; // unsubscribed / unknown query
|
|
91
|
+
if (msg.t === "hello")
|
|
92
|
+
this.openSubscriber(msg.queryId, s, msg.hello);
|
|
93
|
+
else
|
|
94
|
+
this.applyBatch(msg.queryId, s, msg.batch);
|
|
95
|
+
}
|
|
96
|
+
openSubscriber(qid, s, hello) {
|
|
97
|
+
try {
|
|
98
|
+
// The Subscriber ctor validates the comparator + fingerprint and emits the `hello`
|
|
99
|
+
// ChangeEvent (→ the Store resets the view to this schema).
|
|
100
|
+
s.subscriber = new Subscriber(hello, (ev) => this.handler(qid, ev));
|
|
101
|
+
s.epoch = hello.epoch;
|
|
102
|
+
s.resubscribing = false;
|
|
103
|
+
}
|
|
104
|
+
catch (e) {
|
|
105
|
+
// A comparator/schema mismatch at hello is unrecoverable (a code-contract divergence) —
|
|
106
|
+
// leave the view pending and report it; do not loop.
|
|
107
|
+
s.subscriber = null;
|
|
108
|
+
console.error(`[rindle-remote] query ${qid} subscription rejected: ${e.message}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
applyBatch(qid, s, batch) {
|
|
112
|
+
if (!s.subscriber)
|
|
113
|
+
return; // no hello yet (or mid re-hydrate)
|
|
114
|
+
if (batch.epoch < s.epoch)
|
|
115
|
+
return; // a stale batch from a superseded epoch — drop
|
|
116
|
+
try {
|
|
117
|
+
s.subscriber.apply(batch);
|
|
118
|
+
}
|
|
119
|
+
catch (e) {
|
|
120
|
+
if (!(e instanceof ProtocolError))
|
|
121
|
+
throw e;
|
|
122
|
+
if (s.resubscribing)
|
|
123
|
+
return; // already recovering
|
|
124
|
+
// Gap / drift → re-hydrate under a new epoch (the server bumps it on re-subscribe).
|
|
125
|
+
s.resubscribing = true;
|
|
126
|
+
s.subscriber = null;
|
|
127
|
+
this.subscribe(qid, s.remote);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** Convenience: a `Store` backed by a remote server, over a ws URL or a custom transport. */
|
|
132
|
+
export function createRemoteStore(schema, urlOrTransport, opts = {}) {
|
|
133
|
+
const transport = typeof urlOrTransport === "string" ? new WsTransport(urlOrTransport) : urlOrTransport;
|
|
134
|
+
return new Store(schema, new RemoteBackend(transport, opts));
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=backend.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backend.js","sourceRoot":"","sources":["../src/backend.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,iFAAiF;AACjF,0FAA0F;AAC1F,kGAAkG;AAClG,EAAE;AACF,6FAA6F;AAC7F,6FAA6F;AAC7F,2FAA2F;AAE3F,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAGvC,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE1D,OAAO,EACL,sBAAsB,EACtB,UAAU,EACV,gBAAgB,GAGjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAqB7C,MAAM,OAAO,aAAa;IACP,SAAS,CAAY;IACrB,gBAAgB,CAAoB;IACpC,YAAY,CAAqB;IAC1C,OAAO,GAA4C,GAAG,EAAE,GAAE,CAAC,CAAC;IACnD,IAAI,GAAG,IAAI,GAAG,EAAmB,CAAC;IAEnD,YAAY,SAAoB,EAAE,OAA6B,EAAE;QAC/D,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,gBAAgB,GAAG,IAAI,CAAC,gBAAgB,IAAI,sBAAsB,CAAC;QACxE,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC;QACtC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC;IAC3D,CAAC;IAED,aAAa,CAAC,GA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AAU,CAAC,GAAY,EAAE,CAAS,EAAE,KAAY;QACtD,IAAI,CAAC,CAAC,CAAC,UAAU;YAAE,OAAO,CAAC,mCAAmC;QAC9D,IAAI,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK;YAAE,OAAO,CAAC,+CAA+C;QAClF,IAAI,CAAC;YACH,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC5B,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,IAAI,CAAC,CAAC,CAAC,YAAY,aAAa,CAAC;gBAAE,MAAM,CAAC,CAAC;YAC3C,IAAI,CAAC,CAAC,aAAa;gBAAE,OAAO,CAAC,qBAAqB;YAClD,oFAAoF;YACpF,CAAC,CAAC,aAAa,GAAG,IAAI,CAAC;YACvB,CAAC,CAAC,UAAU,GAAG,IAAI,CAAC;YACpB,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;CACF;AAED,6FAA6F;AAC7F,MAAM,UAAU,iBAAiB,CAC/B,MAAiB,EACjB,cAAkC,EAClC,OAA6B,EAAE;IAE/B,MAAM,SAAS,GAAG,OAAO,cAAc,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,WAAW,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC;IACxG,OAAO,IAAI,KAAK,CAAC,MAAM,EAAE,IAAI,aAAa,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC;AAC/D,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from "@rindle/client";
|
|
2
|
+
export { RemoteBackend, createRemoteStore } from "./backend.ts";
|
|
3
|
+
export type { RemoteBackendOptions } from "./backend.ts";
|
|
4
|
+
export { WsTransport } from "./transport.ts";
|
|
5
|
+
export type { Transport } from "./transport.ts";
|
|
6
|
+
export type { MutationEnvelopeSender, RawMutationSender, SubscribeMode, SubscribeRequest, SubscribeResolver, SubscribeTarget, } from "./subscribe.ts";
|
|
7
|
+
export { COMPARATOR_VERSION, Publisher, ProtocolError, Subscriber, schemaFp } from "./protocol.ts";
|
|
8
|
+
export type { Batch, ClientMsg, Hello, ProtocolErrorKind, ServerMsg, SubscribeClientMsg } from "./protocol.ts";
|
|
9
|
+
export { NormalizedSubscriber, normalizedFp } from "./normalized.ts";
|
|
10
|
+
export type { NormalizedBatch, NormalizedHello } from "./normalized.ts";
|
|
11
|
+
export { RemoteNormalizedSource, createRemoteNormalizedSource } from "./remote-source.ts";
|
|
12
|
+
export type { RemoteNormalizedSourceOptions } from "./remote-source.ts";
|
|
13
|
+
export { RemoteOptimisticSource, createRemoteOptimisticSource } from "./optimistic-source.ts";
|
|
14
|
+
export type { RemoteOptimisticConnection, RemoteOptimisticSourceOptions, TransportFactory, } from "./optimistic-source.ts";
|
|
15
|
+
export { createQueuedMutationSender } from "./mutation-queue.ts";
|
|
16
|
+
export type { PushOutcome, QueuedMutationSenderOptions } from "./mutation-queue.ts";
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,cAAc,gBAAgB,CAAC;AAE/B,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAChE,YAAY,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,YAAY,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,YAAY,EACV,sBAAsB,EACtB,iBAAiB,EACjB,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACnG,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAG/G,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACrE,YAAY,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,sBAAsB,EAAE,4BAA4B,EAAE,MAAM,oBAAoB,CAAC;AAC1F,YAAY,EAAE,6BAA6B,EAAE,MAAM,oBAAoB,CAAC;AAGxE,OAAO,EAAE,sBAAsB,EAAE,4BAA4B,EAAE,MAAM,wBAAwB,CAAC;AAC9F,YAAY,EACV,0BAA0B,EAC1B,6BAA6B,EAC7B,gBAAgB,GACjB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AACjE,YAAY,EAAE,WAAW,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// @rindle/remote — the network backend for @rindle/client: a `RemoteBackend` over a transport,
|
|
2
|
+
// driving the SAME Store/ArrayView as the local (wasm/replica) backends. Also exports the
|
|
3
|
+
// wire protocol (frames + `Publisher`/`Subscriber` + `schemaFp`) a server uses to talk to it.
|
|
4
|
+
// Re-exports @rindle/client so an app can import everything from here.
|
|
5
|
+
export * from "@rindle/client";
|
|
6
|
+
export { RemoteBackend, createRemoteStore } from "./backend.js";
|
|
7
|
+
export { WsTransport } from "./transport.js";
|
|
8
|
+
export { COMPARATOR_VERSION, Publisher, ProtocolError, Subscriber, schemaFp } from "./protocol.js";
|
|
9
|
+
// The normalized subscription protocol (the path-free twin) + the client-side source.
|
|
10
|
+
export { NormalizedSubscriber, normalizedFp } from "./normalized.js";
|
|
11
|
+
export { RemoteNormalizedSource, createRemoteNormalizedSource } from "./remote-source.js";
|
|
12
|
+
// The optimistic-writes source (the normalized stream + mutation push + progress frames).
|
|
13
|
+
export { RemoteOptimisticSource, createRemoteOptimisticSource } from "./optimistic-source.js";
|
|
14
|
+
// The serverless mutation queue: confirmed, in-order batches over an unordered HTTP hop.
|
|
15
|
+
export { createQueuedMutationSender } from "./mutation-queue.js";
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,0FAA0F;AAC1F,8FAA8F;AAC9F,uEAAuE;AAEvE,cAAc,gBAAgB,CAAC;AAE/B,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEhE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAW7C,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGnG,sFAAsF;AACtF,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAErE,OAAO,EAAE,sBAAsB,EAAE,4BAA4B,EAAE,MAAM,oBAAoB,CAAC;AAG1F,0FAA0F;AAC1F,OAAO,EAAE,sBAAsB,EAAE,4BAA4B,EAAE,MAAM,wBAAwB,CAAC;AAO9F,yFAAyF;AACzF,OAAO,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { MutationEnvelope } from "@rindle/client";
|
|
2
|
+
import type { MutationEnvelopeSender } from "./subscribe.ts";
|
|
3
|
+
/** One envelope's outcome from the API server's mutate endpoint. */
|
|
4
|
+
export interface PushOutcome {
|
|
5
|
+
accepted: boolean;
|
|
6
|
+
reason?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface QueuedMutationSenderOptions {
|
|
9
|
+
/** Deliver one in-order batch; resolve once the daemon durably processed it (i.e. the
|
|
10
|
+
* API server awaited its daemon call before responding). Throw to have the SAME batch
|
|
11
|
+
* retried. Return per-envelope outcomes, or void when everything was accepted. */
|
|
12
|
+
send: (envelopes: MutationEnvelope[]) => Promise<PushOutcome[] | void>;
|
|
13
|
+
/** A policy rejection for one envelope (never retried; the prediction snaps back via the
|
|
14
|
+
* lmid release — this callback is where the reason reaches the app). */
|
|
15
|
+
onRejected?: (envelope: MutationEnvelope, reason: string) => void;
|
|
16
|
+
/** A failed flush attempt, before its retry (observability). */
|
|
17
|
+
onError?: (err: unknown, attempt: number) => void;
|
|
18
|
+
/** Backoff before retry `attempt` (1-based). Default: 200ms · 2^(attempt-1), capped at 5s. */
|
|
19
|
+
retryDelayMs?: (attempt: number) => number;
|
|
20
|
+
/** Max envelopes per flush. Default 32. */
|
|
21
|
+
maxBatch?: number;
|
|
22
|
+
}
|
|
23
|
+
/** A `MutationEnvelopeSender` (the `pushMutation` option of `RemoteOptimisticSource`) that
|
|
24
|
+
* queues envelopes and delivers them as confirmed, in-order batches. */
|
|
25
|
+
export declare function createQueuedMutationSender(opts: QueuedMutationSenderOptions): MutationEnvelopeSender;
|
|
26
|
+
//# sourceMappingURL=mutation-queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutation-queue.d.ts","sourceRoot":"","sources":["../src/mutation-queue.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAE7D,oEAAoE;AACpE,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,2BAA2B;IAC1C;;uFAEmF;IACnF,IAAI,EAAE,CAAC,SAAS,EAAE,gBAAgB,EAAE,KAAK,OAAO,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,CAAC;IACvE;6EACyE;IACzE,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,gBAAgB,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAClE,gEAAgE;IAChE,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAClD,8FAA8F;IAC9F,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IAC3C,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAaD;yEACyE;AACzE,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,2BAA2B,GAAG,sBAAsB,CA2CpG"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// The client-side mutation queue for daemon/serverless deployments. The optimistic engine
|
|
2
|
+
// requires mids to arrive at the daemon CONTIGUOUSLY (`mid == lmid + 1`); a websocket gives
|
|
3
|
+
// that ordering for free, but the serverless hop (client → API server over HTTP → daemon)
|
|
4
|
+
// does not — two parallel fetches can reorder. The queue restores the invariant the cheap
|
|
5
|
+
// way: envelopes are sent as in-order batches, and the next batch does not leave until the
|
|
6
|
+
// prior one is confirmed (the API server's response means the daemon durably processed it).
|
|
7
|
+
//
|
|
8
|
+
// Delivery is at-least-once: a failed flush retries the SAME batch with backoff, which is
|
|
9
|
+
// safe because the daemon absorbs `mid ≤ lmid` as an idempotent replay. A per-envelope
|
|
10
|
+
// REJECTION (the API server's policy saying no) is not retried — the daemon has already
|
|
11
|
+
// advanced lmid past it, the authoritative snap-back rides the lmid release, and the reason
|
|
12
|
+
// surfaces here through `onRejected` (the wire itself carries no rejection signal).
|
|
13
|
+
const defaultDelay = (attempt) => Math.min(200 * 2 ** (attempt - 1), 5_000);
|
|
14
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
15
|
+
/** A `MutationEnvelopeSender` (the `pushMutation` option of `RemoteOptimisticSource`) that
|
|
16
|
+
* queues envelopes and delivers them as confirmed, in-order batches. */
|
|
17
|
+
export function createQueuedMutationSender(opts) {
|
|
18
|
+
const queue = [];
|
|
19
|
+
const maxBatch = Math.max(1, opts.maxBatch ?? 32);
|
|
20
|
+
const delay = opts.retryDelayMs ?? defaultDelay;
|
|
21
|
+
let flushing = false;
|
|
22
|
+
const flush = async () => {
|
|
23
|
+
if (flushing)
|
|
24
|
+
return;
|
|
25
|
+
flushing = true;
|
|
26
|
+
try {
|
|
27
|
+
while (queue.length > 0) {
|
|
28
|
+
const batch = queue.slice(0, maxBatch);
|
|
29
|
+
let outcomes;
|
|
30
|
+
for (let attempt = 1;; attempt++) {
|
|
31
|
+
try {
|
|
32
|
+
outcomes = await opts.send(batch.map((p) => p.envelope));
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
opts.onError?.(err, attempt);
|
|
37
|
+
await sleep(delay(attempt));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
queue.splice(0, batch.length);
|
|
41
|
+
batch.forEach((pending, i) => {
|
|
42
|
+
const outcome = Array.isArray(outcomes) ? outcomes[i] : undefined;
|
|
43
|
+
if (outcome && !outcome.accepted) {
|
|
44
|
+
opts.onRejected?.(pending.envelope, outcome.reason ?? "mutation rejected");
|
|
45
|
+
}
|
|
46
|
+
pending.resolve();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
flushing = false;
|
|
52
|
+
}
|
|
53
|
+
// Envelopes enqueued during the final await: a new flush owns them.
|
|
54
|
+
if (queue.length > 0)
|
|
55
|
+
void flush();
|
|
56
|
+
};
|
|
57
|
+
return (envelope) => new Promise((resolve) => {
|
|
58
|
+
queue.push({ envelope, resolve });
|
|
59
|
+
void flush();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=mutation-queue.js.map
|
|
@@ -0,0 +1 @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