@yopdev/dev-server 1.4.0-alpha.1 → 1.5.0-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.
@@ -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,82 @@
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
+ });
63
+ }); };
64
+ this.stop = function () { return __awaiter(_this, void 0, void 0, function () {
65
+ var _this = this;
66
+ return __generator(this, function (_a) {
67
+ return [2 /*return*/, this.started.stop().then(function () { return _this.LOGGER.info('stop'); })];
68
+ });
69
+ }); };
70
+ this.LOGGER = logging_1.LoggerFactory.create("CONTAINER[".concat(name, "]"));
71
+ var generic = new testcontainers_1.GenericContainer(image);
72
+ var withEnvironment = environment ? generic.withEnvironment(environment) : generic;
73
+ var withBindMounts = bindMounts ? withEnvironment.withBindMounts(bindMounts) : withEnvironment;
74
+ var withExposedPorts = exposedPorts ? withBindMounts.withExposedPorts.apply(withBindMounts, exposedPorts) : withBindMounts;
75
+ var withLogConsumer = logConsumer ? withExposedPorts.withLogConsumer(logConsumer) : withExposedPorts;
76
+ this.container = startup ? withLogConsumer.withWaitStrategy(startup.waitStrategy).withStartupTimeout(startup.timeout) : withLogConsumer;
77
+ this.networkAlias = networkAlias;
78
+ this.endpointBuilder = endpointBuilder !== null && endpointBuilder !== void 0 ? endpointBuilder : (function (port) { return "http://localhost:".concat(port); });
79
+ }
80
+ return Container;
81
+ }());
82
+ ;
@@ -1,10 +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[];
5
6
  topic?: string;
6
7
  }) => Service<any>;
7
8
  export type EventHandler = {
9
+ name: string;
8
10
  handler: (event: SQSEvent) => Promise<unknown>;
9
- matcher: (subject: string) => boolean;
11
+ matcher: (subject: string, attributes: Record<string, MessageAttributeValue>) => boolean;
10
12
  };
@@ -89,29 +89,26 @@ var EventsProxy = /** @class */ (function () {
89
89
  }); };
90
90
  this.extractErrorMessage = function (e) { return typeof e === 'string' ? e : e.message !== undefined && typeof e.message === 'string' ? e.message : ''; };
91
91
  this.onEachMessage = function (message) {
92
- var body = (0, assert_1.assertNotUndefined)(message.Body, 'body is not present');
93
- var subject = JSON.parse(body).Subject;
94
92
  if (_this.stopped) {
95
- _this.LOGGER.warn('stopped events proxy received message with subject %s', subject);
93
+ _this.LOGGER.warn('stopped events proxy received message %o', message);
96
94
  return;
97
95
  }
98
- 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 : {}); });
99
99
  if (handlers.length === 0) {
100
- _this.LOGGER.warn('no handlers found for subject %s, message %o', subject, message);
100
+ _this.LOGGER.warn('no handlers found for message %o', message);
101
101
  }
102
+ var record = (0, mappers_1.mapToLambdaSqsRecord)(message);
102
103
  handlers.forEach(function (handler) {
103
- var record = (0, mappers_1.mapToLambdaSqsRecord)(message);
104
- handler
104
+ return handler
105
105
  .handler({ Records: [record] })
106
106
  .then(function () {
107
- var _a;
108
- var json = JSON.parse(record.body);
109
- var subject = json.Subject || ((_a = json.Message) === null || _a === void 0 ? void 0 : _a.eventname) || 'unknown';
110
- _this.LOGGER.debug('subject accepted %s', subject);
107
+ _this.LOGGER.debug('handler %s accepted message', handler.name);
111
108
  })
112
109
  .catch(function (e) {
113
110
  var error = _this.extractErrorMessage(e);
114
- _this.LOGGER.error(e, 'failed %s with %s', subject, error);
111
+ _this.LOGGER.error(e, 'handler %s failed with %s', handler.name, error);
115
112
  });
116
113
  });
117
114
  };
@@ -7,5 +7,9 @@ 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, terminate } 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';
package/dist/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.promised = exports.lazy = exports.oneThenOther = exports.allOf = exports.dynamoDbTable = exports.preTrafficHooks = exports.scheduledTasks = exports.eventsProxy = exports.internalQueue = exports.snsHttpProxy = exports.lambdaHttpProxy = exports.httpServer = exports.DevServer = void 0;
3
+ exports.Service = exports.promised = exports.lazy = exports.oneThenOther = exports.allOf = exports.terminate = 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
4
  var dev_server_1 = require("./dev-server");
