@travetto/web-aws-lambda 7.1.3 → 8.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/web-aws-lambda",
3
- "version": "7.1.3",
3
+ "version": "8.0.0-alpha.0",
4
4
  "type": "module",
5
5
  "description": "Web APIs entry point support for AWS Lambdas.",
6
6
  "keywords": [
@@ -26,13 +26,13 @@
26
26
  "directory": "module/web-aws-lambda"
27
27
  },
28
28
  "dependencies": {
29
- "@travetto/web": "^7.1.3",
30
- "@types/aws-lambda": "^8.10.159"
29
+ "@travetto/web": "^8.0.0-alpha.0",
30
+ "@types/aws-lambda": "^8.10.161"
31
31
  },
32
32
  "peerDependencies": {
33
- "@travetto/cli": "^7.1.3",
34
- "@travetto/pack": "^7.1.3",
35
- "@travetto/test": "^7.1.3"
33
+ "@travetto/cli": "^8.0.0-alpha.0",
34
+ "@travetto/pack": "^8.0.0-alpha.0",
35
+ "@travetto/test": "^8.0.0-alpha.0"
36
36
  },
37
37
  "peerDependenciesMeta": {
38
38
  "@travetto/test": {
package/src/util.ts CHANGED
@@ -1,8 +1,6 @@
1
- import { buffer } from 'node:stream/consumers';
2
-
3
1
  import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
4
2
 
5
- import { castTo } from '@travetto/runtime';
3
+ import { BinaryUtil, castTo, CodecUtil, type BinaryArray } from '@travetto/runtime';
6
4
  import { WebBodyUtil, WebCommonUtil, WebRequest, WebResponse } from '@travetto/web';
7
5
 
8
6
  export class AwsLambdaWebUtil {
@@ -12,7 +10,10 @@ export class AwsLambdaWebUtil {
12
10
  */
13
11
  static toWebRequest(event: APIGatewayProxyEvent): WebRequest {
14
12
  // Build request
15
- const body = event.body ? Buffer.from(event.body, event.isBase64Encoded ? 'base64' : 'utf8') : undefined;
13
+ const body = !event.body ? undefined :
14
+ event.isBase64Encoded ?
15
+ CodecUtil.fromBase64String(event.body) :
16
+ CodecUtil.fromUTF8String(event.body);
16
17
 
17
18
  return new WebRequest({
18
19
  context: {
@@ -25,7 +26,7 @@ export class AwsLambdaWebUtil {
25
26
  path: event.path,
26
27
  },
27
28
  headers: { ...event.headers, ...event.multiValueHeaders },
28
- body: WebBodyUtil.markRaw(body)
29
+ body: WebBodyUtil.markRawBinary(body)
29
30
  });
30
31
  }
31
32
 
@@ -37,13 +38,11 @@ export class AwsLambdaWebUtil {
37
38
  context: response.context,
38
39
  ...WebBodyUtil.toBinaryMessage(response)
39
40
  });
40
- let output: Buffer = Buffer.alloc(0);
41
- if (Buffer.isBuffer(binaryResponse.body)) {
42
- output = binaryResponse.body;
43
- } else if (binaryResponse.body) {
44
- output = await buffer(binaryResponse.body);
45
- }
46
- const isBase64Encoded = !!output.length && base64Encoded;
41
+ const output: BinaryArray = binaryResponse.body ?
42
+ await BinaryUtil.toBinaryArray(binaryResponse.body) :
43
+ BinaryUtil.makeBinaryArray(0);
44
+
45
+ const isBase64Encoded = !!output.byteLength && base64Encoded;
47
46
  const headers: Record<string, string> = {};
48
47
  const multiValueHeaders: Record<string, string[]> = {};
49
48
 
@@ -58,7 +57,7 @@ export class AwsLambdaWebUtil {
58
57
  return {
59
58
  statusCode: WebCommonUtil.getStatusCode(binaryResponse),
60
59
  isBase64Encoded,
61
- body: output.toString(isBase64Encoded ? 'base64' : 'utf8'),
60
+ body: isBase64Encoded ? CodecUtil.toBase64String(output) : CodecUtil.toUTF8String(output),
62
61
  headers,
63
62
  multiValueHeaders,
64
63
  };
@@ -2,7 +2,7 @@ import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
2
2
 
3
3
  import { Inject, Injectable } from '@travetto/di';
4
4
  import { type WebDispatcher, type WebFilterContext, type WebRequest, WebResponse } from '@travetto/web';
5
- import { AppError, asFull, castTo } from '@travetto/runtime';
5
+ import { RuntimeError, asFull, BinaryUtil, castTo, CodecUtil, type BinaryArray } from '@travetto/runtime';
6
6
 
7
7
  import { WebTestDispatchUtil } from '@travetto/web/support/test/dispatch-util.ts';
8
8
 
@@ -11,15 +11,15 @@ import type { AwsLambdaWebHandler } from '../../src/handler.ts';
11
11
  /**
12
12
  * Create an api gateway event given a web request
13
13
  */
14
- function toLambdaEvent(request: WebRequest): APIGatewayProxyEvent {
14
+ function toLambdaEvent(request: WebRequest<BinaryArray>): APIGatewayProxyEvent {
15
15
  const body = request.body;
16
16
  const headers: Record<string, string> = {};
17
17
  const multiValueHeaders: Record<string, string[]> = {};
18
18
  const queryStringParameters: Record<string, string> = {};
19
19
  const multiValueQueryStringParameters: Record<string, string[]> = {};
20
20
 
21
- if (!(body === undefined || body === null || Buffer.isBuffer(body))) {
22
- throw new AppError('Unsupported request type, only buffer bodies supported');
21
+ if (body && !BinaryUtil.isBinaryArray(body)) {
22
+ throw new RuntimeError('Unsupported request type, only buffer bodies supported');
23
23
  }
24
24
 
25
25
  request.headers.forEach((v, k) => {
@@ -46,7 +46,7 @@ function toLambdaEvent(request: WebRequest): APIGatewayProxyEvent {
46
46
  headers,
47
47
  multiValueHeaders,
48
48
  isBase64Encoded: true,
49
- body: body?.toString('base64')!,
49
+ body: request.body ? CodecUtil.toBase64String(request.body) : null,
50
50
  requestContext: castTo({
51
51
  identity: castTo({ sourceIp: '127.0.0.1' }),
52
52
  }),
@@ -63,13 +63,14 @@ export class LocalAwsLambdaWebDispatcher implements WebDispatcher {
63
63
  app: AwsLambdaWebHandler;
64
64
 
65
65
  async dispatch({ request }: WebFilterContext): Promise<WebResponse> {
66
- const event = toLambdaEvent(await WebTestDispatchUtil.applyRequestBody(request));
67
-
66
+ const event = toLambdaEvent(await WebTestDispatchUtil.applyRequestBody(request, true));
68
67
  const response = await this.app.handle(event, asFull<Context>({}));
69
68
 
70
69
  return WebTestDispatchUtil.finalizeResponseBody(
71
70
  new WebResponse<unknown>({
72
- body: Buffer.from(response.body, response.isBase64Encoded ? 'base64' : 'utf8'),
71
+ body: response.isBase64Encoded ?
72
+ CodecUtil.fromBase64String(response.body) :
73
+ CodecUtil.fromUTF8String(response.body),
73
74
  headers: { ...response.headers ?? {}, ...response.multiValueHeaders ?? {} },
74
75
  context: {
75
76
  httpStatusCode: response.statusCode