|
| 1 | +import * as v from '@badrap/valita'; |
| 2 | + |
| 3 | +import * as err from './errors.js'; |
| 4 | +import { SizeLimitStream } from './streams/size-limit.js'; |
| 5 | + |
| 6 | +export type ParsedJsonResponse<T = unknown> = { |
| 7 | + response: Response; |
| 8 | + json: T; |
| 9 | +}; |
| 10 | + |
| 11 | +export const isResponseOk = async (response: Response): Promise<Response> => { |
| 12 | + if (response.ok) { |
| 13 | + return response; |
| 14 | + } |
| 15 | + |
| 16 | + if (response.body) { |
| 17 | + await response.body.cancel(); |
| 18 | + } |
| 19 | + |
| 20 | + throw new err.FailedResponseError(response.status, `got http ${response.status}`); |
| 21 | +}; |
| 22 | + |
| 23 | +export const parseResponseAsJson = |
| 24 | + (typeRegex: RegExp, maxSize: number) => |
| 25 | + async (response: Response): Promise<ParsedJsonResponse> => { |
| 26 | + assertContentType(response, typeRegex); |
| 27 | + |
| 28 | + const text = await readResponseAsString(response, maxSize); |
| 29 | + |
| 30 | + try { |
| 31 | + return JSON.parse(text); |
| 32 | + } catch (error) { |
| 33 | + throw new err.ImproperJsonResponseError(`response json invalid`, { cause: error }); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | +export const validateJsonWith = |
| 38 | + <T>(schema: v.Type<T>) => |
| 39 | + async (parsed: ParsedJsonResponse): Promise<ParsedJsonResponse<T>> => { |
| 40 | + const json = schema.parse(parsed.json); |
| 41 | + return { response: parsed.response, json }; |
| 42 | + }; |
| 43 | + |
| 44 | +const assertContentType = async (response: Response, typeRegex: RegExp): Promise<void> => { |
| 45 | + const type = response.headers.get('content-type')?.split(';', 1)[0].trim(); |
| 46 | + |
| 47 | + if (type === undefined) { |
| 48 | + if (response.body) { |
| 49 | + await response.body.cancel(); |
| 50 | + } |
| 51 | + |
| 52 | + throw new err.ImproperContentTypeError(null, `missing response content-type`); |
| 53 | + } |
| 54 | + |
| 55 | + if (!typeRegex.test(type)) { |
| 56 | + if (response.body) { |
| 57 | + await response.body.cancel(); |
| 58 | + } |
| 59 | + |
| 60 | + throw new err.ImproperContentTypeError(type, `unexpected response content-type`); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +const readResponseAsString = async (response: Response, maxSize: number): Promise<string> => { |
| 65 | + const rawSize = response.headers.get('content-length'); |
| 66 | + if (rawSize !== null) { |
| 67 | + const size = Number(rawSize); |
| 68 | + |
| 69 | + if (!Number.isSafeInteger(size) || size <= 0) { |
| 70 | + response.body?.cancel(); |
| 71 | + throw new err.ImproperContentLengthError(maxSize, null, `invalid response content-length`); |
| 72 | + } |
| 73 | + |
| 74 | + if (size > maxSize) { |
| 75 | + response.body?.cancel(); |
| 76 | + throw new err.ImproperContentLengthError(maxSize, size, `response content-length too large`); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const stream = response |
| 81 | + .body!.pipeThrough(new SizeLimitStream(maxSize)) |
| 82 | + .pipeThrough(new TextDecoderStream()); |
| 83 | + |
| 84 | + let text = ''; |
| 85 | + for await (const chunk of stream) { |
| 86 | + text += chunk; |
| 87 | + } |
| 88 | + |
| 89 | + return text; |
| 90 | +}; |
0 commit comments