@nxtedition/nxt-undici 4.2.25 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/request.js ADDED
@@ -0,0 +1,176 @@
1
+ import assert from 'node:assert'
2
+ import { InvalidArgumentError, RequestAbortedError } from './errors.js'
3
+ import { isStream, parseHeaders } from './utils.js'
4
+ import { BodyReadable as Readable } from './readable.js'
5
+
6
+ function noop() {}
7
+
8
+ export class RequestHandler {
9
+ constructor(opts, callback) {
10
+ if (!opts || typeof opts !== 'object') {
11
+ throw new InvalidArgumentError('invalid opts')
12
+ }
13
+
14
+ const { signal, method, body, highWaterMark } = opts
15
+
16
+ try {
17
+ if (typeof callback !== 'function') {
18
+ throw new InvalidArgumentError('invalid callback')
19
+ }
20
+
21
+ if (highWaterMark && (typeof highWaterMark !== 'number' || highWaterMark < 0)) {
22
+ throw new InvalidArgumentError('invalid highWaterMark')
23
+ }
24
+
25
+ if (
26
+ signal &&
27
+ typeof signal.on !== 'function' &&
28
+ typeof signal.addEventListener !== 'function'
29
+ ) {
30
+ throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
31
+ }
32
+
33
+ if (method === 'CONNECT') {
34
+ throw new InvalidArgumentError('invalid method')
35
+ }
36
+ } catch (err) {
37
+ if (isStream(body)) {
38
+ body.on('error', noop).destroy(err)
39
+ }
40
+ throw err
41
+ }
42
+
43
+ this.method = method
44
+ this.callback = callback
45
+ this.res = null
46
+ this.abort = null
47
+ this.body = body
48
+ this.context = null
49
+ this.highWaterMark = highWaterMark
50
+ this.reason = null
51
+
52
+ if (signal?.aborted) {
53
+ this.reason = signal.reason ?? new RequestAbortedError()
54
+ } else if (signal) {
55
+ const onAbort = () => {
56
+ this.reason = signal.reason ?? new RequestAbortedError()
57
+ if (this.res) {
58
+ this.res.on('error', noop).destroy(this.reason)
59
+ } else if (this.abort) {
60
+ this.abort(this.reason)
61
+ }
62
+ }
63
+ signal.addEventListener('abort', onAbort)
64
+ this.removeAbortListener = () => signal.removeEventListener('abort', onAbort)
65
+ }
66
+ }
67
+
68
+ onConnect(abort) {
69
+ if (this.reason) {
70
+ abort(this.reason)
71
+ return
72
+ }
73
+
74
+ assert(this.callback)
75
+
76
+ this.abort = abort
77
+ }
78
+
79
+ onHeaders(statusCode, rawHeaders, resume, headers = parseHeaders(rawHeaders)) {
80
+ const { callback, abort, highWaterMark } = this
81
+
82
+ if (statusCode < 200) {
83
+ return true
84
+ }
85
+
86
+ const contentType = headers['content-type']
87
+ const contentLength = headers['content-length']
88
+ const res = new Readable({
89
+ resume,
90
+ abort,
91
+ contentType: typeof contentType === 'string' ? contentType : undefined,
92
+ contentLength: this.method !== 'HEAD' && contentLength ? Number(contentLength) : undefined,
93
+ highWaterMark,
94
+ })
95
+
96
+ if (this.removeAbortListener) {
97
+ res.on('close', this.removeAbortListener)
98
+ this.removeAbortListener = undefined
99
+ }
100
+
101
+ this.callback = null
102
+ this.res = res
103
+
104
+ if (callback !== null) {
105
+ callback(null, { statusCode, headers, body: res })
106
+ }
107
+ }
108
+
109
+ onData(chunk) {
110
+ return this.res?.push(chunk)
111
+ }
112
+
113
+ onComplete() {
114
+ this.res?.push(null)
115
+ }
116
+
117
+ onError(err) {
118
+ const { res, callback, body } = this
119
+
120
+ if (callback) {
121
+ // TODO: Does this need queueMicrotask?
122
+ this.callback = null
123
+ queueMicrotask(() => {
124
+ callback(err)
125
+ })
126
+ }
127
+
128
+ if (res) {
129
+ this.res = null
130
+ // Ensure all queued handlers are invoked before destroying res.
131
+ queueMicrotask(() => {
132
+ res.on('error', noop).destroy(err)
133
+ })
134
+ }
135
+
136
+ if (body) {
137
+ this.body = null
138
+
139
+ if (isStream(body)) {
140
+ body.on('error', noop).destroy(err)
141
+ }
142
+ }
143
+
144
+ if (this.removeAbortListener) {
145
+ this.removeAbortListener()
146
+ this.removeAbortListener = undefined
147
+ }
148
+ }
149
+ }
150
+
151
+ export function request(dispatch, url, opts) {
152
+ return new Promise((resolve, reject) => {
153
+ if (typeof url === 'string') {
154
+ opts = { url: new URL(url), ...opts }
155
+ } else if (url instanceof URL) {
156
+ opts = { url, ...opts }
157
+ } else if (typeof url.origin === 'string' && typeof (url.path ?? url.pathname) === 'string') {
158
+ opts = opts ? { ...url, ...opts } : url
159
+ }
160
+
161
+ if (opts == null && typeof url === 'object' && url != null) {
162
+ opts = url
163
+ }
164
+
165
+ dispatch(
166
+ opts,
167
+ new RequestHandler(opts, (err, res) => {
168
+ if (err) {
169
+ reject(err)
170
+ } else {
171
+ resolve(res)
172
+ }
173
+ }),
174
+ )
175
+ })
176
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "4.2.25",
3
+ "version": "5.0.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",