@nxtedition/nxt-undici 2.0.34 → 2.0.35

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
@@ -24,7 +24,6 @@ const dispatchers = {
24
24
  responseError: (await import('./interceptor/response-error.js')).default,
25
25
  requestBodyFactory: (await import('./interceptor/request-body-factory.js')).default,
26
26
  responseContent: (await import('./interceptor/response-content.js')).default,
27
- requestContent: (await import('./interceptor/request-content.js')).default,
28
27
  log: (await import('./interceptor/log.js')).default,
29
28
  redirect: (await import('./interceptor/redirect.js')).default,
30
29
  responseRetry: (await import('./interceptor/response-retry.js')).default,
@@ -126,7 +125,6 @@ export async function request(url, opts) {
126
125
  dispatch = dispatchers.responseRetry(dispatch)
127
126
  dispatch = dispatchers.responseRetryBody(dispatch)
128
127
  dispatch = dispatchers.responseContent(dispatch)
129
- dispatch = dispatchers.requestContent(dispatch)
130
128
  dispatch = dispatchers.cache(dispatch)
131
129
  dispatch = dispatchers.redirect(dispatch)
132
130
  dispatch = dispatchers.proxy(dispatch)
@@ -51,7 +51,7 @@ class Handler {
51
51
  throw new Error(`Disturbed request cannot be redirected.`)
52
52
  }
53
53
 
54
- this.location = findHeader(rawHeaders, 'location')
54
+ this.location = headers ? headers.location : findHeader(rawHeaders, 'location')
55
55
 
56
56
  if (!this.location) {
57
57
  throw new Error(`Missing redirection location .`)
@@ -20,8 +20,8 @@ class Handler {
20
20
  }
21
21
 
22
22
  onHeaders(statusCode, rawHeaders, resume, statusMessage, headers) {
23
- this.md5 = this.opts.md5 ? findHeader(rawHeaders, 'content-md5') : null
24
- this.length = findHeader(rawHeaders, 'content-length')
23
+ this.md5 = headers ? headers['content-md5'] : findHeader(rawHeaders, 'content-md5')
24
+ this.length = headers ? headers['content-length'] : findHeader(rawHeaders, 'content-length')
25
25
  this.hasher = this.md5 != null ? crypto.createHash('md5') : null
26
26
 
27
27
  return this.handler.onHeaders(statusCode, rawHeaders, resume, statusMessage, headers)
@@ -23,7 +23,7 @@ class Handler {
23
23
  if (statusCode >= 400) {
24
24
  this.statusCode = statusCode
25
25
  this.headers = headers
26
- this.contentType = findHeader(rawHeaders, 'content-type')
26
+ this.contentType = headers ? headers['content-type'] : findHeader(rawHeaders, 'content-type')
27
27
  if (this.contentType === 'application/json' || this.contentType === 'text/plain') {
28
28
  this.decoder = new TextDecoder('utf-8')
29
29
  this.body = ''
@@ -41,7 +41,7 @@ class Handler {
41
41
  }
42
42
 
43
43
  onHeaders(statusCode, rawHeaders, resume, statusMessage, headers) {
44
- const etag = findHeader(rawHeaders, 'etag')
44
+ const etag = headers ? headers.etag : findHeader(rawHeaders, 'etag')
45
45
 
46
46
  if (this.resume) {
47
47
  this.resume = null
@@ -56,7 +56,9 @@ class Handler {
56
56
  throw this.error
57
57
  }
58
58
 
59
- const contentRange = parseContentRange(findHeader(rawHeaders, 'content-range'))
59
+ const contentRange = parseContentRange(
60
+ headers ? headers['content-range'] : findHeader(rawHeaders, 'content-range'),
61
+ )
60
62
  if (!contentRange) {
61
63
  throw this.error
62
64
  }
@@ -72,7 +74,9 @@ class Handler {
72
74
 
73
75
  if (this.end == null) {
74
76
  if (statusCode === 206) {
75
- const contentRange = parseContentRange(findHeader(rawHeaders, 'content-range'))
77
+ const contentRange = parseContentRange(
78
+ headers ? headers['content-range'] : findHeader(rawHeaders, 'content-range'),
79
+ )
76
80
  if (!contentRange) {
77
81
  assert(!this.headersSent)
78
82
  this.headersSent = true
@@ -90,7 +94,9 @@ class Handler {
90
94
  this.end = end
91
95
  this.pos = Number(start)
92
96
  } else {
93
- const contentLength = findHeader(rawHeaders, 'content-length')
97
+ const contentLength = headers
98
+ ? headers['content-length']
99
+ : findHeader(rawHeaders, 'content-length')
94
100
  if (contentLength) {
95
101
  this.end = Number(contentLength)
96
102
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "2.0.34",
3
+ "version": "2.0.35",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",
@@ -1,65 +0,0 @@
1
- import crypto from 'node:crypto'
2
- import { Transform, pipeline, Readable } from 'node:stream'
3
- import { findHeader, isBuffer, isStream } from '../utils.js'
4
-
5
- function getBody(body, opts) {
6
- if (body == null) {
7
- return getBody(Readable.from([]), opts)
8
- } else if (typeof body === 'string') {
9
- return getBody(Readable.from([Buffer.from(body)]), opts)
10
- } else if (isBuffer(body)) {
11
- return getBody(Readable.from([body]), opts)
12
- } else if (typeof body === 'function') {
13
- return async (...args) => getBody(await body(...args), opts)
14
- } else if (isStream(body)) {
15
- const hasher = opts.md5 ? crypto.createHash('md5') : null
16
- let pos = 0
17
- body = pipeline(
18
- body,
19
- new Transform({
20
- transform(chunk, encoding, callback) {
21
- pos += chunk.length
22
- this.push(chunk)
23
- hasher?.update(chunk, encoding)
24
- callback()
25
- },
26
- flush(callback) {
27
- const hash = hasher?.digest('base64') ?? null
28
-
29
- if (opts.length != null && pos !== Number(opts.length)) {
30
- callback(
31
- Object.assign(new Error('Request Content-Length mismatch'), {
32
- expected: Number(opts.length),
33
- actual: pos,
34
- }),
35
- )
36
- } else if (opts.md5 != null && hash !== opts.md5) {
37
- callback(
38
- Object.assign(new Error('Request Content-MD5 mismatch'), {
39
- expected: opts.md5,
40
- actual: hash,
41
- }),
42
- )
43
- } else {
44
- callback(null)
45
- }
46
- },
47
- }),
48
- )
49
- } else {
50
- return body
51
- }
52
- }
53
-
54
- export default (dispatch) => (opts, handler) => {
55
- if (opts.upgrade) {
56
- return dispatch(opts, handler)
57
- }
58
-
59
- const md5 = opts.md5 ? findHeader(opts.headers, 'content-md5') : null
60
- const length = findHeader(opts.headers, 'content-length')
61
-
62
- return md5 || length
63
- ? dispatch({ ...opts, body: getBody(opts.body, { md5, length }) }, handler)
64
- : dispatch(opts, handler)
65
- }