mirror of
https://github.com/logos-storage/logos-storage-js.git
synced 2026-01-07 16:03:06 +00:00
parent
6b04d115c1
commit
59837ece13
12
README.md
12
README.md
@ -313,7 +313,7 @@ Upload a file in a streaming manner
|
|||||||
|
|
||||||
#### Browser
|
#### Browser
|
||||||
|
|
||||||
- stategy [BrowserUploadStategy](./src/data/browser-upload.ts#L5)
|
- strategy [BrowserUploadStrategy](./src/data/browser-upload.ts#L5)
|
||||||
- returns [UploadResponse](./src/data/types.ts#L17)
|
- returns [UploadResponse](./src/data/types.ts#L17)
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
@ -327,9 +327,9 @@ const onProgress = (loaded, total) => {
|
|||||||
|
|
||||||
const metadata = { filename: "foo.xt", mimetype: "text/plain" };
|
const metadata = { filename: "foo.xt", mimetype: "text/plain" };
|
||||||
|
|
||||||
const stategy = new BrowserUploadStategy(file, onProgress, metadata);
|
const strategy = new BrowserUploadStrategy(file, onProgress, metadata);
|
||||||
|
|
||||||
const uploadResponse = data.upload(stategy);
|
const uploadResponse = data.upload(strategy);
|
||||||
|
|
||||||
const res = await uploadResponse.result;
|
const res = await uploadResponse.result;
|
||||||
|
|
||||||
@ -343,14 +343,14 @@ console.info("CID is", res.data);
|
|||||||
|
|
||||||
#### Node
|
#### Node
|
||||||
|
|
||||||
- stategy [NodeUploadStategy](./src/data/node-upload.ts#L9)
|
- strategy [NodeUploadStrategy](./src/data/node-upload.ts#L9)
|
||||||
- returns [UploadResponse](./src/data/types.ts#L17)
|
- returns [UploadResponse](./src/data/types.ts#L17)
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const stategy = new NodeUploadStategy("Hello World !");
|
const strategy = new NodeUploadStrategy("Hello World !");
|
||||||
const uploadResponse = data.upload(stategy);
|
const uploadResponse = data.upload(strategy);
|
||||||
|
|
||||||
const res = await uploadResponse.result;
|
const res = await uploadResponse.result;
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { Codex } from "@codex-storage/sdk-js";
|
import { Codex } from "@codex-storage/sdk-js";
|
||||||
import { BrowserUploadStategy } from "@codex-storage/sdk-js/browser";
|
import { BrowserUploadStrategy } from "@codex-storage/sdk-js/browser";
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const codex = new Codex(process.env.CODEX_NODE_URL);
|
const codex = new Codex(process.env.CODEX_NODE_URL);
|
||||||
@ -19,9 +19,9 @@ async function main() {
|
|||||||
mimetype: "text/plain",
|
mimetype: "text/plain",
|
||||||
};
|
};
|
||||||
|
|
||||||
const stategy = new BrowserUploadStategy(file, onProgress, metadata);
|
const strategy = new BrowserUploadStrategy(file, onProgress, metadata);
|
||||||
|
|
||||||
const uploadResponse = data.upload(stategy);
|
const uploadResponse = data.upload(strategy);
|
||||||
|
|
||||||
const res = await uploadResponse.result;
|
const res = await uploadResponse.result;
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
const { Codex } = require("@codex-storage/sdk-js");
|
const { Codex } = require("@codex-storage/sdk-js");
|
||||||
const { NodeUploadStategy } = require("@codex-storage/sdk-js/node");
|
const { NodeUploadStrategy } = require("@codex-storage/sdk-js/node");
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const codex = new Codex(
|
const codex = new Codex(
|
||||||
@ -7,8 +7,8 @@ async function main() {
|
|||||||
);
|
);
|
||||||
const data = codex.data;
|
const data = codex.data;
|
||||||
|
|
||||||
const stategy = new NodeUploadStategy("Hello World !");
|
const strategy = new NodeUploadStrategy("Hello World !");
|
||||||
const uploadResponse = data.upload(stategy);
|
const uploadResponse = data.upload(strategy);
|
||||||
|
|
||||||
const res = await uploadResponse.result;
|
const res = await uploadResponse.result;
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import { CodexError } from "../errors/errors";
|
import { CodexError } from "../errors/errors";
|
||||||
import type { SafeValue } from "../values/values";
|
import type { SafeValue } from "../values/values";
|
||||||
import type { UploadStategy, UploadStategyOptions } from "./types";
|
import type { UploadStrategy, UploadStrategyOptions } from "./types";
|
||||||
|
|
||||||
export class BrowserUploadStategy implements UploadStategy {
|
export class BrowserUploadStrategy implements UploadStrategy {
|
||||||
private readonly file: Document | XMLHttpRequestBodyInit;
|
private readonly file: Document | XMLHttpRequestBodyInit;
|
||||||
private readonly onProgress:
|
private readonly onProgress:
|
||||||
| ((loaded: number, total: number) => void)
|
| ((loaded: number, total: number) => void)
|
||||||
@ -24,7 +24,7 @@ export class BrowserUploadStategy implements UploadStategy {
|
|||||||
|
|
||||||
upload(
|
upload(
|
||||||
url: string,
|
url: string,
|
||||||
{ auth }: UploadStategyOptions
|
{ auth }: UploadStrategyOptions
|
||||||
): Promise<SafeValue<string>> {
|
): Promise<SafeValue<string>> {
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
this.xhr = xhr;
|
this.xhr = xhr;
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { assert, describe, it } from "vitest";
|
import { assert, describe, it } from "vitest";
|
||||||
import { CodexData } from "./data";
|
import { CodexData } from "./data";
|
||||||
import { NodeUploadStategy } from "./node-upload";
|
import { NodeUploadStrategy } from "./node-upload";
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
|
|
||||||
describe("data", () => {
|
describe("data", () => {
|
||||||
@ -14,7 +14,7 @@ describe("data", () => {
|
|||||||
it("uploads a file a download it locally", async () => {
|
it("uploads a file a download it locally", async () => {
|
||||||
const content = crypto.randomBytes(16).toString("hex");
|
const content = crypto.randomBytes(16).toString("hex");
|
||||||
|
|
||||||
const strategy = new NodeUploadStategy(content);
|
const strategy = new NodeUploadStrategy(content);
|
||||||
const res = data.upload(strategy);
|
const res = data.upload(strategy);
|
||||||
const cid = await res.result;
|
const cid = await res.result;
|
||||||
assert.ok(cid.error == false);
|
assert.ok(cid.error == false);
|
||||||
@ -41,7 +41,7 @@ describe("data", () => {
|
|||||||
it("saves the metadata uploads provided during the upload", async () => {
|
it("saves the metadata uploads provided during the upload", async () => {
|
||||||
const content = crypto.randomBytes(16).toString("hex");
|
const content = crypto.randomBytes(16).toString("hex");
|
||||||
|
|
||||||
const strategy = new NodeUploadStategy(content, {
|
const strategy = new NodeUploadStrategy(content, {
|
||||||
filename: "hello.txt",
|
filename: "hello.txt",
|
||||||
mimetype: "text/plain",
|
mimetype: "text/plain",
|
||||||
});
|
});
|
||||||
@ -63,7 +63,7 @@ describe("data", () => {
|
|||||||
it("fails when providing wrong metadata", async () => {
|
it("fails when providing wrong metadata", async () => {
|
||||||
const content = crypto.randomBytes(16).toString("hex");
|
const content = crypto.randomBytes(16).toString("hex");
|
||||||
|
|
||||||
const strategy = new NodeUploadStategy(content, {
|
const strategy = new NodeUploadStrategy(content, {
|
||||||
filename: "hello.txt",
|
filename: "hello.txt",
|
||||||
mimetype: "plain/text",
|
mimetype: "plain/text",
|
||||||
});
|
});
|
||||||
@ -79,7 +79,7 @@ describe("data", () => {
|
|||||||
|
|
||||||
it("delete a file a locally", async () => {
|
it("delete a file a locally", async () => {
|
||||||
const content = "b".repeat(131072);
|
const content = "b".repeat(131072);
|
||||||
const strategy = new NodeUploadStategy(content);
|
const strategy = new NodeUploadStrategy(content);
|
||||||
const res = data.upload(strategy);
|
const res = data.upload(strategy);
|
||||||
const cid = await res.result;
|
const cid = await res.result;
|
||||||
assert.ok(cid.error == false);
|
assert.ok(cid.error == false);
|
||||||
@ -99,7 +99,7 @@ describe("data", () => {
|
|||||||
|
|
||||||
it("doesn't do anything when trying to delete a non existing cid", async () => {
|
it("doesn't do anything when trying to delete a non existing cid", async () => {
|
||||||
const content = crypto.randomBytes(16).toString("hex");
|
const content = crypto.randomBytes(16).toString("hex");
|
||||||
const strategy = new NodeUploadStategy(content);
|
const strategy = new NodeUploadStrategy(content);
|
||||||
const res = spData.upload(strategy);
|
const res = spData.upload(strategy);
|
||||||
const cid = await res.result;
|
const cid = await res.result;
|
||||||
assert.ok(cid.error == false);
|
assert.ok(cid.error == false);
|
||||||
@ -124,7 +124,7 @@ describe("data", () => {
|
|||||||
|
|
||||||
const usedBytes = space.data.quotaUsedBytes;
|
const usedBytes = space.data.quotaUsedBytes;
|
||||||
|
|
||||||
const strategy = new NodeUploadStategy(content);
|
const strategy = new NodeUploadStrategy(content);
|
||||||
const res = data.upload(strategy);
|
const res = data.upload(strategy);
|
||||||
const cid = await res.result;
|
const cid = await res.result;
|
||||||
assert.ok(cid.error == false);
|
assert.ok(cid.error == false);
|
||||||
@ -139,7 +139,7 @@ describe("data", () => {
|
|||||||
it("stream downloads a file on the network", async () => {
|
it("stream downloads a file on the network", async () => {
|
||||||
const content = crypto.randomBytes(16).toString("hex");
|
const content = crypto.randomBytes(16).toString("hex");
|
||||||
|
|
||||||
const strategy = new NodeUploadStategy(content);
|
const strategy = new NodeUploadStrategy(content);
|
||||||
const res = spData.upload(strategy);
|
const res = spData.upload(strategy);
|
||||||
const cid = await res.result;
|
const cid = await res.result;
|
||||||
assert.ok(cid.error == false);
|
assert.ok(cid.error == false);
|
||||||
@ -153,7 +153,7 @@ describe("data", () => {
|
|||||||
it("downloads a file on the network", async () => {
|
it("downloads a file on the network", async () => {
|
||||||
const content = crypto.randomBytes(16).toString("hex");
|
const content = crypto.randomBytes(16).toString("hex");
|
||||||
|
|
||||||
const strategy = new NodeUploadStategy(content);
|
const strategy = new NodeUploadStrategy(content);
|
||||||
const res = spData.upload(strategy);
|
const res = spData.upload(strategy);
|
||||||
const cid = await res.result;
|
const cid = await res.result;
|
||||||
assert.ok(cid.error == false);
|
assert.ok(cid.error == false);
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import {
|
|||||||
import type { SafeValue } from "../values/values";
|
import type { SafeValue } from "../values/values";
|
||||||
import type {
|
import type {
|
||||||
CodexDataResponse,
|
CodexDataResponse,
|
||||||
UploadStategy,
|
UploadStrategy,
|
||||||
UploadResponse,
|
UploadResponse,
|
||||||
CodexSpaceResponse,
|
CodexSpaceResponse,
|
||||||
CodexNodeSpace,
|
CodexNodeSpace,
|
||||||
@ -70,13 +70,13 @@ export class CodexData {
|
|||||||
* XMLHttpRequest is used instead of fetch for this case, to obtain progress information.
|
* 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.
|
* A callback onProgress can be passed to receive upload progress data information.
|
||||||
*/
|
*/
|
||||||
upload(stategy: UploadStategy): UploadResponse {
|
upload(strategy: UploadStrategy): UploadResponse {
|
||||||
const url = this.url + Api.config.prefix + "/data";
|
const url = this.url + Api.config.prefix + "/data";
|
||||||
|
|
||||||
return {
|
return {
|
||||||
result: stategy.upload(url, { auth: this.auth }),
|
result: strategy.upload(url, { auth: this.auth }),
|
||||||
abort: () => {
|
abort: () => {
|
||||||
stategy.abort();
|
strategy.abort();
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,10 +3,10 @@ import { CodexError } from "../errors/errors";
|
|||||||
import type { SafeValue } from "../values/values";
|
import type { SafeValue } from "../values/values";
|
||||||
import Undici from "undici";
|
import Undici from "undici";
|
||||||
import { type FormData } from "undici";
|
import { type FormData } from "undici";
|
||||||
import type { UploadStategy, UploadStategyOptions } from "./types";
|
import type { UploadStrategy, UploadStrategyOptions } from "./types";
|
||||||
import { FetchAuthBuilder } from "../fetch-safe/fetch-safe";
|
import { FetchAuthBuilder } from "../fetch-safe/fetch-safe";
|
||||||
|
|
||||||
export class NodeUploadStategy implements UploadStategy {
|
export class NodeUploadStrategy implements UploadStrategy {
|
||||||
private readonly body:
|
private readonly body:
|
||||||
| string
|
| string
|
||||||
| Buffer
|
| Buffer
|
||||||
@ -29,7 +29,7 @@ export class NodeUploadStategy implements UploadStategy {
|
|||||||
|
|
||||||
async upload(
|
async upload(
|
||||||
url: string,
|
url: string,
|
||||||
{ auth }: UploadStategyOptions
|
{ auth }: UploadStrategyOptions
|
||||||
): Promise<SafeValue<string>> {
|
): Promise<SafeValue<string>> {
|
||||||
const headers: Record<string, string> = FetchAuthBuilder.build(auth);
|
const headers: Record<string, string> = FetchAuthBuilder.build(auth);
|
||||||
|
|
||||||
|
|||||||
@ -29,14 +29,14 @@ export type CodexFetchManifestResponse =
|
|||||||
|
|
||||||
export type CodexManifest = CodexFetchManifestResponse;
|
export type CodexManifest = CodexFetchManifestResponse;
|
||||||
|
|
||||||
export type UploadStategyOptions = {
|
export type UploadStrategyOptions = {
|
||||||
auth?: FetchAuth;
|
auth?: FetchAuth;
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface UploadStategy {
|
export interface UploadStrategy {
|
||||||
upload(
|
upload(
|
||||||
url: string,
|
url: string,
|
||||||
options?: UploadStategyOptions
|
options?: UploadStrategyOptions
|
||||||
): Promise<SafeValue<string>>;
|
): Promise<SafeValue<string>>;
|
||||||
abort(): void;
|
abort(): void;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { assert, describe, it } from "vitest";
|
import { assert, describe, it } from "vitest";
|
||||||
import { CodexMarketplace } from "./marketplace";
|
import { CodexMarketplace } from "./marketplace";
|
||||||
import { CodexData } from "../data/data";
|
import { CodexData } from "../data/data";
|
||||||
import { NodeUploadStategy } from "../data/node-upload";
|
import { NodeUploadStrategy } from "../data/node-upload";
|
||||||
import type {
|
import type {
|
||||||
CodexAvailabilityPatchInput,
|
CodexAvailabilityPatchInput,
|
||||||
CodexCreateAvailabilityInput,
|
CodexCreateAvailabilityInput,
|
||||||
@ -154,7 +154,7 @@ describe("marketplace", async () => {
|
|||||||
|
|
||||||
async function uploadContent(sizeInBytes: number) {
|
async function uploadContent(sizeInBytes: number) {
|
||||||
const content = "a".repeat(sizeInBytes);
|
const content = "a".repeat(sizeInBytes);
|
||||||
const strategy = new NodeUploadStategy(content);
|
const strategy = new NodeUploadStrategy(content);
|
||||||
const res = data.upload(strategy);
|
const res = data.upload(strategy);
|
||||||
const cid = await res.result;
|
const cid = await res.result;
|
||||||
assert.ok(cid.error == false);
|
assert.ok(cid.error == false);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user