@jaypie/express 1.2.3 → 1.2.4-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.
@@ -0,0 +1,53 @@
1
+ import type { IncomingHttpHeaders } from "node:http";
2
+ import { Readable } from "node:stream";
3
+ import type { LambdaContext, LambdaEvent } from "./types.js";
4
+ interface LambdaRequestOptions {
5
+ body?: Buffer | null;
6
+ headers: Record<string, string>;
7
+ lambdaContext: LambdaContext;
8
+ lambdaEvent: LambdaEvent;
9
+ method: string;
10
+ protocol: string;
11
+ remoteAddress: string;
12
+ url: string;
13
+ }
14
+ interface MockSocket {
15
+ destroy: () => void;
16
+ encrypted: boolean;
17
+ remoteAddress: string;
18
+ }
19
+ /**
20
+ * Mock IncomingMessage that extends Readable stream.
21
+ * Provides Express-compatible request interface from Lambda Function URL events.
22
+ */
23
+ export declare class LambdaRequest extends Readable {
24
+ readonly method: string;
25
+ readonly url: string;
26
+ readonly headers: IncomingHttpHeaders;
27
+ readonly httpVersion: string;
28
+ readonly httpVersionMajor: number;
29
+ readonly httpVersionMinor: number;
30
+ complete: boolean;
31
+ readonly socket: MockSocket;
32
+ readonly connection: MockSocket;
33
+ readonly originalUrl: string;
34
+ readonly path: string;
35
+ baseUrl: string;
36
+ body: unknown;
37
+ params: Record<string, string>;
38
+ query: Record<string, unknown>;
39
+ readonly _lambdaContext: LambdaContext;
40
+ readonly _lambdaEvent: LambdaEvent;
41
+ private bodyBuffer;
42
+ private bodyPushed;
43
+ constructor(options: LambdaRequestOptions);
44
+ _read(): void;
45
+ get(headerName: string): string | undefined;
46
+ header(headerName: string): string | undefined;
47
+ private normalizeHeaders;
48
+ }
49
+ /**
50
+ * Create a LambdaRequest from a Lambda event (Function URL, HTTP API v2, or REST API v1).
51
+ */
52
+ export declare function createLambdaRequest(event: LambdaEvent, context: LambdaContext): LambdaRequest;
53
+ export default LambdaRequest;
@@ -0,0 +1,37 @@
1
+ import type { OutgoingHttpHeaders } from "node:http";
2
+ import { Writable } from "node:stream";
3
+ import type { LambdaResponse } from "./types.js";
4
+ /**
5
+ * Mock ServerResponse that buffers the response.
6
+ * Collects status, headers, and body chunks, then returns a Lambda response.
7
+ */
8
+ export declare class LambdaResponseBuffered extends Writable {
9
+ statusCode: number;
10
+ statusMessage: string;
11
+ readonly socket: {
12
+ remoteAddress: string;
13
+ };
14
+ private _chunks;
15
+ private _headers;
16
+ private _headersSent;
17
+ private _resolve;
18
+ constructor();
19
+ getResult(): Promise<LambdaResponse>;
20
+ setHeader(name: string, value: number | string | string[]): this;
21
+ getHeader(name: string): number | string | string[] | undefined;
22
+ removeHeader(name: string): void;
23
+ getHeaders(): OutgoingHttpHeaders;
24
+ hasHeader(name: string): boolean;
25
+ getHeaderNames(): string[];
26
+ writeHead(statusCode: number, statusMessageOrHeaders?: OutgoingHttpHeaders | string, headers?: OutgoingHttpHeaders): this;
27
+ get headersSent(): boolean;
28
+ status(code: number): this;
29
+ json(data: unknown): this;
30
+ send(body?: Buffer | object | string): this;
31
+ _write(chunk: Buffer | string, encoding: BufferEncoding, // eslint-disable-line no-undef
32
+ callback: (error?: Error | null) => void): void;
33
+ _final(callback: (error?: Error | null) => void): void;
34
+ private buildResult;
35
+ private isBinaryContentType;
36
+ }
37
+ export default LambdaResponseBuffered;
@@ -0,0 +1,36 @@
1
+ import type { OutgoingHttpHeaders } from "node:http";
2
+ import { Writable } from "node:stream";
3
+ import type { ResponseStream } from "./types.js";
4
+ /**
5
+ * Mock ServerResponse that streams directly to Lambda responseStream.
6
+ * Uses awslambda.HttpResponseStream.from() to set status and headers.
7
+ */
8
+ export declare class LambdaResponseStreaming extends Writable {
9
+ statusCode: number;
10
+ statusMessage: string;
11
+ readonly socket: {
12
+ remoteAddress: string;
13
+ };
14
+ private _headers;
15
+ private _headersSent;
16
+ private _pendingWrites;
17
+ private _responseStream;
18
+ private _wrappedStream;
19
+ constructor(responseStream: ResponseStream);
20
+ setHeader(name: string, value: number | string | string[]): this;
21
+ getHeader(name: string): number | string | string[] | undefined;
22
+ removeHeader(name: string): void;
23
+ getHeaders(): OutgoingHttpHeaders;
24
+ hasHeader(name: string): boolean;
25
+ getHeaderNames(): string[];
26
+ writeHead(statusCode: number, statusMessageOrHeaders?: OutgoingHttpHeaders | string, headers?: OutgoingHttpHeaders): this;
27
+ get headersSent(): boolean;
28
+ flushHeaders(): void;
29
+ status(code: number): this;
30
+ json(data: unknown): this;
31
+ send(body?: Buffer | object | string): this;
32
+ _write(chunk: Buffer | string, encoding: BufferEncoding, // eslint-disable-line no-undef
33
+ callback: (error?: Error | null) => void): void;
34
+ _final(callback: (error?: Error | null) => void): void;
35
+ }
36
+ export default LambdaResponseStreaming;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,50 @@
1
+ import type { Application } from "express";
2
+ import type { CreateLambdaHandlerOptions, LambdaContext, LambdaEvent, LambdaHandler, LambdaStreamHandler } from "./types.js";
3
+ /**
4
+ * Get the current Lambda invoke context.
5
+ * Used by getCurrentInvokeUuid adapter to get the request ID.
6
+ */
7
+ export declare function getCurrentInvoke(): {
8
+ context: LambdaContext;
9
+ event: LambdaEvent;
10
+ } | null;
11
+ /**
12
+ * Create a Lambda handler that buffers the Express response.
13
+ * Returns the complete response as a Lambda response object.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import express from "express";
18
+ * import { createLambdaHandler } from "@jaypie/express";
19
+ *
20
+ * const app = express();
21
+ * app.get("/", (req, res) => res.json({ message: "Hello" }));
22
+ *
23
+ * export const handler = createLambdaHandler(app);
24
+ * ```
25
+ */
26
+ export declare function createLambdaHandler(app: Application, _options?: CreateLambdaHandlerOptions): LambdaHandler;
27
+ /**
28
+ * Create a Lambda handler that streams the Express response.
29
+ * Uses awslambda.streamifyResponse() for Lambda response streaming.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * import express from "express";
34
+ * import { createLambdaStreamHandler } from "@jaypie/express";
35
+ *
36
+ * const app = express();
37
+ * app.get("/stream", (req, res) => {
38
+ * res.setHeader("Content-Type", "text/event-stream");
39
+ * res.write("data: Hello\n\n");
40
+ * res.end();
41
+ * });
42
+ *
43
+ * export const handler = createLambdaStreamHandler(app);
44
+ * ```
45
+ */
46
+ export declare function createLambdaStreamHandler(app: Application, _options?: CreateLambdaHandlerOptions): LambdaStreamHandler;
47
+ export { LambdaRequest, createLambdaRequest } from "./LambdaRequest.js";
48
+ export { LambdaResponseBuffered } from "./LambdaResponseBuffered.js";
49
+ export { LambdaResponseStreaming } from "./LambdaResponseStreaming.js";
50
+ export type { ApiGatewayV1Event, AwsLambdaGlobal, CreateLambdaHandlerOptions, FunctionUrlEvent, HttpResponseStreamMetadata, LambdaContext, LambdaEvent, LambdaHandler, LambdaHandlerFactory, LambdaResponse, LambdaStreamHandler, LambdaStreamHandlerFactory, ResponseStream, } from "./types.js";
@@ -0,0 +1,107 @@
1
+ import type { Application } from "express";
2
+ export interface LambdaContext {
3
+ awsRequestId: string;
4
+ callbackWaitsForEmptyEventLoop?: boolean;
5
+ functionName?: string;
6
+ functionVersion?: string;
7
+ invokedFunctionArn?: string;
8
+ logGroupName?: string;
9
+ logStreamName?: string;
10
+ memoryLimitInMB?: string;
11
+ [key: string]: unknown;
12
+ }
13
+ export interface ApiGatewayV1Event {
14
+ body?: string | null;
15
+ headers: Record<string, string>;
16
+ httpMethod: string;
17
+ isBase64Encoded: boolean;
18
+ multiValueHeaders?: Record<string, string[]>;
19
+ multiValueQueryStringParameters?: Record<string, string[]> | null;
20
+ path: string;
21
+ pathParameters?: Record<string, string> | null;
22
+ queryStringParameters?: Record<string, string> | null;
23
+ requestContext: {
24
+ accountId: string;
25
+ apiId: string;
26
+ domainName?: string;
27
+ httpMethod: string;
28
+ identity: {
29
+ sourceIp: string;
30
+ userAgent?: string;
31
+ };
32
+ path: string;
33
+ protocol: string;
34
+ requestId: string;
35
+ requestTime?: string;
36
+ requestTimeEpoch?: number;
37
+ resourceId?: string;
38
+ resourcePath?: string;
39
+ stage: string;
40
+ };
41
+ resource?: string;
42
+ stageVariables?: Record<string, string> | null;
43
+ }
44
+ export interface FunctionUrlEvent {
45
+ body?: string;
46
+ cookies?: string[];
47
+ headers: Record<string, string>;
48
+ isBase64Encoded: boolean;
49
+ rawPath: string;
50
+ rawQueryString: string;
51
+ requestContext: {
52
+ accountId: string;
53
+ apiId: string;
54
+ domainName: string;
55
+ domainPrefix: string;
56
+ http: {
57
+ method: string;
58
+ path: string;
59
+ protocol: string;
60
+ sourceIp: string;
61
+ userAgent: string;
62
+ };
63
+ requestId: string;
64
+ routeKey: string;
65
+ stage: string;
66
+ time: string;
67
+ timeEpoch: number;
68
+ };
69
+ routeKey: string;
70
+ version: "2.0";
71
+ }
72
+ export type LambdaEvent = ApiGatewayV1Event | FunctionUrlEvent;
73
+ export interface LambdaResponse {
74
+ body: string;
75
+ cookies?: string[];
76
+ headers: Record<string, string>;
77
+ isBase64Encoded: boolean;
78
+ statusCode: number;
79
+ }
80
+ export interface ResponseStream {
81
+ end(): void;
82
+ write(chunk: string | Uint8Array): void;
83
+ }
84
+ export interface HttpResponseStreamMetadata {
85
+ headers: Record<string, string>;
86
+ statusCode: number;
87
+ }
88
+ /**
89
+ * Global awslambda object provided by Lambda runtime.
90
+ * This is only available when running in AWS Lambda.
91
+ */
92
+ export interface AwsLambdaGlobal {
93
+ HttpResponseStream: {
94
+ from(stream: ResponseStream, metadata: HttpResponseStreamMetadata): ResponseStream;
95
+ };
96
+ streamifyResponse<TEvent = unknown>(handler: (event: TEvent, responseStream: ResponseStream, context: LambdaContext) => Promise<void>): (event: TEvent, context: LambdaContext) => Promise<void>;
97
+ }
98
+ export type LambdaHandler = (event: LambdaEvent, context: LambdaContext) => Promise<LambdaResponse>;
99
+ export type LambdaStreamHandler = (event: LambdaEvent, context: LambdaContext) => Promise<void>;
100
+ export type CreateLambdaHandlerOptions = {
101
+ /**
102
+ * Optional name for logging and debugging
103
+ */
104
+ name?: string;
105
+ };
106
+ export type LambdaHandlerFactory = (app: Application, options?: CreateLambdaHandlerOptions) => LambdaHandler;
107
+ export type LambdaStreamHandlerFactory = (app: Application, options?: CreateLambdaHandlerOptions) => LambdaStreamHandler;
@@ -1,10 +1,10 @@
1
1
  import type { Request } from "express";
2
2
  /**
3
3
  * Get the current invoke UUID from Lambda context.
4
- * Works in both serverless-express mode and Lambda Web Adapter mode.
4
+ * Works with Jaypie Lambda adapter and Lambda Web Adapter mode.
5
5
  *
6
- * @param req - Optional Express request object. Required for Web Adapter mode
7
- * to extract the x-amzn-request-id header.
6
+ * @param req - Optional Express request object. Used to extract context
7
+ * from Web Adapter headers or Jaypie adapter's _lambdaContext.
8
8
  * @returns The AWS request ID or undefined if not in Lambda context
9
9
  */
10
10
  declare function getCurrentInvokeUuid(req?: Request): string | undefined;