|
6 | 6 |
|
7 | 7 | /// RequestInit
|
8 | 8 | export interface TransferableRequestInit {
|
9 |
| - /** A Base64 string or null to set request's body. */ |
10 |
| - body?: string | null; |
11 |
| - /** A string indicating how the request will interact with the browser's cache to set request's cache. */ |
12 |
| - cache?: RequestCache; |
13 |
| - /** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */ |
14 |
| - credentials?: RequestCredentials; |
15 |
| - /** A Headers object, an object literal, or an array of two-item arrays to set request's headers. */ |
16 |
| - headers?: [string, string][] | Record<string, string>; |
17 |
| - /** A cryptographic hash of the resource to be fetched by request. Sets request's integrity. */ |
18 |
| - integrity?: string; |
19 |
| - /** A boolean to set request's keepalive. */ |
20 |
| - keepalive?: boolean; |
21 |
| - /** A string to set request's method. */ |
22 |
| - method?: string; |
23 |
| - /** A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. */ |
24 |
| - mode?: RequestMode; |
25 |
| - /** A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. */ |
26 |
| - redirect?: RequestRedirect; |
27 |
| - /** A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. */ |
28 |
| - referrer?: string; |
29 |
| - /** A referrer policy to set request's referrerPolicy. */ |
30 |
| - referrerPolicy?: ReferrerPolicy; |
| 9 | + /** A Base64 string or null to set request's body. */ |
| 10 | + body?: string | null |
| 11 | + /** A string indicating how the request will interact with the browser's cache to set request's cache. */ |
| 12 | + cache?: RequestCache |
| 13 | + /** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */ |
| 14 | + credentials?: RequestCredentials |
| 15 | + /** A Headers object, an object literal, or an array of two-item arrays to set request's headers. */ |
| 16 | + headers?: [string, string][] | Record<string, string> |
| 17 | + /** A cryptographic hash of the resource to be fetched by request. Sets request's integrity. */ |
| 18 | + integrity?: string |
| 19 | + /** A boolean to set request's keepalive. */ |
| 20 | + keepalive?: boolean |
| 21 | + /** A string to set request's method. */ |
| 22 | + method?: string |
| 23 | + /** A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. */ |
| 24 | + mode?: RequestMode |
| 25 | + /** A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. */ |
| 26 | + redirect?: RequestRedirect |
| 27 | + /** A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. */ |
| 28 | + referrer?: string |
| 29 | + /** A referrer policy to set request's referrerPolicy. */ |
| 30 | + referrerPolicy?: ReferrerPolicy |
31 | 31 | }
|
32 | 32 |
|
33 | 33 | export const transferableRequestInitToRequestInit = (
|
34 |
| - transferableRequest: TransferableRequestInit, |
| 34 | + transferableRequest: TransferableRequestInit, |
35 | 35 | ): RequestInit => ({
|
36 |
| - body: |
37 |
| - transferableRequest.body && stringToArrayBuffer(transferableRequest.body), |
38 |
| - cache: transferableRequest.cache, |
39 |
| - credentials: transferableRequest.credentials, |
40 |
| - headers: new Headers(transferableRequest.headers), |
41 |
| - integrity: transferableRequest.integrity, |
42 |
| - method: transferableRequest.method, |
43 |
| - mode: transferableRequest.mode, |
44 |
| - redirect: transferableRequest.redirect, |
45 |
| - referrer: transferableRequest.referrer, |
46 |
| - referrerPolicy: transferableRequest.referrerPolicy, |
47 |
| -}); |
| 36 | + body: transferableRequest.body && stringToArrayBuffer(transferableRequest.body), |
| 37 | + cache: transferableRequest.cache, |
| 38 | + credentials: transferableRequest.credentials, |
| 39 | + headers: new Headers(transferableRequest.headers), |
| 40 | + integrity: transferableRequest.integrity, |
| 41 | + method: transferableRequest.method, |
| 42 | + mode: transferableRequest.mode, |
| 43 | + redirect: transferableRequest.redirect, |
| 44 | + referrer: transferableRequest.referrer, |
| 45 | + referrerPolicy: transferableRequest.referrerPolicy, |
| 46 | +}) |
48 | 47 |
|
49 | 48 | export const requestInitToTransferableRequestInit = async (
|
50 |
| - requestInit: RequestInit, |
| 49 | + requestInit: RequestInit, |
51 | 50 | ): Promise<TransferableRequestInit> => ({
|
52 |
| - ...requestInit, |
53 |
| - body: requestInit.body && (await new Response(requestInit.body).text()), |
54 |
| - // @ts-expect-error Valid but Typescript doesn't recognize it |
55 |
| - headers: |
56 |
| - requestInit.headers instanceof Headers |
57 |
| - ? requestInit.headers.entries() |
58 |
| - : requestInit.headers, |
59 |
| -}); |
| 51 | + ...requestInit, |
| 52 | + body: requestInit.body && (await new Response(requestInit.body).text()), |
| 53 | + // @ts-expect-error Valid but Typescript doesn't recognize it |
| 54 | + headers: requestInit.headers instanceof Headers ? requestInit.headers.entries() : requestInit.headers, |
| 55 | +}) |
60 | 56 |
|
61 | 57 | /// Response
|
62 | 58 | type TransferableResponse = {
|
63 |
| - /** Base64 encoded */ |
64 |
| - body: null | string; |
65 |
| - headers: Record<string, string>; |
66 |
| - status: number; |
67 |
| - statusText: string; |
68 |
| -}; |
69 |
| -export const responseToTransferableResponse = async ( |
70 |
| - response: Response, |
71 |
| -): Promise<TransferableResponse> => ({ |
72 |
| - body: response.body && (await response.text()), |
73 |
| - headers: headersToObject(response.headers), |
74 |
| - status: response.status, |
75 |
| - statusText: response.statusText, |
76 |
| -}); |
| 59 | + /** Base64 encoded */ |
| 60 | + body: null | string |
| 61 | + headers: Record<string, string> |
| 62 | + status: number |
| 63 | + statusText: string |
| 64 | +} |
| 65 | +export const responseToTransferableResponse = async (response: Response): Promise<TransferableResponse> => ({ |
| 66 | + body: response.body && (await response.text()), |
| 67 | + headers: headersToObject(response.headers), |
| 68 | + status: response.status, |
| 69 | + statusText: response.statusText, |
| 70 | +}) |
77 | 71 |
|
78 |
| -export const transferableResponseToResponse = ( |
79 |
| - transferableResponse: TransferableResponse, |
80 |
| -): Response => |
81 |
| - new Response( |
82 |
| - transferableResponse.body && stringToArrayBuffer(transferableResponse.body), |
83 |
| - { |
84 |
| - headers: new Headers(transferableResponse.headers), |
85 |
| - status: transferableResponse.status, |
86 |
| - statusText: transferableResponse.statusText, |
87 |
| - }, |
88 |
| - ); |
| 72 | +export const transferableResponseToResponse = (transferableResponse: TransferableResponse): Response => |
| 73 | + new Response( |
| 74 | + [101, 204, 205, 304].includes(transferableResponse.status) |
| 75 | + ? null |
| 76 | + : transferableResponse.body |
| 77 | + ? stringToArrayBuffer(transferableResponse.body) |
| 78 | + : undefined, |
| 79 | + { |
| 80 | + headers: new Headers(transferableResponse.headers), |
| 81 | + status: transferableResponse.status, |
| 82 | + statusText: transferableResponse.statusText, |
| 83 | + }, |
| 84 | + ) |
89 | 85 |
|
90 | 86 | /// Helpers
|
91 | 87 | const stringToArrayBuffer = (str: string) => {
|
92 |
| - const encoder = new TextEncoder(); |
93 |
| - return encoder.encode(str); |
94 |
| -}; |
| 88 | + const encoder = new TextEncoder() |
| 89 | + return encoder.encode(str) |
| 90 | +} |
95 | 91 | const headersToObject = (headers: Headers) => {
|
96 |
| - const headersObject: Record<string, string> = {}; |
97 |
| - // no type-safe way to do it with for of that I know of |
98 |
| - // biome-ignore lint/complexity/noForEach: |
99 |
| - headers.forEach((value, key) => { |
100 |
| - headersObject[key] = value; |
101 |
| - }); |
102 |
| - return headersObject; |
103 |
| -}; |
| 92 | + const headersObject: Record<string, string> = {} |
| 93 | + // no type-safe way to do it with for of that I know of |
| 94 | + // biome-ignore lint/complexity/noForEach: |
| 95 | + headers.forEach((value, key) => { |
| 96 | + headersObject[key] = value |
| 97 | + }) |
| 98 | + return headersObject |
| 99 | +} |
0 commit comments