castle-web-cli 0.4.149 → 0.4.150
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/api.d.ts +14 -0
- package/dist/api.js +18 -0
- package/dist/castle-host/host.d.ts +17 -0
- package/dist/castle-host/host.js +10 -7
- package/dist/castleJson.d.ts +15 -0
- package/dist/castleJson.js +36 -0
- package/dist/devSessionChild.d.ts +1 -0
- package/dist/devSessionChild.js +129 -0
- package/dist/devSessionServer.d.ts +19 -0
- package/dist/devSessionServer.js +511 -0
- package/dist/index.js +8 -8
- package/dist/init.js +9 -1
- package/dist/list-decks.js +7 -6
- package/dist/preview.d.ts +1 -1
- package/dist/preview.js +4 -2
- package/dist/save-deck.js +63 -14
- package/dist/serve.js +220 -85
- package/dist/serveSecurity.d.ts +5 -1
- package/dist/serveSecurity.js +48 -10
- package/dist/serverBundle.d.ts +8 -0
- package/dist/serverBundle.js +82 -0
- package/dist/shell/assets/{index-BzFzFtEP.js → index-Dlhc3eZW.js} +42 -42
- package/dist/shell/assets/index-DwezawBs.css +1 -0
- package/dist/shell/index.html +2 -2
- package/kits/blank/.castle/agent/children.json +1 -0
- package/kits/blank/.castle/agent/messages.json +9 -0
- package/kits/blank/.castle/agent/pending-sends.json +1 -0
- package/kits/blank/.castle/agent/tasks/index.md +2 -0
- package/kits/blank/.castle/docs/platform.md +36 -0
- package/kits/blank/castle.json +5 -2
- package/kits/blank/node_modules/.vite/deps/_metadata.json +8 -0
- package/kits/blank/node_modules/.vite/deps/package.json +3 -0
- package/kits/physics-2d/behaviors/Collider.jsx +1 -2
- package/kits/physics-2d/behaviors/Joints.jsx +1 -1
- package/kits/physics-2d/castle.json +1 -1
- package/kits/physics-2d/editors/BlueprintLibrary.jsx +33 -3
- package/kits/physics-2d/editors/SceneEditor.jsx +171 -57
- package/kits/physics-2d/editors/SingleEditor.jsx +6 -0
- package/kits/physics-2d/engine/blueprint.js +106 -14
- package/kits/physics-2d/engine/scene.js +9 -2
- package/kits/physics-2d/engine/ui.jsx +30 -2
- package/kits/physics-2d/engine/ui.module.css +59 -0
- package/package.json +1 -1
- package/dist/shell/assets/index-DN8HQBnW.css +0 -1
package/dist/serveSecurity.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { createHmac, randomBytes, timingSafeEqual } from 'crypto';
|
|
2
|
+
export const SERVE_AUTH_FAILURE_HEADER = 'x-castle-serve-auth';
|
|
3
|
+
export const SERVE_AUTH_FAILURE_VALUE = 'invalid';
|
|
2
4
|
const CROCKFORD = '0123456789abcdefghjkmnpqrstvwxyz';
|
|
3
5
|
const CONTENT_LABEL_RE = /^1([ep])([0-9a-z]{1,4})-([a-z0-9]{14})-([0-9a-z]{1,8})-([0-9a-hjkmnp-tv-z]{13})-([0-9a-hjkmnp-tv-z]{20})$/;
|
|
4
6
|
const MAX_GRANT_SECONDS = 12 * 60 * 60;
|
|
@@ -50,23 +52,35 @@ export function mintContentGrantLabel(grant, signingKey) {
|
|
|
50
52
|
}
|
|
51
53
|
export class ServeSecurity {
|
|
52
54
|
publicPort;
|
|
53
|
-
localContentPorts;
|
|
54
55
|
serveToken = randomBytes(32).toString('base64url');
|
|
55
56
|
contentProxyToken = randomBytes(32).toString('base64url');
|
|
56
57
|
signingKey = process.env.CASTLE_AGENT_TOKEN || randomBytes(32).toString('base64url');
|
|
57
58
|
serveNonce = base32Lower(randomBytes(8));
|
|
58
59
|
tickets = new Map();
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
play: publicPort + 3,
|
|
62
|
-
}) {
|
|
60
|
+
localContentPorts;
|
|
61
|
+
constructor(publicPort, localContentPorts) {
|
|
63
62
|
this.publicPort = publicPort;
|
|
64
|
-
this.localContentPorts = localContentPorts;
|
|
63
|
+
this.localContentPorts = localContentPorts ?? null;
|
|
64
|
+
}
|
|
65
|
+
setLocalContentPorts(ports) {
|
|
66
|
+
if (this.localContentPorts)
|
|
67
|
+
throw new Error('local content ports are already configured');
|
|
68
|
+
if (ports.edit === ports.play)
|
|
69
|
+
throw new Error('edit and play content ports must be distinct');
|
|
70
|
+
this.localContentPorts = ports;
|
|
65
71
|
}
|
|
66
72
|
authorizeHttp(req) {
|
|
67
73
|
const value = req.headers['x-castle-serve-token'];
|
|
68
74
|
return typeof value === 'string' && safeEqual(value, this.serveToken);
|
|
69
75
|
}
|
|
76
|
+
rejectHttpAuth(res) {
|
|
77
|
+
res
|
|
78
|
+
.writeHead(403, {
|
|
79
|
+
[SERVE_AUTH_FAILURE_HEADER]: SERVE_AUTH_FAILURE_VALUE,
|
|
80
|
+
'cache-control': 'no-store',
|
|
81
|
+
})
|
|
82
|
+
.end();
|
|
83
|
+
}
|
|
70
84
|
issueTicket(endpoint) {
|
|
71
85
|
this.pruneTickets();
|
|
72
86
|
const ticket = randomBytes(16).toString('base64url');
|
|
@@ -119,6 +133,25 @@ window.__CASTLE_SERVE_TOKEN__=${JSON.stringify(this.serveToken)};
|
|
|
119
133
|
window.__CASTLE_CONTENT_ORIGINS__=${JSON.stringify({ edit: editOrigin, play: playOrigin })};
|
|
120
134
|
window.__CASTLE_HOST_ORIGIN__=${JSON.stringify(hostOrigin)};
|
|
121
135
|
var nativeFetch=window.fetch.bind(window);
|
|
136
|
+
var authReloadKey='castle:serve-auth-reload';
|
|
137
|
+
var authReloadScheduled=false;
|
|
138
|
+
var authReloadStorage=true;
|
|
139
|
+
try {
|
|
140
|
+
var previousAuthReload=sessionStorage.getItem(authReloadKey);
|
|
141
|
+
if(previousAuthReload&&previousAuthReload!==window.__CASTLE_SERVE_TOKEN__){
|
|
142
|
+
sessionStorage.removeItem(authReloadKey);
|
|
143
|
+
}
|
|
144
|
+
}catch(e){authReloadStorage=false;}
|
|
145
|
+
function recoverStaleServeAuth(response){
|
|
146
|
+
if(response.headers.get(${JSON.stringify(SERVE_AUTH_FAILURE_HEADER)})!==${JSON.stringify(SERVE_AUTH_FAILURE_VALUE)})return;
|
|
147
|
+
if(authReloadScheduled||!authReloadStorage)return;
|
|
148
|
+
try {
|
|
149
|
+
if(sessionStorage.getItem(authReloadKey)===window.__CASTLE_SERVE_TOKEN__)return;
|
|
150
|
+
sessionStorage.setItem(authReloadKey,window.__CASTLE_SERVE_TOKEN__);
|
|
151
|
+
}catch(e){authReloadStorage=false;return;}
|
|
152
|
+
authReloadScheduled=true;
|
|
153
|
+
window.location.reload();
|
|
154
|
+
}
|
|
122
155
|
window.fetch=function(input,init){
|
|
123
156
|
try {
|
|
124
157
|
var u=new URL(typeof input==='string'?input:input.url,location.href);
|
|
@@ -129,7 +162,10 @@ window.fetch=function(input,init){
|
|
|
129
162
|
init.headers=h;
|
|
130
163
|
}
|
|
131
164
|
}catch(e){}
|
|
132
|
-
return nativeFetch(input,init)
|
|
165
|
+
return nativeFetch(input,init).then(function(response){
|
|
166
|
+
recoverStaleServeAuth(response);
|
|
167
|
+
return response;
|
|
168
|
+
});
|
|
133
169
|
};
|
|
134
170
|
})();</script>`;
|
|
135
171
|
}
|
|
@@ -168,7 +204,10 @@ window.fetch=function(input,init){
|
|
|
168
204
|
return { editOrigin: originFor('edit'), playOrigin: originFor('play') };
|
|
169
205
|
}
|
|
170
206
|
localContentOrigin(role) {
|
|
171
|
-
|
|
207
|
+
const port = this.localContentPorts?.[role];
|
|
208
|
+
if (!port)
|
|
209
|
+
throw new Error('local content ports are not configured');
|
|
210
|
+
return `http://127.0.0.1:${port}`;
|
|
172
211
|
}
|
|
173
212
|
localContentProxyHeaders(role) {
|
|
174
213
|
return {
|
|
@@ -231,8 +270,7 @@ window.fetch=function(input,init){
|
|
|
231
270
|
if (typeof referer === 'string') {
|
|
232
271
|
try {
|
|
233
272
|
const referrerUrl = new URL(referer);
|
|
234
|
-
if (referrerUrl.origin === requestOrigin(req) &&
|
|
235
|
-
referrerUrl.pathname === '/index.html') {
|
|
273
|
+
if (referrerUrl.origin === requestOrigin(req) && referrerUrl.pathname === '/index.html') {
|
|
236
274
|
return referrerUrl.searchParams.get('edit') === '0' ? 'play' : 'edit';
|
|
237
275
|
}
|
|
238
276
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ResolvedServerConfig } from './castleJson.js';
|
|
2
|
+
export interface ServerBundle {
|
|
3
|
+
code: string;
|
|
4
|
+
sha256: string;
|
|
5
|
+
sizeBytes: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function buildServerBundle(projectDir: string): Promise<ServerBundle | null>;
|
|
8
|
+
export declare function bundleServerModule(projectDir: string, config: ResolvedServerConfig, minify: boolean): Promise<string>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
import { createRequire } from 'module';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { build } from 'vite';
|
|
5
|
+
import { resolveServerConfig, } from './castleJson.js';
|
|
6
|
+
import { forksOverlayPlugin, importsAliasPlugin } from './vitePlugins.js';
|
|
7
|
+
const WARN_BYTES = 1024 * 1024;
|
|
8
|
+
const MAX_BYTES = 5 * 1024 * 1024;
|
|
9
|
+
const VIRTUAL_ENTRY = '\0castle-server-entry';
|
|
10
|
+
// Build the artifact Cauldron executes. A deck without a valid server entry is
|
|
11
|
+
// not an error: multiplayer is opt-in, so callers can handle that as a skip (or
|
|
12
|
+
// clear a bundle they know they published earlier).
|
|
13
|
+
export async function buildServerBundle(projectDir) {
|
|
14
|
+
const config = resolveServerConfig(projectDir);
|
|
15
|
+
if (!config)
|
|
16
|
+
return null;
|
|
17
|
+
const code = await bundleServerModule(projectDir, config, true);
|
|
18
|
+
const sizeBytes = Buffer.byteLength(code, 'utf8');
|
|
19
|
+
if (sizeBytes > MAX_BYTES) {
|
|
20
|
+
throw new Error(`Server bundle is ${(sizeBytes / 1024 / 1024).toFixed(2)}MB; the maximum is 5MB.`);
|
|
21
|
+
}
|
|
22
|
+
if (sizeBytes > WARN_BYTES) {
|
|
23
|
+
console.warn(`Warning: Server bundle is ${(sizeBytes / 1024 / 1024).toFixed(2)}MB. ` +
|
|
24
|
+
'Large server bundles take longer to start.');
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
code,
|
|
28
|
+
sha256: createHash('sha256').update(code).digest('hex'),
|
|
29
|
+
sizeBytes,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
// Dev and publish deliberately share the synthesized entry and Vite semantics.
|
|
33
|
+
// Dev keeps readable code; the published artifact is minified and checked below
|
|
34
|
+
// for the single-output-file contract.
|
|
35
|
+
export async function bundleServerModule(projectDir, config, minify) {
|
|
36
|
+
const sdkEntry = createRequire(path.join(projectDir, 'package.json')).resolve('castle-web-sdk/server');
|
|
37
|
+
const authorEntry = path.resolve(projectDir, config.entry);
|
|
38
|
+
const entryPlugin = {
|
|
39
|
+
name: 'castle-server-entry',
|
|
40
|
+
resolveId(id) {
|
|
41
|
+
return id === VIRTUAL_ENTRY ? VIRTUAL_ENTRY : null;
|
|
42
|
+
},
|
|
43
|
+
load(id) {
|
|
44
|
+
if (id !== VIRTUAL_ENTRY)
|
|
45
|
+
return null;
|
|
46
|
+
return (`import callbacks from ${JSON.stringify(authorEntry)};\n` +
|
|
47
|
+
`import { createCastleSessionBoot } from ${JSON.stringify(sdkEntry)};\n` +
|
|
48
|
+
'export const __castleBootSession = createCastleSessionBoot(callbacks);\n');
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
const result = await build({
|
|
52
|
+
root: projectDir,
|
|
53
|
+
configFile: false,
|
|
54
|
+
logLevel: 'silent',
|
|
55
|
+
plugins: [forksOverlayPlugin(), importsAliasPlugin(), entryPlugin],
|
|
56
|
+
ssr: { noExternal: true },
|
|
57
|
+
build: {
|
|
58
|
+
write: false,
|
|
59
|
+
ssr: true,
|
|
60
|
+
target: 'node20',
|
|
61
|
+
minify,
|
|
62
|
+
sourcemap: false,
|
|
63
|
+
copyPublicDir: false,
|
|
64
|
+
assetsInlineLimit: Number.MAX_SAFE_INTEGER,
|
|
65
|
+
rollupOptions: {
|
|
66
|
+
input: VIRTUAL_ENTRY,
|
|
67
|
+
output: { format: 'es', inlineDynamicImports: true },
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
const output = Array.isArray(result) ? result[0] : result;
|
|
72
|
+
if (!('output' in output))
|
|
73
|
+
throw new Error('unexpected server build result');
|
|
74
|
+
const chunk = output.output.find((item) => item.type === 'chunk' && item.isEntry);
|
|
75
|
+
if (!chunk || chunk.type !== 'chunk')
|
|
76
|
+
throw new Error('server bundle produced no entry chunk');
|
|
77
|
+
if (output.output.length !== 1) {
|
|
78
|
+
const extras = output.output.filter((item) => item !== chunk).map((item) => item.fileName);
|
|
79
|
+
throw new Error(`server bundle produced additional files: ${extras.join(', ')}`);
|
|
80
|
+
}
|
|
81
|
+
return chunk.code;
|
|
82
|
+
}
|