@moreapp/common-nodejs 0.12.2 → 0.12.4

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 (31) hide show
  1. package/dist/__tests__/MoreAppClient.test.js +85 -0
  2. package/dist/__tests__/dateUtil.test.js +42 -0
  3. package/dist/{logger.test.js → __tests__/logger.test.js} +23 -10
  4. package/dist/__tests__/utils.test.js +124 -0
  5. package/dist/dateUtil.js +38 -8
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/logger.js +2 -1
  9. package/dist/observability/TerminationHandler.js +5 -1
  10. package/dist/observability/{ExpressRequestTracker.test.js → __tests__/ExpressRequestTracker.test.js} +16 -15
  11. package/dist/observability/{ObservabilityServer.test.js → __tests__/ObservabilityServer.test.js} +30 -23
  12. package/dist/observability/__tests__/TerminationHandler.test.js +171 -0
  13. package/dist/observability/index.d.ts +2 -1
  14. package/dist/observability/index.js +3 -1
  15. package/dist/observability/tracer.d.ts +12 -0
  16. package/dist/{tracer.js → observability/tracer.js} +15 -7
  17. package/package.json +12 -12
  18. package/dist/MoreAppClient.test.js +0 -79
  19. package/dist/dateUtil.test.js +0 -41
  20. package/dist/observability/TerminationHandler.test.js +0 -160
  21. package/dist/testUtils.d.ts +0 -1
  22. package/dist/testUtils.js +0 -13
  23. package/dist/tracer.d.ts +0 -8
  24. package/dist/utils.test.js +0 -123
  25. /package/dist/{MoreAppClient.test.d.ts → __tests__/MoreAppClient.test.d.ts} +0 -0
  26. /package/dist/{dateUtil.test.d.ts → __tests__/dateUtil.test.d.ts} +0 -0
  27. /package/dist/{logger.test.d.ts → __tests__/logger.test.d.ts} +0 -0
  28. /package/dist/{utils.test.d.ts → __tests__/utils.test.d.ts} +0 -0
  29. /package/dist/observability/{ExpressRequestTracker.test.d.ts → __tests__/ExpressRequestTracker.test.d.ts} +0 -0
  30. /package/dist/observability/{ObservabilityServer.test.d.ts → __tests__/ObservabilityServer.test.d.ts} +0 -0
  31. /package/dist/observability/{TerminationHandler.test.d.ts → __tests__/TerminationHandler.test.d.ts} +0 -0
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const vitest_1 = require("vitest");
7
+ const nock_1 = __importDefault(require("nock"));
8
+ const MoreAppClient_1 = __importDefault(require("../MoreAppClient"));
9
+ (0, vitest_1.describe)("MoreAppClient", () => {
10
+ (0, vitest_1.test)("download binary file", async () => {
11
+ const file = Buffer.from("Some data");
12
+ const apiMock = (0, nock_1.default)("https://api.moreapp.com")
13
+ .get("/download")
14
+ .matchHeader("x-more-seal", "my-seal")
15
+ .reply(200, file, {
16
+ "Content-Type": "image/jpg",
17
+ "content-disposition": 'attachment; filename="my%20photo.jpg"',
18
+ });
19
+ const client = new MoreAppClient_1.default({
20
+ serviceName: "Common Test",
21
+ prefix: "https://api.moreapp.com",
22
+ seal: "my-seal",
23
+ });
24
+ const binaryFile = await client.getBinary("/download");
25
+ (0, vitest_1.expect)(binaryFile.buffer).toStrictEqual(file);
26
+ (0, vitest_1.expect)(binaryFile.contentType).toBe("image/jpg");
27
+ (0, vitest_1.expect)(binaryFile.filename).toBe("my photo.jpg");
28
+ apiMock.done();
29
+ });
30
+ (0, vitest_1.test)("404", async () => {
31
+ const apiMock = (0, nock_1.default)("https://api.moreapp.com").get("/download").reply(404);
32
+ const client = new MoreAppClient_1.default({
33
+ serviceName: "Common Test",
34
+ prefix: "https://api.moreapp.com",
35
+ seal: "my-seal",
36
+ });
37
+ await (0, vitest_1.expect)(client.getBinary("/download")).rejects.toMatchObject({
38
+ message: "Request failed with status code 404",
39
+ name: "AxiosError",
40
+ status: 404,
41
+ });
42
+ apiMock.done();
43
+ });
44
+ (0, vitest_1.test)("unable to connect", async () => {
45
+ const client = new MoreAppClient_1.default({
46
+ serviceName: "Common Test",
47
+ prefix: "https://non-existing.moreapp.com",
48
+ seal: "my-seal",
49
+ });
50
+ await (0, vitest_1.expect)(client.getBinary("/download")).rejects.toThrow("getaddrinfo ENOTFOUND non-existing.moreapp.com");
51
+ });
52
+ const client = new MoreAppClient_1.default({
53
+ serviceName: "Common Test",
54
+ prefix: "https://api.moreapp.com",
55
+ seal: "my-seal",
56
+ });
57
+ (0, vitest_1.test)("Handle non existing file", async () => {
58
+ const apiMock = (0, nock_1.default)("https://api.moreapp.com").get("/download").reply(404);
59
+ await (0, vitest_1.expect)(client.getBinary("/download")).rejects.toThrow("Request failed with status code 404");
60
+ apiMock.done();
61
+ });
62
+ (0, vitest_1.test)("Handle missing 'content-type' header", async () => {
63
+ const apiMock = (0, nock_1.default)("https://api.moreapp.com").get("/download").reply(200);
64
+ const binaryFile = await client.getBinary("/download");
65
+ (0, vitest_1.expect)(binaryFile).not.toBeNull();
66
+ (0, vitest_1.expect)(binaryFile.contentType).toBe("unknown");
67
+ apiMock.done();
68
+ });
69
+ (0, vitest_1.test)("Handle missing 'content-disposition' header", async () => {
70
+ const apiMock = (0, nock_1.default)("https://api.moreapp.com").get("/download").reply(200);
71
+ const binaryFile = await client.getBinary("/download");
72
+ (0, vitest_1.expect)(binaryFile).not.toBeNull();
73
+ (0, vitest_1.expect)(binaryFile.filename).toBeUndefined();
74
+ apiMock.done();
75
+ });
76
+ (0, vitest_1.test)("Handle invalid 'content-disposition' header", async () => {
77
+ const apiMock = (0, nock_1.default)("https://api.moreapp.com").get("/download").reply(200, {}, {
78
+ "content-disposition": "filename=my-photo.png",
79
+ });
80
+ const binaryFile = await client.getBinary("/download");
81
+ (0, vitest_1.expect)(binaryFile).not.toBeNull();
82
+ (0, vitest_1.expect)(binaryFile.filename).toBeUndefined();
83
+ apiMock.done();
84
+ });
85
+ });
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const dateUtil_1 = require("../dateUtil");
5
+ (0, vitest_1.describe)("isDate", () => {
6
+ (0, vitest_1.test)("should handle valid dates", () => {
7
+ (0, vitest_1.expect)((0, dateUtil_1.isDate)("2020-01-01")).toBe(true);
8
+ (0, vitest_1.expect)((0, dateUtil_1.isDate)("2020-1-1")).toBe(true);
9
+ (0, vitest_1.expect)((0, dateUtil_1.isDate)("0000-1-1")).toBe(true);
10
+ });
11
+ (0, vitest_1.test)("should handle invalid dates", () => {
12
+ (0, vitest_1.expect)((0, dateUtil_1.isDate)("not-a-date")).toBe(false);
13
+ (0, vitest_1.expect)((0, dateUtil_1.isDate)("3039-20-01")).toBe(false);
14
+ });
15
+ });
16
+ (0, vitest_1.describe)("isDateTime", () => {
17
+ (0, vitest_1.test)("should handle valid date times", () => {
18
+ (0, vitest_1.expect)((0, dateUtil_1.isDateTime)("2020-01-01 12:00")).toBe(true);
19
+ (0, vitest_1.expect)((0, dateUtil_1.isDateTime)("2020-01-01 0:00")).toBe(true);
20
+ });
21
+ (0, vitest_1.test)("should handle invalid date times", () => {
22
+ (0, vitest_1.expect)((0, dateUtil_1.isDateTime)("2020-01-01 0:99")).toBe(false);
23
+ (0, vitest_1.expect)((0, dateUtil_1.isDateTime)("2020-01-01 25:12")).toBe(false);
24
+ });
25
+ });
26
+ (0, vitest_1.describe)("formatDate", () => {
27
+ (0, vitest_1.test)("should format to given input", () => {
28
+ (0, vitest_1.expect)((0, dateUtil_1.formatDate)("2022-02-18", "DDMMYYYY")).toBe("18-02-2022");
29
+ (0, vitest_1.expect)((0, dateUtil_1.formatDate)("2022-02-18", "MMDDYYYY")).toBe("02-18-2022");
30
+ (0, vitest_1.expect)((0, dateUtil_1.formatDate)("2022-02-18", "YYYYMMDD")).toBe("2022-02-18");
31
+ (0, vitest_1.expect)((0, dateUtil_1.formatDate)("2022-02-18", "invalid")).toBe("18-02-2022");
32
+ });
33
+ });
34
+ (0, vitest_1.describe)("formatDateTime", () => {
35
+ (0, vitest_1.test)("should format to given input", () => {
36
+ (0, vitest_1.expect)((0, dateUtil_1.formatDateTime)(1645191420000, "DDMMYYYY")).toBe("18-02-2022 13:37");
37
+ (0, vitest_1.expect)((0, dateUtil_1.formatDateTime)("2022-02-18 13:37", "DDMMYYYY")).toBe("18-02-2022 13:37");
38
+ (0, vitest_1.expect)((0, dateUtil_1.formatDateTime)("2022-02-18 13:37", "MMDDYYYY")).toBe("02-18-2022 13:37");
39
+ (0, vitest_1.expect)((0, dateUtil_1.formatDateTime)("2022-02-18 13:37", "YYYYMMDD")).toBe("2022-02-18 13:37");
40
+ (0, vitest_1.expect)((0, dateUtil_1.formatDateTime)("2022-02-18 13:37", "invalid")).toBe("18-02-2022 13:37");
41
+ });
42
+ });
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const logger_1 = require("./logger");
4
- describe("errorFormat", () => {
5
- test("should format standard errors", () => {
3
+ const vitest_1 = require("vitest");
4
+ const logger_1 = require("../logger");
5
+ (0, vitest_1.describe)("errorFormat", () => {
6
+ (0, vitest_1.test)("should format standard errors", () => {
6
7
  const error = new Error("Failed to persist");
7
8
  const formatted = logger_1.formatters
8
9
  .errorFormat()
9
10
  .transform({ level: "error", message: "Something went wrong", exception: error });
10
- expect(formatted).toStrictEqual({
11
+ (0, vitest_1.expect)(formatted).toStrictEqual({
11
12
  level: "error",
12
13
  message: "Something went wrong",
13
14
  exception: {
@@ -16,7 +17,7 @@ describe("errorFormat", () => {
16
17
  },
17
18
  });
18
19
  });
19
- test("should format error with (deeply nested) cause", () => {
20
+ (0, vitest_1.test)("should format error with (deeply nested) cause", () => {
20
21
  const error = new Error("Failed to persist", {
21
22
  cause: new Error("Caused by me!", { cause: "Well actually..." }),
22
23
  });
@@ -25,7 +26,7 @@ describe("errorFormat", () => {
25
26
  message: "Something went wrong",
26
27
  exception: error,
27
28
  });
28
- expect(formatted).toStrictEqual({
29
+ (0, vitest_1.expect)(formatted).toStrictEqual({
29
30
  level: "error",
30
31
  message: "Something went wrong",
31
32
  exception: {
@@ -40,20 +41,32 @@ describe("errorFormat", () => {
40
41
  });
41
42
  });
42
43
  });
43
- describe("tracingFormat", () => {
44
- test("should transform tracing data to Stackdriver format", () => {
44
+ (0, vitest_1.describe)("tracingFormat", () => {
45
+ (0, vitest_1.test)("should transform tracing data to Stackdriver format", () => {
45
46
  const formatted = logger_1.formatters.stackdriverTracingFormat().transform({
46
47
  level: "error",
47
48
  message: "Something went wrong",
48
49
  trace_id: "TRACE_ID",
49
50
  span_id: "SPAN_ID",
50
51
  });
51
- expect(formatted).toStrictEqual({
52
+ (0, vitest_1.expect)(formatted).toStrictEqual({
52
53
  level: "error",
53
54
  message: "Something went wrong",
54
55
  "logging.googleapis.com/trace": "TRACE_ID",
55
56
  "logging.googleapis.com/spanId": "SPAN_ID",
56
- "logging.googleapis.com/trace_sampled": true,
57
+ "logging.googleapis.com/trace_sampled": false,
57
58
  });
58
59
  });
60
+ (0, vitest_1.test)("should correctly resolve sampling", () => {
61
+ const formatted = logger_1.formatters.stackdriverTracingFormat().transform({
62
+ level: "info",
63
+ message: "Some good news",
64
+ trace_id: "TRACE_ID",
65
+ span_id: "SPAN_ID",
66
+ trace_flags: "01",
67
+ });
68
+ (0, vitest_1.expect)(formatted).toStrictEqual(vitest_1.expect.objectContaining({
69
+ "logging.googleapis.com/trace_sampled": true,
70
+ }));
71
+ });
59
72
  });
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const utils_1 = require("../utils");
5
+ (0, vitest_1.describe)("environmentVariable", () => {
6
+ (0, vitest_1.test)("Should throw when environment variable does not exist", () => {
7
+ (0, vitest_1.expect)(() => (0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING")).toThrow("Missing environment variable 'COMMONS_NODEJS_TEST_NON_EXISTING'");
8
+ });
9
+ (0, vitest_1.test)("Should return fallback value when environment variable does not exist (string)", () => {
10
+ (0, vitest_1.expect)((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING", "my-fallback")).toBe("my-fallback");
11
+ });
12
+ (0, vitest_1.test)("Should return fallback value when environment variable does not exist (number)", () => {
13
+ (0, vitest_1.expect)((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING", 1)).toBe(1);
14
+ });
15
+ (0, vitest_1.test)("Should return fallback value when environment variable does not exist (boolean)", () => {
16
+ (0, vitest_1.expect)((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING", false)).toBe(false);
17
+ });
18
+ (0, vitest_1.test)("Should return actual value when environment variable exists (string)", () => {
19
+ process.env["COMMONS_NODEJS_TEST_EXISTING"] = "actual-value";
20
+ (0, vitest_1.expect)((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", "my-fallback")).toBe("actual-value");
21
+ });
22
+ (0, vitest_1.test)("Should return actual value when environment variable exists (number)", () => {
23
+ process.env["COMMONS_NODEJS_TEST_EXISTING"] = "2";
24
+ (0, vitest_1.expect)((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", 1)).toBe(2);
25
+ });
26
+ (0, vitest_1.test)("Should return actual value when environment variable exists (boolean)", () => {
27
+ process.env["COMMONS_NODEJS_TEST_EXISTING"] = "true";
28
+ (0, vitest_1.expect)((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", false)).toBe(true);
29
+ });
30
+ (0, vitest_1.test)("should throw when environment variable contains an invalid type (number)", () => {
31
+ process.env["COMMONS_NODEJS_TEST_EXISTING"] = "not-a-number";
32
+ (0, vitest_1.expect)(() => (0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", 1)).toThrow("Wrong type for environment variable 'COMMONS_NODEJS_TEST_EXISTING'");
33
+ });
34
+ (0, vitest_1.test)("should throw when environment variable contains an invalid type (boolean)", () => {
35
+ process.env["COMMONS_NODEJS_TEST_EXISTING"] = "not-a-boolean";
36
+ (0, vitest_1.expect)(() => (0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", false)).toThrow("Wrong type for environment variable 'COMMONS_NODEJS_TEST_EXISTING'");
37
+ });
38
+ });
39
+ (0, vitest_1.describe)("isValidEmail", () => {
40
+ (0, vitest_1.describe)("Valid email addresses", () => {
41
+ (0, vitest_1.test)("Standard email address", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a@moreapp.dev")).toBeTruthy());
42
+ (0, vitest_1.test)("Non-alphanumeric characters", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a+-._&%'@moreapp.dev")).toBeTruthy());
43
+ (0, vitest_1.test)("Shortest email address", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a@b.co")).toBeTruthy());
44
+ (0, vitest_1.test)("Subdomain", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a@sub.domain.dev")).toBeTruthy());
45
+ (0, vitest_1.test)("Subdomain with a dash", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a@sub.dom-ain.dev")).toBeTruthy());
46
+ (0, vitest_1.test)("Long TLD", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a@b.business")).toBeTruthy());
47
+ (0, vitest_1.test)("Diacritics", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("ẞçäöü@möręæppß.dev")).toBeTruthy());
48
+ (0, vitest_1.test)("Casing", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("TØmÂs@mŒREÄPp.cOm")).toBeTruthy());
49
+ });
50
+ (0, vitest_1.describe)("Invalid email addresses", () => {
51
+ (0, vitest_1.test)("Missing '@' character and domain", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("john")).toBeFalsy());
52
+ (0, vitest_1.test)("Multiple '@' characters", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a@b@moreapp.dev")).toBeFalsy());
53
+ (0, vitest_1.test)("Ends with a '@' character", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a@moreapp.dev@")).toBeFalsy());
54
+ (0, vitest_1.test)("Empty", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("")).toBeFalsy());
55
+ (0, vitest_1.test)("Whitespace before/after", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)(" a@moreapp.dev ")).toBeFalsy());
56
+ (0, vitest_1.test)("Newlines, tabs, ..., before/after", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("\n \ta@moreapp.dev\n ")).toBeFalsy());
57
+ (0, vitest_1.test)("Missing '@' character", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("ab.com")).toBeFalsy());
58
+ (0, vitest_1.test)("Double dot in domain", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a@moreapp..com")).toBeFalsy());
59
+ (0, vitest_1.test)("Too short TLD", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a@b.c")).toBeFalsy());
60
+ (0, vitest_1.test)("Missing local part", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("@moreapp.dev")).toBeFalsy());
61
+ (0, vitest_1.test)("Too long", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-@moreapp.dev")).toBeFalsy());
62
+ (0, vitest_1.test)("Starts with '.'", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)(".a@moreapp.dev")).toBeFalsy());
63
+ (0, vitest_1.test)("Ends with a '.'", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a.@moreapp.dev")).toBeFalsy());
64
+ (0, vitest_1.test)("Local part contains whitespace", () => (0, vitest_1.expect)((0, utils_1.isValidEmail)("a b@moreapp.dev")).toBeFalsy());
65
+ });
66
+ });
67
+ (0, vitest_1.describe)("parseDynamicRecipients", () => {
68
+ (0, vitest_1.test)("should parse all type of data (root, object, array)", () => {
69
+ const fields = [
70
+ { uid: "UID-root", properties: { data_name: "customer" } },
71
+ { uid: "UID-object", properties: { data_name: "vendor" } },
72
+ { uid: "UID-array", properties: { data_name: "contacts" } },
73
+ ];
74
+ (0, vitest_1.expect)((0, utils_1.parseDynamicRecipients)(["UID-root", "UID-object.email", "UID-array"], fields, {
75
+ customer: "customer@example.com",
76
+ vendor: { id: 1, email: "vendor@example.com" },
77
+ contacts: ["contact1@example.com", "contact2@example.com"],
78
+ })).toEqual([
79
+ "customer@example.com",
80
+ "vendor@example.com",
81
+ "contact1@example.com",
82
+ "contact2@example.com",
83
+ ]);
84
+ });
85
+ (0, vitest_1.test)("should skip invalid emails", () => {
86
+ const fields = [
87
+ { uid: "UID-root", properties: { data_name: "customer" } },
88
+ { uid: "UID-object", properties: { data_name: "vendor" } },
89
+ { uid: "UID-array", properties: { data_name: "contacts" } },
90
+ ];
91
+ (0, vitest_1.expect)((0, utils_1.parseDynamicRecipients)(["UID-root", "UID-object.email", "UID-array"], fields, {
92
+ customer: "invalid1",
93
+ vendor: { id: 1, email: "invalid2" },
94
+ contacts: ["invalid3", "invalid4"],
95
+ })).toEqual([]);
96
+ });
97
+ });
98
+ (0, vitest_1.describe)("getDataForDataName", () => {
99
+ (0, vitest_1.test)("should return (nested) values in objects based on dataName", () => {
100
+ const data = {
101
+ name: "John",
102
+ age: 10,
103
+ address: {
104
+ street: "Main Street",
105
+ city: "New York",
106
+ location: {
107
+ longitude: 10,
108
+ latitude: 10,
109
+ },
110
+ },
111
+ "flat.test": "hi",
112
+ };
113
+ (0, vitest_1.expect)((0, utils_1.getDataForDataName)(data, "name")).toBe("John");
114
+ (0, vitest_1.expect)((0, utils_1.getDataForDataName)(data, "age")).toBe(10);
115
+ (0, vitest_1.expect)((0, utils_1.getDataForDataName)(data, "address.city")).toBe("New York");
116
+ (0, vitest_1.expect)((0, utils_1.getDataForDataName)(data, "address.location.longitude")).toBe(10);
117
+ (0, vitest_1.expect)((0, utils_1.getDataForDataName)(data, "flat.test")).toBe("hi");
118
+ (0, vitest_1.expect)((0, utils_1.getDataForDataName)(data, "unknown")).toBe(undefined);
119
+ (0, vitest_1.expect)((0, utils_1.getDataForDataName)(data, "unknown.nested")).toBe(undefined);
120
+ (0, vitest_1.expect)((0, utils_1.getDataForDataName)(data, "address.unknown")).toBe(undefined);
121
+ (0, vitest_1.expect)((0, utils_1.getDataForDataName)(data, "flat..")).toBe(undefined);
122
+ (0, vitest_1.expect)((0, utils_1.getDataForDataName)(data, ".")).toBe(undefined);
123
+ });
124
+ });
package/dist/dateUtil.js CHANGED
@@ -1,13 +1,43 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
5
35
  Object.defineProperty(exports, "__esModule", { value: true });
6
36
  exports.isDate = isDate;
7
37
  exports.isDateTime = isDateTime;
8
38
  exports.formatDate = formatDate;
9
39
  exports.formatDateTime = formatDateTime;
10
- const date_and_time_1 = __importDefault(require("date-and-time"));
40
+ const dateAndTime = __importStar(require("date-and-time"));
11
41
  const DATE_REGEX = /^\d{4}-\d{1,2}-\d{1,2}$/;
12
42
  const DATE_TIME_REGEX = /^\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{2}$/;
13
43
  function isDate(string) {
@@ -17,14 +47,14 @@ function isDateTime(string) {
17
47
  return DATE_TIME_REGEX.test(string) && !isNaN(Date.parse(string));
18
48
  }
19
49
  function formatDate(dateString, format) {
20
- const date = date_and_time_1.default.parse(dateString, "YYYY-MM-DD");
21
- return date_and_time_1.default.format(date, formatDateFormat(format));
50
+ const date = dateAndTime.parse(dateString, "YYYY-MM-DD");
51
+ return dateAndTime.format(date, formatDateFormat(format));
22
52
  }
23
53
  function formatDateTime(dateTime, format) {
24
54
  const date = typeof dateTime === "string"
25
- ? date_and_time_1.default.parse(dateTime, "YYYY-MM-DD HH:mm", { timeZone: "UTC" })
55
+ ? dateAndTime.parse(dateTime, "YYYY-MM-DD HH:mm", { timeZone: "UTC" })
26
56
  : new Date(dateTime);
27
- return date_and_time_1.default.format(date, formatDateFormat(format) + " HH:mm", { timeZone: "UTC" });
57
+ return dateAndTime.format(date, formatDateFormat(format) + " HH:mm", { timeZone: "UTC" });
28
58
  }
29
59
  function formatDateFormat(format) {
30
60
  switch (format) {
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import MoreAppClient from "./MoreAppClient";
2
2
  export * from "./types";
3
3
  export * from "./schema";
4
- export * from "./tracer";
5
4
  export * from "./logger";
6
5
  export * from "./dateUtil";
7
6
  export * from "./utils";
8
7
  export * from "./observability";
8
+ export * from "./observability/tracer";
9
9
  export { BinaryFile } from "./MoreAppClient";
10
10
  export { MoreAppClient };
package/dist/index.js CHANGED
@@ -22,8 +22,8 @@ const MoreAppClient_1 = __importDefault(require("./MoreAppClient"));
22
22
  exports.MoreAppClient = MoreAppClient_1.default;
23
23
  __exportStar(require("./types"), exports);
24
24
  __exportStar(require("./schema"), exports);
25
- __exportStar(require("./tracer"), exports);
26
25
  __exportStar(require("./logger"), exports);
27
26
  __exportStar(require("./dateUtil"), exports);
28
27
  __exportStar(require("./utils"), exports);
29
28
  __exportStar(require("./observability"), exports);
29
+ __exportStar(require("./observability/tracer"), exports);
package/dist/logger.js CHANGED
@@ -6,9 +6,10 @@ const stackdriverTracingFormat = (0, winston_1.format)((info) => {
6
6
  if (info["trace_id"] && info["span_id"]) {
7
7
  info["logging.googleapis.com/trace"] = info["trace_id"];
8
8
  info["logging.googleapis.com/spanId"] = info["span_id"];
9
- info["logging.googleapis.com/trace_sampled"] = true;
9
+ info["logging.googleapis.com/trace_sampled"] = info["trace_flags"] === "01";
10
10
  delete info["trace_id"];
11
11
  delete info["span_id"];
12
+ delete info["trace_flags"];
12
13
  }
13
14
  return info;
14
15
  });
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  const node_timers_1 = require("node:timers");
4
7
  const logger_1 = require("../logger");
8
+ const tracer_1 = __importDefault(require("./tracer"));
5
9
  const TERMINATION_SIGNALS = ["SIGTERM", "SIGHUP", "SIGINT"];
6
10
  const SHUTDOWN_TIMEOUT = 30_000;
7
11
  class TerminationHandler {
@@ -34,7 +38,7 @@ class TerminationHandler {
34
38
  (0, node_timers_1.clearInterval)(interval);
35
39
  // Destroy any remaining sockets
36
40
  this.sockets.forEach((socket) => socket.destroy());
37
- void this.hs.stop().finally(() => {
41
+ void Promise.all([this.hs.stop(), tracer_1.default.shutdown()]).finally(() => {
38
42
  logger_1.logger.info("Server stopped");
39
43
  process.exit(0);
40
44
  });
@@ -3,8 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const vitest_1 = require("vitest");
6
7
  const node_events_1 = require("node:events");
7
- const ExpressRequestTracker_1 = __importDefault(require("./ExpressRequestTracker"));
8
+ const ExpressRequestTracker_1 = __importDefault(require("../ExpressRequestTracker"));
8
9
  // Create a minimal Response mock that can emit the 'finish' event
9
10
  const createMockResponse = () => {
10
11
  const emitter = new node_events_1.EventEmitter();
@@ -12,42 +13,42 @@ const createMockResponse = () => {
12
13
  emitFinish: () => emitter.emit("finish"),
13
14
  });
14
15
  };
15
- describe("ExpressRequestTracker", () => {
16
- test("increments active requests on middleware entry and calls next", () => {
16
+ (0, vitest_1.describe)("ExpressRequestTracker", () => {
17
+ (0, vitest_1.test)("increments active requests on middleware entry and calls next", () => {
17
18
  const tracker = new ExpressRequestTracker_1.default();
18
19
  const middleware = tracker.track();
19
20
  const res = createMockResponse();
20
- const next = jest.fn();
21
- expect(tracker.nrOfActiveRequests()).toBe(0);
21
+ const next = vitest_1.vi.fn();
22
+ (0, vitest_1.expect)(tracker.nrOfActiveRequests()).toBe(0);
22
23
  middleware({}, res, next);
23
- expect(tracker.nrOfActiveRequests()).toBe(1);
24
- expect(next).toHaveBeenCalledTimes(1);
24
+ (0, vitest_1.expect)(tracker.nrOfActiveRequests()).toBe(1);
25
+ (0, vitest_1.expect)(next).toHaveBeenCalledTimes(1);
25
26
  // Finish should decrement
26
27
  res.emitFinish();
27
- expect(tracker.nrOfActiveRequests()).toBe(0);
28
+ (0, vitest_1.expect)(tracker.nrOfActiveRequests()).toBe(0);
28
29
  });
29
- test("decrements active requests when response finishes", () => {
30
+ (0, vitest_1.test)("decrements active requests when response finishes", () => {
30
31
  const tracker = new ExpressRequestTracker_1.default();
31
32
  const middleware = tracker.track();
32
33
  const res = createMockResponse();
33
34
  middleware({}, res, (() => { }));
34
- expect(tracker.nrOfActiveRequests()).toBe(1);
35
+ (0, vitest_1.expect)(tracker.nrOfActiveRequests()).toBe(1);
35
36
  res.emitFinish();
36
- expect(tracker.nrOfActiveRequests()).toBe(0);
37
+ (0, vitest_1.expect)(tracker.nrOfActiveRequests()).toBe(0);
37
38
  });
38
- test("tracks multiple concurrent requests and decrements in order", () => {
39
+ (0, vitest_1.test)("tracks multiple concurrent requests and decrements in order", () => {
39
40
  const tracker = new ExpressRequestTracker_1.default();
40
41
  const middleware = tracker.track();
41
42
  const res1 = createMockResponse();
42
43
  const res2 = createMockResponse();
43
44
  middleware({}, res1, (() => { }));
44
45
  middleware({}, res2, (() => { }));
45
- expect(tracker.nrOfActiveRequests()).toBe(2);
46
+ (0, vitest_1.expect)(tracker.nrOfActiveRequests()).toBe(2);
46
47
  // Finish the first request
47
48
  res1.emitFinish();
48
- expect(tracker.nrOfActiveRequests()).toBe(1);
49
+ (0, vitest_1.expect)(tracker.nrOfActiveRequests()).toBe(1);
49
50
  // Finish the second request
50
51
  res2.emitFinish();
51
- expect(tracker.nrOfActiveRequests()).toBe(0);
52
+ (0, vitest_1.expect)(tracker.nrOfActiveRequests()).toBe(0);
52
53
  });
53
54
  });
@@ -3,27 +3,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const testUtils_1 = require("../testUtils");
7
- (0, testUtils_1.silenceLogger)();
6
+ const vitest_1 = require("vitest");
8
7
  const node_http_1 = __importDefault(require("node:http"));
9
8
  const node_events_1 = require("node:events");
10
- const ObservabilityServer_1 = __importDefault(require("./ObservabilityServer"));
11
- describe("ObservabilityServer", () => {
9
+ const ObservabilityServer_1 = __importDefault(require("../ObservabilityServer"));
10
+ vitest_1.vi.mock("../../logger", () => ({
11
+ logger: {
12
+ info: vitest_1.vi.fn(),
13
+ warn: vitest_1.vi.fn(),
14
+ error: vitest_1.vi.fn(),
15
+ debug: vitest_1.vi.fn(),
16
+ },
17
+ }));
18
+ (0, vitest_1.describe)("ObservabilityServer", () => {
12
19
  const createFakeServer = () => {
13
20
  const emitter = new node_events_1.EventEmitter();
14
21
  const server = {
15
- listen: jest.fn((...args) => {
22
+ listen: vitest_1.vi.fn((...args) => {
16
23
  const cb = args.find((a) => typeof a === "function");
17
24
  if (cb)
18
25
  cb();
19
26
  return server;
20
27
  }),
21
- close: jest.fn((cb) => {
28
+ close: vitest_1.vi.fn((cb) => {
22
29
  if (cb)
23
30
  cb();
24
31
  return server;
25
32
  }),
26
- on: jest.fn((event, listener) => {
33
+ on: vitest_1.vi.fn((event, listener) => {
27
34
  emitter.on(event, listener);
28
35
  return server;
29
36
  }),
@@ -32,7 +39,7 @@ describe("ObservabilityServer", () => {
32
39
  return server;
33
40
  };
34
41
  const createRes = () => {
35
- const res = {
42
+ return {
36
43
  statusCode: 200,
37
44
  headers: {},
38
45
  writeHead: function (code, headers) {
@@ -43,13 +50,12 @@ describe("ObservabilityServer", () => {
43
50
  this.body = body;
44
51
  },
45
52
  };
46
- return res;
47
53
  };
48
- test("handles /live and /ready endpoints and start/stop lifecycle", async () => {
54
+ (0, vitest_1.test)("handles /live and /ready endpoints and start/stop lifecycle", async () => {
49
55
  const fakeServer = createFakeServer();
50
56
  // Capture handler passed to createServer
51
57
  let capturedHandler;
52
- const createServerSpy = jest
58
+ const createServerSpy = vitest_1.vi
53
59
  .spyOn(node_http_1.default, "createServer")
54
60
  // @ts-expect-error simplify handler typing for test
55
61
  .mockImplementation((handler) => {
@@ -57,40 +63,41 @@ describe("ObservabilityServer", () => {
57
63
  return fakeServer;
58
64
  });
59
65
  const requestTracker = {
60
- nrOfActiveRequests: jest.fn(() => 0),
66
+ nrOfActiveRequests: vitest_1.vi.fn(() => 0),
61
67
  };
62
68
  const hs = await ObservabilityServer_1.default.create(requestTracker, { port: 0 });
63
- expect(fakeServer.listen).toHaveBeenCalled();
69
+ (0, vitest_1.expect)(fakeServer.listen).toHaveBeenCalled();
64
70
  // /live
65
71
  const resLive = createRes();
66
72
  capturedHandler({ url: "/live" }, resLive);
67
- expect(resLive.statusCode).toBe(200);
68
- expect(resLive.headers).toEqual({ "Content-Type": "application/json" });
69
- expect(JSON.parse(resLive.body)).toEqual({ status: "ok" });
73
+ (0, vitest_1.expect)(resLive.statusCode).toBe(200);
74
+ (0, vitest_1.expect)(resLive.headers).toEqual({ "Content-Type": "application/json" });
75
+ (0, vitest_1.expect)(JSON.parse(resLive.body)).toEqual({ status: "ok" });
70
76
  // /ready when not ready
71
77
  const resReady1 = createRes();
72
78
  capturedHandler({ url: "/ready" }, resReady1);
73
- expect(resReady1.statusCode).toBe(503);
74
- expect(JSON.parse(resReady1.body)).toEqual({
79
+ (0, vitest_1.expect)(resReady1.statusCode).toBe(503);
80
+ (0, vitest_1.expect)(JSON.parse(resReady1.body)).toEqual({
75
81
  status: "not ready",
76
82
  activeRequests: 0,
77
83
  });
78
84
  // Now mark ready and simulate active requests
79
- requestTracker.nrOfActiveRequests.mockReturnValue(3);
85
+ // eslint-disable-next-line @typescript-eslint/unbound-method
86
+ vitest_1.vi.mocked(requestTracker.nrOfActiveRequests).mockReturnValue(3);
80
87
  hs.setReady(true);
81
88
  const resReady2 = createRes();
82
89
  capturedHandler({ url: "/ready" }, resReady2);
83
- expect(resReady2.statusCode).toBe(200);
84
- expect(JSON.parse(resReady2.body)).toEqual({
90
+ (0, vitest_1.expect)(resReady2.statusCode).toBe(200);
91
+ (0, vitest_1.expect)(JSON.parse(resReady2.body)).toEqual({
85
92
  status: "ok",
86
93
  activeRequests: 3,
87
94
  });
88
95
  // unknown route
89
96
  const res404 = createRes();
90
97
  capturedHandler({ url: "/unknown" }, res404);
91
- expect(res404.statusCode).toBe(404);
98
+ (0, vitest_1.expect)(res404.statusCode).toBe(404);
92
99
  await hs.stop();
93
- expect(fakeServer.close).toHaveBeenCalled();
100
+ (0, vitest_1.expect)(fakeServer.close).toHaveBeenCalled();
94
101
  createServerSpy.mockRestore();
95
102
  });
96
103
  });