diff --git a/README.md b/README.md index f90540b..6bce562 100644 --- a/README.md +++ b/README.md @@ -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 | 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: diff --git a/package-lock.json b/package-lock.json index 5caee91..7a17255 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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" diff --git a/package.json b/package.json index 0ee745e..3e2e699 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/data/data.ts b/src/data/data.ts index 2179c76..0c19523 100644 --- a/src/data/data.ts +++ b/src/data/data.ts @@ -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 | 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 | null>> { + async networkDownload(cid: string): Promise> { 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 | 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; diff --git a/src/data/types.ts b/src/data/types.ts index 4f8c1ee..fa04eba 100644 --- a/src/data/types.ts +++ b/src/data/types.ts @@ -86,3 +86,9 @@ export type UploadResponse = { result: Promise>; abort: () => void; }; + + +export type NetworkDownloadResponse = { + cid: string + manifest: CodexManifest +} \ No newline at end of file