@jxsuite/server 9.3.0 → 9.4.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/README.md +11 -8
- package/package.json +22 -18
- package/src/class-schema.ts +201 -0
- package/src/code-api.ts +195 -92
- package/src/collab.ts +58 -37
- package/src/data-api.ts +17 -52
- package/src/dev.ts +6 -2
- package/src/extension-catalog.ts +4 -5
- package/src/jx-mounts.ts +78 -29
- package/src/live-preview.ts +10 -35
- package/src/packages.ts +20 -8
- package/src/preview-bundles.ts +93 -0
- package/src/project-server.ts +7 -2
- package/src/project-state/host.ts +2 -3
- package/src/refactor/scan.ts +5 -2
- package/src/resolve.ts +43 -3
- package/src/server.ts +12 -9
- package/src/site-output.ts +3 -1
- package/src/studio-api.ts +86 -315
- package/src/types.ts +2 -1
package/src/studio-api.ts
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
|
|
10
10
|
import { basename, dirname, extname, isAbsolute, join, relative, resolve } from "node:path";
|
|
11
11
|
import { componentMetaFrom } from "@jxsuite/schema/component-meta";
|
|
12
|
-
import {
|
|
12
|
+
import { discoverNpmComponents } from "@jxsuite/schema/npm-components";
|
|
13
|
+
import { errorMessage, parseClassDef, parseJxDocument } from "@jxsuite/schema/parse";
|
|
13
14
|
import { mkdir, readFile, readdir, rename, rmdir, stat, unlink, writeFile } from "node:fs/promises";
|
|
14
15
|
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
15
16
|
import { homedir } from "node:os";
|
|
@@ -37,6 +38,7 @@ import { isConditionalFileRequest } from "./project-events.ts";
|
|
|
37
38
|
import type { ProjectEventsService } from "./project-events.ts";
|
|
38
39
|
import {
|
|
39
40
|
addPackage,
|
|
41
|
+
declaredPackages,
|
|
40
42
|
dependenciesNeedInstall,
|
|
41
43
|
installDependencies,
|
|
42
44
|
packageVersions,
|
|
@@ -46,6 +48,7 @@ import {
|
|
|
46
48
|
import type { ExtensionRegistry } from "@jxsuite/schema/extension-registry";
|
|
47
49
|
import type { FormatRegistry } from "@jxsuite/schema/format-registry";
|
|
48
50
|
import type { ClassJsonDef } from "./types.ts";
|
|
51
|
+
import { extractStudioSchema } from "./class-schema.ts";
|
|
49
52
|
import type { DesignOptions, ProjectOptions } from "@jxsuite/create/generate";
|
|
50
53
|
import type { JsonObject, JsonPrimitive, JsonValue, ProjectConfig } from "@jxsuite/schema/types";
|
|
51
54
|
import { isFunction, isJsonObject, isNonNullObject, isString } from "@jxsuite/schema/guards";
|
|
@@ -87,33 +90,6 @@ interface PackageJson {
|
|
|
87
90
|
customElements?: string;
|
|
88
91
|
}
|
|
89
92
|
|
|
90
|
-
/** A custom-element-manifest declaration (subset consumed here). */
|
|
91
|
-
interface CemDeclaration {
|
|
92
|
-
customElement?: boolean;
|
|
93
|
-
tagName?: string;
|
|
94
|
-
description?: string;
|
|
95
|
-
cssProperties?: JsonValue[];
|
|
96
|
-
events?: JsonValue[];
|
|
97
|
-
slots?: JsonValue[];
|
|
98
|
-
/** Relayed verbatim once filtered on `kind` and `privacy`. */
|
|
99
|
-
members?: JsonObject[];
|
|
100
|
-
attributes?: {
|
|
101
|
-
name?: string;
|
|
102
|
-
default?: JsonValue;
|
|
103
|
-
description?: string;
|
|
104
|
-
type?: { text?: string };
|
|
105
|
-
}[];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
interface CemModule {
|
|
109
|
-
path?: string;
|
|
110
|
-
declarations?: CemDeclaration[];
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
interface Cem {
|
|
114
|
-
modules?: CemModule[];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
93
|
// ─── Extension registry (per project root, invalidated on a project file change) ──
|
|
118
94
|
|
|
119
95
|
const extensionRegistryCache = new Map<
|
|
@@ -178,6 +154,22 @@ export function assertAccessible(filePath: string, root: string, activeProjectRo
|
|
|
178
154
|
throw new Error("Path outside project root");
|
|
179
155
|
}
|
|
180
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Resolve the project directory a `/__studio/packages*` route works in: `dir` (resolved against the
|
|
159
|
+
* server root), else the active project, else the server root, refused when it lies outside both
|
|
160
|
+
* (server.md §4.2). Throws before any `bun` is spawned.
|
|
161
|
+
*/
|
|
162
|
+
function packageDir(
|
|
163
|
+
dir: string | null | undefined,
|
|
164
|
+
root: string,
|
|
165
|
+
activeProjectRoot: string | null,
|
|
166
|
+
): string {
|
|
167
|
+
const chosen = dir || activeProjectRoot || root;
|
|
168
|
+
const abs = isAbsolute(chosen) ? chosen : resolve(root, chosen);
|
|
169
|
+
assertAccessible(abs, root, activeProjectRoot);
|
|
170
|
+
return abs;
|
|
171
|
+
}
|
|
172
|
+
|
|
181
173
|
/**
|
|
182
174
|
* Check that a NEW project may be scaffolded under `parent` (specs/server.md §4.2).
|
|
183
175
|
*
|
|
@@ -381,10 +373,9 @@ export interface StudioApiOptions {
|
|
|
381
373
|
* uses (`server.ts`), handed to the one mutation the watcher cannot describe.
|
|
382
374
|
*
|
|
383
375
|
* A rename MOVES a file, and a live room is keyed by the old path. Without this the room survives
|
|
384
|
-
* the move holding pre-rename content, and
|
|
385
|
-
* RECREATING the file the rename deleted.
|
|
386
|
-
*
|
|
387
|
-
* path lets the registry reset that room, which deletes it and makes the later flush a no-op.
|
|
376
|
+
* the move holding pre-rename content, and the next save from anyone in it writes that back —
|
|
377
|
+
* RECREATING the file the rename deleted. Passing the OLD absolute path lets the registry reset
|
|
378
|
+
* that room, so a later save from a peer of the old path writes nothing.
|
|
388
379
|
*/
|
|
389
380
|
onFileMoved?: (absPath: string) => void;
|
|
390
381
|
/**
|
|
@@ -830,8 +821,8 @@ export async function handleStudioApi(
|
|
|
830
821
|
try {
|
|
831
822
|
let content: JsonValue;
|
|
832
823
|
if (match.endsWith(".json")) {
|
|
833
|
-
// SAFETY:
|
|
834
|
-
content =
|
|
824
|
+
// SAFETY: a parsed document is a JSON object, which is a JsonValue.
|
|
825
|
+
content = parseJxDocument(await readFile(fp, "utf8"), fwd(match)) as JsonObject;
|
|
835
826
|
} else {
|
|
836
827
|
const entry = registry.byExtension(extname(match), "parse");
|
|
837
828
|
if (!entry) {
|
|
@@ -855,84 +846,9 @@ export async function handleStudioApi(
|
|
|
855
846
|
} catch {} // Skip non-JSON or parse errors
|
|
856
847
|
}
|
|
857
848
|
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
if (existsSync(projectPkgPath)) {
|
|
862
|
-
/* SAFETY: only the keys of the dependency maps are used, each as a package name that must
|
|
863
|
-
resolve to node_modules/<name>/package.json before anything else is read, so a name
|
|
864
|
-
that does not is skipped; a throw skips this whole scan. */
|
|
865
|
-
const pkg = JSON.parse(await readFile(projectPkgPath, "utf8")) as PackageJson;
|
|
866
|
-
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
867
|
-
for (const name of Object.keys(deps)) {
|
|
868
|
-
try {
|
|
869
|
-
const depPkgPath = resolve(
|
|
870
|
-
scanRoot,
|
|
871
|
-
"node_modules",
|
|
872
|
-
...name.split("/"),
|
|
873
|
-
"package.json",
|
|
874
|
-
);
|
|
875
|
-
// Fall back to root node_modules for hoisted packages
|
|
876
|
-
const fallbackPath = resolve(
|
|
877
|
-
root,
|
|
878
|
-
"node_modules",
|
|
879
|
-
...name.split("/"),
|
|
880
|
-
"package.json",
|
|
881
|
-
);
|
|
882
|
-
const actualPath = existsSync(depPkgPath)
|
|
883
|
-
? depPkgPath
|
|
884
|
-
: existsSync(fallbackPath)
|
|
885
|
-
? fallbackPath
|
|
886
|
-
: null;
|
|
887
|
-
if (!actualPath) {
|
|
888
|
-
continue;
|
|
889
|
-
}
|
|
890
|
-
/* SAFETY: `customElements` is the one member read; npm defines it as the manifest
|
|
891
|
-
path, and a non-string value makes resolve() throw into this package's catch. */
|
|
892
|
-
const depPkg = JSON.parse(await readFile(actualPath, "utf8")) as PackageJson;
|
|
893
|
-
if (!depPkg.customElements) {
|
|
894
|
-
continue;
|
|
895
|
-
}
|
|
896
|
-
const cemPath = resolve(dirname(actualPath), depPkg.customElements);
|
|
897
|
-
if (!existsSync(cemPath)) {
|
|
898
|
-
continue;
|
|
899
|
-
}
|
|
900
|
-
/* SAFETY: the package names this file as its custom-elements manifest, and every
|
|
901
|
-
member read below has a fallback; a manifest of another shape either throws into
|
|
902
|
-
this package's catch or contributes no declarations. */
|
|
903
|
-
const cem = JSON.parse(await readFile(cemPath, "utf8")) as Cem;
|
|
904
|
-
for (const mod of cem.modules || []) {
|
|
905
|
-
for (const decl of mod.declarations || []) {
|
|
906
|
-
if (decl.customElement && decl.tagName) {
|
|
907
|
-
components.push({
|
|
908
|
-
$id: null,
|
|
909
|
-
cssProperties: decl.cssProperties || [],
|
|
910
|
-
description: decl.description || null,
|
|
911
|
-
events: decl.events || [],
|
|
912
|
-
hasElements: false,
|
|
913
|
-
members: (decl.members || []).filter(
|
|
914
|
-
(m) => m.kind === "field" && m.privacy !== "private",
|
|
915
|
-
),
|
|
916
|
-
modulePath: mod.path,
|
|
917
|
-
package: name,
|
|
918
|
-
path: null,
|
|
919
|
-
props: (decl.attributes || []).map((a) => ({
|
|
920
|
-
default: a.default,
|
|
921
|
-
description: a.description || null,
|
|
922
|
-
name: a.name,
|
|
923
|
-
type: a.type?.text,
|
|
924
|
-
})),
|
|
925
|
-
slots: decl.slots || [],
|
|
926
|
-
source: "npm",
|
|
927
|
-
tagName: decl.tagName,
|
|
928
|
-
});
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
} catch {} // Skip packages without valid CEM
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
} catch {} // Skip if no project package.json
|
|
849
|
+
/* Dependencies' custom elements: the scan shared with the desktop session. `roots` falls back
|
|
850
|
+
to the server root for a hoisted install, as this route always has. */
|
|
851
|
+
components.push(...(await discoverNpmComponents(scanRoot, { roots: [scanRoot, root] })));
|
|
936
852
|
|
|
937
853
|
return Response.json(components);
|
|
938
854
|
} catch (error) {
|
|
@@ -1067,10 +983,14 @@ export async function handleStudioApi(
|
|
|
1067
983
|
|
|
1068
984
|
// ─── Package management ──────────────────────────────────────────────────────
|
|
1069
985
|
|
|
1070
|
-
//
|
|
986
|
+
// The dependencies package.json declares, installed or not (imports.md §4.2)
|
|
1071
987
|
if (path === "/__studio/packages" && req.method === "GET") {
|
|
1072
|
-
|
|
1073
|
-
|
|
988
|
+
let scanRoot: string;
|
|
989
|
+
try {
|
|
990
|
+
scanRoot = packageDir(url.searchParams.get("dir"), root, activeProjectRoot);
|
|
991
|
+
} catch (error) {
|
|
992
|
+
return problem("invalidRequest", errorMessage(error));
|
|
993
|
+
}
|
|
1074
994
|
try {
|
|
1075
995
|
const pkgPath = resolve(scanRoot, "package.json");
|
|
1076
996
|
if (!existsSync(pkgPath)) {
|
|
@@ -1079,39 +999,7 @@ export async function handleStudioApi(
|
|
|
1079
999
|
/* SAFETY: npm and bun write `dependencies` / `devDependencies` as name-to-range string maps,
|
|
1080
1000
|
which is all this route reads; a package.json that is not JSON throws into the catch. */
|
|
1081
1001
|
const pkg = JSON.parse(await readFile(pkgPath, "utf8")) as PackageJson;
|
|
1082
|
-
|
|
1083
|
-
const packages: {
|
|
1084
|
-
name: string;
|
|
1085
|
-
version: string;
|
|
1086
|
-
dev: boolean;
|
|
1087
|
-
hasCem: boolean;
|
|
1088
|
-
customElementsPath: string | null;
|
|
1089
|
-
}[] = [];
|
|
1090
|
-
for (const [name, version] of Object.entries(deps)) {
|
|
1091
|
-
const depPkgPath = resolve(scanRoot, "node_modules", ...name.split("/"), "package.json");
|
|
1092
|
-
const fallbackPath = resolve(root, "node_modules", ...name.split("/"), "package.json");
|
|
1093
|
-
const actualPath = existsSync(depPkgPath)
|
|
1094
|
-
? depPkgPath
|
|
1095
|
-
: existsSync(fallbackPath)
|
|
1096
|
-
? fallbackPath
|
|
1097
|
-
: null;
|
|
1098
|
-
if (!actualPath) {
|
|
1099
|
-
continue;
|
|
1100
|
-
}
|
|
1101
|
-
try {
|
|
1102
|
-
/* SAFETY: `customElements` is the one member read, and it is relayed verbatim or tested
|
|
1103
|
-
for truthiness; malformed JSON skips the package in the catch. */
|
|
1104
|
-
const depPkg = JSON.parse(await readFile(actualPath, "utf8")) as PackageJson;
|
|
1105
|
-
packages.push({
|
|
1106
|
-
customElementsPath: depPkg.customElements || null,
|
|
1107
|
-
dev: pkg.dependencies?.[name] === undefined,
|
|
1108
|
-
hasCem: Boolean(depPkg.customElements),
|
|
1109
|
-
name,
|
|
1110
|
-
version,
|
|
1111
|
-
});
|
|
1112
|
-
} catch {}
|
|
1113
|
-
}
|
|
1114
|
-
return Response.json(packages);
|
|
1002
|
+
return Response.json(declaredPackages(pkg));
|
|
1115
1003
|
} catch (error) {
|
|
1116
1004
|
return problem("internalError", errorMessage(error));
|
|
1117
1005
|
}
|
|
@@ -1159,15 +1047,19 @@ export async function handleStudioApi(
|
|
|
1159
1047
|
if (path === "/__studio/packages/add" && req.method === "POST") {
|
|
1160
1048
|
try {
|
|
1161
1049
|
/* SAFETY: `name` is re-checked with isString below; `dir` is only path-resolved and `dev`
|
|
1162
|
-
only tested for truthiness, and a wrongly typed `dir` throws in
|
|
1163
|
-
|
|
1050
|
+
only tested for truthiness, and a wrongly typed `dir` throws in packageDir and answers an
|
|
1051
|
+
invalidRequest problem. */
|
|
1164
1052
|
const body = (await req.json()) as { name?: string; dir?: string; dev?: boolean };
|
|
1165
1053
|
const { name } = body;
|
|
1166
1054
|
if (!name || !isString(name)) {
|
|
1167
1055
|
return problem("invalidRequest", "Missing name");
|
|
1168
1056
|
}
|
|
1169
|
-
|
|
1170
|
-
|
|
1057
|
+
let cwd: string;
|
|
1058
|
+
try {
|
|
1059
|
+
cwd = packageDir(body.dir, root, activeProjectRoot);
|
|
1060
|
+
} catch (error) {
|
|
1061
|
+
return problem("invalidRequest", errorMessage(error));
|
|
1062
|
+
}
|
|
1171
1063
|
const result = await addPackage(cwd, name, body.dev === true);
|
|
1172
1064
|
if (!result.ok) {
|
|
1173
1065
|
return problem("internalError", result.log ?? "Adding the package failed");
|
|
@@ -1182,14 +1074,18 @@ export async function handleStudioApi(
|
|
|
1182
1074
|
if (path === "/__studio/packages/remove" && req.method === "POST") {
|
|
1183
1075
|
try {
|
|
1184
1076
|
/* SAFETY: `name` is re-checked with isString below; a wrongly typed `dir` throws in
|
|
1185
|
-
|
|
1077
|
+
packageDir and answers an invalidRequest problem. */
|
|
1186
1078
|
const body = (await req.json()) as { name?: string; dir?: string };
|
|
1187
1079
|
const { name } = body;
|
|
1188
1080
|
if (!name || !isString(name)) {
|
|
1189
1081
|
return problem("invalidRequest", "Missing name");
|
|
1190
1082
|
}
|
|
1191
|
-
|
|
1192
|
-
|
|
1083
|
+
let cwd: string;
|
|
1084
|
+
try {
|
|
1085
|
+
cwd = packageDir(body.dir, root, activeProjectRoot);
|
|
1086
|
+
} catch (error) {
|
|
1087
|
+
return problem("invalidRequest", errorMessage(error));
|
|
1088
|
+
}
|
|
1193
1089
|
const result = await removePackage(cwd, name);
|
|
1194
1090
|
if (!result.ok) {
|
|
1195
1091
|
return problem("internalError", result.log ?? "Removing the package failed");
|
|
@@ -1204,10 +1100,14 @@ export async function handleStudioApi(
|
|
|
1204
1100
|
if (path === "/__studio/packages/install" && req.method === "POST") {
|
|
1205
1101
|
try {
|
|
1206
1102
|
/* SAFETY: `dir` is the only member read and is only path-resolved; a wrongly typed `dir`
|
|
1207
|
-
throws in
|
|
1103
|
+
throws in packageDir and answers an invalidRequest problem. */
|
|
1208
1104
|
const body = (await req.json().catch(() => ({}))) as { dir?: string };
|
|
1209
|
-
|
|
1210
|
-
|
|
1105
|
+
let scanRoot: string;
|
|
1106
|
+
try {
|
|
1107
|
+
scanRoot = packageDir(body.dir, root, activeProjectRoot);
|
|
1108
|
+
} catch (error) {
|
|
1109
|
+
return problem("invalidRequest", errorMessage(error));
|
|
1110
|
+
}
|
|
1211
1111
|
return Response.json(await installDependencies(scanRoot));
|
|
1212
1112
|
} catch (error) {
|
|
1213
1113
|
return problem("internalError", errorMessage(error));
|
|
@@ -1216,16 +1116,23 @@ export async function handleStudioApi(
|
|
|
1216
1116
|
|
|
1217
1117
|
// Whether dependencies need installing (node_modules missing)
|
|
1218
1118
|
if (path === "/__studio/packages/needs-install" && req.method === "GET") {
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1119
|
+
try {
|
|
1120
|
+
const scanRoot = packageDir(url.searchParams.get("dir"), root, activeProjectRoot);
|
|
1121
|
+
return Response.json({ needsInstall: dependenciesNeedInstall(scanRoot) });
|
|
1122
|
+
} catch (error) {
|
|
1123
|
+
return problem("invalidRequest", errorMessage(error));
|
|
1124
|
+
}
|
|
1222
1125
|
}
|
|
1223
1126
|
|
|
1224
1127
|
// The newest published version of each dependency (whether or not it is behind)
|
|
1225
1128
|
if (path === "/__studio/packages/versions" && req.method === "GET") {
|
|
1226
1129
|
try {
|
|
1227
|
-
|
|
1228
|
-
|
|
1130
|
+
let scanRoot: string;
|
|
1131
|
+
try {
|
|
1132
|
+
scanRoot = packageDir(url.searchParams.get("dir"), root, activeProjectRoot);
|
|
1133
|
+
} catch (error) {
|
|
1134
|
+
return problem("invalidRequest", errorMessage(error));
|
|
1135
|
+
}
|
|
1229
1136
|
return Response.json(await packageVersions(scanRoot));
|
|
1230
1137
|
} catch (error) {
|
|
1231
1138
|
return problem("internalError", errorMessage(error));
|
|
@@ -1237,7 +1144,7 @@ export async function handleStudioApi(
|
|
|
1237
1144
|
try {
|
|
1238
1145
|
/* SAFETY: `updates` is checked to be an array below and each entry is Studio's own
|
|
1239
1146
|
`{ name, version, dev }` triple from the versions panel; a wrongly typed `dir` throws in
|
|
1240
|
-
|
|
1147
|
+
packageDir and answers an invalidRequest problem. */
|
|
1241
1148
|
const body = (await req.json()) as {
|
|
1242
1149
|
dir?: string;
|
|
1243
1150
|
updates?: { name: string; version: string; dev?: boolean }[];
|
|
@@ -1245,8 +1152,12 @@ export async function handleStudioApi(
|
|
|
1245
1152
|
if (!Array.isArray(body.updates)) {
|
|
1246
1153
|
return problem("invalidRequest", "Missing updates");
|
|
1247
1154
|
}
|
|
1248
|
-
|
|
1249
|
-
|
|
1155
|
+
let scanRoot: string;
|
|
1156
|
+
try {
|
|
1157
|
+
scanRoot = packageDir(body.dir, root, activeProjectRoot);
|
|
1158
|
+
} catch (error) {
|
|
1159
|
+
return problem("invalidRequest", errorMessage(error));
|
|
1160
|
+
}
|
|
1250
1161
|
return Response.json(await setPackageVersions(scanRoot, body.updates));
|
|
1251
1162
|
} catch (error) {
|
|
1252
1163
|
return problem("internalError", errorMessage(error));
|
|
@@ -1440,7 +1351,7 @@ export async function handleStudioApi(
|
|
|
1440
1351
|
return problem("internalError", errorMessage(error));
|
|
1441
1352
|
}
|
|
1442
1353
|
// Immediately, and before the refactor pass: a room still keyed to the old path would be
|
|
1443
|
-
//
|
|
1354
|
+
// Written back onto disk by the next save from anyone in it, recreating the file this rename just moved.
|
|
1444
1355
|
opts.onFileMoved?.(absFrom);
|
|
1445
1356
|
// Refactor pass: rewrite path references project-wide and, for a component, auto-rename its tag.
|
|
1446
1357
|
// The move already succeeded, so a refactor failure is reported but never fails the rename.
|
|
@@ -1611,7 +1522,10 @@ export async function handleStudioApi(
|
|
|
1611
1522
|
const registry = await getFormatRegistry(projectRoot);
|
|
1612
1523
|
const entry = registry.byName(format);
|
|
1613
1524
|
if (!entry) {
|
|
1614
|
-
return problem(
|
|
1525
|
+
return problem(
|
|
1526
|
+
"notFound",
|
|
1527
|
+
`Format "${format}" is not a registered format class. Enable an extension providing it in project.json "extensions", e.g. "@jxsuite/parser".`,
|
|
1528
|
+
);
|
|
1615
1529
|
}
|
|
1616
1530
|
const result =
|
|
1617
1531
|
action === "parse"
|
|
@@ -1731,8 +1645,9 @@ export async function handleStudioApi(
|
|
|
1731
1645
|
try {
|
|
1732
1646
|
const content = readFileSync(moduleAbsPath, "utf8");
|
|
1733
1647
|
/* SAFETY: every ClassJsonDef member is optional and extractStudioSchema guards or relays
|
|
1734
|
-
each one it reads; a non-object file
|
|
1735
|
-
|
|
1648
|
+
each one it reads; a non-object file or one that fails the parse boundary throws into the
|
|
1649
|
+
catch below. */
|
|
1650
|
+
const classDef = parseClassDef(content, moduleAbsPath) as ClassJsonDef;
|
|
1736
1651
|
return Response.json({
|
|
1737
1652
|
schema: extractStudioSchema(classDef, moduleAbsPath),
|
|
1738
1653
|
});
|
|
@@ -1752,7 +1667,7 @@ export async function handleStudioApi(
|
|
|
1752
1667
|
const content = readFileSync(classJsonPath, "utf8");
|
|
1753
1668
|
/* SAFETY: every ClassJsonDef member is optional and extractStudioSchema guards or relays
|
|
1754
1669
|
each one it reads; a malformed file throws and falls through to the module import. */
|
|
1755
|
-
const classDef =
|
|
1670
|
+
const classDef = parseClassDef(content, classJsonPath) as ClassJsonDef;
|
|
1756
1671
|
return Response.json({
|
|
1757
1672
|
schema: extractStudioSchema(classDef, classJsonPath),
|
|
1758
1673
|
});
|
|
@@ -2153,147 +2068,3 @@ export async function handleStudioApi(
|
|
|
2153
2068
|
|
|
2154
2069
|
return null;
|
|
2155
2070
|
}
|
|
2156
|
-
|
|
2157
|
-
/** The flat form-rendering schema the studio's renderSchemaFields() consumes. */
|
|
2158
|
-
interface StudioSchema {
|
|
2159
|
-
description: string | undefined;
|
|
2160
|
-
properties: Record<string, JsonObject>;
|
|
2161
|
-
required: string[];
|
|
2162
|
-
returns?: JsonValue;
|
|
2163
|
-
format?: JsonValue;
|
|
2164
|
-
$studio?: JsonValue;
|
|
2165
|
-
capabilities?: Record<string, { identifier: string; timing: string[] }>;
|
|
2166
|
-
}
|
|
2167
|
-
|
|
2168
|
-
/**
|
|
2169
|
-
* Extract a studio-friendly schema from a .class.json definition. Transforms $defs.parameters and
|
|
2170
|
-
* $defs.fields into the flat { description, properties, required } shape that renderSchemaFields()
|
|
2171
|
-
* in the studio already consumes.
|
|
2172
|
-
*
|
|
2173
|
-
* @param {ClassJsonDef} classDef
|
|
2174
|
-
* @param {string} classJsonPath
|
|
2175
|
-
*/
|
|
2176
|
-
function extractStudioSchema(classDef: ClassJsonDef, classJsonPath: string): StudioSchema {
|
|
2177
|
-
// If extends.$ref points to a parent, recursively merge
|
|
2178
|
-
let parentSchema = null;
|
|
2179
|
-
// `?.$ref` is undefined for a non-object `extends`, so a primitive never reaches resolve().
|
|
2180
|
-
if (classDef.extends?.$ref) {
|
|
2181
|
-
try {
|
|
2182
|
-
const parentPath = resolve(dirname(classJsonPath), classDef.extends.$ref);
|
|
2183
|
-
const parentContent = readFileSync(parentPath, "utf8");
|
|
2184
|
-
/* SAFETY: every ClassJsonDef member is optional and each one read here is either guarded
|
|
2185
|
-
(isNonNullObject on `type`, truthiness elsewhere) or relayed verbatim; a non-object file
|
|
2186
|
-
throws into the catch below or yields no members. */
|
|
2187
|
-
const parentDef = JSON.parse(parentContent) as ClassJsonDef;
|
|
2188
|
-
parentSchema = extractStudioSchema(parentDef, parentPath);
|
|
2189
|
-
} catch {
|
|
2190
|
-
// Parent not found — proceed without inheritance
|
|
2191
|
-
}
|
|
2192
|
-
}
|
|
2193
|
-
|
|
2194
|
-
const params = classDef.$defs?.parameters ?? {};
|
|
2195
|
-
const fields = classDef.$defs?.fields ?? {};
|
|
2196
|
-
const properties: Record<string, JsonObject> = {};
|
|
2197
|
-
const required: string[] = [];
|
|
2198
|
-
|
|
2199
|
-
// Start with parent properties (child overrides)
|
|
2200
|
-
if (parentSchema?.properties) {
|
|
2201
|
-
Object.assign(properties, parentSchema.properties);
|
|
2202
|
-
}
|
|
2203
|
-
if (parentSchema?.required) {
|
|
2204
|
-
required.push(...parentSchema.required);
|
|
2205
|
-
}
|
|
2206
|
-
|
|
2207
|
-
// Build properties from parameters (constructor config surface)
|
|
2208
|
-
for (const [key, param] of Object.entries(params)) {
|
|
2209
|
-
const id = param.identifier ?? key;
|
|
2210
|
-
const prop: JsonObject = {};
|
|
2211
|
-
if (isNonNullObject(param.type)) {
|
|
2212
|
-
Object.assign(prop, param.type);
|
|
2213
|
-
}
|
|
2214
|
-
if (param.description) {
|
|
2215
|
-
prop.description = param.description;
|
|
2216
|
-
}
|
|
2217
|
-
if (param.examples) {
|
|
2218
|
-
prop.examples = param.examples;
|
|
2219
|
-
}
|
|
2220
|
-
if (param.format) {
|
|
2221
|
-
prop.format = param.format;
|
|
2222
|
-
}
|
|
2223
|
-
properties[id] = prop;
|
|
2224
|
-
}
|
|
2225
|
-
|
|
2226
|
-
// Build properties from fields (config-visible ones only)
|
|
2227
|
-
for (const [key, field] of Object.entries(fields)) {
|
|
2228
|
-
if (field.role !== "field") {
|
|
2229
|
-
continue;
|
|
2230
|
-
}
|
|
2231
|
-
if (field.access === "private") {
|
|
2232
|
-
continue;
|
|
2233
|
-
}
|
|
2234
|
-
const id = field.identifier ?? key;
|
|
2235
|
-
const prop: JsonObject = {};
|
|
2236
|
-
if (isNonNullObject(field.type)) {
|
|
2237
|
-
Object.assign(prop, field.type);
|
|
2238
|
-
}
|
|
2239
|
-
if (field.description) {
|
|
2240
|
-
prop.description = field.description;
|
|
2241
|
-
}
|
|
2242
|
-
if (field.default !== undefined) {
|
|
2243
|
-
prop.default = field.default;
|
|
2244
|
-
}
|
|
2245
|
-
if (field.initializer !== undefined && prop.default === undefined) {
|
|
2246
|
-
prop.default = field.initializer;
|
|
2247
|
-
}
|
|
2248
|
-
if (field.examples) {
|
|
2249
|
-
prop.examples = field.examples;
|
|
2250
|
-
}
|
|
2251
|
-
properties[id] = prop;
|
|
2252
|
-
}
|
|
2253
|
-
|
|
2254
|
-
// Determine required from constructor parameters that have no default
|
|
2255
|
-
const ctorParams = classDef.$defs?.constructor?.parameters ?? [];
|
|
2256
|
-
const requiredSet = new Set<string>(required);
|
|
2257
|
-
for (const p of ctorParams) {
|
|
2258
|
-
const name = p.$ref ? p.$ref.split("/").pop() : (p.identifier ?? p.name);
|
|
2259
|
-
if (name && properties[name] && properties[name].default === undefined) {
|
|
2260
|
-
requiredSet.add(name);
|
|
2261
|
-
}
|
|
2262
|
-
}
|
|
2263
|
-
|
|
2264
|
-
const resolveMethod = classDef.$defs?.methods?.resolve;
|
|
2265
|
-
|
|
2266
|
-
// Surface format-extension metadata: the format block, studio hints, and a
|
|
2267
|
-
// Capability summary ({ parse: { timing }, serialize: { timing }, ... }).
|
|
2268
|
-
const capabilityRoles = new Set(["parse", "serialize", "discover", "load"]);
|
|
2269
|
-
const capabilities: Record<string, { identifier: string; timing: string[] }> = {};
|
|
2270
|
-
const methods = classDef.$defs?.methods ?? {};
|
|
2271
|
-
for (const [key, method] of Object.entries(methods)) {
|
|
2272
|
-
if (method.role && capabilityRoles.has(method.role)) {
|
|
2273
|
-
capabilities[method.role] = {
|
|
2274
|
-
identifier: method.identifier ?? key,
|
|
2275
|
-
timing: method.timing ?? ["compiler", "server"],
|
|
2276
|
-
};
|
|
2277
|
-
}
|
|
2278
|
-
}
|
|
2279
|
-
|
|
2280
|
-
// Optional members are added in this order, which is the key order the studio receives.
|
|
2281
|
-
const schema: StudioSchema = {
|
|
2282
|
-
description: classDef.description ?? classDef.title,
|
|
2283
|
-
properties,
|
|
2284
|
-
required: [...requiredSet],
|
|
2285
|
-
};
|
|
2286
|
-
if (resolveMethod?.returns) {
|
|
2287
|
-
schema.returns = resolveMethod.returns;
|
|
2288
|
-
}
|
|
2289
|
-
if (classDef.format) {
|
|
2290
|
-
schema.format = classDef.format;
|
|
2291
|
-
}
|
|
2292
|
-
if (classDef.$studio) {
|
|
2293
|
-
schema.$studio = classDef.$studio;
|
|
2294
|
-
}
|
|
2295
|
-
if (Object.keys(capabilities).length > 0) {
|
|
2296
|
-
schema.capabilities = capabilities;
|
|
2297
|
-
}
|
|
2298
|
-
return schema;
|
|
2299
|
-
}
|
package/src/types.ts
CHANGED
|
@@ -34,7 +34,8 @@ export interface ClassJsonMethod {
|
|
|
34
34
|
getter?: { body: string };
|
|
35
35
|
setter?: { parameters?: ClassJsonParam[]; body: string };
|
|
36
36
|
parameters?: ClassJsonParam[];
|
|
37
|
-
returns
|
|
37
|
+
/** What the method returns, as a JSON Schema (spec.md §12.3); only `resolve()`'s is read. */
|
|
38
|
+
returnType?: JsonValue;
|
|
38
39
|
/** Where a format-extension capability method may run (`compiler`, `server`, `client`). */
|
|
39
40
|
timing?: string[];
|
|
40
41
|
}
|