@moreapp/common-nodejs 0.2.0 → 0.4.0

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/README.md CHANGED
@@ -1,11 +1,6 @@
1
- # Notification service
1
+ # MoreApp Common NodeJS
2
2
 
3
- # Direnv
4
-
5
- `direnv` loads the `.envrc` file automatically
6
- This loads Sparkpost secrets via `kubectl`
7
-
8
- **NOTE:** make sure the dev-cluster in `kubectx` is named `dev`!
3
+ .. TODO
9
4
 
10
5
  # Pre-commit hooks
11
6
 
@@ -0,0 +1,3 @@
1
+ export * from "./tracer";
2
+ export * from "./logger";
3
+ export * from "./utils";
@@ -14,4 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./tracer"), exports);
18
+ __exportStar(require("./logger"), exports);
17
19
  __exportStar(require("./utils"), exports);
@@ -0,0 +1 @@
1
+ export declare const logger: import("winston").Logger;
package/dist/logger.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.logger = void 0;
4
+ const winston_1 = require("winston");
5
+ const entry_1 = require("@google-cloud/logging/build/src/entry");
6
+ const stackdriverTracingFormat = (0, winston_1.format)((info) => {
7
+ if (info["trace_id"] && info["span_id"]) {
8
+ info[entry_1.TRACE_KEY] = info["trace_id"];
9
+ info[entry_1.SPAN_ID_KEY] = info["span_id"];
10
+ info[entry_1.TRACE_SAMPLED_KEY] = true;
11
+ }
12
+ return info;
13
+ });
14
+ exports.logger = (0, winston_1.createLogger)({
15
+ level: "info",
16
+ transports: [new winston_1.transports.Console()],
17
+ format: winston_1.format.combine(stackdriverTracingFormat(), winston_1.format.json()),
18
+ });
@@ -0,0 +1,2 @@
1
+ import * as types from "@opentelemetry/instrumentation/build/src/types";
2
+ export declare const tracer: (serviceName: string, instrumentations: types.Instrumentation[]) => void;
package/dist/tracer.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tracer = void 0;
4
+ const opentelemetry_cloud_trace_exporter_1 = require("@google-cloud/opentelemetry-cloud-trace-exporter");
5
+ const instrumentation_1 = require("@opentelemetry/instrumentation");
6
+ const instrumentation_dns_1 = require("@opentelemetry/instrumentation-dns");
7
+ const instrumentation_http_1 = require("@opentelemetry/instrumentation-http");
8
+ const instrumentation_express_1 = require("@opentelemetry/instrumentation-express");
9
+ const instrumentation_winston_1 = require("@opentelemetry/instrumentation-winston");
10
+ const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
11
+ const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
12
+ const resources_1 = require("@opentelemetry/resources");
13
+ const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
14
+ const propagator_b3_1 = require("@opentelemetry/propagator-b3");
15
+ const utils_1 = require("./utils");
16
+ const tracer = (serviceName, instrumentations) => {
17
+ const provider = new sdk_trace_node_1.NodeTracerProvider({
18
+ resource: new resources_1.Resource({
19
+ [semantic_conventions_1.SemanticResourceAttributes.SERVICE_NAME]: serviceName,
20
+ [semantic_conventions_1.SemanticResourceAttributes.SERVICE_INSTANCE_ID]: (0, utils_1.environmentVariable)("HOSTNAME", "local"),
21
+ }),
22
+ });
23
+ provider.register({
24
+ propagator: new propagator_b3_1.B3Propagator({
25
+ injectEncoding: propagator_b3_1.B3InjectEncoding.MULTI_HEADER,
26
+ }),
27
+ });
28
+ (0, instrumentation_1.registerInstrumentations)({
29
+ instrumentations: [
30
+ new instrumentation_dns_1.DnsInstrumentation(),
31
+ new instrumentation_http_1.HttpInstrumentation({
32
+ ignoreIncomingRequestHook: (_req) => {
33
+ // Disable instrumentation for incoming requests, handled by ExpressInstrumentation below
34
+ return true;
35
+ },
36
+ }),
37
+ new instrumentation_express_1.ExpressInstrumentation(),
38
+ new instrumentation_winston_1.WinstonInstrumentation(),
39
+ ...instrumentations,
40
+ ],
41
+ });
42
+ if (process.env["STACKDRIVER_TRACING_ENABLED"] === "true") {
43
+ const exporter = new opentelemetry_cloud_trace_exporter_1.TraceExporter({
44
+ resourceFilter: /^service\./,
45
+ });
46
+ provider.addSpanProcessor(new sdk_trace_base_1.BatchSpanProcessor(exporter));
47
+ }
48
+ };
49
+ exports.tracer = tracer;
@@ -1 +1,2 @@
1
1
  export declare function environmentVariable(key: string, fallback?: string | number): string;
