All files / app-leaderboard/src/http body.ts

93.54% Statements 29/31
83.33% Branches 20/24
100% Functions 3/3
93.33% Lines 28/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67            13x                   239x 239x 1x   238x 239x 1x   237x 239x     237x       237x 237x 234x 234x     237x 237x   3x 1x   234x 234x   1x             471x 471x   470x 470x 2x 2x   468x        
/** Upper bounds of a submission body: the bytes on the wire, and the JSON once decompressed. */
export interface BodyLimits {
  maxEncodedBytes: number;
  maxDecodedBytes: number;
}
 
export const SUBMISSION_LIMITS: BodyLimits = { maxEncodedBytes: 2 * 1024 * 1024, maxDecodedBytes: 16 * 1024 * 1024 };
 
export type BodyResult = { ok: true; value: unknown } | { ok: false; status: 400 | 413 | 415; reason: string };
 
/**
 * Reads a JSON request body, gunzipping it when it is sent with `Content-Encoding: gzip` (replays compress about ten
 * to one). Both the encoded and the decoded size are counted while streaming, so an oversized or decompression-bomb
 * body is cut off instead of buffered.
 */
export async function readJSONBody(request: Request, limits: BodyLimits): Promise<BodyResult> {
  const contentType = request.headers.get('content-type') ?? '';
  if (!contentType.toLowerCase().startsWith('application/json')) {
    return { ok: false, status: 415, reason: 'expected-json' };
  }
  const encoding = (request.headers.get('content-encoding') ?? 'identity').toLowerCase();
  if (encoding !== 'identity' && encoding !== 'gzip') {
    return { ok: false, status: 415, reason: 'unsupported-encoding' };
  }
  const declared = Number(request.headers.get('content-length') ?? Number.NaN);
  Iif (Number.isFinite(declared) && declared > limits.maxEncodedBytes) {
    return { ok: false, status: 413, reason: 'body-too-large' };
  }
  Iif (request.body === null) {
    return { ok: false, status: 400, reason: 'empty-body' };
  }
 
  let stream: ReadableStream<Uint8Array> = request.body.pipeThrough(byteLimit(limits.maxEncodedBytes));
  if (encoding === 'gzip') {
    const gunzip = new DecompressionStream('gzip') as unknown as TransformStream<Uint8Array, Uint8Array>;
    stream = stream.pipeThrough(gunzip).pipeThrough(byteLimit(limits.maxDecodedBytes));
  }
  let text: string;
  try {
    text = await new Response(stream).text();
  } catch (error) {
    if (error instanceof BodyTooLargeError) return { ok: false, status: 413, reason: 'body-too-large' };
    return { ok: false, status: 400, reason: 'invalid-encoding' };
  }
  try {
    return { ok: true, value: JSON.parse(text) };
  } catch {
    return { ok: false, status: 400, reason: 'invalid-json' };
  }
}
 
class BodyTooLargeError extends Error {}
 
function byteLimit(maxBytes: number): TransformStream<Uint8Array, Uint8Array> {
  let total = 0;
  return new TransformStream({
    transform(chunk, controller) {
      total += chunk.byteLength;
      if (total > maxBytes) {
        controller.error(new BodyTooLargeError());
        return;
      }
      controller.enqueue(chunk);
    },
  });
}