@yopdev/dev-server 1.4.1-alpha.0 → 1.4.5
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/src/assert.js +1 -2
- package/dist/src/cloudformation-event-proxy.d.ts +13 -0
- package/dist/src/cloudformation-event-proxy.js +24 -0
- package/dist/src/cloudformation-lambda-http-proxy.d.ts +12 -0
- package/dist/src/cloudformation-lambda-http-proxy.js +42 -0
- package/dist/src/cloudformation.d.ts +28 -0
- package/dist/src/cloudformation.js +47 -0
- package/dist/src/config.d.ts +2 -0
- package/dist/src/container.d.ts +18 -0
- package/dist/src/container.js +33 -0
- package/dist/src/deferred.js +28 -93
- package/dist/src/dev-server.js +48 -48
- package/dist/src/dynamodb.js +25 -75
- package/dist/src/event-proxy.d.ts +5 -4
- package/dist/src/event-proxy.js +52 -113
- package/dist/src/factories.js +11 -64
- package/dist/src/http-server.d.ts +1 -1
- package/dist/src/http-server.js +23 -26
- package/dist/src/index.d.ts +14 -1
- package/dist/src/index.js +18 -1
- package/dist/src/internal-queue.d.ts +5 -2
- package/dist/src/internal-queue.js +44 -101
- package/dist/src/lambda-http-proxy.d.ts +9 -3
- package/dist/src/lambda-http-proxy.js +33 -79
- package/dist/src/localstack.js +37 -79
- package/dist/src/mappers.d.ts +1 -1
- package/dist/src/mappers.js +14 -26
- package/dist/src/pre-traffic-hooks.d.ts +2 -11
- package/dist/src/pre-traffic-hooks.js +12 -61
- package/dist/src/responses.d.ts +1 -1
- package/dist/src/responses.js +4 -1
- package/dist/src/s3.d.ts +7 -0
- package/dist/src/s3.js +20 -0
- package/dist/src/scheduled-tasks.d.ts +2 -13
- package/dist/src/scheduled-tasks.js +12 -61
- package/dist/src/services.d.ts +4 -2
- package/dist/src/services.js +13 -53
- package/dist/src/sns-http-proxy.js +41 -59
- package/dist/src/sns.js +29 -79
- package/dist/src/sqs.d.ts +3 -1
- package/dist/src/sqs.js +27 -65
- package/dist/src/stoppable.d.ts +1 -1
- package/dist/src/stoppable.js +10 -43
- package/dist/src/tunnel.d.ts +10 -0
- package/dist/src/tunnel.js +52 -0
- package/package.json +7 -2
package/dist/src/event-proxy.js
CHANGED
|
@@ -1,129 +1,68 @@
|
|
|
1
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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.newEventsProxy = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
4
|
+
const sqs_consumer_1 = require("sqs-consumer");
|
|
5
|
+
const mappers_1 = require("./mappers");
|
|
6
|
+
const logging_1 = require("@yopdev/logging");
|
|
7
|
+
const stoppable_1 = require("./stoppable");
|
|
8
|
+
const assert_1 = require("./assert");
|
|
9
|
+
const services_1 = require("./services");
|
|
10
|
+
const newEventsProxy = (name, config) => new services_1.Service(new EventsProxy(name, config.handlers, config.mapper ?? mappers_1.mapToLambdaSqsRecord, config.topic));
|
|
47
11
|
exports.newEventsProxy = newEventsProxy;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
var _this = this;
|
|
12
|
+
class EventsProxy {
|
|
13
|
+
constructor(name, handlers, mapper, topic) {
|
|
51
14
|
this.name = name;
|
|
52
15
|
this.handlers = handlers;
|
|
16
|
+
this.mapper = mapper;
|
|
53
17
|
this.topic = topic;
|
|
54
18
|
this.pollingFrequency = 1;
|
|
55
19
|
this.stopped = true;
|
|
56
|
-
this.queueName =
|
|
57
|
-
this.start =
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
.createSubscription({ arn: topic }, { arn: queue.arn })
|
|
72
|
-
.then(function () { return queue.url; }); })];
|
|
73
|
-
});
|
|
74
|
-
}); };
|
|
75
|
-
this.startEventsConsumer = function (sqs, url) { return __awaiter(_this, void 0, void 0, function () {
|
|
76
|
-
var _this = this;
|
|
77
|
-
return __generator(this, function (_a) {
|
|
78
|
-
this.consumer = sqs_consumer_1.Consumer.create({
|
|
79
|
-
waitTimeSeconds: this.pollingFrequency,
|
|
80
|
-
queueUrl: url,
|
|
81
|
-
sqs: sqs,
|
|
82
|
-
handleMessage: function (message) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
83
|
-
return [2 /*return*/, this.onEachMessage(message)];
|
|
84
|
-
}); }); },
|
|
85
|
-
});
|
|
86
|
-
this.consumer.start();
|
|
87
|
-
return [2 /*return*/, url];
|
|
20
|
+
this.queueName = () => `EventProxyQueue${Buffer.from(this.name).toString('hex').substring(0, 64)}`;
|
|
21
|
+
this.start = async (config) => this.queueConnectedToTopic(config.sns, this.topic ?? config.eventsProxy.topic.arn, config.sqs, this.queueName())
|
|
22
|
+
.then((url) => this.startEventsConsumer(config.sqs.client, url))
|
|
23
|
+
.tap(() => this.stopped = false)
|
|
24
|
+
.then((consumer) => this.LOGGER.debug(consumer));
|
|
25
|
+
this.queueConnectedToTopic = async (sns, topic, sqs, queue) => sqs.createStandardQueue(queue)
|
|
26
|
+
.then((queue) => sns
|
|
27
|
+
.createSubscription({ arn: topic }, { arn: queue.arn })
|
|
28
|
+
.then(() => queue.url));
|
|
29
|
+
this.startEventsConsumer = async (sqs, url) => {
|
|
30
|
+
this.consumer = sqs_consumer_1.Consumer.create({
|
|
31
|
+
waitTimeSeconds: this.pollingFrequency,
|
|
32
|
+
queueUrl: url,
|
|
33
|
+
sqs: sqs,
|
|
34
|
+
handleMessage: async (message) => this.onEachMessage(message),
|
|
88
35
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
36
|
+
this.consumer.start();
|
|
37
|
+
return url;
|
|
38
|
+
};
|
|
39
|
+
this.extractErrorMessage = (e) => typeof e === 'string' ? e : e.message !== undefined && typeof e.message === 'string' ? e.message : '';
|
|
40
|
+
this.onEachMessage = (message) => {
|
|
41
|
+
if (this.stopped) {
|
|
42
|
+
this.LOGGER.warn('stopped events proxy received message %o', message);
|
|
94
43
|
return;
|
|
95
44
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
45
|
+
const body = (0, assert_1.assertNotUndefined)(message.Body, 'body is not present');
|
|
46
|
+
const json = JSON.parse(body);
|
|
47
|
+
const attributes = json.MessageAttributes;
|
|
48
|
+
const subject = json.Subject;
|
|
49
|
+
const handlers = this.handlers.filter((handler) => handler.matcher(subject, attributes));
|
|
99
50
|
if (handlers.length === 0) {
|
|
100
|
-
|
|
51
|
+
this.LOGGER.warn('no handlers found for message %o', message);
|
|
101
52
|
}
|
|
102
|
-
|
|
103
|
-
handlers.forEach(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
.catch(function (e) {
|
|
113
|
-
var error = _this.extractErrorMessage(e);
|
|
114
|
-
_this.LOGGER.error(e, 'handler %s failed with %s', handler.name, error);
|
|
115
|
-
});
|
|
116
|
-
});
|
|
53
|
+
const record = this.mapper(message);
|
|
54
|
+
handlers.forEach((handler) => handler
|
|
55
|
+
.handler({ Records: [record] })
|
|
56
|
+
.then(() => {
|
|
57
|
+
this.LOGGER.debug('handler %s accepted message', handler.name);
|
|
58
|
+
})
|
|
59
|
+
.catch((e) => {
|
|
60
|
+
const error = this.extractErrorMessage(e);
|
|
61
|
+
this.LOGGER.error(e, 'handler %s failed with %s', handler.name, error);
|
|
62
|
+
}));
|
|
117
63
|
};
|
|
118
|
-
this.stop =
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return [2 /*return*/, Promise.resolve(this.consumer)
|
|
122
|
-
.then(function (consumer) { return consumer !== undefined ? (0, stoppable_1.stopConsumer)(consumer, _this.pollingFrequency).then(function () { return 'stopped'; }) : 'not started'; })
|
|
123
|
-
.then(function (status) { return _this.LOGGER.info(status); })];
|
|
124
|
-
});
|
|
125
|
-
}); };
|
|
126
|
-
this.LOGGER = logging_1.LoggerFactory.create("SNS->SQS[".concat(name, "]"));
|
|
64
|
+
this.stop = async () => (0, stoppable_1.stopConsumer)(this.pollingFrequency, this.consumer);
|
|
65
|
+
this.LOGGER = logging_1.LoggerFactory.create(`SNS->SQS[${name}]`);
|
|
66
|
+
this.LOGGER.debug('handling events from %s', topic ?? 'the event bus');
|
|
127
67
|
}
|
|
128
|
-
|
|
129
|
-
}());
|
|
68
|
+
}
|
package/dist/src/factories.js
CHANGED
|
@@ -1,69 +1,16 @@
|
|
|
1
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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.oneThenOther = exports.all = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
name:
|
|
43
|
-
start:
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return [2 /*return*/, Promise.all(services.map(function (each) { return each.start(config); })).then(function () { return _this; })];
|
|
47
|
-
});
|
|
48
|
-
}); },
|
|
49
|
-
stop: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
50
|
-
return [2 /*return*/, Promise.all(services.map(function (each) { return each.stop(); })).then()];
|
|
51
|
-
}); }); }
|
|
52
|
-
}); };
|
|
4
|
+
const services_1 = require("./services");
|
|
5
|
+
const all = (services) => new services_1.Service({
|
|
6
|
+
name: `ALLOF[${services.map((each) => each.name).join(',')}]`,
|
|
7
|
+
start: async (config) => Promise.all(services.map((each) => each.start(config))).then(() => this),
|
|
8
|
+
stop: async () => Promise.all(services.map((each) => each.stop())).then()
|
|
9
|
+
});
|
|
53
10
|
exports.all = all;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return [2 /*return*/, other.start(config)];
|
|
60
|
-
}); }); })];
|
|
61
|
-
}); }); },
|
|
62
|
-
stop: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
63
|
-
return [2 /*return*/, other.stop().then(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
64
|
-
return [2 /*return*/, one.stop()];
|
|
65
|
-
}); }); })];
|
|
66
|
-
}); }); },
|
|
67
|
-
});
|
|
68
|
-
};
|
|
11
|
+
const oneThenOther = (one, other) => new services_1.Service({
|
|
12
|
+
name: `[${one.name}]->[${other.name}]`,
|
|
13
|
+
start: async (config) => one.start(config).then(async () => other.start(config)),
|
|
14
|
+
stop: async () => other.stop().then(async () => one.stop()),
|
|
15
|
+
});
|
|
69
16
|
exports.oneThenOther = oneThenOther;
|
|
@@ -16,7 +16,7 @@ export declare class HttpServer implements Lifecycle<string> {
|
|
|
16
16
|
constructor(name: string, settings: HttpSettings, handler: (request: IncomingMessage, requestBody: string, response: ServerResponse) => void);
|
|
17
17
|
start: () => Promise<string>;
|
|
18
18
|
private endpointUrl;
|
|
19
|
-
stop: () => Promise<
|
|
19
|
+
stop: () => Promise<any>;
|
|
20
20
|
}
|
|
21
21
|
export type HttpSettings = {
|
|
22
22
|
protocol: string;
|
package/dist/src/http-server.js
CHANGED
|
@@ -1,40 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HttpServer = exports.newHttpServer = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
const http_1 = require("http");
|
|
5
|
+
const events_1 = require("events");
|
|
6
|
+
const logging_1 = require("@yopdev/logging");
|
|
7
|
+
const assert_1 = require("./assert");
|
|
8
|
+
const services_1 = require("./services");
|
|
9
|
+
const newHttpServer = (name, config, callback) => new services_1.Service(new HttpServer(name, config.settings, config.handler), callback);
|
|
10
10
|
exports.newHttpServer = newHttpServer;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var _this = this;
|
|
11
|
+
class HttpServer {
|
|
12
|
+
constructor(name, settings, handler) {
|
|
14
13
|
this.name = name;
|
|
15
14
|
this.settings = settings;
|
|
16
15
|
this.handler = handler;
|
|
17
|
-
this.start =
|
|
18
|
-
.then(
|
|
19
|
-
.then(
|
|
20
|
-
this.endpointUrl =
|
|
21
|
-
|
|
22
|
-
return
|
|
16
|
+
this.start = () => this.server
|
|
17
|
+
.then(() => this.LOGGER.debug('listening [%s]', (0, assert_1.assertNotUndefined)(this.address).port))
|
|
18
|
+
.then(() => this.endpointUrl());
|
|
19
|
+
this.endpointUrl = () => {
|
|
20
|
+
const address = (0, assert_1.assertNotUndefined)(this.address, 'server not started');
|
|
21
|
+
return `${this.protocol}//${address.address}:${address.port}/`;
|
|
23
22
|
};
|
|
24
|
-
this.stop =
|
|
25
|
-
|
|
26
|
-
var server = (0, http_1.createServer)(function (req, res) {
|
|
23
|
+
this.stop = () => this.server.then((server) => server.close()).then(() => undefined);
|
|
24
|
+
const server = (0, http_1.createServer)((req, res) => {
|
|
27
25
|
req.setEncoding('utf-8');
|
|
28
|
-
|
|
29
|
-
req.on('data',
|
|
30
|
-
req.on('end',
|
|
26
|
+
let body = '';
|
|
27
|
+
req.on('data', (chunks) => body = body.concat(chunks));
|
|
28
|
+
req.on('end', () => handler(req, body, res));
|
|
31
29
|
})
|
|
32
|
-
.listen(settings.port, settings.host,
|
|
30
|
+
.listen(settings.port, settings.host, () => this.address = server.address());
|
|
33
31
|
this.server = (0, events_1.once)(server, 'listening')
|
|
34
|
-
.then(
|
|
32
|
+
.then(() => server);
|
|
35
33
|
this.protocol = settings.protocol;
|
|
36
|
-
this.LOGGER = logging_1.LoggerFactory.create(
|
|
34
|
+
this.LOGGER = logging_1.LoggerFactory.create(`HTTPSERVER[${name}]`);
|
|
37
35
|
}
|
|
38
|
-
|
|
39
|
-
}());
|
|
36
|
+
}
|
|
40
37
|
exports.HttpServer = HttpServer;
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
export { DevServer } from './dev-server';
|
|
2
2
|
export { newHttpServer as httpServer } from './http-server';
|
|
3
|
-
export { newLambdaHttpProxy as lambdaHttpProxy } from './lambda-http-proxy';
|
|
3
|
+
export { newLambdaHttpProxy as lambdaHttpProxy, UNAUTHORIZED } from './lambda-http-proxy';
|
|
4
|
+
export { newLambdaProxyFromCloudFormationTemplate as cloudFormationLambdaProxy } from './cloudformation-lambda-http-proxy';
|
|
4
5
|
export { newSnsHttpProxy as snsHttpProxy } from './sns-http-proxy';
|
|
5
6
|
export { newInternalQueue as internalQueue } from './internal-queue';
|
|
6
7
|
export { newEventsProxy as eventsProxy } from './event-proxy';
|
|
8
|
+
export { snsEventsFromCloudFormationTemplate as cloudFormationSnsEventHandlers } from './cloudformation-event-proxy';
|
|
7
9
|
export { newScheduledTasks as scheduledTasks } from './scheduled-tasks';
|
|
8
10
|
export { newPreTrafficHooks as preTrafficHooks } from './pre-traffic-hooks';
|
|
9
11
|
export { newDynamoDbTable as dynamoDbTable } from './dynamodb';
|
|
12
|
+
export { newContainer as container } from './container';
|
|
13
|
+
export { newTunnel as tunnel } from './tunnel';
|
|
10
14
|
export { all as allOf, oneThenOther } from './factories';
|
|
11
15
|
export { lazy, promised } from './deferred';
|
|
16
|
+
export { Lifecycle, Service } from './services';
|
|
17
|
+
export { DevServerConfig } from './config';
|
|
18
|
+
export { Route } from './lambda-http-proxy';
|
|
19
|
+
export { mapToLambdaEvent as defaultApiGatewayLambdaMapper, mapToLambdaSqsRecord as defaultSqsLambdaMapper } from './mappers';
|
|
20
|
+
declare global {
|
|
21
|
+
interface Promise<T> {
|
|
22
|
+
tap(onfulfilled?: (value: T) => void, onrejected?: (reason: unknown) => void): Promise<T>;
|
|
23
|
+
}
|
|
24
|
+
}
|
package/dist/src/index.js
CHANGED
|
@@ -1,27 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.promised = exports.lazy = exports.oneThenOther = exports.allOf = exports.dynamoDbTable = exports.preTrafficHooks = exports.scheduledTasks = exports.eventsProxy = exports.internalQueue = exports.snsHttpProxy = exports.lambdaHttpProxy = exports.httpServer = exports.DevServer = void 0;
|
|
3
|
+
exports.defaultSqsLambdaMapper = exports.defaultApiGatewayLambdaMapper = exports.Service = exports.promised = exports.lazy = exports.oneThenOther = exports.allOf = exports.tunnel = exports.container = exports.dynamoDbTable = exports.preTrafficHooks = exports.scheduledTasks = exports.cloudFormationSnsEventHandlers = exports.eventsProxy = exports.internalQueue = exports.snsHttpProxy = exports.cloudFormationLambdaProxy = exports.UNAUTHORIZED = exports.lambdaHttpProxy = exports.httpServer = exports.DevServer = void 0;
|
|
4
4
|
var dev_server_1 = require("./dev-server");
|
|
5
5
|
Object.defineProperty(exports, "DevServer", { enumerable: true, get: function () { return dev_server_1.DevServer; } });
|
|
6
6
|
var http_server_1 = require("./http-server");
|
|
7
7
|
Object.defineProperty(exports, "httpServer", { enumerable: true, get: function () { return http_server_1.newHttpServer; } });
|
|
8
8
|
var lambda_http_proxy_1 = require("./lambda-http-proxy");
|
|
9
9
|
Object.defineProperty(exports, "lambdaHttpProxy", { enumerable: true, get: function () { return lambda_http_proxy_1.newLambdaHttpProxy; } });
|
|
10
|
+
Object.defineProperty(exports, "UNAUTHORIZED", { enumerable: true, get: function () { return lambda_http_proxy_1.UNAUTHORIZED; } });
|
|
11
|
+
var cloudformation_lambda_http_proxy_1 = require("./cloudformation-lambda-http-proxy");
|
|
12
|
+
Object.defineProperty(exports, "cloudFormationLambdaProxy", { enumerable: true, get: function () { return cloudformation_lambda_http_proxy_1.newLambdaProxyFromCloudFormationTemplate; } });
|
|
10
13
|
var sns_http_proxy_1 = require("./sns-http-proxy");
|
|
11
14
|
Object.defineProperty(exports, "snsHttpProxy", { enumerable: true, get: function () { return sns_http_proxy_1.newSnsHttpProxy; } });
|
|
12
15
|
var internal_queue_1 = require("./internal-queue");
|
|
13
16
|
Object.defineProperty(exports, "internalQueue", { enumerable: true, get: function () { return internal_queue_1.newInternalQueue; } });
|
|
14
17
|
var event_proxy_1 = require("./event-proxy");
|
|
15
18
|
Object.defineProperty(exports, "eventsProxy", { enumerable: true, get: function () { return event_proxy_1.newEventsProxy; } });
|
|
19
|
+
var cloudformation_event_proxy_1 = require("./cloudformation-event-proxy");
|
|
20
|
+
Object.defineProperty(exports, "cloudFormationSnsEventHandlers", { enumerable: true, get: function () { return cloudformation_event_proxy_1.snsEventsFromCloudFormationTemplate; } });
|
|
16
21
|
var scheduled_tasks_1 = require("./scheduled-tasks");
|
|
17
22
|
Object.defineProperty(exports, "scheduledTasks", { enumerable: true, get: function () { return scheduled_tasks_1.newScheduledTasks; } });
|
|
18
23
|
var pre_traffic_hooks_1 = require("./pre-traffic-hooks");
|
|
19
24
|
Object.defineProperty(exports, "preTrafficHooks", { enumerable: true, get: function () { return pre_traffic_hooks_1.newPreTrafficHooks; } });
|
|
20
25
|
var dynamodb_1 = require("./dynamodb");
|
|
21
26
|
Object.defineProperty(exports, "dynamoDbTable", { enumerable: true, get: function () { return dynamodb_1.newDynamoDbTable; } });
|
|
27
|
+
var container_1 = require("./container");
|
|
28
|
+
Object.defineProperty(exports, "container", { enumerable: true, get: function () { return container_1.newContainer; } });
|
|
29
|
+
var tunnel_1 = require("./tunnel");
|
|
30
|
+
Object.defineProperty(exports, "tunnel", { enumerable: true, get: function () { return tunnel_1.newTunnel; } });
|
|
22
31
|
var factories_1 = require("./factories");
|
|
23
32
|
Object.defineProperty(exports, "allOf", { enumerable: true, get: function () { return factories_1.all; } });
|
|
24
33
|
Object.defineProperty(exports, "oneThenOther", { enumerable: true, get: function () { return factories_1.oneThenOther; } });
|
|
25
34
|
var deferred_1 = require("./deferred");
|
|
26
35
|
Object.defineProperty(exports, "lazy", { enumerable: true, get: function () { return deferred_1.lazy; } });
|
|
27
36
|
Object.defineProperty(exports, "promised", { enumerable: true, get: function () { return deferred_1.promised; } });
|
|
37
|
+
var services_1 = require("./services");
|
|
38
|
+
Object.defineProperty(exports, "Service", { enumerable: true, get: function () { return services_1.Service; } });
|
|
39
|
+
var mappers_1 = require("./mappers");
|
|
40
|
+
Object.defineProperty(exports, "defaultApiGatewayLambdaMapper", { enumerable: true, get: function () { return mappers_1.mapToLambdaEvent; } });
|
|
41
|
+
Object.defineProperty(exports, "defaultSqsLambdaMapper", { enumerable: true, get: function () { return mappers_1.mapToLambdaSqsRecord; } });
|
|
42
|
+
Promise.prototype.tap = async function (onfulfilled, onrejected) {
|
|
43
|
+
return this.then(onfulfilled, onrejected).then(async () => this);
|
|
44
|
+
};
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { Queue } from "./sqs";
|
|
2
|
-
import { SQSEvent } from "aws-lambda";
|
|
2
|
+
import { SQSEvent, SQSRecord } from "aws-lambda";
|
|
3
3
|
import { Service, Callback } from "./services";
|
|
4
|
+
import { Message } from "@aws-sdk/client-sqs";
|
|
4
5
|
export declare const newInternalQueue: (name: string, config: {
|
|
5
|
-
|
|
6
|
+
name: string;
|
|
6
7
|
visibility: number;
|
|
7
8
|
handler: (queue: Queue) => (e: SQSEvent) => Promise<void>;
|
|
9
|
+
fifo: boolean;
|
|
10
|
+
mapper?: (message: Message) => SQSRecord;
|
|
8
11
|
}, callback?: Callback<string>) => Service<string>;
|
|
@@ -1,112 +1,55 @@
|
|
|
1
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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.newInternalQueue = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
4
|
+
const logging_1 = require("@yopdev/logging");
|
|
5
|
+
const sqs_consumer_1 = require("sqs-consumer");
|
|
6
|
+
const stoppable_1 = require("./stoppable");
|
|
7
|
+
const mappers_1 = require("./mappers");
|
|
8
|
+
const services_1 = require("./services");
|
|
9
|
+
const newInternalQueue = (name, config, callback) => new services_1.Service(new InternalQueue(name, config.visibility, config.handler, (sqs) => config.fifo ? sqs.createFifoQueue : sqs.createStandardQueue, config.mapper ?? mappers_1.mapToLambdaSqsRecord), callback);
|
|
46
10
|
exports.newInternalQueue = newInternalQueue;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
var _this = this;
|
|
11
|
+
class InternalQueue {
|
|
12
|
+
constructor(name, visibility, handler, creator, mapper) {
|
|
50
13
|
this.name = name;
|
|
51
14
|
this.visibility = visibility;
|
|
52
15
|
this.handler = handler;
|
|
53
|
-
this.
|
|
54
|
-
this.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
16
|
+
this.creator = creator;
|
|
17
|
+
this.mapper = mapper;
|
|
18
|
+
this.start = (config) => this.create(config.sqs);
|
|
19
|
+
this.create = async (sqs) => this.createQueue(sqs, this.name)
|
|
20
|
+
.then((queue) => this
|
|
21
|
+
.createConsumer(sqs.client, this.name, queue.url, this.handler(queue), this.visibility)
|
|
22
|
+
.then(() => queue.url));
|
|
23
|
+
this.createConsumer = async (sqs, name, url, handler, visibility) => Promise.resolve(sqs_consumer_1.Consumer.create({
|
|
24
|
+
queueUrl: url,
|
|
25
|
+
waitTimeSeconds: visibility,
|
|
26
|
+
sqs: sqs,
|
|
27
|
+
handleMessage: async (message) => handler({ Records: [this.mapper(message)] }),
|
|
28
|
+
}))
|
|
29
|
+
.then((consumer) => {
|
|
30
|
+
consumer.on('error', (err) => {
|
|
31
|
+
this.LOGGER.error(err, 'failed to handle message');
|
|
61
32
|
});
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
var _this = this;
|
|
65
|
-
return __generator(this, function (_a) {
|
|
66
|
-
return [2 /*return*/, Promise.resolve(sqs_consumer_1.Consumer.create({
|
|
67
|
-
queueUrl: url,
|
|
68
|
-
waitTimeSeconds: visibility,
|
|
69
|
-
sqs: sqs,
|
|
70
|
-
handleMessage: function (message) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
71
|
-
return [2 /*return*/, handler({ Records: [(0, mappers_1.mapToLambdaSqsRecord)(message)] })];
|
|
72
|
-
}); }); },
|
|
73
|
-
}))
|
|
74
|
-
.then(function (consumer) {
|
|
75
|
-
consumer.on('error', function (err) {
|
|
76
|
-
_this.LOGGER.error(err, 'failed to handle message');
|
|
77
|
-
});
|
|
78
|
-
consumer.on('processing_error', function (err) {
|
|
79
|
-
_this.LOGGER.error(err, 'failed to process message');
|
|
80
|
-
});
|
|
81
|
-
consumer.start();
|
|
82
|
-
_this.LOGGER.info('consumer for %s initialized', name);
|
|
83
|
-
_this.state = {
|
|
84
|
-
consumer: consumer,
|
|
85
|
-
visibility: visibility,
|
|
86
|
-
};
|
|
87
|
-
})
|
|
88
|
-
.then(function () { return _this.LOGGER.info('started'); })
|
|
89
|
-
.then(function () { return _this; })];
|
|
33
|
+
consumer.on('processing_error', (err) => {
|
|
34
|
+
this.LOGGER.error(err, 'failed to process message');
|
|
90
35
|
});
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
36
|
+
consumer.start();
|
|
37
|
+
this.LOGGER.info('consumer for %s initialized', name);
|
|
38
|
+
this.state = {
|
|
39
|
+
consumer: consumer,
|
|
40
|
+
visibility: visibility,
|
|
41
|
+
};
|
|
42
|
+
})
|
|
43
|
+
.then(() => this.LOGGER.info('started'))
|
|
44
|
+
.then(() => this);
|
|
45
|
+
this.stop = () => (0, stoppable_1.stopConsumer)(this.state.visibility, this.state.consumer);
|
|
46
|
+
this.LOGGER = logging_1.LoggerFactory.create(`INTERNALQUEUE[${name}]`);
|
|
97
47
|
}
|
|
98
|
-
|
|
99
|
-
return
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
.createNamedQueue(name) //
|
|
104
|
-
.then(function (queue) {
|
|
105
|
-
_this.LOGGER.info('created: %s', queue.url);
|
|
106
|
-
return queue;
|
|
107
|
-
})];
|
|
108
|
-
});
|
|
48
|
+
async createQueue(sqs, name) {
|
|
49
|
+
return this.creator(sqs)(name) //
|
|
50
|
+
.then((queue) => {
|
|
51
|
+
this.LOGGER.info('created: %s', queue.url);
|
|
52
|
+
return queue;
|
|
109
53
|
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
}());
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { HttpSettings } from "./http-server";
|
|
2
|
-
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
|
|
2
|
+
import { APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
|
|
3
|
+
import { IncomingMessage } from "http";
|
|
3
4
|
import { Service, Callback } from "./services";
|
|
5
|
+
export declare const UNAUTHORIZED: Error;
|
|
4
6
|
export declare const newLambdaHttpProxy: (name: string, config: {
|
|
5
7
|
settings: HttpSettings;
|
|
6
8
|
routes: Route[];
|
|
9
|
+
mapper?: (request: IncomingMessage, body: string) => Promise<APIGatewayProxyEvent>;
|
|
10
|
+
authorizer?: Authorizer;
|
|
7
11
|
}, callback?: Callback<string>) => Service<string>;
|
|
8
|
-
type Route = {
|
|
12
|
+
export type Route = {
|
|
9
13
|
method: RegExp;
|
|
10
14
|
path: RegExp;
|
|
15
|
+
weight: number;
|
|
16
|
+
authorizer?: Authorizer;
|
|
11
17
|
handler: (event: APIGatewayProxyEvent) => Promise<APIGatewayProxyResult>;
|
|
12
18
|
};
|
|
13
|
-
export
|
|
19
|
+
export type Authorizer = (authorization: string) => Promise<APIGatewayEventDefaultAuthorizerContext | undefined>;
|