[next] Add @vercel/next Builder (#7793)

This commit is contained in:
Nathan Rajlich
2022-05-18 16:51:48 -07:00
committed by GitHub
parent 3e696513a2
commit 623e43f865
1046 changed files with 62143 additions and 238 deletions

View File

@@ -0,0 +1,93 @@
/// <reference lib="es2019" />
process.env.NEXT_TELEMETRY_DISABLED = '1';
import type { Context } from '../types';
import { duplicateWithConfig } from '../utils';
import fs from 'fs-extra';
import path from 'path';
import runBuildLambda from '../../../../test/lib/run-build-lambda';
import { EdgeFunction, Files, streamToBuffer } from '@vercel/build-utils';
import { createHash } from 'crypto';
const SIMPLE_PROJECT = path.resolve(__dirname, 'middleware');
describe('Middleware simple project', () => {
const ctx: Context = {};
beforeAll(async () => {
const result = await runBuildLambda(SIMPLE_PROJECT);
ctx.buildResult = result.buildResult;
});
it('generates deterministic code', async () => {
const result = await runBuildLambda(SIMPLE_PROJECT);
const output = Object.entries(result.buildResult.output).filter(
(pair): pair is [string, EdgeFunction] => {
return pair[1].type === 'EdgeFunction';
}
);
expect(output.length).toBeGreaterThanOrEqual(1);
for (const [key, ef1] of output) {
const ef2 = result.buildResult.output[key];
if (ef2.type !== 'EdgeFunction') {
return fail(`${key} is not an EdgeFunction`);
}
const [hash1, hash2] = await Promise.all([
hashAllFiles(ef1.files),
hashAllFiles(ef2.files),
]);
expect(hash1).toEqual(hash2);
}
});
sharedTests(ctx);
});
describe('Middleware with basePath', () => {
let projectPath: string;
const context: Context = {
basePath: '/root',
};
beforeAll(async () => {
projectPath = await duplicateWithConfig({
context: context,
path: SIMPLE_PROJECT,
suffix: 'basepath',
});
const result = await runBuildLambda(projectPath);
context.buildResult = result.buildResult;
});
afterAll(async () => {
await fs.remove(projectPath);
});
sharedTests(context);
});
function sharedTests(ctx: Context) {
it('worker uses `middleware` or `middlewarePath` keyword as route path', async () => {
const routes = ctx.buildResult.routes.filter(
route => 'middleware' in route || 'middlewarePath' in route
);
expect(routes.length).toBeGreaterThan(0);
});
}
async function hashAllFiles(files: Files): Promise<string> {
const hash = createHash('sha1');
for (const [pathname, file] of Object.entries(files)) {
hash.update(`pathname:${pathname}`);
const buffer = await streamToBuffer(file.toStream());
hash.update(buffer);
}
return hash.digest('hex');
}