-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfetch.js
More file actions
221 lines (192 loc) · 8.52 KB
/
Copy pathfetch.js
File metadata and controls
221 lines (192 loc) · 8.52 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { isPlainObject, prettyJSON, keySortedJSON, serializeBody, deserializeBody } from './util.js'
let haveHeaders
function makeHeaders(headers) {
// Lazy to allow polyfilling
if (haveHeaders === undefined) {
haveHeaders = false
if (typeof Headers === 'function') {
try {
// We don't want non-iterable headers, see e.g. https://github.com/boa-dev/boa/issues/4611
haveHeaders = [...new Headers([['a', 'b']])].length === 1
} catch {}
}
}
return haveHeaders ? new Headers(headers) : [...headers]
}
function serializeHeaders(headers) {
if (!headers || Array.isArray(headers)) return headers
if (isPlainObject(headers)) return Object.entries(headers)
return [...headers]
}
const sortHeaders = (headers) => {
if (!headers) return headers
const clone = [...headers]
return headers.sort((a, b) => {
if (a[0] < b[0]) return -1
if (a[0] > b[0]) return 1
return clone.indexOf(a) - clone.indexOf(b)
})
}
const serializeRequest = async (resource, options = {}) => {
const serializable = Object.entries(options).filter(([key, value]) => {
if (key === 'body' || key === 'headers') return false // included directly
if (key === 'trailers') throw new Error('Trailers not supported in this version') // not in spec (yet?)
if (key === 'signal') return false // ignored
if (!value || ['string', 'number', 'boolean'].includes(typeof value)) return true
throw new Error(`Can not process option ${key} with value type ${typeof value}`)
})
return {
resource: `${resource}`,
options: {
...Object.fromEntries(serializable),
body: await serializeBody(options.body),
headers: sortHeaders(serializeHeaders(options.headers)),
},
}
}
function maybeText(response) {
// Doesn't have to be strong, this just avoids attempting to .text() on certain response types
try {
const type = response.headers.get('content-type').trim().split(';')[0]
if (type.startsWith('image/') && !type.endsWith('+xml')) return false
if (type.startsWith('audio/') || type.startsWith('video/')) return false
} catch {}
return true
}
async function serializeResponseBody(response) {
try {
if (response.headers.get('content-type').trim().split(';')[0] === 'application/json') {
return { bodyType: 'json', body: await response.clone().json() }
}
} catch {}
if (maybeText(response)) {
const text = await response.clone().text()
if (!text.includes('\uFFFD')) return { bodyType: 'text', body: text }
}
const buffer = await response.clone().arrayBuffer()
return { bodyType: 'binary', body: await serializeBody(buffer) }
}
function deserializeResponseBody(body, bodyType, toJSON = (arg) => JSON.stringify(arg)) {
if (bodyType === 'text') return body
if (bodyType === 'json') return toJSON(body) // need to re-encode it as clone() exists
if (bodyType === 'binary' && body?.type === 'ArrayBuffer') return deserializeBody(body)
throw new Error('Unexpected bodyType in fetch recording log')
}
async function serializeResponse(resource, options = {}, response) {
if (!['default', 'basic'].includes(response.type)) {
throw new Error(`Can not record fetch response, unexpected type: ${response.type}`)
}
return {
request: await serializeRequest(resource, options),
status: response.status,
statusText: response.statusText,
ok: response.ok,
headers: [...response.headers],
url: response.url,
redirected: response.redirected,
type: response.type,
...(await serializeResponseBody(response)),
}
}
async function serializeError(resource, options = {}, error) {
return {
request: await serializeRequest(resource, options),
error: { message: error.message, code: error.code },
}
}
function makeResponseBase(bodyType, body, init) {
if (bodyType === 'json' && Response.json) return Response.json(body, init)
if (bodyType === 'text' && Response.text) return Response.text(body, init)
if (bodyType === 'json') return new Response(prettyJSON(body), init)
if (bodyType === 'text') return new Response(body, init)
if (bodyType === 'binary' && body?.type === 'ArrayBuffer') {
return new Response(deserializeBody(body), init)
}
throw new Error('Unexpected bodyType')
}
function makeResponse({ bodyType, body }, headers, { status, statusText, ok, ...extra }) {
// init supports only { status, statusText, headers } per spec, we have to restore the rest manually
const response = makeResponseBase(bodyType, body, { status, statusText, headers })
if (response.ok !== ok) throw new Error('Unexpected: ok mismatch')
if (Object.hasOwn(extra, 'trailers')) throw new Error('Trailers not supported in this version')
// We have { url, redirected, type } to set here
const wrap = ([name, value]) => [name, { get: () => value, enumerable: true }]
Object.defineProperties(response, Object.fromEntries(Object.entries(extra).map((el) => wrap(el))))
return response
}
export function fetchRecorder(log, { fetch: _fetch = globalThis.fetch?.bind?.(globalThis) } = {}) {
if (!Array.isArray(log)) throw new Error('log should be passed')
if (!_fetch) throw new Error('No fetch implementation passed, no global fetch exists')
return async function fetch(resource, options) {
try {
const res = await _fetch(resource, options)
log.push(await serializeResponse(resource, options, res))
return res
} catch (err) {
log.push(await serializeError(resource, options, err))
throw err
}
}
}
export function fetchReplayer(log) {
if (!Array.isArray(log)) throw new Error('log should be passed')
log = log.map((entry) => ({ _request: keySortedJSON(entry.request), ...entry })) // cloned as we mutate it
return fetchApi(async (resource, options) => {
const request = keySortedJSON(await serializeRequest(resource, options))
const id = log.findIndex((entry) => entry._request === request)
if (id < 0) throw new Error(`Request to ${resource} not found, ${log.length} more entries left`)
const [entry] = log.splice(id, 1)
return entry
})
}
export const fetchApi = (lookup, fallback) =>
async function fetch(resource, options = {}) {
const entry = await lookup(resource, options)
if (!entry) {
if (!fallback) throw new Error(`Request to ${resource} not found`)
return fallback(resource, options)
}
const { status, statusText, ok, url, redirected, type, headers = [], body, bodyType } = entry
if (entry.error) {
const { message, ...rest } = entry.error
throw new TypeError(message, rest)
}
const props = { status, statusText, ok, url, redirected, type }
try {
// Try to return a native Response
if (typeof Response !== 'undefined') return makeResponse({ body, bodyType }, headers, props)
} catch {} // passthrough and return a plain object
const bufferToAB = (buf) => buf.buffer.slice(0, buf.byteOffset, buf.byteOffset + buf.byteLength)
const getHeaders = () => makeHeaders(headers)
const cType = () => getHeaders().get?.('content-type') ?? new Map(headers).get('content-type')
const c = [undefined, undefined] // 0 is non pretty-printed, 1 is pretty-printed in case of json
const raw = () => c[0] ?? c[1] ?? (c[0] = deserializeResponseBody(body, bodyType))
const pretty = () => c[1] ?? (c[1] = deserializeResponseBody(body, bodyType, prettyJSON))
const data = bodyType === 'json' ? pretty : raw
const getText = (arg) => (bodyType === 'binary' ? Buffer.from(arg).toString('utf8') : arg)
const getAB = () => (bodyType === 'binary' ? data().slice(0) : bufferToAB(Buffer.from(data()))) // eslint-disable-line unicorn/prefer-spread
const fallbackResponse = () => ({
...props,
text: async () => getText(data()),
json: async () => JSON.parse(getText(raw())),
arrayBuffer: async () => getAB(),
blob: async () => new Blob([data()], { type: cType() }),
headers: getHeaders(),
clone: () => fallbackResponse(),
get body() {
// Try to use a native ReadableStream
if (typeof ReadableStream !== 'undefined') {
return ReadableStream.from(new Uint8Array(getAB()))
}
// Some clients do this: if ('locked' in stream) return response.json()
// Or: if (typeof stream.getReader === 'function') return response.json()/.text()
// Else they attempt to concat chunks from stream
// So we trigger browser detection to make those clients use .json()/.text() instead
const getReader = () => {
throw new Error('getReader() not supported')
}
return { locked: false, getReader }
},
})
return fallbackResponse()
}