106 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-08-15 12:08:51 +02:00
import { Api } from "../api/config";
import { Fetch } from "../fetch-safe/fetch-safe";
import type { SafeValue } from "../values/values";
2024-08-30 12:24:00 +02:00
import type {
CodexDataResponse,
2024-10-23 16:53:40 +02:00
CodexManifest,
2024-08-30 12:24:00 +02:00
CodexNodeSpace,
2025-03-26 12:31:10 +01:00
UploadStategy,
2024-10-25 11:10:13 +02:00
NetworkDownloadResponse,
2024-08-30 12:24:00 +02:00
UploadResponse,
} from "./types";
2024-08-15 12:08:51 +02:00
2024-08-29 19:41:28 +02:00
export class CodexData {
2024-08-15 12:08:51 +02:00
readonly url: string;
constructor(url: string) {
this.url = url;
}
/**
* Lists manifest CIDs stored locally in node.
* TODO: remove the faker data part when the api is ready
*/
cids(): Promise<SafeValue<CodexDataResponse>> {
const url = this.url + Api.config.prefix + "/data";
2025-03-26 12:21:51 +01:00
return Fetch.safeJson<CodexDataResponse>(url, { method: "GET" }).then(
(data) => {
if (data.error) {
return data;
}
2024-08-15 12:08:51 +02:00
2025-03-26 12:21:51 +01:00
return { error: false, data: { content: data.data.content } };
}
);
2024-08-15 12:08:51 +02:00
}
/**
* Gets a summary of the storage space allocation of the node.
*/
space() {
const url = this.url + Api.config.prefix + "/space";
2025-03-26 12:21:51 +01:00
return Fetch.safeJson<CodexNodeSpace>(url, { method: "GET" });
2024-08-15 12:08:51 +02:00
}
/**
* Upload a file in a streaming manner.
* Once completed, the file is stored in the node and can be retrieved by any node in the network using the returned CID.
* XMLHttpRequest is used instead of fetch for this case, to obtain progress information.
* A callback onProgress can be passed to receive upload progress data information.
*/
2025-03-26 12:31:10 +01:00
upload(stategy: UploadStategy): UploadResponse {
2024-08-15 12:08:51 +02:00
const url = this.url + Api.config.prefix + "/data";
return {
2025-03-26 12:21:51 +01:00
result: stategy.download(url),
2024-08-15 12:08:51 +02:00
abort: () => {
2025-03-26 12:21:51 +01:00
stategy.abort();
2024-08-15 12:08:51 +02:00
},
};
}
/**
* Download a file from the local node in a streaming manner.
* If the file is not available locally, a 404 is returned.
*/
async localDownload(cid: string): Promise<SafeValue<Response>> {
2024-08-15 12:08:51 +02:00
const url = this.url + Api.config.prefix + "/data/" + cid;
2025-03-26 12:21:51 +01:00
return Fetch.safe(url, { method: "GET" });
2024-08-15 12:08:51 +02:00
}
/**
* 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.
*/
2025-03-26 12:21:51 +01:00
async networkDownload(
cid: string
): Promise<SafeValue<NetworkDownloadResponse>> {
2024-08-15 12:08:51 +02:00
const url = this.url + Api.config.prefix + `/data/${cid}/network`;
2025-03-26 12:21:51 +01:00
return Fetch.safeJson(url, { method: "POST" });
2024-10-25 11:10:13 +02:00
}
/**
2025-03-26 12:21:51 +01:00
* Download a file from the network in a streaming manner.
2024-10-25 11:10:13 +02:00
* 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<Response>> {
2024-10-25 11:10:13 +02:00
const url = this.url + Api.config.prefix + `/data/${cid}/network/stream`;
2025-03-26 12:21:51 +01:00
return Fetch.safe(url, { method: "GET" });
2024-08-15 12:08:51 +02:00
}
2024-10-23 16:53:40 +02:00
/**
2025-03-26 12:21:51 +01:00
* Download only the dataset manifest from the network to the local node
* if it's not available locally.
2024-10-23 16:53:40 +02:00
*/
async fetchManifest(cid: string) {
const url = this.url + Api.config.prefix + `/data/${cid}/network/manifest`;
2025-03-26 12:21:51 +01:00
return Fetch.safeJson<CodexManifest>(url, { method: "GET" });
2024-10-23 16:53:40 +02:00
}
2024-08-15 12:08:51 +02:00
}