Skip to content

Commit 3462f9a

Browse files
committed
An alternate implementation that throws errors when the underlying implementation doesn’t support callbacks.
1 parent 9d55cfd commit 3462f9a

File tree

2 files changed

+87
-24
lines changed

2 files changed

+87
-24
lines changed

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ module.exports.filter = shouldCompress
3636
*/
3737

3838
var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
39-
var nodeHasCallbacks = (http.OutgoingMessage.prototype.write.length === 3 && http.OutgoingMessage.prototype.end.length === 3)
4039

4140
/**
4241
* Compress response data with gzip / deflate.
@@ -67,6 +66,9 @@ function compression (options) {
6766
var _on = res.on
6867
var _write = res.write
6968

69+
var writeHasCallback = (_write.length == 3)
70+
var endHasCallback = (_end.length == 3)
71+
7072
// flush
7173
res.flush = function flush () {
7274
if (stream) {
@@ -81,20 +83,28 @@ function compression (options) {
8183
return false
8284
}
8385

86+
if (!writeHasCallback && cb !== undefined) {
87+
throw new Error("compression, request.write does not support callback")
88+
}
89+
8490
if (!this._header) {
8591
this._implicitHeader()
8692
}
8793

8894
return stream
89-
? stream.write(new Buffer(chunk, encoding), nodeHasCallbacks ? cb : undefined)
90-
: _write.call(this, chunk, encoding, nodeHasCallbacks ? cb : undefined)
95+
? stream.write(new Buffer(chunk, encoding), cb)
96+
: _write.call(this, chunk, encoding, cb)
9197
}
9298

9399
res.end = function end (chunk, encoding, cb) {
94100
if (ended) {
95101
return false
96102
}
97103

104+
if (!endHasCallback && cb !== undefined) {
105+
throw new Error("compression, request.end does not support callback")
106+
}
107+
98108
if (!this._header) {
99109
// estimate the length
100110
if (!this.getHeader('Content-Length')) {
@@ -105,16 +115,16 @@ function compression (options) {
105115
}
106116

107117
if (!stream) {
108-
return _end.call(this, chunk, encoding, nodeHasCallbacks ? cb : undefined)
118+
return _end.call(this, chunk, encoding, cb)
109119
}
110120

111121
// mark ended
112122
ended = true
113123

114124
// write Buffer for Node.js 0.8
115125
return chunk
116-
? stream.end(new Buffer(chunk, encoding), nodeHasCallbacks ? cb : undefined)
117-
: stream.end(null, null, nodeHasCallbacks ? cb : undefined)
126+
? stream.end(new Buffer(chunk, encoding), cb)
127+
: stream.end(null, null, cb)
118128
}
119129

120130
res.on = function on (type, listener) {

test/compression.js

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -681,18 +681,44 @@ describe('compression()', function () {
681681
it('should call the passed callbacks in the order passed when compressing', function (done) {
682682
var hasCallbacks = false
683683
var callbackOutput = []
684+
var errorsCaught = 0
684685
var server = createServer(null, function (req, res) {
685686
hasCallbacks = (http.OutgoingMessage.prototype.write.length === 3 && http.OutgoingMessage.prototype.end.length === 3)
686687
res.setHeader('Content-Type', 'text/plain')
687-
res.write('Hello', null, function () {
688-
callbackOutput.push(0)
689-
})
690-
res.write(' World', null, function () {
691-
callbackOutput.push(1)
692-
})
693-
res.end(null, null, function () {
694-
callbackOutput.push(2)
695-
})
688+
try {
689+
res.write('Hello', null, function () {
690+
callbackOutput.push(0)
691+
})
692+
} catch(error) {
693+
errorsCaught++
694+
if (hasCallbacks || error.message !== "compression, request.write does not support callback") {
695+
throw error;
696+
}
697+
res.write('Hello');
698+
}
699+
try {
700+
res.write(' World', null, function () {
701+
callbackOutput.push(1)
702+
})
703+
} catch(error) {
704+
errorsCaught++
705+
if (hasCallbacks || error.message !== "compression, request.write does not support callback") {
706+
throw error;
707+
}
708+
res.write(' World')
709+
}
710+
try {
711+
res.end(null, null, function () {
712+
callbackOutput.push(2)
713+
})
714+
} catch(error) {
715+
errorsCaught++
716+
console.log(error)
717+
if (hasCallbacks || error.message !== "compression, request.end does not support callback") {
718+
throw error;
719+
}
720+
res.end()
721+
}
696722
})
697723

698724
request(server)
@@ -709,6 +735,7 @@ describe('compression()', function () {
709735
} else {
710736
// nodejs 0.10 zlib has callbacks but OutgoingMessage doesn't, assert that no callbacks were made
711737
assert.equal(callbackOutput.length, 0)
738+
assert.equal(errorsCaught, 3)
712739
}
713740
done()
714741
})
@@ -717,19 +744,44 @@ describe('compression()', function () {
717744
it('should call the passed callbacks in the order passed when not compressing', function (done) {
718745
var hasCallbacks = false
719746
var callbackOutput = []
747+
var errorsCaught = 0
720748
var server = createServer(null, function (req, res) {
721749
hasCallbacks = (http.OutgoingMessage.prototype.write.length === 3 && http.OutgoingMessage.prototype.end.length === 3)
722750
res.setHeader('Cache-Control', 'no-transform')
723751
res.setHeader('Content-Type', 'text/plain')
724-
res.write('hello,', null, function () {
725-
callbackOutput.push(0)
726-
})
727-
res.write(' world', null, function () {
728-
callbackOutput.push(1)
729-
})
730-
res.end(null, null, function () {
731-
callbackOutput.push(2)
732-
})
752+
try {
753+
res.write('hello,', null, function () {
754+
callbackOutput.push(0)
755+
})
756+
} catch(error) {
757+
errorsCaught++
758+
if (hasCallbacks || error.message !== "compression, request.write does not support callback") {
759+
throw error;
760+
}
761+
res.write('Hello');
762+
}
763+
try {
764+
res.write(' world', null, function () {
765+
callbackOutput.push(1)
766+
})
767+
} catch(error) {
768+
errorsCaught++
769+
if (hasCallbacks || error.message !== "compression, request.write does not support callback") {
770+
throw error;
771+
}
772+
res.write(' world');
773+
}
774+
try {
775+
res.end(null, null, function () {
776+
callbackOutput.push(2)
777+
})
778+
} catch(error) {
779+
errorsCaught++
780+
if (hasCallbacks || error.message !== "compression, request.end does not support callback") {
781+
throw error;
782+
}
783+
res.end();
784+
}
733785
})
734786

735787
request(server)
@@ -746,6 +798,7 @@ describe('compression()', function () {
746798
assert.deepEqual(callbackOutput, [0, 1, 2])
747799
} else {
748800
assert.equal(callbackOutput.length, 0)
801+
assert.equal(errorsCaught, 3)
749802
}
750803
done()
751804
})

0 commit comments

Comments
 (0)