Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes cloudflare throwing over process #4072

Merged
merged 4 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/shy-kids-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/cloudflare': patch
---

Fixes Cloudflare throwing an error for process
3 changes: 2 additions & 1 deletion packages/astro/src/core/render/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ const canonicalURL = new URL(Astro.url.pathname, Astro.site);
if (!renderMarkdown) {
// The package is saved in this variable because Vite is too smart
// and will try to inline it in buildtime
let astroRemark = '@astrojs/markdown-remark';
let astroRemark = '@astrojs/';
astroRemark += 'markdown-remark';

renderMarkdown = (await import(astroRemark)).renderMarkdown;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/integrations/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"dev": "astro-scripts dev \"src/**/*.ts\"",
"test": "mocha --exit --timeout 20000 test/"
"test": "mocha --exit --timeout 30000 test/"
},
"dependencies": {
"esbuild": "^0.14.42"
"esbuild": "^0.14.42",
"wrangler": "^2.0.23"
},
"devDependencies": {
"astro": "workspace:*",
Expand Down
8 changes: 8 additions & 0 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export function getAdapter(): AstroAdapter {
};
}

const SHIM = `globalThis.process = {
argv: [],
env: {},
};`;

export default function createIntegration(): AstroIntegration {
let _config: AstroConfig;
let _buildConfig: BuildConfig;
Expand Down Expand Up @@ -69,6 +74,9 @@ export default function createIntegration(): AstroIntegration {
format: 'esm',
bundle: true,
minify: true,
banner: {
js: SHIM
}
});

// throw the server folder in the bin
Expand Down
32 changes: 32 additions & 0 deletions packages/integrations/cloudflare/test/basics.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { loadFixture, runCLI } from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';

describe('Basic app', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/basics/',
});
await fixture.build();
});

it('can render', async () => {
const { ready, stop } = runCLI('./fixtures/basics/', { silent: true });

try {
await ready;

let res = await fetch(`http://localhost:8787/`);
expect(res.status).to.equal(200);
let html = await res.text();
let $ = cheerio.load(html);
expect($('h1').text()).to.equal('Testing');
} finally {
await stop();
}
});
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import cloudflare from '@astrojs/cloudflare';

export default defineConfig({
adapter: cloudflare(),
output: 'server',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/astro-cloudflare-basics",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/cloudflare": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
</body>
</html>
52 changes: 52 additions & 0 deletions packages/integrations/cloudflare/test/test-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';

export { fixLineEndings } from '../../../astro/test/test-utils.js';

Expand All @@ -8,3 +10,53 @@ export function loadFixture(config) {
}
return baseLoadFixture(config);
}

const wranglerPath = fileURLToPath(new URL('../node_modules/wrangler/bin/wrangler.js', import.meta.url));

export function runCLI(basePath, { silent }) {
const script = fileURLToPath(new URL(`${basePath}/dist/_worker.js`, import.meta.url));
const p = spawn('node', [wranglerPath, 'dev', '-l', script]);

p.stderr.setEncoding('utf-8');
p.stdout.setEncoding('utf-8');

const timeout = 10000;

const ready = new Promise(async (resolve, reject) => {
const failed = setTimeout(() => reject(new Error(`Timed out starting the wrangler CLI`)), timeout);

(async function () {
for(const msg of p.stderr) {
if(!silent) {
// eslint-disable-next-line
console.error(msg);
}
}
})();

for await(const msg of p.stdout) {
if(!silent) {
// eslint-disable-next-line
console.log(msg);
}
if(msg.includes(`Listening on`)) {
break;
}
}

clearTimeout(failed);
resolve();
});

return {
ready,
stop() {
p.kill();
return new Promise(resolve => {
p.addListener('exit', () => {
resolve();
});
})
}
}
}
Loading