@moreapp/common-nodejs 0.12.1 → 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 (33) hide show
  1. package/dist/__tests__/MoreAppClient.test.js +85 -0
  2. package/dist/__tests__/dateUtil.test.js +42 -0
  3. package/dist/__tests__/logger.test.js +72 -0
  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.d.ts +1 -0
  9. package/dist/logger.js +13 -1
  10. package/dist/observability/TerminationHandler.js +5 -1
  11. package/dist/observability/{ExpressRequestTracker.test.js → __tests__/ExpressRequestTracker.test.js} +16 -15
  12. package/dist/observability/{ObservabilityServer.test.js → __tests__/ObservabilityServer.test.js} +30 -23
  13. package/dist/observability/__tests__/TerminationHandler.test.js +171 -0
  14. package/dist/observability/index.d.ts +2 -1
  15. package/dist/observability/index.js +3 -1
  16. package/dist/observability/tracer.d.ts +12 -0
  17. package/dist/{tracer.js → observability/tracer.js} +15 -7
  18. package/package.json +12 -12
  19. package/dist/MoreAppClient.test.js +0 -79
  20. package/dist/dateUtil.test.js +0 -41
  21. package/dist/logger.test.js +0 -42
  22. package/dist/observability/TerminationHandler.test.js +0 -160
  23. package/dist/testUtils.d.ts +0 -1
  24. package/dist/testUtils.js +0 -13
  25. package/dist/tracer.d.ts +0 -8
  26. package/dist/utils.test.js +0 -123
  27. /package/dist/{MoreAppClient.test.d.ts → __tests__/MoreAppClient.test.d.ts} +0 -0
  28. /package/dist/{dateUtil.test.d.ts → __tests__/dateUtil.test.d.ts} +0 -0
  29. /package/dist/{logger.test.d.ts → __tests__/logger.test.d.ts} +0 -0
  30. /package/dist/{utils.test.d.ts → __tests__/utils.test.d.ts} +0 -0
  31. /package/dist/observability/{ExpressRequestTracker.test.d.ts → __tests__/ExpressRequestTracker.test.d.ts} +0 -0
  32. /package/dist/observability/{ObservabilityServer.test.d.ts → __tests__/ObservabilityServer.test.d.ts} +0 -0
  33. /package/dist/observability/{TerminationHandler.test.d.ts → __tests__/TerminationHandler.test.d.ts} +0 -0
