@cfasim-ui/docs 0.6.4 → 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.
@@ -4,26 +4,7 @@ import {
4
4
  postErrorWithTransfer,
5
5
  } from "@cfasim-ui/shared/transfer";
6
6
  import type { ColumnDescriptor, ModelOutputsWire } from "@cfasim-ui/shared";
7
-
8
- interface RunMessage {
9
- id: number;
10
- type?: "run";
11
- python: string;
12
- context?: Record<string, unknown>;
13
- }
14
- interface CallMessage {
15
- id: number;
16
- type: "call";
17
- module: string;
18
- fn: string;
19
- kwargs?: Record<string, unknown>;
20
- }
21
- interface LoadModuleMessage {
22
- id: number;
23
- type: "loadModule";
24
- module: string;
25
- }
26
- type WorkerMessage = RunMessage | CallMessage | LoadModuleMessage;
7
+ import type { WorkerMessage } from "./messages.js";
27
8
 
28
9
  let wheelMap: Record<string, string> = {};
29
10
 
@@ -63,8 +44,8 @@ async function fetchPyodidePackages(): Promise<string[]> {
63
44
 
64
45
  const pyodideReadyPromise = (async () => {
65
46
  const [pyodideModule, packages] = await Promise.all([
66
- // @ts-expect-error - Pyodide types from CDN
67
47
  import(
48
+ // @ts-expect-error - Pyodide types from CDN
68
49
  /* @vite-ignore */ "https://cdn.jsdelivr.net/pyodide/v0.29.3/full/pyodide.mjs"
69
50
  ),
70
51
  fetchPyodidePackages(),
@@ -1,53 +1,18 @@
1
1
  import type { TransferableResponse } from "@cfasim-ui/shared";
2
- import { unwrapResponse } from "@cfasim-ui/shared";
2
+ import {
3
+ unwrapResponse,
4
+ describeWorkerError,
5
+ MESSAGEERROR_TEXT,
6
+ } from "@cfasim-ui/shared";
7
+ import type { WorkerMessage, OutgoingMessage } from "./messages.js";
3
8
 
4
9
  let lastId = 0;
5
10
  function getId(): number {
6
11
  return ++lastId;
7
12
  }
8
13
 
9
- interface RunMessage {
10
- id: number;
11
- type?: "run";
12
- python: string;
13
- context?: Record<string, unknown>;
14
- }
15
- interface CallMessage {
16
- id: number;
17
- type: "call";
18
- module: string;
19
- fn: string;
20
- kwargs?: Record<string, unknown>;
21
- }
22
- interface LoadModuleMessage {
23
- id: number;
24
- type: "loadModule";
25
- module: string;
26
- }
27
- type WorkerMessage = RunMessage | CallMessage | LoadModuleMessage;
28
- type OutgoingMessage =
29
- | Omit<RunMessage, "id">
30
- | Omit<CallMessage, "id">
31
- | Omit<LoadModuleMessage, "id">;
32
-
33
14
  export type PyodideResponse = { result?: unknown; error?: string };
34
15
 
35
- /**
36
- * Convert a worker `ErrorEvent` into a human-readable message. Cross-origin
37
- * worker failures sanitize the event so every field is null/empty (e.g. a
38
- * dynamic `import("https://cdn…")` that fails at module evaluation): in
39
- * that case we fall back to a hint about likely causes instead of an
40
- * empty string, which would otherwise surface as a blank error.
41
- */
42
- function describeWorkerError(e: ErrorEvent): string {
43
- const msg = e.message || e.error?.message || "";
44
- if (msg) {
45
- if (e.filename) return `${msg} (${e.filename}:${e.lineno})`;
46
- return msg;
47
- }
48
- return "Worker failed to load (no error details available — most often a cross-origin script load failure or a syntax error in the worker module)";
49
- }
50
-
51
16
  /**
52
17
  * Worker names are arbitrary string keys. Workers are spawned lazily on first
53
18
  * use; each one is an independent Pyodide interpreter. Use distinct names when
@@ -96,9 +61,7 @@ function getWorker(name: string): WorkerEntry {
96
61
  failAll(describeWorkerError(e));
97
62
  });
98
63
  worker.addEventListener("messageerror", () => {
99
- failAll(
100
- "Worker received a message it could not deserialize (messageerror)",
101
- );
64
+ failAll(MESSAGEERROR_TEXT);
102
65
  });
103
66
  entry = newEntry;
104
67
  workers.set(name, entry);
package/shared/index.ts CHANGED
@@ -14,6 +14,7 @@ export type {
14
14
  ModelOutputsWire,
15
15
  } from "./ModelOutput.js";
16
16
  export { modelOutputToCSV } from "./csv.js";
17
+ export { describeWorkerError, MESSAGEERROR_TEXT } from "./workerError.js";
17
18
  export { formatNumber, isNumberFormat } from "./formatNumber.js";
18
19
  export type { NumberFormat, NumberFormatPreset } from "./formatNumber.js";
19
20
  export {
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Convert a worker `ErrorEvent` into a human-readable message. Cross-origin
3
+ * worker failures sanitize the event so every field is null/empty (e.g. the
4
+ * worker's module — or a dynamic CDN import inside it — fails to evaluate);
5
+ * fall back to a hint about likely causes rather than an empty string.
6
+ */
7
+ export function describeWorkerError(e: ErrorEvent): string {
8
+ const msg = e.message || e.error?.message || "";
9
+ if (msg) {
10
+ if (e.filename) return `${msg} (${e.filename}:${e.lineno})`;
11
+ return msg;
12
+ }
13
+ return "Worker failed to load (no error details available — most often a cross-origin script load failure or a syntax error in the worker module)";
14
+ }
15
+
16
+ export const MESSAGEERROR_TEXT =
17
+ "Worker received a message it could not deserialize (messageerror)";
@@ -0,0 +1,6 @@
1
+ export interface WorkerMessage {
2
+ id: number;
3
+ model: string;
4
+ fn: string;
5
+ args: string[];
6
+ }
@@ -3,13 +3,7 @@ import {
3
3
  postModelOutputsWithTransfer,
4
4
  postErrorWithTransfer,
5
5
  } from "@cfasim-ui/shared/transfer";
6
-
7
- interface WorkerMessage {
8
- id: number;
9
- model: string;
10
- fn: string;
11
- args: string[];
12
- }
6
+ import type { WorkerMessage } from "./messages.js";
13
7
 
14
8
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
9
  const modulePromises = new Map<string, Promise<Record<string, any>>>();
@@ -1,30 +1,13 @@
1
1
  import type { TransferableResponse } from "@cfasim-ui/shared";
2
- import { unwrapResponse } from "@cfasim-ui/shared";
3
-
4
- interface WorkerMessage {
5
- id: number;
6
- model: string;
7
- fn: string;
8
- args: string[];
9
- }
2
+ import {
3
+ unwrapResponse,
4
+ describeWorkerError,
5
+ MESSAGEERROR_TEXT,
6
+ } from "@cfasim-ui/shared";
7
+ import type { WorkerMessage } from "./messages.js";
10
8
 
11
9
  let lastId = 0;
12
10
 
13
- /**
14
- * Convert a worker `ErrorEvent` into a human-readable message. Cross-origin
15
- * worker failures sanitize the event so every field is null/empty (e.g.
16
- * the worker's module fails to evaluate); fall back to a hint rather than
17
- * an empty string.
18
- */
19
- function describeWorkerError(e: ErrorEvent): string {
20
- const msg = e.message || e.error?.message || "";
21
- if (msg) {
22
- if (e.filename) return `${msg} (${e.filename}:${e.lineno})`;
23
- return msg;
24
- }
25
- return "Worker failed to load (no error details available — most often a cross-origin script load failure or a syntax error in the worker module)";
26
- }
27
-
28
11
  // Rejects keyed by request id so `cancelWasm` can fail in-flight promises
29
12
  // when we terminate the worker mid-run, and so worker-level errors can drain
30
13
  // every pending call at once.
@@ -40,6 +23,7 @@ function newWorker(): Worker {
40
23
  type: "module",
41
24
  });
42
25
  function failAll(message: string) {
26
+ if (workerDeadError) return;
43
27
  workerDeadError = new Error(message);
44
28
  const rejects = Array.from(pendingRejects.values());
45
29
  pendingRejects.clear();
@@ -49,9 +33,7 @@ function newWorker(): Worker {
49
33
  failAll(describeWorkerError(e));
50
34
  });
51
35
  w.addEventListener("messageerror", () => {
52
- failAll(
53
- "Worker received a message it could not deserialize (messageerror)",
54
- );
36
+ failAll(MESSAGEERROR_TEXT);
55
37
  });
56
38
  return w;
57
39
  }