@vercube/serverless 0.0.22 → 0.0.23

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/README.md CHANGED
@@ -3,10 +3,10 @@
3
3
  <br>
4
4
  <br>
5
5
 
6
- # @vercube/serverless
7
-
8
- Serverless deployment adapters for Vercube applications
9
-
6
+ # @vercube/serverless
7
+
8
+ Serverless deployment adapters for Vercube applications
9
+
10
10
  <a href="https://www.npmjs.com/package/@vercube/serverless">
11
11
  <img src="https://img.shields.io/npm/v/%40vercube%2Fserverless?style=for-the-badge&logo=npm&color=%23767eff" alt="npm"/>
12
12
  </a>
@@ -16,6 +16,9 @@
16
16
  <a href="https://github.com/vercube/vercube/blob/main/LICENSE" target="_blank">
17
17
  <img src="https://img.shields.io/npm/l/%40vercube%2Fserverless?style=for-the-badge&color=%23767eff" alt="License"/>
18
18
  </a>
19
+ <a href="https://codecov.io/gh/vercube/vercube" target="_blank">
20
+ <img src="https://img.shields.io/codecov/c/github/vercube/vercube?style=for-the-badge&color=%23767eff" alt="Coverage"/>
21
+ </a>
19
22
  <br/>
20
23
  <br/>
21
24
  </div>
@@ -107,12 +110,15 @@ Full support for AWS Lambda with API Gateway integration:
107
110
  Converts a Vercube App instance into a serverless handler function.
108
111
 
109
112
  **Parameters:**
113
+
110
114
  - `app` - The Vercube App instance that will handle requests
111
115
 
112
116
  **Returns:**
117
+
113
118
  - An async function that accepts serverless events and returns platform-specific responses
114
119
 
115
120
  **Example:**
