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