Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add head bytes endpoint #941

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
PublicKey,
RedistributionState,
Reference,
ReferenceInformation,
RemovePeerResponse,
ReserveState,
SOCReader,
Expand Down Expand Up @@ -207,6 +208,18 @@
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<ReferenceInformation> {
assertRequestOptions(options)
assertReferenceOrEns(reference)

return bytes.head(this.getRequestOptionsForCall(options), reference)
}

/**
* Download data as a byte array
*
Expand Down Expand Up @@ -346,7 +359,7 @@
ReferenceType.MANIFEST,
)
} else if (isReadable(data) && options?.tag && !options.size) {
// TODO: Needed until https://github.com/ethersphere/bee/issues/2317 is resolved

Check warning on line 362 in src/bee.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Unexpected 'todo' comment: 'TODO: Needed until...'
const result = await bzz.uploadFile(
this.getRequestOptionsForCall(requestOptions),
data,
Expand Down Expand Up @@ -724,7 +737,7 @@
await this.makeFeedReader(type, canonicalTopic, canonicalOwner).download()

return true
} catch (e: any) {

Check warning on line 740 in src/bee.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Unexpected any. Specify a different type
if (e?.response?.status === 404) {
return false
}
Expand Down Expand Up @@ -1818,7 +1831,7 @@
if (stamp.usable) {
return
}
} catch (error: any) {}

Check warning on line 1834 in src/bee.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Unexpected any. Specify a different type

Check warning on line 1834 in src/bee.ts

View workflow job for this annotation

GitHub Actions / check (16.x)

Empty block statement

await System.sleepMillis(TIME_STEP)
}
Expand Down
19 changes: 19 additions & 0 deletions src/modules/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Data,
DownloadRedundancyOptions,
Reference,
ReferenceInformation,
ReferenceOrEns,
UploadOptions,
UploadRedundancyOptions,
Expand Down Expand Up @@ -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<ReferenceInformation> {
const response = await http<void>(requestOptions, {
url: `${endpoint}/${hash}`,
method: 'head',
responseType: 'json',
})

return {
contentLength: parseInt(response.headers['content-length'] as string),
}
}

/**
* Download data as a byte array
*
Expand Down
4 changes: 4 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading