Skip to content

Commit 30deb7b

Browse files
committed
fix(cli): address PR review on lambda CLI
Two blockers + four important items from Vai's review: - `--memory` was parsed and recorded in the local state file but never forwarded to `sam deploy` as a parameter override. Worse, `progress.ts` then read the *recorded* value for cost math, so `--memory 5120` produced wrong cost numbers downstream. Thread `LambdaMemoryMb` through samDeploy's --parameter-overrides. - `--profile` was only consumed by deploy / destroy. render and progress fell back to the default credentials chain — a user with `--profile prod` would silently render against their default account (wrong-account billing footgun). Set `process.env.AWS_PROFILE` (and `AWS_REGION`) in the dispatcher before any subverb runs; the AWS SDK reads them natively, so render / progress / sites all benefit without each subverb threading the flag through the SDK call. - `--profile` + destroy now also reads `process.env.AWS_PROFILE` as a fallback (matching deploy's existing env fallback). - `--wait --json` printed both the start handle AND the final progress snapshot, producing two concatenated JSON blobs that `jq` rejected. Now emits a single document: handle (without --wait) OR final progress (with --wait). - Negative integers on `--width` / `--height` / `--chunk-size` / `--max-parallel-chunks` / `--memory` / `--concurrency` now fail loudly via a new `parsePositiveInt` wrapper instead of flowing into the SDK and producing opaque AWS validation errors mid- render. - `DEFAULT_STACK_NAME` is now centralized to the literal `"hyperframes-default"` and consumed from one place. Previously the value was assembled as `hyperframes-${"default"}` in three sites and hardcoded as `"hyperframes-default"` in a fourth. `requireStack`'s hint now matches the dispatcher's default. The faked `SiteHandle` for `--site-id` keeps the documented placeholder fields but also surfaces `bucketName` (from PR 909's extended SiteHandle interface), matching the SDK contract. All CLI unit tests + the full bundler build still pass.
1 parent 808cf5e commit 30deb7b

7 files changed

Lines changed: 73 additions & 18 deletions

File tree

