@nxtedition/lib 14.0.25 → 14.0.26

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 (3) hide show
  1. package/app.js +10 -16
  2. package/couch.js +70 -61
  3. package/package.json +1 -1
package/app.js CHANGED
@@ -4,6 +4,7 @@ const stream = require('node:stream')
4
4
  const { Buffer } = require('node:buffer')
5
5
  const net = require('net')
6
6
  const fp = require('lodash/fp.js')
7
+ const assert = require('node:assert')
7
8
 
8
9
  module.exports = function (appConfig, onTerminate) {
9
10
  let ds
@@ -540,26 +541,19 @@ module.exports = function (appConfig, onTerminate) {
540
541
  }
541
542
 
542
543
  try {
543
- const { body } = await client.request({
544
+ const { body, statusCode } = await client.request({
544
545
  method: 'GET',
545
546
  path: '/healthcheck',
546
547
  })
547
548
  await body.dump()
548
- } catch {
549
- try {
550
- const { body } = await client.request({
551
- method: 'GET',
552
- path: '/healthcheck',
553
- })
554
- await body.dump()
555
- } catch (err) {
556
- messages.push({
557
- id: 'app:ds_http_connection',
558
- level: 40,
559
- code: err.code,
560
- msg: 'ds: ' + err.message,
561
- })
562
- }
549
+ assert(statusCode >= 200 && statusCode < 300)
550
+ } catch (err) {
551
+ messages.push({
552
+ id: 'app:ds_http_connection',
553
+ level: 40,
554
+ code: err.code,
555
+ msg: 'ds: ' + err.message,
556
+ })
563
557
  }
564
558
 
565
559
  return messages
package/couch.js CHANGED
@@ -453,81 +453,90 @@ module.exports = function (opts) {
453
453
  path += `?${querystring.stringify(params)}`
454
454
  }
455
455
 
456
+ const req = {
457
+ path,
458
+ origin: dbOrigin,
459
+ idempotent,
460
+ method,
461
+ body: typeof body === 'object' && body ? JSON.stringify(body) : body,
462
+ headers,
463
+ }
464
+
456
465
  return new Promise((resolve, reject) =>
457
- client.dispatch(
458
- {
459
- path,
460
- origin: dbOrigin,
461
- idempotent,
462
- method,
463
- body: typeof body === 'object' && body ? JSON.stringify(body) : body,
464
- headers,
465
- },
466
- {
467
- resolve,
468
- reject,
469
- signal,
470
- status: null,
471
- headers: null,
472
- abort: null,
473
- data: '',
474
- onConnect(abort) {
475
- if (!this.signal) {
476
- return
477
- }
466
+ client.dispatch(req, {
467
+ resolve,
468
+ reject,
469
+ signal,
470
+ status: null,
471
+ headers: null,
472
+ abort: null,
473
+ data: '',
474
+ onConnect(abort) {
475
+ if (!this.signal) {
476
+ return
477
+ }
478
478
 
479
- if (this.signal.aborted) {
480
- abort()
481
- return
482
- }
479
+ if (this.signal.aborted) {
480
+ abort()
481
+ return
482
+ }
483
483
 
484
- this.abort = abort
485
- if ('addEventListener' in this.signal) {
486
- this.signal.addEventListener('abort', abort)
484
+ this.abort = abort
485
+ if ('addEventListener' in this.signal) {
486
+ this.signal.addEventListener('abort', abort)
487
+ } else {
488
+ this.signal.addListener('abort', abort)
489
+ }
490
+ },
491
+ onHeaders(statusCode, headers) {
492
+ this.status = statusCode
493
+ this.headers = parseHeaders(headers)
494
+ },
495
+ onData(chunk) {
496
+ this.data += chunk
497
+ },
498
+ onComplete() {
499
+ if (this.signal) {
500
+ if ('removeEventListener' in this.signal) {
501
+ this.signal.removeEventListener('abort', this.abort)
487
502
  } else {
488
- this.signal.addListener('abort', abort)
489
- }
490
- },
491
- onHeaders(statusCode, headers) {
492
- this.status = statusCode
493
- this.headers = parseHeaders(headers)
494
- },
495
- onData(chunk) {
496
- this.data += chunk
497
- },
498
- onComplete() {
499
- if (this.signal) {
500
- if ('removeEventListener' in this.signal) {
501
- this.signal.removeEventListener('abort', this.abort)
502
- } else {
503
- this.signal.removeListener('abort', this.abort)
504
- }
503
+ this.signal.removeListener('abort', this.abort)
505
504
  }
505
+ }
506
506
 
507
- let data = this.data
508
- if (this.headers['content-type']?.toLowerCase() === 'application/json') {
509
- data = JSON.parse(this.data)
510
- }
507
+ let data = this.data
508
+ if (this.headers['content-type']?.toLowerCase() === 'application/json') {
509
+ data = JSON.parse(this.data)
510
+ }
511
511
 
512
+ if (this.status < 200 || this.status >= 300) {
513
+ this.reject(
514
+ makeError(req, {
515
+ status: this.status,
516
+ headers: this.headers,
517
+ data: this.data,
518
+ })
519
+ )
520
+ } else {
512
521
  this.resolve({
513
522
  data,
514
523
  status: this.status,
515
524
  headers: this.headers,
516
525
  })
517
- },
518
- onError(err) {
519
- if (this.signal) {
520
- if ('removeEventListener' in this.signal) {
521
- this.signal.removeEventListener('abort', this.abort)
522
- } else {
523
- this.signal.removeListener('abort', this.abort)
524
- }
526
+ }
527
+ },
528
+ onError(err) {
529
+ if (this.signal) {
530
+ if ('removeEventListener' in this.signal) {
531
+ this.signal.removeEventListener('abort', this.abort)
532
+ } else {
533
+ this.signal.removeListener('abort', this.abort)
525
534
  }
535
+ }
526
536
 
527
- this.reject(err)
528
- },
529
- }
530
- )
537
+ this.reject(err)
538
+ },
539
+ })
531
540
  )
532
541
  }
533
542
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "14.0.25",
3
+ "version": "14.0.26",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "files": [