castle-web-cli 0.4.148 → 0.4.150

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/api.d.ts +14 -0
  2. package/dist/api.js +18 -0
  3. package/dist/castle-host/host.d.ts +17 -0
  4. package/dist/castle-host/host.js +58 -0
  5. package/dist/castleJson.d.ts +15 -0
  6. package/dist/castleJson.js +36 -0
  7. package/dist/devSessionChild.d.ts +1 -0
  8. package/dist/devSessionChild.js +129 -0
  9. package/dist/devSessionServer.d.ts +19 -0
  10. package/dist/devSessionServer.js +511 -0
  11. package/dist/index.js +8 -8
  12. package/dist/init.js +10 -2
  13. package/dist/list-decks.js +7 -6
  14. package/dist/preview.d.ts +1 -1
  15. package/dist/preview.js +4 -2
  16. package/dist/save-deck.js +63 -14
  17. package/dist/serve.js +269 -108
  18. package/dist/serveSecurity.d.ts +7 -1
  19. package/dist/serveSecurity.js +65 -16
  20. package/dist/serverBundle.d.ts +8 -0
  21. package/dist/serverBundle.js +82 -0
  22. package/dist/shell/assets/{index-CL9TuLXb.js → index-Dlhc3eZW.js} +42 -42
  23. package/dist/shell/assets/index-DwezawBs.css +1 -0
  24. package/dist/shell/index.html +2 -2
  25. package/kits/blank/castle.json +5 -2
  26. package/kits/physics-2d/behaviors/Collider.jsx +1 -2
  27. package/kits/physics-2d/behaviors/Joints.jsx +1 -1
  28. package/kits/physics-2d/castle.json +1 -1
  29. package/kits/physics-2d/editors/BlueprintLibrary.jsx +33 -3
  30. package/kits/physics-2d/editors/SceneEditor.jsx +319 -140
  31. package/kits/physics-2d/editors/SingleEditor.jsx +6 -0
  32. package/kits/physics-2d/engine/blueprint.js +106 -14
  33. package/kits/physics-2d/engine/scene.js +9 -2
  34. package/kits/physics-2d/engine/ui.jsx +30 -2
  35. package/kits/physics-2d/engine/ui.module.css +59 -0
  36. package/kits/physics-3d/castle.json +1 -1
  37. package/kits/physics-3d/engine3d/PxModelEditor.jsx +29 -13
  38. package/kits/physics-3d/engine3d/Scene3DEditor.jsx +26 -7
  39. package/kits/physics-3d/engine3d/editorChrome.jsx +26 -1
  40. package/package.json +1 -1
  41. package/dist/shell/assets/index-DN8HQBnW.css +0 -1
package/dist/save-deck.js CHANGED
@@ -6,7 +6,9 @@ import { nanoid } from 'nanoid';
6
6
  import * as api from './api.js';
7
7
  import * as config from './config.js';
8
8
  import { bundleProject } from './bundle.js';
9
+ import { resolveServerConfig } from './castleJson.js';
9
10
  import { COVER_FILE } from './ide.js';
11
+ import { buildServerBundle } from './serverBundle.js';
10
12
  // `imports/` is derived state like node_modules: a dependency's files belong to
11
13
  // the deck that published them, and castle.json's import pins are enough to fetch
12
14
  // them again -- so they neither bloat this deck's archive nor get republished as
@@ -57,30 +59,76 @@ export async function archiveSource(projectDir) {
57
59
  }
58
60
  }
59
61
  }
