diff --git a/README.md b/README.md index 94d5de4..1f3478a 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ First, you'll need a `vercel.json` file in your project: { "functions": { "api/**/*.rs": { - "runtime": "vercel-rust@4.0.0-beta.1" + "runtime": "vercel-rust@4.0.0-beta.2" } } } diff --git a/examples/cron/vercel.json b/examples/cron/vercel.json index 1256a50..d114cc3 100644 --- a/examples/cron/vercel.json +++ b/examples/cron/vercel.json @@ -1,7 +1,7 @@ { "functions": { "api/**/*.rs": { - "runtime": "vercel-rust@4.0.0-beta.1" + "runtime": "vercel-rust@4.0.0-beta.2" } }, "crons": [ diff --git a/examples/nextjs/vercel.json b/examples/nextjs/vercel.json index 88678ee..5ab2393 100644 --- a/examples/nextjs/vercel.json +++ b/examples/nextjs/vercel.json @@ -1,7 +1,7 @@ { "functions": { "api/**/*.rs": { - "runtime": "vercel-rust@4.0.0-beta.1" + "runtime": "vercel-rust@4.0.0-beta.2" } } } diff --git a/examples/simple/vercel.json b/examples/simple/vercel.json index 88678ee..5ab2393 100644 --- a/examples/simple/vercel.json +++ b/examples/simple/vercel.json @@ -1,7 +1,7 @@ { "functions": { "api/**/*.rs": { - "runtime": "vercel-rust@4.0.0-beta.1" + "runtime": "vercel-rust@4.0.0-beta.2" } } } diff --git a/package.json b/package.json index 7d20dc0..3d38ea5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vercel-rust", - "version": "4.0.0-beta.1", + "version": "4.0.0-beta.2", "description": "Rust runtime for Vercel Functions.", "homepage": "https://github.com/vercel-community/rust", "repository": { diff --git a/src/index.ts b/src/index.ts index 9ee3004..c219a25 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ -import fs from 'node:fs'; import path from 'node:path'; import type { BuildOptions, BuildResultV3 } from '@vercel/build-utils'; import { @@ -6,57 +5,25 @@ import { debug, download, glob, - runShellScript, createLambda, } from '@vercel/build-utils'; import execa from 'execa'; import { installRustToolchain } from './lib/rust-toolchain'; import type { Runtime } from './lib/runtime'; -import { getCargoMetadata, findBinaryName, findCargoWorkspace } from './lib/cargo'; +import { + getCargoMetadata, + findBinaryName, + findCargoWorkspace, +} from './lib/cargo'; +import { + assertEnv, + getExecutableName, + gatherExtraFiles, + runUserScripts, +} from './lib/utils'; type RustEnv = Record<'RUSTFLAGS' | 'PATH', string>; -function assertEnv(name: string): string { - if (!process.env[name]) { - throw new Error(`Missing ENV variable process.env.${name}`); - } - - return process.env[name] as unknown as string; -} - -async function runUserScripts(dir: string): Promise { - const buildScriptPath = path.join(dir, 'build.sh'); - const buildScriptExists = fs.existsSync(buildScriptPath); - - if (buildScriptExists) { - debug('Running `build.sh`'); - await runShellScript(buildScriptPath); - } -} - -async function gatherExtraFiles( - globMatcher: string | string[] | undefined, - workPath: string, -): Promise> { - if (!globMatcher) return {}; - - debug( - `Gathering extra files for glob \`${JSON.stringify( - globMatcher, - )}\` in ${workPath}`, - ); - - if (Array.isArray(globMatcher)) { - const allMatches = await Promise.all( - globMatcher.map((pattern) => glob(pattern, workPath)), - ); - - return allMatches.reduce((acc, matches) => ({ ...acc, ...matches }), {}); - } - - return glob(globMatcher, workPath); -} - async function buildHandler(options: BuildOptions): Promise { const BUILDER_DEBUG = Boolean(process.env.VERCEL_BUILDER_DEBUG ?? false); const { files, entrypoint, workPath, config, meta } = options; @@ -103,11 +70,7 @@ async function buildHandler(options: BuildOptions): Promise { throw err; } - debug(`Building \`${binaryName}\` completed`); - - // The compiled binary in Windows has the `.exe` extension - const binExtension = process.platform === 'win32' ? '.exe' : ''; - const bootstrap = `bootstrap${binExtension}`; + debug(`Building \`${binaryName}\` for \`${process.platform}\` completed`); const { target_directory: targetDirectory } = await getCargoMetadata({ cwd: process.cwd(), @@ -117,9 +80,10 @@ async function buildHandler(options: BuildOptions): Promise { const bin = path.join( targetDirectory, BUILDER_DEBUG ? 'debug' : 'release', - binaryName, + getExecutableName(binaryName), ); + const bootstrap = getExecutableName('bootstrap'); const lambda = await createLambda({ files: { ...extraFiles, @@ -141,7 +105,6 @@ const runtime: Runtime = { prepareCache: async ({ workPath }) => { debug(`Caching \`${workPath}\``); const cacheFiles = await glob('target/**', workPath); - // Convert this into a reduce for (const f of Object.keys(cacheFiles)) { const accept = @@ -155,7 +118,6 @@ const runtime: Runtime = { delete cacheFiles[f]; } } - return cacheFiles; }, shouldServe: async (options): Promise => { diff --git a/src/lib/rust-toolchain.ts b/src/lib/rust-toolchain.ts index 25b4a1e..42722fa 100644 --- a/src/lib/rust-toolchain.ts +++ b/src/lib/rust-toolchain.ts @@ -5,7 +5,7 @@ async function downloadRustToolchain({ version = 'stable', }: { version?: string; -}) { +}): Promise { try { await execa( `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${version}`, @@ -21,7 +21,7 @@ async function downloadRustToolchain({ } } -export const installRustToolchain = async (version?: string) => { +export const installRustToolchain = async (version?: string): Promise => { try { await execa(`rustup -V`, [], { shell: true, stdio: 'ignore' }); debug('Rust Toolchain is already installed, skipping download'); diff --git a/src/lib/utils.ts b/src/lib/utils.ts new file mode 100644 index 0000000..123c33a --- /dev/null +++ b/src/lib/utils.ts @@ -0,0 +1,49 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import type { FileFsRef } from '@vercel/build-utils'; +import { debug, glob, runShellScript } from '@vercel/build-utils'; + +export function getExecutableName(binName: string): string { + // The compiled binary in Windows has the `.exe` extension + return process.platform === 'win32' ? `${binName}.exe` : binName; +} + +export function assertEnv(name: string): string { + if (!process.env[name]) { + throw new Error(`Missing ENV variable process.env.${name}`); + } + return process.env[name] as unknown as string; +} + +export async function runUserScripts(dir: string): Promise { + const buildScriptPath = path.join(dir, 'build.sh'); + const buildScriptExists = fs.existsSync(buildScriptPath); + + if (buildScriptExists) { + debug('Running `build.sh`'); + await runShellScript(buildScriptPath); + } +} + +export async function gatherExtraFiles( + globMatcher: string | string[] | undefined, + workPath: string, +): Promise> { + if (!globMatcher) return {}; + + debug( + `Gathering extra files for glob \`${JSON.stringify( + globMatcher, + )}\` in ${workPath}`, + ); + + if (Array.isArray(globMatcher)) { + const allMatches = await Promise.all( + globMatcher.map((pattern) => glob(pattern, workPath)), + ); + + return allMatches.reduce((acc, matches) => ({ ...acc, ...matches }), {}); + } + + return glob(globMatcher, workPath); +} diff --git a/test/fixtures/01-include-files/vercel.json b/test/fixtures/01-include-files/vercel.json index bae9a18..0a36219 100644 --- a/test/fixtures/01-include-files/vercel.json +++ b/test/fixtures/01-include-files/vercel.json @@ -1,7 +1,7 @@ { "functions": { "api/**/*.rs": { - "runtime": "vercel-rust@4.0.0-beta.1", + "runtime": "vercel-rust@4.0.0-beta.2", "includeFiles": "static/**/*.{txt,svg}" } } diff --git a/test/fixtures/02-with-utility/vercel.json b/test/fixtures/02-with-utility/vercel.json index 88678ee..5ab2393 100644 --- a/test/fixtures/02-with-utility/vercel.json +++ b/test/fixtures/02-with-utility/vercel.json @@ -1,7 +1,7 @@ { "functions": { "api/**/*.rs": { - "runtime": "vercel-rust@4.0.0-beta.1" + "runtime": "vercel-rust@4.0.0-beta.2" } } } diff --git a/test/fixtures/03-with-function/vercel.json b/test/fixtures/03-with-function/vercel.json index 88678ee..5ab2393 100644 --- a/test/fixtures/03-with-function/vercel.json +++ b/test/fixtures/03-with-function/vercel.json @@ -1,7 +1,7 @@ { "functions": { "api/**/*.rs": { - "runtime": "vercel-rust@4.0.0-beta.1" + "runtime": "vercel-rust@4.0.0-beta.2" } } } diff --git a/test/fixtures/04-with-parameter/vercel.json b/test/fixtures/04-with-parameter/vercel.json index 88678ee..5ab2393 100644 --- a/test/fixtures/04-with-parameter/vercel.json +++ b/test/fixtures/04-with-parameter/vercel.json @@ -1,7 +1,7 @@ { "functions": { "api/**/*.rs": { - "runtime": "vercel-rust@4.0.0-beta.1" + "runtime": "vercel-rust@4.0.0-beta.2" } } } diff --git a/test/fixtures/05-with-similar-entrypaths/vercel.json b/test/fixtures/05-with-similar-entrypaths/vercel.json index 88678ee..5ab2393 100644 --- a/test/fixtures/05-with-similar-entrypaths/vercel.json +++ b/test/fixtures/05-with-similar-entrypaths/vercel.json @@ -1,7 +1,7 @@ { "functions": { "api/**/*.rs": { - "runtime": "vercel-rust@4.0.0-beta.1" + "runtime": "vercel-rust@4.0.0-beta.2" } } }