castle-web-sdk 0.4.55 → 0.4.56

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
@@ -2018,6 +2018,13 @@ window.addEventListener("touchstart", (event) => {
2018
2018
  });
2019
2019
  ```
2020
2020
 
2021
+ To open it without changing the deck, the deck's owner adds `?debug=1`
2022
+ to the deck's address on castle.xyz (`https://castle.xyz/d/<deck id>?debug=1`).
2023
+ The page checks that the signed-in viewer owns the deck, and only then
2024
+ opens the console as the deck starts; anyone else gets the deck as
2025
+ usual. It needs a deck saved with SDK 0.4.56 or later, and it works on
2026
+ the website only. In the Castle app, use `DebugConsole.show()`.
2027
+
2021
2028
  ## Setup
2022
2029
 
2023
2030
  Startup, editor-mode check, and a file-write call for editor UI.
package/dist/context.d.ts CHANGED
@@ -13,6 +13,11 @@ export interface CastleEmbed {
13
13
  * `experiments.get` command instead (experiments.ts).
14
14
  */
15
15
  experiments?: Record<string, string>;
16
+ /**
17
+ * Opens `DebugConsole` as the deck starts. castle.xyz sets it only for the
18
+ * deck's owner, when they add `?debug=1` to the deck's URL.
19
+ */
20
+ debugConsole?: boolean;
16
21
  }
