@moreapp/common-nodejs 0.12.2 → 0.12.5
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.
- package/dist/__tests__/MoreAppClient.test.js +85 -0
- package/dist/__tests__/dateUtil.test.js +42 -0
- package/dist/{logger.test.js → __tests__/logger.test.js} +23 -10
- package/dist/__tests__/utils.test.js +90 -0
- package/dist/dateUtil.js +38 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/logger.js +2 -1
- package/dist/observability/TerminationHandler.js +5 -1
- package/dist/observability/{ExpressRequestTracker.test.js → __tests__/ExpressRequestTracker.test.js} +16 -15
- package/dist/observability/{ObservabilityServer.test.js → __tests__/ObservabilityServer.test.js} +30 -23
- package/dist/observability/__tests__/TerminationHandler.test.js +171 -0
- package/dist/observability/index.d.ts +2 -1
- package/dist/observability/index.js +3 -1
- package/dist/observability/tracer.d.ts +12 -0
- package/dist/{tracer.js → observability/tracer.js} +49 -8
- package/dist/utils/__tests__/environmentVariable.test.d.ts +1 -0
- package/dist/utils/__tests__/environmentVariable.test.js +38 -0
- package/dist/utils/environmentVariable.d.ts +4 -0
- package/dist/utils/environmentVariable.js +23 -0
- package/dist/utils.d.ts +0 -4
- package/dist/utils.js +1 -55
- package/package.json +31 -31
- package/dist/MoreAppClient.test.js +0 -79
- package/dist/dateUtil.test.js +0 -41
- package/dist/observability/TerminationHandler.test.js +0 -160
- package/dist/testUtils.d.ts +0 -1
- package/dist/testUtils.js +0 -13
- package/dist/tracer.d.ts +0 -8
- package/dist/utils.test.js +0 -123
- /package/dist/{MoreAppClient.test.d.ts → __tests__/MoreAppClient.test.d.ts} +0 -0
- /package/dist/{dateUtil.test.d.ts → __tests__/dateUtil.test.d.ts} +0 -0
- /package/dist/{logger.test.d.ts → __tests__/logger.test.d.ts} +0 -0
- /package/dist/{utils.test.d.ts → __tests__/utils.test.d.ts} +0 -0
- /package/dist/observability/{ExpressRequestTracker.test.d.ts → __tests__/ExpressRequestTracker.test.d.ts} +0 -0
- /package/dist/observability/{ObservabilityServer.test.d.ts → __tests__/ObservabilityServer.test.d.ts} +0 -0
- /package/dist/observability/{TerminationHandler.test.d.ts → __tests__/TerminationHandler.test.d.ts} +0 -0
|
@@ -0,0 +1,171 @@
|
|
|
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 vitest_1 = require("vitest");
|
|
40
|
+
const node_events_1 = require("node:events");
|
|
41
|
+
const timers = __importStar(require("node:timers"));
|
|
42
|
+
const TerminationHandler_1 = __importDefault(require("../TerminationHandler"));
|
|
43
|
+
vitest_1.vi.mock("../../logger", () => ({
|
|
44
|
+
logger: {
|
|
45
|
+
info: vitest_1.vi.fn(),
|
|
46
|
+
warn: vitest_1.vi.fn(),
|
|
47
|
+
error: vitest_1.vi.fn(),
|
|
48
|
+
debug: vitest_1.vi.fn(),
|
|
49
|
+
},
|
|
50
|
+
}));
|
|
51
|
+
// Mock tracer
|
|
52
|
+
vitest_1.vi.mock("../tracer", () => ({
|
|
53
|
+
default: {
|
|
54
|
+
shutdown: vitest_1.vi.fn(() => Promise.resolve()),
|
|
55
|
+
},
|
|
56
|
+
}));
|
|
57
|
+
// Mock node:timers so we can control callbacks (TerminationHandler imports from node:timers)
|
|
58
|
+
vitest_1.vi.mock("node:timers", () => ({
|
|
59
|
+
setInterval: vitest_1.vi.fn(),
|
|
60
|
+
clearInterval: vitest_1.vi.fn(),
|
|
61
|
+
setTimeout: vitest_1.vi.fn(),
|
|
62
|
+
}));
|
|
63
|
+
(0, vitest_1.describe)("TerminationHandler", () => {
|
|
64
|
+
let intervalCb;
|
|
65
|
+
let timeoutCallbacks = [];
|
|
66
|
+
(0, vitest_1.beforeEach)(() => {
|
|
67
|
+
intervalCb = undefined;
|
|
68
|
+
timeoutCallbacks = [];
|
|
69
|
+
vitest_1.vi.mocked(timers.setInterval).mockImplementation((cb) => {
|
|
70
|
+
intervalCb = cb;
|
|
71
|
+
return 1;
|
|
72
|
+
});
|
|
73
|
+
vitest_1.vi.mocked(timers.clearInterval).mockImplementation(() => { });
|
|
74
|
+
vitest_1.vi.spyOn(globalThis, "setTimeout").mockImplementation((cb) => {
|
|
75
|
+
timeoutCallbacks.push(cb);
|
|
76
|
+
return timeoutCallbacks.length;
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
(0, vitest_1.afterEach)(() => {
|
|
80
|
+
vitest_1.vi.resetAllMocks();
|
|
81
|
+
});
|
|
82
|
+
const createFakeServer = () => {
|
|
83
|
+
const emitter = new node_events_1.EventEmitter();
|
|
84
|
+
const server = {
|
|
85
|
+
close: vitest_1.vi.fn((cb) => {
|
|
86
|
+
if (cb)
|
|
87
|
+
cb();
|
|
88
|
+
return server;
|
|
89
|
+
}),
|
|
90
|
+
on: vitest_1.vi.fn((event, listener) => {
|
|
91
|
+
emitter.on(event, listener);
|
|
92
|
+
return server;
|
|
93
|
+
}),
|
|
94
|
+
emit: emitter.emit.bind(emitter),
|
|
95
|
+
};
|
|
96
|
+
return server;
|
|
97
|
+
};
|
|
98
|
+
const createSocket = () => {
|
|
99
|
+
const emitter = new node_events_1.EventEmitter();
|
|
100
|
+
const socket = emitter;
|
|
101
|
+
socket.destroy = vitest_1.vi.fn();
|
|
102
|
+
return socket;
|
|
103
|
+
};
|
|
104
|
+
(0, vitest_1.test)("shutdown waits for active requests to reach 0 then stops and exits 0", async () => {
|
|
105
|
+
const server = createFakeServer();
|
|
106
|
+
const hs = {
|
|
107
|
+
setReady: vitest_1.vi.fn(),
|
|
108
|
+
stop: vitest_1.vi.fn(() => Promise.resolve()),
|
|
109
|
+
};
|
|
110
|
+
const requestTracker = { nrOfActiveRequests: vitest_1.vi.fn(() => 0) };
|
|
111
|
+
const exitSpy = vitest_1.vi.spyOn(process, "exit").mockImplementation(() => undefined);
|
|
112
|
+
const th = new TerminationHandler_1.default(server, hs, requestTracker);
|
|
113
|
+
th.shutdown();
|
|
114
|
+
(0, vitest_1.expect)(hs.setReady).toHaveBeenCalledWith(false);
|
|
115
|
+
(0, vitest_1.expect)(server.close).toHaveBeenCalled();
|
|
116
|
+
// First interval tick should see 0 active and exit
|
|
117
|
+
intervalCb?.();
|
|
118
|
+
// ensure hs.stop called
|
|
119
|
+
(0, vitest_1.expect)(hs.stop).toHaveBeenCalled();
|
|
120
|
+
// Wait for Promise.all and .finally() to execute
|
|
121
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
122
|
+
// Trigger log flush timeout (index 1, after the 30s timeout at index 0)
|
|
123
|
+
timeoutCallbacks[1]?.();
|
|
124
|
+
(0, vitest_1.expect)(exitSpy).toHaveBeenCalledWith(0);
|
|
125
|
+
});
|
|
126
|
+
(0, vitest_1.test)("shutdown polls until requests finish then exits 0", async () => {
|
|
127
|
+
const server = createFakeServer();
|
|
128
|
+
const hs = {
|
|
129
|
+
setReady: vitest_1.vi.fn(),
|
|
130
|
+
stop: vitest_1.vi.fn(() => Promise.resolve()),
|
|
131
|
+
};
|
|
132
|
+
const requestTracker = { nrOfActiveRequests: vitest_1.vi.fn() };
|
|
133
|
+
// Return >0 first, then 0
|
|
134
|
+
requestTracker.nrOfActiveRequests.mockReturnValueOnce(2).mockReturnValueOnce(0);
|
|
135
|
+
const exitSpy = vitest_1.vi.spyOn(process, "exit").mockImplementation(() => undefined);
|
|
136
|
+
const th = new TerminationHandler_1.default(server, hs, requestTracker);
|
|
137
|
+
th.shutdown();
|
|
138
|
+
// First tick: still waiting
|
|
139
|
+
intervalCb?.();
|
|
140
|
+
(0, vitest_1.expect)(exitSpy).not.toHaveBeenCalled();
|
|
141
|
+
// Second tick: now 0
|
|
142
|
+
intervalCb?.();
|
|
143
|
+
(0, vitest_1.expect)(hs.stop).toHaveBeenCalled();
|
|
144
|
+
// Wait for Promise.all and .finally() to execute
|
|
145
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
146
|
+
// Trigger log flush timeout (index 1, after the 30s timeout at index 0)
|
|
147
|
+
timeoutCallbacks[1]?.();
|
|
148
|
+
(0, vitest_1.expect)(exitSpy).toHaveBeenCalledWith(0);
|
|
149
|
+
});
|
|
150
|
+
(0, vitest_1.test)("forceful stop after 30s destroys sockets and exits 1", () => {
|
|
151
|
+
const server = createFakeServer();
|
|
152
|
+
const hs = {
|
|
153
|
+
setReady: vitest_1.vi.fn(),
|
|
154
|
+
stop: vitest_1.vi.fn(() => Promise.resolve()),
|
|
155
|
+
};
|
|
156
|
+
const requestTracker = { nrOfActiveRequests: vitest_1.vi.fn(() => 5) }; // never reaches 0
|
|
157
|
+
const exitSpy = vitest_1.vi.spyOn(process, "exit").mockImplementation(() => undefined);
|
|
158
|
+
const th = new TerminationHandler_1.default(server, hs, requestTracker);
|
|
159
|
+
// Register a socket via the server's connection event
|
|
160
|
+
const socket = createSocket();
|
|
161
|
+
server.emit("connection", socket);
|
|
162
|
+
th.shutdown();
|
|
163
|
+
// Before timeout callback, no exit
|
|
164
|
+
(0, vitest_1.expect)(exitSpy).not.toHaveBeenCalled();
|
|
165
|
+
(0, vitest_1.expect)(socket.destroy).not.toHaveBeenCalled();
|
|
166
|
+
// Trigger shutdown timeout (index 0 is the 30s timeout)
|
|
167
|
+
timeoutCallbacks[0]?.();
|
|
168
|
+
(0, vitest_1.expect)(socket.destroy).toHaveBeenCalled();
|
|
169
|
+
(0, vitest_1.expect)(exitSpy).toHaveBeenCalledWith(1);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import ExpressRequestTracker from "./ExpressRequestTracker";
|
|
2
2
|
import ObservabilityServer from "./ObservabilityServer";
|
|
3
3
|
import TerminationHandler from "./TerminationHandler";
|
|
4
|
-
|
|
4
|
+
import tracer from "./tracer";
|
|
5
|
+
export { ExpressRequestTracker, ObservabilityServer, TerminationHandler, tracer };
|
|
@@ -3,10 +3,12 @@ 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
|
-
exports.TerminationHandler = exports.ObservabilityServer = exports.ExpressRequestTracker = void 0;
|
|
6
|
+
exports.tracer = exports.TerminationHandler = exports.ObservabilityServer = exports.ExpressRequestTracker = void 0;
|
|
7
7
|
const ExpressRequestTracker_1 = __importDefault(require("./ExpressRequestTracker"));
|
|
8
8
|
exports.ExpressRequestTracker = ExpressRequestTracker_1.default;
|
|
9
9
|
const ObservabilityServer_1 = __importDefault(require("./ObservabilityServer"));
|
|
10
10
|
exports.ObservabilityServer = ObservabilityServer_1.default;
|
|
11
11
|
const TerminationHandler_1 = __importDefault(require("./TerminationHandler"));
|
|
12
12
|
exports.TerminationHandler = TerminationHandler_1.default;
|
|
13
|
+
const tracer_1 = __importDefault(require("./tracer"));
|
|
14
|
+
exports.tracer = tracer_1.default;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as types from "@opentelemetry/instrumentation/build/src/types";
|
|
2
|
+
declare const _default: {
|
|
3
|
+
start: (serviceName: string, { http, extraInstrumentations, debug, }: {
|
|
4
|
+
http?: {
|
|
5
|
+
portsToInstrument: number[];
|
|
6
|
+
};
|
|
7
|
+
extraInstrumentations?: types.Instrumentation[];
|
|
8
|
+
debug?: boolean;
|
|
9
|
+
}) => void;
|
|
10
|
+
shutdown: () => Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
export default _default;
|
|
@@ -1,6 +1,38 @@
|
|
|
1
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
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.tracer = void 0;
|
|
4
36
|
const api_1 = require("@opentelemetry/api");
|
|
5
37
|
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
6
38
|
const instrumentation_1 = require("@opentelemetry/instrumentation");
|
|
@@ -12,21 +44,23 @@ const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
|
12
44
|
const resources_1 = require("@opentelemetry/resources");
|
|
13
45
|
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
14
46
|
const propagator_b3_1 = require("@opentelemetry/propagator-b3");
|
|
15
|
-
const
|
|
47
|
+
const environmentVariable_1 = require("../utils/environmentVariable");
|
|
16
48
|
const exporter_trace_otlp_grpc_1 = require("@opentelemetry/exporter-trace-otlp-grpc");
|
|
17
|
-
const
|
|
49
|
+
const lodash_1 = require("lodash");
|
|
50
|
+
let provider;
|
|
51
|
+
const start = (serviceName, { http = {
|
|
18
52
|
portsToInstrument: [3000],
|
|
19
53
|
}, extraInstrumentations = [], debug = false, }) => {
|
|
20
54
|
if (debug) {
|
|
21
55
|
api_1.diag.setLogger(new api_1.DiagConsoleLogger(), api_1.DiagLogLevel.DEBUG);
|
|
22
56
|
}
|
|
23
|
-
|
|
57
|
+
provider = new sdk_trace_node_1.NodeTracerProvider({
|
|
24
58
|
resource: (0, resources_1.resourceFromAttributes)({
|
|
25
|
-
[semantic_conventions_1.
|
|
26
|
-
[semantic_conventions_1.
|
|
59
|
+
[semantic_conventions_1.ATTR_SERVICE_NAME]: serviceName,
|
|
60
|
+
[semantic_conventions_1.ATTR_SERVICE_INSTANCE_ID]: (0, environmentVariable_1.environmentVariable)("HOSTNAME", "local"),
|
|
27
61
|
}),
|
|
28
62
|
spanProcessors: [
|
|
29
|
-
...((0,
|
|
63
|
+
...((0, environmentVariable_1.environmentVariable)("STACKDRIVER_TRACING_ENABLED", false)
|
|
30
64
|
? [new sdk_trace_base_1.BatchSpanProcessor(new exporter_trace_otlp_grpc_1.OTLPTraceExporter())]
|
|
31
65
|
: []),
|
|
32
66
|
...(debug ? [new sdk_trace_base_1.BatchSpanProcessor(new sdk_trace_node_1.ConsoleSpanExporter())] : []),
|
|
@@ -51,4 +85,11 @@ const tracer = (serviceName, { http = {
|
|
|
51
85
|
],
|
|
52
86
|
});
|
|
53
87
|
};
|
|
54
|
-
|
|
88
|
+
const shutdown = async () => {
|
|
89
|
+
if (!(0, lodash_1.isNil)(provider)) {
|
|
90
|
+
const { logger } = await Promise.resolve().then(() => __importStar(require("../logger")));
|
|
91
|
+
logger.info("Shutting down tracer");
|
|
92
|
+
await provider.shutdown();
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
exports.default = { start, shutdown };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const environmentVariable_1 = require("../environmentVariable");
|
|
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, environmentVariable_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, environmentVariable_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, environmentVariable_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, environmentVariable_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, environmentVariable_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, environmentVariable_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, environmentVariable_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, environmentVariable_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, environmentVariable_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING", false)).toThrow("Wrong type for environment variable 'COMMONS_NODEJS_TEST_EXISTING'");
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function environmentVariable(key: string): string;
|
|
2
|
+
export declare function environmentVariable(key: string, fallback: string): string;
|
|
3
|
+
export declare function environmentVariable(key: string, fallback: number): number;
|
|
4
|
+
export declare function environmentVariable(key: string, fallback: boolean): boolean;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.environmentVariable = environmentVariable;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
function environmentVariable(key, fallback) {
|
|
6
|
+
const value = process.env[key] || fallback;
|
|
7
|
+
if (value === undefined) {
|
|
8
|
+
throw new Error(`Missing environment variable '${key}'`);
|
|
9
|
+
}
|
|
10
|
+
else if (fallback === undefined) {
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
else if ((0, lodash_1.isString)(fallback) && (0, lodash_1.isString)(value)) {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
else if ((0, lodash_1.isFinite)(fallback) && (0, lodash_1.isFinite)(Number(value))) {
|
|
17
|
+
return Number(value);
|
|
18
|
+
}
|
|
19
|
+
else if ((0, lodash_1.isBoolean)(fallback) && ((0, lodash_1.isBoolean)(value) || value === "true" || value === "false")) {
|
|
20
|
+
return Boolean(value);
|
|
21
|
+
}
|
|
22
|
+
throw new Error(`Wrong type for environment variable '${key}'`);
|
|
23
|
+
}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { FormVersionField } from "./types";
|
|
2
|
-
export declare function environmentVariable(key: string): string;
|
|
3
|
-
export declare function environmentVariable(key: string, fallback: string): string;
|
|
4
|
-
export declare function environmentVariable(key: string, fallback: number): number;
|
|
5
|
-
export declare function environmentVariable(key: string, fallback: boolean): boolean;
|
|
6
2
|
export declare function currentTraceId(): string | undefined;
|
|
7
3
|
export declare function isValidEmail(email: string): boolean;
|
|
8
4
|
export declare function parseDynamicRecipients(dynamicRecipients: string[], fields: FormVersionField[], data: Record<string, unknown>): string[];
|
package/dist/utils.js
CHANGED
|
@@ -1,65 +1,11 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.environmentVariable = environmentVariable;
|
|
37
3
|
exports.currentTraceId = currentTraceId;
|
|
38
4
|
exports.isValidEmail = isValidEmail;
|
|
39
5
|
exports.parseDynamicRecipients = parseDynamicRecipients;
|
|
40
6
|
exports.getDataForDataName = getDataForDataName;
|
|
41
7
|
const api_1 = require("@opentelemetry/api");
|
|
42
|
-
const lodash_1 =
|
|
43
|
-
function environmentVariable(key, fallback) {
|
|
44
|
-
const value = process.env[key] || fallback;
|
|
45
|
-
if (value === undefined) {
|
|
46
|
-
throw new Error(`Missing environment variable '${key}'`);
|
|
47
|
-
}
|
|
48
|
-
else if (fallback === undefined) {
|
|
49
|
-
return value;
|
|
50
|
-
}
|
|
51
|
-
else if (lodash_1.default.isString(fallback) && lodash_1.default.isString(value)) {
|
|
52
|
-
return value;
|
|
53
|
-
}
|
|
54
|
-
else if (lodash_1.default.isFinite(fallback) && lodash_1.default.isFinite(Number(value))) {
|
|
55
|
-
return Number(value);
|
|
56
|
-
}
|
|
57
|
-
else if (lodash_1.default.isBoolean(fallback) &&
|
|
58
|
-
(lodash_1.default.isBoolean(value) || value === "true" || value === "false")) {
|
|
59
|
-
return Boolean(value);
|
|
60
|
-
}
|
|
61
|
-
throw new Error(`Wrong type for environment variable '${key}'`);
|
|
62
|
-
}
|
|
8
|
+
const lodash_1 = require("lodash");
|
|
63
9
|
function currentTraceId() {
|
|
64
10
|
return api_1.trace.getSpanContext(api_1.context.active())?.traceId;
|
|
65
11
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moreapp/common-nodejs",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.5",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
"types": "./dist/index.d.ts"
|
|
11
11
|
},
|
|
12
12
|
"./tracer": {
|
|
13
|
-
"default": "./dist/tracer.js",
|
|
14
|
-
"types": "./dist/tracer.d.ts"
|
|
13
|
+
"default": "./dist/observability/tracer.js",
|
|
14
|
+
"types": "./dist/observability/tracer.d.ts"
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
18
|
"build": "tsc",
|
|
19
|
-
"test": "
|
|
20
|
-
"test:workflow": "
|
|
19
|
+
"test": "vitest run",
|
|
20
|
+
"test:workflow": "vitest run --coverage",
|
|
21
21
|
"prettier": "prettier './**/*.{ts,json,yaml,md,}'",
|
|
22
22
|
"format:check": "yarn prettier --check",
|
|
23
23
|
"format:fix": "yarn prettier --write",
|
|
@@ -30,21 +30,21 @@
|
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@opentelemetry/api": "1.9.1",
|
|
33
|
-
"@opentelemetry/core": "2.
|
|
34
|
-
"@opentelemetry/exporter-trace-otlp-grpc": "0.
|
|
35
|
-
"@opentelemetry/instrumentation": "0.
|
|
36
|
-
"@opentelemetry/instrumentation-dns": "0.
|
|
37
|
-
"@opentelemetry/instrumentation-express": "0.
|
|
38
|
-
"@opentelemetry/instrumentation-http": "0.
|
|
39
|
-
"@opentelemetry/instrumentation-winston": "0.
|
|
40
|
-
"@opentelemetry/resources": "2.
|
|
41
|
-
"@opentelemetry/sdk-node": "0.
|
|
42
|
-
"@opentelemetry/sdk-trace-base": "2.
|
|
43
|
-
"@opentelemetry/sdk-trace-node": "2.
|
|
33
|
+
"@opentelemetry/core": "2.8.0",
|
|
34
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "0.219.0",
|
|
35
|
+
"@opentelemetry/instrumentation": "0.219.0",
|
|
36
|
+
"@opentelemetry/instrumentation-dns": "0.62.0",
|
|
37
|
+
"@opentelemetry/instrumentation-express": "0.67.0",
|
|
38
|
+
"@opentelemetry/instrumentation-http": "0.219.0",
|
|
39
|
+
"@opentelemetry/instrumentation-winston": "0.63.0",
|
|
40
|
+
"@opentelemetry/resources": "2.8.0",
|
|
41
|
+
"@opentelemetry/sdk-node": "0.219.0",
|
|
42
|
+
"@opentelemetry/sdk-trace-base": "2.8.0",
|
|
43
|
+
"@opentelemetry/sdk-trace-node": "2.8.0",
|
|
44
44
|
"@opentelemetry/semantic-conventions": "1.41.1",
|
|
45
|
-
"axios": "1.
|
|
45
|
+
"axios": "1.18.1",
|
|
46
46
|
"content-disposition-parser": "1.0.2",
|
|
47
|
-
"date-and-time": "4.5.
|
|
47
|
+
"date-and-time": "4.5.1",
|
|
48
48
|
"express": "5.2.1",
|
|
49
49
|
"lodash": "4.18.1",
|
|
50
50
|
"prom-client": "15.1.3",
|
|
@@ -53,24 +53,24 @@
|
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/express": "5.0.6",
|
|
56
|
-
"@types/jest": "30.0.0",
|
|
57
56
|
"@types/lodash": "4.17.24",
|
|
58
|
-
"@types/node": "24.
|
|
59
|
-
"@typescript-eslint/eslint-plugin": "8.
|
|
60
|
-
"@typescript-eslint/parser": "8.
|
|
57
|
+
"@types/node": "24.13.2",
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "8.61.1",
|
|
59
|
+
"@typescript-eslint/parser": "8.61.1",
|
|
60
|
+
"@vitest/coverage-v8": "4.1.9",
|
|
61
|
+
"@vitest/ui": "4.1.9",
|
|
61
62
|
"eslint": "9.39.4",
|
|
62
63
|
"eslint-config-prettier": "10.1.8",
|
|
63
|
-
"eslint-plugin-prettier": "5.5.
|
|
64
|
+
"eslint-plugin-prettier": "5.5.6",
|
|
64
65
|
"husky": "9.1.7",
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"nock": "14.0.15",
|
|
69
|
-
"prettier": "3.8.3",
|
|
70
|
-
"ts-jest": "29.4.9",
|
|
66
|
+
"lint-staged": "17.0.8",
|
|
67
|
+
"nock": "14.0.16",
|
|
68
|
+
"prettier": "3.8.5",
|
|
71
69
|
"ts-node": "10.9.2",
|
|
72
|
-
"typescript": "
|
|
73
|
-
"typescript-eslint": "8.
|
|
70
|
+
"typescript": "6.0.3",
|
|
71
|
+
"typescript-eslint": "8.61.1",
|
|
72
|
+
"vitest": "4.1.9",
|
|
73
|
+
"vitest-mock-extended": "4.0.0"
|
|
74
74
|
},
|
|
75
75
|
"prettier": {
|
|
76
76
|
"printWidth": 100,
|
|
@@ -1,79 +0,0 @@
|
|
|
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 nock_1 = __importDefault(require("nock"));
|
|
7
|
-
const axios_1 = require("axios");
|
|
8
|
-
const MoreAppClient_1 = __importDefault(require("./MoreAppClient"));
|
|
9
|
-
test("download binary file", async () => {
|
|
10
|
-
const file = Buffer.from("Some data");
|
|
11
|
-
const apiMock = (0, nock_1.default)("https://api.moreapp.com")
|
|
12
|
-
.get("/download")
|
|
13
|
-
.matchHeader("x-more-seal", "my-seal")
|
|
14
|
-
.reply(200, file, {
|
|
15
|
-
"Content-Type": "image/jpg",
|
|
16
|
-
"content-disposition": 'attachment; filename="my%20photo.jpg"',
|
|
17
|
-
});
|
|
18
|
-
const client = new MoreAppClient_1.default({
|
|
19
|
-
serviceName: "Common Test",
|
|
20
|
-
prefix: "https://api.moreapp.com",
|
|
21
|
-
seal: "my-seal",
|
|
22
|
-
});
|
|
23
|
-
const binaryFile = await client.getBinary("/download");
|
|
24
|
-
expect(binaryFile.buffer).toStrictEqual(file);
|
|
25
|
-
expect(binaryFile.contentType).toBe("image/jpg");
|
|
26
|
-
expect(binaryFile.filename).toBe("my photo.jpg");
|
|
27
|
-
apiMock.done();
|
|
28
|
-
});
|
|
29
|
-
test("404", async () => {
|
|
30
|
-
const apiMock = (0, nock_1.default)("https://api.moreapp.com").get("/download").reply(404);
|
|
31
|
-
const client = new MoreAppClient_1.default({
|
|
32
|
-
serviceName: "Common Test",
|
|
33
|
-
prefix: "https://api.moreapp.com",
|
|
34
|
-
seal: "my-seal",
|
|
35
|
-
});
|
|
36
|
-
await expect(client.getBinary("/download")).rejects.toStrictEqual(new axios_1.AxiosError("Request failed with status code 404"));
|
|
37
|
-
apiMock.done();
|
|
38
|
-
});
|
|
39
|
-
test("unable to connect", async () => {
|
|
40
|
-
const client = new MoreAppClient_1.default({
|
|
41
|
-
serviceName: "Common Test",
|
|
42
|
-
prefix: "https://non-existing.moreapp.com",
|
|
43
|
-
seal: "my-seal",
|
|
44
|
-
});
|
|
45
|
-
await expect(client.getBinary("/download")).rejects.toThrow("getaddrinfo ENOTFOUND non-existing.moreapp.com");
|
|
46
|
-
});
|
|
47
|
-
const client = new MoreAppClient_1.default({
|
|
48
|
-
serviceName: "Common Test",
|
|
49
|
-
prefix: "https://api.moreapp.com",
|
|
50
|
-
seal: "my-seal",
|
|
51
|
-
});
|
|
52
|
-
test("Handle non existing file", async () => {
|
|
53
|
-
const apiMock = (0, nock_1.default)("https://api.moreapp.com").get("/download").reply(404);
|
|
54
|
-
await expect(client.getBinary("/download")).rejects.toThrow("Request failed with status code 404");
|
|
55
|
-
apiMock.done();
|
|
56
|
-
});
|
|
57
|
-
test("Handle missing 'content-type' header", async () => {
|
|
58
|
-
const apiMock = (0, nock_1.default)("https://api.moreapp.com").get("/download").reply(200);
|
|
59
|
-
const binaryFile = await client.getBinary("/download");
|
|
60
|
-
expect(binaryFile).not.toBeNull();
|
|
61
|
-
expect(binaryFile.contentType).toBe("unknown");
|
|
62
|
-
apiMock.done();
|
|
63
|
-
});
|
|
64
|
-
test("Handle missing 'content-disposition' header", async () => {
|
|
65
|
-
const apiMock = (0, nock_1.default)("https://api.moreapp.com").get("/download").reply(200);
|
|
66
|
-
const binaryFile = await client.getBinary("/download");
|
|
67
|
-
expect(binaryFile).not.toBeNull();
|
|
68
|
-
expect(binaryFile.filename).toBeUndefined();
|
|
69
|
-
apiMock.done();
|
|
70
|
-
});
|
|
71
|
-
test("Handle invalid 'content-disposition' header", async () => {
|
|
72
|
-
const apiMock = (0, nock_1.default)("https://api.moreapp.com").get("/download").reply(200, {}, {
|
|
73
|
-
"content-disposition": "filename=my-photo.png",
|
|
74
|
-
});
|
|
75
|
-
const binaryFile = await client.getBinary("/download");
|
|
76
|
-
expect(binaryFile).not.toBeNull();
|
|
77
|
-
expect(binaryFile.filename).toBeUndefined();
|
|
78
|
-
apiMock.done();
|
|
79
|
-
});
|