@nxtedition/nxt-undici 6.2.18 → 6.2.20

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.
@@ -10,12 +10,14 @@ class CacheHandler extends DecoratorHandler {
10
10
  #key
11
11
  #value
12
12
  #store
13
+ #logger
13
14
  #maxEntrySize
14
15
 
15
- constructor(key, { store, handler, maxEntrySize }) {
16
+ constructor(key, { store, logger, handler, maxEntrySize }) {
16
17
  super(handler)
17
18
 
18
19
  this.#key = key
20
+ this.#logger = logger
19
21
  this.#value = null
20
22
  this.#store = store
21
23
  this.#maxEntrySize = maxEntrySize ?? store.maxEntrySize ?? DEFAULT_MAX_ENTRY_SIZE
@@ -156,7 +158,11 @@ class CacheHandler extends DecoratorHandler {
156
158
  onComplete(trailers) {
157
159
  if (this.#value && (!trailers || Object.keys(trailers).length === 0)) {
158
160
  this.#value.end ??= this.#value.start + this.#value.size
159
- this.#store.set(this.#key, this.#value)
161
+ try {
162
+ this.#store.set(this.#key, this.#value)
163
+ } catch (err) {
164
+ this.#logger?.error({ err }, 'failed to set cache entry')
165
+ }
160
166
  this.#value = null
161
167
  }
162
168
 
@@ -194,7 +200,12 @@ export default () => (dispatch) => (opts, handler) => {
194
200
 
195
201
  // TODO (fix): enable range requests
196
202
 
197
- const entry = store.get(opts)
203
+ let entry
204
+ try {
205
+ entry = store.get(opts)
206
+ } catch (err) {
207
+ opts.logger?.error({ err }, 'failed to get cache entry')
208
+ }
198
209
 
199
210
  if (!entry && !cacheControlDirectives['only-if-cached']) {
200
211
  return dispatch(
@@ -204,6 +215,7 @@ export default () => (dispatch) => (opts, handler) => {
204
215
  : new CacheHandler(undici.util.cache.makeCacheKey(opts), {
205
216
  maxEntrySize: opts.cache.maxEntrySize,
206
217
  store,
218
+ logger: opts.logger,
207
219
  handler,
208
220
  }),
209
221
  )
package/lib/request.js CHANGED
@@ -163,7 +163,14 @@ export function request(dispatch, url, opts) {
163
163
  let origin = url.origin
164
164
  if (!origin) {
165
165
  const protocol = url.protocol ?? 'http:'
166
- const host = url.host ?? `${url.hostname}:${url.port ?? (protocol === 'https:' ? 443 : 80)}`
166
+ const host =
167
+ url.host ??
168
+ (url.hostname ? `${url.hostname}:${url.port ?? (protocol === 'https:' ? 443 : 80)}` : null)
169
+
170
+ if (!host || !protocol) {
171
+ throw new InvalidArgumentError('invalid url')
172
+ }
173
+
167
174
  origin = `${protocol}//${host}`
168
175
  }
169
176
 
@@ -407,22 +407,30 @@ function makeResult(value) {
407
407
  }
408
408
  }
409
409
 
410
+ function printType(val) {
411
+ return val == null ? 'null' : typeof val === 'object' ? val.constructor.name : typeof val
412
+ }
413
+
410
414
  /**
411
415
  * @param {any} key
412
416
  */
413
417
  function assertCacheKey(key) {
414
- if (typeof key !== 'object') {
415
- throw new TypeError(`expected key to be object, got ${typeof key}`)
418
+ if (typeof key !== 'object' || key == null) {
419
+ throw new TypeError(`expected key to be object, got ${typeof printType(key)} [${key}]`)
416
420
  }
417
421
 
418
422
  for (const property of ['origin', 'method', 'path']) {
419
423
  if (typeof key[property] !== 'string') {
420
- throw new TypeError(`expected key.${property} to be string, got ${typeof key[property]}`)
424
+ throw new TypeError(
425
+ `expected key.${property} to be string, got ${printType(key[property])} [${key[property]}]`,
426
+ )
421
427
  }
422
428
  }
423
429
 
424
430
  if (key.headers !== undefined && typeof key.headers !== 'object') {
425
- throw new TypeError(`expected headers to be object, got ${typeof key}`)
431
+ throw new TypeError(
432
+ `expected headers to be object, got ${typeof printType(key)} [${key.headers}]`,
433
+ )
426
434
  }
427
435
  }
428
436
 
@@ -430,31 +438,39 @@ function assertCacheKey(key) {
430
438
  * @param {any} value
431
439
  */
432
440
  function assertCacheValue(value) {
433
- if (typeof value !== 'object') {
434
- throw new TypeError(`expected value to be object, got ${typeof value}`)
441
+ if (typeof value !== 'object' || value == null) {
442
+ throw new TypeError(`expected value to be object, got ${typeof printType(value)}`)
435
443
  }
436
444
 
437
445
  for (const property of ['statusCode', 'cachedAt', 'staleAt', 'deleteAt']) {
438
446
  if (typeof value[property] !== 'number') {
439
- throw new TypeError(`expected value.${property} to be number, got ${typeof value[property]}`)
447
+ throw new TypeError(
448
+ `expected value.${property} to be number, got ${printType(value[property])} [${value[property]}]`,
449
+ )
440
450
  }
441
451
  }
442
452
 
443
453
  if (typeof value.statusMessage !== 'string') {
444
454
  throw new TypeError(
445
- `expected value.statusMessage to be string, got ${typeof value.statusMessage}`,
455
+ `expected value.statusMessage to be string, got ${printType(value.statusMessage)} [${value.statusMessage}]`,
446
456
  )
447
457
  }
448
458
 
449
459
  if (value.headers != null && typeof value.headers !== 'object') {
450
- throw new TypeError(`expected value.rawHeaders to be object, got ${typeof value.headers}`)
460
+ throw new TypeError(
461
+ `expected value.rawHeaders to be object, got ${printType(value.headers)} [${value.headers}]`,
462
+ )
451
463
  }
452
464
 
453
465
  if (value.vary !== undefined && typeof value.vary !== 'object') {
454
- throw new TypeError(`expected value.vary to be object, got ${typeof value.vary}`)
466
+ throw new TypeError(
467
+ `expected value.vary to be object, got ${printType(value.vary)} [${value.vary}]`,
468
+ )
455
469
  }
456
470
 
457
471
  if (value.etag !== undefined && typeof value.etag !== 'string') {
458
- throw new TypeError(`expected value.etag to be string, got ${typeof value.etag}`)
472
+ throw new TypeError(
473
+ `expected value.etag to be string, got ${printType(value.etag)} [${value.etag}]`,
474
+ )
459
475
  }
460
476
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "6.2.18",
3
+ "version": "6.2.20",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",