17
22
  declare global {
18
23
  interface Window {
@@ -10,6 +10,7 @@ export interface DebugConsoleApi {
10
10
  hide(): void;
11
11
  lines(): DebugConsoleLine[];
12
12
  }
13
+ export declare const originalConsole: Record<DebugConsoleLevel, (...args: unknown[]) => void>;
13
14
  export declare function formatConsoleArgs(args: unknown[]): string;
14
15
  export declare function recordConsoleLine(level: DebugConsoleLevel, text: string): void;
15
16
  export declare function noteConsoleHooked(): void;
@@ -6,6 +6,10 @@
6
6
  // `setup()` records every console line and uncaught error from the start, so
7
7
  // `show()` reveals what happened before it was called. A deck that never calls
8
8
  // `setup()` gets recording from its first `show()` on.
9
+ //
10
+ // The host can also open it without any deck code (`openForHost` below): on
11
+ // castle.xyz the deck's owner adds `?debug=1` to the deck's URL.
12
+ import { getCastleEmbed } from "./context";
9
13
  const MAX_LINES = 300;
10
14
  const MAX_LINE_CHARS = 2000;
11
15
  const COLORS = {
@@ -13,27 +17,49 @@ const COLORS = {
13
17
  warn: "#ffd66b",
14
18
  error: "#ff8a80",
15
19
  };
20
+ // The console as it was before anything here or in runtime.ts wrapped it.
21
+ // runtime.ts writes through these, so a line is never recorded twice when the
22
+ // host opened the console before the deck called `setup()`.
23
+ export const originalConsole = {
24
+ log: console.log,
25
+ warn: console.warn,
26
+ error: console.error,
27
+ };
16
28
  const recorded = [];
17
29
  let consoleHooked = false;
18
30
  let errorsHooked = false;
19
31
  let panel = null;
20
32
  let list = null;
21
- export function formatConsoleArgs(args) {
22
- return args
23
- .map((arg) => {
33
+ // Never throws, and always a string: a console call the deck makes must not
34
+ // fail because of what it logged (a cyclic object with no prototype, a revoked
35
+ // proxy, an Error whose stack is not a string).
36
+ const UNPRINTABLE = "[unprintable]";
37
+ function formatConsoleArg(arg) {
38
+ try {
24
39
  if (typeof arg === "string")
25
40
  return arg;
26
- if (arg instanceof Error)
27
- return arg.stack || arg.message;
28
- try {
29
- const json = JSON.stringify(arg);
30
- return json ?? String(arg);
41
+ if (arg instanceof Error) {
42
+ const text = arg.stack || arg.message;
43
+ if (typeof text === "string")
44
+ return text;
31
45
  }
32
- catch {
33
- return String(arg);
34
- }
35
- })
36
- .join(" ");
46
+ const json = JSON.stringify(arg);
47
+ if (typeof json === "string")
48
+ return json;
49
+ }
50
+ catch {
51
+ // fall through to String()
52
+ }
53
+ try {
54
+ const text = String(arg);
55
+ return typeof text === "string" ? text : UNPRINTABLE;
56
+ }
57
+ catch {
58
+ return UNPRINTABLE;
59
+ }
60
+ }
61
+ export function formatConsoleArgs(args) {
62
+ return args.map(formatConsoleArg).join(" ");
37
63
  }
38
64
  // runtime.ts's own console hook calls this, so the line is formatted once for
39
65
  // both the dev serve and this buffer.
@@ -74,6 +100,8 @@ function hookConsole() {
74
100
  return;
75
101
  consoleHooked = true;
76
102
  for (const level of ["log", "warn", "error"]) {
103
+ // The console as it is now, so a wrapper the deck installed before
104
+ // calling show() keeps getting its lines.
77
105
  const original = console[level];
78
106
  console[level] = (...args) => {
79
107
  original(...args);
@@ -131,8 +159,12 @@ function show() {
131
159
  return;
132
160
  hookConsole();
133
161
  captureErrors();
134
- if (panel)
162
+ if (panel?.isConnected)
135
163
  return;
164
+ // A deck that replaces the body (innerHTML, replaceChildren) takes the
165
+ // panel with it; draw a new one.
166
+ panel?.remove();
167
+ panel = null;
136
168
  panel = buildPanel();
137
169
  (document.body ?? document.documentElement).appendChild(panel);
138
170
  for (const line of recorded) {
@@ -143,6 +175,7 @@ function show() {
143
175
  }
144
176
  }
145
177
  function hide() {
178
+ stopKeepingOpen();
146
179
  panel?.remove();
147
180
  panel = null;
148
181
  list = null;
@@ -152,3 +185,43 @@ export const DebugConsole = {
152
185
  hide,
153
186
  lines: () => recorded.slice(),
154
187
  };
188
+ // The host decides who sees the console: castle.xyz sets
189
+ // `CastleEmbed.debugConsole` only when the viewer who added `?debug=1` owns the
190
+ // deck (castle-www DeckPlayer). It is in the document before any deck script,
191
+ // so recording starts with the deck. Nothing here reads the deck's own URL:
192
+ // anyone can add a query string, only the host knows who is watching. A deck
193
+ // that sets the flag itself only shows its own console to its own players.
194
+ //
195
+ // Opened this way, the panel comes back when the deck's own startup clears the
196
+ // body (or replaces it), until the owner presses Hide.
197
+ let keepOpen = null;
198
+ function stopKeepingOpen() {
199
+ keepOpen?.disconnect();
200
+ keepOpen = null;
201
+ }
202
+ function openForHost() {
203
+ if (typeof document === "undefined" || getCastleEmbed()?.debugConsole !== true)
204
+ return;
205
+ hookConsole();
206
+ captureErrors();
207
+ const open = () => {
208
+ show();
209
+ if (keepOpen || typeof MutationObserver === "undefined")
210
+ return;
211
+ keepOpen = new MutationObserver(() => {
212
+ if (panel && !panel.isConnected)
213
+ show();
214
+ });
215
+ keepOpen.observe(document.documentElement, { childList: true, subtree: true });
216
+ };
217
+ // A classic script in the head runs before there is a body to draw into.
218
+ // A module deck runs once the document is parsed, still ahead of its own
219
+ // first pass over the body: the observer puts the panel back after that.
220
+ if (document.readyState === "loading") {
221
+ document.addEventListener("DOMContentLoaded", open, { once: true });
222
+ }
223
+ else {
224
+ open();
225
+ }
226
+ }
227
+ openForHost();
package/dist/runtime.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { CASTLE_SDK_PROTOCOL, } from "./commands";
2
2
  import { getCastleEmbed, isEdit } from "./context";
3
- import { captureErrors, formatConsoleArgs, noteConsoleHooked, recordConsoleLine } from "./debugConsole";
3
+ import { captureErrors, formatConsoleArgs, noteConsoleHooked, originalConsole, recordConsoleLine, } from "./debugConsole";
4
4
  import { cardUnit, fitStage, getDisplay } from "./display";
5
5
  import { appliedNotice, applyTunedParams, } from "./paramsRegistry";
6
6
  export const CARD_RATIO = 5 / 7;
@@ -20,9 +20,10 @@ const pendingRequests = new Map();
20
20
  // screenshot/write_file request map above. transport.ts owns the timeouts.
21
21
  const SOCKET_WAIT_TIMEOUT_MS = 10000;
22
22
  const localCommandListeners = new Map();
23
- const origLog = console.log;
24
- const origWarn = console.warn;
25
- const origError = console.error;
23
+ // Not `console.log` itself: the host may have had debugConsole.ts wrap it already.
24
+ const origLog = originalConsole.log;
25
+ const origWarn = originalConsole.warn;
26
+ const origError = originalConsole.error;
26
27
  // Indirected dynamic import so deck bundlers don't statically rewrite it.
27
28
  // eslint-disable-next-line @typescript-eslint/no-implied-eval
28
29
  const dynamicImport = new Function("u", "return import(u)");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "castle-web-sdk",
3
- "version": "0.4.55",
3
+ "version": "0.4.56",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/castle-xyz/castle-experimental-web.git"