@guren/plugin-lambda 0.4.0 → 0.6.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/README.md +1 -1
- package/dist/{build-CHTrBLNm.js → build-Dn_V0LNZ.js} +23 -39
- package/dist/cdk/index.d.ts +15 -14
- package/dist/cdk/index.js +33 -49
- package/dist/commands.js +1 -1
- package/dist/{handlers-CkWGWQQX.js → handlers-Dtt3k5Ue.js} +1 -1
- package/dist/index.d.ts +62 -6
- package/dist/index.js +129 -7
- package/package.json +18 -2
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ Installing registers a `lambda:build` command and scaffolds `src/lambda.ts` —
|
|
|
15
15
|
bunx guren lambda:build
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
`lambda:build` runs your app's `build` script, then assembles a `.lambda/` directory:
|
|
18
|
+
`lambda:build` runs the deploy-runtime checks `guren doctor` reports (a warning, never a failure, on an in-memory session or OAuth store, a `ScryptHasher`, or filesystem provider discovery), then your app's `build` script, then assembles a `.lambda/` directory:
|
|
19
19
|
|
|
20
20
|
| Path | Contents |
|
|
21
21
|
|------|----------|
|
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import { n as LAMBDA_HANDLER_MODULE, t as LAMBDA_HANDLER_EXPORTS } from "./handlers-
|
|
1
|
+
import { n as LAMBDA_HANDLER_MODULE, t as LAMBDA_HANDLER_EXPORTS } from "./handlers-Dtt3k5Ue.js";
|
|
2
2
|
import { cpSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { MCP_SDK_SUBPATH_PREFIX, appUsesMcpPlugin, assertOutputDirOutsideRoot, clientManifestJson, importSpecifier, renderDevOnlyStub, resetOutputDir, resolveClientAssetEnv, resolvePathLike, resolveSsrEntryFile, ssrRuntimePaths, stageStaticAssets, stubbableDevOnlyModules, unusedSqlClients } from "@guren/core/internal/deploy-build";
|
|
5
|
+
import { reportDeployRuntimeHazards } from "@guren/core/internal/deploy-check";
|
|
5
6
|
//#region src/build.ts
|
|
6
7
|
const MCP_UNAVAILABLE = "The MCP endpoint is unavailable on AWS Lambda — it generates files on disk.";
|
|
7
8
|
/**
|
|
8
|
-
* Why the dev-only modules in `DEV_ONLY_MODULES` cannot run here,
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* that exists. The SQL clients are load-bearing on Lambda — the app connects
|
|
13
|
-
* to Postgres or the Data API through them — so which of those are dead
|
|
14
|
-
* weight is decided per app, not here, and their messages come from
|
|
15
|
-
* `unusedSqlClients`.
|
|
9
|
+
* Why the dev-only modules in `DEV_ONLY_MODULES` cannot run here, each naming
|
|
10
|
+
* the Lambda-appropriate replacement. Keyed only on the kinds this platform
|
|
11
|
+
* stubs unconditionally: the SQL clients are load-bearing on Lambda, so which
|
|
12
|
+
* of those are dead weight is decided per app by `unusedSqlClients`.
|
|
16
13
|
*/
|
|
17
14
|
const UNAVAILABLE_ON_LAMBDA = {
|
|
18
15
|
sqlite: "bun:sqlite is unavailable on AWS Lambda — use createAwsDataApiDatabase() or createPostgresDatabase().",
|
|
@@ -20,32 +17,21 @@ const UNAVAILABLE_ON_LAMBDA = {
|
|
|
20
17
|
mcp: MCP_UNAVAILABLE
|
|
21
18
|
};
|
|
22
19
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* Which of them are in force is per app, and for one reason only: an app that
|
|
30
|
-
* declares `@guren/plugin-mcp` serves the App MCP endpoint here, so its
|
|
31
|
-
* transport must reach the bundle rather than a stub (RFC 0016 §7). The rest
|
|
32
|
-
* are unconditional — nothing an app can declare makes the Vite dev server or
|
|
33
|
-
* the *Dev* MCP's generators run on Lambda. `stubbableDevOnlyModules` owns
|
|
34
|
-
* that distinction; this function only renders what it is handed.
|
|
20
|
+
* Stubs rather than externals: inlining a lazily-imported module hoists its
|
|
21
|
+
* static imports to the bundle top level, so an external reached that way (the
|
|
22
|
+
* MCP SDK) fails at import time though no code path runs it. An app declaring
|
|
23
|
+
* `@guren/plugin-mcp` serves the endpoint here, so its transport must reach the
|
|
24
|
+
* bundle (RFC 0016 §7) — `stubbableDevOnlyModules` owns that split.
|
|
35
25
|
*/
|
|
36
26
|
function devOnlyStubs(mcpPlugin) {
|
|
37
27
|
return Object.fromEntries(stubbableDevOnlyModules({ mcpPlugin }).map((module) => [module.specifier, renderDevOnlyStub(module, UNAVAILABLE_ON_LAMBDA[module.kind])]));
|
|
38
28
|
}
|
|
39
29
|
/**
|
|
40
30
|
* The dev-only stubs plus one per database client this app does not connect
|
|
41
|
-
* through.
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* bundler consults plugins before it consults the external list, so stubbing
|
|
46
|
-
* `@aws-sdk/client-rds-data` for a Postgres app really does replace it rather
|
|
47
|
-
* than leaving `external: ['@aws-sdk/*']` to keep drizzle's hoisted top-level
|
|
48
|
-
* import of a package the runtime may not provide.
|
|
31
|
+
* through. A stub also *overrides* `external`, verified against Bun 1.3.14: the
|
|
32
|
+
* bundler consults plugins before the external list, so stubbing
|
|
33
|
+
* `@aws-sdk/client-rds-data` for a Postgres app really does replace drizzle's
|
|
34
|
+
* hoisted top-level import of it.
|
|
49
35
|
*/
|
|
50
36
|
function stubsFor(root, dialects, mcpPlugin) {
|
|
51
37
|
const unused = unusedSqlClients({
|
|
@@ -60,16 +46,10 @@ function stubsFor(root, dialects, mcpPlugin) {
|
|
|
60
46
|
}
|
|
61
47
|
/**
|
|
62
48
|
* Fallback for an MCP SDK subpath `DEV_ONLY_MODULES` does not name. It cannot
|
|
63
|
-
* know which names the importer destructures, so it throws on evaluation
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* Reachable only for an app that does *not* declare `@guren/plugin-mcp`. For
|
|
69
|
-
* one that does, the SDK is a dependency it deliberately ships, and this
|
|
70
|
-
* fallback would stub `server/index.js` and `types.js` — which
|
|
71
|
-
* `@guren/plugin-mcp` imports statically — leaving the endpoint just as
|
|
72
|
-
* compiled shut as the transport stub did. See `stubFilter`.
|
|
49
|
+
* know which names the importer destructures, so it throws on evaluation rather
|
|
50
|
+
* than handing back missing exports. Reachable only for an app that does *not*
|
|
51
|
+
* declare `@guren/plugin-mcp`, whose statically imported `server/index.js` and
|
|
52
|
+
* `types.js` it would otherwise stub shut — see `stubFilter`.
|
|
73
53
|
*/
|
|
74
54
|
const unlistedMcpStub = `throw new Error(${JSON.stringify(MCP_UNAVAILABLE)})\n`;
|
|
75
55
|
function escapeRegExp(value) {
|
|
@@ -96,6 +76,10 @@ async function buildLambdaOutput(options = {}) {
|
|
|
96
76
|
const clientEntryKey = options.clientEntryKey ?? "resources/js/app.tsx";
|
|
97
77
|
const ssrEntryKey = options.ssrEntryKey ?? "resources/js/ssr.tsx";
|
|
98
78
|
assertOutputDirOutsideRoot(out, root, "Lambda build");
|
|
79
|
+
await reportDeployRuntimeHazards({
|
|
80
|
+
root,
|
|
81
|
+
label: "Lambda build"
|
|
82
|
+
});
|
|
99
83
|
if (!options.skipAppBuild) runAppBuild(root);
|
|
100
84
|
if (!existsSync(entrypoint)) throw new Error(`Lambda build: entrypoint not found at ${entrypoint}. Run \`bunx guren plugin @guren/plugin-lambda\` to scaffold src/lambda.ts, or pass "entrypoint".`);
|
|
101
85
|
const ssrFile = resolveSsrEntry(ssrDir, ssrEntryKey);
|
package/dist/cdk/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Duration } from "aws-cdk-lib";
|
|
2
2
|
import * as apigwv2 from "aws-cdk-lib/aws-apigatewayv2";
|
|
3
3
|
import * as cloudfront from "aws-cdk-lib/aws-cloudfront";
|
|
4
|
+
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
|
|
4
5
|
import * as lambda from "aws-cdk-lib/aws-lambda";
|
|
5
6
|
import * as s3 from "aws-cdk-lib/aws-s3";
|
|
6
7
|
import * as sqs from "aws-cdk-lib/aws-sqs";
|
|
@@ -22,6 +23,12 @@ interface GurenLambdaAssetsProps {
|
|
|
22
23
|
/** The staged assets directory produced by `guren lambda:build` (`.lambda/assets`). */
|
|
23
24
|
dir: string;
|
|
24
25
|
}
|
|
26
|
+
interface GurenLambdaSessionsTableProps {
|
|
27
|
+
/** Explicit table name. Omitted, CloudFormation names it. */
|
|
28
|
+
tableName?: string;
|
|
29
|
+
/** Kept on `cdk destroy`. Defaults to true: a destroyed table logs everyone out. */
|
|
30
|
+
retainOnDelete?: boolean;
|
|
31
|
+
}
|
|
25
32
|
interface GurenLambdaDataApiProps {
|
|
26
33
|
/** Aurora cluster ARN — exposed as DATABASE_RESOURCE_ARN. */
|
|
27
34
|
resourceArn: string;
|
|
@@ -51,22 +58,15 @@ interface GurenLambdaAppProps {
|
|
|
51
58
|
assets?: GurenLambdaAssetsProps;
|
|
52
59
|
/** Wire every function to Aurora via the RDS Data API: environment plus the rds-data and secret-read grants. */
|
|
53
60
|
dataApi?: GurenLambdaDataApiProps;
|
|
61
|
+
/** Provision the table the `dynamodb` session driver reads (RFC 0020 §4). */
|
|
62
|
+
sessionsTable?: GurenLambdaSessionsTableProps | true;
|
|
54
63
|
}
|
|
55
64
|
/**
|
|
56
|
-
* The full serverless topology
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* `guren lambda:build` emits (`handler.http`, `handler.queue`, ...).
|
|
65
|
+
* The full serverless topology in one construct: an HTTP API in front of the
|
|
66
|
+
* `http` handler, opt-in SQS/EventBridge/console wiring, and CloudFront + S3
|
|
67
|
+
* assets. Handler names follow `guren lambda:build` (`handler.http`, ...).
|
|
60
68
|
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```typescript
|
|
63
|
-
* new GurenLambdaApp(this, 'App', {
|
|
64
|
-
* functionDir: '../.lambda/function',
|
|
65
|
-
* assets: { dir: '../.lambda/assets' },
|
|
66
|
-
* queue: {},
|
|
67
|
-
* environment: { DATABASE_NAME: 'appdb', DATABASE_RESOURCE_ARN: '...', DATABASE_SECRET_ARN: '...' },
|
|
68
|
-
* })
|
|
69
|
-
* ```
|
|
69
|
+
* @example new GurenLambdaApp(this, 'App', { functionDir: '../.lambda/function' })
|
|
70
70
|
*/
|
|
71
71
|
declare class GurenLambdaApp extends Construct {
|
|
72
72
|
readonly httpFunction: lambda.Function;
|
|
@@ -76,9 +76,10 @@ declare class GurenLambdaApp extends Construct {
|
|
|
76
76
|
readonly queueFunction?: lambda.Function;
|
|
77
77
|
readonly scheduleFunction?: lambda.Function;
|
|
78
78
|
readonly consoleFunction?: lambda.Function;
|
|
79
|
+
readonly sessionsTable?: dynamodb.Table;
|
|
79
80
|
readonly assetsBucket?: s3.Bucket;
|
|
80
81
|
readonly distribution?: cloudfront.Distribution;
|
|
81
82
|
constructor(scope: Construct, id: string, props: GurenLambdaAppProps);
|
|
82
83
|
}
|
|
83
84
|
//#endregion
|
|
84
|
-
export { GurenLambdaApp, GurenLambdaAppProps, GurenLambdaAssetsProps, GurenLambdaDataApiProps, GurenLambdaQueueProps, GurenLambdaScheduleProps };
|
|
85
|
+
export { GurenLambdaApp, GurenLambdaAppProps, GurenLambdaAssetsProps, GurenLambdaDataApiProps, GurenLambdaQueueProps, GurenLambdaScheduleProps, GurenLambdaSessionsTableProps };
|
package/dist/cdk/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { r as lambdaHandlerId } from "../handlers-
|
|
1
|
+
import { r as lambdaHandlerId } from "../handlers-Dtt3k5Ue.js";
|
|
2
2
|
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { DOCUMENT_ASSET_EXTENSIONS, DOCUMENT_ASSET_HEADERS } from "@guren/core/internal/deploy-build";
|
|
5
|
-
import { CfnOutput, Duration } from "aws-cdk-lib";
|
|
5
|
+
import { CfnOutput, Duration, RemovalPolicy } from "aws-cdk-lib";
|
|
6
6
|
import * as apigwv2 from "aws-cdk-lib/aws-apigatewayv2";
|
|
7
7
|
import { HttpLambdaIntegration } from "aws-cdk-lib/aws-apigatewayv2-integrations";
|
|
8
8
|
import * as cloudfront from "aws-cdk-lib/aws-cloudfront";
|
|
9
9
|
import * as origins from "aws-cdk-lib/aws-cloudfront-origins";
|
|
10
|
+
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
|
|
10
11
|
import * as events from "aws-cdk-lib/aws-events";
|
|
11
12
|
import * as targets from "aws-cdk-lib/aws-events-targets";
|
|
12
13
|
import * as iam from "aws-cdk-lib/aws-iam";
|
|
@@ -18,20 +19,11 @@ import * as sqs from "aws-cdk-lib/aws-sqs";
|
|
|
18
19
|
import { Construct } from "constructs";
|
|
19
20
|
//#region src/cdk/index.ts
|
|
20
21
|
/**
|
|
21
|
-
* The full serverless topology
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* `guren lambda:build` emits (`handler.http`, `handler.queue`, ...).
|
|
22
|
+
* The full serverless topology in one construct: an HTTP API in front of the
|
|
23
|
+
* `http` handler, opt-in SQS/EventBridge/console wiring, and CloudFront + S3
|
|
24
|
+
* assets. Handler names follow `guren lambda:build` (`handler.http`, ...).
|
|
25
25
|
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* new GurenLambdaApp(this, 'App', {
|
|
29
|
-
* functionDir: '../.lambda/function',
|
|
30
|
-
* assets: { dir: '../.lambda/assets' },
|
|
31
|
-
* queue: {},
|
|
32
|
-
* environment: { DATABASE_NAME: 'appdb', DATABASE_RESOURCE_ARN: '...', DATABASE_SECRET_ARN: '...' },
|
|
33
|
-
* })
|
|
34
|
-
* ```
|
|
26
|
+
* @example new GurenLambdaApp(this, 'App', { functionDir: '../.lambda/function' })
|
|
35
27
|
*/
|
|
36
28
|
var GurenLambdaApp = class extends Construct {
|
|
37
29
|
httpFunction;
|
|
@@ -41,6 +33,7 @@ var GurenLambdaApp = class extends Construct {
|
|
|
41
33
|
queueFunction;
|
|
42
34
|
scheduleFunction;
|
|
43
35
|
consoleFunction;
|
|
36
|
+
sessionsTable;
|
|
44
37
|
assetsBucket;
|
|
45
38
|
distribution;
|
|
46
39
|
constructor(scope, id, props) {
|
|
@@ -108,6 +101,23 @@ var GurenLambdaApp = class extends Construct {
|
|
|
108
101
|
fn.addEnvironment("SQS_QUEUE_URL", this.queue.queueUrl);
|
|
109
102
|
this.queue.grantSendMessages(fn);
|
|
110
103
|
}
|
|
104
|
+
if (props.sessionsTable) {
|
|
105
|
+
const sessionsProps = props.sessionsTable === true ? {} : props.sessionsTable;
|
|
106
|
+
this.sessionsTable = new dynamodb.Table(this, "SessionsTable", {
|
|
107
|
+
tableName: sessionsProps.tableName,
|
|
108
|
+
partitionKey: {
|
|
109
|
+
name: "id",
|
|
110
|
+
type: dynamodb.AttributeType.STRING
|
|
111
|
+
},
|
|
112
|
+
timeToLiveAttribute: "expires_at",
|
|
113
|
+
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
|
|
114
|
+
removalPolicy: sessionsProps.retainOnDelete === false ? RemovalPolicy.DESTROY : RemovalPolicy.RETAIN
|
|
115
|
+
});
|
|
116
|
+
for (const fn of functions) {
|
|
117
|
+
fn.addEnvironment("DYNAMODB_SESSIONS_TABLE", this.sessionsTable.tableName);
|
|
118
|
+
this.sessionsTable.grantReadWriteData(fn);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
111
121
|
if (props.dataApi) for (const fn of functions) {
|
|
112
122
|
fn.addToRolePolicy(new iam.PolicyStatement({
|
|
113
123
|
actions: [
|
|
@@ -174,32 +184,11 @@ var GurenLambdaApp = class extends Construct {
|
|
|
174
184
|
}
|
|
175
185
|
};
|
|
176
186
|
/**
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
* `
|
|
181
|
-
*
|
|
182
|
-
* downloads an SVG locally and renders it inline, script and all, on its own
|
|
183
|
-
* origin in production.
|
|
184
|
-
*
|
|
185
|
-
* A viewer-response function rather than one cache behaviour per extension:
|
|
186
|
-
* a behaviour is chosen by path, so `*.svg` would also capture a `/feed.svg`
|
|
187
|
-
* the *app* renders and send it to S3, and the default limit of 25
|
|
188
|
-
* behaviours is already spent one per staged root entry. The association
|
|
189
|
-
* rides on `assetBehavior` alone, which is what keeps it off the default
|
|
190
|
-
* behaviour — the app, whose responses carry the framework's own headers.
|
|
191
|
-
*
|
|
192
|
-
* The extension is read from the request path because that path is the S3
|
|
193
|
-
* key here: this distribution sets no `defaultRootObject` and rewrites no
|
|
194
|
-
* index, so nothing stands between `/page.html` and the document. A platform
|
|
195
|
-
* that served `public/page.html` at `/page` would leave the `.html` half of
|
|
196
|
-
* this rule matching only the redirect.
|
|
197
|
-
*
|
|
198
|
-
* `toLowerCase()` covers the extension case `DOCUMENT_ASSET_EXTENSIONS`
|
|
199
|
-
* records as unreachable from a literal pattern: `getMimeType` lowercases
|
|
200
|
-
* before its lookup, so `logo.SVG` downloads under the framework's own guard,
|
|
201
|
-
* and reading the extension rather than matching a pattern is what lets this
|
|
202
|
-
* target agree with it.
|
|
187
|
+
* CloudFront answers asset behaviours from S3 directly, so `guardStaticDocument`
|
|
188
|
+
* never sees these files and an SVG would render inline, script and all. A
|
|
189
|
+
* viewer-response function, not a cache behaviour per extension: those match by
|
|
190
|
+
* path, so `*.svg` would also capture a `/feed.svg` the *app* renders, and the
|
|
191
|
+
* limit of 25 is spent one per staged root entry. Lowercased, so `logo.SVG` too.
|
|
203
192
|
*/
|
|
204
193
|
function renderAssetDocumentGuard() {
|
|
205
194
|
const headers = Object.fromEntries(Object.entries(DOCUMENT_ASSET_HEADERS).map(([name, value]) => [name.toLowerCase(), value]));
|
|
@@ -212,14 +201,9 @@ function renderAssetDocumentGuard() {
|
|
|
212
201
|
}
|
|
213
202
|
/**
|
|
214
203
|
* The function CloudFront runs, verbatim, reading the two constants
|
|
215
|
-
* {@link renderAssetDocumentGuard} declares above it.
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
* body has to be read twice, once as TypeScript and once as the JavaScript it
|
|
219
|
-
* becomes, and a `$` or a backtick in the JavaScript changes the result of the
|
|
220
|
-
* first reading. Here the string is the program: it can be pasted into the
|
|
221
|
-
* CloudFront console, or run beside the two `var` lines anywhere else, and
|
|
222
|
-
* behave exactly as it does deployed.
|
|
204
|
+
* {@link renderAssetDocumentGuard} declares above it. Free of interpolation on
|
|
205
|
+
* purpose: a `$` or a backtick in the JavaScript would change how the
|
|
206
|
+
* TypeScript reads, and as written the string is exactly the deployed program.
|
|
223
207
|
*/
|
|
224
208
|
const ASSET_DOCUMENT_GUARD_BODY = `function handler(event) {
|
|
225
209
|
var uri = event.request.uri
|
package/dist/commands.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as buildLambdaOutput } from "./build-
|
|
1
|
+
import { t as buildLambdaOutput } from "./build-Dn_V0LNZ.js";
|
|
2
2
|
import { DATABASE_DIALECTS, parseDatabaseDialects } from "@guren/core/internal/deploy-build";
|
|
3
3
|
import { defineCommand } from "citty";
|
|
4
4
|
var commands_default = { "lambda:build": defineCommand({
|
|
@@ -11,7 +11,7 @@ const LAMBDA_HANDLER_EXPORTS = [
|
|
|
11
11
|
"schedule",
|
|
12
12
|
"console"
|
|
13
13
|
];
|
|
14
|
-
/** Lambda handler identifier for an export
|
|
14
|
+
/** Lambda handler identifier for an export. */
|
|
15
15
|
function lambdaHandlerId(name) {
|
|
16
16
|
return `${LAMBDA_HANDLER_MODULE}.${name}`;
|
|
17
17
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ServiceProviderConstructor } from "@guren/core";
|
|
2
2
|
import { DatabaseDialect, PathLike } from "@guren/core/internal/deploy-build";
|
|
3
|
+
import { SessionData, SessionManager, SessionStore } from "@guren/server";
|
|
3
4
|
//#region src/build.d.ts
|
|
4
5
|
interface BuildLambdaOutputOptions {
|
|
5
6
|
/** App root directory. Defaults to the current working directory. */
|
|
@@ -47,9 +48,67 @@ declare function buildLambdaOutput(options?: BuildLambdaOutputOptions): Promise<
|
|
|
47
48
|
declare const LAMBDA_HANDLER_MODULE = "handler";
|
|
48
49
|
declare const LAMBDA_HANDLER_EXPORTS: readonly ['http', 'queue', 'schedule', 'console'];
|
|
49
50
|
type LambdaHandlerExport = (typeof LAMBDA_HANDLER_EXPORTS)[number];
|
|
50
|
-
/** Lambda handler identifier for an export
|
|
51
|
+
/** Lambda handler identifier for an export. */
|
|
51
52
|
declare function lambdaHandlerId(name: LambdaHandlerExport): string;
|
|
52
53
|
//#endregion
|
|
54
|
+
//#region src/session/dynamodb-store.d.ts
|
|
55
|
+
/**
|
|
56
|
+
* Sessions in DynamoDB, for Lambda apps that want session churn off the
|
|
57
|
+
* primary database (RFC 0020 §4). Items are `{ id, data, expires_at }`; the
|
|
58
|
+
* table's TTL attribute is `expires_at`, in epoch seconds.
|
|
59
|
+
* `@aws-sdk/client-dynamodb` is an optional peer, imported on first use.
|
|
60
|
+
*/
|
|
61
|
+
interface DynamoDbClient {
|
|
62
|
+
send(command: unknown): Promise<unknown>;
|
|
63
|
+
}
|
|
64
|
+
interface DynamoDbSessionStoreOptions {
|
|
65
|
+
/** Table name. `guren lambda:build` apps read it from DYNAMODB_SESSIONS_TABLE. */
|
|
66
|
+
table: string;
|
|
67
|
+
/** Defaults to AWS_REGION, which Lambda always sets. */
|
|
68
|
+
region?: string;
|
|
69
|
+
/** For DynamoDB Local, or a VPC endpoint. */
|
|
70
|
+
endpoint?: string;
|
|
71
|
+
/** A pre-built client. Skips both the lazy import and the credential chain. */
|
|
72
|
+
client?: DynamoDbClient;
|
|
73
|
+
now?: () => number;
|
|
74
|
+
}
|
|
75
|
+
declare class DynamoDbSessionStore implements SessionStore {
|
|
76
|
+
private client;
|
|
77
|
+
private readonly table;
|
|
78
|
+
private readonly region?;
|
|
79
|
+
private readonly endpoint?;
|
|
80
|
+
private readonly now;
|
|
81
|
+
constructor(options: DynamoDbSessionStoreOptions);
|
|
82
|
+
private nowSeconds;
|
|
83
|
+
private getClient;
|
|
84
|
+
read(id: string): Promise<SessionData | undefined>;
|
|
85
|
+
write(id: string, data: SessionData, ttlSeconds: number): Promise<void>;
|
|
86
|
+
destroy(id: string): Promise<void>;
|
|
87
|
+
touch(id: string, ttlSeconds: number): Promise<void>;
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/session/index.d.ts
|
|
91
|
+
/**
|
|
92
|
+
* The `dynamodb` driver's config. `table` defaults to DYNAMODB_SESSIONS_TABLE,
|
|
93
|
+
* which the CDK construct's `sessionsTable` sets on every function.
|
|
94
|
+
*/
|
|
95
|
+
interface DynamoDbSessionDriverOptions extends Omit<DynamoDbSessionStoreOptions, 'table'> {
|
|
96
|
+
table?: string;
|
|
97
|
+
}
|
|
98
|
+
declare module '@guren/server' {
|
|
99
|
+
interface SessionDrivers {
|
|
100
|
+
dynamodb: DynamoDbSessionDriverOptions;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Adds the `dynamodb` driver to a manager. A function call, never a module
|
|
105
|
+
* side effect: a bundler that drops an unused import would otherwise drop the
|
|
106
|
+
* driver with it (RFC 0020 §4, same rule as core's `database`).
|
|
107
|
+
*
|
|
108
|
+
* @example registerDynamoDbSessionDriver(createSessionManager(sessionConfig))
|
|
109
|
+
*/
|
|
110
|
+
declare function registerDynamoDbSessionDriver(manager: SessionManager): void;
|
|
111
|
+
//#endregion
|
|
53
112
|
//#region src/index.d.ts
|
|
54
113
|
/**
|
|
55
114
|
* Configuration for the Lambda plugin. Currently empty — reserved so future
|
|
@@ -59,11 +118,8 @@ interface LambdaPluginConfig {}
|
|
|
59
118
|
/**
|
|
60
119
|
* Register the Lambda plugin.
|
|
61
120
|
*
|
|
62
|
-
* @example
|
|
63
|
-
* ```typescript
|
|
64
|
-
* createApp({ providers: [lambdaPlugin()] })
|
|
65
|
-
* ```
|
|
121
|
+
* @example createApp({ providers: [lambdaPlugin()] })
|
|
66
122
|
*/
|
|
67
123
|
declare function lambdaPlugin(config?: LambdaPluginConfig): ServiceProviderConstructor;
|
|
68
124
|
//#endregion
|
|
69
|
-
export { type BuildLambdaOutputOptions, LAMBDA_HANDLER_EXPORTS, LAMBDA_HANDLER_MODULE, type LambdaHandlerExport, LambdaPluginConfig, buildLambdaOutput, lambdaHandlerId, lambdaPlugin };
|
|
125
|
+
export { type BuildLambdaOutputOptions, type DynamoDbSessionDriverOptions, DynamoDbSessionStore, type DynamoDbSessionStoreOptions, LAMBDA_HANDLER_EXPORTS, LAMBDA_HANDLER_MODULE, type LambdaHandlerExport, LambdaPluginConfig, buildLambdaOutput, lambdaHandlerId, lambdaPlugin, registerDynamoDbSessionDriver };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,131 @@
|
|
|
1
|
-
import { n as LAMBDA_HANDLER_MODULE, r as lambdaHandlerId, t as LAMBDA_HANDLER_EXPORTS } from "./handlers-
|
|
2
|
-
import { t as buildLambdaOutput } from "./build-
|
|
1
|
+
import { n as LAMBDA_HANDLER_MODULE, r as lambdaHandlerId, t as LAMBDA_HANDLER_EXPORTS } from "./handlers-Dtt3k5Ue.js";
|
|
2
|
+
import { t as buildLambdaOutput } from "./build-Dn_V0LNZ.js";
|
|
3
3
|
import { definePlugin } from "@guren/core";
|
|
4
|
+
//#region src/session/dynamodb-store.ts
|
|
5
|
+
var DynamoDbSessionStore = class {
|
|
6
|
+
client;
|
|
7
|
+
table;
|
|
8
|
+
region;
|
|
9
|
+
endpoint;
|
|
10
|
+
now;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.client = options.client ?? null;
|
|
13
|
+
this.table = options.table;
|
|
14
|
+
this.region = options.region;
|
|
15
|
+
this.endpoint = options.endpoint;
|
|
16
|
+
this.now = options.now ?? (() => Date.now());
|
|
17
|
+
}
|
|
18
|
+
nowSeconds() {
|
|
19
|
+
return Math.floor(this.now() / 1e3);
|
|
20
|
+
}
|
|
21
|
+
async getClient() {
|
|
22
|
+
if (this.client) return this.client;
|
|
23
|
+
const { DynamoDBClient } = await importDynamoDb();
|
|
24
|
+
const config = {};
|
|
25
|
+
if (this.region) config.region = this.region;
|
|
26
|
+
if (this.endpoint) config.endpoint = this.endpoint;
|
|
27
|
+
this.client = new DynamoDBClient(config);
|
|
28
|
+
return this.client;
|
|
29
|
+
}
|
|
30
|
+
async read(id) {
|
|
31
|
+
const { GetItemCommand } = await importDynamoDb();
|
|
32
|
+
const item = (await (await this.getClient()).send(new GetItemCommand({
|
|
33
|
+
TableName: this.table,
|
|
34
|
+
Key: { id: { S: id } },
|
|
35
|
+
ConsistentRead: true
|
|
36
|
+
}))).Item;
|
|
37
|
+
if (!item) return;
|
|
38
|
+
const expiresAt = Number(item.expires_at?.N ?? "0");
|
|
39
|
+
if (!Number.isFinite(expiresAt) || expiresAt <= this.nowSeconds()) return;
|
|
40
|
+
return parseSessionData(item.data?.S);
|
|
41
|
+
}
|
|
42
|
+
async write(id, data, ttlSeconds) {
|
|
43
|
+
const { PutItemCommand } = await importDynamoDb();
|
|
44
|
+
await (await this.getClient()).send(new PutItemCommand({
|
|
45
|
+
TableName: this.table,
|
|
46
|
+
Item: {
|
|
47
|
+
id: { S: id },
|
|
48
|
+
data: { S: JSON.stringify(data) },
|
|
49
|
+
expires_at: { N: String(this.nowSeconds() + ttlSeconds) }
|
|
50
|
+
}
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
async destroy(id) {
|
|
54
|
+
const { DeleteItemCommand } = await importDynamoDb();
|
|
55
|
+
await (await this.getClient()).send(new DeleteItemCommand({
|
|
56
|
+
TableName: this.table,
|
|
57
|
+
Key: { id: { S: id } }
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
async touch(id, ttlSeconds) {
|
|
61
|
+
const { UpdateItemCommand } = await importDynamoDb();
|
|
62
|
+
const client = await this.getClient();
|
|
63
|
+
const now = this.nowSeconds();
|
|
64
|
+
try {
|
|
65
|
+
await client.send(new UpdateItemCommand({
|
|
66
|
+
TableName: this.table,
|
|
67
|
+
Key: { id: { S: id } },
|
|
68
|
+
UpdateExpression: "SET expires_at = :expires",
|
|
69
|
+
ConditionExpression: "attribute_exists(id) AND expires_at > :now",
|
|
70
|
+
ExpressionAttributeValues: {
|
|
71
|
+
":expires": { N: String(now + ttlSeconds) },
|
|
72
|
+
":now": { N: String(now) }
|
|
73
|
+
}
|
|
74
|
+
}));
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (!isConditionalCheckFailed(error)) throw error;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
function parseSessionData(raw) {
|
|
81
|
+
if (raw === void 0) return void 0;
|
|
82
|
+
try {
|
|
83
|
+
const parsed = JSON.parse(raw);
|
|
84
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return void 0;
|
|
85
|
+
return parsed;
|
|
86
|
+
} catch {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function isConditionalCheckFailed(error) {
|
|
91
|
+
if (!error || typeof error !== "object") return false;
|
|
92
|
+
return error.name === "ConditionalCheckFailedException";
|
|
93
|
+
}
|
|
94
|
+
function isMissingModule(error, moduleName) {
|
|
95
|
+
if (!error || typeof error !== "object") return false;
|
|
96
|
+
if (error.code === "ERR_MODULE_NOT_FOUND") return true;
|
|
97
|
+
const message = String(error.message ?? "");
|
|
98
|
+
return message.includes(`Cannot find package '${moduleName}'`) || message.includes(`Cannot find module '${moduleName}'`);
|
|
99
|
+
}
|
|
100
|
+
async function importDynamoDb() {
|
|
101
|
+
const moduleName = "@aws-sdk/client-dynamodb";
|
|
102
|
+
try {
|
|
103
|
+
return await import(moduleName);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (isMissingModule(error, moduleName)) throw new Error(`Missing optional dependency "${moduleName}". Install it to use the dynamodb session driver.`);
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/session/index.ts
|
|
111
|
+
/**
|
|
112
|
+
* Adds the `dynamodb` driver to a manager. A function call, never a module
|
|
113
|
+
* side effect: a bundler that drops an unused import would otherwise drop the
|
|
114
|
+
* driver with it (RFC 0020 §4, same rule as core's `database`).
|
|
115
|
+
*
|
|
116
|
+
* @example registerDynamoDbSessionDriver(createSessionManager(sessionConfig))
|
|
117
|
+
*/
|
|
118
|
+
function registerDynamoDbSessionDriver(manager) {
|
|
119
|
+
manager.registerDriver("dynamodb", ({ table, ...options }) => {
|
|
120
|
+
const name = table ?? process.env.DYNAMODB_SESSIONS_TABLE;
|
|
121
|
+
if (!name) throw new Error("The dynamodb session driver needs a table: set DYNAMODB_SESSIONS_TABLE, or pass `table` in config/session.ts.");
|
|
122
|
+
return new DynamoDbSessionStore({
|
|
123
|
+
...options,
|
|
124
|
+
table: name
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
4
129
|
//#region src/index.ts
|
|
5
130
|
const factory = definePlugin({
|
|
6
131
|
name: "lambda",
|
|
@@ -9,13 +134,10 @@ const factory = definePlugin({
|
|
|
9
134
|
/**
|
|
10
135
|
* Register the Lambda plugin.
|
|
11
136
|
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* createApp({ providers: [lambdaPlugin()] })
|
|
15
|
-
* ```
|
|
137
|
+
* @example createApp({ providers: [lambdaPlugin()] })
|
|
16
138
|
*/
|
|
17
139
|
function lambdaPlugin(config = {}) {
|
|
18
140
|
return factory(config);
|
|
19
141
|
}
|
|
20
142
|
//#endregion
|
|
21
|
-
export { LAMBDA_HANDLER_EXPORTS, LAMBDA_HANDLER_MODULE, buildLambdaOutput, lambdaHandlerId, lambdaPlugin };
|
|
143
|
+
export { DynamoDbSessionStore, LAMBDA_HANDLER_EXPORTS, LAMBDA_HANDLER_MODULE, buildLambdaOutput, lambdaHandlerId, lambdaPlugin, registerDynamoDbSessionDriver };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guren/plugin-lambda",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -22,6 +22,14 @@
|
|
|
22
22
|
"names": [
|
|
23
23
|
"lambda:build"
|
|
24
24
|
]
|
|
25
|
+
},
|
|
26
|
+
"drivers": {
|
|
27
|
+
"session": [
|
|
28
|
+
{
|
|
29
|
+
"name": "dynamodb",
|
|
30
|
+
"persistent": true
|
|
31
|
+
}
|
|
32
|
+
]
|
|
25
33
|
}
|
|
26
34
|
},
|
|
27
35
|
"files": [
|
|
@@ -45,14 +53,22 @@
|
|
|
45
53
|
"typecheck": "tsc --noEmit"
|
|
46
54
|
},
|
|
47
55
|
"dependencies": {
|
|
48
|
-
"@guren/core": "^1.
|
|
56
|
+
"@guren/core": "^1.16.0",
|
|
49
57
|
"citty": "^0.1.6"
|
|
50
58
|
},
|
|
51
59
|
"peerDependencies": {
|
|
60
|
+
"@aws-sdk/client-dynamodb": "^3.0.0",
|
|
61
|
+
"@guren/server": ">=2.20.0",
|
|
52
62
|
"aws-cdk-lib": "^2.156.0",
|
|
53
63
|
"constructs": "^10.0.0"
|
|
54
64
|
},
|
|
55
65
|
"peerDependenciesMeta": {
|
|
66
|
+
"@aws-sdk/client-dynamodb": {
|
|
67
|
+
"optional": true
|
|
68
|
+
},
|
|
69
|
+
"@guren/server": {
|
|
70
|
+
"optional": true
|
|
71
|
+
},
|
|
56
72
|
"aws-cdk-lib": {
|
|
57
73
|
"optional": true
|
|
58
74
|
},
|