@digitraffic/common 2026.10.27-2 → 2026.10.28-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/dist/__test__/runtime/http-interceptors.test.d.ts +1 -0
- package/dist/__test__/runtime/http-interceptors.test.js +154 -0
- package/dist/__test__/runtime/http-logger.test.js +4 -4
- package/dist/aws/runtime/logging/http-interceptors.d.ts +9 -0
- package/dist/aws/runtime/logging/http-interceptors.js +87 -0
- package/dist/aws/runtime/logging/http-logger.d.ts +7 -12
- package/dist/aws/runtime/logging/http-logger.js +27 -52
- package/dist/types/openapi-schema.d.ts +20 -20
- package/package.json +2 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { HTTPError } from "ky";
|
|
2
|
+
import { describe, expect, test, vi } from "vitest";
|
|
3
|
+
import { createKyHooks, createLogginInterceptor, } from "../../aws/runtime/logging/http-interceptors.js";
|
|
4
|
+
function mockHttpLogger() {
|
|
5
|
+
return {
|
|
6
|
+
start: vi.fn(),
|
|
7
|
+
logResponse: vi.fn(),
|
|
8
|
+
logError: vi.fn(),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function asHttpLogger(mock) {
|
|
12
|
+
return mock;
|
|
13
|
+
}
|
|
14
|
+
/** Runs the interceptor and returns the handler it passed on to the next dispatch. */
|
|
15
|
+
function intercept(httpLogger, innerHandler) {
|
|
16
|
+
const dispatch = vi.fn(() => true);
|
|
17
|
+
const interceptor = createLogginInterceptor(asHttpLogger(httpLogger));
|
|
18
|
+
interceptor(dispatch)({}, innerHandler);
|
|
19
|
+
expect(dispatch).toHaveBeenCalledTimes(1);
|
|
20
|
+
return dispatch.mock.calls[0][1];
|
|
21
|
+
}
|
|
22
|
+
// Prototype methods are not own properties, as with undici's real handlers
|
|
23
|
+
class InnerHandler {
|
|
24
|
+
onRequestStart = vi.fn();
|
|
25
|
+
onResponseStart = vi.fn();
|
|
26
|
+
onResponseData = vi.fn();
|
|
27
|
+
onResponseEnd = vi.fn();
|
|
28
|
+
onResponseError = vi.fn();
|
|
29
|
+
}
|
|
30
|
+
describe("createLogginInterceptor", () => {
|
|
31
|
+
const controller = {};
|
|
32
|
+
test("exposes onRequestStart so undici accepts the handler", () => {
|
|
33
|
+
const wrapped = intercept(mockHttpLogger(), new InnerHandler());
|
|
34
|
+
expect(typeof wrapped.onRequestStart).toBe("function");
|
|
35
|
+
});
|
|
36
|
+
test("starts the timer and delegates onRequestStart", () => {
|
|
37
|
+
const httpLogger = mockHttpLogger();
|
|
38
|
+
const inner = new InnerHandler();
|
|
39
|
+
const wrapped = intercept(httpLogger, inner);
|
|
40
|
+
wrapped.onRequestStart?.(controller, { ctx: 1 });
|
|
41
|
+
expect(httpLogger.start).toHaveBeenCalledTimes(1);
|
|
42
|
+
expect(inner.onRequestStart).toHaveBeenCalledWith(controller, { ctx: 1 });
|
|
43
|
+
});
|
|
44
|
+
test("logs status captured at response start when the response ends", () => {
|
|
45
|
+
const httpLogger = mockHttpLogger();
|
|
46
|
+
const inner = new InnerHandler();
|
|
47
|
+
const wrapped = intercept(httpLogger, inner);
|
|
48
|
+
const headers = {};
|
|
49
|
+
wrapped.onResponseStart?.(controller, 404, headers, "Not Found");
|
|
50
|
+
wrapped.onResponseData?.(controller, Buffer.alloc(42));
|
|
51
|
+
wrapped.onResponseEnd?.(controller, {});
|
|
52
|
+
expect(inner.onResponseStart).toHaveBeenCalledWith(controller, 404, headers, "Not Found");
|
|
53
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(404, 42);
|
|
54
|
+
expect(inner.onResponseEnd).toHaveBeenCalledWith(controller, {});
|
|
55
|
+
});
|
|
56
|
+
test("logs counted bytes regardless of the content-length header", () => {
|
|
57
|
+
const httpLogger = mockHttpLogger();
|
|
58
|
+
const wrapped = intercept(httpLogger, new InnerHandler());
|
|
59
|
+
wrapped.onResponseStart?.(controller, 200, { "content-length": "999" });
|
|
60
|
+
wrapped.onResponseData?.(controller, Buffer.from("abc"));
|
|
61
|
+
wrapped.onResponseEnd?.(controller, {});
|
|
62
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(200, 3);
|
|
63
|
+
});
|
|
64
|
+
test("counts bytes of chunked responses and forwards each chunk", () => {
|
|
65
|
+
const httpLogger = mockHttpLogger();
|
|
66
|
+
const inner = new InnerHandler();
|
|
67
|
+
const wrapped = intercept(httpLogger, inner);
|
|
68
|
+
const first = Buffer.from("hello");
|
|
69
|
+
const second = Buffer.from("world!");
|
|
70
|
+
wrapped.onResponseStart?.(controller, 200, {});
|
|
71
|
+
wrapped.onResponseData?.(controller, first);
|
|
72
|
+
wrapped.onResponseData?.(controller, second);
|
|
73
|
+
wrapped.onResponseEnd?.(controller, {});
|
|
74
|
+
expect(inner.onResponseData).toHaveBeenNthCalledWith(1, controller, first);
|
|
75
|
+
expect(inner.onResponseData).toHaveBeenNthCalledWith(2, controller, second);
|
|
76
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(200, 11);
|
|
77
|
+
});
|
|
78
|
+
test("logs zero length for an empty body without header", () => {
|
|
79
|
+
const httpLogger = mockHttpLogger();
|
|
80
|
+
const wrapped = intercept(httpLogger, new InnerHandler());
|
|
81
|
+
wrapped.onResponseStart?.(controller, 204, {});
|
|
82
|
+
wrapped.onResponseEnd?.(controller, {});
|
|
83
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(204, 0);
|
|
84
|
+
});
|
|
85
|
+
test("logs errors and delegates onResponseError", () => {
|
|
86
|
+
const httpLogger = mockHttpLogger();
|
|
87
|
+
const inner = new InnerHandler();
|
|
88
|
+
const wrapped = intercept(httpLogger, inner);
|
|
89
|
+
const error = new Error("boom");
|
|
90
|
+
wrapped.onResponseError?.(controller, error);
|
|
91
|
+
expect(httpLogger.logError).toHaveBeenCalledWith(error);
|
|
92
|
+
expect(httpLogger.logResponse).not.toHaveBeenCalled();
|
|
93
|
+
expect(inner.onResponseError).toHaveBeenCalledWith(controller, error);
|
|
94
|
+
});
|
|
95
|
+
test("keeps status per handler instance", () => {
|
|
96
|
+
const httpLogger = mockHttpLogger();
|
|
97
|
+
const first = intercept(httpLogger, new InnerHandler());
|
|
98
|
+
const second = intercept(httpLogger, new InnerHandler());
|
|
99
|
+
first.onResponseStart?.(controller, 200, {});
|
|
100
|
+
second.onResponseStart?.(controller, 500, {});
|
|
101
|
+
first.onResponseData?.(controller, Buffer.from("a"));
|
|
102
|
+
second.onResponseData?.(controller, Buffer.from("bb"));
|
|
103
|
+
first.onResponseEnd?.(controller, {});
|
|
104
|
+
second.onResponseEnd?.(controller, {});
|
|
105
|
+
expect(httpLogger.logResponse).toHaveBeenNthCalledWith(1, 200, 1);
|
|
106
|
+
expect(httpLogger.logResponse).toHaveBeenNthCalledWith(2, 500, 2);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
describe("createKyHooks", () => {
|
|
110
|
+
function hook(name) {
|
|
111
|
+
const httpLogger = mockHttpLogger();
|
|
112
|
+
const hooks = createKyHooks(asHttpLogger(httpLogger));
|
|
113
|
+
const fn = hooks[name]?.[0];
|
|
114
|
+
return { httpLogger, fn };
|
|
115
|
+
}
|
|
116
|
+
test.each(["beforeRequest", "beforeRetry"])("%s starts the timer and returns the request", (name) => {
|
|
117
|
+
const { httpLogger, fn } = hook(name);
|
|
118
|
+
const request = new Request("https://example.com");
|
|
119
|
+
expect(fn({ request })).toBe(request);
|
|
120
|
+
expect(httpLogger.start).toHaveBeenCalledTimes(1);
|
|
121
|
+
});
|
|
122
|
+
test("afterResponse uses content-length header without reading the body", async () => {
|
|
123
|
+
const { httpLogger, fn } = hook("afterResponse");
|
|
124
|
+
const response = new Response("hello", {
|
|
125
|
+
status: 200,
|
|
126
|
+
headers: { "content-length": "5" },
|
|
127
|
+
});
|
|
128
|
+
const cloneSpy = vi.spyOn(response, "clone");
|
|
129
|
+
const result = await fn({ response });
|
|
130
|
+
expect(result).toBe(response);
|
|
131
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(200, 5);
|
|
132
|
+
expect(cloneSpy).not.toHaveBeenCalled();
|
|
133
|
+
});
|
|
134
|
+
test("afterResponse measures body length when header is missing and keeps body readable", async () => {
|
|
135
|
+
const { httpLogger, fn } = hook("afterResponse");
|
|
136
|
+
const response = new Response("hello", { status: 201 });
|
|
137
|
+
const result = (await fn({ response }));
|
|
138
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(201, 5);
|
|
139
|
+
expect(await result.text()).toBe("hello");
|
|
140
|
+
});
|
|
141
|
+
test("beforeError logs and returns non-HTTP errors", () => {
|
|
142
|
+
const { httpLogger, fn } = hook("beforeError");
|
|
143
|
+
const error = new Error("failed");
|
|
144
|
+
expect(fn({ error })).toBe(error);
|
|
145
|
+
expect(httpLogger.logError).toHaveBeenCalledWith(error);
|
|
146
|
+
});
|
|
147
|
+
test("beforeError does not log HTTPError again after afterResponse logged it", () => {
|
|
148
|
+
const { httpLogger, fn } = hook("beforeError");
|
|
149
|
+
const error = new HTTPError(new Response(null, { status: 500 }), new Request("https://example.com"), {});
|
|
150
|
+
expect(fn({ error })).toBe(error);
|
|
151
|
+
expect(httpLogger.logError).not.toHaveBeenCalled();
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
//# sourceMappingURL=http-interceptors.test.js.map
|
|
@@ -26,7 +26,7 @@ describe("HttpLogger", () => {
|
|
|
26
26
|
urlTemplate: "https://example.test/visits/{id}",
|
|
27
27
|
});
|
|
28
28
|
vi.advanceTimersByTime(1250);
|
|
29
|
-
httpLogger.
|
|
29
|
+
httpLogger.logResponse(200, 512);
|
|
30
30
|
expect(infoSpy).toHaveBeenCalledWith({
|
|
31
31
|
method: "HttpLogger.logStatus",
|
|
32
32
|
[WIDE_CONTEXT.CONTEXT]: "Port-call",
|
|
@@ -47,12 +47,12 @@ describe("HttpLogger", () => {
|
|
|
47
47
|
context: "Test",
|
|
48
48
|
url: "https://example.test",
|
|
49
49
|
});
|
|
50
|
-
httpLogger.
|
|
50
|
+
httpLogger.logError(new TypeError("request failed"));
|
|
51
51
|
expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
52
52
|
method: "HttpLogger.logStatus",
|
|
53
53
|
[WIDE_CONTEXT.CONTEXT]: "Test",
|
|
54
54
|
[WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: "TypeError",
|
|
55
|
-
[WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]:
|
|
55
|
+
[WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: undefined,
|
|
56
56
|
[WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: undefined,
|
|
57
57
|
}));
|
|
58
58
|
});
|
|
@@ -67,7 +67,7 @@ describe("HttpLogger", () => {
|
|
|
67
67
|
context: "Test",
|
|
68
68
|
url: "https://example.test",
|
|
69
69
|
});
|
|
70
|
-
httpLogger.
|
|
70
|
+
httpLogger.logError(error);
|
|
71
71
|
expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
72
72
|
[WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: typeof error === "string" ? error : "Unknown",
|
|
73
73
|
}));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Hooks } from "ky";
|
|
2
|
+
import type { Dispatcher } from "undici";
|
|
3
|
+
import type { HttpLogger } from "./http-logger.js";
|
|
4
|
+
/**
|
|
5
|
+
* Create an Undici interceptor that logs HTTP requests and responses using the provided HttpLogger.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createLogginInterceptor(httpLogger: HttpLogger): Dispatcher.DispatcherComposeInterceptor;
|
|
8
|
+
/** Create hooks for Ky HTTP client using the provided logger */
|
|
9
|
+
export declare function createKyHooks(httpLogger: HttpLogger): Hooks;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { HTTPError } from "ky";
|
|
2
|
+
import { DecoratorHandler } from "undici";
|
|
3
|
+
// undici's DecoratorHandler typings omit the methods it implements at runtime
|
|
4
|
+
const TypedDecoratorHandler = DecoratorHandler;
|
|
5
|
+
function parseContentLengthFromHeader(headerValue) {
|
|
6
|
+
const value = Array.isArray(headerValue) ? headerValue[0] : headerValue;
|
|
7
|
+
const length = value ? parseInt(value, 10) : Number.NaN;
|
|
8
|
+
return Number.isNaN(length) ? undefined : length;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Create an Undici interceptor that logs HTTP requests and responses using the provided HttpLogger.
|
|
12
|
+
*/
|
|
13
|
+
export function createLogginInterceptor(httpLogger) {
|
|
14
|
+
class LoggingHandler extends TypedDecoratorHandler {
|
|
15
|
+
_statusCode;
|
|
16
|
+
_receivedBytes = 0;
|
|
17
|
+
onRequestStart(controller, context) {
|
|
18
|
+
httpLogger.start();
|
|
19
|
+
super.onRequestStart?.(controller, context);
|
|
20
|
+
}
|
|
21
|
+
onResponseStart(controller, statusCode, headers, statusMessage) {
|
|
22
|
+
this._statusCode = statusCode;
|
|
23
|
+
super.onResponseStart?.(controller, statusCode, headers, statusMessage);
|
|
24
|
+
}
|
|
25
|
+
onResponseData(controller, chunk) {
|
|
26
|
+
this._receivedBytes += chunk.length;
|
|
27
|
+
super.onResponseData?.(controller, chunk);
|
|
28
|
+
}
|
|
29
|
+
onResponseEnd(controller, trailers) {
|
|
30
|
+
if (this._statusCode !== undefined) {
|
|
31
|
+
httpLogger.logResponse(this._statusCode, this._receivedBytes);
|
|
32
|
+
}
|
|
33
|
+
super.onResponseEnd?.(controller, trailers);
|
|
34
|
+
}
|
|
35
|
+
onResponseError(controller, error) {
|
|
36
|
+
httpLogger.logError(error);
|
|
37
|
+
super.onResponseError?.(controller, error);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return (dispatch) => {
|
|
41
|
+
return (options, handler) => {
|
|
42
|
+
return dispatch(options, new LoggingHandler(handler));
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Create hooks for Ky HTTP client using the provided logger */
|
|
47
|
+
export function createKyHooks(httpLogger) {
|
|
48
|
+
return {
|
|
49
|
+
beforeRequest: [
|
|
50
|
+
(state) => {
|
|
51
|
+
httpLogger.start();
|
|
52
|
+
return state.request;
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
beforeRetry: [
|
|
56
|
+
(state) => {
|
|
57
|
+
httpLogger.start();
|
|
58
|
+
return state.request;
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
afterResponse: [
|
|
62
|
+
async (state) => {
|
|
63
|
+
const contentLengthFromHeader = parseContentLengthFromHeader(state.response.headers.get("content-length"));
|
|
64
|
+
// use header when available to determine content length
|
|
65
|
+
if (contentLengthFromHeader !== undefined) {
|
|
66
|
+
httpLogger.logResponse(state.response.status, contentLengthFromHeader);
|
|
67
|
+
return state.response;
|
|
68
|
+
}
|
|
69
|
+
// create clone from the response, so we can read its body without affecting the original response
|
|
70
|
+
const resp = state.response.clone();
|
|
71
|
+
const contentLength = (await resp.bytes()).length;
|
|
72
|
+
httpLogger.logResponse(resp.status, contentLength);
|
|
73
|
+
return state.response;
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
beforeError: [
|
|
77
|
+
(state) => {
|
|
78
|
+
// afterResponse has already logged the HTTP status of this attempt
|
|
79
|
+
if (!(state.error instanceof HTTPError)) {
|
|
80
|
+
httpLogger.logError(state.error);
|
|
81
|
+
}
|
|
82
|
+
return state.error;
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=http-interceptors.js.map
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Hooks } from "ky";
|
|
2
1
|
import type { Dispatcher } from "undici";
|
|
3
2
|
/**
|
|
4
3
|
* Creates request loggers that share integration-level context.
|
|
@@ -20,7 +19,7 @@ export declare class LoggerFactory {
|
|
|
20
19
|
logger(url: string, urlTemplate?: string): HttpLogger;
|
|
21
20
|
}
|
|
22
21
|
/**
|
|
23
|
-
* Logs the outcome and duration of one
|
|
22
|
+
* Logs the outcome and duration of HTTP requests. Usually one, but in case of retries there may be multiple logs for the same logical request.
|
|
24
23
|
*
|
|
25
24
|
* Call {@link end} when a response status is available. Call {@link error}
|
|
26
25
|
* when the request fails before a response is received. Each terminal method
|
|
@@ -31,30 +30,26 @@ export declare class HttpLogger {
|
|
|
31
30
|
private readonly _context;
|
|
32
31
|
private readonly _url;
|
|
33
32
|
private readonly _urlTemplate?;
|
|
34
|
-
private readonly _startTime;
|
|
35
33
|
private readonly _traceId?;
|
|
36
|
-
private
|
|
37
|
-
private _contentLength?;
|
|
38
|
-
private _statusCode;
|
|
34
|
+
private _startTime;
|
|
39
35
|
constructor(parameters: {
|
|
40
36
|
context: string;
|
|
41
37
|
url: string;
|
|
42
38
|
traceId?: string;
|
|
43
39
|
urlTemplate?: string;
|
|
44
40
|
});
|
|
45
|
-
|
|
46
|
-
end(statusCode: number, contentLength?: number): void;
|
|
47
|
-
/** Create hooks for Ky HTTP client using the provided logger */
|
|
48
|
-
createKyHooks(): Hooks;
|
|
41
|
+
start(): void;
|
|
49
42
|
/** Hook logging into undici response events */
|
|
50
43
|
log(response: Dispatcher.ResponseData<unknown>): void;
|
|
44
|
+
/** Records a received HTTP response and writes the request log entry. */
|
|
45
|
+
logResponse(statusCode: number, contentLength?: number): void;
|
|
51
46
|
/**
|
|
52
47
|
* Records a failed request and writes the request log entry.
|
|
53
48
|
*
|
|
54
49
|
* Errors are represented by their name, strings are kept as-is, and
|
|
55
50
|
* other values are recorded as `Unknown`. Since no response was received,
|
|
56
|
-
* the status code
|
|
51
|
+
* the status code is left undefined.
|
|
57
52
|
*/
|
|
58
|
-
|
|
53
|
+
logError(error: unknown): void;
|
|
59
54
|
private logStatus;
|
|
60
55
|
}
|
|
@@ -28,7 +28,7 @@ export class LoggerFactory {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
|
-
* Logs the outcome and duration of one
|
|
31
|
+
* Logs the outcome and duration of HTTP requests. Usually one, but in case of retries there may be multiple logs for the same logical request.
|
|
32
32
|
*
|
|
33
33
|
* Call {@link end} when a response status is available. Call {@link error}
|
|
34
34
|
* when the request fails before a response is received. Each terminal method
|
|
@@ -39,11 +39,8 @@ export class HttpLogger {
|
|
|
39
39
|
_context;
|
|
40
40
|
_url;
|
|
41
41
|
_urlTemplate;
|
|
42
|
-
_startTime;
|
|
43
42
|
_traceId;
|
|
44
|
-
|
|
45
|
-
_contentLength;
|
|
46
|
-
_statusCode = -1;
|
|
43
|
+
_startTime;
|
|
47
44
|
constructor(parameters) {
|
|
48
45
|
this._context = parameters.context;
|
|
49
46
|
this._url = parameters.url;
|
|
@@ -51,84 +48,62 @@ export class HttpLogger {
|
|
|
51
48
|
this._urlTemplate = parameters.urlTemplate;
|
|
52
49
|
this._startTime = Date.now();
|
|
53
50
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
this._statusCode = statusCode;
|
|
57
|
-
this._contentLength = contentLength;
|
|
58
|
-
this.logStatus();
|
|
59
|
-
}
|
|
60
|
-
/** Create hooks for Ky HTTP client using the provided logger */
|
|
61
|
-
createKyHooks() {
|
|
62
|
-
return {
|
|
63
|
-
afterResponse: [
|
|
64
|
-
async (state) => {
|
|
65
|
-
const contentLengthHeader = state.response.headers.get("content-length");
|
|
66
|
-
// use header when available to determine content length
|
|
67
|
-
if (contentLengthHeader) {
|
|
68
|
-
const contentLength = parseInt(contentLengthHeader, 10);
|
|
69
|
-
this.end(state.response.status, contentLength);
|
|
70
|
-
return state.response;
|
|
71
|
-
}
|
|
72
|
-
// create clone from the response, so we can read its body without affecting the original response
|
|
73
|
-
const resp = state.response.clone();
|
|
74
|
-
const contentLength = (await resp.bytes()).length;
|
|
75
|
-
this.end(resp.status, contentLength);
|
|
76
|
-
return state.response;
|
|
77
|
-
},
|
|
78
|
-
],
|
|
79
|
-
beforeError: [
|
|
80
|
-
(state) => {
|
|
81
|
-
this.error(state.error);
|
|
82
|
-
return state.error;
|
|
83
|
-
},
|
|
84
|
-
],
|
|
85
|
-
};
|
|
51
|
+
start() {
|
|
52
|
+
this._startTime = Date.now();
|
|
86
53
|
}
|
|
87
54
|
/** Hook logging into undici response events */
|
|
88
55
|
log(response) {
|
|
89
|
-
this._statusCode = response.statusCode;
|
|
90
56
|
const contentLengthHeader = response.headers["content-length"];
|
|
91
57
|
const contentLength = contentLengthHeader
|
|
92
58
|
? parseInt(contentLengthHeader, 10)
|
|
93
59
|
: undefined;
|
|
94
60
|
response.body.on("error", (err) => {
|
|
95
|
-
this.
|
|
61
|
+
this.logError(err);
|
|
96
62
|
});
|
|
97
63
|
response.body.on("end", () => {
|
|
98
|
-
this.
|
|
64
|
+
this.logResponse(response.statusCode, contentLength);
|
|
99
65
|
});
|
|
100
66
|
}
|
|
67
|
+
/** Records a received HTTP response and writes the request log entry. */
|
|
68
|
+
logResponse(statusCode, contentLength) {
|
|
69
|
+
const endTime = Date.now();
|
|
70
|
+
this.logStatus({ endTime, statusCode, contentLength });
|
|
71
|
+
}
|
|
101
72
|
/**
|
|
102
73
|
* Records a failed request and writes the request log entry.
|
|
103
74
|
*
|
|
104
75
|
* Errors are represented by their name, strings are kept as-is, and
|
|
105
76
|
* other values are recorded as `Unknown`. Since no response was received,
|
|
106
|
-
* the status code
|
|
77
|
+
* the status code is left undefined.
|
|
107
78
|
*/
|
|
108
|
-
|
|
79
|
+
logError(error) {
|
|
80
|
+
let errorType;
|
|
109
81
|
if (typeof error === "string") {
|
|
110
|
-
|
|
82
|
+
errorType = error;
|
|
111
83
|
}
|
|
112
84
|
else if (error instanceof Error) {
|
|
113
|
-
|
|
85
|
+
errorType = error.name;
|
|
114
86
|
}
|
|
115
87
|
else {
|
|
116
|
-
|
|
88
|
+
errorType = "Unknown";
|
|
117
89
|
}
|
|
118
|
-
this.logStatus();
|
|
119
|
-
}
|
|
120
|
-
logStatus() {
|
|
121
90
|
const endTime = Date.now();
|
|
91
|
+
this.logStatus({
|
|
92
|
+
endTime,
|
|
93
|
+
errorType,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
logStatus(parameters) {
|
|
122
97
|
logger.info({
|
|
123
98
|
method: METHOD,
|
|
124
99
|
[WIDE_CONTEXT.CONTEXT]: this._context,
|
|
125
100
|
[WIDE_CONTEXT.TRACE_ID]: this._traceId,
|
|
126
101
|
[WIDE_CONTEXT.HTTP_KEYS.URL]: this._url,
|
|
127
102
|
[WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: this._urlTemplate,
|
|
128
|
-
[WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]:
|
|
129
|
-
[WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]:
|
|
130
|
-
[WIDE_CONTEXT.HTTP_KEYS.DURATION]: (endTime - this._startTime) / 1000,
|
|
131
|
-
[WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]:
|
|
103
|
+
[WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: parameters.errorType,
|
|
104
|
+
[WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: parameters.statusCode,
|
|
105
|
+
[WIDE_CONTEXT.HTTP_KEYS.DURATION]: (parameters.endTime - this._startTime) / 1000,
|
|
106
|
+
[WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: parameters.contentLength,
|
|
132
107
|
});
|
|
133
108
|
}
|
|
134
109
|
}
|
|
@@ -9,8 +9,8 @@ export declare const parameterObject: z.ZodObject<{
|
|
|
9
9
|
in: z.ZodEnum<{
|
|
10
10
|
path: "path";
|
|
11
11
|
header: "header";
|
|
12
|
-
query: "query";
|
|
13
12
|
cookie: "cookie";
|
|
13
|
+
query: "query";
|
|
14
14
|
}>;
|
|
15
15
|
description: z.ZodOptional<z.ZodString>;
|
|
16
16
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -48,8 +48,8 @@ export declare const openapiOperation: z.ZodObject<{
|
|
|
48
48
|
in: z.ZodEnum<{
|
|
49
49
|
path: "path";
|
|
50
50
|
header: "header";
|
|
51
|
-
query: "query";
|
|
52
51
|
cookie: "cookie";
|
|
52
|
+
query: "query";
|
|
53
53
|
}>;
|
|
54
54
|
description: z.ZodOptional<z.ZodString>;
|
|
55
55
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -96,8 +96,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
96
96
|
in: z.ZodEnum<{
|
|
97
97
|
path: "path";
|
|
98
98
|
header: "header";
|
|
99
|
-
query: "query";
|
|
100
99
|
cookie: "cookie";
|
|
100
|
+
query: "query";
|
|
101
101
|
}>;
|
|
102
102
|
description: z.ZodOptional<z.ZodString>;
|
|
103
103
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -141,8 +141,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
141
141
|
in: z.ZodEnum<{
|
|
142
142
|
path: "path";
|
|
143
143
|
header: "header";
|
|
144
|
-
query: "query";
|
|
145
144
|
cookie: "cookie";
|
|
145
|
+
query: "query";
|
|
146
146
|
}>;
|
|
147
147
|
description: z.ZodOptional<z.ZodString>;
|
|
148
148
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -186,8 +186,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
186
186
|
in: z.ZodEnum<{
|
|
187
187
|
path: "path";
|
|
188
188
|
header: "header";
|
|
189
|
-
query: "query";
|
|
190
189
|
cookie: "cookie";
|
|
190
|
+
query: "query";
|
|
191
191
|
}>;
|
|
192
192
|
description: z.ZodOptional<z.ZodString>;
|
|
193
193
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -231,8 +231,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
231
231
|
in: z.ZodEnum<{
|
|
232
232
|
path: "path";
|
|
233
233
|
header: "header";
|
|
234
|
-
query: "query";
|
|
235
234
|
cookie: "cookie";
|
|
235
|
+
query: "query";
|
|
236
236
|
}>;
|
|
237
237
|
description: z.ZodOptional<z.ZodString>;
|
|
238
238
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -276,8 +276,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
276
276
|
in: z.ZodEnum<{
|
|
277
277
|
path: "path";
|
|
278
278
|
header: "header";
|
|
279
|
-
query: "query";
|
|
280
279
|
cookie: "cookie";
|
|
280
|
+
query: "query";
|
|
281
281
|
}>;
|
|
282
282
|
description: z.ZodOptional<z.ZodString>;
|
|
283
283
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -321,8 +321,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
321
321
|
in: z.ZodEnum<{
|
|
322
322
|
path: "path";
|
|
323
323
|
header: "header";
|
|
324
|
-
query: "query";
|
|
325
324
|
cookie: "cookie";
|
|
325
|
+
query: "query";
|
|
326
326
|
}>;
|
|
327
327
|
description: z.ZodOptional<z.ZodString>;
|
|
328
328
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -366,8 +366,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
366
366
|
in: z.ZodEnum<{
|
|
367
367
|
path: "path";
|
|
368
368
|
header: "header";
|
|
369
|
-
query: "query";
|
|
370
369
|
cookie: "cookie";
|
|
370
|
+
query: "query";
|
|
371
371
|
}>;
|
|
372
372
|
description: z.ZodOptional<z.ZodString>;
|
|
373
373
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -411,8 +411,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
411
411
|
in: z.ZodEnum<{
|
|
412
412
|
path: "path";
|
|
413
413
|
header: "header";
|
|
414
|
-
query: "query";
|
|
415
414
|
cookie: "cookie";
|
|
415
|
+
query: "query";
|
|
416
416
|
}>;
|
|
417
417
|
description: z.ZodOptional<z.ZodString>;
|
|
418
418
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -455,8 +455,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
455
455
|
in: z.ZodEnum<{
|
|
456
456
|
path: "path";
|
|
457
457
|
header: "header";
|
|
458
|
-
query: "query";
|
|
459
458
|
cookie: "cookie";
|
|
459
|
+
query: "query";
|
|
460
460
|
}>;
|
|
461
461
|
description: z.ZodOptional<z.ZodString>;
|
|
462
462
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -532,8 +532,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
532
532
|
in: z.ZodEnum<{
|
|
533
533
|
path: "path";
|
|
534
534
|
header: "header";
|
|
535
|
-
query: "query";
|
|
536
535
|
cookie: "cookie";
|
|
536
|
+
query: "query";
|
|
537
537
|
}>;
|
|
538
538
|
description: z.ZodOptional<z.ZodString>;
|
|
539
539
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -577,8 +577,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
577
577
|
in: z.ZodEnum<{
|
|
578
578
|
path: "path";
|
|
579
579
|
header: "header";
|
|
580
|
-
query: "query";
|
|
581
580
|
cookie: "cookie";
|
|
581
|
+
query: "query";
|
|
582
582
|
}>;
|
|
583
583
|
description: z.ZodOptional<z.ZodString>;
|
|
584
584
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -622,8 +622,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
622
622
|
in: z.ZodEnum<{
|
|
623
623
|
path: "path";
|
|
624
624
|
header: "header";
|
|
625
|
-
query: "query";
|
|
626
625
|
cookie: "cookie";
|
|
626
|
+
query: "query";
|
|
627
627
|
}>;
|
|
628
628
|
description: z.ZodOptional<z.ZodString>;
|
|
629
629
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -667,8 +667,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
667
667
|
in: z.ZodEnum<{
|
|
668
668
|
path: "path";
|
|
669
669
|
header: "header";
|
|
670
|
-
query: "query";
|
|
671
670
|
cookie: "cookie";
|
|
671
|
+
query: "query";
|
|
672
672
|
}>;
|
|
673
673
|
description: z.ZodOptional<z.ZodString>;
|
|
674
674
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -712,8 +712,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
712
712
|
in: z.ZodEnum<{
|
|
713
713
|
path: "path";
|
|
714
714
|
header: "header";
|
|
715
|
-
query: "query";
|
|
716
715
|
cookie: "cookie";
|
|
716
|
+
query: "query";
|
|
717
717
|
}>;
|
|
718
718
|
description: z.ZodOptional<z.ZodString>;
|
|
719
719
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -757,8 +757,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
757
757
|
in: z.ZodEnum<{
|
|
758
758
|
path: "path";
|
|
759
759
|
header: "header";
|
|
760
|
-
query: "query";
|
|
761
760
|
cookie: "cookie";
|
|
761
|
+
query: "query";
|
|
762
762
|
}>;
|
|
763
763
|
description: z.ZodOptional<z.ZodString>;
|
|
764
764
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -802,8 +802,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
802
802
|
in: z.ZodEnum<{
|
|
803
803
|
path: "path";
|
|
804
804
|
header: "header";
|
|
805
|
-
query: "query";
|
|
806
805
|
cookie: "cookie";
|
|
806
|
+
query: "query";
|
|
807
807
|
}>;
|
|
808
808
|
description: z.ZodOptional<z.ZodString>;
|
|
809
809
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -847,8 +847,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
847
847
|
in: z.ZodEnum<{
|
|
848
848
|
path: "path";
|
|
849
849
|
header: "header";
|
|
850
|
-
query: "query";
|
|
851
850
|
cookie: "cookie";
|
|
851
|
+
query: "query";
|
|
852
852
|
}>;
|
|
853
853
|
description: z.ZodOptional<z.ZodString>;
|
|
854
854
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -891,8 +891,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
891
891
|
in: z.ZodEnum<{
|
|
892
892
|
path: "path";
|
|
893
893
|
header: "header";
|
|
894
|
-
query: "query";
|
|
895
894
|
cookie: "cookie";
|
|
895
|
+
query: "query";
|
|
896
896
|
}>;
|
|
897
897
|
description: z.ZodOptional<z.ZodString>;
|
|
898
898
|
required: z.ZodOptional<z.ZodBoolean>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digitraffic/common",
|
|
3
|
-
"version": "2026.10.
|
|
3
|
+
"version": "2026.10.28-1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "",
|
|
6
6
|
"repository": {
|
|
@@ -96,6 +96,7 @@
|
|
|
96
96
|
"./dist/aws/runtime/logging/dt-logger": "./dist/aws/runtime/logging/dt-logger.js",
|
|
97
97
|
"./dist/aws/runtime/logging/http-logger": "./dist/aws/runtime/logging/http-logger.js",
|
|
98
98
|
"./dist/aws/runtime/logging/wide-context": "./dist/aws/runtime/logging/wide-context.js",
|
|
99
|
+
"./dist/aws/runtime/logging/http-interceptors": "./dist/aws/runtime/logging/http-interceptors.js",
|
|
99
100
|
"./dist/aws/runtime/secrets/secret": "./dist/aws/runtime/secrets/secret.js",
|
|
100
101
|
"./dist/aws/runtime/secrets/proxy-holder": "./dist/aws/runtime/secrets/proxy-holder.js",
|
|
101
102
|
"./dist/aws/runtime/secrets/dbsecret": "./dist/aws/runtime/secrets/dbsecret.js",
|