@yopdev/dev-server 1.3.0-alpha.4 → 1.3.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,27 +1,11 @@
1
- import { SQSEvent } from 'aws-lambda';
2
- import { Lifecycle, Service, Callback } from './services';
3
- import { DevServerConfig } from './config';
1
+ import { SNSMessageAttributes, SQSEvent } from 'aws-lambda';
2
+ import { Service } from './services';
4
3
  export declare const newEventsProxy: (name: string, config: {
5
4
  handlers: EventHandler[];
6
- }, callback?: Callback<EventsProxy>) => Service<EventsProxy>;
7
- declare class EventsProxy implements Lifecycle<EventsProxy> {
8
- readonly name: string;
9
- private readonly handlers;
10
- private LOGGER;
11
- private consumer;
12
- private readonly pollingFrequency;
13
- private readonly queue;
14
- private stopped;
15
- constructor(name: string, handlers: EventHandler[]);
16
- start: (config: DevServerConfig) => Promise<this>;
17
- private queueConnectedToTopic;
18
- private startEventsConsumer;
19
- private extractErrorMessage;
20
- private onEachMessage;
21
- stop: () => Promise<void>;
22
- }
5
+ topic?: string;
6
+ }) => Service<void>;
23
7
  export type EventHandler = {
8
+ name: string;
24
9
  handler: (event: SQSEvent) => Promise<unknown>;
25
- matcher: (subject: string) => boolean;
10
+ matcher: (subject: string, attributes: SNSMessageAttributes) => boolean;
26
11
  };
27
- export {};
@@ -43,22 +43,25 @@ 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, callback) { return new services_1.Service(new EventsProxy(name, config.handlers), callback); };
46
+ var newEventsProxy = function (name, config) { return new services_1.Service(new EventsProxy(name, config.handlers, config.topic)); };
47
47
  exports.newEventsProxy = newEventsProxy;
