@astrojs/cloudflare 0.0.0-cloudcannon-fix-20230306211609

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/CHANGELOG.md +379 -0
  3. package/LICENSE +61 -0
  4. package/README.md +119 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.js +156 -0
  7. package/dist/runtime.d.ts +16 -0
  8. package/dist/runtime.js +12 -0
  9. package/dist/server.advanced.d.ts +13 -0
  10. package/dist/server.advanced.js +39 -0
  11. package/dist/server.directory.d.ts +7 -0
  12. package/dist/server.directory.js +47 -0
  13. package/dist/util.d.ts +2 -0
  14. package/dist/util.js +17 -0
  15. package/package.json +51 -0
  16. package/runtime.d.ts +3 -0
  17. package/src/index.ts +219 -0
  18. package/src/runtime.ts +28 -0
  19. package/src/server.advanced.ts +53 -0
  20. package/src/server.directory.ts +58 -0
  21. package/src/util.ts +19 -0
  22. package/test/basics.test.js +32 -0
  23. package/test/directory.test.js +22 -0
  24. package/test/fixtures/basics/astro.config.mjs +10 -0
  25. package/test/fixtures/basics/node_modules/.bin/astro +17 -0
  26. package/test/fixtures/basics/package.json +9 -0
  27. package/test/fixtures/basics/src/pages/index.astro +9 -0
  28. package/test/fixtures/no-output/astro.config.mjs +6 -0
  29. package/test/fixtures/no-output/node_modules/.bin/astro +17 -0
  30. package/test/fixtures/no-output/package.json +9 -0
  31. package/test/fixtures/prerender/astro.config.mjs +7 -0
  32. package/test/fixtures/prerender/node_modules/.bin/astro +17 -0
  33. package/test/fixtures/prerender/package.json +9 -0
  34. package/test/fixtures/prerender/src/pages/index.astro +8 -0
  35. package/test/fixtures/prerender/src/pages/one.astro +11 -0
  36. package/test/no-output.test.js +24 -0
  37. package/test/prerender.test.js +19 -0
  38. package/test/test-utils.js +62 -0
  39. package/test/wrangler.toml +4 -0
  40. package/tsconfig.json +10 -0
