Skip to content

Commit

Permalink
feature(cloudflare): add Caches bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr committed Sep 26, 2023
1 parent 6755acd commit 3976c39
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
25 changes: 5 additions & 20 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { AstroConfig, AstroIntegration, RouteData } from 'astro';

import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
import { CacheStorage } from '@miniflare/cache';
import { NoOpLog } from '@miniflare/shared';
import { MemoryStorage } from '@miniflare/storage-memory';
import { AstroError } from 'astro/errors';
import esbuild from 'esbuild';
import { Miniflare } from 'miniflare';
Expand All @@ -23,6 +20,8 @@ import { wasmModuleLoader } from './utils/wasm-module-loader.js';
export type { AdvancedRuntime } from './entrypoints/server.advanced.js';
export type { DirectoryRuntime } from './entrypoints/server.directory.js';

export { Response as MiniflareResponse } from 'miniflare';

type Options = {
mode?: 'directory' | 'advanced';
functionPerRoute?: boolean;
Expand Down Expand Up @@ -56,17 +55,6 @@ interface BuildConfig {
split?: boolean;
}

class StorageFactory {
storages = new Map();

storage(namespace: string) {
let storage = this.storages.get(namespace);
if (storage) return storage;
this.storages.set(namespace, (storage = new MemoryStorage()));
return storage;
}
}

export default function createIntegration(args?: Options): AstroIntegration {
let _config: AstroConfig;
let _buildConfig: BuildConfig;
Expand Down Expand Up @@ -140,6 +128,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
script: '',
cache: true,
cachePersist: true,
cacheWarnUsage: true,
d1Databases: D1Bindings,
d1Persist: true,
r2Buckets: R2Bindings,
Expand All @@ -161,6 +150,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
const namespace = await _mf.getKVNamespace(KVBinding);
Reflect.set(bindingsEnv, KVBinding, namespace);
}
const mfCache = await _mf.getCaches();

process.env.PWD = originalPWD;
const clientLocalsSymbol = Symbol.for('astro.locals');
Expand All @@ -183,12 +173,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
waitUntil: (_promise: Promise<any>) => {
return;
},
caches: new CacheStorage(
{ cache: true, cachePersist: false },
new NoOpLog(),
new StorageFactory(),
{}
),
caches: mfCache,
},
});
next();
Expand Down
15 changes: 12 additions & 3 deletions packages/integrations/cloudflare/test/cf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,22 @@ describe('Astro Cloudflare Runtime', () => {
await devServer?.stop();
});

it('Populates CF, Vars & Bindings', async () => {
it('adds cf object', async () => {
let res = await fixture.fetch('/');
expect(res.status).to.equal(200);
let html = await res.text();
let $ = cheerio.load(html);
expect($('#hasRuntime').text()).to.equal('true');
expect($('#hasCache').text()).to.equal('true');
expect($('#hasCF').text()).to.equal('true');
});

it('adds cache mocking', async () => {
let res = await fixture.fetch('/caches');
expect(res.status).to.equal(200);
let html = await res.text();
let $ = cheerio.load(html);
expect($('#hasCACHE').text()).to.equal('true');
expect($('#hasDEFAULT').text()).to.equal('true');
// expect($('#hasCUSTOM').text()).to.equal('true');
});

it('adds D1 mocking', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
import { MiniflareResponse } from "@astrojs/cloudflare";
const runtime = Astro.locals.runtime;
try {
await runtime.caches.default.put(
'http://localhost/cache',
new MiniflareResponse('true', {
headers: { 'Cache-Control': 'max-age=3600', 'X-Key': 'value' },
})
);
} catch (error) {
console.log(error);
}
const response = await runtime.caches.default.match('http://localhost/cache');
const body = await response.text();
---

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>CACHES</title>
</head>
<body>
<pre id="hasCACHE">{!!runtime.caches}</pre>
<pre id="hasDEFAULT">{!!body}</pre>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const runtime = Astro.locals.runtime;
</head>
<body>
<h1>Testing</h1>
<div id="hasRuntime">{!!runtime.cf?.colo}</div>
<div id="hasCache">{!!runtime.caches}</div>
<div id="hasCF">{!!runtime.cf?.colo}</div>
</body>
</html>

0 comments on commit 3976c39

Please sign in to comment.