@yopdev/dev-server 1.4.0-alpha.0 → 1.4.1-alpha.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.
@@ -1,9 +1,12 @@
1
1
  import { SQSEvent } from 'aws-lambda';
2
+ import { MessageAttributeValue } from '@aws-sdk/client-sqs';
2
3
  import { Service } from './services';
3
4
  export declare const newEventsProxy: (name: string, config: {
4
5
  handlers: EventHandler[];
6
+ topic?: string;
5
7
  }) => Service<any>;
6
8
  export type EventHandler = {
9
+ name: string;
7
10
  handler: (event: SQSEvent) => Promise<unknown>;
8
- matcher: (subject: string) => boolean;
11
+ matcher: (subject: string, attributes: Record<string, MessageAttributeValue>) => boolean;
9
12
  };
@@ -43,20 +43,22 @@ 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) { 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;
55
56
  this.queueName = function () { return "EventProxyQueue".concat(Buffer.from(_this.name).toString('hex').substring(0, 64)); };
56
57
  this.start = function (config) { return __awaiter(_this, void 0, void 0, function () {
57
58
  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())
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())
60
62
  .then(function (url) { return _this.startEventsConsumer(config.sqs.client, url); })
61
63
  .then(function () { return _this.stopped = false; })
62
64
  .then(function () { return undefined; })];
@@ -87,29 +89,29 @@ var EventsProxy = /** @class */ (function () {
87
89
  }); };
88
90
  this.extractErrorMessage = function (e) { return typeof e === 'string' ? e : e.message !== undefined && typeof e.message === 'string' ? e.message : ''; };
89
91
  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
92
  if (_this.stopped) {
93
- _this.LOGGER.warn('stopped events proxy received message with subject %s', subject);
93
+ _this.LOGGER.warn('stopped events proxy received message %o', message);
94
94
  return;
95
95
  }
96
- 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 subject = JSON.parse(body).Subject;
98
+ var handlers = _this.handlers.filter(function (handler) { var _a; return handler.matcher(subject, (_a = message.MessageAttributes) !== null && _a !== void 0 ? _a : {}); });
97
99
  if (handlers.length === 0) {
98
- _this.LOGGER.warn('no handlers found for subject %s, message %o', subject, message);
100
+ _this.LOGGER.warn('no handlers found for message %o', message);
99
101
  }
102
+ var record = (0, mappers_1.mapToLambdaSqsRecord)(message);
100
103
  handlers.forEach(function (handler) {
101
- var record = (0, mappers_1.mapToLambdaSqsRecord)(message);
102
- handler
104
+ return handler
103
105
  .handler({ Records: [record] })
104
106
  .then(function () {
105
107
  var _a;
106
108
  var json = JSON.parse(record.body);
107
109
  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
  };
@@ -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,20 +17,22 @@ 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
32
  .then(function (endpoint) {
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
38
  return endpoint;
@@ -43,7 +45,7 @@ var SnsHttpProxy = /** @class */ (function () {
43
45
  return Promise.resolve(((0, assert_1.assertNotUndefined)(_this.config)))
44
46
  .then(function (config) {
45
47
  return config.sns
46
- .publish(config.topic, _this.extractMessage(method, request, body), subject, _this.extractMessageAttributes(request))
48
+ .publish({ arn: config.topic }, _this.extractMessage(method, request, body), subject, _this.extractMessageAttributes(request))
47
49
  .then(function () { return (0, responses_1.writeResponse)(response, 200, ''); })
48
50
  .catch(function (e) {
49
51
  _this.LOGGER.error(e, 'request failed to execute');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yopdev/dev-server",
3
- "version": "1.4.0-alpha.0",
3
+ "version": "1.4.1-alpha.0",
4
4
  "scripts": {
5
5
  "compile": "tsc",
6
6
  "pretest": "npm run compile",