@autofleet/outbreak 0.1.1 → 0.2.0-beta-1
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/{config.d.ts → src/http-wrapper/config.d.ts} +0 -0
- package/dist/{config.js → src/http-wrapper/config.js} +0 -0
- package/dist/src/http-wrapper/config.js.map +1 -0
- package/dist/{index.d.ts → src/http-wrapper/index.d.ts} +0 -3
- package/dist/{index.js → src/http-wrapper/index.js} +6 -9
- package/dist/src/http-wrapper/index.js.map +1 -0
- package/dist/{http_wrapper.d.ts → src/http-wrapper/wrapper.d.ts} +0 -0
- package/dist/{http_wrapper.js → src/http-wrapper/wrapper.js} +9 -16
- package/dist/src/http-wrapper/wrapper.js.map +1 -0
- package/dist/src/identity/ApiUser.d.ts +25 -0
- package/dist/src/identity/ApiUser.js +73 -0
- package/dist/src/identity/ApiUser.js.map +1 -0
- package/dist/src/identity/index.d.ts +3 -0
- package/dist/src/identity/index.js +13 -0
- package/dist/src/identity/index.js.map +1 -0
- package/dist/src/identity/middlewares.d.ts +12 -0
- package/dist/src/identity/middlewares.js +69 -0
- package/dist/src/identity/middlewares.js.map +1 -0
- package/dist/src/identity/utils.d.ts +5 -0
- package/dist/src/identity/utils.js +79 -0
- package/dist/src/identity/utils.js.map +1 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +17 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/network/index.d.ts +1 -0
- package/dist/src/network/index.js +17 -0
- package/dist/src/network/index.js.map +1 -0
- package/dist/src/tracer/index.d.ts +16 -0
- package/dist/src/tracer/index.js +48 -0
- package/dist/src/tracer/index.js.map +1 -0
- package/dist/{example.d.ts → tests/http-wrapper.test.d.ts} +0 -0
- package/dist/tests/http-wrapper.test.js +76 -0
- package/dist/tests/http-wrapper.test.js.map +1 -0
- package/examples/example.ts +39 -0
- package/package.json +7 -4
- package/tests/{api.test.ts → http-wrapper.test.ts} +21 -20
- package/tsconfig.json +4 -1
- package/dist/config.js.map +0 -1
- package/dist/example.js +0 -46
- package/dist/example.js.map +0 -1
- package/dist/http_wrapper.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/tracer.d.ts +0 -10
- package/dist/tracer.js +0 -55
- package/dist/tracer.js.map +0 -1
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../../src/http-wrapper/config.ts"],"names":[],"mappings":";;AAAA,MAAM,mBAAmB,GAAG,YAAY,CAAC;AAEzC,MAAM,kBAAkB,GAAG,CAAC,OAAO,EAAQ,EAAE;IAC3C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QAC3B,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;KAChD;IAED,iEAAiE;IACjE,MAAM,eAAe,GAAG,iBAAiB,CAAC;IAE1C,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAEhG,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE;QAC7B,MAAM,IAAI,KAAK,CAAC,yCAAyC,cAAc,EAAE,CAAC,CAAC;KAC5E;AACH,CAAC,CAAC;AASF,MAAM,IAAI,GAAG,CAAC,YAAuB,EAAE,EAAE,EAAE;IACzC,MAAM,EACJ,4BAA4B,GAAG,IAAI,EACnC,oBAAoB,GAAG,KAAK,EAC5B,kBAAkB,GAAG;QACnB,cAAc;QACd,cAAc;KACf,EACD,aAAa,GAAG,OAAO,GACxB,GAAG,SAAS,CAAC;IAEd,kBAAkB,CAAC,kBAAkB,CAAC,CAAC;IAEvC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;IAE5C,IAAI,eAAe,GAAG,gBAAgB,CAAC;IACvC,IAAI,4BAA4B,KAAK,IAAI,EAAE;QACzC,eAAe,GAAG,CAAC,mBAAmB,CAAC,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;KAClE;IAED,OAAO;QACL,4BAA4B;QAC5B,oBAAoB;QACpB,mBAAmB;QACnB,gBAAgB;QAChB,eAAe;QACf,aAAa;KACd,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe;IACb,IAAI;IACJ,mBAAmB;CACpB,CAAC"}
|
@@ -1,14 +1,11 @@
|
|
1
1
|
import winston from 'winston';
|
2
2
|
interface Options {
|
3
3
|
winstonLogger?: winston.Logger;
|
4
|
-
loggerTraceKey?: string;
|
5
4
|
headersPrefix?: string;
|
6
5
|
setAndPropagateCorrelationId?: boolean;
|
7
6
|
headersToPropagate?: string[];
|
8
7
|
}
|
9
8
|
export default function (options: Options): void;
|
10
|
-
export declare const getCurrentContext: () => any;
|
11
|
-
export declare const newTrace: (type: any) => void;
|
12
9
|
export declare const traceTypes: {
|
13
10
|
HTTP_REQUEST: string;
|
14
11
|
WEB_SOCKET: string;
|
@@ -11,29 +11,26 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
11
11
|
};
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
13
13
|
const winston_1 = require("winston");
|
14
|
-
const
|
14
|
+
const wrapper_1 = __importDefault(require("./wrapper"));
|
15
15
|
const config_1 = __importDefault(require("./config"));
|
16
|
-
const tracer = __importStar(require("
|
17
|
-
const addMetadataToLog =
|
16
|
+
const tracer = __importStar(require("../tracer"));
|
17
|
+
const addMetadataToLog = winston_1.format((info) => {
|
18
18
|
const currentTrace = tracer.getCurrentTrace();
|
19
19
|
if (currentTrace && currentTrace.context && currentTrace.context.get) {
|
20
20
|
// eslint-disable-next-line no-param-reassign
|
21
|
-
info
|
21
|
+
info.traceId = currentTrace.context.get(config_1.default.correlationIdHeader);
|
22
22
|
}
|
23
23
|
return info;
|
24
24
|
});
|
25
25
|
function default_1(options) {
|
26
|
-
|
26
|
+
wrapper_1.default(config_1.default.load(options));
|
27
27
|
tracer.enable();
|
28
28
|
if (options.winstonLogger) {
|
29
29
|
// eslint-disable-next-line no-param-reassign
|
30
|
-
|
31
|
-
options.winstonLogger.format = winston_1.format.combine(addMetadataToLog(traceKey)(), options.winstonLogger.format);
|
30
|
+
options.winstonLogger.format = winston_1.format.combine(addMetadataToLog(), options.winstonLogger.format);
|
32
31
|
}
|
33
32
|
}
|
34
33
|
exports.default = default_1;
|
35
|
-
exports.getCurrentContext = () => tracer.getCurrentTrace();
|
36
|
-
exports.newTrace = tracer.newTrace;
|
37
34
|
exports.traceTypes = {
|
38
35
|
HTTP_REQUEST: 'httpRequest',
|
39
36
|
WEB_SOCKET: 'webSocket',
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/http-wrapper/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAA0C;AAE1C,wDAAoC;AACpC,sDAA8B;AAC9B,kDAAoC;AASpC,MAAM,gBAAgB,GAAG,gBAAM,CAAC,CAAC,IAAI,EAAE,EAAE;IACvC,MAAM,YAAY,GAAG,MAAM,CAAC,eAAe,EAAE,CAAC;IAC9C,IAAI,YAAY,IAAI,YAAY,CAAC,OAAO,IAAI,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE;QACpE,6CAA6C;QAC7C,IAAI,CAAC,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,gBAAM,CAAC,mBAAmB,CAAC,CAAC;KACrE;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC,CAAC;AAEH,mBAAyB,OAAgB;IACvC,iBAAW,CAAC,gBAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAClC,MAAM,CAAC,MAAM,EAAE,CAAC;IAEhB,IAAI,OAAO,CAAC,aAAa,EAAE;QACzB,6CAA6C;QAC7C,OAAO,CAAC,aAAa,CAAC,MAAM,GAAG,gBAAM,CAAC,OAAO,CAC3C,gBAAgB,EAAE,EAClB,OAAO,CAAC,aAAa,CAAC,MAAM,CAC7B,CAAC;KACH;AACH,CAAC;AAXD,4BAWC;AAEY,QAAA,UAAU,GAAG;IACxB,YAAY,EAAE,aAAa;IAC3B,UAAU,EAAE,WAAW;IACvB,MAAM,EAAE,QAAQ;CACjB,CAAC"}
|
File without changes
|
@@ -2,18 +2,11 @@
|
|
2
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
6
|
-
if (mod && mod.__esModule) return mod;
|
7
|
-
var result = {};
|
8
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
9
|
-
result["default"] = mod;
|
10
|
-
return result;
|
11
|
-
};
|
12
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
13
6
|
/* eslint-disable max-len */
|
14
7
|
const http_1 = __importDefault(require("http"));
|
15
8
|
const shortid_1 = __importDefault(require("shortid"));
|
16
|
-
const tracer_1 =
|
9
|
+
const tracer_1 = require("../tracer");
|
17
10
|
const originalHttpCreateServer = http_1.default.createServer;
|
18
11
|
const originalRequest = http_1.default.request;
|
19
12
|
const originalGet = http_1.default.get;
|
@@ -22,7 +15,7 @@ function setAndCollectCorrelationId(config, req, res) {
|
|
22
15
|
if (typeof correlationId === 'undefined') {
|
23
16
|
correlationId = shortid_1.default.generate();
|
24
17
|
}
|
25
|
-
tracer_1.
|
18
|
+
tracer_1.getCurrentTrace().context.set(config.correlationIdHeader, correlationId);
|
26
19
|
res.setHeader(config.correlationIdHeader, correlationId);
|
27
20
|
}
|
28
21
|
function collect(req, headers, headersPrefix) {
|
@@ -31,14 +24,14 @@ function collect(req, headers, headersPrefix) {
|
|
31
24
|
: [];
|
32
25
|
[...headers, ...prefixHeaders].forEach((header) => {
|
33
26
|
if (typeof req.headers[header] !== 'undefined') {
|
34
|
-
tracer_1.
|
27
|
+
tracer_1.getCurrentTrace().context.set(header, req.headers[header]);
|
35
28
|
}
|
36
29
|
});
|
37
30
|
}
|
38
31
|
function injectInResponse(response, headers) {
|
39
|
-
if (tracer_1.
|
32
|
+
if (tracer_1.getCurrentTrace()) {
|
40
33
|
headers.forEach((header) => {
|
41
|
-
response.setHeader(header, tracer_1.
|
34
|
+
response.setHeader(header, tracer_1.getCurrentTrace().context.get(header));
|
42
35
|
});
|
43
36
|
}
|
44
37
|
}
|
@@ -62,14 +55,14 @@ function wrapHttpCreateServer(config) {
|
|
62
55
|
};
|
63
56
|
}
|
64
57
|
function inject(options) {
|
65
|
-
if (tracer_1.
|
58
|
+
if (tracer_1.getCurrentTrace()) {
|
66
59
|
if (!options.headers) {
|
67
60
|
// eslint-disable-next-line no-param-reassign
|
68
61
|
options.headers = {};
|
69
62
|
}
|
70
|
-
[...(tracer_1.
|
63
|
+
[...(tracer_1.getCurrentTrace().context.keys())].forEach((header) => {
|
71
64
|
// eslint-disable-next-line no-param-reassign
|
72
|
-
options.headers[header] = tracer_1.
|
65
|
+
options.headers[header] = tracer_1.getCurrentTrace().context.get(header);
|
73
66
|
});
|
74
67
|
}
|
75
68
|
}
|
@@ -102,4 +95,4 @@ function wrapHttp(config) {
|
|
102
95
|
http_1.default.get = wrapHttpRequest(originalGet);
|
103
96
|
}
|
104
97
|
exports.default = wrapHttp;
|
105
|
-
//# sourceMappingURL=
|
98
|
+
//# sourceMappingURL=wrapper.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"wrapper.js","sourceRoot":"","sources":["../../../src/http-wrapper/wrapper.ts"],"names":[],"mappings":";;;;;AAAA,4BAA4B;AAC5B,gDAAwB;AACxB,sDAA8B;AAG9B,sCAAsD;AAEtD,MAAM,wBAAwB,GAAG,cAAI,CAAC,YAAY,CAAC;AACnD,MAAM,eAAe,GAAG,cAAI,CAAC,OAAO,CAAC;AACrC,MAAM,WAAW,GAAG,cAAI,CAAC,GAAG,CAAC;AAE7B,SAAS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Models: EntityPermissions;
|
6
|
+
fleets: EntityPermissions;
|
7
|
+
demandSources: EntityPermissions;
|
8
|
+
createdAt?: string;
|
9
|
+
}
|
10
|
+
export default class ApiUser {
|
11
|
+
id: string | undefined;
|
12
|
+
privatePermissions: UserPayload | undefined;
|
13
|
+
privatePermissionsLegacy: any;
|
14
|
+
emptyUser: boolean;
|
15
|
+
constructor(id?: string);
|
16
|
+
getUserPermissions(): Promise<UserPayload>;
|
17
|
+
get businessModels(): string[] | undefined;
|
18
|
+
get fleets(): string[] | undefined;
|
19
|
+
get demandSources(): string[] | undefined;
|
20
|
+
getUserProperty(key: any): string[] | undefined;
|
21
|
+
get permissions(): UserPayload | undefined;
|
22
|
+
getUserPermissionsLegacy(): Promise<any>;
|
23
|
+
get permissionsLegacy(): any;
|
24
|
+
}
|
25
|
+
export {};
|
@@ -0,0 +1,73 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
const network_1 = require("../network");
|
13
|
+
class ApiUser {
|
14
|
+
constructor(id) {
|
15
|
+
this.id = id;
|
16
|
+
this.emptyUser = !!id;
|
17
|
+
}
|
18
|
+
getUserPermissions() {
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
20
|
+
if (!this.id) {
|
21
|
+
return null;
|
22
|
+
}
|
23
|
+
if (this.privatePermissions) {
|
24
|
+
return this.privatePermissions;
|
25
|
+
}
|
26
|
+
const { data } = yield network_1.IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload`);
|
27
|
+
this.privatePermissions = data;
|
28
|
+
return this.privatePermissions;
|
29
|
+
});
|
30
|
+
}
|
31
|
+
get businessModels() {
|
32
|
+
return this.getUserProperty('businessModels');
|
33
|
+
}
|
34
|
+
get fleets() {
|
35
|
+
return this.getUserProperty('fleets');
|
36
|
+
}
|
37
|
+
get demandSources() {
|
38
|
+
return this.getUserProperty('demandSources');
|
39
|
+
}
|
40
|
+
getUserProperty(key) {
|
41
|
+
if (!this.privatePermissions) {
|
42
|
+
throw new Error(`Cannot get ${key} without calling (async) getUserPermissions before`);
|
43
|
+
}
|
44
|
+
return Object.keys(this.privatePermissions[key] || {});
|
45
|
+
}
|
46
|
+
get permissions() {
|
47
|
+
if (!this.privatePermissions) {
|
48
|
+
throw new Error('Cannot get permissions without calling (async) getUserPermissions before');
|
49
|
+
}
|
50
|
+
return this.privatePermissions;
|
51
|
+
}
|
52
|
+
getUserPermissionsLegacy() {
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
54
|
+
if (!this.id) {
|
55
|
+
return null;
|
56
|
+
}
|
57
|
+
if (this.privatePermissionsLegacy) {
|
58
|
+
return this.privatePermissionsLegacy;
|
59
|
+
}
|
60
|
+
const { data } = yield network_1.IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload-legacy`);
|
61
|
+
this.privatePermissionsLegacy = data;
|
62
|
+
return this.privatePermissionsLegacy;
|
63
|
+
});
|
64
|
+
}
|
65
|
+
get permissionsLegacy() {
|
66
|
+
if (!this.privatePermissionsLegacy) {
|
67
|
+
throw new Error('Cannot get permissionsLegacy without calling (async) getUserPermissionsLegacy before');
|
68
|
+
}
|
69
|
+
return this.privatePermissionsLegacy;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
exports.default = ApiUser;
|
73
|
+
//# sourceMappingURL=ApiUser.js.map
|
@@ -0,0 +1 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,CAAC,0EAA0E,CAAC,CAAC;SAC7F;QAED,OAAO,IAAI,CAAC,kBAAkB,CAAC;IACjC,CAAC;IAEK,wBAAwB;;YAC5B,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE;gBACZ,OAAO,IAAI,CAAC;aACb;YAED,IAAI,IAAI,CAAC,wBAAwB,EAAE;gBACjC,OAAO,IAAI,CAAC,wBAAwB,CAAC;aACtC;YAED,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,yBAAe,CAAC,GAAG,CAAC,iBAAiB,IAAI,CAAC,EAAE,+BAA+B,CAAC,CAAC;YAEpG,IAAI,CAAC,wBAAwB,GAAG,IAAI,CAAC;YACrC,OAAO,IAAI,CAAC,wBAAwB,CAAC;QACvC,CAAC;KAAA;IAED,IAAI,iBAAiB;QACnB,IAAI,CAAC,IAAI,CAAC,wBAAwB,EAAE;YAClC,MAAM,IAAI,KAAK,CAAC,sFAAsF,CAAC,CAAC;SACzG;QACD,OAAO,IAAI,CAAC,wBAAwB,CAAC;IACvC,CAAC;CACF;AA7ED,0BA6EC"}
|
@@ -0,0 +1,13 @@
|
|
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 ApiUser_1 = __importDefault(require("./ApiUser"));
|
7
|
+
exports.ApiUser = ApiUser_1.default;
|
8
|
+
const middlewares_1 = require("./middlewares");
|
9
|
+
exports.middleware = middlewares_1.middleware;
|
10
|
+
exports.middlewareWithDecode = middlewares_1.middlewareWithDecode;
|
11
|
+
exports.eagerLoadPermissionsMiddleware = middlewares_1.eagerLoadPermissionsMiddleware;
|
12
|
+
exports.getDecodedBearer = middlewares_1.getDecodedBearer;
|
13
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/identity/index.ts"],"names":[],"mappings":";;;;;AAAA,wDAAgC;AAS9B,kBATK,iBAAO,CASL;AART,+CAKuB;AAIrB,qBARA,wBAAU,CAQA;AACV,+BARA,kCAAoB,CAQA;AACpB,yCARA,4CAA8B,CAQA;AAC9B,2BARA,8BAAgB,CAQA"}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import ApiUser from './ApiUser';
|
2
|
+
export declare const middleware: (options?: {
|
3
|
+
eagerLoadUserPermissions?: boolean;
|
4
|
+
eagerLoadUserPermissionsLegacy?: boolean;
|
5
|
+
}) => (req: any, res: any, next: any) => Promise<void>;
|
6
|
+
export declare const middlewareWithDecode: (options?: {
|
7
|
+
eagerLoadUserPermissions?: boolean;
|
8
|
+
eagerLoadUserPermissionsLegacy?: boolean;
|
9
|
+
}) => (req: any, res: any, next: any) => Promise<void>;
|
10
|
+
export declare const eagerLoadPermissionsMiddleware: (req: any, res: any, next: any) => Promise<any>;
|
11
|
+
export declare const getDecodedBearer: (req: any) => any;
|
12
|
+
export default ApiUser;
|
@@ -0,0 +1,69 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
const ApiUser_1 = __importDefault(require("./ApiUser"));
|
16
|
+
const tracer_1 = require("../tracer");
|
17
|
+
const utils_1 = require("./utils");
|
18
|
+
exports.middleware = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
19
|
+
const userId = req.headers['x-af-user-id'];
|
20
|
+
const trace = tracer_1.getCurrentTrace();
|
21
|
+
if (!userId) {
|
22
|
+
trace.context.set('userObject', {});
|
23
|
+
return next();
|
24
|
+
}
|
25
|
+
const userObject = new ApiUser_1.default(userId);
|
26
|
+
if (options.eagerLoadUserPermissions) {
|
27
|
+
yield userObject.getUserPermissions();
|
28
|
+
}
|
29
|
+
if (options.eagerLoadUserPermissionsLegacy) {
|
30
|
+
yield userObject.getUserPermissionsLegacy();
|
31
|
+
}
|
32
|
+
req.user = userObject;
|
33
|
+
trace.context.set('userObject', userObject);
|
34
|
+
return next();
|
35
|
+
});
|
36
|
+
exports.middlewareWithDecode = (options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
37
|
+
var _a, _b;
|
38
|
+
const trace = tracer_1.getCurrentTrace();
|
39
|
+
if (req.headers.authorization) {
|
40
|
+
const decoded = utils_1.decodeBearer(req.headers.authorization);
|
41
|
+
const userId = (_b = (_a = decoded) === null || _a === void 0 ? void 0 : _a.user) === null || _b === void 0 ? void 0 : _b.id;
|
42
|
+
if (userId) {
|
43
|
+
req.headers['X-AF-USER-ID'] = userId;
|
44
|
+
}
|
45
|
+
const userObject = new ApiUser_1.default(userId);
|
46
|
+
if (options.eagerLoadUserPermissions) {
|
47
|
+
yield userObject.getUserPermissions();
|
48
|
+
}
|
49
|
+
if (options.eagerLoadUserPermissionsLegacy) {
|
50
|
+
yield userObject.getUserPermissionsLegacy();
|
51
|
+
}
|
52
|
+
req.user = userObject;
|
53
|
+
trace.context.set('userObject', userObject);
|
54
|
+
}
|
55
|
+
trace.context.set('userObject', {});
|
56
|
+
return next();
|
57
|
+
});
|
58
|
+
exports.eagerLoadPermissionsMiddleware = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
59
|
+
yield req.user.getUserPermissions();
|
60
|
+
return next();
|
61
|
+
});
|
62
|
+
exports.getDecodedBearer = (req) => {
|
63
|
+
if (req.headers.authorization) {
|
64
|
+
return utils_1.decodeBearer(req.headers.authorization);
|
65
|
+
}
|
66
|
+
return null;
|
67
|
+
};
|
68
|
+
exports.default = ApiUser_1.default;
|
69
|
+
//# sourceMappingURL=middlewares.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"middlewares.js","sourceRoot":"","sources":["../../../src/identity/middlewares.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,wDAAgC;AAChC,sCAA4C;AAC5C,mCAAuC;AAE1B,QAAA,UAAU,GAAG,CAAC,UAGvB,EAAE,EAAE,EAAE,CAAC,CAAO,GAAG,EAAE,GAAG,EAAE,IAAI,EAAiB,EAAE;IACjD,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;IAC3C,MAAM,KAAK,GAAG,wBAAe,EAAE,CAAC;IAChC,IAAI,CAAC,MAAM,EAAE;QACX,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;QACpC,OAAO,IAAI,EAAE,CAAC;KACf;IAED,MAAM,UAAU,GAAG,IAAI,iBAAO,CAAC,MAAM,CAAC,CAAC;IAEvC,IAAI,OAAO,CAAC,wBAAwB,EAAE;QACpC,MAAM,UAAU,CAAC,kBAAkB,EAAE,CAAC;KACvC;IAED,IAAI,OAAO,CAAC,8BAA8B,EAAE;QAC1C,MAAM,UAAU,CAAC,wBAAwB,EAAE,CAAC;KAC7C;IAED,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC;IACtB,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;IAE5C,OAAO,IAAI,EAAE,CAAC;AAChB,CAAC,CAAA,CAAC;AAEW,QAAA,oBAAoB,GAAG,CAAC,UAGjC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,CAAC;KAChD;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF,kBAAe,iBAAO,CAAC"}
|
@@ -0,0 +1,5 @@
|
|
1
|
+
export declare const getAuthFromBearer: (bearer: string) => string;
|
2
|
+
export declare const decodeBearer: (bearer: string) => any;
|
3
|
+
export declare const parsePermissions: (contextId: any, decodedToken: any) => any;
|
4
|
+
export declare const getEntitiesFromContext: (contextId: string, decodedToken: any) => any;
|
5
|
+
export declare const getContextAttributes: (contextId: string, decodedToken: any) => any;
|
@@ -0,0 +1,79 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
3
|
+
if (mod && mod.__esModule) return mod;
|
4
|
+
var result = {};
|
5
|
+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
6
|
+
result["default"] = mod;
|
7
|
+
return result;
|
8
|
+
};
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
10
|
+
/* eslint-disable prefer-destructuring */
|
11
|
+
const jwt = __importStar(require("jsonwebtoken"));
|
12
|
+
const CONTEXT_PROPS = ['fleetId', 'businessModelId', 'demandSourceId'];
|
13
|
+
const CONTEXT_MAP_PROPS = {
|
14
|
+
fleet: 'fleets',
|
15
|
+
business: 'businessModels',
|
16
|
+
demand: 'demandSources',
|
17
|
+
};
|
18
|
+
exports.getAuthFromBearer = (bearer) => bearer.replace('Bearer ', '');
|
19
|
+
exports.decodeBearer = (bearer) => {
|
20
|
+
const token = bearer.replace('Bearer ', '');
|
21
|
+
const decoded = jwt.decode(token);
|
22
|
+
return decoded;
|
23
|
+
};
|
24
|
+
exports.parsePermissions = (contextId, decodedToken) => {
|
25
|
+
var _a;
|
26
|
+
if (!decodedToken) {
|
27
|
+
return [];
|
28
|
+
}
|
29
|
+
const contexts = decodedToken.contexts;
|
30
|
+
const activeContext = contexts.find((context) => context.id === contextId);
|
31
|
+
const permissionsByContext = {};
|
32
|
+
const permissionsValue = `${(_a = activeContext.permissions) === null || _a === void 0 ? void 0 : _a.map((cp) => `${cp},`)}`;
|
33
|
+
return {
|
34
|
+
key: activeContext.entityId,
|
35
|
+
value: permissionsValue,
|
36
|
+
};
|
37
|
+
};
|
38
|
+
exports.getEntitiesFromContext = (contextId, decodedToken) => {
|
39
|
+
if (!decodedToken) {
|
40
|
+
return [];
|
41
|
+
}
|
42
|
+
let contexts = decodedToken.contexts;
|
43
|
+
if (contextId) {
|
44
|
+
contexts = contexts.filter((context) => context.id === contextId);
|
45
|
+
}
|
46
|
+
const attributes = {};
|
47
|
+
contexts.forEach((context) => {
|
48
|
+
const prop = CONTEXT_MAP_PROPS[context.subSystem || 'business'];
|
49
|
+
const permissions = exports.parsePermissions(context.id, decodedToken);
|
50
|
+
// eslint-disable-next-line no-unused-expressions
|
51
|
+
attributes[prop]
|
52
|
+
? attributes[prop][permissions.key] = permissions.value
|
53
|
+
: attributes[prop] = { [permissions.key]: permissions.value };
|
54
|
+
});
|
55
|
+
return attributes;
|
56
|
+
};
|
57
|
+
exports.getContextAttributes = (contextId, decodedToken) => {
|
58
|
+
if (!decodedToken) {
|
59
|
+
return [];
|
60
|
+
}
|
61
|
+
let contexts = decodedToken.contexts;
|
62
|
+
if (contextId) {
|
63
|
+
contexts = contexts.filter((context) => context.id === contextId);
|
64
|
+
}
|
65
|
+
const attributes = {};
|
66
|
+
contexts.forEach((context) => {
|
67
|
+
CONTEXT_PROPS.forEach((prop) => {
|
68
|
+
if (context[prop]) {
|
69
|
+
const contextPropWrapped = [context[prop]];
|
70
|
+
// eslint-disable-next-line no-unused-expressions
|
71
|
+
attributes[prop]
|
72
|
+
? attributes[prop] = attributes[prop].concat(contextPropWrapped)
|
73
|
+
: attributes[prop] = contextPropWrapped;
|
74
|
+
}
|
75
|
+
});
|
76
|
+
});
|
77
|
+
return attributes;
|
78
|
+
};
|
79
|
+
//# sourceMappingURL=utils.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"utils.js","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,CAAC;IACtB,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC3B,aAAa,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YAC7B,IAAI,OAAO,CAAC,IAAI,CAAC,EAAE;gBACjB,MAAM,kBAAkB,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;gBAC3C,iDAAiD;gBACjD,UAAU,CAAC,IAAI,CAAC;oBACd,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,kBAAkB,CAAC;oBAChE,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,kBAAkB,CAAC;aAC3C;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IACH,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC"}
|
@@ -0,0 +1,4 @@
|
|
1
|
+
import headersTracer from './http-wrapper';
|
2
|
+
import { newTrace, getCurrentTrace } from './tracer';
|
3
|
+
import { ApiUser, middleware, middlewareWithDecode, eagerLoadPermissionsMiddleware, getDecodedBearer } from './identity';
|
4
|
+
export { headersTracer, getCurrentTrace, newTrace, ApiUser, middleware, middlewareWithDecode, eagerLoadPermissionsMiddleware, getDecodedBearer, };
|
@@ -0,0 +1,17 @@
|
|
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 http_wrapper_1 = __importDefault(require("./http-wrapper"));
|
7
|
+
exports.headersTracer = http_wrapper_1.default;
|
8
|
+
const tracer_1 = require("./tracer");
|
9
|
+
exports.newTrace = tracer_1.newTrace;
|
10
|
+
exports.getCurrentTrace = tracer_1.getCurrentTrace;
|
11
|
+
const identity_1 = require("./identity");
|
12
|
+
exports.ApiUser = identity_1.ApiUser;
|
13
|
+
exports.middleware = identity_1.middleware;
|
14
|
+
exports.middlewareWithDecode = identity_1.middlewareWithDecode;
|
15
|
+
exports.eagerLoadPermissionsMiddleware = identity_1.eagerLoadPermissionsMiddleware;
|
16
|
+
exports.getDecodedBearer = identity_1.getDecodedBearer;
|
17
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,kEAA2C;AAWzC,wBAXK,sBAAa,CAWL;AAVf,qCAAqD;AAYnD,mBAZO,iBAAQ,CAYP;AADR,0BAXiB,wBAAe,CAWjB;AAVjB,yCAMoB;AAMlB,kBAXA,kBAAO,CAWA;AACP,qBAXA,qBAAU,CAWA;AACV,+BAXA,+BAAoB,CAWA;AACpB,yCAXA,yCAA8B,CAWA;AAC9B,2BAXA,2BAAgB,CAWA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const IdentityNetwork: any;
|
@@ -0,0 +1,17 @@
|
|
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
|
+
/* eslint-disable import/prefer-default-export */
|
7
|
+
const network_1 = __importDefault(require("@autofleet/network"));
|
8
|
+
const CACHE_LIFETIME_IN_SEC = 10;
|
9
|
+
exports.IdentityNetwork = new network_1.default({
|
10
|
+
serviceName: 'IDENTITY_MS',
|
11
|
+
retries: 3,
|
12
|
+
retryCondition: () => true,
|
13
|
+
cache: process.env.NODE_ENV !== 'test' ? {
|
14
|
+
maxAge: CACHE_LIFETIME_IN_SEC * 1000,
|
15
|
+
} : undefined,
|
16
|
+
});
|
17
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/network/index.ts"],"names":[],"mappings":";;;;;AAAA,iDAAiD;AACjD,iEAAyC;AAEzC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAEpB,QAAA,eAAe,GAAG,IAAI,iBAAO,CAAC;IACzC,WAAW,EAAE,aAAa;IAC1B,OAAO,EAAE,CAAC;IACV,cAAc,EAAE,GAAG,EAAE,CAAC,IAAI;IAC1B,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,CAAC,CAAC,CAAC;QACvC,MAAM,EAAE,qBAAqB,GAAG,IAAI;KACrC,CAAC,CAAC,CAAC,SAAS;CACd,CAAC,CAAC"}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import asyncHooks from 'async_hooks';
|
3
|
+
declare class Trace {
|
4
|
+
id: string;
|
5
|
+
type: string;
|
6
|
+
context: Map<string, any>;
|
7
|
+
hooks: {
|
8
|
+
[key: string]: Function[];
|
9
|
+
};
|
10
|
+
constructor(type: any);
|
11
|
+
}
|
12
|
+
declare const executionContextMap: Map<number, Trace>;
|
13
|
+
export declare const newTrace: (type: any) => Trace;
|
14
|
+
export declare const enable: () => asyncHooks.AsyncHook;
|
15
|
+
export declare const getCurrentTrace: () => Trace;
|
16
|
+
export default executionContextMap;
|
@@ -0,0 +1,48 @@
|
|
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 async_hooks_1 = __importDefault(require("async_hooks"));
|
7
|
+
const uuid_1 = __importDefault(require("uuid"));
|
8
|
+
class Trace {
|
9
|
+
constructor(type) {
|
10
|
+
this.id = uuid_1.default.v1();
|
11
|
+
this.type = type;
|
12
|
+
this.context = new Map();
|
13
|
+
this.hooks = {
|
14
|
+
onDestroy: [],
|
15
|
+
};
|
16
|
+
}
|
17
|
+
}
|
18
|
+
const executionContextMap = new Map();
|
19
|
+
function init(asyncId, type, triggerAsyncId) {
|
20
|
+
if (executionContextMap.has(triggerAsyncId)) {
|
21
|
+
executionContextMap.set(asyncId, executionContextMap.get(triggerAsyncId));
|
22
|
+
}
|
23
|
+
}
|
24
|
+
function destroy(asyncId) {
|
25
|
+
const ecValue = executionContextMap.get(asyncId);
|
26
|
+
if (ecValue) {
|
27
|
+
ecValue.hooks.onDestroy.map((cb) => cb(asyncId, ecValue));
|
28
|
+
}
|
29
|
+
executionContextMap.delete(asyncId);
|
30
|
+
}
|
31
|
+
const hook = async_hooks_1.default.createHook({
|
32
|
+
init, destroy,
|
33
|
+
});
|
34
|
+
exports.newTrace = (type) => {
|
35
|
+
const trace = new Trace(type);
|
36
|
+
executionContextMap.set(async_hooks_1.default.executionAsyncId(), trace);
|
37
|
+
return trace;
|
38
|
+
};
|
39
|
+
exports.enable = () => hook.enable();
|
40
|
+
exports.getCurrentTrace = () => {
|
41
|
+
const currentContext = executionContextMap.get(async_hooks_1.default.executionAsyncId());
|
42
|
+
if (!currentContext) {
|
43
|
+
return exports.newTrace('initialContext');
|
44
|
+
}
|
45
|
+
return currentContext;
|
46
|
+
};
|
47
|
+
exports.default = executionContextMap;
|
48
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/tracer/index.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AACrC,gDAAwB;AAExB,MAAM,KAAK;IAST,YAAY,IAAI;QACd,IAAI,CAAC,EAAE,GAAG,cAAI,CAAC,EAAE,EAAE,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG;YACX,SAAS,EAAE,EAAE;SACd,CAAA;IACH,CAAC;CACF;AAED,MAAM,mBAAmB,GAAuB,IAAI,GAAG,EAAE,CAAC;AAE1D,SAAS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-0,0 +1,76 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
const express_1 = __importDefault(require("express"));
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
17
|
+
const winston_1 = __importDefault(require("winston"));
|
18
|
+
const http_wrapper_1 = __importDefault(require("../src/http-wrapper"));
|
19
|
+
const index_1 = require("../src/index");
|
20
|
+
console.log(index_1.headersTracer.toString(), index_1.middlewareWithDecode.toString());
|
21
|
+
const logger = winston_1.default.createLogger({
|
22
|
+
transports: [
|
23
|
+
new winston_1.default.transports.Console(),
|
24
|
+
],
|
25
|
+
});
|
26
|
+
const generateApp = (addEndpoints, port) => __awaiter(void 0, void 0, void 0, function* () {
|
27
|
+
const app = express_1.default();
|
28
|
+
addEndpoints(app);
|
29
|
+
const server = yield new Promise((resolve) => {
|
30
|
+
const s = app.listen(port, () => {
|
31
|
+
console.log('Listen on port', port);
|
32
|
+
resolve(s);
|
33
|
+
});
|
34
|
+
});
|
35
|
+
return () => server.close();
|
36
|
+
});
|
37
|
+
describe('E2E', () => {
|
38
|
+
it('Basic functionality', () => __awaiter(void 0, void 0, void 0, function* () {
|
39
|
+
let server2TraceId = null;
|
40
|
+
http_wrapper_1.default({
|
41
|
+
setAndPropagateCorrelationId: true,
|
42
|
+
headersToPropagate: [
|
43
|
+
'x-test-header',
|
44
|
+
],
|
45
|
+
headersPrefix: 'x-wakanda-',
|
46
|
+
winstonLogger: logger,
|
47
|
+
});
|
48
|
+
const closeServer1 = yield generateApp((app) => {
|
49
|
+
app.get('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
50
|
+
const { data: res1 } = yield axios_1.default.post(`http://localhost:8082`);
|
51
|
+
res.json(res1);
|
52
|
+
}));
|
53
|
+
}, 8081);
|
54
|
+
const closeServer2 = yield generateApp((app) => {
|
55
|
+
app.post('/', (req, res) => {
|
56
|
+
server2TraceId = req.headers['x-trace-id'];
|
57
|
+
res.json({
|
58
|
+
value: req.headers['x-test-header'],
|
59
|
+
wkanda: req.headers['x-wakanda-id']
|
60
|
+
});
|
61
|
+
});
|
62
|
+
}, 8082);
|
63
|
+
const { data: res1, headers } = yield axios_1.default.get(`http://localhost:8081`, {
|
64
|
+
headers: {
|
65
|
+
'x-test-header': 'testHeader',
|
66
|
+
'x-wakanda-id': 'my-wakanda-id'
|
67
|
+
}
|
68
|
+
});
|
69
|
+
closeServer1();
|
70
|
+
closeServer2();
|
71
|
+
expect(headers['x-trace-id']).toEqual(server2TraceId);
|
72
|
+
expect(res1.value).toEqual('testHeader');
|
73
|
+
expect(res1.wkanda).toEqual('my-wakanda-id');
|
74
|
+
}));
|
75
|
+
});
|
76
|
+
//# sourceMappingURL=http-wrapper.test.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"http-wrapper.test.js","sourceRoot":"","sources":["../../tests/http-wrapper.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,sDAA8B;AAE9B,kDAA0B;AAC1B,sDAA8B;AAE9B,uEAAgD;AAChD,wCAA6E;AAE7E,OAAO,CAAC,GAAG,CAAC,qBAAC,CAAC,QAAQ,EAAE,EAAE,4BAAC,CAAC,QAAQ,EAAE,CAAC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AA;YAChB,CAAC,CAAA,CAAC,CAAA;QACJ,CAAC,EAAE,IAAI,CAAC,CAAA;QAER,MAAM,YAAY,GAAG,MAAM,WAAW,CAAC,CAAC,GAAG,EAAE,EAAE;YAC7C,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;gBACzB,cAAc,GAAG,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,CAAA;gBAC1C,GAAG,CAAC,IAAI,CAAC;oBACP,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,eAAe,CAAC;oBACnC,MAAM,EAAE,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC;iBACpC,CAAC,CAAA;YACJ,CAAC,CAAC,CAAA;QACJ,CAAC,EAAE,IAAI,CAAC,CAAA;QAER,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,eAAK,CAAC,GAAG,CAAC,uBAAuB,EAAE;YACvE,OAAO,EAAE;gBACP,eAAe,EAAE,YAAY;gBAC7B,cAAc,EAAE,eAAe;aAChC;SACF,CAAC,CAAC;QACH,YAAY,EAAE,CAAC;QACf,YAAY,EAAE,CAAC;QAGf,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,CAAC,CAAA;QACrD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAA;QACxC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,eAAe,CAAC,CAAA;IAC9C,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAC,CAAA"}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import express from 'express';
|
2
|
+
import axios from 'axios';
|
3
|
+
import winston from 'winston';
|
4
|
+
|
5
|
+
import headersTracer from '../src/http-wrapper/index';
|
6
|
+
|
7
|
+
axios.defaults.adapter = require('axios/lib/adapters/http');
|
8
|
+
|
9
|
+
const logger = winston.createLogger({
|
10
|
+
transports: [
|
11
|
+
new winston.transports.Console(),
|
12
|
+
],
|
13
|
+
});
|
14
|
+
|
15
|
+
headersTracer({
|
16
|
+
setAndPropagateCorrelationId: true,
|
17
|
+
headersToPropagate: [
|
18
|
+
'x-text',
|
19
|
+
],
|
20
|
+
headersPrefix: 'x-af-',
|
21
|
+
winstonLogger: logger,
|
22
|
+
});
|
23
|
+
|
24
|
+
const app = express();
|
25
|
+
|
26
|
+
app.all('/', async (req, res) => {
|
27
|
+
const network = axios.create({ baseURL: 'http://localhost:8080/' });
|
28
|
+
const { data } = await network.get('/header-parrot');
|
29
|
+
res.json(data);
|
30
|
+
});
|
31
|
+
|
32
|
+
app.all('/header-parrot', (req, res) => {
|
33
|
+
logger.info('Test message', { t: 1 });
|
34
|
+
res.json(req.headers);
|
35
|
+
});
|
36
|
+
|
37
|
+
app.listen(8080, () => {
|
38
|
+
logger.info('App listen');
|
39
|
+
});
|
package/package.json
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "@autofleet/outbreak",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.2.0-beta-1",
|
4
4
|
"description": "",
|
5
|
-
"main": "dist/index.js",
|
6
|
-
"types": "dist/index.d.ts",
|
5
|
+
"main": "dist/src/index.js",
|
6
|
+
"types": "dist/src/index.d.ts",
|
7
7
|
"scripts": {
|
8
|
-
"
|
8
|
+
"build": "rm -rf ./dist && tsc",
|
9
|
+
"prepublish": "npm run build",
|
9
10
|
"start": "ts-node src/example.ts",
|
10
11
|
"dev": "./node_modules/nodemon/bin/nodemon.js",
|
11
12
|
"lint": "./node_modules/.bin/eslint src/**/*.ts",
|
@@ -15,8 +16,10 @@
|
|
15
16
|
"author": "Dor Shay",
|
16
17
|
"license": "ISC",
|
17
18
|
"dependencies": {
|
19
|
+
"@autofleet/network": "^1.2.1",
|
18
20
|
"axios": "^0.19.2",
|
19
21
|
"express": "^4.17.1",
|
22
|
+
"jsonwebtoken": "^8.5.1",
|
20
23
|
"shortid": "^2.2.15",
|
21
24
|
"uuid": "^3.4.0",
|
22
25
|
"winston": "^3.2.1"
|
@@ -5,6 +5,7 @@ import winston from 'winston';
|
|
5
5
|
|
6
6
|
import headersTracer from '../src/index';
|
7
7
|
|
8
|
+
|
8
9
|
const logger = winston.createLogger({
|
9
10
|
transports: [
|
10
11
|
new winston.transports.Console(),
|
@@ -16,15 +17,15 @@ const generateApp = async (addEndpoints, port) => {
|
|
16
17
|
|
17
18
|
addEndpoints(app);
|
18
19
|
|
19
|
-
const server: http.Server
|
20
|
+
const server: http.Server = await new Promise((resolve) => {
|
20
21
|
const s = app.listen(port, () => {
|
21
|
-
console.log('Listen on port', port)
|
22
|
+
console.log('Listen on port', port);
|
22
23
|
resolve(s);
|
23
24
|
});
|
24
|
-
})
|
25
|
+
});
|
25
26
|
|
26
27
|
return () => server.close();
|
27
|
-
}
|
28
|
+
};
|
28
29
|
|
29
30
|
describe('E2E', () => {
|
30
31
|
it('Basic functionality', async () => {
|
@@ -40,33 +41,33 @@ describe('E2E', () => {
|
|
40
41
|
|
41
42
|
const closeServer1 = await generateApp((app) => {
|
42
43
|
app.get('/', async (req, res) => {
|
43
|
-
const { data: res1 } = await axios.post(
|
44
|
-
res.json(res1)
|
45
|
-
})
|
46
|
-
}, 8081)
|
44
|
+
const { data: res1 } = await axios.post('http://localhost:8082');
|
45
|
+
res.json(res1);
|
46
|
+
});
|
47
|
+
}, 8081);
|
47
48
|
|
48
49
|
const closeServer2 = await generateApp((app) => {
|
49
50
|
app.post('/', (req, res) => {
|
50
|
-
server2TraceId = req.headers['x-trace-id']
|
51
|
+
server2TraceId = req.headers['x-trace-id'];
|
51
52
|
res.json({
|
52
53
|
value: req.headers['x-test-header'],
|
53
|
-
wkanda: req.headers['x-wakanda-id']
|
54
|
-
})
|
55
|
-
})
|
56
|
-
}, 8082)
|
54
|
+
wkanda: req.headers['x-wakanda-id'],
|
55
|
+
});
|
56
|
+
});
|
57
|
+
}, 8082);
|
57
58
|
|
58
|
-
const { data: res1, headers } = await axios.get(
|
59
|
+
const { data: res1, headers } = await axios.get('http://localhost:8081', {
|
59
60
|
headers: {
|
60
61
|
'x-test-header': 'testHeader',
|
61
|
-
'x-wakanda-id': 'my-wakanda-id'
|
62
|
-
}
|
62
|
+
'x-wakanda-id': 'my-wakanda-id',
|
63
|
+
},
|
63
64
|
});
|
64
65
|
closeServer1();
|
65
66
|
closeServer2();
|
66
67
|
|
67
68
|
|
68
|
-
expect(headers['x-trace-id']).toEqual(server2TraceId)
|
69
|
-
expect(res1.value).toEqual('testHeader')
|
70
|
-
expect(res1.wkanda).toEqual('my-wakanda-id')
|
69
|
+
expect(headers['x-trace-id']).toEqual(server2TraceId);
|
70
|
+
expect(res1.value).toEqual('testHeader');
|
71
|
+
expect(res1.wkanda).toEqual('my-wakanda-id');
|
71
72
|
});
|
72
|
-
})
|
73
|
+
});
|
package/tsconfig.json
CHANGED
package/dist/config.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;AAAA,MAAM,mBAAmB,GAAG,YAAY,CAAC;AAEzC,MAAM,kBAAkB,GAAG,CAAC,OAAO,EAAQ,EAAE;IAC3C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QAC3B,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;KAChD;IAED,iEAAiE;IACjE,MAAM,eAAe,GAAG,iBAAiB,CAAC;IAE1C,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAEhG,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE;QAC7B,MAAM,IAAI,KAAK,CAAC,yCAAyC,cAAc,EAAE,CAAC,CAAC;KAC5E;AACH,CAAC,CAAC;AASF,MAAM,IAAI,GAAG,CAAC,YAAuB,EAAE,EAAE,EAAE;IACzC,MAAM,EACJ,4BAA4B,GAAG,IAAI,EACnC,oBAAoB,GAAG,KAAK,EAC5B,kBAAkB,GAAG;QACnB,cAAc;QACd,cAAc;KACf,EACD,aAAa,GAAG,OAAO,GACxB,GAAG,SAAS,CAAC;IAEd,kBAAkB,CAAC,kBAAkB,CAAC,CAAC;IAEvC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;IAE5C,IAAI,eAAe,GAAG,gBAAgB,CAAC;IACvC,IAAI,4BAA4B,KAAK,IAAI,EAAE;QACzC,eAAe,GAAG,CAAC,mBAAmB,CAAC,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;KAClE;IAED,OAAO;QACL,4BAA4B;QAC5B,oBAAoB;QACpB,mBAAmB;QACnB,gBAAgB;QAChB,eAAe;QACf,aAAa;KACd,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe;IACb,IAAI;IACJ,mBAAmB;CACpB,CAAC"}
|
package/dist/example.js
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
-
};
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
-
const express_1 = __importDefault(require("express"));
|
16
|
-
const axios_1 = __importDefault(require("axios"));
|
17
|
-
const winston_1 = __importDefault(require("winston"));
|
18
|
-
const index_1 = __importDefault(require("./index"));
|
19
|
-
axios_1.default.defaults.adapter = require('axios/lib/adapters/http');
|
20
|
-
const logger = winston_1.default.createLogger({
|
21
|
-
transports: [
|
22
|
-
new winston_1.default.transports.Console(),
|
23
|
-
],
|
24
|
-
});
|
25
|
-
index_1.default({
|
26
|
-
setAndPropagateCorrelationId: true,
|
27
|
-
headersToPropagate: [
|
28
|
-
'x-text',
|
29
|
-
],
|
30
|
-
headersPrefix: 'x-af-',
|
31
|
-
winstonLogger: logger,
|
32
|
-
});
|
33
|
-
const app = express_1.default();
|
34
|
-
app.all('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
35
|
-
const network = axios_1.default.create({ baseURL: 'http://localhost:8080/' });
|
36
|
-
const { data } = yield network.get('/header-parrot');
|
37
|
-
res.json(data);
|
38
|
-
}));
|
39
|
-
app.all('/header-parrot', (req, res) => {
|
40
|
-
logger.info('Test message', { t: 1 });
|
41
|
-
res.json(req.headers);
|
42
|
-
});
|
43
|
-
app.listen(8080, () => {
|
44
|
-
logger.info('App listen');
|
45
|
-
});
|
46
|
-
//# sourceMappingURL=example.js.map
|
package/dist/example.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"example.js","sourceRoot":"","sources":["../src/example.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,sDAA8B;AAC9B,kDAA0B;AAC1B,sDAA8B;AAE9B,oDAAoC;AAEpC,eAAK,CAAC,QAAQ,CAAC,OAAO,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAAC;AAE5D,MAAM,MAAM,GAAG,iBAAO,CAAC,YAAY,CAAC;IAClC,UAAU,EAAE;QACV,IAAI,iBAAO,CAAC,UAAU,CAAC,OAAO,EAAE;KACjC;CACF,CAAC,CAAC;AAEH,eAAa,CAAC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1,10 +0,0 @@
|
|
1
|
-
/// <reference types="node" />
|
2
|
-
import asyncHooks from 'async_hooks';
|
3
|
-
declare const tracer: {
|
4
|
-
currentTrace: any;
|
5
|
-
traces: {};
|
6
|
-
};
|
7
|
-
export declare const newTrace: (type: any) => void;
|
8
|
-
export declare const enable: () => asyncHooks.AsyncHook;
|
9
|
-
export declare const getCurrentTrace: () => any;
|
10
|
-
export default tracer;
|
package/dist/tracer.js
DELETED
@@ -1,55 +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 async_hooks_1 = __importDefault(require("async_hooks"));
|
7
|
-
const uuid_1 = __importDefault(require("uuid"));
|
8
|
-
const prevStates = {};
|
9
|
-
const tracer = {
|
10
|
-
currentTrace: null,
|
11
|
-
traces: {},
|
12
|
-
};
|
13
|
-
function init(asyncId, type, triggerAsyncId) {
|
14
|
-
if (tracer.traces[triggerAsyncId]) {
|
15
|
-
tracer.traces[asyncId] = tracer.traces[triggerAsyncId];
|
16
|
-
}
|
17
|
-
}
|
18
|
-
function before(asyncId) {
|
19
|
-
if (!tracer.traces[asyncId]) {
|
20
|
-
return;
|
21
|
-
}
|
22
|
-
prevStates[asyncId] = tracer.currentTrace;
|
23
|
-
tracer.currentTrace = tracer.traces[asyncId];
|
24
|
-
}
|
25
|
-
function after(asyncId) {
|
26
|
-
if (!tracer.traces[asyncId]) {
|
27
|
-
return;
|
28
|
-
}
|
29
|
-
tracer.currentTrace = prevStates[asyncId];
|
30
|
-
}
|
31
|
-
function destroy(asyncId) {
|
32
|
-
if (tracer.traces[asyncId]) {
|
33
|
-
delete tracer.traces[asyncId];
|
34
|
-
delete prevStates[asyncId];
|
35
|
-
}
|
36
|
-
}
|
37
|
-
const hook = async_hooks_1.default.createHook({
|
38
|
-
init, before, after, destroy,
|
39
|
-
});
|
40
|
-
class Trace {
|
41
|
-
constructor(type) {
|
42
|
-
this.id = uuid_1.default.v1();
|
43
|
-
this.type = type;
|
44
|
-
this.context = new Map();
|
45
|
-
}
|
46
|
-
}
|
47
|
-
exports.newTrace = (type) => {
|
48
|
-
tracer.currentTrace = new Trace(type);
|
49
|
-
tracer.traces[async_hooks_1.default.executionAsyncId()] = tracer.currentTrace;
|
50
|
-
return tracer.currentTrace;
|
51
|
-
};
|
52
|
-
exports.enable = () => hook.enable();
|
53
|
-
exports.getCurrentTrace = () => tracer.currentTrace || {};
|
54
|
-
exports.default = tracer;
|
55
|
-
//# sourceMappingURL=tracer.js.map
|
package/dist/tracer.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"tracer.js","sourceRoot":"","sources":["../src/tracer.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AACrC,gDAAwB;AAExB,MAAM,UAAU,GAAG,EAAE,CAAC;AAEtB,MAAM,MAAM,GAAG;IACb,YAAY,EAAE,IAAI;IAClB,MAAM,EAAE,EAAE;CACX,CAAC;AAEF,SAAS,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,cAAc;IACzC,IAAI,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE;QACjC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;KACxD;AACH,CAAC;AAED,SAAS,MAAM,CAAC,OAAO;IACrB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE;QAC3B,OAAO;KACR;IACD,UAAU,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,YAAY,CAAC;IAC1C,MAAM,CAAC,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAC/C,CAAC;AAED,SAAS,KAAK,CAAC,OAAO;IACpB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EA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