@mostra/react 0.6.2 → 0.8.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/dist/form.js +1 -1
- package/dist/{hooks-Dh_l8KqJ.js → hooks-BHhOYg3U.js} +65 -7
- package/dist/hooks-BHhOYg3U.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/{mount-Bpg0M5ln.js → mount-DSuEcMdp.js} +55 -3
- package/dist/mount-DSuEcMdp.js.map +1 -0
- package/dist/router.js +1 -1
- package/package.json +2 -2
- package/src/runtime/boot.ts +7 -0
- package/src/runtime/server-client.ts +12 -1
- package/src/runtime/service-worker.ts +55 -0
- package/src/runtime/transport.ts +73 -8
- package/src/shell-bridge.ts +8 -0
- package/dist/hooks-Dh_l8KqJ.js.map +0 -1
- package/dist/mount-Bpg0M5ln.js.map +0 -1
package/dist/form.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { g as fieldKey, o as useStore } from "./hooks-
|
|
2
|
+
import { g as fieldKey, o as useStore } from "./hooks-BHhOYg3U.js";
|
|
3
3
|
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
4
4
|
import { get, set, useForm as useForm$1 } from "react-hook-form";
|
|
5
5
|
//#region src/form.ts
|
|
@@ -166,6 +166,12 @@ var NetworkError = class extends Error {
|
|
|
166
166
|
/** After this many stream failures in a row, the client long-polls instead (FAILURE_MODEL §8). */
|
|
167
167
|
const STREAM_FAILURES_BEFORE_POLL = 2;
|
|
168
168
|
/**
|
|
169
|
+
* How long a stream may stay silent after it opens before the page long-polls instead: the
|
|
170
|
+
* gateway's first message (`ready`) comes at once, so silence means something holds the stream
|
|
171
|
+
* back (a buffering proxy or tunnel, or a browser that stalled it, WP-13).
|
|
172
|
+
*/
|
|
173
|
+
const STREAM_FIRST_MESSAGE_MS = 6e3;
|
|
174
|
+
/**
|
|
169
175
|
* The browser protocol over HTTP, on the artifact's origin. The CSRF token comes from
|
|
170
176
|
* `/_mostra/session`, once; a refusal is a {@link MostraError} with the gateway's diagnostic, and
|
|
171
177
|
* an unreachable gateway a {@link NetworkError}.
|
|
@@ -173,6 +179,8 @@ const STREAM_FAILURES_BEFORE_POLL = 2;
|
|
|
173
179
|
var HttpTransport = class {
|
|
174
180
|
#viewId;
|
|
175
181
|
#base;
|
|
182
|
+
#tickets;
|
|
183
|
+
#streams;
|
|
176
184
|
#csrf;
|
|
177
185
|
/**
|
|
178
186
|
* `csrf` is the session's token when the caller already read it (the bridge does): a save made
|
|
@@ -181,8 +189,16 @@ var HttpTransport = class {
|
|
|
181
189
|
constructor(viewId, options = {}) {
|
|
182
190
|
this.#viewId = viewId;
|
|
183
191
|
this.#base = options.base ?? `/_mostra/v/${BROWSER_PROTOCOL_REVISION}`;
|
|
192
|
+
this.#tickets = options.tickets === true;
|
|
193
|
+
this.#streams = options.streams !== false;
|
|
184
194
|
if (options.csrf) this.#csrf = Promise.resolve(options.csrf);
|
|
185
195
|
}
|
|
196
|
+
/** `&ticket=…` for a stream or poll in worker mode, else nothing. */
|
|
197
|
+
async #ticketQuery() {
|
|
198
|
+
if (!this.#tickets) return "";
|
|
199
|
+
const { ticket } = await this.#post("/ticket", { purpose: "stream" });
|
|
200
|
+
return `&ticket=${encodeURIComponent(ticket)}`;
|
|
201
|
+
}
|
|
186
202
|
#token() {
|
|
187
203
|
this.#csrf ??= (async () => {
|
|
188
204
|
return (await this.#request("/_mostra/session", { method: "GET" })).csrf;
|
|
@@ -328,7 +344,8 @@ var HttpTransport = class {
|
|
|
328
344
|
polling = true;
|
|
329
345
|
let backoff = 1e3;
|
|
330
346
|
while (!closed) try {
|
|
331
|
-
const
|
|
347
|
+
const ticket = await this.#ticketQuery();
|
|
348
|
+
const result = await this.#request(`${this.#base}/events/poll?${query()}&wait=25${ticket}`, { method: "GET" });
|
|
332
349
|
backoff = 1e3;
|
|
333
350
|
handlers.onStatus("live");
|
|
334
351
|
if (result.snapshot) handlers.onSnapshot(result.snapshot);
|
|
@@ -347,13 +364,37 @@ var HttpTransport = class {
|
|
|
347
364
|
backoff = Math.min(backoff * 2, 3e4);
|
|
348
365
|
}
|
|
349
366
|
};
|
|
350
|
-
const open = () => {
|
|
367
|
+
const open = async () => {
|
|
351
368
|
if (closed) return;
|
|
352
|
-
if (typeof EventSource === "undefined") {
|
|
369
|
+
if (typeof EventSource === "undefined" || !this.#streams) {
|
|
370
|
+
poll();
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
let ticket = "";
|
|
374
|
+
try {
|
|
375
|
+
ticket = await this.#ticketQuery();
|
|
376
|
+
} catch {
|
|
353
377
|
poll();
|
|
354
378
|
return;
|
|
355
379
|
}
|
|
356
|
-
|
|
380
|
+
if (closed) return;
|
|
381
|
+
const opened = new EventSource(`${this.#base}/events?${query()}${ticket}`);
|
|
382
|
+
source = opened;
|
|
383
|
+
let heard = false;
|
|
384
|
+
const silent = setTimeout(() => {
|
|
385
|
+
if (heard || closed || source !== opened) return;
|
|
386
|
+
opened.close();
|
|
387
|
+
source = void 0;
|
|
388
|
+
handlers.onStatus("offline");
|
|
389
|
+
if (!polling) poll();
|
|
390
|
+
}, STREAM_FIRST_MESSAGE_MS);
|
|
391
|
+
const hear = () => {
|
|
392
|
+
heard = true;
|
|
393
|
+
clearTimeout(silent);
|
|
394
|
+
};
|
|
395
|
+
opened.addEventListener("ready", hear);
|
|
396
|
+
opened.addEventListener("snapshot", hear);
|
|
397
|
+
opened.addEventListener("mostra", hear);
|
|
357
398
|
source.addEventListener("open", () => {
|
|
358
399
|
failures = 0;
|
|
359
400
|
handlers.onStatus("live");
|
|
@@ -392,6 +433,10 @@ var HttpTransport = class {
|
|
|
392
433
|
source?.close();
|
|
393
434
|
source = void 0;
|
|
394
435
|
if (!polling) poll();
|
|
436
|
+
} else if (this.#tickets) {
|
|
437
|
+
source?.close();
|
|
438
|
+
source = void 0;
|
|
439
|
+
retry = setTimeout(() => void open(), 1e3);
|
|
395
440
|
}
|
|
396
441
|
});
|
|
397
442
|
};
|
|
@@ -887,9 +932,10 @@ var ServerClient = class {
|
|
|
887
932
|
...state.at !== void 0 ? { at: state.at } : {}
|
|
888
933
|
};
|
|
889
934
|
}
|
|
935
|
+
const settled = tracked.newest > 0 || state.error !== void 0 || state.awaiting !== true;
|
|
890
936
|
tracked.view = {
|
|
891
937
|
...tracked.view,
|
|
892
|
-
loading:
|
|
938
|
+
loading: !settled,
|
|
893
939
|
...state.error ? { error: state.error } : {}
|
|
894
940
|
};
|
|
895
941
|
if (!state.error && tracked.view.error) {
|
|
@@ -899,6 +945,16 @@ var ServerClient = class {
|
|
|
899
945
|
this.#changed();
|
|
900
946
|
}
|
|
901
947
|
async #applyLiveUpdateNow(tracked, update) {
|
|
948
|
+
if (update.awaiting === false && !update.value && !update.error) {
|
|
949
|
+
if (tracked.newest === 0 && tracked.view.loading) {
|
|
950
|
+
tracked.view = {
|
|
951
|
+
...tracked.view,
|
|
952
|
+
loading: false
|
|
953
|
+
};
|
|
954
|
+
this.#changed();
|
|
955
|
+
}
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
902
958
|
if (update.error && !update.value) {
|
|
903
959
|
tracked.view = {
|
|
904
960
|
...tracked.view,
|
|
@@ -2317,7 +2373,9 @@ function connectShell() {
|
|
|
2317
2373
|
session: {
|
|
2318
2374
|
csrf: typeof session.csrf === "string" ? session.csrf : "",
|
|
2319
2375
|
scopes: Array.isArray(session.scopes) ? session.scopes.filter((scope) => typeof scope === "string") : [],
|
|
2320
|
-
...isViewer(session.viewer) ? { viewer: session.viewer } : {}
|
|
2376
|
+
...isViewer(session.viewer) ? { viewer: session.viewer } : {},
|
|
2377
|
+
mode: session.mode === "worker" ? "worker" : "cookie",
|
|
2378
|
+
streams: session.streams !== false
|
|
2321
2379
|
}
|
|
2322
2380
|
};
|
|
2323
2381
|
})().then((connection) => {
|
|
@@ -2734,4 +2792,4 @@ function useArtifact() {
|
|
|
2734
2792
|
//#endregion
|
|
2735
2793
|
export { serverFunctionId as _, useSignal as a, MemoryJournal as b, useSubmitControl as c, connectShell as d, listenShell as f, fieldKey as g, Store as h, useField as i, useViewer as l, reportReady as m, useArtifact as n, useStore as o, openLink as p, useChoice as r, useSubmit as s, RuntimeProvider as t, builtVersion as u, HttpTransport as v, MostraError as x, IndexedDbJournal as y };
|
|
2736
2794
|
|
|
2737
|
-
//# sourceMappingURL=hooks-
|
|
2795
|
+
//# sourceMappingURL=hooks-BHhOYg3U.js.map
|