castle-web-cli 0.4.236 → 0.4.238
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 +2 -0
- package/dist/bundle.d.ts +24 -0
- package/dist/bundle.js +447 -4
- package/dist/bundleLatin1.d.ts +6 -0
- package/dist/bundleLatin1.js +333 -0
- package/dist/castle-host/host.d.ts +10 -1
- package/dist/castle-host/host.js +78 -9
- package/dist/castleJson.d.ts +20 -0
- package/dist/castleJson.js +40 -0
- package/dist/deckExports.d.ts +35 -0
- package/dist/deckExports.js +365 -0
- package/dist/importCheckHook.d.ts +1 -0
- package/dist/importCheckHook.js +1 -1
- package/dist/index.js +5 -0
- package/dist/init.js +2 -1
- package/dist/save-deck.js +18 -13
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +47 -0
- package/kits/base/sdk/castle.d.ts +3 -1
- package/kits/base/sdk/castle.js +1 -0
- package/kits/base/sdk/commands.d.ts +25 -1
- package/kits/base/sdk/commands.js +29 -1
- package/kits/base/sdk/hub.d.ts +17 -0
- package/kits/base/sdk/hub.js +33 -9
- package/kits/base/sdk/localCache.d.ts +27 -0
- package/kits/base/sdk/localCache.js +115 -0
- package/kits/base/sdk/localCacheStore.d.ts +15 -0
- package/kits/base/sdk/localCacheStore.js +358 -0
- package/kits/base/sdk/transport.d.ts +1 -0
- package/kits/base/sdk/transport.js +4 -5
- package/kits/hub-game/CLAUDE.md +22 -2
- package/kits/hub-game/castle.json +4 -1
- package/kits/hub-game/code/client/act.js +66 -91
- package/kits/hub-game/code/client/actCore.js +134 -0
- package/kits/hub-game/code/client/car.js +35 -0
- package/kits/hub-game/code/client/chrome.js +207 -381
- package/kits/hub-game/code/client/crowdNames.js +2 -2
- package/kits/hub-game/code/client/emoteWheel.js +109 -0
- package/kits/hub-game/code/client/hats.js +2 -0
- package/kits/hub-game/code/client/hookLayer.js +99 -48
- package/kits/hub-game/code/client/hostGo.js +77 -0
- package/kits/hub-game/code/client/hubControls.js +125 -0
- package/kits/hub-game/code/client/hubEntry.js +187 -0
- package/kits/hub-game/code/client/hudSurface.js +148 -0
- package/kits/hub-game/code/client/hudTree.js +96 -0
- package/kits/hub-game/code/client/idle.js +59 -13
- package/kits/hub-game/code/client/invites.js +13 -4
- package/kits/hub-game/code/client/menuIcons.js +8 -0
- package/kits/hub-game/code/client/noTrips.js +38 -0
- package/kits/hub-game/code/client/qualityEntry.js +6 -1
- package/kits/hub-game/code/client/rehello.js +3 -1
- package/kits/hub-game/code/client/riders.js +26 -0
- package/kits/hub-game/code/client/role.js +79 -0
- package/kits/hub-game/code/client/safeLayout.js +32 -3
- package/kits/hub-game/code/client/sound.js +10 -3
- package/kits/hub-game/code/client/soundscape.js +2 -2
- package/kits/hub-game/code/client/soundscapeAmbience.js +38 -8
- package/kits/hub-game/code/client/soundscapeBaked.js +24 -5
- package/kits/hub-game/code/client/soundscapeCues.js +2 -1
- package/kits/hub-game/code/client/state.js +19 -0
- package/kits/hub-game/code/client/stuck.js +2 -1
- package/kits/hub-game/code/client/talkSurface.js +178 -0
- package/kits/hub-game/code/client/talkTree.js +238 -0
- package/kits/hub-game/code/client/tiers.js +8 -5
- package/kits/hub-game/code/client/uiDom.js +600 -0
- package/kits/hub-game/code/client/uiTree.js +359 -0
- package/kits/hub-game/code/client/vehicle.js +154 -0
- package/kits/hub-game/code/client/wear.js +27 -5
- package/kits/hub-game/code/client/worldLabels.js +3 -2
- package/kits/hub-game/code/client/yours.js +10 -3
- package/kits/hub-game/code/server/actors.js +4 -3
- package/kits/hub-game/code/server/analytics.js +56 -6
- package/kits/hub-game/code/server/decor.js +56 -16
- package/kits/hub-game/code/server/hub.js +258 -46
- package/kits/hub-game/code/server/idle.js +118 -5
- package/kits/hub-game/code/server/manifest.js +46 -4
- package/kits/hub-game/code/server/npcs.js +4 -2
- package/kits/hub-game/code/server/players.js +3 -0
- package/kits/hub-game/code/server/role.js +142 -0
- package/kits/hub-game/code/server/standing.js +6 -4
- package/kits/hub-game/code/server/vehicle.js +126 -0
- package/kits/hub-game/code/shared/car.js +52 -0
- package/kits/hub-game/code/shared/deckFlags.js +34 -0
- package/kits/hub-game/code/shared/deviceClass.js +39 -0
- package/kits/hub-game/code/shared/expression.js +9 -0
- package/kits/hub-game/code/systems/hub.js +300 -85
- package/kits/hub-game/docs/analytics.md +11 -2
- package/kits/hub-game/docs/contract.md +353 -14
- package/kits/hub-game/docs/vehicle-preset.md +132 -0
- package/kits/hub-game/models/hats/baker-cap.pxmodel +89 -0
- package/kits/hub-game/models/hats/chef-toque.pxmodel +60 -0
- package/kits/hub-game/models/hats/delivery-cap.pxmodel +105 -0
- package/kits/hub-game/models/hats/fire-helmet.pxmodel +87 -0
- package/kits/hub-game/models/hats/hard-hat.pxmodel +71 -0
- package/kits/hub-game/models/hats/nurse-cap.pxmodel +61 -0
- package/kits/hub-game/models/hats/police-cap.pxmodel +105 -0
- package/kits/hub-game/models/hats/teacher-beret.pxmodel +52 -0
- package/kits/hub-game/models/role/square/top-apron.pxmodel +109 -0
- package/kits/hub-game/models/role/square/top-cardigan.pxmodel +141 -0
- package/kits/hub-game/models/role/square/top-coat.pxmodel +157 -0
- package/kits/hub-game/models/role/square/top-scrubs.pxmodel +93 -0
- package/kits/hub-game/models/role/square/top-tabard.pxmodel +109 -0
- package/kits/hub-game/models/role/square/top-vest.pxmodel +109 -0
- package/kits/hub-game/models/role/tool-bag.pxmodel +75 -0
- package/kits/hub-game/models/role/tool-bell.pxmodel +71 -0
- package/kits/hub-game/models/role/tool-hose.pxmodel +60 -0
- package/kits/hub-game/models/role/tool-parcel.pxmodel +61 -0
- package/kits/hub-game/models/role/tool-torch.pxmodel +55 -0
- package/kits/hub-game/models/role/tool-tray.pxmodel +75 -0
- package/kits/hub-game/models/role/top-apron.pxmodel +70 -0
- package/kits/hub-game/models/role/top-cardigan.pxmodel +102 -0
- package/kits/hub-game/models/role/top-coat.pxmodel +79 -0
- package/kits/hub-game/models/role/top-scrubs.pxmodel +54 -0
- package/kits/hub-game/models/role/top-tabard.pxmodel +70 -0
- package/kits/hub-game/models/role/top-vest.pxmodel +70 -0
- package/kits/hub-game/package-lock.json +1 -1
- package/kits/hub-game/package.json +1 -1
- package/kits/hub-game/scripts/check.mjs +7 -4
- package/kits/hub-game/scripts/fixtures/car-vectors.json +279 -0
- package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +2 -1
- package/kits/hub-game/scripts/fixtures/hud-surface.json +670 -0
- package/kits/hub-game/scripts/fixtures/talk-surface.json +3930 -0
- package/kits/hub-game/scripts/gen-hats.mjs +46 -2
- package/kits/hub-game/scripts/gen-role.mjs +80 -0
- package/kits/hub-game/scripts/test-act-core.mjs +167 -0
- package/kits/hub-game/scripts/test-actions-client.mjs +34 -0
- package/kits/hub-game/scripts/test-actors.mjs +48 -0
- package/kits/hub-game/scripts/test-analytics.mjs +99 -5
- package/kits/hub-game/scripts/test-apply-deck.mjs +122 -0
- package/kits/hub-game/scripts/test-back-visibility.mjs +3 -1
- package/kits/hub-game/scripts/test-ballot.mjs +2 -0
- package/kits/hub-game/scripts/test-car.mjs +43 -0
- package/kits/hub-game/scripts/test-decor-client.mjs +2 -1
- package/kits/hub-game/scripts/test-decor.mjs +77 -0
- package/kits/hub-game/scripts/test-device-class.mjs +83 -0
- package/kits/hub-game/scripts/test-emote-wheel.mjs +374 -0
- package/kits/hub-game/scripts/test-hats.mjs +5 -4
- package/kits/hub-game/scripts/test-host-go.mjs +265 -0
- package/kits/hub-game/scripts/test-hub-controls.mjs +108 -0
- package/kits/hub-game/scripts/test-hub-entry.mjs +531 -0
- package/kits/hub-game/scripts/test-hub-ops.mjs +75 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +85 -0
- package/kits/hub-game/scripts/test-hud-clear.mjs +12 -2
- package/kits/hub-game/scripts/test-hud-rate.mjs +6 -2
- package/kits/hub-game/scripts/test-hud-surface.mjs +272 -0
- package/kits/hub-game/scripts/test-idle.mjs +347 -1
- package/kits/hub-game/scripts/test-interest-cells.mjs +8 -1
- package/kits/hub-game/scripts/test-invites-client.mjs +42 -1
- package/kits/hub-game/scripts/test-manifest.mjs +30 -0
- package/kits/hub-game/scripts/test-no-trips.mjs +42 -0
- package/kits/hub-game/scripts/test-placement-cache.mjs +1 -1
- package/kits/hub-game/scripts/test-quality.mjs +13 -0
- package/kits/hub-game/scripts/test-rehello.mjs +258 -3
- package/kits/hub-game/scripts/test-resume-pose-server.mjs +203 -0
- package/kits/hub-game/scripts/test-resync.mjs +183 -0
- package/kits/hub-game/scripts/test-role-client.mjs +421 -0
- package/kits/hub-game/scripts/test-role.mjs +350 -0
- package/kits/hub-game/scripts/test-safe-layout.mjs +50 -1
- package/kits/hub-game/scripts/test-soundscape.mjs +124 -7
- package/kits/hub-game/scripts/test-standing.mjs +15 -1
- package/kits/hub-game/scripts/test-talk-surface.mjs +296 -0
- package/kits/hub-game/scripts/test-tiers.mjs +13 -0
- package/kits/hub-game/scripts/test-ui-dom-dom.mjs +96 -0
- package/kits/hub-game/scripts/test-ui-tree.mjs +711 -0
- package/kits/hub-game/scripts/test-vehicle-client.mjs +159 -0
- package/kits/hub-game/scripts/test-vehicle.mjs +135 -0
- package/kits/hub-game/scripts/test-yours.mjs +33 -0
- package/kits/hub-game/tools/hubScene/export.mjs +613 -0
- package/kits/hub-game/tools/hubScene/hubdata.mjs +921 -0
- package/kits/hub-game/tools/hubScene/merge-root.mjs +39 -0
- package/kits/hub-game/tools/hubScene/nodeworld.mjs +227 -0
- package/kits/hub-game/tools/hubScene/test/export-fixture.mjs +798 -0
- package/kits/hub-game/tools/hubScene/test/fixture.json +637 -0
- package/kits/hub-game/tools/hubScene/test/paint-export.mjs +40 -0
- package/kits/hub-game/tools/hubScene/test/upload.test.mjs +655 -0
- package/kits/hub-game/tools/hubScene/upload.mjs +636 -0
- package/kits/hub-game/tools/hubScene/validate-manifest.mjs +291 -0
- package/kits/multiplayer-3d/castle.json +4 -4
- package/kits/multiplayer-3d/code/client/resumeGate.js +30 -2
- package/kits/multiplayer-3d/code/client/steer.js +3 -1
- package/kits/multiplayer-3d/code/systems/multiplayer.js +30 -2
- package/kits/multiplayer-3d/package-lock.json +1 -1
- package/kits/multiplayer-3d/scripts/test-resume-gate.mjs +74 -2
- package/kits/physics-3d/CLAUDE.md +1 -1
- package/kits/physics-3d/behaviors/Player.jsx +37 -27
- package/kits/physics-3d/castle.json +2 -2
- package/kits/physics-3d/docs/engine-geometry-memory.md +23 -0
- package/kits/physics-3d/engine3d/memoryprofile.js +17 -0
- package/kits/physics-3d/engine3d/playerRun.js +6 -1
- package/kits/physics-3d/engine3d/pxmodel.js +6 -1
- package/kits/physics-3d/engine3d/world3d.js +6 -3
- package/kits/physics-3d/package.json +1 -1
- package/kits/physics-3d/play/behaviors/Player.js +40 -28
- package/kits/physics-3d/scripts/test-memory-profile.mjs +237 -0
- package/kits/physics-3d/systems/physics3d.js +5 -2
- package/kits/real-time/castle.json +2 -2
- package/kits/real-time/code/client/messages.js +5 -0
- package/kits/real-time/code/server/session.js +28 -1
- package/kits/real-time/package-lock.json +1 -1
- package/package.json +5 -4
- package/vendor/three/LICENSE +21 -0
- package/vendor/three/build/three.core.js +58773 -0
- package/vendor/three/build/three.module.js +18251 -0
- package/vendor/three/examples/jsm/controls/OrbitControls.js +1860 -0
- package/vendor/three/examples/jsm/controls/TransformControls.js +1918 -0
- package/vendor/three/examples/jsm/exporters/GLTFExporter.js +3595 -0
- package/vendor/three/examples/jsm/geometries/RoundedBoxGeometry.js +216 -0
- package/vendor/three/examples/jsm/math/MeshSurfaceSampler.js +315 -0
- package/vendor/three/examples/jsm/postprocessing/EffectComposer.js +363 -0
- package/vendor/three/examples/jsm/postprocessing/MaskPass.js +195 -0
- package/vendor/three/examples/jsm/postprocessing/OutputPass.js +139 -0
- package/vendor/three/examples/jsm/postprocessing/Pass.js +191 -0
- package/vendor/three/examples/jsm/postprocessing/RenderPass.js +183 -0
- package/vendor/three/examples/jsm/postprocessing/ShaderPass.js +135 -0
- package/vendor/three/examples/jsm/shaders/CopyShader.js +52 -0
- package/vendor/three/examples/jsm/shaders/OutputShader.js +103 -0
- package/vendor/three/examples/jsm/utils/BufferGeometryUtils.js +1435 -0
- package/vendor/three/package.json +134 -0
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
// Exports hub decks' scenes (export.mjs) and puts them on Castle's hosted-games CDN,
|
|
2
|
+
// content-addressed, for the native hub engine to fetch by URL:
|
|
3
|
+
//
|
|
4
|
+
// node upload.mjs [--upload] [--list <file>] [--out <file>] [--decks-root <dir> ...]
|
|
5
|
+
// [--bucket <b>] [--prefix <p>] [--base-url <url>] [--region <r>] [--profile <p>]
|
|
6
|
+
// [--cache <dir>] [--head] <deckId | deckDir> ...
|
|
7
|
+
// node upload.mjs --from-export <dir> [--list <file>] [--upload] [...same flags] [--] [<deckId> ...]
|
|
8
|
+
//
|
|
9
|
+
// Each deck is exported into a fresh temp stage (never into the deck: nothing in a deck folder is
|
|
10
|
+
// written), then
|
|
11
|
+
// every asset -> <prefix>/nativehub/assets/<sha256>.<glb|png|webp|mp3|ogg|wav|m4a>
|
|
12
|
+
// the manifest -> <prefix>/nativehub/manifests/<sha256>.json
|
|
13
|
+
// The manifest's asset `uri`s are rewritten to the assets' absolute URLs first, so its sha256 is
|
|
14
|
+
// of its final bytes. Assets go up before the manifest that names them. Every key is
|
|
15
|
+
// content-addressed and immutable: an existing key is never rewritten (HEAD first, then a put
|
|
16
|
+
// with If-None-Match: *), and an existing one (or one a 412 race left) is accepted only when its
|
|
17
|
+
// stored SHA-256 checksum is ours; none, or another, is a fault. source.exportedAt is
|
|
18
|
+
// dropped from the uploaded manifest so an unchanged deck gives the same manifest key.
|
|
19
|
+
//
|
|
20
|
+
// DRY RUN BY DEFAULT: without --upload nothing is uploaded; it exports, computes keys and the
|
|
21
|
+
// allowlist entries, and lists what would go up. --head (dry run only) adds a read-only HEAD per
|
|
22
|
+
// key to say which already exist.
|
|
23
|
+
//
|
|
24
|
+
// Where it goes: the bucket ghost-server stores hub content packs and bundle chunks in
|
|
25
|
+
// (castle-hosted-games, served at https://hosted.castle.games; src/utils/cauldronContentPack.ts),
|
|
26
|
+
// under content-packs/ by default (content-packs/nativehub/...): CloudFront reads only web/,
|
|
27
|
+
// content-packs/ and cauldron-chunks/ from that bucket, so a key outside them answers 403. Credentials: none here. The
|
|
28
|
+
// `aws` CLI is run with the ambient AWS credential chain (AWS_PROFILE, or --profile), the same
|
|
29
|
+
// way ghost-server's operator scripts run from a laptop.
|
|
30
|
+
//
|
|
31
|
+
// --from-export <dir> uploads what castle-client's export-hub.mjs already wrote instead of
|
|
32
|
+
// exporting: <dir> is one deck dir (<root>/<deckId>/manifest.json) or the root holding them, its
|
|
33
|
+
// assets in <root>/shared/<sha256>.<ext> (each manifest's uris are `../shared/<sha256>.<ext>`).
|
|
34
|
+
// Deck ids (arguments or --list) pick deck dirs under a root; none picks every one. Each asset file
|
|
35
|
+
// must exist and hold the sha256 (and size) its manifest names, or nothing for that run goes up.
|
|
36
|
+
// The deck's kitVersion, which that exporter does not record, comes from --kit-versions, else the
|
|
37
|
+
// local deck folder's castle.hub-game pin under --decks-root.
|
|
38
|
+
//
|
|
39
|
+
// --kit-versions <file.json> is { "<deckId>": "<castle.hub-game version>", ... }; it is used when the
|
|
40
|
+
// manifest records no pin, and a manifest pin it disagrees with is a fault. A deck left with no
|
|
41
|
+
// valid kitVersion (ghost-server's rule: a non-empty string of at most 64 characters) is refused:
|
|
42
|
+
// no entry, nothing of it uploaded, listed as REFUSED, and the run exits 1.
|
|
43
|
+
//
|
|
44
|
+
// Output: one allowlist entry per deck, { deckId, manifestUrl, manifestSha256, kitVersion, source },
|
|
45
|
+
// source being { exporter, access, url } from the manifest (how the scene was exported),
|
|
46
|
+
// printed and written to --out (default <os tmpdir>/nativehub-scenes.json; never inside kits/,
|
|
47
|
+
// which the export hashes); `enabled` is added by hand.
|
|
48
|
+
// kitVersion is the deck's pinned castle.hub-game version (what its published bundle runs), else
|
|
49
|
+
// the shipped kit's published version the export was built with.
|
|
50
|
+
import { spawnSync } from 'node:child_process';
|
|
51
|
+
import crypto from 'node:crypto';
|
|
52
|
+
import fs from 'node:fs';
|
|
53
|
+
import os from 'node:os';
|
|
54
|
+
import path from 'node:path';
|
|
55
|
+
import { pathToFileURL } from 'node:url';
|
|
56
|
+
import { validateManifest } from './validate-manifest.mjs';
|
|
57
|
+
|
|
58
|
+
const TOOLS_DIR = import.meta.dirname;
|
|
59
|
+
export const DEFAULTS = Object.freeze({
|
|
60
|
+
bucket: 'castle-hosted-games',
|
|
61
|
+
baseUrl: 'https://hosted.castle.games',
|
|
62
|
+
prefix: 'content-packs',
|
|
63
|
+
region: 'us-east-1',
|
|
64
|
+
});
|
|
65
|
+
export const CONTENT_TYPES = Object.freeze({
|
|
66
|
+
glb: 'model/gltf-binary',
|
|
67
|
+
png: 'image/png',
|
|
68
|
+
webp: 'image/webp',
|
|
69
|
+
// v1.6 sound assets.
|
|
70
|
+
mp3: 'audio/mpeg',
|
|
71
|
+
ogg: 'audio/ogg',
|
|
72
|
+
wav: 'audio/wav',
|
|
73
|
+
m4a: 'audio/mp4',
|
|
74
|
+
json: 'application/json',
|
|
75
|
+
});
|
|
76
|
+
export const CACHE_CONTROL = 'public, max-age=31536000, immutable';
|
|
77
|
+
const SHA = /^[0-9a-f]{64}$/;
|
|
78
|
+
// export.mjs writes exactly this uri for a shared asset.
|
|
79
|
+
const SHARED_URI = /^\.\.\/shared\/([0-9a-f]{64})\.(glb|png|webp|mp3|ogg|wav|m4a)$/;
|
|
80
|
+
const DECK_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
81
|
+
|
|
82
|
+
export const sha256 = (buf) => crypto.createHash('sha256').update(buf).digest('hex');
|
|
83
|
+
|
|
84
|
+
// A key prefix: empty, or segments of [a-z0-9_-] joined by single slashes (no '.', so no '..';
|
|
85
|
+
// no leading or trailing slash). Anything else could break a URL or escape the nativehub tree.
|
|
86
|
+
const PREFIX = /^(?:[a-z0-9_-]+(?:\/[a-z0-9_-]+)*)?$/;
|
|
87
|
+
export function checkPrefix(prefix) {
|
|
88
|
+
if (typeof prefix !== 'string' || !PREFIX.test(prefix)) {
|
|
89
|
+
throw new Error(`--prefix ${JSON.stringify(prefix)} must be empty or [a-z0-9_-] segments joined by '/', with no leading or trailing slash`);
|
|
90
|
+
}
|
|
91
|
+
return prefix;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** A bucket key from a checked prefix and fixed parts. */
|
|
95
|
+
export function joinKey(prefix, ...parts) {
|
|
96
|
+
return [checkPrefix(prefix), ...parts].filter(Boolean).join('/');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function assetKey(prefix, hash, ext) {
|
|
100
|
+
if (!SHA.test(hash)) throw new Error(`not a sha256: ${hash}`);
|
|
101
|
+
if (!CONTENT_TYPES[ext] || ext === 'json') throw new Error(`not an asset type: ${ext}`);
|
|
102
|
+
return joinKey(prefix, 'nativehub/assets', `${hash}.${ext}`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function manifestKey(prefix, hash) {
|
|
106
|
+
if (!SHA.test(hash)) throw new Error(`not a sha256: ${hash}`);
|
|
107
|
+
return joinKey(prefix, 'nativehub/manifests', `${hash}.json`);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function publicUrl(baseUrl, key) {
|
|
111
|
+
let base;
|
|
112
|
+
try { base = new URL(String(baseUrl)); } catch { throw new Error(`base url is not a URL: ${baseUrl}`); }
|
|
113
|
+
if (base.protocol !== 'https:' || base.search || base.hash || base.username || base.password) {
|
|
114
|
+
throw new Error(`base url must be a plain https URL: ${baseUrl}`);
|
|
115
|
+
}
|
|
116
|
+
// Every key segment encoded on its own: the URL names exactly the key.
|
|
117
|
+
const segments = String(key).split('/');
|
|
118
|
+
if (segments.some((seg) => !seg || seg === '.' || seg === '..')) throw new Error(`not a key: ${key}`);
|
|
119
|
+
return `${base.origin}${base.pathname.replace(/\/+$/, '')}/${segments.map(encodeURIComponent).join('/')}`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The uploads for one exported manifest. `readShared(name)` gives the bytes of `shared/<name>`
|
|
124
|
+
* from the export stage. Every asset's bytes are checked against its sha256 and size; the
|
|
125
|
+
* returned manifest is a copy whose asset uris are the CDN URLs, and `manifestBytes` its exact
|
|
126
|
+
* bytes (compact JSON, as export.mjs writes it).
|
|
127
|
+
*/
|
|
128
|
+
export function planManifest(manifest, { readShared, prefix = DEFAULTS.prefix, baseUrl = DEFAULTS.baseUrl }) {
|
|
129
|
+
if (manifest?.format !== 'castle.hubScene') throw new Error('not a castle.hubScene manifest');
|
|
130
|
+
const out = structuredClone(manifest);
|
|
131
|
+
// The one field that differs between two exports of the same inputs. `source` is provenance
|
|
132
|
+
// only (SPEC.md) and source.inputKey still names the inputs; without it the same deck gives
|
|
133
|
+
// the same manifest key, so an unchanged deck uploads nothing.
|
|
134
|
+
if (out.source && typeof out.source === 'object') delete out.source.exportedAt;
|
|
135
|
+
const assets = new Map(); // key -> upload
|
|
136
|
+
// v1.7 wearModels embeds asset records too; rewrite their URIs alongside assets. The
|
|
137
|
+
// content-addressed uploads below remain deduplicated even after JSON loses shared identity.
|
|
138
|
+
for (const table of [out.assets, out.wearModels]) {
|
|
139
|
+
for (const [id, a] of Object.entries(table ?? {})) {
|
|
140
|
+
const m = SHARED_URI.exec(a?.uri ?? '');
|
|
141
|
+
if (!m || m[1] !== a.sha256 || m[2] !== a.type) throw new Error(`asset ${id}: uri ${JSON.stringify(a?.uri)} is not ../shared/<sha256>.<type>`);
|
|
142
|
+
const name = `${a.sha256}.${a.type}`;
|
|
143
|
+
const key = assetKey(prefix, a.sha256, a.type);
|
|
144
|
+
if (!assets.has(key)) {
|
|
145
|
+
const body = readShared(name);
|
|
146
|
+
if (sha256(body) !== a.sha256) throw new Error(`asset ${id}: shared/${name} does not hold its sha256`);
|
|
147
|
+
assets.set(key, { kind: 'asset', key, body, bytes: body.length, sha256: a.sha256, contentType: CONTENT_TYPES[a.type] });
|
|
148
|
+
}
|
|
149
|
+
// Every entry, not only the first naming these bytes: its declared size and type are the
|
|
150
|
+
// verified body's (the key carries the type, and the uri check tied it to a.type).
|
|
151
|
+
const got = assets.get(key);
|
|
152
|
+
if (a.bytes !== undefined && a.bytes !== got.bytes) throw new Error(`asset ${id}: ${got.bytes} bytes, manifest says ${a.bytes}`);
|
|
153
|
+
if (got.contentType !== CONTENT_TYPES[a.type]) throw new Error(`asset ${id}: type ${a.type} does not match ${got.contentType}`);
|
|
154
|
+
table[id] = { ...a, uri: publicUrl(baseUrl, key) };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const manifestBytes = Buffer.from(JSON.stringify(out), 'utf8');
|
|
158
|
+
const manifestSha256 = sha256(manifestBytes);
|
|
159
|
+
const mKey = manifestKey(prefix, manifestSha256);
|
|
160
|
+
const manifestUpload = { kind: 'manifest', key: mKey, body: manifestBytes, bytes: manifestBytes.length, sha256: manifestSha256, contentType: CONTENT_TYPES.json };
|
|
161
|
+
return {
|
|
162
|
+
manifest: out,
|
|
163
|
+
manifestBytes,
|
|
164
|
+
manifestSha256,
|
|
165
|
+
manifestKey: mKey,
|
|
166
|
+
manifestUrl: publicUrl(baseUrl, mKey),
|
|
167
|
+
// The manifest last: it is never up before every asset it names.
|
|
168
|
+
uploads: [...assets.values(), manifestUpload],
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The deck's pinned castle.hub-game version, else the shipped kit's the export was built with. */
|
|
173
|
+
export function kitVersionOf(manifest) {
|
|
174
|
+
const pin = manifest?.source?.pins?.['castle.hub-game'];
|
|
175
|
+
if (typeof pin === 'string' && pin) return pin;
|
|
176
|
+
const shipped = manifest?.source?.kits?.['castle.hub-game'];
|
|
177
|
+
return shipped?.publishedVersion ?? shipped?.package ?? null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* One ghost-server nativeHubScenes allowlist entry (`enabled` is added by hand), with the
|
|
182
|
+
* manifest's export provenance: which exporter, with what access, from which page.
|
|
183
|
+
*/
|
|
184
|
+
export function allowlistEntry(deckId, plan, kitVersion) {
|
|
185
|
+
const src = plan.manifest?.source ?? {};
|
|
186
|
+
const source = { exporter: src.exporter ?? null, access: src.access ?? null, url: src.url ?? null };
|
|
187
|
+
return { deckId, manifestUrl: plan.manifestUrl, manifestSha256: plan.manifestSha256, kitVersion, source };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The deck dirs of an export-hub.mjs output: `dir` itself when it holds a manifest.json, else
|
|
192
|
+
* its subfolders that do, sorted (or, given `ids`, exactly `<dir>/<id>` for each, all required).
|
|
193
|
+
*/
|
|
194
|
+
export function findExportDecks(dir, ids = null) {
|
|
195
|
+
const root = path.resolve(dir);
|
|
196
|
+
if (!fs.statSync(root).isDirectory()) throw new Error(`--from-export ${dir} is not a folder`);
|
|
197
|
+
const isDeck = (d) => fs.existsSync(path.join(d, 'manifest.json'));
|
|
198
|
+
const one = (d) => {
|
|
199
|
+
const deckId = path.basename(d);
|
|
200
|
+
if (!DECK_ID.test(deckId)) throw new Error(`${d}: folder name is not a deck id`);
|
|
201
|
+
return { deckId, dir: d };
|
|
202
|
+
};
|
|
203
|
+
if (isDeck(root)) {
|
|
204
|
+
const deck = one(root);
|
|
205
|
+
if (ids?.length && (ids.length !== 1 || ids[0] !== deck.deckId)) throw new Error(`${root} is the export of ${deck.deckId} only, not ${ids.join(', ')}`);
|
|
206
|
+
return [deck];
|
|
207
|
+
}
|
|
208
|
+
if (ids?.length) {
|
|
209
|
+
return [...new Set(ids)].map((id) => {
|
|
210
|
+
if (!DECK_ID.test(id)) throw new Error(`not a deck id: ${JSON.stringify(id)}`);
|
|
211
|
+
const d = path.join(root, id);
|
|
212
|
+
if (!isDeck(d)) throw new Error(`no ${id}/manifest.json under ${root}`);
|
|
213
|
+
return one(d);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
const entries = fs.readdirSync(root, { withFileTypes: true });
|
|
217
|
+
// A symlinked deck or shared dir could point anywhere; refused rather than skipped.
|
|
218
|
+
const link = entries.find((e) => e.isSymbolicLink());
|
|
219
|
+
if (link) throw new Error(`${path.join(root, link.name)} is a symlink; nothing under the export root may be one`);
|
|
220
|
+
const decks = entries
|
|
221
|
+
.filter((e) => e.isDirectory() && e.name !== 'shared' && isDeck(path.join(root, e.name)))
|
|
222
|
+
.map((e) => e.name).sort()
|
|
223
|
+
.map((name) => one(path.join(root, name)));
|
|
224
|
+
if (!decks.length) throw new Error(`no <deckId>/manifest.json under ${root}`);
|
|
225
|
+
return decks;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* One export-hub.mjs deck dir: its manifest, and `readShared(name)` giving the bytes of the file
|
|
230
|
+
* its `../shared/<name>` uris name (path.resolve(deckDir, uri)). A file that is missing or not a
|
|
231
|
+
* regular file is an error; planManifest checks each one's sha256 and size.
|
|
232
|
+
*/
|
|
233
|
+
export function readExportDeck(deckDir) {
|
|
234
|
+
const deckId = path.basename(path.resolve(deckDir));
|
|
235
|
+
// The export root is the deck dir's parent (it holds shared/ beside the deck dirs). Its own
|
|
236
|
+
// path may go through symlinks; below it, nothing read may.
|
|
237
|
+
const root = fs.realpathSync(path.dirname(path.resolve(deckDir)));
|
|
238
|
+
const realDir = path.join(root, deckId);
|
|
239
|
+
let manifest;
|
|
240
|
+
try { manifest = JSON.parse(readUnder(root, [deckId, 'manifest.json']).toString('utf8')); } catch (e) {
|
|
241
|
+
throw new Error(`${deckDir}/manifest.json: ${e.message}`);
|
|
242
|
+
}
|
|
243
|
+
if (manifest?.format !== 'castle.hubScene') throw new Error(`${deckDir}/manifest.json is not a castle.hubScene manifest`);
|
|
244
|
+
if (manifest.hubId !== deckId) throw new Error(`${deckDir}/manifest.json is hub ${JSON.stringify(manifest.hubId)}, not ${deckId}`);
|
|
245
|
+
const srcDeck = manifest.source?.deckId;
|
|
246
|
+
if (srcDeck !== undefined && srcDeck !== null && srcDeck !== deckId) throw new Error(`${deckDir}/manifest.json was exported from deck ${JSON.stringify(srcDeck)}, not ${deckId}`);
|
|
247
|
+
const readShared = (name) => {
|
|
248
|
+
if (!/^[0-9a-f]{64}\.(glb|png|webp|mp3|ogg|wav|m4a)$/.test(name)) throw new Error(`not a shared name: ${name}`);
|
|
249
|
+
try { return readUnder(root, ['shared', name]); } catch (e) { throw new Error(`${deckId}: asset ${e.message}`); }
|
|
250
|
+
};
|
|
251
|
+
return { deckId, manifest, readShared, dir: realDir, root };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* The bytes of `<root>/<parts...>`, `root` a real path: every component below it must exist and
|
|
256
|
+
* not be a symlink, the last a regular file, and its real path must be inside `root`.
|
|
257
|
+
*/
|
|
258
|
+
export function readUnder(root, parts) {
|
|
259
|
+
let cur = root;
|
|
260
|
+
for (const [i, part] of parts.entries()) {
|
|
261
|
+
if (!part || part === '.' || part === '..' || part.includes('/') || part.includes(path.sep)) throw new Error(`bad path part ${JSON.stringify(part)}`);
|
|
262
|
+
cur = path.join(cur, part);
|
|
263
|
+
let st;
|
|
264
|
+
try { st = fs.lstatSync(cur); } catch { throw new Error(`file missing: ${cur}`); }
|
|
265
|
+
if (st.isSymbolicLink()) throw new Error(`${cur} is a symlink; nothing under the export root may be one`);
|
|
266
|
+
if (i < parts.length - 1 ? !st.isDirectory() : !st.isFile()) throw new Error(`${cur} is not a regular ${i < parts.length - 1 ? 'folder' : 'file'}`);
|
|
267
|
+
}
|
|
268
|
+
const real = fs.realpathSync(cur);
|
|
269
|
+
if (!real.startsWith(root + path.sep)) throw new Error(`${cur} resolves outside the export root ${root}`);
|
|
270
|
+
return fs.readFileSync(real);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Faults of an imported manifest: the kit's validator (validate-manifest.mjs, as export.mjs runs
|
|
275
|
+
* it, against the files in `dir`), plus every asset reference resolving to an entry of `assets`:
|
|
276
|
+
* each layer's asset (required), avatar.asset (required when there is an avatar), sky.dome and
|
|
277
|
+
* each sky zone's image (when not null), and any other `asset` field anywhere.
|
|
278
|
+
*/
|
|
279
|
+
export function importFaults(manifest, dir) {
|
|
280
|
+
const faults = [...validateManifest(manifest, dir)];
|
|
281
|
+
const assets = manifest?.assets && typeof manifest.assets === 'object' ? manifest.assets : {};
|
|
282
|
+
const has = (id) => typeof id === 'string' && Object.hasOwn(assets, id);
|
|
283
|
+
const ref = (id, where, required) => {
|
|
284
|
+
if ((id === null || id === undefined) && !required) return;
|
|
285
|
+
if (!has(id)) faults.push(`${where}: asset ${JSON.stringify(id)} is not in assets`);
|
|
286
|
+
};
|
|
287
|
+
for (const [i, l] of (Array.isArray(manifest?.layers) ? manifest.layers : []).entries()) ref(l?.asset, `layers[${i}]`, true);
|
|
288
|
+
if (manifest?.avatar) ref(manifest.avatar.asset, 'avatar', true);
|
|
289
|
+
if (manifest?.sky) {
|
|
290
|
+
ref(manifest.sky.dome, 'sky.dome', false);
|
|
291
|
+
for (const [i, z] of (Array.isArray(manifest.sky.zones) ? manifest.sky.zones : []).entries()) ref(z?.image, `sky.zones[${i}].image`, false);
|
|
292
|
+
}
|
|
293
|
+
const walk = (v, where) => {
|
|
294
|
+
if (Array.isArray(v)) v.forEach((x, i) => walk(x, `${where}[${i}]`));
|
|
295
|
+
else if (v && typeof v === 'object') {
|
|
296
|
+
for (const [k, x] of Object.entries(v)) {
|
|
297
|
+
if (k === 'asset' && !/^layers\[\d+\]$/.test(where) && where !== 'avatar') ref(x, `${where}.asset`, false);
|
|
298
|
+
else walk(x, where ? `${where}.${k}` : k);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
for (const [k, v] of Object.entries(manifest ?? {})) if (k !== 'assets' && k !== 'source') walk(v, k);
|
|
303
|
+
return [...new Set(faults)];
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// ghost-server src/utils/nativeHubScenes.ts: NATIVE_HUB_SCENE_KIT_VERSION_MAX_LENGTH.
|
|
307
|
+
export const KIT_VERSION_MAX_LENGTH = 64;
|
|
308
|
+
export const isKitVersion = (v) => typeof v === 'string' && v.length > 0 && v.length <= KIT_VERSION_MAX_LENGTH;
|
|
309
|
+
|
|
310
|
+
/** A --kit-versions file's text: a JSON object of deck id -> kitVersion, every one valid. */
|
|
311
|
+
export function parseKitVersions(text, file = '--kit-versions') {
|
|
312
|
+
let o;
|
|
313
|
+
try { o = JSON.parse(text); } catch (e) { throw new Error(`${file}: ${e.message}`); }
|
|
314
|
+
if (!o || typeof o !== 'object' || Array.isArray(o)) throw new Error(`${file}: not a JSON object of deckId -> kitVersion`);
|
|
315
|
+
for (const [id, v] of Object.entries(o)) {
|
|
316
|
+
if (!DECK_ID.test(id)) throw new Error(`${file}: ${JSON.stringify(id)} is not a deck id`);
|
|
317
|
+
if (!isKitVersion(v)) throw new Error(`${file}: ${id}: kitVersion ${JSON.stringify(v)} must be a non-empty string of at most ${KIT_VERSION_MAX_LENGTH} characters`);
|
|
318
|
+
}
|
|
319
|
+
return new Map(Object.entries(o));
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* A deck's kitVersion: the manifest's pin (or shipped kit version), else `kitVersions` (a Map
|
|
324
|
+
* from --kit-versions), else, with `localFallback`, the local deck folder's pin under `roots`.
|
|
325
|
+
* Throws, naming the deck, when none is found, the one found is not valid, or --kit-versions
|
|
326
|
+
* disagrees with the manifest.
|
|
327
|
+
*/
|
|
328
|
+
export function resolveKitVersion(deckId, manifest, { kitVersions = new Map(), roots = [], localFallback = false } = {}) {
|
|
329
|
+
const fromManifest = kitVersionOf(manifest);
|
|
330
|
+
const given = kitVersions.get(deckId);
|
|
331
|
+
if (fromManifest && given && fromManifest !== given) throw new Error(`${deckId}: --kit-versions says ${given}, the manifest says ${fromManifest}`);
|
|
332
|
+
const v = fromManifest ?? given ?? (localFallback ? localKitPin(deckId, roots) : null);
|
|
333
|
+
if (v === null || v === undefined) throw new Error(`${deckId}: no kitVersion (no pin in the manifest, --kit-versions or a local deck folder)`);
|
|
334
|
+
if (!isKitVersion(v)) throw new Error(`${deckId}: kitVersion ${JSON.stringify(v)} must be a non-empty string of at most ${KIT_VERSION_MAX_LENGTH} characters`);
|
|
335
|
+
return v;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Settles each planned deck's kitVersion, then uploads (or, dry run, lists) the decks that have
|
|
340
|
+
* one, in order. A deck without one is refused: no entry, and none of its keys is put or listed.
|
|
341
|
+
* A deck with `faults` (importFaults) is refused the same way.
|
|
342
|
+
* `decks` is [{ deckId, title, manifest, plan, faults? }]. Returns { entries, refused: [{ deckId, reason }] }.
|
|
343
|
+
*/
|
|
344
|
+
export async function runDecks(decks, s3, { upload = false, log = () => {}, ...kitOpts } = {}) {
|
|
345
|
+
const ready = [];
|
|
346
|
+
const refused = [];
|
|
347
|
+
for (const d of decks) {
|
|
348
|
+
if (d.faults?.length) { refused.push({ deckId: d.deckId, reason: `${d.deckId}: manifest faults: ${d.faults.join('; ')}` }); continue; }
|
|
349
|
+
try { ready.push({ ...d, kitVersion: resolveKitVersion(d.deckId, d.manifest, kitOpts) }); } catch (e) { refused.push({ deckId: d.deckId, reason: e.message }); }
|
|
350
|
+
}
|
|
351
|
+
for (const { deckId, reason } of refused) log(`REFUSED ${deckId}: ${reason}; nothing of it ${upload ? 'uploaded' : 'would go up'}`);
|
|
352
|
+
const entries = [];
|
|
353
|
+
for (const { deckId, title, plan, kitVersion } of ready) {
|
|
354
|
+
log(`${deckId} (${title}): ${plan.uploads.length - 1} assets, manifest ${plan.manifestBytes.length} bytes, kitVersion ${kitVersion}`);
|
|
355
|
+
if (upload) await uploadAll(plan.uploads, s3, { log });
|
|
356
|
+
else {
|
|
357
|
+
for (const u of plan.uploads) {
|
|
358
|
+
const head = await s3.head(u.key);
|
|
359
|
+
const state = head.unchecked || !head.exists ? 'would put' : head.sha256 === u.sha256 ? 'exists ' : 'MISMATCH ';
|
|
360
|
+
log(` ${state} ${u.key} (${u.bytes} bytes, ${u.contentType})`);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
entries.push(allowlistEntry(deckId, plan, kitVersion));
|
|
364
|
+
}
|
|
365
|
+
return { entries, refused };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** The castle.hub-game version a local deck folder pins, found by id under `roots`; null if none. */
|
|
369
|
+
export function localKitPin(deckId, roots) {
|
|
370
|
+
let dir;
|
|
371
|
+
try { [{ dir }] = resolveDecks([deckId], roots); } catch { return null; }
|
|
372
|
+
const pin = readJson(path.join(dir, 'castle.json'))?.imports?.['castle.hub-game']?.version;
|
|
373
|
+
return typeof pin === 'string' && pin ? pin : null;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Puts each upload whose key is not there yet, in order (assets, then the manifest). `s3` is
|
|
378
|
+
* { head(key) -> {exists, bytes?, sha256?}, put(key, body, {contentType, cacheControl, sha256}) -> {raced?} }
|
|
379
|
+
* (head's sha256 is the object's stored full-object SHA-256 checksum, hex, or null).
|
|
380
|
+
* An existing key, found by HEAD or by losing a put race (412), is accepted only when its
|
|
381
|
+
* stored SHA-256 is ours (and its size, when given); one without a checksum or with another is a
|
|
382
|
+
* fault: a content-addressed key holding other bytes is never overwritten, nor trusted. Stops at
|
|
383
|
+
* the first failure, so a manifest is never put after an asset failed.
|
|
384
|
+
*/
|
|
385
|
+
export async function uploadAll(uploads, s3, { log = () => {} } = {}) {
|
|
386
|
+
const results = [];
|
|
387
|
+
for (const u of uploads) {
|
|
388
|
+
const head = await s3.head(u.key);
|
|
389
|
+
if (head.exists) {
|
|
390
|
+
verifyExisting(u, head);
|
|
391
|
+
results.push({ key: u.key, action: 'skipped' });
|
|
392
|
+
log(`exists ${u.key} (sha256 verified)`);
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
const r = await s3.put(u.key, u.body, { contentType: u.contentType, cacheControl: CACHE_CONTROL, sha256: u.sha256 });
|
|
396
|
+
if (r?.raced) {
|
|
397
|
+
// Another writer put the key between our HEAD and put: check what it put.
|
|
398
|
+
const after = await s3.head(u.key);
|
|
399
|
+
if (!after.exists) throw new Error(`${u.key}: put refused as existing (412) but HEAD finds nothing`);
|
|
400
|
+
verifyExisting(u, after);
|
|
401
|
+
results.push({ key: u.key, action: 'skipped' });
|
|
402
|
+
log(`exists ${u.key} (put by another writer; sha256 verified)`);
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
results.push({ key: u.key, action: 'uploaded' });
|
|
406
|
+
log(`uploaded ${u.key} (${u.bytes} bytes, ${u.contentType})`);
|
|
407
|
+
}
|
|
408
|
+
return results;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function verifyExisting(u, head) {
|
|
412
|
+
if (head.bytes !== undefined && head.bytes !== null && head.bytes !== u.bytes) {
|
|
413
|
+
throw new Error(`${u.key} exists with ${head.bytes} bytes, ours is ${u.bytes}; not overwriting it`);
|
|
414
|
+
}
|
|
415
|
+
if (!head.sha256) throw new Error(`${u.key} exists with no SHA-256 checksum; cannot verify it, not overwriting it (check it by hand)`);
|
|
416
|
+
if (head.sha256 !== u.sha256) throw new Error(`${u.key} exists with SHA-256 ${head.sha256}, ours is ${u.sha256}; not overwriting it`);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** S3's ChecksumSHA256 (base64 of a full-object digest) as hex; null for none or a composite one. */
|
|
420
|
+
export function checksumHex(value, type) {
|
|
421
|
+
if (typeof value !== 'string' || !value || (type && type !== 'FULL_OBJECT')) return null;
|
|
422
|
+
const buf = Buffer.from(value, 'base64');
|
|
423
|
+
return buf.length === 32 && buf.toString('base64') === value ? buf.toString('hex') : null;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* S3 through the `aws` CLI, with the ambient credential chain (AWS_PROFILE, or `profile`).
|
|
428
|
+
* `run` is spawnSync's shape, for tests.
|
|
429
|
+
*/
|
|
430
|
+
export function awsCliS3({ bucket, region = DEFAULTS.region, profile = null, run = spawnSync, tmpDir = os.tmpdir() }) {
|
|
431
|
+
if (!bucket) throw new Error('no bucket');
|
|
432
|
+
const common = ['--bucket', bucket, '--region', region, '--output', 'json', ...(profile ? ['--profile', profile] : [])];
|
|
433
|
+
const aws = (args) => {
|
|
434
|
+
const r = run('aws', ['s3api', ...args, ...common], { encoding: 'utf8', timeout: 300_000 });
|
|
435
|
+
if (r.error) throw new Error(`aws: ${r.error.message}`);
|
|
436
|
+
return r;
|
|
437
|
+
};
|
|
438
|
+
return {
|
|
439
|
+
async head(key) {
|
|
440
|
+
const r = aws(['head-object', '--key', key, '--checksum-mode', 'ENABLED']);
|
|
441
|
+
if (r.status === 0) {
|
|
442
|
+
const h = JSON.parse(r.stdout || '{}');
|
|
443
|
+
return { exists: true, bytes: typeof h.ContentLength === 'number' ? h.ContentLength : undefined, sha256: checksumHex(h.ChecksumSHA256, h.ChecksumType) };
|
|
444
|
+
}
|
|
445
|
+
if (/\(404\)|Not Found|NoSuchKey/.test(r.stderr ?? '')) return { exists: false };
|
|
446
|
+
// A 403 is not "missing": without s3:ListBucket a missing key answers 403, so stop.
|
|
447
|
+
throw new Error(`head ${key}: ${(r.stderr ?? '').trim() || `exit ${r.status}`}`);
|
|
448
|
+
},
|
|
449
|
+
async put(key, body, { contentType, cacheControl, sha256: hex }) {
|
|
450
|
+
const dir = fs.mkdtempSync(path.join(tmpDir, 'nativehub-put-'));
|
|
451
|
+
const file = path.join(dir, 'body');
|
|
452
|
+
try {
|
|
453
|
+
fs.writeFileSync(file, body, { flag: 'wx' });
|
|
454
|
+
const r = aws(['put-object', '--key', key, '--body', file, '--content-type', contentType, '--cache-control', cacheControl,
|
|
455
|
+
'--checksum-sha256', Buffer.from(hex, 'hex').toString('base64'), '--if-none-match', '*']);
|
|
456
|
+
if (r.status === 0) return { raced: false };
|
|
457
|
+
if (/PreconditionFailed|\(412\)/.test(r.stderr ?? '')) return { raced: true };
|
|
458
|
+
throw new Error(`put ${key}: ${(r.stderr ?? '').trim() || `exit ${r.status}`}`);
|
|
459
|
+
} finally {
|
|
460
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
461
|
+
}
|
|
462
|
+
},
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** A dry run's stand-in: HEADs through `real` when given (read-only), never puts. */
|
|
467
|
+
export function dryRunS3(real = null) {
|
|
468
|
+
return {
|
|
469
|
+
async head(key) { return real ? real.head(key) : { exists: false, unchecked: true }; },
|
|
470
|
+
async put() { throw new Error('dry run: no uploads'); },
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Runs export.mjs on a deck into a fresh temp stage (and `cacheDir`, a temp dir unless given),
|
|
476
|
+
* the way `castle-web export` does: a child node, since export.mjs installs a module loader
|
|
477
|
+
* guard and exits when done. Nothing is written in the deck folder.
|
|
478
|
+
*/
|
|
479
|
+
export function exportDeck(deckDir, { workDir, cacheDir, kitsDir = path.resolve(TOOLS_DIR, '../../..'), timeoutMs = 600_000 }) {
|
|
480
|
+
const stageDir = fs.mkdtempSync(path.join(workDir, 'stage-'));
|
|
481
|
+
const cache = cacheDir ?? fs.mkdtempSync(path.join(workDir, 'cache-'));
|
|
482
|
+
const r = spawnSync(process.execPath, [path.join(TOOLS_DIR, 'export.mjs'), '--deck', deckDir, '--stage', stageDir, '--cache', cache, '--kits', kitsDir, '--force'], {
|
|
483
|
+
encoding: 'utf8', timeout: timeoutMs, maxBuffer: 64 * 1024 * 1024,
|
|
484
|
+
// CASTLE_EXPORT_* from a parent export must not redirect this one.
|
|
485
|
+
env: Object.fromEntries(Object.entries(process.env).filter(([k]) => !k.startsWith('CASTLE_EXPORT_'))),
|
|
486
|
+
});
|
|
487
|
+
const last = (r.stdout ?? '').trim().split('\n').pop() ?? '';
|
|
488
|
+
let summary = null;
|
|
489
|
+
try { summary = JSON.parse(last); } catch {}
|
|
490
|
+
if (r.status !== 0 || !summary) {
|
|
491
|
+
throw new Error(`export of ${deckDir} failed (exit ${r.status ?? r.signal}): ${(r.stderr ?? '').trim().split('\n').slice(-5).join(' | ')}${summary?.faults?.length ? ` faults: ${summary.faults.join('; ')}` : ''}`);
|
|
492
|
+
}
|
|
493
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(stageDir, summary.hubId, 'manifest.json'), 'utf8'));
|
|
494
|
+
const readShared = (name) => {
|
|
495
|
+
if (!/^[0-9a-f]{64}\.(glb|png|webp|mp3|ogg|wav|m4a)$/.test(name)) throw new Error(`not a shared name: ${name}`);
|
|
496
|
+
return fs.readFileSync(path.join(stageDir, 'shared', name));
|
|
497
|
+
};
|
|
498
|
+
return { summary, manifest, readShared, stageDir };
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** Deck ids from a list file: one per line; blank lines and `#` comments ignored. */
|
|
502
|
+
export function readDeckList(text) {
|
|
503
|
+
return text.split('\n').map((l) => l.replace(/#.*/, '').trim()).filter(Boolean);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** A deck folder (castle.json with a deckId) for each id or path, found under `roots`. */
|
|
507
|
+
export function resolveDecks(refs, roots) {
|
|
508
|
+
let index = null;
|
|
509
|
+
const byId = () => {
|
|
510
|
+
if (index) return index;
|
|
511
|
+
index = new Map();
|
|
512
|
+
for (const root of roots) {
|
|
513
|
+
let entries = [];
|
|
514
|
+
try { entries = fs.readdirSync(root, { withFileTypes: true }); } catch { continue; }
|
|
515
|
+
for (const e of entries) {
|
|
516
|
+
if (!e.isDirectory()) continue;
|
|
517
|
+
const dir = path.join(root, e.name);
|
|
518
|
+
const id = readJson(path.join(dir, 'castle.json'))?.deckId;
|
|
519
|
+
if (typeof id === 'string' && !index.has(id)) index.set(id, dir);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return index;
|
|
523
|
+
};
|
|
524
|
+
return refs.map((ref) => {
|
|
525
|
+
let dir = null;
|
|
526
|
+
if (fs.existsSync(path.join(path.resolve(ref), 'castle.json'))) dir = path.resolve(ref);
|
|
527
|
+
else if (DECK_ID.test(ref)) dir = byId().get(ref) ?? null;
|
|
528
|
+
if (!dir) throw new Error(`no deck folder for ${ref} under ${roots.join(', ')} (pass its folder, or --decks-root)`);
|
|
529
|
+
const deckId = readJson(path.join(dir, 'castle.json'))?.deckId;
|
|
530
|
+
if (typeof deckId !== 'string' || !DECK_ID.test(deckId)) throw new Error(`${dir}: castle.json has no deckId (an unsaved deck has no allowlist entry)`);
|
|
531
|
+
if (DECK_ID.test(ref) && !fs.existsSync(path.join(path.resolve(ref), 'castle.json')) && deckId !== ref) throw new Error(`${dir} is deck ${deckId}, not ${ref}`);
|
|
532
|
+
return { deckId, dir };
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function readJson(file) {
|
|
537
|
+
try { return JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return null; }
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Refuses an --out inside the kits folder: export.mjs hashes those trees into every manifest's
|
|
542
|
+
* source, so a file written there changes the next run's manifest key.
|
|
543
|
+
*/
|
|
544
|
+
export function checkOut(out, kitsDir = path.resolve(TOOLS_DIR, '../../..')) {
|
|
545
|
+
const real = (p) => {
|
|
546
|
+
// The nearest existing ancestor's real path, plus the rest.
|
|
547
|
+
let cur = path.resolve(p);
|
|
548
|
+
const rest = [];
|
|
549
|
+
while (!fs.existsSync(cur)) { rest.unshift(path.basename(cur)); const up = path.dirname(cur); if (up === cur) break; cur = up; }
|
|
550
|
+
return path.join(fs.realpathSync(cur), ...rest);
|
|
551
|
+
};
|
|
552
|
+
const file = real(out);
|
|
553
|
+
const kits = real(kitsDir);
|
|
554
|
+
if (file === kits || file.startsWith(kits + path.sep)) throw new Error(`--out ${out} is inside ${kitsDir}, which the export hashes; write it elsewhere`);
|
|
555
|
+
return file;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
export function parseArgs(argv) {
|
|
559
|
+
const o = { upload: false, head: false, list: null, fromExport: null, kitVersions: null, out: path.join(os.tmpdir(), 'nativehub-scenes.json'), decksRoots: [], cache: null, profile: null, refs: [], ...DEFAULTS };
|
|
560
|
+
const value = (i, name) => {
|
|
561
|
+
const v = argv[i + 1];
|
|
562
|
+
if (v === undefined || v.startsWith('--')) throw new Error(`--${name} needs a value`);
|
|
563
|
+
return v;
|
|
564
|
+
};
|
|
565
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
566
|
+
const a = argv[i];
|
|
567
|
+
if (a === '--') { o.refs.push(...argv.slice(i + 1)); break; }
|
|
568
|
+
const flags = { '--from-export': 'fromExport', '--kit-versions': 'kitVersions', '--list': 'list', '--out': 'out', '--bucket': 'bucket', '--prefix': 'prefix', '--base-url': 'baseUrl', '--region': 'region', '--profile': 'profile', '--cache': 'cache' };
|
|
569
|
+
if (a === '--upload') o.upload = true;
|
|
570
|
+
else if (a === '--dry-run') o.upload = false;
|
|
571
|
+
else if (a === '--head') o.head = true;
|
|
572
|
+
else if (a === '--decks-root') { o.decksRoots.push(value(i, 'decks-root')); i += 1; }
|
|
573
|
+
else if (flags[a]) { o[flags[a]] = value(i, a.slice(2)); i += 1; }
|
|
574
|
+
else if (a.startsWith('--')) throw new Error(`unknown flag ${a}`);
|
|
575
|
+
else o.refs.push(a);
|
|
576
|
+
}
|
|
577
|
+
checkPrefix(o.prefix);
|
|
578
|
+
checkOut(o.out);
|
|
579
|
+
if (!o.decksRoots.length) o.decksRoots.push(path.resolve(TOOLS_DIR, '../../../../experiments'));
|
|
580
|
+
return o;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
async function main() {
|
|
584
|
+
let o;
|
|
585
|
+
try { o = parseArgs(process.argv.slice(2)); } catch (e) {
|
|
586
|
+
console.error(`${e.message}\nusage: node upload.mjs [--upload] [--from-export <dir>] [--kit-versions <file.json>] [--list <file>] [--out <file>] [--decks-root <dir>] [--bucket <b>] [--prefix <p>] [--base-url <url>] [--region <r>] [--profile <p>] [--cache <dir>] [--head] <deckId|deckDir> ...`);
|
|
587
|
+
process.exit(2);
|
|
588
|
+
}
|
|
589
|
+
const refs = [...o.refs, ...(o.list ? readDeckList(fs.readFileSync(o.list, 'utf8')) : [])];
|
|
590
|
+
const roots = o.decksRoots.map((r) => path.resolve(r));
|
|
591
|
+
if (!refs.length && !o.fromExport) { console.error('no decks named'); process.exit(2); }
|
|
592
|
+
const kitVersions = o.kitVersions ? parseKitVersions(fs.readFileSync(o.kitVersions, 'utf8'), o.kitVersions) : new Map();
|
|
593
|
+
const decks = o.fromExport ? findExportDecks(o.fromExport, refs) : resolveDecks(refs, roots);
|
|
594
|
+
const real = o.upload || o.head ? awsCliS3({ bucket: o.bucket, region: o.region, profile: o.profile }) : null;
|
|
595
|
+
const s3 = o.upload ? real : dryRunS3(o.head ? real : null);
|
|
596
|
+
const log = (s) => console.error(s);
|
|
597
|
+
log(`${o.upload ? 'UPLOAD' : 'dry run'}${o.fromExport ? ` from export ${path.resolve(o.fromExport)}` : ''}: s3://${o.bucket}/${joinKey(o.prefix, 'nativehub')}/ -> ${publicUrl(o.baseUrl, joinKey(o.prefix, 'nativehub'))}/`);
|
|
598
|
+
const workDir = o.fromExport ? null : fs.mkdtempSync(path.join(os.tmpdir(), 'nativehub-upload-'));
|
|
599
|
+
const plans = [];
|
|
600
|
+
let result;
|
|
601
|
+
try {
|
|
602
|
+
// Every deck is planned (and every asset read and hash-checked) before anything goes up.
|
|
603
|
+
for (const { deckId, dir } of decks) {
|
|
604
|
+
let manifest, readShared, title, realDir;
|
|
605
|
+
if (o.fromExport) {
|
|
606
|
+
({ manifest, readShared, dir: realDir } = readExportDeck(dir));
|
|
607
|
+
title = manifest.title;
|
|
608
|
+
} else {
|
|
609
|
+
const ex = exportDeck(dir, { workDir, cacheDir: o.cache ? path.resolve(o.cache) : undefined });
|
|
610
|
+
if (ex.summary.hubId !== deckId) throw new Error(`${dir}: exported hub ${ex.summary.hubId}, expected ${deckId}`);
|
|
611
|
+
({ manifest, readShared } = ex);
|
|
612
|
+
title = ex.summary.title;
|
|
613
|
+
}
|
|
614
|
+
const plan = planManifest(manifest, { readShared, prefix: o.prefix, baseUrl: o.baseUrl });
|
|
615
|
+
// Imported manifests get the validator export.mjs runs on its own (after planManifest has
|
|
616
|
+
// read every asset through the symlink-free reader, so the validator reads the same files).
|
|
617
|
+
const faults = o.fromExport ? importFaults(manifest, realDir) : [];
|
|
618
|
+
plans.push({ deckId, title, manifest, plan, faults });
|
|
619
|
+
}
|
|
620
|
+
result = await runDecks(plans, s3, { upload: o.upload, log, kitVersions, roots, localFallback: Boolean(o.fromExport) });
|
|
621
|
+
} finally {
|
|
622
|
+
if (workDir) fs.rmSync(workDir, { recursive: true, force: true });
|
|
623
|
+
}
|
|
624
|
+
const { entries, refused } = result;
|
|
625
|
+
fs.writeFileSync(o.out, `${JSON.stringify(entries, null, 2)}\n`);
|
|
626
|
+
console.log(JSON.stringify(entries, null, 2));
|
|
627
|
+
log(`wrote ${entries.length} entr${entries.length === 1 ? 'y' : 'ies'} to ${path.resolve(o.out)}${o.upload ? '' : ' (dry run: nothing uploaded)'}`);
|
|
628
|
+
if (refused.length) {
|
|
629
|
+
log(`REFUSED ${refused.length} deck${refused.length === 1 ? '' : 's'} (no entry, nothing uploaded): ${refused.map((r) => r.deckId).join(', ')}`);
|
|
630
|
+
process.exitCode = 1;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
|
|
635
|
+
main().catch((e) => { console.error(e.message); process.exit(1); });
|
|
636
|
+
}
|