@cabn/cli 0.1.0
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/LICENSE +21 -0
- package/README.md +36 -0
- package/dist/assets/originals/cabin_256.webp +0 -0
- package/dist/assets/originals/cabinet_256.webp +0 -0
- package/dist/assets/originals/file_256.webp +0 -0
- package/dist/assets/originals/key_256.webp +0 -0
- package/dist/assets/originals/letter_opener_256.webp +0 -0
- package/dist/assets/placeholders/bonfire_frame0_soft.png +0 -0
- package/dist/assets/placeholders/bonfire_frame1_soft.png +0 -0
- package/dist/assets/placeholders/bonfire_frame2_soft.png +0 -0
- package/dist/assets/placeholders/bonfire_frame3_soft.png +0 -0
- package/dist/assets/placeholders/bramble_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/bramble_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/bramble_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/bramble_hit_soft.png +0 -0
- package/dist/assets/placeholders/bramble_idle0_soft.png +0 -0
- package/dist/assets/placeholders/bramble_idle1_soft.png +0 -0
- package/dist/assets/placeholders/character_idle_back_soft.png +0 -0
- package/dist/assets/placeholders/character_idle_soft.png +0 -0
- package/dist/assets/placeholders/decals_soft.png +0 -0
- package/dist/assets/placeholders/fx_spark.png +0 -0
- package/dist/assets/placeholders/ghost_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/ghost_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/ghost_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/ghost_hit_soft.png +0 -0
- package/dist/assets/placeholders/ghost_soft.png +0 -0
- package/dist/assets/placeholders/glade_tiles_soft.png +0 -0
- package/dist/assets/placeholders/gremlin_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/gremlin_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/gremlin_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/gremlin_hit_soft.png +0 -0
- package/dist/assets/placeholders/gremlin_idle0_soft.png +0 -0
- package/dist/assets/placeholders/gremlin_idle1_soft.png +0 -0
- package/dist/assets/placeholders/grove_tiles_soft.png +0 -0
- package/dist/assets/placeholders/imp_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/imp_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/imp_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/imp_hit_soft.png +0 -0
- package/dist/assets/placeholders/imp_idle0_soft.png +0 -0
- package/dist/assets/placeholders/imp_idle1_soft.png +0 -0
- package/dist/assets/placeholders/magpie_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/magpie_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/magpie_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/magpie_hit_soft.png +0 -0
- package/dist/assets/placeholders/magpie_idle0_soft.png +0 -0
- package/dist/assets/placeholders/magpie_idle1_soft.png +0 -0
- package/dist/assets/placeholders/meadow_tiles_soft.png +0 -0
- package/dist/assets/placeholders/npc_guide_bubble_soft.png +0 -0
- package/dist/assets/placeholders/npc_guide_portrait_soft.png +0 -0
- package/dist/assets/placeholders/npc_guide_strip_soft.png +0 -0
- package/dist/assets/placeholders/ouroboros_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/ouroboros_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/ouroboros_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/ouroboros_hit_soft.png +0 -0
- package/dist/assets/placeholders/ouroboros_idle0_soft.png +0 -0
- package/dist/assets/placeholders/ouroboros_idle1_soft.png +0 -0
- package/dist/assets/placeholders/path_bed_disc_soft.png +0 -0
- package/dist/assets/placeholders/path_cobble_0_soft.png +0 -0
- package/dist/assets/placeholders/path_cobble_1_soft.png +0 -0
- package/dist/assets/placeholders/path_cobble_2_soft.png +0 -0
- package/dist/assets/placeholders/path_cobble_3_soft.png +0 -0
- package/dist/assets/placeholders/path_edge_disc_soft.png +0 -0
- package/dist/assets/placeholders/path_stamp_0_soft.png +0 -0
- package/dist/assets/placeholders/path_stamp_1_soft.png +0 -0
- package/dist/assets/placeholders/path_stamp_2_soft.png +0 -0
- package/dist/assets/placeholders/pet_bird_portrait_soft.png +0 -0
- package/dist/assets/placeholders/pet_bird_strip_soft.png +0 -0
- package/dist/assets/placeholders/pet_cat_portrait_soft.png +0 -0
- package/dist/assets/placeholders/pet_cat_strip_soft.png +0 -0
- package/dist/assets/placeholders/pet_ferret_portrait_soft.png +0 -0
- package/dist/assets/placeholders/pet_ferret_strip_soft.png +0 -0
- package/dist/assets/placeholders/pet_llama_portrait_soft.png +0 -0
- package/dist/assets/placeholders/pet_llama_strip_soft.png +0 -0
- package/dist/assets/placeholders/pet_owl_portrait_soft.png +0 -0
- package/dist/assets/placeholders/pet_owl_strip_soft.png +0 -0
- package/dist/assets/placeholders/pet_whale_portrait_soft.png +0 -0
- package/dist/assets/placeholders/pet_whale_strip_soft.png +0 -0
- package/dist/assets/placeholders/portal_arch_soft.png +0 -0
- package/dist/assets/placeholders/portal_arch_strip_soft.png +0 -0
- package/dist/assets/placeholders/portal_arch_variants_soft.png +0 -0
- package/dist/assets/placeholders/prop_bench_soft.png +0 -0
- package/dist/assets/placeholders/prop_bush_soft.png +0 -0
- package/dist/assets/placeholders/prop_castle_keep_soft.png +0 -0
- package/dist/assets/placeholders/prop_cottage_soft.png +0 -0
- package/dist/assets/placeholders/prop_fence_soft.png +0 -0
- package/dist/assets/placeholders/prop_flower_bed_soft.png +0 -0
- package/dist/assets/placeholders/prop_flower_pot_soft.png +0 -0
- package/dist/assets/placeholders/prop_hedge_soft.png +0 -0
- package/dist/assets/placeholders/prop_lamp_post_soft.png +0 -0
- package/dist/assets/placeholders/prop_seyn_sign_soft.png +0 -0
- package/dist/assets/placeholders/prop_shelf_cabin_soft.png +0 -0
- package/dist/assets/placeholders/prop_signpost_soft.png +0 -0
- package/dist/assets/placeholders/prop_stone_wall_soft.png +0 -0
- package/dist/assets/placeholders/prop_tree_large_soft.png +0 -0
- package/dist/assets/placeholders/prop_tree_small_soft.png +0 -0
- package/dist/assets/placeholders/prop_well_soft.png +0 -0
- package/dist/assets/placeholders/prop_world_fountain_gem_soft.png +0 -0
- package/dist/assets/placeholders/prop_world_fountain_strip_soft.png +0 -0
- package/dist/assets/placeholders/rot_sprite_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/rot_sprite_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/rot_sprite_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/rot_sprite_hit_soft.png +0 -0
- package/dist/assets/placeholders/rot_sprite_idle0_soft.png +0 -0
- package/dist/assets/placeholders/rot_sprite_idle1_soft.png +0 -0
- package/dist/assets/placeholders/scenery_berry_shrub_soft.png +0 -0
- package/dist/assets/placeholders/scenery_blossom_oak_soft.png +0 -0
- package/dist/assets/placeholders/scenery_boulder_soft.png +0 -0
- package/dist/assets/placeholders/scenery_fallen_log_soft.png +0 -0
- package/dist/assets/placeholders/scenery_flower_patch_soft.png +0 -0
- package/dist/assets/placeholders/scenery_mushroom_soft.png +0 -0
- package/dist/assets/placeholders/scenery_oak_soft.png +0 -0
- package/dist/assets/placeholders/scenery_pine_soft.png +0 -0
- package/dist/assets/placeholders/scenery_pond_soft.png +0 -0
- package/dist/assets/placeholders/scenery_reeds_soft.png +0 -0
- package/dist/assets/placeholders/scenery_rock_small_soft.png +0 -0
- package/dist/assets/placeholders/scenery_ruin_soft.png +0 -0
- package/dist/assets/placeholders/scenery_shrub_soft.png +0 -0
- package/dist/assets/placeholders/scenery_stump_soft.png +0 -0
- package/dist/assets/placeholders/scenery_waymarker_soft.png +0 -0
- package/dist/assets/placeholders/scenery_windmill_sails_soft.png +0 -0
- package/dist/assets/placeholders/scenery_windmill_soft.png +0 -0
- package/dist/assets/placeholders/shade_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/shade_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/shade_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/shade_hit_soft.png +0 -0
- package/dist/assets/placeholders/shade_idle0_soft.png +0 -0
- package/dist/assets/placeholders/shade_idle1_soft.png +0 -0
- package/dist/assets/placeholders/skeleton_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/skeleton_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/skeleton_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/skeleton_hit_soft.png +0 -0
- package/dist/assets/placeholders/skeleton_idle0_soft.png +0 -0
- package/dist/assets/placeholders/skeleton_idle1_soft.png +0 -0
- package/dist/assets/placeholders/sky_day.png +0 -0
- package/dist/assets/placeholders/sky_moon_soft.png +0 -0
- package/dist/assets/placeholders/sky_night.png +0 -0
- package/dist/assets/placeholders/sky_star_soft.png +0 -0
- package/dist/assets/placeholders/skyline_castle_day_soft.png +0 -0
- package/dist/assets/placeholders/skyline_castle_night_soft.png +0 -0
- package/dist/assets/placeholders/skyline_hill_day_soft.png +0 -0
- package/dist/assets/placeholders/skyline_hill_night_soft.png +0 -0
- package/dist/assets/placeholders/skyline_treeline_day_soft.png +0 -0
- package/dist/assets/placeholders/skyline_treeline_night_soft.png +0 -0
- package/dist/assets/placeholders/skyline_village_day_soft.png +0 -0
- package/dist/assets/placeholders/skyline_village_night_soft.png +0 -0
- package/dist/assets/placeholders/skyline_watchtower_day_soft.png +0 -0
- package/dist/assets/placeholders/skyline_watchtower_night_soft.png +0 -0
- package/dist/assets/placeholders/ui_icon_bag_soft.png +0 -0
- package/dist/assets/placeholders/ui_icon_key_soft.png +0 -0
- package/dist/assets/placeholders/ui_icon_orb_soft.png +0 -0
- package/dist/assets/placeholders/ui_icon_owner_soft.png +0 -0
- package/dist/assets/placeholders/ui_icon_quill_soft.png +0 -0
- package/dist/assets/placeholders/ui_icon_sign_soft.png +0 -0
- package/dist/assets/placeholders/ui_icon_spyglass_soft.png +0 -0
- package/dist/assets/placeholders/ui_icon_wand_soft.png +0 -0
- package/dist/assets/placeholders/ui_screen_orb_soft.png +0 -0
- package/dist/assets/placeholders/ui_screen_satchel_flap_soft.png +0 -0
- package/dist/assets/placeholders/ui_screen_satchel_soft.png +0 -0
- package/dist/assets/placeholders/ui_screen_spyglass_soft.png +0 -0
- package/dist/assets/placeholders/ui_sparkle_cyan@8x.png +0 -0
- package/dist/assets/placeholders/ui_sparkle_gold@8x.png +0 -0
- package/dist/assets/placeholders/ui_sparkle_violet@8x.png +0 -0
- package/dist/assets/placeholders/ui_tool_comment_soft.png +0 -0
- package/dist/assets/placeholders/ui_tool_find_soft.png +0 -0
- package/dist/assets/placeholders/ui_tool_fold_soft.png +0 -0
- package/dist/assets/placeholders/ui_tool_format_soft.png +0 -0
- package/dist/assets/placeholders/ui_tool_goto_soft.png +0 -0
- package/dist/assets/placeholders/ui_tool_rename_soft.png +0 -0
- package/dist/assets/placeholders/ui_tool_replace_soft.png +0 -0
- package/dist/assets/placeholders/ui_tool_save_soft.png +0 -0
- package/dist/assets/placeholders/ui_tool_symbol_soft.png +0 -0
- package/dist/assets/placeholders/ui_tool_unfold_soft.png +0 -0
- package/dist/assets/placeholders/warded_mimic_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/warded_mimic_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/warded_mimic_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/warded_mimic_hit_soft.png +0 -0
- package/dist/assets/placeholders/warded_mimic_idle0_soft.png +0 -0
- package/dist/assets/placeholders/warded_mimic_idle1_soft.png +0 -0
- package/dist/assets/placeholders/will_o_wisp_defeat0_soft.png +0 -0
- package/dist/assets/placeholders/will_o_wisp_defeat1_soft.png +0 -0
- package/dist/assets/placeholders/will_o_wisp_defeat2_soft.png +0 -0
- package/dist/assets/placeholders/will_o_wisp_hit_soft.png +0 -0
- package/dist/assets/placeholders/will_o_wisp_idle0_soft.png +0 -0
- package/dist/assets/placeholders/will_o_wisp_idle1_soft.png +0 -0
- package/dist/assets/placeholders/wizard_tower_soft.png +0 -0
- package/dist/build.d.ts +84 -0
- package/dist/build.js +185 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +163 -0
- package/dist/help.d.ts +2 -0
- package/dist/help.js +35 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/inspect.d.ts +3 -0
- package/dist/inspect.js +35 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +3 -0
- package/dist/serve/execRunner.d.ts +43 -0
- package/dist/serve/execRunner.js +113 -0
- package/dist/serve/hostPage.d.ts +15 -0
- package/dist/serve/hostPage.js +100 -0
- package/dist/serve/ownerAuth.d.ts +80 -0
- package/dist/serve/ownerAuth.js +241 -0
- package/dist/serve/ownerGit.d.ts +40 -0
- package/dist/serve/ownerGit.js +265 -0
- package/dist/serve/ownerShadow.d.ts +52 -0
- package/dist/serve/ownerShadow.js +250 -0
- package/dist/serve/ownerSigns.d.ts +30 -0
- package/dist/serve/ownerSigns.js +224 -0
- package/dist/serve/pyTracer.d.ts +33 -0
- package/dist/serve/pyTracer.js +76 -0
- package/dist/serve/runtime.d.ts +8 -0
- package/dist/serve/runtime.js +35 -0
- package/dist/serve/security.d.ts +47 -0
- package/dist/serve/security.js +106 -0
- package/dist/serve/server.d.ts +52 -0
- package/dist/serve/server.js +490 -0
- package/dist/serve/shadowArt.d.ts +10 -0
- package/dist/serve/shadowArt.js +29 -0
- package/dist/shelf.d.ts +10 -0
- package/dist/shelf.js +23 -0
- package/package.json +60 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import * as nodeFs from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { DEFAULT_MAX_FILE_BYTES, openGitRepo, redactRepoPaths, } from "@cabn/converter";
|
|
4
|
+
import { add, branch, checkout, commit, currentBranch, getConfig, listBranches, statusMatrix, } from "isomorphic-git";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { checkOwnerRequest, OwnerPathError, overwriteFileAtomic, readOwnerJson, resolveExistingOwnerFile, sendOwnerJson, } from "./ownerAuth.js";
|
|
7
|
+
/**
|
|
8
|
+
* The owner's git writes, run by isomorphic-git inside this Node process on
|
|
9
|
+
* the real repository: commit in-game edits, create a branch, switch
|
|
10
|
+
* branches. Nothing here talks to a remote — no push, fetch, pull or clone
|
|
11
|
+
* is imported, and no isomorphic-git http client exists in this package.
|
|
12
|
+
*/
|
|
13
|
+
/** Commit bodies carry whole files, so they get more room than the shared 128 KB default. */
|
|
14
|
+
const OWNER_GIT_COMMIT_MAX_BYTES = 2 * 1024 * 1024;
|
|
15
|
+
export const OWNER_GIT_ROUTES = {
|
|
16
|
+
status: "/owner/git/status",
|
|
17
|
+
commit: "/owner/git/commit",
|
|
18
|
+
branch: "/owner/git/branch",
|
|
19
|
+
checkout: "/owner/git/checkout",
|
|
20
|
+
};
|
|
21
|
+
// Shape only; resolveOwnerTarget (ownerAuth.ts) is the confinement authority.
|
|
22
|
+
const WorldPathSchema = z
|
|
23
|
+
.string()
|
|
24
|
+
.min(1)
|
|
25
|
+
.max(512)
|
|
26
|
+
.refine((p) => !p.startsWith("/") &&
|
|
27
|
+
!p.includes("\\") &&
|
|
28
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: rejecting them is the point
|
|
29
|
+
!/[\u0000-\u001F\u007F]/.test(p) &&
|
|
30
|
+
p.split("/").every((s) => s.length > 0 && s !== "." && s !== "..") &&
|
|
31
|
+
!p.split("/").some((s) => s.toLowerCase() === ".git"), "must be a relative world path outside .git");
|
|
32
|
+
// Conservative subset of git-check-ref-format: enough for real branch names,
|
|
33
|
+
// nothing that could be read as an option, a revision expression or a path escape.
|
|
34
|
+
export const BranchNameSchema = z
|
|
35
|
+
.string()
|
|
36
|
+
.min(1)
|
|
37
|
+
.max(100)
|
|
38
|
+
.regex(/^[A-Za-z0-9._/-]+$/, "letters, digits, . _ / - only")
|
|
39
|
+
.refine((n) => !n.startsWith("-") &&
|
|
40
|
+
!n.startsWith("/") &&
|
|
41
|
+
!n.endsWith("/") &&
|
|
42
|
+
!n.endsWith(".") &&
|
|
43
|
+
!n.includes("..") &&
|
|
44
|
+
!n.includes("//") &&
|
|
45
|
+
!n.split("/").some((s) => s.startsWith(".") || s.endsWith(".lock")) &&
|
|
46
|
+
n !== "HEAD", "not a valid branch name");
|
|
47
|
+
const AuthorSchema = z.object({
|
|
48
|
+
name: z.string().trim().min(1).max(100),
|
|
49
|
+
email: z
|
|
50
|
+
.string()
|
|
51
|
+
.trim()
|
|
52
|
+
.min(3)
|
|
53
|
+
.max(200)
|
|
54
|
+
.regex(/^[^\s<>]+@[^\s<>]+$/, "not an email address"),
|
|
55
|
+
});
|
|
56
|
+
export const CommitBodySchema = z.strictObject({
|
|
57
|
+
message: z.string().trim().min(1).max(2000),
|
|
58
|
+
author: AuthorSchema.optional(),
|
|
59
|
+
files: z
|
|
60
|
+
.array(z.strictObject({
|
|
61
|
+
path: WorldPathSchema,
|
|
62
|
+
content: z.string().max(DEFAULT_MAX_FILE_BYTES),
|
|
63
|
+
}))
|
|
64
|
+
.min(1)
|
|
65
|
+
.max(100),
|
|
66
|
+
});
|
|
67
|
+
export const BranchBodySchema = z.strictObject({
|
|
68
|
+
name: BranchNameSchema,
|
|
69
|
+
checkout: z.boolean().optional(),
|
|
70
|
+
});
|
|
71
|
+
export const CheckoutBodySchema = z.strictObject({ branch: BranchNameSchema });
|
|
72
|
+
class OwnerGitError extends Error {
|
|
73
|
+
status;
|
|
74
|
+
extra;
|
|
75
|
+
constructor(status, message, extra = {}) {
|
|
76
|
+
super(message);
|
|
77
|
+
this.status = status;
|
|
78
|
+
this.extra = extra;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async function repoFor(ctx) {
|
|
82
|
+
const opened = await openGitRepo({
|
|
83
|
+
fs: nodeFs,
|
|
84
|
+
dir: ctx.dir,
|
|
85
|
+
...(ctx.gitdir ? { gitdir: ctx.gitdir } : {}),
|
|
86
|
+
});
|
|
87
|
+
if (!opened.ok)
|
|
88
|
+
throw new OwnerGitError(409, `no usable git repository: ${opened.reason}`);
|
|
89
|
+
return { fs: nodeFs, dir: opened.repo.root, gitdir: opened.repo.gitdir };
|
|
90
|
+
}
|
|
91
|
+
async function configuredAuthor(repo) {
|
|
92
|
+
const name = await getConfig({ ...repo, path: "user.name" }).catch(() => undefined);
|
|
93
|
+
const email = await getConfig({ ...repo, path: "user.email" }).catch(() => undefined);
|
|
94
|
+
return typeof name === "string" && name && typeof email === "string" && email
|
|
95
|
+
? { name, email }
|
|
96
|
+
: null;
|
|
97
|
+
}
|
|
98
|
+
/** Staged or unstaged changes to tracked files, and anything staged at all. */
|
|
99
|
+
async function workingTreeState(repo) {
|
|
100
|
+
const rows = await statusMatrix({ ...repo });
|
|
101
|
+
const dirty = [];
|
|
102
|
+
const staged = [];
|
|
103
|
+
// statusMatrix: head 0/1 absent/present; workdir 1 = same as HEAD; stage 1 = same as HEAD, 0 = absent.
|
|
104
|
+
for (const [file, head, workdir, stage] of rows) {
|
|
105
|
+
const tracked = head === 1;
|
|
106
|
+
const isStaged = tracked ? stage !== 1 : stage !== 0;
|
|
107
|
+
if (isStaged)
|
|
108
|
+
staged.push(file);
|
|
109
|
+
if (isStaged || (tracked && workdir !== 1))
|
|
110
|
+
dirty.push(file);
|
|
111
|
+
}
|
|
112
|
+
return { dirty, staged };
|
|
113
|
+
}
|
|
114
|
+
async function status(ctx) {
|
|
115
|
+
const repo = await repoFor(ctx);
|
|
116
|
+
const state = await workingTreeState(repo);
|
|
117
|
+
return {
|
|
118
|
+
branch: (await currentBranch({ ...repo, fullname: false })) || null,
|
|
119
|
+
branches: await listBranches({ ...repo }),
|
|
120
|
+
author: await configuredAuthor(repo),
|
|
121
|
+
dirtyFiles: state.dirty.slice(0, 50),
|
|
122
|
+
dirtyCount: state.dirty.length,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Resolves a world path to the real file it names, refusing anything a
|
|
127
|
+
* world file couldn't be: outside the served directory (traversal or a
|
|
128
|
+
* symlink hop anywhere on the way), inside the git directory, or not a
|
|
129
|
+
* regular text file the world was converted from.
|
|
130
|
+
*/
|
|
131
|
+
async function confinedWorldFile(ctx, repo, path) {
|
|
132
|
+
if (!ctx.worldText().has(path))
|
|
133
|
+
throw new OwnerGitError(403, `"${path}" is not a text file in this world`);
|
|
134
|
+
try {
|
|
135
|
+
return await resolveExistingOwnerFile(ctx.dir, path, repo.gitdir);
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
if (err instanceof OwnerPathError)
|
|
139
|
+
throw new OwnerGitError(err.status === 404 ? 404 : 403, err.message.startsWith('"') ? err.message : `"${path}": ${err.message}`);
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function commitEdits(ctx, body) {
|
|
144
|
+
const repo = await repoFor(ctx);
|
|
145
|
+
const author = body.author ?? (await configuredAuthor(repo));
|
|
146
|
+
if (!author)
|
|
147
|
+
throw new OwnerGitError(409, "this repository has no user.name/user.email; enter them to commit", { needsAuthor: true });
|
|
148
|
+
const paths = new Set(body.files.map((f) => f.path));
|
|
149
|
+
if (paths.size !== body.files.length)
|
|
150
|
+
throw new OwnerGitError(400, "each file may appear once");
|
|
151
|
+
const { staged } = await workingTreeState(repo);
|
|
152
|
+
const otherStaged = staged.filter((f) => !paths.has(f));
|
|
153
|
+
if (otherStaged.length > 0)
|
|
154
|
+
throw new OwnerGitError(409, `other changes are already staged (${otherStaged.slice(0, 5).join(", ")}); commit or unstage them in a terminal first`);
|
|
155
|
+
const targets = [];
|
|
156
|
+
for (const file of body.files) {
|
|
157
|
+
const real = await confinedWorldFile(ctx, repo, file.path);
|
|
158
|
+
const onDisk = await readFile(real, "utf8");
|
|
159
|
+
if (onDisk !== ctx.worldText().get(file.path))
|
|
160
|
+
throw new OwnerGitError(409, `"${file.path}" changed on disk since cabn serve converted it; reload to pick up the new version`);
|
|
161
|
+
targets.push({ real, path: file.path, content: file.content });
|
|
162
|
+
}
|
|
163
|
+
for (const t of targets)
|
|
164
|
+
await overwriteFileAtomic(t.real, t.content);
|
|
165
|
+
for (const t of targets)
|
|
166
|
+
await add({ ...repo, filepath: t.path });
|
|
167
|
+
const oid = await commit({ ...repo, message: body.message, author });
|
|
168
|
+
return { oid, files: targets.map((t) => t.path) };
|
|
169
|
+
}
|
|
170
|
+
async function createBranch(ctx, body) {
|
|
171
|
+
const repo = await repoFor(ctx);
|
|
172
|
+
if ((await listBranches({ ...repo })).includes(body.name))
|
|
173
|
+
throw new OwnerGitError(409, `branch "${body.name}" already exists`);
|
|
174
|
+
// A new branch starts at HEAD, so checking it out moves no files.
|
|
175
|
+
await branch({ ...repo, ref: body.name, checkout: body.checkout ?? false });
|
|
176
|
+
return { branch: body.name, checkedOut: body.checkout ?? false };
|
|
177
|
+
}
|
|
178
|
+
async function switchBranch(ctx, body) {
|
|
179
|
+
const repo = await repoFor(ctx);
|
|
180
|
+
if (!(await listBranches({ ...repo })).includes(body.branch))
|
|
181
|
+
throw new OwnerGitError(404, `no local branch "${body.branch}"`);
|
|
182
|
+
const { dirty } = await workingTreeState(repo);
|
|
183
|
+
if (dirty.length > 0)
|
|
184
|
+
throw new OwnerGitError(409, `the working tree has uncommitted changes (${dirty.slice(0, 5).join(", ")}); commit them first`);
|
|
185
|
+
try {
|
|
186
|
+
await checkout({ ...repo, ref: body.branch });
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
throw new OwnerGitError(409, `checkout refused: ${redactRepoPaths(err.message, { root: repo.dir, gitdir: repo.gitdir })}`);
|
|
190
|
+
}
|
|
191
|
+
return { branch: body.branch };
|
|
192
|
+
}
|
|
193
|
+
let queue = Promise.resolve();
|
|
194
|
+
// One git operation at a time: two overlapping requests must never interleave index writes.
|
|
195
|
+
function serialized(work) {
|
|
196
|
+
const next = queue.then(work, work);
|
|
197
|
+
queue = next.catch(() => undefined);
|
|
198
|
+
return next;
|
|
199
|
+
}
|
|
200
|
+
/** Returns false when the path isn't an owner-git route (the caller 404s). */
|
|
201
|
+
export async function handleOwnerGit(req, res, pathname, ctx) {
|
|
202
|
+
const route = Object.entries(OWNER_GIT_ROUTES).find(([, p]) => p === pathname)?.[0];
|
|
203
|
+
if (!route)
|
|
204
|
+
return false;
|
|
205
|
+
const gate = checkOwnerRequest(req, {
|
|
206
|
+
port: ctx.port(),
|
|
207
|
+
token: ctx.ownerToken,
|
|
208
|
+
method: route === "status" ? "GET" : "POST",
|
|
209
|
+
});
|
|
210
|
+
if (!gate.ok) {
|
|
211
|
+
sendOwnerJson(res, gate.status, { error: gate.reason });
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
let result;
|
|
216
|
+
if (route === "status") {
|
|
217
|
+
result = await serialized(() => status(ctx));
|
|
218
|
+
}
|
|
219
|
+
else if (route === "commit") {
|
|
220
|
+
const body = await readOwnerJson(req, CommitBodySchema, OWNER_GIT_COMMIT_MAX_BYTES);
|
|
221
|
+
if (!body.ok)
|
|
222
|
+
return sendError(res, body.status, body.reason);
|
|
223
|
+
result = await serialized(async () => {
|
|
224
|
+
const out = await commitEdits(ctx, body.data);
|
|
225
|
+
await ctx.reconvert();
|
|
226
|
+
return out;
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
else if (route === "branch") {
|
|
230
|
+
const body = await readOwnerJson(req, BranchBodySchema);
|
|
231
|
+
if (!body.ok)
|
|
232
|
+
return sendError(res, body.status, body.reason);
|
|
233
|
+
result = await serialized(async () => {
|
|
234
|
+
const out = await createBranch(ctx, body.data);
|
|
235
|
+
await ctx.reconvert();
|
|
236
|
+
return out;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
const body = await readOwnerJson(req, CheckoutBodySchema);
|
|
241
|
+
if (!body.ok)
|
|
242
|
+
return sendError(res, body.status, body.reason);
|
|
243
|
+
result = await serialized(async () => {
|
|
244
|
+
const out = await switchBranch(ctx, body.data);
|
|
245
|
+
await ctx.reconvert();
|
|
246
|
+
return out;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
sendOwnerJson(res, 200, result);
|
|
250
|
+
}
|
|
251
|
+
catch (err) {
|
|
252
|
+
if (err instanceof OwnerGitError) {
|
|
253
|
+
sendOwnerJson(res, err.status, { error: err.message, ...err.extra });
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
console.error("cabn serve: owner git error", err);
|
|
257
|
+
sendOwnerJson(res, 500, { error: "git operation failed" });
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
function sendError(res, status, message) {
|
|
263
|
+
sendOwnerJson(res, status, { error: message });
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { type WorldBundle } from "@cabn/converter";
|
|
3
|
+
import type { SignEntry, WorldLayerDelta, WorldManifest } from "@cabn/world-schema";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
export declare const OWNER_SHADOW_ROUTES: {
|
|
6
|
+
readonly manifest: "/owner/shadow/manifest";
|
|
7
|
+
readonly searchIndex: "/owner/shadow/search-index";
|
|
8
|
+
readonly save: "/owner/shadow/save";
|
|
9
|
+
/** Followed by the URL-encoded cluster id. */
|
|
10
|
+
readonly chunkPrefix: "/owner/shadow/chunk/";
|
|
11
|
+
};
|
|
12
|
+
export declare const ShadowSaveBodySchema: z.ZodObject<{
|
|
13
|
+
path: z.ZodString;
|
|
14
|
+
content: z.ZodString;
|
|
15
|
+
baseSha256: z.ZodString;
|
|
16
|
+
}, z.core.$strict>;
|
|
17
|
+
export interface ShadowRealmOptions {
|
|
18
|
+
dir: string;
|
|
19
|
+
/** `--git-dir`, when given. */
|
|
20
|
+
gitdir?: string;
|
|
21
|
+
/** The currently served bundle (replaced wholesale on reconvert). */
|
|
22
|
+
bundle(): WorldBundle;
|
|
23
|
+
}
|
|
24
|
+
export declare class ShadowRealm {
|
|
25
|
+
private readonly opts;
|
|
26
|
+
private cached;
|
|
27
|
+
private pending;
|
|
28
|
+
private generation;
|
|
29
|
+
constructor(opts: ShadowRealmOptions);
|
|
30
|
+
baseManifest(): WorldManifest;
|
|
31
|
+
/** Forgets the layer; the next request recomputes it. Called on every reconvert. */
|
|
32
|
+
clear(): void;
|
|
33
|
+
/** The cached layer, if it still matches the served base world. */
|
|
34
|
+
peek(): WorldLayerDelta | undefined;
|
|
35
|
+
delta(): Promise<WorldLayerDelta>;
|
|
36
|
+
/** The git directory (for the not-in-gitdir check) and the paths git tracks (magpies on secret-named hidden files). No repository: neither. */
|
|
37
|
+
repo(): Promise<{
|
|
38
|
+
gitdir?: string;
|
|
39
|
+
tracked: Set<string>;
|
|
40
|
+
}>;
|
|
41
|
+
/** A shadow sign was written: resolve its anchor against base ∪ layer and put it in the cached layer only. */
|
|
42
|
+
putSign(path: string, content: string): Promise<SignEntry | null>;
|
|
43
|
+
dropSign(path: string): void;
|
|
44
|
+
}
|
|
45
|
+
export interface OwnerShadowContext {
|
|
46
|
+
dir: string;
|
|
47
|
+
port(): number;
|
|
48
|
+
ownerToken: string;
|
|
49
|
+
shadow: ShadowRealm;
|
|
50
|
+
}
|
|
51
|
+
/** Returns false when the path isn't a shadow route (the caller 404s). */
|
|
52
|
+
export declare function handleOwnerShadow(req: IncomingMessage, res: ServerResponse, pathname: string, ctx: OwnerShadowContext): Promise<boolean>;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `cabn serve --owner`'s shadow realm: the hidden files (dotfiles and
|
|
3
|
+
* dot-folders) every normal world leaves out, served to the owner as a world
|
|
4
|
+
* layer over the base world. The layer is computed lazily on the first
|
|
5
|
+
* manifest request and cached here, in the serve process — it is never
|
|
6
|
+
* written into the served bundle, so nothing hidden is ever reachable under
|
|
7
|
+
* `/world/*`. Every route goes through ownerAuth.ts's gate and answers
|
|
8
|
+
* `Cache-Control: no-store`; without `--owner` none of them exist.
|
|
9
|
+
*
|
|
10
|
+
* Edits save to disk only — never staged or committed — and only to hidden
|
|
11
|
+
* text files that already exist; nothing here creates or deletes a file.
|
|
12
|
+
*/
|
|
13
|
+
import { createHash } from "node:crypto";
|
|
14
|
+
import * as nodeFs from "node:fs";
|
|
15
|
+
import { readFile } from "node:fs/promises";
|
|
16
|
+
import { buildSignEntry, convertShadow, DEFAULT_MAX_FILE_BYTES, DirSource, isHiddenPath, openGitRepo, } from "@cabn/converter";
|
|
17
|
+
import { listFiles } from "isomorphic-git";
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
import { checkOwnerRequest, OwnerPathError, overwriteFileAtomic, readOwnerJson, resolveExistingOwnerFile, sendOwnerJson, } from "./ownerAuth.js";
|
|
20
|
+
export const OWNER_SHADOW_ROUTES = {
|
|
21
|
+
manifest: "/owner/shadow/manifest",
|
|
22
|
+
searchIndex: "/owner/shadow/search-index",
|
|
23
|
+
save: "/owner/shadow/save",
|
|
24
|
+
/** Followed by the URL-encoded cluster id. */
|
|
25
|
+
chunkPrefix: "/owner/shadow/chunk/",
|
|
26
|
+
};
|
|
27
|
+
/** A save carries one whole file, so it gets more room than the shared 128 KB default. */
|
|
28
|
+
const OWNER_SHADOW_SAVE_MAX_BYTES = 2 * 1024 * 1024;
|
|
29
|
+
export const ShadowSaveBodySchema = z.strictObject({
|
|
30
|
+
path: z.string().min(1).max(512),
|
|
31
|
+
content: z.string(),
|
|
32
|
+
baseSha256: z.string().regex(/^[0-9a-f]{64}$/, "not a sha256 hex digest"),
|
|
33
|
+
});
|
|
34
|
+
function sha256Hex(bytes) {
|
|
35
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
36
|
+
}
|
|
37
|
+
export class ShadowRealm {
|
|
38
|
+
opts;
|
|
39
|
+
cached;
|
|
40
|
+
pending;
|
|
41
|
+
// Bumped by clear(): a computation that started before a reconvert must
|
|
42
|
+
// not be cached after it.
|
|
43
|
+
generation = 0;
|
|
44
|
+
constructor(opts) {
|
|
45
|
+
this.opts = opts;
|
|
46
|
+
}
|
|
47
|
+
baseManifest() {
|
|
48
|
+
const raw = this.opts.bundle().get("world.json");
|
|
49
|
+
if (typeof raw !== "string")
|
|
50
|
+
throw new Error("no world.json to extend");
|
|
51
|
+
return JSON.parse(raw);
|
|
52
|
+
}
|
|
53
|
+
/** Forgets the layer; the next request recomputes it. Called on every reconvert. */
|
|
54
|
+
clear() {
|
|
55
|
+
this.generation++;
|
|
56
|
+
this.cached = undefined;
|
|
57
|
+
this.pending = undefined;
|
|
58
|
+
}
|
|
59
|
+
/** The cached layer, if it still matches the served base world. */
|
|
60
|
+
peek() {
|
|
61
|
+
const base = this.baseManifest();
|
|
62
|
+
return this.cached?.baseGeneratedAt === base.meta.generatedAt
|
|
63
|
+
? this.cached
|
|
64
|
+
: undefined;
|
|
65
|
+
}
|
|
66
|
+
async delta() {
|
|
67
|
+
const fresh = this.peek();
|
|
68
|
+
if (fresh)
|
|
69
|
+
return fresh;
|
|
70
|
+
if (this.pending)
|
|
71
|
+
return this.pending;
|
|
72
|
+
const generation = this.generation;
|
|
73
|
+
const base = this.baseManifest();
|
|
74
|
+
const work = (async () => {
|
|
75
|
+
const repo = await this.repo();
|
|
76
|
+
const delta = await convertShadow(new DirSource(this.opts.dir), base, {
|
|
77
|
+
trackedPaths: repo.tracked,
|
|
78
|
+
});
|
|
79
|
+
if (generation === this.generation)
|
|
80
|
+
this.cached = delta;
|
|
81
|
+
return delta;
|
|
82
|
+
})();
|
|
83
|
+
this.pending = work;
|
|
84
|
+
try {
|
|
85
|
+
return await work;
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
if (this.pending === work)
|
|
89
|
+
this.pending = undefined;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** The git directory (for the not-in-gitdir check) and the paths git tracks (magpies on secret-named hidden files). No repository: neither. */
|
|
93
|
+
async repo() {
|
|
94
|
+
const opened = await openGitRepo({
|
|
95
|
+
fs: nodeFs,
|
|
96
|
+
dir: this.opts.dir,
|
|
97
|
+
...(this.opts.gitdir ? { gitdir: this.opts.gitdir } : {}),
|
|
98
|
+
});
|
|
99
|
+
if (!opened.ok)
|
|
100
|
+
return { tracked: new Set() };
|
|
101
|
+
const { root, gitdir } = opened.repo;
|
|
102
|
+
try {
|
|
103
|
+
const files = await listFiles({ fs: nodeFs, dir: root, gitdir });
|
|
104
|
+
return { gitdir, tracked: new Set(files) };
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return { gitdir, tracked: new Set() };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** A shadow sign was written: resolve its anchor against base ∪ layer and put it in the cached layer only. */
|
|
111
|
+
async putSign(path, content) {
|
|
112
|
+
const delta = await this.delta();
|
|
113
|
+
const base = this.baseManifest();
|
|
114
|
+
const built = buildSignEntry(path, content, {
|
|
115
|
+
portalIds: new Set([
|
|
116
|
+
...base.portals.map((p) => p.id),
|
|
117
|
+
...delta.portals.map((p) => p.id),
|
|
118
|
+
]),
|
|
119
|
+
clusters: [...base.clusters, ...delta.clusters],
|
|
120
|
+
});
|
|
121
|
+
if (!built)
|
|
122
|
+
return null;
|
|
123
|
+
delta.signs = [
|
|
124
|
+
...delta.signs.filter((s) => s.path !== path),
|
|
125
|
+
built.entry,
|
|
126
|
+
].sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
127
|
+
return built.entry;
|
|
128
|
+
}
|
|
129
|
+
dropSign(path) {
|
|
130
|
+
if (this.cached)
|
|
131
|
+
this.cached.signs = this.cached.signs.filter((s) => s.path !== path);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
class ShadowRouteError extends Error {
|
|
135
|
+
status;
|
|
136
|
+
extra;
|
|
137
|
+
constructor(status, message, extra = {}) {
|
|
138
|
+
super(message);
|
|
139
|
+
this.status = status;
|
|
140
|
+
this.extra = extra;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
let queue = Promise.resolve();
|
|
144
|
+
// One save at a time: the hash check and the write must not interleave with another save.
|
|
145
|
+
function serialized(work) {
|
|
146
|
+
const next = queue.then(work, work);
|
|
147
|
+
queue = next.catch(() => undefined);
|
|
148
|
+
return next;
|
|
149
|
+
}
|
|
150
|
+
function layerManifest(delta) {
|
|
151
|
+
const { chunks: _chunks, searchIndex: _searchIndex, ...manifest } = delta;
|
|
152
|
+
return manifest;
|
|
153
|
+
}
|
|
154
|
+
function chunkIdFrom(pathname) {
|
|
155
|
+
let id;
|
|
156
|
+
try {
|
|
157
|
+
id = decodeURIComponent(pathname.slice(OWNER_SHADOW_ROUTES.chunkPrefix.length));
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
throw new ShadowRouteError(400, "malformed cluster id");
|
|
161
|
+
}
|
|
162
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: rejecting them is the point
|
|
163
|
+
if (id.length === 0 || id.length > 512 || /[\u0000-\u001F\u007F]/.test(id))
|
|
164
|
+
throw new ShadowRouteError(400, "malformed cluster id");
|
|
165
|
+
return id;
|
|
166
|
+
}
|
|
167
|
+
async function saveHiddenFile(ctx, body) {
|
|
168
|
+
const { path, content, baseSha256 } = body;
|
|
169
|
+
if (Buffer.byteLength(content, "utf8") > DEFAULT_MAX_FILE_BYTES)
|
|
170
|
+
throw new ShadowRouteError(413, `a file is at most ${DEFAULT_MAX_FILE_BYTES} bytes`);
|
|
171
|
+
const delta = await ctx.shadow.delta();
|
|
172
|
+
if (!Object.hasOwn(delta.textSha256, path) || !isHiddenPath(path))
|
|
173
|
+
throw new ShadowRouteError(403, `"${path}" is not a hidden text file in the shadow realm`);
|
|
174
|
+
const { gitdir } = await ctx.shadow.repo();
|
|
175
|
+
const real = await resolveExistingOwnerFile(ctx.dir, path, gitdir);
|
|
176
|
+
const current = sha256Hex(await readFile(real));
|
|
177
|
+
if (current !== baseSha256) {
|
|
178
|
+
// The cached layer holds the old text too: without this a
|
|
179
|
+
// "reload from disk" would be served the same stale copy.
|
|
180
|
+
ctx.shadow.clear();
|
|
181
|
+
throw new ShadowRouteError(409, `"${path}" changed on disk since it was loaded; reload the shadow realm`, { currentSha256: current });
|
|
182
|
+
}
|
|
183
|
+
await overwriteFileAtomic(real, content);
|
|
184
|
+
// Contents, previews and monsters all changed: recompute on next request.
|
|
185
|
+
ctx.shadow.clear();
|
|
186
|
+
return { path, sha256: sha256Hex(content) };
|
|
187
|
+
}
|
|
188
|
+
/** Returns false when the path isn't a shadow route (the caller 404s). */
|
|
189
|
+
export async function handleOwnerShadow(req, res, pathname, ctx) {
|
|
190
|
+
const isChunk = pathname.startsWith(OWNER_SHADOW_ROUTES.chunkPrefix);
|
|
191
|
+
const route = isChunk
|
|
192
|
+
? "chunk"
|
|
193
|
+
: pathname === OWNER_SHADOW_ROUTES.manifest
|
|
194
|
+
? "manifest"
|
|
195
|
+
: pathname === OWNER_SHADOW_ROUTES.searchIndex
|
|
196
|
+
? "searchIndex"
|
|
197
|
+
: pathname === OWNER_SHADOW_ROUTES.save
|
|
198
|
+
? "save"
|
|
199
|
+
: undefined;
|
|
200
|
+
if (!route)
|
|
201
|
+
return false;
|
|
202
|
+
const gate = checkOwnerRequest(req, {
|
|
203
|
+
port: ctx.port(),
|
|
204
|
+
token: ctx.ownerToken,
|
|
205
|
+
method: route === "save" ? "POST" : "GET",
|
|
206
|
+
});
|
|
207
|
+
if (!gate.ok) {
|
|
208
|
+
sendOwnerJson(res, gate.status, { error: gate.reason });
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
if (route === "manifest") {
|
|
213
|
+
sendOwnerJson(res, 200, layerManifest(await ctx.shadow.delta()));
|
|
214
|
+
}
|
|
215
|
+
else if (route === "searchIndex") {
|
|
216
|
+
sendOwnerJson(res, 200, (await ctx.shadow.delta()).searchIndex);
|
|
217
|
+
}
|
|
218
|
+
else if (route === "chunk") {
|
|
219
|
+
const id = chunkIdFrom(pathname);
|
|
220
|
+
const delta = await ctx.shadow.delta();
|
|
221
|
+
if (!Object.hasOwn(delta.chunks, id))
|
|
222
|
+
throw new ShadowRouteError(404, "no such shadow cluster");
|
|
223
|
+
sendOwnerJson(res, 200, delta.chunks[id]);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
const body = await readOwnerJson(req, ShadowSaveBodySchema, OWNER_SHADOW_SAVE_MAX_BYTES);
|
|
227
|
+
if (!body.ok) {
|
|
228
|
+
sendOwnerJson(res, body.status, { error: body.reason });
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
const data = body.data;
|
|
232
|
+
sendOwnerJson(res, 200, await serialized(() => saveHiddenFile(ctx, data)));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
catch (err) {
|
|
236
|
+
if (err instanceof ShadowRouteError) {
|
|
237
|
+
sendOwnerJson(res, err.status, { error: err.message, ...err.extra });
|
|
238
|
+
}
|
|
239
|
+
else if (err instanceof OwnerPathError) {
|
|
240
|
+
sendOwnerJson(res, err.status, { error: err.message });
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
// Name and code only: a message could quote file contents.
|
|
244
|
+
const e = err;
|
|
245
|
+
console.error(`cabn serve: owner shadow request failed (${e?.name ?? "Error"}${e?.code ? ` ${e.code}` : ""})`);
|
|
246
|
+
sendOwnerJson(res, 500, { error: "shadow realm request failed" });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { type WorldBundle } from "@cabn/converter";
|
|
3
|
+
import type { ShadowRealm } from "./ownerShadow.js";
|
|
4
|
+
export declare const OWNER_SIGNS_SAVE_ROUTE = "/owner/signs/save";
|
|
5
|
+
export declare const OWNER_SIGNS_DELETE_ROUTE = "/owner/signs/delete";
|
|
6
|
+
/**
|
|
7
|
+
* Stricter than ownerAuth's generic confinement: plain names only (the same
|
|
8
|
+
* rule the engine's editor checks, systems/signs.ts isValidSignFileName), no
|
|
9
|
+
* hidden folders (.git and friends), and nothing inside a folder the
|
|
10
|
+
* converter ignores — a sign saved there would vanish on the next start.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isAllowedSignPath(path: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* The shadow realm's rule: the sign must sit in a hidden folder — that is
|
|
15
|
+
* what makes it a shadow sign — but never in `.git` or a folder the
|
|
16
|
+
* converter ignores (`.venv`, `node_modules`, ...), both case-folded for
|
|
17
|
+
* case-insensitive file systems. The file name still starts with a letter
|
|
18
|
+
* or digit.
|
|
19
|
+
*/
|
|
20
|
+
export declare function isAllowedShadowSignPath(path: string): boolean;
|
|
21
|
+
export interface OwnerSignsContext {
|
|
22
|
+
dir: string;
|
|
23
|
+
port: number;
|
|
24
|
+
ownerToken: string;
|
|
25
|
+
bundle: WorldBundle;
|
|
26
|
+
/** Owner mode's shadow realm; a `realm: "shadow"` request without one is refused. */
|
|
27
|
+
shadow?: ShadowRealm;
|
|
28
|
+
}
|
|
29
|
+
/** Returns true when it handled the request (any owner-signs route); false for every other path, which the caller routes as before. */
|
|
30
|
+
export declare function handleOwnerSignsRoute(req: IncomingMessage, res: ServerResponse, pathname: string, ctx: OwnerSignsContext): Promise<boolean>;
|