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

Method constructor

lib/api/api-stream.js:60–115  ·  view source on GitHub ↗
(opts, factory, callback)

Source from the content-addressed store, hash-verified

58
59class StreamHandler extends AsyncResource {
60 constructor (opts, factory, callback) {
61 if (!opts || typeof opts !== 'object') {
62 throw new InvalidArgumentError('invalid opts')
63 }
64
65 const { signal, method, opaque, body, onInfo, responseHeaders } = opts
66
67 try {
68 if (typeof callback !== 'function') {
69 throw new InvalidArgumentError('invalid callback')
70 }
71
72 if (typeof factory !== 'function') {
73 throw new InvalidArgumentError('invalid factory')
74 }
75
76 if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
77 throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
78 }
79
80 if (method === 'CONNECT') {
81 throw new InvalidArgumentError('invalid method')
82 }
83
84 if (onInfo && typeof onInfo !== 'function') {
85 throw new InvalidArgumentError('invalid onInfo callback')
86 }
87
88 super('UNDICI_STREAM')
89 } catch (err) {
90 if (util.isStream(body)) {
91 util.destroy(body.on('error', noop), err)
92 }
93 throw err
94 }
95
96 this.responseHeaders = responseHeaders || null
97 this.opaque = opaque || null
98 this.factory = factory
99 this.callback = callback
100 this.res = null
101 this.abort = null
102 this.context = null
103 this.controller = null
104 this.trailers = null
105 this.body = body
106 this.onInfo = onInfo || null
107
108 if (util.isStream(body)) {
109 body.on('error', (err) => {
110 this.onResponseError(this.controller, err)
111 })
112 }
113
114 addSignal(this, signal)
115 }
116
117 onRequestStart (controller, context) {

Callers

nothing calls this directly

Calls 4

onResponseErrorMethod · 0.95
addSignalFunction · 0.85
destroyMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected