mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-07 21:07:46 +00:00
* Move now-cli to /packages/now-cli * Fix .gitignore paths * Add now-client * Add lerna to top level * Add scripts * Update codeowners * Fix `/now-cli/build.ts` script * Fix circleci path to artifacts * Use relative paths * Fix path to scripts * Add test-lint script * Add missing return type * Fix typo in test-lint * Fix string match in shell scripts * Fix path to hugo * Add package node_modules * Delete lock files in packages, use root yarn.lock * Add missing b.js file * Add test-integration-now-dev script * Add missing test files * Add missing integration test script * Add missing test files * Delete travis.yml * Fix ts-jest in now-client * Add support for Node 8 (ES2015 target) * Add support for Node 8 * Add polyfill for Node 8 * Fix polyfill for Node 8 * Only run coverage for now-cli * Add packages from now-builders * Run integration tests for builders * Add node_modules to cache * Add root readme.md * Move readme to top level * Add yarn bootstrap * Add bootstrap step * Add dist to `persist_to_workspace` * Fix 08-yarn-npm integration test * Remove duplicate path * Change stdio to inherit * Add back store_artifacts * testing - remove bootstrap step * Add back now-build-utils * Remove bootstrap step * Fix test again * Add console.log() * Fix lint * Use local ncc version * Install go * Revert changes to stdio and console.log() * Add missing now-go test * Add missing integration tests * Add --runInBand flag * Fix now-node-bridge persistence * Add missing symlinks * Add codeowners * Consolidate into single run.sh function * Run uniq * Fix typo * Change now-routing-utils to test-unit * Special case test for node 8 * Add docs from builders * Only run script for modified packages * Add test-integration-once which only runs once * Fix set intersection
210 lines
6.1 KiB
JavaScript
210 lines
6.1 KiB
JavaScript
const assert = require('assert');
|
|
const bufferReplace = require('buffer-replace');
|
|
const fs = require('fs');
|
|
const glob = require('util').promisify(require('glob'));
|
|
const path = require('path');
|
|
const { spawn } = require('child_process');
|
|
const fetch = require('./fetch-retry.js');
|
|
const { nowDeploy } = require('./now-deploy.js');
|
|
|
|
async function packAndDeploy (builderPath) {
|
|
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);
|
|
const url = await nowDeployIndexTgz(tgzPath);
|
|
await fetchTgzUrl(`https://${url}`);
|
|
fs.unlinkSync(tgzPath);
|
|
return url;
|
|
}
|
|
|
|
const RANDOMNESS_PLACEHOLDER_STRING = 'RANDOMNESS_PLACEHOLDER';
|
|
|
|
async function testDeployment (
|
|
{ builderUrl, buildUtilsUrl },
|
|
fixturePath,
|
|
buildDelegate
|
|
) {
|
|
console.log('testDeployment', fixturePath);
|
|
const globResult = await glob(`${fixturePath}/**`, { nodir: true });
|
|
const bodies = globResult.reduce((b, f) => {
|
|
const r = path.relative(fixturePath, f);
|
|
b[r] = fs.readFileSync(f);
|
|
return b;
|
|
}, {});
|
|
|
|
const randomness = Math.floor(Math.random() * 0x7fffffff)
|
|
.toString(16)
|
|
.repeat(6)
|
|
.slice(0, RANDOMNESS_PLACEHOLDER_STRING.length);
|
|
|
|
for (const file of Object.keys(bodies)) {
|
|
bodies[file] = bufferReplace(
|
|
bodies[file],
|
|
RANDOMNESS_PLACEHOLDER_STRING,
|
|
randomness
|
|
);
|
|
}
|
|
|
|
const nowJson = JSON.parse(bodies['now.json']);
|
|
for (const build of nowJson.builds) {
|
|
if (builderUrl) {
|
|
if (builderUrl === '@canary') {
|
|
build.use = `${build.use}@canary`;
|
|
} else {
|
|
build.use = `https://${builderUrl}`;
|
|
}
|
|
}
|
|
if (buildUtilsUrl) {
|
|
build.config = build.config || {};
|
|
const { config } = build;
|
|
if (buildUtilsUrl === '@canary') {
|
|
config.useBuildUtils = config.useBuildUtils || '@now/build-utils';
|
|
config.useBuildUtils = `${config.useBuildUtils}@canary`;
|
|
} else {
|
|
config.useBuildUtils = `https://${buildUtilsUrl}`;
|
|
}
|
|
}
|
|
|
|
if (buildDelegate) {
|
|
buildDelegate(build);
|
|
}
|
|
}
|
|
|
|
bodies['now.json'] = Buffer.from(JSON.stringify(nowJson));
|
|
delete bodies['probe.js'];
|
|
const { deploymentId, deploymentUrl } = await nowDeploy(bodies, randomness);
|
|
console.log('deploymentUrl', `https://${deploymentUrl}`);
|
|
|
|
for (const probe of nowJson.probes || []) {
|
|
console.log('testing', JSON.stringify(probe));
|
|
const probeUrl = `https://${deploymentUrl}${probe.path}`;
|
|
const fetchOpts = { 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) {
|
|
if (!text.includes(probe.mustContain)) {
|
|
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 not contain ${probe.mustContain}.`
|
|
+ ` 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) => {
|
|
if (resp.headers.get(header) !== probe.responseHeaders[header]) {
|
|
const headers = Array.from(resp.headers.entries())
|
|
.map(([ k, v ]) => ` ${k}=${v}`)
|
|
.join('\n');
|
|
|
|
throw new Error(
|
|
`Fetched page ${probeUrl} does not contain header ${header}: \`${
|
|
probe.responseHeaders[header]
|
|
}\`.\n\nResponse headers:\n ${headers}`
|
|
);
|
|
}
|
|
});
|
|
} else if (!probe.status) {
|
|
assert(false, 'probe must have a test condition');
|
|
}
|
|
}
|
|
|
|
const probeJsFullPath = path.resolve(fixturePath, 'probe.js');
|
|
if (fs.existsSync(probeJsFullPath)) {
|
|
await require(probeJsFullPath)({ deploymentUrl, fetch, randomness });
|
|
}
|
|
|
|
return { deploymentId, deploymentUrl };
|
|
}
|
|
|
|
async function nowDeployIndexTgz (file) {
|
|
const bodies = {
|
|
'index.tgz': fs.readFileSync(file),
|
|
'now.json': Buffer.from(JSON.stringify({ version: 2 })),
|
|
};
|
|
|
|
return (await nowDeploy(bodies)).deploymentUrl;
|
|
}
|
|
|
|
async function fetchDeploymentUrl (url, opts) {
|
|
for (let i = 0; i < 50; i += 1) {
|
|
const resp = await fetch(url, opts);
|
|
const text = await resp.text();
|
|
if (text && !text.includes('Join Free')) {
|
|
return { resp, text };
|
|
}
|
|
|
|
await new Promise((r) => setTimeout(r, 1000));
|
|
}
|
|
|
|
throw new Error(`Failed to wait for deployment READY. Url is ${url}`);
|
|
}
|
|
|
|
async function fetchTgzUrl (url) {
|
|
for (let i = 0; i < 500; i += 1) {
|
|
const resp = await fetch(url);
|
|
if (resp.status === 200) {
|
|
const buffer = await resp.buffer();
|
|
if (buffer[0] === 0x1f) {
|
|
// tgz beginning
|
|
return;
|
|
}
|
|
}
|
|
|
|
await new Promise((r) => setTimeout(r, 1000));
|
|
}
|
|
|
|
throw new Error(`Failed to wait for builder url READY. Url is ${url}`);
|
|
}
|
|
|
|
async function spawnAsync (...args) {
|
|
return await new Promise((resolve, reject) => {
|
|
const child = spawn(...args);
|
|
let result;
|
|
if (child.stdout) {
|
|
result = '';
|
|
child.stdout.on('data', (chunk) => {
|
|
result += chunk.toString();
|
|
});
|
|
}
|
|
|
|
child.on('error', reject);
|
|
child.on('close', (code, signal) => {
|
|
if (code !== 0) {
|
|
if (result) console.log(result);
|
|
reject(new Error(`Exited with ${code || signal}`));
|
|
return;
|
|
}
|
|
resolve(result);
|
|
});
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
packAndDeploy,
|
|
testDeployment,
|
|
};
|