Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/cron/vercel.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"functions": {
"api/**/*.rs": {
"runtime": "vercel-rust@4.0.0-beta.1"
"runtime": "vercel-rust@4.0.0-beta.2"
}
},
"crons": [
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/vercel.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"functions": {
"api/**/*.rs": {
"runtime": "vercel-rust@4.0.0-beta.1"
"runtime": "vercel-rust@4.0.0-beta.2"
}
}
}
2 changes: 1 addition & 1 deletion examples/simple/vercel.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"functions": {
"api/**/*.rs": {
"runtime": "vercel-rust@4.0.0-beta.1"
"runtime": "vercel-rust@4.0.0-beta.2"
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
66 changes: 14 additions & 52 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,29 @@
import fs from 'node:fs';
import path from 'node:path';
import type { BuildOptions, BuildResultV3 } from '@vercel/build-utils';
import {
FileFsRef,
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<void> {
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<Record<string, FileFsRef>> {
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<BuildResultV3> {
const BUILDER_DEBUG = Boolean(process.env.VERCEL_BUILDER_DEBUG ?? false);
const { files, entrypoint, workPath, config, meta } = options;
Expand Down Expand Up @@ -103,11 +70,7 @@ async function buildHandler(options: BuildOptions): Promise<BuildResultV3> {
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(),
Expand All @@ -117,9 +80,10 @@ async function buildHandler(options: BuildOptions): Promise<BuildResultV3> {
const bin = path.join(
targetDirectory,
BUILDER_DEBUG ? 'debug' : 'release',
binaryName,
getExecutableName(binaryName),
);

const bootstrap = getExecutableName('bootstrap');
const lambda = await createLambda({
files: {
...extraFiles,
Expand All @@ -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 =
Expand All @@ -155,7 +118,6 @@ const runtime: Runtime = {
delete cacheFiles[f];
}
}

return cacheFiles;
},
shouldServe: async (options): Promise<boolean> => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/rust-toolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async function downloadRustToolchain({
version = 'stable',
}: {
version?: string;
}) {
}): Promise<void> {
try {
await execa(
`curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${version}`,
Expand All @@ -21,7 +21,7 @@ async function downloadRustToolchain({
}
}

export const installRustToolchain = async (version?: string) => {
export const installRustToolchain = async (version?: string): Promise<void> => {
try {
await execa(`rustup -V`, [], { shell: true, stdio: 'ignore' });
debug('Rust Toolchain is already installed, skipping download');
Expand Down
49 changes: 49 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<Record<string, FileFsRef>> {
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);
}
2 changes: 1 addition & 1 deletion test/fixtures/01-include-files/vercel.json
Original file line number Diff line number Diff line change
@@ -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}"
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/02-with-utility/vercel.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"functions": {
"api/**/*.rs": {
"runtime": "vercel-rust@4.0.0-beta.1"
"runtime": "vercel-rust@4.0.0-beta.2"
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/03-with-function/vercel.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"functions": {
"api/**/*.rs": {
"runtime": "vercel-rust@4.0.0-beta.1"
"runtime": "vercel-rust@4.0.0-beta.2"
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/04-with-parameter/vercel.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"functions": {
"api/**/*.rs": {
"runtime": "vercel-rust@4.0.0-beta.1"
"runtime": "vercel-rust@4.0.0-beta.2"
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/05-with-similar-entrypaths/vercel.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"functions": {
"api/**/*.rs": {
"runtime": "vercel-rust@4.0.0-beta.1"
"runtime": "vercel-rust@4.0.0-beta.2"
}
}
}