@yopdev/dev-server 1.5.0 → 1.5.7

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.
@@ -16,7 +16,8 @@ class CloudFormationEventsProxy extends cloudformation_1.CloudFormationSetup {
16
16
  const actual = Object.entries(attributes).flatMap((entry) => entry[1].Value + entry[0]);
17
17
  return required.filter((e) => actual.includes(e)).length > 0;
18
18
  },
19
- }), config.prepare, (_name, _config, routes) => routes, config);
19
+ }), config.prepare, config);
20
+ this.afterStart = (_name, _config, routes) => routes;
20
21
  }
21
22
  logHandler(handler) {
22
23
  return `detected sns handler ${handler.name}`;
@@ -3,10 +3,11 @@ import { DevServerConfig } from './config';
3
3
  import { HttpSettings } from './http-server';
4
4
  import { Authorizer, Route } from './lambda-http-proxy';
5
5
  import { Callback, Service, Startable } from './services';
6
- export declare const newLambdaProxyFromCloudFormationTemplate: (name: string, settings: HttpSettings, config: CloudFormationLambdaProxyConfig, callback: Callback<string>) => Startable<Service<string>>;
7
- type CloudFormationLambdaProxyConfig = {
6
+ export declare const newLambdaProxyFromCloudFormationTemplate: <C>(name: string, settings: HttpSettings, config: CloudFormationLambdaProxyConfig<C>, callback: Callback<string>) => Startable<Service<string>>;
7
+ type CloudFormationLambdaProxyConfig<C> = {
8
8
  authorizer?: (config: DevServerConfig) => Authorizer | undefined;
9
- extraRoutes: Route[];
9
+ extraRoutes: Route<C>[];
10
10
  prepare?: (config: DevServerConfig) => Promise<void>;
11
+ context?: () => C;
11
12
  } & CloudFormationSetupConfig;
12
13
  export {};
@@ -14,27 +14,28 @@ class CloudFormationLambdaProxy extends cloudformation_1.CloudFormationSetup {
14
14
  method: new RegExp(event.Properties.Method),
15
15
  path: new RegExp(`^${pathWithCapturingGroups}${QUERY_STRING_OR_LOCATION_REG_EXP}?$`),
16
16
  weight: this.computeWeight(pathWithCapturingGroups),
17
- handler: this.pathParameterCapture(new RegExp(pathWithCapturingGroups), handler),
18
- })), config.prepare, (name, cfg, routes) => {
19
- return (0, lambda_http_proxy_1.newLambdaHttpProxy)(`${name}:apigw`, {
20
- settings: this.settings,
21
- routes: this.extraRoutes.concat(routes),
22
- authorizer: (config?.authorizer || (() => undefined))(cfg),
23
- }, callback);
24
- }, config);
17
+ handler: this.pathParameterCapture(new RegExp(pathWithCapturingGroups), handler, config.context),
18
+ })), config.prepare, config);
25
19
  this.settings = settings;
26
20
  this.extraRoutes = extraRoutes;
21
+ this.callback = callback;
22
+ this.afterStart = (name, cfg, routes) => (0, lambda_http_proxy_1.newLambdaHttpProxy)(`${name}:apigw`, {
23
+ settings: this.settings,
24
+ routes: this.extraRoutes.concat(routes),
25
+ authorizer: (this.authorizer || (() => undefined))(cfg),
26
+ }, this.callback);
27
27
  this.computeWeight = (path) => this.segments(path) - this.variables(path);
28
28
  this.segments = (path) => path.split('/').length;
29
29
  this.variables = (path) => path.split('(?<').length;
30
30
  this.pathWithCapturingGroups = (source) => [...source.matchAll(PATH_VARIABLE_CAPTURE)].reduce((prev, curr) => prev.replaceAll(curr[0], `(?<${this.normalizeCaptureGroupName(curr[1])}>.*)`), source);
31
31
  this.normalizeCaptureGroupName = (original) => (original === 'proxy+' ? PROXY_PATH_PARAM : original);
