Skip to content

Commit

Permalink
r2 delete
Browse files Browse the repository at this point in the history
  • Loading branch information
chientrm committed Aug 23, 2023
1 parent 7a5863d commit fb5bd34
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ import { connectR2 } from 'wrangler-proxy';
| Function | Status |
| ------------------------- | ------ |
| `head()` ||
| `get()` | 🟡 |
| `put()` | 🟡 |
| `get()` | |
| `put()` | |
| `createMultipartUpload()` ||
| `resumeMultipartUpload()` ||
| `delete()` | |
| `delete()` | |
| `list()` ||

### `waitUntil`
Expand Down
3 changes: 3 additions & 0 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.');
}
Expand Down
31 changes: 31 additions & 0 deletions src/proxies/r2/delete/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Proxy } from '../../proxy';

interface Metadata {
keys: string | string[];
}

export class R2DeleteProxy extends Proxy<Metadata> {
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<any> {
return Promise.resolve();
}
}
9 changes: 7 additions & 2 deletions src/proxies/r2/proxy_holder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ProxyHolder } from '../proxy';
import { R2DeleteProxy } from './delete/proxy';
import { R2GetProxy } from './get/proxy';
import { R2PutProxy } from './put/proxy';

Expand Down Expand Up @@ -81,7 +82,9 @@ export class R2ProxyHolder extends ProxyHolder<{}> implements R2Bucket {
options?: unknown
): Promise<R2Object | null> | Promise<R2Object> {
const data =
typeof value === 'string' || value instanceof ArrayBuffer
typeof value === 'string' ||
value instanceof ArrayBuffer ||
value instanceof Buffer
? new ReadableStream({
start(controller) {
controller.enqueue(value);
Expand Down Expand Up @@ -115,7 +118,9 @@ export class R2ProxyHolder extends ProxyHolder<{}> implements R2Bucket {
throw new Error('Method not implemented.');
}
delete(keys: string | string[]): Promise<void> {
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<R2Objects> {
throw new Error('Method not implemented.');
Expand Down

0 comments on commit fb5bd34

Please sign in to comment.