castle-web-sdk 0.4.55 → 0.4.57
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 +9 -1
- package/dist/context.d.ts +5 -0
- package/dist/debugConsole.d.ts +1 -0
- package/dist/debugConsole.js +87 -14
- package/dist/runtime.js +62 -4
- package/dist/server/hub.js +5 -1
- package/dist/server/platformHandle.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -233,7 +233,8 @@ dev serve.
|
|
|
233
233
|
- `session.hub.place`, `.title` and `.result` are the place and the result shape
|
|
234
234
|
as published (`result` is `null` for a place that reports none, such as a town).
|
|
235
235
|
- `session.hub.reconnectWindowMs`, when present, is Castle's pinned hub-place
|
|
236
|
-
reconnect grace in milliseconds, inclusive at the deadline
|
|
236
|
+
reconnect grace in milliseconds, inclusive at the deadline (at most 180000, or
|
|
237
|
+
300000 on an owner house that opted into `houseResume`). It is absent on
|
|
237
238
|
sessions without a pinned grace and older hosts. Kits use it to match visit retention to the
|
|
238
239
|
transport window; deck code does not choose it.
|
|
239
240
|
- `session.hub.arrival(playerId)` is how a player came in when Castle placed them
|
|
@@ -2018,6 +2019,13 @@ window.addEventListener("touchstart", (event) => {
|
|
|
2018
2019
|
});
|
|
2019
2020
|
```
|
|
2020
2021
|
|
|
2022
|
+
To open it without changing the deck, the deck's owner adds `?debug=1`
|
|
2023
|
+
to the deck's address on castle.xyz (`https://castle.xyz/d/<deck id>?debug=1`).
|
|
2024
|
+
The page checks that the signed-in viewer owns the deck, and only then
|
|
2025
|
+
opens the console as the deck starts; anyone else gets the deck as
|
|
2026
|
+
usual. It needs a deck saved with SDK 0.4.56 or later, and it works on
|
|
2027
|
+
the website only. In the Castle app, use `DebugConsole.show()`.
|
|
2028
|
+
|
|
2021
2029
|
## Setup
|
|
2022
2030
|
|
|
2023
2031
|
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 {
|
package/dist/debugConsole.d.ts
CHANGED
|
@@ -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;
|
package/dist/debugConsole.js
CHANGED
|
@@ -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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
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
|
-
|
|
24
|
-
const
|
|
25
|
-
const
|
|
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)");
|
|
@@ -740,6 +741,10 @@ function connectBridge() {
|
|
|
740
741
|
return;
|
|
741
742
|
}
|
|
742
743
|
const port = event.ports[0];
|
|
744
|
+
// A shell that answers reload requests: tell it this SDK will ask before
|
|
745
|
+
// reloading, so it lets restarts through to us (we flush saves first).
|
|
746
|
+
const reloadRequests = data.reloadRequests === true;
|
|
747
|
+
bridgeReloadPort = reloadRequests ? port : null;
|
|
743
748
|
const socket = {
|
|
744
749
|
readyState: WebSocket.OPEN,
|
|
745
750
|
send: (value) => {
|
|
@@ -755,9 +760,15 @@ function connectBridge() {
|
|
|
755
760
|
const value = messageEvent.data;
|
|
756
761
|
if (!value || typeof value !== "object")
|
|
757
762
|
return;
|
|
763
|
+
if (value.type === "castle-reload-response") {
|
|
764
|
+
settleReloadRequest(value);
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
758
767
|
handleLocalMessage(value);
|
|
759
768
|
};
|
|
760
769
|
port.start();
|
|
770
|
+
if (reloadRequests)
|
|
771
|
+
port.postMessage({ type: "castle-sdk-hello", reloadRequests: true });
|
|
761
772
|
localServeSeen = true;
|
|
762
773
|
ws = socket;
|
|
763
774
|
for (const msg of logBuffer)
|
|
@@ -1107,6 +1118,48 @@ function stashReloadState() {
|
|
|
1107
1118
|
export function requestReload() {
|
|
1108
1119
|
scheduleReload(REQUEST_RELOAD_DEBOUNCE_MS, "requestReload");
|
|
1109
1120
|
}
|
|
1121
|
+
// Set when the shell's bridge answers reload requests (castle-bridge-init's
|
|
1122
|
+
// `reloadRequests`). Before reloading, this frame asks the shell: a frame whose
|
|
1123
|
+
// content-origin grant has expired would only fail to reload in place, so the
|
|
1124
|
+
// shell moves it to fresh origins instead and this page does nothing more.
|
|
1125
|
+
let bridgeReloadPort = null;
|
|
1126
|
+
const RELOAD_REQUEST_TIMEOUT_MS = 4000;
|
|
1127
|
+
const pendingReloadRequests = new Map();
|
|
1128
|
+
let nextReloadRequest = 1;
|
|
1129
|
+
function settleReloadRequest(value) {
|
|
1130
|
+
if (typeof value.requestId !== "string")
|
|
1131
|
+
return;
|
|
1132
|
+
const settle = pendingReloadRequests.get(value.requestId);
|
|
1133
|
+
if (!settle)
|
|
1134
|
+
return;
|
|
1135
|
+
pendingReloadRequests.delete(value.requestId);
|
|
1136
|
+
settle(value.relocated === true);
|
|
1137
|
+
}
|
|
1138
|
+
/** Whether the shell moved this frame (so it must not reload itself). */
|
|
1139
|
+
function askShellToRelocate() {
|
|
1140
|
+
const port = bridgeReloadPort;
|
|
1141
|
+
if (!port)
|
|
1142
|
+
return Promise.resolve(false);
|
|
1143
|
+
const requestId = `reload-${nextReloadRequest++}`;
|
|
1144
|
+
return new Promise((resolve) => {
|
|
1145
|
+
const timer = setTimeout(() => {
|
|
1146
|
+
pendingReloadRequests.delete(requestId);
|
|
1147
|
+
resolve(false);
|
|
1148
|
+
}, RELOAD_REQUEST_TIMEOUT_MS);
|
|
1149
|
+
pendingReloadRequests.set(requestId, (relocated) => {
|
|
1150
|
+
clearTimeout(timer);
|
|
1151
|
+
resolve(relocated);
|
|
1152
|
+
});
|
|
1153
|
+
try {
|
|
1154
|
+
port.postMessage({ type: "castle-reload-request", requestId, href: location.href });
|
|
1155
|
+
}
|
|
1156
|
+
catch {
|
|
1157
|
+
clearTimeout(timer);
|
|
1158
|
+
pendingReloadRequests.delete(requestId);
|
|
1159
|
+
resolve(false);
|
|
1160
|
+
}
|
|
1161
|
+
});
|
|
1162
|
+
}
|
|
1110
1163
|
function scheduleReload(delayMs, reason = "unknown") {
|
|
1111
1164
|
console.log(`[reload] ${panelTag()} scheduling reload (${reason})`);
|
|
1112
1165
|
if (restartTimer !== null)
|
|
@@ -1120,6 +1173,11 @@ function scheduleReload(delayMs, reason = "unknown") {
|
|
|
1120
1173
|
// a failed flush shouldn't block the reload
|
|
1121
1174
|
}
|
|
1122
1175
|
stashReloadState();
|
|
1176
|
+
// Saves are flushed above either way. A frame the shell moves (its
|
|
1177
|
+
// grant expired) loads afresh elsewhere; otherwise reload in place,
|
|
1178
|
+
// keeping this origin and the save queue stored on it.
|
|
1179
|
+
if (await askShellToRelocate())
|
|
1180
|
+
return;
|
|
1123
1181
|
location.reload();
|
|
1124
1182
|
})();
|
|
1125
1183
|
}, delayMs);
|
package/dist/server/hub.js
CHANGED
|
@@ -46,9 +46,13 @@ function unavailable(op, what) {
|
|
|
46
46
|
operation: `session.hub.${op}`,
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
|
+
// An opted-in owner house may be pinned up to 300 s (a three-minute app switch keeps
|
|
50
|
+
// it); every other hub place keeps the host's 180 s cap.
|
|
49
51
|
function reconnectMetadata(pinned) {
|
|
50
52
|
const value = pinned.reconnectWindowMs;
|
|
51
|
-
|
|
53
|
+
const cap = pinned.houseResume === true && typeof pinned.owner === "string" && pinned.owner.length > 0
|
|
54
|
+
? 300_000 : 180_000;
|
|
55
|
+
return pinned.result === null && typeof value === "number" && Number.isInteger(value) && value > 0 && value <= cap
|
|
52
56
|
? { reconnectWindowMs: value } : {};
|
|
53
57
|
}
|
|
54
58
|
export function createServerHub(platformHandle, arrivals, hiddenPair = () => false) {
|
|
@@ -126,6 +126,8 @@ export interface PlatformHubPlace {
|
|
|
126
126
|
version: 1;
|
|
127
127
|
};
|
|
128
128
|
owner?: string;
|
|
129
|
+
/** Ghost's trusted owner-house opt-in; such a house may pin up to 300 s of grace. */
|
|
130
|
+
houseResume?: true;
|
|
129
131
|
reconnectWindowMs?: number;
|
|
130
132
|
place?: string;
|
|
131
133
|
title?: string | null;
|