Skip to content

Commit e348732

Browse files
author
Bin Chang
committed
remove isStream to the common lib.
1 parent fd73ca8 commit e348732

File tree

4 files changed

+16
-24
lines changed

4 files changed

+16
-24
lines changed

lib/common.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ function formatQueryValue(key, value, options) {
306306
return [key, value];
307307
}
308308

309+
function isStream(obj) {
310+
return (typeof obj !== 'undefined') &&
311+
(typeof a !== 'string') &&
312+
(! Buffer.isBuffer(obj)) &&
313+
_.isFunction(obj.setEncoding);
314+
}
315+
309316
exports.normalizeRequestOptions = normalizeRequestOptions;
310317
exports.isBinaryBuffer = isBinaryBuffer;
311318
exports.mergeChunks = mergeChunks;
@@ -321,3 +328,5 @@ exports.percentEncode = percentEncode;
321328
exports.percentDecode = percentDecode;
322329
exports.matchStringOrRegexp = matchStringOrRegexp;
323330
exports.formatQueryValue = formatQueryValue;
331+
exports.isStream = isStream;
332+

lib/delayed_body.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ var Transform = require('stream').Transform;
1414
var EventEmitter = require('events').EventEmitter;
1515
var noop = function () {};
1616
var util = require('util');
17-
var timers = require('timers');
18-
19-
function isStream(obj) {
20-
var is = obj && (typeof a !== 'string') && (! Buffer.isBuffer(obj)) && (typeof obj.setEncoding === 'function');
21-
return is;
22-
}
17+
var common = require('./common');
2318

2419
if (!Transform) {
2520
// for barebones compatibility for node < 0.10
@@ -56,7 +51,7 @@ function DelayedBody(ms, body) {
5651
var data = '';
5752
var ended = false;
5853

59-
if (isStream(body)) {
54+
if (common.isStream(body)) {
6055
body.on('data', function (chunk) {
6156
data += Buffer.isBuffer(chunk) ? chunk.toString() : chunk;
6257
});
@@ -69,7 +64,7 @@ function DelayedBody(ms, body) {
6964
}
7065

7166
setTimeout(function () {
72-
if (isStream(body) && !ended) {
67+
if (common.isStream(body) && !ended) {
7368
body.once('end', function () {
7469
self.end(data);
7570
});

lib/interceptor.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Interceptor.prototype.reply = function reply(statusCode, body, headers) {
9999
if (body && typeof(body) !== 'string' &&
100100
typeof(body) !== 'function' &&
101101
!Buffer.isBuffer(body) &&
102-
!isStream(body)) {
102+
!common.isStream(body)) {
103103
try {
104104
body = stringify(body);
105105
if (!this.headers) {
@@ -496,10 +496,3 @@ Interceptor.prototype.socketDelay = function socketDelay(ms) {
496496
this.socketDelayInMs = ms;
497497
return this;
498498
};
499-
500-
function isStream(obj) {
501-
return (typeof obj !== 'undefined') &&
502-
(typeof a !== 'string') &&
503-
(! Buffer.isBuffer(obj)) &&
504-
_.isFunction(obj.setEncoding);
505-
}

lib/request_overrider.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ function setHeader(request, name, value) {
4444
}
4545
}
4646

47-
function isStream(obj) {
48-
var is = obj && (typeof obj !== 'string') && (!Buffer.isBuffer(obj)) && (typeof obj.setEncoding === 'function');
49-
return is;
50-
}
51-
5247
// Sets request headers of the given request. This is needed during both matching phase
5348
// (in case header filters were specified) and mocking phase (to correctly pass mocked
5449
// request headers).
@@ -317,7 +312,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
317312
// of response buffers which should be mocked one by one.
318313
// (otherwise decompressions after the first one fails as unzip expects to receive
319314
// buffer by buffer and not one single merged buffer)
320-
if(common.isContentEncoded(response.headers) && ! isStream(interceptor.body)) {
315+
if(common.isContentEncoded(response.headers) && ! common.isStream(interceptor.body)) {
321316

322317
if (interceptor.delayInMs) {
323318
emitError(new Error('Response delay is currently not supported with content-encoded responses.'));
@@ -400,7 +395,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
400395
responseBody = new DelayedBody(interceptor.delayInMs, responseBody);
401396
}
402397

403-
if (isStream(responseBody)) {
398+
if (common.isStream(responseBody)) {
404399
debug('response body is a stream');
405400
responseBody.pause();
406401
responseBody.on('data', function(d) {
@@ -492,7 +487,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
492487
req.emit('response', response);
493488
}
494489

495-
if (isStream(responseBody)) {
490+
if (common.isStream(responseBody)) {
496491
debug('resuming response stream');
497492
responseBody.resume();
498493
}

0 commit comments

Comments
 (0)