@codex-storage/sdk-js
Version:
Codex SDK to interact with the Codex decentralized storage network.
103 lines (101 loc) • 3.2 kB
JavaScript
import { Api } from './chunk-JUEFSJIF.mjs';
import { Fetch, FetchAuthBuilder } from './chunk-2VOCE3TZ.mjs';
// src/data/data.ts
var CodexData = class {
url;
auth = {};
constructor(url, options) {
this.url = url;
if (options == null ? void 0 : options.auth) {
this.auth = options.auth;
}
}
/**
* Lists manifest CIDs stored locally in node.
* TODO: remove the faker data part when the api is ready
*/
cids() {
const url = this.url + Api.config.prefix + "/data";
return Fetch.safeJson(url, {
method: "GET",
headers: FetchAuthBuilder.build(this.auth)
}).then((data) => {
if (data.error) {
return data;
}
return { error: false, data: { content: data.data.content } };
});
}
/**
* Gets a summary of the storage space allocation of the node.
*/
space() {
const url = this.url + Api.config.prefix + "/space";
return Fetch.safeJson(url, {
method: "GET",
headers: FetchAuthBuilder.build(this.auth)
});
}
/**
* 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.
*/
upload(stategy) {
const url = this.url + Api.config.prefix + "/data";
return {
result: stategy.upload(url, { auth: this.auth }),
abort: () => {
stategy.abort();
}
};
}
/**
* 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) {
const url = this.url + Api.config.prefix + "/data/" + cid;
return Fetch.safe(url, {
method: "GET",
headers: FetchAuthBuilder.build(this.auth)
});
}
/**
* Download a file from the network to the local node if it's not available locally.
* Note: Download is performed async. Call can return before download is completed.
*/
async networkDownload(cid) {
const url = this.url + Api.config.prefix + `/data/${cid}/network`;
return Fetch.safeJson(url, {
method: "POST",
headers: FetchAuthBuilder.build(this.auth)
});
}
/**
* 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) {
const url = this.url + Api.config.prefix + `/data/${cid}/network/stream`;
return Fetch.safe(url, {
method: "GET",
headers: FetchAuthBuilder.build(this.auth)
});
}
/**
* Download only the dataset manifest from the network to the local node
* if it's not available locally.
*/
async fetchManifest(cid) {
const url = this.url + Api.config.prefix + `/data/${cid}/network/manifest`;
return Fetch.safeJson(url, {
method: "GET",
headers: FetchAuthBuilder.build(this.auth)
});
}
};
export { CodexData };
//# sourceMappingURL=chunk-HDKK24O2.mjs.map
//# sourceMappingURL=chunk-HDKK24O2.mjs.map