lambder 5.1.3 → 6.0.1
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 +8 -3
- package/dist/client/LambderCaller.d.ts +8 -83
- package/dist/client/LambderCaller.js +32 -64
- package/dist/client/LambderMSW.js +4 -4
- package/dist/client.d.ts +5 -3
- package/dist/client.js +3 -1
- package/dist/core/Lambder.js +10 -3
- package/dist/core/LambderContext.d.ts +6 -5
- package/dist/core/LambderContext.js +20 -12
- package/dist/core/LambderResolver.d.ts +7 -5
- package/dist/core/LambderResolver.js +7 -3
- package/dist/core/LambderResponseBuilder.d.ts +17 -3
- package/dist/core/LambderResponseBuilder.js +2 -2
- package/dist/index.d.ts +10 -6
- package/dist/index.js +6 -2
- package/dist/invoke/LambderInvokeCaller.d.ts +322 -0
- package/dist/invoke/LambderInvokeCaller.js +654 -0
- package/dist/session/LambderSessionManager.d.ts +5 -1
- package/dist/session/LambderSessionManager.js +26 -13
- package/dist/shared/LambderApiContract.d.ts +9 -0
- package/dist/shared/LambderApiOutcome.d.ts +69 -0
- package/dist/shared/LambderApiOutcome.js +79 -0
- package/dist/shared/LambderCallOptions.d.ts +71 -0
- package/dist/shared/LambderCallOptions.js +16 -0
- package/dist/shared/LambderCompressionCodec.d.ts +45 -8
- package/dist/shared/LambderCompressionCodec.js +44 -14
- package/dist/shared/LambderCrashDetail.d.ts +48 -0
- package/dist/shared/LambderCrashDetail.js +78 -0
- package/dist/shared/LambderRequestPayload.d.ts +46 -23
- package/dist/shared/LambderRequestPayload.js +47 -31
- package/dist/stores/LambderDdbCache.d.ts +7 -2
- package/dist/stores/LambderDdbCache.js +30 -12
- package/dist/stores/LambderDdbIdempotency.d.ts +7 -2
- package/dist/stores/LambderDdbIdempotency.js +26 -10
- package/dist/stores/LambderDdbRateLimiter.d.ts +7 -2
- package/dist/stores/LambderDdbRateLimiter.js +16 -4
- package/dist/stores/LambderDdbSdk.d.ts +20 -0
- package/dist/stores/LambderDdbSdk.js +31 -0
- package/package.json +6 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,11 @@ import Lambder from './core/Lambder.js';
|
|
|
2
2
|
export default Lambder;
|
|
3
3
|
export { initLambder } from './core/Lambder.js';
|
|
4
4
|
export { default as LambderCaller } from "./client/LambderCaller.js";
|
|
5
|
-
export type { LambderApiOutcome, LambderApiFailureReason, LambderCallOptions, LambderCallerOptions, LambderGuardInputsProvider, LambderProvidedGuardInputs, LambderIdempotencyKeyScope } from "./client/LambderCaller.js";
|
|
5
|
+
export type { LambderApiOutcome, LambderApiFailureReason, LambderValidationError, LambderCallOptions, LambderCallerOptions, LambderGuardInputsProvider, LambderProvidedGuardInputs, LambderIdempotencyKeyScope } from "./client/LambderCaller.js";
|
|
6
|
+
export { default as LambderInvokeCaller, LambderInvokeError, isLambderInvokeError, compressPayloadBrotli, LAMBDER_INVOKE_HEADER, LAMBDER_INVOKED_BY_HEADER, LAMBDER_INVOKE_PROTOCOL, LAMBDER_INVOKE_MAX_EVENT_BYTES, DEFAULT_INVOKE_REQUEST_COMPRESSION_SETTINGS, } from "./invoke/LambderInvokeCaller.js";
|
|
7
|
+
export type { LambderInvokeOutcome, LambderInvokeFailure, LambderInvokeFailureReason, LambderInvokeFunctionError, LambderInvokeCallerOptions, LambderInvokeCallOptions, LambderInvokeFailureHandler, LambderInvokeLogListHandler, LambderInvokeSession, LambderInvokeTransport, LambderInvokeTransportResult, LambderInvokeHttpResult, LambderInvokeRequestInit, LambderInvokeEventInit, } from "./invoke/LambderInvokeCaller.js";
|
|
8
|
+
export { describeCrash, errorFromCrashDetail } from "./shared/LambderCrashDetail.js";
|
|
9
|
+
export type { LambderCrashDetail, LambderCrashCause } from "./shared/LambderCrashDetail.js";
|
|
6
10
|
export { LambderApiError, isLambderApiError, refuse, LAMBDER_REFUSAL_CODES } from "./shared/LambderApiError.js";
|
|
7
11
|
export type { LambderApiErrorOptions, LambderRefusalMessage, LambderRefusalCode, LambderRefuseOptions } from "./shared/LambderApiError.js";
|
|
8
12
|
export { default as LambderResponseBuilder } from "./core/LambderResponseBuilder.js";
|
|
@@ -13,7 +17,7 @@ export { LambderResponse, finalizeResponse, acceptsEncoding, type HttpStatusCode
|
|
|
13
17
|
export { html, xml, raw, jsonScript, escapeHtml, renderHtmlValue, LambderSafeHtml, type LambderHtmlValue } from "./shared/LambderHtml.js";
|
|
14
18
|
export { LambderTemplatingEngine } from "./core/LambderTemplatingEngine.js";
|
|
15
19
|
export type { LambderTemplateData, LambderTemplatingEngineOptions } from "./core/LambderTemplatingEngine.js";
|
|
16
|
-
export type { LambderResponseOptions, LambderRawResponseInit, } from "./core/LambderResponseBuilder.js";
|
|
20
|
+
export type { LambderResponseOptions, LambderRawResponseInit, LambderApiAnswer, } from "./core/LambderResponseBuilder.js";
|
|
17
21
|
export type { LambderRouteMatcher, LambderCorsConfig, LambderCreateOptions, LambderSessionOptions, ConditionFunction, RouteCondition, PathParamsOf, LambderActionTools, LambderHandler, LambderIndexHtmlOptions, } from "./core/Lambder.js";
|
|
18
22
|
export { LambderPublicFilesHandler } from "./core/LambderPublicFiles.js";
|
|
19
23
|
export type { LambderPublicFilesOptions } from "./core/LambderPublicFiles.js";
|
|
@@ -27,8 +31,8 @@ export type { LambderSessionCookieOptions } from "./session/LambderSessionContro
|
|
|
27
31
|
export type { LambderSessionContext, LambderCreatedSession, LambderSessionDataRefreshConfig } from "./session/LambderSessionManager.js";
|
|
28
32
|
export { resolveCompressionOption, LAMBDER_ENCODINGS } from "./shared/LambderCompressionOption.js";
|
|
29
33
|
export type { LambderCompressionOption, LambderCompressionSettings, LambderCompressionSettingsBase, LambderEncoding, } from "./shared/LambderCompressionOption.js";
|
|
30
|
-
export { compressText,
|
|
31
|
-
export type { LambderRestoreFailure } from "./shared/LambderCompressionCodec.js";
|
|
34
|
+
export { compressText, restoreBytes, restoreText, LambderCompressionError, LAMBDER_RESTORE_FAILURES, } from "./shared/LambderCompressionCodec.js";
|
|
35
|
+
export type { LambderRestoreFailure, LambderRestoreBound } from "./shared/LambderCompressionCodec.js";
|
|
32
36
|
export { LambderSessionDataRefreshError, LambderSessionReadError } from "./session/LambderSessionManager.js";
|
|
33
37
|
export { LambderDdbCache } from "./stores/LambderDdbCache.js";
|
|
34
38
|
export type { LambderCacheKey, LambderDdbCacheOptions, LambderDdbCacheSetOptions, LambderDdbCacheGetOrSetOptions, LambderDdbCacheListOptions, } from "./stores/LambderDdbCache.js";
|
|
@@ -46,7 +50,7 @@ export type { LambderLanguageMeta, LambderI18nConfig, LambderI18nInstance, Lambd
|
|
|
46
50
|
export { type ApiContractShape, type LambderApiResponse, type LambderApiResponseConfig, } from "./shared/LambderApiContract.js";
|
|
47
51
|
export type { LambderRenderContext, LambderSessionRenderContext, LambderHttpEvent } from "./core/LambderContext.js";
|
|
48
52
|
export { createContext, isV2HttpEvent } from "./core/LambderContext.js";
|
|
49
|
-
export {
|
|
50
|
-
export type {
|
|
53
|
+
export { COMPRESSED_PAYLOAD_GZ_FIELD, COMPRESSED_PAYLOAD_BR_FIELD, COMPRESSED_PAYLOAD_BYTES_FIELD, DEFAULT_REQUEST_COMPRESSION_SETTINGS, DEFAULT_MAX_RESTORED_PAYLOAD_BYTES, } from "./shared/LambderRequestPayload.js";
|
|
54
|
+
export type { LambderCompressedGzipPayload, LambderCompressedBrotliPayload, LambderRequestCompressionOption, LambderRequestCompressionSettings, } from "./shared/LambderRequestPayload.js";
|
|
51
55
|
export { serializeCookie, serializeClearCookie, resolveCookieDomain } from "./core/LambderCookie.js";
|
|
52
56
|
export type { LambderCookieOptions, LambderClearCookieOptions, LambderCookieDomain } from "./core/LambderCookie.js";
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,10 @@ import Lambder from './core/Lambder.js';
|
|
|
2
2
|
export default Lambder;
|
|
3
3
|
export { initLambder } from './core/Lambder.js';
|
|
4
4
|
export { default as LambderCaller } from "./client/LambderCaller.js";
|
|
5
|
+
// Calling a Lambder app from another lambda (server-only: the Lambda SDK, zlib)
|
|
6
|
+
export { default as LambderInvokeCaller, LambderInvokeError, isLambderInvokeError, compressPayloadBrotli, LAMBDER_INVOKE_HEADER, LAMBDER_INVOKED_BY_HEADER, LAMBDER_INVOKE_PROTOCOL, LAMBDER_INVOKE_MAX_EVENT_BYTES, DEFAULT_INVOKE_REQUEST_COMPRESSION_SETTINGS, } from "./invoke/LambderInvokeCaller.js";
|
|
7
|
+
// A crash described for a caller allowed to see it (the envelope's `crash` field)
|
|
8
|
+
export { describeCrash, errorFromCrashDetail } from "./shared/LambderCrashDetail.js";
|
|
5
9
|
// Typed API refusals (isomorphic: shared code may throw them from anywhere)
|
|
6
10
|
export { LambderApiError, isLambderApiError, refuse, LAMBDER_REFUSAL_CODES } from "./shared/LambderApiError.js";
|
|
7
11
|
export { default as LambderResponseBuilder } from "./core/LambderResponseBuilder.js";
|
|
@@ -23,7 +27,7 @@ export { LambderHttpFileSource } from "./stores/LambderHttpFileSource.js";
|
|
|
23
27
|
export { resolveCompressionOption, LAMBDER_ENCODINGS } from "./shared/LambderCompressionOption.js";
|
|
24
28
|
// Brotli/gzip plus the bounded, length-verified restore every compressed
|
|
25
29
|
// value in Lambder (records at rest, request payloads) goes through.
|
|
26
|
-
export { compressText,
|
|
30
|
+
export { compressText, restoreBytes, restoreText, LambderCompressionError, LAMBDER_RESTORE_FAILURES, } from "./shared/LambderCompressionCodec.js";
|
|
27
31
|
export { LambderSessionDataRefreshError, LambderSessionReadError } from "./session/LambderSessionManager.js";
|
|
28
32
|
// DynamoDB-backed compressed cache (standalone, server-only)
|
|
29
33
|
export { LambderDdbCache } from "./stores/LambderDdbCache.js";
|
|
@@ -39,6 +43,6 @@ export { lambderRateLimitKey } from "./policies/LambderApiRateLimits.js";
|
|
|
39
43
|
export { createLambderI18n } from "./shared/LambderI18n.js";
|
|
40
44
|
export { createContext, isV2HttpEvent } from "./core/LambderContext.js";
|
|
41
45
|
// Request payload compression: the wire format LambderCaller and the server share.
|
|
42
|
-
export {
|
|
46
|
+
export { COMPRESSED_PAYLOAD_GZ_FIELD, COMPRESSED_PAYLOAD_BR_FIELD, COMPRESSED_PAYLOAD_BYTES_FIELD, DEFAULT_REQUEST_COMPRESSION_SETTINGS, DEFAULT_MAX_RESTORED_PAYLOAD_BYTES, } from "./shared/LambderRequestPayload.js";
|
|
43
47
|
// Cookies (res.setCookie / res.clearCookie build on these; exported for code holding a LambderResponse)
|
|
44
48
|
export { serializeCookie, serializeClearCookie, resolveCookieDomain } from "./core/LambderCookie.js";
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calling a Lambder app from another lambda, or from any server code that
|
|
3
|
+
* holds AWS credentials and lambda:InvokeFunction on it.
|
|
4
|
+
*
|
|
5
|
+
* API Gateway delivers an HTTP request to a Lambder app as a JSON event and
|
|
6
|
+
* takes a JSON response object back; a direct InvokeCommand carries JSON in
|
|
7
|
+
* both directions too. So this caller builds the payload-format-2.0 event
|
|
8
|
+
* API Gateway would have built, invokes the function with it, and reads the
|
|
9
|
+
* response object Lambder returns. The callee is an unmodified Lambder app,
|
|
10
|
+
* and everything it offers over HTTP (zod validation, the inferred contract,
|
|
11
|
+
* refusals, guards, idempotency keys, Brotli answers, logList, the crash
|
|
12
|
+
* detail its global error handler chooses to send) applies unchanged. The
|
|
13
|
+
* callee tells an invoke from a browser only by the x-lambder-invoke header,
|
|
14
|
+
* which is a marker for guards and hooks, never an authorization: the IAM
|
|
15
|
+
* grant is that.
|
|
16
|
+
*
|
|
17
|
+
* Server-only (zlib, the Lambda SDK), so it is exported from the root entry
|
|
18
|
+
* and never from lambder/client. The SDK is an optional peer dependency
|
|
19
|
+
* loaded on the first call; the `transport` option replaces it, and
|
|
20
|
+
* LambderInvokeCaller.localTransport runs a callee's handler in-process for
|
|
21
|
+
* tests.
|
|
22
|
+
*/
|
|
23
|
+
import type { APIGatewayProxyEventV2, Context } from "aws-lambda";
|
|
24
|
+
import type { LambdaClient, LambdaClientConfig } from "@aws-sdk/client-lambda";
|
|
25
|
+
import type { ApiContractShape, LambderApiResponse } from "../shared/LambderApiContract.js";
|
|
26
|
+
import { type LambderApiFailureReason, type LambderValidationError } from "../shared/LambderApiOutcome.js";
|
|
27
|
+
import { type LambderCallOptionsArg, type LambderGuardInputsProviderOption } from "../shared/LambderCallOptions.js";
|
|
28
|
+
import { type LambderCrashDetail } from "../shared/LambderCrashDetail.js";
|
|
29
|
+
import { type LambderCompressionOption, type LambderCompressionSettings } from "../shared/LambderCompressionOption.js";
|
|
30
|
+
import { type LambderCompressedBrotliPayload } from "../shared/LambderRequestPayload.js";
|
|
31
|
+
/** Marks a synthesized request as an invoke, for guards and hooks that want to tell. Not an authorization. */
|
|
32
|
+
export declare const LAMBDER_INVOKE_HEADER = "x-lambder-invoke";
|
|
33
|
+
/** The invoking function's name, when the caller runs in Lambda; for the callee's logs. */
|
|
34
|
+
export declare const LAMBDER_INVOKED_BY_HEADER = "x-lambder-invoked-by";
|
|
35
|
+
/** The value of the marker header; a future incompatible event shape would bump it. */
|
|
36
|
+
export declare const LAMBDER_INVOKE_PROTOCOL = "1";
|
|
37
|
+
/**
|
|
38
|
+
* Lambda caps a synchronous invoke's request and its response at about 6MB;
|
|
39
|
+
* the same guard threshold finalizeResponse applies to an answer, applied
|
|
40
|
+
* here to the event before it is sent.
|
|
41
|
+
*/
|
|
42
|
+
export declare const LAMBDER_INVOKE_MAX_EVENT_BYTES = 5500000;
|
|
43
|
+
/** Request Brotli when `requestCompression: true`: the HTTP request threshold, at the quality every other Lambder site uses. */
|
|
44
|
+
export declare const DEFAULT_INVOKE_REQUEST_COMPRESSION_SETTINGS: LambderCompressionSettings;
|
|
45
|
+
export type LambderInvokeFailureReason = LambderApiFailureReason | 'crash' | 'protocol' | 'payloadTooLarge';
|
|
46
|
+
/** Lambda's own error payload for a FunctionError invocation. */
|
|
47
|
+
export type LambderInvokeFunctionError = {
|
|
48
|
+
errorType?: string;
|
|
49
|
+
errorMessage?: string;
|
|
50
|
+
trace?: string[];
|
|
51
|
+
};
|
|
52
|
+
export type LambderInvokeFailure = {
|
|
53
|
+
ok: false;
|
|
54
|
+
reason: LambderInvokeFailureReason;
|
|
55
|
+
/** HTTP status, when the callee answered. */
|
|
56
|
+
status?: number;
|
|
57
|
+
/** Envelope errorMessage, when the callee provided one. */
|
|
58
|
+
errorMessage?: any;
|
|
59
|
+
/** Seconds to wait before retrying, from the answer's Retry-After header. */
|
|
60
|
+
retryAfterSeconds?: number;
|
|
61
|
+
/** Always present: the error api() throws for this failure, with the callee's error as its cause when one is known. */
|
|
62
|
+
error: LambderInvokeError;
|
|
63
|
+
/** Zod issue detail for 'validation'. */
|
|
64
|
+
zodError?: LambderValidationError;
|
|
65
|
+
/** The parsed envelope, when one came back. */
|
|
66
|
+
response?: LambderApiResponse<any>;
|
|
67
|
+
/** The callee's crash detail, when its global error handler sent one (the envelope's `crash` field). */
|
|
68
|
+
crash?: LambderCrashDetail;
|
|
69
|
+
/** Lambda's error payload for reason 'crash'. */
|
|
70
|
+
functionError?: LambderInvokeFunctionError;
|
|
71
|
+
/** The answer's logList; empty when no envelope came back. */
|
|
72
|
+
logList: unknown[];
|
|
73
|
+
/** For 'payloadTooLarge': the event's byte size. */
|
|
74
|
+
bytes?: number;
|
|
75
|
+
};
|
|
76
|
+
export type LambderInvokeOutcome<T> = {
|
|
77
|
+
ok: true;
|
|
78
|
+
payload: T;
|
|
79
|
+
response: LambderApiResponse<T>;
|
|
80
|
+
logList: unknown[];
|
|
81
|
+
} | LambderInvokeFailure;
|
|
82
|
+
type LambderInvokeErrorInit = {
|
|
83
|
+
message: string;
|
|
84
|
+
reason: LambderInvokeFailureReason;
|
|
85
|
+
apiName: string;
|
|
86
|
+
functionName: string;
|
|
87
|
+
status?: number;
|
|
88
|
+
errorMessage?: any;
|
|
89
|
+
crash?: LambderCrashDetail;
|
|
90
|
+
functionError?: LambderInvokeFunctionError;
|
|
91
|
+
logList: unknown[];
|
|
92
|
+
zodError?: LambderValidationError;
|
|
93
|
+
retryAfterSeconds?: number;
|
|
94
|
+
bytes?: number;
|
|
95
|
+
cause?: unknown;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* What api() throws. Its message names the function, the API and the reason,
|
|
99
|
+
* so an error reporter that fingerprints on the message groups one broken
|
|
100
|
+
* API into one row; its cause is the callee's own error rebuilt from the
|
|
101
|
+
* crash detail (or Lambda's FunctionError, or the SDK's rejection), so a
|
|
102
|
+
* reporter that walks causes stores the callee's stack.
|
|
103
|
+
*/
|
|
104
|
+
export declare class LambderInvokeError extends Error {
|
|
105
|
+
/** Brand for detection across duplicate lambder installs, like LambderApiError. */
|
|
106
|
+
readonly isLambderInvokeError = true;
|
|
107
|
+
readonly reason: LambderInvokeFailureReason;
|
|
108
|
+
readonly apiName: string;
|
|
109
|
+
readonly functionName: string;
|
|
110
|
+
readonly status?: number;
|
|
111
|
+
readonly errorMessage?: any;
|
|
112
|
+
readonly crash?: LambderCrashDetail;
|
|
113
|
+
readonly functionError?: LambderInvokeFunctionError;
|
|
114
|
+
readonly logList: unknown[];
|
|
115
|
+
readonly zodError?: LambderValidationError;
|
|
116
|
+
readonly retryAfterSeconds?: number;
|
|
117
|
+
readonly bytes?: number;
|
|
118
|
+
/** The full failure outcome; it carries this error and this error carries it. */
|
|
119
|
+
outcome: LambderInvokeFailure;
|
|
120
|
+
constructor(init: LambderInvokeErrorInit);
|
|
121
|
+
}
|
|
122
|
+
/** Brand-based type guard (see LambderInvokeError.isLambderInvokeError). */
|
|
123
|
+
export declare const isLambderInvokeError: (err: unknown) => err is LambderInvokeError;
|
|
124
|
+
/** What a transport hands back: Lambda's FunctionError marker (null when the function returned normally) and the parsed JSON it returned. */
|
|
125
|
+
export type LambderInvokeTransportResult = {
|
|
126
|
+
functionError: string | null;
|
|
127
|
+
result: unknown;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Delivers one synthesized event and returns what the function answered;
|
|
131
|
+
* rejects when the invoke itself failed. `eventJson` is the event serialized
|
|
132
|
+
* once by the caller: the SDK transport sends those bytes as they are, and a
|
|
133
|
+
* custom transport that works from the object may ignore it.
|
|
134
|
+
*/
|
|
135
|
+
export type LambderInvokeTransport = (event: APIGatewayProxyEventV2, options: {
|
|
136
|
+
functionName: string;
|
|
137
|
+
eventJson: string;
|
|
138
|
+
signal?: AbortSignal;
|
|
139
|
+
}) => Promise<LambderInvokeTransportResult>;
|
|
140
|
+
/** The onLogList option: each answer's logList, success or failure, when it has entries. */
|
|
141
|
+
export type LambderInvokeLogListHandler = (apiName: string, logList: unknown[]) => void | Promise<void>;
|
|
142
|
+
/** The onFailure option: every failed call, once, awaited before api() throws or apiOutcome() returns. */
|
|
143
|
+
export type LambderInvokeFailureHandler = (failure: LambderInvokeFailure, info: {
|
|
144
|
+
apiName: string;
|
|
145
|
+
functionName: string;
|
|
146
|
+
}) => void | Promise<void>;
|
|
147
|
+
/** A session carried on a user's behalf: the two values a browser holds. */
|
|
148
|
+
export type LambderInvokeSession = {
|
|
149
|
+
token: string;
|
|
150
|
+
csrf: string;
|
|
151
|
+
};
|
|
152
|
+
export type LambderInvokeCallOptions = {
|
|
153
|
+
/** Abort after this many ms; overrides the constructor default. The callee keeps running regardless. */
|
|
154
|
+
timeoutMs?: number;
|
|
155
|
+
/** External abort signal, combined with the timeout when both are set. */
|
|
156
|
+
signal?: AbortSignal;
|
|
157
|
+
/** Overrides the constructor's requestCompression for this call: false sends the payload plainly, true compresses it regardless of the threshold. Either way it is only sent compressed when that is smaller. */
|
|
158
|
+
compressRequest?: boolean;
|
|
159
|
+
/** Forwarded as X-Forwarded-For, so ctx.ip on the callee is the end user's address rather than nothing. */
|
|
160
|
+
clientIp?: string;
|
|
161
|
+
/** Extra request headers the callee sees. */
|
|
162
|
+
headers?: Record<string, string>;
|
|
163
|
+
/** Values for the API's guardInput-mode guards, keyed by guard name; the contract makes this REQUIRED for APIs that declare such guards, except those a guardInputsProvider covers. */
|
|
164
|
+
guardInputs?: Record<string, unknown>;
|
|
165
|
+
/** Replay-protection key for APIs declared idempotent on the callee. Same rules as LambderCaller: unguessable, at least 16 characters, one per logical operation. */
|
|
166
|
+
idempotencyKey?: string;
|
|
167
|
+
/** A user's session, so a session API on the callee runs on their behalf. */
|
|
168
|
+
session?: LambderInvokeSession;
|
|
169
|
+
};
|
|
170
|
+
type LambderInvokeCallerBaseOptions = {
|
|
171
|
+
/** Function name or ARN. */
|
|
172
|
+
functionName: string;
|
|
173
|
+
/** A ready client, e.g. one shared with the rest of the app. */
|
|
174
|
+
client?: LambdaClient;
|
|
175
|
+
/** Otherwise the client is created from this on the first call (region, credentials, maxAttempts). */
|
|
176
|
+
clientConfig?: LambdaClientConfig;
|
|
177
|
+
/** Must match the callee's apiPath. Default: "/api". */
|
|
178
|
+
apiPath?: string;
|
|
179
|
+
/** Sent as `version`; the callee answers versionExpired on a mismatch when it has one too. Default: none. */
|
|
180
|
+
apiVersion?: string;
|
|
181
|
+
/** The Host the callee sees (ctx.host). Default: functionName. */
|
|
182
|
+
host?: string;
|
|
183
|
+
/**
|
|
184
|
+
* Brotli the request payload when its JSON reaches the threshold, sent
|
|
185
|
+
* as `payloadBr` beside its byte length instead of `payload` whenever
|
|
186
|
+
* that is smaller. Off by default; `true` is { minBytes: 4096,
|
|
187
|
+
* quality: 5 }. The callee understands both shapes either way.
|
|
188
|
+
*/
|
|
189
|
+
requestCompression?: LambderCompressionOption;
|
|
190
|
+
/** Ceiling on what a compressed answer may restore to, the counterpart of the server's maxRequestPayloadBytes. Default: 20,000,000. */
|
|
191
|
+
maxResponsePayloadBytes?: number;
|
|
192
|
+
/** Default per-call timeout in ms. The callee's own timeout is the real ceiling. Default: none. */
|
|
193
|
+
timeoutMs?: number;
|
|
194
|
+
/** Receives each answer's logList. Default: console.log with the function and api name. A throw is logged and otherwise ignored. */
|
|
195
|
+
onLogList?: LambderInvokeLogListHandler;
|
|
196
|
+
/**
|
|
197
|
+
* Called, and awaited, for every failed call before api() throws or
|
|
198
|
+
* apiOutcome() returns, so failures are reported in one place whichever
|
|
199
|
+
* method the site used, and before the lambda answers. A throw inside it
|
|
200
|
+
* is logged and otherwise ignored: apiOutcome() never throws.
|
|
201
|
+
*/
|
|
202
|
+
onFailure?: LambderInvokeFailureHandler;
|
|
203
|
+
/** The session token cookie's name, when a session is carried and the callee uses a non-default `tokenCookieKey`. The CSRF value rides in the envelope's `token` field, which has no name to configure. */
|
|
204
|
+
sessionTokenCookieKey?: string;
|
|
205
|
+
/** Replaces the Lambda SDK. */
|
|
206
|
+
transport?: LambderInvokeTransport;
|
|
207
|
+
};
|
|
208
|
+
/** Constructor options: the base options plus guardInputsProvider, mandatory once TProvided names guards. */
|
|
209
|
+
export type LambderInvokeCallerOptions<TContract, TProvided extends string = never> = LambderInvokeCallerBaseOptions & LambderGuardInputsProviderOption<TContract, TProvided>;
|
|
210
|
+
/** The decoded HTTP answer of request(): status, lowercased headers, cookies and the body bytes (decompressed when the callee compressed them). */
|
|
211
|
+
export type LambderInvokeHttpResult = {
|
|
212
|
+
statusCode: number;
|
|
213
|
+
headers: Record<string, string>;
|
|
214
|
+
cookies: string[];
|
|
215
|
+
body: Buffer;
|
|
216
|
+
text: () => string;
|
|
217
|
+
json: () => unknown;
|
|
218
|
+
};
|
|
219
|
+
export type LambderInvokeRequestInit = {
|
|
220
|
+
/** Default: GET. */
|
|
221
|
+
method?: string;
|
|
222
|
+
path: string;
|
|
223
|
+
query?: Record<string, string>;
|
|
224
|
+
headers?: Record<string, string>;
|
|
225
|
+
/** A string body is sent as-is (JSON unless a content-type says otherwise); a Buffer is sent base64-encoded as API Gateway would. */
|
|
226
|
+
body?: string | Buffer;
|
|
227
|
+
/** Cookie header pairs, `name=value`. */
|
|
228
|
+
cookies?: string[];
|
|
229
|
+
clientIp?: string;
|
|
230
|
+
timeoutMs?: number;
|
|
231
|
+
signal?: AbortSignal;
|
|
232
|
+
};
|
|
233
|
+
/** What createEvent builds an API call event from; the instance path adds compression on top. */
|
|
234
|
+
export type LambderInvokeEventInit = {
|
|
235
|
+
/** Default: "/api". */
|
|
236
|
+
apiPath?: string;
|
|
237
|
+
apiName: string;
|
|
238
|
+
payload?: unknown;
|
|
239
|
+
/** Default: "lambder-invoke". */
|
|
240
|
+
host?: string;
|
|
241
|
+
apiVersion?: string;
|
|
242
|
+
guardInputs?: Record<string, unknown>;
|
|
243
|
+
idempotencyKey?: string;
|
|
244
|
+
clientIp?: string;
|
|
245
|
+
headers?: Record<string, string>;
|
|
246
|
+
session?: LambderInvokeSession;
|
|
247
|
+
sessionTokenCookieKey?: string;
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* Brotli one payload's JSON for sending, or null when the plain JSON should
|
|
251
|
+
* go instead: the browser's compressPayloadGzip with Brotli, because both
|
|
252
|
+
* ends are Node. The threshold and the only-when-smaller rule are
|
|
253
|
+
* compressPayloadWith's, shared with the gzip side.
|
|
254
|
+
*/
|
|
255
|
+
export declare const compressPayloadBrotli: (json: string, minBytes: number, quality: number) => Promise<LambderCompressedBrotliPayload | null>;
|
|
256
|
+
/**
|
|
257
|
+
* @typeParam TContract - The callee's API contract (`typeof lambder.ApiContract`, imported type-only), for typed names, payloads, results and guard inputs.
|
|
258
|
+
* @typeParam TProvidedGuards - Guard names guardInputsProvider covers; those APIs' options argument becomes optional.
|
|
259
|
+
*/
|
|
260
|
+
export default class LambderInvokeCaller<TContract extends ApiContractShape = any, TProvidedGuards extends string = never> {
|
|
261
|
+
private readonly functionName;
|
|
262
|
+
private readonly apiPath;
|
|
263
|
+
private readonly apiVersion?;
|
|
264
|
+
private readonly host;
|
|
265
|
+
private readonly requestCompression;
|
|
266
|
+
private readonly maxResponsePayloadBytes;
|
|
267
|
+
private readonly timeoutMs?;
|
|
268
|
+
private readonly onLogList?;
|
|
269
|
+
private readonly onFailure?;
|
|
270
|
+
private readonly guardInputsProvider?;
|
|
271
|
+
private readonly sessionTokenCookieKey;
|
|
272
|
+
private readonly transport;
|
|
273
|
+
private readonly clientConfig;
|
|
274
|
+
private client;
|
|
275
|
+
private sdk;
|
|
276
|
+
constructor(options: LambderInvokeCallerOptions<TContract, TProvidedGuards>);
|
|
277
|
+
/**
|
|
278
|
+
* The event api() would send for this call, with a plain payload. For
|
|
279
|
+
* tests and boot checks that hand a built package an event file.
|
|
280
|
+
*/
|
|
281
|
+
static createEvent(init: LambderInvokeEventInit): APIGatewayProxyEventV2;
|
|
282
|
+
/**
|
|
283
|
+
* A transport that runs a callee's handler in this process, the way
|
|
284
|
+
* Lambda would: a thrown error becomes a FunctionError payload. For
|
|
285
|
+
* tests that want the real handlers behind the real envelope.
|
|
286
|
+
*/
|
|
287
|
+
static localTransport(handler: (event: APIGatewayProxyEventV2, context: Context) => Promise<unknown>, context?: Partial<Context>): LambderInvokeTransport;
|
|
288
|
+
private loadSdk;
|
|
289
|
+
private invokeThroughSdk;
|
|
290
|
+
/** Delivers one event, serialized exactly once; a rejected transport is a network or timeout failure. */
|
|
291
|
+
private deliver;
|
|
292
|
+
/** The function's answer as an HTTP result; throws when it is not one, or its compressed body cannot be restored. */
|
|
293
|
+
private decodeHttpResult;
|
|
294
|
+
/** Builds the failure and its error, reports it once, and hands it back. */
|
|
295
|
+
private fail;
|
|
296
|
+
private surfaceLogs;
|
|
297
|
+
/** One call, one outcome. Never throws; api() is what throws. */
|
|
298
|
+
private dispatch;
|
|
299
|
+
/**
|
|
300
|
+
* Full-fidelity call: resolves to a discriminated LambderInvokeOutcome
|
|
301
|
+
* instead of throwing. Never throws; for sites that degrade gracefully.
|
|
302
|
+
*/
|
|
303
|
+
apiOutcome<TApiName extends keyof TContract & string = string, TOutput = TApiName extends keyof TContract ? TContract[TApiName]['output'] : any>(apiName: TApiName, payload?: TApiName extends keyof TContract ? TContract[TApiName]['input'] : any, ...rest: LambderCallOptionsArg<TContract, TApiName, TProvidedGuards, LambderInvokeCallOptions>): Promise<LambderInvokeOutcome<TOutput>>;
|
|
304
|
+
/**
|
|
305
|
+
* The declared output, or a thrown LambderInvokeError carrying the
|
|
306
|
+
* outcome. A failed dependency is a failed request: the throw reaches the
|
|
307
|
+
* app's global error handler with the callee's error as its cause. The
|
|
308
|
+
* result is the callee's output type as it declared it: the resolver
|
|
309
|
+
* only lets a handler answer null when the output allows it or beside a
|
|
310
|
+
* reason (LambderApiAnswer), so a nullable output is the one place null
|
|
311
|
+
* arrives.
|
|
312
|
+
*/
|
|
313
|
+
api<TApiName extends keyof TContract & string = string, TOutput = TApiName extends keyof TContract ? TContract[TApiName]['output'] : any>(apiName: TApiName, payload?: TApiName extends keyof TContract ? TContract[TApiName]['input'] : any, ...rest: LambderCallOptionsArg<TContract, TApiName, TProvidedGuards, LambderInvokeCallOptions>): Promise<TOutput>;
|
|
314
|
+
/**
|
|
315
|
+
* Any route of the callee, untyped: the synthesized request and the
|
|
316
|
+
* decoded answer, whatever its status. Throws a LambderInvokeError only
|
|
317
|
+
* when no HTTP answer came back (a rejected invoke, a FunctionError, a
|
|
318
|
+
* non-HTTP answer); those go through onFailure like an API call's.
|
|
319
|
+
*/
|
|
320
|
+
request(init: LambderInvokeRequestInit): Promise<LambderInvokeHttpResult>;
|
|
321
|
+
}
|
|
322
|
+
export {};
|