62
+ async function uploadPresignedFile(uploadConfig, file, contentType, timeoutMs, label) {
63
+ const formData = new FormData();
64
+ formData.append('Content-Type', contentType);
65
+ for (const [key, value] of Object.entries(uploadConfig.postFields)) {
66
+ formData.append(key, String(value));
67
+ }
68
+ formData.append('file', file);
69
+ const response = await fetch(uploadConfig.postUrl, {
70
+ method: 'POST',
71
+ body: formData,
72
+ signal: AbortSignal.timeout(timeoutMs),
73
+ });
74
+ if (response.status >= 300)
75
+ throw new Error(`${label} failed: HTTP ${response.status}`);
76
+ }
60
77
  async function uploadSource(projectDir, deckId) {
61
78
  const archive = await archiveSource(projectDir);
62
79
  const sizeKB = archive.length / 1024;
63
80
  console.log(`Source archive: ${sizeKB.toFixed(1)}KB`);
64
81
  const uploadConfig = await api.createWebDeckSourceUploadConfig(deckId);
65
- const formData = new FormData();
66
- formData.append('Content-Type', 'application/gzip');
67
- for (const [k, v] of Object.entries(uploadConfig.postFields)) {
68
- formData.append(k, String(v));
69
- }
70
82
  // FormData expects a Blob; copy the Buffer's bytes so we don't share the underlying ArrayBuffer.
71
83
  const view = new Uint8Array(archive.byteLength);
72
84
  view.set(archive);
73
- formData.append('file', new Blob([view], { type: 'application/gzip' }));
74
- const s3Res = await fetch(uploadConfig.postUrl, {
75
- method: 'POST',
76
- body: formData,
77
- signal: AbortSignal.timeout(60000),
78
- });
79
- if (s3Res.status >= 300) {
80
- throw new Error(`Source upload failed: HTTP ${s3Res.status}`);
81
- }
85
+ await uploadPresignedFile(uploadConfig, new Blob([view], { type: 'application/gzip' }), 'application/gzip', 60_000, 'Source upload');
82
86
  await api.saveWebDeckSource(deckId, uploadConfig.uploadId);
83
87
  }
88
+ const SERVER_BUNDLE_STATE = path.join('.castle', 'published-server-bundle.json');
89
+ function readPublishedServerBundleState(projectDir) {
90
+ try {
91
+ const value = JSON.parse(fs.readFileSync(path.join(projectDir, SERVER_BUNDLE_STATE), 'utf8'));
92
+ return typeof value.deckId === 'string' && typeof value.sha256 === 'string'
93
+ ? { deckId: value.deckId, sha256: value.sha256 }
94
+ : null;
95
+ }
96
+ catch {
97
+ return null;
98
+ }
99
+ }
100
+ function writePublishedServerBundleState(projectDir, state) {
101
+ const file = path.join(projectDir, SERVER_BUNDLE_STATE);
102
+ fs.mkdirSync(path.dirname(file), { recursive: true });
103
+ fs.writeFileSync(file, `${JSON.stringify(state, null, 2)}\n`, 'utf8');
104
+ }
105
+ async function publishServerBundle(projectDir, deckId) {
106
+ const serverConfig = resolveServerConfig(projectDir);
107
+ if (!serverConfig) {
108
+ const prior = readPublishedServerBundleState(projectDir);
109
+ if (prior?.deckId !== deckId) {
110
+ console.log('Server bundle skipped: no server entry found.');
111
+ return;
112
+ }
113
+ await api.saveCauldronServerBundle(deckId, null);
114
+ fs.rmSync(path.join(projectDir, SERVER_BUNDLE_STATE), { force: true });
115
+ console.log('Cleared the previously published server bundle (no server entry found).');
116
+ return;
117
+ }
118
+ console.log(`Bundling server (${serverConfig.entry})...`);
119
+ const bundle = await buildServerBundle(projectDir);
120
+ if (!bundle)
121
+ throw new Error('Server entry disappeared while bundling.');
122
+ console.log(`Server bundle size: ${(bundle.sizeBytes / 1024).toFixed(1)}KB`);
123
+ const uploadConfig = await api.createCauldronServerBundleUploadConfig(deckId);
124
+ await uploadPresignedFile(uploadConfig, new Blob([bundle.code], { type: 'application/javascript' }), 'application/javascript', 60_000, 'Server bundle upload');
125
+ await api.saveCauldronServerBundle(deckId, uploadConfig.uploadId, {
126
+ tickRate: serverConfig.tickRate,
127
+ maxPlayers: serverConfig.maxPlayers,
128
+ });
129
+ writePublishedServerBundleState(projectDir, { deckId, sha256: bundle.sha256 });
130
+ console.log(`Server bundle published (sha256 ${bundle.sha256}).`);
131
+ }
84
132
  // Publish the deck's local cover, if it has one, and return the file id for the
85
133
  // card. The editor writes `preview.png` as you play (see the serve's cover
86
134
  // endpoint); this is the moment it becomes public.
@@ -273,6 +321,7 @@ export async function saveDeck(dir, opts = {}) {
273
321
  else {
274
322
  console.log(`Saved "${title}" to Castle.`);
275
323
  }
324
+ await publishServerBundle(projectDir, result.deckId);
276
325
  console.log('Uploading source archive...');
277
326
  await uploadSource(projectDir, result.deckId);
278
327
  console.log('Source archive saved.');