@mostra/react 0.6.2 → 0.7.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 CHANGED
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import { g as fieldKey, o as useStore } from "./hooks-Dh_l8KqJ.js";
2
+ import { g as fieldKey, o as useStore } from "./hooks-BUQOrPQg.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 result = await this.#request(`${this.#base}/events/poll?${query()}&wait=25`, { method: "GET" });
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) {
353
370
  poll();
354
371
  return;
355
372
  }
356
- source = new EventSource(`${this.#base}/events?${query()}`);
373
+ let ticket = "";
374
+ try {
375
+ ticket = await this.#ticketQuery();
376
+ } catch {
377
+ poll();
378
+ return;
379
+ }
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
  };
@@ -2317,7 +2362,9 @@ function connectShell() {
2317
2362
  session: {
2318
2363
  csrf: typeof session.csrf === "string" ? session.csrf : "",
2319
2364
  scopes: Array.isArray(session.scopes) ? session.scopes.filter((scope) => typeof scope === "string") : [],
2320
- ...isViewer(session.viewer) ? { viewer: session.viewer } : {}
2365
+ ...isViewer(session.viewer) ? { viewer: session.viewer } : {},
2366
+ mode: session.mode === "worker" ? "worker" : "cookie",
2367
+ streams: session.streams !== false
2321
2368
  }
2322
2369
  };
2323
2370
  })().then((connection) => {
@@ -2734,4 +2781,4 @@ function useArtifact() {
2734
2781
  //#endregion
2735
2782
  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
2783
 
2737
- //# sourceMappingURL=hooks-Dh_l8KqJ.js.map
2784
+ //# sourceMappingURL=hooks-BUQOrPQg.js.map