48
48
  var EventsProxy = /** @class */ (function () {
49
- function EventsProxy(name, handlers) {
49
+ function EventsProxy(name, handlers, topic) {
50
50
  var _this = this;
51
51
  this.name = name;
52
52
  this.handlers = handlers;
53
+ this.topic = topic;
53
54
  this.pollingFrequency = 1;
54
55
  this.stopped = true;
56
+ this.queueName = function () { return "EventProxyQueue".concat(Buffer.from(_this.name).toString('hex').substring(0, 64)); };
55
57
  this.start = function (config) { return __awaiter(_this, void 0, void 0, function () {
56
58
  var _this = this;
57
- return __generator(this, function (_a) {
58
- return [2 /*return*/, this.queueConnectedToTopic(config.sns, config.eventsProxy.topic.arn, config.sqs, this.queue)
59
+ var _a;
60
+ return __generator(this, function (_b) {
61
+ return [2 /*return*/, this.queueConnectedToTopic(config.sns, (_a = this.topic) !== null && _a !== void 0 ? _a : config.eventsProxy.topic.arn, config.sqs, this.queueName())
59
62
  .then(function (url) { return _this.startEventsConsumer(config.sqs.client, url); })
60
- .then(function () { return _this.stopped = false; })
61
- .then(function () { return _this; })];
63
+ .tap(function () { return _this.stopped = false; })
64
+ .then(function (consumer) { return _this.LOGGER.debug(consumer); })];
62
65
  });
63
66
  }); };
64
67
  this.queueConnectedToTopic = function (sns, topic, sqs, queue) { return __awaiter(_this, void 0, void 0, function () {
@@ -86,43 +89,36 @@ var EventsProxy = /** @class */ (function () {
86
89
  }); };
87
90
  this.extractErrorMessage = function (e) { return typeof e === 'string' ? e : e.message !== undefined && typeof e.message === 'string' ? e.message : ''; };
88
91
  this.onEachMessage = function (message) {
89
- var body = (0, assert_1.assertNotUndefined)(message.Body, 'body is not present');
90
- var subject = JSON.parse(body).Subject;
91
92
  if (_this.stopped) {
92
- _this.LOGGER.warn('stopped events proxy received message with subject %s', subject);
93
+ _this.LOGGER.warn('stopped events proxy received message %o', message);
93
94
  return;
94
95
  }
95
- var handlers = _this.handlers.filter(function (handler) { return handler.matcher(subject); });
96
+ var body = (0, assert_1.assertNotUndefined)(message.Body, 'body is not present');
97
+ var json = JSON.parse(body);
98
+ var attributes = json.MessageAttributes;
99
+ var subject = json.Subject;
100
+ var handlers = _this.handlers.filter(function (handler) { return handler.matcher(subject, attributes); });
96
101
  if (handlers.length === 0) {
97
- _this.LOGGER.warn('no handlers found for subject %s, message %o', subject, message);
102
+ _this.LOGGER.warn('no handlers found for message %o', message);
98
103
  }
104
+ var record = (0, mappers_1.mapToLambdaSqsRecord)(message);
99
105
  handlers.forEach(function (handler) {
100
- var record = (0, mappers_1.mapToLambdaSqsRecord)(message);
101
- handler
106
+ return handler
102
107
  .handler({ Records: [record] })
103
108
  .then(function () {
104
- var _a;
105
- var json = JSON.parse(record.body);
106
- var subject = json.Subject || ((_a = json.Message) === null || _a === void 0 ? void 0 : _a.eventname) || 'unknown';
107
- _this.LOGGER.debug('subject accepted %s', subject);
109
+ _this.LOGGER.debug('handler %s accepted message', handler.name);
108
110
  })
109
111
  .catch(function (e) {
110
112
  var error = _this.extractErrorMessage(e);
111
- _this.LOGGER.error(e, 'failed %s with %s', subject, error);
113
+ _this.LOGGER.error(e, 'handler %s failed with %s', handler.name, error);
112
114
  });
113
115
  });
114
116
  };
115
- this.stop = function () { return __awaiter(_this, void 0, void 0, function () {
116
- var _this = this;
117
- return __generator(this, function (_a) {
118
- return [2 /*return*/, Promise.resolve(this.consumer)
119
- .then(function (consumer) { return consumer !== undefined ? (0, stoppable_1.stopConsumer)(consumer, _this.pollingFrequency).then(function () { return 'stopped'; }) : 'not started'; })
120
- .then(function (status) { return _this.LOGGER.info(status); })];
121
- });
122
- }); };
117
+ this.stop = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
118
+ return [2 /*return*/, (0, stoppable_1.stopConsumer)(this.pollingFrequency, this.consumer)];
119
+ }); }); };
123
120
  this.LOGGER = logging_1.LoggerFactory.create("SNS->SQS[".concat(name, "]"));
124
- var encodedName = Buffer.from(name).toString('hex').substring(0, 64);
125
- this.queue = "EventProxyQueue".concat(encodedName);
121
+ this.LOGGER.debug('handling events from %s', topic !== null && topic !== void 0 ? topic : 'the event bus');
126
122
  }
127
123
  return EventsProxy;
128
124
  }());
@@ -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 = '';
@@ -7,5 +7,15 @@ 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
+ declare global {
18
+ interface Promise<T> {
19
+ tap(onfulfilled?: (value: T) => void, onrejected?: (reason: unknown) => void): Promise<T>;
20
+ }
21
+ }
package/dist/src/index.js CHANGED
@@ -1,6 +1,42 @@
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.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.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");
@@ -19,9 +55,25 @@ var pre_traffic_hooks_1 = require("./pre-traffic-hooks");
19
55
  Object.defineProperty(exports, "preTrafficHooks", { enumerable: true, get: function () { return pre_traffic_hooks_1.newPreTrafficHooks; } });
20
56
  var dynamodb_1 = require("./dynamodb");
21
57
  Object.defineProperty(exports, "dynamoDbTable", { enumerable: true, get: function () { return dynamodb_1.newDynamoDbTable; } });
58
+ var container_1 = require("./container");
59
+ Object.defineProperty(exports, "container", { enumerable: true, get: function () { return container_1.newContainer; } });
60
+ var tunnel_1 = require("./tunnel");
61
+ Object.defineProperty(exports, "tunnel", { enumerable: true, get: function () { return tunnel_1.newTunnel; } });
22
62
  var factories_1 = require("./factories");
23
63
  Object.defineProperty(exports, "allOf", { enumerable: true, get: function () { return factories_1.all; } });
24
64
  Object.defineProperty(exports, "oneThenOther", { enumerable: true, get: function () { return factories_1.oneThenOther; } });
