@yopdev/dev-server 1.4.0-alpha.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ import { Sqs } from "./sqs";
2
2
  import { Sns } from "./sns";
3
3
  import { DynamoDb } from "./dynamodb";
4
4
  import { StartedNetwork } from "testcontainers";
5
+ import { S3 } from "./s3";
5
6
  export type Config = {
6
7
  boundServicesPort?: number;
7
8
  localStackDockerImage?: string;
@@ -20,6 +21,7 @@ export type DevServerConfig = {
20
21
  network: StartedNetwork;
21
22
  sqs: Sqs;
22
23
  sns: Sns;
24
+ s3: S3;
23
25
  dynamo: DynamoDb;
24
26
  eventsProxy: {
25
27
  topic: {
@@ -0,0 +1,18 @@
1
+ import { BindMount, Environment } from 'testcontainers/build/types';
2
+ import { Callback, Service } from './services';
3
+ import { PortWithOptionalBinding } from 'testcontainers/build/utils/port';
4
+ import { Readable } from 'stream';
5
+ import { WaitStrategy } from 'testcontainers/build/wait-strategies/wait-strategy';
6
+ export declare const newContainer: (name: string, config: {
7
+ image: string;
8
+ networkAlias: string;
9
+ environment?: Environment;
10
+ bindMounts?: BindMount[];
11
+ exposedPorts?: PortWithOptionalBinding[];
12
+ logConsumer?: (stream: Readable) => unknown;
13
+ startup?: {
14
+ waitStrategy: WaitStrategy;
15
+ timeout: number;
16
+ };
17
+ endpointBuilder?: (port: number) => string;
18
+ }, callback?: Callback<string>) => Service<string>;
@@ -0,0 +1,80 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.newContainer = void 0;
40
+ var logging_1 = require("@yopdev/logging");
41
+ var testcontainers_1 = require("testcontainers");
42
+ var services_1 = require("./services");
43
+ var newContainer = function (name, config, callback) {
44
+ return new services_1.Service(new Container(name, config.image, config.networkAlias, config.environment, config.bindMounts, config.exposedPorts, config.logConsumer, config.startup, config.endpointBuilder), callback);
45
+ };
46
+ exports.newContainer = newContainer;
47
+ var Container = /** @class */ (function () {
48
+ function Container(name, image, networkAlias, environment, bindMounts, exposedPorts, logConsumer, startup, endpointBuilder) {
49
+ var _this = this;
50
+ this.name = name;
51
+ this.networkAlias = networkAlias;
52
+ this.start = function (config) { return __awaiter(_this, void 0, void 0, function () {
53
+ var _this = this;
54
+ return __generator(this, function (_a) {
55
+ return [2 /*return*/, Promise.resolve(this.LOGGER.info('start'))
56
+ .then(function () { return _this.container
57
+ .withNetwork(config.network)
58
+ .withNetworkAliases(_this.networkAlias)
59
+ .start(); })
60
+ .then(function (started) { return _this.started = started; })
61
+ .then(function () { return _this.endpointBuilder(_this.started.getFirstMappedPort()); })
62
+ .tap(function (url) { return _this.LOGGER.debug(url); })];
63
+ });
64
+ }); };
65
+ this.stop = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
66
+ return [2 /*return*/, this.started.stop().then(function () { return undefined; })];
67
+ }); }); };
68
+ this.LOGGER = logging_1.LoggerFactory.create("CONTAINER[".concat(name, "]"));
69
+ var generic = new testcontainers_1.GenericContainer(image);
70
+ var withEnvironment = environment ? generic.withEnvironment(environment) : generic;
71
+ var withBindMounts = bindMounts ? withEnvironment.withBindMounts(bindMounts) : withEnvironment;
72
+ var withExposedPorts = exposedPorts ? withBindMounts.withExposedPorts.apply(withBindMounts, exposedPorts) : withBindMounts;
73
+ var withLogConsumer = logConsumer ? withExposedPorts.withLogConsumer(logConsumer) : withExposedPorts;
74
+ this.container = startup ? withLogConsumer.withWaitStrategy(startup.waitStrategy).withStartupTimeout(startup.timeout) : withLogConsumer;
75
+ this.networkAlias = networkAlias;
76
+ this.endpointBuilder = endpointBuilder !== null && endpointBuilder !== void 0 ? endpointBuilder : (function (port) { return "http://localhost:".concat(port); });
77
+ }
78
+ return Container;
79
+ }());
80
+ ;
@@ -95,13 +95,9 @@ var Promised = /** @class */ (function () {
95
95
  .then(function () { return _this.service.start(config); })];
96
96
  });
