Skip to content

Callback implementation and tests for write and end #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: v2
Choose a base branch
from
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function compression (options) {

// proxy

res.write = function write (chunk, encoding) {
res.write = function write (chunk, encoding, cb) {
if (ended) {
return false
}
Expand All @@ -97,11 +97,11 @@ function compression (options) {
}

return stream
? stream.write(toBuffer(chunk, encoding))
: _write.call(this, chunk, encoding)
? stream.write(toBuffer(chunk, encoding), cb)
: _write.call(this, chunk, encoding, cb)
}

res.end = function end (chunk, encoding) {
res.end = function end (chunk, encoding, cb) {
if (ended) {
return false
}
Expand All @@ -116,7 +116,7 @@ function compression (options) {
}

if (!stream) {
return _end.call(this, chunk, encoding)
return _end.call(this, chunk, encoding, cb)
}

// mark ended
Expand All @@ -125,7 +125,7 @@ function compression (options) {
// write Buffer for Node.js 0.8
return chunk
? stream.end(toBuffer(chunk, encoding))
: stream.end()
: stream.end(null, null, cb)
}

res.on = function on (type, listener) {
Expand Down
62 changes: 62 additions & 0 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,68 @@
var socket = openSocketWithRequest(port)
})
})

describe('when callbacks are used', function () {
it('should call the passed callbacks in the order passed when compressing', function (done) {
var callbackOutput = []
var server = createServer(null, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('Hello', null, function () {
callbackOutput.push(0)
})
res.write(' World', null, function () {
callbackOutput.push(1)
})
res.end(null, null, function () {
callbackOutput.push(2)
})
})

request(server)
.get('/')

Check failure on line 1278 in test/compression.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 6
.set('Accept-Encoding', 'gzip')

Check failure on line 1279 in test/compression.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 6
.expect('Content-Encoding', 'gzip')

Check failure on line 1280 in test/compression.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 6
.end(function (err) {

Check failure on line 1281 in test/compression.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 6
if (err) {

Check failure on line 1282 in test/compression.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 10 spaces but found 8
throw new Error(err)

Check failure on line 1283 in test/compression.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 12 spaces but found 10
}

Check failure on line 1284 in test/compression.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 10 spaces but found 8
assert.equal(callbackOutput.length, 3)

Check failure on line 1285 in test/compression.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 10 spaces but found 8
assert.deepEqual(callbackOutput, [0, 1, 2])

Check failure on line 1286 in test/compression.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 10 spaces but found 8
done()

Check failure on line 1287 in test/compression.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 10 spaces but found 8
})
})

it('should call the passed callbacks in the order passed when not compressing', function (done) {
var callbackOutput = []
var server = createServer(null, function (req, res) {
res.setHeader('Cache-Control', 'no-transform')
res.setHeader('Content-Type', 'text/plain')
res.write('hello,', null, function () {
callbackOutput.push(0)
})
res.write(' world', null, function () {
callbackOutput.push(1)
})
res.end(null, null, function () {
callbackOutput.push(2)
})
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect('Cache-Control', 'no-transform')
.expect(shouldNotHaveHeader('Content-Encoding'))
.end(function (err) {
if (err) {
throw new Error(err)
}
assert.equal(callbackOutput.length, 3)
assert.deepEqual(callbackOutput, [0, 1, 2])
done()
})
})
})
})

function createServer (opts, fn) {
Expand Down