@yopdev/dev-server 1.3.0-alpha.2 → 1.3.0-alpha.4

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.
@@ -21,4 +21,9 @@ export type DevServerConfig = {
21
21
  sqs: Sqs;
22
22
  sns: Sns;
23
23
  dynamo: DynamoDb;
24
+ eventsProxy: {
25
+ topic: {
26
+ arn: string;
27
+ };
28
+ };
24
29
  };
@@ -4,6 +4,7 @@ export declare class DevServer implements Lifecycle<DevServer> {
4
4
  readonly name: string;
5
5
  private readonly service;
6
6
  private readonly config?;
7
+ private readonly eventProxyTopic;
7
8
  private LOGGER;
8
9
  private localStack;
9
10
  constructor(name: string, service: Service<any>, config?: Config);
@@ -23,13 +23,22 @@ var DevServer = /** @class */ (function () {
23
23
  aws: config,
24
24
  network: infra.network
25
25
  }); }); })
26
- .then(function (infra) { return ({
26
+ .then(function (infra) { return Promise
27
+ .resolve(new sns_1.Sns(infra.aws))
28
+ .then(function (sns) { return sns
29
+ .createTopic(_this.eventProxyTopic)
30
+ .then(function (eventProxyTopicArn) { return ({
27
31
  raw: infra.aws,
28
32
  network: infra.network,
29
33
  sqs: new sqs_1.Sqs(infra.aws),
30
34
  sns: new sns_1.Sns(infra.aws),
31
35
  dynamo: new dynamodb_1.DynamoDb(infra.aws),
32
- }); })
36
+ eventsProxy: {
37
+ topic: {
38
+ arn: eventProxyTopicArn
39
+ }
40
+ },
41
+ }); }); }); })
33
42
  .then(function (config) { _this.LOGGER.debug('starting. config is %o', config); return config; })
34
43
  .then(function (aws) { return _this.service.start(aws); })
35
44
  .then(function () { return _this.LOGGER.info('started'); })
@@ -39,6 +48,8 @@ var DevServer = /** @class */ (function () {
39
48
  .then(function () { var _a; return (_a = _this.localStack) === null || _a === void 0 ? void 0 : _a.stop(); })
40
49
  .then(function () { return _this.LOGGER.info('stopped'); }); };
41
50
  this.LOGGER = logging_1.LoggerFactory.create("DEVSERVER[".concat(name, "]"));
51
+ var encodedName = Buffer.from(name).toString('hex').substring(0, 64);
52
+ this.eventProxyTopic = "EventProxyTopic".concat(encodedName);
42
53
  }
43
54
  return DevServer;
44
55
  }());
@@ -1,17 +1,27 @@
1
1
  import { SQSEvent } from 'aws-lambda';
2
- import { Service, Callback } from './services';
2
+ import { Lifecycle, Service, Callback } from './services';
3
+ import { DevServerConfig } from './config';
3
4
  export declare const newEventsProxy: (name: string, config: {
4
5
  handlers: EventHandler[];
5
- topic?: string;
6
- queue?: string;
7
- }, callback?: Callback<{
8
- arn: string;
9
- name: string;
10
- }>) => Service<{
11
- arn: string;
12
- name: string;
13
- }>;
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
+ }
14
23
  export type EventHandler = {
15
24
  handler: (event: SQSEvent) => Promise<unknown>;
16
25
  matcher: (subject: string) => boolean;
17
26
  };
27
+ export {};
@@ -46,7 +46,7 @@ var services_1 = require("./services");
46
46
  var newEventsProxy = function (name, config, callback) { return new services_1.Service(new EventsProxy(name, config.handlers), callback); };
47
47
  exports.newEventsProxy = newEventsProxy;
