From 86d041f52ce47d16cf1287f9de7095b5adf05683 Mon Sep 17 00:00:00 2001 From: chientrm Date: Mon, 21 Aug 2023 15:12:56 +0000 Subject: [PATCH] worker.js --- build.js | 8 ++++++++ package.json | 2 +- src/worker.ts | 15 ++++++++------- worker.ts | 3 +++ 4 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 worker.ts diff --git a/build.js b/build.js index ec721bb..5256ccf 100644 --- a/build.js +++ b/build.js @@ -7,3 +7,11 @@ build({ minify: true, format: 'esm', }); + +build({ + entryPoints: ['worker.ts'], + outdir: 'dist', + bundle: true, + minify: true, + format: 'esm', +}); diff --git a/package.json b/package.json index d8554e7..2117ad0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cf-workers-proxy", - "description": "Enable Cloudflare Workers runtime for local development", + "description": "Enable Cloudflare Workers runtime for local development. Compatible with DrizzleORM", "version": "0.0.1", "type": "module", "main": "dist/index.js", diff --git a/src/worker.ts b/src/worker.ts index 1f427cc..7edd588 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -4,7 +4,8 @@ import { D1DatabaseProxyHolder } from './proxies/d1_database/proxy_holder'; import { FetcherProxyHolder } from './proxies/fetcher/proxy_holder'; import { KVProxyHolder } from './proxies/kv/proxy_holder'; -const json = (data: T) => { +const defaultHostname = 'http://127.0.0.1:8787', + json = (data: T) => { const result: SuccessResult = { status: 200, data }, response = new Response(JSON.stringify(result), { headers: { 'Content-Type': 'application/json' }, @@ -44,24 +45,24 @@ const json = (data: T) => { return new Response(null, { status: 400 }); }, }, - createD1 = (name: string, options?: { hostname?: string }): D1Database => + createD1 = (name: string, options?: { hostname: string }): D1Database => new D1DatabaseProxyHolder({ - host: options?.hostname ?? 'http://127.0.0.1:8787', + host: options?.hostname ?? defaultHostname, name, payload: {}, }), - createKV = (name: string, options?: { hostname?: string }): KVNamespace => + createKV = (name: string, options?: { hostname: string }): KVNamespace => new KVProxyHolder({ - host: options?.hostname ?? 'http://127.0.0.1:8787', + host: options?.hostname ?? defaultHostname, name, payload: {}, }), createServiceBinding = ( name: string, - options?: { hostname?: string } + options?: { hostname: string } ): Fetcher => new FetcherProxyHolder({ - host: options?.hostname ?? 'http://127.0.0.1:8787', + host: options?.hostname ?? defaultHostname, name, payload: {}, }), diff --git a/worker.ts b/worker.ts new file mode 100644 index 0000000..e1046f4 --- /dev/null +++ b/worker.ts @@ -0,0 +1,3 @@ +import { createWorker } from './src/worker'; + +export default createWorker();