cf-workers-proxy
Enable Cloudflare Workers runtime for local development. Compatible with DrizzleORM.
Get Started
Install wrangler
npm i -D wrangler
Install cf-workers-proxy
npm i -D cf-workers-proxy
Example wrangler.toml
name = "worker"
compatibility_date = "2023-07-02"
[[d1_databases]]
binding = "D1"
database_name = "D1"
database_id = "<d1-id>"
[[kv_namespaces]]
binding = "KV"
id = "<kv-id>"
preview_id = "<same-kv-id-as-above>"
[[services]]
binding = "WORKER"
service = "<worker-name>"
environment = "production"
Start proxy
npx wrangler node_modules/cf-workers-proxy/dist/worker.js --remote
Example SvelteKit project
// file: app.d.ts
declare global {
namespace App {
interface Locals {
D1: D1Database;
}
interface Platform {
env?: {
D1: D1Database;
};
}
}
}
export {};
// file: src/hooks.server.ts
import { createD1 } from 'cf-workers-proxy';
export const handle = ({ event, resolve }) => {
event.locals.D1 = event.platform?.env?.D1 ?? createD1('D1');
// or createD1('D1', { hostname: 'custom-host-name' });
// default hostname is `http://127.0.0.1:8787`
return resolve(event);
};
Roadmap
- ❌ Not started
- 🟡 Partially implemented
- ✅ Complete
D1Database
import { createD1 } from 'cf-workers-proxy';
Function |
Status |
prepare() |
✅ |
batch() |
❌ |
dump() |
❌ |
exec() |
✅ |
D1PreparedStatement
Function |
Status |
first() |
✅ |
run() |
✅ |
all() |
✅ |
raw() |
✅ |
bind() |
✅ |
Service Bindings
import { createServiceBinding } from 'cf-workers-proxy';
Function |
Status |
fetch() |
✅ |
connect() |
❌ |
KVNamespace
import { createKV } from 'cf-workers-proxy';
Function |
Status |
put() |
🟡 |
get() |
🟡 |
getWithMetadata() |
❌ |
delete() |
❌ |
list() |
❌ |
waitUntil
// file: app.d.ts
namespace App {
interface Platform {
context: {
waitUntil(promise: Promise<any>): void;
};
}
}
// file: +page.server.ts
import { waitUntil } from 'cf-workers-proxy';
export const actions = {
default: ({ locals, platform }) => {
waitUntil(async () => {}, platform?.context.waitUntil);
return { message: 'success' };
},
};
Contributing
or pull request 😐