@nxtedition/lib 14.0.21 → 14.0.22

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/errors.js CHANGED
@@ -32,7 +32,6 @@ module.exports.parseError = function parseError(error) {
32
32
  : new Error(message || 'unknown error'),
33
33
  {
34
34
  ...properties,
35
- data: typeof data === 'string' ? data : JSON.stringify(data),
36
35
  cause: cause ? parseError(error.cause) : undefined,
37
36
  }
38
37
  )
@@ -69,12 +68,6 @@ module.exports.serializeError = function serializeError(error) {
69
68
  errors = Array.isArray(errors) ? errors.map(serializeError) : undefined
70
69
  cause = cause ? serializeError(cause) : undefined
71
70
 
72
- if (typeof data === 'string') {
73
- try {
74
- data = JSON.parse(data)
75
- } catch {}
76
- }
77
-
78
71
  return JSON.parse(
79
72
  JSON.stringify({
80
73
  ...properties,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "14.0.21",
3
+ "version": "14.0.22",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "files": [
package/serializers.js CHANGED
@@ -28,7 +28,14 @@ function getHeaders(obj) {
28
28
  }
29
29
 
30
30
  module.exports = {
31
- err: serializers.err,
31
+ err: (err) => {
32
+ if (Array.isArray(err)) {
33
+ err = new AggregateError(err)
34
+ } else if (err.data !== null && typeof err.data === 'object') {
35
+ err = { ...err, data: JSON.stringify(err.data) }
36
+ }
37
+ return serializers.err(err)
38
+ },
32
39
  res: (res) =>
33
40
  res && {
34
41
  id: res.id || res.req?.id || getHeader(res, 'request-id') || getHeader(res.req, 'request-id'),
@@ -174,16 +174,16 @@ const globals = {
174
174
  nxt: null,
175
175
  }
176
176
 
177
- function proxify(value, expression, handler) {
177
+ function proxify(value, expression, handler, suspend = true) {
178
178
  assert(expression)
179
179
  assert(handler)
180
180
 
181
181
  if (!value) {
182
182
  return value
183
183
  } else if (rxjs.isObservable(value)) {
184
- return proxify(expression.observe(value), expression, handler)
184
+ return proxify(expression.observe(value, suspend), expression, handler, suspend)
185
185
  } else if (typeof value?.then === 'function') {
186
- return proxify(expression.wait(value), expression, handler)
186
+ return proxify(expression.wait(value, suspend), expression, handler, suspend)
187
187
  } else if (typeof value === 'object') {
188
188
  return new Proxy(value, handler)
189
189
  } else {
@@ -197,7 +197,7 @@ function makeWrapper(expression) {
197
197
  const handler = {
198
198
  get: (target, prop) => proxify(target[prop], expression, handler),
199
199
  }
200
- return (value) => proxify(value, expression, handler)
200
+ return (value, suspend = true) => proxify(value, expression, handler, suspend)
201
201
  }
202
202
 
203
203
  module.exports = ({ ds, proxify, compiler }) => {
@@ -240,53 +240,53 @@ module.exports = ({ ds, proxify, compiler }) => {
240
240
  }
241
241
  }
242
242
 
243
- wrap(value) {
243
+ wrap(value, suspend = true) {
244
244
  this._wrap ??= makeWrapper(this)
245
- return this._wrap(value)
245
+ return this._wrap(value, suspend)
246
246
  }
247
247
 
248
248
  suspend() {
249
249
  throw kSuspend
250
250
  }
251
251
 
252
- fetch(url, init, throws) {
253
- return this._getFetch(url, init, throws)
252
+ fetch(url, init, suspend = true) {
253
+ return this._getFetch(url, init, suspend)
254
254
  }
255
255
 
256
- observe(observable, throws) {
257
- return this._getObservable(observable, throws)
256
+ observe(observable, suspend = true) {
257
+ return this._getObservable(observable, suspend)
258
258
  }
259
259
 
260
- wait(promise, throws) {
261
- return this._getWait(promise, throws)
260
+ wait(promise, suspend = true) {
261
+ return this._getWait(promise, suspend)
262
262
  }
263
263
 
264
- ds(id, state, throws) {
265
- return this._getRecord(id, state, throws)
264
+ ds(id, state, suspend = true) {
265
+ return this._getRecord(id, state, suspend)
266
266
  }
267
267
 
268
- _ds(key, postfix, state, throws) {
268
+ _ds(key, postfix, state, suspend = true) {
269
269
  return !key || typeof key !== 'string'
270
270
  ? null
271
- : this._getRecord(postfix ? key + postfix : key, state, throws)
271
+ : this._getRecord(postfix ? key + postfix : key, state, suspend)
272
272
  }
273
273
 
274
- asset(id, type, state, throws) {
275
- return this._getHasRawAssetType(id, type, state, throws)
274
+ asset(id, type, state, suspend = true) {
275
+ return this._getHasRawAssetType(id, type, state, suspend)
276
276
  }
277
277
 
278
- _asset(id, type, state, throws) {
278
+ _asset(id, type, state, suspend) {
279
279
  if (!type || typeof type !== 'string') {
280
280
  throw new Error(`invalid argument: type (${type})`)
281
281
  }
282
282
 
283
283
  return !id || typeof id !== 'string'
284
284
  ? null
285
- : this._getHasRawAssetType(id, type, state, throws)
285
+ : this._getHasRawAssetType(id, type, state, suspend)
286
286
  }
287
287
 
288
- timer(dueTime, dueValue) {
289
- return this._getTimer(dueTime, dueValue)
288
+ timer(dueTime, dueValue, suspend = true) {
289
+ return this._getTimer(dueTime, dueValue, suspend)
290
290
  }
291
291
 
292
292
  hash(value) {
@@ -393,7 +393,7 @@ module.exports = ({ ds, proxify, compiler }) => {
393
393
  return entry
394
394
  }
395
395
 
396
- _getFetch(resource, options, throws) {
396
+ _getFetch(resource, options, suspend) {
397
397
  const key = JSON.stringify({ resource, options })
398
398
  const entry = this._getEntry(key, FetchEntry, { resource, options })
399
399
 
@@ -402,7 +402,7 @@ module.exports = ({ ds, proxify, compiler }) => {
402
402
  }
403
403
 
404
404
  if (!entry.status) {
405
- if (throws ?? true) {
405
+ if (suspend ?? true) {
406
406
  throw kSuspend
407
407
  } else {
408
408
  return null
@@ -412,7 +412,7 @@ module.exports = ({ ds, proxify, compiler }) => {
412
412
  return { status: entry.status, headers: entry.headers, body: entry.body }
413
413
  }
414
414
 
415
- _getObservable(observable, throws) {
415
+ _getObservable(observable, suspend) {
416
416
  if (!rxjs.isObservable(observable)) {
417
417
  throw new Error(`invalid argument: observable (${observable})`)
418
418
  }
@@ -424,7 +424,7 @@ module.exports = ({ ds, proxify, compiler }) => {
424
424
  }
425
425
 
426
426
  if (entry.value === kEmpty) {
427
- if (throws ?? true) {
427
+ if (suspend ?? true) {
428
428
  throw kSuspend
429
429
  } else {
430
430
  return null
@@ -434,7 +434,7 @@ module.exports = ({ ds, proxify, compiler }) => {
434
434
  return entry.value
435
435
  }
436
436
 
437
- _getWait(promise, throws) {
437
+ _getWait(promise, suspend) {
438
438
  if (typeof promise?.then !== 'function') {
439
439
  throw new Error(`invalid argument: Promise (${promise})`)
440
440
  }
@@ -446,7 +446,7 @@ module.exports = ({ ds, proxify, compiler }) => {
446
446
  }
447
447
 
448
448
  if (entry.value === kEmpty) {
449
- if (throws ?? true) {
449
+ if (suspend ?? true) {
450
450
  throw kSuspend
451
451
  } else {
452
452
  return null
@@ -456,7 +456,27 @@ module.exports = ({ ds, proxify, compiler }) => {
456
456
  return entry.value
457
457
  }
458
458
 
459
- _getRecord(key, state, throws) {
459
+ _getTimer(dueTime, dueValue = dueTime, suspend) {
460
+ const key = JSON.stringify({ dueTime, dueValue })
461
+
462
+ dueTime = Number.isFinite(dueTime) ? dueTime : new Date(dueTime).valueOf()
463
+
464
+ const timeout = dueTime - Date.now()
465
+
466
+ if (Number.isFinite(dueTime) && timeout > 0) {
467
+ this._getEntry(key, TimerEntry, timeout)
468
+
469
+ if (suspend ?? true) {
470
+ throw kSuspend
471
+ } else {
472
+ return null
473
+ }
474
+ }
475
+
476
+ return dueValue
477
+ }
478
+
479
+ _getRecord(key, state, suspend) {
460
480
  if (!key || typeof key !== 'string') {
461
481
  throw new Error(`invalid argument: key (${key})`)
462
482
  }
@@ -476,7 +496,7 @@ module.exports = ({ ds, proxify, compiler }) => {
476
496
  const entry = this._getEntry(key, RecordEntry, ds)
477
497
 
478
498
  if (entry.record.state < state) {
479
- if (throws ?? true) {
499
+ if (suspend ?? true) {
480
500
  throw kSuspend
481
501
  } else {
482
502
  return null
@@ -486,7 +506,7 @@ module.exports = ({ ds, proxify, compiler }) => {
486
506
  return entry.record.data
487
507
  }
488
508
 
489
- _getHasRawAssetType(id, type, state, throws) {
509
+ _getHasRawAssetType(id, type, state, suspend) {
490
510
  if (!id || typeof id !== 'string') {
491
511
  throw new Error(`invalid argument: id (${id})`)
492
512
  }
@@ -498,28 +518,10 @@ module.exports = ({ ds, proxify, compiler }) => {
498
518
  const data = this._getRecord(
499
519
  id + ':asset.rawTypes?',
500
520
  state ?? ds.record.PROVIDER,
501
- throws ?? true
521
+ suspend ?? true
502
522
  )
503
523
  return data && Array.isArray(data.value) && data.value.includes(type) ? id : null
504
524
  }
505
-
506
- _getTimer(dueTime, dueValue = dueTime, undueValue = null) {
507
- dueTime = Number.isFinite(dueTime) ? dueTime : new Date(dueTime).valueOf()
508
-
509
- if (!Number.isFinite(dueTime)) {
510
- return undueValue
511
- }
512
-
513
- const nowTime = Date.now()
514
-
515
- if (nowTime >= dueTime) {
516
- return dueValue
517
- }
518
-
519
- this._getEntry(objectHash({ dueTime, dueValue, undueValue }), TimerEntry, dueTime - nowTime)
520
-
521
- return undueValue
522
- }
523
525
  }
524
526
 
525
527
  return weakCache((expression) => {