MCPcopy Create free account
hub / github.com/nodejs/undici / constructor

Method constructor

lib/api/api-request.js:12–81  ·  view source on GitHub ↗
(opts, callback)

Source from the content-addressed store, hash-verified

10
11class RequestHandler extends AsyncResource {
12 constructor (opts, callback) {
13 if (!opts || typeof opts !== 'object') {
14 throw new InvalidArgumentError('invalid opts')
15 }
16
17 const { signal, method, opaque, body, onInfo, responseHeaders, highWaterMark } = opts
18
19 try {
20 if (typeof callback !== 'function') {
21 throw new InvalidArgumentError('invalid callback')
22 }
23
24 if (highWaterMark != null && (!Number.isFinite(highWaterMark) || highWaterMark < 0)) {
25 throw new InvalidArgumentError('invalid highWaterMark')
26 }
27
28 if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
29 throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
30 }
31
32 if (method === 'CONNECT') {
33 throw new InvalidArgumentError('invalid method')
34 }
35
36 if (onInfo && typeof onInfo !== 'function') {
37 throw new InvalidArgumentError('invalid onInfo callback')
38 }
39
40 super('UNDICI_REQUEST')
41 } catch (err) {
42 if (util.isStream(body)) {
43 util.destroy(body.on('error', noop), err)
44 }
45 throw err
46 }
47
48 this.method = method
49 this.responseHeaders = responseHeaders || null
50 this.opaque = opaque || null
51 this.callback = callback
52 this.res = null
53 this.abort = null
54 this.body = body
55 this.trailers = {}
56 this.context = null
57 this.controller = null
58 this.onInfo = onInfo || null
59 this.highWaterMark = highWaterMark
60 this.reason = null
61 this.removeAbortListener = null
62
63 if (signal?.aborted) {
64 this.reason = signal.reason ?? new RequestAbortedError()
65 } else if (signal) {
66 this.removeAbortListener = util.addAbortListener(signal, () => {
67 this.reason = signal.reason ?? new RequestAbortedError()
68 if (this.res) {
69 // Null the reference before destroying, mirroring onResponseError, so

Callers

nothing calls this directly

Calls 3

abortMethod · 0.65
destroyMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected