@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/README.md
CHANGED
|
@@ -57,11 +57,12 @@ All file operations are constrained to the project root (path traversal is rejec
|
|
|
57
57
|
|
|
58
58
|
OXC-powered tools for the Studio function body editor:
|
|
59
59
|
|
|
60
|
-
| Endpoint | Tool
|
|
61
|
-
| ---------------------------- |
|
|
62
|
-
| `POST /__studio/code/format` | oxfmt
|
|
63
|
-
| `POST /__studio/code/
|
|
64
|
-
|
|
60
|
+
| Endpoint | Tool | Description |
|
|
61
|
+
| ---------------------------- | ------ | ------------------- |
|
|
62
|
+
| `POST /__studio/code/format` | oxfmt | Format a JS snippet |
|
|
63
|
+
| `POST /__studio/code/lint` | oxlint | Lint a JS snippet |
|
|
64
|
+
|
|
65
|
+
`@jxsuite/server/code-api` exports `runCodeService`, the same dispatch without HTTP, which the desktop backend answers its `codeService` request with.
|
|
65
66
|
|
|
66
67
|
## Build pipeline
|
|
67
68
|
|
|
@@ -69,9 +70,11 @@ OXC-powered tools for the Studio function body editor:
|
|
|
69
70
|
|
|
70
71
|
## Dependencies
|
|
71
72
|
|
|
72
|
-
| Package | Purpose
|
|
73
|
-
| ---------- |
|
|
74
|
-
| `chokidar` | File watching for live reload
|
|
73
|
+
| Package | Purpose |
|
|
74
|
+
| ---------- | ------------------------------------------------------------- |
|
|
75
|
+
| `chokidar` | File watching for live reload |
|
|
76
|
+
| `oxfmt` | Formats function bodies for the code services |
|
|
77
|
+
| `oxlint` | Lints function bodies, run as a CLI by the host's own runtime |
|
|
75
78
|
|
|
76
79
|
| `@npmcli/arborist` | Package operations (npm's installer) | | `npm-package-arg` | Validating a package spec before anything is installed | | `ws` | WebSockets for `serve()` on Node |
|
|
77
80
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jxsuite/server",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.4.0",
|
|
4
4
|
"description": "Jx development server with live reload, server-side proxy, and studio integration",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"./dev": "./src/dev.ts",
|
|
24
24
|
"./convert": "./src/convert.ts",
|
|
25
25
|
"./format": "./src/format.ts",
|
|
26
|
+
"./code-api": "./src/code-api.ts",
|
|
26
27
|
"./ai-api": "./src/ai-api.ts",
|
|
27
28
|
"./data": "./src/data-api.ts",
|
|
28
29
|
"./import-api": "./src/import-api.ts",
|
|
@@ -32,6 +33,7 @@
|
|
|
32
33
|
"./oauth-loopback": "./src/oauth-loopback.ts",
|
|
33
34
|
"./net-guard": "./src/net-guard.ts",
|
|
34
35
|
"./live-preview": "./src/live-preview.ts",
|
|
36
|
+
"./preview-bundles": "./src/preview-bundles.ts",
|
|
35
37
|
"./site-preview": "./src/site-preview.ts",
|
|
36
38
|
"./refactor": "./src/refactor/index.ts",
|
|
37
39
|
"./refactor/refs": "./src/refactor/refs.ts",
|
|
@@ -40,6 +42,7 @@
|
|
|
40
42
|
"./project-state/host": "./src/project-state/host.ts",
|
|
41
43
|
"./project-state/overlays": "./src/project-state/overlays.ts",
|
|
42
44
|
"./project-state/settle": "./src/project-state/settle.ts",
|
|
45
|
+
"./class-schema": "./src/class-schema.ts",
|
|
43
46
|
"./resolve": "./src/resolve.ts",
|
|
44
47
|
"./types": "./src/types.ts",
|
|
45
48
|
"./jx-mounts": "./src/jx-mounts.ts",
|
|
@@ -62,30 +65,31 @@
|
|
|
62
65
|
"upgrade": "bun update --latest && bun install"
|
|
63
66
|
},
|
|
64
67
|
"dependencies": {
|
|
65
|
-
"@jxsuite/ai": "^0.45.
|
|
66
|
-
"@jxsuite/catalog": "^0.1.
|
|
67
|
-
"@jxsuite/collab": "^0.
|
|
68
|
-
"@jxsuite/compiler": "^11.
|
|
69
|
-
"@jxsuite/create": "^1.8.
|
|
70
|
-
"@jxsuite/erase": "^0.3.
|
|
71
|
-
"@jxsuite/import": "^0.44.
|
|
72
|
-
"@jxsuite/protocol": "^
|
|
73
|
-
"@jxsuite/runtime": "^9.0
|
|
74
|
-
"@jxsuite/schema": "^8.
|
|
75
|
-
"@jxsuite/site": "^4.
|
|
76
|
-
"@jxsuite/starters": "^5.
|
|
68
|
+
"@jxsuite/ai": "^0.45.4",
|
|
69
|
+
"@jxsuite/catalog": "^0.1.21",
|
|
70
|
+
"@jxsuite/collab": "^0.14.0",
|
|
71
|
+
"@jxsuite/compiler": "^11.2.0",
|
|
72
|
+
"@jxsuite/create": "^1.8.9",
|
|
73
|
+
"@jxsuite/erase": "^0.3.8",
|
|
74
|
+
"@jxsuite/import": "^0.44.11",
|
|
75
|
+
"@jxsuite/protocol": "^7.1.0",
|
|
76
|
+
"@jxsuite/runtime": "^9.1.0",
|
|
77
|
+
"@jxsuite/schema": "^8.3.0",
|
|
78
|
+
"@jxsuite/site": "^4.2.0",
|
|
79
|
+
"@jxsuite/starters": "^5.3.0",
|
|
77
80
|
"@npmcli/arborist": "^10.0.5",
|
|
78
81
|
"chokidar": "^6.0.0",
|
|
79
82
|
"kysely": "^0.29.9",
|
|
80
83
|
"npm-package-arg": "^14.0.0",
|
|
81
|
-
"oxfmt": "^0.
|
|
82
|
-
"
|
|
84
|
+
"oxfmt": "^0.80.0",
|
|
85
|
+
"oxlint": "^1.95.0",
|
|
86
|
+
"ws": "^8.23.1",
|
|
83
87
|
"zod": "^4.6.7"
|
|
84
88
|
},
|
|
85
89
|
"devDependencies": {
|
|
86
|
-
"@jxsuite/auth": "^0.
|
|
87
|
-
"@jxsuite/connector": "^0.
|
|
88
|
-
"@jxsuite/parser": "^5.
|
|
90
|
+
"@jxsuite/auth": "^0.9.0",
|
|
91
|
+
"@jxsuite/connector": "^0.9.0",
|
|
92
|
+
"@jxsuite/parser": "^5.3.0",
|
|
89
93
|
"@jxsuite/test-support": "^0.0.0",
|
|
90
94
|
"@types/npm-package-arg": "^6.1.4",
|
|
91
95
|
"@types/npmcli__arborist": "^6.3.3",
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class-schema.ts — the studio-facing schema of a `.class.json`, shared by the dev server and the
|
|
3
|
+
* desktop app so both answer a plugin-schema request with the same shape.
|
|
4
|
+
*
|
|
5
|
+
* @docs extending/extensions/classes
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { dirname, resolve } from "node:path";
|
|
9
|
+
import { readFileSync } from "node:fs";
|
|
10
|
+
import { readPath } from "@jxsuite/runtime/pointer";
|
|
11
|
+
import { isJsonObject, isNonNullObject, isString } from "@jxsuite/schema/guards";
|
|
12
|
+
import { parseClassDef } from "@jxsuite/schema/parse";
|
|
13
|
+
import type { JsonObject, JsonValue } from "@jxsuite/schema/types";
|
|
14
|
+
import type { ClassJsonDef } from "./types.ts";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The flat form-rendering schema the studio's renderSchemaFields() consumes. A JSON object first,
|
|
18
|
+
* because it crosses the RPC bridge as one, and then the members {@link extractStudioSchema} writes.
|
|
19
|
+
*/
|
|
20
|
+
export type StudioSchema = JsonObject & {
|
|
21
|
+
type?: string;
|
|
22
|
+
description?: string;
|
|
23
|
+
properties: Record<string, JsonObject>;
|
|
24
|
+
required: string[];
|
|
25
|
+
/**
|
|
26
|
+
* What `resolve()` returns, as its class declares it (spec.md §12.3), with a local `$ref`
|
|
27
|
+
* followed.
|
|
28
|
+
*/
|
|
29
|
+
returnType?: JsonObject;
|
|
30
|
+
format?: JsonValue;
|
|
31
|
+
$studio?: JsonValue;
|
|
32
|
+
capabilities?: Record<string, { identifier: string; timing: string[] }>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The `returnType` a class declares on one of its methods, with a top-level local `$ref` followed.
|
|
37
|
+
*
|
|
38
|
+
* Only a pointer into the same class document (`#/...`) is followed, and a reference already seen
|
|
39
|
+
* ends the walk. A non-local reference is returned as declared; a local one that leads nowhere
|
|
40
|
+
* yields nothing. Nested references stay as written.
|
|
41
|
+
*
|
|
42
|
+
* @param {ClassJsonDef} classDef
|
|
43
|
+
* @param {string} [method] - The method name, `resolve` by default
|
|
44
|
+
* @returns {JsonObject | undefined}
|
|
45
|
+
*/
|
|
46
|
+
export function resolveReturnType(
|
|
47
|
+
classDef: ClassJsonDef,
|
|
48
|
+
method = "resolve",
|
|
49
|
+
): JsonObject | undefined {
|
|
50
|
+
let current: unknown = classDef.$defs?.methods?.[method]?.returnType;
|
|
51
|
+
const seen = new Set<string>();
|
|
52
|
+
while (isJsonObject(current) && isString(current.$ref) && current.$ref.startsWith("#/")) {
|
|
53
|
+
if (seen.has(current.$ref)) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
seen.add(current.$ref);
|
|
57
|
+
/* SAFETY: a class document is parsed JSON, so every member readPath walks is a JSON value;
|
|
58
|
+
ClassJsonDef only types the members the readers name. */
|
|
59
|
+
current = readPath(classDef as JsonObject, current.$ref.slice(2));
|
|
60
|
+
}
|
|
61
|
+
return isJsonObject(current) ? current : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Extract a studio-friendly schema from a .class.json definition. Transforms $defs.parameters and
|
|
66
|
+
* $defs.fields into the flat { description, properties, required } shape that renderSchemaFields()
|
|
67
|
+
* in the studio already consumes.
|
|
68
|
+
*
|
|
69
|
+
* @param {ClassJsonDef} classDef
|
|
70
|
+
* @param {string} classJsonPath
|
|
71
|
+
* @returns {StudioSchema}
|
|
72
|
+
*/
|
|
73
|
+
export function extractStudioSchema(classDef: ClassJsonDef, classJsonPath: string): StudioSchema {
|
|
74
|
+
// If extends.$ref points to a parent, recursively merge
|
|
75
|
+
let parentSchema = null;
|
|
76
|
+
// `?.$ref` is undefined for a non-object `extends`, so a primitive never reaches resolve().
|
|
77
|
+
if (classDef.extends?.$ref) {
|
|
78
|
+
try {
|
|
79
|
+
const parentPath = resolve(dirname(classJsonPath), classDef.extends.$ref);
|
|
80
|
+
const parentContent = readFileSync(parentPath, "utf8");
|
|
81
|
+
/* SAFETY: every ClassJsonDef member is optional and each one read here is either guarded
|
|
82
|
+
(isNonNullObject on `type`, truthiness elsewhere) or relayed verbatim; a file the parse
|
|
83
|
+
boundary refuses, or a non-object one, throws into the catch below or yields no members. */
|
|
84
|
+
const parentDef = parseClassDef(parentContent, parentPath) as ClassJsonDef;
|
|
85
|
+
parentSchema = extractStudioSchema(parentDef, parentPath);
|
|
86
|
+
} catch {
|
|
87
|
+
// Parent not found — proceed without inheritance
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const params = classDef.$defs?.parameters ?? {};
|
|
92
|
+
const fields = classDef.$defs?.fields ?? {};
|
|
93
|
+
const properties: Record<string, JsonObject> = {};
|
|
94
|
+
const required: string[] = [];
|
|
95
|
+
|
|
96
|
+
// Start with parent properties (child overrides)
|
|
97
|
+
if (parentSchema?.properties) {
|
|
98
|
+
Object.assign(properties, parentSchema.properties);
|
|
99
|
+
}
|
|
100
|
+
if (parentSchema?.required) {
|
|
101
|
+
required.push(...parentSchema.required);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Build properties from parameters (constructor config surface)
|
|
105
|
+
for (const [key, param] of Object.entries(params)) {
|
|
106
|
+
const id = param.identifier ?? key;
|
|
107
|
+
const prop: JsonObject = {};
|
|
108
|
+
if (isNonNullObject(param.type)) {
|
|
109
|
+
Object.assign(prop, param.type);
|
|
110
|
+
}
|
|
111
|
+
if (param.description) {
|
|
112
|
+
prop.description = param.description;
|
|
113
|
+
}
|
|
114
|
+
if (param.examples) {
|
|
115
|
+
prop.examples = param.examples;
|
|
116
|
+
}
|
|
117
|
+
if (param.format) {
|
|
118
|
+
prop.format = param.format;
|
|
119
|
+
}
|
|
120
|
+
properties[id] = prop;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Build properties from fields (config-visible ones only)
|
|
124
|
+
for (const [key, field] of Object.entries(fields)) {
|
|
125
|
+
if (field.role !== "field") {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (field.access === "private") {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const id = field.identifier ?? key;
|
|
132
|
+
const prop: JsonObject = {};
|
|
133
|
+
if (isNonNullObject(field.type)) {
|
|
134
|
+
Object.assign(prop, field.type);
|
|
135
|
+
}
|
|
136
|
+
if (field.description) {
|
|
137
|
+
prop.description = field.description;
|
|
138
|
+
}
|
|
139
|
+
if (field.default !== undefined) {
|
|
140
|
+
prop.default = field.default;
|
|
141
|
+
}
|
|
142
|
+
if (field.initializer !== undefined && prop.default === undefined) {
|
|
143
|
+
prop.default = field.initializer;
|
|
144
|
+
}
|
|
145
|
+
if (field.examples) {
|
|
146
|
+
prop.examples = field.examples;
|
|
147
|
+
}
|
|
148
|
+
properties[id] = prop;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Determine required from constructor parameters that have no default
|
|
152
|
+
const ctorParams = classDef.$defs?.constructor?.parameters ?? [];
|
|
153
|
+
const requiredSet = new Set<string>(required);
|
|
154
|
+
for (const p of ctorParams) {
|
|
155
|
+
const name = p.$ref ? p.$ref.split("/").pop() : (p.identifier ?? p.name);
|
|
156
|
+
if (name && properties[name] && properties[name].default === undefined) {
|
|
157
|
+
requiredSet.add(name);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Surface format-extension metadata: the format block, studio hints, and a
|
|
162
|
+
// Capability summary ({ parse: { timing }, serialize: { timing }, ... }).
|
|
163
|
+
const capabilityRoles = new Set(["parse", "serialize", "discover", "load"]);
|
|
164
|
+
const capabilities: Record<string, { identifier: string; timing: string[] }> = {};
|
|
165
|
+
const methods = classDef.$defs?.methods ?? {};
|
|
166
|
+
for (const [key, method] of Object.entries(methods)) {
|
|
167
|
+
if (method.role && capabilityRoles.has(method.role)) {
|
|
168
|
+
capabilities[method.role] = {
|
|
169
|
+
identifier: method.identifier ?? key,
|
|
170
|
+
timing: method.timing ?? ["compiler", "server"],
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Optional members are added in this order, which is the key order the studio receives.
|
|
176
|
+
const schema: StudioSchema = {
|
|
177
|
+
properties,
|
|
178
|
+
required: [...requiredSet],
|
|
179
|
+
};
|
|
180
|
+
const description = classDef.description ?? classDef.title;
|
|
181
|
+
if (description !== undefined) {
|
|
182
|
+
schema.description = description;
|
|
183
|
+
}
|
|
184
|
+
// A class with no `resolve()` of its own inherits the method, so it inherits the type.
|
|
185
|
+
const returnType = classDef.$defs?.methods?.resolve
|
|
186
|
+
? resolveReturnType(classDef)
|
|
187
|
+
: parentSchema?.returnType;
|
|
188
|
+
if (returnType) {
|
|
189
|
+
schema.returnType = returnType;
|
|
190
|
+
}
|
|
191
|
+
if (classDef.format) {
|
|
192
|
+
schema.format = classDef.format;
|
|
193
|
+
}
|
|
194
|
+
if (classDef.$studio) {
|
|
195
|
+
schema.$studio = classDef.$studio;
|
|
196
|
+
}
|
|
197
|
+
if (Object.keys(capabilities).length > 0) {
|
|
198
|
+
schema.capabilities = capabilities;
|
|
199
|
+
}
|
|
200
|
+
return schema;
|
|
201
|
+
}
|
package/src/code-api.ts
CHANGED
|
@@ -5,12 +5,16 @@
|
|
|
5
5
|
* CLI) a function body. A body may be TypeScript (`spec.md` §23.1), so both read it as `.ts`.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
import { existsSync } from "node:fs";
|
|
10
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
8
11
|
import { tmpdir } from "node:os";
|
|
12
|
+
import { delimiter, dirname, join } from "node:path";
|
|
13
|
+
import { format } from "oxfmt";
|
|
14
|
+
import { isJsonObject } from "@jxsuite/schema/guards";
|
|
9
15
|
import { errorMessage } from "@jxsuite/schema/parse";
|
|
10
16
|
import type { JsonValue } from "@jxsuite/schema/types";
|
|
11
|
-
import {
|
|
12
|
-
import { existsSync } from "node:fs";
|
|
13
|
-
import { unlink } from "node:fs/promises";
|
|
17
|
+
import type { CodeServiceResult } from "@jxsuite/protocol";
|
|
14
18
|
import { formatBody } from "./code-format.ts";
|
|
15
19
|
import { problem } from "./problem.ts";
|
|
16
20
|
|
|
@@ -32,38 +36,84 @@ interface OxcDiagnostic {
|
|
|
32
36
|
[key: string]: JsonValue | undefined;
|
|
33
37
|
}
|
|
34
38
|
|
|
39
|
+
/** The actions the code services answer, in the order the editor asks for them. */
|
|
40
|
+
export const CODE_SERVICE_ACTIONS = ["format", "lint"] as const;
|
|
41
|
+
|
|
42
|
+
/** What a request body carries, once narrowed: anything else is dropped. */
|
|
35
43
|
interface CodeApiBody {
|
|
36
44
|
code?: string;
|
|
37
45
|
args?: string[];
|
|
38
46
|
}
|
|
39
47
|
|
|
40
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Run oxlint's CLI the way the host runs JavaScript: a script is started as `[process.execPath,
|
|
50
|
+
* script]`, which is Bun on the dev server and Electron's own Node in the packaged application, and
|
|
51
|
+
* `ELECTRON_RUN_AS_NODE` makes the second behave as plain Node. No host needs a `node` on its
|
|
52
|
+
* PATH.
|
|
53
|
+
*/
|
|
54
|
+
const RUN_AS_NODE = { ELECTRON_RUN_AS_NODE: "1" };
|
|
55
|
+
|
|
56
|
+
/** The first file named `name` on `PATH`, or null. */
|
|
57
|
+
function onPath(name: string): string | null {
|
|
58
|
+
const names = process.platform === "win32" ? [`${name}.exe`, `${name}.cmd`] : [name];
|
|
59
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter)) {
|
|
60
|
+
for (const candidate of names) {
|
|
61
|
+
const path = join(dir, candidate);
|
|
62
|
+
if (dir && existsSync(path)) {
|
|
63
|
+
return path;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
41
69
|
|
|
42
70
|
/**
|
|
43
|
-
* Locate the oxlint CLI: explicit JX_OXLINT_BIN override (empty
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
71
|
+
* Locate the oxlint CLI as a command to spawn. In order: an explicit JX_OXLINT_BIN override (empty
|
|
72
|
+
* means lint is disabled outright); the copy a packaged application stages beside its bundled
|
|
73
|
+
* module (`oxlint/bin/oxlint`); a walk up from this module for `node_modules/oxlint/bin/oxlint`,
|
|
74
|
+
* which is the monorepo's and the hoisted install's; and `oxlint` on PATH. Null when none exists,
|
|
75
|
+
* and lint then answers no diagnostics.
|
|
76
|
+
*
|
|
77
|
+
* @param startDir Where the search starts, the directory of this module unless a test says so.
|
|
47
78
|
*/
|
|
48
|
-
export function
|
|
79
|
+
export function resolveOxlintCommand(startDir: string = import.meta.dirname): string[] | null {
|
|
49
80
|
const override = process.env.JX_OXLINT_BIN;
|
|
50
81
|
if (override !== undefined) {
|
|
51
|
-
return override
|
|
82
|
+
return override ? [override] : null;
|
|
83
|
+
}
|
|
84
|
+
const staged = join(startDir, "oxlint", "bin", "oxlint");
|
|
85
|
+
if (existsSync(staged)) {
|
|
86
|
+
return [process.execPath, staged];
|
|
52
87
|
}
|
|
53
88
|
let dir = startDir;
|
|
54
89
|
for (;;) {
|
|
55
|
-
const candidate = join(dir, "node_modules", "
|
|
90
|
+
const candidate = join(dir, "node_modules", "oxlint", "bin", "oxlint");
|
|
56
91
|
if (existsSync(candidate)) {
|
|
57
|
-
return candidate;
|
|
92
|
+
return [process.execPath, candidate];
|
|
58
93
|
}
|
|
59
94
|
const parent = dirname(dir);
|
|
60
95
|
if (parent === dir) {
|
|
61
|
-
|
|
96
|
+
const found = onPath("oxlint");
|
|
97
|
+
return found === null ? null : [found];
|
|
62
98
|
}
|
|
63
99
|
dir = parent;
|
|
64
100
|
}
|
|
65
101
|
}
|
|
66
102
|
|
|
103
|
+
let formatterProbe: Promise<boolean> | undefined;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Whether the formatter's native binding loads on this host. Settled once: the editor asks to
|
|
107
|
+
* format and to lint together when it opens, and a binding that is missing is missing every time.
|
|
108
|
+
*/
|
|
109
|
+
export function formatterLoads(): Promise<boolean> {
|
|
110
|
+
formatterProbe ??= format("probe.js", "0", {}).then(
|
|
111
|
+
() => true,
|
|
112
|
+
() => false,
|
|
113
|
+
);
|
|
114
|
+
return formatterProbe;
|
|
115
|
+
}
|
|
116
|
+
|
|
67
117
|
// ─── Wrapper utilities ───────────────────────────────────────────────────────
|
|
68
118
|
|
|
69
119
|
/**
|
|
@@ -99,100 +149,153 @@ function adjustDiagnostics(diagnostics: OxcDiagnostic[], headerLen: number) {
|
|
|
99
149
|
});
|
|
100
150
|
}
|
|
101
151
|
|
|
102
|
-
// ───
|
|
152
|
+
// ─── Services ────────────────────────────────────────────────────────────────
|
|
103
153
|
|
|
104
|
-
/**
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
*/
|
|
108
|
-
export async function handleCodeApi(req: Request, url: URL) {
|
|
109
|
-
const path = url.pathname;
|
|
110
|
-
if (!path.startsWith("/__studio/code/") || req.method !== "POST") {
|
|
154
|
+
/** Format a body; null when the formatter cannot load. A body it cannot parse comes back as sent. */
|
|
155
|
+
async function formatCode({ code }: CodeApiBody): Promise<CodeServiceResult | null> {
|
|
156
|
+
if (!(await formatterLoads())) {
|
|
111
157
|
return null;
|
|
112
158
|
}
|
|
113
|
-
|
|
114
|
-
|
|
159
|
+
if (!code?.trim()) {
|
|
160
|
+
return { code: "", errors: [] };
|
|
161
|
+
}
|
|
115
162
|
try {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return problem("invalidRequest", "Invalid JSON");
|
|
163
|
+
const result = await formatBody(code);
|
|
164
|
+
return { code: result.code, errors: result.errors.map((message) => ({ message })) };
|
|
165
|
+
} catch (error) {
|
|
166
|
+
return { code, errors: [{ message: errorMessage(error) }] };
|
|
121
167
|
}
|
|
168
|
+
}
|
|
122
169
|
|
|
123
|
-
|
|
170
|
+
/** Run a command to its end and read what it printed. */
|
|
171
|
+
function output(command: string[], cwd: string): Promise<string> {
|
|
172
|
+
return new Promise((resolve, reject) => {
|
|
173
|
+
const [file = "", ...args] = command;
|
|
174
|
+
const child = spawn(file, args, {
|
|
175
|
+
cwd,
|
|
176
|
+
env: { ...process.env, ...RUN_AS_NODE },
|
|
177
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
178
|
+
});
|
|
179
|
+
let text = "";
|
|
180
|
+
child.stdout.setEncoding("utf8");
|
|
181
|
+
child.stdout.on("data", (chunk: string) => {
|
|
182
|
+
text += chunk;
|
|
183
|
+
});
|
|
184
|
+
child.on("error", reject);
|
|
185
|
+
child.on("close", () => resolve(text));
|
|
186
|
+
});
|
|
187
|
+
}
|
|
124
188
|
|
|
125
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Lint a body with oxlint's default rules. The body is written into a private directory beside an
|
|
191
|
+
* empty configuration given with `-c`, which is also the working directory: oxlint otherwise uses
|
|
192
|
+
* the nearest `.oxlintrc.json` above where it runs, and a world-writable temp directory is a place
|
|
193
|
+
* another user could plant one.
|
|
194
|
+
*/
|
|
195
|
+
async function lintCode({ code, args }: CodeApiBody): Promise<CodeServiceResult> {
|
|
196
|
+
if (!code?.trim()) {
|
|
197
|
+
return { diagnostics: [] };
|
|
198
|
+
}
|
|
199
|
+
const command = resolveOxlintCommand();
|
|
200
|
+
if (!command) {
|
|
201
|
+
// No oxlint anywhere: lint quietly disables.
|
|
202
|
+
return { diagnostics: [] };
|
|
203
|
+
}
|
|
204
|
+
const wrapped = wrapBody(code, args);
|
|
205
|
+
const headerLen = wrapped.indexOf("\n") + 1;
|
|
206
|
+
const dir = await mkdtemp(join(tmpdir(), "jx-lint-"));
|
|
207
|
+
try {
|
|
208
|
+
const config = join(dir, ".oxlintrc.json");
|
|
209
|
+
const file = join(dir, "fn.ts");
|
|
210
|
+
await writeFile(config, "{}");
|
|
211
|
+
await writeFile(file, wrapped);
|
|
212
|
+
const text = await output(
|
|
213
|
+
[...command, "-c", config, "--format=json", "-A", "no-unused-vars", file],
|
|
214
|
+
dir,
|
|
215
|
+
);
|
|
216
|
+
/* SAFETY: `oxlint --format=json` prints `{ diagnostics: [...] }` whose labels each carry a
|
|
217
|
+
numeric `span.line`/`span.offset`; output that is not JSON throws into the catch below. */
|
|
218
|
+
const parsed = JSON.parse(text) as { diagnostics?: OxcDiagnostic[] };
|
|
219
|
+
const adjusted = adjustDiagnostics(parsed.diagnostics ?? [], headerLen);
|
|
220
|
+
/* SAFETY: every member was just parsed from JSON, so each diagnostic is a JSON value; the
|
|
221
|
+
interface leaves its optional `labels` open to `undefined`, which a parse never produces. */
|
|
222
|
+
return { diagnostics: adjusted as JsonValue[] };
|
|
223
|
+
} catch (error) {
|
|
224
|
+
return { diagnostics: [], error: errorMessage(error) };
|
|
225
|
+
} finally {
|
|
226
|
+
await rm(dir, { force: true, recursive: true });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
126
229
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
if (!code?.trim()) {
|
|
130
|
-
return Response.json({ code: "", errors: [] });
|
|
131
|
-
}
|
|
230
|
+
/* oxlint-disable deslop/no-unknown-parameters, deslop/no-runtime-typeof -- A code-service payload is
|
|
231
|
+
the editor's JSON body at the host boundary, and narrowing it member by member is the parse. */
|
|
132
232
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
});
|
|
139
|
-
} catch (error) {
|
|
140
|
-
return Response.json({
|
|
141
|
-
code,
|
|
142
|
-
errors: [{ message: errorMessage(error) }],
|
|
143
|
-
});
|
|
144
|
-
}
|
|
233
|
+
/** Keep `code` when it is a string and `args` when it is a list of them; drop everything else. */
|
|
234
|
+
function narrow(payload: unknown): CodeApiBody {
|
|
235
|
+
const body: CodeApiBody = {};
|
|
236
|
+
if (!isJsonObject(payload)) {
|
|
237
|
+
return body;
|
|
145
238
|
}
|
|
239
|
+
const { code, args } = payload;
|
|
240
|
+
if (typeof code === "string") {
|
|
241
|
+
body.code = code;
|
|
242
|
+
}
|
|
243
|
+
if (Array.isArray(args) && args.every((arg) => typeof arg === "string")) {
|
|
244
|
+
body.args = args;
|
|
245
|
+
}
|
|
246
|
+
return body;
|
|
247
|
+
}
|
|
146
248
|
|
|
147
|
-
|
|
148
|
-
|
|
249
|
+
/**
|
|
250
|
+
* Answer one code-service request, with no HTTP in between: the dev server's route and the desktop
|
|
251
|
+
* backend's `codeService` request both call it.
|
|
252
|
+
*
|
|
253
|
+
* @param action One of {@link CODE_SERVICE_ACTIONS}.
|
|
254
|
+
* @param payload The request body, `{ code, args? }`; anything else is read as an empty body.
|
|
255
|
+
* @returns The result, or null for an unknown action and for `format` where the formatter cannot
|
|
256
|
+
* load.
|
|
257
|
+
*/
|
|
258
|
+
export async function runCodeService(
|
|
259
|
+
action: string,
|
|
260
|
+
payload: unknown,
|
|
261
|
+
): Promise<CodeServiceResult | null> {
|
|
262
|
+
if (action === "format") {
|
|
263
|
+
return formatCode(narrow(payload));
|
|
264
|
+
}
|
|
149
265
|
if (action === "lint") {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
266
|
+
return lintCode(narrow(payload));
|
|
267
|
+
}
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
/* oxlint-enable deslop/no-unknown-parameters, deslop/no-runtime-typeof */
|
|
154
271
|
|
|
155
|
-
|
|
156
|
-
if (!oxlintBin) {
|
|
157
|
-
// No oxlint anywhere (packaged desktop app) — lint quietly disables.
|
|
158
|
-
return Response.json({ diagnostics: [] });
|
|
159
|
-
}
|
|
272
|
+
// ─── Handler ─────────────────────────────────────────────────────────────────
|
|
160
273
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
274
|
+
/**
|
|
275
|
+
* The HTTP adapter of {@link runCodeService}.
|
|
276
|
+
*
|
|
277
|
+
* @param {Request} req
|
|
278
|
+
* @param {URL} url
|
|
279
|
+
*/
|
|
280
|
+
export async function handleCodeApi(req: Request, url: URL) {
|
|
281
|
+
const path = url.pathname;
|
|
282
|
+
if (!path.startsWith("/__studio/code/") || req.method !== "POST") {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
const action = path.slice("/__studio/code/".length);
|
|
286
|
+
if (!CODE_SERVICE_ACTIONS.some((known) => known === action)) {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
167
289
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
cwd: tmpdir(),
|
|
174
|
-
stderr: "pipe",
|
|
175
|
-
stdout: "pipe",
|
|
176
|
-
});
|
|
177
|
-
const output = await new Response(proc.stdout).text();
|
|
178
|
-
await proc.exited;
|
|
179
|
-
|
|
180
|
-
/* SAFETY: `oxlint --format=json` prints `{ diagnostics: [...] }` whose labels each carry a
|
|
181
|
-
numeric `span.line`/`span.offset`; output that is not JSON throws into the catch below. */
|
|
182
|
-
const parsed = JSON.parse(output) as { diagnostics?: OxcDiagnostic[] };
|
|
183
|
-
const adjusted = adjustDiagnostics(parsed.diagnostics ?? [], headerLen);
|
|
184
|
-
return Response.json({ diagnostics: adjusted });
|
|
185
|
-
} catch (error) {
|
|
186
|
-
return Response.json({
|
|
187
|
-
diagnostics: [],
|
|
188
|
-
error: errorMessage(error),
|
|
189
|
-
});
|
|
190
|
-
} finally {
|
|
191
|
-
try {
|
|
192
|
-
await unlink(tmpFile);
|
|
193
|
-
} catch {}
|
|
194
|
-
}
|
|
290
|
+
let body: unknown;
|
|
291
|
+
try {
|
|
292
|
+
body = await req.json();
|
|
293
|
+
} catch {
|
|
294
|
+
return problem("invalidRequest", "Invalid JSON");
|
|
195
295
|
}
|
|
196
296
|
|
|
197
|
-
|
|
297
|
+
const result = await runCodeService(action, body);
|
|
298
|
+
return result === null
|
|
299
|
+
? problem("capabilityUnavailable", "The formatter did not load, so format is unavailable")
|
|
300
|
+
: Response.json(result);
|
|
198
301
|
}
|