Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
/** Reads a request body, giving up as soon as it is larger than the limit. */
export async function readLimitedBody(
request: Request,
maxBytes: number,
): Promise<Uint8Array | null> {
if (!request.body) {
return new Uint8Array();
}
const reader = request.body.getReader();
const chunks: Uint8Array[] = [];
let total = 0;
for (;;) {
const { done, value } = await reader.read();
if (done) {
break;
}
total += value.byteLength;
if (total > maxBytes) {
await reader.cancel().catch(() => undefined);
return null;
}
chunks.push(value);
}
const body = new Uint8Array(total);
let offset = 0;
for (const chunk of chunks) {
body.set(chunk, offset);
offset += chunk.byteLength;
}
return body;
}