48
48
  var EventsProxy = /** @class */ (function () {
49
- function EventsProxy(name, handlers, topic, queue) {
49
+ function EventsProxy(name, handlers) {
50
50
  var _this = this;
51
51
  this.name = name;
52
52
  this.handlers = handlers;
@@ -55,24 +55,18 @@ var EventsProxy = /** @class */ (function () {
55
55
  this.start = function (config) { return __awaiter(_this, void 0, void 0, function () {
56
56
  var _this = this;
57
57
  return __generator(this, function (_a) {
58
- return [2 /*return*/, this.queueConnectedToTopic(config.sns, this.topic, config.sqs, this.queue)
59
- .then(function (_a) {
60
- var topic = _a[0], queue = _a[1];
61
- return _this.startEventsConsumer(config.sqs.client, queue.url)
62
- .then(function () { return _this.LOGGER.info('started'); })
63
- .then(function () { return _this.stopped = false; })
64
- .then(function () { return topic; });
65
- })];
58
+ return [2 /*return*/, this.queueConnectedToTopic(config.sns, config.eventsProxy.topic.arn, config.sqs, this.queue)
59
+ .then(function (url) { return _this.startEventsConsumer(config.sqs.client, url); })
60
+ .then(function () { return _this.stopped = false; })
61
+ .then(function () { return _this; })];
66
62
  });
67
63
  }); };
68
64
  this.queueConnectedToTopic = function (sns, topic, sqs, queue) { return __awaiter(_this, void 0, void 0, function () {
69
65
  return __generator(this, function (_a) {
70
- return [2 /*return*/, Promise.all([
71
- sns.createTopic(topic), sqs.createNamedQueue(queue)
72
- ])
73
- .then(function (values) { return sns
74
- .createSubscription(values[0], values[1])
75
- .then(function () { return values; }); })];
66
+ return [2 /*return*/, sqs.createNamedQueue(queue)
67
+ .then(function (queue) { return sns
68
+ .createSubscription({ arn: topic }, { arn: queue.arn })
69
+ .then(function () { return queue.url; }); })];
76
70
  });
77
71
  }); };
78
72
  this.startEventsConsumer = function (sqs, url) { return __awaiter(_this, void 0, void 0, function () {
@@ -128,9 +122,7 @@ var EventsProxy = /** @class */ (function () {
128
122
  }); };
129
123
  this.LOGGER = logging_1.LoggerFactory.create("SNS->SQS[".concat(name, "]"));
130
124
  var encodedName = Buffer.from(name).toString('hex').substring(0, 64);
131
- this.topic = topic !== null && topic !== void 0 ? topic : "EventProxyTopic".concat(encodedName);
132
- this.queue = queue !== null && queue !== void 0 ? queue : "EventProxyQueue".concat(encodedName);
133
- this.LOGGER.info('proxying events from %s to %s', this.topic, this.queue);
125
+ this.queue = "EventProxyQueue".concat(encodedName);
134
126
  }
135
127
  return EventsProxy;
136
128
  }());
@@ -5,23 +5,17 @@ import { DevServerConfig } from "./config";
5
5
  export declare const newSnsHttpProxy: (name: string, config: {
6
6
  settings: HttpSettings;
7
7
  method: string;
8
- topic: {
9
- arn: string;
10
- };
11
8
  subject: string | undefined;
12
9
  }, callback?: Callback<string>) => Service<string>;
13
10
  export declare class SnsHttpProxy implements Lifecycle<string> {
14
11
  readonly name: string;
15
12
  private readonly settings;
16
13
  private readonly method;
17
- private readonly topic;
18
14
  private readonly subject?;
19
15
  private LOGGER;
20
- private readonly server;
16
+ private server;
21
17
  private config;
22
- constructor(name: string, settings: HttpSettings, method: string, topic: {
23
- arn: string;
24
- }, subject?: string);
18
+ constructor(name: string, settings: HttpSettings, method: string, subject?: string);
25
19
  start: (config: DevServerConfig) => Promise<string>;
26
20
  stop: () => Promise<void>;
27
21
  handler: (method: string, subject?: string) => (request: IncomingMessage, body: string, response: ServerResponse) => Promise<void>;
@@ -17,21 +17,20 @@ 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.topic, config.subject), callback); };
20
+ var newSnsHttpProxy = function (name, config, callback) { return new services_1.Service(new SnsHttpProxy(name, config.settings, config.method, config.subject), callback); };
21
21
  exports.newSnsHttpProxy = newSnsHttpProxy;
22
22
  var SnsHttpProxy = /** @class */ (function () {
23
- function SnsHttpProxy(name, settings, method, topic, subject) {
23
+ function SnsHttpProxy(name, settings, method, subject) {
24
24
  var _this = this;
25
25
  this.name = name;
26
26
  this.settings = settings;
27
27
  this.method = method;
28
- this.topic = topic;
29
28
  this.subject = subject;
30
29
  this.start = function (config) { return Promise.resolve(_this.server)
31
30
  .then(function (server) { return server.start(); })
32
31
  .then(function (endpoint) {
33
32
  _this.config = {
34
- topic: _this.topic.arn,
33
+ topic: config.eventsProxy.topic,
35
34
  sns: config.sns
36
35
  };
37
36
  return endpoint;
@@ -44,7 +43,7 @@ var SnsHttpProxy = /** @class */ (function () {
44
43
  return Promise.resolve(((0, assert_1.assertNotUndefined)(_this.config)))
45
44
  .then(function (config) {
46
45
  return config.sns
47
- .publish({ arn: config.topic }, _this.extractMessage(method, request, body), subject, _this.extractMessageAttributes(request))
46
+ .publish(config.topic, _this.extractMessage(method, request, body), subject, _this.extractMessageAttributes(request))
48
47
  .then(function () { return (0, responses_1.writeResponse)(response, 200, ''); })
49
48
  .catch(function (e) {
50
49
  _this.LOGGER.error(e, 'request failed to execute');
package/dist/src/sns.d.ts CHANGED
@@ -3,10 +3,7 @@ import { AwsConfig } from "./config";
3
3
  export declare class Sns {
4
4
  readonly client: SNSClient;
5
5
  constructor(aws: AwsConfig);
6
- createTopic: (topic: string) => Promise<{
7
- arn: string;
8
- name: string;
9
- }>;
6
+ createTopic: (topic: string) => Promise<string>;
10
7
  createSubscription: (topic: {
11
8
  arn: string;
12
9
  }, queue: {
package/dist/src/sns.js CHANGED
@@ -50,11 +50,7 @@ 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'); })
54
- .then(function (arn) { return ({
55
- arn: arn,
56
- name: topic,
57
- }); })];
53
+ .then(function (o) { return (0, assert_1.assertNotUndefined)(o.TopicArn, 'TopicArn is not present'); })];
58
54
  });
59
55
  }); };
60
56
  this.createSubscription = function (topic, queue) { return __awaiter(_this, void 0, void 0, function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yopdev/dev-server",
3
- "version": "1.3.0-alpha.2",
3
+ "version": "1.3.0-alpha.4",
4
4
  "scripts": {
5
5
  "compile": "tsc",
6
6
  "pretest": "npm run compile",