Skip to content

Commit

Permalink
feat(cloudflare): add local mocking for DO bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr committed Oct 2, 2023
1 parent bc6c259 commit 5081969
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-readers-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': minor
---

Introduce support for local Durable Objects bindings. Enhance development experience by allowing direct integration with `astro dev`.
1 change: 1 addition & 0 deletions packages/integrations/cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ Currently supported bindings:
- [Cloudflare D1](https://developers.cloudflare.com/d1/)
- [Cloudflare R2](https://developers.cloudflare.com/r2/)
- [Cloudflare Workers KV](https://developers.cloudflare.com/kv/)
- [Cloudflare Durable Objects](https://developers.cloudflare.com/durable-objects/)

You can access the runtime from Astro components through `Astro.locals` inside any .astro` file.

Expand Down
18 changes: 16 additions & 2 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import glob from 'tiny-glob';
import { getAdapter } from './getAdapter.js';
import { deduplicatePatterns } from './utils/deduplicatePatterns.js';
import { getCFObject } from './utils/getCFObject.js';
import { getD1Bindings, getEnvVars, getKVBindings, getR2Bindings } from './utils/parser.js';
import {
getD1Bindings,
getDOBindings,
getEnvVars,
getKVBindings,
getR2Bindings,
} from './utils/parser.js';
import { prependForwardSlash } from './utils/prependForwardSlash.js';
import { rewriteWasmImportPath } from './utils/rewriteWasmImportPath.js';
import { wasmModuleLoader } from './utils/wasm-module-loader.js';
Expand Down Expand Up @@ -113,7 +119,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
const D1Bindings = await getD1Bindings();
const R2Bindings = await getR2Bindings();
const KVBindings = await getKVBindings();

const DOBindings = await getDOBindings();
let bindingsEnv = new Object({});

// fix for the error "kj/filesystem-disk-unix.c++:1709: warning: PWD environment variable doesn't match current directory."
Expand All @@ -133,6 +139,8 @@ export default function createIntegration(args?: Options): AstroIntegration {
r2Persist: true,
kvNamespaces: KVBindings,
kvPersist: true,
durableObjects: DOBindings,
durableObjectsPersist: true,
});
await _mf.ready;

Expand All @@ -148,6 +156,12 @@ export default function createIntegration(args?: Options): AstroIntegration {
const namespace = await _mf.getKVNamespace(KVBinding);
Reflect.set(bindingsEnv, KVBinding, namespace);
}
for (const key in DOBindings) {
if (Object.prototype.hasOwnProperty.call(DOBindings, key)) {
const DO = await _mf.getDurableObjectNamespace(key);
Reflect.set(bindingsEnv, key, DO);
}
}
const mfCache = await _mf.getCaches();

process.env.PWD = originalPWD;
Expand Down
17 changes: 17 additions & 0 deletions packages/integrations/cloudflare/src/utils/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,20 @@ export async function getKVBindings() {
);
return bindings;
}

export function getDOBindings(): Record<
string,
{ scriptName?: string | undefined; unsafeUniqueKey?: string | undefined; className: string }
> {
const { rawConfig } = parseConfig();
if (!rawConfig) return {};
if (!rawConfig?.durable_objects) return {};
const output = new Object({}) as Record<
string,
{ scriptName?: string | undefined; unsafeUniqueKey?: string | undefined; className: string }
>;
for (const binding of rawConfig?.durable_objects.bindings) {
Reflect.set(output, binding.name, { className: binding.class_name });
}
return output;
}
10 changes: 10 additions & 0 deletions packages/integrations/cloudflare/test/cf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,14 @@ describe('Astro Cloudflare Runtime', () => {
expect($('#hasPRODKV').text()).to.equal('true');
expect($('#hasACCESS').text()).to.equal('true');
});

it('adds DO mocking', async () => {
expect(await fixture.pathExists('../.mf/do')).to.be.true;

let res = await fixture.fetch('/do');
expect(res.status).to.equal(200);
let html = await res.text();
let $ = cheerio.load(html);
expect($('#hasDO').text()).to.equal('true');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
const runtime = Astro.locals.runtime;
---

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DO</title>
</head>
<body>
<pre id="hasDO">{!!runtime.env.DO}</pre>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ bucket_name = '<YOUR_BUCKET_NAME>'
binding = 'R2_PROD' # <~ valid JavaScript variable name
bucket_name = '<YOUR_BUCKET_NAME>'

[[durable_objects.bindings]]
name = "DO"
class_name = "DurableObjectExample"

[[durable_objects.bindings]]
name = "DO_PROD"
class_name = "DurableObjectProductionExample"

0 comments on commit 5081969

Please sign in to comment.