@digitraffic/common 2026.10.2-2 → 2026.10.26-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.
Files changed (42) hide show
  1. package/dist/__test__/imports.test.js +3 -8
  2. package/dist/__test__/infra/acl-builder.test.js +20 -0
  3. package/dist/__test__/infra/api/handler-factory.test.js +1 -1
  4. package/dist/__test__/infra/sqs-queue.test.js +1 -1
  5. package/dist/__test__/promise/promise.test.js +1 -1
  6. package/dist/__test__/runtime/dt-logger.test.js +1 -1
  7. package/dist/__test__/runtime/http-logger.test.d.ts +1 -0
  8. package/dist/__test__/runtime/http-logger.test.js +94 -0
  9. package/dist/__test__/utils/lead-time-logging.test.js +1 -1
  10. package/dist/__test__/utils/logging.test.js +1 -1
  11. package/dist/aws/infra/acl-builder.d.ts +5 -1
  12. package/dist/aws/infra/acl-builder.js +27 -3
  13. package/dist/aws/infra/api/handler-factory.d.ts +1 -1
  14. package/dist/aws/infra/api/handler-factory.js +1 -1
  15. package/dist/aws/infra/canaries/database-checker.js +1 -1
  16. package/dist/aws/infra/canaries/url-checker.js +1 -1
  17. package/dist/aws/infra/sqs-queue-internal.js +1 -1
  18. package/dist/aws/runtime/{dt-logger.js → logging/dt-logger.js} +2 -2
  19. package/dist/aws/runtime/logging/http-logger.d.ts +57 -0
  20. package/dist/aws/runtime/logging/http-logger.js +108 -0
  21. package/dist/aws/runtime/logging/wide-context.d.ts +12 -0
  22. package/dist/aws/runtime/logging/wide-context.js +13 -0
  23. package/dist/aws/runtime/s3.js +1 -1
  24. package/dist/aws/runtime/secrets/dbsecret.js +1 -1
  25. package/dist/aws/runtime/secrets/secret-holder.js +1 -1
  26. package/dist/aws/types/lambda-proxy-types.js +1 -1
  27. package/dist/aws/types/lambda-response.js +1 -1
  28. package/dist/database/database.js +1 -1
  29. package/dist/marine/id_utils.js +1 -1
  30. package/dist/utils/geometry.js +1 -1
  31. package/dist/utils/lead-time-logging.d.ts +1 -1
  32. package/dist/utils/lead-time-logging.js +1 -1
  33. package/dist/utils/logging.d.ts +1 -1
  34. package/dist/utils/logging.js +1 -1
  35. package/dist/utils/retry.js +1 -1
  36. package/dist/utils/slack.js +1 -1
  37. package/dist/utils/stop-watch.d.ts +1 -1
  38. package/dist/utils/stop-watch.js +1 -1
  39. package/package.json +9 -5
  40. /package/dist/aws/runtime/{dt-logger-default.d.ts → logging/dt-logger-default.d.ts} +0 -0
  41. /package/dist/aws/runtime/{dt-logger-default.js → logging/dt-logger-default.js} +0 -0
  42. /package/dist/aws/runtime/{dt-logger.d.ts → logging/dt-logger.d.ts} +0 -0
@@ -71,20 +71,15 @@ expectImportOk("mediatypes import ok?", "../aws/types/mediatypes.js");
71
71
  expectImportOk("modelWithReference import ok?", "../aws/types/model-with-reference.js");
72
72
  expectImportOk("errors import ok?", "../aws/types/errors.js");
73
73
  expectImportOk("lambdaResponse import ok?", "../aws/types/lambda-response.js");
74
- expectImportOk("dtLoggerDefault import ok?", "../aws/runtime/dt-logger-default.js");
74
+ expectImportOk("dtLoggerDefault import ok?", "../aws/runtime/logging/dt-logger-default.js");
75
75
  expectImportOk("secret import ok?", "../aws/runtime/secrets/secret.js");
76
76
  expectImportOk("proxyHolder import ok?", "../aws/runtime/secrets/proxy-holder.js");
77
77
  expectImportOk("dbsecret import ok?", "../aws/runtime/secrets/dbsecret.js");
78
78
  expectImportOk("rdsHolder import ok?", "../aws/runtime/secrets/rds-holder.js");
79
79
  expectImportOk("secretHolder import ok?", "../aws/runtime/secrets/secret-holder.js");
80
- expectImportOk("dtLogger import ok?", "../aws/runtime/dt-logger.js");
80
+ expectImportOk("dtLogger import ok?", "../aws/runtime/logging/dt-logger.js");
81
81
  expectImportOk("s3 import ok?", "../aws/runtime/s3.js");
