mirror of
https://github.com/logos-storage/logos-storage-js.git
synced 2026-01-02 13:33:07 +00:00
Add networkDownloadStream method
This commit is contained in:
parent
0747eba9d9
commit
c477f793cb
15
README.md
15
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<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
4
package-lock.json
generated
@ -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"
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -86,3 +86,9 @@ export type UploadResponse = {
|
||||
result: Promise<SafeValue<string>>;
|
||||
abort: () => void;
|
||||
};
|
||||
|
||||
|
||||
export type NetworkDownloadResponse = {
|
||||
cid: string
|
||||
manifest: CodexManifest
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user