@travetto/base
Advanced tools
Comparing version 3.2.1 to 3.2.2
{ | ||
"name": "@travetto/base", | ||
"version": "3.2.1", | ||
"version": "3.2.2", | ||
"description": "Environment config and common utilities for travetto applications.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -17,2 +17,10 @@ /// <reference types='node' /> | ||
interface ReadableStreamReader { | ||
read(): Promise<{ value?: Uint8Array, done?: boolean }> | ||
} | ||
interface ReadableStream { | ||
getReader(): ReadableStreamReader; | ||
} | ||
interface Options { | ||
@@ -57,9 +65,8 @@ writable?: boolean; | ||
blob(): Promise<Blob>; | ||
body: NodeJS.ReadableStream; | ||
formData(): FormData; | ||
body: ReadableStreamReader; | ||
bodyUsed: boolean; | ||
buffer(): Promise<Buffer>; | ||
json(): Promise<any>; | ||
size: number; | ||
text(): Promise<string>; | ||
textConverted(): Promise<string>; | ||
timeout: number; | ||
@@ -66,0 +73,0 @@ } |
@@ -9,4 +9,5 @@ import fs from 'fs/promises'; | ||
import type { ExecutionState } from './exec'; | ||
import type { _Fetch } from './fetch'; | ||
type All = Buffer | string | Readable | Uint8Array | NodeJS.ReadableStream; | ||
type All = Buffer | string | Readable | Uint8Array | NodeJS.ReadableStream | _Fetch.ReadableStream; | ||
@@ -19,2 +20,25 @@ /** | ||
/** | ||
* Convert a fetch/ReadableStream into a standard node Readable | ||
* @param src | ||
* @returns | ||
*/ | ||
static fetchBodyToStream(src: _Fetch.ReadableStream): Readable { | ||
const reader = src.getReader(); | ||
return new Readable({ | ||
read(): void { | ||
reader.read().then(({ done, value }) => { | ||
if (value) { | ||
this.push(value); | ||
} | ||
if (done) { | ||
this.emit('end'); | ||
} | ||
}); | ||
}, | ||
emitClose: true | ||
}); | ||
} | ||
/** | ||
* Convert buffer to a stream | ||
@@ -33,3 +57,6 @@ * @param src The buffer to stream | ||
*/ | ||
static async streamToBuffer(src: Readable | NodeJS.ReadableStream): Promise<Buffer> { | ||
static async streamToBuffer(src: Readable | NodeJS.ReadableStream | _Fetch.ReadableStream): Promise<Buffer> { | ||
if ('getReader' in src) { | ||
return this.streamToBuffer(this.fetchBodyToStream(src)); | ||
} | ||
return new Promise<Buffer>((res, rej) => { | ||
@@ -54,3 +81,3 @@ const data: Buffer[] = []; | ||
return Buffer.from(src); | ||
} else if (typeof src !== 'string' && 'pipe' in src) { | ||
} else if (typeof src !== 'string' && ('pipe' in src || 'getReader' in src)) { | ||
return this.streamToBuffer(src); | ||
@@ -70,2 +97,4 @@ } else { | ||
return src as Readable; | ||
} else if (typeof src !== 'string' && 'getReader' in src) { | ||
return this.fetchBodyToStream(src); | ||
} else { | ||
@@ -72,0 +101,0 @@ return this.bufferToStream(await this.toBuffer(src)); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107849
2387