82
- /*
83
- temporary disable, enable after sdk v2 is kicked out
84
- test('apikey import ok?', () => {
85
- const apikey = import("../aws/runtime/apikey.js");
86
- return expect(apikey).resolves.toBeDefined();
87
- });*/
82
+ expectImportOk("apikey import ok?", "../aws/runtime/apikey.js");
88
83
  expectImportOk("environment import ok?", "../aws/runtime/environment.js");
89
84
  expectImportOk("digitrafficIntegrationResponse import ok?", "../aws/runtime/digitraffic-integration-response.js");
90
85
  /*
@@ -32,6 +32,16 @@ describe("acl-builder tests", () => {
32
32
  .build();
33
33
  expect(acl.rules).toHaveLength(1);
34
34
  });
35
+ test("geo block", () => {
36
+ const acl = createBuilder().withGeoBlockRule(["C1", "C2"]).build();
37
+ const geoBlockRule = acl.rules[0];
38
+ expect(geoBlockRule.action).toEqual({ block: {} });
39
+ expect(geoBlockRule.statement).toEqual({
40
+ geoMatchStatement: {
41
+ countryCodes: ["C1", "C2"],
42
+ },
43
+ });
44
+ });
35
45
  test("throttle rules", () => {
36
46
  for (const aclBuilder of [
37
47
  createBuilder().withThrottleDigitrafficUserIp(100),
@@ -69,6 +79,16 @@ describe("acl-builder tests", () => {
69
79
  expect(scopeDownStatements).toHaveLength(2);
70
80
  expect(regexMatchStatement.regexString).toBe("^\\/roadnetwork\\/");
71
81
  });
82
+ test("path throttle rules have unique names", () => {
83
+ const acl = createBuilder()
84
+ .withThrottleAnonymousUserIpByUriPath(100, /^\/first/, "First")
85
+ .withThrottleAnonymousUserIpByUriPath(100, /^\/second/, "Second")
86
+ .build();
87
+ expect(acl.rules.map((rule) => rule.name)).toEqual([
88
+ "ThrottleRuleWithAnonymousUserByPath-First",
89
+ "ThrottleRuleWithAnonymousUserByPath-Second",
90
+ ]);
91
+ });
72
92
  test("Cannot define two rules with the same name", () => {
73
93
  expect(() => createBuilder()
74
94
  .withThrottleAnonymousUserIp(10)
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, test, vi } from "vitest";
2
2
  import { HandlerFactory } from "../../../aws/infra/api/handler-factory.js";
3
- import { DtLogger } from "../../../aws/runtime/dt-logger.js";
3
+ import { DtLogger } from "../../../aws/runtime/logging/dt-logger.js";
4
4
  const logger = new DtLogger();
5
5
  describe("handler-factory tests", () => {
6
6
  test("test defaults", async () => {
@@ -4,7 +4,7 @@ import { describe, expect, test } from "vitest";
4
4
  // Import from the internal module directly — these helpers are intentionally
5
5
  // not exported from sqs-queue.ts to keep the public API surface clean.
6
6
  import { getDlqCode, sanitizeS3BucketName, } from "../../aws/infra/sqs-queue-internal.js";
7
- import { DtLogger } from "../../aws/runtime/dt-logger.js";
7
+ import { DtLogger } from "../../aws/runtime/logging/dt-logger.js";
8
8
  describe("sqs-queue", () => {
9
9
  test("getDlqCode generates valid JavaScript", () => {
10
10
  const inlineCode = getDlqCode("test-bucket");
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, test, vi } from "vitest";
2
2
  import { getRandomInteger } from "../../__test__/testutils.js";
3
- import { logger } from "../../aws/runtime/dt-logger-default.js";
3
+ import { logger } from "../../aws/runtime/logging/dt-logger-default.js";
4
4
  import { RetryLogError, retry } from "../../utils/retry.js";
5
5
  vi.useFakeTimers();
6
6
  describe("Promise utils tests", () => {
@@ -1,6 +1,6 @@
1
1
  import { Writable } from "node:stream";
2
2
  import { describe, expect, test } from "vitest";
3
- import { DtLogger } from "../../aws/runtime/dt-logger.js";
3
+ import { DtLogger } from "../../aws/runtime/logging/dt-logger.js";
4
4
  const LOG_LINE = {
5
5
  method: "dt-logger.test",
6
6
  message: "FOO",
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,94 @@
1
+ import { EventEmitter } from "node:events";
2
+ import { afterEach, describe, expect, test, vi } from "vitest";
3
+ import { logger } from "../../aws/runtime/logging/dt-logger-default.js";
4
+ import { HttpLogger, LoggerFactory, } from "../../aws/runtime/logging/http-logger.js";
5
+ import { WIDE_CONTEXT } from "../../aws/runtime/logging/wide-context.js";
6
+ vi.mock("../../aws/runtime/logging/dt-logger-default.js", () => ({
7
+ logger: {
8
+ info: vi.fn(),
9
+ },
10
+ }));
11
+ describe("HttpLogger", () => {
12
+ afterEach(() => {
13
+ vi.restoreAllMocks();
14
+ vi.useRealTimers();
15
+ });
16
+ test("logs response status, metadata, content length, and duration", () => {
17
+ vi.useFakeTimers();
18
+ vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
19
+ const infoSpy = vi
20
+ .spyOn(logger, "info")
21
+ .mockImplementation(() => undefined);
22
+ const httpLogger = new HttpLogger({
23
+ context: "Port-call",
24
+ traceId: "trace-123",
25
+ url: "https://example.test/visits/1",
26
+ urlTemplate: "https://example.test/visits/{id}",
27
+ });
28
+ vi.advanceTimersByTime(1250);
29
+ httpLogger.end(200, 512);
30
+ expect(infoSpy).toHaveBeenCalledWith({
31
+ method: "HttpLogger.logStatus",
32
+ [WIDE_CONTEXT.CONTEXT]: "Port-call",
33
+ [WIDE_CONTEXT.TRACE_ID]: "trace-123",
34
+ [WIDE_CONTEXT.HTTP_KEYS.URL]: "https://example.test/visits/1",
35
+ [WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: "https://example.test/visits/{id}",
36
+ [WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: undefined,
37
+ [WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: 200,
38
+ [WIDE_CONTEXT.HTTP_KEYS.DURATION]: 1.25,
39
+ [WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: 512,
40
+ });
41
+ });
42
+ test("logs error name and keeps status code unset when request fails", () => {
43
+ const infoSpy = vi
44
+ .spyOn(logger, "info")
45
+ .mockImplementation(() => undefined);
46
+ const httpLogger = new HttpLogger({
47
+ context: "Test",
48
+ url: "https://example.test",
49
+ });
50
+ httpLogger.error(new TypeError("request failed"));
51
+ expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
52
+ method: "HttpLogger.logStatus",
53
+ [WIDE_CONTEXT.CONTEXT]: "Test",
54
+ [WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: "TypeError",
55
+ [WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: -1,
56
+ [WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: undefined,
57
+ }));
58
+ });
59
+ test.each([
60
+ ["string error", "Connection refused"],
61
+ ["unknown error", { reason: "Connection refused" }],
62
+ ])("classifies %s", (_description, error) => {
63
+ const infoSpy = vi
64
+ .spyOn(logger, "info")
65
+ .mockImplementation(() => undefined);
66
+ const httpLogger = new HttpLogger({
67
+ context: "Test",
68
+ url: "https://example.test",
69
+ });
70
+ httpLogger.error(error);
71
+ expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
72
+ [WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: typeof error === "string" ? error : "Unknown",
73
+ }));
74
+ });
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
+ });
94
+ //# sourceMappingURL=http-logger.test.js.map
@@ -1,5 +1,5 @@
1
1
  import { afterEach, describe, expect, test, vi } from "vitest";
2
- import { logger } from "../../aws/runtime/dt-logger-default.js";
2
+ import { logger } from "../../aws/runtime/logging/dt-logger-default.js";
3
3
  import { logLeadTime, logLeadTimes } from "../../utils/lead-time-logging.js";
4
4
  describe("lead-time-logging", () => {
5
5
  afterEach(() => {
@@ -1,6 +1,6 @@
1
1
  import { Writable } from "node:stream";
2
2
  import { describe, expect, test } from "vitest";
3
- import { DtLogger } from "../../aws/runtime/dt-logger.js";
3
+ import { DtLogger } from "../../aws/runtime/logging/dt-logger.js";
4
4
  import { logException, truncateEnd, truncateMiddle, } from "../../utils/logging.js";
5
5
  const TEST_METHODNAME = "test.logException";
6
6
  describe("logging-test", () => {
@@ -32,6 +32,10 @@ export declare class AclBuilder {
32
32
  * Allow access only from the given addresses
33
33
  */
