Skip to content

Commit

Permalink
fix: improve stream detection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lushu authored and bcoe committed Sep 6, 2019
1 parent 70f1217 commit 6c41537
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
7 changes: 2 additions & 5 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<T>(
opts: GaxiosOptions,
res: Response | NodeFetchResponse,
Expand Down

0 comments on commit 6c41537

Please sign in to comment.