97
97
  }); };
98
- this.stop = function () { return __awaiter(_this, void 0, void 0, function () {
99
- var _this = this;
100
- return __generator(this, function (_a) {
101
- return [2 /*return*/, Promise.resolve(this.LOGGER.info('stopped'))
102
- .then(function () { return _this.service.stop(); })];
103
- });
104
- }); };
98
+ this.stop = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
99
+ return [2 /*return*/, this.service.stop()];
100
+ }); }); };
105
101
  this.name = (0, crypto_1.randomUUID)();
106
102
  this.LOGGER = logging_1.LoggerFactory.create("PROMISED[".concat(this.name, "]"));
107
103
  }
@@ -7,6 +7,8 @@ var sqs_1 = require("./sqs");
7
7
  var dynamodb_1 = require("./dynamodb");
8
8
  var logging_1 = require("@yopdev/logging");
9
9
  var sns_1 = require("./sns");
10
+ var tunnel_1 = require("./tunnel");
11
+ var s3_1 = require("./s3");
10
12
  var DevServer = /** @class */ (function () {
11
13
  function DevServer(name, service, config) {
12
14
  var _this = this;
@@ -32,6 +34,7 @@ var DevServer = /** @class */ (function () {
32
34
  network: infra.network,
33
35
  sqs: new sqs_1.Sqs(infra.aws),
34
36
  sns: new sns_1.Sns(infra.aws),
37
+ s3: new s3_1.S3(infra.aws),
35
38
  dynamo: new dynamodb_1.DynamoDb(infra.aws),
36
39
  eventsProxy: {
37
40
  topic: {
@@ -39,12 +42,13 @@ var DevServer = /** @class */ (function () {
39
42
  }
40
43
  },
41
44
  }); }); }); })
42
- .then(function (config) { _this.LOGGER.debug('starting. config is %o', config); return config; })
45
+ .tap(function (config) { return _this.LOGGER.debug('config is %o', config); })
43
46
  .then(function (aws) { return _this.service.start(aws); })
44
47
  .then(function () { return _this.LOGGER.info('started'); })
45
48
  .then(function () { return _this; });
46
49
  };
47
50
  this.stop = function () { return _this.service.stop()
51
+ .then(tunnel_1.terminate)
48
52
  .then(function () { var _a; return (_a = _this.localStack) === null || _a === void 0 ? void 0 : _a.stop(); })
49
53
  .then(function () { return _this.LOGGER.info('stopped'); }); };
50
54
  this.LOGGER = logging_1.LoggerFactory.create("DEVSERVER[".concat(name, "]"));
@@ -40,6 +40,7 @@ exports.DynamoDb = exports.newDynamoDbTable = void 0;
40
40
  var client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
41
41
  var logging_1 = require("@yopdev/logging");
42
42
  var services_1 = require("./services");
43
+ var assert_1 = require("./assert");
43
44
  var newDynamoDbTable = function (name, config, callback) { return new services_1.Service(new DynamoDbTableCreator(name, config.command), callback); };
44
45
  exports.newDynamoDbTable = newDynamoDbTable;
