@huaqiu/dsh-hardware-ui 0.7.17 → 0.7.19

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/src/index.ts CHANGED
@@ -1,28 +1,144 @@
1
1
  /**
2
2
  * Node half of `@huaqiu/dsh-hardware-ui`.
3
3
  *
4
- * All product UI is browser-side. This entry exists so the package is a normal
5
- * installable plugin in the DSH bundle; it intentionally provides no service
6
- * and injects nothing. Host operations live in `@huaqiu/hardware-artifacts`.
4
+ * It does exactly one thing: serve the packaged STEP reader's WebAssembly
5
+ * module to the browser half, from this plugin's own files, over the Host's
6
+ * existing HTTP server. Everything else this product does is browser-side.
7
+ *
8
+ * Why this route exists at all. The client bundle is a single classic script
9
+ * loaded by DSH's module loader; it has no `import.meta.url` and no asset route
10
+ * of its own, so a 7.6 MB `.wasm` beside it has no URL the renderer can fetch.
11
+ * The alternatives were worse: base64-inlining the module into `client.js` would
12
+ * make every DSH start parse 10 MB of text whether or not a STEP file is ever
13
+ * opened, and fetching from a CDN is forbidden outright — the preview must work
14
+ * with no network (`docs/tasks/…full-impl-code-spec.md` §7.4, §12.3).
15
+ *
16
+ * The served bytes are a build artefact of a declared dependency, not workspace
17
+ * content: the request carries no path parameter, the file is resolved once at
18
+ * load through Node's own module resolution, and the response is immutable. No
19
+ * user-controlled string ever reaches a filesystem call here, which is why this
20
+ * is not the "second filesystem server" the spec rules out.
21
+ *
22
+ * When the Host has no HTTP server (Electron file:// + IPC), nothing is
23
+ * registered and the STEP body reports the reader as unavailable in-document —
24
+ * a degraded viewer, never a broken app.
25
+ *
26
+ * @module @huaqiu/dsh-hardware-ui
7
27
  */
8
- export const name = '@huaqiu/dsh-hardware-ui'
28
+ import { readFileSync } from 'node:fs'
29
+ import path from 'node:path'
30
+ import { createRequire } from 'node:module'
31
+ import type { IncomingMessage, ServerResponse } from 'node:http'
32
+ import { OCCT_WASM_FILE, OCCT_WASM_ROUTE } from './shared/occt-asset.js'
9
33
 
10
- /** Browser-only plugin: the node Loader seat intentionally does nothing. */
11
- export function apply(): void {}
34
+ export const name = '@huaqiu/dsh-hardware-ui'
12
35
 
13
36
  /** The tab kind owned by this product UI. */
14
37
  export const HARDWARE_TAB_KIND = 'hardware-engineering'
15
38
 
16
39
  /**
17
- * Slots this product UI occupies. Exported for contract tests and readers.
40
+ * Slots this product UI occupies, plus the DSH seats it depends on.
18
41
  *
19
42
  * `conversation.session.header.corner` is DSH's own expand button (we occupy
20
- * nothing there); it is listed because the product depends on it. The
21
- * conversation header carries no product navigation any more: the right panel's
22
- * header is the only one.
43
+ * nothing there); it is listed because the product depends on it. The document
44
+ * seats are contributed by the browser half through DSH's document-preview
45
+ * registry, not by taking over the MDI.
23
46
  */
24
47
  export const HARDWARE_UI_SLOTS = Object.freeze([
25
48
  'conversation.session.header.corner',
26
49
  'sidebar.right.pane.tab',
50
+ 'sidebar.right.tab.document',
27
51
  'settings.section',
52
+ // First-run toolchain step, its transient notices, and the quiet sidebar dot.
53
+ 'settings.onboarding',
54
+ 'shell.overlay',
55
+ 'sidebar.footer.action',
28
56
  ] as const)
57
+
58
+ /** Structural view of the Host route seat; declared locally to keep this half dependency-free. */
59
+ interface RouteSeat {
60
+ register(route: {
61
+ kind: 'exact' | 'prefix'
62
+ path: string
63
+ handler: (req: IncomingMessage, res: ServerResponse) => void | Promise<void>
64
+ }): () => void
65
+ }
66
+
67
+ interface HostLike {
68
+ effect?(fn: () => (() => void) | void, label?: string): void
69
+ /**
70
+ * Cordis's nested injection. `webServer` is a *dependency*, not a precondition:
71
+ * reading `ctx.webServer` directly would make the whole product UI absent on a
72
+ * Host without an HTTP server, so it is acquired here and the rest of the
73
+ * plugin never learns whether it exists.
74
+ */
75
+ inject?(dependencies: readonly string[], callback: (scope: HostLike & { readonly webServer: RouteSeat }) => void): void
76
+ }
77
+
78
+ /** Resolved absolute path of the packaged reader, or undefined when the dependency is missing. */
79
+ function wasmFile(): string | undefined {
80
+ try {
81
+ const require = createRequire(import.meta.url)
82
+ return path.join(path.dirname(require.resolve('occt-import-js/package.json')), 'dist', OCCT_WASM_FILE)
83
+ } catch {
84
+ return undefined
85
+ }
86
+ }
87
+
88
+ /** Bytes are read once per process: the file cannot change while the app runs. */
89
+ let cached: Buffer | undefined
90
+
91
+ function wasmBytes(): Buffer | undefined {
92
+ if (cached !== undefined) return cached
93
+ const file = wasmFile()
94
+ if (file === undefined) return undefined
95
+ try {
96
+ cached = readFileSync(file)
97
+ return cached
98
+ } catch {
99
+ return undefined
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Node half entry.
105
+ *
106
+ * Registers the reader route when the Host serves HTTP; on a Host without one
107
+ * (Electron file:// + IPC) this intentionally does nothing and the STEP body
108
+ * reports the reader as unavailable.
109
+ * @param ctx - Host context; the web server is acquired through nested injection.
110
+ */
111
+ export function apply(ctx: HostLike): void {
112
+ if (ctx.inject === undefined) return
113
+ ctx.inject(['webServer'], (scope) => {
114
+ const server = scope.webServer
115
+ const install = (): (() => void) | void => server.register({
116
+ kind: 'exact',
117
+ path: OCCT_WASM_ROUTE,
118
+ handler: (req, res) => {
119
+ if (req.method !== 'GET' && req.method !== 'HEAD') {
120
+ res.writeHead(405, { allow: 'GET, HEAD' })
121
+ res.end()
122
+ return
123
+ }
124
+ const bytes = wasmBytes()
125
+ if (bytes === undefined) {
126
+ res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' })
127
+ res.end('occt-import-js.wasm is not installed')
128
+ return
129
+ }
130
+ res.writeHead(200, {
131
+ 'content-type': 'application/wasm',
132
+ 'content-length': String(bytes.byteLength),
133
+ // The module is versioned by the installed dependency, so a client that
134
+ // has it may keep it for the life of the app session.
135
+ 'cache-control': 'private, max-age=86400',
136
+ })
137
+ if (req.method === 'HEAD') res.end()
138
+ else res.end(bytes)
139
+ },
140
+ })
141
+ if (scope.effect !== undefined) scope.effect(install, 'hardware-ui: STEP reader asset')
142
+ else void install()
143
+ })
144
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Route and file name of the packaged STEP reader, shared by both halves.
3
+ *
4
+ * The browser half must fetch the `.wasm` from a same-origin URL and the Node
5
+ * half is the only side that can read the installed package's `dist` directory,
6
+ * so the two must agree on exactly one string. Sharing it here is cheaper than
7
+ * a protocol and impossible to drift: a change breaks both bundles or neither.
8
+ *
9
+ * Why a plugin route rather than a CDN or an inline base64 blob: the ordering
10
+ * spec requires the preview to work with no network access (§7.4, §12.3), and
11
+ * base64-inlining 7.6 MB into the client bundle would parse on every DSH start
12
+ * whether or not a STEP document is ever opened.
13
+ *
14
+ * @module @huaqiu/dsh-hardware-ui/shared/occt-asset
15
+ */
16
+
17
+ /** Absolute route the Host serves the reader wasm from; also the browser's fetch URL. */
18
+ export const OCCT_WASM_ROUTE = '/hardware-ui/assets/occt-import-js.wasm'
19
+
20
+ /** File name inside the installed `occt-import-js` package. */
21
+ export const OCCT_WASM_FILE = 'occt-import-js.wasm'