-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.ts
188 lines (162 loc) · 5.22 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import path from 'node:path';
import { existsSync, readFileSync } from 'node:fs';
import {
FileFsRef,
debug,
download,
glob,
Lambda,
type BuildOptions,
type BuildResultV2Typical,
getLambdaOptionsFromFunction,
getProvidedRuntime,
} from '@vercel/build-utils';
import execa from 'execa';
import { installRustToolchain } from './lib/rust-toolchain';
import type { Runtime } from './lib/runtime';
import {
getCargoMetadata,
findBinaryName,
findCargoWorkspace,
findCargoBuildConfiguration,
} from './lib/cargo';
import {
assertEnv,
getExecutableName,
gatherExtraFiles,
runUserScripts,
} from './lib/utils';
import { generateRoutes, parseRoute } from './lib/routes';
type RustEnv = Record<'RUSTFLAGS' | 'PATH', string>;
async function buildHandler(
options: BuildOptions,
): Promise<BuildResultV2Typical> {
const BUILDER_DEBUG = Boolean(process.env.VERCEL_BUILDER_DEBUG ?? false);
const { files, entrypoint, workPath, config, meta } = options;
await installRustToolchain();
debug('Creating file system');
const downloadedFiles = await download(files, workPath, meta);
const entryPath = downloadedFiles[entrypoint].fsPath;
const HOME =
process.platform === 'win32' ? assertEnv('USERPROFILE') : assertEnv('HOME');
const PATH = assertEnv('PATH');
const rustEnv: RustEnv = {
PATH: `${path.join(HOME, '.cargo/bin')}:${PATH}`,
RUSTFLAGS: [process.env.RUSTFLAGS].filter(Boolean).join(' '),
};
const cargoWorkspace = await findCargoWorkspace({
env: rustEnv,
cwd: path.dirname(entryPath),
});
const binaryName = findBinaryName(cargoWorkspace, entryPath);
const cargoBuildConfiguration = await findCargoBuildConfiguration(
cargoWorkspace,
);
const buildTarget = cargoBuildConfiguration?.build.target ?? '';
await runUserScripts(workPath);
const extraFiles = await gatherExtraFiles(config.includeFiles, workPath);
debug(`Running \`cargo build\` for \`${binaryName}\``);
try {
await execa(
'cargo',
['build', '--bin', binaryName].concat(
BUILDER_DEBUG ? ['--verbose'] : ['--quiet'], meta?.isDev ? []:["--release"]
),
{
cwd: workPath,
env: rustEnv,
stdio: 'inherit',
},
);
} catch (err) {
debug(`Running \`cargo build\` for \`${binaryName}\` failed`);
throw err;
}
debug(`Building \`${binaryName}\` for \`${process.platform}\` completed`);
let { target_directory: targetDirectory } = await getCargoMetadata({
cwd: workPath,
env: rustEnv,
});
targetDirectory = path.join(targetDirectory, buildTarget);
const bin = path.join(
targetDirectory,
BUILDER_DEBUG ? 'debug' : 'release',
getExecutableName(binaryName),
);
const lambdaOptions = await getLambdaOptionsFromFunction({
sourceFile: entrypoint,
config,
});
const bootstrap = getExecutableName('bootstrap');
const runtime = meta?.isDev ? 'provided' : await getProvidedRuntime();
const lambda = new Lambda({
files: {
...extraFiles,
[bootstrap]: new FileFsRef({ mode: 0o755, fsPath: bin }),
},
handler: bootstrap,
runtime,
...lambdaOptions,
});
lambda.zipBuffer = await lambda.createZip();
if (isBundledRoute()) {
debug(
`experimental \`route-bundling\` detected - generating single entrypoint`,
);
const handlerFiles = await glob('api/**/[!main]*.rs', workPath);
const routes = generateRoutes(Object.keys(handlerFiles));
return {
output: routes.reduce<Record<string, Lambda>>((acc, route) => {
acc[route.path] = lambda;
return acc;
}, {}),
routes: routes.map(({ src, dest }) => ({ src, dest })),
};
}
debug(`generating lambda for \`${entrypoint}\``);
const route = parseRoute(entrypoint);
return {
output: {
[route.path]: lambda,
},
routes: [{ src: route.src, dest: route.dest }],
};
}
function isBundledRoute(): boolean {
if (existsSync('api/main.rs')) {
const content = readFileSync('api/main.rs', 'utf8');
return content.includes('bundled_api]') || content.includes('bundled_api(');
}
return false;
}
// Reference - https://github.com/vercel/vercel/blob/main/DEVELOPING_A_RUNTIME.md#runtime-developer-reference
const runtime: Runtime = {
version: 2,
build: buildHandler,
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 =
/(?:^|\/)target\/release\/\.fingerprint\//.test(f) ||
/(?:^|\/)target\/release\/build\//.test(f) ||
/(?:^|\/)target\/release\/deps\//.test(f) ||
/(?:^|\/)target\/debug\/\.fingerprint\//.test(f) ||
/(?:^|\/)target\/debug\/build\//.test(f) ||
/(?:^|\/)target\/debug\/deps\//.test(f);
if (!accept) {
delete cacheFiles[f];
}
}
return cacheFiles;
},
shouldServe: async (options): Promise<boolean> => {
debug(`Requested ${options.requestPath} for ${options.entrypoint}`);
if (isBundledRoute()) {
return Promise.resolve(options.entrypoint === 'api/main');
}
return Promise.resolve(options.requestPath === options.entrypoint);
},
};
export const { version, build, prepareCache, shouldServe } = runtime;