Skip to content
Closed
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
21 changes: 18 additions & 3 deletions packages/void/src/cli/cf-build-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,24 @@ export async function prepareCfBuildOutput(
if (!config.worker) {
throw new Error('cf did not produce a Worker configuration.');
}
const main = raw.main;
if (typeof main !== 'string' || basename(main) !== main || !existsSync(join(source, main))) {
return null;
if (typeof raw.main !== 'string' || !raw.main) {
throw new Error('The built Worker configuration is missing its main entry. Rebuild the app.');
}
const entry = raw.main.replaceAll('\\', '/');
const main = relative(source, resolve(source, entry));
if (
isAbsolute(entry) ||
/^[a-z]:/i.test(entry) ||
!main ||
main === '..' ||
main.startsWith('../')
) {
throw new Error('The built Worker main entry must be inside its output directory.');
}
if (!existsSync(join(source, main))) {
throw new Error(
`The built Worker main entry ${JSON.stringify(main)} is missing. Rebuild the app.`,
);
}

const assets = object(raw.assets);
Expand Down
61 changes: 61 additions & 0 deletions packages/void/test/unit/cf-build-output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,67 @@ it(
CF_BUILD_TIMEOUT_MS,
);

it(
'accepts a nested hashed Worker entry with a relative module import',
async () => {
const output = worker({
name: 'void-test',
compatibility_date: '2026-09-29',
main: './assets/index-hash.js',
});
mkdirSync(join(output, 'assets'));
writeFileSync(
join(output, 'assets/index-hash.js'),
"import { message } from './message.js'; export default { fetch() { return new Response(message) } };\n",
);
writeFileSync(join(output, 'assets/message.js'), 'export const message = "nested entry";\n');
rmSync(join(output, 'index.js'));
const prepared = await prepareCfBuildOutput(output, 'account-id');
expect(prepared).not.toBeNull();
if (!prepared) {
return;
}
try {
const built = await readBuildOutput(prepared.directory);
expect(built.workers.default.config.manifest).toMatchObject({
mainModule: 'assets/index-hash.js',
modules: { 'assets/message.js': { type: 'esm' } },
});
const cf = join(dirname(fileURLToPath(import.meta.resolve('cf/package.json'))), 'bin', 'cf');
const dryRun = spawnSync(
process.execPath,
[cf, 'workers', 'versions', 'create', '--prebuilt', '--dry-run'],
{ cwd: prepared.directory, encoding: 'utf8' },
);
expect(dryRun.status, `${dryRun.stdout}\n${dryRun.stderr}`).toBe(0);
} finally {
prepared.dispose();
}
},
CF_BUILD_TIMEOUT_MS,
);

it.each(['../index.js', '/index.js', 'C:\\index.js'])(
'rejects an out-of-bundle entry %s',
async (main) => {
const output = worker({ name: 'void-test', compatibility_date: '2026-09-29', main });
await expect(prepareCfBuildOutput(output, 'account-id')).rejects.toThrow(
'inside its output directory',
);
},
);

it('reports a missing built entry', async () => {
const output = worker({
name: 'void-test',
compatibility_date: '2026-09-29',
main: 'assets/missing.js',
});
await expect(prepareCfBuildOutput(output, 'account-id')).rejects.toThrow(
'"assets/missing.js" is missing',
);
});

it('preserves worker settings that the previous bridge dropped', async () => {
const output = worker({
name: 'void-test',
Expand Down
Loading