-
-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathcurly.ts
More file actions
685 lines (605 loc) · 19.3 KB
/
curly.ts
File metadata and controls
685 lines (605 loc) · 19.3 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/**
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import './moduleSetup'
import { Readable } from 'stream'
import {
CurlOptionName,
CurlOptionCamelCaseMap,
CurlOptionValueType,
} from './generated/CurlOption'
import { HeaderInfo } from './parseHeaders'
import { Curl } from './Curl'
import { Easy } from './Easy'
import { CurlFeature } from './enum/CurlFeature'
import { CurlError } from './CurlError'
import { CurlEasyError } from './CurlEasyError'
import { CurlyMimePart } from './CurlyMimeTypes'
/**
* Object the curly call resolves to.
*
* @public
*/
export interface CurlyResult<ResultData = any> {
/**
* Data will be the body of the requested URL
*/
data: ResultData
/**
* Parsed headers
*
* See {@link HeaderInfo}
*/
headers: HeaderInfo[]
/**
* HTTP Status code for the last request
*/
statusCode: number
}
// This is basically http.METHODS
const methods = [
'acl',
'bind',
'checkout',
'connect',
'copy',
'delete',
'get',
'head',
'link',
'lock',
'm-search',
'merge',
'mkactivity',
'mkcalendar',
'mkcol',
'move',
'notify',
'options',
'patch',
'post',
'propfind',
'proppatch',
'purge',
'put',
'rebind',
'report',
'search',
'source',
'subscribe',
'trace',
'unbind',
'unlink',
'unlock',
'unsubscribe',
] as const
type HttpMethod = (typeof methods)[number]
export type CurlyResponseBodyParser = (
data: Buffer,
header: HeaderInfo[],
) => any
export type CurlyResponseBodyParsersProperty = {
[key: string]: CurlyResponseBodyParser
}
/**
* These are the options accepted by the {@link CurlyFunction | `CurlyFunction`} API.
*
* Most libcurl options are accepted as their specific name, like `PROXY_CAPATH`, or as a camel
* case version of that name, like `proxyCaPath`.
*
* Options specific to the `curly` API are prefixed with `curly`, like `curlyBaseUrl`.
*
* For quick navigation use the sidebar.
*/
export interface CurlyOptions extends CurlOptionValueType {
/**
* Set this to a callback function that should be used as the progress callback.
*
* This is the only reliable way to set the progress callback.
*
* @remarks
*
* This basically calls one of the following methods, depending on if any of the streams feature is being used or not:
* - If using streams: {@link Curl.setStreamProgressCallback | `Curl#setStreamProgressCallback`}
* - else: {@link Curl.setProgressCallback | `Curl#setProgressCallback`}
*/
curlyProgressCallback?: CurlOptionValueType['xferInfoFunction']
/**
* If set to a function this will always be called
* for all requests, ignoring other response body parsers.
*
* This can also be set to `false`, which will disable the response parsing and will make
* the raw `Buffer` of the response to be returned.
*/
curlyResponseBodyParser?: CurlyResponseBodyParser | false
/**
* Add more response body parsers, or overwrite existing ones.
*
* This object is merged with the {@link CurlyFunction.defaultResponseBodyParsers | `curly.defaultResponseBodyParsers`}
*/
curlyResponseBodyParsers?: CurlyResponseBodyParsersProperty
/**
* If set, this value will always prefix the `URL` of the request.
*
* No special handling is done, so make sure you set the url correctly later on.
*/
curlyBaseUrl?: string
/**
* If `true`, `curly` will lower case all headers before returning then.
*
* By default this is `false`.
*/
curlyLowerCaseHeaders?: boolean
/**
* If `true`, `curly` will return the response data as a stream.
*
* The `curly` call will resolve as soon as the stream is available.
*
* When using this option, if an error is thrown in the internal {@link Curl | `Curl`} instance
* after the `curly` call has been resolved (it resolves as soon as the stream is available)
* it will cause the `error` event to be emitted on the stream itself, this way it's possible
* to handle these too, if necessary. The error object will inherit from the {@link CurlError | `CurlError`} class.
*
* Calling `destroy()` on the stream will always cause the `Curl` instance to emit the error event.
* Even if an error argument was not supplied to `stream.destroy()`.
*
* By default this is `false`.
*
* @remarks
*
* Make sure your libcurl version is greater than or equal 7.69.1.
* Versions older than that one are not reliable for streams usage.
*
* This basically enables the {@link CurlFeature.StreamResponse | `CurlFeature.StreamResponse`} feature
* flag in the internal {@link Curl | `Curl`} instance.
*/
curlyStreamResponse?: boolean
/**
* This will set the `highWaterMark` option in the response stream, if `curlyStreamResponse` is `true`.
*
* @remarks
*
* This basically calls {@link Curl.setStreamResponseHighWaterMark | `Curl#setStreamResponseHighWaterMark`}
* method in the internal {@link Curl | `Curl`} instance.
*/
curlyStreamResponseHighWaterMark?: number
/**
* If set, the contents of this stream will be uploaded to the server.
*
* Keep in mind that if you set this option you **SHOULD** not set
* `progressFunction` or `xferInfoFunction`, as these are used internally.
*
* If you need to set a progress callback, use the `curlyProgressCallback` option.
*
* If the stream set here is destroyed before libcurl finishes uploading it, the error
* `Curl upload stream was unexpectedly destroyed` (Code `42`) will be emitted in the
* internal {@link Curl | `Curl`} instance, and so will cause the curly call to be rejected with that error.
*
* If the stream was destroyed with a specific error, this error will be passed instead.
*
* By default this is not set.
*
* @remarks
*
* Make sure your libcurl version is greater than or equal 7.69.1.
* Versions older than that one are not reliable for streams usage.
*
* This basically calls {@link Curl.setUploadStream | `Curl#setUploadStream`}
* method in the internal {@link Curl | `Curl`} instance.
*/
curlyStreamUpload?: Readable | null
/**
* Array of MIME parts to upload as multipart/form-data.
*
* This will automatically build a {@link CurlMime} structure internally and set
* it using the `MIMEPOST` option. For stream-based parts, the unpause callback
* is automatically generated, so you don't need to provide it.
*
* @remarks
*
* Requires libcurl 7.56.0 or later.
*
* @example
* Basic multipart upload:
* ```typescript
* await curly.post('https://httpbin.org/post', {
* curlyMimePost: [
* { type: 'data', name: 'username', data: 'john_doe' },
* { type: 'file', name: 'avatar', file: '/path/to/image.png', mimeType: 'image/png' }
* ]
* })
* ```
*
* @example
* With streams:
* ```typescript
* import { createReadStream } from 'fs'
*
* await curly.post('https://httpbin.org/post', {
* curlyMimePost: [
* { type: 'data', name: 'field', data: 'value' },
* {
* type: 'stream',
* name: 'document',
* stream: createReadStream('/path/to/file.txt'),
* size: 12345
* }
* ]
* })
* ```
*
* See {@link Easy.setMimePost | `Easy.setMimePost`} for more details.
*/
curlyMimePost?: CurlyMimePart[]
}
export interface CurlyHttpMethodCall {
/**
* **EXPERIMENTAL** This API can change between minor releases
*
* Async wrapper around the Curl class.
*
* The `curly.<field>` being used will be the HTTP verb sent.
*
* @typeParam ResultData You can use this to specify the type of the `data` property returned from this call.
*/
<ResultData = any>(
url: string,
options?: CurlyOptions,
): Promise<CurlyResult<ResultData>>
}
// type HttpMethodCalls = { readonly [K in HttpMethod]: CurlyHttpMethodCall }
type HttpMethodCalls = Record<HttpMethod, CurlyHttpMethodCall>
/** @expand */
export interface CurlyFunction extends HttpMethodCalls {
/**
* **EXPERIMENTAL** This API can change between minor releases
*
* Async wrapper around the Curl class.
*
* It's also possible to request using a specific http verb
* directly by using `curl.<http-verb>(url: string, options?: CurlyOptions)`, like:
*
* ```js
* curly.get('https://www.google.com')
* ```
* @typeParam ResultData You can use this to specify the type of the `data` property returned from this call.
*/
<ResultData = any>(
url: string,
options?: CurlyOptions,
): Promise<CurlyResult<ResultData>>
/**
* **EXPERIMENTAL** This API can change between minor releases
*
* This returns a new `curly` with the specified options set by default.
*/
create: (defaultOptions?: CurlyOptions) => CurlyFunction
/**
* These are the default response body parsers to be used.
*
* By default there are parsers for the following:
*
* - application/json
* - text/*
* - *
*/
defaultResponseBodyParsers: CurlyResponseBodyParsersProperty
/**
* Set the object pool limit for Curl instances.
*
* When set to 0 (default), pooling is disabled and Curl instances are created/destroyed on each request.
* When set to a positive number, that many Curl instances will be pre-created and reused.
*
* @param limit - Number of Curl instances to keep in the pool. 0 disables pooling.
*/
setObjectPoolLimit: (limit: number) => void
}
const create = (defaultOptions: CurlyOptions = {}): CurlyFunction => {
// Object pool for Curl instances
let poolLimit = 0
const pool: Curl[] = []
const getFromPool = (): Curl => {
if (poolLimit === 0) {
// Pooling disabled, create new instance
return new Curl()
}
if (pool.length > 0) {
// Get from pool
return pool.pop()!
}
// Pool empty, create new instance
return new Curl()
}
const returnToPool = (curlHandle: Curl): void => {
if (poolLimit === 0) {
// Pooling disabled, close the handle
curlHandle.close()
return
}
if (pool.length < poolLimit) {
// Reset the handle for reuse
curlHandle.reset()
pool.push(curlHandle)
} else {
// Pool full, close the handle
curlHandle.close()
}
}
const setObjectPoolLimit = (limit: number): void => {
poolLimit = limit
if (limit <= 0) {
for (const handle of pool) {
handle.close()
}
pool.length = 0
} else {
// If reducing limit, close excess instances
while (pool.length > limit) {
const handle = pool.pop()!
handle.close()
}
}
}
function curly<ResultData>(
url: string,
options: CurlyOptions = {},
): Promise<CurlyResult<ResultData>> {
const curlHandle = getFromPool()
curlHandle.enable(CurlFeature.NoDataParsing)
curlHandle.setOpt('URL', `${options.curlyBaseUrl || ''}${url}`)
const finalOptions = {
...defaultOptions,
...options,
}
for (const key of Object.keys(finalOptions)) {
const keyTyped = key as keyof CurlyOptions
const optionName: CurlOptionName =
keyTyped in CurlOptionCamelCaseMap
? CurlOptionCamelCaseMap[
keyTyped as keyof typeof CurlOptionCamelCaseMap
]
: (keyTyped as CurlOptionName)
// if it begins with curly we do not set it on the curlHandle
// as it's an specific option for curly
if (optionName.startsWith('curly')) continue
// @ts-ignore @TODO Try to type this
curlHandle.setOpt(optionName, finalOptions[key])
}
// streams!
const {
curlyStreamResponse,
curlyStreamResponseHighWaterMark,
curlyStreamUpload,
curlyMimePost,
} = finalOptions
const isUsingStream = !!(curlyStreamResponse || curlyStreamUpload)
if (finalOptions.curlyProgressCallback) {
if (typeof finalOptions.curlyProgressCallback !== 'function') {
throw new TypeError(
'curlyProgressCallback must be a function with signature (number, number, number, number) => number',
)
}
const fnToCall = isUsingStream
? 'setStreamProgressCallback'
: 'setProgressCallback'
curlHandle[fnToCall](finalOptions.curlyProgressCallback)
}
if (curlyStreamResponse) {
curlHandle.enable(CurlFeature.StreamResponse)
if (curlyStreamResponseHighWaterMark) {
curlHandle.setStreamResponseHighWaterMark(
curlyStreamResponseHighWaterMark,
)
}
}
if (curlyStreamUpload) {
curlHandle.setUploadStream(curlyStreamUpload)
}
if (curlyMimePost && curlyMimePost.length > 0) {
curlHandle.setMimePost(curlyMimePost)
}
const lowerCaseHeadersIfNecessary = (headers: HeaderInfo[]) => {
// in-place modification
// yeah, I know mutability is bad and all that
if (finalOptions.curlyLowerCaseHeaders) {
for (const headersReq of headers) {
const entries = Object.entries(headersReq)
for (const [headerKey, headerValue] of entries) {
delete headersReq[headerKey]
// @ts-expect-error ignoring this for now
headersReq[headerKey.toLowerCase()] = headerValue
}
}
}
}
return new Promise((resolve, reject) => {
let stream: Readable
if (curlyStreamResponse) {
curlHandle.on(
'stream',
(_stream, statusCode, headers: HeaderInfo[]) => {
lowerCaseHeadersIfNecessary(headers)
stream = _stream
resolve({
// @ts-ignore cannot be subtype yada yada
data: stream,
statusCode,
headers,
})
},
)
}
curlHandle.on(
'end',
(statusCode, data: Buffer, headers: HeaderInfo[]) => {
returnToPool(curlHandle)
// only need to the remaining here if we did not enabled
// the stream response
if (curlyStreamResponse) {
return
}
const contentTypeEntry =
headers.length > 0
? Object.entries(headers[headers.length - 1]).find(
([k]) => k.toLowerCase() === 'content-type',
)
: null
let contentType = (
contentTypeEntry ? contentTypeEntry[1] : ''
) as string
// remove the metadata of the content-type, like charset
// See https://tools.ietf.org/html/rfc7231#section-3.1.1.5
contentType = contentType.split(';')[0]
const responseBodyParsers = {
...curly.defaultResponseBodyParsers,
...finalOptions.curlyResponseBodyParsers,
}
let foundParser = finalOptions.curlyResponseBodyParser
if (typeof foundParser === 'undefined') {
for (const [contentTypeFormat, parser] of Object.entries(
responseBodyParsers,
)) {
if (typeof parser !== 'function') {
return reject(
new TypeError(
`Response body parser for ${contentTypeFormat} must be a function`,
),
)
}
if (contentType === contentTypeFormat) {
foundParser = parser
break
} else if (contentTypeFormat === '*') {
foundParser = parser
break
} else {
const partsFormat = contentTypeFormat.split('/')
const partsContentType = contentType.split('/')
if (
partsContentType.length === partsFormat.length &&
partsContentType.every(
(val, index) =>
partsFormat[index] === '*' || partsFormat[index] === val,
)
) {
foundParser = parser
break
}
}
}
}
if (foundParser && typeof foundParser !== 'function') {
return reject(
new TypeError(
'`curlyResponseBodyParser` passed to curly must be false or a function.',
),
)
}
lowerCaseHeadersIfNecessary(headers)
try {
resolve({
statusCode: statusCode,
data: foundParser ? foundParser(data, headers) : data,
headers: headers,
})
} catch (error) {
reject(error)
}
},
)
curlHandle.on('error', (error, errorCode) => {
returnToPool(curlHandle)
const errorToUse =
error instanceof CurlError
? error
: new CurlEasyError(error.message, errorCode, { cause: error })
// oops, if have a stream it means the promise
// has been resolved with it
// so instead of rejecting the original promise
// we are emitting the error event on the stream
if (stream) {
stream.emit('error', errorToUse)
} else {
reject(errorToUse)
}
})
try {
curlHandle.perform()
} catch (error) /* istanbul ignore next: this should never happen 🤷♂️ */ {
returnToPool(curlHandle)
reject(error)
}
})
}
curly.create = create
curly.setObjectPoolLimit = setObjectPoolLimit
curly.defaultResponseBodyParsers = {
'application/json': (data, _headers) => {
try {
const string = data.toString('utf8')
return JSON.parse(string)
} catch {
throw new Error(
`curly failed to parse "application/json" content as JSON. This is generally caused by receiving malformed JSON data from the server.
You can disable this automatic behavior by setting the option curlyResponseBodyParser to false, then a Buffer will be returned as the data.
You can also overwrite the "application/json" parser with your own by changing one of the following:
- curly.defaultResponseBodyParsers['application/json']
or
- options.curlyResponseBodyParsers = { 'application/json': parser }
If you want just a single function to handle all content-types, you can use the option "curlyResponseBodyParser".
`,
)
}
},
// We are in [INSERT CURRENT YEAR], let's assume everyone is using utf8 encoding for text/* content-type.
'text/*': (data, _headers) => data.toString('utf8'),
// otherwise let's just return the raw buffer
'*': (data, _headers) => data,
} as CurlyResponseBodyParsersProperty
const httpMethodOptionsMap: Record<
string,
null | ((m: string, o: CurlyOptions) => CurlyOptions)
> = {
get: null,
post: (_m, o) => ({
post: true,
...o,
}),
head: (_m, o) => ({
nobody: true,
...o,
}),
_: (m, o) => ({
customRequest: m.toUpperCase(),
...o,
}),
}
for (const httpMethod of methods) {
const httpMethodOptionsKey = Object.prototype.hasOwnProperty.call(
httpMethodOptionsMap,
httpMethod,
)
? httpMethod
: '_'
const httpMethodOptions = httpMethodOptionsMap[httpMethodOptionsKey]
// @ts-ignore
curly[httpMethod] =
httpMethodOptions === null
? curly
: (url: string, options: CurlyOptions = {}) =>
curly(url, {
...httpMethodOptions(httpMethod, options),
})
}
// @ts-ignore
return curly
}
/**
* Curly function
*
* @public
*/
export const curly = create()