Files
hyperframes/packages/cli/src/commands/lambda/prebuiltHandler.ts
T
Michael WangandClaude Opus 5.5 fa318d9727 feat(cli,docs): links to our sites say where they came from (#7178)
README links to hyperframes.studio, hyperframes.heygen.com and heygen.com
carry utm_source=github&utm_medium=readme, the values the README's download
links already use. Three README links went through docs redirects that drop
the query string (/showcase, /guides/claude-design, /guides/open-design);
they now point at the pages themselves.

The docs navbar's Community and changelog links carry src=docs-navbar, and
the export page's reinstall link src=docs-export, beside the existing docs-*
tags.

Links the CLI prints to our sites (init's docs links, the Lambda docs, the
publish claim URL, the HeyGen API-key page) carry utm_source=cli. Discord
invites stay untagged.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 23:19:51 +00:00

128 lines
4.6 KiB
TypeScript

// `lambda deploy` without a repo checkout: fetch the pinned handler ZIP, verify its sha256 and
// stage it beside the packaged SAM template.
import { createHash } from "node:crypto";
import {
copyFileSync,
createReadStream,
existsSync,
mkdirSync,
readdirSync,
renameSync,
unlinkSync,
} from "node:fs";
import { dirname, join } from "node:path";
import { downloadFile } from "../../utils/download.js";
import { isMissingPackageError } from "../../utils/missingPackage.js";
import { VERSION } from "../../version.js";
import { hyperframesCacheDir } from "@hyperframes/core/cache-dir";
import { cliSourced } from "../../utils/sourceTag.js";
const DOCS_URL = cliSourced("https://hyperframes.heygen.com/deploy/aws-lambda");
const HANDLER_CACHE_DIR = hyperframesCacheDir("lambda-handler");
type AdapterSdk = typeof import("@hyperframes/aws-lambda/sdk");
export interface StageDeps {
loadAdapter: () => Promise<Partial<AdapterSdk>>;
download: (url: string, dest: string, options: { maxBytes: number }) => Promise<void>;
cacheDir: string;
log: (line: string) => void;
}
export const defaultDeps: StageDeps = {
loadAdapter: () => import("@hyperframes/aws-lambda/sdk"),
download: downloadFile,
cacheDir: HANDLER_CACHE_DIR,
log: (line) => console.log(line),
};
/** The one message for "no checkout and no pinned handler": everything the user needs to fix it. */
export function noHandlerMessage(adapterInstalled: boolean): string {
const adapter = adapterInstalled
? " - The installed @hyperframes/aws-lambda has no prebuilt handler for its version. Install one that does:\n"
: " - Install the Lambda adapter; its release pins the prebuilt handler:\n";
return (
"[lambda deploy] This is not a HyperFrames checkout, and no prebuilt Lambda handler is available.\n" +
adapter +
` npm install -g @hyperframes/aws-lambda@${VERSION}\n` +
" - Or run from a hyperframes checkout, or set HYPERFRAMES_REPO_ROOT to one.\n" +
`Docs: ${DOCS_URL}`
);
}
async function sha256File(path: string): Promise<string> {
const hash = createHash("sha256");
for await (const chunk of createReadStream(path)) hash.update(chunk);
return hash.digest("hex");
}
export interface PinnedAsset {
version: string;
url: string;
sha256: string;
bytes: number;
}
export async function fetchPinned(
pinned: PinnedAsset,
asset: { fileName: string; label: string; urlOverride?: string },
deps: Pick<StageDeps, "download" | "cacheDir" | "log">,
): Promise<string> {
const dir = join(deps.cacheDir, `${pinned.version}-${pinned.sha256}`);
const file = join(dir, asset.fileName);
if (existsSync(file) && (await sha256File(file)) === pinned.sha256) {
deps.log(`→ Using verified ${asset.label} ${pinned.version} from ${dir}`);
return file;
}
// Override for mirrors and pre-release testing; the pinned sha256 still decides.
const url = asset.urlOverride ?? pinned.url;
deps.log(`→ Downloading ${asset.label} ${pinned.version} (${pinned.bytes} bytes)`);
mkdirSync(dir, { recursive: true });
const partial = `${file}.download`;
await deps.download(url, partial, { maxBytes: pinned.bytes });
const actual = await sha256File(partial);
if (actual !== pinned.sha256) {
unlinkSync(partial);
const urlWithoutCredentials = url.split("?")[0];
throw new Error(
`[lambda deploy] Refusing the ${asset.label} from ${urlWithoutCredentials}: sha256 ${actual} does not match ` +
`the ${pinned.sha256} pinned by @hyperframes/aws-lambda@${pinned.version}.`,
);
}
renameSync(partial, file);
deps.log(`→ Verified sha256 ${pinned.sha256}`);
return file;
}
/** Returns the directory holding the verified `handler.zip` and its `template.yaml`. */
export async function stagePrebuiltHandler(deps: StageDeps = defaultDeps): Promise<string> {
let sdk: Partial<AdapterSdk>;
try {
sdk = await deps.loadAdapter();
} catch (err) {
if (isMissingPackageError(err, "@hyperframes/aws-lambda"))
throw new Error(noHandlerMessage(false));
throw err;
}
// Adapters released before handler zips were published lack prebuiltHandler.
const pinned = sdk.prebuiltHandler?.();
if (!pinned) throw new Error(noHandlerMessage(true));
const zip = await fetchPinned(
pinned,
{
fileName: "handler.zip",
label: "handler ZIP",
urlOverride: process.env.HYPERFRAMES_LAMBDA_HANDLER_URL,
},
deps,
);
const dir = dirname(zip);
const templateDir = dirname(pinned.templatePath);
for (const file of readdirSync(templateDir).filter((name) => name.endsWith(".yaml"))) {
copyFileSync(join(templateDir, file), join(dir, file));
}
return dir;
}