@guren/plugin-lambda 0.2.0 → 0.2.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/dist/build-56qBHosh.js +221 -0
- package/dist/cdk/index.d.ts +56 -56
- package/dist/cdk/index.js +164 -178
- package/dist/commands.d.ts +21 -22
- package/dist/commands.js +36 -45
- package/dist/handlers-CkWGWQQX.js +19 -0
- package/dist/index.d.ts +38 -37
- package/dist/index.js +17 -22
- package/package.json +5 -5
- package/dist/chunk-K64F77DP.js +0 -12
- package/dist/chunk-OTE5NGTW.js +0 -262
package/dist/index.js
CHANGED
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from "./chunk-OTE5NGTW.js";
|
|
4
|
-
import {
|
|
5
|
-
LAMBDA_HANDLER_EXPORTS,
|
|
6
|
-
LAMBDA_HANDLER_MODULE,
|
|
7
|
-
lambdaHandlerId
|
|
8
|
-
} from "./chunk-K64F77DP.js";
|
|
9
|
-
|
|
10
|
-
// src/index.ts
|
|
1
|
+
import { n as LAMBDA_HANDLER_MODULE, r as lambdaHandlerId, t as LAMBDA_HANDLER_EXPORTS } from "./handlers-CkWGWQQX.js";
|
|
2
|
+
import { t as buildLambdaOutput } from "./build-56qBHosh.js";
|
|
11
3
|
import { definePlugin } from "@guren/core";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
const factory = definePlugin({
|
|
6
|
+
name: "lambda",
|
|
7
|
+
register() {}
|
|
16
8
|
});
|
|
9
|
+
/**
|
|
10
|
+
* Register the Lambda plugin.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* createApp({ providers: [lambdaPlugin()] })
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
17
|
function lambdaPlugin(config = {}) {
|
|
18
|
-
|
|
18
|
+
return factory(config);
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
LAMBDA_HANDLER_MODULE,
|
|
23
|
-
buildLambdaOutput,
|
|
24
|
-
lambdaHandlerId,
|
|
25
|
-
lambdaPlugin
|
|
26
|
-
};
|
|
20
|
+
//#endregion
|
|
21
|
+
export { LAMBDA_HANDLER_EXPORTS, LAMBDA_HANDLER_MODULE, buildLambdaOutput, lambdaHandlerId, lambdaPlugin };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guren/plugin-lambda",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -40,12 +40,12 @@
|
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
|
-
"build": "
|
|
43
|
+
"build": "bun --bun tsdown",
|
|
44
44
|
"test": "bun test",
|
|
45
45
|
"typecheck": "tsc --noEmit"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@guren/core": "^1.
|
|
48
|
+
"@guren/core": "^1.8.1",
|
|
49
49
|
"citty": "^0.1.6"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"@types/node": "^20.14.10",
|
|
65
65
|
"aws-cdk-lib": "^2.262.1",
|
|
66
66
|
"constructs": "^10.7.1",
|
|
67
|
-
"
|
|
68
|
-
"typescript": "^
|
|
67
|
+
"tsdown": "^0.22.14",
|
|
68
|
+
"typescript": "^7.0.2"
|
|
69
69
|
}
|
|
70
70
|
}
|
package/dist/chunk-K64F77DP.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// src/handlers.ts
|
|
2
|
-
var LAMBDA_HANDLER_MODULE = "handler";
|
|
3
|
-
var LAMBDA_HANDLER_EXPORTS = ["http", "queue", "schedule", "console"];
|
|
4
|
-
function lambdaHandlerId(name) {
|
|
5
|
-
return `${LAMBDA_HANDLER_MODULE}.${name}`;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
LAMBDA_HANDLER_MODULE,
|
|
10
|
-
LAMBDA_HANDLER_EXPORTS,
|
|
11
|
-
lambdaHandlerId
|
|
12
|
-
};
|
package/dist/chunk-OTE5NGTW.js
DELETED
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LAMBDA_HANDLER_EXPORTS,
|
|
3
|
-
LAMBDA_HANDLER_MODULE
|
|
4
|
-
} from "./chunk-K64F77DP.js";
|
|
5
|
-
|
|
6
|
-
// src/build.ts
|
|
7
|
-
import { cpSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
8
|
-
import { resolve } from "path";
|
|
9
|
-
import {
|
|
10
|
-
DEV_ONLY_MODULES,
|
|
11
|
-
importSpecifier,
|
|
12
|
-
MCP_SDK_SUBPATH_PREFIX,
|
|
13
|
-
renderDevOnlyStub,
|
|
14
|
-
assertOutputDirOutsideRoot,
|
|
15
|
-
resetOutputDir,
|
|
16
|
-
resolveClientAssetEnv,
|
|
17
|
-
resolvePathLike,
|
|
18
|
-
resolveSsrEntryFile,
|
|
19
|
-
ssrRuntimePaths,
|
|
20
|
-
stageStaticAssets,
|
|
21
|
-
unusedSqlClients
|
|
22
|
-
} from "@guren/core/internal/deploy-build";
|
|
23
|
-
var MCP_UNAVAILABLE = "The MCP endpoint is unavailable on AWS Lambda \u2014 it generates files on disk.";
|
|
24
|
-
var UNAVAILABLE_ON_LAMBDA = {
|
|
25
|
-
sqlite: "bun:sqlite is unavailable on AWS Lambda \u2014 use createAwsDataApiDatabase() or createPostgresDatabase().",
|
|
26
|
-
vite: "The Vite dev server is unavailable on AWS Lambda \u2014 serve assets from S3/CloudFront.",
|
|
27
|
-
mcp: MCP_UNAVAILABLE
|
|
28
|
-
};
|
|
29
|
-
var DEV_ONLY_STUBS = Object.fromEntries(
|
|
30
|
-
DEV_ONLY_MODULES.map((module) => [
|
|
31
|
-
module.specifier,
|
|
32
|
-
renderDevOnlyStub(module, UNAVAILABLE_ON_LAMBDA[module.kind])
|
|
33
|
-
])
|
|
34
|
-
);
|
|
35
|
-
function stubsFor(root, dialects) {
|
|
36
|
-
const unused = unusedSqlClients({ root, label: "Lambda build", dialects });
|
|
37
|
-
return {
|
|
38
|
-
...DEV_ONLY_STUBS,
|
|
39
|
-
...Object.fromEntries(
|
|
40
|
-
unused.map(({ module, message }) => [module.specifier, renderDevOnlyStub(module, message)])
|
|
41
|
-
)
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
var unlistedMcpStub = `throw new Error(${JSON.stringify(MCP_UNAVAILABLE)})
|
|
45
|
-
`;
|
|
46
|
-
function escapeRegExp(value) {
|
|
47
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
48
|
-
}
|
|
49
|
-
function stubFilter(stubs) {
|
|
50
|
-
return new RegExp(
|
|
51
|
-
`^(?:${[
|
|
52
|
-
...Object.keys(stubs).map(escapeRegExp),
|
|
53
|
-
`${escapeRegExp(MCP_SDK_SUBPATH_PREFIX)}.+`
|
|
54
|
-
].join("|")})$`
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
async function buildLambdaOutput(options = {}) {
|
|
58
|
-
const root = resolvePathLike(options.rootDir ?? process.cwd());
|
|
59
|
-
const out = resolvePathLike(options.outputDir ?? resolve(root, ".lambda"));
|
|
60
|
-
const entrypoint = resolvePathLike(options.entrypoint ?? resolve(root, "src/lambda.ts"));
|
|
61
|
-
const publicDir = resolvePathLike(options.publicDir ?? resolve(root, "public"));
|
|
62
|
-
const ssrDir = resolvePathLike(options.ssrDir ?? resolve(root, ".guren/ssr"));
|
|
63
|
-
const migrationsDir = resolvePathLike(options.migrationsDir ?? resolve(root, "db/migrations"));
|
|
64
|
-
const clientEntryKey = options.clientEntryKey ?? "resources/js/app.tsx";
|
|
65
|
-
const ssrEntryKey = options.ssrEntryKey ?? "resources/js/ssr.tsx";
|
|
66
|
-
assertOutputDirOutsideRoot(out, root, "Lambda build");
|
|
67
|
-
if (!options.skipAppBuild) {
|
|
68
|
-
runAppBuild(root);
|
|
69
|
-
}
|
|
70
|
-
if (!existsSync(entrypoint)) {
|
|
71
|
-
throw new Error(
|
|
72
|
-
`Lambda build: entrypoint not found at ${entrypoint}. Run \`bunx guren plugin @guren/plugin-lambda\` to scaffold src/lambda.ts, or pass "entrypoint".`
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
const ssrFile = resolveSsrEntry(ssrDir, ssrEntryKey);
|
|
76
|
-
const assetEnv = resolveClientAssetEnv(publicDir, clientEntryKey, "Lambda build");
|
|
77
|
-
resetOutputDir(out, root, "Lambda build");
|
|
78
|
-
const funcDir = resolve(out, "function");
|
|
79
|
-
mkdirSync(funcDir, { recursive: true });
|
|
80
|
-
stageStaticAssets(publicDir, resolve(out, "assets"));
|
|
81
|
-
const env = buildLambdaEnvironment(assetEnv, ssrFile, ssrDir);
|
|
82
|
-
const wrapperPath = resolve(out, `${LAMBDA_HANDLER_MODULE}.ts`);
|
|
83
|
-
writeFileSync(wrapperPath, renderHandlerModule({ out, entrypoint, env }));
|
|
84
|
-
await bundleHandler(wrapperPath, funcDir, stubsFor(root, options.databaseDialects));
|
|
85
|
-
writeFileSync(resolve(funcDir, "package.json"), `${JSON.stringify({ type: "module" }, null, 2)}
|
|
86
|
-
`);
|
|
87
|
-
if (ssrFile) {
|
|
88
|
-
cpSync(ssrDir, resolve(funcDir, ".guren/ssr"), { recursive: true });
|
|
89
|
-
}
|
|
90
|
-
if (existsSync(migrationsDir)) {
|
|
91
|
-
cpSync(migrationsDir, resolve(funcDir, "db/migrations"), { recursive: true });
|
|
92
|
-
}
|
|
93
|
-
writeFileSync(resolve(out, "env.json"), `${JSON.stringify({ NODE_ENV: "production", ...env }, null, 2)}
|
|
94
|
-
`);
|
|
95
|
-
if (options.zip) {
|
|
96
|
-
zipFunction(out, funcDir);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
function buildLambdaEnvironment(assetEnv, ssrFile, ssrDir) {
|
|
100
|
-
const env = {};
|
|
101
|
-
if (assetEnv.entry) {
|
|
102
|
-
env.GUREN_INERTIA_ENTRY = assetEnv.entry;
|
|
103
|
-
}
|
|
104
|
-
if (assetEnv.styles) {
|
|
105
|
-
env.GUREN_INERTIA_STYLES = assetEnv.styles;
|
|
106
|
-
}
|
|
107
|
-
if (ssrFile) {
|
|
108
|
-
const ssrPaths = ssrRuntimePaths(ssrDir, ssrFile, "./.guren/ssr");
|
|
109
|
-
env.GUREN_INERTIA_SSR_ENTRY = ssrPaths.entry;
|
|
110
|
-
if (ssrPaths.manifest) {
|
|
111
|
-
env.GUREN_INERTIA_SSR_MANIFEST = ssrPaths.manifest;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return env;
|
|
115
|
-
}
|
|
116
|
-
function renderHandlerModule(input) {
|
|
117
|
-
const lines = [
|
|
118
|
-
"// Generated by `guren lambda:build`. Do not edit \u2014 regenerate instead.",
|
|
119
|
-
""
|
|
120
|
-
];
|
|
121
|
-
const envEntries = Object.entries(input.env);
|
|
122
|
-
if (envEntries.length > 0) {
|
|
123
|
-
lines.push(
|
|
124
|
-
"// Baked as defaults so a deploy needs no function configuration; real",
|
|
125
|
-
"// environment variables (e.g. absolute CloudFront URLs) still win.",
|
|
126
|
-
...envEntries.map(([key, value]) => `process.env.${key} ??= ${JSON.stringify(value)}`),
|
|
127
|
-
""
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
lines.push(
|
|
131
|
-
"// Imported dynamically so the assignments above run before the app module",
|
|
132
|
-
"// graph evaluates \u2014 static imports are hoisted past them.",
|
|
133
|
-
`const module = await import(${JSON.stringify(importSpecifier(input.out, input.entrypoint, "Lambda build"))})`,
|
|
134
|
-
""
|
|
135
|
-
);
|
|
136
|
-
for (const name of LAMBDA_HANDLER_EXPORTS) {
|
|
137
|
-
if (name === "console") {
|
|
138
|
-
lines.push("const consoleHandler = module.console", "export { consoleHandler as console }");
|
|
139
|
-
} else {
|
|
140
|
-
lines.push(`export const ${name} = module.${name}`);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
lines.push("");
|
|
144
|
-
return lines.join("\n");
|
|
145
|
-
}
|
|
146
|
-
async function bundleHandler(handlerEntry, funcDir, stubs) {
|
|
147
|
-
const filter = stubFilter(stubs);
|
|
148
|
-
const result = await Bun.build({
|
|
149
|
-
entrypoints: [handlerEntry],
|
|
150
|
-
// `Bun.build` rejects with a bare "Bundle failed" AggregateError by
|
|
151
|
-
// default (Bun >= 1.2), which discards the one line that matters — the
|
|
152
|
-
// module it could not resolve. Opting out of that is what makes the
|
|
153
|
-
// `result.logs` report below reachable at all.
|
|
154
|
-
throw: false,
|
|
155
|
-
outdir: funcDir,
|
|
156
|
-
target: "node",
|
|
157
|
-
// `identifiers: false`: class names are runtime identity here.
|
|
158
|
-
// `registerJob()`/`getJob()` key the job registry on `JobClass.name`, and
|
|
159
|
-
// that name is serialized into every queued message (SqsDriver,
|
|
160
|
-
// RedisDriver), a notification's persisted `type`, and `HttpException.name`
|
|
161
|
-
// — mangling silently breaks all of them.
|
|
162
|
-
//
|
|
163
|
-
// Not `keepNames`/`--keep-names`: as of Bun 1.3.14 both are accepted and
|
|
164
|
-
// silently leave class names mangled, so they cannot replace this.
|
|
165
|
-
minify: { whitespace: true, syntax: true, identifiers: false },
|
|
166
|
-
define: {
|
|
167
|
-
// `bun build` inlines `process.env.NODE_ENV` at bundle time (defaulting
|
|
168
|
-
// to "development"), so pin it to "production" for the deployed function.
|
|
169
|
-
"process.env.NODE_ENV": '"production"',
|
|
170
|
-
// Every module in the bundle shares one `import.meta.url` — the real
|
|
171
|
-
// runtime value of the single output file, `file:///var/task/handler.js`.
|
|
172
|
-
// That breaks the framework's `new URL('../db/migrations', import.meta.url)`
|
|
173
|
-
// convention (config/database.ts, config/app.ts's migration check): those
|
|
174
|
-
// files live one directory below the app root, so the same expression
|
|
175
|
-
// must resolve from one level under /var/task to reach the db/migrations
|
|
176
|
-
// folder `lambda:build` copies next to the bundle.
|
|
177
|
-
"import.meta.url": '"file:///var/task/config/lambda.js"'
|
|
178
|
-
},
|
|
179
|
-
// Provided by the managed Lambda runtime.
|
|
180
|
-
external: ["@aws-sdk/*"],
|
|
181
|
-
plugins: [
|
|
182
|
-
{
|
|
183
|
-
name: "guren-lambda-stubs",
|
|
184
|
-
setup(build) {
|
|
185
|
-
build.onResolve({ filter }, (args) => ({
|
|
186
|
-
path: args.path,
|
|
187
|
-
namespace: "guren-lambda-stub"
|
|
188
|
-
}));
|
|
189
|
-
build.onLoad({ filter: /.*/, namespace: "guren-lambda-stub" }, (args) => ({
|
|
190
|
-
contents: stubs[args.path] ?? unlistedMcpStub,
|
|
191
|
-
loader: "js"
|
|
192
|
-
}));
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
]
|
|
196
|
-
});
|
|
197
|
-
if (!result.success) {
|
|
198
|
-
const details = result.logs.map((log) => String(log)).join("\n");
|
|
199
|
-
throw new Error(`Lambda build: bun build failed.
|
|
200
|
-
${details}`);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
function zipFunction(out, funcDir) {
|
|
204
|
-
const result = Bun.spawnSync({
|
|
205
|
-
cmd: ["zip", "-qr", resolve(out, "function.zip"), "."],
|
|
206
|
-
cwd: funcDir,
|
|
207
|
-
stdout: "inherit",
|
|
208
|
-
stderr: "inherit"
|
|
209
|
-
});
|
|
210
|
-
if (result.exitCode !== 0) {
|
|
211
|
-
throw new Error("Lambda build: zip failed. Install the `zip` binary or deploy the function directory directly (CDK archives it for you).");
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
function hasBuildScript(root) {
|
|
215
|
-
const packageJsonPath = resolve(root, "package.json");
|
|
216
|
-
if (!existsSync(packageJsonPath)) {
|
|
217
|
-
return false;
|
|
218
|
-
}
|
|
219
|
-
try {
|
|
220
|
-
const { scripts } = JSON.parse(readFileSync(packageJsonPath, "utf8"));
|
|
221
|
-
return Boolean(scripts?.build);
|
|
222
|
-
} catch {
|
|
223
|
-
return false;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
function runAppBuild(root) {
|
|
227
|
-
if (!hasBuildScript(root)) {
|
|
228
|
-
console.warn('Lambda build: no "build" script in package.json \u2014 skipping the app build step.');
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
const result = Bun.spawnSync({
|
|
232
|
-
cmd: ["bun", "run", "build"],
|
|
233
|
-
cwd: root,
|
|
234
|
-
stdout: "inherit",
|
|
235
|
-
stderr: "inherit"
|
|
236
|
-
});
|
|
237
|
-
if (result.exitCode !== 0) {
|
|
238
|
-
throw new Error('Lambda build: the app "build" script failed.');
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
function resolveSsrEntry(ssrDir, ssrEntryKey) {
|
|
242
|
-
const file = resolveSsrEntryFile(ssrDir, ssrEntryKey, "Lambda build");
|
|
243
|
-
if (!file) {
|
|
244
|
-
return void 0;
|
|
245
|
-
}
|
|
246
|
-
if (!hasRendererExport(readFileSync(file, "utf8"))) {
|
|
247
|
-
throw new Error(
|
|
248
|
-
`Lambda build: SSR entry ${file} does not export a renderer (expected a named "render" or default export).`
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
return file;
|
|
252
|
-
}
|
|
253
|
-
function hasRendererExport(source) {
|
|
254
|
-
if (/export\s*\*\s*from/.test(source)) {
|
|
255
|
-
return true;
|
|
256
|
-
}
|
|
257
|
-
return /export\s+default\b/.test(source) || /export\s*\{[^}]*\b(?:render|default)\b[^}]*\}/.test(source) || /export\s+(?:const|let|var|function|async\s+function)\s+render\b/.test(source);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
export {
|
|
261
|
-
buildLambdaOutput
|
|
262
|
-
};
|