Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/adapter-cloudflare/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Cache, CacheStorage, IncomingRequestCfProperties } from '@cloudflare/wo
declare global {
namespace App {
export interface Platform {
context?: {
context: {
waitUntil(promise: Promise<any>): void;
};
caches?: CacheStorage & { default: Cache };
cf?: IncomingRequestCfProperties;
caches: CacheStorage;
cf: IncomingRequestCfProperties;
}
}
}
26 changes: 26 additions & 0 deletions packages/adapter-cloudflare/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ export interface AdapterOptions {
*/
exclude?: string[];
};

/**
* What bindings will be emulated in local development. This does not sync with your setup
* on the Cloudflare Dashboard automatically.
*/
bindings?: {
kvNamespaces?: string[] | Record<string, string>;
r2Buckets?: string[] | Record<string, string>;
d1Databases?: string[] | Record<string, string>;
/**
* Durable Object Bindings. Separate Worker required for Durable Object definition.
*/
durableObjects: Record<
string,
{
className: string;
scriptName: string;
}
>;
vars?: Record<string, string>;
};

/**
* Where to persist Cloudflare-related data. Defaults to `.wrangler`
*/
persistTo?: string;
}

export interface RoutesJSONSpec {
Expand Down
31 changes: 31 additions & 0 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { writeFileSync } from 'node:fs';
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild';
import { Miniflare } from 'miniflare';

// list from https://developers.cloudflare.com/workers/runtime-apis/nodejs/
const compatible_node_modules = [
Expand All @@ -20,6 +21,19 @@ const compatible_node_modules = [

/** @type {import('./index.js').default} */
export default function (options = {}) {
const persistTo = options.persistTo || '.wrangler';
const mf = new Miniflare({
script: 'export default{fetch({cf:e}){return Response.json(e)}}',
modules: true,
kvNamespaces: options.bindings?.kvNamespaces,
kvPersist: persistTo,
r2Buckets: options.bindings?.r2Buckets,
r2Persist: persistTo,
durableObjects: options.bindings?.durableObjects,
durableObjectsPersist: persistTo,
bindings: options.bindings?.vars,
cachePersist: persistTo
});
return {
name: '@sveltejs/adapter-cloudflare',
async adapt(builder) {
Expand Down Expand Up @@ -124,6 +138,23 @@ export default function (options = {}) {
}`
);
}
},
async emulate() {
/** @type {import('@cloudflare/workers-types').CacheStorage} */
const caches = /** @type {any} */ (await mf.getCaches());
/** @type {import('@cloudflare/workers-types').IncomingRequestCfProperties} */
const cf = /** @type {any} */ (await (await mf.dispatchFetch('http://svelte.kit')).json());
const env = await mf.getBindings();
return {
platform: () => ({
context: {
waitUntil: (prom) => prom
},
caches,
cf,
env
})
};
}
};
}
Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"dependencies": {
"@cloudflare/workers-types": "^4.20231121.0",
"esbuild": "^0.19.11",
"worktop": "0.8.0-next.18"
"worktop": "0.8.0-next.18",
"miniflare": "^3.20240129.0"
},
"devDependencies": {
"@sveltejs/kit": "workspace:^",
Expand Down
Loading