@yopdev/dev-server 3.0.1 → 3.0.2-RC1

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.
Files changed (92) hide show
  1. package/.github/workflows/npm-publish.yml +33 -0
  2. package/__tests__/bootstrap.test.ts +89 -0
  3. package/__tests__/deferred.test.ts +86 -0
  4. package/__tests__/event-proxy.test.ts +42 -0
  5. package/__tests__/lambda-http-proxy.test.ts +179 -0
  6. package/jest.config.js +7 -0
  7. package/package.json +2 -5
  8. package/src/assert.ts +4 -0
  9. package/src/cloudformation-dynamodb-table.ts +97 -0
  10. package/src/cloudformation-event-proxy.ts +61 -0
  11. package/src/cloudformation-lambda-http-proxy.ts +125 -0
  12. package/src/cloudformation.ts +95 -0
  13. package/src/config.ts +34 -0
  14. package/src/container.ts +82 -0
  15. package/src/deferred.ts +60 -0
  16. package/src/dev-server.ts +78 -0
  17. package/src/dynamodb.ts +62 -0
  18. package/src/event-proxy.ts +101 -0
  19. package/src/factories.ts +19 -0
  20. package/src/http-server.ts +59 -0
  21. package/src/index.ts +32 -0
  22. package/src/internal-queue.ts +89 -0
  23. package/src/lambda-http-proxy.ts +111 -0
  24. package/src/localstack.ts +74 -0
  25. package/src/mappers.ts +231 -0
  26. package/src/pre-traffic-hooks.ts +24 -0
  27. package/src/responses.ts +28 -0
  28. package/src/s3.ts +24 -0
  29. package/src/scheduled-tasks.ts +31 -0
  30. package/src/services.ts +46 -0
  31. package/src/sns-http-proxy.ts +109 -0
  32. package/src/sns.ts +49 -0
  33. package/src/sqs.ts +46 -0
  34. package/src/stoppable.ts +10 -0
  35. package/src/tunnel.ts +32 -0
  36. package/tsconfig.json +9 -0
  37. package/dist/src/assert.d.ts +0 -1
  38. package/dist/src/assert.js +0 -9
  39. package/dist/src/cloudformation-dynamodb-table.d.ts +0 -13
  40. package/dist/src/cloudformation-dynamodb-table.js +0 -45
  41. package/dist/src/cloudformation-event-proxy.d.ts +0 -13
  42. package/dist/src/cloudformation-event-proxy.js +0 -25
  43. package/dist/src/cloudformation-lambda-http-proxy.d.ts +0 -14
  44. package/dist/src/cloudformation-lambda-http-proxy.js +0 -62
  45. package/dist/src/cloudformation.d.ts +0 -28
  46. package/dist/src/cloudformation.js +0 -50
  47. package/dist/src/config.d.ts +0 -31
  48. package/dist/src/config.js +0 -2
  49. package/dist/src/container.d.ts +0 -18
  50. package/dist/src/container.js +0 -33
  51. package/dist/src/deferred.d.ts +0 -4
  52. package/dist/src/deferred.js +0 -45
  53. package/dist/src/dev-server.d.ts +0 -19
  54. package/dist/src/dev-server.js +0 -63
  55. package/dist/src/dynamodb.d.ts +0 -16
  56. package/dist/src/dynamodb.js +0 -48
  57. package/dist/src/event-proxy.d.ts +0 -13
  58. package/dist/src/event-proxy.js +0 -68
  59. package/dist/src/factories.d.ts +0 -3
  60. package/dist/src/factories.js +0 -16
  61. package/dist/src/http-server.d.ts +0 -25
  62. package/dist/src/http-server.js +0 -37
  63. package/dist/src/index.d.ts +0 -24
  64. package/dist/src/index.js +0 -46
  65. package/dist/src/internal-queue.d.ts +0 -11
  66. package/dist/src/internal-queue.js +0 -53
  67. package/dist/src/lambda-http-proxy.d.ts +0 -27
  68. package/dist/src/lambda-http-proxy.js +0 -49
  69. package/dist/src/localstack.d.ts +0 -11
  70. package/dist/src/localstack.js +0 -62
  71. package/dist/src/mappers.d.ts +0 -25
  72. package/dist/src/mappers.js +0 -158
  73. package/dist/src/pre-traffic-hooks.d.ts +0 -2
  74. package/dist/src/pre-traffic-hooks.js +0 -19
  75. package/dist/src/responses.d.ts +0 -5
  76. package/dist/src/responses.js +0 -22
  77. package/dist/src/s3.d.ts +0 -7
  78. package/dist/src/s3.js +0 -20
  79. package/dist/src/scheduled-tasks.d.ts +0 -6
  80. package/dist/src/scheduled-tasks.js +0 -20
  81. package/dist/src/services.d.ts +0 -22
  82. package/dist/src/services.js +0 -26
  83. package/dist/src/sns-http-proxy.d.ts +0 -28
  84. package/dist/src/sns-http-proxy.js +0 -66
  85. package/dist/src/sns.d.ts +0 -15
  86. package/dist/src/sns.js +0 -35
  87. package/dist/src/sqs.d.ts +0 -13
  88. package/dist/src/sqs.js +0 -33
  89. package/dist/src/stoppable.d.ts +0 -2
  90. package/dist/src/stoppable.js +0 -15
  91. package/dist/src/tunnel.d.ts +0 -10
  92. package/dist/src/tunnel.js +0 -52
