From 5f4c5006d4bdb5a466301558c00e79e431ec44cd Mon Sep 17 00:00:00 2001 From: Cafe137 Date: Thu, 8 Aug 2024 15:15:20 +0200 Subject: [PATCH] feat: add head bytes endpoint --- src/bee.ts | 13 +++++++++++++ src/modules/bytes.ts | 19 +++++++++++++++++++ src/types/index.ts | 4 ++++ 3 files changed, 36 insertions(+) diff --git a/src/bee.ts b/src/bee.ts index 42a29b97..482b9e6a 100644 --- a/src/bee.ts +++ b/src/bee.ts @@ -67,6 +67,7 @@ import type { PublicKey, RedistributionState, Reference, + ReferenceInformation, RemovePeerResponse, ReserveState, SOCReader, @@ -207,6 +208,18 @@ export class Bee { return bytes.upload(this.getRequestOptionsForCall(requestOptions), data, postageBatchId, options) } + /** + * Requests content length for a `/bytes` reference + * + * @see [Bee API reference - `HEAD /bytes/`](https://docs.ethswarm.org/api/#tag/Bytes/paths/~1bytes~1%7Breference%7D/head) + */ + async probeData(reference: ReferenceOrEns | string, options?: BeeRequestOptions): Promise { + assertRequestOptions(options) + assertReferenceOrEns(reference) + + return bytes.head(this.getRequestOptionsForCall(options), reference) + } + /** * Download data as a byte array * diff --git a/src/modules/bytes.ts b/src/modules/bytes.ts index 04ec5585..00b578e6 100644 --- a/src/modules/bytes.ts +++ b/src/modules/bytes.ts @@ -4,6 +4,7 @@ import type { Data, DownloadRedundancyOptions, Reference, + ReferenceInformation, ReferenceOrEns, UploadOptions, UploadRedundancyOptions, @@ -47,6 +48,24 @@ export async function upload( } } +/** + * Requests content length for a reference + * + * @param requestOptions Options for making requests + * @param hash Bee content reference + */ +export async function head(requestOptions: BeeRequestOptions, hash: ReferenceOrEns): Promise { + const response = await http(requestOptions, { + url: `${endpoint}/${hash}`, + method: 'head', + responseType: 'json', + }) + + return { + contentLength: parseInt(response.headers['content-length'] as string), + } +} + /** * Download data as a byte array * diff --git a/src/types/index.ts b/src/types/index.ts index 4450964c..10371ce1 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -323,6 +323,10 @@ export interface Pin { reference: string } +export interface ReferenceInformation { + contentLength: number +} + /** * Helper interface that adds utility functions * to work more conveniently with bytes in normal