‎packages/cli/src/commands/lambda.ts‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,22 @@ export default defineCommand({
122122
return;
123123
}
124124

125-
const stackName = (args["stack-name"] as string | undefined) ?? "hyperframes-default";
125+
const stackName =
126+
(args["stack-name"] as string | undefined) ??
127+
// Lazy-imported so the dispatcher doesn't pull state.ts (and its
128+
// node:fs deps) on every CLI invocation — only on lambda runs.
129+
(await import("./lambda/state.js")).DEFAULT_STACK_NAME;
130+
131+
// Apply --profile globally before any AWS-SDK / `aws` / `sam` call runs.
132+
// The AWS SDK + the SAM CLI both read AWS_PROFILE from the environment,
133+
// so setting it here threads the value through render / progress / sites
134+
// (which don't take an explicit awsProfile arg) without each subverb
135+
// having to know about it. Region gets the same treatment so the SDK
136+
// clients constructed inside the SDK pick it up too.
137+
const profileFlag = args.profile as string | undefined;
138+
if (profileFlag) process.env.AWS_PROFILE = profileFlag;
139+
const regionFlag = args.region as string | undefined;
140+
if (regionFlag) process.env.AWS_REGION = regionFlag;
126141

127142
switch (subcommand) {
128143
case "deploy": {
@@ -131,9 +146,9 @@ export default defineCommand({
131146
stackName,
132147
region: args.region as string | undefined,
133148
awsProfile: args.profile as string | undefined,
134-
reservedConcurrency: parseIntFlag(args.concurrency),
149+
reservedConcurrency: parsePositiveInt(args.concurrency, "--concurrency"),
135150
chromeSource: parseChromeSource(args["chrome-source"]),
136-
lambdaMemoryMb: parseIntFlag(args.memory),
151+
lambdaMemoryMb: parsePositiveInt(args.memory, "--memory"),
137152
skipBuild: Boolean(args["skip-build"]),
138153
});
139154
return;
@@ -169,9 +184,9 @@ export default defineCommand({
169184
);
170185
process.exit(1);
171186
}
172-
const width = parseIntFlag(args.width);
173-
const height = parseIntFlag(args.height);
174-
if (!width || !height) {
187+
const width = parsePositiveInt(args.width, "--width");
188+
const height = parsePositiveInt(args.height, "--height");
189+
if (width === undefined || height === undefined) {
175190
console.error("[lambda render] --width and --height are required.");
176191
process.exit(1);
177192
}
@@ -191,13 +206,13 @@ export default defineCommand({
191206
format: parseFormat(args.format),
192207
codec: parseCodec(args.codec),
193208
quality: parseQuality(args.quality),
194-
chunkSize: parseIntFlag(args["chunk-size"]),
195-
maxParallelChunks: parseIntFlag(args["max-parallel-chunks"]),
209+
chunkSize: parsePositiveInt(args["chunk-size"], "--chunk-size"),
210+
maxParallelChunks: parsePositiveInt(args["max-parallel-chunks"], "--max-parallel-chunks"),
196211
executionName: args["execution-name"] as string | undefined,
197212
outputKey: args["output-key"] as string | undefined,
198213
json: Boolean(args.json),
199214
wait: Boolean(args.wait),
200-
waitIntervalMs: parseIntFlag(args["wait-interval-ms"]) ?? 5000,
215+
waitIntervalMs: parsePositiveInt(args["wait-interval-ms"], "--wait-interval-ms") ?? 5000,
201216
});
202217
return;
203218
}
@@ -231,6 +246,20 @@ function parseIntFlag(raw: unknown): number | undefined {
231246
return Number.isFinite(n) ? n : undefined;
232247
}
233248

249+
/**
250+
* Parse a flag that must be a positive integer (>= 1) when supplied.
251+
* Negative values or non-integers fail loudly instead of flowing into
252+
* the SDK and producing opaque AWS validation errors mid-render.
253+
*/
254+
function parsePositiveInt(raw: unknown, flagName: string): number | undefined {
255+
const n = parseIntFlag(raw);
256+
if (n === undefined) return undefined;
257+
if (!Number.isInteger(n) || n < 1) {
258+
throw new Error(`[lambda] ${flagName} must be a positive integer; got ${n}`);
259+
}
260+
return n;
261+
}
262+
234263
/**
235264
* Parse a string-union flag against a closed set of allowed values.
236265
* Returns `defaultValue` (which may be `undefined`) when the input is

‎packages/cli/src/commands/lambda/deploy.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const DEFAULT_CONCURRENCY = 8;
3535

3636
export async function runDeploy(args: Partial<DeployArgs> = {}): Promise<void> {
3737
const resolved: DeployArgs = {
38-
stackName: args.stackName ?? `hyperframes-${DEFAULT_STACK_NAME}`,
38+
stackName: args.stackName ?? DEFAULT_STACK_NAME,
3939
region: args.region ?? process.env.AWS_REGION ?? DEFAULT_REGION,
4040
awsProfile: args.awsProfile ?? process.env.AWS_PROFILE,
4141
reservedConcurrency: args.reservedConcurrency ?? DEFAULT_CONCURRENCY,
@@ -68,6 +68,7 @@ export async function runDeploy(args: Partial<DeployArgs> = {}): Promise<void> {
6868
region: resolved.region,
6969
awsProfile: resolved.awsProfile,
7070
reservedConcurrency: resolved.reservedConcurrency,
71+
lambdaMemoryMb: resolved.lambdaMemoryMb,
7172
chromeSource: resolved.chromeSource,
7273
});
7374

‎packages/cli/src/commands/lambda/destroy.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ export interface DestroyArgs {
2222
export async function runDestroy(args: DestroyArgs): Promise<void> {
2323
const stack = requireStack(args.stackName);
2424
console.log(c.dim(`→ sam delete (stack=${stack.stackName} region=${stack.region})`));
25+
// Mirror deploy.ts's AWS_PROFILE env fallback — `AWS_PROFILE=prod
26+
// hyperframes lambda destroy` should hit the same account `deploy`
27+
// did, not the default credentials chain.
2528
samDelete({
2629
repoRoot: repoRoot(),
2730
stackName: stack.stackName,
2831
region: stack.region,
29-
awsProfile: args.awsProfile,
32+
awsProfile: args.awsProfile ?? process.env.AWS_PROFILE,
3033
});
3134
deleteStackOutputs(args.stackName);
3235
console.log();

‎packages/cli/src/commands/lambda/render.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ export async function runRender(args: RenderArgs): Promise<void> {
5151
runtimeCap: "lambda",
5252
};
5353

54+
// When the caller passes only --site-id, synthesise the minimum-shape
55+
// SiteHandle pointing at the deterministic content-addressed key. The
56+
// `bytes` / `uploadedAt` fields are intentionally placeholders — the
57+
// SDK reads only `siteId` + `projectS3Uri` when `uploaded: false`.
5458
const siteHandle = args.siteId
5559
? {
5660
siteId: args.siteId,
61+
bucketName: stack.bucketName,
5762
projectS3Uri: `s3://${stack.bucketName}/sites/${args.siteId}/project.tar.gz`,
5863
bytes: 0,
5964
uploadedAt: "",
@@ -73,9 +78,15 @@ export async function runRender(args: RenderArgs): Promise<void> {
7378
});
7479

7580
if (args.json) {
76-
console.log(JSON.stringify(handle, null, 2));
81+
// --wait + --json should emit a single parseable JSON document: the
82+
// final progress snapshot. Without --wait, emit the handle (the
83+
// caller will poll progress separately). Previously this printed
84+
// both, producing two concatenated JSON blobs that `jq -r` would
85+
// misparse.
7786
if (args.wait) {
7887
await waitForCompletion(handle.executionArn, stack, args.waitIntervalMs, args.json);
88+
} else {
89+
console.log(JSON.stringify(handle, null, 2));
7990
}
8091
return;
8192
}

‎packages/cli/src/commands/lambda/sam.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export interface DeployOptions {
5252
region: string;
5353
awsProfile?: string;
5454
reservedConcurrency?: number;
55+
/** Lambda memory in MB. Forwarded as the `LambdaMemoryMb` parameter override. */
56+
lambdaMemoryMb?: number;
5557
chromeSource?: "sparticuz" | "chrome-headless-shell";
5658
/** Pass-through stdio. Defaults to "inherit" so SAM's progress lines stream live. */
5759
stdio?: "inherit" | "pipe";
@@ -77,6 +79,13 @@ export function locateSamTemplate(repoRoot: string): string {
7779
/** Run `sam deploy` non-interactively. Returns when SAM exits 0; throws on non-zero. */
7880
export function samDeploy(opts: DeployOptions): void {
7981
assertSamAvailable();
82+
const paramOverrides = [
83+
`ChromeSource=${opts.chromeSource ?? "sparticuz"}`,
84+
`ReservedConcurrency=${opts.reservedConcurrency ?? -1}`,
85+
];
86+
if (opts.lambdaMemoryMb !== undefined) {
87+
paramOverrides.push(`LambdaMemoryMb=${opts.lambdaMemoryMb}`);
88+
}
8089
const args = [
8190
"deploy",
8291
"--stack-name",
@@ -89,8 +98,7 @@ export function samDeploy(opts: DeployOptions): void {
8998
"--no-confirm-changeset",
9099
"--no-fail-on-empty-changeset",
91100
"--parameter-overrides",
92-
`ChromeSource=${opts.chromeSource ?? "sparticuz"}`,
93-
`ReservedConcurrency=${opts.reservedConcurrency ?? -1}`,
101+
...paramOverrides,
94102
];
95103
if (opts.awsProfile) {
96104
args.push("--profile", opts.awsProfile);

‎packages/cli/src/commands/lambda/sites.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ export async function runSitesCreate(args: SitesCreateArgs): Promise<void> {
4949
console.log(
5050
c.dim(
5151
`Render with: hyperframes lambda render ${args.projectDir} --site-id=${handle.siteId}` +
52-
(args.stackName === `hyperframes-${DEFAULT_STACK_NAME}`
53-
? ""
54-
: ` --stack-name=${args.stackName}`),
52+
(args.stackName === DEFAULT_STACK_NAME ? "" : ` --stack-name=${args.stackName}`),
5553
),
5654
);
5755
}

‎packages/cli/src/commands/lambda/state.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ export interface StackOutputs {
2727

2828
const STATE_DIR_NAME = ".hyperframes";
2929
const STATE_FILE_PREFIX = "lambda-stack-";
30-
export const DEFAULT_STACK_NAME = "default";
30+
/**
31+
* Default CloudFormation stack name used when the caller doesn't pass
32+
* `--stack-name`. Centralised so deploy/destroy/sites/dispatcher all
33+
* agree on the literal `"hyperframes-default"`.
34+
*/
35+
export const DEFAULT_STACK_NAME = "hyperframes-default";
3136

3237
export function stateFilePath(
3338
stackName: string = DEFAULT_STACK_NAME,

0 commit comments

Comments
 (0)