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