@mdbase-dev/connect 0.1.0-beta.111 → 0.1.0-beta.112

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 CHANGED
@@ -119,6 +119,53 @@ function useMdbaseSession() {
119
119
  }
120
120
  ```
121
121
 
122
+ An application that lets people type into a record opens it as a session
123
+ instead of calling `update()` per change. Every view that opens the same path
124
+ shares one session, so a record never has two competing writers:
125
+
126
+ ```ts
127
+ if (watch.ok) connection.records.follow(watch.value);
128
+
129
+ const opened = await connection.records.open("Notes/today.md", {
130
+ autosave: { idleMs: 1_000 },
131
+ timeoutMs: 8_000
132
+ });
133
+ if (!opened.ok) return renderProblem(opened.problem);
134
+ const { session, release } = opened.value;
135
+
136
+ const unsubscribeRecord = session.subscribe(() => render(session.getSnapshot()));
137
+ editor.onChange((text) => session.setBody(text));
138
+ propertiesPanel.onChange((patch) => session.patchFrontmatter(patch));
139
+
140
+ // Leaving the view: write what is left, then let the session go.
141
+ const flushed = await session.flush();
142
+ if (!flushed.ok && flushed.problem.code === "concurrent_modification") {
143
+ showConflict(session.getSnapshot()); // then session.resolve({ keep: "mine" | "theirs" })
144
+ }
145
+ unsubscribeRecord();
146
+ release();
147
+ ```
148
+
149
+ The session autosaves after the idle interval, serializes its writes, sends
150
+ only the body and frontmatter keys that changed, and checks each write against
151
+ the revision it was based on. It recognizes its own saves after they settle, so
152
+ a watch event or another view that publishes the saved (possibly normalized)
153
+ record first is not mistaken for someone else's edit. A change made elsewhere
154
+ becomes a `conflict` only when it touched something edited locally; otherwise
155
+ the session rebases onto it, and a clean session adopts it. A write whose
156
+ response is lost enters `recovery` and is continued only through its durable
157
+ pending mutation, never sent again, even after a reload: `open()` finds the
158
+ record's interrupted update (by a digest, so its path is never stored) and
159
+ resumes it as `recovery`. `follow()` refreshes open sessions from the
160
+ watch, follows renames, marks deletions (local text is kept), and re-reads
161
+ everything after a change gap. Releasing the last view never cancels a pending
162
+ save. `snapshot.state` is one of `saved`, `unsaved`, `saving`, `conflict`,
163
+ `recovery`, `error` or `deleted`; `snapshot.problem` says why a write failed.
164
+ The session is an external store, so `externalStore(session)` works with
165
+ `useSyncExternalStore`. Applications whose records live behind their own
166
+ repository construct `MdbaseRecordSession` with an `MdbaseRecordSessionAdapter`
167
+ from `/advanced`.
168
+
122
169
  Browser and native callbacks share the same typed completion boundary:
123
170
 
124
171
  ```ts
@@ -7,3 +7,5 @@ export { connectFailure, connectSuccess } from "./outcomes.js";
7
7
  export type { MdbaseApplicationVerificationStore, MdbaseApplicationSessionConnect } from "./application-session.js";
8
8
  export { MdbaseSession } from "./session.js";
9
9
  export type { MdbaseSessionConnect, MdbaseSessionOptions, MdbaseSessionSnapshot } from "./session.js";
10
+ /** Custom record transports, for sessions over an application's own repository or a test double. */
11
+ export type { MdbaseRecordChange, MdbaseRecordSessionAdapter } from "./record-session.js";
package/dist/base64.js CHANGED
@@ -3,11 +3,19 @@ export function randomBase64Url(size) {
3
3
  crypto.getRandomValues(bytes);
4
4
  return bytesToBase64Url(bytes);
5
5
  }
6
+ // Encrypted requests and responses carry whole records, so these run over
7
+ // megabytes: convert in chunks rather than one character at a time.
8
+ const CHUNK = 0x8000;
6
9
  export function bytesToBase64Url(bytes) {
7
10
  let binary = "";
8
- for (const byte of bytes)
9
- binary += String.fromCharCode(byte);
10
- return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
11
+ for (let index = 0; index < bytes.length; index += CHUNK) {
12
+ binary += String.fromCharCode.apply(null, bytes.subarray(index, index + CHUNK));
13
+ }
14
+ const encoded = btoa(binary).replaceAll("+", "-").replaceAll("/", "_");
15
+ let end = encoded.length;
16
+ while (end > 0 && encoded.charCodeAt(end - 1) === 61 /* = */)
17
+ end -= 1;
18
+ return encoded.slice(0, end);
11
19
  }
12
20
  export function base64UrlBytes(value) {
13
21
  const padded = value.replaceAll("-", "+").replaceAll("_", "/")
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "file": "mdbase-connect.min.js",
3
- "integrity": "sha384-/62JQssTDGRQGVQwcO5KLWO7BOqrDVxB0ne307QlEFVO3/MzYMBJfjmbz/OdCRWP"
3
+ "integrity": "sha384-uKL9FxAY8Ji2mQSA44JTIIarA2CnPGN3EFC4jxk1Kx+OfDsPbFr2J+iwlH/p77Qz"
4
4
  }