25
65
  var deferred_1 = require("./deferred");
26
66
  Object.defineProperty(exports, "lazy", { enumerable: true, get: function () { return deferred_1.lazy; } });
27
67
  Object.defineProperty(exports, "promised", { enumerable: true, get: function () { return deferred_1.promised; } });
68
+ var services_1 = require("./services");
69
+ Object.defineProperty(exports, "Service", { enumerable: true, get: function () { return services_1.Service; } });
70
+ Promise.prototype.tap = function (onfulfilled, onrejected) {
71
+ return __awaiter(this, void 0, void 0, function () {
72
+ var _this = this;
73
+ return __generator(this, function (_a) {
74
+ return [2 /*return*/, this.then(onfulfilled, onrejected).then(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
75
+ return [2 /*return*/, this];
76
+ }); }); })];
77
+ });
78
+ });
79
+ };
@@ -1,7 +1,8 @@
1
+ import { Queue } from "./sqs";
1
2
  import { SQSEvent } from "aws-lambda";
2
3
  import { Service, Callback } from "./services";
3
4
  export declare const newInternalQueue: (name: string, config: {
4
5
  index: number;
5
6
  visibility: number;
6
- handler: (e: SQSEvent) => Promise<void>;
7
+ handler: (queue: Queue) => (e: SQSEvent) => Promise<void>;
7
8
  }, callback?: Callback<string>) => Service<string>;
@@ -56,7 +56,7 @@ var InternalQueue = /** @class */ (function () {
56
56
  return __generator(this, function (_a) {
57
57
  return [2 /*return*/, this.createQueue(sqs, this.queueName)
58
58
  .then(function (queue) { return _this
59
- .createConsumer(sqs.client, _this.queueName, queue.url, _this.handler, _this.visibility)
59
+ .createConsumer(sqs.client, _this.queueName, queue.url, _this.handler(queue), _this.visibility)
60
60
  .then(function () { return queue.url; }); })];
61
61
  });
62
62
  }); };
@@ -89,9 +89,7 @@ var InternalQueue = /** @class */ (function () {
89
89
  .then(function () { return _this; })];
90
90
  });
91
91
  }); };
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); }); };
92
+ this.stop = function () { return (0, stoppable_1.stopConsumer)(_this.state.visibility, _this.state.consumer); };
95
93
  this.queueName = (index + 1000).toString().padStart(10, '0');
96
94
  this.LOGGER = logging_1.LoggerFactory.create("INTERNALQUEUE[".concat(name, "]"));
97
95
  }
@@ -5,9 +5,9 @@ export declare const newLambdaHttpProxy: (name: string, config: {
5
5
  settings: HttpSettings;
6
6
  routes: Route[];
7
7
  }, callback?: Callback<string>) => Service<string>;
8
- type Route = {
8
+ export type Route = {
9
9
  method: RegExp;
10
10
  path: RegExp;
11
+ weight: number;
11
12
  handler: (event: APIGatewayProxyEvent) => Promise<APIGatewayProxyResult>;
12
13
  };
13
- export {};
@@ -50,8 +50,7 @@ var LambdaHttpProxy = /** @class */ (function () {
50
50
  this.name = name;
51
51
  this.routes = routes;
52
52
  this.start = function () { return _this.server.start(); };
53
- this.stop = function () { return _this.server.stop()
54
- .then(function () { return _this.LOGGER.info('stopped'); }); };
53
+ this.stop = function () { return _this.server.stop(); };
55
54
  this.handler = function (lambdaHandler) {
56
55
  return function (request, body, response) {
57
56
  return lambdaHandler((0, mappers_1.mapToLambdaEvent)(request, body))
@@ -76,12 +75,13 @@ var LambdaHttpProxy = /** @class */ (function () {
76
75
  var _a;
77
76
  return ((_a = _this.routes.filter(function (r) { return request.method.match(r.method); })
78
77
  .filter(function (r) { return request.url.match(r.path); })
79
- .sort(function (r1, r2) { return r2.path.source.length - r1.path.source.length; })
78
+ .sort(function (r1, r2) { return r2.weight - r1.weight; })
80
79
  .map(function (r) { return _this.handler(r.handler); })
81
80
  .find(function () { return true; })) !== null && _a !== void 0 ? _a : _this.fallback)(request, body, response);
82
81
  };
83
82
  this.server = new http_server_1.HttpServer(name, settings, this.resolveRoute);
84
83
  this.LOGGER = logging_1.LoggerFactory.create("HTTP->LAMBDA[".concat(this.name, "]"));
84
+ this.LOGGER.info('registered %i routes', routes.length);
85
85
  }
86
86
  return LambdaHttpProxy;
87
87
  }());
@@ -57,12 +57,13 @@ var LocalStack = /** @class */ (function () {
57
57
  accessKeyId: 'dummy',
58
58
  secretAccessKey: 'dummy'
59
59
  }
60
- }); })];
60
+ }); })
61
+ .tap(function () { return LOGGER.debug('started'); })];
61
62
  });
