@superhero/http-server 4.3.3 → 4.3.4

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/config.json CHANGED
@@ -6,6 +6,7 @@
6
6
  "locator":
7
7
  {
8
8
  "@superhero/http-server": true,
9
+ "@superhero/http-server/dispatcher/*/": "./dispatcher/*.js",
9
10
  "@superhero/http-server/dispatcher/*/*": "./dispatcher/*/*.js",
10
11
  "@superhero/http-server/dispatcher/*/*/*": "./dispatcher/*/*/*.js",
11
12
  "@superhero/http-server/dispatcher/*/*/*/*/*": "./dispatcher/*/*/*/*/*.js"
@@ -0,0 +1,30 @@
1
+ export function locate(locator)
2
+ {
3
+ const server = locator.locate('@superhero/http-server')
4
+ return new StatusDispatcher(server)
5
+ }
6
+
7
+ /**
8
+ * @memberof @superhero/http-server:dispatcher
9
+ */
10
+ export default class StatusDispatcher
11
+ {
12
+ started = new Date()
13
+ started_json = this.started.toJSON().substring(0, 19).replace('T', ' ') + 'Z'
14
+
15
+ constructor(server)
16
+ {
17
+ this.server = server
18
+ }
19
+
20
+ dispatch(_, session)
21
+ {
22
+ session.view.name = this.server.name
23
+ session.view.started = started_json
24
+ session.view.uptime = this.started.getTime() - new Date().getTime()
25
+ session.view.dispatched = String(this.server.dispatched) + 'n'
26
+ session.view.completed = String(this.server.completed) + 'n'
27
+ session.view.abortions = String(this.server.abortions) + 'n'
28
+ session.view.rejections = String(this.server.rejections) + 'n'
29
+ }
30
+ }
package/index.js CHANGED
@@ -1,12 +1,13 @@
1
- import View from '@superhero/http-server/view'
2
- import Router from '@superhero/router'
3
- import Log from '@superhero/log'
4
- import http from 'node:http'
5
- import https from 'node:https'
6
- import http2 from 'node:http2'
7
- import net from 'node:net'
8
- import tls from 'node:tls'
9
- import { URL } from 'node:url'
1
+ import View from '@superhero/http-server/view'
2
+ import Router from '@superhero/router'
3
+ import Log from '@superhero/log'
4
+ import NameGenerator from '@superhero/id-name-generator'
5
+ import http from 'node:http'
6
+ import https from 'node:https'
7
+ import http2 from 'node:http2'
8
+ import net from 'node:net'
9
+ import tls from 'node:tls'
10
+ import { URL } from 'node:url'
10
11
 
11
12
  export function locate(locator)
12
13
  {
@@ -19,8 +20,21 @@ export function locate(locator)
19
20
  */
20
21
  export default class HttpServer
21
22
  {
22
- log = new Log({ label: '[HTTP:SERVER]' })
23
+ // Name the server to easier be able to identify what logs
24
+ // belong to what server in a clustered environment.
25
+ name = new NameGenerator().generateName().toUpperCase()
23
26
 
27
+ // Tracks request statistics
28
+ // BigInt used for large numbers
29
+ dispatched = 0n
30
+ completed = 0n
31
+ abortions = 0n
32
+ rejections = 0n
33
+
34
+ // Internal server log
35
+ log = new Log({ label: `[HTTP:SERVER:${this.name}]` })
36
+
37
+ // TCP sockets
24
38
  #sessions = new Set()
25
39
 
26
40
  constructor(router)
@@ -255,9 +269,10 @@ export default class HttpServer
255
269
  downstream.on('close', this.#onStreamClosed.bind(this, session))
256
270
  downstream.on('close', () => this.log.info`${requestID} ⇣ closed`)
257
271
 
272
+ this.dispatched++
258
273
  this.router.dispatch(request, session)
259
274
  .catch(this.#onRouterDispatchRejected.bind(this, session))
260
- .then(session.view.present.bind(session.view))
275
+ .then(this.#onRouterDispatchCompleted.bind(this, session))
261
276
  }
262
277
 
263
278
  #bufferBody(upstream, request)
@@ -375,8 +390,15 @@ export default class HttpServer
375
390
  session.abortion.abort(error)
376
391
  }
377
392
 
393
+ #onRouterDispatchCompleted(session)
394
+ {
395
+ this.completed++
396
+ session.view.present()
397
+ }
398
+
378
399
  #onAbortedRequest(session)
379
400
  {
401
+ this.abortions++
380
402
  session.abortion.signal.reason instanceof Error
381
403
  ? session.view.presentError(session.abortion.signal.reason)
382
404
  : session.view.present()
@@ -384,6 +406,7 @@ export default class HttpServer
384
406
 
385
407
  #onRouterDispatchRejected(session, reason)
386
408
  {
409
+ this.rejections++
387
410
  session.view.presentError(reason.cause)
388
411
  this.log.fail`${reason}`
389
412
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superhero/http-server",
3
- "version": "4.3.3",
3
+ "version": "4.3.4",
4
4
  "description": "HTTP(S) server component supporting both HTTP 1.1 and HTTP 2.0",
5
5
  "keywords": [
6
6
  "http server",
@@ -16,6 +16,7 @@
16
16
  "exports": {
17
17
  ".": "./index.js",
18
18
  "./view": "./view.js",
19
+ "./*/*": "./*/*.js",
19
20
  "./*/*/*": "./*/*/*.js",
20
21
  "./*/*/*/*": "./*/*/*/*.js",
21
22
  "./*/*/*/*/*": "./*/*/*/*/*.js",
@@ -24,7 +25,8 @@
24
25
  "dependencies": {
25
26
  "@superhero/router": "^4.1.4",
26
27
  "@superhero/deep": "^4.1.0",
27
- "@superhero/log": "^4.0.1"
28
+ "@superhero/log": "^4.0.1",
29
+ "@superhero/id-name-generator": "^4.0.0"
28
30
  },
29
31
  "devDependencies": {
30
32
  "@superhero/locator": "^4.3.7",