From c786e1bc55e4460c8944ba11abfe21bacbcf203f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 29 Apr 2024 15:32:18 -0500 Subject: [PATCH] Uploader: make second argument an options object --- src/uploaders/config.ts | 8 ++++---- src/uploaders/ipfs.ts | 8 ++++---- src/uploaders/s3.ts | 6 ++---- src/uploaders/types.ts | 4 ++-- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/uploaders/config.ts b/src/uploaders/config.ts index 2ee2f9ab..83874f6e 100644 --- a/src/uploaders/config.ts +++ b/src/uploaders/config.ts @@ -7,11 +7,11 @@ import type { Uploader } from './types.ts'; /** Meta-uploader determined from configuration. */ const configUploader: Uploader = { - upload(file, signal) { - return uploader().upload(file, signal); + upload(file, opts) { + return uploader().upload(file, opts); }, - delete(cid, signal) { - return uploader().delete(cid, signal); + delete(cid, opts) { + return uploader().delete(cid, opts); }, }; diff --git a/src/uploaders/ipfs.ts b/src/uploaders/ipfs.ts index 5d82e2d5..6e0e0e7e 100644 --- a/src/uploaders/ipfs.ts +++ b/src/uploaders/ipfs.ts @@ -18,7 +18,7 @@ const ipfsAddResponseSchema = z.object({ * and upload the file using the REST API. */ const ipfsUploader: Uploader = { - async upload(file, signal) { + async upload(file, opts) { const url = new URL('/api/v0/add', Conf.ipfs.apiUrl); const formData = new FormData(); @@ -27,7 +27,7 @@ const ipfsUploader: Uploader = { const response = await fetchWorker(url, { method: 'POST', body: formData, - signal, + signal: opts?.signal, }); const { Hash } = ipfsAddResponseSchema.parse(await response.json()); @@ -36,7 +36,7 @@ const ipfsUploader: Uploader = { cid: Hash, }; }, - async delete(cid, signal) { + async delete(cid, opts) { const url = new URL('/api/v0/pin/rm', Conf.ipfs.apiUrl); const query = new URLSearchParams(); @@ -46,7 +46,7 @@ const ipfsUploader: Uploader = { await fetchWorker(url, { method: 'POST', - signal, + signal: opts?.signal, }); }, }; diff --git a/src/uploaders/s3.ts b/src/uploaders/s3.ts index 2e02cc30..378b2790 100644 --- a/src/uploaders/s3.ts +++ b/src/uploaders/s3.ts @@ -9,10 +9,9 @@ import type { Uploader } from './types.ts'; * take advantage of IPFS features while not really using IPFS. */ const s3Uploader: Uploader = { - async upload(file, _signal) { + async upload(file) { const cid = await IpfsHash.of(file.stream()) as string; - // FIXME: Can't cancel S3 requests: https://github.com/bradenmacdonald/deno-s3-lite-client/issues/24 await client().putObject(`ipfs/${cid}`, file.stream(), { metadata: { 'Content-Type': file.type, @@ -24,8 +23,7 @@ const s3Uploader: Uploader = { cid, }; }, - async delete(cid, _signal) { - // FIXME: Can't cancel S3 requests: https://github.com/bradenmacdonald/deno-s3-lite-client/issues/24 + async delete(cid) { await client().deleteObject(`ipfs/${cid}`); }, }; diff --git a/src/uploaders/types.ts b/src/uploaders/types.ts index 8f115459..88980483 100644 --- a/src/uploaders/types.ts +++ b/src/uploaders/types.ts @@ -1,9 +1,9 @@ /** Modular uploader interface, to support uploading to different backends. */ interface Uploader { /** Upload the file to the backend. */ - upload(file: File, signal?: AbortSignal): Promise; + upload(file: File, opts?: { signal?: AbortSignal }): Promise; /** Delete the file from the backend. */ - delete(cid: string, signal?: AbortSignal): Promise; + delete(cid: string, opts?: { signal?: AbortSignal }): Promise; } /** Return value from the uploader after uploading a file. */