[Cloudflare integration] Expose cf metadata and Cloudflare caches API (#7386)

* Add cf and cache properties to runtime

* add changeset

* reorder import

* fix types and add tests

* fix package name

* test
This commit is contained in:
beynar 2023-06-13 22:34:44 +02:00 committed by GitHub
parent 3e1fbc2c8f
commit 6d8aa4b61f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 114 additions and 10 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': minor
---
Expose cf metadata and Cloudflare Worker Cache API through `caches` in runtime.

View file

@ -39,6 +39,7 @@
},
"dependencies": {
"@astrojs/underscore-redirects": "^0.1.0",
"@cloudflare/workers-types": "^4.20230518.0",
"esbuild": "^0.17.12",
"tiny-glob": "^0.2.9"
},

View file

@ -1,8 +1,12 @@
import type { Cache, CacheStorage, IncomingRequestCfProperties } from '@cloudflare/workers-types';
export type WorkerRuntime<T = unknown> = {
name: 'cloudflare';
env: T;
waitUntil(promise: Promise<any>): void;
passThroughOnException(): void;
caches?: CacheStorage & { default: Cache };
cf?: IncomingRequestCfProperties;
};
export type PagesRuntime<T = unknown, U = unknown> = {
@ -13,6 +17,8 @@ export type PagesRuntime<T = unknown, U = unknown> = {
data: U;
waitUntil(promise: Promise<any>): void;
next(request: Request): void;
caches?: CacheStorage & { default: Cache };
cf?: IncomingRequestCfProperties;
};
export function getRuntime<T = unknown, U = unknown>(

View file

@ -1,4 +1,5 @@
import type { SSRManifest } from 'astro';
import type { Request as CFRequest } from '@cloudflare/workers-types';
import { App } from 'astro/app';
import { getProcessEnvProxy, isNode } from './util.js';
@ -14,7 +15,7 @@ type Env = {
export function createExports(manifest: SSRManifest) {
const app = new App(manifest);
const fetch = async (request: Request, env: Env, context: any) => {
const fetch = async (request: Request & CFRequest, env: Env, context: any) => {
process.env = env as any;
const { pathname } = new URL(request.url);
@ -31,7 +32,13 @@ export function createExports(manifest: SSRManifest) {
Symbol.for('astro.clientAddress'),
request.headers.get('cf-connecting-ip')
);
Reflect.set(request, Symbol.for('runtime'), { env, name: 'cloudflare', ...context });
Reflect.set(request, Symbol.for('runtime'), {
env,
name: 'cloudflare',
caches,
cf: request.cf,
...context,
});
let response = await app.render(request, routeData);
if (app.setCookieHeaders) {

View file

@ -1,4 +1,5 @@
import type { SSRManifest } from 'astro';
import type { Request as CFRequest } from '@cloudflare/workers-types';
import { App } from 'astro/app';
import { getProcessEnvProxy, isNode } from './util.js';
@ -14,7 +15,7 @@ export function createExports(manifest: SSRManifest) {
next,
...runtimeEnv
}: {
request: Request;
request: Request & CFRequest;
next: (request: Request) => void;
} & Record<string, unknown>) => {
process.env = runtimeEnv.env as any;
@ -36,6 +37,8 @@ export function createExports(manifest: SSRManifest) {
...runtimeEnv,
name: 'cloudflare',
next,
caches,
cf: request.cf,
});
let response = await app.render(request, routeData);

View file

@ -0,0 +1,35 @@
import { loadFixture, runCLI } from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import cloudflare from '../dist/index.js';
describe('Cf metadata and caches', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/cf/',
output: 'server',
adapter: cloudflare(),
});
await fixture.build();
});
it('Load cf and caches API', async () => {
const { ready, stop } = runCLI('./fixtures/cf/', { silent: false, port: 8788 });
try {
await ready;
let res = await fetch(`http://localhost:8788/`);
expect(res.status).to.equal(200);
let html = await res.text();
let $ = cheerio.load(html);
// console.log($('#cf').text(), html);
expect($('#cf').text()).to.contain('city');
expect($('#hasCache').text()).to.equal('true');
} finally {
stop();
}
});
});

View file

@ -0,0 +1,8 @@
import { defineConfig } from 'astro/config';
import cloudflare from '@astrojs/cloudflare';
export default defineConfig({
adapter: cloudflare(),
output: 'server',
});

View file

@ -0,0 +1,9 @@
{
"name": "@test/astro-cloudflare-cf",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/cloudflare": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,14 @@
---
import { getRuntime } from '@astrojs/cloudflare/runtime';
const runtime = getRuntime(Astro.request);
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
<div id="cf">{JSON.stringify(runtime.cf)}</div>
<div id="hasCache">{!!runtime.caches}</div>
</body>
</html>

View file

@ -19,9 +19,9 @@ const wranglerPath = fileURLToPath(
new URL('../node_modules/wrangler/bin/wrangler.js', import.meta.url)
);
export function runCLI(basePath, { silent }) {
export function runCLI(basePath, { silent, port = 8787 }) {
const script = fileURLToPath(new URL(`${basePath}/dist/_worker.js`, import.meta.url));
const p = spawn('node', [wranglerPath, 'dev', '-l', script]);
const p = spawn('node', [wranglerPath, 'dev', '-l', script, '--port', port]);
p.stderr.setEncoding('utf-8');
p.stdout.setEncoding('utf-8');

View file

@ -1,8 +1,4 @@
lockfileVersion: '6.1'
settings:
autoInstallPeers: false
excludeLinksFromLockfile: false
lockfileVersion: '6.0'
overrides:
tsconfig-resolver>type-fest: 3.0.0
@ -3589,6 +3585,9 @@ importers:
'@astrojs/underscore-redirects':
specifier: ^0.1.0
version: link:../../underscore-redirects
'@cloudflare/workers-types':
specifier: ^4.20230518.0
version: 4.20230518.0
esbuild:
specifier: ^0.17.12
version: 0.17.12
@ -3627,6 +3626,15 @@ importers:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/cloudflare/test/fixtures/cf:
dependencies:
'@astrojs/cloudflare':
specifier: workspace:*
version: link:../../..
astro:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/cloudflare/test/fixtures/no-output:
dependencies:
'@astrojs/cloudflare':
@ -7226,6 +7234,10 @@ packages:
mime: 3.0.0
dev: true
/@cloudflare/workers-types@4.20230518.0:
resolution: {integrity: sha512-A0w1V+5SUawGaaPRlhFhSC/SCDT9oQG8TMoWOKFLA4qbqagELqEAFD4KySBIkeVOvCBLT1DZSYBMCxbXddl0kw==}
dev: false
/@colors/colors@1.5.0:
resolution: {integrity: sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==}
engines: {node: '>=0.1.90'}
@ -18393,3 +18405,7 @@ packages:
dependencies:
solid-js: 1.7.4
dev: false
settings:
autoInstallPeers: false
excludeLinksFromLockfile: false