Files
scriptc/tests/fixtures/npm/node_modules/hono/dist/utils/buffer.js
T
Malte UblandClaude Opus 5.5 dddcbed335 Project zod 4 and @hono/zod-validator values at the declaration boundary
The declaration-typed boundary already reaches zod 4 schemas and hono's
c.req.valid(): zod 4's z.infer and hono's validator Input generics resolve
to plain object types in the declaration world. Three gaps remained:

- Boolean literal types printed as `false` (the TS7 facade leaves
  intrinsicName unset), so `{ ok: true } | { ok: false }` lost its true
  arm and `boolean | undefined` became `undefined | false`. The printer
  reads the literal's value.
- `r.data` after `const r = S.safeParse(x); if (r.success)` never
  crossed: zod 4's result union is not data (its error arm is a class),
  and reads on projectable receivers were skipped on the assumption that
  something else types the receiver. Such reads now cross when the
  receiver is a package call whose type does not project, or a const
  bound to one.
- Reads through bindings annotated with a projected type no longer get a
  redundant cast, and aliases are named only for casts that survive.

New npm cases zod4-boundary and hono-zod-validator run real zod 4.6.7,
hono 4.13.13 and @hono/zod-validator 0.9.1 (vendored file closures) under
Node and natively; decl-boundary.test.ts checks that c.req.valid() is a
checked cast with a path TypeError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 14:09:39 -07:00

52 lines
1.9 KiB
JavaScript

import { sha256 } from "./crypto.js";
//#region src/utils/buffer.ts
/**
* @module
* Buffer utility.
*/
const equal = (a, b) => {
if (a === b) return true;
if (a.byteLength !== b.byteLength) return false;
const va = new DataView(a);
const vb = new DataView(b);
let i = va.byteLength;
while (i--) if (va.getUint8(i) !== vb.getUint8(i)) return false;
return true;
};
const constantTimeEqualString = (a, b) => {
const aLen = a.length;
const bLen = b.length;
const maxLen = Math.max(aLen, bLen);
let out = aLen ^ bLen;
for (let i = 0; i < maxLen; i++) {
const aChar = i < aLen ? a.charCodeAt(i) : 0;
const bChar = i < bLen ? b.charCodeAt(i) : 0;
out |= aChar ^ bChar;
}
return out === 0;
};
const timingSafeEqualString = async (a, b, hashFunction) => {
if (!hashFunction) hashFunction = sha256;
const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)]);
if (sa == null || sb == null || typeof sa !== "string" || typeof sb !== "string") return false;
const hashEqual = constantTimeEqualString(sa, sb);
const originalEqual = constantTimeEqualString(a, b);
return hashEqual && originalEqual;
};
const timingSafeEqual = async (a, b, hashFunction) => {
if (typeof a === "string" && typeof b === "string") return timingSafeEqualString(a, b, hashFunction);
if (!hashFunction) hashFunction = sha256;
const [sa, sb] = await Promise.all([hashFunction(a), hashFunction(b)]);
if (!sa || !sb || typeof sa !== "string" || typeof sb !== "string") return false;
return timingSafeEqualString(sa, sb);
};
const bufferToString = (buffer) => {
if (buffer instanceof ArrayBuffer) return new TextDecoder("utf-8").decode(buffer);
return buffer;
};
const bufferToFormData = (arrayBuffer, contentType) => {
return new Response(arrayBuffer, { headers: { "Content-Type": contentType.replace(/^[^;]+/, (mediaType) => mediaType.toLowerCase()) } }).formData();
};
//#endregion
export { bufferToFormData, bufferToString, equal, timingSafeEqual };