@nxtedition/nxt-undici 5.1.1 → 5.1.2

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.
Files changed (2) hide show
  1. package/lib/request.js +29 -21
  2. package/package.json +1 -1
package/lib/request.js CHANGED
@@ -6,13 +6,7 @@ import { BodyReadable as Readable } from './readable.js'
6
6
  function noop() {}
7
7
 
8
8
  export class RequestHandler {
9
- constructor(opts, resolve) {
10
- if (!opts || typeof opts !== 'object') {
11
- throw new InvalidArgumentError('invalid opts')
12
- }
13
-
14
- const { signal, method, body, highWaterMark } = opts
15
-
9
+ constructor({ signal, method, body, highWaterMark }, resolve) {
16
10
  try {
17
11
  if (typeof resolve !== 'function') {
18
12
  throw new InvalidArgumentError('invalid resolve')
@@ -149,20 +143,34 @@ export class RequestHandler {
149
143
  }
150
144
 
151
145
  export function request(dispatch, url, opts) {
152
- return new Promise((resolve) => {
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
- }
146
+ if (!url || (typeof url !== 'string' && typeof url !== 'object' && !(url instanceof URL))) {
147
+ throw new InvalidArgumentError('invalid url')
148
+ }
160
149
 
161
- if (opts == null && typeof url === 'object' && url != null) {
162
- url.protocol ??= 'http:'
163
- opts = { protocol: 'http:', ...url }
164
- }
150
+ if (opts != null && typeof opts !== 'object') {
151
+ throw new InvalidArgumentError('invalid opts')
152
+ }
153
+
154
+ if (typeof url === 'string') {
155
+ url = new URL(url)
156
+ } else if (typeof url === 'object' && url != null && opts == null) {
157
+ opts = url
158
+ }
159
+
160
+ let origin = url.origin
161
+
162
+ if (!origin) {
163
+ const protocol = url.protocol ?? 'http:'
164
+ const host = url.host ?? `${url.hostname}:${url.port ?? (protocol === 'https:' ? 443 : 80)}`
165
+ origin = `${protocol}//${host}`
166
+ }
167
+
168
+ let path = url.path
169
+ if (!path) {
170
+ path = url.search ? `${url.pathname}${url.search}` : url
171
+ }
172
+
173
+ opts = { ...opts, origin, path }
165
174
 
166
- dispatch(opts, new RequestHandler(opts, resolve))
167
- })
175
+ return new Promise((resolve) => dispatch(opts, new RequestHandler(opts, resolve)))
168
176
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "5.1.1",
3
+ "version": "5.1.2",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",