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