5
5
  Object.defineProperty(exports, "DevServer", { enumerable: true, get: function () { return dev_server_1.DevServer; } });
6
6
  var http_server_1 = require("./http-server");
@@ -19,9 +19,16 @@ var pre_traffic_hooks_1 = require("./pre-traffic-hooks");
19
19
  Object.defineProperty(exports, "preTrafficHooks", { enumerable: true, get: function () { return pre_traffic_hooks_1.newPreTrafficHooks; } });
20
20
  var dynamodb_1 = require("./dynamodb");
21
21
  Object.defineProperty(exports, "dynamoDbTable", { enumerable: true, get: function () { return dynamodb_1.newDynamoDbTable; } });
22
+ var container_1 = require("./container");
23
+ Object.defineProperty(exports, "container", { enumerable: true, get: function () { return container_1.newContainer; } });
24
+ var tunnel_1 = require("./tunnel");
25
+ Object.defineProperty(exports, "tunnel", { enumerable: true, get: function () { return tunnel_1.newTunnel; } });
26
+ Object.defineProperty(exports, "terminate", { enumerable: true, get: function () { return tunnel_1.terminate; } });
22
27
  var factories_1 = require("./factories");
23
28
  Object.defineProperty(exports, "allOf", { enumerable: true, get: function () { return factories_1.all; } });
24
29
  Object.defineProperty(exports, "oneThenOther", { enumerable: true, get: function () { return factories_1.oneThenOther; } });
25
30
  var deferred_1 = require("./deferred");
26
31
  Object.defineProperty(exports, "lazy", { enumerable: true, get: function () { return deferred_1.lazy; } });
27
32
  Object.defineProperty(exports, "promised", { enumerable: true, get: function () { return deferred_1.promised; } });
33
+ var services_1 = require("./services");
34
+ Object.defineProperty(exports, "Service", { enumerable: true, get: function () { return services_1.Service; } });
@@ -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,84 @@
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 terminate = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
44
+ return [2 /*return*/, ngrok.kill()];
45
+ }); }); };
46
+ exports.terminate = terminate;
47
+ var newTunnel = function (config) {
48
+ return new services_1.Service(new Tunnel(config.name, config.port, config.fixed));
49
+ };
50
+ exports.newTunnel = newTunnel;
51
+ var Tunnel = /** @class */ (function () {
52
+ function Tunnel(name, port, domain) {
53
+ var _this = this;
54
+ this.name = name;
55
+ this.port = port;
56
+ this.domain = domain;
57
+ this.start = function () { return __awaiter(_this, void 0, void 0, function () {
58
+ var _this = this;
59
+ var _a, _b;
60
+ return __generator(this, function (_c) {
61
+ return [2 /*return*/, ngrok
62
+ .forward({
63
+ proto: 'http',
64
+ domain: (_a = this.domain) === null || _a === void 0 ? void 0 : _a.name,
65
+ addr: this.port,
66
+ authtoken: (_b = this.domain) === null || _b === void 0 ? void 0 : _b.token,
67
+ })
68
+ .then(function (listener) {
69
+ var url = listener.url();
70
+ _this.logger.info('start %s', url);
71
+ return url || 'undefined';
72
+ })];
73
+ });
74
+ }); };
75
+ this.stop = function () { return __awaiter(_this, void 0, void 0, function () {
76
+ var _this = this;
77
+ return __generator(this, function (_a) {
78
+ return [2 /*return*/, ngrok.disconnect().then(function () { return _this.logger.info('stop'); })];
79
+ });
80
+ }); };
81
+ this.logger = logging_1.LoggerFactory.create("TUNNEL[".concat(name, "]"));
82
+ }
83
+ return Tunnel;
84
+ }());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yopdev/dev-server",
3
- "version": "1.4.0-alpha.1",
3
+ "version": "1.5.0-alpha.0",
4
4
  "scripts": {
5
5
  "compile": "tsc",
6
6
  "pretest": "npm run compile",
@@ -20,6 +20,7 @@
20
20
  "@aws-sdk/client-dynamodb": "^3.451.0",
21
21
  "@aws-sdk/client-sns": "^3.451.0",
22
22
  "@aws-sdk/client-sqs": "^3.451.0",
23
+ "@ngrok/ngrok": "^0.9.1",
23
24
  "@yopdev/logging": "^0.0.2",
24
25
  "sqs-consumer": "^7.4.0",
25
26
  "testcontainers": "^10.2.2"