dsh-builtin-browser 1.0.1 → 1.0.4
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/CHANGELOG.md +55 -0
- package/README.md +1 -1
- package/assets/browser-panel-locales.mjs +37 -5
- package/assets/browser-policy.d.mts +29 -0
- package/assets/browser-policy.mjs +40 -0
- package/desktop-bridge/desktop-discovery.d.mts +1 -1
- package/desktop-bridge/desktop-discovery.mjs +59 -9
- package/desktop-bridge/desktop-identity.json +12 -0
- package/desktop-bridge/install.mjs +14 -7
- package/desktop-bridge/plugin-browser-bridge.js +19 -7
- package/docs/acceptance-2026-11-27.md +67 -0
- package/docs/architecture.md +1 -1
- package/docs/development.md +33 -4
- package/docs/dsh-0.2.3-host-patch-task.md +34 -0
- package/docs/hardcoding-audit.md +28 -8
- package/docs/known-issues.md +26 -0
- package/docs/limitations.md +6 -2
- package/docs/tool-reference.md +1 -1
- package/docs/v1.1.0-bu-data-batching-task.md +66 -0
- package/docs/v1.1.0-compat-hardcoding-task.md +85 -0
- package/dsh-host-patch/README.md +4 -0
- package/dsh-host-patch/apply.mjs +61 -13
- package/dsh-host-patch/rc3-revision9.json.gz +0 -0
- package/dsh-host-patch/rc5-revision9.json.gz +0 -0
- package/dsh-host-patch/tauri/desktop-main.cjs +62 -0
- package/dsh-host-patch/tauri/sidebar-session.cjs +104 -0
- package/dsh-host-patch/tauri-revision1.json +39 -0
- package/dsh-host-patch/tauri.json +43 -0
- package/dsh-host-patch/v023-alpha2.json +153 -0
- package/lib/browser/anti-bot.js +215 -0
- package/lib/browser/deep-dom.js +39 -0
- package/lib/browser/runtime.js +15 -0
- package/lib/browser/takeover-target.js +13 -6
- package/lib/browser/types.js +30 -1
- package/lib/browser-electron/authenticated-route.js +16 -2
- package/lib/browser-electron/automatic-maintenance.js +16 -10
- package/lib/browser-electron/bridge-connection.js +3 -8
- package/lib/browser-electron/bridge-install.js +2 -1
- package/lib/browser-electron/bridge-status.js +55 -26
- package/lib/browser-electron/desktop-bridge-host.js +7 -6
- package/lib/browser-electron/desktop-identity.js +28 -5
- package/lib/browser-electron/desktop-shell.js +64 -0
- package/lib/browser-electron/electron-provision.js +105 -18
- package/lib/browser-electron/entry.js +129 -44
- package/lib/browser-electron/extension-browser.js +4 -4
- package/lib/browser-electron/foreground-watch.js +134 -0
- package/lib/browser-electron/guest-cookie-jar.js +13 -13
- package/lib/browser-electron/guest-route.js +181 -32
- package/lib/browser-electron/guest-runtime.js +61 -10
- package/lib/browser-electron/guest-storage.js +2 -2
- package/lib/browser-electron/history-store.js +1 -5
- package/lib/browser-electron/host-main.js +8 -4
- package/lib/browser-electron/host-patch-maintenance.js +14 -8
- package/lib/browser-electron/maintenance-route.js +2 -1
- package/lib/browser-electron/missing-system-browser.js +5 -9
- package/lib/browser-electron/preferred-browser.js +7 -3
- package/lib/browser-electron/preview-route.js +9 -13
- package/lib/browser-electron/process-ownership.js +1 -1
- package/lib/browser-electron/profile-command.js +2 -1
- package/lib/browser-electron/profile-extension.js +5 -0
- package/lib/browser-electron/profile-lease.js +152 -47
- package/lib/browser-electron/provider-core.js +527 -0
- package/lib/browser-electron/provider-interaction.js +963 -0
- package/lib/browser-electron/provider-media.js +518 -0
- package/lib/browser-electron/provider-navigation.js +301 -0
- package/lib/browser-electron/provider-observation.js +623 -0
- package/lib/browser-electron/provider-tabs.js +372 -0
- package/lib/browser-electron/provider.js +150 -3301
- package/lib/browser-electron/reference-route.js +9 -18
- package/lib/browser-electron/remote-host.js +79 -34
- package/lib/browser-electron/runtime-status.js +56 -6
- package/lib/browser-electron/settings-store.js +3 -6
- package/lib/browser-electron/sidebar-only-host.js +1 -1
- package/lib/browser-electron/system-browser.js +539 -172
- package/lib/browser-electron/tauri-host.js +208 -0
- package/lib/browser-electron/virtual-cursor.js +23 -13
- package/lib/browser-reference/service.js +22 -4
- package/lib/browser-reference/web-sources.js +1 -1
- package/lib/client.js +43409 -0
- package/lib/compat/host-features.js +75 -0
- package/lib/reference-source.js +1 -1
- package/lib/reference-text.js +23 -1
- package/lib/takeover-protocol.js +1 -1
- package/lib/tool-browser/block-recovery.js +69 -0
- package/lib/tool-browser/code-tool.js +170 -19
- package/lib/tool-browser/code-worker.js +115 -20
- package/lib/tool-browser/errors.js +34 -0
- package/lib/tool-browser/index.js +66 -3593
- package/lib/tool-browser/page-probe.js +227 -28
- package/lib/tool-browser/panel-page.js +2 -1
- package/lib/tool-browser/probe-client.js +157 -0
- package/lib/tool-browser/screenshot-annotations.js +108 -0
- package/lib/tool-browser/state.js +337 -0
- package/lib/tool-browser/takeover.js +1 -1
- package/lib/tool-browser/tools-debug.js +862 -0
- package/lib/tool-browser/tools-interaction.js +993 -0
- package/lib/tool-browser/tools-navigation.js +807 -0
- package/lib/tool-browser/tools-observation.js +461 -0
- package/lib/tool-browser/tools-reference.js +293 -0
- package/lib/web-guest.js +1 -0
- package/package.json +16 -36
- package/scripts/build-client.mjs +305 -0
- package/scripts/build.mjs +15 -12
- package/scripts/fix-exports.mjs +6 -16
- package/scripts/install-electron.mjs +55 -5
- package/scripts/pack.mjs +99 -0
- package/scripts/publish-manifest.mjs +56 -0
- package/scripts/verify-exports.mjs +26 -15
- package/skills/dsh-builtin-browser/SKILL.md +27 -2
- package/skills/dsh-builtin-browser/references/batch.md +15 -0
- package/src/browser/anti-bot.ts +251 -0
- package/src/browser/deep-dom.ts +40 -0
- package/src/browser/runtime.ts +18 -0
- package/src/browser/takeover-target.ts +16 -7
- package/src/browser/types.ts +56 -3
- package/src/browser-electron/authenticated-route.ts +29 -4
- package/src/browser-electron/automatic-maintenance.ts +23 -13
- package/src/browser-electron/bridge-connection.ts +11 -10
- package/src/browser-electron/bridge-install.ts +2 -1
- package/src/browser-electron/bridge-status.ts +61 -22
- package/src/browser-electron/desktop-bridge-host.ts +9 -7
- package/src/browser-electron/desktop-identity.ts +38 -5
- package/src/browser-electron/desktop-shell.ts +100 -0
- package/src/browser-electron/electron-provision.ts +104 -18
- package/src/browser-electron/entry.ts +1378 -1299
- package/src/browser-electron/extension-browser.ts +11 -4
- package/src/browser-electron/foreground-watch.ts +142 -0
- package/src/browser-electron/guest-cookie-jar.ts +3 -2
- package/src/browser-electron/guest-route.ts +180 -32
- package/src/browser-electron/guest-runtime.ts +61 -10
- package/src/browser-electron/guest-storage.ts +5 -2
- package/src/browser-electron/history-store.ts +3 -6
- package/src/browser-electron/host-main.ts +8 -4
- package/src/browser-electron/host-patch-maintenance.ts +18 -9
- package/src/browser-electron/maintenance-route.ts +2 -1
- package/src/browser-electron/missing-system-browser.ts +11 -10
- package/src/browser-electron/preferred-browser.ts +13 -3
- package/src/browser-electron/preview-route.ts +9 -12
- package/src/browser-electron/process-ownership.ts +9 -3
- package/src/browser-electron/profile-command.ts +2 -1
- package/src/browser-electron/profile-extension.ts +5 -0
- package/src/browser-electron/profile-lease.ts +131 -35
- package/src/browser-electron/provider-core.ts +1107 -0
- package/src/browser-electron/provider-interaction.ts +1034 -0
- package/src/browser-electron/provider-media.ts +589 -0
- package/src/browser-electron/provider-navigation.ts +323 -0
- package/src/browser-electron/provider-observation.ts +664 -0
- package/src/browser-electron/provider-tabs.ts +372 -0
- package/src/browser-electron/provider.ts +158 -4063
- package/src/browser-electron/reference-route.ts +598 -608
- package/src/browser-electron/remote-host.ts +118 -42
- package/src/browser-electron/runtime-status.ts +80 -13
- package/src/browser-electron/settings-store.ts +10 -6
- package/src/browser-electron/sidebar-only-host.ts +4 -2
- package/src/browser-electron/system-browser.ts +537 -184
- package/src/browser-electron/tauri-host.ts +194 -0
- package/src/browser-electron/virtual-cursor.ts +24 -14
- package/src/browser-reference/service.ts +38 -7
- package/src/browser-reference/web-sources.ts +4 -1
- package/src/client/apply-guard.ts +22 -0
- package/src/client/browser/BrowserController.ts +3 -3
- package/src/client/browser/IframeImpl.ts +182 -176
- package/src/client/browser/frame-history.ts +125 -0
- package/src/client/browser/frame-state.ts +27 -0
- package/src/client/browser/web-reference-source.ts +5 -1
- package/src/client/definition.tsx +36 -30
- package/src/client/electron/ElectronWebViewImpl.ts +20 -13
- package/src/client/electron/ElectronWebviewPresentation.ts +4 -2
- package/src/client/electron/NativeChildPresentation.ts +11 -3
- package/src/client/electron/NativeViewPage.ts +19 -5
- package/src/client/index.ts +333 -293
- package/src/client/locales.ts +9 -6
- package/src/client/reference/client/plugin.ts +21 -2
- package/src/client/reference/client/production/ReferenceCapsules.tsx +3 -3
- package/src/client/reference/client/production/assembly.ts +4 -28
- package/src/client/reference/client/production/availability.ts +0 -2
- package/src/client/reference/client/production/composer-attachments.ts +44 -0
- package/src/client/reference/client/production/draft-adapter.ts +4 -0
- package/src/client/reference/client/production/http-transport.ts +0 -5
- package/src/client/reference/client/production/production-submission.ts +50 -290
- package/src/client/reference/client/production/protocol.ts +7 -4
- package/src/client/reference/client/production/runtime.ts +41 -86
- package/src/client/reference/client/production/source-marker.ts +49 -13
- package/src/client/reference/client/production/user-text-rule.ts +10 -1
- package/src/client/reference/client/reveal.ts +3 -1
- package/src/client/reference/client/session-schedule.ts +36 -0
- package/src/client/settings/index.ts +8 -0
- package/src/client/settings/mount-guard.ts +31 -0
- package/src/client/settings/panel.ts +163 -52
- package/src/client/startpage/controller.ts +3 -6
- package/src/client/view/BrowserBody.tsx +2 -1
- package/src/client/view/IframePresentation.ts +37 -16
- package/src/client/view/StartPage.tsx +2 -1
- package/src/compat/host-features.ts +92 -0
- package/src/reference-source.ts +1 -1
- package/src/reference-text.ts +23 -1
- package/src/takeover-protocol.ts +11 -2
- package/src/tool-browser/block-recovery.ts +85 -0
- package/src/tool-browser/code-tool.ts +173 -19
- package/src/tool-browser/code-worker.ts +101 -15
- package/src/tool-browser/errors.ts +40 -0
- package/src/tool-browser/index.ts +66 -3534
- package/src/tool-browser/page-probe.ts +229 -28
- package/src/tool-browser/panel-page.ts +3 -1
- package/src/tool-browser/probe-client.ts +183 -0
- package/src/tool-browser/screenshot-annotations.ts +131 -0
- package/src/tool-browser/state.ts +375 -0
- package/src/tool-browser/takeover.ts +3 -2
- package/src/tool-browser/tools-debug.ts +806 -0
- package/src/tool-browser/tools-interaction.ts +956 -0
- package/src/tool-browser/tools-navigation.ts +786 -0
- package/src/tool-browser/tools-observation.ts +474 -0
- package/src/tool-browser/tools-reference.ts +294 -0
- package/src/web-guest.ts +35 -34
- package/dist/client.cjs +0 -43167
- package/dist/client.d.cts +0 -516
- package/dist/client.d.mts +0 -516
- package/dist/index.cjs +0 -92
- package/dist/index.d.mts +0 -2960
- package/dist/index.mjs +0 -24
- package/lib/browser-reference/submission-receipts.js +0 -139
- package/src/browser-reference/submission-receipts.ts +0 -122
|
@@ -1,608 +1,598 @@
|
|
|
1
|
-
import { REFERENCE_LIST_ROUTE, REFERENCE_GET_ROUTE, CLIENT_VERSION_ROUTE, REFERENCE_CAPTURE_ROUTE, REFERENCE_REVEAL_ROUTE, REFERENCE_INSERT_ROUTE, REFERENCE_EXPORT_ROUTE, REFERENCE_IMPORT_ROUTE, REFERENCE_DELIVERIES_ROUTE, REFERENCE_DELIVERY_ACK_ROUTE, REFERENCE_DELIVERY_REMOVE_ROUTE, REFERENCE_ASSET_ROUTE, REFERENCE_STATUS_ROUTE, URL_PARSE_BASE } from '../../assets/browser-policy.mjs'
|
|
2
|
-
/**
|
|
3
|
-
* HTTP surface for browser references: list, read, capture, reveal, delivery
|
|
4
|
-
* into the conversation draft, and the actual image bytes behind a reference.
|
|
5
|
-
*
|
|
6
|
-
* Reads are authenticated by the host cookie alone, like every other plugin
|
|
7
|
-
* route. Writes add the same two browser boundaries the bridge action uses —
|
|
8
|
-
* same origin and a direct loopback peer — because a capture reads the page the
|
|
9
|
-
* human is looking at and a delivery writes into their draft.
|
|
10
|
-
*
|
|
11
|
-
* The page is never named by the caller: capture and reveal act on the browser
|
|
12
|
-
* session recorded in the reference, so a request cannot point the plugin at
|
|
13
|
-
* another conversation's page by inventing an id.
|
|
14
|
-
* @module dsh-browser/browser-electron/reference-route
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
18
|
-
import { referenceOwnerKey } from '../reference-owner.ts'
|
|
19
|
-
import { WEB_REVEAL_STATUSES, type WebRevealStatus } from '../reference-source.ts'
|
|
20
|
-
import { WebSourceLeaseExpired } from '../browser-reference/web-sources.ts'
|
|
21
|
-
import { readFileSync } from 'node:fs'
|
|
22
|
-
import { dirname, join } from 'node:path'
|
|
23
|
-
import type { BrowserHttpHandler } from './authenticated-route.ts'
|
|
24
|
-
import { isDirectLoopback } from './authenticated-route.ts'
|
|
25
|
-
import { readBinaryBody, readJsonBody } from './http-body.ts'
|
|
26
|
-
import { replyJson } from './json-reply.ts'
|
|
27
|
-
import { sameOrigin } from './settings-route.ts'
|
|
28
|
-
import { importReferenceBundle } from '../browser-reference/import.ts'
|
|
29
|
-
import { validateFrameSelector } from '../browser-reference/entry.ts'
|
|
30
|
-
import {
|
|
31
|
-
import type
|
|
32
|
-
import type {
|
|
33
|
-
import type {
|
|
34
|
-
import type { ReferenceExportFormat } from '../browser-reference/export.ts'
|
|
35
|
-
import type { BrowserReferenceCarrier, BrowserReferenceEntry, BrowserReferenceOrigin, BrowserReferenceTarget } from '../browser-reference/types.ts'
|
|
36
|
-
|
|
37
|
-
/** Paths this module serves. */
|
|
38
|
-
export { REFERENCE_LIST_ROUTE }
|
|
39
|
-
export { REFERENCE_GET_ROUTE }
|
|
40
|
-
/**
|
|
41
|
-
* The id the host keys its stores on.
|
|
42
|
-
*
|
|
43
|
-
* The shell stamps its own DOM with \`session-<uuid>\`, and the client polls with that value, while
|
|
44
|
-
* captures are stored under the bare id. Without this the dock asked for "session-…" and always got an
|
|
45
|
-
* empty delivery list, so a capture from the Web sidebar never became a pill.
|
|
46
|
-
* @param value - a session id in either form.
|
|
47
|
-
* @returns the same id without a leading \`session-\`.
|
|
48
|
-
*/
|
|
49
|
-
export function bareSession(value: string): string {
|
|
50
|
-
return referenceOwnerKey(value)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** 客户端产物的版本(mtime+size),用于让浏览器自动换到新 bundle。 */
|
|
54
|
-
export { CLIENT_VERSION_ROUTE }
|
|
55
|
-
|
|
56
|
-
export { REFERENCE_CAPTURE_ROUTE }
|
|
57
|
-
export { REFERENCE_REVEAL_ROUTE }
|
|
58
|
-
export { REFERENCE_INSERT_ROUTE }
|
|
59
|
-
export { REFERENCE_EXPORT_ROUTE }
|
|
60
|
-
export { REFERENCE_IMPORT_ROUTE }
|
|
61
|
-
/** The client polls this to pick up captures that belong to its conversation. */
|
|
62
|
-
export { REFERENCE_DELIVERIES_ROUTE }
|
|
63
|
-
/** Only the client may ACK, after the draft AND every image asset are durable. */
|
|
64
|
-
export { REFERENCE_DELIVERY_ACK_ROUTE }
|
|
65
|
-
/** Remove a card: a tombstone that late retries cannot resurrect. */
|
|
66
|
-
export { REFERENCE_DELIVERY_REMOVE_ROUTE }
|
|
67
|
-
/** Real bytes of a captured image, for the card thumbnail and the model block. */
|
|
68
|
-
export { REFERENCE_ASSET_ROUTE }
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Diagnostic answer for the sidebar entry: which conversation this host serves, and
|
|
72
|
-
* what its last scan of the conversation's sidebar pages found.
|
|
73
|
-
*/
|
|
74
|
-
export { REFERENCE_STATUS_ROUTE }
|
|
75
|
-
|
|
76
|
-
const EXPORT_FORMATS: readonly ReferenceExportFormat[] = ['jsonl', 'markdown', 'zip', 'markdown-zip']
|
|
77
|
-
|
|
78
|
-
const CARRIERS: readonly BrowserReferenceCarrier[] = ['sidebar', 'bundled', 'chrome', 'edge']
|
|
79
|
-
const ENTRIES: readonly BrowserReferenceEntry[] = ['native-menu', 'extension-action', 'page-overlay', 'agent-tool']
|
|
80
|
-
const ORIGINS: readonly BrowserReferenceOrigin[] = ['user-selection', 'agent-excerpt']
|
|
81
|
-
|
|
82
|
-
function stringOf(value: unknown, max: number): string | undefined {
|
|
83
|
-
return typeof value === 'string' && value !== '' ? value.slice(0, max) : undefined
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function oneOf<T extends string>(value: unknown, allowed: readonly T[]): T | undefined {
|
|
87
|
-
return typeof value === 'string' && (allowed as readonly string[]).includes(value) ? value as T : undefined
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** Options for the route set. */
|
|
91
|
-
export interface ReferenceRouteOptions {
|
|
92
|
-
readonly service: BrowserReferenceService
|
|
93
|
-
/** Carrier recorded for captures that do not name one (the panel's capture path). */
|
|
94
|
-
readonly carrier?: () => BrowserReferenceCarrier
|
|
95
|
-
/** Durable hand-off queue between capture and the conversation draft. */
|
|
96
|
-
readonly queue?: DeliveryQueue
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
readonly
|
|
103
|
-
readonly
|
|
104
|
-
readonly
|
|
105
|
-
readonly
|
|
106
|
-
readonly
|
|
107
|
-
readonly
|
|
108
|
-
readonly
|
|
109
|
-
readonly
|
|
110
|
-
readonly
|
|
111
|
-
readonly
|
|
112
|
-
readonly
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (request
|
|
118
|
-
if (!
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
replyJson(response, 400, { ok: false, error:
|
|
196
|
-
return
|
|
197
|
-
}
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
if (
|
|
201
|
-
replyJson(response, 400, { ok: false, error: '
|
|
202
|
-
return
|
|
203
|
-
}
|
|
204
|
-
const
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const
|
|
236
|
-
const
|
|
237
|
-
if (
|
|
238
|
-
replyJson(response, 400, { ok: false, error: 'target.
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
// The
|
|
254
|
-
//
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
...
|
|
267
|
-
...
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
...
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
if (
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
//
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
const
|
|
441
|
-
const
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
if (
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
//
|
|
545
|
-
//
|
|
546
|
-
//
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
if (
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
'x-content-type-options': 'nosniff',
|
|
600
|
-
})
|
|
601
|
-
response.end(bytes)
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
return {
|
|
605
|
-
source, list, get, capture, reveal, insert, export: exportBundle, importBundle,
|
|
606
|
-
deliveries, ack, removeDelivery, asset,
|
|
607
|
-
}
|
|
608
|
-
}
|
|
1
|
+
import { REFERENCE_LIST_ROUTE, REFERENCE_GET_ROUTE, CLIENT_VERSION_ROUTE, REFERENCE_CAPTURE_ROUTE, REFERENCE_REVEAL_ROUTE, REFERENCE_INSERT_ROUTE, REFERENCE_EXPORT_ROUTE, REFERENCE_IMPORT_ROUTE, REFERENCE_DELIVERIES_ROUTE, REFERENCE_DELIVERY_ACK_ROUTE, REFERENCE_DELIVERY_REMOVE_ROUTE, REFERENCE_ASSET_ROUTE, REFERENCE_STATUS_ROUTE, URL_PARSE_BASE } from '../../assets/browser-policy.mjs'
|
|
2
|
+
/**
|
|
3
|
+
* HTTP surface for browser references: list, read, capture, reveal, delivery
|
|
4
|
+
* into the conversation draft, and the actual image bytes behind a reference.
|
|
5
|
+
*
|
|
6
|
+
* Reads are authenticated by the host cookie alone, like every other plugin
|
|
7
|
+
* route. Writes add the same two browser boundaries the bridge action uses —
|
|
8
|
+
* same origin and a direct loopback peer — because a capture reads the page the
|
|
9
|
+
* human is looking at and a delivery writes into their draft.
|
|
10
|
+
*
|
|
11
|
+
* The page is never named by the caller: capture and reveal act on the browser
|
|
12
|
+
* session recorded in the reference, so a request cannot point the plugin at
|
|
13
|
+
* another conversation's page by inventing an id.
|
|
14
|
+
* @module dsh-browser/browser-electron/reference-route
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
18
|
+
import { referenceOwnerKey } from '../reference-owner.ts'
|
|
19
|
+
import { WEB_REVEAL_STATUSES, type WebRevealStatus } from '../reference-source.ts'
|
|
20
|
+
import { WebSourceLeaseExpired } from '../browser-reference/web-sources.ts'
|
|
21
|
+
import { readFileSync } from 'node:fs'
|
|
22
|
+
import { dirname, join } from 'node:path'
|
|
23
|
+
import type { BrowserHttpHandler } from './authenticated-route.ts'
|
|
24
|
+
import { isDirectLoopback } from './authenticated-route.ts'
|
|
25
|
+
import { readBinaryBody, readJsonBody } from './http-body.ts'
|
|
26
|
+
import { replyJson } from './json-reply.ts'
|
|
27
|
+
import { sameOrigin } from './settings-route.ts'
|
|
28
|
+
import { importReferenceBundle } from '../browser-reference/import.ts'
|
|
29
|
+
import { validateFrameSelector } from '../browser-reference/entry.ts'
|
|
30
|
+
import { MAX_FROZEN_CAPTURE_CHARS } from '../browser-reference/page-script.ts'
|
|
31
|
+
import { summarizeReference, type ReferenceSummary } from '../browser-reference/service.ts'
|
|
32
|
+
import type { BrowserReferenceService } from '../browser-reference/service.ts'
|
|
33
|
+
import type { DeliveryQueue } from '../browser-reference/delivery.ts'
|
|
34
|
+
import type { ReferenceExportFormat } from '../browser-reference/export.ts'
|
|
35
|
+
import type { BrowserReferenceCarrier, BrowserReferenceEntry, BrowserReferenceOrigin, BrowserReferenceTarget } from '../browser-reference/types.ts'
|
|
36
|
+
|
|
37
|
+
/** Paths this module serves. */
|
|
38
|
+
export { REFERENCE_LIST_ROUTE }
|
|
39
|
+
export { REFERENCE_GET_ROUTE }
|
|
40
|
+
/**
|
|
41
|
+
* The id the host keys its stores on.
|
|
42
|
+
*
|
|
43
|
+
* The shell stamps its own DOM with \`session-<uuid>\`, and the client polls with that value, while
|
|
44
|
+
* captures are stored under the bare id. Without this the dock asked for "session-…" and always got an
|
|
45
|
+
* empty delivery list, so a capture from the Web sidebar never became a pill.
|
|
46
|
+
* @param value - a session id in either form.
|
|
47
|
+
* @returns the same id without a leading \`session-\`.
|
|
48
|
+
*/
|
|
49
|
+
export function bareSession(value: string): string {
|
|
50
|
+
return referenceOwnerKey(value)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 客户端产物的版本(mtime+size),用于让浏览器自动换到新 bundle。 */
|
|
54
|
+
export { CLIENT_VERSION_ROUTE }
|
|
55
|
+
|
|
56
|
+
export { REFERENCE_CAPTURE_ROUTE }
|
|
57
|
+
export { REFERENCE_REVEAL_ROUTE }
|
|
58
|
+
export { REFERENCE_INSERT_ROUTE }
|
|
59
|
+
export { REFERENCE_EXPORT_ROUTE }
|
|
60
|
+
export { REFERENCE_IMPORT_ROUTE }
|
|
61
|
+
/** The client polls this to pick up captures that belong to its conversation. */
|
|
62
|
+
export { REFERENCE_DELIVERIES_ROUTE }
|
|
63
|
+
/** Only the client may ACK, after the draft AND every image asset are durable. */
|
|
64
|
+
export { REFERENCE_DELIVERY_ACK_ROUTE }
|
|
65
|
+
/** Remove a card: a tombstone that late retries cannot resurrect. */
|
|
66
|
+
export { REFERENCE_DELIVERY_REMOVE_ROUTE }
|
|
67
|
+
/** Real bytes of a captured image, for the card thumbnail and the model block. */
|
|
68
|
+
export { REFERENCE_ASSET_ROUTE }
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Diagnostic answer for the sidebar entry: which conversation this host serves, and
|
|
72
|
+
* what its last scan of the conversation's sidebar pages found.
|
|
73
|
+
*/
|
|
74
|
+
export { REFERENCE_STATUS_ROUTE }
|
|
75
|
+
|
|
76
|
+
const EXPORT_FORMATS: readonly ReferenceExportFormat[] = ['jsonl', 'markdown', 'zip', 'markdown-zip']
|
|
77
|
+
|
|
78
|
+
const CARRIERS: readonly BrowserReferenceCarrier[] = ['sidebar', 'bundled', 'chrome', 'edge']
|
|
79
|
+
const ENTRIES: readonly BrowserReferenceEntry[] = ['native-menu', 'extension-action', 'page-overlay', 'agent-tool']
|
|
80
|
+
const ORIGINS: readonly BrowserReferenceOrigin[] = ['user-selection', 'agent-excerpt']
|
|
81
|
+
|
|
82
|
+
function stringOf(value: unknown, max: number): string | undefined {
|
|
83
|
+
return typeof value === 'string' && value !== '' ? value.slice(0, max) : undefined
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function oneOf<T extends string>(value: unknown, allowed: readonly T[]): T | undefined {
|
|
87
|
+
return typeof value === 'string' && (allowed as readonly string[]).includes(value) ? value as T : undefined
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Options for the route set. */
|
|
91
|
+
export interface ReferenceRouteOptions {
|
|
92
|
+
readonly service: BrowserReferenceService
|
|
93
|
+
/** Carrier recorded for captures that do not name one (the panel's capture path). */
|
|
94
|
+
readonly carrier?: () => BrowserReferenceCarrier
|
|
95
|
+
/** Durable hand-off queue between capture and the conversation draft. */
|
|
96
|
+
readonly queue?: DeliveryQueue
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The handlers, one per registered exact path. */
|
|
100
|
+
export interface ReferenceRouteHandlers {
|
|
101
|
+
readonly source: BrowserHttpHandler
|
|
102
|
+
readonly list: BrowserHttpHandler
|
|
103
|
+
readonly get: BrowserHttpHandler
|
|
104
|
+
readonly capture: BrowserHttpHandler
|
|
105
|
+
readonly reveal: BrowserHttpHandler
|
|
106
|
+
readonly insert: BrowserHttpHandler
|
|
107
|
+
readonly export: BrowserHttpHandler
|
|
108
|
+
readonly importBundle: BrowserHttpHandler
|
|
109
|
+
readonly deliveries: BrowserHttpHandler
|
|
110
|
+
readonly ack: BrowserHttpHandler
|
|
111
|
+
readonly removeDelivery: BrowserHttpHandler
|
|
112
|
+
readonly asset: BrowserHttpHandler
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function requireWrite(request: IncomingMessage, response: ServerResponse): boolean {
|
|
116
|
+
if (request.method !== 'POST') { replyJson(response, 405, { ok: false, error: 'method-not-allowed' }); return false }
|
|
117
|
+
if (!sameOrigin(request, true)) { replyJson(response, 403, { ok: false, error: 'not-same-origin' }); return false }
|
|
118
|
+
if (!isDirectLoopback(request)) { replyJson(response, 403, { ok: false, error: 'local-request-required' }); return false }
|
|
119
|
+
return true
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function requireRead(request: IncomingMessage, response: ServerResponse): boolean {
|
|
123
|
+
if (request.method !== 'GET') { replyJson(response, 405, { ok: false, error: 'method-not-allowed' }); return false }
|
|
124
|
+
return true
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The page-side bar posts its whole frozen capture, images inlined, and that capture is bounded by
|
|
129
|
+
* `MAX_FROZEN_CAPTURE_CHARS`. JSON keeps non-ASCII raw, so a character can take three UTF-8 bytes;
|
|
130
|
+
* the other fields are short and bounded on their own.
|
|
131
|
+
*/
|
|
132
|
+
const MAX_CAPTURE_BODY_BYTES = MAX_FROZEN_CAPTURE_CHARS * 3 + 64 * 1024
|
|
133
|
+
|
|
134
|
+
/** Build every reference handler over one service. */
|
|
135
|
+
export function createReferenceRouteHandlers(options: ReferenceRouteOptions): ReferenceRouteHandlers {
|
|
136
|
+
const { service, queue } = options
|
|
137
|
+
const carrier = options.carrier ?? (() => 'bundled' as BrowserReferenceCarrier)
|
|
138
|
+
|
|
139
|
+
const source: BrowserHttpHandler = async (request, response) => {
|
|
140
|
+
if (!requireWrite(request, response)) return
|
|
141
|
+
if (service.webSources === undefined) { replyJson(response, 503, { ok: false, error: 'web source channel unavailable' }); return }
|
|
142
|
+
try {
|
|
143
|
+
const body = await readJsonBody<Record<string, unknown>>(request)
|
|
144
|
+
const lease = stringOf(body.lease, 100)
|
|
145
|
+
if (body.operation === 'register') {
|
|
146
|
+
const owner = stringOf(body.sessionId, 200)
|
|
147
|
+
const pageId = stringOf(body.pageId, 200)
|
|
148
|
+
const url = stringOf(body.url, 4000)
|
|
149
|
+
if (!owner || !pageId || !url) throw new Error('source owner, page and URL required')
|
|
150
|
+
replyJson(response, 200, { ok: true, lease: service.webSources.register(owner, pageId, url) })
|
|
151
|
+
} else if (body.operation === 'poll' && lease !== undefined && typeof body.url === 'string') {
|
|
152
|
+
const acks = body.acknowledgements ?? []
|
|
153
|
+
if (!Array.isArray(acks) || acks.length > 64 || !acks.every(ack => ack !== null && typeof ack === 'object' && typeof ack.id === 'string' && WEB_REVEAL_STATUSES.includes(ack.status))) throw new Error('invalid source acknowledgements')
|
|
154
|
+
const commands = service.webSources.poll(lease, body.url, acks as { id: string; status: WebRevealStatus }[])
|
|
155
|
+
replyJson(response, 200, { ok: true, commands })
|
|
156
|
+
} else if (body.operation === 'remove' && lease !== undefined) {
|
|
157
|
+
service.webSources.remove(lease)
|
|
158
|
+
replyJson(response, 200, { ok: true })
|
|
159
|
+
} else throw new Error('invalid source operation')
|
|
160
|
+
} catch (error) {
|
|
161
|
+
replyJson(response, error instanceof WebSourceLeaseExpired ? 410 : 400, { ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const list: BrowserHttpHandler = (request, response) => {
|
|
166
|
+
if (!requireRead(request, response)) return
|
|
167
|
+
const url = new URL(request.url ?? '/', URL_PARSE_BASE)
|
|
168
|
+
const limitText = url.searchParams.get('limit')
|
|
169
|
+
const limit = limitText === null ? 20 : Number(limitText)
|
|
170
|
+
const references = service.list({
|
|
171
|
+
...url.searchParams.get('sessionId') !== null ? { sessionId: bareSession(String(url.searchParams.get('sessionId'))) } : {},
|
|
172
|
+
...url.searchParams.get('pageUrl') !== null ? { pageUrl: String(url.searchParams.get('pageUrl')) } : {},
|
|
173
|
+
limit: Number.isFinite(limit) ? Math.min(Math.max(Math.trunc(limit), 1), 100) : 20,
|
|
174
|
+
})
|
|
175
|
+
// Oldest first in the store; a panel reads newest first.
|
|
176
|
+
replyJson(response, 200, { ok: true, references: references.slice().reverse().map(summarizeReference) })
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const get: BrowserHttpHandler = (request, response) => {
|
|
180
|
+
if (!requireRead(request, response)) return
|
|
181
|
+
const url = new URL(request.url ?? '/', URL_PARSE_BASE)
|
|
182
|
+
const id = url.searchParams.get('id')
|
|
183
|
+
if (id === null || id === '') { replyJson(response, 400, { ok: false, error: 'id is required' }); return }
|
|
184
|
+
const reference = service.get(id)
|
|
185
|
+
if (reference === undefined) { replyJson(response, 404, { ok: false, error: 'reference-not-found' }); return }
|
|
186
|
+
replyJson(response, 200, { ok: true, reference, summary: summarizeReference(reference) })
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const capture: BrowserHttpHandler = async (request, response) => {
|
|
190
|
+
if (!requireWrite(request, response)) return
|
|
191
|
+
let body: Record<string, unknown>
|
|
192
|
+
try {
|
|
193
|
+
body = await readJsonBody<Record<string, unknown>>(request, MAX_CAPTURE_BODY_BYTES)
|
|
194
|
+
} catch (error) {
|
|
195
|
+
replyJson(response, 400, { ok: false, error: `unreadable body: ${String(error)}` })
|
|
196
|
+
return
|
|
197
|
+
}
|
|
198
|
+
const sessionId = stringOf(body.sessionId, 200)
|
|
199
|
+
if (sessionId === undefined) { replyJson(response, 400, { ok: false, error: 'sessionId is required' }); return }
|
|
200
|
+
if (body.allowPartial !== undefined && typeof body.allowPartial !== 'boolean') {
|
|
201
|
+
replyJson(response, 400, { ok: false, error: 'allowPartial must be a boolean' })
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
const origin = body.origin === undefined ? 'user-selection' : oneOf(body.origin, ORIGINS)
|
|
205
|
+
const entry = body.entry === undefined ? 'page-overlay' : oneOf(body.entry, ENTRIES)
|
|
206
|
+
if (origin === undefined || entry === undefined) {
|
|
207
|
+
replyJson(response, 400, { ok: false, error: 'origin or entry is not a known value' })
|
|
208
|
+
return
|
|
209
|
+
}
|
|
210
|
+
const browserSessionId = stringOf(body.browserSessionId, 200)
|
|
211
|
+
const sourcePageId = stringOf(body.sourcePageId, 200)
|
|
212
|
+
const tableSelector = stringOf(body.tableSelector, 1000)
|
|
213
|
+
const excerptText = stringOf(body.excerptText, 20_000)
|
|
214
|
+
let frameSelector: string | undefined
|
|
215
|
+
// null means "no frame": the page-side bar sends the field explicitly, and validating null refused
|
|
216
|
+
// every quote from the Web sidebar ("frameSelector is not a valid selector", measured 400 in the
|
|
217
|
+
// guest's own log).
|
|
218
|
+
if (body.frameSelector !== undefined && body.frameSelector !== null) {
|
|
219
|
+
frameSelector = validateFrameSelector(body.frameSelector)
|
|
220
|
+
if (frameSelector === undefined) {
|
|
221
|
+
replyJson(response, 400, { ok: false, error: 'frameSelector is not a valid selector' })
|
|
222
|
+
return
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
// An explicit target names the image/table the human acted on. It is
|
|
226
|
+
// validated here (shape, bounds, kind) so the page never receives arbitrary
|
|
227
|
+
// values and a malformed target cannot silently degrade into "whatever the
|
|
228
|
+
// selection happens to be".
|
|
229
|
+
let target: BrowserReferenceTarget | undefined
|
|
230
|
+
if (body.target !== undefined) {
|
|
231
|
+
const raw = body.target
|
|
232
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
233
|
+
replyJson(response, 400, { ok: false, error: 'target must be an object' }); return
|
|
234
|
+
}
|
|
235
|
+
const candidate = raw as Record<string, unknown>
|
|
236
|
+
const kind = candidate.kind
|
|
237
|
+
if (kind !== 'text' && kind !== 'image' && kind !== 'table') {
|
|
238
|
+
replyJson(response, 400, { ok: false, error: 'target.kind must be text, image or table' }); return
|
|
239
|
+
}
|
|
240
|
+
const srcUrl = stringOf(candidate.srcUrl, 4000)
|
|
241
|
+
const x = typeof candidate.x === 'number' && Number.isFinite(candidate.x) ? candidate.x : undefined
|
|
242
|
+
const y = typeof candidate.y === 'number' && Number.isFinite(candidate.y) ? candidate.y : undefined
|
|
243
|
+
if ((candidate.x !== undefined && x === undefined) || (candidate.y !== undefined && y === undefined)) {
|
|
244
|
+
replyJson(response, 400, { ok: false, error: 'target.x/y must be finite numbers' }); return
|
|
245
|
+
}
|
|
246
|
+
target = {
|
|
247
|
+
kind,
|
|
248
|
+
...srcUrl !== undefined ? { srcUrl } : {},
|
|
249
|
+
...x !== undefined ? { x } : {},
|
|
250
|
+
...y !== undefined ? { y } : {},
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
// The comment is the human's own note; an over-long one is bounded by the
|
|
254
|
+
// service, but a non-string is a caller bug and is rejected.
|
|
255
|
+
if (body.comment !== undefined && typeof body.comment !== 'string') {
|
|
256
|
+
replyJson(response, 400, { ok: false, error: 'comment must be a string' }); return
|
|
257
|
+
}
|
|
258
|
+
const comment = typeof body.comment === 'string' ? body.comment : undefined
|
|
259
|
+
// The page-side bar hands over a COMPLETE frozen snapshot. Forwarding it is what lets a capture
|
|
260
|
+
// happen without a browser session at all: the Web sidebar's guest is an iframe the plugin does
|
|
261
|
+
// not own, so it has no browser:<uuid>, and dropping this field refused every quote with
|
|
262
|
+
// "session-unavailable" (measured 422 on every attempt).
|
|
263
|
+
const frozenCapture = body.capture === undefined || body.capture === null ? undefined : body.capture as never
|
|
264
|
+
const outcome = await service.capture({
|
|
265
|
+
sessionId,
|
|
266
|
+
...browserSessionId !== undefined ? { browserSessionId } : {},
|
|
267
|
+
...sourcePageId !== undefined ? { sourcePageId } : {},
|
|
268
|
+
origin,
|
|
269
|
+
entry,
|
|
270
|
+
carrier: entry === 'page-overlay' && frozenCapture !== undefined ? 'sidebar' : oneOf(body.carrier, CARRIERS) ?? carrier(),
|
|
271
|
+
...typeof body.allowPartial === 'boolean' ? { allowPartial: body.allowPartial } : {},
|
|
272
|
+
...tableSelector !== undefined ? { tableSelector } : {},
|
|
273
|
+
...excerptText !== undefined ? { excerptText } : {},
|
|
274
|
+
...frameSelector !== undefined ? { frameSelector } : {},
|
|
275
|
+
...target !== undefined ? { target } : {},
|
|
276
|
+
...comment !== undefined ? { comment } : {},
|
|
277
|
+
...frozenCapture !== undefined ? { frozenCapture } : {},
|
|
278
|
+
...typeof request.headers.host === 'string' ? { dshHost: request.headers.host } : {},
|
|
279
|
+
})
|
|
280
|
+
if (outcome.reference === undefined) {
|
|
281
|
+
replyJson(response, 422, { ok: false, failures: outcome.failures })
|
|
282
|
+
return
|
|
283
|
+
}
|
|
284
|
+
// The HTTP capture path (agent tool / panel) feeds the SAME hand-off as a
|
|
285
|
+
// right-click or floating entry: one durable queue, one owning session.
|
|
286
|
+
if (queue !== undefined) {
|
|
287
|
+
try {
|
|
288
|
+
queue.prepare(outcome.reference, {
|
|
289
|
+
failures: outcome.failures.map(failure => ({
|
|
290
|
+
kind: failure.kind === 'text' || failure.kind === 'image' || failure.kind === 'table'
|
|
291
|
+
? failure.kind : 'reference',
|
|
292
|
+
reason: failure.reason,
|
|
293
|
+
})),
|
|
294
|
+
})
|
|
295
|
+
} catch (error) {
|
|
296
|
+
// The reference is already durable, but WITHOUT a delivery the client
|
|
297
|
+
// can never poll it: reporting 200 here produced the worst possible
|
|
298
|
+
// state — a successful capture with a card that silently never appears.
|
|
299
|
+
process.stderr.write(`[dsh-browser] reference delivery prepare failed: ${String(error)}\n`)
|
|
300
|
+
replyJson(response, 409, {
|
|
301
|
+
ok: false,
|
|
302
|
+
error: 'delivery-enqueue-failed',
|
|
303
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
304
|
+
reference: summarizeReference(outcome.reference),
|
|
305
|
+
})
|
|
306
|
+
return
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
replyJson(response, 200, { ok: true, reference: summarizeReference(outcome.reference), failures: outcome.failures })
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const reveal: BrowserHttpHandler = async (request, response) => {
|
|
313
|
+
if (!requireWrite(request, response)) return
|
|
314
|
+
let body: Record<string, unknown>
|
|
315
|
+
try {
|
|
316
|
+
body = await readJsonBody<Record<string, unknown>>(request)
|
|
317
|
+
} catch (error) {
|
|
318
|
+
replyJson(response, 400, { ok: false, error: `unreadable body: ${String(error)}` })
|
|
319
|
+
return
|
|
320
|
+
}
|
|
321
|
+
const referenceId = stringOf(body.referenceId, 200)
|
|
322
|
+
if (referenceId === undefined) { replyJson(response, 400, { ok: false, error: 'referenceId is required' }); return }
|
|
323
|
+
const partId = stringOf(body.partId, 200)
|
|
324
|
+
// Explicit jump: the caller wants the page moved to the stored URL rather
|
|
325
|
+
// than a report that it is showing something else.
|
|
326
|
+
const outcome = await service.reveal(referenceId, {
|
|
327
|
+
...typeof body.sourcePageId === 'string' && body.sourcePageId !== '' ? { sourcePageId: body.sourcePageId } : {},
|
|
328
|
+
...partId !== undefined ? { partId } : {},
|
|
329
|
+
...body.navigate === true ? { navigate: true } : {},
|
|
330
|
+
})
|
|
331
|
+
replyJson(response, 200, { ok: true, ...outcome })
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const insert: BrowserHttpHandler = async (request, response) => {
|
|
335
|
+
if (!requireWrite(request, response)) return
|
|
336
|
+
let body: Record<string, unknown>
|
|
337
|
+
try {
|
|
338
|
+
body = await readJsonBody<Record<string, unknown>>(request)
|
|
339
|
+
} catch (error) {
|
|
340
|
+
replyJson(response, 400, { ok: false, error: `unreadable body: ${String(error)}` })
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
const referenceId = stringOf(body.referenceId, 200)
|
|
344
|
+
if (referenceId === undefined) { replyJson(response, 400, { ok: false, error: 'referenceId is required' }); return }
|
|
345
|
+
// The durable hand-off is the real path: the DSH client drains this queue
|
|
346
|
+
// into the conversation draft, so a card appears only once the draft and its
|
|
347
|
+
// assets are saved. A host without the queue keeps the adapter contract.
|
|
348
|
+
if (queue !== undefined) {
|
|
349
|
+
const reference = service.get(referenceId)
|
|
350
|
+
if (reference === undefined) { replyJson(response, 404, { ok: false, reason: 'reference-not-found' }); return }
|
|
351
|
+
try {
|
|
352
|
+
const record = queue.prepare(reference)
|
|
353
|
+
// A removed intent is a tombstone: `prepare` answers with it instead of
|
|
354
|
+
// creating a second entry, so "insert" after a removal must say so rather
|
|
355
|
+
// than report success for a card that will never appear.
|
|
356
|
+
if (record.state === 'removed') {
|
|
357
|
+
replyJson(response, 409, { ok: false, referenceId, intentId: record.intentId, state: record.state, reason: 'reference-was-removed' })
|
|
358
|
+
return
|
|
359
|
+
}
|
|
360
|
+
replyJson(response, 200, {
|
|
361
|
+
ok: true, referenceId, intentId: record.intentId, state: record.state,
|
|
362
|
+
})
|
|
363
|
+
return
|
|
364
|
+
} catch (error) {
|
|
365
|
+
replyJson(response, 409, { ok: false, reason: String(error instanceof Error ? error.message : error) })
|
|
366
|
+
return
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
const result = await service.insertIntoDraft(referenceId)
|
|
370
|
+
replyJson(response, result.ok ? 200 : 409, result)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const exportBundle: BrowserHttpHandler = (request, response) => {
|
|
374
|
+
if (!requireRead(request, response)) return
|
|
375
|
+
const url = new URL(request.url ?? '/', URL_PARSE_BASE)
|
|
376
|
+
const format = url.searchParams.get('format') ?? 'jsonl'
|
|
377
|
+
if (!(EXPORT_FORMATS as readonly string[]).includes(format)) {
|
|
378
|
+
replyJson(response, 400, { ok: false, error: 'format must be jsonl, markdown, zip, or markdown-zip' })
|
|
379
|
+
return
|
|
380
|
+
}
|
|
381
|
+
const sessionId = url.searchParams.get('sessionId')
|
|
382
|
+
const encoded = service.exportBundle({
|
|
383
|
+
format: format as ReferenceExportFormat,
|
|
384
|
+
...sessionId !== null && sessionId !== '' ? { sessionId } : {},
|
|
385
|
+
})
|
|
386
|
+
response.writeHead(200, {
|
|
387
|
+
'content-type': encoded.mime,
|
|
388
|
+
'content-disposition': `attachment; filename="${encoded.fileName.replace(/["\\]/gu, '_')}"`,
|
|
389
|
+
'content-length': String(encoded.body.length),
|
|
390
|
+
'cache-control': 'no-store',
|
|
391
|
+
})
|
|
392
|
+
response.end(encoded.body)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Restore a `format=zip` export. The same write boundaries as capture/insert apply: the body
|
|
397
|
+
* is raw ZIP bytes (bounded), every hash is recomputed inside importReferenceBundle, and a
|
|
398
|
+
* partially rejected package reports its per-reference reasons instead of claiming success.
|
|
399
|
+
* The store and asset directory are the production defaults, which is what entry.ts builds the
|
|
400
|
+
* service with; a caller that customises them can pass a store/assetDir through the options.
|
|
401
|
+
*/
|
|
402
|
+
const importBundle: BrowserHttpHandler = async (request, response) => {
|
|
403
|
+
if (!requireWrite(request, response)) return
|
|
404
|
+
try {
|
|
405
|
+
const zip = await readBinaryBody(request)
|
|
406
|
+
const result = importReferenceBundle(zip)
|
|
407
|
+
replyJson(response, 200, { ok: true, ...result })
|
|
408
|
+
} catch (error) {
|
|
409
|
+
replyJson(response, 400, { ok: false, error: `unreadable reference package: ${String(error)}` })
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* The client polls this with its OWN conversation id. A delivery is never
|
|
415
|
+
* routed to "whatever session is active": `owner.sessionId` was fixed when
|
|
416
|
+
* the page was captured, so switching conversations mid-capture cannot move
|
|
417
|
+
* the reference (§3.5). Each record carries the whole frozen snapshot.
|
|
418
|
+
*/
|
|
419
|
+
const deliveries: BrowserHttpHandler = (request, response) => {
|
|
420
|
+
if (!requireRead(request, response)) return
|
|
421
|
+
if (queue === undefined) { replyJson(response, 503, { ok: false, error: 'delivery-unavailable' }); return }
|
|
422
|
+
const url = new URL(request.url ?? '/', URL_PARSE_BASE)
|
|
423
|
+
const sessionId = url.searchParams.get('sessionId')
|
|
424
|
+
// A poll must name its conversation. Answering without one would hand every
|
|
425
|
+
// session's records to whoever asked, and the client would advance its cursor
|
|
426
|
+
// past deliveries that were never meant for it.
|
|
427
|
+
if (sessionId === null || sessionId === '') {
|
|
428
|
+
replyJson(response, 400, { ok: false, error: 'sessionId is required' })
|
|
429
|
+
return
|
|
430
|
+
}
|
|
431
|
+
const rawSince = url.searchParams.get('since')
|
|
432
|
+
const since = rawSince === null ? 0 : Number(rawSince)
|
|
433
|
+
// An unparsable/out-of-range cursor used to silently degrade into "no
|
|
434
|
+
// since", replaying the conversation's ENTIRE history. A bad cursor is a
|
|
435
|
+
// caller bug: say so instead of duplicating every old card.
|
|
436
|
+
if (!Number.isSafeInteger(since) || since < 0) {
|
|
437
|
+
replyJson(response, 400, { ok: false, error: 'invalid since' })
|
|
438
|
+
return
|
|
439
|
+
}
|
|
440
|
+
const includeConsumed = url.searchParams.get('includeConsumed') === '1'
|
|
441
|
+
const snapshot = queue.list({ sessionId, includeConsumed, ...since > 0 ? { since } : {} })
|
|
442
|
+
// The cursor must never hide work that is still PENDING, whatever includeConsumed says. The client
|
|
443
|
+
// advances its cursor to the queue's sequence on every poll, so a capture written between two polls
|
|
444
|
+
// used to be invisible forever (measured: since=252 → 0 deliveries, since=0 → the same records).
|
|
445
|
+
// Pending cards are idempotent by intentId, so re-offering them costs nothing.
|
|
446
|
+
const pendingAll = queue.list({ sessionId, includeConsumed: false })
|
|
447
|
+
const seen = new Set(snapshot.deliveries.map(record => String((record as { intentId?: string }).intentId)))
|
|
448
|
+
const pending = [...snapshot.deliveries,
|
|
449
|
+
...pendingAll.deliveries.filter(record => !seen.has(String((record as { intentId?: string }).intentId)))]
|
|
450
|
+
replyJson(response, 200, {
|
|
451
|
+
ok: true,
|
|
452
|
+
sequence: snapshot.sequence,
|
|
453
|
+
deliveries: pending.map(record => ({
|
|
454
|
+
intentId: record.intentId,
|
|
455
|
+
sessionId: record.sessionId,
|
|
456
|
+
browserSessionId: record.browserSessionId,
|
|
457
|
+
referenceId: record.referenceId,
|
|
458
|
+
state: record.state,
|
|
459
|
+
failures: record.failures,
|
|
460
|
+
assetKeys: record.assetKeys,
|
|
461
|
+
requiredAssets: queue.requiredAssets(record),
|
|
462
|
+
...record.draftRev !== undefined ? { draftRev: record.draftRev } : {},
|
|
463
|
+
createdAt: record.createdAt,
|
|
464
|
+
updatedAt: record.updatedAt,
|
|
465
|
+
sequence: record.sequence,
|
|
466
|
+
snapshot: record.snapshot,
|
|
467
|
+
})),
|
|
468
|
+
})
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* `phase: 'acknowledged'` is refused unless the draft revision is real and
|
|
473
|
+
* every image asset this reference needs is bound. That is the server-side
|
|
474
|
+
* half of the rule "only claim success after the draft and assets are saved".
|
|
475
|
+
*/
|
|
476
|
+
const ack: BrowserHttpHandler = async (request, response) => {
|
|
477
|
+
if (!requireWrite(request, response)) return
|
|
478
|
+
if (queue === undefined) { replyJson(response, 503, { ok: false, error: 'delivery-unavailable' }); return }
|
|
479
|
+
let body: Record<string, unknown>
|
|
480
|
+
try {
|
|
481
|
+
body = await readJsonBody<Record<string, unknown>>(request)
|
|
482
|
+
} catch (error) {
|
|
483
|
+
replyJson(response, 400, { ok: false, error: `unreadable body: ${String(error)}` })
|
|
484
|
+
return
|
|
485
|
+
}
|
|
486
|
+
let intentId = stringOf(body.intentId, 200)
|
|
487
|
+
// The records menu knows a reference id, not the intent that carried it.
|
|
488
|
+
// Resolve it rather than making every caller learn the internal id.
|
|
489
|
+
if (intentId === undefined) {
|
|
490
|
+
const referenceId = stringOf(body.referenceId, 200)
|
|
491
|
+
if (referenceId !== undefined) {
|
|
492
|
+
intentId = queue.list().deliveries.find(entry => entry.referenceId === referenceId)?.intentId
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if (intentId === undefined) { replyJson(response, 400, { ok: false, error: 'intentId or referenceId is required' }); return }
|
|
496
|
+
if (body.phase === 'consumed') {
|
|
497
|
+
const sessionId = stringOf(body.sessionId, 200)
|
|
498
|
+
if (sessionId === undefined) { replyJson(response, 400, { ok: false, error: 'sessionId is required' }); return }
|
|
499
|
+
const record = queue.consume(intentId, sessionId)
|
|
500
|
+
if (record === undefined) { replyJson(response, 409, { ok: false, error: 'delivery-owner-mismatch-or-removed' }); return }
|
|
501
|
+
replyJson(response, 200, { ok: true, intentId, state: record.state, draftRev: record.draftRev ?? 0, assetKeys: record.assetKeys })
|
|
502
|
+
return
|
|
503
|
+
}
|
|
504
|
+
if (!Number.isInteger(body.draftRev) || (body.draftRev as number) < 1) {
|
|
505
|
+
replyJson(response, 400, { ok: false, error: 'draftRev must be a saved positive revision' }); return
|
|
506
|
+
}
|
|
507
|
+
if (!Array.isArray(body.assetKeys) || body.assetKeys.some(key => typeof key !== 'string' || !/^[0-9a-f]{64}$/.test(key))) {
|
|
508
|
+
replyJson(response, 400, { ok: false, error: 'assetKeys must be content hashes' }); return
|
|
509
|
+
}
|
|
510
|
+
const deliveryAck = { draftRev: body.draftRev as number, assetKeys: body.assetKeys as string[] }
|
|
511
|
+
const phase = body.phase === 'draft-applied' ? 'draft-applied' : 'acknowledged'
|
|
512
|
+
const record = phase === 'draft-applied'
|
|
513
|
+
? queue.markDraftApplied(intentId, deliveryAck)
|
|
514
|
+
: queue.acknowledge(intentId, deliveryAck)
|
|
515
|
+
if (record === undefined) {
|
|
516
|
+
replyJson(response, 409, { ok: false, error: 'delivery-not-acknowledgeable', reason: 'missing intent, removed, or assets not bound' })
|
|
517
|
+
return
|
|
518
|
+
}
|
|
519
|
+
replyJson(response, 200, { ok: true, intentId, state: record.state, draftRev: record.draftRev, assetKeys: record.assetKeys })
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/** Remove a card. The tombstone survives retries and a DSH restart. */
|
|
523
|
+
const removeDelivery: BrowserHttpHandler = async (request, response) => {
|
|
524
|
+
if (!requireWrite(request, response)) return
|
|
525
|
+
if (queue === undefined) { replyJson(response, 503, { ok: false, error: 'delivery-unavailable' }); return }
|
|
526
|
+
let body: Record<string, unknown>
|
|
527
|
+
try {
|
|
528
|
+
body = await readJsonBody<Record<string, unknown>>(request)
|
|
529
|
+
} catch (error) {
|
|
530
|
+
replyJson(response, 400, { ok: false, error: `unreadable body: ${String(error)}` })
|
|
531
|
+
return
|
|
532
|
+
}
|
|
533
|
+
let intentId = stringOf(body.intentId, 200)
|
|
534
|
+
// The settings panel's records menu knows a reference id, not the intent
|
|
535
|
+
// that carried it — client.js `removeFromDraft` posts { referenceId }.
|
|
536
|
+
// Resolve it the same way the ACK handler does, instead of answering 400
|
|
537
|
+
// for a perfectly valid removal and letting the next poll re-insert the card.
|
|
538
|
+
if (intentId === undefined) {
|
|
539
|
+
const referenceId = stringOf(body.referenceId, 200)
|
|
540
|
+
if (referenceId !== undefined) {
|
|
541
|
+
intentId = queue.list().deliveries.find(entry => entry.referenceId === referenceId)?.intentId
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
// A missing/unresolvable id means the card is already gone: `list()` drops
|
|
545
|
+
// removed records, so a tombstoned reference cannot be looked up again.
|
|
546
|
+
// Say so with a stable 409 rather than a misleading 400/404.
|
|
547
|
+
if (intentId === undefined) { replyJson(response, 409, { ok: false, error: 'reference-was-removed' }); return }
|
|
548
|
+
const record = queue.remove(intentId)
|
|
549
|
+
if (record === undefined) { replyJson(response, 404, { ok: false, error: 'delivery-not-found' }); return }
|
|
550
|
+
replyJson(response, 200, { ok: true, intentId, state: record.state })
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Actual bytes of a captured image. `encoding=base64` returns JSON so the
|
|
555
|
+
* client can build a standard image block without a second fetch; the raw
|
|
556
|
+
* form drives the card thumbnail. A reference is not "an image URL".
|
|
557
|
+
*/
|
|
558
|
+
const asset: BrowserHttpHandler = (request, response) => {
|
|
559
|
+
if (!requireRead(request, response)) return
|
|
560
|
+
const url = new URL(request.url ?? '/', URL_PARSE_BASE)
|
|
561
|
+
const key = url.searchParams.get('key') ?? ''
|
|
562
|
+
if (!/^[0-9a-f]{64}$/.test(key)) { replyJson(response, 400, { ok: false, error: 'invalid asset key' }); return }
|
|
563
|
+
const file = service.assetPath(key)
|
|
564
|
+
if (file === undefined) { replyJson(response, 404, { ok: false, error: 'asset-not-found' }); return }
|
|
565
|
+
let mime = 'application/octet-stream'
|
|
566
|
+
try {
|
|
567
|
+
const meta: unknown = JSON.parse(readFileSync(join(dirname(file), `${key}.json`), 'utf8'))
|
|
568
|
+
const candidate = (meta as { mime?: unknown }).mime
|
|
569
|
+
if (typeof candidate === 'string' && /^image\/[a-z0-9.+-]+$/i.test(candidate)) mime = candidate
|
|
570
|
+
} catch { /* a missing sidecar still serves the bytes */ }
|
|
571
|
+
let bytes: Buffer
|
|
572
|
+
try {
|
|
573
|
+
bytes = readFileSync(file)
|
|
574
|
+
} catch {
|
|
575
|
+
// The sidecar/existsSync check can race with an external asset cleanup;
|
|
576
|
+
// an uncaught ENOENT here used to kill the response before any byte was
|
|
577
|
+
// written, leaving the card thumbnail broken with no status at all.
|
|
578
|
+
replyJson(response, 404, { ok: false, error: 'asset-not-found' })
|
|
579
|
+
return
|
|
580
|
+
}
|
|
581
|
+
if (url.searchParams.get('encoding') === 'base64') {
|
|
582
|
+
replyJson(response, 200, { ok: true, key, mime, bytes: bytes.length, sha256: key, data: bytes.toString('base64') })
|
|
583
|
+
return
|
|
584
|
+
}
|
|
585
|
+
response.writeHead(200, {
|
|
586
|
+
'content-type': mime,
|
|
587
|
+
'content-length': String(bytes.length),
|
|
588
|
+
'cache-control': 'no-store',
|
|
589
|
+
'x-content-type-options': 'nosniff',
|
|
590
|
+
})
|
|
591
|
+
response.end(bytes)
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
return {
|
|
595
|
+
source, list, get, capture, reveal, insert, export: exportBundle, importBundle,
|
|
596
|
+
deliveries, ack, removeDelivery, asset,
|
|
597
|
+
}
|
|
598
|
+
}
|