@yopdev/dev-server 1.3.10 → 1.3.16
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.
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import { HttpSettings } from "./http-server";
|
|
2
|
-
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
|
|
2
|
+
import { APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
|
|
3
3
|
import { IncomingMessage } from "http";
|
|
4
4
|
import { Service, Callback } from "./services";
|
|
5
|
+
export declare const UNAUTHORIZED: Error;
|
|
5
6
|
export declare const newLambdaHttpProxy: (name: string, config: {
|
|
6
7
|
settings: HttpSettings;
|
|
7
8
|
routes: Route[];
|
|
8
9
|
mapper?: (request: IncomingMessage, body: string) => Promise<APIGatewayProxyEvent>;
|
|
10
|
+
authorizer?: Authorizer;
|
|
9
11
|
}, callback?: Callback<string>) => Service<string>;
|
|
10
12
|
export type Route = {
|
|
11
13
|
method: RegExp;
|
|
12
14
|
path: RegExp;
|
|
13
15
|
weight: number;
|
|
16
|
+
authorizer?: Authorizer;
|
|
14
17
|
handler: (event: APIGatewayProxyEvent) => Promise<APIGatewayProxyResult>;
|
|
15
18
|
};
|
|
19
|
+
type Authorizer = (authorization: string) => Promise<APIGatewayEventDefaultAuthorizerContext | undefined>;
|
|
20
|
+
export {};
|
|
@@ -36,36 +36,51 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.newLambdaHttpProxy = void 0;
|
|
39
|
+
exports.newLambdaHttpProxy = exports.UNAUTHORIZED = void 0;
|
|
40
40
|
var logging_1 = require("@yopdev/logging");
|
|
41
41
|
var http_server_1 = require("./http-server");
|
|
42
42
|
var mappers_1 = require("./mappers");
|
|
43
43
|
var responses_1 = require("./responses");
|
|
44
44
|
var services_1 = require("./services");
|
|
45
|
-
|
|
45
|
+
exports.UNAUTHORIZED = new Error('UNAUTHORIZED');
|
|
46
|
+
var newLambdaHttpProxy = function (name, config, callback) {
|
|
47
|
+
var _a, _b;
|
|
48
|
+
return new services_1.Service(new LambdaHttpProxy(name, config.settings, config.routes, (_a = config.mapper) !== null && _a !== void 0 ? _a : mappers_1.mapToLambdaEvent, (_b = config.authorizer) !== null && _b !== void 0 ? _b : (function () { return Promise.resolve(undefined); })), callback);
|
|
49
|
+
};
|
|
46
50
|
exports.newLambdaHttpProxy = newLambdaHttpProxy;
|
|
47
51
|
var LambdaHttpProxy = /** @class */ (function () {
|
|
48
|
-
function LambdaHttpProxy(name, settings, routes, mapper) {
|
|
52
|
+
function LambdaHttpProxy(name, settings, routes, mapper, defaultAuthorizer) {
|
|
49
53
|
var _this = this;
|
|
50
54
|
this.name = name;
|
|
51
55
|
this.routes = routes;
|
|
52
56
|
this.mapper = mapper;
|
|
57
|
+
this.defaultAuthorizer = defaultAuthorizer;
|
|
53
58
|
this.start = function () { return _this.server.start(); };
|
|
54
59
|
this.stop = function () { return _this.server.stop(); };
|
|
55
|
-
this.handler = function (lambdaHandler) {
|
|
60
|
+
this.handler = function (authorizer, lambdaHandler) {
|
|
56
61
|
return function (request, body, response) {
|
|
57
62
|
return _this.mapper(request, body)
|
|
58
|
-
.then(function (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
.then(function (proxyEvent) { return __awaiter(_this, void 0, void 0, function () {
|
|
64
|
+
var _this = this;
|
|
65
|
+
return __generator(this, function (_a) {
|
|
66
|
+
return [2 /*return*/, authorizer(proxyEvent.headers['authorization'])
|
|
67
|
+
.then(function (context) { return __awaiter(_this, void 0, void 0, function () {
|
|
68
|
+
var _this = this;
|
|
69
|
+
return __generator(this, function (_a) {
|
|
70
|
+
proxyEvent.requestContext.authorizer = context;
|
|
71
|
+
return [2 /*return*/, lambdaHandler(proxyEvent)
|
|
72
|
+
.then(function (lambda) {
|
|
73
|
+
var _a, _b, _c, _d, _e, _f;
|
|
74
|
+
return (0, responses_1.writeResponse)(response, lambda.statusCode, lambda.body, (_c = (_b = (_a = lambda.headers) === null || _a === void 0 ? void 0 : _a['Content-Type']) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : undefined, (_f = (_e = (_d = lambda.headers) === null || _d === void 0 ? void 0 : _d['Location']) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : undefined);
|
|
75
|
+
})
|
|
76
|
+
.catch(function (e) {
|
|
77
|
+
_this.LOGGER.error(e, 'request failed to execute');
|
|
78
|
+
(0, responses_1.internalServerError)(response, e.body);
|
|
79
|
+
})];
|
|
80
|
+
});
|
|
81
|
+
}); }, function (e) { return e === exports.UNAUTHORIZED ? (0, responses_1.writeResponse)(response, 401, '') : (0, responses_1.internalServerError)(response, e); })];
|
|
67
82
|
});
|
|
68
|
-
});
|
|
83
|
+
}); });
|
|
69
84
|
};
|
|
70
85
|
};
|
|
71
86
|
this.fallback = function (request, _, response) { return __awaiter(_this, void 0, void 0, function () {
|
|
@@ -80,7 +95,7 @@ var LambdaHttpProxy = /** @class */ (function () {
|
|
|
80
95
|
return ((_a = _this.routes.filter(function (r) { return request.method.match(r.method); })
|
|
81
96
|
.filter(function (r) { return request.url.match(r.path); })
|
|
82
97
|
.sort(function (r1, r2) { return r2.weight - r1.weight; })
|
|
83
|
-
.map(function (r) { return _this.handler(r.handler); })
|
|
98
|
+
.map(function (r) { var _a; return _this.handler((_a = r.authorizer) !== null && _a !== void 0 ? _a : _this.defaultAuthorizer, r.handler); })
|
|
84
99
|
.find(function () { return true; })) !== null && _a !== void 0 ? _a : _this.fallback)(request, body, response);
|
|
85
100
|
};
|
|
86
101
|
this.server = new http_server_1.HttpServer(name, settings, this.resolveRoute);
|
package/dist/src/mappers.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.mapToLambdaEvent = exports.mapToLambdaSqsRecord = void 0;
|
|
4
|
-
var logging_1 = require("@yopdev/logging");
|
|
5
4
|
var url_1 = require("url");
|
|
6
|
-
var LOGGER = logging_1.LoggerFactory.create('MAPPERS');
|
|
7
5
|
var mapToLambdaSqsRecord = function (message) {
|
|
8
6
|
if (!message.Body)
|
|
9
7
|
throw new Error('message Body must be present');
|
|
@@ -31,7 +29,6 @@ function mapToLambdaEvent(req, requestBody) {
|
|
|
31
29
|
var url = new url_1.URL(decodeURIComponent(req.url), "http://".concat(req.headers.host));
|
|
32
30
|
var qsp = {};
|
|
33
31
|
url.searchParams.forEach(function (v, k) { return (qsp[k] = v); });
|
|
34
|
-
var claims = extractClaims(req);
|
|
35
32
|
var headers = {};
|
|
36
33
|
if (req.headers['content-type'])
|
|
37
34
|
headers['content-type'] = req.headers['content-type'];
|
|
@@ -54,7 +51,7 @@ function mapToLambdaEvent(req, requestBody) {
|
|
|
54
51
|
requestContext: {
|
|
55
52
|
accountId: '',
|
|
56
53
|
apiId: '',
|
|
57
|
-
authorizer:
|
|
54
|
+
authorizer: undefined,
|
|
58
55
|
httpMethod: '',
|
|
59
56
|
identity: {
|
|
60
57
|
accessKey: '',
|
|
@@ -90,14 +87,3 @@ function mapToLambdaEvent(req, requestBody) {
|
|
|
90
87
|
});
|
|
91
88
|
}
|
|
92
89
|
exports.mapToLambdaEvent = mapToLambdaEvent;
|
|
93
|
-
function extractClaims(req) {
|
|
94
|
-
var _a;
|
|
95
|
-
var jwt = (_a = req.headers['authorization']) === null || _a === void 0 ? void 0 : _a.replace('Bearer ', '');
|
|
96
|
-
try {
|
|
97
|
-
return jwt ? JSON.parse(atob(jwt.split('.')[1])) : null;
|
|
98
|
-
}
|
|
99
|
-
catch (e) {
|
|
100
|
-
LOGGER.warn('Invalid token, claims could not be extracted: %O', jwt);
|
|
101
|
-
return undefined;
|
|
102
|
-
}
|
|
103
|
-
}
|