Add networkDownloadStream method

This commit is contained in:
Arnaud 2024-10-25 11:10:13 +02:00
parent 0747eba9d9
commit c477f793cb
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
5 changed files with 41 additions and 8 deletions

View File

@ -294,6 +294,21 @@ const cid = "QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N";
const manifest = await data.fetchManifest(cid);
```
#### networkDownloadStream
Download a file from the network in a streaming manner.
If the file is not available locally, it will be retrieved from other nodes in the network if able.
- cid (string, required)
- returns ReadableStream<Uint8Array> | null
Example:
```js
const cid = "QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N";
const result = await data.networkDownloadStream(cid);
```
### Debug
The following API assume that you have already a node module loaded, example:

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@codex-storage/sdk-js",
"version": "0.0.10",
"version": "0.0.11",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@codex-storage/sdk-js",
"version": "0.0.10",
"version": "0.0.11",
"license": "MIT",
"dependencies": {
"valibot": "^0.32.0"

View File

@ -1,6 +1,6 @@
{
"name": "@codex-storage/sdk-js",
"version": "0.0.10",
"version": "0.0.11",
"description": "Codex SDK to interact with the Codex decentralized storage network.",
"repository": {
"type": "git",

View File

@ -6,6 +6,7 @@ import type {
CodexDataResponse,
CodexManifest,
CodexNodeSpace,
NetworkDownloadResponse,
UploadResponse,
} from "./types";
@ -107,9 +108,8 @@ export class CodexData {
/**
* Download a file from the local node in a streaming manner.
* If the file is not available locally, a 404 is returned.
* There result is a readable stream.
*/
async localDownload(cid: string) {
async localDownload(cid: string): Promise<SafeValue<ReadableStream<Uint8Array> | null>> {
const url = this.url + Api.config.prefix + "/data/" + cid;
const res = await Fetch.safe(url, {
@ -123,22 +123,34 @@ export class CodexData {
return res;
}
return res.data.body;
return { error: false, data: res.data.body };
}
/**
* Download a file from the network in a streaming manner.
* If the file is not available locally, it will be retrieved from other nodes in the network if able.
*/
async networkDownload(cid: string): Promise<SafeValue<ReadableStream<Uint8Array> | null>> {
async networkDownload(cid: string): Promise<SafeValue<NetworkDownloadResponse>> {
const url = this.url + Api.config.prefix + `/data/${cid}/network`;
const res = await Fetch.safe(url, {
return Fetch.safeJson(url, {
method: "POST",
headers: {
"content-type": "application/octet-stream",
},
});
}
/**
* Download a file from the network in a streaming manner.
* If the file is not available locally, it will be retrieved from other nodes in the network if able.
*/
async networkDownloadStream(cid: string): Promise<SafeValue<ReadableStream<Uint8Array> | null>> {
const url = this.url + Api.config.prefix + `/data/${cid}/network/stream`;
const res = await Fetch.safe(url, {
method: "GET"
});
if (res.error) {
return res;

View File

@ -86,3 +86,9 @@ export type UploadResponse = {
result: Promise<SafeValue<string>>;
abort: () => void;
};
export type NetworkDownloadResponse = {
cid: string
manifest: CodexManifest
}