@linxin666/dsh-remote-bridge 0.4.7 → 0.4.9

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 (78) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +93 -26
  3. package/README.zh.md +91 -26
  4. package/icon.svg +3 -0
  5. package/lib/client.js +274 -253
  6. package/lib/client.js.map +1 -1
  7. package/lib/index.js +8891 -2433
  8. package/lib/types/client/PairEntry.d.ts +2 -2
  9. package/lib/types/client/PairPanel.d.ts +10 -2
  10. package/lib/types/client/icons.d.ts +5 -13
  11. package/lib/types/client/locales.d.ts +10 -14
  12. package/lib/types/client/semantic.d.ts +9 -5
  13. package/lib/types/core/approvals.d.ts +24 -0
  14. package/lib/types/core/commands.d.ts +59 -0
  15. package/lib/types/core/connection.d.ts +124 -5
  16. package/lib/types/core/delta.d.ts +101 -0
  17. package/lib/types/core/devices.d.ts +98 -2
  18. package/lib/types/core/dsh-frames.d.ts +155 -4
  19. package/lib/types/core/envelope.d.ts +34 -0
  20. package/lib/types/core/extension.d.ts +144 -0
  21. package/lib/types/core/hub.d.ts +96 -20
  22. package/lib/types/core/jobs.d.ts +204 -0
  23. package/lib/types/core/liveactivity.d.ts +324 -0
  24. package/lib/types/core/protocol.d.ts +491 -2
  25. package/lib/types/core/push.d.ts +178 -0
  26. package/lib/types/core/questions.d.ts +109 -9
  27. package/lib/types/core/session-meta.d.ts +55 -1
  28. package/lib/types/core/squads.d.ts +130 -0
  29. package/lib/types/core/usage-stats.d.ts +160 -0
  30. package/lib/types/core/usage.d.ts +294 -0
  31. package/lib/types/core/wire.d.ts +1 -1
  32. package/lib/types/core/ws-frames.d.ts +11 -0
  33. package/lib/types/host/apns.d.ts +151 -0
  34. package/lib/types/host/squads-ledger.d.ts +39 -0
  35. package/lib/types/host/squads.d.ts +77 -0
  36. package/lib/types/host/usage-stats.d.ts +75 -0
  37. package/lib/types/host/watchers.d.ts +88 -0
  38. package/lib/types/host/websocket.d.ts +8 -1
  39. package/lib/types/index.d.ts +34 -3
  40. package/lib/types/mount-once.d.ts +55 -0
  41. package/locale/en.json +6 -0
  42. package/locale/ru.json +6 -0
  43. package/locale/zh.json +6 -0
  44. package/package.json +20 -14
  45. package/src/client/PairEntry.tsx +58 -31
  46. package/src/client/PairPanel.tsx +181 -97
  47. package/src/client/icons.tsx +17 -60
  48. package/src/client/locales.ts +15 -23
  49. package/src/client/panel.module.css +239 -186
  50. package/src/client/semantic.ts +10 -6
  51. package/src/core/approvals.ts +32 -1
  52. package/src/core/commands.ts +95 -0
  53. package/src/core/connection.ts +1483 -31
  54. package/src/core/delta.ts +204 -0
  55. package/src/core/devices.ts +221 -3
  56. package/src/core/dsh-frames.ts +365 -8
  57. package/src/core/envelope.ts +46 -0
  58. package/src/core/extension.ts +197 -0
  59. package/src/core/hub.ts +441 -49
  60. package/src/core/jobs.ts +372 -0
  61. package/src/core/liveactivity.ts +833 -0
  62. package/src/core/protocol.ts +556 -2
  63. package/src/core/push.ts +354 -0
  64. package/src/core/questions.ts +264 -17
  65. package/src/core/session-meta.ts +73 -1
  66. package/src/core/squads.ts +345 -0
  67. package/src/core/usage-stats.ts +424 -0
  68. package/src/core/usage.ts +937 -0
  69. package/src/core/wire.ts +13 -1
  70. package/src/core/ws-frames.ts +14 -0
  71. package/src/host/apns.ts +320 -0
  72. package/src/host/squads-ledger.ts +105 -0
  73. package/src/host/squads.ts +206 -0
  74. package/src/host/usage-stats.ts +155 -0
  75. package/src/host/watchers.ts +272 -0
  76. package/src/host/websocket.ts +37 -3
  77. package/src/index.ts +310 -9
  78. package/src/mount-once.ts +142 -0
@@ -0,0 +1,197 @@
1
+ /**
2
+ * The bridge's extension seam: the one place another plugin adds methods and
3
+ * events to protocol v1 without the bridge learning what they mean.
4
+ *
5
+ * The bridge publishes this seam as the cordis service
6
+ * {@link REMOTE_BRIDGE_SERVICE}. A plugin registers a table of methods plus the
7
+ * capability a client must declare before those methods exist for it, and
8
+ * publishes events to the clients that declared the capability it names.
9
+ * Nothing here carries protocol vocabulary of its own: method names, payloads,
10
+ * topics and refusal codes belong to the registering plugin, which is why this
11
+ * module never appears in `KNOWN_METHODS` and never reads a payload.
12
+ *
13
+ * A connection dispatches an extension method only after the protocol's own
14
+ * gates: an unregistered name, a plugin that is not mounted, and a client that
15
+ * never declared the capability all answer `unknown-method` — the method is
16
+ * absent from that client's protocol, not refused by it.
17
+ * @module dsh-remote-bridge/core/extension
18
+ */
19
+
20
+ import type { RemoteBridgeDeltas } from './delta.ts'
21
+ import { KNOWN_METHODS } from './protocol.ts'
22
+
23
+ /** Cordis service this seam is published under. */
24
+ export const REMOTE_BRIDGE_SERVICE = 'remoteBridge'
25
+
26
+ /**
27
+ * Who is calling one extension method.
28
+ *
29
+ * The bridge exposes no device or session identity here on purpose: what an
30
+ * extension keys by connection is its own decision.
31
+ */
32
+ export interface RemoteBridgeCall {
33
+ /**
34
+ * Stable identity of the calling connection, unique within this bridge
35
+ * process. Two calls from one socket carry the same id; a reconnected socket
36
+ * carries a new one, because nothing proves it is the same peer.
37
+ */
38
+ readonly connectionId: string
39
+ /**
40
+ * @param capability - one capability name of this protocol or of the calling
41
+ * extension.
42
+ * @returns whether this connection announced it in `hello`.
43
+ *
44
+ * The seam hands handlers the same predicate the bridge itself gates on, so
45
+ * an extension can gate behind a bridge capability (§15's `delta.v1`) without
46
+ * holding a copy of the client's declaration.
47
+ */
48
+ declares(capability: string): boolean
49
+ /**
50
+ * This connection's delta windows, released with the connection.
51
+ *
52
+ * The mechanism of §15 lives once, in this bridge, and an extension borrows it
53
+ * per connection: the difference is always computed where the last answer is
54
+ * already held, and a dropped connection takes its base with it instead of
55
+ * leaving one to be expired by a second registry.
56
+ */
57
+ readonly deltas: RemoteBridgeDeltas
58
+ }
59
+
60
+ /**
61
+ * What one extension method answers: the response body, or a refusal stated in
62
+ * the extension's own code vocabulary. The bridge forwards either verbatim —
63
+ * it holds no table of extension codes and invents none.
64
+ */
65
+ export type RemoteBridgeMethodResult =
66
+ | { readonly ok: true; readonly r?: unknown }
67
+ | { readonly ok: false; readonly code: string; readonly msg: string }
68
+
69
+ /** One client-callable method an extension owns. */
70
+ export type RemoteBridgeMethodHandler = (
71
+ payload: Readonly<Record<string, unknown>>,
72
+ call: RemoteBridgeCall,
73
+ ) => RemoteBridgeMethodResult | Promise<RemoteBridgeMethodResult>
74
+
75
+ /** A table of methods and the capability that admits a client to them. */
76
+ export interface RemoteBridgeMethods {
77
+ /**
78
+ * Capability a client declares in `hello`'s `cap` array before these methods
79
+ * are part of its protocol. Absent means every paired connection may call
80
+ * them; an unpaired connection never may, whatever it declared.
81
+ */
82
+ readonly capability?: string
83
+ /** Method name to handler; the name is the extension's, not the bridge's. */
84
+ readonly methods: Readonly<Record<string, RemoteBridgeMethodHandler>>
85
+ }
86
+
87
+ /** The seam, exactly as published on {@link REMOTE_BRIDGE_SERVICE}. */
88
+ export interface RemoteBridgeFace {
89
+ /**
90
+ * Add methods to the protocol surface.
91
+ * @returns the disposer withdrawing them; afterwards they answer `unknown-method` again.
92
+ */
93
+ registerMethods(table: RemoteBridgeMethods): () => void
94
+ /**
95
+ * Publish one event to the connections that are paired — and, when a
96
+ * capability is named, have declared it.
97
+ * @returns how many connections took the frame.
98
+ */
99
+ broadcast(topic: string, payload: unknown, capability?: string): number
100
+ }
101
+
102
+ /** The fan-out the seam publishes through; `BridgeHub` satisfies it. */
103
+ export interface RemoteBridgeFanout {
104
+ broadcast(topic: string, payload: unknown): number
105
+ broadcastTo(capability: string, topic: string, payload: unknown): number
106
+ }
107
+
108
+ /** One registered method. */
109
+ interface RegisteredMethod {
110
+ readonly capability: string | undefined
111
+ readonly handler: RemoteBridgeMethodHandler
112
+ }
113
+
114
+ /**
115
+ * The methods extensions registered, and the capability each one needs.
116
+ *
117
+ * Every connection consults this registry; nothing else holds extension state,
118
+ * so unmounting one plugin withdraws exactly its own methods.
119
+ */
120
+ export class RemoteBridgeRegistry {
121
+ readonly #methods = new Map<string, RegisteredMethod>()
122
+
123
+ /**
124
+ * Register one table of methods, all or nothing: a table naming a protocol
125
+ * method, an empty name or a name already taken is refused before any of its
126
+ * methods becomes callable.
127
+ * @param table - the methods and the capability they require.
128
+ * @returns the disposer withdrawing this table; idempotent.
129
+ */
130
+ registerMethods(table: RemoteBridgeMethods): () => void {
131
+ const entries: [string, RegisteredMethod][] = []
132
+ for (const [name, handler] of Object.entries(table.methods)) {
133
+ if (name.trim() === '') throw new Error('a remote bridge method needs a non-empty name')
134
+ if (typeof handler !== 'function') throw new Error(`remote bridge method ${name} has no handler`)
135
+ // A protocol method is decided by the bridge's own dispatch. Shadowing one
136
+ // here would change protocol v1 without changing its version, so it is
137
+ // refused at registration rather than resolved by dispatch order.
138
+ if (KNOWN_METHODS.has(name)) throw new Error(`remote bridge method ${name} is a protocol method`)
139
+ if (this.#methods.has(name)) throw new Error(`remote bridge method ${name} is already registered`)
140
+ entries.push([name, { capability: table.capability, handler }])
141
+ }
142
+ for (const [name, entry] of entries) this.#methods.set(name, entry)
143
+ return () => {
144
+ for (const [name, entry] of entries) {
145
+ // Identity, not just the name: a table registered after this disposer
146
+ // ran keeps its own method.
147
+ if (this.#methods.get(name) === entry) this.#methods.delete(name)
148
+ }
149
+ }
150
+ }
151
+
152
+ /**
153
+ * @param method - the method name a client asked for.
154
+ * @returns whether any plugin registered it, whatever capability it requires.
155
+ */
156
+ has(method: string): boolean {
157
+ return this.#methods.has(method)
158
+ }
159
+
160
+ /**
161
+ * @param method - the method name a client asked for.
162
+ * @param declares - whether the calling connection declared a capability.
163
+ * @returns the handler when the caller may use the method, else undefined.
164
+ */
165
+ handlerFor(method: string, declares: (capability: string) => boolean): RemoteBridgeMethodHandler | undefined {
166
+ const entry = this.#methods.get(method)
167
+ if (entry === undefined) return undefined
168
+ if (entry.capability !== undefined && !declares(entry.capability)) return undefined
169
+ return entry.handler
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Compose the published seam over a registry and a fan-out.
175
+ * @param methods - the registry this bridge owns.
176
+ * @param fanout - the hub's broadcast pair.
177
+ * @returns the face a plugin resolves from {@link REMOTE_BRIDGE_SERVICE}.
178
+ */
179
+ export function createRemoteBridgeFace(methods: RemoteBridgeRegistry, fanout: RemoteBridgeFanout): RemoteBridgeFace {
180
+ return {
181
+ registerMethods: table => methods.registerMethods(table),
182
+ broadcast: (topic, payload, capability) => capability === undefined
183
+ ? fanout.broadcast(topic, payload)
184
+ : fanout.broadcastTo(capability, topic, payload),
185
+ }
186
+ }
187
+
188
+ declare module '@deepseek-ai/cordis' {
189
+ interface Context {
190
+ /**
191
+ * The bridge's extension seam, while this package serves protocol v1. A
192
+ * plugin resolves it structurally at use time: requiring it would let the
193
+ * bridge's absence take the registering plugin's start down with it.
194
+ */
195
+ remoteBridge: RemoteBridgeFace
196
+ }
197
+ }