62
63
  }); };
63
64
  this.stop = function () { return Promise.resolve(_this.started)
64
65
  .then(function (container) { return container ? container.stop().then(function () { return 'stopped'; }) : 'not started'; })
65
- .then(function (status) { return LOGGER.info(status); }); };
66
+ .then(function (status) { return LOGGER.debug(status); }); };
66
67
  var concreteConfig = this.configOrDefaults(config);
67
68
  var container = new testcontainers_1.GenericContainer(concreteConfig.localStackDockerImage);
68
69
  var localStatePath = concreteConfig.localStateBindMount;
@@ -83,7 +84,6 @@ var LocalStack = /** @class */ (function () {
83
84
  .start()
84
85
  .then(function (container) { return _this.started = container; })
85
86
  .then(function (container) { return "".concat(PROTOCOL, "://").concat(container.getHost(), ":").concat(container.getMappedPort(SERVICES_PORT)); }); };
86
- LOGGER.debug('initialized');
87
87
  }
88
88
  LocalStack.prototype.configOrDefaults = function (config) {
89
89
  var selectedServicesPort = config === null || config === void 0 ? void 0 : config.boundServicesPort;
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  Object.defineProperty(exports, "__esModule", { value: true });
3
14
  exports.mapToLambdaEvent = exports.mapToLambdaSqsRecord = void 0;
4
15
  var logging_1 = require("@yopdev/logging");
@@ -6,7 +17,7 @@ var url_1 = require("url");
6
17
  var LOGGER = logging_1.LoggerFactory.create('MAPPERS');
7
18
  var mapToLambdaSqsRecord = function (message) {
8
19
  if (!message.Body)
9
- throw new Error("message Body must be present: ".concat(message.Body));
20
+ throw new Error('message Body must be present');
10
21
  return {
11
22
  messageId: 'N/A',
12
23
  receiptHandle: 'N/A',
@@ -34,9 +45,11 @@ function mapToLambdaEvent(req, requestBody) {
34
45
  var claims = extractClaims(req);
35
46
  var headers = {};
36
47
  if (req.headers['content-type'])
37
- headers['Content-Type'] = req.headers['content-type'];
48
+ headers['content-type'] = req.headers['content-type'];
38
49
  if (req.headers['accept'])
39
- headers['Accept'] = req.headers['accept'];
50
+ headers['accept'] = req.headers['accept'];
51
+ if (req.headers['authorization'])
52
+ headers['authorization'] = req.headers['authorization'];
40
53
  return {
41
54
  httpMethod: req.method,
42
55
  body: requestBody,
@@ -52,7 +65,7 @@ function mapToLambdaEvent(req, requestBody) {
52
65
  requestContext: {
53
66
  accountId: '',
54
67
  apiId: '',
55
- authorizer: { claims: claims },
68
+ authorizer: __assign({ jwt: { claims: claims } }, claims),
56
69
  httpMethod: '',
57
70
  identity: {
58
71
  accessKey: '',
@@ -51,18 +51,14 @@ var PreTrafficHooks = /** @class */ (function () {
51
51
  return __generator(this, function (_a) {
52
52
  return [2 /*return*/, Promise
53
53
  .all(this.hooks())
54
- .then(function () { return _this.LOGGER.info('started'); })
55
54
  .then(function () { return _this; })];
56
55
  });
57
56
  }); };
58
- this.stop = function () { return __awaiter(_this, void 0, void 0, function () {
59
- var _this = this;
60
- return __generator(this, function (_a) {
61
- return [2 /*return*/, Promise.resolve()
62
- .then(function () { return _this.LOGGER.info('stopped'); })];
63
- });
64
- }); };
57
+ this.stop = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
58
+ return [2 /*return*/, Promise.resolve()];
59
+ }); }); };
65
60
  this.LOGGER = logging_1.LoggerFactory.create("PRETRAFFIC[".concat(name, "]"));
61
+ this.LOGGER.info('%i hooks registered', hooks.length);
66
62
  }
67
63
  return PreTrafficHooks;
68
64
  }());
