@brass-build/client 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +72 -63
- package/CHANGELOG.md +86 -0
- package/README.md +317 -231
- package/dist/auth-flow.d.ts +10 -1
- package/dist/auth-flow.d.ts.map +1 -1
- package/dist/auth-flow.js +64 -20
- package/dist/auth-flow.js.map +1 -1
- package/dist/client.d.ts +3 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +217 -75
- package/dist/client.js.map +1 -1
- package/dist/document-engine.d.ts +4 -0
- package/dist/document-engine.d.ts.map +1 -0
- package/dist/document-engine.js +9 -0
- package/dist/document-engine.js.map +1 -0
- package/dist/document-streams.d.ts +8 -1
- package/dist/document-streams.d.ts.map +1 -1
- package/dist/document-streams.js +79 -8
- package/dist/document-streams.js.map +1 -1
- package/dist/documents-namespace.d.ts.map +1 -1
- package/dist/documents-namespace.js +19 -3
- package/dist/documents-namespace.js.map +1 -1
- package/dist/documents.d.ts +43 -14
- package/dist/documents.d.ts.map +1 -1
- package/dist/documents.js +871 -223
- package/dist/documents.js.map +1 -1
- package/dist/embed-protocol.d.ts +3 -0
- package/dist/embed-protocol.d.ts.map +1 -0
- package/dist/embed-protocol.js +26 -0
- package/dist/embed-protocol.js.map +1 -0
- package/dist/emitter.d.ts.map +1 -1
- package/dist/emitter.js +8 -1
- package/dist/emitter.js.map +1 -1
- package/dist/export-guest.d.ts.map +1 -1
- package/dist/export-guest.js +14 -6
- package/dist/export-guest.js.map +1 -1
- package/dist/fetch-deadline.d.ts +22 -0
- package/dist/fetch-deadline.d.ts.map +1 -0
- package/dist/fetch-deadline.js +96 -0
- package/dist/fetch-deadline.js.map +1 -0
- package/dist/fetch-retry.d.ts +1 -0
- package/dist/fetch-retry.d.ts.map +1 -1
- package/dist/fetch-retry.js +12 -5
- package/dist/fetch-retry.js.map +1 -1
- package/dist/handle-types.d.ts +150 -0
- package/dist/handle-types.d.ts.map +1 -0
- package/dist/handle-types.js +8 -0
- package/dist/handle-types.js.map +1 -0
- package/dist/handles.d.ts +48 -48
- package/dist/handles.d.ts.map +1 -1
- package/dist/handles.js +3122 -501
- package/dist/handles.js.map +1 -1
- package/dist/hosted-runtime.d.ts.map +1 -1
- package/dist/hosted-runtime.js +8 -12
- package/dist/hosted-runtime.js.map +1 -1
- package/dist/hosted-ui.d.ts +0 -1
- package/dist/hosted-ui.d.ts.map +1 -1
- package/dist/hosted-ui.js +29 -24
- package/dist/hosted-ui.js.map +1 -1
- package/dist/importer.d.ts.map +1 -1
- package/dist/importer.js +253 -8
- package/dist/importer.js.map +1 -1
- package/dist/index.d.ts +10 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +1 -1
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js.map +1 -1
- package/dist/join-coverage.d.ts +12 -0
- package/dist/join-coverage.d.ts.map +1 -0
- package/dist/join-coverage.js +37 -0
- package/dist/join-coverage.js.map +1 -0
- package/dist/jwt.d.ts.map +1 -1
- package/dist/jwt.js +5 -1
- package/dist/jwt.js.map +1 -1
- package/dist/launch-context.js +2 -2
- package/dist/launch-context.js.map +1 -1
- package/dist/mutate-batching.d.ts +25 -2
- package/dist/mutate-batching.d.ts.map +1 -1
- package/dist/mutate-batching.js +57 -10
- package/dist/mutate-batching.js.map +1 -1
- package/dist/open-document.d.ts +15 -1
- package/dist/open-document.d.ts.map +1 -1
- package/dist/open-document.js +1851 -321
- package/dist/open-document.js.map +1 -1
- package/dist/presence.d.ts +28 -0
- package/dist/presence.d.ts.map +1 -1
- package/dist/presence.js +149 -7
- package/dist/presence.js.map +1 -1
- package/dist/record-paging.d.ts +336 -71
- package/dist/record-paging.d.ts.map +1 -1
- package/dist/record-paging.js +591 -127
- package/dist/record-paging.js.map +1 -1
- package/dist/selection-persistence.d.ts.map +1 -1
- package/dist/selection-persistence.js +11 -1
- package/dist/selection-persistence.js.map +1 -1
- package/dist/serializer.d.ts +30 -1
- package/dist/serializer.d.ts.map +1 -1
- package/dist/session-persistence.d.ts.map +1 -1
- package/dist/session-persistence.js +9 -8
- package/dist/session-persistence.js.map +1 -1
- package/dist/session-toolbar.d.ts +0 -1
- package/dist/session-toolbar.d.ts.map +1 -1
- package/dist/session-toolbar.js +226 -75
- package/dist/session-toolbar.js.map +1 -1
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +87 -18
- package/dist/session.js.map +1 -1
- package/dist/tail-ops.d.ts +14 -4
- package/dist/tail-ops.d.ts.map +1 -1
- package/dist/tail-ops.js +75 -15
- package/dist/tail-ops.js.map +1 -1
- package/dist/topic-lifecycle.d.ts +4 -0
- package/dist/topic-lifecycle.d.ts.map +1 -1
- package/dist/topic-lifecycle.js +25 -1
- package/dist/topic-lifecycle.js.map +1 -1
- package/dist/types.d.ts +47 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/undo.d.ts +2 -3
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +11 -0
- package/dist/undo.js.map +1 -1
- package/dist/unload-guard.d.ts +5 -0
- package/dist/unload-guard.d.ts.map +1 -0
- package/dist/unload-guard.js +88 -0
- package/dist/unload-guard.js.map +1 -0
- package/dist/version-bar.d.ts +4 -3
- package/dist/version-bar.d.ts.map +1 -1
- package/dist/version-bar.js +140 -19
- package/dist/version-bar.js.map +1 -1
- package/dist/version.d.ts +2 -2
- package/dist/version.js +1 -1
- package/dist/vite.d.ts +18 -2
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +133 -8
- package/dist/vite.js.map +1 -1
- package/dist/ws-client.d.ts +23 -3
- package/dist/ws-client.d.ts.map +1 -1
- package/dist/ws-client.js +339 -89
- package/dist/ws-client.js.map +1 -1
- package/dist/ws-frame-limit.d.ts.map +1 -1
- package/dist/ws-frame-limit.js +19 -12
- package/dist/ws-frame-limit.js.map +1 -1
- package/docs/app-manifest.md +7 -8
- package/docs/custom-chrome.md +17 -20
- package/docs/hosted-ui-advanced.md +14 -17
- package/docs/imported-documents.md +53 -197
- package/docs/presence.md +45 -21
- package/docs/schemas.md +12 -17
- package/docs/streams.md +55 -57
- package/package.json +4 -3
- package/src/auth-flow.ts +90 -23
- package/src/client.ts +225 -78
- package/src/document-engine.ts +15 -0
- package/src/document-signals.ts +7 -7
- package/src/document-streams.ts +95 -9
- package/src/documents-namespace.ts +18 -3
- package/src/documents.ts +983 -277
- package/src/embed-protocol.ts +25 -0
- package/src/emitter.ts +8 -1
- package/src/export-guest.ts +15 -9
- package/src/fetch-deadline.ts +119 -0
- package/src/fetch-retry.ts +14 -5
- package/src/handle-types.ts +303 -0
- package/src/handles.ts +3406 -683
- package/src/hosted-runtime.ts +8 -12
- package/src/hosted-ui.ts +28 -32
- package/src/importer.ts +261 -8
- package/src/index.ts +29 -18
- package/src/internal.ts +1 -0
- package/src/join-coverage.ts +42 -0
- package/src/jwt.ts +8 -1
- package/src/launch-context.ts +2 -2
- package/src/mutate-batching.ts +60 -11
- package/src/open-document.ts +2355 -515
- package/src/presence.ts +210 -11
- package/src/record-paging.ts +687 -137
- package/src/selection-persistence.ts +10 -1
- package/src/serializer.ts +33 -1
- package/src/session-persistence.ts +9 -8
- package/src/session-toolbar.ts +249 -80
- package/src/session.ts +91 -17
- package/src/tail-ops.ts +76 -10
- package/src/topic-lifecycle.ts +31 -1
- package/src/types.ts +56 -4
- package/src/undo.ts +13 -5
- package/src/unload-guard.ts +91 -0
- package/src/version-bar.ts +163 -25
- package/src/version.ts +1 -1
- package/src/vite.ts +175 -10
- package/src/ws-client.ts +392 -92
- package/src/ws-frame-limit.ts +19 -12
package/src/version-bar.ts
CHANGED
|
@@ -9,13 +9,23 @@
|
|
|
9
9
|
// grow with no SDK or app change.
|
|
10
10
|
//
|
|
11
11
|
// Bridge protocol (shared envelope `__brass_embed: 1`):
|
|
12
|
-
// host -> iframe: init{surface,params}
|
|
13
|
-
//
|
|
14
|
-
//
|
|
12
|
+
// host -> iframe: init{surface,params}
|
|
13
|
+
// | version-changed{version, requestId?, ok?}
|
|
14
|
+
// iframe -> host: ready | resize{height} | preview{version, requestId?}
|
|
15
|
+
// | return-to-head{requestId?} | fork{fromVersion} | browse
|
|
15
16
|
// | error{code,message}
|
|
17
|
+
//
|
|
18
|
+
// A navigating intent (`preview` / `return-to-head`) is CONFIRMED: the host
|
|
19
|
+
// awaits the document method it routes to and answers with the version the
|
|
20
|
+
// document actually shows afterwards, plus whether the intent was accepted.
|
|
21
|
+
// The bar moves optimistically on the click, so without that answer a refused
|
|
22
|
+
// checkout (a document pinned to a version, a materialise that failed) leaves
|
|
23
|
+
// it naming a version nothing is showing, and nothing later corrects it: the
|
|
24
|
+
// host's own re-render is driven by the session, which does not notify on a
|
|
25
|
+
// preview -> preview step.
|
|
26
|
+
|
|
27
|
+
import { EMBED_ENVELOPE, EMBED_PROTOCOL_VERSION } from './embed-protocol.js';
|
|
16
28
|
|
|
17
|
-
const ENVELOPE = '__brass_embed';
|
|
18
|
-
const PROTOCOL_VERSION = 1;
|
|
19
29
|
// Collapsed until the embed reports `ready`, so the load moment shows a
|
|
20
30
|
// themed placeholder strip rather than an empty white iframe (no flash, no
|
|
21
31
|
// layout jump: the placeholder reserves the strip's height).
|
|
@@ -29,14 +39,17 @@ export interface VersionBarDeps {
|
|
|
29
39
|
dashboardBaseUrl: string;
|
|
30
40
|
// Pins the embed's light/dark theme (matches the other hosted surfaces).
|
|
31
41
|
theme?: 'light' | 'dark' | 'auto';
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
returnToHead: () => void;
|
|
42
|
+
// Intents from the embed, routed to the live active document. The two
|
|
43
|
+
// navigating ones settle (a rejection is a refused intent, which the frame
|
|
44
|
+
// reports back rather than swallowing).
|
|
45
|
+
checkoutVersion: (version: number) => Promise<void>;
|
|
46
|
+
returnToHead: () => Promise<void>;
|
|
38
47
|
fork: (fromVersion: number, name?: string) => void;
|
|
39
48
|
browse: () => void;
|
|
49
|
+
// The version the active document shows right now (`null` at the live
|
|
50
|
+
// head). Read after an intent settles, so the answer describes the
|
|
51
|
+
// document rather than the request.
|
|
52
|
+
currentVersion: () => number | null;
|
|
40
53
|
// Dismiss the bar. The bar stays open through the whole browsing session
|
|
41
54
|
// (including once returned to the live head), so it is closed only by the
|
|
42
55
|
// user's explicit "Close", never as a side effect of navigation.
|
|
@@ -52,6 +65,10 @@ export interface VersionBarContext {
|
|
|
52
65
|
// The active branch's display name when on a non-default branch, else
|
|
53
66
|
// undefined. The bar composes a branch-from-version's initial name from it.
|
|
54
67
|
branchName?: string | undefined;
|
|
68
|
+
// True when the document is pinned to this version and has no live channel
|
|
69
|
+
// (a cold open at a version). Stepping and returning to the head are both
|
|
70
|
+
// refused on that shape, so the bar offers neither.
|
|
71
|
+
pinned?: boolean;
|
|
55
72
|
}
|
|
56
73
|
|
|
57
74
|
export interface VersionBarFrame {
|
|
@@ -67,6 +84,10 @@ function clampHeight(h: number): number {
|
|
|
67
84
|
return Math.max(MIN_HEIGHT, Math.min(MAX_HEIGHT, Math.round(h)));
|
|
68
85
|
}
|
|
69
86
|
|
|
87
|
+
// How long the embed has to say it is ready before the SDK-drawn fallback
|
|
88
|
+
// takes the strip. Matches the hosted-UI launcher's own long window.
|
|
89
|
+
const VERSION_BAR_READY_TIMEOUT_MS = 10_000;
|
|
90
|
+
|
|
70
91
|
export function createVersionBarFrame(
|
|
71
92
|
root: HTMLElement,
|
|
72
93
|
deps: VersionBarDeps,
|
|
@@ -81,6 +102,37 @@ export function createVersionBarFrame(
|
|
|
81
102
|
placeholder.className = 'b-version-loading';
|
|
82
103
|
placeholder.textContent = 'Loading version controls...';
|
|
83
104
|
|
|
105
|
+
// Shown when the embed reports it cannot render (not signed in in this
|
|
106
|
+
// framing context, an account mismatch). The bar is the only surface that
|
|
107
|
+
// returns a document to its live head, so collapsing the strip on that
|
|
108
|
+
// failure would leave the reader in a read-only preview with nothing to
|
|
109
|
+
// leave it by; this carries the two controls that need no timeline.
|
|
110
|
+
const fallback = document.createElement('div');
|
|
111
|
+
fallback.className = 'b-version-fallback';
|
|
112
|
+
fallback.hidden = true;
|
|
113
|
+
const fallbackText = document.createElement('span');
|
|
114
|
+
fallbackText.className = 'b-version-fallback-msg';
|
|
115
|
+
const fallbackHead = document.createElement('button');
|
|
116
|
+
fallbackHead.type = 'button';
|
|
117
|
+
fallbackHead.textContent = 'Return to latest';
|
|
118
|
+
fallbackHead.onclick = () => {
|
|
119
|
+
void deps.returnToHead().catch(() => {});
|
|
120
|
+
};
|
|
121
|
+
const fallbackClose = document.createElement('button');
|
|
122
|
+
fallbackClose.type = 'button';
|
|
123
|
+
fallbackClose.textContent = 'Close';
|
|
124
|
+
fallbackClose.onclick = () => deps.close();
|
|
125
|
+
fallback.append(fallbackText, fallbackHead, fallbackClose);
|
|
126
|
+
|
|
127
|
+
function renderFallback(): void {
|
|
128
|
+
const pinned = ctx?.pinned === true;
|
|
129
|
+
const previewing = deps.currentVersion() !== null;
|
|
130
|
+
fallbackText.textContent = previewing
|
|
131
|
+
? 'Viewing an earlier version'
|
|
132
|
+
: 'Latest version';
|
|
133
|
+
fallbackHead.hidden = !previewing || pinned;
|
|
134
|
+
}
|
|
135
|
+
|
|
84
136
|
const iframe = document.createElement('iframe');
|
|
85
137
|
const url = new URL(`${base}/embed/version-bar`);
|
|
86
138
|
url.searchParams.set('theme', theme);
|
|
@@ -95,10 +147,46 @@ export function createVersionBarFrame(
|
|
|
95
147
|
background: 'transparent',
|
|
96
148
|
} satisfies Partial<CSSStyleDeclaration>);
|
|
97
149
|
|
|
98
|
-
root.append(placeholder, iframe);
|
|
150
|
+
root.append(placeholder, fallback, iframe);
|
|
99
151
|
|
|
100
152
|
let ctx: VersionBarContext | null = null;
|
|
101
153
|
let ready = false;
|
|
154
|
+
// Set once the embed reports it cannot render, so the fallback strip owns
|
|
155
|
+
// the surface and a later `update` does not restore the dead iframe.
|
|
156
|
+
let failed = false;
|
|
157
|
+
let destroyed = false;
|
|
158
|
+
// Armed whenever the iframe is pointed at the embed. Returning a document to
|
|
159
|
+
// its live head is this bar's exclusive capability, so an embed that never
|
|
160
|
+
// loads at all (a host CSP that refuses the frame, an offline reader, a
|
|
161
|
+
// blocking extension) would otherwise leave the placeholder up forever and
|
|
162
|
+
// the reader in a read-only preview with no control to leave it by. The
|
|
163
|
+
// `error` message covers an embed that loaded and refused; nothing covers
|
|
164
|
+
// one that never answers, which is why this is a timer rather than a
|
|
165
|
+
// handler.
|
|
166
|
+
let readyTimer: ReturnType<typeof setTimeout> | undefined;
|
|
167
|
+
|
|
168
|
+
// Hand the strip to the SDK-drawn fallback, which carries the two controls
|
|
169
|
+
// that need no timeline (return to latest, close).
|
|
170
|
+
function enterFallback(): void {
|
|
171
|
+
if (destroyed || failed) return;
|
|
172
|
+
failed = true;
|
|
173
|
+
if (readyTimer !== undefined) {
|
|
174
|
+
clearTimeout(readyTimer);
|
|
175
|
+
readyTimer = undefined;
|
|
176
|
+
}
|
|
177
|
+
placeholder.style.display = 'none';
|
|
178
|
+
iframe.style.height = '0';
|
|
179
|
+
fallback.hidden = false;
|
|
180
|
+
renderFallback();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function armReadyTimer(): void {
|
|
184
|
+
if (readyTimer !== undefined) clearTimeout(readyTimer);
|
|
185
|
+
readyTimer = setTimeout(() => {
|
|
186
|
+
readyTimer = undefined;
|
|
187
|
+
if (!ready) enterFallback();
|
|
188
|
+
}, VERSION_BAR_READY_TIMEOUT_MS);
|
|
189
|
+
}
|
|
102
190
|
// The version last delivered to the embed (via `init` or `version-changed`).
|
|
103
191
|
// The host re-renders on many events (a commit, a saving-count bump, ...),
|
|
104
192
|
// each calling `update`, but only an ACTUAL version change should reach the
|
|
@@ -107,11 +195,10 @@ export function createVersionBarFrame(
|
|
|
107
195
|
let deliveredVersion: number | null | undefined = undefined;
|
|
108
196
|
|
|
109
197
|
function postInit(): void {
|
|
110
|
-
const sid = deps.getSessionToken?.();
|
|
111
198
|
if (!ctx) return;
|
|
112
199
|
iframe.contentWindow?.postMessage(
|
|
113
200
|
{
|
|
114
|
-
[
|
|
201
|
+
[EMBED_ENVELOPE]: EMBED_PROTOCOL_VERSION,
|
|
115
202
|
type: 'init',
|
|
116
203
|
surface: 'version-bar',
|
|
117
204
|
params: {
|
|
@@ -119,7 +206,7 @@ export function createVersionBarFrame(
|
|
|
119
206
|
...(ctx.branch !== undefined ? { branch: ctx.branch } : {}),
|
|
120
207
|
version: ctx.version,
|
|
121
208
|
...(ctx.branchName !== undefined ? { branchName: ctx.branchName } : {}),
|
|
122
|
-
...(
|
|
209
|
+
...(ctx.pinned ? { pinned: true } : {}),
|
|
123
210
|
},
|
|
124
211
|
},
|
|
125
212
|
origin,
|
|
@@ -127,14 +214,50 @@ export function createVersionBarFrame(
|
|
|
127
214
|
deliveredVersion = ctx.version;
|
|
128
215
|
}
|
|
129
216
|
|
|
217
|
+
// Answer a navigating intent with what the document shows now. `ok` is
|
|
218
|
+
// whether the intent itself was accepted, which the version alone cannot
|
|
219
|
+
// say: a checkout the server rounded down and one that was refused both
|
|
220
|
+
// report a version other than the requested one, and the bar keeps the
|
|
221
|
+
// reader's step position for the first and reverts it for the second.
|
|
222
|
+
function confirmIntent(requestId: unknown, ok: boolean): void {
|
|
223
|
+
// An intent settles a turn after it was routed, by which time the bar may
|
|
224
|
+
// have been torn down (the reader closed it, the document changed). There
|
|
225
|
+
// is nothing listening then, and posting into a removed frame is an error
|
|
226
|
+
// that would surface as an unhandled rejection in the app.
|
|
227
|
+
if (!ready || destroyed) return;
|
|
228
|
+
const version = deps.currentVersion();
|
|
229
|
+
deliveredVersion = version;
|
|
230
|
+
iframe.contentWindow?.postMessage(
|
|
231
|
+
{
|
|
232
|
+
[EMBED_ENVELOPE]: EMBED_PROTOCOL_VERSION,
|
|
233
|
+
type: 'version-changed',
|
|
234
|
+
version,
|
|
235
|
+
ok,
|
|
236
|
+
...(typeof requestId === 'string' ? { requestId } : {}),
|
|
237
|
+
},
|
|
238
|
+
origin,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function runIntent(requestId: unknown, intent: () => Promise<void>): void {
|
|
243
|
+
void intent().then(
|
|
244
|
+
() => confirmIntent(requestId, true),
|
|
245
|
+
() => confirmIntent(requestId, false),
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
130
249
|
function onMessage(event: MessageEvent): void {
|
|
131
250
|
if (event.origin !== origin) return;
|
|
132
251
|
if (event.source !== iframe.contentWindow) return;
|
|
133
252
|
const data = event.data as Record<string, unknown> | null;
|
|
134
|
-
if (!data || data[
|
|
253
|
+
if (!data || data[EMBED_ENVELOPE] !== EMBED_PROTOCOL_VERSION) return;
|
|
135
254
|
switch (data['type']) {
|
|
136
255
|
case 'ready': {
|
|
137
256
|
ready = true;
|
|
257
|
+
if (readyTimer !== undefined) {
|
|
258
|
+
clearTimeout(readyTimer);
|
|
259
|
+
readyTimer = undefined;
|
|
260
|
+
}
|
|
138
261
|
placeholder.style.display = 'none';
|
|
139
262
|
iframe.style.height = `${READY_HEIGHT}px`;
|
|
140
263
|
postInit();
|
|
@@ -148,12 +271,12 @@ export function createVersionBarFrame(
|
|
|
148
271
|
case 'preview': {
|
|
149
272
|
const v = data['version'];
|
|
150
273
|
if (typeof v === 'number' && Number.isInteger(v) && v >= 0) {
|
|
151
|
-
deps.checkoutVersion(v);
|
|
274
|
+
runIntent(data['requestId'], () => deps.checkoutVersion(v));
|
|
152
275
|
}
|
|
153
276
|
break;
|
|
154
277
|
}
|
|
155
278
|
case 'return-to-head': {
|
|
156
|
-
deps.returnToHead();
|
|
279
|
+
runIntent(data['requestId'], () => deps.returnToHead());
|
|
157
280
|
break;
|
|
158
281
|
}
|
|
159
282
|
case 'fork': {
|
|
@@ -173,16 +296,17 @@ export function createVersionBarFrame(
|
|
|
173
296
|
break;
|
|
174
297
|
}
|
|
175
298
|
case 'error': {
|
|
176
|
-
// The embed
|
|
177
|
-
//
|
|
178
|
-
|
|
179
|
-
placeholder.style.display = 'none';
|
|
180
|
-
iframe.style.height = '0';
|
|
299
|
+
// The embed loaded and reported it cannot render (not signed in in
|
|
300
|
+
// this context, an account mismatch).
|
|
301
|
+
enterFallback();
|
|
181
302
|
break;
|
|
182
303
|
}
|
|
183
304
|
}
|
|
184
305
|
}
|
|
185
306
|
|
|
307
|
+
// Armed here rather than beside the `src` assignment above, which runs
|
|
308
|
+
// before `readyTimer` is initialised.
|
|
309
|
+
armReadyTimer();
|
|
186
310
|
window.addEventListener('message', onMessage);
|
|
187
311
|
|
|
188
312
|
return {
|
|
@@ -194,12 +318,20 @@ export function createVersionBarFrame(
|
|
|
194
318
|
// just pushes the new version down.
|
|
195
319
|
if (prev && (prev.docId !== next.docId || prev.branch !== next.branch)) {
|
|
196
320
|
// Reload the iframe: reset ready, restore the placeholder, and the
|
|
197
|
-
// new page will `ready` -> we re-`init` with the new context.
|
|
321
|
+
// new page will `ready` -> we re-`init` with the new context. A fresh
|
|
322
|
+
// page gets a fresh verdict, so the failed state resets with it.
|
|
198
323
|
ready = false;
|
|
324
|
+
failed = false;
|
|
199
325
|
deliveredVersion = undefined;
|
|
326
|
+
fallback.hidden = true;
|
|
200
327
|
placeholder.style.display = '';
|
|
201
328
|
iframe.style.height = '0';
|
|
202
329
|
iframe.src = url.toString();
|
|
330
|
+
armReadyTimer();
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (failed) {
|
|
334
|
+
renderFallback();
|
|
203
335
|
return;
|
|
204
336
|
}
|
|
205
337
|
// Only an actual version change reaches the embed (dedup), so a
|
|
@@ -207,15 +339,21 @@ export function createVersionBarFrame(
|
|
|
207
339
|
if (ready && next.version !== deliveredVersion) {
|
|
208
340
|
deliveredVersion = next.version;
|
|
209
341
|
iframe.contentWindow?.postMessage(
|
|
210
|
-
{ [
|
|
342
|
+
{ [EMBED_ENVELOPE]: EMBED_PROTOCOL_VERSION, type: 'version-changed', version: next.version },
|
|
211
343
|
origin,
|
|
212
344
|
);
|
|
213
345
|
}
|
|
214
346
|
},
|
|
215
347
|
destroy(): void {
|
|
348
|
+
destroyed = true;
|
|
349
|
+
if (readyTimer !== undefined) {
|
|
350
|
+
clearTimeout(readyTimer);
|
|
351
|
+
readyTimer = undefined;
|
|
352
|
+
}
|
|
216
353
|
window.removeEventListener('message', onMessage);
|
|
217
354
|
iframe.remove();
|
|
218
355
|
placeholder.remove();
|
|
356
|
+
fallback.remove();
|
|
219
357
|
},
|
|
220
358
|
};
|
|
221
359
|
}
|
package/src/version.ts
CHANGED
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
// can measure the version distribution of deployed apps and time
|
|
4
4
|
// compatibility windows against it. A unit test pins this to
|
|
5
5
|
// package.json's version; the release bump touches both together.
|
|
6
|
-
export const SDK_VERSION = '0.
|
|
6
|
+
export const SDK_VERSION = '0.2.0';
|
|
7
7
|
|
|
8
8
|
export const SDK_CLIENT_ID = `sdk/${SDK_VERSION}`;
|
package/src/vite.ts
CHANGED
|
@@ -8,37 +8,202 @@
|
|
|
8
8
|
// plugin excludes the dep from prebundling (leaving it for the build-time
|
|
9
9
|
// WASM loaders) and bumps the prebundle target to es2022.
|
|
10
10
|
//
|
|
11
|
-
// `vite-plugin-wasm`
|
|
12
|
-
//
|
|
11
|
+
// `vite-plugin-wasm` remains the caller's responsibility: it must land before
|
|
12
|
+
// `brassVite()` in the plugin list.
|
|
13
13
|
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
// installed during a migration window needs both
|
|
17
|
-
|
|
14
|
+
// Every WASM-shipping dep the SDK pulls in, and the entry that loads it
|
|
15
|
+
// asynchronously. Add an entry rather than replacing the list: a consumer who
|
|
16
|
+
// has two installed during a migration window needs both handled, and this is
|
|
17
|
+
// the one place any of them is named, so an app's own config never is.
|
|
18
|
+
//
|
|
19
|
+
// A default browser entry fetches its module with a SYNCHRONOUS
|
|
20
|
+
// XMLHttpRequest while it evaluates, so an app freezes its main thread for the
|
|
21
|
+
// whole download before it paints, whether or not anyone opens a document, and
|
|
22
|
+
// the freeze grows with the reader's latency rather than with the app. An
|
|
23
|
+
// `asyncEntry` hands the same module to the bundler's own asset pipeline,
|
|
24
|
+
// which loads it asynchronously and leaves the main thread free. Selecting one
|
|
25
|
+
// emits a top-level await, so it is reachable only on a build target that
|
|
26
|
+
// accepts one, and the synchronous entry is what keeps every other target
|
|
27
|
+
// building.
|
|
28
|
+
const WASM_DEPS: readonly { specifier: string; asyncEntry: string }[] = [
|
|
29
|
+
{ specifier: 'loro-crdt', asyncEntry: 'loro-crdt/bundler' },
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
// A specifier matched whole. A string `find` matches by prefix, which would
|
|
33
|
+
// also catch the async entry the alias points AT and resolve it to itself.
|
|
34
|
+
const wholeSpecifier = (specifier: string): RegExp =>
|
|
35
|
+
new RegExp(`^${specifier.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}$`);
|
|
36
|
+
|
|
37
|
+
// The shape this plugin reads and returns, declared here rather than imported
|
|
38
|
+
// so the SDK does not depend on Vite to describe its own plugin.
|
|
39
|
+
//
|
|
40
|
+
// Each field has to be at least as WIDE as Vite's own, because Vite passes its
|
|
41
|
+
// `UserConfig` into the `config` hook and a hook declared over a narrower
|
|
42
|
+
// config is not assignable to Vite's `Plugin`. `target` is the one that bites:
|
|
43
|
+
// esbuild types it `string | string[]`, so declaring it `string` makes
|
|
44
|
+
// `plugins: [brassVite()]` fail to type-check in a consumer's own
|
|
45
|
+
// `vite.config.ts` (verified against Vite 5, 6 and 7; Vite 8 narrowed its own
|
|
46
|
+
// type and hid it). The plugin only ever passes the value through, so the
|
|
47
|
+
// wider type costs nothing here.
|
|
48
|
+
interface ViteAliasEntry {
|
|
49
|
+
find: string | RegExp;
|
|
50
|
+
replacement: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface ViteBuildOptions {
|
|
54
|
+
target?: string | string[] | false;
|
|
55
|
+
}
|
|
18
56
|
|
|
19
57
|
interface ViteUserConfig {
|
|
58
|
+
build?: ViteBuildOptions;
|
|
59
|
+
// Vite 8 lets a consumer set the browser build's target per environment, and
|
|
60
|
+
// that value, not the top-level one, is what the client bundle is built to.
|
|
61
|
+
environments?: { client?: { build?: ViteBuildOptions } };
|
|
62
|
+
resolve?: {
|
|
63
|
+
alias?: Record<string, string> | readonly ViteAliasEntry[];
|
|
64
|
+
};
|
|
20
65
|
optimizeDeps?: {
|
|
21
66
|
exclude?: string[];
|
|
22
|
-
esbuildOptions?: { target?: string };
|
|
67
|
+
esbuildOptions?: { target?: string | string[] };
|
|
23
68
|
};
|
|
24
69
|
}
|
|
25
70
|
|
|
26
71
|
interface VitePlugin {
|
|
27
72
|
name: string;
|
|
28
|
-
config(config: ViteUserConfig): ViteUserConfig
|
|
73
|
+
config(config: ViteUserConfig): Promise<ViteUserConfig>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Vite 8 prebundles with rolldown, which handles top-level await on its own
|
|
77
|
+
// and deprecates `optimizeDeps.esbuildOptions`. Setting it there does nothing
|
|
78
|
+
// except print a deprecation warning naming this plugin, on every `vite dev`
|
|
79
|
+
// and every `vite build` a consumer runs. Read from the running Vite rather
|
|
80
|
+
// than from the consumer's manifest, and treat an unreadable version as an
|
|
81
|
+
// older Vite, where the target bump is what makes the WASM dep load at all.
|
|
82
|
+
//
|
|
83
|
+
// The specifier is held in a variable so `tsc` does not resolve it. Vite is
|
|
84
|
+
// neither a dependency nor a peer dependency here (the config shapes above are
|
|
85
|
+
// declared for the same reason), so a literal specifier fails the build
|
|
86
|
+
// wherever Vite is absent, which includes `npm install` in this package
|
|
87
|
+
// itself. At runtime the import reaches the Vite running the build.
|
|
88
|
+
const VITE_PACKAGE = 'vite';
|
|
89
|
+
|
|
90
|
+
async function viteMajor(): Promise<number | null> {
|
|
91
|
+
try {
|
|
92
|
+
const { version } = (await import(VITE_PACKAGE)) as { version?: string };
|
|
93
|
+
const major = Number.parseInt(version ?? '', 10);
|
|
94
|
+
return Number.isFinite(major) ? major : null;
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Browser versions that first shipped top-level await, by the engine names
|
|
101
|
+
// esbuild accepts in a target string. A target naming an engine absent here is
|
|
102
|
+
// read as unsupported.
|
|
103
|
+
const TOP_LEVEL_AWAIT_SINCE: Readonly<Record<string, number>> = {
|
|
104
|
+
chrome: 89,
|
|
105
|
+
edge: 89,
|
|
106
|
+
firefox: 89,
|
|
107
|
+
safari: 15,
|
|
108
|
+
ios: 15,
|
|
109
|
+
opera: 75,
|
|
110
|
+
node: 14.8,
|
|
111
|
+
es: 2022,
|
|
112
|
+
hermes: Number.POSITIVE_INFINITY,
|
|
113
|
+
ie: Number.POSITIVE_INFINITY,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// Vite's own named targets. `modules` (the Vite 5 / 6 default) resolves to
|
|
117
|
+
// browsers predating top-level await; `baseline-widely-available` (the Vite 7 /
|
|
118
|
+
// 8 default) resolves to ones that carry it.
|
|
119
|
+
const NAMED_TARGET_SUPPORT: Readonly<Record<string, boolean>> = {
|
|
120
|
+
esnext: true,
|
|
121
|
+
'baseline-widely-available': true,
|
|
122
|
+
modules: false,
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
function targetSupportsTopLevelAwait(target: string): boolean {
|
|
126
|
+
const t = target.trim().toLowerCase();
|
|
127
|
+
const named = NAMED_TARGET_SUPPORT[t];
|
|
128
|
+
if (named !== undefined) return named;
|
|
129
|
+
const parsed = /^([a-z]+)([\d.]+)$/.exec(t);
|
|
130
|
+
if (!parsed) return false;
|
|
131
|
+
const since = TOP_LEVEL_AWAIT_SINCE[parsed[1] as string];
|
|
132
|
+
if (since === undefined) return false;
|
|
133
|
+
const version = Number.parseFloat(parsed[2] as string);
|
|
134
|
+
return Number.isFinite(version) && version >= since;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Whether the build this plugin is configuring can carry a top-level await.
|
|
138
|
+
// An unreadable or unrecognised target answers `false`, so the worst a wrong
|
|
139
|
+
// reading costs is the synchronous entry a consumer already has.
|
|
140
|
+
function buildAcceptsTopLevelAwait(
|
|
141
|
+
config: ViteUserConfig,
|
|
142
|
+
major: number | null,
|
|
143
|
+
): boolean {
|
|
144
|
+
const target = config.environments?.client?.build?.target ?? config.build?.target;
|
|
145
|
+
// No explicit target: the answer is Vite's own default, which carries
|
|
146
|
+
// top-level await from Vite 7 (`baseline-widely-available`) and not before
|
|
147
|
+
// (`modules`).
|
|
148
|
+
if (target === undefined) return major !== null && major >= 7;
|
|
149
|
+
if (target === false) return false;
|
|
150
|
+
const targets = Array.isArray(target) ? target : [target];
|
|
151
|
+
return targets.length > 0 && targets.every(targetSupportsTopLevelAwait);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// A consumer who already resolves the WASM dep themselves owns that decision,
|
|
155
|
+
// so the plugin adds nothing that would take it over.
|
|
156
|
+
function aliasesWasmDep(config: ViteUserConfig): boolean {
|
|
157
|
+
const alias = config.resolve?.alias;
|
|
158
|
+
if (!alias) return false;
|
|
159
|
+
const specifiers = WASM_DEPS.map((dep) => dep.specifier);
|
|
160
|
+
if (Array.isArray(alias)) {
|
|
161
|
+
return alias.some((entry) =>
|
|
162
|
+
specifiers.some((specifier) =>
|
|
163
|
+
typeof entry.find === 'string'
|
|
164
|
+
? entry.find === specifier
|
|
165
|
+
: entry.find.test(specifier),
|
|
166
|
+
),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
return Object.keys(alias as Record<string, string>).some((key) =>
|
|
170
|
+
specifiers.includes(key),
|
|
171
|
+
);
|
|
29
172
|
}
|
|
30
173
|
|
|
31
174
|
export function brassVite(): VitePlugin {
|
|
32
175
|
return {
|
|
33
176
|
name: 'brass-build:vite',
|
|
34
|
-
config(config) {
|
|
177
|
+
async config(config) {
|
|
178
|
+
const major = await viteMajor();
|
|
179
|
+
const useAsyncEntry = buildAcceptsTopLevelAwait(config, major) && !aliasesWasmDep(config);
|
|
35
180
|
const existingExclude = config.optimizeDeps?.exclude ?? [];
|
|
36
|
-
const exclude = Array.from(
|
|
181
|
+
const exclude = Array.from(
|
|
182
|
+
new Set([
|
|
183
|
+
...existingExclude,
|
|
184
|
+
...WASM_DEPS.map((dep) => dep.specifier),
|
|
185
|
+
...(useAsyncEntry ? WASM_DEPS.map((dep) => dep.asyncEntry) : []),
|
|
186
|
+
]),
|
|
187
|
+
);
|
|
188
|
+
const resolve = useAsyncEntry
|
|
189
|
+
? {
|
|
190
|
+
resolve: {
|
|
191
|
+
alias: WASM_DEPS.map((dep) => ({
|
|
192
|
+
find: wholeSpecifier(dep.specifier),
|
|
193
|
+
replacement: dep.asyncEntry,
|
|
194
|
+
})) as ViteAliasEntry[],
|
|
195
|
+
},
|
|
196
|
+
}
|
|
197
|
+
: {};
|
|
198
|
+
if (major !== null && major >= 8) {
|
|
199
|
+
return { ...resolve, optimizeDeps: { exclude } };
|
|
200
|
+
}
|
|
37
201
|
const existingTarget = config.optimizeDeps?.esbuildOptions?.target;
|
|
38
202
|
// Don't downgrade if a consumer has already pinned a newer
|
|
39
203
|
// target; only raise from the Vite default (which lacks TLA).
|
|
40
204
|
const target = existingTarget ?? 'es2022';
|
|
41
205
|
return {
|
|
206
|
+
...resolve,
|
|
42
207
|
optimizeDeps: {
|
|
43
208
|
exclude,
|
|
44
209
|
esbuildOptions: { target },
|