@@ -0,0 +1,58 @@
1
+ import type { SSRManifest } from 'astro';
2
+ import { App } from 'astro/app';
3
+ import { getProcessEnvProxy, isNode } from './util.js';
4
+
5
+ if (!isNode) {
6
+ process.env = getProcessEnvProxy();
7
+ }
8
+
9
+ export function createExports(manifest: SSRManifest) {
10
+ const app = new App(manifest);
11
+
12
+ const onRequest = async ({
13
+ request,
14
+ next,
15
+ ...runtimeEnv
16
+ }: {
17
+ request: Request;
18
+ next: (request: Request) => void;
19
+ } & Record<string, unknown>) => {
20
+ process.env = runtimeEnv.env as any;
21
+
22
+ const { pathname } = new URL(request.url);
23
+ // static assets fallback, in case default _routes.json is not used
24
+ if (manifest.assets.has(pathname)) {
25
+ return next(request);
26
+ }
27
+
28
+ let routeData = app.match(request, { matchNotFound: true });
29
+ if (routeData) {
30
+ Reflect.set(
31
+ request,
32
+ Symbol.for('astro.clientAddress'),
33
+ request.headers.get('cf-connecting-ip')
34
+ );
35
+ Reflect.set(request, Symbol.for('runtime'), {
36
+ ...runtimeEnv,
37
+ name: 'cloudflare',
38
+ next,
39
+ });
40
+ let response = await app.render(request, routeData);
41
+
42
+ if (app.setCookieHeaders) {
43
+ for (const setCookieHeader of app.setCookieHeaders(response)) {
44
+ response.headers.append('Set-Cookie', setCookieHeader);
45
+ }
46
+ }
47
+
48
+ return response;
49
+ }
50
+
51
+ return new Response(null, {
52
+ status: 404,
53
+ statusText: 'Not found',
54
+ });
55
+ };
56
+
57
+ return { onRequest };
58
+ }
package/src/util.ts ADDED
@@ -0,0 +1,19 @@
1
+ export const isNode =
2
+ typeof process === 'object' && Object.prototype.toString.call(process) === '[object process]';
3
+
4
+ export function getProcessEnvProxy() {
5
+ return new Proxy(
6
+ {},
7
+ {
8
+ get: (target, prop) => {
9
+ console.warn(
10
+ // NOTE: \0 prevents Vite replacement
11
+ `Unable to access \`import.meta\0.env.${prop.toString()}\` on initialization ` +
12
+ `as the Cloudflare platform only provides the environment variables per request. ` +
13
+ `Please move the environment variable access inside a function ` +
14
+ `that's only called after a request has been received.`
15
+ );
16
+ },
17
+ }
18
+ );
19
+ }
@@ -0,0 +1,32 @@
1
+ import { loadFixture, runCLI } from './test-utils.js';
2
+ import { expect } from 'chai';
3
+ import * as cheerio from 'cheerio';
4
+
5
+ describe.skip('Basic app', () => {
6
+ /** @type {import('./test-utils').Fixture} */
7
+ let fixture;
8
+
9
+ before(async () => {
10
+ fixture = await loadFixture({
11
+ root: './fixtures/basics/',
12
+ });
13
+ await fixture.build();
14
+ });
15
+
16
+ it('can render', async () => {
17
+ const { ready, stop } = runCLI('./fixtures/basics/', { silent: true });
18
+
19
+ try {
20
+ await ready;
21
+
22
+ let res = await fetch(`http://localhost:8787/`);
23
+ expect(res.status).to.equal(200);
24
+ let html = await res.text();
25
+ let $ = cheerio.load(html);
26
+ expect($('h1').text()).to.equal('Testing');
27
+ expect($('#env').text()).to.equal('secret');
28
+ } finally {
29
+ stop();
30
+ }
31
+ });
32
+ });
@@ -0,0 +1,22 @@
1
+ import { loadFixture } from './test-utils.js';
2
+ import { expect } from 'chai';
3
+ import cloudflare from '../dist/index.js';
4
+
5
+ /** @type {import('./test-utils').Fixture} */
6
+ describe('mode: "directory"', () => {
7
+ let fixture;
8
+
9
+ before(async () => {
10
+ fixture = await loadFixture({
11
+ root: './fixtures/basics/',
12
+ output: 'server',
13
+ adapter: cloudflare({ mode: 'directory' }),
14
+ });
15
+ await fixture.build();
16
+ });
17
+
18
+ it('generates functions folder inside the project root', async () => {
19
+ expect(await fixture.pathExists('../functions')).to.be.true;
20
+ expect(await fixture.pathExists('../functions/[[path]].js')).to.be.true;
21
+ });
22
+ });
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from 'astro/config';
2
+ import cloudflare from '@astrojs/cloudflare';
3
+
4
+ // test env var
5
+ process.env.SECRET_STUFF = 'secret'
6
+
7
+ export default defineConfig({
8
+ adapter: cloudflare(),
9
+ output: 'server',
10
+ });
@@ -0,0 +1,17 @@
1
+ #!/bin/sh
2
+ basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
3
+
4
+ case `uname` in
5
+ *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
6
+ esac
7
+
8
+ if [ -z "$NODE_PATH" ]; then
9
+ export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
10
+ else
11
+ export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
12
+ fi
13
+ if [ -x "$basedir/node" ]; then
14
+ exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
15
+ else
16
+ exec node "$basedir/../../../../../../../astro/astro.js" "$@"
17
+ fi
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "@test/astro-cloudflare-basics",
3
+ "version": "0.0.0",
4
+ "private": true,
5
+ "dependencies": {
6
+ "@astrojs/cloudflare": "workspace:*",
7
+ "astro": "workspace:*"
8
+ }
9
+ }
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <head>
3
+ <title>Testing</title>
4
+ </head>
5
+ <body>
6
+ <h1>Testing</h1>
7
+ <div id="env">{import.meta.env.SECRET_STUFF}</div>
8
+ </body>
9
+ </html>
@@ -0,0 +1,6 @@
1
+ import { defineConfig } from 'astro/config';
2
+ import cloudflare from '@astrojs/cloudflare';
3
+
4
+ export default defineConfig({
5
+ adapter: cloudflare()
6
+ });
@@ -0,0 +1,17 @@
1
+ #!/bin/sh
2
+ basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
3
+
4
+ case `uname` in
5
+ *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
6
+ esac
7
+
8
+ if [ -z "$NODE_PATH" ]; then
9
+ export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
10
+ else
11
+ export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
12
+ fi
13
+ if [ -x "$basedir/node" ]; then
14
+ exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
15
+ else
16
+ exec node "$basedir/../../../../../../../astro/astro.js" "$@"
17
+ fi
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "@test/astro-cloudflare-no-output",
3
+ "version": "0.0.0",
4
+ "private": true,
5
+ "dependencies": {
6
+ "@astrojs/cloudflare": "workspace:*",
7
+ "astro": "workspace:*"
8
+ }
9
+ }
@@ -0,0 +1,7 @@
1
+ import { defineConfig } from 'astro/config';
2
+ import cloudflare from '@astrojs/cloudflare';
3
+
4
+ export default defineConfig({
5
+ adapter: cloudflare(),
6
+ output: 'server',
7
+ });
@@ -0,0 +1,17 @@
1
+ #!/bin/sh
2
+ basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
3
+
4
+ case `uname` in
5
+ *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
6
+ esac
7
+
8
+ if [ -z "$NODE_PATH" ]; then
9
+ export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
10
+ else
11
+ export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
12
+ fi
13
+ if [ -x "$basedir/node" ]; then
14
+ exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
15
+ else
16
+ exec node "$basedir/../../../../../../../astro/astro.js" "$@"
17
+ fi
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "@test/astro-cloudflare-prerender",
3
+ "version": "0.0.0",
4
+ "private": true,
5
+ "dependencies": {
6
+ "@astrojs/cloudflare": "workspace:*",
7
+ "astro": "workspace:*"
8
+ }
9
+ }
@@ -0,0 +1,8 @@
1
+ <html>
2
+ <head>
3
+ <title>Testing</title>
4
+ </head>
5
+ <body>
6
+ <h1>Testing</h1>
7
+ </body>
8
+ </html>
@@ -0,0 +1,11 @@
1
+ ---
2
+ export const prerender = true;
3
+ ---
4
+ <html>
5
+ <head>
6
+ <title>Testing</title>
7
+ </head>
8
+ <body>
9
+ <h1>Testing</h1>
10
+ </body>
11
+ </html>
@@ -0,0 +1,24 @@
1
+ import { loadFixture } from './test-utils.js';
2
+ import { expect } from 'chai';
3
+
4
+ describe('Missing output config', () => {
5
+ /** @type {import('./test-utils').Fixture} */
6
+ let fixture;
7
+
8
+ before(async () => {
9
+ fixture = await loadFixture({
10
+ root: './fixtures/no-output/',
11
+ });
12
+ });
13
+
14
+ it('throws during the build', async () => {
15
+ let error = undefined;
16
+ try {
17
+ await fixture.build();
18
+ } catch (err) {
19
+ error = err;
20
+ }
21
+ expect(error).to.not.be.equal(undefined);
22
+ expect(error.message).to.include(`output: "server"`);
23
+ });
24
+ });
@@ -0,0 +1,19 @@
1
+ import { loadFixture } from './test-utils.js';
2
+ import { expect } from 'chai';
3
+
4
+ describe('Prerendering', () => {
5
+ /** @type {import('./test-utils').Fixture} */
6
+ let fixture;
7
+
8
+ before(async () => {
9
+ fixture = await loadFixture({
10
+ root: './fixtures/prerender/',
11
+ });
12
+ await fixture.build();
13
+ });
14
+
15
+ it('includes prerendered routes in the routes.json config', async () => {
16
+ const routes = JSON.parse(await fixture.readFile('/_routes.json'));
17
+ expect(routes.exclude).to.include('/one/');
18
+ });
19
+ });
@@ -0,0 +1,62 @@
1
+ import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';
2
+ import { spawn } from 'child_process';
3
+ import { fileURLToPath } from 'url';
4
+
5
+ export { fixLineEndings } from '../../../astro/test/test-utils.js';
6
+
7
+ export function loadFixture(config) {
8
+ if (config?.root) {
9
+ config.root = new URL(config.root, import.meta.url);
10
+ }
11
+ return baseLoadFixture(config);
12
+ }
13
+
14
+ const wranglerPath = fileURLToPath(
15
+ new URL('../node_modules/wrangler/bin/wrangler.js', import.meta.url)
16
+ );
17
+
18
+ export function runCLI(basePath, { silent }) {
19
+ const script = fileURLToPath(new URL(`${basePath}/dist/_worker.js`, import.meta.url));
20
+ const p = spawn('node', [wranglerPath, 'dev', '-l', script]);
21
+
22
+ p.stderr.setEncoding('utf-8');
23
+ p.stdout.setEncoding('utf-8');
24
+
25
+ const timeout = 10000;
26
+
27
+ const ready = new Promise(async (resolve, reject) => {
28
+ const failed = setTimeout(
29
+ () => reject(new Error(`Timed out starting the wrangler CLI`)),
30
+ timeout
31
+ );
32
+
33
+ (async function () {
34
+ for (const msg of p.stderr) {
35
+ if (!silent) {
36
+ // eslint-disable-next-line
37
+ console.error(msg);
38
+ }
39
+ }
40
+ })();
41
+
42
+ for await (const msg of p.stdout) {
43
+ if (!silent) {
44
+ // eslint-disable-next-line
45
+ console.log(msg);
46
+ }
47
+ if (msg.includes(`Listening on`)) {
48
+ break;
49
+ }
50
+ }
51
+
52
+ clearTimeout(failed);
53
+ resolve();
54
+ });
55
+
56
+ return {
57
+ ready,
58
+ stop() {
59
+ p.kill();
60
+ },
61
+ };
62
+ }
@@ -0,0 +1,4 @@
1
+ # for tests only
2
+
3
+ [vars]
4
+ SECRET_STUFF = "secret"
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "include": ["src"],
4
+ "compilerOptions": {
5
+ "allowJs": true,
6
+ "module": "ES2022",
7
+ "outDir": "./dist",
8
+ "target": "ES2021"
9
+ }
10
+ }