@superhero/http-server 4.2.7 → 4.3.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.
- package/README.md +6 -17
- package/index.js +8 -18
- package/index.test.js +9 -8
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -53,7 +53,7 @@ const locator = new Locator();
|
|
|
53
53
|
const router = new Router(locator);
|
|
54
54
|
|
|
55
55
|
// Instantiate the server
|
|
56
|
-
const server = HttpServer(
|
|
56
|
+
const server = new HttpServer(router);
|
|
57
57
|
|
|
58
58
|
// Register the route dispatcher service
|
|
59
59
|
locator.set('hello-dispatcher', {
|
|
@@ -230,29 +230,18 @@ await server.listen(3000);
|
|
|
230
230
|
|
|
231
231
|
### Custom Logging
|
|
232
232
|
|
|
233
|
-
You can override the default logging
|
|
233
|
+
You can override, or hook into, the default logging method to integrate reactions with your logging requirements.
|
|
234
234
|
|
|
235
|
-
#### Turn
|
|
235
|
+
#### Turn of logs
|
|
236
236
|
|
|
237
|
-
```javascript
|
|
238
|
-
server.log.info = () => null;
|
|
239
237
|
```
|
|
240
|
-
|
|
241
|
-
#### Custom Error Logging
|
|
242
|
-
|
|
243
|
-
```javascript
|
|
244
|
-
server.log.error = (error) => {
|
|
245
|
-
// TODO: custom error logging logic...
|
|
246
|
-
};
|
|
238
|
+
server.log.config.mute = true
|
|
247
239
|
```
|
|
248
240
|
|
|
249
|
-
####
|
|
241
|
+
#### Learn more
|
|
250
242
|
|
|
251
|
-
|
|
243
|
+
Read the github page for the repository this component depend on for logging: [@superhero/log](https://github.com/superhero/log).
|
|
252
244
|
|
|
253
|
-
```javascript
|
|
254
|
-
server.log.format = server.log.simple;
|
|
255
|
-
```
|
|
256
245
|
|
|
257
246
|
## API
|
|
258
247
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import View from '@superhero/http-server/view'
|
|
2
2
|
import Router from '@superhero/router'
|
|
3
|
+
import Log from '@superhero/log'
|
|
3
4
|
import http from 'node:http'
|
|
4
5
|
import https from 'node:https'
|
|
5
6
|
import http2 from 'node:http2'
|
|
@@ -18,6 +19,8 @@ export function locate(locator)
|
|
|
18
19
|
*/
|
|
19
20
|
export default class HttpServer
|
|
20
21
|
{
|
|
22
|
+
log = new Log({ label: '[HTTP:SERVER]' })
|
|
23
|
+
|
|
21
24
|
#sessions = new Set()
|
|
22
25
|
|
|
23
26
|
constructor(router)
|
|
@@ -102,7 +105,7 @@ export default class HttpServer
|
|
|
102
105
|
|
|
103
106
|
session.on('close', () => this.log.info`${session.id} ⇣ closed`)
|
|
104
107
|
session.on('close', () => this.#sessions.delete(session))
|
|
105
|
-
session.on('error', this.log.error)
|
|
108
|
+
session.on('error', (error) => this.log.fail`${error}`)
|
|
106
109
|
this.#sessions.add(session)
|
|
107
110
|
this.log.info`${session.id} ⇡ session`
|
|
108
111
|
}
|
|
@@ -382,7 +385,7 @@ export default class HttpServer
|
|
|
382
385
|
#onRouterDispatchRejected(session, reason)
|
|
383
386
|
{
|
|
384
387
|
session.view.presentError(reason.cause)
|
|
385
|
-
this.log.
|
|
388
|
+
this.log.fail`${reason}`
|
|
386
389
|
}
|
|
387
390
|
|
|
388
391
|
#onDownstreamError(reason)
|
|
@@ -390,7 +393,7 @@ export default class HttpServer
|
|
|
390
393
|
const error = new Error('Downstream error')
|
|
391
394
|
error.code = 'E_HTTP_SERVER_DOWNSTREAM_ERROR'
|
|
392
395
|
error.cause = reason
|
|
393
|
-
this.log.error
|
|
396
|
+
this.log.fail`${error}`
|
|
394
397
|
}
|
|
395
398
|
|
|
396
399
|
#onUpstreamError(reason)
|
|
@@ -398,7 +401,7 @@ export default class HttpServer
|
|
|
398
401
|
const error = new Error('Upstream error')
|
|
399
402
|
error.code = 'E_HTTP_SERVER_UPSTREAM_ERROR'
|
|
400
403
|
error.cause = reason
|
|
401
|
-
this.log.error
|
|
404
|
+
this.log.fail`${error}`
|
|
402
405
|
}
|
|
403
406
|
|
|
404
407
|
#onServerError(reason)
|
|
@@ -406,19 +409,6 @@ export default class HttpServer
|
|
|
406
409
|
const error = new Error('Server error')
|
|
407
410
|
error.code = 'E_HTTP_SERVER_ERROR'
|
|
408
411
|
error.cause = reason
|
|
409
|
-
this.log.error
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
// Will make the log methods availible to overwrite
|
|
413
|
-
// if a custom logging is desired.
|
|
414
|
-
log =
|
|
415
|
-
{
|
|
416
|
-
label : '[HTTP:SERVER] ⇢ ',
|
|
417
|
-
simple : (template, ...args) => this.log.label + template.reduce((result, part, i) => result + args[i - 1] + part),
|
|
418
|
-
colors : (template, ...args) => '\x1b[90m\x1b[1m\x1b[2m' + this.log.label + '\x1b[22m' + template.reduce((result, part, i) => result + '\x1b[96m\x1b[2m' + args[i - 1] + '\x1b[90m' + part) + '\x1b[0m',
|
|
419
|
-
format : (...args) => this.log.colors(...args),
|
|
420
|
-
info : (...args) => console.info (this.log.format(...args)),
|
|
421
|
-
warning : (...args) => console.warn (this.log.format(...args)),
|
|
422
|
-
error : (...args) => console.error (this.log.format`failure`, ...args)
|
|
412
|
+
this.log.fail`${error}`
|
|
423
413
|
}
|
|
424
414
|
}
|
package/index.test.js
CHANGED
|
@@ -33,7 +33,7 @@ suite('@superhero/http-server', () =>
|
|
|
33
33
|
})
|
|
34
34
|
|
|
35
35
|
server = locator.locate('@superhero/http-server')
|
|
36
|
-
server.log.
|
|
36
|
+
server.log.config.mute = true
|
|
37
37
|
})
|
|
38
38
|
|
|
39
39
|
afterEach (() =>
|
|
@@ -67,7 +67,8 @@ suite('@superhero/http-server', () =>
|
|
|
67
67
|
test('Can be configured by the configuration file', async () =>
|
|
68
68
|
{
|
|
69
69
|
const config = new Config()
|
|
70
|
-
await config.
|
|
70
|
+
const { filepath, config: resolved } = await config.resolve('./config.json')
|
|
71
|
+
config.add(filepath, resolved)
|
|
71
72
|
|
|
72
73
|
assert.ok(config.find('bootstrap'))
|
|
73
74
|
assert.ok(config.find('locator'))
|
|
@@ -327,12 +328,12 @@ suite('@superhero/http-server', () =>
|
|
|
327
328
|
|
|
328
329
|
let errorLoggerCalled = false
|
|
329
330
|
|
|
330
|
-
server.log.
|
|
331
|
+
server.log.on('fail', (_, error) =>
|
|
331
332
|
{
|
|
332
333
|
errorLoggerCalled = true
|
|
333
334
|
assert.equal(error.code, 'E_ROUTER_DISPATCH_FAILED', 'Should throw router error')
|
|
334
335
|
assert.equal(error.cause.code, 'E_TEST_FAILED_DISPATCHER', 'The error should have the dispatcher error as cause')
|
|
335
|
-
}
|
|
336
|
+
})
|
|
336
337
|
|
|
337
338
|
const response = await request.get(`/test/foo`)
|
|
338
339
|
|
|
@@ -354,12 +355,12 @@ suite('@superhero/http-server', () =>
|
|
|
354
355
|
|
|
355
356
|
let errorLoggerCalled = false
|
|
356
357
|
|
|
357
|
-
server.log.
|
|
358
|
+
server.log.on('fail', (_, error) =>
|
|
358
359
|
{
|
|
359
360
|
errorLoggerCalled = true
|
|
360
361
|
assert.equal(error.code, 'E_ROUTER_DISPATCH_FAILED')
|
|
361
362
|
assert.equal(error.cause.code, 'E_HTTP_SERVER_VIEW_MODEL_PROPERTY_NOT_READABLE')
|
|
362
|
-
}
|
|
363
|
+
})
|
|
363
364
|
|
|
364
365
|
const response = await request.get(`/test/foo`)
|
|
365
366
|
|
|
@@ -381,12 +382,12 @@ suite('@superhero/http-server', () =>
|
|
|
381
382
|
|
|
382
383
|
let errorLoggerCalled = false
|
|
383
384
|
|
|
384
|
-
server.log.
|
|
385
|
+
server.log.on('fail', (_, error) =>
|
|
385
386
|
{
|
|
386
387
|
errorLoggerCalled = true
|
|
387
388
|
assert.equal(error.code, 'E_ROUTER_DISPATCH_FAILED')
|
|
388
389
|
assert.equal(error.cause.code, 'E_HTTP_SERVER_VIEW_MODEL_PROPERTY_NOT_WRITABLE')
|
|
389
|
-
}
|
|
390
|
+
})
|
|
390
391
|
|
|
391
392
|
const response = await request.get(`/test/foo`)
|
|
392
393
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superhero/http-server",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.1",
|
|
4
4
|
"description": "HTTP(S) server component supporting both HTTP 1.1 and HTTP 2.0",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"http server",
|
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
"./*/*/*/*/*/*": "./*/*/*/*/*/*.js"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@superhero/router": "^4.1.
|
|
26
|
-
"@superhero/deep": "^4.1.0"
|
|
25
|
+
"@superhero/router": "^4.1.2",
|
|
26
|
+
"@superhero/deep": "^4.1.0",
|
|
27
|
+
"@superhero/log": "^4.0.1"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"@superhero/
|
|
30
|
-
"@superhero/
|
|
31
|
-
"@superhero/http-request": "^4.0.9"
|
|
30
|
+
"@superhero/locator": "^4.2.4",
|
|
31
|
+
"@superhero/http-request": "^4.0.10"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"test": "node --trace-warnings --test --experimental-test-coverage"
|