45
46
  var DynamoDbTableCreator = /** @class */ (function () {
@@ -51,12 +52,11 @@ var DynamoDbTableCreator = /** @class */ (function () {
51
52
  var _this = this;
52
53
  return __generator(this, function (_a) {
53
54
  return [2 /*return*/, config.dynamo.createTable(this.command)
54
- .then(function () { return _this.LOGGER.info('created'); })
55
+ .tap(function (output) { return _this.LOGGER.debug(output); })
55
56
  .then(function () { return _this.name; })];
56
57
  });
57
58
  }); };
58
- this.stop = function () { return Promise.resolve()
59
- .then(function () { return _this.LOGGER.info('stopped'); }); };
59
+ this.stop = function () { return Promise.resolve(); };
60
60
  this.LOGGER = logging_1.LoggerFactory.create("TABLE[".concat(name, "]"));
61
61
  }
62
62
  return DynamoDbTableCreator;
@@ -69,7 +69,7 @@ var DynamoDb = /** @class */ (function () {
69
69
  return __generator(this, function (_a) {
70
70
  return [2 /*return*/, this.client
71
71
  .send(factory) //
72
- .then(function (table) { var _a; LOGGER.info('table created %s', (_a = table.TableDescription) === null || _a === void 0 ? void 0 : _a.TableName); return table; })
72
+ .tap(function (table) { return LOGGER.debug('created %s', (0, assert_1.assertNotUndefined)(table.TableDescription).TableName); })
73
73
  .catch(function (reason) {
74
74
  LOGGER.error('failed to create table');
75
75
  if (reason.name !== 'ResourceInUseException')
@@ -78,7 +78,6 @@ var DynamoDb = /** @class */ (function () {
78
78
  });
79
79
  }); };
80
80
  this.client = new client_dynamodb_1.DynamoDBClient(config);
81
- LOGGER.debug('initialized');
82
81
  }
83
82
  return DynamoDb;
84
83
  }());
@@ -1,9 +1,13 @@
1
- import { SQSEvent } from 'aws-lambda';
1
+ import { SNSMessageAttributes, SQSEvent, SQSRecord } from 'aws-lambda';
2
+ import { Message } from '@aws-sdk/client-sqs';
2
3
  import { Service } from './services';
3
4
  export declare const newEventsProxy: (name: string, config: {
4
5
  handlers: EventHandler[];
5
- }) => Service<any>;
6
+ topic?: string;
7
+ mapper?: (message: Message) => SQSRecord;
8
+ }) => Service<void>;
6
9
  export type EventHandler = {
10
+ name: string;
7
11
  handler: (event: SQSEvent) => Promise<unknown>;
8
- matcher: (subject: string) => boolean;
12
+ matcher: (subject: string, attributes: SNSMessageAttributes) => boolean;
9
13
  };
@@ -43,28 +43,31 @@ var logging_1 = require("@yopdev/logging");
43
43
  var stoppable_1 = require("./stoppable");
44
44
  var assert_1 = require("./assert");
45
45
  var services_1 = require("./services");
46
- var newEventsProxy = function (name, config) { return new services_1.Service(new EventsProxy(name, config.handlers)); };
46
+ var newEventsProxy = function (name, config) { var _a; return new services_1.Service(new EventsProxy(name, config.handlers, (_a = config.mapper) !== null && _a !== void 0 ? _a : mappers_1.mapToLambdaSqsRecord, config.topic)); };
47
47
  exports.newEventsProxy = newEventsProxy;
