astro/packages/integrations/cloudflare/test/test-utils.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

170 lines
3.6 KiB
JavaScript
Raw Normal View History

import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';
import * as net from 'node:net';
export { fixLineEndings } from '../../../astro/test/test-utils.js';
feat: hybrid output (#6991) * update config schema * adapt default route `prerender` value * adapt error message for hybrid output * core hybrid output support * add JSDocs for hybrid output * dev server hybrid output support * defer hybrid output check * update endpoint request warning * support `output=hybrid` in integrations * put constant variable out of for loop * revert: reapply back ssr plugin in ssr mode * change `prerender` option default * apply `prerender` by default in hybrid mode * simplfy conditional * update config schema * add `isHybridOutput` helper * more readable prerender condition * set default prerender value if no export is found * only add `pagesVirtualModuleId` ro rollup input in `output=static` * don't export vite plugin * remove unneeded check * don't prerender when it shouldn't * extract fallback `prerender` meta Extract the fallback `prerender` module meta out of the `scan` function. It shouldn't be its responsibility to handle that * pass missing argument to function * test: update cloudflare integration tests * test: update tests of vercel integration * test: update tests of node integration * test: update tests of netlify func integration * test: update tests of netlify edge integration * throw when `hybrid` mode is malconfigured * update node integraiton `output` warning * test(WIP): skip node prerendering tests for now * remove non-existant import * test: bring back prerendering tests * remove outdated comments * test: refactor test to support windows paths * remove outdated comments * apply sarah review Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * docs: `experiment.hybridOutput` jsodcs * test: prevent import from being cached * refactor: extract hybrid output check to function * add `hybrid` to output warning in adapter hooks * chore: changeset * add `.js` extension to import * chore: use spaces instead of tabs for gh formating * resolve merge conflict * chore: move test to another file for consitency --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Matthew Phillips <matthew@skypack.dev>
2023-05-17 13:23:20 +00:00
/**
* @typedef {{ stop: Promise<void>, port: number }} WranglerCLI
feat: hybrid output (#6991) * update config schema * adapt default route `prerender` value * adapt error message for hybrid output * core hybrid output support * add JSDocs for hybrid output * dev server hybrid output support * defer hybrid output check * update endpoint request warning * support `output=hybrid` in integrations * put constant variable out of for loop * revert: reapply back ssr plugin in ssr mode * change `prerender` option default * apply `prerender` by default in hybrid mode * simplfy conditional * update config schema * add `isHybridOutput` helper * more readable prerender condition * set default prerender value if no export is found * only add `pagesVirtualModuleId` ro rollup input in `output=static` * don't export vite plugin * remove unneeded check * don't prerender when it shouldn't * extract fallback `prerender` meta Extract the fallback `prerender` module meta out of the `scan` function. It shouldn't be its responsibility to handle that * pass missing argument to function * test: update cloudflare integration tests * test: update tests of vercel integration * test: update tests of node integration * test: update tests of netlify func integration * test: update tests of netlify edge integration * throw when `hybrid` mode is malconfigured * update node integraiton `output` warning * test(WIP): skip node prerendering tests for now * remove non-existant import * test: bring back prerendering tests * remove outdated comments * test: refactor test to support windows paths * remove outdated comments * apply sarah review Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * docs: `experiment.hybridOutput` jsodcs * test: prevent import from being cached * refactor: extract hybrid output check to function * add `hybrid` to output warning in adapter hooks * chore: changeset * add `.js` extension to import * chore: use spaces instead of tabs for gh formating * resolve merge conflict * chore: move test to another file for consitency --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Matthew Phillips <matthew@skypack.dev>
2023-05-17 13:23:20 +00:00
* @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
*/
export function loadFixture(config) {
if (config?.root) {
config.root = new URL(config.root, import.meta.url);
}
return baseLoadFixture(config);
}
2022-07-27 20:17:38 +00:00
const wranglerPath = fileURLToPath(
new URL('../node_modules/wrangler/bin/wrangler.js', import.meta.url)
);
let lastPort = 8788;
2023-07-17 12:30:02 +00:00
/**
* @returns {Promise<WranglerCLI>}
2023-07-17 12:30:02 +00:00
*/
export async function runCLI(
basePath,
{
silent,
maxAttempts = 3,
timeoutMillis = 2500, // really short because it often seems to just hang on the first try, but work subsequently, no matter the wait
backoffFactor = 2, // | - 2.5s -- 5s ---- 10s -> onTimeout
onTimeout = (ex) => {
new Error(`Timed out starting the wrangler CLI after ${maxAttempts} tries.`, { cause: ex });
},
}
) {
let triesRemaining = maxAttempts;
let timeout = timeoutMillis;
let cli;
let lastErr;
while (triesRemaining > 0) {
2023-09-22 15:00:18 +00:00
cli = await tryRunCLI(basePath, {
silent,
timeout,
forceRotatePort: triesRemaining !== maxAttempts,
});
try {
await cli.ready;
return cli;
} catch (err) {
lastErr = err;
console.error((err.message || err.name || err) + ' after ' + timeout + 'ms');
cli.stop();
triesRemaining -= 1;
timeout *= backoffFactor;
}
}
onTimeout(lastErr);
return cli;
}
async function tryRunCLI(basePath, { silent, timeout, forceRotatePort = false }) {
const port = await getNextOpenPort(lastPort + (forceRotatePort ? 1 : 0));
lastPort = port;
const fixtureDir = fileURLToPath(new URL(`${basePath}`, import.meta.url));
const p = spawn(
'node',
[
wranglerPath,
'pages',
'dev',
'dist',
'--port',
port,
'--log-level',
'info',
'--persist-to',
'.wrangler/state',
],
{
cwd: fixtureDir,
}
);
p.stderr.setEncoding('utf-8');
p.stdout.setEncoding('utf-8');
const ready = new Promise(async (resolve, reject) => {
2023-08-15 17:13:43 +00:00
const failed = setTimeout(() => {
p.kill('SIGKILL');
2023-08-15 17:13:43 +00:00
reject(new Error(`Timed out starting the wrangler CLI`));
}, timeout);
const success = () => {
clearTimeout(failed);
resolve();
};
p.on('exit', (code) => reject(`wrangler terminated unexpectedly with exit code ${code}`));
p.stderr.on('data', (data) => {
2022-07-27 20:17:38 +00:00
if (!silent) {
process.stdout.write(data);
}
});
let allData = '';
p.stdout.on('data', (data) => {
if (!silent) {
process.stdout.write(data);
}
allData += data;
if (allData.includes(`[mf:inf] Ready on`)) {
success();
}
});
});
return {
port,
ready,
stop() {
2023-06-21 13:09:49 +00:00
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
p.kill('SIGKILL');
}, 1000);
p.on('close', () => {
clearTimeout(timer);
resolve();
});
2023-06-21 13:09:49 +00:00
p.on('error', (err) => reject(err));
p.kill();
});
2022-07-27 20:17:38 +00:00
},
};
}
const isPortOpen = async (port) => {
return new Promise((resolve, reject) => {
let s = net.createServer();
s.once('error', (err) => {
s.close();
if (err['code'] == 'EADDRINUSE') {
resolve(false);
} else {
reject(err);
}
});
s.once('listening', () => {
resolve(true);
s.close();
});
2023-09-22 15:00:18 +00:00
s.listen(port, '0.0.0.0');
});
};
const getNextOpenPort = async (startFrom) => {
let openPort = null;
while (startFrom < 65535 || !!openPort) {
if (await isPortOpen(startFrom)) {
openPort = startFrom;
break;
}
startFrom++;
}
return openPort;
};