@@ -1,11 +0,0 @@
1
- import { StartedNetwork } from "testcontainers";
2
- import { AwsConfig, Config } from "./config";
3
- import { Stoppable } from "./services";
4
- export declare class LocalStack implements Stoppable {
5
- private readonly _start;
6
- private started;
7
- constructor(network: StartedNetwork, config?: Config);
8
- private configOrDefaults;
9
- start: () => Promise<AwsConfig>;
10
- stop: () => Promise<void>;
11
- }
@@ -1,62 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LocalStack = void 0;
4
- const logging_1 = require("@yopdev/logging");
5
- const testcontainers_1 = require("testcontainers");
6
- const LOGGER = logging_1.LoggerFactory.create('LOCALSTACK');
7
- const SERVICES_PORT = 4566;
8
- const NETWORK_ALIAS = 'localstack';
9
- const PROTOCOL = 'http';
10
- const DEFAULT_DOCKER_IMAGE = 'localstack/localstack:stable';
11
- class LocalStack {
12
- constructor(network, config) {
13
- this.start = async () => this._start()
14
- .then((endpoint) => ({
15
- region: 'us-east-1',
16
- endpoint: endpoint,
17
- credentials: {
18
- accessKeyId: 'dummy',
19
- secretAccessKey: 'dummy'
20
- }
21
- }))
22
- .tap(() => LOGGER.debug('started'));
23
- this.stop = () => Promise.resolve(this.started)
24
- .then((container) => container ? container.stop().then(() => 'stopped') : 'not started')
25
- .then((status) => LOGGER.debug(status));
26
- const concreteConfig = this.configOrDefaults(config);
27
- const container = new testcontainers_1.GenericContainer(concreteConfig.localStackDockerImage);
28
- const localStatePath = concreteConfig.localStateBindMount;
29
- const withLocalState = localStatePath !== undefined ?
30
- container
31
- .withEnvironment({
32
- PERSISTENCE: '1',
33
- DYNAMODB_REMOVE_EXPIRED_ITEMS: '1',
34
- })
35
- .withBindMounts([{
36
- source: localStatePath,
37
- target: '/var/lib/localstack',
38
- mode: 'rw',
39
- }])
40
- : container;
41
- const ready = withLocalState
42
- .withExposedPorts(concreteConfig.exposedPort)
43
- .withNetwork(network)
44
- .withNetworkAliases(NETWORK_ALIAS);
45
- this._start = () => ready
46
- .start()
47
- .then((container) => this.started = container)
48
- .then((container) => `${PROTOCOL}://${container.getHost()}:${container.getMappedPort(SERVICES_PORT)}`);
49
- }
50
- configOrDefaults(config) {
51
- const selectedServicesPort = config?.boundServicesPort;
52
- const exposedPort = selectedServicesPort !== undefined ?
53
- { container: SERVICES_PORT, host: selectedServicesPort } :
54
- SERVICES_PORT;
55
- return {
56
- localStackDockerImage: config?.localStackDockerImage || DEFAULT_DOCKER_IMAGE,
57
- exposedPort: exposedPort,
58
- localStateBindMount: config?.localStateBindMount,
59
- };
60
- }
61
- }
62
- exports.LocalStack = LocalStack;
@@ -1,25 +0,0 @@
1
- /// <reference types="node" />
2
- import { Message } from "@aws-sdk/client-sqs";
3
- import { APIGatewayProxyEvent, APIGatewayProxyEventPathParameters, APIGatewayProxyEventV2, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2, SQSRecord } from "aws-lambda";
4
- import { IncomingMessage } from "http";
5
- import { Response } from "./lambda-http-proxy";
6
- export declare const mapToLambdaSqsRecord: (message: Message) => SQSRecord;
7
- export type LambdaPayloadVersion<AuthorizerContext, Event, HandlerResponse> = {
8
- mapper: LambdaMapperFactory<AuthorizerContext, Event, HandlerResponse>;
9
- resolver: PathParameterResolver<Event>;
10
- };
11
- export declare const v1: <AuthorizerContext>() => LambdaPayloadVersion<AuthorizerContext, APIGatewayProxyEvent, APIGatewayProxyResult>;
12
- export declare const v2: <AuthorizerContext>() => LambdaPayloadVersion<AuthorizerContext, APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2>;
13
- interface LambdaMapper<AuthorizerContext, Event, HandlerResponse> {
14
- event(context: AuthorizerContext): Event;
15
- authorization(): string;
16
- toResponse: (response: HandlerResponse) => Response;
17
- }
18
- export interface LambdaMapperFactory<AuthorizerContext, Event, LambdaResponse> {
19
- newInstance(request: IncomingMessage, body: string): Promise<LambdaMapper<AuthorizerContext, Event, LambdaResponse>>;
20
- }
21
- export type PathParameterResolver<Event> = {
22
- locate: (event: Event) => string;
23
- store: (event: Event, params: APIGatewayProxyEventPathParameters) => void;
24
- };
25
- export {};
@@ -1,158 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.v2 = exports.v1 = exports.mapToLambdaSqsRecord = void 0;
4
- const url_1 = require("url");
5
- const lambda_http_proxy_1 = require("./lambda-http-proxy");
6
- const mapToLambdaSqsRecord = (message) => {
7
- if (!message.Body)
8
- throw new Error('message Body must be present');
9
- return {
10
- messageId: 'N/A',
11
- receiptHandle: 'N/A',
12
- body: message.Body,
13
- attributes: {
14
- ApproximateReceiveCount: 'N/A',
15
- SentTimestamp: 'N/A',
16
- SenderId: 'N/A',
17
- ApproximateFirstReceiveTimestamp: 'N/A',
18
- },
19
- messageAttributes: {},
20
- md5OfBody: 'N/A',
21
- eventSource: 'N/A',
22
- eventSourceARN: 'N/A',
23
- awsRegion: 'N/A',
24
- };
25
- };
26
- exports.mapToLambdaSqsRecord = mapToLambdaSqsRecord;
27
- const payloadV1JwtAuthorizerLambdaMapper = () => ({
28
- newInstance: (request, body) => Promise.resolve(new DefaultLambdaMapper(request, body))
29
- });
30
- const payloadV2LambdaAuthorizerLambdaMapper = () => ({
31
- newInstance: (request, body) => Promise.resolve(new DefaultLambdaMapper(request, body))
32
- });
33
- const payloadV1PathParameterResolver = {
34
- locate: (event) => event.path,
35
- store: (event, params) => event.pathParameters = params
36
- };
37
- const payloadV2PathParameterResolver = {
38
- locate: (event) => event.rawPath,
39
- store: (event, params) => event.pathParameters = params
40
- };
41
- const v1 = () => ({
42
- mapper: payloadV1JwtAuthorizerLambdaMapper(),
43
- resolver: payloadV1PathParameterResolver,
44
- });
45
- exports.v1 = v1;
46
- const v2 = () => ({
47
- mapper: payloadV2LambdaAuthorizerLambdaMapper(),
48
- resolver: payloadV2PathParameterResolver,
49
- });
50
- exports.v2 = v2;
51
- class DefaultLambdaMapper {
52
- constructor(request, body) {
53
- this.body = body;
54
- this.toResponse = (response) => response.isBase64Encoded ?
55
- new Base64EncodedResponse(response.statusCode, response.headers?.['content-type']?.toString(), response.headers?.['location']?.toString(), response.body) :
56
- new StringResponse(response.statusCode, response.headers?.['content-type']?.toString(), response.headers?.['location']?.toString(), response.body);
57
- this.event = (context) => ({
58
- version: '2.0',
59
- rawPath: this.url.pathname,
60
- rawQueryString: this.url.search,
61
- routeKey: '',
62
- httpMethod: this.method,
63
- body: this.body,
64
- headers: this.headers,
65
- path: this.url.pathname,
66
- pathParameters: null,
67
- queryStringParameters: this.queryStringParameters,
68
- resource: '',
69
- multiValueHeaders: {},
70
- isBase64Encoded: false,
71
- multiValueQueryStringParameters: null,
72
- stageVariables: null,
73
- requestContext: {
74
- domainName: this.headers.host,
75
- domainPrefix: '',
76
- http: {
77
- method: this.method,
78
- path: this.url.pathname,
79
- protocol: 'http',
80
- sourceIp: '127.0.0.1',
81
- userAgent: this.headers['user-agent'],
82
- },
83
- routeKey: '',
84
- time: new Date(this.time).toString(),
85
- timeEpoch: this.time,
86
- accountId: '',
87
- apiId: '',
88
- authorizer: context,
89
- httpMethod: '',
90
- identity: {
91
- accessKey: '',
92
- accountId: '',
93
- apiKey: '',
94
- apiKeyId: '',
95
- caller: '',
96
- clientCert: {
97
- clientCertPem: '',
98
- issuerDN: '',
99
- serialNumber: '',
100
- subjectDN: '',
101
- validity: { notAfter: '', notBefore: '' },
102
- },
103
- cognitoAuthenticationProvider: '',
104
- cognitoAuthenticationType: '',
105
- cognitoIdentityId: '',
106
- cognitoIdentityPoolId: '',
107
- principalOrgId: '',
108
- sourceIp: '',
109
- user: '',
110
- userAgent: '',
111
- userArn: '',
112
- },
113
- path: '',
114
- protocol: '',
115
- requestId: '',
116
- requestTimeEpoch: this.time,
117
- resourceId: '',
118
- resourcePath: '',
119
- stage: '',
120
- }
121
- });
122
- this.authorization = () => this.authorizationHeaderValue;
123
- if (!request.url || !request.method)
124
- throw new Error('url and method are required');
125
- const url = new url_1.URL(request.url, `http://${request.headers.host}`);
126
- const qsp = {};
127
- url.searchParams.forEach((v, k) => (qsp[k] = decodeURIComponent(v)));
128
- this.queryStringParameters = qsp;
129
- const headers = {};
130
- if (request.headers['content-type'])
131
- headers['content-type'] = request.headers['content-type'];
132
- if (request.headers['accept'])
133
- headers['accept'] = request.headers['accept'];
134
- if (request.headers['origin'])
135
- headers['origin'] = request.headers['origin'];
136
- const authorizationHeaderValue = request.headers['authorization'];
137
- if (authorizationHeaderValue) {
138
- headers['authorization'] = authorizationHeaderValue;
139
- this.authorizationHeaderValue = authorizationHeaderValue;
140
- }
141
- this.headers = headers;
142
- this.url = url;
143
- this.time = 1428582896000;
144
- this.method = request.method;
145
- }
146
- }
147
- class StringResponse extends lambda_http_proxy_1.Response {
148
- constructor(statusCode, contentType, location, body) {
149
- super(statusCode, contentType, location);
150
- this.body = () => body;
151
- }
152
- }
153
- class Base64EncodedResponse extends lambda_http_proxy_1.Response {
154
- constructor(statusCode, contentType, location, body) {
155
- super(statusCode, contentType, location);
156
- this.body = () => Buffer.from(body, 'base64');
157
- }
158
- }
@@ -1,2 +0,0 @@
1
- import { Service } from './services';
2
- export declare const newPreTrafficHooks: (name: string, hooks: () => Promise<void>[]) => Service<any>;
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.newPreTrafficHooks = void 0;
4
- const logging_1 = require("@yopdev/logging");
5
- const services_1 = require("./services");
6
- const newPreTrafficHooks = (name, hooks) => new services_1.Service(new PreTrafficHooks(name, hooks));
7
- exports.newPreTrafficHooks = newPreTrafficHooks;
8
- class PreTrafficHooks {
9
- constructor(name, hooks) {
10
- this.name = name;
11
- this.hooks = hooks;
12
- this.start = async () => Promise
13
- .all(this.hooks())
14
- .then(() => undefined);
15
- this.stop = async () => Promise.resolve();
16
- this.LOGGER = logging_1.LoggerFactory.create(`PRETRAFFIC[${name}]`);
17
- this.LOGGER.info('%i hooks registered', hooks.length);
18
- }
19
- }
@@ -1,5 +0,0 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { ServerResponse } from 'http';
4
- export declare function internalServerError(res: ServerResponse, body: any): void;
5
- export declare function writeResponse(res: ServerResponse, statusCode: number, body: string | Buffer, contentType?: string, location?: string): void;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writeResponse = exports.internalServerError = void 0;
4
- const logging_1 = require("@yopdev/logging");
5
- const LOG = logging_1.LoggerFactory.create('RESPONSES');
6
- function internalServerError(res, body) {
7
- LOG.error(body, 'internal server error handled');
8
- return writeResponse(res, 500, 'internal server error. check system logs');
9
- }
10
- exports.internalServerError = internalServerError;
11
- function writeResponse(res, statusCode, body, contentType, location) {
12
- res.setHeader('Access-Control-Allow-Origin', '*');
13
- res.setHeader('Access-Control-Allow-Methods', '*');
14
- res.setHeader('Access-Control-Allow-Headers', '*');
15
- if (contentType)
16
- res.setHeader('Content-Type', contentType);
17
- if (location)
18
- res.setHeader('Location', location);
19
- res.statusCode = statusCode;
20
- res.end(body);
21
- }
22
- exports.writeResponse = writeResponse;
package/dist/src/s3.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { CORSConfiguration, 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, cors?: CORSConfiguration) => Promise<import("@aws-sdk/client-s3").PutBucketCorsCommandOutput>;
7
- }
package/dist/src/s3.js DELETED
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.S3 = void 0;
4
- const client_s3_1 = require("@aws-sdk/client-s3");
5
- const logging_1 = require("@yopdev/logging");
6
- const LOGGER = logging_1.LoggerFactory.create('S3');
7
- class S3 {
8
- constructor(aws) {
9
- this.createBucket = async (name, cors) => this.client.send(new client_s3_1.CreateBucketCommand({
10
- Bucket: name,
11
- }))
12
- .then(() => cors && this.client.send(new client_s3_1.PutBucketCorsCommand({
13
- Bucket: name,
14
- CORSConfiguration: cors
15
- })))
16
- .tap(() => LOGGER.debug('bucket %s created', name));
17
- this.client = new client_s3_1.S3Client({ forcePathStyle: true, ...aws });
18
- }
19
- }
20
- exports.S3 = S3;
@@ -1,6 +0,0 @@
1
- import { Service } from './services';
2
- export declare const newScheduledTasks: (name: string, schedules: Rate[]) => Service<any>;
3
- export type Rate = {
4
- frequency: number;
5
- task: () => Promise<unknown>;
6
- };
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.newScheduledTasks = void 0;
4
- const logging_1 = require("@yopdev/logging");
5
- const timers_1 = require("timers");
6
- const services_1 = require("./services");
7
- const newScheduledTasks = (name, schedules) => new services_1.Service(new ScheduledTasks(name, schedules));
8
- exports.newScheduledTasks = newScheduledTasks;
9
- class ScheduledTasks {
10
- constructor(name, schedules) {
11
- this.name = name;
12
- this.schedules = schedules;
13
- this.intervals = [];
14
- this.start = async () => Promise.resolve(this.schedules.forEach((schedule) => this.intervals.push(setInterval(schedule.task, schedule.frequency * 1000))))
15
- .then(() => undefined);
16
- this.stop = async () => Promise.resolve(this.intervals.forEach((interval) => (0, timers_1.clearInterval)(interval)));
17
- this.LOGGER = logging_1.LoggerFactory.create(`SCHEDULER[${name}]`);
18
- this.LOGGER.info('registered %i scheduled tasks', schedules.length);
19
- }
20
- }
@@ -1,22 +0,0 @@
1
- import { DevServerConfig } from "./config";
2
- export interface Lifecycle<I> extends Startable<I>, Stoppable {
3
- name: string;
4
- }
5
- export interface Startable<T> {
6
- start(config: DevServerConfig): Promise<T>;
7
- }
8
- export interface Stoppable {
9
- stop(): Promise<void>;
10
- }
11
- export type Callback<I> = (instance: I) => Promise<void>;
12
- export declare class Service<I> implements Lifecycle<I> {
13
- private readonly callback?;
14
- private readonly DEFAULT_CALLBACK;
15
- readonly name: string;
16
- readonly start: (config: DevServerConfig) => Promise<I>;
17
- readonly stop: () => Promise<void>;
18
- private doStop;
19
- private readonly LOGGER;
20
- constructor(service: Lifecycle<I>, callback?: Callback<I>);
21
- private callbackOrDefault;
22
- }
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Service = void 0;
4
- const logging_1 = require("@yopdev/logging");
5
- class Service {
6
- constructor(service, callback) {
7
- this.callback = callback;
8
- this.DEFAULT_CALLBACK = () => Promise.resolve(this.LOGGER.debug('no callback'));
9
- this.callbackOrDefault = async (instance) => (this.callback ?? this.DEFAULT_CALLBACK)(instance);
10
- this.name = service.name;
11
- this.LOGGER = logging_1.LoggerFactory.create(`SERVICE[${this.name}]`);
12
- const stop = async () => service.stop();
13
- this.start = (config) => service.start(config)
14
- .catch((error) => stop()
15
- .then(() => this.doStop = () => Promise.resolve(this.LOGGER.error(error, 'failed')))
16
- .then(() => Promise.reject(error)))
17
- .then((started) => this
18
- .callbackOrDefault(started)
19
- .then(() => this.LOGGER.info('started'))
20
- .then(() => started));
21
- this.doStop = () => stop()
22
- .then(() => this.LOGGER.info('stopped'));
23
- this.stop = () => this.doStop();
24
- }
25
- }
26
- exports.Service = Service;
@@ -1,28 +0,0 @@
1
- import { HttpSettings } from "./http-server";
2
- import { IncomingMessage, ServerResponse } from "http";
3
- import { Lifecycle, Service, Callback } from "./services";
4
- import { DevServerConfig } from "./config";
5
- export declare const newSnsHttpProxy: (name: string, config: {
6
- settings: HttpSettings;
7
- method: string;
8
- subject?: string;
9
- topic?: string;
10
- }, callback?: Callback<string>) => Service<string>;
11
- export declare class SnsHttpProxy implements Lifecycle<string> {
12
- readonly name: string;
13
- private readonly settings;
14
- private readonly method;
15
- private readonly subject?;
16
- private readonly topic?;
17
- private LOGGER;
18
- private server;
19
- private config;
20
- constructor(name: string, settings: HttpSettings, method: string, subject?: string, topic?: string);
21
- start: (config: DevServerConfig) => Promise<string>;
22
- stop: () => Promise<void>;
23
- handler: (method: string, subject?: string) => (request: IncomingMessage, body: string, response: ServerResponse) => Promise<void>;
24
- extractMessage: (method: string, request: IncomingMessage, body: string) => string;
25
- private onlyWhenStarted;
26
- private extractQueryString;
27
- private extractMessageAttributes;
28
- }
@@ -1,66 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SnsHttpProxy = exports.newSnsHttpProxy = void 0;
4
- const logging_1 = require("@yopdev/logging");
5
- const http_server_1 = require("./http-server");
6
- const responses_1 = require("./responses");
7
- const services_1 = require("./services");
8
- const assert_1 = require("./assert");
9
- const newSnsHttpProxy = (name, config, callback) => new services_1.Service(new SnsHttpProxy(name, config.settings, config.method, config.subject, config.topic), callback);
10
- exports.newSnsHttpProxy = newSnsHttpProxy;
11
- class SnsHttpProxy {
12
- constructor(name, settings, method, subject, topic) {
13
- this.name = name;
14
- this.settings = settings;
15
- this.method = method;
16
- this.subject = subject;
17
- this.topic = topic;
18
- this.start = (config) => Promise.resolve(this.server)
19
- .then(server => server.start())
20
- .tap(() => {
21
- this.config = {
22
- topic: this.topic ?? config.eventsProxy.topic.arn,
23
- sns: config.sns
24
- };
25
- })
26
- .then((endpoint) => endpoint);
27
- this.stop = () => this.onlyWhenStarted(this.server).stop()
28
- .then(() => this.LOGGER.debug('stopped'));
29
- this.handler = (method, subject) => (request, body, response) => Promise.resolve(((0, assert_1.assertNotUndefined)(this.config)))
30
- .then((config) => config.sns
31
- .publish({ arn: config.topic }, this.extractMessage(method, request, body), subject, this.extractMessageAttributes(request))
32
- .then(() => (0, responses_1.writeResponse)(response, 200, ''))
33
- .catch((e) => {
34
- this.LOGGER.error(e, 'request failed to execute');
35
- (0, responses_1.internalServerError)(response, e.body);
36
- }));
37
- this.extractMessage = (method, request, body) => {
38
- switch (method) {
39
- case 'GET':
40
- return this.extractQueryString(request) || '';
41
- case 'POST':
42
- return body;
43
- default:
44
- throw new Error();
45
- }
46
- };
47
- this.onlyWhenStarted = (server) => (0, assert_1.assertNotUndefined)(server, 'not started');
48
- this.extractQueryString = (request) => request.url?.substring(request.url?.indexOf('?') + 1);
49
- this.extractMessageAttributes = (request) => {
50
- const initialValue = {};
51
- return Object.keys(request.headers).reduce((obj, item) => {
52
- return {
53
- ...obj,
54
- [item]: {
55
- DataType: 'String',
56
- StringValue: request.headers[item],
57
- },
58
- };
59
- }, initialValue);
60
- };
61
- this.server = new http_server_1.HttpServer(this.name, this.settings, this.handler(this.method, this.subject));
62
- this.LOGGER = logging_1.LoggerFactory.create(`HTTP->SNS[${name}]`);
63
- this.LOGGER.info('forwarding %s %s events to %s', subject ? `events with ${subject} subject` : 'all', method, topic ?? 'the event bus');
64
- }
65
- }
66
- exports.SnsHttpProxy = SnsHttpProxy;
package/dist/src/sns.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import { MessageAttributeValue, SNSClient } from "@aws-sdk/client-sns";
2
- import { AwsConfig } from "./config";
3
- export declare class Sns {
4
- readonly client: SNSClient;
5
- constructor(aws: AwsConfig);
6
- createTopic: (topic: string) => Promise<string>;
7
- createSubscription: (topic: {
8
- arn: string;
9
- }, queue: {
10
- arn: string;
11
- }) => Promise<void>;
12
- publish: (topic: {
13
- arn: string;
14
- }, body: string, subject?: string, attributes?: Record<string, MessageAttributeValue>) => Promise<void>;
15
- }
package/dist/src/sns.js DELETED
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Sns = void 0;
4
- const client_sns_1 = require("@aws-sdk/client-sns");
5
- const assert_1 = require("./assert");
6
- const logging_1 = require("@yopdev/logging");
7
- const LOGGER = logging_1.LoggerFactory.create('SNS');
8
- class Sns {
9
- constructor(aws) {
10
- this.createTopic = async (topic) => this.client
11
- .send(new client_sns_1.CreateTopicCommand({
12
- Name: topic,
13
- }))
14
- .then((o) => (0, assert_1.assertNotUndefined)(o.TopicArn, 'TopicArn is not present'))
15
- .tap((arn) => LOGGER.debug('topic %o created', arn));
16
- this.createSubscription = async (topic, queue) => this.client
17
- .send(new client_sns_1.SubscribeCommand({
18
- TopicArn: topic.arn,
19
- Protocol: 'sqs',
20
- Endpoint: queue.arn,
21
- }))
22
- .then(() => LOGGER.debug('subscription %s->%s created', topic.arn, queue.arn));
23
- this.publish = async (topic, body, subject, attributes) => this.client
24
- .send(new client_sns_1.PublishCommand({
25
- TopicArn: topic.arn,
26
- Message: body,
27
- MessageAttributes: attributes,
28
- Subject: subject,
29
- }))
30
- .then(() => LOGGER.trace('published %s[%s]', topic.arn, subject))
31
- .then();
32
- this.client = new client_sns_1.SNSClient(aws);
33
- }
34
- }
35
- exports.Sns = Sns;
package/dist/src/sqs.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { SQSClient } from "@aws-sdk/client-sqs";
2
- import { AwsConfig } from "./config";
3
- export declare class Sqs {
4
- readonly client: SQSClient;
5
- constructor(config: AwsConfig);
6
- createStandardQueue: (name: string) => Promise<Queue>;
7
- createFifoQueue: (name: string) => Promise<Queue>;
8
- private createNamedQueue;
9
- }
10
- export type Queue = {
11
- arn: string;
12
- url: string;
13
- };
package/dist/src/sqs.js DELETED
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Sqs = void 0;
4
- const client_sqs_1 = require("@aws-sdk/client-sqs");
5
- const assert_1 = require("./assert");
6
- const logging_1 = require("@yopdev/logging");
7
- const QUEUE_NAME_ATTRIBUTE_NAME = 'QueueArn';
8
- const LOGGER = logging_1.LoggerFactory.create('SQS');
9
- class Sqs {
10
- constructor(config) {
11
- this.createStandardQueue = async (name) => this.createNamedQueue(name, false);
12
- this.createFifoQueue = async (name) => this.createNamedQueue(name, true);
13
- this.createNamedQueue = async (name, fifo) => this.client
14
- .send(new client_sqs_1.CreateQueueCommand({
15
- QueueName: fifo ? `${name}.fifo` : name,
16
- Attributes: {
17
- FifoQueue: fifo ? 'true' : undefined,
18
- }
19
- }))
20
- .then((create) => this.client
21
- .send(new client_sqs_1.GetQueueAttributesCommand({
22
- QueueUrl: create.QueueUrl,
23
- AttributeNames: [QUEUE_NAME_ATTRIBUTE_NAME],
24
- }))
25
- .then((attributes) => ({
26
- url: (0, assert_1.assertNotUndefined)(create.QueueUrl),
27
- arn: (0, assert_1.assertNotUndefined)(attributes.Attributes)[QUEUE_NAME_ATTRIBUTE_NAME],
28
- })))
29
- .tap((queue) => LOGGER.debug('created %s', queue.arn));
30
- this.client = new client_sqs_1.SQSClient(config);
31
- }
32
- }
33
- exports.Sqs = Sqs;
@@ -1,2 +0,0 @@
1
- import { Consumer } from "sqs-consumer";
2
- export declare const stopConsumer: (queueWaitTimeSecs: number, target?: Consumer) => Promise<void>;
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stopConsumer = void 0;
4
- const stopConsumer = async (queueWaitTimeSecs, target) => {
5
- if (target) {
6
- target.stop();
7
- return new Promise((resolve) => {
8
- setTimeout(resolve, queueWaitTimeSecs * 1000 + 1);
9
- });
10
- }
11
- else {
12
- return Promise.resolve();
13
- }
14
- };
15
- exports.stopConsumer = stopConsumer;
@@ -1,10 +0,0 @@
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>;