@linxin666/dsh-remote-bridge 0.0.0-stage → 0.4.7

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.
Files changed (69) hide show
  1. package/LICENSE +202 -0
  2. package/README.i18n.yaml +6 -0
  3. package/README.md +243 -2
  4. package/README.zh.md +244 -0
  5. package/cordis.patch.yml +17 -0
  6. package/lib/client.js +1830 -0
  7. package/lib/client.js.map +1 -0
  8. package/lib/index.js +3744 -0
  9. package/lib/types/client/PairEntry.d.ts +16 -0
  10. package/lib/types/client/PairPanel.d.ts +55 -0
  11. package/lib/types/client/icons.d.ts +18 -0
  12. package/lib/types/client/index.d.ts +35 -0
  13. package/lib/types/client/locales.d.ts +42 -0
  14. package/lib/types/client/pair-token.d.ts +73 -0
  15. package/lib/types/client/semantic.d.ts +28 -0
  16. package/lib/types/core/approvals.d.ts +75 -0
  17. package/lib/types/core/connection.d.ts +119 -0
  18. package/lib/types/core/crypto.d.ts +48 -0
  19. package/lib/types/core/devices.d.ts +72 -0
  20. package/lib/types/core/dsh-frames.d.ts +181 -0
  21. package/lib/types/core/envelope.d.ts +98 -0
  22. package/lib/types/core/gateway-errors.d.ts +49 -0
  23. package/lib/types/core/hub.d.ts +101 -0
  24. package/lib/types/core/loopback.d.ts +35 -0
  25. package/lib/types/core/owner-peer.d.ts +49 -0
  26. package/lib/types/core/pairing.d.ts +61 -0
  27. package/lib/types/core/protocol.d.ts +89 -0
  28. package/lib/types/core/questions.d.ts +246 -0
  29. package/lib/types/core/session-meta.d.ts +105 -0
  30. package/lib/types/core/session-permissions.d.ts +134 -0
  31. package/lib/types/core/wire.d.ts +115 -0
  32. package/lib/types/core/ws-frames.d.ts +61 -0
  33. package/lib/types/host/dsh-home.d.ts +16 -0
  34. package/lib/types/host/pair-origin.d.ts +44 -0
  35. package/lib/types/host/routes.d.ts +27 -0
  36. package/lib/types/host/websocket.d.ts +104 -0
  37. package/lib/types/index.d.ts +40 -0
  38. package/package.json +87 -3
  39. package/src/client/PairEntry.tsx +151 -0
  40. package/src/client/PairPanel.tsx +183 -0
  41. package/src/client/icons.tsx +72 -0
  42. package/src/client/index.ts +81 -0
  43. package/src/client/locales.ts +72 -0
  44. package/src/client/pair-token.ts +104 -0
  45. package/src/client/panel.module.css +344 -0
  46. package/src/client/semantic.ts +37 -0
  47. package/src/core/approvals.ts +142 -0
  48. package/src/core/connection.ts +1090 -0
  49. package/src/core/crypto.ts +113 -0
  50. package/src/core/devices.ts +209 -0
  51. package/src/core/dsh-frames.ts +486 -0
  52. package/src/core/envelope.ts +173 -0
  53. package/src/core/gateway-errors.ts +92 -0
  54. package/src/core/hub.ts +238 -0
  55. package/src/core/loopback.ts +60 -0
  56. package/src/core/owner-peer.ts +70 -0
  57. package/src/core/pairing.ts +113 -0
  58. package/src/core/protocol.ts +120 -0
  59. package/src/core/questions.ts +454 -0
  60. package/src/core/session-meta.ts +176 -0
  61. package/src/core/session-permissions.ts +266 -0
  62. package/src/core/wire.ts +155 -0
  63. package/src/core/ws-frames.ts +125 -0
  64. package/src/css-modules.d.ts +6 -0
  65. package/src/host/dsh-home.ts +42 -0
  66. package/src/host/pair-origin.ts +77 -0
  67. package/src/host/routes.ts +85 -0
  68. package/src/host/websocket.ts +325 -0
  69. package/src/index.ts +205 -0
