@broberg/lens-daemon 0.1.61 → 0.1.62

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@broberg/lens-daemon",
3
- "version": "0.1.61",
3
+ "version": "0.1.62",
4
4
  "description": "The Lens module of the cardmem daemon (flows, critic, coverage, capture sessions, the /lens/* routes and the cardmem-lens MCP) as a package. The daemon mounts createLensRouter(deps) + createLensMcpRouter(deps). Bun runtime.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -15,6 +15,7 @@
15
15
  "sharp": "^0.35.5",
16
16
  "turndown": "^7.2.4",
17
17
  "zod": "^3.23.8",
18
+ "@broberg/lens-measure": "0.1.0",
18
19
  "@broberg/lens-engine": "0.14.1",
19
20
  "@broberg/lens-svg": "0.1.1"
20
21
  },
package/src/http.ts CHANGED
@@ -35,6 +35,7 @@ import { drainUploads, uploadStats } from './artifact-uploads.ts';
35
35
  import { backfillFromDisk } from './artifact-backfill.ts';
36
36
  import { artifactContentType } from './storage.ts';
37
37
  import { localRequestGuard } from './local-guard.ts';
38
+ import { measureRequest, MeasureBodyError } from './measure-local.ts';
38
39
 
39
40
  export function createLensRouter(deps: LensDeps): Hono {
40
41
  const r = new Hono();
@@ -77,6 +78,17 @@ export function createLensRouter(deps: LensDeps): Hono {
77
78
  }
78
79
  };
79
80
 
81
+ // F007.34 — Cloud Lens's /measure, run here: same body, same spec sheet (no {ok} wrapper,
82
+ // so the answer is byte-for-byte what Cloud Lens would give).
83
+ r.post('/measure', async (c) => {
84
+ const body = await c.req.json().catch(() => undefined);
85
+ if (body === undefined) return c.json({ error: 'invalid JSON body' }, 400);
86
+ try {
87
+ return c.json(await measureRequest(body));
88
+ } catch (err) {
89
+ return c.json({ error: err instanceof Error ? err.message : 'measure failed' }, err instanceof MeasureBodyError ? 400 : 502);
90
+ }
91
+ });
80
92
  r.post('/capture', handle((body) => lensCapture(deps, body as Parameters<typeof lensCapture>[1])));
81
93
  r.post('/verify', handle((body) => lensVerify(deps, body as Parameters<typeof lensVerify>[1])));
82
94
  // F074.63 — the export verb beside capture. Same pipeline, one terminal call
package/src/index.ts CHANGED
@@ -20,6 +20,9 @@ export { ensureChromium, browsersPresent } from './ensure-chromium.ts';
20
20
  export { sweepChromeClones, chromeCloneStatus } from './chrome-clones.ts';
21
21
  export { drainUploads } from './artifact-uploads.ts';
22
22
  export { closeBrowser } from './browser.ts';
23
+ // F007.34 — Cloud Lens's /measure, in process: cardmem's edge-op calls this for loopback/tailnet URLs.
24
+ export { measureRequest } from './measure-local.ts';
25
+ export type { SpecSheet } from '@broberg/lens-measure';
23
26
 
24
27
  /** The version the daemon reports on /health as {lens:{source, version}} (cardmem #38390). */
25
28
  export { version as LENS_DAEMON_VERSION } from '../package.json';
@@ -0,0 +1,62 @@
1
+ // F007.34 — the daemon measures with Cloud Lens's own instrument (@broberg/lens-measure),
2
+ // for surfaces only reachable from the owner's machine: loopback and the tailnet. ARS
3
+ // listens on 127.0.0.1/Tailscale only, so Cloud Lens /measure can never reach it, and
4
+ // cardmem's F389 gate needs a measurement (cardmem #45105/#45118).
5
+ import { isIP } from 'node:net';
6
+ import { configureMeasure, measureBody, measureBodySchema, type SpecSheet } from '@broberg/lens-measure';
7
+ import { fetchStorageState, isPrivateIp } from '@broberg/lens-engine';
8
+ import { getBrowser } from './browser.ts';
9
+ import { version } from '../package.json';
10
+
11
+ const v4 = (ip: string) => ip.split('.').map(Number);
12
+ const isLoopback = (ip: string) => ip === '::1' || (isIP(ip) === 4 && v4(ip)[0] === 127);
13
+ // 100.64.0.0/10 — Tailscale's CGNAT range.
14
+ const isTailnet = (ip: string) => isIP(ip) === 4 && v4(ip)[0] === 100 && (v4(ip)[1]! & 0xc0) === 64;
15
+
16
+ /** The daemon's guard: loopback and the tailnet are the point; any OTHER private address
17
+ * (a LAN printer, a router admin page) is refused, as Cloud Lens refuses all of them. */
18
+ export async function assertDaemonMeasureUrl(url: string): Promise<void> {
19
+ let u: URL;
20
+ try {
21
+ u = new URL(url);
22
+ } catch {
23
+ throw new Error(`not a URL: ${url}`);
24
+ }
25
+ if (u.protocol !== 'http:' && u.protocol !== 'https:') throw new Error(`only http(s) can be measured, not ${u.protocol}`);
26
+ const host = u.hostname.replace(/^\[|\]$/g, '').toLowerCase();
27
+ if (host === 'localhost' || host.endsWith('.localhost') || host.endsWith('.ts.net')) return;
28
+ if (isIP(host)) {
29
+ if (isLoopback(host) || isTailnet(host)) return;
30
+ if (isPrivateIp(host)) throw new Error(`address ${host} is private and neither loopback nor the tailnet — not measured`);
31
+ }
32
+ }
33
+
34
+ let configured = false;
35
+ function ensureConfigured(): void {
36
+ if (configured) return;
37
+ configureMeasure({
38
+ getBrowser: () => getBrowser('chromium'),
39
+ // The daemon keeps one warm browser and closes it on shutdown (closeBrowser).
40
+ armIdleTimer: () => {},
41
+ assertAllowedUrl: assertDaemonMeasureUrl,
42
+ fetchStorageState,
43
+ lensVersion: () => `lens-daemon@${version}`,
44
+ });
45
+ configured = true;
46
+ }
47
+
48
+ /** Cloud Lens's POST /measure body in, its spec sheet out — the same parser and the same
49
+ * measuring function, run in this daemon. A bad body throws naming the first issue. */
50
+ export async function measureRequest(body: unknown): Promise<SpecSheet> {
51
+ ensureConfigured();
52
+ const p = measureBodySchema.safeParse(body);
53
+ if (!p.success) {
54
+ const i = p.error.issues[0]!;
55
+ throw new MeasureBodyError(`invalid body: ${i.path.join('.') || '(root)'} — ${i.message}`);
56
+ }
57
+ return measureBody(p.data);
58
+ }
59
+
60
+ export class MeasureBodyError extends Error {
61
+ override name = 'MeasureBodyError';
62
+ }