@moreapp/common-nodejs 0.7.5 → 0.7.7
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/tracer.js +1 -1
- package/dist/utils.d.ts +4 -1
- package/dist/utils.js +36 -5
- package/dist/utils.test.js +47 -12
- package/package.json +29 -27
package/dist/tracer.js
CHANGED
|
@@ -41,7 +41,7 @@ const tracer = (serviceName, { http = {
|
|
|
41
41
|
injectEncoding: propagator_b3_1.B3InjectEncoding.MULTI_HEADER,
|
|
42
42
|
}),
|
|
43
43
|
});
|
|
44
|
-
if (
|
|
44
|
+
if ((0, utils_1.environmentVariable)("STACKDRIVER_TRACING_ENABLED", false)) {
|
|
45
45
|
const exporter = new opentelemetry_cloud_trace_exporter_1.TraceExporter({
|
|
46
46
|
resourceFilter: /^service\./,
|
|
47
47
|
});
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
export declare function environmentVariable(key: string
|
|
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;
|
|
2
5
|
export declare function currentTraceId(): string | undefined;
|
|
3
6
|
export declare function isValidEmail(email: string): boolean;
|
package/dist/utils.js
CHANGED
|
@@ -1,22 +1,53 @@
|
|
|
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
|
exports.isValidEmail = exports.currentTraceId = exports.environmentVariable = void 0;
|
|
4
7
|
const context_utils_1 = require("@opentelemetry/api/build/src/trace/context-utils");
|
|
5
8
|
const api_1 = require("@opentelemetry/api");
|
|
9
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
6
10
|
function environmentVariable(key, fallback) {
|
|
7
11
|
const value = process.env[key] || fallback;
|
|
8
|
-
if (value) {
|
|
9
|
-
|
|
12
|
+
if (value === undefined) {
|
|
13
|
+
throw new Error(`Missing environment variable '${key}'`);
|
|
10
14
|
}
|
|
11
|
-
|
|
15
|
+
else if (fallback === undefined) {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
else if (lodash_1.default.isString(fallback) && lodash_1.default.isString(value)) {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
else if (lodash_1.default.isFinite(fallback) && lodash_1.default.isFinite(Number(value))) {
|
|
22
|
+
return Number(value);
|
|
23
|
+
}
|
|
24
|
+
else if (lodash_1.default.isBoolean(fallback) &&
|
|
25
|
+
(lodash_1.default.isBoolean(value) || value === "true" || value === "false")) {
|
|
26
|
+
return Boolean(value);
|
|
27
|
+
}
|
|
28
|
+
throw new Error(`Wrong type for environment variable '${key}'`);
|
|
12
29
|
}
|
|
13
30
|
exports.environmentVariable = environmentVariable;
|
|
14
31
|
function currentTraceId() {
|
|
15
32
|
return (0, context_utils_1.getSpanContext)(api_1.context.active())?.traceId;
|
|
16
33
|
}
|
|
17
34
|
exports.currentTraceId = currentTraceId;
|
|
18
|
-
const
|
|
35
|
+
const emailAddressMaxLength = 254; // https://stackoverflow.com/a/574698
|
|
36
|
+
const emailAddressLocalPattern = /^.+$/;
|
|
37
|
+
const emailAddressDomainPattern = /^(?:[a-z0-9À-ž](?:[a-z0-9À-ž-]{0,61}[a-z0-9À-ž])?\.)+[a-z0-9À-ž][a-z0-9À-ž-]{0,61}[a-z0-9]$/;
|
|
38
|
+
// There are many regex patterns to validate email addresses, but they all have flaws and cannot 100% correctly verify
|
|
39
|
+
// every possible email address. For us, it's more important to avoid the majority of typo's instead of catching every
|
|
40
|
+
// invalid email address. We let our email provider handle and false positives (either by their validation or the fact
|
|
41
|
+
// that the email could not be delivered.
|
|
42
|
+
// ! This code is duplicated in other projects, make sure that changes are made there too (including tests).
|
|
19
43
|
function isValidEmail(email) {
|
|
20
|
-
|
|
44
|
+
if (email.length > emailAddressMaxLength) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
const [localAddr, domain] = email.split("@");
|
|
48
|
+
if (localAddr === undefined || domain === undefined) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return emailAddressLocalPattern.test(localAddr) && emailAddressDomainPattern.test(domain);
|
|
21
52
|
}
|
|
22
53
|
exports.isValidEmail = isValidEmail;
|
package/dist/utils.test.js
CHANGED
|
@@ -3,23 +3,58 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const utils_1 = require("./utils");
|
|
4
4
|
describe("environmentVariable", () => {
|
|
5
5
|
test("Should throw when environment variable does not exist", async () => {
|
|
6
|
-
|
|
7
|
-
expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_EXISTING")).toBe("SOME_VALUE");
|
|
6
|
+
expect(() => (0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING")).toThrow("Missing environment variable 'COMMONS_NODEJS_TEST_NON_EXISTING'");
|
|
8
7
|
});
|
|
9
|
-
test("
|
|
10
|
-
expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING", "
|
|
8
|
+
test("Should return fallback value when environment variable does not exist (string)", async () => {
|
|
9
|
+
expect((0, utils_1.environmentVariable)("COMMONS_NODEJS_TEST_NON_EXISTING", "my-fallback")).toBe("my-fallback");
|
|
11
10
|
});
|
|
12
|
-
test("Should
|
|
13
|
-
expect((
|
|
11
|
+
test("Should return fallback value when environment variable does not exist (number)", async () => {
|
|
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)", async () => {
|
|
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)", async () => {
|
|
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)", async () => {
|
|
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)", async () => {
|
|
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)", async () => {
|
|
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)", async () => {
|
|
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'");
|
|
14
36
|
});
|
|
15
37
|
});
|
|
16
38
|
describe("isValidEmail", () => {
|
|
17
|
-
|
|
18
|
-
expect((0, utils_1.isValidEmail)("
|
|
19
|
-
expect((0, utils_1.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());
|
|
20
47
|
});
|
|
21
|
-
|
|
22
|
-
expect((0, utils_1.isValidEmail)("john")).
|
|
23
|
-
expect((0, utils_1.isValidEmail)("
|
|
48
|
+
describe("Invalid email addresses", () => {
|
|
49
|
+
test("Missing '@' character and domain", () => expect((0, utils_1.isValidEmail)("john")).toBeFalsy());
|
|
50
|
+
test("// Multiple '@' characters", () => expect((0, utils_1.isValidEmail)("a@b@moreapp.dev")).toBeFalsy());
|
|
51
|
+
test("// Empty", () => expect((0, utils_1.isValidEmail)("")).toBeFalsy());
|
|
52
|
+
test("// Whitespace before/after", () => expect((0, utils_1.isValidEmail)(" a@moreapp.dev ")).toBeFalsy());
|
|
53
|
+
test("// Newlines, tabs, ..., before/after", () => expect((0, utils_1.isValidEmail)("\n \ta@moreapp.dev\n ")).toBeFalsy());
|
|
54
|
+
test("// Missing '@' character", () => expect((0, utils_1.isValidEmail)("ab.com")).toBeFalsy());
|
|
55
|
+
test("// Double dot in domain", () => expect((0, utils_1.isValidEmail)("a@moreapp..com")).toBeFalsy());
|
|
56
|
+
test("// Too short TLD", () => expect((0, utils_1.isValidEmail)("a@b.c")).toBeFalsy());
|
|
57
|
+
test("// Missing local part", () => expect((0, utils_1.isValidEmail)("@moreapp.dev")).toBeFalsy());
|
|
58
|
+
test("// Too long", () => expect((0, utils_1.isValidEmail)("abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-@moreapp.dev")).toBeFalsy());
|
|
24
59
|
});
|
|
25
60
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moreapp/common-nodejs",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.7",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,42 +18,44 @@
|
|
|
18
18
|
"dist/**/*"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@google-cloud/logging": "10.
|
|
22
|
-
"@google-cloud/opentelemetry-cloud-trace-exporter": "2.
|
|
21
|
+
"@google-cloud/logging": "10.5.0",
|
|
22
|
+
"@google-cloud/opentelemetry-cloud-trace-exporter": "2.1.0",
|
|
23
23
|
"@opentelemetry/api": "1.4.1",
|
|
24
|
-
"@opentelemetry/instrumentation": "0.
|
|
25
|
-
"@opentelemetry/instrumentation-dns": "0.
|
|
26
|
-
"@opentelemetry/instrumentation-express": "0.
|
|
27
|
-
"@opentelemetry/instrumentation-http": "0.
|
|
28
|
-
"@opentelemetry/instrumentation-winston": "0.
|
|
29
|
-
"@opentelemetry/sdk-node": "0.
|
|
30
|
-
"@opentelemetry/sdk-trace-node": "1.
|
|
31
|
-
"axios": "1.
|
|
24
|
+
"@opentelemetry/instrumentation": "0.41.2",
|
|
25
|
+
"@opentelemetry/instrumentation-dns": "0.32.4",
|
|
26
|
+
"@opentelemetry/instrumentation-express": "0.33.3",
|
|
27
|
+
"@opentelemetry/instrumentation-http": "0.41.2",
|
|
28
|
+
"@opentelemetry/instrumentation-winston": "0.32.2",
|
|
29
|
+
"@opentelemetry/sdk-node": "0.41.2",
|
|
30
|
+
"@opentelemetry/sdk-trace-node": "1.15.2",
|
|
31
|
+
"axios": "1.5.1",
|
|
32
32
|
"content-disposition-parser": "1.0.2",
|
|
33
|
-
"date-and-time": "3.0.
|
|
34
|
-
"
|
|
35
|
-
"
|
|
33
|
+
"date-and-time": "3.0.3",
|
|
34
|
+
"lodash": "4.17.21",
|
|
35
|
+
"winston": "3.10.0",
|
|
36
|
+
"zod": "3.22.4"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
|
-
"@types/jest": "29.5.
|
|
39
|
-
"@types/
|
|
40
|
-
"@
|
|
41
|
-
"@typescript-eslint/
|
|
42
|
-
"eslint": "
|
|
43
|
-
"eslint
|
|
44
|
-
"eslint-config-
|
|
45
|
-
"eslint-
|
|
39
|
+
"@types/jest": "29.5.10",
|
|
40
|
+
"@types/lodash": "4.14.202",
|
|
41
|
+
"@types/node": "18.17.19",
|
|
42
|
+
"@typescript-eslint/eslint-plugin": "5.62.0",
|
|
43
|
+
"@typescript-eslint/parser": "5.62.0",
|
|
44
|
+
"eslint": "8.48.0",
|
|
45
|
+
"eslint-config-airbnb-typescript": "17.1.0",
|
|
46
|
+
"eslint-config-prettier": "8.10.0",
|
|
47
|
+
"eslint-plugin-import": "2.28.1",
|
|
46
48
|
"eslint-plugin-prettier": "4.2.1",
|
|
47
49
|
"husky": "8.0.3",
|
|
48
|
-
"jest": "29.
|
|
49
|
-
"jest-mock-extended": "3.0.
|
|
50
|
+
"jest": "29.6.4",
|
|
51
|
+
"jest-mock-extended": "3.0.5",
|
|
50
52
|
"jest-sonar-reporter": "2.0.0",
|
|
51
|
-
"lint-staged": "13.
|
|
52
|
-
"nock": "13.3.
|
|
53
|
+
"lint-staged": "13.3.0",
|
|
54
|
+
"nock": "13.3.8",
|
|
53
55
|
"prettier": "2.8.8",
|
|
54
56
|
"ts-jest": "29.1.1",
|
|
55
57
|
"ts-node": "10.9.1",
|
|
56
|
-
"typescript": "5.
|
|
58
|
+
"typescript": "5.2.2"
|
|
57
59
|
},
|
|
58
60
|
"prettier": {
|
|
59
61
|
"printWidth": 100,
|