48
48
  var EventsProxy = /** @class */ (function () {
49
- function EventsProxy(name, handlers) {
49
+ function EventsProxy(name, handlers, mapper, topic) {
50
50
  var _this = this;
51
51
  this.name = name;
52
52
  this.handlers = handlers;
53
+ this.mapper = mapper;
54
+ this.topic = topic;
53
55
  this.pollingFrequency = 1;
54
56
  this.stopped = true;
55
57
  this.queueName = function () { return "EventProxyQueue".concat(Buffer.from(_this.name).toString('hex').substring(0, 64)); };
56
58
  this.start = function (config) { return __awaiter(_this, void 0, void 0, function () {
57
59
  var _this = this;
58
- return __generator(this, function (_a) {
59
- return [2 /*return*/, this.queueConnectedToTopic(config.sns, config.eventsProxy.topic.arn, config.sqs, this.queueName())
60
+ var _a;
61
+ return __generator(this, function (_b) {
62
+ return [2 /*return*/, this.queueConnectedToTopic(config.sns, (_a = this.topic) !== null && _a !== void 0 ? _a : config.eventsProxy.topic.arn, config.sqs, this.queueName())
60
63
  .then(function (url) { return _this.startEventsConsumer(config.sqs.client, url); })
61
- .then(function () { return _this.stopped = false; })
62
- .then(function () { return undefined; })];
64
+ .tap(function () { return _this.stopped = false; })
65
+ .then(function (consumer) { return _this.LOGGER.debug(consumer); })];
63
66
  });
64
67
  }); };
65
68
  this.queueConnectedToTopic = function (sns, topic, sqs, queue) { return __awaiter(_this, void 0, void 0, function () {
66
69
  return __generator(this, function (_a) {
67
- return [2 /*return*/, sqs.createNamedQueue(queue)
70
+ return [2 /*return*/, sqs.createStandardQueue(queue)
68
71
  .then(function (queue) { return sns
69
72
  .createSubscription({ arn: topic }, { arn: queue.arn })
70
73
  .then(function () { return queue.url; }); })];
@@ -87,41 +90,36 @@ var EventsProxy = /** @class */ (function () {
87
90
  }); };
88
91
  this.extractErrorMessage = function (e) { return typeof e === 'string' ? e : e.message !== undefined && typeof e.message === 'string' ? e.message : ''; };
89
92
  this.onEachMessage = function (message) {
90
- var body = (0, assert_1.assertNotUndefined)(message.Body, 'body is not present');
91
- var subject = JSON.parse(body).Subject;
92
93
  if (_this.stopped) {
93
- _this.LOGGER.warn('stopped events proxy received message with subject %s', subject);
94
+ _this.LOGGER.warn('stopped events proxy received message %o', message);
94
95
  return;
95
96
  }
96
- var handlers = _this.handlers.filter(function (handler) { return handler.matcher(subject); });
97
+ var body = (0, assert_1.assertNotUndefined)(message.Body, 'body is not present');
98
+ var json = JSON.parse(body);
99
+ var attributes = json.MessageAttributes;
100
+ var subject = json.Subject;
101
+ var handlers = _this.handlers.filter(function (handler) { return handler.matcher(subject, attributes); });
97
102
  if (handlers.length === 0) {
98
- _this.LOGGER.warn('no handlers found for subject %s, message %o', subject, message);
103
+ _this.LOGGER.warn('no handlers found for message %o', message);
99
104
  }
105
+ var record = _this.mapper(message);
100
106
  handlers.forEach(function (handler) {
101
- var record = (0, mappers_1.mapToLambdaSqsRecord)(message);
102
- handler
107
+ return handler
103
108
  .handler({ Records: [record] })
104
109
  .then(function () {
105
- var _a;
106
- var json = JSON.parse(record.body);
107
- var subject = json.Subject || ((_a = json.Message) === null || _a === void 0 ? void 0 : _a.eventname) || 'unknown';
108
- _this.LOGGER.debug('subject accepted %s', subject);
110
+ _this.LOGGER.debug('handler %s accepted message', handler.name);
109
111
  })
110
112
  .catch(function (e) {
111
113
  var error = _this.extractErrorMessage(e);
112
- _this.LOGGER.error(e, 'failed %s with %s', subject, error);
114
+ _this.LOGGER.error(e, 'handler %s failed with %s', handler.name, error);
113
115
  });
114
116
  });
115
117
  };
116
- this.stop = function () { return __awaiter(_this, void 0, void 0, function () {
117
- var _this = this;
118
- return __generator(this, function (_a) {
119
- return [2 /*return*/, Promise.resolve(this.consumer)
120
- .then(function (consumer) { return consumer !== undefined ? (0, stoppable_1.stopConsumer)(consumer, _this.pollingFrequency).then(function () { return 'stopped'; }) : 'not started'; })
121
- .then(function (status) { return _this.LOGGER.info(status); })];
122
- });
123
- }); };
118
+ this.stop = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
119
+ return [2 /*return*/, (0, stoppable_1.stopConsumer)(this.pollingFrequency, this.consumer)];
120
+ }); }); };
124
121
  this.LOGGER = logging_1.LoggerFactory.create("SNS->SQS[".concat(name, "]"));
122
+ this.LOGGER.debug('handling events from %s', topic !== null && topic !== void 0 ? topic : 'the event bus');
125
123
  }
126
124
  return EventsProxy;
127
125
  }());
@@ -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<void>;
19
+ stop: () => Promise<any>;
20
20
  }
21
21
  export type HttpSettings = {
22
22
  protocol: string;
@@ -15,14 +15,13 @@ var HttpServer = /** @class */ (function () {
15
15
  this.settings = settings;
16
16
  this.handler = handler;
17
17
  this.start = function () { return _this.server
18
- .then(function () { return _this.LOGGER.info('listening on %s', (0, assert_1.assertNotUndefined)(_this.address).port); })
18
+ .then(function () { return _this.LOGGER.debug('listening [%s]', (0, assert_1.assertNotUndefined)(_this.address).port); })
19
19
  .then(function () { return _this.endpointUrl(); }); };
20
20
  this.endpointUrl = function () {
21
21
  var address = (0, assert_1.assertNotUndefined)(_this.address, 'server not started');
22
22
  return "".concat(_this.protocol, "//").concat(address.address, ":").concat(address.port, "/");
23
23
  };
24
- this.stop = function () { return _this.server.then(function (server) { return server.close(); })
25
- .then(function () { return _this.LOGGER.info('stop %s', (0, assert_1.assertNotUndefined)(_this.address).port); }); };
24
+ this.stop = function () { return _this.server.then(function (server) { return server.close(); }).then(function () { return undefined; }); };
26
25
  var server = (0, http_1.createServer)(function (req, res) {
27
26
  req.setEncoding('utf-8');
28
27
  var body = '';
@@ -1,11 +1,22 @@
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
4
  export { newSnsHttpProxy as snsHttpProxy } from './sns-http-proxy';
5
5
  export { newInternalQueue as internalQueue } from './internal-queue';
6
6
  export { newEventsProxy as eventsProxy } from './event-proxy';
7
7
  export { newScheduledTasks as scheduledTasks } from './scheduled-tasks';
8
8
  export { newPreTrafficHooks as preTrafficHooks } from './pre-traffic-hooks';
9
9
  export { newDynamoDbTable as dynamoDbTable } from './dynamodb';
10
+ export { newContainer as container } from './container';
11
+ export { newTunnel as tunnel } from './tunnel';
10
12
  export { all as allOf, oneThenOther } from './factories';
11
13
  export { lazy, promised } from './deferred';
14
+ export { Lifecycle, Service } from './services';
15
+ export { DevServerConfig } from './config';
16
+ export { Route } from './lambda-http-proxy';
17
+ export { mapToLambdaEvent as defaultApiGatewayLambdaMapper, mapToLambdaSqsRecord as defaultSqsLambdaMapper } from './mappers';
18
+ declare global {
19
+ interface Promise<T> {
20
+ tap(onfulfilled?: (value: T) => void, onrejected?: (reason: unknown) => void): Promise<T>;
21
+ }
22
+ }
package/dist/src/index.js CHANGED
@@ -1,12 +1,49 @@
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
+ };
2
38
  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;
39
+ 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.eventsProxy = exports.internalQueue = exports.snsHttpProxy = exports.UNAUTHORIZED = exports.lambdaHttpProxy = exports.httpServer = exports.DevServer = void 0;
4
40
  var dev_server_1 = require("./dev-server");
5
41
  Object.defineProperty(exports, "DevServer", { enumerable: true, get: function () { return dev_server_1.DevServer; } });
6
42
  var http_server_1 = require("./http-server");
7
43
  Object.defineProperty(exports, "httpServer", { enumerable: true, get: function () { return http_server_1.newHttpServer; } });
8
44
  var lambda_http_proxy_1 = require("./lambda-http-proxy");
9
45
  Object.defineProperty(exports, "lambdaHttpProxy", { enumerable: true, get: function () { return lambda_http_proxy_1.newLambdaHttpProxy; } });
46
+ Object.defineProperty(exports, "UNAUTHORIZED", { enumerable: true, get: function () { return lambda_http_proxy_1.UNAUTHORIZED; } });
10
47
  var sns_http_proxy_1 = require("./sns-http-proxy");
11
48
  Object.defineProperty(exports, "snsHttpProxy", { enumerable: true, get: function () { return sns_http_proxy_1.newSnsHttpProxy; } });
12
49
  var internal_queue_1 = require("./internal-queue");
@@ -19,9 +56,28 @@ var pre_traffic_hooks_1 = require("./pre-traffic-hooks");
19
56
  Object.defineProperty(exports, "preTrafficHooks", { enumerable: true, get: function () { return pre_traffic_hooks_1.newPreTrafficHooks; } });
20
57
  var dynamodb_1 = require("./dynamodb");
21
58
  Object.defineProperty(exports, "dynamoDbTable", { enumerable: true, get: function () { return dynamodb_1.newDynamoDbTable; } });
59
+ var container_1 = require("./container");
60
+ Object.defineProperty(exports, "container", { enumerable: true, get: function () { return container_1.newContainer; } });
61
+ var tunnel_1 = require("./tunnel");
62
+ Object.defineProperty(exports, "tunnel", { enumerable: true, get: function () { return tunnel_1.newTunnel; } });
22
63
  var factories_1 = require("./factories");
23
64
  Object.defineProperty(exports, "allOf", { enumerable: true, get: function () { return factories_1.all; } });
24
65
  Object.defineProperty(exports, "oneThenOther", { enumerable: true, get: function () { return factories_1.oneThenOther; } });
25
66
  var deferred_1 = require("./deferred");
26
67
  Object.defineProperty(exports, "lazy", { enumerable: true, get: function () { return deferred_1.lazy; } });
27
68
  Object.defineProperty(exports, "promised", { enumerable: true, get: function () { return deferred_1.promised; } });
69
+ var services_1 = require("./services");
70
+ Object.defineProperty(exports, "Service", { enumerable: true, get: function () { return services_1.Service; } });
71
+ var mappers_1 = require("./mappers");
72
+ Object.defineProperty(exports, "defaultApiGatewayLambdaMapper", { enumerable: true, get: function () { return mappers_1.mapToLambdaEvent; } });
73
+ Object.defineProperty(exports, "defaultSqsLambdaMapper", { enumerable: true, get: function () { return mappers_1.mapToLambdaSqsRecord; } });
74
+ Promise.prototype.tap = function (onfulfilled, onrejected) {
75
+ return __awaiter(this, void 0, void 0, function () {
76
+ var _this = this;
77
+ return __generator(this, function (_a) {
78
+ return [2 /*return*/, this.then(onfulfilled, onrejected).then(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
79
+ return [2 /*return*/, this];
80
+ }); }); })];
81
+ });
82
+ });
83
+ };
@@ -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
- index: number;
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>;
@@ -42,21 +42,26 @@ var sqs_consumer_1 = require("sqs-consumer");
42
42
  var stoppable_1 = require("./stoppable");
43
43
  var mappers_1 = require("./mappers");
44
44
  var services_1 = require("./services");
45
- var newInternalQueue = function (name, config, callback) { return new services_1.Service(new InternalQueue(name, config.visibility, config.handler, config.index), callback); };
45
+ var newInternalQueue = function (name, config, callback) {
46
+ var _a;
47
+ return new services_1.Service(new InternalQueue(name, config.visibility, config.handler, function (sqs) { return config.fifo ? sqs.createFifoQueue : sqs.createStandardQueue; }, (_a = config.mapper) !== null && _a !== void 0 ? _a : mappers_1.mapToLambdaSqsRecord), callback);
48
+ };
46
49
  exports.newInternalQueue = newInternalQueue;
47
50
  var InternalQueue = /** @class */ (function () {
48
- function InternalQueue(name, visibility, handler, index) {
51
+ function InternalQueue(name, visibility, handler, creator, mapper) {
49
52
  var _this = this;
50
53
  this.name = name;
51
54
  this.visibility = visibility;
52
55
  this.handler = handler;
56
+ this.creator = creator;
57
+ this.mapper = mapper;
53
58
  this.start = function (config) { return _this.create(config.sqs); };
54
59
  this.create = function (sqs) { return __awaiter(_this, void 0, void 0, function () {
55
60
  var _this = this;
56
61
  return __generator(this, function (_a) {
57
- return [2 /*return*/, this.createQueue(sqs, this.queueName)
62
+ return [2 /*return*/, this.createQueue(sqs, this.name)
58
63
  .then(function (queue) { return _this
59
- .createConsumer(sqs.client, _this.queueName, queue.url, _this.handler(queue), _this.visibility)
64
+ .createConsumer(sqs.client, _this.name, queue.url, _this.handler(queue), _this.visibility)
60
65
  .then(function () { return queue.url; }); })];
61
66
  });
62
67
  }); };
@@ -68,7 +73,7 @@ var InternalQueue = /** @class */ (function () {
68
73
  waitTimeSeconds: visibility,
69
74
  sqs: sqs,
70
75
  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)] })];
76
+ return [2 /*return*/, handler({ Records: [this.mapper(message)] })];
72
77
  }); }); },
