|
| 1 | +/** |
| 2 | + * Custom bindings: host-to-sandbox function bridge. |
| 3 | + * |
| 4 | + * Users register a BindingTree of host-side functions via the `bindings` |
| 5 | + * option. The tree is validated, flattened to __bind.* prefixed keys, and |
| 6 | + * merged into bridgeHandlers so sandbox code can call them through the bridge. |
| 7 | + */ |
| 8 | + |
| 9 | +import type { BridgeHandler } from "./bridge-handlers.js"; |
| 10 | +import { BRIDGE_GLOBAL_KEY_LIST } from "./bridge-contract.js"; |
| 11 | + |
| 12 | +/** A user-defined host-side function callable from the sandbox. */ |
| 13 | +export type BindingFunction = (...args: unknown[]) => unknown | Promise<unknown>; |
| 14 | + |
| 15 | +/** A nested tree of binding functions. Nesting depth limited to 4. */ |
| 16 | +export interface BindingTree { |
| 17 | + [key: string]: BindingFunction | BindingTree; |
| 18 | +} |
| 19 | + |
| 20 | +/** Prefix for flattened binding keys in the bridge handler map. */ |
| 21 | +export const BINDING_PREFIX = "__bind."; |
| 22 | + |
| 23 | +const MAX_DEPTH = 4; |
| 24 | +const MAX_LEAVES = 64; |
| 25 | +const JS_IDENTIFIER_RE = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/; |
| 26 | + |
| 27 | +// eslint-disable-next-line @typescript-eslint/no-empty-function |
| 28 | +const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor; |
| 29 | + |
| 30 | +export interface FlattenedBinding { |
| 31 | + key: string; |
| 32 | + handler: BridgeHandler; |
| 33 | + isAsync: boolean; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Validate and flatten a BindingTree into prefixed bridge handler entries. |
| 38 | + * |
| 39 | + * Throws on: |
| 40 | + * - Invalid JS identifiers as keys |
| 41 | + * - Nesting depth > 4 |
| 42 | + * - More than 64 leaf functions |
| 43 | + * - Binding keys starting with `_` (reserved for internal bridge names) |
| 44 | + */ |
| 45 | +export function flattenBindingTree(tree: BindingTree): FlattenedBinding[] { |
| 46 | + const result: FlattenedBinding[] = []; |
| 47 | + const internalKeys = new Set<string>(BRIDGE_GLOBAL_KEY_LIST as readonly string[]); |
| 48 | + |
| 49 | + function walk(node: BindingTree, path: string[], depth: number): void { |
| 50 | + if (depth > MAX_DEPTH) { |
| 51 | + throw new Error( |
| 52 | + `Binding tree exceeds maximum nesting depth of ${MAX_DEPTH} at path: ${path.join(".")}`, |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + for (const key of Object.keys(node)) { |
| 57 | + if (!JS_IDENTIFIER_RE.test(key)) { |
| 58 | + throw new Error( |
| 59 | + `Invalid binding key "${key}": must be a valid JavaScript identifier`, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + // Reject keys starting with _ to avoid collision with internal bridge names |
| 64 | + if (key.startsWith("_")) { |
| 65 | + throw new Error( |
| 66 | + `Binding key "${key}" starts with "_" which is reserved for internal bridge names`, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + const fullPath = [...path, key]; |
| 71 | + const value = node[key]; |
| 72 | + |
| 73 | + if (typeof value === "function") { |
| 74 | + const flatKey = BINDING_PREFIX + fullPath.join("."); |
| 75 | + |
| 76 | + // Double-check flattened key doesn't collide with known internals |
| 77 | + if (internalKeys.has(flatKey)) { |
| 78 | + throw new Error( |
| 79 | + `Binding "${fullPath.join(".")}" collides with internal bridge name "${flatKey}"`, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + result.push({ |
| 84 | + key: flatKey, |
| 85 | + handler: value as BridgeHandler, |
| 86 | + isAsync: value instanceof AsyncFunction, |
| 87 | + }); |
| 88 | + |
| 89 | + if (result.length > MAX_LEAVES) { |
| 90 | + throw new Error( |
| 91 | + `Binding tree exceeds maximum of ${MAX_LEAVES} leaf functions`, |
| 92 | + ); |
| 93 | + } |
| 94 | + } else if (typeof value === "object" && value !== null) { |
| 95 | + walk(value as BindingTree, fullPath, depth + 1); |
| 96 | + } else { |
| 97 | + throw new Error( |
| 98 | + `Invalid binding value at "${fullPath.join(".")}": expected function or object, got ${typeof value}`, |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + walk(tree, [], 1); |
| 105 | + return result; |
| 106 | +} |
0 commit comments