@johnhenry/wsh 0.48.0 → 0.49.0

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/src/testing.d.ts CHANGED
@@ -46,5 +46,49 @@ export class QMuxProtocolError extends Error {
46
46
  readonly errorCode: number;
47
47
  }
48
48
 
49
+ import type { WshClient } from './index.js';
50
+ import type { WshMemoryHost, WshMemoryHostOptions, WshMemoryConnectOptions } from './memory-host.js';
51
+ import type { PipeRelay, PipeRelayOptions } from './pipe-relay.js';
52
+
53
+ /** One message the in-process relay wrote. */
54
+ export interface RelayTapInfo { to: 'host' | 'operator'; id: string; bytes: Uint8Array }
55
+
56
+ /** A pipe relay that lives in this process (see `createInProcessPipeRelay`). */
57
+ export interface InProcessPipeRelay {
58
+ /** The real relay (`createPipeRelay()`); `accept` is wired to the sockets `WebSocket` hands out. */
59
+ relay: PipeRelay;
60
+ /** Every message the relay wrote, before `tap` ran: `to` is the role it was written to. */
61
+ recorded: Array<{ to: 'host' | 'operator'; id: string; bytes: Uint8Array }>;
62
+ /** Called for each message the relay writes: `undefined` passes it, `null` drops it, bytes (or an array of them) are written instead. May be set at any time. */
63
+ tap: ((info: RelayTapInfo) => Uint8Array | Uint8Array[] | null | undefined) | null;
64
+ /** A `WebSocket` constructor that dials this relay: `new env.WebSocket(pipeUrl(anyBase, id, role))`. */
65
+ WebSocket: new (url: string) => any;
66
+ stats(): { waiting: number; pipes: number };
67
+ /** End every pipe at once, as a relay restart would. */
68
+ dropAll(): void;
69
+ close(): Promise<void>;
70
+ }
71
+
72
+ /** The real pipe relay running in this process, with a record and a tap for a hostile one. No port, no Node built-in. */
73
+ export function createInProcessPipeRelay(options?: { tap?: InProcessPipeRelay['tap']; relayOptions?: PipeRelayOptions }): Promise<InProcessPipeRelay>;
74
+
75
+ export interface MemoryPair {
76
+ host: WshMemoryHost;
77
+ client: WshClient;
78
+ username: string;
79
+ /** The key the client logged in with (`null` when `host.auth` was given and the client logs in some other way). */
80
+ keyPair: CryptoKeyPair | null;
81
+ /** Disconnects the client, then closes the host. */
82
+ close(): Promise<void>;
83
+ }
84
+
85
+ /** A memory host and a client already logged in to it. Without `host.auth`, it makes (or uses) a key and authorizes exactly that key. */
86
+ export function createMemoryPair(options?: {
87
+ host?: WshMemoryHostOptions;
88
+ username?: string;
89
+ keyPair?: CryptoKeyPair;
90
+ credentials?: Partial<WshMemoryConnectOptions>;
91
+ }): Promise<MemoryPair>;
92
+
49
93
  /** Machine-readable form of the notice: `stable` is `false`. */
50
94
  export const QMUX_TESTING_API: Readonly<{ stable: false; notice: string }>;
package/src/testing.mjs CHANGED
@@ -20,6 +20,9 @@ export {
20
20
  QMuxProtocolError,
21
21
  } from './qmux.mjs';
22
22
 
23
+ // Test doubles: a pipe relay that lives in this process (with a tap for a hostile relay), and a memory host with a client logged in.
24
+ export { createInProcessPipeRelay, createMemoryPair } from './testing-doubles.mjs';
25
+
23
26
  /** Machine-readable form of the notice above, so a test (or a reader) can check what it imported. */
