-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
32 lines (24 loc) · 876 Bytes
/
utils.ts
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
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
import { RepresentationHeader } from "./deps.ts";
// TODO(miayuci): add strict parsing.
const ReNoTransform = /(?:^|,)\s*?no-transform\s*?(?:,|$)/;
export function isNoTransform(input: string): boolean {
return ReNoTransform.test(input);
}
/** Return new `Response` if the response include `Content-Length` header and readable. */
export async function reCalcContentLength(
response: Response,
): Promise<Response> {
if (
response.bodyUsed ||
!response.headers.has(RepresentationHeader.ContentLength)
) return response;
const length = await response
.clone()
.arrayBuffer()
.then((buffer) => buffer.byteLength)
.then(String);
response.headers.set(RepresentationHeader.ContentLength, length);
return response;
}