@huaqiu/hardware-artifacts 0.7.12 → 0.7.14
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/index.d.mts +53 -10
- package/lib/index.mjs +80 -8
- package/lib/routes.d.mts +14 -3
- package/lib/routes.mjs +350 -25
- package/lib/service-CtVn7z8b.d.mts +568 -0
- package/lib/service-Yrc2C_eo.mjs +2005 -0
- package/lib/service.d.mts +2 -99
- package/lib/service.mjs +2 -377
- package/package.json +3 -2
- package/src/index.ts +118 -9
- package/src/order.ts +778 -0
- package/src/routes.ts +323 -13
- package/src/service.ts +1290 -62
package/src/routes.ts
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
/** Read-only HTTP surface for the Hardware Engineering client. */
|
|
2
2
|
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
3
|
-
import
|
|
3
|
+
import { readFileSync } from 'node:fs'
|
|
4
|
+
import type { ArtifactKind, HardwareArtifactService, NativeApplication } from './service.js'
|
|
4
5
|
|
|
5
6
|
export const HARDWARE_ROUTE_PREFIX = '/api/v1/hardware'
|
|
6
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Resolve the workspace root of one DSH session, host-side.
|
|
10
|
+
*
|
|
11
|
+
* Supplied by the plugin from the `sessions` service, so the base for an
|
|
12
|
+
* authorization check comes from the session header rather than from a value
|
|
13
|
+
* the browser sent — and never from whichever session discovered last.
|
|
14
|
+
*/
|
|
15
|
+
export type WorkspaceResolver = (sessionId: string) => Promise<string | undefined> | string | undefined
|
|
16
|
+
|
|
17
|
+
export interface HardwareHandlerOptions {
|
|
18
|
+
readonly resolveWorkspace?: WorkspaceResolver
|
|
19
|
+
}
|
|
20
|
+
|
|
7
21
|
function sendJson(res: ServerResponse, status: number, body: unknown): void {
|
|
8
22
|
const text = JSON.stringify(body)
|
|
9
23
|
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'content-length': Buffer.byteLength(text) })
|
|
@@ -33,8 +47,67 @@ function asString(value: unknown): string | undefined {
|
|
|
33
47
|
return typeof value === 'string' && value.length > 0 ? value : undefined
|
|
34
48
|
}
|
|
35
49
|
|
|
36
|
-
|
|
37
|
-
|
|
50
|
+
function failure(error: unknown): { status: number; body: unknown } {
|
|
51
|
+
const code = typeof (error as { code?: unknown }).code === 'string' ? String((error as { code: string }).code) : 'internal'
|
|
52
|
+
const status = typeof (error as { status?: unknown }).status === 'number' ? Number((error as { status: number }).status) : 500
|
|
53
|
+
return { status, body: { ok: false, error: { code, message: error instanceof Error ? error.message : String(error) } } }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Roots a request may read: the caller's workspace, then the host's own. */
|
|
57
|
+
function rootsOf(body: unknown): string[] {
|
|
58
|
+
if (body === null || typeof body !== 'object') return []
|
|
59
|
+
const cwd = asString((body as Record<string, unknown>).cwd)
|
|
60
|
+
const workspace = asString((body as Record<string, unknown>).workspace)
|
|
61
|
+
return [cwd, workspace].filter((value): value is string => value !== undefined)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The DSH session a request belongs to: a query field for reads, a body field for writes. */
|
|
65
|
+
function sessionOf(url: URL, body?: unknown): string | undefined {
|
|
66
|
+
const fromBody = body === null || typeof body !== 'object'
|
|
67
|
+
? undefined
|
|
68
|
+
: asString((body as Record<string, unknown>).session)
|
|
69
|
+
return fromBody ?? asString(url.searchParams.get('session') ?? undefined)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Everything one request may read: the roots it named plus the workspace of the
|
|
74
|
+
* session it belongs to.
|
|
75
|
+
*
|
|
76
|
+
* The session's workspace is resolved here, not taken from `cwd`: the browser
|
|
77
|
+
* sends whichever cwd its session hook held at render time, and a viewer that
|
|
78
|
+
* stayed mounted across a workspace switch would otherwise authorize against a
|
|
79
|
+
* workspace it no longer shows.
|
|
80
|
+
*/
|
|
81
|
+
async function authorizeRequest(
|
|
82
|
+
options: HardwareHandlerOptions,
|
|
83
|
+
url: URL,
|
|
84
|
+
body?: unknown,
|
|
85
|
+
): Promise<{ roots: string[]; sessionId?: string }> {
|
|
86
|
+
const sessionId = sessionOf(url, body)
|
|
87
|
+
const roots = rootsOf(body)
|
|
88
|
+
// Reads carry theirs in the query string; writes in the body. Either way they
|
|
89
|
+
// only add to the session's workspace, never replace it.
|
|
90
|
+
for (const key of ['cwd', 'workspace']) {
|
|
91
|
+
const value = asString(url.searchParams.get(key) ?? undefined)
|
|
92
|
+
if (value !== undefined && !roots.includes(value)) roots.push(value)
|
|
93
|
+
}
|
|
94
|
+
if (sessionId === undefined || options.resolveWorkspace === undefined) {
|
|
95
|
+
return { roots }
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
const workspace = await options.resolveWorkspace(sessionId)
|
|
99
|
+
if (workspace !== undefined && workspace !== '') roots.push(workspace)
|
|
100
|
+
} catch {
|
|
101
|
+
// An unresolvable session is not a reason to fail a local read: the caller's
|
|
102
|
+
// own roots still apply, and they can only come from this machine.
|
|
103
|
+
}
|
|
104
|
+
return { roots, sessionId }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function createHardwareHandler(
|
|
108
|
+
service: HardwareArtifactService,
|
|
109
|
+
options: HardwareHandlerOptions = {},
|
|
110
|
+
): (req: IncomingMessage, res: ServerResponse) => void {
|
|
38
111
|
return (req, res) => {
|
|
39
112
|
if (!isTrustedRequest(req)) {
|
|
40
113
|
sendJson(res, 403, { ok: false, error: { code: 'forbidden', message: 'forbidden' } })
|
|
@@ -45,8 +118,22 @@ export function createHardwareHandler(service: HardwareArtifactService): (req: I
|
|
|
45
118
|
const url = new URL(req.url ?? '/', 'http://dsh.internal')
|
|
46
119
|
const p = url.pathname
|
|
47
120
|
if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/projects`) {
|
|
48
|
-
const
|
|
49
|
-
|
|
121
|
+
const sessionId = asString(url.searchParams.get('session') ?? undefined)
|
|
122
|
+
let workspace = asString(url.searchParams.get('cwd') ?? undefined)
|
|
123
|
+
if (workspace === undefined && sessionId !== undefined && options.resolveWorkspace !== undefined) {
|
|
124
|
+
try {
|
|
125
|
+
workspace = await options.resolveWorkspace(sessionId)
|
|
126
|
+
} catch {
|
|
127
|
+
workspace = undefined
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// The workspace the session declares is the default; `process.cwd()`
|
|
131
|
+
// is only a last resort for an embed with no session at all.
|
|
132
|
+
const result = service.discover(
|
|
133
|
+
workspace ?? process.cwd(),
|
|
134
|
+
asString(url.searchParams.get('project') ?? undefined),
|
|
135
|
+
sessionId,
|
|
136
|
+
)
|
|
50
137
|
sendJson(res, 200, { ok: true, ...result })
|
|
51
138
|
return
|
|
52
139
|
}
|
|
@@ -55,7 +142,231 @@ export function createHardwareHandler(service: HardwareArtifactService): (req: I
|
|
|
55
142
|
return
|
|
56
143
|
}
|
|
57
144
|
if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/environment`) {
|
|
58
|
-
|
|
145
|
+
const sessionId = asString(url.searchParams.get('session') ?? undefined)
|
|
146
|
+
sendJson(res, 200, {
|
|
147
|
+
ok: true,
|
|
148
|
+
text: service.environmentText(service.discovery(sessionId)),
|
|
149
|
+
childEnv: service.childEnvironment(),
|
|
150
|
+
})
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/source`) {
|
|
154
|
+
const artifactPath = asString(url.searchParams.get('path'))
|
|
155
|
+
if (artifactPath === undefined) {
|
|
156
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path is required' } })
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
const revision = asString(url.searchParams.get('revision') ?? undefined)
|
|
160
|
+
const { roots, sessionId } = await authorizeRequest(options, url)
|
|
161
|
+
try {
|
|
162
|
+
const source = await service.readSource({
|
|
163
|
+
artifactPath,
|
|
164
|
+
...(revision === undefined ? {} : { expectedRevision: revision }),
|
|
165
|
+
roots,
|
|
166
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
167
|
+
})
|
|
168
|
+
sendJson(res, 200, { ok: true, ...source })
|
|
169
|
+
} catch (error) {
|
|
170
|
+
const { status, body } = failure(error)
|
|
171
|
+
sendJson(res, status, body)
|
|
172
|
+
}
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/order`) {
|
|
176
|
+
sendJson(res, 200, { ok: true, ...await service.orderConfig() })
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/fabrication`) {
|
|
180
|
+
const body = await readJson(req) as Record<string, unknown>
|
|
181
|
+
const artifactPath = asString(body.path)
|
|
182
|
+
if (artifactPath === undefined) {
|
|
183
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path is required' } })
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
const schematic = asString(body.schematic)
|
|
187
|
+
const { roots, sessionId } = await authorizeRequest(options, url, body)
|
|
188
|
+
try {
|
|
189
|
+
const prepared = await service.fabricate({
|
|
190
|
+
boardPath: artifactPath,
|
|
191
|
+
...(schematic === undefined ? {} : { schematicPath: schematic }),
|
|
192
|
+
roots,
|
|
193
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
194
|
+
})
|
|
195
|
+
sendJson(res, 200, {
|
|
196
|
+
ok: true,
|
|
197
|
+
boardPath: prepared.boardPath,
|
|
198
|
+
revision: prepared.revision,
|
|
199
|
+
generatedAt: prepared.generatedAt,
|
|
200
|
+
facts: prepared.facts,
|
|
201
|
+
archive: prepared.archive,
|
|
202
|
+
...(prepared.assembly === undefined ? {} : { assembly: prepared.assembly }),
|
|
203
|
+
})
|
|
204
|
+
} catch (error) {
|
|
205
|
+
const { status, body: failureBody } = failure(error)
|
|
206
|
+
sendJson(res, status, failureBody)
|
|
207
|
+
}
|
|
208
|
+
return
|
|
209
|
+
}
|
|
210
|
+
if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/order/pcb`) {
|
|
211
|
+
const body = await readJson(req) as Record<string, unknown>
|
|
212
|
+
if (body.confirm !== true) {
|
|
213
|
+
sendJson(res, 400, {
|
|
214
|
+
ok: false,
|
|
215
|
+
error: {
|
|
216
|
+
code: 'confirmation-required',
|
|
217
|
+
message: 'placing an order uploads the board files and requires explicit confirmation',
|
|
218
|
+
},
|
|
219
|
+
})
|
|
220
|
+
return
|
|
221
|
+
}
|
|
222
|
+
const artifactPath = asString(body.path)
|
|
223
|
+
if (artifactPath === undefined) {
|
|
224
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path is required' } })
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
const schematic = asString(body.schematic)
|
|
228
|
+
const { roots, sessionId } = await authorizeRequest(options, url, body)
|
|
229
|
+
try {
|
|
230
|
+
const placed = await service.placeOrder({
|
|
231
|
+
kind: 'pcb',
|
|
232
|
+
boardPath: artifactPath,
|
|
233
|
+
...(schematic === undefined ? {} : { schematicPath: schematic }),
|
|
234
|
+
roots,
|
|
235
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
236
|
+
...(typeof body.quantity === 'number' ? { quantity: body.quantity } : {}),
|
|
237
|
+
...(body.launch === false ? { launch: false } : {}),
|
|
238
|
+
})
|
|
239
|
+
sendJson(res, 200, { ok: true, ...placed })
|
|
240
|
+
} catch (error) {
|
|
241
|
+
const { status, body: failureBody } = failure(error)
|
|
242
|
+
sendJson(res, status, failureBody)
|
|
243
|
+
}
|
|
244
|
+
return
|
|
245
|
+
}
|
|
246
|
+
if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/order/smt`) {
|
|
247
|
+
const body = await readJson(req) as Record<string, unknown>
|
|
248
|
+
if (body.confirm !== true) {
|
|
249
|
+
sendJson(res, 400, {
|
|
250
|
+
ok: false,
|
|
251
|
+
error: {
|
|
252
|
+
code: 'confirmation-required',
|
|
253
|
+
message: 'placing an assembly order uploads the board, BOM and positions and requires explicit confirmation',
|
|
254
|
+
},
|
|
255
|
+
})
|
|
256
|
+
return
|
|
257
|
+
}
|
|
258
|
+
const artifactPath = asString(body.path)
|
|
259
|
+
if (artifactPath === undefined) {
|
|
260
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path is required' } })
|
|
261
|
+
return
|
|
262
|
+
}
|
|
263
|
+
const schematic = asString(body.schematic)
|
|
264
|
+
const { roots, sessionId } = await authorizeRequest(options, url, body)
|
|
265
|
+
try {
|
|
266
|
+
const placed = await service.placeOrder({
|
|
267
|
+
kind: 'smt',
|
|
268
|
+
boardPath: artifactPath,
|
|
269
|
+
...(schematic === undefined ? {} : { schematicPath: schematic }),
|
|
270
|
+
roots,
|
|
271
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
272
|
+
...(typeof body.quantity === 'number' ? { quantity: body.quantity } : {}),
|
|
273
|
+
...(body.launch === false ? { launch: false } : {}),
|
|
274
|
+
})
|
|
275
|
+
sendJson(res, 200, { ok: true, ...placed })
|
|
276
|
+
} catch (error) {
|
|
277
|
+
const { status, body: failureBody } = failure(error)
|
|
278
|
+
sendJson(res, status, failureBody)
|
|
279
|
+
}
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/order/open`) {
|
|
283
|
+
const body = await readJson(req) as Record<string, unknown>
|
|
284
|
+
const orderUrl = asString(body.url)
|
|
285
|
+
if (orderUrl === undefined) {
|
|
286
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'url is required' } })
|
|
287
|
+
return
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
const opened = service.openOrderUrl(orderUrl)
|
|
291
|
+
sendJson(res, 200, { ok: true, ...opened })
|
|
292
|
+
} catch (error) {
|
|
293
|
+
const { status, body: failureBody } = failure(error)
|
|
294
|
+
sendJson(res, status, failureBody)
|
|
295
|
+
}
|
|
296
|
+
return
|
|
297
|
+
}
|
|
298
|
+
if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/bom`) {
|
|
299
|
+
const body = await readJson(req) as Record<string, unknown>
|
|
300
|
+
const artifactPath = asString(body.path)
|
|
301
|
+
if (artifactPath === undefined) {
|
|
302
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path is required' } })
|
|
303
|
+
return
|
|
304
|
+
}
|
|
305
|
+
const { roots, sessionId } = await authorizeRequest(options, url, body)
|
|
306
|
+
try {
|
|
307
|
+
const bom = await service.exportBom({
|
|
308
|
+
artifactPath,
|
|
309
|
+
roots,
|
|
310
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
311
|
+
})
|
|
312
|
+
sendJson(res, 200, { ok: true, ...bom })
|
|
313
|
+
} catch (error) {
|
|
314
|
+
const { status, body } = failure(error)
|
|
315
|
+
sendJson(res, status, body)
|
|
316
|
+
}
|
|
317
|
+
return
|
|
318
|
+
}
|
|
319
|
+
if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/glb`) {
|
|
320
|
+
const body = await readJson(req) as Record<string, unknown>
|
|
321
|
+
const artifactPath = asString(body.path)
|
|
322
|
+
if (artifactPath === undefined) {
|
|
323
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path is required' } })
|
|
324
|
+
return
|
|
325
|
+
}
|
|
326
|
+
const { roots, sessionId } = await authorizeRequest(options, url, body)
|
|
327
|
+
try {
|
|
328
|
+
const glb = await service.exportGlb({
|
|
329
|
+
artifactPath,
|
|
330
|
+
roots,
|
|
331
|
+
...(sessionId === undefined ? {} : { sessionId }),
|
|
332
|
+
})
|
|
333
|
+
const query = new URLSearchParams({ path: glb.path, revision: glb.revision })
|
|
334
|
+
sendJson(res, 200, {
|
|
335
|
+
ok: true,
|
|
336
|
+
path: glb.path,
|
|
337
|
+
revision: glb.revision,
|
|
338
|
+
bytes: glb.bytes,
|
|
339
|
+
generatedAt: glb.generatedAt,
|
|
340
|
+
cached: glb.cached,
|
|
341
|
+
url: `/api/v1/hardware/glb-file?${query.toString()}`,
|
|
342
|
+
})
|
|
343
|
+
} catch (error) {
|
|
344
|
+
const { status, body } = failure(error)
|
|
345
|
+
sendJson(res, status, body)
|
|
346
|
+
}
|
|
347
|
+
return
|
|
348
|
+
}
|
|
349
|
+
if (req.method === 'GET' && p === `${HARDWARE_ROUTE_PREFIX}/glb-file`) {
|
|
350
|
+
const artifactPath = asString(url.searchParams.get('path'))
|
|
351
|
+
const revision = asString(url.searchParams.get('revision'))
|
|
352
|
+
if (artifactPath === undefined || revision === undefined) {
|
|
353
|
+
sendJson(res, 400, { ok: false, error: { code: 'invalid-request', message: 'path and revision are required' } })
|
|
354
|
+
return
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
const file = service.glbFile(artifactPath, revision)
|
|
358
|
+
const bytes = readFileSync(file)
|
|
359
|
+
res.writeHead(200, {
|
|
360
|
+
'content-type': 'model/gltf-binary',
|
|
361
|
+
'content-length': bytes.byteLength,
|
|
362
|
+
'cache-control': 'no-store',
|
|
363
|
+
'x-huaqiu-source-revision': revision,
|
|
364
|
+
})
|
|
365
|
+
res.end(bytes)
|
|
366
|
+
} catch (error) {
|
|
367
|
+
const { status, body } = failure(error)
|
|
368
|
+
sendJson(res, status, body)
|
|
369
|
+
}
|
|
59
370
|
return
|
|
60
371
|
}
|
|
61
372
|
if (req.method === 'POST' && p === `${HARDWARE_ROUTE_PREFIX}/open`) {
|
|
@@ -63,8 +374,8 @@ export function createHardwareHandler(service: HardwareArtifactService): (req: I
|
|
|
63
374
|
const application = asString(body.application)
|
|
64
375
|
const artifactPath = asString(body.artifactPath)
|
|
65
376
|
const kind = asString(body.kind)
|
|
66
|
-
if (application !== 'kicad'
|
|
67
|
-
sendJson(res, 400, {
|
|
377
|
+
if (application !== 'kicad') {
|
|
378
|
+
sendJson(res, 400, { error: { code: 'invalid-application', message: 'application must be kicad' }, ok: false })
|
|
68
379
|
return
|
|
69
380
|
}
|
|
70
381
|
if (artifactPath === undefined || kind === undefined) {
|
|
@@ -80,16 +391,15 @@ export function createHardwareHandler(service: HardwareArtifactService): (req: I
|
|
|
80
391
|
})
|
|
81
392
|
sendJson(res, 200, { ok: true, ...opened })
|
|
82
393
|
} catch (error) {
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
sendJson(res, status, { ok: false, error: { code, message: error instanceof Error ? error.message : String(error) } })
|
|
394
|
+
const { status, body } = failure(error)
|
|
395
|
+
sendJson(res, status, body)
|
|
86
396
|
}
|
|
87
397
|
return
|
|
88
398
|
}
|
|
89
399
|
sendJson(res, 404, { ok: false, error: { code: 'not-found', message: 'unknown hardware route' } })
|
|
90
400
|
} catch (error) {
|
|
91
|
-
const
|
|
92
|
-
sendJson(res, status,
|
|
401
|
+
const { status, body } = failure(error)
|
|
402
|
+
sendJson(res, status, body)
|
|
93
403
|
}
|
|
94
404
|
})()
|
|
95
405
|
}
|