24
27
  export const QMUX_TESTING_API = Object.freeze({
25
28
  stable: false,
package/src/transport.mjs CHANGED
@@ -384,7 +384,7 @@ function decodeHashString(str) {
384
384
  // Strip the separators OpenSSL and friends emit, plus any surrounding
385
385
  // whitespace: "SHA256 Fingerprint=" style output is colon-separated,
386
386
  // and hand-copied values often carry spaces or dashes.
387
- const compact = str.trim().replace(/^[^=]*fingerprint[^=]*=\s*/i, '');
387
+ const compact = str.trim().replace(/^[^=]{0,64}fingerprint[^=]{0,64}=\s*/i, '');
388
388
  const hexish = compact.replace(/[\s:-]/g, '');
389
389
 
390
390
  if (/^[0-9a-fA-F]+$/.test(hexish) && hexish.length % 2 === 0) {
package/src/webmcp.mjs CHANGED
@@ -21,6 +21,7 @@
21
21
  */
22
22
 
23
23
  import { RpcError, RPC_ERROR } from './rpc.mjs';
24
+ import { callerIdentity } from './key-names.mjs';
24
25
 
25
26
  const TOOL_NAME_RE = /^[A-Za-z0-9_.-]{1,128}$/;
26
27
  const APPROVE_MODES = new Set(['never', 'consequential', 'always']);
@@ -441,11 +442,13 @@ export function webmcpTools(options = {}) {
441
442
  }
442
443
 
443
444
  /** What one session may see: allowed, listable, within `maxTools`, and permitted by its key. */
444
- async function visible(permits) {
445
+ async function visible(permits, kind = 'mcp-tool') {
445
446
  const out = [];
446
447
  for (const tool of (await gather()).values()) {
447
448
  if (!allowed.has(tool.name)) continue;
448
- if (permits && !permits({ kind: 'mcp-tool', tool: tool.name })) continue;
449
+ // The hook is told which it is: `mcp-tool` is listing a tool, `mcp-call` is running one (`op` says the same as `'list'` / `'call'`),
450
+ // so an adapter may scope them apart. The notation's `mcp:TOOL` covers both.
451
+ if (permits && !permits({ kind, op: kind === 'mcp-call' ? 'call' : 'list', tool: tool.name })) continue;
449
452
  const bad = unlistable(tool, limits);
450
453
  if (bad) { emit({ outcome: 'unlisted', name: tool.name, limit: bad.limit, detail: bad.detail }); continue; }
451
454
  if (out.length >= limits.maxTools) { emit({ outcome: 'unlisted', name: tool.name, limit: 'maxTools', detail: `more than ${limits.maxTools} tools` }); continue; }
@@ -464,7 +467,8 @@ export function webmcpTools(options = {}) {
464
467
  };
465
468
 
466
469
  function handler(channel, ctx = {}) {
467
- const who = { username: ctx.user ?? null, fingerprint: ctx.fingerprint ?? null };
470
+ // A wsh caller is its key, `{ username, fingerprint }`; a transport that is not wsh (`serveMcpOverDuplex`) gives `ctx.who`, which may carry a `podId`.
471
+ const who = callerIdentity(ctx.who ?? { username: ctx.user ?? null, fingerprint: ctx.fingerprint ?? null });
468
472
  const permits = typeof ctx.permits === 'function' ? ctx.permits : null;
469
473
  const session = { channel, who, inflight: new Map(), starts: [], approvals: Promise.resolve(), unsubscribe: null, listed: null, closed: false };
470
474
  sessions.add(session);
@@ -507,7 +511,7 @@ export function webmcpTools(options = {}) {
507
511
  if (session.starts.length >= limits.maxCallsPerMinute) throw fail('maxCallsPerMinute', `more than ${limits.maxCallsPerMinute} calls in a minute`, { name });
508
512
  session.starts.push(t);
509
513
 
510
- const tool = (await visible(permits)).find((x) => x.name === name);
514
+ const tool = (await visible(permits, 'mcp-call')).find((x) => x.name === name);
511
515
  // A tool that is not allowed, not permitted to this key or not there looks the same.
512
516
  if (!tool) { emit({ outcome: 'unknown', name, who }); throw new RpcError(RPC_ERROR.INVALID_PARAMS, `unknown tool: ${name}`); }
513
517
  const text = jsonOrNull(args) ?? '';