73
78
  }))
74
79
  .then(function (consumer) {
@@ -89,18 +94,14 @@ var InternalQueue = /** @class */ (function () {
89
94
  .then(function () { return _this; })];
90
95
  });
91
96
  }); };
92
- this.stop = function () { return Promise.resolve(_this.state)
93
- .then(function (state) { return state !== undefined ? (0, stoppable_1.stopConsumer)(state.consumer, state.visibility).then(function () { return 'stopped'; }) : 'not started'; })
94
- .then(function (status) { return _this.LOGGER.info(status); }); };
95
- this.queueName = (index + 1000).toString().padStart(10, '0');
97
+ this.stop = function () { return (0, stoppable_1.stopConsumer)(_this.state.visibility, _this.state.consumer); };
96
98
  this.LOGGER = logging_1.LoggerFactory.create("INTERNALQUEUE[".concat(name, "]"));
97
99
  }
98
100
  InternalQueue.prototype.createQueue = function (sqs, name) {
99
101
  return __awaiter(this, void 0, void 0, function () {
100
102
  var _this = this;
101
103
  return __generator(this, function (_a) {
102
- return [2 /*return*/, sqs
103
- .createNamedQueue(name) //
104
+ return [2 /*return*/, this.creator(sqs)(name) //
104
105
  .then(function (queue) {
105
106
  _this.LOGGER.info('created: %s', queue.url);
106
107
  return queue;
@@ -1,13 +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
+ 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
  };
19
+ type Authorizer = (authorization: string) => Promise<APIGatewayEventDefaultAuthorizerContext | undefined>;
13
20
  export {};