@digitraffic/common 2026.10.28-1 → 2026.10.30-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.
@@ -1,6 +1,6 @@
1
1
  import { HTTPError } from "ky";
2
2
  import { describe, expect, test, vi } from "vitest";
3
- import { createKyHooks, createLogginInterceptor, } from "../../aws/runtime/logging/http-interceptors.js";
3
+ import { createKyHooks, createLoggingInterceptor, } from "../../aws/runtime/logging/http-interceptors.js";
4
4
  function mockHttpLogger() {
5
5
  return {
6
6
  start: vi.fn(),
@@ -14,7 +14,7 @@ function asHttpLogger(mock) {
14
14
  /** Runs the interceptor and returns the handler it passed on to the next dispatch. */
15
15
  function intercept(httpLogger, innerHandler) {
16
16
  const dispatch = vi.fn(() => true);
17
- const interceptor = createLogginInterceptor(asHttpLogger(httpLogger));
17
+ const interceptor = createLoggingInterceptor(asHttpLogger(httpLogger));
18
18
  interceptor(dispatch)({}, innerHandler);
19
19
  expect(dispatch).toHaveBeenCalledTimes(1);
20
20
  return dispatch.mock.calls[0][1];
@@ -27,7 +27,7 @@ class InnerHandler {
27
27
  onResponseEnd = vi.fn();
28
28
  onResponseError = vi.fn();
29
29
  }
30
- describe("createLogginInterceptor", () => {
30
+ describe("createLoggingInterceptor", () => {
31
31
  const controller = {};
32
32
  test("exposes onRequestStart so undici accepts the handler", () => {
33
33
  const wrapped = intercept(mockHttpLogger(), new InnerHandler());
@@ -1,4 +1,3 @@
1
- import { EventEmitter } from "node:events";
2
1
  import { afterEach, describe, expect, test, vi } from "vitest";
3
2
  import { logger } from "../../aws/runtime/logging/dt-logger-default.js";
4
3
  import { HttpLogger, LoggerFactory, } from "../../aws/runtime/logging/http-logger.js";
@@ -29,16 +28,58 @@ describe("HttpLogger", () => {
29
28
  httpLogger.logResponse(200, 512);
30
29
  expect(infoSpy).toHaveBeenCalledWith({
31
30
  method: "HttpLogger.logStatus",
32
- [WIDE_CONTEXT.CONTEXT]: "Port-call",
31
+ [WIDE_CONTEXT.LOGGING.CONTEXT]: "port_call",
33
32
  [WIDE_CONTEXT.TRACE_ID]: "trace-123",
34
33
  [WIDE_CONTEXT.HTTP_KEYS.URL]: "https://example.test/visits/1",
35
- [WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: "https://example.test/visits/{id}",
34
+ [WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: "/visits/{id}",
36
35
  [WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: undefined,
37
36
  [WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: 200,
38
37
  [WIDE_CONTEXT.HTTP_KEYS.DURATION]: 1.25,
39
38
  [WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: 512,
40
39
  });
41
40
  });
41
+ test.each([
42
+ ["https://example.test/visits/{id}", "/visits/{id}"],
43
+ ["http://example.test:8080/visits/{id}?a=1", "/visits/{id}?a=1"],
44
+ ["HTTPS://example.test/visits/{id}", "/visits/{id}"],
45
+ ["https://example.test", ""],
46
+ ["https://example.test?a=1", "?a=1"],
47
+ ["/visits/{id}", "/visits/{id}"],
48
+ ["/redirect/https://other.test/{id}", "/redirect/https://other.test/{id}"],
49
+ ])("strips scheme and host from url template %s", (template, expected) => {
50
+ const infoSpy = vi
51
+ .spyOn(logger, "info")
52
+ .mockImplementation(() => undefined);
53
+ new HttpLogger({
54
+ context: "Test",
55
+ url: "https://example.test/visits/1",
56
+ urlTemplate: template,
57
+ }).logResponse(200);
58
+ expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
59
+ [WIDE_CONTEXT.HTTP_KEYS.URL]: "https://example.test/visits/1",
60
+ [WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: expected,
61
+ }));
62
+ });
63
+ test("strips host from factory and per-request url templates", () => {
64
+ const infoSpy = vi
65
+ .spyOn(logger, "info")
66
+ .mockImplementation(() => undefined);
67
+ infoSpy.mockClear();
68
+ const factory = new LoggerFactory({
69
+ context: "Test",
70
+ urlTemplate: "https://example.test/default/{id}",
71
+ });
72
+ factory.logger("https://example.test/default/1").logResponse(200);
73
+ factory
74
+ .logger("https://example.test/other/1", "https://example.test/other/{id}")
75
+ .logResponse(200);
76
+ expect(infoSpy).toHaveBeenNthCalledWith(1, expect.objectContaining({
77
+ [WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: "/default/{id}",
78
+ }));
79
+ expect(infoSpy).toHaveBeenNthCalledWith(2, expect.objectContaining({
80
+ [WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: "/other/{id}",
81
+ }));
82
+ });
42
83
  test("logs error name and keeps status code unset when request fails", () => {
43
84
  const infoSpy = vi
44
85
  .spyOn(logger, "info")
@@ -50,7 +91,7 @@ describe("HttpLogger", () => {
50
91
  httpLogger.logError(new TypeError("request failed"));
51
92
  expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
52
93
  method: "HttpLogger.logStatus",
53
- [WIDE_CONTEXT.CONTEXT]: "Test",
94
+ [WIDE_CONTEXT.LOGGING.CONTEXT]: "test",
54
95
  [WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: "TypeError",
55
96
  [WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: undefined,
56
97
  [WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: undefined,
@@ -72,23 +113,5 @@ describe("HttpLogger", () => {
72
113
  [WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: typeof error === "string" ? error : "Unknown",
73
114
  }));
74
115
  });
75
- test("logs response status and content length from header", () => {
76
- const infoSpy = vi
77
- .spyOn(logger, "info")
78
- .mockImplementation(() => undefined);
79
- const body = new EventEmitter();
80
- const response = {
81
- statusCode: 204,
82
- headers: { "content-length": "5" },
83
- body,
84
- };
85
- const httpLogger = new LoggerFactory({ context: "Test" }).logger("https://example.test");
86
- httpLogger.log(response);
87
- body.emit("end");
88
- expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
89
- [WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: 204,
90
- [WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: 5,
91
- }));
92
- });
93
116
  });
94
117
  //# sourceMappingURL=http-logger.test.js.map
@@ -4,6 +4,6 @@ import type { HttpLogger } from "./http-logger.js";
4
4
  /**
5
5
  * Create an Undici interceptor that logs HTTP requests and responses using the provided HttpLogger.
6
6
  */
7
- export declare function createLogginInterceptor(httpLogger: HttpLogger): Dispatcher.DispatcherComposeInterceptor;
7
+ export declare function createLoggingInterceptor(httpLogger: HttpLogger): Dispatcher.DispatcherComposeInterceptor;
8
8
  /** Create hooks for Ky HTTP client using the provided logger */
9
9
  export declare function createKyHooks(httpLogger: HttpLogger): Hooks;
@@ -10,7 +10,7 @@ function parseContentLengthFromHeader(headerValue) {
10
10
  /**
11
11
  * Create an Undici interceptor that logs HTTP requests and responses using the provided HttpLogger.
12
12
  */
13
- export function createLogginInterceptor(httpLogger) {
13
+ export function createLoggingInterceptor(httpLogger) {
14
14
  class LoggingHandler extends TypedDecoratorHandler {
15
15
  _statusCode;
16
16
  _receivedBytes = 0;
@@ -1,4 +1,3 @@
1
- import type { Dispatcher } from "undici";
2
1
  /**
3
2
  * Creates request loggers that share integration-level context.
4
3
  *
@@ -21,6 +20,9 @@ export declare class LoggerFactory {
21
20
  /**
22
21
  * 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.
23
22
  *
23
+ * Given context will be reduced to snake_case.
24
+ * Given URL templates will have their scheme and host stripped.
25
+ *
24
26
  * Call {@link end} when a response status is available. Call {@link error}
25
27
  * when the request fails before a response is received. Each terminal method
26
28
  * writes one info-level log entry containing the request context, URL, status
@@ -39,8 +41,6 @@ export declare class HttpLogger {
39
41
  urlTemplate?: string;
40
42
  });
41
43
  start(): void;
42
- /** Hook logging into undici response events */
43
- log(response: Dispatcher.ResponseData<unknown>): void;
44
44
  /** Records a received HTTP response and writes the request log entry. */
45
45
  logResponse(statusCode: number, contentLength?: number): void;
46
46
  /**
@@ -1,3 +1,4 @@
1
+ import { snakeCase } from "es-toolkit";
1
2
  import { logger } from "./dt-logger-default.js";
2
3
  import { WIDE_CONTEXT } from "./wide-context.js";
3
4
  const METHOD = "HttpLogger.logStatus";
@@ -30,6 +31,9 @@ export class LoggerFactory {
30
31
  /**
31
32
  * 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
33
  *
34
+ * Given context will be reduced to snake_case.
35
+ * Given URL templates will have their scheme and host stripped.
36
+ *
33
37
  * Call {@link end} when a response status is available. Call {@link error}
34
38
  * when the request fails before a response is received. Each terminal method
35
39
  * writes one info-level log entry containing the request context, URL, status
@@ -42,28 +46,15 @@ export class HttpLogger {
42
46
  _traceId;
43
47
  _startTime;
44
48
  constructor(parameters) {
45
- this._context = parameters.context;
49
+ this._context = snakeCase(parameters.context);
46
50
  this._url = parameters.url;
47
51
  this._traceId = parameters.traceId;
48
- this._urlTemplate = parameters.urlTemplate;
52
+ this._urlTemplate = parameters.urlTemplate?.replace(/^https?:\/\/[^/?#]+/i, "");
49
53
  this._startTime = Date.now();
50
54
  }
51
55
  start() {
52
56
  this._startTime = Date.now();
53
57
  }
54
- /** Hook logging into undici response events */
55
- log(response) {
56
- const contentLengthHeader = response.headers["content-length"];
57
- const contentLength = contentLengthHeader
58
- ? parseInt(contentLengthHeader, 10)
59
- : undefined;
60
- response.body.on("error", (err) => {
61
- this.logError(err);
62
- });
63
- response.body.on("end", () => {
64
- this.logResponse(response.statusCode, contentLength);
65
- });
66
- }
67
58
  /** Records a received HTTP response and writes the request log entry. */
68
59
  logResponse(statusCode, contentLength) {
69
60
  const endTime = Date.now();
@@ -96,8 +87,8 @@ export class HttpLogger {
96
87
  logStatus(parameters) {
97
88
  logger.info({
98
89
  method: METHOD,
99
- [WIDE_CONTEXT.CONTEXT]: this._context,
100
90
  [WIDE_CONTEXT.TRACE_ID]: this._traceId,
91
+ [WIDE_CONTEXT.LOGGING.CONTEXT]: this._context,
101
92
  [WIDE_CONTEXT.HTTP_KEYS.URL]: this._url,
102
93
  [WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: this._urlTemplate,
103
94
  [WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: parameters.errorType,
@@ -1,6 +1,8 @@
1
1
  export declare const WIDE_CONTEXT: {
2
2
  readonly TRACE_ID: "customTrace_id";
3
- readonly CONTEXT: "customContext";
3
+ readonly LOGGING: {
4
+ readonly CONTEXT: "customLogging.context";
5
+ };
4
6
  readonly HTTP_KEYS: {
5
7
  readonly DURATION: "customHttp.client.request.duration";
6
8
  readonly ERROR_TYPE: "customRequest.error.type";
@@ -1,6 +1,8 @@
1
1
  export const WIDE_CONTEXT = {
2
2
  TRACE_ID: "customTrace_id",
3
- CONTEXT: "customContext",
3
+ LOGGING: {
4
+ CONTEXT: "customLogging.context",
5
+ },
4
6
  HTTP_KEYS: {
5
7
  DURATION: "customHttp.client.request.duration",
6
8
  ERROR_TYPE: "customRequest.error.type",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2026.10.28-1",
3
+ "version": "2026.10.30-1",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "repository": {