castle-web-cli 0.4.162 → 0.4.164
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/devSessionChild.js +23 -0
- package/dist/devSessionServer.js +49 -4
- package/dist/headlessCover.js +12 -1
- package/dist/init.js +1 -1
- package/dist/platformDoc.js +5 -1
- package/dist/save-deck.d.ts +2 -0
- package/dist/save-deck.js +53 -20
- package/dist/shell/assets/index-CIRBt1qf.css +1 -0
- package/dist/shell/assets/index-QtCau5vM.js +444 -0
- package/dist/shell/index.html +2 -2
- package/kits/base/castle.json +1 -1
- package/kits/base/package-lock.json +1 -1
- package/kits/base/sdk/README.md +13 -1
- package/kits/base/sdk/multiplayer.d.ts +2 -0
- package/kits/base/sdk/multiplayer.js +59 -16
- package/kits/base/sdk/multiplayerProtocol.d.ts +3 -0
- package/kits/base/sdk/multiplayerProtocol.js +13 -0
- package/kits/base/sdk/server/platformHandle.d.ts +3 -0
- package/kits/base/sdk/server/wrapper.d.ts +3 -0
- package/kits/base/sdk/server/wrapper.js +42 -0
- package/kits/physics-2d/castle.json +1 -1
- package/kits/physics-2d/engine/ui.jsx +24 -0
- package/kits/physics-2d/engine/ui.module.css +5 -0
- package/package.json +3 -2
- package/dist/shell/assets/index--_Rr2xwM.js +0 -441
- package/dist/shell/assets/index-DfZmu3XC.css +0 -1
- package/kits/base/node_modules/.package-lock.json +0 -28
- package/kits/base/node_modules/castle-web-fonts/README.md +0 -114
- package/kits/base/node_modules/castle-web-fonts/dist/catalog.d.ts +0 -18
- package/kits/base/node_modules/castle-web-fonts/dist/catalog.js +0 -142
- package/kits/base/node_modules/castle-web-fonts/dist/face.d.ts +0 -1
- package/kits/base/node_modules/castle-web-fonts/dist/face.js +0 -52
- package/kits/base/node_modules/castle-web-fonts/dist/faces/Bore.d.ts +0 -3
- package/kits/base/node_modules/castle-web-fonts/dist/faces/Bore.js +0 -10
- package/kits/base/node_modules/castle-web-fonts/dist/faces/CourierPrime.d.ts +0 -3
- package/kits/base/node_modules/castle-web-fonts/dist/faces/CourierPrime.js +0 -10
- package/kits/base/node_modules/castle-web-fonts/dist/faces/DMSans.d.ts +0 -3
- package/kits/base/node_modules/castle-web-fonts/dist/faces/DMSans.js +0 -10
- package/kits/base/node_modules/castle-web-fonts/dist/faces/Glacier.d.ts +0 -3
- package/kits/base/node_modules/castle-web-fonts/dist/faces/Glacier.js +0 -10
- package/kits/base/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.d.ts +0 -3
- package/kits/base/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.js +0 -10
- package/kits/base/node_modules/castle-web-fonts/dist/faces/Piazzolla.d.ts +0 -3
- package/kits/base/node_modules/castle-web-fonts/dist/faces/Piazzolla.js +0 -10
- package/kits/base/node_modules/castle-web-fonts/dist/faces/Synco.d.ts +0 -3
- package/kits/base/node_modules/castle-web-fonts/dist/faces/Synco.js +0 -10
- package/kits/base/node_modules/castle-web-fonts/dist/faces/Tektur.d.ts +0 -3
- package/kits/base/node_modules/castle-web-fonts/dist/faces/Tektur.js +0 -10
- package/kits/base/node_modules/castle-web-fonts/dist/faces/YatraOne.d.ts +0 -3
- package/kits/base/node_modules/castle-web-fonts/dist/faces/YatraOne.js +0 -10
- package/kits/base/node_modules/castle-web-fonts/dist/index.d.ts +0 -16
- package/kits/base/node_modules/castle-web-fonts/dist/index.js +0 -15
- package/kits/base/node_modules/castle-web-fonts/licenses/CC-BY-ND-4.0.txt +0 -393
- package/kits/base/node_modules/castle-web-fonts/licenses/FONTS.md +0 -64
- package/kits/base/node_modules/castle-web-fonts/licenses/OFL-1.1.txt +0 -95
- package/kits/base/node_modules/castle-web-fonts/package.json +0 -47
- package/kits/base/node_modules/castle-web-fonts/woff2/Bore.woff2 +0 -0
- package/kits/base/node_modules/castle-web-fonts/woff2/CourierPrime.woff2 +0 -0
- package/kits/base/node_modules/castle-web-fonts/woff2/DMSans.woff2 +0 -0
- package/kits/base/node_modules/castle-web-fonts/woff2/Glacier.woff2 +0 -0
- package/kits/base/node_modules/castle-web-fonts/woff2/HelicoCentrica.woff2 +0 -0
- package/kits/base/node_modules/castle-web-fonts/woff2/Piazzolla.woff2 +0 -0
- package/kits/base/node_modules/castle-web-fonts/woff2/SOURCE.txt +0 -2
- package/kits/base/node_modules/castle-web-fonts/woff2/Synco.woff2 +0 -0
- package/kits/base/node_modules/castle-web-fonts/woff2/Tektur.woff2 +0 -0
- package/kits/base/node_modules/castle-web-fonts/woff2/YatraOne.woff2 +0 -0
package/dist/devSessionChild.js
CHANGED
|
@@ -60,6 +60,17 @@ function createPlatformHandle(init, platformEvents) {
|
|
|
60
60
|
broadcast(text) {
|
|
61
61
|
send({ protocolVersion: PROTOCOL_VERSION, t: 'broadcast', d: text });
|
|
62
62
|
},
|
|
63
|
+
sendBinary(connId, bytes) {
|
|
64
|
+
send({
|
|
65
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
66
|
+
t: 'sendBinary',
|
|
67
|
+
playerId: connId,
|
|
68
|
+
b: binaryBase64(bytes),
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
broadcastBinary(bytes) {
|
|
72
|
+
send({ protocolVersion: PROTOCOL_VERSION, t: 'broadcastBinary', b: binaryBase64(bytes) });
|
|
73
|
+
},
|
|
63
74
|
disconnect(connId, code) {
|
|
64
75
|
send({ protocolVersion: PROTOCOL_VERSION, t: 'kick', playerId: connId, reason: String(code) });
|
|
65
76
|
},
|
|
@@ -90,6 +101,12 @@ async function dispatch(platformEvents, frame) {
|
|
|
90
101
|
case 'clientMessage':
|
|
91
102
|
await platformEvents.emit('message', frame.playerId, JSON.stringify(frame.d));
|
|
92
103
|
return;
|
|
104
|
+
case 'clientBinary':
|
|
105
|
+
if (typeof frame.playerId !== 'string' || typeof frame.b !== 'string') {
|
|
106
|
+
throw new Error('platform channel sent an invalid binary message');
|
|
107
|
+
}
|
|
108
|
+
await platformEvents.emit('binaryMessage', frame.playerId, new Uint8Array(Buffer.from(frame.b, 'base64')));
|
|
109
|
+
return;
|
|
93
110
|
case 'replaced':
|
|
94
111
|
await platformEvents.emit('replaced');
|
|
95
112
|
return;
|
|
@@ -111,6 +128,12 @@ function send(frame) {
|
|
|
111
128
|
throw new Error('platform channel is not open');
|
|
112
129
|
socket.send(JSON.stringify(frame));
|
|
113
130
|
}
|
|
131
|
+
function binaryBase64(bytes) {
|
|
132
|
+
const buffer = bytes instanceof ArrayBuffer
|
|
133
|
+
? Buffer.from(bytes)
|
|
134
|
+
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
135
|
+
return buffer.toString('base64');
|
|
136
|
+
}
|
|
114
137
|
function asRecord(value) {
|
|
115
138
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
116
139
|
throw new Error('platform channel sent an invalid frame');
|
package/dist/devSessionServer.js
CHANGED
|
@@ -10,6 +10,9 @@ import { readCastleJson, resolveServerConfig } from './castleJson.js';
|
|
|
10
10
|
import { bundleServerModule } from './serverBundle.js';
|
|
11
11
|
const PROTOCOL_VERSION = 1;
|
|
12
12
|
const MAX_CLIENT_FRAME_BYTES = 16 * 1024;
|
|
13
|
+
// Mirrors the host-agent's server-to-client broadcast cap.
|
|
14
|
+
const MAX_SERVER_FRAME_BYTES = 64 * 1024;
|
|
15
|
+
const BINARY_MESSAGE_TAG = 0x01;
|
|
13
16
|
const NONCE_TTL_MS = 60_000;
|
|
14
17
|
const CHILD_READY_TIMEOUT_MS = 15_000;
|
|
15
18
|
const CHILD_STOP_GRACE_MS = 2_000;
|
|
@@ -38,7 +41,11 @@ class DevSessionServerImpl {
|
|
|
38
41
|
noServer: true,
|
|
39
42
|
maxPayload: MAX_CLIENT_FRAME_BYTES,
|
|
40
43
|
});
|
|
41
|
-
|
|
44
|
+
// Leaves room for base64 and JSON envelope expansion around a legal server frame.
|
|
45
|
+
platformWss = new WebSocketServer({
|
|
46
|
+
noServer: true,
|
|
47
|
+
maxPayload: MAX_SERVER_FRAME_BYTES * 2 + 1024,
|
|
48
|
+
});
|
|
42
49
|
nonces = new Map();
|
|
43
50
|
reconnects = new Map();
|
|
44
51
|
players = new Map();
|
|
@@ -229,7 +236,19 @@ class DevSessionServerImpl {
|
|
|
229
236
|
}
|
|
230
237
|
handleClientMessage(playerId, socket, data, isBinary) {
|
|
231
238
|
if (isBinary) {
|
|
232
|
-
|
|
239
|
+
const buffer = Buffer.isBuffer(data)
|
|
240
|
+
? data
|
|
241
|
+
: Buffer.concat(Array.isArray(data) ? data : [Buffer.from(data)]);
|
|
242
|
+
if (buffer.length === 0 || buffer[0] !== BINARY_MESSAGE_TAG) {
|
|
243
|
+
socket.close(1003, 'unsupported binary frame');
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
this.sendPlatform({
|
|
247
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
248
|
+
t: 'clientBinary',
|
|
249
|
+
playerId,
|
|
250
|
+
b: buffer.subarray(1).toString('base64'),
|
|
251
|
+
});
|
|
233
252
|
return;
|
|
234
253
|
}
|
|
235
254
|
let frame;
|
|
@@ -313,6 +332,13 @@ class DevSessionServerImpl {
|
|
|
313
332
|
if (socket && payload.ok)
|
|
314
333
|
this.sendClient(socket, { t: 'm', d: payload.value });
|
|
315
334
|
}
|
|
335
|
+
else if (frame.t === 'sendBinary' &&
|
|
336
|
+
typeof frame.playerId === 'string' &&
|
|
337
|
+
typeof frame.b === 'string') {
|
|
338
|
+
const socket = this.players.get(frame.playerId)?.socket;
|
|
339
|
+
if (socket)
|
|
340
|
+
this.sendClientBinary(socket, Buffer.from(frame.b, 'base64'));
|
|
341
|
+
}
|
|
316
342
|
else if (frame.t === 'broadcast') {
|
|
317
343
|
const payload = parseAuthorPayload(frame.d);
|
|
318
344
|
if (payload.ok)
|
|
@@ -321,6 +347,13 @@ class DevSessionServerImpl {
|
|
|
321
347
|
this.sendClient(player.socket, { t: 'm', d: payload.value });
|
|
322
348
|
}
|
|
323
349
|
}
|
|
350
|
+
else if (frame.t === 'broadcastBinary' && typeof frame.b === 'string') {
|
|
351
|
+
const payload = Buffer.from(frame.b, 'base64');
|
|
352
|
+
for (const player of this.players.values()) {
|
|
353
|
+
if (player.socket)
|
|
354
|
+
this.sendClientBinary(player.socket, payload);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
324
357
|
else if (frame.t === 'kick' && typeof frame.playerId === 'string') {
|
|
325
358
|
this.kick(frame.playerId, typeof frame.reason === 'string' ? frame.reason : undefined);
|
|
326
359
|
}
|
|
@@ -420,12 +453,24 @@ class DevSessionServerImpl {
|
|
|
420
453
|
if (socket.readyState !== WebSocket.OPEN)
|
|
421
454
|
return;
|
|
422
455
|
const text = JSON.stringify(frame);
|
|
423
|
-
if (Buffer.byteLength(text) >
|
|
424
|
-
this.log('error', 'dropped a server message larger than
|
|
456
|
+
if (Buffer.byteLength(text) > MAX_SERVER_FRAME_BYTES) {
|
|
457
|
+
this.log('error', 'dropped a server message larger than 64KB');
|
|
425
458
|
return;
|
|
426
459
|
}
|
|
427
460
|
socket.send(text);
|
|
428
461
|
}
|
|
462
|
+
sendClientBinary(socket, payload) {
|
|
463
|
+
if (socket.readyState !== WebSocket.OPEN)
|
|
464
|
+
return;
|
|
465
|
+
const frame = Buffer.allocUnsafe(payload.byteLength + 1);
|
|
466
|
+
frame[0] = BINARY_MESSAGE_TAG;
|
|
467
|
+
payload.copy(frame, 1);
|
|
468
|
+
if (frame.byteLength > MAX_SERVER_FRAME_BYTES) {
|
|
469
|
+
this.log('error', 'dropped a server message larger than 64KB');
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
socket.send(frame, { binary: true });
|
|
473
|
+
}
|
|
429
474
|
broadcastRoster() {
|
|
430
475
|
const players = this.connectedPlayers();
|
|
431
476
|
for (const player of this.players.values()) {
|
package/dist/headlessCover.js
CHANGED
|
@@ -41,6 +41,10 @@ const SETTLE_MS = 3_500;
|
|
|
41
41
|
// watch a spinner for. Whatever is unfinished at this point is abandoned and
|
|
42
42
|
// the save proceeds without a cover.
|
|
43
43
|
const CAPTURE_BUDGET_MS = 60_000;
|
|
44
|
+
// Remote workers normally finish comfortably inside this window. A cover is best-effort and the
|
|
45
|
+
// editor capture remains available as a fallback, so a queue or worker problem must not hold the
|
|
46
|
+
// synchronous save path until the GraphQL client's much larger generic browser-job timeout.
|
|
47
|
+
const REMOTE_CAPTURE_BUDGET_MS = 15_000;
|
|
44
48
|
// The card the SDK mounts (`initCard`) or a deck marks itself. Falling back to
|
|
45
49
|
// the whole viewport keeps a deck that does neither from getting no cover at
|
|
46
50
|
// all -- the viewport IS the card in play mode.
|
|
@@ -91,8 +95,9 @@ export async function captureCoverHeadless(opts) {
|
|
|
91
95
|
}
|
|
92
96
|
}
|
|
93
97
|
async function captureCoverRemote(contentGrant, outPath) {
|
|
98
|
+
const signal = AbortSignal.timeout(REMOTE_CAPTURE_BUDGET_MS);
|
|
94
99
|
try {
|
|
95
|
-
const result = await api.runBrowserJob(contentGrant, { kind: 'cover' });
|
|
100
|
+
const result = await api.runBrowserJob(contentGrant, { kind: 'cover' }, signal);
|
|
96
101
|
const pngBase64 = result.pngBase64;
|
|
97
102
|
if (result.protocolVersion !== 1 ||
|
|
98
103
|
result.operation !== 'cover' ||
|
|
@@ -109,6 +114,12 @@ async function captureCoverRemote(contentGrant, outPath) {
|
|
|
109
114
|
return { ok: true, outPath };
|
|
110
115
|
}
|
|
111
116
|
catch (error) {
|
|
117
|
+
if (signal.aborted) {
|
|
118
|
+
return {
|
|
119
|
+
ok: false,
|
|
120
|
+
error: `remote cover gave up after ${REMOTE_CAPTURE_BUDGET_MS / 1000}s`,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
112
123
|
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
113
124
|
}
|
|
114
125
|
}
|
package/dist/init.js
CHANGED
|
@@ -55,7 +55,7 @@ if (!(await mountEditorRoute())) startGame(initCard());
|
|
|
55
55
|
// Registry version of castle-web-sdk to inject when scaffolding from a
|
|
56
56
|
// globally-installed castle-web (not from inside the workspace). Bumped
|
|
57
57
|
// alongside cli/sdk version bumps.
|
|
58
|
-
const PUBLISHED_SDK_VERSION = '0.4.
|
|
58
|
+
const PUBLISHED_SDK_VERSION = '0.4.25';
|
|
59
59
|
// Range, not a pin: `castle-web-fonts` is its own package precisely so a tenth
|
|
60
60
|
// face can ship without a cli release, and a caret range means an existing
|
|
61
61
|
// deck's next install picks it up. Nothing here has to move when it does.
|
package/dist/platformDoc.js
CHANGED
|
@@ -53,6 +53,9 @@ export function everyDeckBullets(deckDir) {
|
|
|
53
53
|
"- Do not reload the deck after an edit. Nothing auto-reloads: the person applies your changes when they are ready, from the Play panel's restart button or a panel's own reload control. `npm run restart` reloads every open panel at once and breaks their flow — only when asked.",
|
|
54
54
|
"- The deck can define its own editors: declare a file type in `castle.json` (`editor.fileTypes`: ext, label, icon, and an `editors/` module) and the Files panel opens that type in it — whole recipe under `## Editors` in the deck's guide (its kit's `CLAUDE.md` under `imports/`). Editors under `imports/` are read-only; add or fork, don't edit.",
|
|
55
55
|
'- An editor is for content FILES the creator keeps coming back to (catalogs, dialogue, level tables) that nothing already opens. One-off or generated data stays code.',
|
|
56
|
+
"- **Put the values someone would tune while playtesting in a `PARAMS` object**: `export const PARAMS = { jumpHeight: 4.2, coyoteTime: 0.08 };` near the top of the file that uses them. Every `.js` / `.jsx` / `.ts` / `.tsx` file has a Params view beside its Code view that renders those values as fields -- a number scrubs, a boolean toggles, a `'#rrggbb'` string opens a color picker -- and writes the edit back into the file. That is how a creator tunes a game on a phone without putting a cursor inside an expression. Nested one level deep (`player: { speed: 5 }`) renders as a group; a value that is an expression (`speed: BASE * 2`) still shows, read-only.",
|
|
57
|
+
'- **This is for the handful of numbers someone would actually reach for between playtests, not every constant in the file.** A `PARAMS` with sixty keys is a wall nobody can scan, which is how a creator stops opening the pane at all. A magic number that only ever has one right value is not a param.',
|
|
58
|
+
"- **Read them through the object where they are used** -- `PARAMS.jumpHeight`, not `const { jumpHeight } = PARAMS` at module load. A destructured copy is taken once when the module loads and can never track a later change, so keeping the read at the use site is what leaves a tuned value able to reach the running deck.",
|
|
56
59
|
MODE_BULLET,
|
|
57
60
|
];
|
|
58
61
|
}
|
|
@@ -217,7 +220,8 @@ const COMMON_SECTIONS = `## Imports are read-only
|
|
|
217
220
|
\`named\` puts everyone passing the same \`key\` in one session, which is how you build room codes and invite links. \`public\` drops a player into a shared session for the deck; \`party\` uses the Castle party they are already in.
|
|
218
221
|
- **Always handle \`conn.isSolo\`.** When a player cannot join — signed out, blocked, private deck, or no capacity free — the SDK returns a working single-player connection instead of throwing. Keep the deck playable on its own rather than gating the whole game behind other people showing up.
|
|
219
222
|
- **What the server may and may not do.** Roughly 128MB of memory, no filesystem that survives, and **no network access at all**: no \`fetch\`, no database, no third-party API. Keep state in memory and send it to clients. A session ends once its last player leaves and nothing persists between sessions, so use \`sharedDeckStorage\` for anything that must outlive one.
|
|
220
|
-
- **Limits worth designing around.**
|
|
223
|
+
- **Limits worth designing around.** Client→server messages are capped at 16 KB and about 30 per second; server→client messages are capped at 64 KB. These limits apply to both JSON and binary frames and are measured on the frame, so a binary frame's one-byte protocol tag leaves you one byte under the cap for payload. Sending 20–30 updates per second means sending deltas rather than whole-world snapshots, and every player's phone pays for whatever you broadcast.
|
|
224
|
+
- **Binary messages are opt-in per payload.** Pass any typed array, \`DataView\`, or \`ArrayBuffer\` to client \`send\`, server \`send\`, or server \`broadcast\` to send its raw bytes. \`onMessage\` always receives those bytes as a \`Uint8Array\`, while every other payload continues to use JSON. Reconstruct another view type such as \`Float32Array\` on receipt; multi-byte values use the sender platform's byte order.
|
|
221
225
|
- **Test multiplayer locally with no setup.** \`castle-web serve\` runs your server file alongside the deck; open the play URL in two tabs and they share a session. Editing anything under \`server/\` restarts it and clients reconnect, so you exercise your reconnect path constantly. Server \`console.log\` goes to the serve's logs, not the browser console.
|
|
222
226
|
- **Publishing does not disturb games in progress.** \`save-deck\` bundles and publishes \`server/\`, but sessions already running keep the code they started with (they get \`onReplaced\`, so you can prompt players), and the next new session picks up the new version.
|
|
223
227
|
`;
|
package/dist/save-deck.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export declare const SOURCE_ARCHIVE_EXCLUDES: string[];
|
|
2
|
+
export declare const SAVE_STAGE_TELEMETRY_PREFIX = "CASTLE_SAVE_TELEMETRY ";
|
|
3
|
+
export type SaveStage = 'bundle' | 'cover' | 'scene_upload_config' | 'scene_upload' | 'content_publish' | 'server_publish' | 'source_archive' | 'source_upload_config' | 'source_upload' | 'source_commit';
|
|
2
4
|
export declare function runTar(args: string[], opts?: {
|
|
3
5
|
capture?: boolean;
|
|
4
6
|
}): Promise<string>;
|
package/dist/save-deck.js
CHANGED
|
@@ -15,6 +15,37 @@ import { buildServerBundle } from './serverBundle.js';
|
|
|
15
15
|
// part of it. (Being on this list also means `get-deck` leaves an existing
|
|
16
16
|
// `imports/` in place when it refreshes a deck.)
|
|
17
17
|
export const SOURCE_ARCHIVE_EXCLUDES = ['node_modules', 'dist', '.castle', '.git', 'imports'];
|
|
18
|
+
// CastleWWW requests these records through CASTLE_SAVE_STAGE_TELEMETRY and recognizes only this
|
|
19
|
+
// prefix plus a fixed stage schema before relaying them to central logs. Keeping them opt-in avoids
|
|
20
|
+
// adding machine output to interactive local saves. Old CLIs ignore the env, so releases remain
|
|
21
|
+
// independently deployable.
|
|
22
|
+
export const SAVE_STAGE_TELEMETRY_PREFIX = 'CASTLE_SAVE_TELEMETRY ';
|
|
23
|
+
function emitSaveStage(stage, outcome, startedAtMs, fields = {}) {
|
|
24
|
+
if (process.env.CASTLE_SAVE_STAGE_TELEMETRY !== '1')
|
|
25
|
+
return;
|
|
26
|
+
console.log(`${SAVE_STAGE_TELEMETRY_PREFIX}${JSON.stringify({
|
|
27
|
+
event: 'castle_web_save_stage_completed',
|
|
28
|
+
stage,
|
|
29
|
+
outcome,
|
|
30
|
+
duration_ms: Math.max(0, Date.now() - startedAtMs),
|
|
31
|
+
...(fields.bytes === undefined ? {} : { bytes: fields.bytes }),
|
|
32
|
+
...(fields.coverUploaded === undefined
|
|
33
|
+
? {}
|
|
34
|
+
: { cover_uploaded: fields.coverUploaded }),
|
|
35
|
+
})}`);
|
|
36
|
+
}
|
|
37
|
+
async function traceSaveStage(stage, operation, fields) {
|
|
38
|
+
const startedAtMs = Date.now();
|
|
39
|
+
try {
|
|
40
|
+
const result = await operation();
|
|
41
|
+
emitSaveStage(stage, 'success', startedAtMs, fields?.(result));
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
emitSaveStage(stage, 'failure', startedAtMs);
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
18
49
|
// `capture` keeps tar's stdout and resolves with it -- for listing an archive
|
|
19
50
|
// (`-tzf`), where the output IS the answer. Packing and unpacking ignore it.
|
|
20
51
|
export function runTar(args, opts = {}) {
|
|
@@ -75,15 +106,15 @@ async function uploadPresignedFile(uploadConfig, file, contentType, timeoutMs, l
|
|
|
75
106
|
throw new Error(`${label} failed: HTTP ${response.status}`);
|
|
76
107
|
}
|
|
77
108
|
async function uploadSource(projectDir, deckId) {
|
|
78
|
-
const archive = await archiveSource(projectDir);
|
|
109
|
+
const archive = await traceSaveStage('source_archive', () => archiveSource(projectDir), (value) => ({ bytes: value.byteLength }));
|
|
79
110
|
const sizeKB = archive.length / 1024;
|
|
80
111
|
console.log(`Source archive: ${sizeKB.toFixed(1)}KB`);
|
|
81
|
-
const uploadConfig = await api.createWebDeckSourceUploadConfig(deckId);
|
|
112
|
+
const uploadConfig = await traceSaveStage('source_upload_config', () => api.createWebDeckSourceUploadConfig(deckId));
|
|
82
113
|
// FormData expects a Blob; copy the Buffer's bytes so we don't share the underlying ArrayBuffer.
|
|
83
114
|
const view = new Uint8Array(archive.byteLength);
|
|
84
115
|
view.set(archive);
|
|
85
|
-
await uploadPresignedFile(uploadConfig, new Blob([view], { type: 'application/gzip' }), 'application/gzip', 60_000, 'Source upload');
|
|
86
|
-
await api.saveWebDeckSource(deckId, uploadConfig.uploadId);
|
|
116
|
+
await traceSaveStage('source_upload', () => uploadPresignedFile(uploadConfig, new Blob([view], { type: 'application/gzip' }), 'application/gzip', 60_000, 'Source upload'));
|
|
117
|
+
await traceSaveStage('source_commit', () => api.saveWebDeckSource(deckId, uploadConfig.uploadId));
|
|
87
118
|
}
|
|
88
119
|
const SERVER_BUNDLE_STATE = path.join('.castle', 'published-server-bundle.json');
|
|
89
120
|
function readPublishedServerBundleState(projectDir) {
|
|
@@ -264,7 +295,7 @@ export async function saveDeck(dir, opts = {}) {
|
|
|
264
295
|
}
|
|
265
296
|
const castleJson = readCastleJson(projectDir);
|
|
266
297
|
console.log('Bundling...');
|
|
267
|
-
const bundle = await bundleProject(projectDir);
|
|
298
|
+
const bundle = await traceSaveStage('bundle', () => bundleProject(projectDir), (value) => ({ bytes: Buffer.byteLength(value) }));
|
|
268
299
|
const sizeKB = bundle.length / 1024;
|
|
269
300
|
console.log(`Bundle size: ${sizeKB.toFixed(1)}KB`);
|
|
270
301
|
if (sizeKB > 1024) {
|
|
@@ -278,13 +309,14 @@ export async function saveDeck(dir, opts = {}) {
|
|
|
278
309
|
// soon as the bundle has passed, while the independent scene upload is being authorized and
|
|
279
310
|
// transferred. The promise is deliberately non-throwing: a thumbnail must never make the
|
|
280
311
|
// content upload fail or create an unhandled rejection if that upload exits early.
|
|
281
|
-
const backgroundImageFileIdPromise = uploadLocalCover(projectDir);
|
|
312
|
+
const backgroundImageFileIdPromise = traceSaveStage('cover', () => uploadLocalCover(projectDir), (value) => ({ coverUploaded: value !== null }));
|
|
282
313
|
// Upload scene data to S3
|
|
283
|
-
const configs = await
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
314
|
+
const configs = await traceSaveStage('scene_upload_config', async () => {
|
|
315
|
+
const result = await api.createSceneDataUploadConfig([cardId]);
|
|
316
|
+
if (!result?.length)
|
|
317
|
+
throw new Error('Failed to get upload config.');
|
|
318
|
+
return result;
|
|
319
|
+
});
|
|
288
320
|
const uploadConfig = configs[0];
|
|
289
321
|
const formData = new FormData();
|
|
290
322
|
formData.append('Content-Type', 'application/json');
|
|
@@ -292,14 +324,15 @@ export async function saveDeck(dir, opts = {}) {
|
|
|
292
324
|
formData.append(k, String(v));
|
|
293
325
|
}
|
|
294
326
|
formData.append('file', new Blob([JSON.stringify(sceneData)]));
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
327
|
+
await traceSaveStage('scene_upload', async () => {
|
|
328
|
+
const response = await fetch(uploadConfig.postUrl, {
|
|
329
|
+
method: 'POST',
|
|
330
|
+
body: formData,
|
|
331
|
+
signal: AbortSignal.timeout(30000),
|
|
332
|
+
});
|
|
333
|
+
if (response.status >= 300)
|
|
334
|
+
throw new Error(`Upload failed: HTTP ${response.status}`);
|
|
299
335
|
});
|
|
300
|
-
if (s3Res.status >= 300) {
|
|
301
|
-
throw new Error(`Upload failed: HTTP ${s3Res.status}`);
|
|
302
|
-
}
|
|
303
336
|
const title = opts.title ?? castleJson?.title ?? path.basename(path.resolve(projectDir));
|
|
304
337
|
const deckInput = { deckId, title };
|
|
305
338
|
// Omitted unless asked for, or the deck is new and needs one: updateDeckV2
|
|
@@ -327,7 +360,7 @@ export async function saveDeck(dir, opts = {}) {
|
|
|
327
360
|
// here can never overwrite their choice.
|
|
328
361
|
if (backgroundImageFileId)
|
|
329
362
|
cardInput.backgroundImageFileId = backgroundImageFileId;
|
|
330
|
-
const result = await api.updateCardAndDeckV2(deckInput, cardInput);
|
|
363
|
+
const result = await traceSaveStage('content_publish', () => api.updateCardAndDeckV2(deckInput, cardInput));
|
|
331
364
|
// Preserve any deck-owned config already in castle.json (notably the
|
|
332
365
|
// `editor` block); save-deck only owns the identity fields below.
|
|
333
366
|
const newCastleJson = {
|
|
@@ -343,7 +376,7 @@ export async function saveDeck(dir, opts = {}) {
|
|
|
343
376
|
else {
|
|
344
377
|
console.log(`Saved "${title}" to Castle.`);
|
|
345
378
|
}
|
|
346
|
-
await publishServerBundle(projectDir, result.deckId);
|
|
379
|
+
await traceSaveStage('server_publish', () => publishServerBundle(projectDir, result.deckId));
|
|
347
380
|
console.log('Uploading source archive...');
|
|
348
381
|
await uploadSource(projectDir, result.deckId);
|
|
349
382
|
console.log('Source archive saved.');
|