-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
zlib: expose amount of data read for engines #13088
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
Changes from 2 commits
7aa6e2b
df6c1f0
cf58570
f5a87d6
6c1a1e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -385,6 +385,12 @@ added: v0.5.8 | |
| Not exported by the `zlib` module. It is documented here because it is the base | ||
| class of the compressor/decompressor classes. | ||
|
|
||
| ### zlib.bytesRead | ||
|
|
||
| * {number} | ||
|
|
||
| The `zlib.bytesRead` property specifies the number of bytes read by the engine. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's the bytes in. Description expanded. |
||
|
|
||
| ### zlib.flush([kind], callback) | ||
| <!-- YAML | ||
| added: v0.5.8 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| '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) { | ||
| setTimeout(write, 25); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks a bit odd… I think what you want is to do something once the write finished, but the right way to do that would be to pass a callback to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I didn't realize there were callbacks for these methods. That's a much better idea. |
||
| } 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); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add
directly below this heading?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added