-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zlib: expose amount of data read for engines
Added bytesRead property to Zlib engines Fixes: #8874 PR-URL: #13088 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
- Loading branch information
1 parent
d0b1b52
commit e710036
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const zlib = require('zlib'); | ||
|
||
const expectStr = 'abcdefghijklmnopqrstuvwxyz'.repeat(2); | ||
const expectBuf = Buffer.from(expectStr); | ||
|
||
function createWriter(target, buffer) { | ||
const writer = { size: 0 }; | ||
const write = () => { | ||
target.write(Buffer.from([buffer[writer.size++]]), () => { | ||
if (writer.size < buffer.length) { | ||
target.flush(write); | ||
} else { | ||
target.end(); | ||
} | ||
}); | ||
}; | ||
write(); | ||
return writer; | ||
} | ||
|
||
for (const method of [ | ||
['createGzip', 'createGunzip', false], | ||
['createGzip', 'createUnzip', false], | ||
['createDeflate', 'createInflate', true], | ||
['createDeflateRaw', 'createInflateRaw', true] | ||
]) { | ||
let compWriter; | ||
let compData = new Buffer(0); | ||
|
||
const comp = zlib[method[0]](); | ||
comp.on('data', function(d) { | ||
compData = Buffer.concat([compData, d]); | ||
assert.strictEqual(this.bytesRead, compWriter.size, | ||
`Should get write size on ${method[0]} data.`); | ||
}); | ||
comp.on('end', common.mustCall(function() { | ||
assert.strictEqual(this.bytesRead, compWriter.size, | ||
`Should get write size on ${method[0]} end.`); | ||
assert.strictEqual(this.bytesRead, expectStr.length, | ||
`Should get data size on ${method[0]} end.`); | ||
|
||
{ | ||
let decompWriter; | ||
let decompData = new Buffer(0); | ||
|
||
const decomp = zlib[method[1]](); | ||
decomp.on('data', function(d) { | ||
decompData = Buffer.concat([decompData, d]); | ||
assert.strictEqual(this.bytesRead, decompWriter.size, | ||
`Should get write size on ${method[0]}/` + | ||
`${method[1]} data.`); | ||
}); | ||
decomp.on('end', common.mustCall(function() { | ||
assert.strictEqual(this.bytesRead, compData.length, | ||
`Should get compressed size on ${method[0]}/` + | ||
`${method[1]} end.`); | ||
assert.strictEqual(decompData.toString(), expectStr, | ||
`Should get original string on ${method[0]}/` + | ||
`${method[1]} end.`); | ||
})); | ||
decompWriter = createWriter(decomp, compData); | ||
} | ||
|
||
// Some methods should allow extra data after the compressed data | ||
if (method[2]) { | ||
const compDataExtra = Buffer.concat([compData, new Buffer('extra')]); | ||
|
||
let decompWriter; | ||
let decompData = new Buffer(0); | ||
|
||
const decomp = zlib[method[1]](); | ||
decomp.on('data', function(d) { | ||
decompData = Buffer.concat([decompData, d]); | ||
assert.strictEqual(this.bytesRead, decompWriter.size, | ||
`Should get write size on ${method[0]}/` + | ||
`${method[1]} data.`); | ||
}); | ||
decomp.on('end', common.mustCall(function() { | ||
assert.strictEqual(this.bytesRead, compData.length, | ||
`Should get compressed size on ${method[0]}/` + | ||
`${method[1]} end.`); | ||
assert.strictEqual(decompData.toString(), expectStr, | ||
`Should get original string on ${method[0]}/` + | ||
`${method[1]} end.`); | ||
})); | ||
decompWriter = createWriter(decomp, compDataExtra); | ||
} | ||
})); | ||
compWriter = createWriter(comp, expectBuf); | ||
} |