Build utilities for Vercel platform builders and serverless functions, providing utilities for creating and managing Lambda functions, file operations, and build-time helpers for the Vercel deployment platform
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Creation and management of serverless Lambda functions with ZIP packaging, configuration options, and deployment utilities for the Vercel platform.
Represents a serverless Lambda function with all necessary deployment information.
/**
* Represents a serverless Lambda function
*/
class Lambda {
type: 'Lambda';
zipBuffer: Buffer;
handler: string;
runtime: string;
memory?: number;
maxDuration?: number;
environment: Environment;
constructor(options: LambdaOptions);
}
interface LambdaOptions {
zipBuffer: Buffer;
handler: string;
runtime: string;
memory?: number;
maxDuration?: number;
environment: Environment;
}
interface Environment {
[key: string]: string;
}Usage Examples:
import { Lambda, createLambda } from "@vercel/build-utils";
// Create Lambda directly (advanced usage - typically use createLambda instead)
// Note: zipBuffer must be created externally as createZip is not exported
const zipBuffer = /* externally created ZIP buffer */;
const lambda = new Lambda({
zipBuffer,
handler: "index.handler",
runtime: "nodejs18.x",
memory: 512,
maxDuration: 30,
environment: {
NODE_ENV: "production",
API_KEY: "secret"
}
});
// Recommended approach: use createLambda
const lambda = await createLambda({
files,
handler: "index.handler",
runtime: "nodejs18.x",
memory: 512,
maxDuration: 30,
environment: {
NODE_ENV: "production",
API_KEY: "secret"
}
});Creates a Lambda instance from files with automatic ZIP packaging.
/**
* Creates a Lambda instance from files
* @param options - Lambda creation options
* @returns Promise resolving to Lambda instance
*/
function createLambda(options: CreateLambdaOptions): Promise<Lambda>;
interface CreateLambdaOptions {
files: Files;
handler: string;
runtime: string;
memory?: number;
maxDuration?: number;
environment?: Environment;
}Usage Examples:
import { createLambda, FileBlob, FileFsRef } from "@vercel/build-utils";
// Create Lambda from in-memory files
const files = {
"index.js": new FileBlob({
data: `
exports.handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify({ message: "Hello World" })
};
};
`
}),
"package.json": new FileBlob({
data: JSON.stringify({
name: "my-lambda",
main: "index.js"
})
})
};
const lambda = await createLambda({
files,
handler: "index.handler",
runtime: "nodejs18.x",
memory: 256,
maxDuration: 10,
environment: {
NODE_ENV: "production"
}
});
// Create Lambda from filesystem files
const fsFiles = {
"index.js": await FileFsRef.fromFsPath({ fsPath: "/src/handler.js" }),
"lib/utils.js": await FileFsRef.fromFsPath({ fsPath: "/src/lib/utils.js" })
};
const fsLambda = await createLambda({
files: fsFiles,
handler: "index.handler",
runtime: "nodejs18.x"
});Extracts Lambda configuration options from build configuration.
/**
* Extracts Lambda options from configuration
* @param options - Function configuration options
* @returns Promise resolving to Lambda options
*/
function getLambdaOptionsFromFunction(options: GetLambdaOptionsFromFunctionOptions): Promise<Pick<LambdaOptions, 'memory' | 'maxDuration'>>;
interface GetLambdaOptionsFromFunctionOptions {
sourceFile: string;
config?: Config;
}Usage Examples:
import { getLambdaOptionsFromFunction } from "@vercel/build-utils";
// Extract options from configuration
const config = {
functions: {
"api/handler.js": {
memory: 512,
maxDuration: 30
},
"api/*.js": {
memory: 256,
maxDuration: 10
}
}
};
const options = await getLambdaOptionsFromFunction({
sourceFile: "api/handler.js",
config
});
// Returns: { memory: 512, maxDuration: 30 }
// Pattern matching
const patternOptions = await getLambdaOptionsFromFunction({
sourceFile: "api/users.js",
config
});
// Returns: { memory: 256, maxDuration: 10 }Represents prerender configuration for static generation with Lambda fallback.
/**
* Represents prerender configuration for static generation
*/
class Prerender {
type: 'Prerender';
expiration: number | false;
lambda: Lambda;
fallback: FileBlob | FileFsRef | FileRef | null;
group?: number;
bypassToken: string | null;
constructor(options: PrerenderOptions);
}
interface PrerenderOptions {
expiration: number | false;
lambda: Lambda;
fallback: FileBlob | FileFsRef | FileRef | null;
group?: number;
bypassToken?: string | null;
}Usage Examples:
import { Prerender, createLambda, FileBlob } from "@vercel/build-utils";
// Create Lambda for prerendering
const lambda = await createLambda({
files: { "index.js": new FileBlob({ data: "// SSG lambda code" }) },
handler: "index.handler",
runtime: "nodejs18.x"
});
// Create fallback page
const fallback = new FileBlob({
data: "<html><body>Loading...</body></html>",
contentType: "text/html"
});
// Create prerender configuration
const prerender = new Prerender({
expiration: 3600, // 1 hour cache
lambda,
fallback,
group: 1,
bypassToken: "bypass-token-must-be-32-chars-long"
});
// No expiration (static forever)
const staticPrerender = new Prerender({
expiration: false,
lambda,
fallback: null
});Supported runtimes for Lambda functions:
nodejs18.x - Node.js 18.xnodejs16.x - Node.js 16.x (deprecated)nodejs14.x - Node.js 14.x (deprecated)python3.9 - Python 3.9python3.8 - Python 3.8go1.x - Go 1.xLambda functions support environment variables for configuration:
const lambda = await createLambda({
files,
handler: "index.handler",
runtime: "nodejs18.x",
environment: {
NODE_ENV: "production",
DATABASE_URL: "postgresql://...",
API_SECRET: "secret-key",
REGION: "us-east-1"
}
});Install with Tessl CLI
npx tessl i tessl/npm-vercel--build-utils