@nxtedition/nxt-undici 2.0.4 → 2.0.6

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
@@ -129,7 +129,6 @@ class Readable extends stream.Readable {
129
129
  const dispatchers = {
130
130
  requestBody: (await import('./interceptor/request-body.js')).default,
131
131
  requestBodyFactory: (await import('./interceptor/request-body-factory.js')).default,
132
- abort: (await import('./interceptor/abort.js')).default,
133
132
  catch: (await import('./interceptor/catch.js')).default,
134
133
  responseContent: (await import('./interceptor/response-content.js')).default,
135
134
  requestContent: (await import('./interceptor/request-content.js')).default,
@@ -329,12 +328,15 @@ export async function request(url, opts) {
329
328
  ),
330
329
  )
331
330
 
332
- if (res.statusCode >= 400) {
331
+ if (method === 'HEAD') {
332
+ await res.dump()
333
+ } else if (res.statusCode >= 400) {
333
334
  // 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()
335
+ const data = /^application\/json$/i.test(res.headers['content-type'])
336
+ ? await res.json()
337
+ : /^text-.+/i.test(res.headers['content-type'])
338
+ ? await res.text()
339
+ : await res.dump()
338
340
  throw createError(res.statusCode, { headers, data })
339
341
  }
340
342
 
@@ -99,6 +99,7 @@ class Handler {
99
99
  'upstream request failed',
100
100
  )
101
101
  }
102
+
102
103
  return this.handler.onError(err)
103
104
  }
104
105
  }
@@ -7,9 +7,11 @@ class Handler {
7
7
  this.length = length
8
8
  this.hasher = this.md5 ? crypto.createHash('md5') : null
9
9
  this.pos = 0
10
+ this.abort = null
10
11
  }
11
12
 
12
13
  onConnect(abort) {
14
+ this.abort = abort
13
15
  return this.handler.onConnect(abort)
14
16
  }
15
17
 
@@ -25,22 +27,25 @@ class Handler {
25
27
 
26
28
  onRequestSent() {
27
29
  const hash = this.hasher?.digest('base64')
30
+
28
31
  if (this.md5 != null && hash !== this.md5) {
29
- this.handler.onError(
32
+ this.abort(
30
33
  Object.assign(new Error('Request Content-Length mismatch'), {
31
34
  expected: this.md5,
32
35
  actual: hash,
33
36
  }),
34
37
  )
35
38
  }
39
+
36
40
  if (this.length != null && this.pos !== Number(this.length)) {
37
- return this.handler.onError(
41
+ this.abort(
38
42
  Object.assign(new Error('Request Content-Length mismatch'), {
39
43
  expected: Number(this.length),
40
44
  actual: this.pos,
41
45
  }),
42
46
  )
43
47
  }
48
+
44
49
  return this.handler.onRequestSent()
45
50
  }
46
51
 
@@ -41,14 +41,16 @@ class Handler {
41
41
 
42
42
  onComplete(rawTrailers) {
43
43
  const hash = this.hasher?.digest('base64')
44
+
44
45
  if (this.md5 != null && hash !== this.md5) {
45
- this.handler.onError(
46
+ return this.handler.onError(
46
47
  Object.assign(new Error('Request Content-Length mismatch'), {
47
48
  expected: this.md5,
48
49
  actual: hash,
49
50
  }),
50
51
  )
51
52
  }
53
+
52
54
  if (this.length != null && this.pos !== Number(this.length)) {
53
55
  return this.handler.onError(
54
56
  Object.assign(new Error('Request Content-Length mismatch'), {
@@ -57,6 +59,7 @@ class Handler {
57
59
  }),
58
60
  )
59
61
  }
62
+
60
63
  return this.handler.onComplete(rawTrailers)
61
64
  }
62
65
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",
@@ -1,72 +0,0 @@
1
- import { AbortError } from '../utils.js'
2
-
3
- class Handler {
4
- constructor(opts, { handler }) {
5
- this.opts = opts
6
- this.handler = handler
7
- this.pos = 0
8
- this.reason = null
9
- }
10
-
11
- onConnect(abort) {
12
- this.abort = abort
13
- this.handler.onConnect((reason) => {
14
- this.reason = reason ?? new AbortError()
15
- })
16
- }
17
-
18
- onBodySent(chunk) {
19
- return this.handler.onBodySent(chunk)
20
- }
21
-
22
- onRequestSent() {
23
- return this.handler.onRequestSent()
24
- }
25
-
26
- onUpgrade(statusCode, rawHeaders, socket) {
27
- return this.handler.onUpgrade(statusCode, rawHeaders, socket)
28
- }
29
-
30
- onHeaders(statusCode, rawHeaders, resume, statusMessage) {
31
- if (this.reason == null) {
32
- const ret = this.handler.onHeaders(statusCode, rawHeaders, resume, statusMessage)
33
- if (this.reason == null) {
34
- return ret
35
- }
36
- }
37
-
38
- return true
39
- }
40
-
41
- onData(chunk) {
42
- if (this.reason == null) {
43
- const ret = this.handler.onData(chunk)
44
- if (this.reason == null) {
45
- return ret
46
- }
47
- }
48
-
49
- this.pos += chunk.length
50
- if (this.pos < 128 * 1024) {
51
- return true
52
- }
53
-
54
- this.abort(this.reason)
55
-
56
- return false
57
- }
58
-
59
- onComplete(rawTrailers) {
60
- return this.onFinally(this.reason, rawTrailers)
61
- }
62
-
63
- onError(err) {
64
- return this.onFinally(err)
65
- }
66
-
67
- onFinally(err, rawTrailers) {
68
- return err == null ? this.handler.onComplete(rawTrailers) : this.handler.onError(err)
69
- }
70
- }
71
-
72
- export default (dispatch) => (opts, handler) => dispatch(opts, new Handler(opts, { handler }))