-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhttp.ts
310 lines (264 loc) · 9.09 KB
/
http.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* eslint-disable no-undef */
import { logger } from '@libp2p/logger'
import { anySignal } from 'any-signal'
import browserReableStreamToIt from 'browser-readablestream-to-it'
import { URL, URLSearchParams } from 'iso-url'
import all from 'it-all'
import mergeOpts from 'merge-options'
import { isBrowser, isWebWorker } from 'wherearewe'
import { TimeoutError, HTTPError } from './http/error.js'
import { fetch, Request, Headers } from './http/fetch.js'
import type { UploadProgressFn } from '../index.js'
import type { Readable } from 'node:stream'
const merge = mergeOpts.bind({ ignoreUndefined: true })
const log = logger('kubo-rpc-client:fetch')
type Override<T, R> = Omit<T, keyof R> & R
export type FetchOptions = Override<RequestInit, {
/**
* Amount of time until request should timeout in ms.
*/
timeout?: number | string
/**
* URL search param.
*/
searchParams?: URLSearchParams
/**
* Can be passed to track upload progress.
* Note that if this option in passed underlying request will be performed using `XMLHttpRequest` and response will not be streamed.
*/
onUploadProgress?: UploadProgressFn
/**
* Can be passed to track download progress.
*/
onDownloadProgress?: UploadProgressFn
overrideMimeType?: string
}>
export interface HTTPOptions extends FetchOptions {
json?: any
/**
* The base URL to use in case url is a relative URL
*/
base?: string
/**
* Throw not ok responses as Errors
*/
throwHttpErrors?: boolean
/**
* Transform search params
*/
transformSearchParams?(params: URLSearchParams): URLSearchParams
/**
* When iterating the response body, transform each chunk with this function.
*/
transform?(chunk: any): any
/**
* Handle errors
*/
handleError?(rsp: Response): Promise<void>
/**
* Control request creation
*/
agent?: any
}
export interface ExtendedResponse extends Response {
iterator(): AsyncGenerator<Uint8Array, void, undefined>
ndjson(): AsyncGenerator<any, void, undefined>
}
const defaults = {
throwHttpErrors: true,
credentials: 'same-origin'
}
export class HTTP {
static HTTPError = HTTPError
static TimeoutError = TimeoutError
static post = async (resource: string | Request, options?: HTTPOptions): Promise<ExtendedResponse> => new HTTP(options).post(resource, options)
static get = async (resource: string | Request, options?: HTTPOptions): Promise<ExtendedResponse> => new HTTP(options).get(resource, options)
static put = async (resource: string | Request, options?: HTTPOptions): Promise<ExtendedResponse> => new HTTP(options).put(resource, options)
static delete = async (resource: string | Request, options?: HTTPOptions): Promise<ExtendedResponse> => new HTTP(options).delete(resource, options)
static options = async (resource: string | Request, options?: HTTPOptions): Promise<ExtendedResponse> => new HTTP(options).options(resource, options)
public readonly opts: HTTPOptions
constructor (options: HTTPOptions = {}) {
this.opts = merge({}, defaults, options)
}
/**
* Fetch
*/
async fetch (resource: string | Request, options: HTTPOptions = {}): Promise<ExtendedResponse> {
const opts = merge({}, this.opts, options)
const headers = new Headers(opts.headers)
// validate resource type
if (typeof resource !== 'string' && !(resource instanceof URL || resource instanceof Request)) {
throw new TypeError('`resource` must be a string, URL, or Request')
}
// eslint-disable-next-line @typescript-eslint/no-base-to-string
const url = new URL(resource.toString(), opts.base)
const {
searchParams,
transformSearchParams,
json
} = opts
if (searchParams != null) {
if (typeof transformSearchParams === 'function') {
url.search = transformSearchParams(new URLSearchParams(opts.searchParams))
} else {
url.search = new URLSearchParams(opts.searchParams).toString()
}
}
if (json != null) {
opts.body = JSON.stringify(opts.json)
headers.set('content-type', 'application/json')
}
const signals = [opts.signal]
if (opts.timeout != null && isNaN(opts.timeout) && opts.timeout > 0) {
signals.push(AbortSignal.timeout(opts.timeout))
}
const signal = anySignal(signals)
try {
if (globalThis.ReadableStream != null && opts.body instanceof globalThis.ReadableStream && (isBrowser || isWebWorker)) {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1387483
opts.body = new Blob(await all(browserReableStreamToIt<Uint8Array>(opts.body)))
}
log.trace('outgoing headers', opts.headers)
log.trace('%s %s', opts.method, url)
// @ts-expect-error extra properties are added later
const response: ExtendedResponse = await fetch(url.toString(), {
...opts,
signal: opts.signal,
timeout: undefined,
headers,
// https://fetch.spec.whatwg.org/#dom-requestinit-duplex
// https://github.com/whatwg/fetch/issues/1254
duplex: 'half'
})
log('%s %s %d', opts.method, url, response.status)
log.trace('incoming headers', response.headers)
if (!response.ok && opts.throwHttpErrors === true) {
if (opts.handleError != null) {
await opts.handleError(response)
}
throw new HTTPError(response)
}
response.iterator = async function * () {
yield * fromStream(response.body)
}
response.ndjson = async function * () {
for await (const chunk of ndjson(response.iterator())) {
if (options.transform != null) {
yield options.transform(chunk)
} else {
yield chunk
}
}
}
return response
} finally {
signal.clear()
}
}
async post (resource: string | Request, options: HTTPOptions = {}): Promise<ExtendedResponse> {
return this.fetch(resource, { ...options, method: 'POST' })
}
async get (resource: string | Request, options: HTTPOptions = {}): Promise<ExtendedResponse> {
return this.fetch(resource, { ...options, method: 'GET' })
}
async put (resource: string | Request, options: HTTPOptions = {}): Promise<ExtendedResponse> {
return this.fetch(resource, { ...options, method: 'PUT' })
}
async delete (resource: string | Request, options: HTTPOptions = {}): Promise<ExtendedResponse> {
return this.fetch(resource, { ...options, method: 'DELETE' })
}
async options (resource: string | Request, options: HTTPOptions = {}): Promise<ExtendedResponse> {
return this.fetch(resource, { ...options, method: 'OPTIONS' })
}
}
/**
* Parses NDJSON chunks from an iterator
*/
const ndjson = async function * (source: AsyncIterable<Uint8Array>): AsyncIterable<any> {
const decoder = new TextDecoder()
let buf = ''
for await (const chunk of source) {
buf += decoder.decode(chunk, { stream: true })
const lines = buf.split(/\r?\n/)
for (let i = 0; i < lines.length - 1; i++) {
const l = lines[i].trim()
if (l.length > 0) {
yield JSON.parse(l)
}
}
buf = lines[lines.length - 1]
}
buf += decoder.decode()
buf = buf.trim()
if (buf.length !== 0) {
yield JSON.parse(buf)
}
}
/**
* Stream to AsyncIterable
*
* @template TChunk
* @param {ReadableStream<TChunk> | NodeReadableStream | null} source
* @returns {AsyncIterable<TChunk>}
*/
const fromStream = <TChunk> (source: ReadableStream<TChunk> | Readable | null): AsyncIterable<TChunk> => {
if (isAsyncIterable(source)) {
return source
}
// Workaround for https://github.com/node-fetch/node-fetch/issues/766
if (isNodeReadableStream(source)) {
const iter = source[Symbol.asyncIterator]()
return {
[Symbol.asyncIterator] () {
return {
next: iter.next.bind(iter),
return (value: any): any {
source.destroy()
if (typeof iter.return === 'function') {
return iter.return()
}
return Promise.resolve({ done: true, value })
}
}
}
}
}
if (isWebReadableStream(source)) {
const reader = source.getReader()
return (async function * () {
try {
while (true) {
// Read from the stream
const { done, value } = await reader.read()
// Exit if we're done
if (done) return
// Else yield the chunk
if (value != null) {
yield value
}
}
} finally {
reader.releaseLock()
}
})()
}
throw new TypeError('Body can\'t be converted to AsyncIterable')
}
/**
* Check if it's an AsyncIterable
*/
const isAsyncIterable = <TChunk> (value: any): value is AsyncIterable<TChunk> => {
return value !== null && typeof value[Symbol.asyncIterator] === 'function'
}
/**
* Check for web readable stream
*/
const isWebReadableStream = <TChunk> (value: any): value is ReadableStream<TChunk> => {
return value != null && typeof value.getReader === 'function'
}
/**
* Check for node readable stream
*/
const isNodeReadableStream = (value: any): value is Readable =>
Object.prototype.hasOwnProperty.call(value, 'readable') &&
Object.prototype.hasOwnProperty.call(value, 'writable')