@couimet/execution-context-http 0.1.0
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/LICENSE +21 -0
- package/README.md +85 -0
- package/dist/index.d.mts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +44 -0
- package/dist/index.mjs +16 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Charles Ouimet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# @couimet/execution-context-http
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@couimet/execution-context-http) [](https://codecov.io/gh/couimet/ts-npm-packages?flag=execution-context-http) [](https://www.npmjs.com/package/@couimet/execution-context-http)
|
|
4
|
+
|
|
5
|
+
`@couimet/execution-context-http` names the HTTP headers that carry a correlation id and a request id between services. The `HttpHeaders` enum holds the wire names, so inbound parsing and outbound writing share one spelling. The `firstHeaderValue` helper turns a raw inbound value into the single string that `ExecutionContext.run()` accepts. The package has no runtime dependencies and no framework dependency. It lets the core [`@couimet/execution-context`](https://github.com/couimet/ts-npm-packages/tree/main/packages/execution-context) package stay free of HTTP concerns.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @couimet/execution-context @couimet/execution-context-http
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Read the header names off the enum instead of hard-coding strings:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { HttpHeaders } from '@couimet/execution-context-http';
|
|
19
|
+
|
|
20
|
+
HttpHeaders.CorrelationId; // 'x-correlation-id'
|
|
21
|
+
HttpHeaders.RequestId; // 'x-request-id'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
An adapter uses the enum in both directions. It reads the names to pull the ids off an incoming request and reduce each value to one string. Then it echoes the same names back when it writes the response. One enum keeps the two directions in sync:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { ExecutionContext } from '@couimet/execution-context';
|
|
28
|
+
import { firstHeaderValue, HttpHeaders } from '@couimet/execution-context-http';
|
|
29
|
+
|
|
30
|
+
// Inbound: read the wire names off the request. A repeated header arrives as a
|
|
31
|
+
// string[] under Node's IncomingHttpHeaders, so reduce each value to one string.
|
|
32
|
+
const correlationId = firstHeaderValue(request.headers[HttpHeaders.CorrelationId]);
|
|
33
|
+
const requestId = firstHeaderValue(request.headers[HttpHeaders.RequestId]);
|
|
34
|
+
|
|
35
|
+
// Prime a scope for the request handler.
|
|
36
|
+
ExecutionContext.run({ correlationId, requestId }, () => {
|
|
37
|
+
const result = handler();
|
|
38
|
+
|
|
39
|
+
// Outbound, while the scope is still active: echo the ids on the response.
|
|
40
|
+
// run() generates a fresh id when the request carries none.
|
|
41
|
+
response.setHeader(HttpHeaders.CorrelationId, ExecutionContext.correlationId.toString());
|
|
42
|
+
response.setHeader(HttpHeaders.RequestId, ExecutionContext.requestId.toString());
|
|
43
|
+
|
|
44
|
+
return result;
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API reference
|
|
49
|
+
|
|
50
|
+
The barrel re-exports two modules: `firstHeaderValue` and `HttpHeaders`.
|
|
51
|
+
|
|
52
|
+
### firstHeaderValue
|
|
53
|
+
|
|
54
|
+
Reduces a raw request-header value to the single string that `ExecutionContext.run()` accepts. Node's `IncomingHttpHeaders` can present a repeated header as a `string[]`, and `run()` accepts only `string | undefined`.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
function firstHeaderValue(value: string | string[] | undefined): string | undefined;
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
- Returns the value when it is a non-empty primitive string.
|
|
61
|
+
- Returns the first value when a header repeats and the first value is a non-empty string.
|
|
62
|
+
- Returns undefined when the header is absent or its value is empty, so `run()` generates a fresh id.
|
|
63
|
+
|
|
64
|
+
### HttpHeaders
|
|
65
|
+
|
|
66
|
+
The `HttpHeaders` enum:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
enum HttpHeaders {
|
|
70
|
+
CorrelationId = 'x-correlation-id',
|
|
71
|
+
RequestId = 'x-request-id',
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The member values are the lower-case wire header names used when writing a response. When an adapter reads the ids off a request, it cannot assume the headers object lower-cases its keys the way Node's `IncomingHttpHeaders` does. A runtime that preserves case can present `X-Correlation-Id` where the enum reads `x-correlation-id`, so the adapter should look the values up case-insensitively or normalize the header keys to lower case first. When a header repeats, Node's `IncomingHttpHeaders` presents the values as a `string[]`; reduce them to one string with `firstHeaderValue` before priming `run()`, which accepts only `string | undefined`. When an adapter writes a response, it echoes the id held in the active scope. Because `run()` generates a fresh id when the request carried none, the response carries both headers on every request. A downstream consumer cannot tell an id the caller provided from one this adapter generated.
|
|
76
|
+
|
|
77
|
+
## Related packages
|
|
78
|
+
|
|
79
|
+
- [`@couimet/execution-context`](https://github.com/couimet/ts-npm-packages/tree/main/packages/execution-context) is the runtime that consumes these names. Its `ExecutionContext.run()` primes the ids parsed from the headers.
|
|
80
|
+
|
|
81
|
+
A framework adapter reads the names from this package, pulls the ids off the incoming request, and wraps `ExecutionContext.run()`. Each adapter lives in its own package named after its framework, such as `@couimet/execution-context-http-express`. The core package never depends on HTTP or a framework.
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reduces a raw request-header value to the single string that ExecutionContext.run() accepts.
|
|
3
|
+
* Node's IncomingHttpHeaders can present a repeated header as a string[]; the first value wins.
|
|
4
|
+
* A missing or empty value stays undefined, so run() generates a fresh id.
|
|
5
|
+
*/
|
|
6
|
+
declare function firstHeaderValue(value: string | string[] | undefined): string | undefined;
|
|
7
|
+
|
|
8
|
+
declare enum HttpHeaders {
|
|
9
|
+
CorrelationId = "x-correlation-id",
|
|
10
|
+
RequestId = "x-request-id"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { HttpHeaders, firstHeaderValue };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reduces a raw request-header value to the single string that ExecutionContext.run() accepts.
|
|
3
|
+
* Node's IncomingHttpHeaders can present a repeated header as a string[]; the first value wins.
|
|
4
|
+
* A missing or empty value stays undefined, so run() generates a fresh id.
|
|
5
|
+
*/
|
|
6
|
+
declare function firstHeaderValue(value: string | string[] | undefined): string | undefined;
|
|
7
|
+
|
|
8
|
+
declare enum HttpHeaders {
|
|
9
|
+
CorrelationId = "x-correlation-id",
|
|
10
|
+
RequestId = "x-request-id"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { HttpHeaders, firstHeaderValue };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
HttpHeaders: () => HttpHeaders,
|
|
24
|
+
firstHeaderValue: () => firstHeaderValue
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/firstHeaderValue.ts
|
|
29
|
+
function firstHeaderValue(value) {
|
|
30
|
+
const first = typeof value === "string" ? value : value?.[0];
|
|
31
|
+
return first === "" ? void 0 : first;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/httpHeaders.ts
|
|
35
|
+
var HttpHeaders = /* @__PURE__ */ ((HttpHeaders2) => {
|
|
36
|
+
HttpHeaders2["CorrelationId"] = "x-correlation-id";
|
|
37
|
+
HttpHeaders2["RequestId"] = "x-request-id";
|
|
38
|
+
return HttpHeaders2;
|
|
39
|
+
})(HttpHeaders || {});
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
HttpHeaders,
|
|
43
|
+
firstHeaderValue
|
|
44
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/firstHeaderValue.ts
|
|
2
|
+
function firstHeaderValue(value) {
|
|
3
|
+
const first = typeof value === "string" ? value : value?.[0];
|
|
4
|
+
return first === "" ? void 0 : first;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// src/httpHeaders.ts
|
|
8
|
+
var HttpHeaders = /* @__PURE__ */ ((HttpHeaders2) => {
|
|
9
|
+
HttpHeaders2["CorrelationId"] = "x-correlation-id";
|
|
10
|
+
HttpHeaders2["RequestId"] = "x-request-id";
|
|
11
|
+
return HttpHeaders2;
|
|
12
|
+
})(HttpHeaders || {});
|
|
13
|
+
export {
|
|
14
|
+
HttpHeaders,
|
|
15
|
+
firstHeaderValue
|
|
16
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@couimet/execution-context-http",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Framework-free HTTP transport constants (x-correlation-id, x-request-id) for @couimet/execution-context",
|
|
5
|
+
"homepage": "https://github.com/couimet/ts-npm-packages/tree/main/packages/execution-context-http#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/couimet/ts-npm-packages/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git@github.com:couimet/ts-npm-packages.git",
|
|
12
|
+
"directory": "packages/execution-context-http"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Charles Ouimet <charles.ouimet@gmail.com>",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"require": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.mjs",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"prettier": "@couimet/eslint-config/prettier",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/jest": "^29.5.14",
|
|
32
|
+
"@types/node": "^26.2.0",
|
|
33
|
+
"eslint": "^10.8.1",
|
|
34
|
+
"jest": "^29.7.0",
|
|
35
|
+
"prettier": "^3.9.6",
|
|
36
|
+
"ts-jest": "^29.4.12",
|
|
37
|
+
"tsup": "^8.5.1",
|
|
38
|
+
"typescript": "^6.0.3",
|
|
39
|
+
"@couimet/eslint-config": "1.3.0"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=24"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsup",
|
|
49
|
+
"clean": "rm -rf dist coverage *.tsbuildinfo",
|
|
50
|
+
"clean:all": "pnpm clean && rm -rf node_modules .eslintcache *.log",
|
|
51
|
+
"clean:deps": "rm -rf node_modules",
|
|
52
|
+
"format": "prettier --check .",
|
|
53
|
+
"test": "jest --coverage",
|
|
54
|
+
"typecheck": "tsc --noEmit"
|
|
55
|
+
}
|
|
56
|
+
}
|