@@ -0,0 +1,7 @@
1
+ import { S3Client } from '@aws-sdk/client-s3';
2
+ import { AwsConfig } from './config';
3
+ export declare class S3 {
4
+ readonly client: S3Client;
5
+ constructor(aws: AwsConfig);
6
+ createBucket: (name: string) => Promise<import("@aws-sdk/client-s3").CreateBucketCommandOutput>;
7
+ }
package/dist/src/s3.js ADDED
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.S3 = void 0;
51
+ var client_s3_1 = require("@aws-sdk/client-s3");
52
+ var logging_1 = require("@yopdev/logging");
53
+ var LOGGER = logging_1.LoggerFactory.create('S3');
54
+ var S3 = /** @class */ (function () {
55
+ function S3(aws) {
56
+ var _this = this;
57
+ this.createBucket = function (name) { return __awaiter(_this, void 0, void 0, function () {
58
+ return __generator(this, function (_a) {
59
+ return [2 /*return*/, this.client.send(new client_s3_1.CreateBucketCommand({
60
+ Bucket: name,
61
+ }))
62
+ .tap(function () { return LOGGER.debug('bucket %s created', name); })];
63
+ });
64
+ }); };
65
+ this.client = new client_s3_1.S3Client(__assign({ forcePathStyle: true }, aws));
66
+ }
67
+ return S3;
68
+ }());
69
+ exports.S3 = S3;
@@ -1,17 +1,6 @@
1
- /// <reference types="node" />
2
- import { Lifecycle, Service, Callback } from './services';
3
- export declare const newScheduledTasks: (name: string, schedules: Rate[], callback?: Callback<ScheduledTasks>) => Service<ScheduledTasks>;
4
- declare class ScheduledTasks implements Lifecycle<ScheduledTasks> {
5
- readonly name: string;
6
- private readonly schedules;
7
- private LOGGER;
8
- constructor(name: string, schedules: Rate[]);
9
- intervals: NodeJS.Timeout[];
10
- start: () => Promise<this>;
11
- stop: () => Promise<void>;
12
- }
1
+ import { Service } from './services';
2
+ export declare const newScheduledTasks: (name: string, schedules: Rate[]) => Service<any>;
13
3
  export type Rate = {
14
4
  frequency: number;
15
5
  task: () => Promise<unknown>;
16
6
  };
17
- export {};
@@ -40,7 +40,7 @@ exports.newScheduledTasks = void 0;
40
40
  var logging_1 = require("@yopdev/logging");
41
41
  var timers_1 = require("timers");
42
42
  var services_1 = require("./services");
43
- var newScheduledTasks = function (name, schedules, callback) { return new services_1.Service(new ScheduledTasks(name, schedules), callback); };
43
+ var newScheduledTasks = function (name, schedules) { return new services_1.Service(new ScheduledTasks(name, schedules)); };
44
44
  exports.newScheduledTasks = newScheduledTasks;
45
45
  var ScheduledTasks = /** @class */ (function () {
46
46
  function ScheduledTasks(name, schedules) {
@@ -52,18 +52,14 @@ var ScheduledTasks = /** @class */ (function () {
52
52
  var _this = this;
53
53
  return __generator(this, function (_a) {
54
54
  return [2 /*return*/, Promise.resolve(this.schedules.forEach(function (schedule) { return _this.intervals.push(setInterval(schedule.task, schedule.frequency * 1000)); }))
55
- .then(function () { return _this.LOGGER.info('started'); })
56
- .then(function () { return _this; })];
57
- });
58
- }); };
59
- this.stop = function () { return __awaiter(_this, void 0, void 0, function () {
60
- var _this = this;
61
- return __generator(this, function (_a) {
62
- return [2 /*return*/, Promise.resolve(this.intervals.forEach(function (interval) { return (0, timers_1.clearInterval)(interval); }))
63
- .then(function () { return _this.LOGGER.info('stopped'); })];
55
+ .then(function () { return undefined; })];
64
56
  });
65
57
  }); };
58
+ this.stop = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
59
+ return [2 /*return*/, Promise.resolve(this.intervals.forEach(function (interval) { return (0, timers_1.clearInterval)(interval); }))];
60
+ }); }); };
66
61
  this.LOGGER = logging_1.LoggerFactory.create("SCHEDULER[".concat(name, "]"));
62
+ this.LOGGER.info('registered %i scheduled tasks', schedules.length);
67
63
  }
68
64
  return ScheduledTasks;
69
65
  }());
@@ -5,17 +5,19 @@ import { DevServerConfig } from "./config";
5
5
  export declare const newSnsHttpProxy: (name: string, config: {
6
6
  settings: HttpSettings;
7
7
  method: string;
8
- subject: string | undefined;
8
+ subject?: string;
9
+ topic?: string;
9
10
  }, callback?: Callback<string>) => Service<string>;
10
11
  export declare class SnsHttpProxy implements Lifecycle<string> {
11
12
  readonly name: string;
12
13
  private readonly settings;
13
14
  private readonly method;
14
15
  private readonly subject?;
16
+ private readonly topic?;
15
17
  private LOGGER;
16
18
  private server;
17
19
  private config;
18
- constructor(name: string, settings: HttpSettings, method: string, subject?: string);
20
+ constructor(name: string, settings: HttpSettings, method: string, subject?: string, topic?: string);
19
21
  start: (config: DevServerConfig) => Promise<string>;
20
22
  stop: () => Promise<void>;
21
23
  handler: (method: string, subject?: string) => (request: IncomingMessage, body: string, response: ServerResponse) => Promise<void>;
@@ -17,33 +17,34 @@ var http_server_1 = require("./http-server");
17
17
  var responses_1 = require("./responses");
18
18
  var services_1 = require("./services");
19
19
  var assert_1 = require("./assert");
20
- var newSnsHttpProxy = function (name, config, callback) { return new services_1.Service(new SnsHttpProxy(name, config.settings, config.method, config.subject), callback); };
20
+ var newSnsHttpProxy = function (name, config, callback) { return new services_1.Service(new SnsHttpProxy(name, config.settings, config.method, config.subject, config.topic), callback); };
21
21
  exports.newSnsHttpProxy = newSnsHttpProxy;
22
22
  var SnsHttpProxy = /** @class */ (function () {
23
- function SnsHttpProxy(name, settings, method, subject) {
23
+ function SnsHttpProxy(name, settings, method, subject, topic) {
24
24
  var _this = this;
25
25
  this.name = name;
26
26
  this.settings = settings;
27
27
  this.method = method;
28
28
  this.subject = subject;
29
+ this.topic = topic;
29
30
  this.start = function (config) { return Promise.resolve(_this.server)
30
31
  .then(function (server) { return server.start(); })
31
- .then(function (endpoint) {
32
+ .tap(function () {
33
+ var _a;
32
34
  _this.config = {
33
- topic: config.eventsProxy.topic,
35
+ topic: (_a = _this.topic) !== null && _a !== void 0 ? _a : config.eventsProxy.topic.arn,
34
36
  sns: config.sns
35
37
  };
36
- return endpoint;
37
38
  })
38
39
  .then(function (endpoint) { return endpoint; }); };
39
40
  this.stop = function () { return _this.onlyWhenStarted(_this.server).stop()
40
- .then(function () { return _this.LOGGER.info('stopped'); }); };
41
+ .then(function () { return _this.LOGGER.debug('stopped'); }); };
41
42
  this.handler = function (method, subject) {
42
43
  return function (request, body, response) {
43
44
  return Promise.resolve(((0, assert_1.assertNotUndefined)(_this.config)))
44
45
  .then(function (config) {
45
46
  return config.sns
46
- .publish(config.topic, _this.extractMessage(method, request, body), subject, _this.extractMessageAttributes(request))
47
+ .publish({ arn: config.topic }, _this.extractMessage(method, request, body), subject, _this.extractMessageAttributes(request))
47
48
  .then(function () { return (0, responses_1.writeResponse)(response, 200, ''); })
48
49
  .catch(function (e) {
49
50
  _this.LOGGER.error(e, 'request failed to execute');
@@ -76,6 +77,7 @@ var SnsHttpProxy = /** @class */ (function () {
76
77
  };
77
78
  this.server = new http_server_1.HttpServer(this.name, this.settings, this.handler(this.method, this.subject));
78
79
  this.LOGGER = logging_1.LoggerFactory.create("HTTP->SNS[".concat(name, "]"));
80
+ this.LOGGER.info('forwarding %s %s events to %s', subject ? "events with ".concat(subject, " subject") : 'all', method, topic !== null && topic !== void 0 ? topic : 'the event bus');
79
81
  }
80
82
  return SnsHttpProxy;
81
83
  }());
package/dist/src/sns.js CHANGED
@@ -50,7 +50,8 @@ var Sns = /** @class */ (function () {
50
50
  .send(new client_sns_1.CreateTopicCommand({
51
51
  Name: topic,
52
52
  }))
53
- .then(function (o) { return (0, assert_1.assertNotUndefined)(o.TopicArn, 'TopicArn is not present'); })];
53
+ .then(function (o) { return (0, assert_1.assertNotUndefined)(o.TopicArn, 'TopicArn is not present'); })
54
+ .tap(function (arn) { return LOGGER.debug('topic %o created', arn); })];
54
55
  });
55
56
  }); };
56
57
  this.createSubscription = function (topic, queue) { return __awaiter(_this, void 0, void 0, function () {
@@ -61,7 +62,7 @@ var Sns = /** @class */ (function () {
61
62
  Protocol: 'sqs',
62
63
  Endpoint: queue.arn,
63
64
  }))
64
- .then(function () { return LOGGER.debug('connected %s to %s', topic.arn, queue.arn); })];
65
+ .then(function () { return LOGGER.debug('subscription %s->%s created', topic.arn, queue.arn); })];
65
66
  });
66
67
  }); };
