fix: refactor how we process a nodejs stream, as the current approach is extremely slow and wasteful. We need to do a bit of macgyvering and convert it via pipe to a passthrough so it passes a typeof check for Stream, then import it to form-data Response, and request a blob

This commit is contained in:
Derrick Hammer 2023-08-09 06:11:18 -04:00
parent d43f43936b
commit ae35797a25
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 19 additions and 6 deletions

View File

@ -29,6 +29,7 @@ import isNode from "detect-node";
import { utf8ToBytes } from "@noble/curves/abstract/utils";
type NodeReadableStreamType = typeof import("stream").Readable;
type NodePassThroughStreamType = typeof import("stream").PassThrough;
export interface ClientOptions {
portalUrl: string;
@ -340,13 +341,14 @@ export class Client {
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]);
}
let NodePassThroughStream =
(await this.getNodePassThroughObject()) as NodePassThroughStreamType;
stream = data;
if (NodeReadableStream && stream instanceof NodeReadableStream) {
const Response = await this.getFetchResponseObject();
stream = await new Response(
stream.pipe(new NodePassThroughStream()) as any,
).blob();
}
if (stream instanceof Uint8Array) {
@ -541,6 +543,7 @@ export class Client {
return FormData;
}
private async getBlobObject() {
if (isNode) {
return (await import("node-fetch")).Blob;
@ -556,6 +559,15 @@ export class Client {
return undefined;
}
private async getNodePassThroughObject() {
if (isNode) {
return (await import("stream")).PassThrough;
}
return undefined;
}
private async getFetchObject() {
if (isNode) {
return (await import("node-fetch")).default;
@ -563,6 +575,7 @@ export class Client {
return fetch;
}
private async getFetchResponseObject() {
if (isNode) {
return (await import("node-fetch")).Response;