@nxtedition/nxt-undici 2.0.1 → 2.0.3

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/index.js CHANGED
@@ -242,7 +242,7 @@ export async function request(url, opts) {
242
242
  dispatcherCache.set(dispatcher, dispatch)
243
243
  }
244
244
 
245
- return new Promise((resolve, reject) =>
245
+ const res = await new Promise((resolve, reject) =>
246
246
  dispatch(
247
247
  {
248
248
  id: opts.id ?? headers?.['request-id'] ?? headers?.['Request-Id'] ?? genReqId(),
@@ -290,27 +290,23 @@ export async function request(url, opts) {
290
290
  onHeaders(statusCode, rawHeaders, resume, statusMessage) {
291
291
  const headers = parseHeaders(rawHeaders)
292
292
 
293
- if (statusCode >= 400) {
294
- this.abort(createError(statusCode, { headers }))
295
- } else {
296
- assert(statusCode >= 200)
293
+ assert(statusCode >= 200)
297
294
 
298
- const contentLength = Number(headers['content-length'] ?? headers['Content-Length'])
295
+ const contentLength = Number(headers['content-length'] ?? headers['Content-Length'])
299
296
 
300
- this.body = new Readable({
301
- resume,
302
- abort: this.abort,
303
- highWaterMark: this.highWaterMark,
304
- statusCode,
305
- statusMessage,
306
- headers,
307
- size: Number.isFinite(contentLength) ? contentLength : null,
308
- })
297
+ this.body = new Readable({
298
+ resume,
299
+ abort: this.abort,
300
+ highWaterMark: this.highWaterMark,
301
+ statusCode,
302
+ statusMessage,
303
+ headers,
304
+ size: Number.isFinite(contentLength) ? contentLength : null,
305
+ })
309
306
 
310
- this.resolve(this.body)
311
- this.resolve = null
312
- this.reject = null
313
- }
307
+ this.resolve(this.body)
308
+ this.resolve = null
309
+ this.reject = null
314
310
 
315
311
  return false
316
312
  },
@@ -332,4 +328,15 @@ export async function request(url, opts) {
332
328
  },
333
329
  ),
334
330
  )
331
+
332
+ if (res.statusCode >= 400) {
333
+ // TODO (fix): Limit the size of the body?
334
+ const data =
335
+ res.headers['content-type']?.toLowerCase() === 'application/json'
336
+ ? await res.json()
337
+ : await res.text()
338
+ throw createError(res.statusCode, { headers, data })
339
+ }
340
+
341
+ return res
335
342
  }
@@ -1,7 +1,61 @@
1
+ class Handler {
2
+ constructor(opts, { handler, dispatch }) {
3
+ this.handler = handler
4
+ this.dispatch = dispatch
5
+ this.ac = new AbortController()
6
+
7
+ const signal = opts.signal ? AbortSignal.any([this.ac.signal, opts.signal]) : this.ac.signal
8
+
9
+ const body = opts.body({ signal })
10
+
11
+ if (typeof body.then === 'function') {
12
+ body.then(
13
+ (body) => this.dispatch({ ...opts, body }, handler),
14
+ (err) => this.handler.onError(err),
15
+ )
16
+ } else {
17
+ this.dispatch({ ...opts, body }, handler)
18
+ }
19
+ }
20
+
21
+ onConnect(abort) {
22
+ this.abort = (err) => {
23
+ this.ac.abort(err)
24
+ abort(err)
25
+ }
26
+ return this.handler.onConnect(abort)
27
+ }
28
+
29
+ onUpgrade(statusCode, rawHeaders, socket) {
30
+ return this.handler.onUpgrade(statusCode, rawHeaders, socket)
31
+ }
32
+
33
+ onBodySent(chunk) {
34
+ return this.handler.onBodySent(chunk)
35
+ }
36
+
37
+ onRequestSent() {
38
+ return this.handler.onRequestSent()
39
+ }
40
+
41
+ onHeaders(statusCode, rawHeaders, resume, statusMessage) {
42
+ return this.handler.onHeaders(statusCode, rawHeaders, resume, statusMessage)
43
+ }
44
+
45
+ onData(chunk) {
46
+ return this.handler.onData(chunk)
47
+ }
48
+
49
+ onComplete(rawTrailers) {
50
+ return this.handler.onComplete(rawTrailers)
51
+ }
52
+
53
+ onError(err) {
54
+ return this.handler.onError(err)
55
+ }
56
+ }
57
+
1
58
  export default (dispatch) => (opts, handler) =>
2
59
  typeof opts.body === 'function'
3
- ? Promise.resolve(opts.body({ signal: opts.signal })).then(
4
- (body) => dispatch({ ...opts, body }, handler),
5
- (err) => handler.onError(err),
6
- )
60
+ ? dispatch(opts, new Handler(opts, { handler, dispatch }))
7
61
  : dispatch(opts, handler)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",
@@ -16,8 +16,8 @@
16
16
  "undici": "^5.27.0"
17
17
  },
18
18
  "devDependencies": {
19
- "@types/node": "^20.8.9",
20
- "eslint": "^8.52.0",
19
+ "@types/node": "^20.9.1",
20
+ "eslint": "^8.54.0",
21
21
  "eslint-config-prettier": "^9.0.0",
22
22
  "eslint-config-standard": "^17.0.0",
23
23
  "eslint-plugin-import": "^2.29.0",
@@ -27,7 +27,7 @@
27
27
  "lint-staged": "^15.0.2",
28
28
  "pinst": "^3.0.0",
29
29
  "prettier": "^3.0.3",
30
- "tap": "^18.4.2"
30
+ "tap": "^18.6.1"
31
31
  },
32
32
  "scripts": {
33
33
  "prepare": "husky install",