package/src/index.ts ADDED
@@ -0,0 +1,205 @@
1
+ /**
2
+ * `@linxin666/dsh-remote-bridge` — the Host half of the DSH remote bridge.
3
+ *
4
+ * One plugin row exposes the frozen protocol v1 to a paired remote client:
5
+ *
6
+ * - `/ios/v1` (WebSocket upgrade) carries the whole protocol. Admission is
7
+ * unconditional: a connection without a valid device token is UNPAIRED and
8
+ * may only call `hello`, `pair.begin` and `pair.finish` — even from loopback.
9
+ * - `/remote-bridge/pair/token` (exact HTTP route) mints the one-shot bootstrap
10
+ * token. It is admitted only when the request arrives from this machine AND
11
+ * its `Host` header names this machine: a tunneled request reaches the origin
12
+ * from loopback too, so the socket address alone is not a loopback proof.
13
+ * - `approval/request` is claimed in-process while a paired device is connected,
14
+ * and delegated (`next()`) in every other case, which the Host normalizes to
15
+ * the fail-closed `'unavailable'`.
16
+ * - `user-questions/request` is claimed the same way and published as
17
+ * `question.request` to the devices that declared `questions.v1`; their
18
+ * `question.answer` settles the host's own ask while it is open, or reaches a
19
+ * continued question through the host's Remote `answer`. Every other case
20
+ * delegates, and the question keeps the Host's own deadline.
21
+ *
22
+ * The browser half lives in `src/client/` and is declared by `dsh.client`: one
23
+ * sidebar-foot panel that reads the same loopback-gated token route over a
24
+ * same-origin request and renders the secret as a QR symbol beside its
25
+ * plaintext. It is display-only — it cannot mint, spend, list or revoke
26
+ * anything — and this module does not import it.
27
+ * @module @linxin666/dsh-remote-bridge
28
+ */
29
+
30
+ import type { IncomingMessage } from 'node:http'
31
+ import { join } from 'node:path'
32
+ import type { Duplex } from 'node:stream'
33
+ import type { Context } from '@deepseek-ai/cordis'
34
+ import type {} from '@deepseek-ai/dsh-api-gateway'
35
+ import type {} from '@deepseek-ai/dsh-host-webserver'
36
+ import type {} from '@deepseek-ai/dsh-session'
37
+ import type {} from '@deepseek-ai/dsh-user-approval'
38
+ import { ApprovalRegistry } from './core/approvals.ts'
39
+ import type { ConnectionAuth } from './core/connection.ts'
40
+ import { DeviceRoster } from './core/devices.ts'
41
+ import { BridgeHub } from './core/hub.ts'
42
+ import { createOwnerPeer } from './core/owner-peer.ts'
43
+ import { PairingService } from './core/pairing.ts'
44
+ import { QuestionClaims } from './core/questions.ts'
45
+ import { WS_PATH } from './core/protocol.ts'
46
+ import { SessionMeta, type ArchiveSetFace, type SessionStoreFace, type SessionTitleFace } from './core/session-meta.ts'
47
+ import { SessionPermissions, type ApprovalPolicyFace, type PermissionPresetsFace, type SandboxPolicyFace } from './core/session-permissions.ts'
48
+ import { dshHome } from './host/dsh-home.ts'
49
+ import { makePairTokenRoute } from './host/routes.ts'
50
+ import { isWebSocketUpgrade, presentedTokenOf, readSecWebSocketKey, refuseUpgrade, serveUpgrade } from './host/websocket.ts'
51
+
52
+ /** Loader row identity of this plugin. */
53
+ export const name = 'remote-bridge'
54
+
55
+ /** The Host services this plugin cannot run without. */
56
+ export const inject = ['typertGateway', 'webServer']
57
+
58
+ /** Where the paired-device roster lives inside the DSH home. */
59
+ export const ROSTER_PATH = 'remote-bridge/devices.json'
60
+
61
+ /** This machine's IANA time zone, or undefined when the platform has none. */
62
+ function localTimeZone(): string | undefined {
63
+ try {
64
+ const zone = Intl.DateTimeFormat().resolvedOptions().timeZone
65
+ return zone === '' ? undefined : zone
66
+ } catch {
67
+ return undefined
68
+ }
69
+ }
70
+
71
+ /**
72
+ * The `/ios/v1` upgrade handler.
73
+ *
74
+ * A bad, absent or bootstrap token is NOT a refusal: the connection is simply
75
+ * unpaired, which is the state the pairing handshake starts from. Only a
76
+ * malformed handshake is refused.
77
+ */
78
+ function makeUpgradeHandler(hub: BridgeHub, roster: DeviceRoster, log: (message: string) => void) {
79
+ return async (req: IncomingMessage, socket: Duplex, head: Buffer): Promise<void> => {
80
+ if (!isWebSocketUpgrade(req)) {
81
+ refuseUpgrade(socket, 400, 'Bad Request')
82
+ return
83
+ }
84
+ const key = readSecWebSocketKey(req)
85
+ if (key === undefined) {
86
+ refuseUpgrade(socket, 400, 'Bad Request')
87
+ return
88
+ }
89
+ try {
90
+ await roster.load()
91
+ } catch (error) {
92
+ // An unreadable roster leaves every candidate unpaired. Refusing the upgrade
93
+ // says so; letting the handler's promise reject instead would hand the host's
94
+ // upgrade dispatcher a failure it has no answer for and no client would ever see.
95
+ log(`could not read the device roster: ${String(error)}`)
96
+ refuseUpgrade(socket, 503, 'Service Unavailable')
97
+ return
98
+ }
99
+ const token = presentedTokenOf(req)
100
+ const device = token === undefined ? undefined : roster.authenticate(token)
101
+ const auth: ConnectionAuth | undefined = device === undefined ? undefined : { deviceId: device.deviceId }
102
+ serveUpgrade({
103
+ socket,
104
+ secWebSocketKey: key,
105
+ head,
106
+ hub,
107
+ ...(auth === undefined ? {} : { auth }),
108
+ })
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Resolve the host services session metadata is read from, now.
114
+ *
115
+ * All three are read per call rather than captured at mount: none of them is in
116
+ * this plugin's `inject` list, because requiring them would let the title or
117
+ * workspace plugin's absence take the whole bridge down with it. An unmounted
118
+ * service simply reads as undefined, and the protocol degrades one key of one
119
+ * row instead of failing a method. The faces are structural — this package
120
+ * imports no part of those services' own types.
121
+ * @param ctx - the plugin's context.
122
+ * @returns the metadata faces as they stand right now.
123
+ */
124
+ function sessionMetaFaces(ctx: Context) {
125
+ return {
126
+ sessions: ctx.get('sessions') as SessionStoreFace | undefined,
127
+ titles: ctx.get('sessionTitle') as SessionTitleFace | undefined,
128
+ archive: ctx.get('workspaceRegistry') as ArchiveSetFace | undefined,
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Resolve the host services permission presets are read and switched through, now.
134
+ *
135
+ * Read per call, never captured at mount, for the reason the metadata faces are:
136
+ * none of these is in this plugin's `inject` list, and a host that composes no
137
+ * permission plugin answers `unknown-method` instead of failing to start. The
138
+ * `Session` object the preset service needs is the store's own live instance —
139
+ * the same object `sessionTitle` already takes — so no second lookup path exists
140
+ * for it.
141
+ * @param ctx - the plugin's context.
142
+ * @returns the permission faces as they stand right now.
143
+ */
144
+ function permissionFaces(ctx: Context) {
145
+ return {
146
+ sessions: ctx.get('sessions') as SessionStoreFace | undefined,
147
+ presets: ctx.get('permissionPresets') as PermissionPresetsFace | undefined,
148
+ sandbox: ctx.get('sandboxPolicy') as SandboxPolicyFace | undefined,
149
+ approval: ctx.get('approval') as ApprovalPolicyFace | undefined,
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Mount the bridge.
155
+ * @param ctx - the plugin's context, carrying the gateway and web server.
156
+ */
157
+ export function apply(ctx: Context): void {
158
+ const log = (message: string): void => { console.log(`[remote-bridge] ${message}`) }
159
+ const roster = new DeviceRoster({ file: join(dshHome(), ROSTER_PATH), log })
160
+ const pairing = new PairingService()
161
+ const approvals = new ApprovalRegistry()
162
+ // The owner peer is the full-control credential the gateway demands since
163
+ // 0.2.1; it is safe to hold from mount because every call that can trigger
164
+ // it is gated by the bridge's own pairing check (see core/owner-peer.ts).
165
+ const ownerPeer = createOwnerPeer(ctx)
166
+ const questions = new QuestionClaims({ gateway: ctx.typertGateway, peer: ownerPeer, log })
167
+ const sessionMeta = new SessionMeta(() => sessionMetaFaces(ctx), () => Date.now(), log)
168
+ const permissions = new SessionPermissions(() => permissionFaces(ctx), log)
169
+ const hub = new BridgeHub({
170
+ gateway: ctx.typertGateway,
171
+ pairing,
172
+ roster,
173
+ approvals,
174
+ questions,
175
+ sessionMeta,
176
+ permissions,
177
+ ownerPeer,
178
+ now: () => Date.now(),
179
+ clientTimeZone: localTimeZone,
180
+ log,
181
+ })
182
+
183
+ ctx.effect(() => {
184
+ void roster.load()
185
+ const disposeUpgrade = ctx.webServer.registerUpgrade({
186
+ path: WS_PATH,
187
+ handler: makeUpgradeHandler(hub, roster, log),
188
+ })
189
+ const disposeRoute = ctx.webServer.register(makePairTokenRoute({ pairing, log }))
190
+ log(`protocol v1 listening on ${WS_PATH}`)
191
+ return () => {
192
+ disposeUpgrade()
193
+ disposeRoute()
194
+ hub.dispose()
195
+ }
196
+ }, 'remote-bridge: protocol v1 routes')
197
+
198
+ ctx.on('approval/request', (request, next) => hub.approve(request, next))
199
+ // Prepended: the host's waterfall is first-come, and the desktop client's own
200
+ // answerer would otherwise claim every question before this listener is
201
+ // consulted. The claim starts the rest of the chain itself, so prepending does
202
+ // not take the desktop panel away — both surfaces show the question and the
203
+ // first answer settles it.
204
+ ctx.on('user-questions/request', (request, next) => hub.question(request, next), { prepend: true })
205
+ }