castle-web-cli 0.4.229 → 0.4.230
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/agent-prompts.d.ts +2 -0
- package/dist/agent-prompts.js +16 -8
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +21 -21
- package/dist/api.d.ts +28 -2
- package/dist/api.js +74 -16
- package/dist/castle-host/host.d.ts +91 -0
- package/dist/castle-host/host.js +779 -3
- package/dist/castleJson.d.ts +44 -0
- package/dist/castleJson.js +131 -0
- package/dist/check.js +27 -1
- package/dist/contentPack.d.ts +49 -0
- package/dist/contentPack.js +273 -0
- package/dist/devExperiments.d.ts +21 -0
- package/dist/devExperiments.js +139 -0
- package/dist/devHub.d.ts +55 -0
- package/dist/devHub.js +187 -0
- package/dist/devSessionChild.js +3 -1
- package/dist/devSessionServer.d.ts +14 -1
- package/dist/devSessionServer.js +229 -44
- package/dist/experimentAssignment.d.ts +15 -0
- package/dist/experimentAssignment.js +41 -0
- package/dist/importCheckHook.d.ts +40 -0
- package/dist/importCheckHook.js +171 -0
- package/dist/importRegression.d.ts +15 -0
- package/dist/importRegression.js +100 -0
- package/dist/importTrust.d.ts +2 -0
- package/dist/importTrust.js +27 -0
- package/dist/importUpdateHook.d.ts +18 -0
- package/dist/importUpdateHook.js +31 -8
- package/dist/importUpdateHookRunner.js +19 -7
- package/dist/imports.d.ts +16 -0
- package/dist/imports.js +141 -31
- package/dist/index.js +3 -1
- package/dist/init.d.ts +3 -0
- package/dist/init.js +125 -8
- package/dist/install.js +4 -1
- package/dist/localPaths.js +4 -0
- package/dist/login.js +2 -2
- package/dist/platformDoc.js +3 -8
- package/dist/playPagePolicy.js +3 -1
- package/dist/remix.js +5 -0
- package/dist/routerTaskReport.d.ts +28 -0
- package/dist/routerTaskReport.js +56 -0
- package/dist/save-deck.d.ts +18 -0
- package/dist/save-deck.js +142 -6
- package/dist/serve.d.ts +8 -0
- package/dist/serve.js +94 -13
- package/dist/serverBundle.d.ts +1 -1
- package/dist/serverBundle.js +5 -1
- package/dist/webSourceSync.d.ts +2 -0
- package/dist/webSourceSync.js +2 -0
- package/kits/base/CLAUDE.md +2 -3
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +258 -52
- package/kits/base/sdk/agent.js +4 -4
- package/kits/base/sdk/avatar.d.ts +12 -0
- package/kits/base/sdk/avatar.js +23 -0
- package/kits/base/sdk/castle.d.ts +5 -0
- package/kits/base/sdk/castle.js +3 -0
- package/kits/base/sdk/commands.d.ts +208 -0
- package/kits/base/sdk/commands.js +16 -0
- package/kits/base/sdk/context.d.ts +6 -0
- package/kits/base/sdk/experiments.d.ts +15 -0
- package/kits/base/sdk/experiments.js +83 -0
- package/kits/base/sdk/hub.d.ts +85 -0
- package/kits/base/sdk/hub.js +107 -0
- package/kits/base/sdk/multiplayer.d.ts +45 -1
- package/kits/base/sdk/multiplayer.js +106 -4
- package/kits/base/sdk/server/hub.d.ts +169 -0
- package/kits/base/sdk/server/hub.js +125 -0
- package/kits/base/sdk/server/platformHandle.d.ts +42 -1
- package/kits/base/sdk/server/storage.d.ts +55 -2
- package/kits/base/sdk/server/wrapper.d.ts +32 -2
- package/kits/base/sdk/server/wrapper.js +90 -11
- package/kits/base/sdk/transport.d.ts +2 -1
- package/kits/base/sdk/transport.js +13 -1
- package/kits/hub-game/.prettierrc +5 -0
- package/kits/hub-game/CLAUDE.md +146 -0
- package/kits/hub-game/blueprints/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/avatar-3.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
- package/kits/hub-game/castle.json +96 -0
- package/kits/hub-game/code/client/attention.js +133 -0
- package/kits/hub-game/code/client/avatar.js +288 -0
- package/kits/hub-game/code/client/cabinets.js +90 -0
- package/kits/hub-game/code/client/chat.js +135 -0
- package/kits/hub-game/code/client/chrome.js +1001 -0
- package/kits/hub-game/code/client/doorFrame.js +15 -0
- package/kits/hub-game/code/client/firstJoin.js +14 -0
- package/kits/hub-game/code/client/grab.js +241 -0
- package/kits/hub-game/code/client/hookLayer.js +230 -0
- package/kits/hub-game/code/client/idle.js +326 -0
- package/kits/hub-game/code/client/keys.js +48 -0
- package/kits/hub-game/code/client/pack.js +232 -0
- package/kits/hub-game/code/client/packFormat.js +397 -0
- package/kits/hub-game/code/client/say.js +127 -0
- package/kits/hub-game/code/client/sha256.js +80 -0
- package/kits/hub-game/code/client/sound.js +147 -0
- package/kits/hub-game/code/client/state.js +131 -0
- package/kits/hub-game/code/client/talk.js +259 -0
- package/kits/hub-game/code/client/together.js +365 -0
- package/kits/hub-game/code/client/wear.js +149 -0
- package/kits/hub-game/code/server/actors.js +627 -0
- package/kits/hub-game/code/server/blocks.js +103 -0
- package/kits/hub-game/code/server/cabinets.js +309 -0
- package/kits/hub-game/code/server/game.js +137 -0
- package/kits/hub-game/code/server/grab.js +247 -0
- package/kits/hub-game/code/server/history.js +96 -0
- package/kits/hub-game/code/server/hub.js +1904 -0
- package/kits/hub-game/code/server/idle.js +412 -0
- package/kits/hub-game/code/server/index.js +64 -0
- package/kits/hub-game/code/server/labels.js +59 -0
- package/kits/hub-game/code/server/manifest.js +527 -0
- package/kits/hub-game/code/server/npcs.js +745 -0
- package/kits/hub-game/code/server/party.js +381 -0
- package/kits/hub-game/code/server/place.js +43 -0
- package/kits/hub-game/code/server/say.js +83 -0
- package/kits/hub-game/code/server/wear.js +142 -0
- package/kits/hub-game/code/systems/hub.js +2230 -0
- package/kits/hub-game/docs/content-pack.md +151 -0
- package/kits/hub-game/docs/contract.md +1139 -0
- package/kits/hub-game/docs/style.md +137 -0
- package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
- package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
- package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
- package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
- package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
- package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
- package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
- package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
- package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
- package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
- package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
- package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
- package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
- package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
- package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
- package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
- package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
- package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
- package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
- package/kits/hub-game/eslint.config.js +20 -0
- package/kits/hub-game/fonts.generated.js +9 -0
- package/kits/hub-game/index.html +22 -0
- package/kits/hub-game/main.jsx +6 -0
- package/kits/hub-game/models/avatar-0.pxmodel +21 -0
- package/kits/hub-game/models/avatar-1.pxmodel +24 -0
- package/kits/hub-game/models/avatar-2.pxmodel +27 -0
- package/kits/hub-game/models/avatar-3.pxmodel +22 -0
- package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
- package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
- package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
- package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
- package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
- package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
- package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
- package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
- package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
- package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
- package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
- package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
- package/kits/hub-game/package-lock.json +365 -0
- package/kits/hub-game/package.json +32 -0
- package/kits/hub-game/scenes/main.scene +318 -0
- package/kits/hub-game/scripts/check.mjs +339 -0
- package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
- package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
- package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
- package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
- package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
- package/kits/hub-game/scripts/pack.mjs +192 -0
- package/kits/hub-game/scripts/test-actors.mjs +666 -0
- package/kits/hub-game/scripts/test-attention.mjs +103 -0
- package/kits/hub-game/scripts/test-avatar.mjs +472 -0
- package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
- package/kits/hub-game/scripts/test-blocks.mjs +351 -0
- package/kits/hub-game/scripts/test-chat.mjs +98 -0
- package/kits/hub-game/scripts/test-classic.mjs +590 -0
- package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
- package/kits/hub-game/scripts/test-door.mjs +56 -0
- package/kits/hub-game/scripts/test-grab.mjs +331 -0
- package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +1310 -0
- package/kits/hub-game/scripts/test-idle.mjs +518 -0
- package/kits/hub-game/scripts/test-interest.mjs +413 -0
- package/kits/hub-game/scripts/test-keys.mjs +50 -0
- package/kits/hub-game/scripts/test-manifest.mjs +224 -0
- package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
- package/kits/hub-game/scripts/test-npcs.mjs +714 -0
- package/kits/hub-game/scripts/test-pack.mjs +424 -0
- package/kits/hub-game/scripts/test-party.mjs +286 -0
- package/kits/hub-game/scripts/test-say.mjs +149 -0
- package/kits/hub-game/scripts/test-together.mjs +183 -0
- package/kits/hub-game/theme.style +3 -0
- package/kits/multiplayer-3d/castle.json +5 -5
- package/kits/multiplayer-3d/code/client/avatars.js +101 -1
- package/kits/multiplayer-3d/code/server/players.js +4 -1
- package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
- package/kits/multiplayer-3d/code/server/world.js +20 -7
- package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/engine/art.js +13 -0
- package/kits/physics-2d/engine/assets.js +4 -2
- package/kits/physics-2d/engine/scene.js +25 -5
- package/kits/physics-3d/CLAUDE.md +6 -1
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -4
- package/kits/physics-3d/engine3d/artobjects.js +2 -2
- package/kits/physics-3d/engine3d/materials.js +29 -1
- package/kits/physics-3d/engine3d/world3d.js +65 -1
- package/kits/physics-3d/systems/physics3d.js +5 -2
- package/kits/real-time/castle.json +2 -2
- package/kits/real-time/code/client/connection.js +77 -7
- package/kits/real-time/code/client/gameHooks.js +63 -3
- package/kits/real-time/code/client/messages.js +7 -0
- package/kits/real-time/code/server/gameHooks.js +24 -0
- package/kits/real-time/code/server/session.js +390 -22
- package/package.json +2 -2
package/dist/castleJson.d.ts
CHANGED
|
@@ -34,8 +34,37 @@ export interface CastleJson {
|
|
|
34
34
|
};
|
|
35
35
|
importWhenRemixed?: boolean;
|
|
36
36
|
starterScene?: string;
|
|
37
|
+
starterFiles?: string[];
|
|
38
|
+
starterConfig?: Record<string, unknown>;
|
|
39
|
+
onCheck?: string;
|
|
40
|
+
onPack?: string;
|
|
41
|
+
devDecks?: Record<string, string>;
|
|
42
|
+
experiments?: Record<string, ExperimentConfig>;
|
|
43
|
+
devExperiments?: Record<string, string>;
|
|
37
44
|
[key: string]: unknown;
|
|
38
45
|
}
|
|
46
|
+
export interface ExperimentConfig {
|
|
47
|
+
variants: string[];
|
|
48
|
+
weights?: number[];
|
|
49
|
+
winner?: string;
|
|
50
|
+
}
|
|
51
|
+
export declare const EXPERIMENTS_CAP = 8;
|
|
52
|
+
export declare const EXPERIMENT_VARIANTS_MIN = 2;
|
|
53
|
+
export declare const EXPERIMENT_VARIANTS_CAP = 8;
|
|
54
|
+
export declare const EXPERIMENT_NAME: RegExp;
|
|
55
|
+
export declare const PLATFORM_EXPERIMENT_NAME: RegExp;
|
|
56
|
+
/**
|
|
57
|
+
* castle.json `experiments`, checked: the block as Castle stores it, or null
|
|
58
|
+
* when the deck declares none. Throws on a block Castle would refuse, naming
|
|
59
|
+
* the experiment and the rule.
|
|
60
|
+
*/
|
|
61
|
+
export declare function validateExperiments(value: unknown): Record<string, ExperimentConfig> | null;
|
|
62
|
+
/**
|
|
63
|
+
* castle.json `devExperiments`, for `castle-web serve`: every `name: variant`
|
|
64
|
+
* pair that is one, with the rest reported to `log` and left out.
|
|
65
|
+
*/
|
|
66
|
+
export declare function isPlatformExperimentName(value: unknown): boolean;
|
|
67
|
+
export declare function readDevExperiments(value: unknown, log?: (text: string) => void): Record<string, string>;
|
|
39
68
|
export declare const DEFAULT_STARTER_SCENE = "scenes/main.scene";
|
|
40
69
|
export declare const DEFAULT_SERVER_TICK_RATE = 20;
|
|
41
70
|
export declare const DEFAULT_SERVER_MAX_PLAYERS = 8;
|
|
@@ -70,6 +99,21 @@ export interface ResolvedServerConfig {
|
|
|
70
99
|
voiceEnabled: boolean;
|
|
71
100
|
destinations?: ServerDestinationConfig[];
|
|
72
101
|
worldNotifications?: ServerWorldNotificationsConfig;
|
|
102
|
+
hub?: ServerHubConfig;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* What ghost keeps of castle.json `hub` (the hub contract's place manifest): each
|
|
106
|
+
* place's title and declared result shape. Ghost normalizes and validates it and
|
|
107
|
+
* pins it to the place's sessions, so a session's results are checked against
|
|
108
|
+
* what was published rather than what its code says. Every other field of the
|
|
109
|
+
* block is the kit's and stays out of the bundle config.
|
|
110
|
+
*/
|
|
111
|
+
export interface ServerHubPlaceConfig {
|
|
112
|
+
title?: unknown;
|
|
113
|
+
result?: unknown;
|
|
114
|
+
}
|
|
115
|
+
export interface ServerHubConfig extends ServerHubPlaceConfig {
|
|
116
|
+
places?: Record<string, ServerHubPlaceConfig>;
|
|
73
117
|
}
|
|
74
118
|
export declare function resolveServerConfig(dir: string): ResolvedServerConfig | null;
|
|
75
119
|
export declare function deckStarterScene(dir: string): string;
|
package/dist/castleJson.js
CHANGED
|
@@ -25,6 +25,110 @@ export const IMPORTS_DIR = 'imports';
|
|
|
25
25
|
// Defined here rather than in imports.ts so forks.ts can name it without a
|
|
26
26
|
// cycle.
|
|
27
27
|
export const IMPORT_VERSION_FILE = '.castle-import-version.json';
|
|
28
|
+
export const EXPERIMENTS_CAP = 8;
|
|
29
|
+
export const EXPERIMENT_VARIANTS_MIN = 2;
|
|
30
|
+
export const EXPERIMENT_VARIANTS_CAP = 8;
|
|
31
|
+
// Experiment names and variant names.
|
|
32
|
+
export const EXPERIMENT_NAME = /^[a-z0-9_]{1,32}$/;
|
|
33
|
+
// Names of an experiment Castle runs across decks, and of its variants.
|
|
34
|
+
export const PLATFORM_EXPERIMENT_NAME = /^[a-z0-9_]{1,64}$/;
|
|
35
|
+
// Fit the pattern, but read and write Object.prototype on a plain map, so
|
|
36
|
+
// Castle refuses them as an experiment name and as a variant name.
|
|
37
|
+
const RESERVED_EXPERIMENT_NAMES = ['__proto__', 'constructor'];
|
|
38
|
+
function isExperimentName(value, pattern = EXPERIMENT_NAME) {
|
|
39
|
+
return (typeof value === 'string' && pattern.test(value) && !RESERVED_EXPERIMENT_NAMES.includes(value));
|
|
40
|
+
}
|
|
41
|
+
const EXPERIMENT_NAME_RULE = 'must be 1 through 32 characters of a-z, 0-9 and _ (and not __proto__ or constructor)';
|
|
42
|
+
/**
|
|
43
|
+
* castle.json `experiments`, checked: the block as Castle stores it, or null
|
|
44
|
+
* when the deck declares none. Throws on a block Castle would refuse, naming
|
|
45
|
+
* the experiment and the rule.
|
|
46
|
+
*/
|
|
47
|
+
export function validateExperiments(value) {
|
|
48
|
+
if (value === undefined || value === null)
|
|
49
|
+
return null;
|
|
50
|
+
if (!isPlainObject(value)) {
|
|
51
|
+
throw new Error('experiments must be an object keyed by experiment name.');
|
|
52
|
+
}
|
|
53
|
+
const names = Object.keys(value);
|
|
54
|
+
if (names.length > EXPERIMENTS_CAP) {
|
|
55
|
+
throw new Error(`experiments must declare at most ${EXPERIMENTS_CAP} experiments.`);
|
|
56
|
+
}
|
|
57
|
+
return Object.fromEntries(names.map((name) => {
|
|
58
|
+
if (!isExperimentName(name)) {
|
|
59
|
+
throw new Error(`experiments: the name "${name}" ${EXPERIMENT_NAME_RULE}.`);
|
|
60
|
+
}
|
|
61
|
+
return [name, validateExperiment(name, value[name])];
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
function validateExperiment(name, value) {
|
|
65
|
+
if (!isPlainObject(value))
|
|
66
|
+
throw invalidExperiment(name, 'must be an object');
|
|
67
|
+
const unknown = Object.keys(value).find((key) => key !== 'variants' && key !== 'weights' && key !== 'winner');
|
|
68
|
+
if (unknown !== undefined) {
|
|
69
|
+
throw invalidExperiment(name, `has an unknown key "${unknown}" (use variants, weights, winner)`);
|
|
70
|
+
}
|
|
71
|
+
const variants = value.variants;
|
|
72
|
+
if (!Array.isArray(variants) ||
|
|
73
|
+
variants.length < EXPERIMENT_VARIANTS_MIN ||
|
|
74
|
+
variants.length > EXPERIMENT_VARIANTS_CAP) {
|
|
75
|
+
throw invalidExperiment(name, `variants must be an array of ${EXPERIMENT_VARIANTS_MIN} through ${EXPERIMENT_VARIANTS_CAP} names`);
|
|
76
|
+
}
|
|
77
|
+
for (const variant of variants) {
|
|
78
|
+
if (!isExperimentName(variant)) {
|
|
79
|
+
throw invalidExperiment(name, `variant ${JSON.stringify(variant)} ${EXPERIMENT_NAME_RULE}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (new Set(variants).size !== variants.length) {
|
|
83
|
+
throw invalidExperiment(name, 'variants must not contain duplicates');
|
|
84
|
+
}
|
|
85
|
+
const weights = value.weights;
|
|
86
|
+
if (weights !== undefined) {
|
|
87
|
+
if (!Array.isArray(weights) ||
|
|
88
|
+
weights.length !== variants.length ||
|
|
89
|
+
weights.some((weight) => !Number.isInteger(weight) || weight < 0)) {
|
|
90
|
+
throw invalidExperiment(name, 'weights must be an array of non-negative integers, one per variant');
|
|
91
|
+
}
|
|
92
|
+
const total = weights.reduce((sum, weight) => sum + weight, 0);
|
|
93
|
+
if (total !== 100)
|
|
94
|
+
throw invalidExperiment(name, `weights must sum to 100 (they sum to ${total})`);
|
|
95
|
+
}
|
|
96
|
+
const winner = typeof value.winner === 'string' ? value.winner : undefined;
|
|
97
|
+
if (value.winner !== undefined && (winner === undefined || !variants.includes(winner))) {
|
|
98
|
+
throw invalidExperiment(name, `winner ${JSON.stringify(value.winner)} must be one of its variants`);
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
variants: [...variants],
|
|
102
|
+
...(weights === undefined ? {} : { weights: [...weights] }),
|
|
103
|
+
...(winner === undefined ? {} : { winner }),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function invalidExperiment(name, message) {
|
|
107
|
+
return new Error(`experiments.${name} ${message}.`);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* castle.json `devExperiments`, for `castle-web serve`: every `name: variant`
|
|
111
|
+
* pair that is one, with the rest reported to `log` and left out.
|
|
112
|
+
*/
|
|
113
|
+
export function isPlatformExperimentName(value) {
|
|
114
|
+
return isExperimentName(value, PLATFORM_EXPERIMENT_NAME);
|
|
115
|
+
}
|
|
116
|
+
export function readDevExperiments(value, log) {
|
|
117
|
+
if (value === undefined)
|
|
118
|
+
return {};
|
|
119
|
+
if (!isPlainObject(value)) {
|
|
120
|
+
log?.('castle.json devExperiments must be an object of experiment name to variant');
|
|
121
|
+
return {};
|
|
122
|
+
}
|
|
123
|
+
return Object.fromEntries(Object.entries(value).filter((entry) => {
|
|
124
|
+
const [name, variant] = entry;
|
|
125
|
+
const valid = isExperimentName(name, PLATFORM_EXPERIMENT_NAME) &&
|
|
126
|
+
isExperimentName(variant, PLATFORM_EXPERIMENT_NAME);
|
|
127
|
+
if (!valid)
|
|
128
|
+
log?.(`castle.json devExperiments.${name} is not a variant name; it is ignored`);
|
|
129
|
+
return valid;
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
28
132
|
export const DEFAULT_STARTER_SCENE = 'scenes/main.scene';
|
|
29
133
|
export const DEFAULT_SERVER_TICK_RATE = 20;
|
|
30
134
|
export const DEFAULT_SERVER_MAX_PLAYERS = 8;
|
|
@@ -61,6 +165,30 @@ export function resolveServerConfig(dir) {
|
|
|
61
165
|
voiceEnabled: castleJson?.voice?.enabled !== false,
|
|
62
166
|
...(destinations === undefined ? {} : { destinations }),
|
|
63
167
|
...(worldNotifications === undefined ? {} : { worldNotifications }),
|
|
168
|
+
...(castleJson?.hub === undefined ? {} : { hub: serverHubConfig(castleJson.hub) }),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
// Never throws: `serve` and `check` resolve this too, and the hub block is
|
|
172
|
+
// checked by the kit at publish and by ghost when it is saved. A block that is
|
|
173
|
+
// not the shape reduced here goes to ghost as written, for ghost to refuse.
|
|
174
|
+
function serverHubConfig(value) {
|
|
175
|
+
if (!isPlainObject(value))
|
|
176
|
+
return value;
|
|
177
|
+
const hub = hubPlaceConfig(value);
|
|
178
|
+
if (value.places !== undefined) {
|
|
179
|
+
hub.places = isPlainObject(value.places)
|
|
180
|
+
? Object.fromEntries(Object.entries(value.places).map(([id, place]) => [
|
|
181
|
+
id,
|
|
182
|
+
isPlainObject(place) ? hubPlaceConfig(place) : place,
|
|
183
|
+
]))
|
|
184
|
+
: value.places;
|
|
185
|
+
}
|
|
186
|
+
return hub;
|
|
187
|
+
}
|
|
188
|
+
function hubPlaceConfig(place) {
|
|
189
|
+
return {
|
|
190
|
+
...(place.title === undefined ? {} : { title: place.title }),
|
|
191
|
+
...(place.result === undefined ? {} : { result: place.result }),
|
|
64
192
|
};
|
|
65
193
|
}
|
|
66
194
|
function validateServerDestinations(value) {
|
|
@@ -291,3 +419,6 @@ export function readCastleJsonOrThrow(dir) {
|
|
|
291
419
|
throw new Error(`Could not read ${file}: ${e instanceof Error ? e.message : String(e)}`);
|
|
292
420
|
});
|
|
293
421
|
}
|
|
422
|
+
function isPlainObject(value) {
|
|
423
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
424
|
+
}
|
package/dist/check.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import * as fs from 'fs';
|
|
2
2
|
import * as path from 'path';
|
|
3
3
|
import { bundleProject } from './bundle.js';
|
|
4
|
-
import { resolveServerConfig } from './castleJson.js';
|
|
4
|
+
import { readCastleJson, resolveServerConfig, validateExperiments } from './castleJson.js';
|
|
5
|
+
import { importChecksFailedMessage, runImportChecks } from './importCheckHook.js';
|
|
5
6
|
import { buildServerBundle } from './serverBundle.js';
|
|
7
|
+
import { buildContentPack } from './contentPack.js';
|
|
6
8
|
// `castle-web check` -- do this deck and its authoritative server still build?
|
|
7
9
|
//
|
|
8
10
|
// These are the gates that matter wherever a deck came from: they are the same
|
|
@@ -10,6 +12,11 @@ import { buildServerBundle } from './serverBundle.js';
|
|
|
10
12
|
// broken. A kit adds its own lint and duplicate gates on top through its own
|
|
11
13
|
// `check` script; a deck built on nothing has no lint config to run, and used to
|
|
12
14
|
// have no way to answer the question at all.
|
|
15
|
+
//
|
|
16
|
+
// After both builds, every import's `onCheck` (and the deck's own) runs: the
|
|
17
|
+
// rules a kit holds the decks built on it to, which no bundler can see. Those
|
|
18
|
+
// gate `save-deck` the same way (importCheckHook.ts). Then the content pack, when
|
|
19
|
+
// an import exports one (contentPack.ts).
|
|
13
20
|
export async function check(dir) {
|
|
14
21
|
const deckDir = path.resolve(dir);
|
|
15
22
|
if (!fs.existsSync(path.join(deckDir, 'index.html'))) {
|
|
@@ -25,6 +32,25 @@ export async function check(dir) {
|
|
|
25
32
|
throw new Error('Server entry disappeared while bundling.');
|
|
26
33
|
console.log('Server bundle OK.');
|
|
27
34
|
}
|
|
35
|
+
// The same check `save-deck` makes before it publishes anything.
|
|
36
|
+
try {
|
|
37
|
+
if (validateExperiments(readCastleJson(deckDir)?.experiments))
|
|
38
|
+
console.log('Experiments OK.');
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
throw new Error(`castle.json ${e instanceof Error ? e.message : String(e)}`);
|
|
42
|
+
}
|
|
43
|
+
const { outcomes, errors } = await runImportChecks(deckDir);
|
|
44
|
+
if (errors.length > 0)
|
|
45
|
+
throw new Error(importChecksFailedMessage(errors));
|
|
46
|
+
if (outcomes.length > 0) {
|
|
47
|
+
console.log(`Deck checks OK (${outcomes.map((o) => o.alias).join(', ')}).`);
|
|
48
|
+
}
|
|
49
|
+
// A deck that cannot pack would publish without its content, so it fails here.
|
|
50
|
+
const pack = await buildContentPack(deckDir);
|
|
51
|
+
if (pack) {
|
|
52
|
+
console.log(`Content pack OK (${pack.alias}): ${pack.summary ?? `${pack.bytes.byteLength} bytes`}, sha256 ${pack.sha256}.`);
|
|
53
|
+
}
|
|
28
54
|
}
|
|
29
55
|
catch (e) {
|
|
30
56
|
console.error(e instanceof Error ? e.message : String(e));
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** The most a pack may be, whatever its exporter allows. */
|
|
2
|
+
export declare const CONTENT_PACK_MAX_BYTES: number;
|
|
3
|
+
export interface ContentPack {
|
|
4
|
+
sha256: string;
|
|
5
|
+
bytes: Buffer;
|
|
6
|
+
/** Where it is stored, absolute. */
|
|
7
|
+
file: string;
|
|
8
|
+
/** The import that exported it. */
|
|
9
|
+
alias: string;
|
|
10
|
+
/** The exporter's one-line description, if it gave one. */
|
|
11
|
+
summary: string | null;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Build the deck's content pack, or null when no import exports one. Throws
|
|
15
|
+
* with the exporter's own errors when it refuses: a deck that cannot pack is a
|
|
16
|
+
* deck that does not build.
|
|
17
|
+
*/
|
|
18
|
+
export declare function buildContentPack(deckDir: string, log?: (message: string) => void): Promise<ContentPack | null>;
|
|
19
|
+
/** A pack this deck built before, by hash, or null. */
|
|
20
|
+
export declare function readStoredContentPack(deckDir: string, sha256: string): Buffer | null;
|
|
21
|
+
/** JSON with every object's keys sorted and no whitespace. */
|
|
22
|
+
export declare function canonicalJson(value: unknown): string;
|
|
23
|
+
/** The directory of a deck this deck lists in `devDecks`, or null. */
|
|
24
|
+
export declare function devDeckDir(projectDir: string, name: string): string | null;
|
|
25
|
+
/**
|
|
26
|
+
* The castle.json of every deck this deck lists in `devDecks`, by name: what a
|
|
27
|
+
* dev session's server is told about the decks its doors lead to, so a door
|
|
28
|
+
* can read the destination's own settings rather than repeat them. Decks that
|
|
29
|
+
* are missing or unreadable are left out.
|
|
30
|
+
*/
|
|
31
|
+
export declare function devDeckCastleJsons(projectDir: string): Record<string, unknown>;
|
|
32
|
+
/** `deck:<name>/<key>` -> the listed deck's directory and its own key, or null. */
|
|
33
|
+
export declare function devDeckSessionKey(projectDir: string, key: string | null): {
|
|
34
|
+
name: string;
|
|
35
|
+
dir: string;
|
|
36
|
+
key: string;
|
|
37
|
+
} | null;
|
|
38
|
+
/**
|
|
39
|
+
* The content-origin endpoints under `/__castle/content-pack/`. GET only; true
|
|
40
|
+
* when the request was one of them (answered), false to fall through.
|
|
41
|
+
*/
|
|
42
|
+
export declare function handleContentPackRequest(projectDir: string, req: {
|
|
43
|
+
method?: string;
|
|
44
|
+
url?: string;
|
|
45
|
+
}, res: {
|
|
46
|
+
writeHead(status: number, headers?: Record<string, string>): {
|
|
47
|
+
end(body?: string | Buffer): void;
|
|
48
|
+
};
|
|
49
|
+
}, reqPath: string): Promise<boolean>;
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// A deck's content pack: a data artifact an import exports about the deck built
|
|
2
|
+
// on it (castle.json `onPack`), for another deck's engine to mount by hash. The
|
|
3
|
+
// hub-game kit is the one that does today -- a hub game's client half as data
|
|
4
|
+
// -- but the CLI knows nothing of what a pack holds: it runs the import's
|
|
5
|
+
// exporter like an `onCheck`, then canonicalizes the JSON it wrote, hashes it,
|
|
6
|
+
// holds it to a size cap, stores it under `.castle/content-pack/<sha256>.json`
|
|
7
|
+
// and publishes it beside the server bundle.
|
|
8
|
+
//
|
|
9
|
+
// A pack's identity is the SHA-256 of its canonical bytes (object keys sorted,
|
|
10
|
+
// no whitespace), so the same content always has the same hash, whichever
|
|
11
|
+
// machine built it.
|
|
12
|
+
import { createHash, randomUUID } from 'crypto';
|
|
13
|
+
import * as fs from 'fs';
|
|
14
|
+
import * as path from 'path';
|
|
15
|
+
import { execHookModule, failureDetail, HOOK_RESULT_PREFIX, outputLines } from './importUpdateHook.js';
|
|
16
|
+
import { runnableImportHooks } from './importCheckHook.js';
|
|
17
|
+
import { readCastleJson } from './castleJson.js';
|
|
18
|
+
/** The most a pack may be, whatever its exporter allows. */
|
|
19
|
+
export const CONTENT_PACK_MAX_BYTES = 8 * 1024 * 1024;
|
|
20
|
+
const STORE_DIR = path.join('.castle', 'content-pack');
|
|
21
|
+
const STORE_KEEP = 12;
|
|
22
|
+
const SHA256 = /^[0-9a-f]{64}$/;
|
|
23
|
+
/**
|
|
24
|
+
* Build the deck's content pack, or null when no import exports one. Throws
|
|
25
|
+
* with the exporter's own errors when it refuses: a deck that cannot pack is a
|
|
26
|
+
* deck that does not build.
|
|
27
|
+
*/
|
|
28
|
+
export async function buildContentPack(deckDir, log = (message) => console.log(message)) {
|
|
29
|
+
const exporters = await runnableImportHooks(deckDir, 'onPack', log);
|
|
30
|
+
if (exporters.length === 0)
|
|
31
|
+
return null;
|
|
32
|
+
if (exporters.length > 1) {
|
|
33
|
+
throw new Error(`Content pack: more than one import exports one (${exporters.map((e) => e.target.alias).join(', ')}).`);
|
|
34
|
+
}
|
|
35
|
+
const { target, modulePath } = exporters[0];
|
|
36
|
+
const store = path.join(deckDir, STORE_DIR);
|
|
37
|
+
fs.mkdirSync(store, { recursive: true });
|
|
38
|
+
const outFile = path.join(store, `.out-${randomUUID()}.json`);
|
|
39
|
+
try {
|
|
40
|
+
const context = {
|
|
41
|
+
deckDir,
|
|
42
|
+
importDir: target.importDir,
|
|
43
|
+
alias: target.alias,
|
|
44
|
+
self: target.pin === null,
|
|
45
|
+
outFile,
|
|
46
|
+
};
|
|
47
|
+
let output;
|
|
48
|
+
try {
|
|
49
|
+
output = await execHookModule(modulePath, context, deckDir, { reportResult: true });
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
throw new Error(`Content pack: ${target.alias}'s exporter failed: ${failureDetail(e)}`);
|
|
53
|
+
}
|
|
54
|
+
const verdict = parseVerdict(output.stdout);
|
|
55
|
+
if (verdict.errors.length > 0) {
|
|
56
|
+
throw new Error([`Content pack refused by ${target.alias}:`, ...verdict.errors.map((e) => ` - ${e}`)].join('\n'));
|
|
57
|
+
}
|
|
58
|
+
let document;
|
|
59
|
+
try {
|
|
60
|
+
document = JSON.parse(fs.readFileSync(outFile, 'utf8'));
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
throw new Error(`Content pack: ${target.alias}'s exporter wrote no JSON pack.`);
|
|
64
|
+
}
|
|
65
|
+
const shape = document;
|
|
66
|
+
if (!shape || typeof shape !== 'object' || typeof shape.format !== 'string' || !Number.isInteger(shape.version)) {
|
|
67
|
+
throw new Error(`Content pack: ${target.alias}'s pack has no { format, version }.`);
|
|
68
|
+
}
|
|
69
|
+
const bytes = Buffer.from(canonicalJson(document), 'utf8');
|
|
70
|
+
if (bytes.byteLength > CONTENT_PACK_MAX_BYTES) {
|
|
71
|
+
throw new Error(`Content pack is ${(bytes.byteLength / 1048576).toFixed(1)} MB, over the ${CONTENT_PACK_MAX_BYTES / 1048576} MB cap.`);
|
|
72
|
+
}
|
|
73
|
+
const sha256 = createHash('sha256').update(bytes).digest('hex');
|
|
74
|
+
const file = path.join(store, `${sha256}.json`);
|
|
75
|
+
if (!fs.existsSync(file))
|
|
76
|
+
fs.writeFileSync(file, bytes);
|
|
77
|
+
else
|
|
78
|
+
fs.utimesSync(file, new Date(), new Date());
|
|
79
|
+
pruneStore(store);
|
|
80
|
+
return { sha256, bytes, file, alias: target.alias, summary: verdict.summary };
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
fs.rmSync(outFile, { force: true });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** A pack this deck built before, by hash, or null. */
|
|
87
|
+
export function readStoredContentPack(deckDir, sha256) {
|
|
88
|
+
if (!SHA256.test(sha256))
|
|
89
|
+
return null;
|
|
90
|
+
try {
|
|
91
|
+
return fs.readFileSync(path.join(deckDir, STORE_DIR, `${sha256}.json`));
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/** JSON with every object's keys sorted and no whitespace. */
|
|
98
|
+
export function canonicalJson(value) {
|
|
99
|
+
return JSON.stringify(value, (_key, v) => {
|
|
100
|
+
if (v && typeof v === 'object' && !Array.isArray(v)) {
|
|
101
|
+
const sorted = {};
|
|
102
|
+
for (const key of Object.keys(v).sort()) {
|
|
103
|
+
sorted[key] = v[key];
|
|
104
|
+
}
|
|
105
|
+
return sorted;
|
|
106
|
+
}
|
|
107
|
+
return v;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function parseVerdict(stdout) {
|
|
111
|
+
const line = outputLines(stdout)
|
|
112
|
+
.reverse()
|
|
113
|
+
.find((l) => l.startsWith(HOOK_RESULT_PREFIX));
|
|
114
|
+
let result;
|
|
115
|
+
try {
|
|
116
|
+
result = line ? JSON.parse(line.slice(HOOK_RESULT_PREFIX.length)) : null;
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
result = null;
|
|
120
|
+
}
|
|
121
|
+
const errors = result?.errors;
|
|
122
|
+
if (!result || !Array.isArray(errors) || errors.some((e) => typeof e !== 'string')) {
|
|
123
|
+
return { errors: ['its exporter did not return { errors: string[] }.'], summary: null };
|
|
124
|
+
}
|
|
125
|
+
return { errors: errors, summary: typeof result.summary === 'string' ? result.summary : null };
|
|
126
|
+
}
|
|
127
|
+
// The newest packs stay; a deck republished many times does not grow without end.
|
|
128
|
+
function pruneStore(store) {
|
|
129
|
+
const packs = fs
|
|
130
|
+
.readdirSync(store)
|
|
131
|
+
.filter((name) => /^[0-9a-f]{64}\.json$/.test(name))
|
|
132
|
+
.map((name) => ({ name, at: fs.statSync(path.join(store, name)).mtimeMs }))
|
|
133
|
+
.sort((a, b) => b.at - a.at);
|
|
134
|
+
for (const { name } of packs.slice(STORE_KEEP))
|
|
135
|
+
fs.rmSync(path.join(store, name), { force: true });
|
|
136
|
+
}
|
|
137
|
+
// --- On `castle-web serve`: other local decks' packs, for doors to them in dev ---
|
|
138
|
+
//
|
|
139
|
+
// A deck opts the decks beside it in by name: castle.json `devDecks`
|
|
140
|
+
// (`{ "dash": "../hub-dash" }`). A page can then ask the serve to resolve one
|
|
141
|
+
// (`GET /__castle/content-pack/resolve?deck=dash`): the serve builds that deck's
|
|
142
|
+
// pack with that deck's own exporter, and answers with its hash, where to fetch
|
|
143
|
+
// it, and the session key prefix that runs that deck's server
|
|
144
|
+
// (`deck:dash/<key>`, devSessionServer.ts) -- the shape a production host gets
|
|
145
|
+
// from Ghost, with the serve standing in. Only named decks are reachable, so a
|
|
146
|
+
// page cannot make the serve build or run anything its author did not list.
|
|
147
|
+
const DEV_DECK_NAME = /^[A-Za-z0-9_-]{1,40}$/;
|
|
148
|
+
const DEV_DECK_KEY = /^deck:([A-Za-z0-9_-]{1,40})\/(.+)$/;
|
|
149
|
+
/** The directory of a deck this deck lists in `devDecks`, or null. */
|
|
150
|
+
export function devDeckDir(projectDir, name) {
|
|
151
|
+
if (!DEV_DECK_NAME.test(name))
|
|
152
|
+
return null;
|
|
153
|
+
const declared = readCastleJson(projectDir)?.devDecks?.[name];
|
|
154
|
+
if (typeof declared !== 'string' || !declared.trim())
|
|
155
|
+
return null;
|
|
156
|
+
const dir = path.resolve(projectDir, declared);
|
|
157
|
+
return fs.existsSync(path.join(dir, 'castle.json')) && dir !== path.resolve(projectDir) ? dir : null;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The castle.json of every deck this deck lists in `devDecks`, by name: what a
|
|
161
|
+
* dev session's server is told about the decks its doors lead to, so a door
|
|
162
|
+
* can read the destination's own settings rather than repeat them. Decks that
|
|
163
|
+
* are missing or unreadable are left out.
|
|
164
|
+
*/
|
|
165
|
+
export function devDeckCastleJsons(projectDir) {
|
|
166
|
+
const declared = readCastleJson(projectDir)?.devDecks;
|
|
167
|
+
const out = {};
|
|
168
|
+
if (!declared || typeof declared !== 'object')
|
|
169
|
+
return out;
|
|
170
|
+
for (const name of Object.keys(declared)) {
|
|
171
|
+
const dir = devDeckDir(projectDir, name);
|
|
172
|
+
const json = dir ? readCastleJson(dir) : null;
|
|
173
|
+
if (json)
|
|
174
|
+
out[name] = json;
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
/** `deck:<name>/<key>` -> the listed deck's directory and its own key, or null. */
|
|
179
|
+
export function devDeckSessionKey(projectDir, key) {
|
|
180
|
+
const match = key ? DEV_DECK_KEY.exec(key) : null;
|
|
181
|
+
const dir = match ? devDeckDir(projectDir, match[1]) : null;
|
|
182
|
+
return match && dir ? { name: match[1], dir, key: match[2] } : null;
|
|
183
|
+
}
|
|
184
|
+
// Packs this serve has handed out, by hash -> bytes, so a fetch reads only a
|
|
185
|
+
// pack the serve built, and a pinned one survives the store being pruned. A
|
|
186
|
+
// serve hands out few (one per destination edit), and the oldest go first.
|
|
187
|
+
const servedPacks = new Map();
|
|
188
|
+
const SERVED_PACKS_MAX = 32;
|
|
189
|
+
// The pack each destination session was first resolved with
|
|
190
|
+
// (`<deck>/<session key>` -> hash): a late joiner of a running instance gets
|
|
191
|
+
// the content that instance started with, not whatever is on disk now, the
|
|
192
|
+
// way a production session keeps its release.
|
|
193
|
+
const sessionPacks = new Map();
|
|
194
|
+
const SESSION_PACKS_MAX = 500;
|
|
195
|
+
// Drop the oldest packs no session is pinned to, down to the cap. A pinned
|
|
196
|
+
// one stays for as long as its pin does (SESSION_PACKS_MAX of them), and so
|
|
197
|
+
// does `current`, the pack about to be handed out.
|
|
198
|
+
function evictUnpinned(current) {
|
|
199
|
+
const pinned = new Set([...sessionPacks.values(), current]);
|
|
200
|
+
for (const sha of servedPacks.keys()) {
|
|
201
|
+
if (servedPacks.size <= SERVED_PACKS_MAX)
|
|
202
|
+
return;
|
|
203
|
+
if (!pinned.has(sha))
|
|
204
|
+
servedPacks.delete(sha);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* The content-origin endpoints under `/__castle/content-pack/`. GET only; true
|
|
209
|
+
* when the request was one of them (answered), false to fall through.
|
|
210
|
+
*/
|
|
211
|
+
export async function handleContentPackRequest(projectDir, req, res, reqPath) {
|
|
212
|
+
if (!reqPath.startsWith('/__castle/content-pack/'))
|
|
213
|
+
return false;
|
|
214
|
+
const json = (status, body) => res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' }).end(JSON.stringify(body));
|
|
215
|
+
if (req.method !== 'GET') {
|
|
216
|
+
res.writeHead(405).end();
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
const file = /^\/__castle\/content-pack\/([0-9a-f]{64})\.json$/.exec(reqPath);
|
|
220
|
+
if (file) {
|
|
221
|
+
const bytes = servedPacks.get(file[1]) ?? null;
|
|
222
|
+
if (!bytes) {
|
|
223
|
+
res.writeHead(404).end();
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
res
|
|
227
|
+
.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'public, max-age=31536000, immutable' })
|
|
228
|
+
.end(bytes);
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
if (reqPath === '/__castle/content-pack/resolve') {
|
|
232
|
+
const params = new URL(req.url ?? '', 'http://x').searchParams;
|
|
233
|
+
const name = params.get('deck') ?? '';
|
|
234
|
+
const sessionKey = `${name}/${params.get('key') ?? ''}`;
|
|
235
|
+
const dir = devDeckDir(projectDir, name);
|
|
236
|
+
if (!dir) {
|
|
237
|
+
json(404, { status: 'not_found', error: `this deck's castle.json lists no devDecks.${name}` });
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
try {
|
|
241
|
+
let sha256 = params.get('key') ? sessionPacks.get(sessionKey) : undefined;
|
|
242
|
+
if (!sha256 || !servedPacks.has(sha256)) {
|
|
243
|
+
const pack = await buildContentPack(dir, (line) => console.log(`[content-pack ${name}] ${line}`));
|
|
244
|
+
if (!pack) {
|
|
245
|
+
json(409, { status: 'unavailable', error: `${name} has no content pack exporter` });
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
servedPacks.delete(pack.sha256);
|
|
249
|
+
servedPacks.set(pack.sha256, pack.bytes);
|
|
250
|
+
sha256 = pack.sha256;
|
|
251
|
+
if (params.get('key')) {
|
|
252
|
+
sessionPacks.set(sessionKey, sha256);
|
|
253
|
+
if (sessionPacks.size > SESSION_PACKS_MAX)
|
|
254
|
+
sessionPacks.delete(sessionPacks.keys().next().value);
|
|
255
|
+
}
|
|
256
|
+
evictUnpinned(sha256);
|
|
257
|
+
}
|
|
258
|
+
json(200, {
|
|
259
|
+
status: 'ok',
|
|
260
|
+
sha256,
|
|
261
|
+
url: `/__castle/content-pack/${sha256}.json`,
|
|
262
|
+
sessionKeyPrefix: `deck:${name}/`,
|
|
263
|
+
deckId: readCastleJson(dir)?.deckId ?? null,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
catch (e) {
|
|
267
|
+
json(422, { status: 'refused', error: e instanceof Error ? e.message : String(e) });
|
|
268
|
+
}
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
res.writeHead(404).end();
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare class DevExperiments {
|
|
2
|
+
private readonly projectDir;
|
|
3
|
+
private readonly log;
|
|
4
|
+
private readonly overrides;
|
|
5
|
+
private readonly pages;
|
|
6
|
+
private lastWarning;
|
|
7
|
+
constructor(projectDir: string, log?: (text: string) => void);
|
|
8
|
+
/** Takes the `experiment.<name>` overrides out of a requested URL. */
|
|
9
|
+
noteUrl(url: string | undefined): void;
|
|
10
|
+
/** `{ name: variant }` for one player: what a host hands the deck and its server. */
|
|
11
|
+
variantsFor(userId: string, deckId: string | null): Record<string, string>;
|
|
12
|
+
/**
|
|
13
|
+
* The arms of one page load, resolved the first time the page asks anything
|
|
14
|
+
* and the same from then on: the deck's SDK keeps its first answer, so the
|
|
15
|
+
* map handed to the deck's server at a later join has to be that one too. An
|
|
16
|
+
* override taken afterwards shows on the next page load. `page` is null for
|
|
17
|
+
* a caller that names none, which is resolved afresh.
|
|
18
|
+
*/
|
|
19
|
+
variantsForPage(page: string | null, userId: string, deckId: string | null): Record<string, string>;
|
|
20
|
+
private read;
|
|
21
|
+
}
|