[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:
parent
3e1fbc2c8f
commit
6d8aa4b61f
11 changed files with 114 additions and 10 deletions
5
.changeset/angry-socks-sell.md
Normal file
5
.changeset/angry-socks-sell.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/cloudflare': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Expose cf metadata and Cloudflare Worker Cache API through `caches` in runtime.
|
|
@ -39,6 +39,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@astrojs/underscore-redirects": "^0.1.0",
|
"@astrojs/underscore-redirects": "^0.1.0",
|
||||||
|
"@cloudflare/workers-types": "^4.20230518.0",
|
||||||
"esbuild": "^0.17.12",
|
"esbuild": "^0.17.12",
|
||||||
"tiny-glob": "^0.2.9"
|
"tiny-glob": "^0.2.9"
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
|
import type { Cache, CacheStorage, IncomingRequestCfProperties } from '@cloudflare/workers-types';
|
||||||
|
|
||||||
export type WorkerRuntime<T = unknown> = {
|
export type WorkerRuntime<T = unknown> = {
|
||||||
name: 'cloudflare';
|
name: 'cloudflare';
|
||||||
env: T;
|
env: T;
|
||||||
waitUntil(promise: Promise<any>): void;
|
waitUntil(promise: Promise<any>): void;
|
||||||
passThroughOnException(): void;
|
passThroughOnException(): void;
|
||||||
|
caches?: CacheStorage & { default: Cache };
|
||||||
|
cf?: IncomingRequestCfProperties;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PagesRuntime<T = unknown, U = unknown> = {
|
export type PagesRuntime<T = unknown, U = unknown> = {
|
||||||
|
@ -13,6 +17,8 @@ export type PagesRuntime<T = unknown, U = unknown> = {
|
||||||
data: U;
|
data: U;
|
||||||
waitUntil(promise: Promise<any>): void;
|
waitUntil(promise: Promise<any>): void;
|
||||||
next(request: Request): void;
|
next(request: Request): void;
|
||||||
|
caches?: CacheStorage & { default: Cache };
|
||||||
|
cf?: IncomingRequestCfProperties;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getRuntime<T = unknown, U = unknown>(
|
export function getRuntime<T = unknown, U = unknown>(
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import type { SSRManifest } from 'astro';
|
import type { SSRManifest } from 'astro';
|
||||||
|
import type { Request as CFRequest } from '@cloudflare/workers-types';
|
||||||
import { App } from 'astro/app';
|
import { App } from 'astro/app';
|
||||||
import { getProcessEnvProxy, isNode } from './util.js';
|
import { getProcessEnvProxy, isNode } from './util.js';
|
||||||
|
|
||||||
|
@ -14,7 +15,7 @@ type Env = {
|
||||||
export function createExports(manifest: SSRManifest) {
|
export function createExports(manifest: SSRManifest) {
|
||||||
const app = new App(manifest);
|
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;
|
process.env = env as any;
|
||||||
|
|
||||||
const { pathname } = new URL(request.url);
|
const { pathname } = new URL(request.url);
|
||||||
|
@ -31,7 +32,13 @@ export function createExports(manifest: SSRManifest) {
|
||||||
Symbol.for('astro.clientAddress'),
|
Symbol.for('astro.clientAddress'),
|
||||||
request.headers.get('cf-connecting-ip')
|
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);
|
let response = await app.render(request, routeData);
|
||||||
|
|
||||||
if (app.setCookieHeaders) {
|
if (app.setCookieHeaders) {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import type { SSRManifest } from 'astro';
|
import type { SSRManifest } from 'astro';
|
||||||
|
import type { Request as CFRequest } from '@cloudflare/workers-types';
|
||||||
import { App } from 'astro/app';
|
import { App } from 'astro/app';
|
||||||
import { getProcessEnvProxy, isNode } from './util.js';
|
import { getProcessEnvProxy, isNode } from './util.js';
|
||||||
|
|
||||||
|
@ -14,7 +15,7 @@ export function createExports(manifest: SSRManifest) {
|
||||||
next,
|
next,
|
||||||
...runtimeEnv
|
...runtimeEnv
|
||||||
}: {
|
}: {
|
||||||
request: Request;
|
request: Request & CFRequest;
|
||||||
next: (request: Request) => void;
|
next: (request: Request) => void;
|
||||||
} & Record<string, unknown>) => {
|
} & Record<string, unknown>) => {
|
||||||
process.env = runtimeEnv.env as any;
|
process.env = runtimeEnv.env as any;
|
||||||
|
@ -36,6 +37,8 @@ export function createExports(manifest: SSRManifest) {
|
||||||
...runtimeEnv,
|
...runtimeEnv,
|
||||||
name: 'cloudflare',
|
name: 'cloudflare',
|
||||||
next,
|
next,
|
||||||
|
caches,
|
||||||
|
cf: request.cf,
|
||||||
});
|
});
|
||||||
let response = await app.render(request, routeData);
|
let response = await app.render(request, routeData);
|
||||||
|
|
||||||
|
|
35
packages/integrations/cloudflare/test/cf.js
Normal file
35
packages/integrations/cloudflare/test/cf.js
Normal 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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
8
packages/integrations/cloudflare/test/fixtures/cf/astro.config.mjs
vendored
Normal file
8
packages/integrations/cloudflare/test/fixtures/cf/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import { defineConfig } from 'astro/config';
|
||||||
|
import cloudflare from '@astrojs/cloudflare';
|
||||||
|
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
adapter: cloudflare(),
|
||||||
|
output: 'server',
|
||||||
|
});
|
9
packages/integrations/cloudflare/test/fixtures/cf/package.json
vendored
Normal file
9
packages/integrations/cloudflare/test/fixtures/cf/package.json
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"name": "@test/astro-cloudflare-cf",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"@astrojs/cloudflare": "workspace:*",
|
||||||
|
"astro": "workspace:*"
|
||||||
|
}
|
||||||
|
}
|
14
packages/integrations/cloudflare/test/fixtures/cf/src/pages/index.astro
vendored
Normal file
14
packages/integrations/cloudflare/test/fixtures/cf/src/pages/index.astro
vendored
Normal 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>
|
|
@ -19,9 +19,9 @@ const wranglerPath = fileURLToPath(
|
||||||
new URL('../node_modules/wrangler/bin/wrangler.js', import.meta.url)
|
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 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.stderr.setEncoding('utf-8');
|
||||||
p.stdout.setEncoding('utf-8');
|
p.stdout.setEncoding('utf-8');
|
||||||
|
|
26
pnpm-lock.yaml
generated
26
pnpm-lock.yaml
generated
|
@ -1,8 +1,4 @@
|
||||||
lockfileVersion: '6.1'
|
lockfileVersion: '6.0'
|
||||||
|
|
||||||
settings:
|
|
||||||
autoInstallPeers: false
|
|
||||||
excludeLinksFromLockfile: false
|
|
||||||
|
|
||||||
overrides:
|
overrides:
|
||||||
tsconfig-resolver>type-fest: 3.0.0
|
tsconfig-resolver>type-fest: 3.0.0
|
||||||
|
@ -3589,6 +3585,9 @@ importers:
|
||||||
'@astrojs/underscore-redirects':
|
'@astrojs/underscore-redirects':
|
||||||
specifier: ^0.1.0
|
specifier: ^0.1.0
|
||||||
version: link:../../underscore-redirects
|
version: link:../../underscore-redirects
|
||||||
|
'@cloudflare/workers-types':
|
||||||
|
specifier: ^4.20230518.0
|
||||||
|
version: 4.20230518.0
|
||||||
esbuild:
|
esbuild:
|
||||||
specifier: ^0.17.12
|
specifier: ^0.17.12
|
||||||
version: 0.17.12
|
version: 0.17.12
|
||||||
|
@ -3627,6 +3626,15 @@ importers:
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../../../../../astro
|
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:
|
packages/integrations/cloudflare/test/fixtures/no-output:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@astrojs/cloudflare':
|
'@astrojs/cloudflare':
|
||||||
|
@ -7226,6 +7234,10 @@ packages:
|
||||||
mime: 3.0.0
|
mime: 3.0.0
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/@cloudflare/workers-types@4.20230518.0:
|
||||||
|
resolution: {integrity: sha512-A0w1V+5SUawGaaPRlhFhSC/SCDT9oQG8TMoWOKFLA4qbqagELqEAFD4KySBIkeVOvCBLT1DZSYBMCxbXddl0kw==}
|
||||||
|
dev: false
|
||||||
|
|
||||||
/@colors/colors@1.5.0:
|
/@colors/colors@1.5.0:
|
||||||
resolution: {integrity: sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==}
|
resolution: {integrity: sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==}
|
||||||
engines: {node: '>=0.1.90'}
|
engines: {node: '>=0.1.90'}
|
||||||
|
@ -18393,3 +18405,7 @@ packages:
|
||||||
dependencies:
|
dependencies:
|
||||||
solid-js: 1.7.4
|
solid-js: 1.7.4
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
|
settings:
|
||||||
|
autoInstallPeers: false
|
||||||
|
excludeLinksFromLockfile: false
|
||||||
|
|
Loading…
Add table
Reference in a new issue