67
68
  this.publish = function (topic, body, subject, attributes) { return __awaiter(_this, void 0, void 0, function () {
@@ -73,12 +74,11 @@ var Sns = /** @class */ (function () {
73
74
  MessageAttributes: attributes,
74
75
  Subject: subject,
75
76
  }))
76
- .then(function () { return LOGGER.debug('published with subject %s to %s', subject, topic.arn); })
77
+ .then(function () { return LOGGER.trace('published %s[%s]', topic.arn, subject); })
77
78
  .then()];
78
79
  });
79
80
  }); };
80
81
  this.client = new client_sns_1.SNSClient(aws);
81
- LOGGER.debug('initialized');
82
82
  }
83
83
  return Sns;
84
84
  }());
package/dist/src/sqs.d.ts CHANGED
@@ -3,8 +3,9 @@ import { AwsConfig } from "./config";
3
3
  export declare class Sqs {
4
4
  readonly client: SQSClient;
5
5
  constructor(config: AwsConfig);
6
- createNamedQueue: (name: string) => Promise<{
7
- url: string;
8
- arn: string;
9
- }>;
6
+ createNamedQueue: (name: string) => Promise<Queue>;
10
7
  }
8
+ export type Queue = {
9
+ arn: string;
10
+ url: string;
11
+ };
package/dist/src/sqs.js CHANGED
@@ -60,11 +60,11 @@ var Sqs = /** @class */ (function () {
60
60
  .then(function (attributes) { return ({
61
61
  url: (0, assert_1.assertNotUndefined)(create.QueueUrl),
62
62
  arn: (0, assert_1.assertNotUndefined)(attributes.Attributes)[QUEUE_NAME_ATTRIBUTE_NAME],
63
- }); }); })];
63
+ }); }); })
64
+ .tap(function (queue) { return LOGGER.debug('created %s', queue.arn); })];
64
65
  });
65
66
  }); };
66
67
  this.client = new client_sqs_1.SQSClient(config);
67
- LOGGER.debug('initialized');
68
68
  }
69
69
  return Sqs;
70
70
  }());
@@ -1,2 +1,2 @@
1
1
  import { Consumer } from "sqs-consumer";
