@digitraffic/common 2025.4.9-1 → 2025.4.29-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.
@@ -55,14 +55,20 @@ describe("dt-logger", () => {
55
55
  }
56
56
  expect(loggedLine).toMatchObject(expected);
57
57
  }
58
- test("custom values", () => {
58
+ test("custom date, number, text and '=' values", () => {
59
59
  const date = new Date();
60
60
  assertLog({}, {
61
61
  ...LOG_LINE,
62
62
  customDate: date,
63
+ customNumber: 123,
64
+ customText: "abc",
65
+ customEqualsText: "foo=bar",
63
66
  }, {
64
- ...EXPECTED_LOG_LINE,
67
+ method: EXPECTED_LOG_LINE.method,
68
+ message: `${EXPECTED_LOG_LINE.message} date=${date.toISOString()} number=123 text=abc equalsText=\"foo=bar\"`,
65
69
  date: date.toISOString(),
70
+ number: 123,
71
+ text: "abc",
66
72
  });
67
73
  });
68
74
  test("custom count should be a number", () => {
@@ -70,7 +76,8 @@ describe("dt-logger", () => {
70
76
  ...LOG_LINE,
71
77
  customFooCount: 123,
72
78
  }, {
73
- ...EXPECTED_LOG_LINE,
79
+ method: EXPECTED_LOG_LINE.method,
80
+ message: `${EXPECTED_LOG_LINE.message} fooCount=123`,
74
81
  fooCount: 123,
75
82
  });
76
83
  });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import { decodeBase64ToAscii, decodeBase64ToUtf8, encodeAsciiToBase64, encodeUtf8ToBase64, } from "../../utils/base64.js";
2
+ describe("Base64UtilTest", () => {
3
+ const EXPECTED_ASCII = "0a9f4967-faaa-445a-ba11-078f7f9556b4";
4
+ const EXPECTED_ASCII_BASE64 = "MGE5ZjQ5NjctZmFhYS00NDVhLWJhMTEtMDc4ZjdmOTU1NmI0";
5
+ const EXPECTED_UTF8 = '욤頶ɠ_񝝉T񶅶𞜫7郣Ҧ㒢,"鏤ځ峡蟅͆뼐񭫟ؐԓ外󦸴ԉȺꥅ_񟵭';
6
+ const EXPECTED_UTF8_BASE64 = "7Jqk6aC2yaBf8Z2diVTxtoW28J6cqzfug6Ppg6PSpuOSou6Usywi6Y+k2oHls6Hon4XNhuu8kPGtq5/YkNST5aSW86a4tNSJyLrqpYVf8Z+1rQ==";
7
+ test("encode and decode ascii", () => {
8
+ const encodedAscii = encodeAsciiToBase64(EXPECTED_ASCII);
9
+ const decodedAscii = decodeBase64ToAscii(EXPECTED_ASCII_BASE64);
10
+ // encoding/decoding ascii with utf8 should be the same as ascii
11
+ const encodedUtf8 = encodeUtf8ToBase64(EXPECTED_ASCII);
12
+ const decodedUtf8 = decodeBase64ToUtf8(EXPECTED_ASCII_BASE64);
13
+ expect(encodedAscii).toEqual(EXPECTED_ASCII_BASE64);
14
+ expect(decodedAscii).toEqual(EXPECTED_ASCII);
15
+ expect(encodedUtf8).toEqual(EXPECTED_ASCII_BASE64);
16
+ expect(decodedUtf8).toEqual(EXPECTED_ASCII);
17
+ expect(encodedUtf8).toEqual(encodedAscii);
18
+ expect(decodedUtf8).toEqual(decodedAscii);
19
+ });
20
+ test("encode and decode utf8", () => {
21
+ const encodedUtf8 = encodeUtf8ToBase64(EXPECTED_UTF8);
22
+ const decodedUtf8 = decodeBase64ToUtf8(EXPECTED_UTF8_BASE64);
23
+ expect(encodedUtf8).toEqual(EXPECTED_UTF8_BASE64);
24
+ expect(decodedUtf8).toEqual(EXPECTED_UTF8);
25
+ });
26
+ });
27
+ //# sourceMappingURL=base64.test.js.map
@@ -2,7 +2,7 @@ import { Writable } from "stream";
2
2
  import { DtLogger } from "../../aws/runtime/dt-logger.js";
3
3
  import { logException } from "../../utils/logging.js";
4
4
  const TEST_METHODNAME = "test.logException";
5
- describe("dt-logger", () => {
5
+ describe("logging-test", () => {
6
6
  function assertLogError(error, expected, includeStack = false) {
7
7
  assertWrite((logger) => {
8
8
  logException(logger, error, includeStack);
@@ -40,7 +40,7 @@ describe("dt-logger", () => {
40
40
  assertLogError(STRING_ERROR, {
41
41
  type: "Error",
42
42
  method: TEST_METHODNAME,
43
- message: `${TEST_METHODNAME} ${STRING_ERROR}`,
43
+ message: `${TEST_METHODNAME} error=${STRING_ERROR} type=Error`,
44
44
  level: "ERROR",
45
45
  });
46
46
  });
@@ -49,7 +49,7 @@ describe("dt-logger", () => {
49
49
  assertLogError(ERROR, {
50
50
  type: "Error",
51
51
  method: TEST_METHODNAME,
52
- message: `${TEST_METHODNAME} ${ERROR.message}`,
52
+ message: `${TEST_METHODNAME} error=${ERROR.message} type=Error`,
53
53
  level: "ERROR",
54
54
  });
55
55
  });
@@ -58,7 +58,7 @@ describe("dt-logger", () => {
58
58
  assertLogError(ERROR, {
59
59
  type: "Error",
60
60
  method: TEST_METHODNAME,
61
- message: `${TEST_METHODNAME} ${ERROR.message}`,
61
+ message: `${TEST_METHODNAME} error=${ERROR.message} type=Error`,
62
62
  level: "ERROR",
63
63
  stack: true,
64
64
  }, true);
@@ -69,7 +69,7 @@ describe("dt-logger", () => {
69
69
  assertAxiosError(ERROR, {
70
70
  type: "Error",
71
71
  method: TEST_METHODNAME,
72
- message: `${TEST_METHODNAME} ${ERROR.message}`,
72
+ message: `${TEST_METHODNAME} error=${ERROR.message} type=Error code=12`,
73
73
  level: "ERROR",
74
74
  code: ERROR.code,
75
75
  });
@@ -113,4 +113,5 @@ export declare class DtLogger {
113
113
  * @see {@link LoggableType}
114
114
  */
115
115
  private log;
116
+ private appendMessageFieldsToMessage;
116
117
  }
@@ -102,8 +102,10 @@ export class DtLogger {
102
102
  : message.error
103
103
  ? (message.error instanceof Error) ? message.error.stack : undefined
104
104
  : undefined;
105
+ const messageFields = removePrefix("custom", message);
106
+ messageFields.message = this.appendMessageFieldsToMessage(messageFields);
105
107
  const logMessage = {
106
- ...removePrefix("custom", message),
108
+ ...messageFields,
107
109
  error,
108
110
  stack,
109
111
  lambdaName: this.lambdaName,
@@ -111,6 +113,18 @@ export class DtLogger {
111
113
  };
112
114
  this.writeStream.write(JSON.stringify(logMessage) + "\n");
113
115
  }
116
+ appendMessageFieldsToMessage(message) {
117
+ // The order is not guaranteed to be alphabetical etc.
118
+ const fielValuePairs = Object.entries(message)
119
+ // Remove fields not logged in message field
120
+ .filter(([key]) => {
121
+ return !["message", "level", "method", "stack", "error"].includes(key) && !(message.message ?? "").includes(`${key}=`);
122
+ })
123
+ // Map value to string
124
+ .map(([key, value]) => `${key}=${valueToString(value)}`)
125
+ .join(" ");
126
+ return `${message.message ?? ""}${fielValuePairs ? " " + fielValuePairs : ""}`;
127
+ }
114
128
  }
115
129
  /**
116
130
  * Removes given prefixes from the keys of the object.
@@ -118,4 +132,22 @@ export class DtLogger {
118
132
  function removePrefix(prefix, loggable) {
119
133
  return mapKeys(loggable, (_index, key) => key.startsWith(prefix) ? lowerFirst(key.replace(prefix, "")) : key);
120
134
  }
135
+ function valueToString(value) {
136
+ if (value === undefined) {
137
+ return "undefined";
138
+ }
139
+ else if (value === null) {
140
+ return "null";
141
+ }
142
+ else if (value instanceof Date) {
143
+ return value.toISOString();
144
+ }
145
+ else if (typeof value !== "string") {
146
+ return JSON.stringify(value);
147
+ }
148
+ else if (value.includes("=")) {
149
+ return JSON.stringify(value);
150
+ }
151
+ return value;
152
+ }
121
153
  //# sourceMappingURL=dt-logger.js.map
@@ -1,11 +1,32 @@
1
1
  /**
2
- * Decode given string from base64 to ascii
3
- * @param str string
2
+ * Encode given string to base64
3
+ * @param str
4
+ * @param encoding
4
5
  */
5
- export declare function decodeBase64ToAscii(str: string): string;
6
+ export declare function encodeBase64(str: string, encoding: BufferEncoding): string;
6
7
  /**
7
8
  * Decode given string from base64 to given encoding
8
9
  * @param str
9
10
  * @param encoding
10
11
  */
11
12
  export declare function decodeBase64(str: string, encoding: BufferEncoding): string;
13
+ /**
14
+ * Encode given string from ascii to Base64 string
15
+ * @param token
16
+ */
17
+ export declare function encodeAsciiToBase64(token: string): string;
18
+ /**
19
+ * Decode given string from base64 to ascii
20
+ * @param str string
21
+ */
22
+ export declare function decodeBase64ToAscii(str: string): string;
23
+ /**
24
+ * Encode given string from base64 to utf8 string
25
+ * @param str
26
+ */
27
+ export declare function encodeUtf8ToBase64(str: string): string;
28
+ /**
29
+ * Decode given string from base64 to utf8 string
30
+ * @param str
31
+ */
32
+ export declare function decodeBase64ToUtf8(str: string): string;
@@ -1,9 +1,10 @@
1
1
  /**
2
- * Decode given string from base64 to ascii
3
- * @param str string
2
+ * Encode given string to base64
3
+ * @param str
4
+ * @param encoding
4
5
  */
5
- export function decodeBase64ToAscii(str) {
6
- return decodeBase64(str, "ascii");
6
+ export function encodeBase64(str, encoding) {
7
+ return Buffer.from(str, encoding).toString("base64");
7
8
  }
8
9
  /**
9
10
  * Decode given string from base64 to given encoding
@@ -13,4 +14,32 @@ export function decodeBase64ToAscii(str) {
13
14
  export function decodeBase64(str, encoding) {
14
15
  return Buffer.from(str, "base64").toString(encoding);
15
16
  }
17
+ /**
18
+ * Encode given string from ascii to Base64 string
19
+ * @param token
20
+ */
21
+ export function encodeAsciiToBase64(token) {
22
+ return encodeBase64(token, "ascii");
23
+ }
24
+ /**
25
+ * Decode given string from base64 to ascii
26
+ * @param str string
27
+ */
28
+ export function decodeBase64ToAscii(str) {
29
+ return decodeBase64(str, "ascii");
30
+ }
31
+ /**
32
+ * Encode given string from base64 to utf8 string
33
+ * @param str
34
+ */
35
+ export function encodeUtf8ToBase64(str) {
36
+ return encodeBase64(str, "utf8");
37
+ }
38
+ /**
39
+ * Decode given string from base64 to utf8 string
40
+ * @param str
41
+ */
42
+ export function decodeBase64ToUtf8(str) {
43
+ return decodeBase64(str, "utf8");
44
+ }
16
45
  //# sourceMappingURL=base64.js.map
@@ -53,12 +53,22 @@ export function logException(logger, error, includeStack = false) {
53
53
  // In case error is AxiosError, log the custom code property.
54
54
  // eslint-disable-next-line dot-notation
55
55
  const customCode = error["code"];
56
- logger.error({
57
- type: "Error",
58
- method: `${functionName}.logException`,
59
- message,
60
- customCode,
61
- stack,
62
- });
56
+ if (customCode) {
57
+ logger.error({
58
+ type: "Error",
59
+ method: `${functionName}.logException`,
60
+ message: `error=${message}`,
61
+ customCode,
62
+ stack,
63
+ });
64
+ }
65
+ else {
66
+ logger.error({
67
+ type: "Error",
68
+ method: `${functionName}.logException`,
69
+ message: `error=${message}`,
70
+ stack,
71
+ });
72
+ }
63
73
  }
64
74
  //# sourceMappingURL=logging.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digitraffic/common",
3
- "version": "2025.4.9-1",
3
+ "version": "2025.4.29-1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "repository": {
@@ -111,7 +111,7 @@
111
111
  "@aws-sdk/lib-storage": "^3.777.0",
112
112
  "@date-fns/tz": "1.2.0",
113
113
  "@smithy/fetch-http-handler": "5.0.2",
114
- "aws-cdk-lib": "^2.188.0",
114
+ "aws-cdk-lib": "^2.189.0",
115
115
  "change-case": "^5.4.4",
116
116
  "constructs": "~10.4.2",
117
117
  "date-fns": "~4.1.0",
@@ -151,7 +151,7 @@
151
151
  "@types/node": "22.13.14",
152
152
  "@typescript-eslint/eslint-plugin": "~7.14.1",
153
153
  "@typescript-eslint/parser": "^7.18.0",
154
- "aws-cdk-lib": "^2.188.0",
154
+ "aws-cdk-lib": "^2.189.0",
155
155
  "aws-sdk": "^2.1692.0",
156
156
  "change-case": "^5.4.4",
157
157
  "constructs": "~10.4.2",