@@ -1,160 +0,0 @@
1
- "use strict";
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
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- const testUtils_1 = require("../testUtils");
40
- (0, testUtils_1.silenceLogger)();
41
- const node_events_1 = require("node:events");
42
- const timers = __importStar(require("node:timers"));
43
- const TerminationHandler_1 = __importDefault(require("./TerminationHandler"));
44
- // Mock node:timers so we can control callbacks (TerminationHandler imports from node:timers)
45
- jest.mock("node:timers", () => ({
46
- setInterval: jest.fn(),
47
- clearInterval: jest.fn(),
48
- setTimeout: jest.fn(),
49
- }));
50
- describe("TerminationHandler", () => {
51
- let intervalCb;
52
- let timeoutCb;
53
- beforeEach(() => {
54
- intervalCb = undefined;
55
- timeoutCb = undefined;
56
- timers.setInterval.mockImplementation((cb) => {
57
- intervalCb = cb;
58
- return 1;
59
- });
60
- timers.clearInterval.mockImplementation(() => { });
61
- jest.spyOn(globalThis, "setTimeout").mockImplementation((cb) => {
62
- timeoutCb = cb;
63
- return 2;
64
- });
65
- });
66
- afterEach(() => {
67
- jest.resetAllMocks();
68
- });
69
- const createFakeServer = () => {
70
- const emitter = new node_events_1.EventEmitter();
71
- const server = {
72
- close: jest.fn((cb) => {
73
- if (cb)
74
- cb();
75
- return server;
76
- }),
77
- on: jest.fn((event, listener) => {
78
- emitter.on(event, listener);
79
- return server;
80
- }),
81
- emit: emitter.emit.bind(emitter),
82
- };
83
- return server;
84
- };
85
- const createSocket = () => {
86
- const emitter = new node_events_1.EventEmitter();
87
- const socket = emitter;
88
- socket.destroy = jest.fn();
89
- return socket;
90
- };
91
- test("shutdown waits for active requests to reach 0 then stops and exits 0", () => {
92
- const server = createFakeServer();
93
- const hs = {
94
- setReady: jest.fn(),
95
- stop: jest.fn(() => ({
96
- finally: (cb) => {
97
- cb();
98
- return Promise.resolve();
99
- },
100
- })),
101
- };
102
- const requestTracker = { nrOfActiveRequests: jest.fn(() => 0) };
103
- const exitSpy = jest.spyOn(process, "exit").mockImplementation(() => undefined);
104
- const th = new TerminationHandler_1.default(server, hs, requestTracker);
105
- th.shutdown();
106
- expect(hs.setReady).toHaveBeenCalledWith(false);
107
- expect(server.close).toHaveBeenCalled();
108
- // First interval tick should see 0 active and exit
109
- intervalCb?.();
110
- // ensure hs.stop called and exit called with 0
111
- expect(hs.stop).toHaveBeenCalled();
112
- expect(exitSpy).toHaveBeenCalledWith(0);
113
- });
114
- test("shutdown polls until requests finish then exits 0", () => {
115
- const server = createFakeServer();
116
- const hs = {
117
- setReady: jest.fn(),
118
- stop: jest.fn(() => ({
119
- finally: (cb) => {
120
- cb();
121
- return Promise.resolve();
122
- },
123
- })),
124
- };
125
- const requestTracker = { nrOfActiveRequests: jest.fn() };
126
- // Return >0 first, then 0
127
- requestTracker.nrOfActiveRequests.mockReturnValueOnce(2).mockReturnValueOnce(0);
128
- const exitSpy = jest.spyOn(process, "exit").mockImplementation(() => undefined);
129
- const th = new TerminationHandler_1.default(server, hs, requestTracker);
130
- th.shutdown();
131
- // First tick: still waiting
132
- intervalCb?.();
133
- expect(exitSpy).not.toHaveBeenCalled();
134
- // Second tick: now 0
135
- intervalCb?.();
136
- expect(hs.stop).toHaveBeenCalled();
137
- expect(exitSpy).toHaveBeenCalledWith(0);
138
- });
139
- test("forceful stop after 30s destroys sockets and exits 1", () => {
140
- const server = createFakeServer();
141
- const hs = {
142
- setReady: jest.fn(),
143
- stop: jest.fn(() => Promise.resolve()),
144
- };
145
- const requestTracker = { nrOfActiveRequests: jest.fn(() => 5) }; // never reaches 0
146
- const exitSpy = jest.spyOn(process, "exit").mockImplementation(() => undefined);
147
- const th = new TerminationHandler_1.default(server, hs, requestTracker);
148
- // Register a socket via the server's connection event
149
- const socket = createSocket();
150
- server.emit("connection", socket);
151
- th.shutdown();
152
- // Before timeout callback, no exit
153
- expect(exitSpy).not.toHaveBeenCalled();
154
- expect(socket.destroy).not.toHaveBeenCalled();
155
- // Trigger timeout
156
- timeoutCb?.();
157
- expect(socket.destroy).toHaveBeenCalled();
158
- expect(exitSpy).toHaveBeenCalledWith(1);
159
- });
160
- });
@@ -1 +0,0 @@
1
- export declare const silenceLogger: () => typeof jest;
package/dist/testUtils.js DELETED
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.silenceLogger = void 0;
4
- // Utility to silence logger output in tests must be at the top of the test file
5
- const silenceLogger = () => jest.mock("./logger", () => ({
6
- logger: {
7
- info: jest.fn(),
8
- warn: jest.fn(),
9
- error: jest.fn(),
10
- debug: jest.fn(),
11
- },
12
- }));
13
- exports.silenceLogger = silenceLogger;
package/dist/tracer.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import * as types from "@opentelemetry/instrumentation/build/src/types";
2
- export declare const tracer: (serviceName: string, { http, extraInstrumentations, debug, }: {
3
- http?: {
4
- portsToInstrument: number[];
5
- };
6
- extraInstrumentations?: types.Instrumentation[];
7
- debug?: boolean;
8
- }) => void;
@@ -1,123 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const utils_1 = require("./utils");
4
- describe("environmentVariable", () => {
5
- test("Should throw when environment variable does not exist", () => {
6
- expect(() => (0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING")).toThrow("Missing environment variable 'COMMONS_NODEJS_TEST_NON_EXISTING'");
7
- });
8
- test("Should return fallback value when environment variable does not exist (string)", () => {
9
- expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING", "my-fallback")).toBe("my-fallback");
10
- });
11
- test("Should return fallback value when environment variable does not exist (number)", () => {
12
- expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING", 1)).toBe(1);
13
- });
14
- test("Should return fallback value when environment variable does not exist (boolean)", () => {
15
- expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING", false)).toBe(false);
16
- });
17
- test("Should return actual value when environment variable exists (string)", () => {
18
- process.env["COMMONS_NODEJS_TEST_EXISTING"] = "actual-value";
19
- expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", "my-fallback")).toBe("actual-value");
20
- });
21
- test("Should return actual value when environment variable exists (number)", () => {
22
- process.env["COMMONS_NODEJS_TEST_EXISTING"] = "2";
23
- expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", 1)).toBe(2);
24
- });
25
- test("Should return actual value when environment variable exists (boolean)", () => {
26
- process.env["COMMONS_NODEJS_TEST_EXISTING"] = "true";
27
- expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", false)).toBe(true);
28
- });
29
- test("should throw when environment variable contains an invalid type (number)", () => {
30
- process.env["COMMONS_NODEJS_TEST_EXISTING"] = "not-a-number";
31
- expect(() => (0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", 1)).toThrow("Wrong type for environment variable 'COMMONS_NODEJS_TEST_EXISTING'");
32
- });
33
- test("should throw when environment variable contains an invalid type (boolean)", () => {
34
- process.env["COMMONS_NODEJS_TEST_EXISTING"] = "not-a-boolean";
35
- expect(() => (0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", false)).toThrow("Wrong type for environment variable 'COMMONS_NODEJS_TEST_EXISTING'");
36
- });
37
- });
38
- describe("isValidEmail", () => {
39
- describe("Valid email addresses", () => {
40
- test("Standard email address", () => expect((0, utils_1.isValidEmail)("a@moreapp.dev")).toBeTruthy());
41
- test("Non-alphanumeric characters", () => expect((0, utils_1.isValidEmail)("a+-._&%'@moreapp.dev")).toBeTruthy());
42
- test("Shortest email address", () => expect((0, utils_1.isValidEmail)("a@b.co")).toBeTruthy());
43
- test("Subdomain", () => expect((0, utils_1.isValidEmail)("a@sub.domain.dev")).toBeTruthy());
44
- test("Subdomain with a dash", () => expect((0, utils_1.isValidEmail)("a@sub.dom-ain.dev")).toBeTruthy());
45
- test("Long TLD", () => expect((0, utils_1.isValidEmail)("a@b.business")).toBeTruthy());
46
- test("Diacritics", () => expect((0, utils_1.isValidEmail)("ẞçäöü@möręæppß.dev")).toBeTruthy());
47
- test("Casing", () => expect((0, utils_1.isValidEmail)("TØmÂs@mŒREÄPp.cOm")).toBeTruthy());
48
- });
49
- describe("Invalid email addresses", () => {
50
- test("Missing '@' character and domain", () => expect((0, utils_1.isValidEmail)("john")).toBeFalsy());
51
- test("Multiple '@' characters", () => expect((0, utils_1.isValidEmail)("a@b@moreapp.dev")).toBeFalsy());
52
- test("Ends with a '@' character", () => expect((0, utils_1.isValidEmail)("a@moreapp.dev@")).toBeFalsy());
53
- test("Empty", () => expect((0, utils_1.isValidEmail)("")).toBeFalsy());
54
- test("Whitespace before/after", () => expect((0, utils_1.isValidEmail)(" a@moreapp.dev ")).toBeFalsy());
55
- test("Newlines, tabs, ..., before/after", () => expect((0, utils_1.isValidEmail)("\n \ta@moreapp.dev\n ")).toBeFalsy());
56
- test("Missing '@' character", () => expect((0, utils_1.isValidEmail)("ab.com")).toBeFalsy());
57
- test("Double dot in domain", () => expect((0, utils_1.isValidEmail)("a@moreapp..com")).toBeFalsy());
58
- test("Too short TLD", () => expect((0, utils_1.isValidEmail)("a@b.c")).toBeFalsy());
59
- test("Missing local part", () => expect((0, utils_1.isValidEmail)("@moreapp.dev")).toBeFalsy());
60
- test("Too long", () => expect((0, utils_1.isValidEmail)("abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-@moreapp.dev")).toBeFalsy());
61
- test("Starts with '.'", () => expect((0, utils_1.isValidEmail)(".a@moreapp.dev")).toBeFalsy());
62
- test("Ends with a '.'", () => expect((0, utils_1.isValidEmail)("a.@moreapp.dev")).toBeFalsy());
63
- test("Local part contains whitespace", () => expect((0, utils_1.isValidEmail)("a b@moreapp.dev")).toBeFalsy());
64
- });
65
- });
66
- describe("parseDynamicRecipients", () => {
67
- test("should parse all type of data (root, object, array)", () => {
68
- const fields = [
69
- { uid: "UID-root", properties: { data_name: "customer" } },
70
- { uid: "UID-object", properties: { data_name: "vendor" } },
71
- { uid: "UID-array", properties: { data_name: "contacts" } },
72
- ];
73
- expect((0, utils_1.parseDynamicRecipients)(["UID-root", "UID-object.email", "UID-array"], fields, {
74
- customer: "customer@example.com",
75
- vendor: { id: 1, email: "vendor@example.com" },
76
- contacts: ["contact1@example.com", "contact2@example.com"],
77
- })).toEqual([
78
- "customer@example.com",
79
- "vendor@example.com",
80
- "contact1@example.com",
81
- "contact2@example.com",
82
- ]);
83
- });
84
- test("should skip invalid emails", () => {
85
- const fields = [
86
- { uid: "UID-root", properties: { data_name: "customer" } },
87
- { uid: "UID-object", properties: { data_name: "vendor" } },
88
- { uid: "UID-array", properties: { data_name: "contacts" } },
89
- ];
90
- expect((0, utils_1.parseDynamicRecipients)(["UID-root", "UID-object.email", "UID-array"], fields, {
91
- customer: "invalid1",
92
- vendor: { id: 1, email: "invalid2" },
93
- contacts: ["invalid3", "invalid4"],
94
- })).toEqual([]);
95
- });
96
- });
97
- describe("getDataForDataName", () => {
98
- test("should return (nested) values in objects based on dataName", () => {
99
- const data = {
100
- name: "John",
101
- age: 10,
102
- address: {
103
- street: "Main Street",
104
- city: "New York",
105
- location: {
106
- longitude: 10,
107
- latitude: 10,
108
- },
109
- },
110
- "flat.test": "hi",
111
- };
112
- expect((0, utils_1.getDataForDataName)(data, "name")).toBe("John");
113
- expect((0, utils_1.getDataForDataName)(data, "age")).toBe(10);
114
- expect((0, utils_1.getDataForDataName)(data, "address.city")).toBe("New York");
115
- expect((0, utils_1.getDataForDataName)(data, "address.location.longitude")).toBe(10);
116
- expect((0, utils_1.getDataForDataName)(data, "flat.test")).toBe("hi");
117
- expect((0, utils_1.getDataForDataName)(data, "unknown")).toBe(undefined);
118
- expect((0, utils_1.getDataForDataName)(data, "unknown.nested")).toBe(undefined);
119
- expect((0, utils_1.getDataForDataName)(data, "address.unknown")).toBe(undefined);
120
- expect((0, utils_1.getDataForDataName)(data, "flat..")).toBe(undefined);
121
- expect((0, utils_1.getDataForDataName)(data, ".")).toBe(undefined);
122
- });
123
- });