34
34
  withIpWhitelistRule(addresses: string[]): this;
35
+ /**
36
+ * Block access from the given ISO 3166-1 alpha-2 country codes.
37
+ */
38
+ withGeoBlockRule(countryCodes: string[]): this;
35
39
  /**
36
40
  * Adds a rate-based WAF rule for either blocking or counting requests.
37
41
  *
@@ -48,7 +52,7 @@ export declare class AclBuilder {
48
52
  withThrottleDigitrafficUserIp(limit: number | undefined): this;
49
53
  withThrottleDigitrafficUserIpAndUriPath(limit: number | undefined): this;
50
54
  withThrottleAnonymousUserIp(limit: number | undefined): AclBuilder;
51
- withThrottleAnonymousUserIpByUriPath(limit: number | undefined, path: RegExp | undefined): AclBuilder;
55
+ withThrottleAnonymousUserIpByUriPath(limit: number | undefined, path: RegExp | undefined, ruleId?: string): AclBuilder;
52
56
  /**
53
57
  * Throttles anonymous requests by IP address and URI path.
54
58
  *
@@ -1,6 +1,6 @@
1
1
  import { CfnIPSet, CfnWebACL } from "aws-cdk-lib/aws-wafv2";
2
2
  import { range, zipWith } from "es-toolkit";
3
- import { logger } from "../runtime/dt-logger-default.js";
3
+ import { logger } from "../runtime/logging/dt-logger-default.js";
4
4
  /**
5
5
  * Builder class for building CfnWebACL.
6
6
  *
@@ -91,6 +91,29 @@ export class AclBuilder {
91
91
  });
92
92
  return this;
93
93
  }
94
+ /**
95
+ * Block access from the given ISO 3166-1 alpha-2 country codes.
96
+ */
97
+ withGeoBlockRule(countryCodes) {
98
+ if (countryCodes.length === 0) {
99
+ return this;
100
+ }
101
+ this._blockRules.push({
102
+ name: "GeoBlock",
103
+ action: { block: {} },
104
+ statement: {
105
+ geoMatchStatement: {
106
+ countryCodes,
107
+ },
108
+ },
109
+ visibilityConfig: {
110
+ sampledRequestsEnabled: true,
111
+ cloudWatchMetricsEnabled: true,
112
+ metricName: "GeoBlock",
113
+ },
114
+ });
115
+ return this;
116
+ }
94
117
  /**
95
118
  * Adds a rate-based WAF rule for either blocking or counting requests.
96
119
  *
@@ -163,13 +186,14 @@ export class AclBuilder {
163
186
  this._addThrottleResponseBody(customResponseBodyKey, limit);
164
187
  return this.withThrottleRule("ThrottleRuleWithAnonymousUser", limit, false, false, customResponseBodyKey);
165
188
  }
166
- withThrottleAnonymousUserIpByUriPath(limit, path) {
189
+ withThrottleAnonymousUserIpByUriPath(limit, path, ruleId) {
167
190
  if (limit === undefined || path === undefined) {
168
191
  return this;
169
192
  }
170
193
  const customResponseBodyKey = `IP_THROTTLE_ANONYMOUS_USER_BY_PATH_${limit}`;
171
194
  this._addThrottleResponseBody(customResponseBodyKey, limit);
172
- return this.withThrottleRule("ThrottleRuleWithAnonymousUserByPath", limit, false, false, customResponseBodyKey, path);
195
+ const name = `ThrottleRuleWithAnonymousUserByPath${ruleId ? `-${ruleId}` : ""}`;
196
+ return this.withThrottleRule(name, limit, false, false, customResponseBodyKey, path);
173
197
  }
174
198
  /**
175
199
  * Throttles anonymous requests by IP address and URI path.
@@ -1,4 +1,4 @@
1
- import type { DtLogger } from "../../runtime/dt-logger.js";
1
+ import type { DtLogger } from "../../runtime/logging/dt-logger.js";
2
2
  import type { LambdaResponse } from "../../types/lambda-response.js";
3
3
  export type LoggingHandler = (method: () => Promise<LambdaResponse>, logger: DtLogger) => Promise<LambdaResponse>;
4
4
  export type ErrorHandler = (error: unknown, logger: DtLogger) => LambdaResponse;
@@ -1,5 +1,5 @@
1
1
  import { getEnvVariableOrElse } from "../../../utils/utils.js";
2
- import { logger } from "../../runtime/dt-logger-default.js";
2
+ import { logger } from "../../runtime/logging/dt-logger-default.js";
3
3
  const functionName = getEnvVariableOrElse("AWS_LAMBDA_FUNCTION_NAME", "test");
4
4
  /**
5
5
  * Factory class for creating lambda-handler functions. You can set functionality to handle logging and error-handling,
@@ -1,6 +1,6 @@
1
1
  import synthetics from "Synthetics";
2
2
  import { inDatabaseReadonly } from "../../../database/database.js";
3
- import { logger } from "../../runtime/dt-logger-default.js";
3
+ import { logger } from "../../runtime/logging/dt-logger-default.js";
4
4
  import { ProxyHolder } from "../../runtime/secrets/proxy-holder.js";
5
5
  import { RdsHolder } from "../../runtime/secrets/rds-holder.js";
6
6
  class DatabaseCheck {
@@ -5,7 +5,7 @@ import { isValidGeoJson } from "../../../utils/geometry.js";
5
5
  import { logException } from "../../../utils/logging.js";
6
6
  import { getEnvVariable } from "../../../utils/utils.js";
7
7
  import { getApiKeyFromAPIGateway } from "../../runtime/apikey.js";
8
- import { logger } from "../../runtime/dt-logger-default.js";
8
+ import { logger } from "../../runtime/logging/dt-logger-default.js";
9
9
  import { MediaType } from "../../types/mediatypes.js";
10
10
  import { ENV_API_KEY, ENV_HOSTNAME } from "./canary-keys.js";
11
11
  export const API_KEY_HEADER = "x-api-key";
@@ -8,7 +8,7 @@
8
8
  import { createHash } from "node:crypto";
9
9
  import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
10
10
  import { InlineCode } from "aws-cdk-lib/aws-lambda";
11
- import { logger } from "../runtime/dt-logger-default.js";
11
+ import { logger } from "../runtime/logging/dt-logger-default.js";
12
12
  // CJS format is intentional: CDK InlineCode creates index.js, which Node.js treats as CJS.
13
13
  // ESM would require Code.fromAsset with an .mjs file or a bundled NodejsFunction.
14
14
  const DLQ_LAMBDA_CODE = `
@@ -1,6 +1,6 @@
1
1
  import { lowerFirst, mapKeys } from "es-toolkit";
2
- import { getEnvVariableOrElse } from "../../utils/utils.js";
3
- import { EnvKeys } from "./environment.js";
2
+ import { getEnvVariableOrElse } from "../../../utils/utils.js";
3
+ import { EnvKeys } from "../environment.js";
4
4
  /**
5
5
  * Helper class for json-logging.
6
6
  *
@@ -0,0 +1,57 @@
1
+ import type { Dispatcher } from "undici";
2
+ /**
3
+ * Creates request loggers that share integration-level context.
4
+ *
5
+ * Create one factory for the HTTP integration and use {@link logger} for
6
+ * each outbound request. The optional URL template keeps variable path
7
+ * values out of aggregations while the full URL remains available in logs.
8
+ */
9
+ export declare class LoggerFactory {
10
+ private readonly _context;
11
+ private readonly _traceId?;
12
+ private readonly _urlTemplate?;
13
+ constructor(parameters: {
14
+ context: string;
15
+ traceId?: string;
16
+ urlTemplate?: string;
17
+ });
18
+ /** Creates a timer and logger for one outbound HTTP request. */
19
+ logger(url: string): HttpLogger;
20
+ }
21
+ /**
22
+ * Logs the outcome and duration of one outbound HTTP request.
23
+ *
24
+ * Call {@link end} when a response status is available. Call {@link error}
25
+ * when the request fails before a response is received. Each terminal method
26
+ * writes one info-level log entry containing the request context, URL, status
27
+ * or error type, and duration in seconds.
28
+ */
29
+ export declare class HttpLogger {
30
+ private readonly _context;
31
+ private readonly _url;
32
+ private readonly _urlTemplate?;
33
+ private readonly _startTime;
34
+ private readonly _traceId?;
35
+ private _errorType?;
36
+ private _contentLength?;
37
+ private _statusCode;
38
+ constructor(parameters: {
39
+ context: string;
40
+ url: string;
41
+ traceId?: string;
42
+ urlTemplate?: string;
43
+ });
44
+ /** Records a received HTTP response and writes the request log entry. */
45
+ end(statusCode: number, contentLength?: number): void;
46
+ /** Hook logging into response events */
47
+ log(response: Dispatcher.ResponseData<unknown>): void;
48
+ /**
49
+ * Records a failed request and writes the request log entry.
50
+ *
51
+ * Errors are represented by their name, strings are kept as-is, and
52
+ * other values are recorded as `Unknown`. Since no response was received,
53
+ * the status code remains `-1`.
54
+ */
55
+ error(error: unknown): void;
56
+ private logStatus;
57
+ }
@@ -0,0 +1,108 @@
1
+ import { logger } from "./dt-logger-default.js";
2
+ import { WIDE_CONTEXT } from "./wide-context.js";
3
+ const METHOD = "HttpLogger.logStatus";
4
+ /**
5
+ * Creates request loggers that share integration-level context.
6
+ *
7
+ * Create one factory for the HTTP integration and use {@link logger} for
8
+ * each outbound request. The optional URL template keeps variable path
9
+ * values out of aggregations while the full URL remains available in logs.
10
+ */
11
+ export class LoggerFactory {
12
+ _context;
13
+ _traceId;
14
+ _urlTemplate;
15
+ constructor(parameters) {
16
+ this._context = parameters.context;
17
+ this._traceId = parameters.traceId;
18
+ this._urlTemplate = parameters.urlTemplate;
19
+ }
20
+ /** Creates a timer and logger for one outbound HTTP request. */
21
+ logger(url) {
22
+ return new HttpLogger({
23
+ context: this._context,
24
+ url,
25
+ traceId: this._traceId,
26
+ urlTemplate: this._urlTemplate,
27
+ });
28
+ }
29
+ }
30
+ /**
31
+ * Logs the outcome and duration of one outbound HTTP request.
32
+ *
33
+ * Call {@link end} when a response status is available. Call {@link error}
34
+ * when the request fails before a response is received. Each terminal method
35
+ * writes one info-level log entry containing the request context, URL, status
36
+ * or error type, and duration in seconds.
37
+ */
38
+ export class HttpLogger {
39
+ _context;
40
+ _url;
41
+ _urlTemplate;
42
+ _startTime;
43
+ _traceId;
44
+ _errorType;
45
+ _contentLength;
46
+ _statusCode = -1;
47
+ constructor(parameters) {
48
+ this._context = parameters.context;
49
+ this._url = parameters.url;
50
+ this._traceId = parameters.traceId;
51
+ this._urlTemplate = parameters.urlTemplate;
52
+ this._startTime = Date.now();
53
+ }
54
+ /** Records a received HTTP response and writes the request log entry. */
55
+ end(statusCode, contentLength) {
56
+ this._statusCode = statusCode;
57
+ this._contentLength = contentLength;
58
+ this.logStatus();
59
+ }
60
+ /** Hook logging into response events */
61
+ log(response) {
62
+ this._statusCode = response.statusCode;
63
+ const contentLengthHeader = response.headers["content-length"];
64
+ const contentLength = contentLengthHeader
65
+ ? parseInt(contentLengthHeader, 10)
66
+ : undefined;
67
+ response.body.on("error", (err) => {
68
+ this.error(err);
69
+ });
70
+ response.body.on("end", () => {
71
+ this.end(response.statusCode, contentLength);
72
+ });
73
+ }
74
+ /**
75
+ * Records a failed request and writes the request log entry.
76
+ *
77
+ * Errors are represented by their name, strings are kept as-is, and
78
+ * other values are recorded as `Unknown`. Since no response was received,
79
+ * the status code remains `-1`.
80
+ */
81
+ error(error) {
82
+ if (typeof error === "string") {
83
+ this._errorType = error;
84
+ }
85
+ else if (error instanceof Error) {
86
+ this._errorType = error.name;
87
+ }
88
+ else {
89
+ this._errorType = "Unknown";
90
+ }
91
+ this.logStatus();
92
+ }
93
+ logStatus() {
94
+ const endTime = Date.now();
95
+ logger.info({
96
+ method: METHOD,
97
+ [WIDE_CONTEXT.CONTEXT]: this._context,
98
+ [WIDE_CONTEXT.TRACE_ID]: this._traceId,
99
+ [WIDE_CONTEXT.HTTP_KEYS.URL]: this._url,
100
+ [WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: this._urlTemplate,
101
+ [WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: this._errorType,
102
+ [WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: this._statusCode,
103
+ [WIDE_CONTEXT.HTTP_KEYS.DURATION]: (endTime - this._startTime) / 1000,
104
+ [WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: this._contentLength,
105
+ });
106
+ }
107
+ }
108
+ //# sourceMappingURL=http-logger.js.map
@@ -0,0 +1,12 @@
1
+ export declare const WIDE_CONTEXT: {
2
+ readonly TRACE_ID: "customTrace_id";
3
+ readonly CONTEXT: "customContext";
4
+ readonly HTTP_KEYS: {
5
+ readonly DURATION: "customHttp.client.request.duration";
6
+ readonly ERROR_TYPE: "customRequest.error.type";
7
+ readonly STATUS_CODE: "customHttp.response.status_code";
8
+ readonly URL: "customRequest.url.full";
9
+ readonly URL_TEMPLATE: "customRequest.url.template";
10
+ readonly CONTENT_LENGTH: "customHttp.response.body.size";
11
+ };
12
+ };
@@ -0,0 +1,13 @@
1
+ export const WIDE_CONTEXT = {
2
+ TRACE_ID: "customTrace_id",
3
+ CONTEXT: "customContext",
4
+ HTTP_KEYS: {
5
+ DURATION: "customHttp.client.request.duration",
6
+ ERROR_TYPE: "customRequest.error.type",
7
+ STATUS_CODE: "customHttp.response.status_code",
8
+ URL: "customRequest.url.full",
9
+ URL_TEMPLATE: "customRequest.url.template",
10
+ CONTENT_LENGTH: "customHttp.response.body.size",
11
+ },
12
+ };
13
+ //# sourceMappingURL=wide-context.js.map
@@ -1,5 +1,5 @@
1
1
  import { PutObjectCommand } from "@aws-sdk/client-s3";
2
- import { logger } from "./dt-logger-default.js";
2
+ import { logger } from "./logging/dt-logger-default.js";
3
3
  export async function uploadToS3(s3, bucketName, body, objectName, cannedAcl, contentType) {
4
4
  const command = new PutObjectCommand({
5
5
  Bucket: bucketName,
@@ -1,4 +1,4 @@
1
- import { logger } from "../dt-logger-default.js";
1
+ import { logger } from "../logging/dt-logger-default.js";
2
2
  export var RdsProxySecretKey;
3
3
  (function (RdsProxySecretKey) {
4
4
  RdsProxySecretKey["username"] = "username";
@@ -1,5 +1,5 @@
1
1
  import { getEnvVariable } from "../../../utils/utils.js";
2
- import { logger } from "../dt-logger-default.js";
2
+ import { logger } from "../logging/dt-logger-default.js";
3
3
  import { checkExpectedSecretKeys } from "./dbsecret.js";
4
4
  import { getSecret } from "./secret.js";
5
5
  const DEFAULT_PREFIX = "";
@@ -2,7 +2,7 @@ import etag from "etag";
2
2
  import { compressBuffer, decodeBase64ToUtf8 } from "../../utils/base64.js";
3
3
  import { truncateMiddle } from "../../utils/logging.js";
4
4
  import { StopWatch } from "../../utils/stop-watch.js";
5
- import { logger } from "../runtime/dt-logger-default.js";
5
+ import { logger } from "../runtime/logging/dt-logger-default.js";
6
6
  import { MediaType } from "./mediatypes.js";
7
7
  export const MAX_LAMBDA_PAYLOAD_BYTES = 6 * 1024 * 1024; // 6 MiB
8
8
  export const REDIRECTION_HTTP_STATUS_CODES = [301, 302, 303, 307, 308];
@@ -2,7 +2,7 @@ import etag from "etag";
2
2
  import { encodeUtf8ToBase64 } from "../../utils/base64.js";
3
3
  import { truncateMiddle } from "../../utils/logging.js";
4
4
  import { StopWatch } from "../../utils/stop-watch.js";
5
- import { logger } from "../runtime/dt-logger-default.js";
5
+ import { logger } from "../runtime/logging/dt-logger-default.js";
6
6
  import { MAX_LAMBDA_PAYLOAD_BYTES } from "./lambda-proxy-types.js";
7
7
  export class LambdaResponse {
8
8
  status;
@@ -1,5 +1,5 @@
1
1
  import pgpImport from "pg-promise";
2
- import { logger } from "../aws/runtime/dt-logger-default.js";
2
+ import { logger } from "../aws/runtime/logging/dt-logger-default.js";
3
3
  import { logException } from "../utils/logging.js";
4
4
  import { getEnvVariable, getEnvVariableOrElse } from "../utils/utils.js";
5
5
  export var DatabaseEnvironmentKeys;
@@ -1,4 +1,4 @@
1
- import { logger } from "../aws/runtime/dt-logger-default.js";
1
+ import { logger } from "../aws/runtime/logging/dt-logger-default.js";
2
2
  export function isValidIMO(imo) {
3
3
  return imo >= 1000000 && imo <= 9999999 && imoChecksumIsValid(imo);
4
4
  }
@@ -1,5 +1,5 @@
1
1
  import * as geoJsonValidator from "geojson-validation";
2
- import { logger } from "../aws/runtime/dt-logger-default.js";
2
+ import { logger } from "../aws/runtime/logging/dt-logger-default.js";
3
3
  export const SRID_WGS84 = 4326;
4
4
  /**
5
5
  * Creates WKT geometry from GeoJSON geometry
@@ -1,4 +1,4 @@
1
- import type { CustomParams } from "../aws/runtime/dt-logger.js";
1
+ import type { CustomParams } from "../aws/runtime/logging/dt-logger.js";
2
2
  export interface LeadTimeLogging {
3
3
  readonly target: string;
4
4
  readonly leadTimeMs?: number;
@@ -1,4 +1,4 @@
1
- import { logger } from "../aws/runtime/dt-logger-default.js";
1
+ import { logger } from "../aws/runtime/logging/dt-logger-default.js";
2
2
  export function logLeadTime(name, target, tookMs, extraFields) {
3
3
  const method = "LeadTimeLogging.logLeadTime";
4
4
  try {
@@ -1,4 +1,4 @@
1
- import { DtLogger } from "../aws/runtime/dt-logger.js";
1
+ import { DtLogger } from "../aws/runtime/logging/dt-logger.js";
2
2
  /**
3
3
  * Curried version of logException.
4
4
  *
@@ -1,4 +1,4 @@
1
- import { DtLogger } from "../aws/runtime/dt-logger.js";
1
+ import { DtLogger } from "../aws/runtime/logging/dt-logger.js";
2
2
  import { getEnvVariableOrElse } from "./utils.js";
3
3
  const functionName = getEnvVariableOrElse("AWS_LAMBDA_FUNCTION_NAME", "test");
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { logger } from "../aws/runtime/dt-logger-default.js";
1
+ import { logger } from "../aws/runtime/logging/dt-logger-default.js";
2
2
  import { AsyncTimeoutError } from "../types/async-timeout-error.js";
3
3
  import { HttpError } from "../types/http-error.js";
4
4
  export var RetryLogError;
@@ -1,5 +1,5 @@
1
1
  import ky from "ky";
2
- import { logger } from "../aws/runtime/dt-logger-default.js";
2
+ import { logger } from "../aws/runtime/logging/dt-logger-default.js";
3
3
  import { logException } from "./logging.js";
4
4
  export class SlackApi {
5
5
  url;
@@ -1,4 +1,4 @@
1
- import type { DtLogger, LoggerMethodType } from "../aws/runtime/dt-logger.js";
1
+ import type { DtLogger, LoggerMethodType } from "../aws/runtime/logging/dt-logger.js";
2
2
  export interface TaskPeriod {
3
3
  startTime: number;
4
4
  endTime?: number;
@@ -1,4 +1,4 @@
1
- import { logger } from "../aws/runtime/dt-logger-default.js";
1
+ import { logger } from "../aws/runtime/logging/dt-logger-default.js";
2
2
  export class StopWatch {
3
3
  tasks = new Map();
4
4
  static DEFAULT_TASK_NAME = "DEFAULT_INTERNAL_TASK";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2026.10.2-2",
3
+ "version": "2026.10.26-1",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "repository": {
@@ -92,13 +92,15 @@
92
92
  "./dist/aws/types/model-with-reference": "./dist/aws/types/model-with-reference.js",
93
93
  "./dist/aws/types/errors": "./dist/aws/types/errors.js",
94
94
  "./dist/aws/types/lambda-response": "./dist/aws/types/lambda-response.js",
95
- "./dist/aws/runtime/dt-logger-default": "./dist/aws/runtime/dt-logger-default.js",
95
+ "./dist/aws/runtime/logging/dt-logger-default": "./dist/aws/runtime/logging/dt-logger-default.js",
96
+ "./dist/aws/runtime/logging/dt-logger": "./dist/aws/runtime/logging/dt-logger.js",
97
+ "./dist/aws/runtime/logging/http-logger": "./dist/aws/runtime/logging/http-logger.js",
98
+ "./dist/aws/runtime/logging/wide-context": "./dist/aws/runtime/logging/wide-context.js",
96
99
  "./dist/aws/runtime/secrets/secret": "./dist/aws/runtime/secrets/secret.js",
97
100
  "./dist/aws/runtime/secrets/proxy-holder": "./dist/aws/runtime/secrets/proxy-holder.js",
98
101
  "./dist/aws/runtime/secrets/dbsecret": "./dist/aws/runtime/secrets/dbsecret.js",
99
102
  "./dist/aws/runtime/secrets/rds-holder": "./dist/aws/runtime/secrets/rds-holder.js",
100
103
  "./dist/aws/runtime/secrets/secret-holder": "./dist/aws/runtime/secrets/secret-holder.js",
101
- "./dist/aws/runtime/dt-logger": "./dist/aws/runtime/dt-logger.js",
102
104
  "./dist/aws/runtime/s3": "./dist/aws/runtime/s3.js",
103
105
  "./dist/aws/runtime/apikey": "./dist/aws/runtime/apikey.js",
104
106
  "./dist/aws/runtime/environment": "./dist/aws/runtime/environment.js",
@@ -127,7 +129,7 @@
127
129
  "@types/geojson-validation": "1.0.3",
128
130
  "@types/madge": "5.0.3",
129
131
  "@types/node": "24.13.3",
130
- "@vitest/coverage-v8": "4.1.11",
132
+ "@vitest/coverage-v8": "4.1.12",
131
133
  "aws-cdk-lib": "2.262.2",
132
134
  "constructs": "10.7.2",
133
135
  "date-fns": "4.4.0",
@@ -143,8 +145,9 @@
143
145
  "rimraf": "6.1.3",
144
146
  "sort-package-json": "4.0.0",
145
147
  "typescript": "5.9.3",
148
+ "undici": "8.12.2",
146
149
  "velocityjs": "2.1.7",
147
- "vitest": "4.1.11",
150
+ "vitest": "4.1.12",
148
151
  "zod": "4.4.3"
149
152
  },
150
153
  "peerDependencies": {
@@ -164,6 +167,7 @@
164
167
  "ky": "2.0.2",
165
168
  "pg-native": "3.8.0",
166
169
  "pg-promise": "12.7.0",
170
+ "undici": "8.12.2",
167
171
  "zod": "4.4.3"
168
172
  },
169
173
  "engines": {