32
- this.pathParameterCapture = (pathWithCapturingGroups, handler) => async (event) => {
32
+ this.pathParameterCapture = (pathWithCapturingGroups, handler, context) => async (event) => {
33
33
  const pathParameters = pathWithCapturingGroups.exec(event.path)?.groups;
34
34
  event.pathParameters = Object.fromEntries((Object.entries(pathParameters ?? {}))
35
35
  .map(([k, v]) => [k, decodeURIComponent(v)]));
36
- return handler(event);
36
+ return handler(event, context?.());
37
37
  };
38
+ this.authorizer = config?.authorizer;
38
39
  }
39
40
  logHandler(handler) {
40
41
  return `detected handler for ${handler.method} on ${handler.path} with weight ${handler.weight}`;
@@ -5,13 +5,13 @@ export declare abstract class CloudFormationSetup<P, H, R, F> implements Startab
5
5
  private readonly eventFilter;
6
6
  private readonly handlerResolver;
7
7
  private readonly beforeStart;
8
- private readonly afterStart;
9
8
  private readonly template;
10
9
  private readonly handlers;
11
10
  private readonly handlerNameResolver;
12
11
  private readonly LOGGER;
13
- constructor(name: string, eventFilter: (event: Event<P>) => boolean, handlerResolver: (event: Event<P>, handler: H) => Promise<R>, beforeStart: ((config: DevServerConfig) => Promise<void>) | undefined, afterStart: (name: string, config: DevServerConfig, routes: R[]) => F, config: CloudFormationSetupConfig);
12
+ constructor(name: string, eventFilter: (event: Event<P>) => boolean, handlerResolver: (event: Event<P>, handler: H) => Promise<R>, beforeStart: ((config: DevServerConfig) => Promise<void>) | undefined, config: CloudFormationSetupConfig);
14
13
  start: (config: DevServerConfig) => Promise<F>;
14
+ protected abstract afterStart: (name: string, config: DevServerConfig, routes: R[]) => F;
15
15
  private resolvedHandlers;
16
16
  abstract logHandler(handler: R): string;
17
17
  private parseApiEvents;
@@ -6,12 +6,11 @@ const fs_1 = require("fs");
6
6
  const js_yaml_1 = require("js-yaml");
7
7
  const logging_1 = require("@yopdev/logging");
8
8
  class CloudFormationSetup {
9
- constructor(name, eventFilter, handlerResolver, beforeStart, afterStart, config) {
9
+ constructor(name, eventFilter, handlerResolver, beforeStart, config) {
10
10
  this.name = name;
11
11
  this.eventFilter = eventFilter;
12
12
  this.handlerResolver = handlerResolver;
13
13
  this.beforeStart = beforeStart;
14
- this.afterStart = afterStart;
15
14
  this.start = async (config) => (this.beforeStart || Promise.resolve)(config)
16
15
  .then(this.resolvedHandlers)
17
16
  .then(async (routes) => this.afterStart(this.name, config, routes));
@@ -3,17 +3,18 @@ import { APIGatewayEventDefaultAuthorizerContext, APIGatewayProxyEvent, APIGatew
3
3
  import { IncomingMessage } from "http";
4
4
  import { Service, Callback } from "./services";
5
5
  export declare const UNAUTHORIZED: Error;
6
- export declare const newLambdaHttpProxy: (name: string, config: {
6
+ export declare const newLambdaHttpProxy: <C>(name: string, config: {
7
7
  settings: HttpSettings;
8
- routes: Route[];
8
+ routes: Route<C>[];
9
9
  mapper?: (request: IncomingMessage, body: string) => Promise<APIGatewayProxyEvent>;
10
10
  authorizer?: Authorizer;
11
+ context?: () => C;
11
12
  }, callback?: Callback<string>) => Service<string>;
12
- export type Route = {
13
+ export type Route<C> = {
13
14
  method: RegExp;
14
15
  path: RegExp;
15
16
  weight: number;
16
17
  authorizer?: Authorizer;
17
- handler: (event: APIGatewayProxyEvent) => Promise<APIGatewayProxyResult>;
18
+ handler: (event: APIGatewayProxyEvent, context: C) => Promise<APIGatewayProxyResult>;
18
19
  };
19
20
  export type Authorizer = (authorization: string) => Promise<APIGatewayEventDefaultAuthorizerContext | undefined>;
@@ -7,22 +7,23 @@ const mappers_1 = require("./mappers");
7
7
  const responses_1 = require("./responses");
8
8
  const services_1 = require("./services");
9
9
  exports.UNAUTHORIZED = new Error('UNAUTHORIZED');
10
- const newLambdaHttpProxy = (name, config, callback) => new services_1.Service(new LambdaHttpProxy(name, config.settings, config.routes, config.mapper ?? mappers_1.mapToLambdaEvent, config.authorizer ?? (() => Promise.resolve(undefined))), callback);
10
+ const newLambdaHttpProxy = (name, config, callback) => new services_1.Service(new LambdaHttpProxy(name, config.settings, config.routes, config.mapper ?? mappers_1.mapToLambdaEvent, config.authorizer ?? (() => Promise.resolve(undefined)), config.context), callback);
11
11
  exports.newLambdaHttpProxy = newLambdaHttpProxy;
12
12
  class LambdaHttpProxy {
13
- constructor(name, settings, routes, mapper, defaultAuthorizer) {
13
+ constructor(name, settings, routes, mapper, defaultAuthorizer, context = () => undefined) {
14
14
  this.name = name;
15
15
  this.routes = routes;
16
16
  this.mapper = mapper;
17
17
  this.defaultAuthorizer = defaultAuthorizer;
18
+ this.context = context;
18
19
  this.start = () => this.server.start();
19
20
  this.stop = () => this.server.stop();
20
21
  this.handler = (authorizer, lambdaHandler) => (request, body, response) => this.mapper(request, body)
21
22
  .then(async (proxyEvent) => authorizer(proxyEvent.headers['authorization'])
22
23
  .then(async (context) => {
23
24
  proxyEvent.requestContext.authorizer = context;
24
- return lambdaHandler(proxyEvent)
25
- .then((lambda) => (0, responses_1.writeResponse)(response, lambda.statusCode, lambda.body, lambda.headers?.['content-type']?.toString() ?? undefined, lambda.headers?.['location']?.toString() ?? undefined))
25
+ return lambdaHandler(proxyEvent, this.context())
26
+ .then((lambda) => (0, responses_1.writeResponse)(response, lambda.statusCode, lambda.isBase64Encoded ? Buffer.from(lambda.body, 'base64') : lambda.body, lambda.headers?.['content-type']?.toString() ?? undefined, lambda.headers?.['location']?.toString() ?? undefined))
26
27
  .catch((e) => {
27
28
  this.LOGGER.error(e, 'request failed to execute');
28
29
  (0, responses_1.internalServerError)(response, e.body);
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { ServerResponse } from 'http';
3
4
  export declare function internalServerError(res: ServerResponse, body: any): void;
4
- export declare function writeResponse(res: ServerResponse, statusCode: number, body: string, contentType?: string, location?: string): void;
5
+ export declare function writeResponse(res: ServerResponse, statusCode: number, body: string | Buffer, contentType?: string, location?: string): void;
package/dist/src/sqs.js CHANGED
@@ -14,7 +14,7 @@ class Sqs {
14
14
  .send(new client_sqs_1.CreateQueueCommand({
15
15
  QueueName: fifo ? `${name}.fifo` : name,
16
16
  Attributes: {
17
- FifoQueue: fifo ? 'true' : 'false',
17
+ FifoQueue: fifo ? 'true' : undefined,
18
18
  }
19
19
  }))
20
20
  .then((create) => this.client
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yopdev/dev-server",
3
- "version": "1.5.0",
3
+ "version": "1.5.7",
4
4
  "scripts": {
5
5
  "compile": "tsc",
6
6
  "pretest": "npm run compile",