[python][ruby][cli] Remove deprecated createLambda() usage (#11080)

* Use `Lambda` constructor instead of `createLambda()`
* Return `FileBlob` instance for the entrypoint files, so that they do not get written to cwd
* In `vc dev`, support `Lambda` instances which do not have `zipBuffer` property
This commit is contained in:
Nathan Rajlich
2024-01-23 11:53:36 -08:00
committed by GitHub
parent 2b71ee6b42
commit 62ca2efa73
6 changed files with 63 additions and 31 deletions

View File

@@ -0,0 +1,6 @@
---
'@vercel/python': patch
'@vercel/ruby': patch
---
Remove deprecated `createLambda()` usage

View File

@@ -0,0 +1,5 @@
---
'vercel': patch
---
Update `vc dev` to support `Lambda` instances without `zipBuffer`

View File

@@ -42,6 +42,18 @@ async function processMessage(message) {
// structure to JSON" errors, so delete the property...
delete result.childProcesses;
if (builder.version === 3) {
if (result.output.type === 'Lambda') {
result.output.zipBuffer = await result.output.createZip();
}
} else {
for (const output of Object.values(result.output)) {
if (output.type === 'Lambda') {
output.zipBuffer = await output.createZip();
}
}
}
process.send({ type: 'buildResult', result });
}

View File

@@ -361,8 +361,10 @@ export async function executeBuild(
await oldAsset.fn.destroy();
}
const ZipFile = asset.zipBuffer || (await asset.createZip());
asset.fn = await createFunction({
Code: { ZipFile: asset.zipBuffer },
Code: { ZipFile },
Handler: asset.handler,
Runtime: asset.runtime,
MemorySize: asset.memory || 3008,

View File

@@ -1,23 +1,27 @@
import { join, dirname, basename } from 'path';
import execa from 'execa';
import fs from 'fs';
import execa from 'execa';
import { promisify } from 'util';
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
import { join, dirname, basename } from 'path';
import {
GlobOptions,
BuildOptions,
getWriteableDirectory,
download,
glob,
createLambda,
Lambda,
FileBlob,
shouldServe,
debug,
NowBuildError,
type BuildOptions,
type GlobOptions,
type BuildV3,
type Files,
} from '@vercel/build-utils';
import { installRequirement, installRequirementsFile } from './install';
import { getLatestPythonVersion, getSupportedPythonVersion } from './version';
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
async function pipenvConvert(cmd: string, srcDir: string) {
debug('Running pipfile2req...');
try {
@@ -53,13 +57,13 @@ export async function downloadFilesInWorkPath({
return workPath;
}
export const build = async ({
export const build: BuildV3 = async ({
workPath,
files: originalFiles,
entrypoint,
meta = {},
config,
}: BuildOptions) => {
}) => {
let pythonVersion = getLatestPythonVersion(meta);
workPath = await downloadFilesInWorkPath({
@@ -190,12 +194,6 @@ export const build = async ({
.replace(/__VC_HANDLER_MODULE_NAME/g, moduleName)
.replace(/__VC_HANDLER_ENTRYPOINT/g, entrypointWithSuffix);
// in order to allow the user to have `server.py`, we need our `server.py` to be called
// somethig else
const handlerPyFilename = 'vc__handler__python';
await writeFile(join(workPath, `${handlerPyFilename}.py`), handlerPyContents);
const globOptions: GlobOptions = {
cwd: workPath,
ignore:
@@ -204,14 +202,22 @@ export const build = async ({
: 'node_modules/**',
};
const lambda = await createLambda({
files: await glob('**', globOptions),
const files: Files = await glob('**', globOptions);
// in order to allow the user to have `server.py`, we
// need our `server.py` to be called something else
const handlerPyFilename = 'vc__handler__python';
files[`${handlerPyFilename}.py`] = new FileBlob({ data: handlerPyContents });
const output = new Lambda({
files,
handler: `${handlerPyFilename}.vc_handler`,
runtime: pythonVersion.runtime,
environment: {},
});
return { output: lambda };
return { output };
};
export { shouldServe };

View File

@@ -10,14 +10,16 @@ import {
writeFile,
} from 'fs-extra';
import {
BuildOptions,
download,
getWriteableDirectory,
glob,
createLambda,
Lambda,
debug,
walkParentDirs,
cloneEnv,
FileBlob,
type Files,
type BuildV3,
} from '@vercel/build-utils';
import { installBundler } from './install-ruby';
@@ -114,13 +116,13 @@ async function bundleInstall(
export const version = 3;
export async function build({
export const build: BuildV3 = async ({
workPath,
files,
entrypoint,
config,
meta = {},
}: BuildOptions) {
}) => {
await download(files, workPath, meta);
const entrypointFsDirname = join(workPath, dirname(entrypoint));
const gemfileName = 'Gemfile';
@@ -217,12 +219,11 @@ export async function build({
// somethig else
const handlerRbFilename = 'vc__handler__ruby';
await writeFile(
join(workPath, `${handlerRbFilename}.rb`),
nowHandlerRbContents
);
const outputFiles: Files = await glob('**', workPath);
const outputFiles = await glob('**', workPath);
outputFiles[`${handlerRbFilename}.rb`] = new FileBlob({
data: nowHandlerRbContents,
});
// static analysis is impossible with ruby.
// instead, provide `includeFiles` and `excludeFiles` config options to reduce bundle size.
@@ -253,12 +254,12 @@ export async function build({
}
}
const lambda = await createLambda({
const output = new Lambda({
files: outputFiles,
handler: `${handlerRbFilename}.vc__handler`,
runtime,
environment: {},
});
return { output: lambda };
}
return { output };
};