@nxtedition/lib 21.10.2 → 22.0.1

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 +6 -2
  2. package/http.js +38 -26
  3. package/package.json +1 -1
package/app.js CHANGED
@@ -329,7 +329,7 @@ export function makeApp(appConfig, onTerminate) {
329
329
  if (appConfig.couchdb || appConfig.couch) {
330
330
  const couchConfig = fp.mergeAll(
331
331
  [
332
- { userAgent, url: isProduction ? null : 'ws://127.0.0.1:5984/nxt' },
332
+ { userAgent, url: isProduction ? 'http://couchdb:5984/nxt' : 'http://127.0.0.1:5984/nxt' },
333
333
  appConfig.couchdb,
334
334
  appConfig.couch,
335
335
  config.couchdb,
@@ -356,7 +356,10 @@ export function makeApp(appConfig, onTerminate) {
356
356
  if (appConfig.deepstream) {
357
357
  let dsConfig = fp.mergeAll(
358
358
  [
359
- { userAgent, url: isProduction ? null : 'ws://127.0.0.1:6020/deepstream' },
359
+ {
360
+ userAgent,
361
+ url: isProduction ? 'ws://deepstream:6020/deepstream' : 'ws://127.0.0.1:6020/deepstream',
362
+ },
360
363
  appConfig.deepstream,
361
364
  config.deepstream,
362
365
  ].map((x) => {
@@ -582,6 +585,7 @@ export function makeApp(appConfig, onTerminate) {
582
585
  [
583
586
  status$.pipe(
584
587
  rx.filter(Boolean),
588
+ rx.map((xs) => (Array.isArray(xs) ? { messages: xs } : xs)),
585
589
  rx.catchError((err) => {
586
590
  logger.error({ err }, 'monitor.status')
587
591
  return rxjs.of([
package/http.js CHANGED
@@ -53,8 +53,6 @@ export class Context {
53
53
  this.#req = req
54
54
  this.#res = res
55
55
  this.#logger = logger.child({ reqId: this.#id, url: req.url, userAgent: this.#userAgent })
56
-
57
- res.setHeader('request-id', this.id)
58
56
  }
59
57
 
60
58
  get id() {
@@ -65,20 +63,20 @@ export class Context {
65
63
  return this.#userAgent
66
64
  }
67
65
 
68
- get [kAbortController]() {
69
- return this.#ac
70
- }
71
-
72
- get signal() {
73
- this.#ac ??= new AbortController()
74
- return this.#ac.signal
66
+ /** @deprecated */
67
+ get method() {
68
+ return this.#req.method
75
69
  }
76
70
 
71
+ /** @deprecated */
77
72
  get url() {
78
- this.#url ??= requestTarget(this.#req)
73
+ if (this.#url === undefined) {
74
+ this.#url = requestTarget(this.#req)
75
+ }
79
76
  return this.#url
80
77
  }
81
78
 
79
+ /** @deprecated */
82
80
  set url(value) {
83
81
  if (typeof value !== 'string') {
84
82
  throw new Error('url must be a string')
@@ -89,6 +87,7 @@ export class Context {
89
87
  this.#query = undefined
90
88
  }
91
89
 
90
+ /** @deprecated */
92
91
  get query() {
93
92
  if (this.#query === undefined) {
94
93
  this.#query = this.url.search.length > 1 ? querystring.parse(this.url.search.slice(1)) : {}
@@ -108,8 +107,13 @@ export class Context {
108
107
  return this.#res
109
108
  }
110
109
 
111
- get method() {
112
- return this.#req.method
110
+ get [kAbortController]() {
111
+ return this.#ac
112
+ }
113
+
114
+ get signal() {
115
+ this.#ac ??= new AbortController()
116
+ return this.#ac.signal
113
117
  }
114
118
  }
115
119
 
@@ -117,7 +121,7 @@ function noop() {}
117
121
 
118
122
  const pendingSet = (globalThis._nxt_lib_http_pending ??= new Set())
119
123
 
120
- export async function request2(ctx, next) {
124
+ export async function requestMiddleware(ctx, next) {
121
125
  const { req, res, logger } = ctx
122
126
  const startTime = performance.now()
123
127
 
@@ -138,20 +142,28 @@ export async function request2(ctx, next) {
138
142
  logger.debug({ req }, 'request started')
139
143
  }
140
144
 
145
+ if (ctx.id) {
146
+ res.setHeader('request-id', ctx.id)
147
+ }
148
+
141
149
  const thenable = next()
142
150
 
143
151
  if (thenable?.then) {
144
152
  await thenable
145
153
  }
146
154
 
147
- const elapsedTime = Math.ceil(performance.now() - startTime)
155
+ assert(res.destroyed, 'response not completed')
156
+
157
+ const elapsedTime = performance.now() - startTime
148
158
 
149
159
  if (isHealthcheck) {
150
160
  // Do nothing...
161
+ } else if (res.errored) {
162
+ logger.error({ err: res.errored, res, elapsedTime }, 'request error')
151
163
  } else if (!res.writableEnded) {
152
164
  logger.debug({ res, elapsedTime }, 'request aborted')
153
- } else if (res.statusCode >= 500 || res.errored) {
154
- logger.error({ err: res.errored, res, elapsedTime }, 'request error')
165
+ } else if (res.statusCode >= 500) {
166
+ logger.error({ res, elapsedTime }, 'request error')
155
167
  } else if (res.statusCode >= 400) {
156
168
  logger.warn({ res, elapsedTime }, 'request failed')
157
169
  } else {
@@ -161,21 +173,19 @@ export async function request2(ctx, next) {
161
173
  ctx[kAbortController]?.abort(err)
162
174
 
163
175
  const statusCode = err.statusCode || err.$metadata?.httpStatusCode || 500
164
- const elapsedTime = Math.ceil(performance.now() - startTime)
176
+ const elapsedTime = performance.now() - startTime
165
177
 
166
- if (!res.headersSent && !res.destroyed) {
178
+ // res.destroyed is not properly set by http2 compat so we need to
179
+ // also check res.closed.
180
+ if (!res.headersSent && !res.destroyed && !res.closed) {
167
181
  res.statusCode = statusCode
168
182
 
169
- let reqId = ctx.id || req?.id || err.id
170
183
  for (const name of res.getHeaderNames()) {
171
- if (!reqId && name === 'request-id') {
172
- reqId = res.getHeader(name)
173
- }
174
184
  res.removeHeader(name)
175
185
  }
176
186
 
177
- if (reqId) {
178
- res.setHeader('request-id', reqId)
187
+ if (ctx.id) {
188
+ res.setHeader('request-id', ctx.id)
179
189
  }
180
190
 
181
191
  const { headers } = err
@@ -196,6 +206,8 @@ export async function request2(ctx, next) {
196
206
  res.setHeader(key, val)
197
207
  }
198
208
  }
209
+ } else if (err.headers != null) {
210
+ logger.warn({ err }, 'invalid headers')
199
211
  }
200
212
 
201
213
  if (fp.isPlainObject(err.body)) {
@@ -285,7 +297,7 @@ export async function request(ctx, next) {
285
297
  await responsePromise
286
298
  }
287
299
 
288
- const elapsedTime = Math.ceil(performance.now() - startTime)
300
+ const elapsedTime = performance.now() - startTime
289
301
 
290
302
  if (isHealthcheck) {
291
303
  // Do nothing...
@@ -302,7 +314,7 @@ export async function request(ctx, next) {
302
314
  ac?.abort(err)
303
315
 
304
316
  const statusCode = err.statusCode || err.$metadata?.httpStatusCode || 500
305
- const elapsedTime = Math.ceil(performance.now() - startTime)
317
+ const elapsedTime = performance.now() - startTime
306
318
 
307
319
  if (!res.headersSent && !res.destroyed) {
308
320
  res.statusCode = statusCode
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "21.10.2",
3
+ "version": "22.0.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",