0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
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.

91 lines
1.9 KiB
JavaScript
Raw Normal View History

import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import kill from 'kill-port';
import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';
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 08:23:20 -05:00
/**
2023-07-17 07:30:02 -05:00
* @typedef {{ ready: Promise<void>, stop: Promise<void> }} 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 08:23:20 -05: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 15:17:38 -05:00
const wranglerPath = fileURLToPath(
new URL('../node_modules/wrangler/bin/wrangler.js', import.meta.url)
);
2023-07-17 07:30:02 -05:00
/**
* @returns {Promise<WranglerCLI>}
2023-07-17 07:30:02 -05:00
*/
export async function runCLI(basePath, { silent, port }) {
// Hack: force existing process on port to be killed
try {
await kill(port, 'tcp');
} catch {
// Will throw if port is not in use, but that's fine
}
const script = fileURLToPath(new URL(`${basePath}/dist/_worker.js`, import.meta.url));
2023-08-15 12:13:43 -05:00
const p = spawn('node', [
wranglerPath,
'dev',
script,
'--port',
port,
'--log-level',
'info',
'--persist-to',
`${basePath}/.wrangler/state`,
]);
p.stderr.setEncoding('utf-8');
p.stdout.setEncoding('utf-8');
const timeout = 20_000;
const ready = new Promise(async (resolve, reject) => {
2023-08-15 12:13:43 -05:00
const failed = setTimeout(() => {
p.kill();
reject(new Error(`Timed out starting the wrangler CLI`));
}, timeout);
(async function () {
2022-07-27 15:17:38 -05:00
for (const msg of p.stderr) {
if (!silent) {
console.error(msg);
}
}
})();
2022-07-27 15:17:38 -05:00
for await (const msg of p.stdout) {
if (!silent) {
console.log(msg);
}
if (msg.includes(`[mf:inf] Ready on`)) {
break;
}
}
clearTimeout(failed);
resolve();
});
return {
ready,
stop() {
2023-06-21 08:09:49 -05:00
return new Promise((resolve, reject) => {
p.on('close', () => resolve());
p.on('error', (err) => reject(err));
p.kill();
});
2022-07-27 15:17:38 -05:00
},
};
}