@jaypie/express 1.2.2 → 1.2.4-rc0
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/dist/cjs/adapter/LambdaRequest.d.ts +53 -0
- package/dist/cjs/adapter/LambdaResponseBuffered.d.ts +41 -0
- package/dist/cjs/adapter/LambdaResponseStreaming.d.ts +40 -0
- package/dist/cjs/adapter/__tests__/LambdaRequest.spec.d.ts +1 -0
- package/dist/cjs/adapter/__tests__/LambdaResponseBuffered.spec.d.ts +1 -0
- package/dist/cjs/adapter/__tests__/LambdaResponseStreaming.spec.d.ts +1 -0
- package/dist/cjs/adapter/__tests__/integration.spec.d.ts +1 -0
- package/dist/cjs/adapter/index.d.ts +50 -0
- package/dist/cjs/adapter/types.d.ts +75 -0
- package/dist/cjs/createServer.d.ts +60 -0
- package/dist/cjs/getCurrentInvokeUuid.adapter.d.ts +10 -1
- package/dist/cjs/getCurrentInvokeUuid.webadapter.d.ts +12 -0
- package/dist/cjs/index.cjs +815 -53
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +7 -2
- package/dist/esm/adapter/LambdaRequest.d.ts +53 -0
- package/dist/esm/adapter/LambdaResponseBuffered.d.ts +41 -0
- package/dist/esm/adapter/LambdaResponseStreaming.d.ts +40 -0
- package/dist/esm/adapter/__tests__/LambdaRequest.spec.d.ts +1 -0
- package/dist/esm/adapter/__tests__/LambdaResponseBuffered.spec.d.ts +1 -0
- package/dist/esm/adapter/__tests__/LambdaResponseStreaming.spec.d.ts +1 -0
- package/dist/esm/adapter/__tests__/integration.spec.d.ts +1 -0
- package/dist/esm/adapter/index.d.ts +50 -0
- package/dist/esm/adapter/types.d.ts +75 -0
- package/dist/esm/createServer.d.ts +60 -0
- package/dist/esm/getCurrentInvokeUuid.adapter.d.ts +10 -1
- package/dist/esm/getCurrentInvokeUuid.webadapter.d.ts +12 -0
- package/dist/esm/index.d.ts +7 -2
- package/dist/esm/index.js +806 -52
- package/dist/esm/index.js.map +1 -1
- package/package.json +4 -2
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { IncomingHttpHeaders } from "node:http";
|
|
2
|
+
import { Readable } from "node:stream";
|
|
3
|
+
import type { FunctionUrlEvent, LambdaContext } from "./types.js";
|
|
4
|
+
interface LambdaRequestOptions {
|
|
5
|
+
body?: Buffer | null;
|
|
6
|
+
headers: Record<string, string>;
|
|
7
|
+
lambdaContext: LambdaContext;
|
|
8
|
+
lambdaEvent: FunctionUrlEvent;
|
|
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: FunctionUrlEvent;
|
|
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 Function URL event.
|
|
51
|
+
*/
|
|
52
|
+
export declare function createLambdaRequest(event: FunctionUrlEvent, context: LambdaContext): LambdaRequest;
|
|
53
|
+
export default LambdaRequest;
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
cork: () => void;
|
|
13
|
+
destroy: () => void;
|
|
14
|
+
remoteAddress: string;
|
|
15
|
+
uncork: () => void;
|
|
16
|
+
writable: boolean;
|
|
17
|
+
};
|
|
18
|
+
private _chunks;
|
|
19
|
+
private _headers;
|
|
20
|
+
private _headersSent;
|
|
21
|
+
private _resolve;
|
|
22
|
+
constructor();
|
|
23
|
+
getResult(): Promise<LambdaResponse>;
|
|
24
|
+
setHeader(name: string, value: number | string | string[]): this;
|
|
25
|
+
getHeader(name: string): number | string | string[] | undefined;
|
|
26
|
+
removeHeader(name: string): void;
|
|
27
|
+
getHeaders(): OutgoingHttpHeaders;
|
|
28
|
+
hasHeader(name: string): boolean;
|
|
29
|
+
getHeaderNames(): string[];
|
|
30
|
+
writeHead(statusCode: number, statusMessageOrHeaders?: OutgoingHttpHeaders | string, headers?: OutgoingHttpHeaders): this;
|
|
31
|
+
get headersSent(): boolean;
|
|
32
|
+
status(code: number): this;
|
|
33
|
+
json(data: unknown): this;
|
|
34
|
+
send(body?: Buffer | object | string): this;
|
|
35
|
+
_write(chunk: Buffer | string, encoding: BufferEncoding, // eslint-disable-line no-undef
|
|
36
|
+
callback: (error?: Error | null) => void): void;
|
|
37
|
+
_final(callback: (error?: Error | null) => void): void;
|
|
38
|
+
private buildResult;
|
|
39
|
+
private isBinaryContentType;
|
|
40
|
+
}
|
|
41
|
+
export default LambdaResponseBuffered;
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
cork: () => void;
|
|
13
|
+
destroy: () => void;
|
|
14
|
+
remoteAddress: string;
|
|
15
|
+
uncork: () => void;
|
|
16
|
+
writable: boolean;
|
|
17
|
+
};
|
|
18
|
+
private _headers;
|
|
19
|
+
private _headersSent;
|
|
20
|
+
private _pendingWrites;
|
|
21
|
+
private _responseStream;
|
|
22
|
+
private _wrappedStream;
|
|
23
|
+
constructor(responseStream: ResponseStream);
|
|
24
|
+
setHeader(name: string, value: number | string | string[]): this;
|
|
25
|
+
getHeader(name: string): number | string | string[] | undefined;
|
|
26
|
+
removeHeader(name: string): void;
|
|
27
|
+
getHeaders(): OutgoingHttpHeaders;
|
|
28
|
+
hasHeader(name: string): boolean;
|
|
29
|
+
getHeaderNames(): string[];
|
|
30
|
+
writeHead(statusCode: number, statusMessageOrHeaders?: OutgoingHttpHeaders | string, headers?: OutgoingHttpHeaders): this;
|
|
31
|
+
get headersSent(): boolean;
|
|
32
|
+
flushHeaders(): void;
|
|
33
|
+
status(code: number): this;
|
|
34
|
+
json(data: unknown): this;
|
|
35
|
+
send(body?: Buffer | object | string): this;
|
|
36
|
+
_write(chunk: Buffer | string, encoding: BufferEncoding, // eslint-disable-line no-undef
|
|
37
|
+
callback: (error?: Error | null) => void): void;
|
|
38
|
+
_final(callback: (error?: Error | null) => void): void;
|
|
39
|
+
}
|
|
40
|
+
export default LambdaResponseStreaming;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Application } from "express";
|
|
2
|
+
import type { CreateLambdaHandlerOptions, FunctionUrlEvent, LambdaContext, 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: FunctionUrlEvent;
|
|
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 { AwsLambdaGlobal, CreateLambdaHandlerOptions, FunctionUrlEvent, HttpResponseStreamMetadata, LambdaContext, LambdaHandler, LambdaHandlerFactory, LambdaResponse, LambdaStreamHandler, LambdaStreamHandlerFactory, ResponseStream, } from "./types.js";
|
|
@@ -0,0 +1,75 @@
|
|
|
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 FunctionUrlEvent {
|
|
14
|
+
body?: string;
|
|
15
|
+
cookies?: string[];
|
|
16
|
+
headers: Record<string, string>;
|
|
17
|
+
isBase64Encoded: boolean;
|
|
18
|
+
rawPath: string;
|
|
19
|
+
rawQueryString: string;
|
|
20
|
+
requestContext: {
|
|
21
|
+
accountId: string;
|
|
22
|
+
apiId: string;
|
|
23
|
+
domainName: string;
|
|
24
|
+
domainPrefix: string;
|
|
25
|
+
http: {
|
|
26
|
+
method: string;
|
|
27
|
+
path: string;
|
|
28
|
+
protocol: string;
|
|
29
|
+
sourceIp: string;
|
|
30
|
+
userAgent: string;
|
|
31
|
+
};
|
|
32
|
+
requestId: string;
|
|
33
|
+
routeKey: string;
|
|
34
|
+
stage: string;
|
|
35
|
+
time: string;
|
|
36
|
+
timeEpoch: number;
|
|
37
|
+
};
|
|
38
|
+
routeKey: string;
|
|
39
|
+
version: "2.0";
|
|
40
|
+
}
|
|
41
|
+
export interface LambdaResponse {
|
|
42
|
+
body: string;
|
|
43
|
+
cookies?: string[];
|
|
44
|
+
headers: Record<string, string>;
|
|
45
|
+
isBase64Encoded: boolean;
|
|
46
|
+
statusCode: number;
|
|
47
|
+
}
|
|
48
|
+
export interface ResponseStream {
|
|
49
|
+
end(): void;
|
|
50
|
+
write(chunk: string | Uint8Array): void;
|
|
51
|
+
}
|
|
52
|
+
export interface HttpResponseStreamMetadata {
|
|
53
|
+
headers: Record<string, string>;
|
|
54
|
+
statusCode: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Global awslambda object provided by Lambda runtime.
|
|
58
|
+
* This is only available when running in AWS Lambda.
|
|
59
|
+
*/
|
|
60
|
+
export interface AwsLambdaGlobal {
|
|
61
|
+
HttpResponseStream: {
|
|
62
|
+
from(stream: ResponseStream, metadata: HttpResponseStreamMetadata): ResponseStream;
|
|
63
|
+
};
|
|
64
|
+
streamifyResponse<TEvent = unknown>(handler: (event: TEvent, responseStream: ResponseStream, context: LambdaContext) => Promise<void>): (event: TEvent, context: LambdaContext) => Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
export type LambdaHandler = (event: FunctionUrlEvent, context: LambdaContext) => Promise<LambdaResponse>;
|
|
67
|
+
export type LambdaStreamHandler = (event: FunctionUrlEvent, context: LambdaContext) => Promise<void>;
|
|
68
|
+
export type CreateLambdaHandlerOptions = {
|
|
69
|
+
/**
|
|
70
|
+
* Optional name for logging and debugging
|
|
71
|
+
*/
|
|
72
|
+
name?: string;
|
|
73
|
+
};
|
|
74
|
+
export type LambdaHandlerFactory = (app: Application, options?: CreateLambdaHandlerOptions) => LambdaHandler;
|
|
75
|
+
export type LambdaStreamHandlerFactory = (app: Application, options?: CreateLambdaHandlerOptions) => LambdaStreamHandler;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Application, RequestHandler } from "express";
|
|
2
|
+
import type { Server } from "http";
|
|
3
|
+
import type { CorsConfig } from "./cors.helper.js";
|
|
4
|
+
export interface CreateServerOptions {
|
|
5
|
+
/**
|
|
6
|
+
* CORS configuration. Pass false to disable CORS middleware.
|
|
7
|
+
*/
|
|
8
|
+
cors?: CorsConfig | false;
|
|
9
|
+
/**
|
|
10
|
+
* JSON body parser limit. Defaults to "1mb".
|
|
11
|
+
*/
|
|
12
|
+
jsonLimit?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Additional middleware to apply before routes.
|
|
15
|
+
*/
|
|
16
|
+
middleware?: RequestHandler[];
|
|
17
|
+
/**
|
|
18
|
+
* Port to listen on. Defaults to PORT env var or 8080.
|
|
19
|
+
*/
|
|
20
|
+
port?: number | string;
|
|
21
|
+
}
|
|
22
|
+
export interface ServerResult {
|
|
23
|
+
/**
|
|
24
|
+
* The HTTP server instance.
|
|
25
|
+
*/
|
|
26
|
+
server: Server;
|
|
27
|
+
/**
|
|
28
|
+
* The port the server is listening on.
|
|
29
|
+
*/
|
|
30
|
+
port: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates and starts an Express server with standard Jaypie middleware.
|
|
34
|
+
*
|
|
35
|
+
* Features:
|
|
36
|
+
* - CORS handling (configurable)
|
|
37
|
+
* - JSON body parsing
|
|
38
|
+
* - Listens on PORT env var (default 8080)
|
|
39
|
+
*
|
|
40
|
+
* Usage:
|
|
41
|
+
* ```ts
|
|
42
|
+
* import express from "express";
|
|
43
|
+
* import { createServer, expressHandler } from "@jaypie/express";
|
|
44
|
+
*
|
|
45
|
+
* const app = express();
|
|
46
|
+
*
|
|
47
|
+
* app.get("/", expressHandler(async (req, res) => {
|
|
48
|
+
* return { message: "Hello World" };
|
|
49
|
+
* }));
|
|
50
|
+
*
|
|
51
|
+
* const { server, port } = await createServer(app);
|
|
52
|
+
* console.log(`Server running on port ${port}`);
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @param app - Express application instance
|
|
56
|
+
* @param options - Server configuration options
|
|
57
|
+
* @returns Promise resolving to server instance and port
|
|
58
|
+
*/
|
|
59
|
+
declare function createServer(app: Application, options?: CreateServerOptions): Promise<ServerResult>;
|
|
60
|
+
export default createServer;
|
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Request } from "express";
|
|
2
|
+
/**
|
|
3
|
+
* Get the current invoke UUID from Lambda context.
|
|
4
|
+
* Works with Jaypie Lambda adapter and Lambda Web Adapter mode.
|
|
5
|
+
*
|
|
6
|
+
* @param req - Optional Express request object. Used to extract context
|
|
7
|
+
* from Web Adapter headers or Jaypie adapter's _lambdaContext.
|
|
8
|
+
* @returns The AWS request ID or undefined if not in Lambda context
|
|
9
|
+
*/
|
|
10
|
+
declare function getCurrentInvokeUuid(req?: Request): string | undefined;
|
|
2
11
|
export default getCurrentInvokeUuid;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Request } from "express";
|
|
2
|
+
/**
|
|
3
|
+
* Get the current invoke UUID from Lambda Web Adapter context.
|
|
4
|
+
* This function extracts the request ID from either:
|
|
5
|
+
* 1. The x-amzn-request-id header (set by Lambda Web Adapter)
|
|
6
|
+
* 2. The _X_AMZN_TRACE_ID environment variable (set by Lambda runtime)
|
|
7
|
+
*
|
|
8
|
+
* @param req - Optional Express request object to extract headers from
|
|
9
|
+
* @returns The AWS request ID or undefined if not in Lambda context
|
|
10
|
+
*/
|
|
11
|
+
declare function getWebAdapterUuid(req?: Request): string | undefined;
|
|
12
|
+
export default getWebAdapterUuid;
|