2
+ export declare function currentTraceId(): string | undefined;
package/dist/utils.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.currentTraceId = exports.environmentVariable = void 0;
4
+ const context_utils_1 = require("@opentelemetry/api/build/src/trace/context-utils");
5
+ const api_1 = require("@opentelemetry/api");
6
+ function environmentVariable(key, fallback) {
7
+ const value = process.env[key] || fallback;
8
+ if (value) {
9
+ return value.toString();
10
+ }
11
+ throw new Error(`Missing environment variable '${key}'`);
12
+ }
13
+ exports.environmentVariable = environmentVariable;
14
+ function currentTraceId() {
15
+ return (0, context_utils_1.getSpanContext)(api_1.context.active())?.traceId;
16
+ }
17
+ exports.currentTraceId = currentTraceId;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@moreapp/common-nodejs",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "license": "UNLICENSED",
5
- "main": "build/index.js",
6
- "types": "build/index.d.ts",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
9
  "test": "jest",
@@ -14,7 +14,22 @@
14
14
  "lint:fix": "yarn lint:check --fix",
15
15
  "prepare": "husky install"
16
16
  },
17
- "dependencies": {},
17
+ "files": [
18
+ "dist/**/*"
19
+ ],
20
+ "dependencies": {
21
+ "@google-cloud/logging": "10.4.0",
22
+ "@google-cloud/opentelemetry-cloud-trace-exporter": "2.0.0",
23
+ "@opentelemetry/api": "1.4.1",
24
+ "@opentelemetry/instrumentation": "0.36.1",
25
+ "@opentelemetry/instrumentation-dns": "0.31.2",
26
+ "@opentelemetry/instrumentation-express": "0.32.1",
27
+ "@opentelemetry/instrumentation-http": "0.36.1",
28
+ "@opentelemetry/instrumentation-winston": "0.31.1",
29
+ "@opentelemetry/sdk-node": "0.36.1",
30
+ "@opentelemetry/sdk-trace-node": "1.10.1",
31
+ "winston": "3.8.2"
32
+ },
18
33
  "devDependencies": {
19
34
  "@types/jest": "29.2.0",
20
35
  "@types/node": "18.11.8",
package/.eslintignore DELETED
@@ -1,2 +0,0 @@
1
- **/*.js
2
- node_modules
package/.eslintrc.json DELETED
@@ -1,19 +0,0 @@
1
- {
2
- "env": {
3
- "node": true
4
- },
5
- "extends": ["plugin:prettier/recommended"],
6
- "parser": "@typescript-eslint/parser",
7
- "parserOptions": {
8
- "project": ["./tsconfig.json"],
9
- "ecmaVersion": 12,
10
- "sourceType": "module"
11
- },
12
- "plugins": ["prettier", "@typescript-eslint"],
13
- "rules": {
14
- "prettier/prettier": ["error"],
15
- "default-case": "off",
16
- "@typescript-eslint/switch-exhaustiveness-check": "error"
17
- },
18
- "ignorePatterns": ["build"]
19
- }
package/.husky/pre-commit DELETED
@@ -1,5 +0,0 @@
1
- #!/bin/sh
2
- . "$(dirname "$0")/_/husky.sh"
3
-
4
- yarn build
5
- npx lint-staged
package/.nvmrc DELETED
@@ -1 +0,0 @@
1
- 18.12.0
package/.prettierignore DELETED
@@ -1 +0,0 @@
1
- dist
@@ -1,53 +0,0 @@
1
- image: node:18.12.0
2
-
3
- pipelines:
4
- pull-requests:
5
- '**':
6
- - step:
7
- name: Analyse
8
- services:
9
- - docker
10
- caches:
11
- - docker
12
- - node
13
- - sonar
14
- script:
15
- - yarn install --frozen-lockfile
16
- - yarn format:check
17
- - yarn lint:check
18
- - yarn build
19
- - yarn test --coverage --testResultsProcessor=jest-sonar-reporter --detectOpenHandles
20
- - pipe: sonarsource/sonarcloud-scan:1.4.0
21
- variables:
22
- EXTRA_ARGS: '-Dsonar.sources=src -Dsonar.tests=src -Dsonar.test.inclusions="**/*.test.js" -Dsonar.coverage.exclusions="**/node_modules/**,**/*.spec.ts" -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info'
23
- - pipe: sonarsource/sonarcloud-quality-gate:0.1.6
24
- branches:
25
- master:
26
- - step:
27
- name: "Build"
28
- caches:
29
- - node
30
- script:
31
- - yarn --frozen-lockfile
32
- - yarn build
33
- artifacts:
34
- - build/**
35
- - step:
36
- name: "Publish to NPM"
37
- trigger: manual
38
- caches:
39
- - node
40
- script:
41
- - npm version minor -m "Upgrade to %s [skip ci]"
42
- - git push && git push --tags
43
- - pipe: atlassian/npm-publish:0.3.2
44
- variables:
45
- NPM_TOKEN: $NPM_TOKEN
46
- EXTRA_ARGS: "--access public"
47
-
48
- definitions:
49
- caches:
50
- sonar: ~/.sonar/cache
51
- services:
52
- docker:
53
- memory: 2048
@@ -1,3 +0,0 @@
1
- import type { Config } from "@jest/types";
2
- declare const config: Config.InitialOptions;
3
- export default config;
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const config = {
4
- verbose: true,
5
- preset: "ts-jest",
6
- testEnvironment: "node",
7
- testPathIgnorePatterns: ["/node_modules/", "/dist/"],
8
- roots: ["<rootDir>/src"],
9
- transform: {
10
- "^.+\\.ts?$": "ts-jest",
11
- },
12
- };
13
- exports.default = config;
@@ -1 +0,0 @@
1
- export * from "./utils";
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.environmentVariable = void 0;
4
- function environmentVariable(key, fallback) {
5
- const value = process.env[key] || fallback;
6
- if (value) {
7
- return value.toString();
8
- }
9
- throw new Error(`Missing environment variable '${key}'`);
10
- }
11
- exports.environmentVariable = environmentVariable;
package/jest.config.ts DELETED
@@ -1,14 +0,0 @@
1
- import type { Config } from "@jest/types";
2
-
3
- const config: Config.InitialOptions = {
4
- verbose: true,
5
- preset: "ts-jest",
6
- testEnvironment: "node",
7
- testPathIgnorePatterns: ["/node_modules/", "/dist/"],
8
- roots: ["<rootDir>/src"],
9
- transform: {
10
- "^.+\\.ts?$": "ts-jest",
11
- },
12
- };
13
-
14
- export default config;
@@ -1,5 +0,0 @@
1
- sonar.sources=src
2
- sonar.exclusions=src/**/*.test.ts
3
- sonar.tests=src/**/*.test.ts
4
- sonar.testExecutionReportPaths=test-report.xml
5
- sonar.javascript.lcov.reportPaths=coverage/lcov.info
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./utils";
package/src/utils.test.ts DELETED
@@ -1,20 +0,0 @@
1
- import { environmentVariable } from "./utils";
2
-
3
- describe("environmentVariable", () => {
4
- test("Should throw when environment variable does not exist", async () => {
5
- process.env["COMMONS_NODEJS_TEST_EXISTING"] = "SOME_VALUE";
6
- expect(environmentVariable("COMMONS_NODEJS_TEST_EXISTING")).toBe("SOME_VALUE");
7
- });
8
-
9
- test("Use fallback value when environment variable does not exist", async () => {
10
- expect(environmentVariable("COMMONS_NODEJS_TEST_NON_EXISTING", "DEFAULT_VALUE")).toBe(
11
- "DEFAULT_VALUE"
12
- );
13
- });
14
-
15
- test("Should throw when environment variable does not exist", async () => {
16
- expect(() => environmentVariable("COMMONS_NODEJS_TEST_NON_EXISTING")).toThrow(
17
- "Missing environment variable 'COMMONS_NODEJS_TEST_NON_EXISTING'"
18
- );
19
- });
20
- });
package/src/utils.ts DELETED
@@ -1,8 +0,0 @@
1
- export function environmentVariable(key: string, fallback?: string | number): string {
2
- const value = process.env[key] || fallback;
3
- if (value) {
4
- return value.toString();
5
- }
6
-
7
- throw new Error(`Missing environment variable '${key}'`);
8
- }
package/tsconfig.json DELETED
@@ -1,24 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
-
5
- "lib": ["ES2021"],
6
- "module": "commonjs",
7
- "target": "ES2021",
8
- "outDir": "./build",
9
- "strict": true,
10
- "declaration": true,
11
- "noUnusedLocals": true,
12
- "noUnusedParameters": true,
13
- "noImplicitReturns": true,
14
- "noFallthroughCasesInSwitch": true,
15
- "noUncheckedIndexedAccess": true,
16
- "noImplicitOverride": true,
17
- "noPropertyAccessFromIndexSignature": true,
18
- "esModuleInterop": true,
19
- "skipLibCheck": true,
20
- "forceConsistentCasingInFileNames": true,
21
- // Required to load JSON files
22
- "resolveJsonModule": true
23
- }
24
- }
File without changes
File without changes