From 6c4153748df9c46e27f8cd551c2f65d707c68d9b Mon Sep 17 00:00:00 2001 From: Shu Lu Date: Sat, 7 Sep 2019 03:51:51 +1200 Subject: [PATCH] fix: improve stream detection logic --- package.json | 1 + src/gaxios.ts | 7 ++----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index b2068814..36960465 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,7 @@ "abort-controller": "^3.0.0", "extend": "^3.0.2", "https-proxy-agent": "^2.2.1", + "is-stream": "^2.0.0", "node-fetch": "^2.3.0" } } diff --git a/src/gaxios.ts b/src/gaxios.ts index 5541e56a..aba2fe7c 100644 --- a/src/gaxios.ts +++ b/src/gaxios.ts @@ -16,6 +16,7 @@ import {Agent} from 'https'; import nodeFetch, {Response as NodeFetchResponse} from 'node-fetch'; import qs from 'querystring'; import stream from 'stream'; +import isStream from 'is-stream'; import url from 'url'; import { @@ -183,7 +184,7 @@ export class Gaxios { opts.headers = opts.headers || {}; if (opts.data) { - if (this.isReadableStream(opts.data)) { + if (isStream.readable(opts.data)) { opts.body = opts.data; } else if (typeof opts.data === 'object') { opts.body = JSON.stringify(opts.data); @@ -229,10 +230,6 @@ export class Gaxios { return qs.stringify(params); } - private isReadableStream(obj: any): boolean { - return obj instanceof stream.Readable && typeof obj._read === 'function'; - } - private translateResponse( opts: GaxiosOptions, res: Response | NodeFetchResponse,