@huaqiu/hardware-artifacts 0.7.12 → 0.7.13
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/lib/routes.mjs +150 -20
- package/lib/service.d.mts +129 -2
- package/lib/service.mjs +538 -77
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/routes.ts +108 -9
- package/src/service.ts +639 -54
package/lib/routes.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
1
2
|
//#region src/routes.ts
|
|
2
3
|
const HARDWARE_ROUTE_PREFIX = "/api/v1/hardware";
|
|
3
4
|
function sendJson(res, status, body) {
|
|
@@ -28,6 +29,24 @@ async function readJson(req, maxBytes = 65536) {
|
|
|
28
29
|
function asString(value) {
|
|
29
30
|
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
30
31
|
}
|
|
32
|
+
function failure(error) {
|
|
33
|
+
const code = typeof error.code === "string" ? String(error.code) : "internal";
|
|
34
|
+
return {
|
|
35
|
+
status: typeof error.status === "number" ? Number(error.status) : 500,
|
|
36
|
+
body: {
|
|
37
|
+
ok: false,
|
|
38
|
+
error: {
|
|
39
|
+
code,
|
|
40
|
+
message: error instanceof Error ? error.message : String(error)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** Roots a request may read: the caller's workspace, then the host's own. */
|
|
46
|
+
function rootsOf(body) {
|
|
47
|
+
if (body === null || typeof body !== "object") return [];
|
|
48
|
+
return [asString(body.cwd), asString(body.workspace)].filter((value) => value !== void 0);
|
|
49
|
+
}
|
|
31
50
|
function createHardwareHandler(service) {
|
|
32
51
|
let lastDiscovery;
|
|
33
52
|
return (req, res) => {
|
|
@@ -64,23 +83,145 @@ function createHardwareHandler(service) {
|
|
|
64
83
|
if (req.method === "GET" && p === `/api/v1/hardware/environment`) {
|
|
65
84
|
sendJson(res, 200, {
|
|
66
85
|
ok: true,
|
|
67
|
-
text: service.environmentText(lastDiscovery),
|
|
86
|
+
text: service.environmentText(lastDiscovery ?? service.discovery()),
|
|
68
87
|
childEnv: service.childEnvironment()
|
|
69
88
|
});
|
|
70
89
|
return;
|
|
71
90
|
}
|
|
91
|
+
if (req.method === "GET" && p === `/api/v1/hardware/source`) {
|
|
92
|
+
const artifactPath = asString(url.searchParams.get("path"));
|
|
93
|
+
if (artifactPath === void 0) {
|
|
94
|
+
sendJson(res, 400, {
|
|
95
|
+
ok: false,
|
|
96
|
+
error: {
|
|
97
|
+
code: "invalid-request",
|
|
98
|
+
message: "path is required"
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const cwd = asString(url.searchParams.get("cwd") ?? void 0);
|
|
104
|
+
const revision = asString(url.searchParams.get("revision") ?? void 0);
|
|
105
|
+
try {
|
|
106
|
+
sendJson(res, 200, {
|
|
107
|
+
ok: true,
|
|
108
|
+
...await service.readSource({
|
|
109
|
+
artifactPath,
|
|
110
|
+
...revision === void 0 ? {} : { expectedRevision: revision },
|
|
111
|
+
...cwd === void 0 ? {} : { roots: [cwd] }
|
|
112
|
+
})
|
|
113
|
+
});
|
|
114
|
+
} catch (error) {
|
|
115
|
+
const { status, body } = failure(error);
|
|
116
|
+
sendJson(res, status, body);
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (req.method === "POST" && p === `/api/v1/hardware/bom`) {
|
|
121
|
+
const body = await readJson(req);
|
|
122
|
+
const artifactPath = asString(body.path);
|
|
123
|
+
if (artifactPath === void 0) {
|
|
124
|
+
sendJson(res, 400, {
|
|
125
|
+
ok: false,
|
|
126
|
+
error: {
|
|
127
|
+
code: "invalid-request",
|
|
128
|
+
message: "path is required"
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
sendJson(res, 200, {
|
|
135
|
+
ok: true,
|
|
136
|
+
...await service.exportBom({
|
|
137
|
+
artifactPath,
|
|
138
|
+
roots: rootsOf(body)
|
|
139
|
+
})
|
|
140
|
+
});
|
|
141
|
+
} catch (error) {
|
|
142
|
+
const { status, body } = failure(error);
|
|
143
|
+
sendJson(res, status, body);
|
|
144
|
+
}
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (req.method === "POST" && p === `/api/v1/hardware/glb`) {
|
|
148
|
+
const body = await readJson(req);
|
|
149
|
+
const artifactPath = asString(body.path);
|
|
150
|
+
if (artifactPath === void 0) {
|
|
151
|
+
sendJson(res, 400, {
|
|
152
|
+
ok: false,
|
|
153
|
+
error: {
|
|
154
|
+
code: "invalid-request",
|
|
155
|
+
message: "path is required"
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
const glb = await service.exportGlb({
|
|
162
|
+
artifactPath,
|
|
163
|
+
roots: rootsOf(body)
|
|
164
|
+
});
|
|
165
|
+
const query = new URLSearchParams({
|
|
166
|
+
path: glb.path,
|
|
167
|
+
revision: glb.revision
|
|
168
|
+
});
|
|
169
|
+
sendJson(res, 200, {
|
|
170
|
+
ok: true,
|
|
171
|
+
path: glb.path,
|
|
172
|
+
revision: glb.revision,
|
|
173
|
+
bytes: glb.bytes,
|
|
174
|
+
generatedAt: glb.generatedAt,
|
|
175
|
+
cached: glb.cached,
|
|
176
|
+
url: `/api/v1/hardware/glb-file?${query.toString()}`
|
|
177
|
+
});
|
|
178
|
+
} catch (error) {
|
|
179
|
+
const { status, body } = failure(error);
|
|
180
|
+
sendJson(res, status, body);
|
|
181
|
+
}
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (req.method === "GET" && p === `/api/v1/hardware/glb-file`) {
|
|
185
|
+
const artifactPath = asString(url.searchParams.get("path"));
|
|
186
|
+
const revision = asString(url.searchParams.get("revision"));
|
|
187
|
+
if (artifactPath === void 0 || revision === void 0) {
|
|
188
|
+
sendJson(res, 400, {
|
|
189
|
+
ok: false,
|
|
190
|
+
error: {
|
|
191
|
+
code: "invalid-request",
|
|
192
|
+
message: "path and revision are required"
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
const file = service.glbFile(artifactPath, revision);
|
|
199
|
+
const bytes = readFileSync(file);
|
|
200
|
+
res.writeHead(200, {
|
|
201
|
+
"content-type": "model/gltf-binary",
|
|
202
|
+
"content-length": bytes.byteLength,
|
|
203
|
+
"cache-control": "no-store",
|
|
204
|
+
"x-huaqiu-source-revision": revision
|
|
205
|
+
});
|
|
206
|
+
res.end(bytes);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
const { status, body } = failure(error);
|
|
209
|
+
sendJson(res, status, body);
|
|
210
|
+
}
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
72
213
|
if (req.method === "POST" && p === `/api/v1/hardware/open`) {
|
|
73
214
|
const body = await readJson(req);
|
|
74
215
|
const application = asString(body.application);
|
|
75
216
|
const artifactPath = asString(body.artifactPath);
|
|
76
217
|
const kind = asString(body.kind);
|
|
77
|
-
if (application !== "kicad"
|
|
218
|
+
if (application !== "kicad") {
|
|
78
219
|
sendJson(res, 400, {
|
|
79
|
-
ok: false,
|
|
80
220
|
error: {
|
|
81
221
|
code: "invalid-application",
|
|
82
|
-
message: "application must be kicad
|
|
83
|
-
}
|
|
222
|
+
message: "application must be kicad"
|
|
223
|
+
},
|
|
224
|
+
ok: false
|
|
84
225
|
});
|
|
85
226
|
return;
|
|
86
227
|
}
|
|
@@ -105,14 +246,8 @@ function createHardwareHandler(service) {
|
|
|
105
246
|
})
|
|
106
247
|
});
|
|
107
248
|
} catch (error) {
|
|
108
|
-
const
|
|
109
|
-
sendJson(res,
|
|
110
|
-
ok: false,
|
|
111
|
-
error: {
|
|
112
|
-
code,
|
|
113
|
-
message: error instanceof Error ? error.message : String(error)
|
|
114
|
-
}
|
|
115
|
-
});
|
|
249
|
+
const { status, body } = failure(error);
|
|
250
|
+
sendJson(res, status, body);
|
|
116
251
|
}
|
|
117
252
|
return;
|
|
118
253
|
}
|
|
@@ -124,13 +259,8 @@ function createHardwareHandler(service) {
|
|
|
124
259
|
}
|
|
125
260
|
});
|
|
126
261
|
} catch (error) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
error: {
|
|
130
|
-
code: "internal",
|
|
131
|
-
message: error instanceof Error ? error.message : String(error)
|
|
132
|
-
}
|
|
133
|
-
});
|
|
262
|
+
const { status, body } = failure(error);
|
|
263
|
+
sendJson(res, status, body);
|
|
134
264
|
}
|
|
135
265
|
})();
|
|
136
266
|
};
|
package/lib/service.d.mts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ChildProcess } from "node:child_process";
|
|
2
2
|
//#region src/service.d.ts
|
|
3
3
|
type ArtifactKind = 'schematic' | 'pcb' | 'pcb-3d' | 'bom' | 'mechanical-3d';
|
|
4
|
-
|
|
4
|
+
/** Native handoff is KiCad-only in this POC; SolidCraft stays agent context. */
|
|
5
|
+
type NativeApplication = 'kicad';
|
|
5
6
|
interface HardwareArtifact {
|
|
6
7
|
readonly id: string;
|
|
7
8
|
readonly kind: ArtifactKind;
|
|
@@ -11,15 +12,21 @@ interface HardwareArtifact {
|
|
|
11
12
|
readonly sourcePath?: string;
|
|
12
13
|
readonly title: string;
|
|
13
14
|
readonly format: string;
|
|
15
|
+
/** Content hash of the saved source, filled when the source is read. */
|
|
16
|
+
readonly revision?: string;
|
|
14
17
|
}
|
|
15
18
|
interface HardwareProject {
|
|
16
19
|
readonly path: string;
|
|
17
20
|
readonly name: string;
|
|
21
|
+
/** Root schematic: the authoritative project entry point. */
|
|
22
|
+
readonly rootSheet?: HardwareArtifact;
|
|
18
23
|
readonly schematic?: HardwareArtifact;
|
|
19
24
|
/** Semantic views; both keep the identity of their source artifact. */
|
|
20
25
|
readonly bom?: HardwareArtifact;
|
|
21
26
|
readonly pcb3d?: HardwareArtifact;
|
|
22
27
|
readonly sheets: readonly HardwareArtifact[];
|
|
28
|
+
/** Deterministic default board, only when ownership is unambiguous. */
|
|
29
|
+
readonly defaultBoard?: string;
|
|
23
30
|
readonly boards: readonly HardwareArtifact[];
|
|
24
31
|
readonly mechanical: readonly HardwareArtifact[];
|
|
25
32
|
readonly selectionRequired: boolean;
|
|
@@ -62,6 +69,50 @@ interface OpenArtifactResult {
|
|
|
62
69
|
readonly cwd: string;
|
|
63
70
|
readonly pid?: number;
|
|
64
71
|
}
|
|
72
|
+
interface SourceRequest {
|
|
73
|
+
readonly artifactPath: string;
|
|
74
|
+
readonly expectedRevision?: string;
|
|
75
|
+
/** Workspace roots the caller is currently authorized to read. */
|
|
76
|
+
readonly roots?: readonly string[];
|
|
77
|
+
}
|
|
78
|
+
interface SourceResult {
|
|
79
|
+
readonly path: string;
|
|
80
|
+
readonly kind: 'schematic' | 'pcb';
|
|
81
|
+
readonly revision: string;
|
|
82
|
+
readonly bytes: number;
|
|
83
|
+
readonly mtimeMs: number;
|
|
84
|
+
readonly text: string;
|
|
85
|
+
}
|
|
86
|
+
interface GlbRequest {
|
|
87
|
+
readonly artifactPath: string;
|
|
88
|
+
readonly roots?: readonly string[];
|
|
89
|
+
readonly signal?: AbortSignal;
|
|
90
|
+
}
|
|
91
|
+
interface GlbResult {
|
|
92
|
+
readonly path: string;
|
|
93
|
+
readonly revision: string;
|
|
94
|
+
readonly bytes: number;
|
|
95
|
+
readonly generatedAt: string;
|
|
96
|
+
readonly cached: boolean;
|
|
97
|
+
readonly file: string;
|
|
98
|
+
}
|
|
99
|
+
/** One grouped BOM line, as KiCad's own BOM export groups it. */
|
|
100
|
+
interface BomRow {
|
|
101
|
+
readonly references: readonly string[];
|
|
102
|
+
readonly quantity: number;
|
|
103
|
+
readonly value: string;
|
|
104
|
+
readonly footprint: string;
|
|
105
|
+
readonly dnp: boolean;
|
|
106
|
+
readonly fields: Readonly<Record<string, string>>;
|
|
107
|
+
}
|
|
108
|
+
interface BomResult {
|
|
109
|
+
readonly path: string;
|
|
110
|
+
readonly revision: string;
|
|
111
|
+
readonly generatedAt: string;
|
|
112
|
+
readonly cached: boolean;
|
|
113
|
+
readonly rows: readonly BomRow[];
|
|
114
|
+
readonly source: 'kicad-cli-bom';
|
|
115
|
+
}
|
|
65
116
|
interface HardwareArtifactsOptions {
|
|
66
117
|
readonly kicadRoot?: string;
|
|
67
118
|
readonly solidcraftRoot?: string;
|
|
@@ -73,11 +124,37 @@ interface HardwareArtifactsOptions {
|
|
|
73
124
|
windowsHide: boolean;
|
|
74
125
|
shell: false;
|
|
75
126
|
}) => Pick<ChildProcess, 'pid'>;
|
|
127
|
+
/** Runs one short-lived CLI command and returns its exit facts. */
|
|
128
|
+
readonly runCommand?: (command: string, args: readonly string[], options: {
|
|
129
|
+
cwd: string;
|
|
130
|
+
timeoutMs: number;
|
|
131
|
+
signal?: AbortSignal;
|
|
132
|
+
}) => Promise<{
|
|
133
|
+
exitCode: number | null;
|
|
134
|
+
stdout: string;
|
|
135
|
+
stderr: string;
|
|
136
|
+
timedOut: boolean;
|
|
137
|
+
}>;
|
|
138
|
+
/** Cache directory for derived outputs; defaults to a temp subdirectory. */
|
|
139
|
+
readonly cacheRoot?: string;
|
|
140
|
+
readonly glbTimeoutMs?: number;
|
|
141
|
+
/** Source text limit; protects the host from pathological files. */
|
|
142
|
+
readonly maxSourceBytes?: number;
|
|
76
143
|
}
|
|
77
144
|
/** Small, immutable host capability implementation. */
|
|
78
145
|
declare class HardwareArtifactService {
|
|
79
146
|
private readonly config;
|
|
80
147
|
private readonly options;
|
|
148
|
+
private readonly cacheRoot;
|
|
149
|
+
private readonly glbTimeoutMs;
|
|
150
|
+
private readonly maxSourceBytes;
|
|
151
|
+
private lastDiscovery?;
|
|
152
|
+
/** Completed GLB exports keyed by `sourcePath:revision`. */
|
|
153
|
+
private readonly glbCache;
|
|
154
|
+
/** In-flight GLB exports keyed by the same key, so concurrent callers share one run. */
|
|
155
|
+
private readonly glbPending;
|
|
156
|
+
/** BOM projections keyed by `sourcePath:revision`. */
|
|
157
|
+
private readonly bomCache;
|
|
81
158
|
constructor(config?: HardwareArtifactsOptions);
|
|
82
159
|
/** Resolve configured installations from validated absolute paths only. */
|
|
83
160
|
installations(): Installations;
|
|
@@ -91,9 +168,59 @@ declare class HardwareArtifactService {
|
|
|
91
168
|
* returned as a selection requirement, never resolved by taking the first.
|
|
92
169
|
*/
|
|
93
170
|
discover(cwdInput: string, explicitProject?: string): DiscoveryResult;
|
|
171
|
+
/** The most recent discovery, used to authorize source reads. */
|
|
172
|
+
discovery(): DiscoveryResult | undefined;
|
|
94
173
|
private projectFrom;
|
|
95
174
|
/** Validate and launch a native application without a shell. */
|
|
96
175
|
open(request: OpenArtifactRequest): OpenArtifactResult;
|
|
176
|
+
/** Workspace roots a read may target: explicit request, last discovery, config. */
|
|
177
|
+
private authorizedRoots;
|
|
178
|
+
/**
|
|
179
|
+
* Authorize one artifact path: canonical, realpath-checked, inside a
|
|
180
|
+
* workspace root, and of an expected type. A browser request cannot turn
|
|
181
|
+
* this into an arbitrary filesystem read, even from loopback.
|
|
182
|
+
*/
|
|
183
|
+
private authorize;
|
|
184
|
+
/** Saved source text plus a content-addressed revision. */
|
|
185
|
+
readSource(request: SourceRequest): Promise<SourceResult>;
|
|
186
|
+
private sourceRevision;
|
|
187
|
+
/** Cache directory for one board's derived outputs. */
|
|
188
|
+
private glbDirectory;
|
|
189
|
+
/**
|
|
190
|
+
* Generate (or reuse) the GLB for one board.
|
|
191
|
+
*
|
|
192
|
+
* - cache key includes the source content hash, so same-size/same-mtime edits invalidate
|
|
193
|
+
* - one in-flight export is shared by concurrent callers for the same revision
|
|
194
|
+
* - output is written to a temp file and renamed only after it validates
|
|
195
|
+
* - a timeout or abort leaves no partial file behind
|
|
196
|
+
*/
|
|
197
|
+
exportGlb(request: GlbRequest): Promise<GlbResult>;
|
|
198
|
+
/**
|
|
199
|
+
* Grouped BOM for one saved root schematic.
|
|
200
|
+
*
|
|
201
|
+
* Reuses KiCad's own BOM extraction (`kicad-cli sch export bom`), the same
|
|
202
|
+
* authoritative source `analyze_kicad_module` consumes, rather than building a
|
|
203
|
+
* second component analyser. Grouping key is every non-reference column, so
|
|
204
|
+
* different MPNs never merge and DNP stays explicit.
|
|
205
|
+
*/
|
|
206
|
+
exportBom(request: {
|
|
207
|
+
readonly artifactPath: string;
|
|
208
|
+
readonly roots?: readonly string[];
|
|
209
|
+
readonly signal?: AbortSignal;
|
|
210
|
+
}): Promise<BomResult>;
|
|
211
|
+
/** Serve a generated GLB file, re-validating the cache entry first. */ glbFile(boardPath: string, revision: string): string;
|
|
212
|
+
/** Drop cached derived outputs for one board (or all of them). */
|
|
213
|
+
clearCache(boardPath?: string): void;
|
|
97
214
|
}
|
|
215
|
+
/** RFC4180-style CSV reader: quoted fields, doubled quotes, CRLF rows. */
|
|
216
|
+
declare function parseCsv(text: string): string[][];
|
|
217
|
+
/**
|
|
218
|
+
* Group KiCad's per-reference BOM rows.
|
|
219
|
+
*
|
|
220
|
+
* Identity is every column except the reference and quantity columns, so a
|
|
221
|
+
* different MPN or a DNP flag always produces a separate line; ordering follows
|
|
222
|
+
* the first reference seen, which keeps the table stable between reads.
|
|
223
|
+
*/
|
|
224
|
+
declare function groupBomRows(table: readonly string[][]): BomRow[];
|
|
98
225
|
//#endregion
|
|
99
|
-
export { ArtifactKind, DiscoveryResult, HardwareArtifact, HardwareArtifactService, HardwareArtifactsOptions, HardwareProject, InstallationInfo, Installations, NativeApplication, OpenArtifactRequest, OpenArtifactResult };
|
|
226
|
+
export { ArtifactKind, BomResult, BomRow, DiscoveryResult, GlbRequest, GlbResult, HardwareArtifact, HardwareArtifactService, HardwareArtifactsOptions, HardwareProject, InstallationInfo, Installations, NativeApplication, OpenArtifactRequest, OpenArtifactResult, SourceRequest, SourceResult, groupBomRows, parseCsv };
|