[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

@@ -6,30 +6,252 @@ const glob = require('util').promisify(require('glob'));
const path = require('path');
const { spawn } = require('child_process');
const fetch = require('./fetch-retry.js');
const { nowDeploy, fileModeSymbol } = require('./now-deploy.js');
const { nowDeploy, fileModeSymbol, fetchWithAuth } = require('./now-deploy.js');
const { logWithinTest } = require('./log');
async function packAndDeploy(builderPath) {
async function packAndDeploy(builderPath, shouldUnlink = true) {
await spawnAsync('npm', ['--loglevel', 'warn', 'pack'], {
stdio: 'inherit',
cwd: builderPath,
});
const tarballs = await glob('*.tgz', { cwd: builderPath });
const tgzPath = path.join(builderPath, tarballs[0]);
console.log('tgzPath', tgzPath);
logWithinTest('tgzPath', tgzPath);
const url = await nowDeployIndexTgz(tgzPath);
await fetchTgzUrl(`https://${url}`);
fs.unlinkSync(tgzPath);
logWithinTest('finished calling the tgz');
if (shouldUnlink) {
fs.unlinkSync(tgzPath);
logWithinTest('finished unlinking tgz');
} else {
logWithinTest('leaving tgz in place');
}
return url;
}
const RANDOMNESS_PLACEHOLDER_STRING = 'RANDOMNESS_PLACEHOLDER';
async function runProbe(probe, deploymentId, deploymentUrl, ctx) {
if (probe.delay) {
await new Promise(resolve => setTimeout(resolve, probe.delay));
return;
}
if (probe.logMustContain || probe.logMustNotContain) {
const shouldContain = !!probe.logMustContain;
const toCheck = probe.logMustContain || probe.logMustNotContain;
if (probe.logMustContain && probe.logMustNotContain) {
throw new Error(
`probe can not check logMustContain and logMustNotContain in the same check`
);
}
if (!ctx.deploymentLogs) {
let lastErr;
for (let i = 0; i < 5; i++) {
try {
const logsRes = await fetchWithAuth(
`/v1/now/deployments/${deploymentId}/events?limit=-1`
);
if (!logsRes.ok) {
throw new Error(
`fetching logs failed with status ${logsRes.status}`
);
}
ctx.deploymentLogs = await logsRes.json();
if (
Array.isArray(ctx.deploymentLogs) &&
ctx.deploymentLogs.length > 2
) {
break;
}
} catch (err) {
lastErr = err;
}
ctx.deploymentLogs = null;
logWithinTest(
'Retrying to fetch logs for',
deploymentId,
'in 2 seconds. Read lines:',
Array.isArray(ctx.deploymentLogs)
? ctx.deploymentLogs.length
: typeof ctx.deploymentLogs
);
await new Promise(resolve => setTimeout(resolve, 2000));
}
if (
!Array.isArray(ctx.deploymentLogs) ||
ctx.deploymentLogs.length === 0
) {
throw new Error(
`Failed to get deployment logs for probe: ${
lastErr ? lastErr.message : 'received empty logs'
}`
);
}
}
let found = false;
const deploymentLogs = ctx.deploymentLogs;
for (const log of deploymentLogs) {
if (log.text && log.text.includes(toCheck)) {
if (shouldContain) {
found = true;
break;
} else {
throw new Error(
`Expected deployment logs of ${deploymentId} not to contain ${toCheck}, but found ${log.text}`
);
}
}
}
if (!found && shouldContain) {
logWithinTest({
deploymentId,
deploymentUrl,
deploymentLogs,
logLength: deploymentLogs.length,
});
throw new Error(
`Expected deployment logs of ${deploymentId} to contain ${toCheck}, it was not found`
);
} else {
logWithinTest('finished testing', JSON.stringify(probe));
return;
}
}
const nextScriptIndex = probe.path.indexOf('__NEXT_SCRIPT__(');
if (nextScriptIndex > -1) {
const scriptNameEnd = probe.path.lastIndexOf(')');
let scriptName = probe.path.substring(
nextScriptIndex + '__NEXT_SCRIPT__('.length,
scriptNameEnd
);
const scriptArgs = scriptName.split(',');
scriptName = scriptArgs.shift();
const manifestPrefix = scriptArgs.shift() || '';
if (!ctx.nextBuildManifest) {
const manifestUrl = `https://${deploymentUrl}${manifestPrefix}/_next/static/testing-build-id/_buildManifest.js`;
logWithinTest('fetching buildManifest at', manifestUrl);
const { text: manifestContent } = await fetchDeploymentUrl(manifestUrl);
// we must eval it since we use devalue to stringify it
global.__BUILD_MANIFEST_CB = null;
ctx.nextBuildManifest = eval(
`self = {};` + manifestContent + `;self.__BUILD_MANIFEST`
);
}
let scriptRelativePath = ctx.nextBuildManifest[scriptName];
if (Array.isArray(scriptRelativePath)) {
scriptRelativePath = scriptRelativePath[0];
}
probe.path =
probe.path.substring(0, nextScriptIndex) +
scriptRelativePath +
probe.path.substring(scriptNameEnd + 1);
}
const probeUrl = `https://${deploymentUrl}${probe.path}`;
const fetchOpts = {
...probe.fetchOptions,
method: probe.method,
headers: { ...probe.headers },
};
if (probe.body) {
fetchOpts.headers['content-type'] = 'application/json';
fetchOpts.body = JSON.stringify(probe.body);
}
const { text, resp } = await fetchDeploymentUrl(probeUrl, fetchOpts);
logWithinTest('finished testing', JSON.stringify(probe));
if (probe.status) {
if (probe.status !== resp.status) {
throw new Error(
`Fetched page ${probeUrl} does not return the status ${probe.status} Instead it has ${resp.status}`
);
}
}
if (probe.mustContain || probe.mustNotContain) {
const shouldContain = !!probe.mustContain;
const containsIt = text.includes(probe.mustContain);
if (
(!containsIt && probe.mustContain) ||
(containsIt && probe.mustNotContain)
) {
fs.writeFileSync(path.join(__dirname, 'failed-page.txt'), text);
const headers = Array.from(resp.headers.entries())
.map(([k, v]) => ` ${k}=${v}`)
.join('\n');
throw new Error(
`Fetched page ${probeUrl} does${shouldContain ? ' not' : ''} contain ${
shouldContain ? probe.mustContain : probe.mustNotContain
}.` +
(shouldContain ? ` Instead it contains ${text.slice(0, 60)}` : '') +
` Response headers:\n ${headers}`
);
}
} else if (probe.responseHeaders) {
// eslint-disable-next-line no-loop-func
Object.keys(probe.responseHeaders).forEach(header => {
const actual = resp.headers.get(header);
const expected = probe.responseHeaders[header];
const isEqual = Array.isArray(expected)
? expected.every(h => actual.includes(h))
: typeof expected === 'string' &&
expected.startsWith('/') &&
expected.endsWith('/')
? new RegExp(expected.slice(1, -1)).test(actual)
: expected === actual;
if (!isEqual) {
const headers = Array.from(resp.headers.entries())
.map(([k, v]) => ` ${k}=${v}`)
.join('\n');
throw new Error(
`Page ${probeUrl} does not have header ${header}.\n\nExpected: ${expected}.\nActual: ${headers}`
);
}
});
} else if (probe.notResponseHeaders) {
Object.keys(probe.notResponseHeaders).forEach(header => {
const headerValue = resp.headers.get(header);
const expected = probe.notResponseHeaders[header];
if (headerValue === expected) {
const headers = Array.from(resp.headers.entries())
.map(([k, v]) => ` ${k}=${v}`)
.join('\n');
throw new Error(
`Page ${probeUrl} invalid page header ${header}.\n\n Did not expect: ${header}=${expected}.\nBut got ${headers}`
);
}
});
} else if (!probe.status) {
assert(false, 'probe must have a test condition');
}
}
async function testDeployment(
{ builderUrl, buildUtilsUrl },
fixturePath,
buildDelegate
) {
console.log('testDeployment', fixturePath);
logWithinTest('testDeployment', fixturePath);
const globResult = await glob(`${fixturePath}/**`, {
nodir: true,
dot: true,
@@ -61,15 +283,17 @@ async function testDeployment(
const uploadNowJson = nowJson.uploadNowJson;
delete nowJson.uploadNowJson;
if (process.env.VERCEL_BUILDER_DEBUG) {
if (!nowJson.build) {
nowJson.build = {};
['VERCEL_BUILDER_DEBUG', 'VERCEL_BUILD_CLI_PACKAGE'].forEach(name => {
if (process.env[name]) {
if (!nowJson.build) {
nowJson.build = {};
}
if (!nowJson.build.env) {
nowJson.build.env = {};
}
nowJson.build.env[name] = process.env[name];
}
if (!nowJson.build.env) {
nowJson.build.env = {};
}
nowJson.build.env.VERCEL_BUILDER_DEBUG = process.env.VERCEL_BUILDER_DEBUG;
}
});
for (const build of nowJson.builds || []) {
if (builderUrl) {
@@ -103,186 +327,36 @@ async function testDeployment(
randomness,
uploadNowJson
);
let nextBuildManifest;
let deploymentLogs;
const probeCtx = {};
for (const probe of nowJson.probes || []) {
console.log('testing', JSON.stringify(probe));
if (probe.delay) {
await new Promise(resolve => setTimeout(resolve, probe.delay));
continue;
}
const stringifiedProbe = JSON.stringify(probe);
logWithinTest('testing', stringifiedProbe);
if (probe.logMustContain || probe.logMustNotContain) {
const shouldContain = !!probe.logMustContain;
const toCheck = probe.logMustContain || probe.logMustNotContain;
if (probe.logMustContain && probe.logMustNotContain) {
throw new Error(
`probe can not check logMustContain and logMustNotContain in the same check`
);
try {
await runProbe(probe, deploymentId, deploymentUrl, probeCtx);
} catch (err) {
if (!probe.retries) {
throw err;
}
if (!deploymentLogs) {
for (let i = 0; i < probe.retries; i++) {
logWithinTest(`re-trying ${i + 1}/${probe.retries}:`, stringifiedProbe);
try {
const logsRes = await fetch(
`https://vercel.com/api/v1/now/deployments/${deploymentId}/events?limit=-1`
);
if (!logsRes.ok) {
throw new Error(
`fetching logs failed with status ${logsRes.status}`
);
}
deploymentLogs = await logsRes.json();
await runProbe(probe, deploymentId, deploymentUrl, probeCtx);
break;
} catch (err) {
throw new Error(
`Failed to get deployment logs for probe: ${err.message}`
);
}
}
if (i === probe.retries - 1) {
throw err;
}
let found = false;
for (const log of deploymentLogs) {
if (log.text && log.text.includes(toCheck)) {
if (shouldContain) {
found = true;
break;
} else {
throw new Error(
`Expected deployment logs not to contain ${toCheck}, but found ${log.text}`
);
if (probe.retryDelay) {
logWithinTest(`Waiting ${probe.retryDelay}ms before retrying`);
await new Promise(resolve => setTimeout(resolve, probe.retryDelay));
}
}
}
if (!found && shouldContain) {
throw new Error(
`Expected deployment logs to contain ${toCheck}, it was not found`
);
} else {
console.log('finished testing', JSON.stringify(probe));
continue;
}
}
const nextScriptIndex = probe.path.indexOf('__NEXT_SCRIPT__(');
if (nextScriptIndex > -1) {
const scriptNameEnd = probe.path.lastIndexOf(')');
let scriptName = probe.path.substring(
nextScriptIndex + '__NEXT_SCRIPT__('.length,
scriptNameEnd
);
const scriptArgs = scriptName.split(',');
scriptName = scriptArgs.shift();
const manifestPrefix = scriptArgs.shift() || '';
if (!nextBuildManifest) {
const manifestUrl = `https://${deploymentUrl}${manifestPrefix}/_next/static/testing-build-id/_buildManifest.js`;
console.log('fetching buildManifest at', manifestUrl);
const { text: manifestContent } = await fetchDeploymentUrl(manifestUrl);
// we must eval it since we use devalue to stringify it
global.__BUILD_MANIFEST_CB = null;
nextBuildManifest = eval(
manifestContent
.replace('self.__BUILD_MANIFEST', 'manifest')
.replace(/self.__BUILD_MANIFEST_CB.*/, '')
);
}
const scriptRelativePath = nextBuildManifest[scriptName];
probe.path =
probe.path.substring(0, nextScriptIndex) +
scriptRelativePath +
probe.path.slice(scriptNameEnd + 1);
}
const probeUrl = `https://${deploymentUrl}${probe.path}`;
const fetchOpts = {
...probe.fetchOptions,
method: probe.method,
headers: { ...probe.headers },
};
if (probe.body) {
fetchOpts.headers['content-type'] = 'application/json';
fetchOpts.body = JSON.stringify(probe.body);
}
const { text, resp } = await fetchDeploymentUrl(probeUrl, fetchOpts);
console.log('finished testing', JSON.stringify(probe));
if (probe.status) {
if (probe.status !== resp.status) {
throw new Error(
`Fetched page ${probeUrl} does not return the status ${probe.status} Instead it has ${resp.status}`
);
}
}
if (probe.mustContain || probe.mustNotContain) {
const shouldContain = !!probe.mustContain;
const containsIt = text.includes(probe.mustContain);
if (
(!containsIt && probe.mustContain) ||
(containsIt && probe.mustNotContain)
) {
fs.writeFileSync(path.join(__dirname, 'failed-page.txt'), text);
const headers = Array.from(resp.headers.entries())
.map(([k, v]) => ` ${k}=${v}`)
.join('\n');
throw new Error(
`Fetched page ${probeUrl} does${
shouldContain ? ' not' : ''
} contain ${
shouldContain ? probe.mustContain : probe.mustNotContain
}.` +
(shouldContain ? ` Instead it contains ${text.slice(0, 60)}` : '') +
` Response headers:\n ${headers}`
);
}
} else if (probe.responseHeaders) {
// eslint-disable-next-line no-loop-func
Object.keys(probe.responseHeaders).forEach(header => {
const actual = resp.headers.get(header);
const expected = probe.responseHeaders[header];
const isEqual = Array.isArray(expected)
? expected.every(h => actual.includes(h))
: typeof expected === 'string' &&
expected.startsWith('/') &&
expected.endsWith('/')
? new RegExp(expected.slice(1, -1)).test(actual)
: expected === actual;
if (!isEqual) {
const headers = Array.from(resp.headers.entries())
.map(([k, v]) => ` ${k}=${v}`)
.join('\n');
throw new Error(
`Page ${probeUrl} does not have header ${header}.\n\nExpected: ${expected}.\nActual: ${headers}`
);
}
});
} else if (probe.notResponseHeaders) {
Object.keys(probe.notResponseHeaders).forEach(header => {
const headerValue = resp.headers.get(header);
const expected = probe.notResponseHeaders[header];
if (headerValue === expected) {
const headers = Array.from(resp.headers.entries())
.map(([k, v]) => ` ${k}=${v}`)
.join('\n');
throw new Error(
`Page ${probeUrl} invalid page header ${header}.\n\n Did not expect: ${header}=${expected}.\nBut got ${headers}`
);
}
});
} else if (!probe.status) {
assert(false, 'probe must have a test condition');
}
}
@@ -348,7 +422,7 @@ async function spawnAsync(...args) {
child.on('error', reject);
child.on('close', (code, signal) => {
if (code !== 0) {
if (result) console.log(result);
if (result) logWithinTest(result);
reject(new Error(`Exited with ${code || signal}`));
return;
}