2
- export declare const stopConsumer: (target: Consumer, queueWaitTimeSecs: number) => Promise<void>;
2
+ export declare const stopConsumer: (queueWaitTimeSecs: number, target?: Consumer) => Promise<void>;
@@ -37,12 +37,18 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.stopConsumer = void 0;
40
- var stopConsumer = function (target, queueWaitTimeSecs) { return __awaiter(void 0, void 0, void 0, function () {
40
+ var stopConsumer = function (queueWaitTimeSecs, target) { return __awaiter(void 0, void 0, void 0, function () {
41
41
  return __generator(this, function (_a) {
42
- target.stop();
43
- return [2 /*return*/, new Promise(function (resolve) {
44
- setTimeout(resolve, queueWaitTimeSecs * 1000 + 1);
45
- })];
42
+ if (target) {
43
+ target.stop();
44
+ return [2 /*return*/, new Promise(function (resolve) {
45
+ setTimeout(resolve, queueWaitTimeSecs * 1000 + 1);
46
+ })];
47
+ }
48
+ else {
49
+ return [2 /*return*/, Promise.resolve()];
50
+ }
51
+ return [2 /*return*/];
46
52
  });
47
53
  }); };
48
54
  exports.stopConsumer = stopConsumer;
@@ -0,0 +1,10 @@
1
+ import { Service } from './services';
2
+ export declare const terminate: () => Promise<void>;
3
+ export declare const newTunnel: (config: {
4
+ name: string;
5
+ port: number;
6
+ fixed?: {
7
+ name: string;
8
+ token: string;
9
+ };
10
+ }) => Service<string>;
@@ -0,0 +1,79 @@
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.newTunnel = exports.terminate = void 0;
40
+ var services_1 = require("./services");
41
+ var logging_1 = require("@yopdev/logging");
42
+ var ngrok = require("@ngrok/ngrok");
43
+ var assert_1 = require("./assert");
44
+ var terminate = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
45
+ return [2 /*return*/, ngrok.kill()];
46
+ }); }); };
47
+ exports.terminate = terminate;
48
+ var newTunnel = function (config) {
49
+ return new services_1.Service(new Tunnel(config.name, config.port, config.fixed));
50
+ };
51
+ exports.newTunnel = newTunnel;
52
+ var Tunnel = /** @class */ (function () {
53
+ function Tunnel(name, port, domain) {
54
+ var _this = this;
55
+ this.name = name;
56
+ this.port = port;
57
+ this.domain = domain;
58
+ this.start = function () { return __awaiter(_this, void 0, void 0, function () {
59
+ var _this = this;
60
+ var _a, _b;
61
+ return __generator(this, function (_c) {
62
+ return [2 /*return*/, ngrok
63
+ .forward({
64
+ proto: 'http',
65
+ domain: (_a = this.domain) === null || _a === void 0 ? void 0 : _a.name,
66
+ addr: this.port,
67
+ authtoken: (_b = this.domain) === null || _b === void 0 ? void 0 : _b.token,
68
+ })
69
+ .then(function (listener) { return (0, assert_1.assertNotUndefined)(listener.url()); })
70
+ .tap(function (url) { return _this.logger.debug(url); })];
71
+ });
72
+ }); };
73
+ this.stop = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
74
+ return [2 /*return*/, ngrok.disconnect()];
75
+ }); }); };
76
+ this.logger = logging_1.LoggerFactory.create("TUNNEL[".concat(name, "]"));
77
+ }
78
+ return Tunnel;
79
+ }());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yopdev/dev-server",
3
- "version": "1.3.0-alpha.4",
3
+ "version": "1.3.0",
4
4
  "scripts": {
5
5
  "compile": "tsc",
6
6
  "pretest": "npm run compile",
@@ -18,15 +18,17 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@aws-sdk/client-dynamodb": "^3.451.0",
21
+ "@aws-sdk/client-s3": "^3.451.0",
21
22
  "@aws-sdk/client-sns": "^3.451.0",
22
23
  "@aws-sdk/client-sqs": "^3.451.0",
24
+ "@ngrok/ngrok": "^0.9.1",
23
25
  "@yopdev/logging": "^0.0.2",
24
26
  "sqs-consumer": "^7.4.0",
25
27
  "testcontainers": "^10.2.2"
26
28
  },
27
29
  "repository": {
28
30
  "type": "git",
29
- "url": "github:blockademy/devserver"
31
+ "url": "git+https://github.com/blockademy/devserver.git"
30
32
  },
31
33
  "main": "dist/src/index.js",
32
34
  "types": "dist/src/index.d.ts",