@nxtedition/lib 19.3.5 → 19.4.0

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/app.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import os from 'node:os'
2
2
  import net from 'node:net'
3
+ import assert from 'node:assert'
3
4
  import stream from 'node:stream'
4
5
  import { Buffer } from 'node:buffer'
5
6
  import { getDockerSecretsSync } from './docker-secrets.js'
@@ -100,6 +101,8 @@ export function makeApp(appConfig, onTerminate) {
100
101
  }
101
102
  }
102
103
 
104
+ assert(appConfig.name, 'name is required')
105
+
103
106
  const appDestroyers = []
104
107
 
105
108
  const serviceName = appConfig.name
@@ -389,7 +392,7 @@ export function makeApp(appConfig, onTerminate) {
389
392
  memory: process.memoryUsage(),
390
393
  utilization: performance.eventLoopUtilization?.(elu2, elu1),
391
394
  heap: v8.getHeapStatistics(),
392
- ...stats,
395
+ [serviceName]: stats,
393
396
  },
394
397
  'STATS',
395
398
  )
package/couch.js CHANGED
@@ -925,7 +925,6 @@ class ErrorHandler extends undici.DecoratorHandler {
925
925
  #str
926
926
  #decoder
927
927
  #statusCode
928
- #statusText
929
928
  #contentType
930
929
  #opts
931
930
  #handler
@@ -947,12 +946,11 @@ class ErrorHandler extends undici.DecoratorHandler {
947
946
 
948
947
  onHeaders(statusCode, headers, resume, statusText) {
949
948
  this.#statusCode = statusCode
950
- this.#statusText = statusText
951
949
 
952
950
  if (this.#statusCode < 200 || this.#statusCode > 300) {
953
951
  this.#headers = Array.isArray(headers) ? undiciUtil.parseHeaders(headers) : headers
954
952
 
955
- this.#error = new Error(this.#statusText ?? this.#statusCode)
953
+ this.#error = new Error(statusText ?? statusCode)
956
954
 
957
955
  this.#contentType = parseContentType(this.#headers['content-type'] ?? '')?.type
958
956
  if (this.#contentType !== 'application/json' && this.#contentType !== 'plain/text') {
@@ -1069,6 +1067,9 @@ class StreamOutput extends stream.Readable {
1069
1067
 
1070
1068
  let ret = true
1071
1069
  for (const line of lines) {
1070
+ if (!line) {
1071
+ continue
1072
+ }
1072
1073
  if (this.#state === 0) {
1073
1074
  if (line.endsWith('[')) {
1074
1075
  this.#state = 1
@@ -1151,13 +1152,14 @@ export function request(
1151
1152
  if (url.search) {
1152
1153
  throw new Error('invalid url: ' + url.href)
1153
1154
  }
1155
+
1154
1156
  const opts = {
1155
1157
  origin: url.origin,
1156
1158
  path: url.pathname + '?' + querystring.stringify(query),
1157
1159
  method,
1158
1160
  headers: {
1159
1161
  'content-type': body != null && typeof body === 'object' ? 'application/json' : 'plain/text',
1160
- 'user-agent': globalThis.userAgent,
1162
+ 'user-agent': globalThis.userAgent ?? 'nxt-lib',
1161
1163
  'request-id': genReqId(),
1162
1164
  accept: 'application/json',
1163
1165
  ...headers,
package/errors.js CHANGED
@@ -126,7 +126,7 @@ function _serializeError(error, { depth }) {
126
126
  JSON.stringify({
127
127
  ...properties,
128
128
  message,
129
- type,
129
+ type: type !== 'Object' ? type : undefined,
130
130
  code,
131
131
  exitCode,
132
132
  signalCode,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "19.3.5",
3
+ "version": "19.4.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -18,6 +18,7 @@
18
18
  "http.js",
19
19
  "s3.js",
20
20
  "deepstream.js",
21
+ "sequence.js",
21
22
  "logger.js",
22
23
  "mime.js",
23
24
  "proxy.js",
package/sequence.js ADDED
@@ -0,0 +1,71 @@
1
+ import assert from 'node:assert'
2
+
3
+ export const ID_SEP = '~'
4
+
5
+ export class Sequence {
6
+ #value
7
+ #parts = []
8
+
9
+ constructor(value) {
10
+ try {
11
+ if (Array.isArray(value)) {
12
+ for (const part of value) {
13
+ if (typeof part === 'string') {
14
+ const [sequenceStr, id] = part.split(ID_SEP)
15
+ const sequence = parseInt(sequenceStr)
16
+ assert(Number.isFinite(sequence) && sequence >= 0)
17
+ this.#parts.push(id, sequence)
18
+ } else {
19
+ assert(Number.isFinite(part.sequence) && part.sequence >= 0)
20
+ assert(typeof part.id === 'string' && part.id.length > 0)
21
+ this.#parts.push(part.id, part.sequence)
22
+ }
23
+ }
24
+ } else if (typeof value === 'string') {
25
+ const [countStr, token] = value.split('-')
26
+ const count = parseInt(countStr)
27
+ assert(Number.isFinite(count) && count >= 0)
28
+ for (const str of token.split('_')) {
29
+ const [sequenceStr, id] = str.split(ID_SEP)
30
+ const sequence = parseInt(sequenceStr)
31
+ assert(Number.isFinite(sequence) && sequence >= 0)
32
+ this.#parts.push(id, sequence)
33
+ }
34
+ this.#value = value
35
+ } else {
36
+ assert(false)
37
+ }
38
+ } catch (err) {
39
+ throw Object.assign(new Error('Invalid sequence value'), { cause: err, data: value })
40
+ }
41
+ }
42
+
43
+ get length() {
44
+ return this.#parts.length / 2
45
+ }
46
+
47
+ at(index) {
48
+ assert(Number.isFinite(index) && index >= 0 && index * 2 < this.#parts.length)
49
+ return this.#parts[index * 2 + 1]
50
+ }
51
+
52
+ set(index, sequence) {
53
+ assert(Number.isFinite(index) && index >= 0 && index < this.#parts.length)
54
+ assert(Number.isFinite(sequence) && sequence >= 0)
55
+ this.#parts[index * 2 + 1] = sequence
56
+ this.#value = undefined
57
+ }
58
+
59
+ toString() {
60
+ if (!this.#value) {
61
+ let count = 0
62
+ let token = ''
63
+ for (let n = 0; n < this.#parts.length; n += 2) {
64
+ count += this.#parts[n + 1]
65
+ token += (n === 0 ? '-' : '_') + this.#parts[n + 1] + ID_SEP + this.#parts[n]
66
+ }
67
+ this.#value = String(count) + token
68
+ }
69
+ return this.#value
70
+ }
71
+ }