@nxtedition/lib 23.6.0 → 23.6.2

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/http.js +68 -41
  2. package/package.json +1 -1
  3. package/serializers.js +9 -10
package/http.js CHANGED
@@ -33,101 +33,128 @@ function onTimeout() {
33
33
  this.destroy((timeoutError ??= new createError.RequestTimeout()))
34
34
  }
35
35
 
36
+ // TODO (fix): Make custom ServerRequest class with the properties
37
+ // that is currently deprecated by Context.
38
+
36
39
  export class Context {
37
40
  #id
38
41
  #userAgent
39
42
  #req
40
43
  #res
41
44
  #ac
42
- #url
45
+ #target
43
46
  #logger
44
47
  #query
45
48
 
46
49
  constructor(req, res, logger) {
47
50
  assert(req)
48
51
  assert(res)
49
- assert(logger)
50
-
51
- const id = req.headers['request-id'] || req.headers['Request-Id']
52
52
 
53
- this.#id = (id ? `${id},` : '') + genReqId()
53
+ this.#id = req.id || req.headers['request-id'] || req.headers['Request-Id'] || genReqId()
54
54
  this.#userAgent = req.headers['user-agent'] || req.headers['User-Agent'] || ''
55
55
  this.#req = req
56
56
  this.#res = res
57
- this.#logger = logger.child({ reqId: this.#id, url: req.url, userAgent: this.#userAgent })
57
+ this.#logger = logger.child({ id: this.#req.id })
58
+ this.#target = requestTarget(this.#req)
59
+ this.#query = undefined
60
+
61
+ // TODO (fix): This should not be necessary...
62
+ req.target ??= this.#target
63
+ req.userAgent ??= this.#userAgent
64
+ req.id ??= this.#id
65
+ req.logger ??= this.#logger
58
66
  }
59
67
 
60
68
  get id() {
69
+ if (this.#req.id) {
70
+ return this.#req.id
71
+ }
72
+
61
73
  return this.#id
62
74
  }
63
75
 
76
+ get logger() {
77
+ return this.#logger
78
+ }
79
+
80
+ get req() {
81
+ return this.#req
82
+ }
83
+
84
+ get res() {
85
+ return this.#res
86
+ }
87
+
88
+ get [kAbortController]() {
89
+ return this.#ac
90
+ }
91
+
92
+ get signal() {
93
+ this.#ac ??= new AbortController()
94
+ return this.#ac.signal
95
+ }
96
+
97
+ /** @deprecated */
64
98
  get userAgent() {
99
+ if (this.#req.userAgent) {
100
+ return this.#req.userAgent
101
+ }
102
+
65
103
  return this.#userAgent
66
104
  }
67
105
 
106
+ /** @deprecated */
68
107
  get method() {
69
108
  return this.#req.method
70
109
  }
71
110
 
111
+ /** @deprecated */
72
112
  get url() {
73
- if (this.#url === undefined) {
74
- this.#url = requestTarget(this.#req)
113
+ if (this.#req.target) {
114
+ return this.#req.target
75
115
  }
76
116
 
77
- if (!this.#url) {
117
+ this.#target ??= requestTarget(this.#req)
118
+
119
+ if (!this.#target) {
78
120
  throw new createError.BadRequest('invalid url')
79
121
  }
80
122
 
81
- return this.#url
123
+ return this.#target
82
124
  }
83
125
 
126
+ /** @deprecated */
84
127
  set url(value) {
85
- if (value instanceof URL) {
86
- value = value.toString()
87
- }
88
-
89
128
  if (typeof value !== 'string') {
90
129
  throw new Error('url must be a string')
91
130
  }
92
131
 
93
132
  this.#req.url = value
94
- this.#url = undefined
133
+ this.#target = undefined
95
134
  this.#query = undefined
96
135
  }
97
136
 
137
+ /** @deprecated */
98
138
  get query() {
99
- if (this.#query === undefined) {
100
- this.#query =
101
- this.url.search.length > 1 ? Object.freeze(querystring.parse(this.url.search.slice(1))) : {}
139
+ if (this.#req.query) {
140
+ return this.#req.query
102
141
  }
103
- return this.#query
104
- }
105
-
106
- set query(value) {}
107
142
 
108
- get logger() {
109
- return this.#logger
110
- }
143
+ this.#query ??=
144
+ this.#target.search.length > 1
145
+ ? Object.freeze(querystring.parse(this.#target.search.slice(1)))
146
+ : {}
111
147
 
112
- set logger(val) {
113
- this.#logger = val
114
- }
115
-
116
- get req() {
117
- return this.#req
118
- }
119
-
120
- get res() {
121
- return this.#res
148
+ return this.#query
122
149
  }
123
150
 
124
- get [kAbortController]() {
125
- return this.#ac
126
- }
151
+ /** @deprecated */
152
+ set query(value) {}
127
153
 
128
- get signal() {
129
- this.#ac ??= new AbortController()
130
- return this.#ac.signal
154
+ /** @deprecated */
155
+ set logger(logger) {
156
+ this.#logger = logger.child({ req: { id: this.#req.id } })
157
+ this.#req.logger = this.#logger
131
158
  }
132
159
  }
133
160
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.6.0",
3
+ "version": "23.6.2",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/serializers.js CHANGED
@@ -2,7 +2,9 @@ import { SIGNALS } from './platform.js'
2
2
  import { parseHeaders } from '@nxtedition/nxt-undici'
3
3
 
4
4
  function getHeader(obj, key) {
5
- return obj?.headers?.get?.(key) || obj?.getHeader?.(key) || obj?.headers?.[key]
5
+ return !obj || !key
6
+ ? undefined
7
+ : obj?.headers?.get?.(key) || obj?.getHeader?.(key) || obj?.headers?.[key]
6
8
  }
7
9
 
8
10
  function getHeaders(obj) {
@@ -91,8 +93,8 @@ export default {
91
93
  },
92
94
  res: (res) =>
93
95
  res && {
94
- id: res.id || res.req?.id || getHeader(res, 'request-id') || getHeader(res.req, 'request-id'),
95
- userAgent: res.userAgent ?? getHeader(res, 'user-agent'),
96
+ id: res.id ?? res.req?.id ?? getHeader(res, 'request-id') ?? getHeader(res.req, 'request-id'),
97
+ userAgent: res.userAgent ?? res.req?.userAgent ?? getHeader(res.req, 'user-agent'),
96
98
  timing: getTiming(res),
97
99
  statusCode: res.statusCode || res.status,
98
100
  bytesWritten: res.bytesWritten,
@@ -109,17 +111,14 @@ export default {
109
111
  },
110
112
  req: (req) =>
111
113
  req && {
114
+ url: req.url,
112
115
  id: req.id || getHeader(req, 'request-id'),
113
116
  userAgent: req.userAgent ?? getHeader(req, 'user-agent'),
114
117
  timing: getTiming(req),
118
+ target: req.target,
115
119
  method: req.method,
116
- url: req.url,
117
- scheme: req.scheme,
118
- host: req.host,
119
- port: req.port,
120
- path: req.path,
121
- query: req.query,
122
120
  headers: getHeaders(req),
121
+ query: req.query,
123
122
  bytesRead: req.bytesRead,
124
123
  bytesReadPerSecond:
125
124
  req.bytesReadPerSecond ??
@@ -149,11 +148,11 @@ export default {
149
148
  },
150
149
  ureq: (ureq) =>
151
150
  ureq && {
151
+ url: getUrl(ureq),
152
152
  id: ureq.id || getHeader(ureq, 'request-id'),
153
153
  userAgent: ureq.userAgent ?? getHeader(ureq, 'user-agent'),
154
154
  timing: getTiming(ureq),
155
155
  method: ureq.method,
156
- url: getUrl(ureq),
157
156
  body: typeof ureq.body === 'string' ? ureq.body : undefined,
158
157
  bytesWritten: ureq.bytesWritten,
159
158
  bytesReadPerSecond: