MCPcopy Create free account
hub / github.com/cloudflare/computer / assertResponseWithin

Function assertResponseWithin

packages/computer/src/runtime/bridge.ts:476–497  ·  view source on GitHub ↗
(value: unknown, maxBytes: number)

Source from the content-addressed store, hash-verified

474}
475
476function assertResponseWithin(value: unknown, maxBytes: number) {
477 let bytes = 0;
478 let nodes = 0;
479 const visit = (item: unknown): void => {
480 nodes += 1;
481 if (nodes > 4096) throw new Error("Workspace capability response has too many values.");
482 if (typeof item === "string") bytes += item.length * 3;
483 else if (item instanceof Uint8Array) bytes += item.byteLength * 4;
484 else if (typeof item === "number" || typeof item === "boolean" || item === null) bytes += 16;
485 else if (Array.isArray(item)) for (const child of item) visit(child);
486 else if (item && typeof item === "object") {
487 for (const [key, child] of Object.entries(item)) {
488 bytes += key.length * 3;
489 visit(child);
490 }
491 }
492 if (bytes > maxBytes) {
493 throw new Error(`Workspace capability response exceeds ${maxBytes} bytes.`);
494 }
495 };
496 visit(value);
497}
498
499function encodeBoundedError(error: unknown, maxPayloadBytes: number) {
500 const value = error as { code?: unknown; path?: unknown };

Callers 1

encodeCallFunction · 0.85

Calls 1

visitFunction · 0.70

Tested by

no test coverage detected