diff --git a/README.md b/README.md index c1c208e..b124d92 100644 --- a/README.md +++ b/README.md @@ -161,11 +161,11 @@ import { connectR2 } from 'wrangler-proxy'; | Function | Status | | ------------------------- | ------ | | `head()` | ❌ | -| `get()` | 🟡 | -| `put()` | 🟡 | +| `get()` | ✅ | +| `put()` | ✅ | | `createMultipartUpload()` | ❌ | | `resumeMultipartUpload()` | ❌ | -| `delete()` | ❌ | +| `delete()` | ✅ | | `list()` | ❌ | ### `waitUntil` diff --git a/src/factory.ts b/src/factory.ts index 053ef0e..5989334 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -10,6 +10,7 @@ import { KVGetProxy } from './proxies/kv/get/proxy'; import { KVGetWithMetadataProxy } from './proxies/kv/getWithMetadata/proxy'; import { KVListProxy } from './proxies/kv/list/proxy'; import { KVPutProxy } from './proxies/kv/put/proxy'; +import { R2DeleteProxy } from './proxies/r2/delete/proxy'; import { R2GetProxy } from './proxies/r2/get/proxy'; import { R2PutProxy } from './proxies/r2/put/proxy'; @@ -46,6 +47,8 @@ class ProxyFactory { return new R2PutProxy({ name, metadata, data }); case R2GetProxy.proxyType: return new R2GetProxy({ name, metadata }); + case R2DeleteProxy.proxyType: + return new R2DeleteProxy({ name, metadata }); default: throw new Error('Unknown proxy type.'); } diff --git a/src/proxies/r2/delete/proxy.ts b/src/proxies/r2/delete/proxy.ts new file mode 100644 index 0000000..7a727d1 --- /dev/null +++ b/src/proxies/r2/delete/proxy.ts @@ -0,0 +1,31 @@ +import { Proxy } from '../../proxy'; + +interface Metadata { + keys: string | string[]; +} + +export class R2DeleteProxy extends Proxy { + static readonly proxyType = 'R2DeleteProxy'; + constructor({ + host, + name, + metadata, + }: { + host?: string; + name: string; + metadata: Metadata; + }) { + const proxyType = R2DeleteProxy.proxyType; + super({ proxyType, host, name, metadata, data: null }); + } + async execute(env: any) { + const { name, metadata } = this, + { keys } = metadata, + r2 = env[name] as R2Bucket; + await r2.delete(keys); + return new Response(); + } + receive(response: Response): Promise { + return Promise.resolve(); + } +} diff --git a/src/proxies/r2/proxy_holder.ts b/src/proxies/r2/proxy_holder.ts index 374e3b0..c41258f 100644 --- a/src/proxies/r2/proxy_holder.ts +++ b/src/proxies/r2/proxy_holder.ts @@ -1,4 +1,5 @@ import { ProxyHolder } from '../proxy'; +import { R2DeleteProxy } from './delete/proxy'; import { R2GetProxy } from './get/proxy'; import { R2PutProxy } from './put/proxy'; @@ -81,7 +82,9 @@ export class R2ProxyHolder extends ProxyHolder<{}> implements R2Bucket { options?: unknown ): Promise | Promise { const data = - typeof value === 'string' || value instanceof ArrayBuffer + typeof value === 'string' || + value instanceof ArrayBuffer || + value instanceof Buffer ? new ReadableStream({ start(controller) { controller.enqueue(value); @@ -115,7 +118,9 @@ export class R2ProxyHolder extends ProxyHolder<{}> implements R2Bucket { throw new Error('Method not implemented.'); } delete(keys: string | string[]): Promise { - throw new Error('Method not implemented.'); + const { host, name } = this, + proxy = new R2DeleteProxy({ host, name, metadata: { keys } }); + return proxy.post(); } list(options?: R2ListOptions | undefined): Promise { throw new Error('Method not implemented.');