@sera4/essentia 1.0.31 → 1.0.33

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/index.js CHANGED
@@ -9,5 +9,6 @@ module.exports = {
9
9
  formatter : require("./formatter"),
10
10
  healthCheck : require("./health"),
11
11
  queue : require("./queue"),
12
- utils : require("./utils")
12
+ utils : require("./utils"),
13
+ safePRoxy : require("./safe_proxy")
13
14
  }
@@ -5,6 +5,22 @@ const colorizer = winston.format.colorize();
5
5
 
6
6
  require('winston-logstash');
7
7
 
8
+ // IETF RFC5424:
9
+ // Severity of all levels is assumed to be numerically ascending from most important to least important.
10
+ const levels = {
11
+ error: 0,
12
+ warn: 1,
13
+ info: 2,
14
+ debug: 3,
15
+ trace: 4,
16
+ }
17
+
18
+ // These colors are not used, however if colors are not defined for custom levels
19
+ // then winston will throw an exception.
20
+ winston.addColors({
21
+ trace: 'white'
22
+ })
23
+
8
24
  const devFormat = {
9
25
  transform(info) {
10
26
  const { message } = info;
@@ -17,10 +33,12 @@ const devFormat = {
17
33
  };
18
34
 
19
35
  const defaultTransport = new winston.transports.Console({
20
- format: devFormat
36
+ format: devFormat,
37
+ level: 'trace'
21
38
  });
22
39
 
23
40
  const logger = winston.createLogger({
41
+ levels: levels,
24
42
  level: 'debug',
25
43
  defaultMeta: { },
26
44
  transports: [defaultTransport]
@@ -32,9 +50,7 @@ class S4Logger {
32
50
  return;
33
51
  }
34
52
 
35
- if (options.level && ["debug", "info", "error", "warn"].indexOf(options.level.toLowerCase())) {
36
- logger.level = options.level;
37
- }
53
+ this.setLevel(options.level)
38
54
 
39
55
  logger.defaultMeta.service = options.service;
40
56
  switch(options.format.toLowerCase()) {
@@ -51,7 +67,7 @@ class S4Logger {
51
67
  }
52
68
 
53
69
  setLevel(level) {
54
- if (level && ["debug", "info", "error", "warn"].indexOf(level.toLowerCase())) {
70
+ if (level && ["trace", "debug", "info", "error", "warn"].indexOf(level.toLowerCase())) {
55
71
  logger.level = level;
56
72
  }
57
73
  }
@@ -68,6 +84,10 @@ class S4Logger {
68
84
  });
69
85
  }
70
86
 
87
+ trace(...args) {
88
+ logger.trace(util.format(...args));
89
+ }
90
+
71
91
  debug(...args) {
72
92
  logger.debug(util.format(...args));
73
93
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -27,11 +27,13 @@
27
27
  "sinon": "^5.0.1"
28
28
  },
29
29
  "dependencies": {
30
- "amqplib": "^0.5.5",
30
+ "amqplib": "^0.8.0",
31
31
  "async": "^2.6.1",
32
+ "axios": "^0.21.1",
32
33
  "fast-safe-stringify": "^2.0.6",
33
34
  "git-rev": "^0.2.1",
34
- "url-parse": "^1.4.7",
35
+ "lodash": "^4.17.21",
36
+ "url-parse": "^1.5.1",
35
37
  "uuid": "^3.3.3",
36
38
  "winston": "^3.2.1",
37
39
  "winston-logstash": "^0.4.0"
package/package.tar.gz CHANGED
Binary file
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+
3
+ const axios = require("axios"); // required only until migration is complete
4
+ const { omitBy, isNil } = require("lodash");
5
+
6
+ /* A class which safely wraps an axios connection in order to pass the current user through to
7
+ * the next service. Useful when one of our microservices requires auth as either an admin (sera4tal)
8
+ * or as the end user (example: DELETE /auth/sessions/:id
9
+ *
10
+ * each method requires a res.locals from a pre-authd session
11
+ */
12
+ class SafeProxy {
13
+ constructor() {
14
+ }
15
+
16
+ // a safe post request
17
+ // locals = res.locals - your authentication details
18
+ post(locals, url, options = { headers: {}, data: {}}) {
19
+ let { account, tenant, membership } = locals;
20
+ return this._proxyRequest({ account, tenant, membership }, url, "post", options);
21
+ }
22
+ delete(locals, url, options = { headers: {}, data: {}}) {
23
+ let { account, tenant, membership } = locals;
24
+ return this._proxyRequest({ account, tenant, membership }, url, "delete", options);
25
+ }
26
+
27
+ /* private methods */
28
+ // supports get, delete, post
29
+ // options includes { headers, data } for post / delete payloads
30
+ async _proxyRequest(locals, url, method = "get", options) {
31
+ let request;
32
+ let membership = locals.membership ? JSON.stringify(locals.membership) : null;
33
+ let headers = omitBy({ ...options.headers,
34
+ "tws-account-id": locals.account?.id,
35
+ "tws-tenant-id": locals.tenant?.id,
36
+ "tws-membership-id": locals.membership?.id,
37
+ "tws-membership": membership
38
+ }, isNil);
39
+
40
+ switch(method) {
41
+ case "post":
42
+ request = axios.post(url, { ...options.data }, { headers })
43
+ break;
44
+ case "delete":
45
+ request = axios.delete(url, { ...options.data, headers } )
46
+ break;
47
+ case "get":
48
+ request = axios.get(url, { headers, data } )
49
+ break;
50
+ default:
51
+ throw new Error("uknown type called in proxyRequest");
52
+ }
53
+
54
+ return request;
55
+ }
56
+
57
+ }
58
+
59
+ module.exports = new SafeProxy();