Isolated HTTP Transactions for the Whook framework
Whook takes a very unusual direction when
it comes to dealing with HTTP transactions. It makes requests and responses
serializable (thanks to WhookRequest
and WhookResponse
types) to:
- only work with functions that take request and return responses ( allowing your handlers to be pure functions),
- have easily unit testable handlers thanks to concise snapshots.
This service is intended to build those litteral objects from Node HTTP ones (famously known as req/res) before passing them to the handlers. It also keeps track of running queries and ensure it is well handled by the server before releasing it. If not, the transaction is resolved with an error response (for timeouts or when an error were catched).
-
initHTTPTransaction(services) ⇒
Promise.<WhookHTTPTransaction>
-
Instantiate the httpTransaction service
-
pickFirstHeaderValue(name, headers) ⇒
string
-
Pick the first header value if exists
-
pickAllHeaderValues(name, headers) ⇒
Array
-
Pick header values
-
initAPM(services) ⇒
Promise.<Object>
-
Application monitoring service that simply log stringified contents.
-
initObfuscator(services) ⇒
Promise.<Object>
-
Obfuscate sensible informations.
initHTTPTransaction(services) ⇒ Promise.<WhookHTTPTransaction>
Instantiate the httpTransaction service
Kind: global function
Returns: Promise.<WhookHTTPTransaction>
- A promise of the httpTransaction function
Param | Type | Default | Description |
---|---|---|---|
services | Object |
The services to inject | |
[services.TIMEOUT] | Number |
30000 |
A number indicating how many ms the transaction should take to complete before being cancelled. |
[services.TRANSACTIONS] | Object |
{} |
A hash of every current transactions |
services.delay | Object |
A delaying service | |
services.obfuscator | Object |
A service to avoid logging sensible informations | |
[services.log] | function |
A logging function | |
[services.apm] | function |
An apm function | |
[services.time] | function |
A timing function | |
[services.uniqueId] | function |
A function returning unique identifiers |
Example
import initHTTPTransaction from '@whook/http-transaction';
import { log } from 'node:console';
const httpTransaction = await initHTTPTransaction({
log,
time: Date.now.bind(Date),
});
Create a new HTTP transaction
Kind: inner method of initHTTPTransaction
Returns: Array
- The normalized request and the HTTP
transaction created in an array.
Param | Type | Description |
---|---|---|
req | HTTPRequest |
A raw NodeJS HTTP incoming message |
res | HTTPResponse |
A raw NodeJS HTTP response |
Pick the first header value if exists
Kind: global function
Returns: string
- The value if defined.
Param | Type | Description |
---|---|---|
name | string |
The header name |
headers | Object |
The headers map |
Pick header values
Kind: global function
Returns: Array
- The values in an array.
Param | Type | Description |
---|---|---|
name | string |
The header name |
headers | Object |
The headers map |
Application monitoring service that simply log stringified contents.
Kind: global function
Returns: Promise.<Object>
- A promise of the apm service.
Param | Type | Description |
---|---|---|
services | Object |
The services to inject |
[services.log] | function |
A logging function |
Obfuscate sensible informations.
Kind: global function
Returns: Promise.<Object>
- A promise of an object containing the gathered constants.
Param | Type | Description |
---|---|---|
services | Object |
The service dependend on |
[services.SHIELD_CHAR] | Object |
The char for replacing sensible informations |
[services.MAX_CLEAR_CHARS] | Object |
The maximum clear chars to display |
[services.MAX_CLEAR_RATIO] | Object |
The maximum clear chars ratio to display |
[services.SENSIBLE_PROPS] | Object |
Sensible properties names |
[services.SENSIBLE_HEADERS] | Object |
Sensible headers names |
Example
import { initObfuscator } from '@whook/http-transaction';
import { alsoInject } from 'knifecycle';
import { log } from 'node:console';
const obfuscator = await initObfuscator();
log(obfuscator('my very secret information!));
// my ...on!
Kind: global typedef
Id of the transaction
Kind: static property of WhookHTTPTransaction
Start the transaction
Kind: static property of WhookHTTPTransaction
Returns: Promise.<Object>
- A promise to be resolved with the signed token.
Param | Type | Description |
---|---|---|
buildResponse | function |
A function that builds a response |
Catch a transaction error
Kind: static property of WhookHTTPTransaction
Returns: Promise
- A promise to be resolved with the signed token.
Param | Type | Description |
---|---|---|
err | Error |
A function that builds a response |
End the transaction
Kind: static property of WhookHTTPTransaction
Returns: Promise.<Object>
- A promise to be resolved with the signed token.
Param | Type | Description |
---|---|---|
response | Object |
A response for the transaction |