121
+
116
122
  ```ts
117
123
  import { createApp } from '@vercube/core';
118
124
  import { toServerlessHandler } from '@vercube/serverless/aws-lambda';
@@ -128,12 +134,14 @@ export const handler = toServerlessHandler(app);
128
134
  The serverless adapters handle automatic conversion between platform-specific events and standard web requests:
129
135
 
130
136
  ### Request Conversion
137
+
131
138
  - **HTTP Method** - Extracted from event properties
132
139
  - **URL Construction** - Built from path, query parameters, and headers
133
140
  - **Headers** - Converted to standard Headers object
134
141
  - **Body** - Properly decoded and converted to Request body
135
142
 
136
143
  ### Response Conversion
144
+
137
145
  - **Status Code** - Mapped from Response status
138
146
  - **Headers** - Converted to platform-specific format
139
147
  - **Body** - Encoded appropriately (text vs binary)
@@ -160,8 +168,8 @@ import { toServerlessHandler } from '@vercube/serverless/aws-lambda';
160
168
 
161
169
  const app = createApp({
162
170
  logger: {
163
- level: 'debug'
164
- }
171
+ level: 'debug',
172
+ },
165
173
  });
166
174
 
167
175
  export const handler = toServerlessHandler(app);
@@ -180,13 +188,12 @@ Explore guides, API references, and best practices to master Vercube serverless
180
188
 
181
189
  This module is inspired by:
182
190
 
183
- * [Nitro AWS Lambda Preset](https://nitro.build/presets/aws-lambda)
184
- * [Hono AWS Lambda Adapter](https://hono.dev/guides/aws-lambda)
185
- * [Vercel Serverless Functions](https://vercel.com/docs/functions)
191
+ - [Nitro AWS Lambda Preset](https://nitro.build/presets/aws-lambda)
192
+ - [Hono AWS Lambda Adapter](https://hono.dev/guides/aws-lambda)
193
+ - [Vercel Serverless Functions](https://vercel.com/docs/functions)
186
194
 
187
195
  ---
188
196
 
189
197
  ## 🪪 License
190
198
 
191
199
  [MIT License](https://github.com/vercube/vercube/blob/main/LICENSE)
192
-
@@ -35,11 +35,11 @@ function getHeaderValue(headers, keys) {
35
35
  }
36
36
  /**
37
37
  * Extracts the HTTP method from an API Gateway event.
38
- *
38
+ *
39
39
  * Handles both v1 and v2 event formats:
40
40
  * - v1: Uses `httpMethod` property
41
41
  * - v2: Uses `requestContext.http.method` property
42
- *
42
+ *
43
43
  * @param event - The AWS API Gateway event object (v1 or v2 format)
44
44
  * @returns The HTTP method as a string, defaults to 'GET' if not found
45
45
  */
@@ -50,13 +50,13 @@ function getEventMethod(event) {
50
50
  }
51
51
  /**
52
52
  * Constructs a complete URL from an API Gateway event.
53
- *
53
+ *
54
54
  * Builds the URL by combining:
55
55
  * - Protocol (http/https based on X-Forwarded-Proto header)
56
56
  * - Hostname (from headers or requestContext)
57
57
  * - Path (from event path properties)
58
58
  * - Query string (from query parameters)
59
- *
59
+ *
60
60
  * @param event - The AWS API Gateway event object (v1 or v2 format)
61
61
  * @returns A complete URL object
62
62
  */
@@ -93,11 +93,11 @@ function getEventProtocol(event) {
93
93
  }
94
94
  /**
95
95
  * Extracts and formats query parameters from an API Gateway event.
96
- *
96
+ *
97
97
  * Handles both v1 and v2 event formats:
98
98
  * - v2: Uses `rawQueryString` if available
99
99
  * - v1: Combines `queryStringParameters` and `multiValueQueryStringParameters`
100
- *
100
+ *
101
101
  * @param event - The AWS API Gateway event object (v1 or v2 format)
102
102
  * @returns A formatted query string (without the leading '?')
103
103
  */
@@ -113,11 +113,11 @@ function getEventQuery(event) {
113
113
  }
114
114
  /**
115
115
  * Converts API Gateway event headers to a standard Headers object.
116
- *
116
+ *
117
117
  * Processes all headers from the event and handles cookies specially:
118
118
  * - Sets all event headers in the Headers object
119
119
  * - Appends cookies from the event's cookies array (v2 format)
120
- *
120
+ *
121
121
  * @param event - The AWS API Gateway event object (v1 or v2 format)
122
122
  * @returns A Headers object with all event headers and cookies
123
123
  */
@@ -133,12 +133,12 @@ function getEventHeaders(event) {
133
133
  }
134
134
  /**
135
135
  * Extracts the request body from an API Gateway event.
136
- *
136
+ *
137
137
  * Handles different body formats:
138
138
  * - Returns undefined if no body is present
139
139
  * - Decodes base64-encoded bodies when `isBase64Encoded` is true
140
140
  * - Returns the raw body string for regular requests
141
- *
141
+ *
142
142
  * @param event - The AWS API Gateway event object (v1 or v2 format)
143
143
  * @returns The request body as BodyInit (string, Buffer, or undefined)
144
144
  */
@@ -153,13 +153,13 @@ function getEventBody(event) {
153
153
  }
154
154
  /**
155
155
  * Converts an AWS API Gateway event to a standard Request object.
156
- *
156
+ *
157
157
  * This function handles both APIGatewayProxyEvent (v1) and APIGatewayProxyEventV2 (v2) formats,
158
158
  * extracting the HTTP method, URL, headers, and body to create a web-standard Request object.
159
- *
159
+ *
160
160
  * This file is highly inspired by the `awsRequest` from `nitro`
161
161
  * @see https://github.com/nitrojs/nitro/blob/v3/src/presets/aws-lambda/runtime/_utils.ts
162
- *
162
+ *
163
163
  * @param event - The AWS API Gateway event object (v1 or v2 format)
164
164
  * @returns A new Request object with the extracted event data
165
165
  * @throws {Error} If the event is invalid or missing required properties
@@ -192,16 +192,16 @@ const TEXT_CONTENT_TYPE_PATTERNS = [
192
192
  ];
193
193
  /**
194
194
  * Converts a standard web Response object to AWS API Gateway compatible response format.
195
- *
195
+ *
196
196
  * This function transforms the Response headers and cookies into the format expected by
197
197
  * AWS API Gateway proxy integrations. It handles both v1 and v2 API Gateway formats:
198
198
  * - v1: Uses `multiValueHeaders` for cookies
199
199
  * - v2: Uses `cookies` array for cookies
200
- *
200
+ *
201
201
  * The function processes all response headers, converting them to the appropriate format
202
202
  * for AWS Lambda integration. Headers with multiple values are joined with commas,
203
203
  * and cookies are handled specially for both API Gateway versions.
204
- *
204
+ *
205
205
  * @param response - The standard web Response object to convert
206
206
  * @returns An object containing headers and cookies in AWS API Gateway format
207
207
  * @throws {Error} If the response object is invalid or headers cannot be processed
@@ -224,21 +224,21 @@ function convertResponseToAWSResponse(response) {
224
224
  }
225
225
  /**
226
226
  * Converts a Response body to AWS API Gateway compatible format with proper encoding.
227
- *
227
+ *
228
228
  * AWS Lambda proxy integrations require special handling for binary content:
229
229
  * - Text content types are returned as UTF-8 strings
230
230
  * - Binary content types are base64 encoded with the `isBase64Encoded` flag set
231
- *
231
+ *
232
232
  * This function determines the appropriate encoding based on the response's content-type
233
233
  * header and converts the body accordingly. It supports both text and binary content
234
234
  * types, ensuring compatibility with API Gateway's payload encoding requirements.
235
- *
235
+ *
236
236
  * Binary media types should be configured as * in API Gateway settings.
237
237
  * @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html
238
- *
238
+ *
239
239
  * This function is heavily inspired by the `awsResponseBody` from `nitro`
240
240
  * @see https://github.com/nitrojs/nitro/blob/v3/src/presets/aws-lambda/runtime/_utils.ts
241
- *
241
+ *
242
242
  * @param response - The standard web Response object containing the body to convert
243
243
  * @returns A promise that resolves to an object with the encoded body and encoding flag
244
244
  * @throws {Error} If the response body cannot be read or converted
@@ -261,15 +261,15 @@ async function convertBodyToAWSResponse(response) {
261
261
  }
262
262
  /**
263
263
  * Determines if a content type should be treated as text content.
264
- *
264
+ *
265
265
  * This function uses a set of patterns to identify text-based content types:
266
266
  * - Content types starting with "text/" (e.g., text/plain, text/html)
267
267
  * - JavaScript, JSON, or XML content types
268
268
  * - Content types containing UTF-8 encoding specification
269
- *
269
+ *
270
270
  * The function performs case-insensitive matching to handle various content type
271
271
  * formats and specifications.
272
- *
272
+ *
273
273
  * @param contentType - The content type string to evaluate (e.g., "text/plain", "application/json")
274
274
  * @returns True if the content type should be treated as text, false otherwise
275
275
  */
@@ -279,15 +279,15 @@ function isTextType(contentType = DEFAULT_CONTENT_TYPE) {
279
279
  }
280
280
  /**
281
281
  * Converts a ReadableStream to a Buffer using Web Streams API.
282
- *
282
+ *
283
283
  * This function reads all chunks from a ReadableStream and concatenates them
284
284
  * into a single Buffer. It uses the modern Web Streams API with WritableStream
285
285
  * to efficiently process the stream data without blocking.
286
- *
286
+ *
287
287
  * The function handles stream errors gracefully and provides proper cleanup
288
288
  * of stream resources. It's designed to work with Response.body streams
289
289
  * from fetch API responses.
290
- *
290
+ *
291
291
  * @param data - The ReadableStream to convert to a Buffer
292
292
  * @returns A promise that resolves to a Buffer containing all stream data
293
293
  * @throws {Error} If the stream cannot be read or an error occurs during processing
@@ -314,25 +314,25 @@ function toBuffer(data) {
314
314
  //#region src/Adapters/aws-lambda/index.ts
315
315
  /**
316
316
  * Converts a Vercube App instance into an AWS Lambda handler function for API Gateway integration.
317
- *
317
+ *
318
318
  * This function creates a serverless handler that bridges between AWS API Gateway events and
319
319
  * the Vercube application framework. It handles the conversion of AWS Lambda events to standard
320
320
  * web Request objects, processes them through the Vercube app, and converts the responses back
321
321
  * to the format expected by AWS API Gateway.
322
- *
322
+ *
323
323
  * The handler supports both API Gateway v1 (APIGatewayProxyEvent) and v2 (APIGatewayProxyEventV2)
324
324
  * event formats, automatically detecting and handling the appropriate format. It processes:
325
325
  * - HTTP method, URL, headers, and body from the Lambda event
326
326
  * - Converts them to a standard web Request object
327
327
  * - Passes the request through the Vercube application
328
328
  * - Converts the Response back to AWS API Gateway format
329
- *
329
+ *
330
330
  * The returned handler function can be directly used as an AWS Lambda function handler,
331
331
  * providing seamless integration between AWS Lambda and Vercube applications.
332
- *
332
+ *
333
333
  * @param app - The Vercube App instance that will handle the requests
334
334
  * @returns An async function that accepts AWS API Gateway events and returns API Gateway responses
335
- *
335
+ *
336
336
  * @see {@link convertEventToRequest} For details on event to request conversion
337
337
  * @see {@link convertResponseToAWSResponse} For details on response header conversion
338
338
  * @see {@link convertBodyToAWSResponse} For details on response body conversion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercube/serverless",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "description": "Serverless module for Vercube framework",
5
5
  "repository": {
6
6
  "type": "git",
@@ -35,8 +35,8 @@
35
35
  ],
36
36
  "dependencies": {
37
37
  "ufo": "1.6.1",
38
- "@vercube/core": "0.0.22",
39
- "@vercube/di": "0.0.22"
38
+ "@vercube/core": "0.0.23",
39
+ "@vercube/di": "0.0.23"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/aws-lambda": "8.10.152",