Compare commits
No commits in common. "a82436f1c093b2dadb775faf95ddc99153b73f10" and "99e6500d09e10c5d26ca9c9ef7587de50f35b9d9" have entirely different histories.
a82436f1c0
...
99e6500d09
File diff suppressed because it is too large
Load Diff
|
@ -36,9 +36,9 @@
|
|||
"dependencies": {
|
||||
"@noble/curves": "^1.1.0",
|
||||
"@noble/hashes": "^1.3.1",
|
||||
"cross-fetch": "^3.1.6",
|
||||
"detect-node": "^2.1.0",
|
||||
"multiformats": "^11.0.2",
|
||||
"node-fetch": "^3.3.1",
|
||||
"p-defer": "^4.0.0",
|
||||
"stream-to-blob": "^2.0.1",
|
||||
"tus-js-client": "^3.1.0",
|
||||
|
|
|
@ -2,7 +2,13 @@ import { ed25519 as ed } from "@noble/curves/ed25519";
|
|||
import * as edUtils from "@noble/curves/abstract/utils";
|
||||
|
||||
import { RegisterRequest } from "./requests/account.js";
|
||||
import fetch, { Response } from "cross-fetch";
|
||||
import fetch, {
|
||||
FormData,
|
||||
Blob,
|
||||
RequestInit,
|
||||
Response,
|
||||
HeadersInit,
|
||||
} from "node-fetch";
|
||||
import {
|
||||
LoginRequest,
|
||||
LogoutRequest,
|
||||
|
@ -21,14 +27,12 @@ import streamToBlob from "stream-to-blob";
|
|||
import defer from "p-defer";
|
||||
import { blake3 } from "@noble/hashes/blake3";
|
||||
import { encodeCid } from "./cid.js";
|
||||
import { Readable as NodeReadableStream } from "stream";
|
||||
import {
|
||||
AuthStatusResponse,
|
||||
LoginResponse,
|
||||
PubkeyChallengeResponse,
|
||||
} from "./responses/auth.js";
|
||||
import isNode from "detect-node";
|
||||
|
||||
type NodeReadableStreamType = typeof import("stream").Readable;
|
||||
|
||||
export interface ClientOptions {
|
||||
portalUrl: string;
|
||||
|
@ -207,9 +211,7 @@ export class Client {
|
|||
if (options.data) {
|
||||
fetchOptions.body = options.data;
|
||||
|
||||
const _FormData = await this.getFormDataObject();
|
||||
|
||||
if (!(fetchOptions.body instanceof _FormData)) {
|
||||
if (!(fetchOptions.body instanceof FormData)) {
|
||||
fetchOptions.headers["Content-Type"] = "application/json";
|
||||
fetchOptions.body = JSON.stringify(fetchOptions.body);
|
||||
}
|
||||
|
@ -310,10 +312,7 @@ export class Client {
|
|||
stream = await streamToBlob(stream);
|
||||
}
|
||||
|
||||
let NodeReadableStream =
|
||||
(await this.getNodeReadableObject()) as NodeReadableStreamType;
|
||||
|
||||
if (NodeReadableStream && stream instanceof NodeReadableStream) {
|
||||
if (stream instanceof NodeReadableStream) {
|
||||
let data = new Uint8Array();
|
||||
for await (const chunk of stream) {
|
||||
data = Uint8Array.from([...data, ...chunk]);
|
||||
|
@ -326,16 +325,11 @@ export class Client {
|
|||
stream = new Blob([Buffer.from(stream)]);
|
||||
}
|
||||
|
||||
if (
|
||||
!(stream instanceof Blob) &&
|
||||
!(NodeReadableStream && stream instanceof NodeReadableStream)
|
||||
) {
|
||||
if (!(stream instanceof Blob) && !(stream instanceof NodeReadableStream)) {
|
||||
throw new Error("Invalid stream");
|
||||
}
|
||||
|
||||
const _FormData = await this.getFormDataObject();
|
||||
|
||||
const formData = new _FormData();
|
||||
const formData = new FormData();
|
||||
formData.set("file", stream as Blob);
|
||||
|
||||
const response = await this.post<UploadResponse>(
|
||||
|
@ -379,10 +373,7 @@ export class Client {
|
|||
hash = await this.computeHash(hashStream);
|
||||
}
|
||||
|
||||
let NodeReadableStream =
|
||||
(await this.getNodeReadableObject()) as NodeReadableStreamType;
|
||||
|
||||
if (NodeReadableStream && stream instanceof NodeReadableStream) {
|
||||
if (stream instanceof NodeReadableStream) {
|
||||
hash = await this.computeHash(hashStream);
|
||||
}
|
||||
|
||||
|
@ -395,7 +386,7 @@ export class Client {
|
|||
if (
|
||||
!(stream instanceof ReadableStreamDefaultReader) &&
|
||||
!(stream instanceof Blob) &&
|
||||
!(NodeReadableStream && stream instanceof NodeReadableStream)
|
||||
!(stream instanceof NodeReadableStream)
|
||||
) {
|
||||
throw new Error("Invalid stream");
|
||||
}
|
||||
|
@ -485,10 +476,7 @@ export class Client {
|
|||
return edUtils.bytesToHex(hasher.digest());
|
||||
}
|
||||
|
||||
let NodeReadableStream =
|
||||
(await this.getNodeReadableObject()) as NodeReadableStreamType;
|
||||
|
||||
if (NodeReadableStream && stream instanceof NodeReadableStream) {
|
||||
if (stream instanceof NodeReadableStream) {
|
||||
const hasher = blake3.create({});
|
||||
|
||||
for await (const chunk of stream) {
|
||||
|
@ -506,20 +494,4 @@ export class Client {
|
|||
|
||||
throw new Error("Invalid stream");
|
||||
}
|
||||
|
||||
private async getFormDataObject() {
|
||||
if (isNode) {
|
||||
return (await import("node-fetch")).FormData;
|
||||
}
|
||||
|
||||
return FormData;
|
||||
}
|
||||
|
||||
private async getNodeReadableObject() {
|
||||
if (isNode) {
|
||||
return (await import("stream")).Readable;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue