Compare commits

...

3 Commits

Author SHA1 Message Date
semantic-release-bot 716a6e37d3 chore(release): 0.2.0-develop.4 [skip ci]
# [0.2.0-develop.4](https://git.lumeweb.com/LumeWeb/libportal/compare/v0.2.0-develop.3...v0.2.0-develop.4) (2023-06-23)

### Bug Fixes

* do conditional node checks and dynamic imports ([4a90fb0](4a90fb0f6e))
2023-06-23 12:53:36 +00:00
Derrick Hammer a82436f1c0
Merge remote-tracking branch 'origin/develop' into develop
# Conflicts:
#	npm-shrinkwrap.json
2023-06-23 08:51:43 -04:00
Derrick Hammer 4a90fb0f6e
fix: do conditional node checks and dynamic imports 2023-06-23 08:51:03 -04:00
3 changed files with 30656 additions and 30788 deletions

61382
npm-shrinkwrap.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@lumeweb/libportal",
"version": "0.2.0-develop.3",
"version": "0.2.0-develop.4",
"main": "lib/index.js",
"module": "lib/index.mjs",
"types": "lib/index.d.ts",
@ -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",

View File

@ -2,13 +2,7 @@ import { ed25519 as ed } from "@noble/curves/ed25519";
import * as edUtils from "@noble/curves/abstract/utils";
import { RegisterRequest } from "./requests/account.js";
import fetch, {
FormData,
Blob,
RequestInit,
Response,
HeadersInit,
} from "node-fetch";
import fetch, { Response } from "cross-fetch";
import {
LoginRequest,
LogoutRequest,
@ -27,12 +21,14 @@ 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;
@ -211,7 +207,9 @@ export class Client {
if (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.body = JSON.stringify(fetchOptions.body);
}
@ -312,7 +310,10 @@ export class Client {
stream = await streamToBlob(stream);
}
if (stream instanceof NodeReadableStream) {
let NodeReadableStream =
(await this.getNodeReadableObject()) as NodeReadableStreamType;
if (NodeReadableStream && stream instanceof NodeReadableStream) {
let data = new Uint8Array();
for await (const chunk of stream) {
data = Uint8Array.from([...data, ...chunk]);
@ -325,11 +326,16 @@ export class Client {
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");
}
const formData = new FormData();
const _FormData = await this.getFormDataObject();
const formData = new _FormData();
formData.set("file", stream as Blob);
const response = await this.post<UploadResponse>(
@ -373,7 +379,10 @@ export class Client {
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);
}
@ -386,7 +395,7 @@ export class Client {
if (
!(stream instanceof ReadableStreamDefaultReader) &&
!(stream instanceof Blob) &&
!(stream instanceof NodeReadableStream)
!(NodeReadableStream && stream instanceof NodeReadableStream)
) {
throw new Error("Invalid stream");
}
@ -476,7 +485,10 @@ export class Client {
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({});
for await (const chunk of stream) {
@ -494,4 +506,20 @@ 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;
}
}