@linxin666/dsh-remote-bridge 0.4.8 → 0.4.10
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/README.i18n.yaml +2 -2
- package/README.md +75 -18
- package/README.zh.md +74 -18
- package/lib/client.js +783 -260
- package/lib/client.js.map +1 -1
- package/lib/index.js +15849 -2913
- package/lib/types/client/PairEntry.d.ts +2 -2
- package/lib/types/client/PairPanel.d.ts +10 -2
- package/lib/types/client/UiPresentRow.d.ts +13 -0
- package/lib/types/client/icons.d.ts +5 -13
- package/lib/types/client/index.d.ts +4 -0
- package/lib/types/client/locales.d.ts +24 -15
- package/lib/types/client/semantic.d.ts +9 -5
- package/lib/types/client/toolview-slot.d.ts +68 -0
- package/lib/types/client/ui-card.d.ts +160 -0
- package/lib/types/core/commands.d.ts +59 -0
- package/lib/types/core/connection.d.ts +106 -0
- package/lib/types/core/devices.d.ts +102 -2
- package/lib/types/core/extension.d.ts +13 -0
- package/lib/types/core/hub.d.ts +129 -15
- package/lib/types/core/liveactivity.d.ts +324 -0
- package/lib/types/core/protocol.d.ts +328 -5
- package/lib/types/core/push.d.ts +186 -0
- package/lib/types/core/questions.d.ts +61 -9
- package/lib/types/core/session-meta.d.ts +55 -1
- package/lib/types/core/squads.d.ts +130 -0
- package/lib/types/core/turn-end-quiet.d.ts +135 -0
- package/lib/types/core/ui.d.ts +238 -0
- package/lib/types/core/usage-stats.d.ts +160 -0
- package/lib/types/core/usage.d.ts +294 -0
- package/lib/types/host/apns.d.ts +151 -0
- package/lib/types/host/squads-ledger.d.ts +39 -0
- package/lib/types/host/squads.d.ts +77 -0
- package/lib/types/host/ui-tools.d.ts +80 -0
- package/lib/types/host/usage-stats.d.ts +75 -0
- package/lib/types/host/watchers.d.ts +95 -0
- package/lib/types/index.d.ts +47 -3
- package/package.json +15 -14
- package/src/client/PairEntry.tsx +58 -31
- package/src/client/PairPanel.tsx +181 -97
- package/src/client/UiPresentRow.tsx +149 -0
- package/src/client/icons.tsx +17 -60
- package/src/client/index.ts +11 -0
- package/src/client/locales.ts +36 -24
- package/src/client/panel.module.css +239 -186
- package/src/client/semantic.ts +10 -6
- package/src/client/toolview-slot.ts +84 -0
- package/src/client/ui-card.ts +368 -0
- package/src/client/ui-present.module.css +87 -0
- package/src/core/commands.ts +95 -0
- package/src/core/connection.ts +811 -12
- package/src/core/devices.ts +337 -5
- package/src/core/extension.ts +20 -0
- package/src/core/hub.ts +518 -43
- package/src/core/liveactivity.ts +833 -0
- package/src/core/protocol.ts +370 -5
- package/src/core/push.ts +385 -0
- package/src/core/questions.ts +81 -10
- package/src/core/session-meta.ts +73 -1
- package/src/core/squads.ts +345 -0
- package/src/core/turn-end-quiet.ts +388 -0
- package/src/core/ui.ts +398 -0
- package/src/core/usage-stats.ts +424 -0
- package/src/core/usage.ts +937 -0
- package/src/core/wire.ts +8 -0
- package/src/host/apns.ts +320 -0
- package/src/host/squads-ledger.ts +105 -0
- package/src/host/squads.ts +206 -0
- package/src/host/ui-tools.ts +233 -0
- package/src/host/usage-stats.ts +155 -0
- package/src/host/watchers.ts +296 -0
- package/src/index.ts +327 -10
package/src/core/devices.ts
CHANGED
|
@@ -20,8 +20,61 @@ import { base64UrlEncode, constantTimeEqualText, fingerprintOfSpki, isP256Spki,
|
|
|
20
20
|
export const DEVICE_TOKEN_BYTES = 32
|
|
21
21
|
/** Entropy of one device id. */
|
|
22
22
|
export const DEVICE_ID_BYTES = 12
|
|
23
|
-
/**
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* On-disk schema version of the roster file.
|
|
25
|
+
*
|
|
26
|
+
* 1 carried the six identity fields only; 2 adds the optional `push` block per
|
|
27
|
+
* row (§28). The difference is additive, so a version-1 file reads as-is: no row
|
|
28
|
+
* loses a field, every paired phone stays paired, and the file is rewritten as
|
|
29
|
+
* version 2 the next time anything is written. The reader therefore accepts any
|
|
30
|
+
* version whose row table validates rather than quarantining a file it merely
|
|
31
|
+
* does not recognise — a downgrade must never un-pair every phone.
|
|
32
|
+
*/
|
|
33
|
+
export const ROSTER_VERSION = 2
|
|
34
|
+
|
|
35
|
+
/** The APNs environments a device may register for. */
|
|
36
|
+
export type PushEnvironment = 'sandbox' | 'production'
|
|
37
|
+
|
|
38
|
+
/** One Live Activity push token, with the session the card belongs to. */
|
|
39
|
+
export interface PushLiveActivityToken {
|
|
40
|
+
readonly activityId: string
|
|
41
|
+
readonly token: string
|
|
42
|
+
/**
|
|
43
|
+
* The session this card belongs to (§29.9).
|
|
44
|
+
*
|
|
45
|
+
* A card is about one session, and a token without this field cannot be
|
|
46
|
+
* addressed at all: this bridge has nothing to put on it. Absent is therefore
|
|
47
|
+
* "never pushed to", not "pushed to with whatever is at hand" — the other
|
|
48
|
+
* reading would put one session's state on another session's lock screen.
|
|
49
|
+
*/
|
|
50
|
+
readonly sessionId?: string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What one device registered so this bridge can notify it.
|
|
55
|
+
*
|
|
56
|
+
* Stored beside the device's identity in the same row: it is a fact about that
|
|
57
|
+
* device, so it disappears with the row when the device is revoked and it never
|
|
58
|
+
* becomes a second identity. Nothing here is a secret of the bridge's — an APNs
|
|
59
|
+
* token is the phone's, and holding it grants nothing without the signing key,
|
|
60
|
+
* which never touches this file.
|
|
61
|
+
*/
|
|
62
|
+
export interface PushRegistration {
|
|
63
|
+
/** APNs device token, as the hex string the system handed the app. */
|
|
64
|
+
readonly deviceToken: string
|
|
65
|
+
readonly environment: PushEnvironment
|
|
66
|
+
/** The app's bundle id, used verbatim as `apns-topic`. */
|
|
67
|
+
readonly bundle: string
|
|
68
|
+
readonly locale?: string
|
|
69
|
+
readonly liveActivityTokens?: readonly PushLiveActivityToken[]
|
|
70
|
+
readonly registeredAt: number
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One device a notification can be addressed to. */
|
|
74
|
+
export interface PushTargetView {
|
|
75
|
+
readonly deviceId: string
|
|
76
|
+
readonly push: PushRegistration
|
|
77
|
+
}
|
|
25
78
|
|
|
26
79
|
/** One paired device as the bridge keeps it. */
|
|
27
80
|
export interface DeviceRecord {
|
|
@@ -32,6 +85,12 @@ export interface DeviceRecord {
|
|
|
32
85
|
readonly fingerprint: string
|
|
33
86
|
readonly createdAt: number
|
|
34
87
|
readonly lastSeenAt: number
|
|
88
|
+
/**
|
|
89
|
+
* The device's APNs registration, absent until it calls `push.register`.
|
|
90
|
+
* A row without it is a paired phone that asks for no notification, which is
|
|
91
|
+
* every phone that predates the channel.
|
|
92
|
+
*/
|
|
93
|
+
readonly push?: PushRegistration
|
|
35
94
|
}
|
|
36
95
|
|
|
37
96
|
/** A roster row plus the one field that never leaves the file. */
|
|
@@ -53,6 +112,59 @@ export interface DeviceRosterOptions {
|
|
|
53
112
|
readonly log?: (message: string) => void
|
|
54
113
|
}
|
|
55
114
|
|
|
115
|
+
/**
|
|
116
|
+
* Read one row's `push` block.
|
|
117
|
+
*
|
|
118
|
+
* A malformed block is dropped rather than quarantining the file: the roster is
|
|
119
|
+
* every paired phone, and one unreadable push token is not a reason to un-pair
|
|
120
|
+
* them all. The row keeps its identity and simply asks for no notification.
|
|
121
|
+
* @param value - the raw `push` field of a row.
|
|
122
|
+
* @param log - where a dropped block is reported.
|
|
123
|
+
* @returns the registration, or undefined when there is none to read.
|
|
124
|
+
*/
|
|
125
|
+
function pushOf(value: unknown, log: (message: string) => void): PushRegistration | undefined {
|
|
126
|
+
if (value === undefined) return undefined
|
|
127
|
+
if (typeof value !== 'object' || value === null) {
|
|
128
|
+
log('a device row carries an unreadable push block; ignoring it and keeping the row')
|
|
129
|
+
return undefined
|
|
130
|
+
}
|
|
131
|
+
const row = value as Record<string, unknown>
|
|
132
|
+
const environment = row.environment
|
|
133
|
+
if (typeof row.deviceToken !== 'string' || row.deviceToken === ''
|
|
134
|
+
|| typeof row.bundle !== 'string' || row.bundle === ''
|
|
135
|
+
|| (environment !== 'sandbox' && environment !== 'production')) {
|
|
136
|
+
log('a device row carries an unreadable push block; ignoring it and keeping the row')
|
|
137
|
+
return undefined
|
|
138
|
+
}
|
|
139
|
+
const liveActivityTokens = Array.isArray(row.liveActivityTokens)
|
|
140
|
+
? row.liveActivityTokens.filter((entry): entry is PushLiveActivityToken => {
|
|
141
|
+
if (typeof entry !== 'object' || entry === null) return false
|
|
142
|
+
const token = entry as Record<string, unknown>
|
|
143
|
+
return typeof token.activityId === 'string' && token.activityId !== ''
|
|
144
|
+
&& typeof token.token === 'string' && token.token !== ''
|
|
145
|
+
}).map((entry): PushLiveActivityToken => {
|
|
146
|
+
// A session id this reader cannot trust is dropped rather than repaired: the
|
|
147
|
+
// entry keeps its token and simply names no session, which is the state this
|
|
148
|
+
// bridge never pushes to. One unreadable card is not a reason to un-pair a
|
|
149
|
+
// phone, the same way one unreadable push block is not.
|
|
150
|
+
const raw = (entry as unknown as Record<string, unknown>).sessionId
|
|
151
|
+
return {
|
|
152
|
+
activityId: entry.activityId,
|
|
153
|
+
token: entry.token,
|
|
154
|
+
...(typeof raw === 'string' && raw !== '' ? { sessionId: raw } : {}),
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
: undefined
|
|
158
|
+
return {
|
|
159
|
+
deviceToken: row.deviceToken,
|
|
160
|
+
environment,
|
|
161
|
+
bundle: row.bundle,
|
|
162
|
+
...(typeof row.locale === 'string' && row.locale !== '' ? { locale: row.locale } : {}),
|
|
163
|
+
...(liveActivityTokens === undefined ? {} : { liveActivityTokens }),
|
|
164
|
+
registeredAt: typeof row.registeredAt === 'number' ? row.registeredAt : 0,
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
56
168
|
function isStoredDevice(value: unknown): value is StoredDevice {
|
|
57
169
|
if (typeof value !== 'object' || value === null) return false
|
|
58
170
|
const row = value as Record<string, unknown>
|
|
@@ -64,12 +176,34 @@ function isStoredDevice(value: unknown): value is StoredDevice {
|
|
|
64
176
|
&& typeof row.lastSeenAt === 'number'
|
|
65
177
|
}
|
|
66
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Which of several rows for one phone is the one that survives.
|
|
181
|
+
*
|
|
182
|
+
* The most recently seen row wins and a tie is settled by the later
|
|
183
|
+
* `createdAt`: a pairing refreshes `lastSeenAt` on the row that is actually on
|
|
184
|
+
* the wire, so that is the row holding the credential in use.
|
|
185
|
+
* @param rows - rows that share one fingerprint.
|
|
186
|
+
* @returns the row to keep, or undefined when there is none.
|
|
187
|
+
*/
|
|
188
|
+
function survivorOf(rows: readonly StoredDevice[]): StoredDevice | undefined {
|
|
189
|
+
let survivor: StoredDevice | undefined
|
|
190
|
+
for (const row of rows) {
|
|
191
|
+
if (survivor === undefined || row.lastSeenAt > survivor.lastSeenAt
|
|
192
|
+
|| (row.lastSeenAt === survivor.lastSeenAt && row.createdAt > survivor.createdAt)) survivor = row
|
|
193
|
+
}
|
|
194
|
+
return survivor
|
|
195
|
+
}
|
|
196
|
+
|
|
67
197
|
/**
|
|
68
198
|
* The persisted, revocable device roster.
|
|
69
199
|
*
|
|
70
200
|
* Revocation is a file edit: protocol v1 has no revoke method, so an operator
|
|
71
201
|
* removes the row (or deletes the file) and restarts. The in-memory copy is
|
|
72
202
|
* loaded once per process.
|
|
203
|
+
*
|
|
204
|
+
* One phone is one row. Pairing again replaces the credential on the row the
|
|
205
|
+
* phone already has instead of adding a second one, and a table that still
|
|
206
|
+
* carries duplicates is compacted when it is read.
|
|
73
207
|
*/
|
|
74
208
|
export class DeviceRoster {
|
|
75
209
|
readonly #file: string
|
|
@@ -77,6 +211,7 @@ export class DeviceRoster {
|
|
|
77
211
|
readonly #log: (message: string) => void
|
|
78
212
|
readonly #devices = new Map<string, StoredDevice>()
|
|
79
213
|
#loaded: Promise<void> | undefined
|
|
214
|
+
#writeQueue: Promise<void> = Promise.resolve()
|
|
80
215
|
|
|
81
216
|
constructor(options: DeviceRosterOptions) {
|
|
82
217
|
this.#file = options.file
|
|
@@ -129,12 +264,28 @@ export class DeviceRoster {
|
|
|
129
264
|
if (!isP256Spki(pubKey)) return undefined
|
|
130
265
|
const fingerprint = fingerprintOfSpki(pubKey)
|
|
131
266
|
if (fingerprint === undefined) return undefined
|
|
267
|
+
// Pairing again is a new credential for a phone that is already here, not a
|
|
268
|
+
// second phone: the same public key is the same fingerprint, so the rows it
|
|
269
|
+
// already owns are settled first, the newest one kept and the rest dropped.
|
|
270
|
+
const sameDevice = [...this.#devices.values()].filter(row => row.fingerprint === fingerprint)
|
|
271
|
+
for (const row of sameDevice) this.#devices.delete(row.tokenHash)
|
|
272
|
+
const survivor = survivorOf(sameDevice)
|
|
132
273
|
const now = this.#now()
|
|
133
274
|
const deviceId = `dev_${randomBase64Url(DEVICE_ID_BYTES)}`
|
|
134
275
|
const token = base64UrlEncode(randomBytes(DEVICE_TOKEN_BYTES))
|
|
135
|
-
|
|
276
|
+
const hash = tokenHash(token)
|
|
277
|
+
if (survivor === undefined) {
|
|
278
|
+
this.#devices.set(hash, { deviceId, pubKey, fingerprint, createdAt: now, lastSeenAt: now, tokenHash: hash })
|
|
279
|
+
await this.#persist()
|
|
280
|
+
return { deviceId, deviceToken: token }
|
|
281
|
+
}
|
|
282
|
+
// The row keeps its id, its creation time and its registration; only the
|
|
283
|
+
// credential moves. The old map key went with the old token, so the token
|
|
284
|
+
// this phone was issued before the pairing no longer authenticates it.
|
|
285
|
+
const { tokenHash: _retired, ...kept } = survivor
|
|
286
|
+
this.#devices.set(hash, { ...kept, lastSeenAt: now, tokenHash: hash })
|
|
136
287
|
await this.#persist()
|
|
137
|
-
return { deviceId, deviceToken: token }
|
|
288
|
+
return { deviceId: kept.deviceId, deviceToken: token }
|
|
138
289
|
}
|
|
139
290
|
|
|
140
291
|
/** Record that a device just reconnected; the file is refreshed at most once per call. */
|
|
@@ -149,6 +300,161 @@ export class DeviceRoster {
|
|
|
149
300
|
if (touched !== undefined) await this.#persist()
|
|
150
301
|
}
|
|
151
302
|
|
|
303
|
+
/**
|
|
304
|
+
* Record this device's APNs registration, replacing any it had.
|
|
305
|
+
*
|
|
306
|
+
* Registering twice is what a token rotation looks like from here, so this is
|
|
307
|
+
* a plain overwrite rather than a merge: the newest complete registration is
|
|
308
|
+
* the only one worth sending to, and a half-merged pair of rows would send to
|
|
309
|
+
* a token the phone already retired.
|
|
310
|
+
* @param deviceId - the device that just registered.
|
|
311
|
+
* @param registration - what it registered.
|
|
312
|
+
* @returns false when no such device is paired.
|
|
313
|
+
*/
|
|
314
|
+
async registerPush(deviceId: string, registration: PushRegistration): Promise<boolean> {
|
|
315
|
+
for (const [hash, row] of this.#devices) {
|
|
316
|
+
if (row.deviceId !== deviceId) continue
|
|
317
|
+
this.#devices.set(hash, { ...row, push: registration })
|
|
318
|
+
await this.#persist()
|
|
319
|
+
return true
|
|
320
|
+
}
|
|
321
|
+
return false
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Forget one Live Activity token the phone no longer holds.
|
|
326
|
+
*
|
|
327
|
+
* APNs answers 410 (or 400 `BadDeviceToken`) for the token of a card the app
|
|
328
|
+
* already ended, and such a token never works again: the next card arrives as a
|
|
329
|
+
* new activity with a new token through `push.register`, which is a complete
|
|
330
|
+
* replacement of this set. Dropping it here is therefore the whole recovery —
|
|
331
|
+
* retrying would only repeat the same refusal (§29 token lifecycle).
|
|
332
|
+
* @param activityId - the card the token belonged to.
|
|
333
|
+
* @param token - the token APNs refused.
|
|
334
|
+
* @returns whether any row changed.
|
|
335
|
+
*/
|
|
336
|
+
async removeLiveActivityToken(activityId: string, token: string): Promise<boolean> {
|
|
337
|
+
for (const [hash, row] of this.#devices) {
|
|
338
|
+
const registration = row.push
|
|
339
|
+
if (registration === undefined) continue
|
|
340
|
+
const held = registration.liveActivityTokens
|
|
341
|
+
if (held === undefined) continue
|
|
342
|
+
const kept = held.filter(entry => !(entry.activityId === activityId && entry.token === token))
|
|
343
|
+
if (kept.length === held.length) continue
|
|
344
|
+
this.#devices.set(hash, { ...row, push: { ...registration, liveActivityTokens: kept } })
|
|
345
|
+
await this.#persist()
|
|
346
|
+
return true
|
|
347
|
+
}
|
|
348
|
+
return false
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Forget one device's notification registration for a token APNs will never
|
|
353
|
+
* accept again.
|
|
354
|
+
*
|
|
355
|
+
* A dead alert token is the whole app install's refusal: the phone was restored,
|
|
356
|
+
* reinstalled, or the token belongs to another environment or bundle, so the
|
|
357
|
+
* Live Activity tokens that same install registered are dead by the same fact
|
|
358
|
+
* and go with it. The next `push.register` — which a live app performs on launch
|
|
359
|
+
* — writes a complete new block. The device itself stays paired: revoking a
|
|
360
|
+
* phone is a roster edit, never something a push refusal may do.
|
|
361
|
+
* @param deviceToken - the token APNs refused.
|
|
362
|
+
* @returns whether any row changed.
|
|
363
|
+
*/
|
|
364
|
+
async removePush(deviceToken: string): Promise<boolean> {
|
|
365
|
+
for (const [hash, row] of this.#devices) {
|
|
366
|
+
const registration = row.push
|
|
367
|
+
if (registration === undefined || registration.deviceToken !== deviceToken) continue
|
|
368
|
+
const { tokenHash, deviceId, pubKey, fingerprint, createdAt, lastSeenAt } = row
|
|
369
|
+
this.#devices.set(hash, { tokenHash, deviceId, pubKey, fingerprint, createdAt, lastSeenAt })
|
|
370
|
+
await this.#persist()
|
|
371
|
+
return true
|
|
372
|
+
}
|
|
373
|
+
return false
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** @returns every paired device that registered for notifications. */
|
|
377
|
+
pushTargets(): readonly PushTargetView[] {
|
|
378
|
+
const targets: PushTargetView[] = []
|
|
379
|
+
for (const row of this.#devices.values()) {
|
|
380
|
+
if (row.push === undefined) continue
|
|
381
|
+
targets.push({ deviceId: row.deviceId, push: row.push })
|
|
382
|
+
}
|
|
383
|
+
return targets
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Collapse what earlier enrollments left behind, in memory.
|
|
388
|
+
*
|
|
389
|
+
* Two rules, both saying one phone is one row. Rows are collapsed to one per
|
|
390
|
+
* fingerprint first, because a table written before `enroll` reused a row
|
|
391
|
+
* carries a row per pairing. Then an APNs token is kept on the row that
|
|
392
|
+
* registered it last: a token belongs to the app install, so every other row
|
|
393
|
+
* holding it is the same phone and would be a second delivery of the same
|
|
394
|
+
* event. A row that loses its registration keeps its identity and simply asks
|
|
395
|
+
* for no notification, exactly as {@link removePush} leaves it.
|
|
396
|
+
* @returns how many rows and registrations this pass retired.
|
|
397
|
+
*/
|
|
398
|
+
#compact(): { readonly rows: number; readonly registrations: number } {
|
|
399
|
+
const phones = new Map<string, StoredDevice[]>()
|
|
400
|
+
for (const row of this.#devices.values()) {
|
|
401
|
+
const held = phones.get(row.fingerprint)
|
|
402
|
+
if (held === undefined) phones.set(row.fingerprint, [row])
|
|
403
|
+
else held.push(row)
|
|
404
|
+
}
|
|
405
|
+
let rows = 0
|
|
406
|
+
for (const held of phones.values()) {
|
|
407
|
+
if (held.length < 2) continue
|
|
408
|
+
const survivor = survivorOf(held)
|
|
409
|
+
if (survivor === undefined) continue
|
|
410
|
+
for (const row of held) {
|
|
411
|
+
if (row === survivor) continue
|
|
412
|
+
this.#devices.delete(row.tokenHash)
|
|
413
|
+
rows += 1
|
|
414
|
+
}
|
|
415
|
+
if (survivor.push !== undefined) continue
|
|
416
|
+
// A survivor that never registered takes the newest registration one of
|
|
417
|
+
// its dropped rows carried: same phone, same install, same tokens.
|
|
418
|
+
let newest: PushRegistration | undefined
|
|
419
|
+
for (const row of held) {
|
|
420
|
+
if (row === survivor || row.push === undefined) continue
|
|
421
|
+
if (newest === undefined || row.push.registeredAt > newest.registeredAt) newest = row.push
|
|
422
|
+
}
|
|
423
|
+
if (newest !== undefined) this.#devices.set(survivor.tokenHash, { ...survivor, push: newest })
|
|
424
|
+
}
|
|
425
|
+
const holders = new Map<string, StoredDevice[]>()
|
|
426
|
+
for (const row of this.#devices.values()) {
|
|
427
|
+
const token = row.push?.deviceToken
|
|
428
|
+
if (token === undefined) continue
|
|
429
|
+
const held = holders.get(token)
|
|
430
|
+
if (held === undefined) holders.set(token, [row])
|
|
431
|
+
else held.push(row)
|
|
432
|
+
}
|
|
433
|
+
let registrations = 0
|
|
434
|
+
for (const held of holders.values()) {
|
|
435
|
+
if (held.length < 2) continue
|
|
436
|
+
const keeper = held.reduce((best, row) => (row.push?.registeredAt ?? 0) > (best.push?.registeredAt ?? 0) ? row : best)
|
|
437
|
+
for (const row of held) {
|
|
438
|
+
if (row === keeper) continue
|
|
439
|
+
if (this.#retirePush(row.tokenHash)) registrations += 1
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return { rows, registrations }
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Drop one row's registration and keep its identity.
|
|
447
|
+
* @param hash - the map key of the row holding it.
|
|
448
|
+
* @returns whether the row carried a registration to drop.
|
|
449
|
+
*/
|
|
450
|
+
#retirePush(hash: string): boolean {
|
|
451
|
+
const row = this.#devices.get(hash)
|
|
452
|
+
if (row === undefined || row.push === undefined) return false
|
|
453
|
+
const { push: _dropped, ...rest } = row
|
|
454
|
+
this.#devices.set(hash, rest)
|
|
455
|
+
return true
|
|
456
|
+
}
|
|
457
|
+
|
|
152
458
|
async #read(): Promise<void> {
|
|
153
459
|
let text: string
|
|
154
460
|
try {
|
|
@@ -170,8 +476,26 @@ export class DeviceRoster {
|
|
|
170
476
|
await this.#quarantine('has no valid device table')
|
|
171
477
|
return
|
|
172
478
|
}
|
|
173
|
-
|
|
479
|
+
const declared = (parsed as { version?: unknown }).version
|
|
480
|
+
if (typeof declared === 'number' && declared !== ROSTER_VERSION) {
|
|
481
|
+
// Not an error and not a quarantine: the row table above already validated,
|
|
482
|
+
// so an older file is read as the roster it is and written back as the
|
|
483
|
+
// current schema on the next write.
|
|
484
|
+
this.#log(`device roster ${this.#file} is schema ${declared}; reading it as ${ROSTER_VERSION}`)
|
|
485
|
+
}
|
|
486
|
+
for (const row of rows) {
|
|
487
|
+
// The raw block is re-read per row instead of trusted from the guard, so a
|
|
488
|
+
// row that carries an unreadable token keeps its identity and loses only
|
|
489
|
+
// its notification.
|
|
490
|
+
const { push: raw, ...rest } = row as StoredDevice & { readonly push?: unknown }
|
|
491
|
+
const push = pushOf(raw, this.#log)
|
|
492
|
+
this.#devices.set(row.tokenHash, push === undefined ? rest : { ...rest, push })
|
|
493
|
+
}
|
|
174
494
|
this.#log(`device roster ${this.#file}: ${rows.length} paired device(s)`)
|
|
495
|
+
const compacted = this.#compact()
|
|
496
|
+
if (compacted.rows === 0 && compacted.registrations === 0) return
|
|
497
|
+
this.#log(`device roster ${this.#file}: compacted ${compacted.rows} duplicate row(s), retired ${compacted.registrations} stale registration(s)`)
|
|
498
|
+
await this.#persist()
|
|
175
499
|
}
|
|
176
500
|
|
|
177
501
|
/** Move a corrupt roster aside so the failure survives and the next write cannot erase it. */
|
|
@@ -188,8 +512,16 @@ export class DeviceRoster {
|
|
|
188
512
|
/**
|
|
189
513
|
* Write the roster atomically: same directory, 0600 from creation, flushed
|
|
190
514
|
* before the rename so a crash cannot leave a truncated file in place.
|
|
515
|
+
* Writes are serialized per roster so concurrent token retirement cannot race
|
|
516
|
+
* the replacement (Windows rejects overlapping renames).
|
|
191
517
|
*/
|
|
192
518
|
async #persist(): Promise<void> {
|
|
519
|
+
const pending = this.#writeQueue.then(() => this.#write())
|
|
520
|
+
this.#writeQueue = pending.catch(() => undefined)
|
|
521
|
+
return pending
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
async #write(): Promise<void> {
|
|
193
525
|
const directory = dirname(this.#file)
|
|
194
526
|
await mkdir(directory, { recursive: true, mode: 0o700 })
|
|
195
527
|
const payload = JSON.stringify({ version: ROSTER_VERSION, devices: [...this.#devices.values()] }, null, 2)
|
package/src/core/extension.ts
CHANGED
|
@@ -157,6 +157,26 @@ export class RemoteBridgeRegistry {
|
|
|
157
157
|
return this.#methods.has(method)
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
+
/**
|
|
161
|
+
* The capabilities the registered tables require a client to declare.
|
|
162
|
+
*
|
|
163
|
+
* A table registered without a capability contributes none: nothing gates its
|
|
164
|
+
* methods, so there is nothing for a client to announce. A name stays listed
|
|
165
|
+
* while any registered method still requires it, so withdrawing one table
|
|
166
|
+
* withdraws its capability only when it was the last one holding it. Sorted,
|
|
167
|
+
* because this answer travels on the wire: one set must read the same way
|
|
168
|
+
* whatever order the plugins happened to mount in.
|
|
169
|
+
* @returns the distinct non-blank capabilities, ascending; empty when no
|
|
170
|
+
* extension is registered.
|
|
171
|
+
*/
|
|
172
|
+
capabilities(): readonly string[] {
|
|
173
|
+
const names = new Set<string>()
|
|
174
|
+
for (const entry of this.#methods.values()) {
|
|
175
|
+
if (entry.capability !== undefined && entry.capability.trim() !== '') names.add(entry.capability)
|
|
176
|
+
}
|
|
177
|
+
return [...names].sort()
|
|
178
|
+
}
|
|
179
|
+
|
|
160
180
|
/**
|
|
161
181
|
* @param method - the method name a client asked for.
|
|
162
182
|
* @param declares - whether the calling connection declared a capability.
|