Skip to content

Commit 307ce80

Browse files
addaleaxtargos
authored andcommitted
zlib: use .bytesWritten instead of .bytesRead
The introduction of `.bytesRead` to zlib streams was unfortunate, because other streams in Node.js core use the exact opposite naming of `.bytesRead` and `.bytesWritten`. While one could see how the original naming makes sense in a `Transform` stream context, we should try to work towards more consistent APIs in core for these things. This introduces `zlib.bytesWritten` and documentation-only deprecates `zlib.bytesRead`. PR-URL: #19414 Refs: #8874 Refs: #13088 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent b476102 commit 307ce80

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

doc/api/deprecations.md

+11
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,16 @@ Use [`asyncResource.runInAsyncScope()`][] API instead which provides a much
796796
safer, and more convenient, alternative. See
797797
https://github.com/nodejs/node/pull/18513 for more details.
798798
799+
<a id="DEP0108"></a>
800+
### DEP0108: zlib.bytesRead
801+
802+
Type: Documentation-only
803+
804+
Deprecated alias for [`zlib.bytesWritten`][]. This original name was chosen
805+
because it also made sense to interpret the value as the number of bytes
806+
read by the engine, but is inconsistent with other streams in Node.js that
807+
expose values under these names.
808+
799809
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
800810
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
801811
[`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer
@@ -857,6 +867,7 @@ https://github.com/nodejs/node/pull/18513 for more details.
857867
[`util.puts()`]: util.html#util_util_puts_strings
858868
[`util`]: util.html
859869
[`worker.exitedAfterDisconnect`]: cluster.html#cluster_worker_exitedafterdisconnect
870+
[`zlib.bytesWritten`]: zlib.html#zlib_zlib_byteswritten
860871
[alloc]: buffer.html#buffer_class_method_buffer_alloc_size_fill_encoding
861872
[alloc_unsafe_size]: buffer.html#buffer_class_method_buffer_allocunsafe_size
862873
[from_arraybuffer]: buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length

doc/api/zlib.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,28 @@ class of the compressor/decompressor classes.
400400
### zlib.bytesRead
401401
<!-- YAML
402402
added: v8.1.0
403+
deprecated: REPLACEME
403404
-->
404405

406+
> Stability: 0 - Deprecated: Use [`zlib.bytesWritten`][] instead.
407+
405408
* {number}
406409

407-
The `zlib.bytesRead` property specifies the number of bytes read by the engine
408-
before the bytes are processed (compressed or decompressed, as appropriate for
409-
the derived class).
410+
Deprecated alias for [`zlib.bytesWritten`][]. This original name was chosen
411+
because it also made sense to interpret the value as the number of bytes
412+
read by the engine, but is inconsistent with other streams in Node.js that
413+
expose values under these names.
414+
415+
### zlib.bytesWritten
416+
<!-- YAML
417+
added: REPLACEME
418+
-->
419+
420+
* {number}
421+
422+
The `zlib.bytesWritten` property specifies the number of bytes written to
423+
the engine, before the bytes are processed (compressed or decompressed,
424+
as appropriate for the derived class).
410425

411426
### zlib.close([callback])
412427
<!-- YAML
@@ -763,7 +778,8 @@ Decompress a chunk of data with [Unzip][].
763778
[InflateRaw]: #zlib_class_zlib_inflateraw
764779
[Inflate]: #zlib_class_zlib_inflate
765780
[Memory Usage Tuning]: #zlib_memory_usage_tuning
781+
[options]: #zlib_class_options
766782
[Unzip]: #zlib_class_zlib_unzip
767783
[`UV_THREADPOOL_SIZE`]: cli.html#cli_uv_threadpool_size_size
768-
[options]: #zlib_class_options
784+
[`zlib.bytesWritten`]: #zlib_zlib_byteswritten
769785
[zlib documentation]: https://zlib.net/manual.html#Constants

lib/zlib.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ function Zlib(opts, mode) {
270270
}
271271
}
272272
Transform.call(this, opts);
273-
this.bytesRead = 0;
273+
this.bytesWritten = 0;
274274
this._handle = new binding.Zlib(mode);
275275
this._handle.jsref = this; // Used by processCallback() and zlibOnError()
276276
this._handle.onerror = zlibOnError;
@@ -309,6 +309,21 @@ Object.defineProperty(Zlib.prototype, '_closed', {
309309
}
310310
});
311311

312+
// `bytesRead` made sense as a name when looking from the zlib engine's
313+
// perspective, but it is inconsistent with all other streams exposed by Node.js
314+
// that have this concept, where it stands for the number of bytes read
315+
// *from* the stream (that is, net.Socket/tls.Socket & file system streams).
316+
Object.defineProperty(Zlib.prototype, 'bytesRead', {
317+
configurable: true,
318+
enumerable: true,
319+
get() {
320+
return this.bytesWritten;
321+
},
322+
set(value) {
323+
this.bytesWritten = value;
324+
}
325+
});
326+
312327
Zlib.prototype.params = function params(level, strategy, callback) {
313328
if (level < Z_MIN_LEVEL || level > Z_MAX_LEVEL)
314329
throw new errors.RangeError('ERR_INVALID_ARG_VALUE', 'level', level);
@@ -489,7 +504,7 @@ function processChunkSync(self, chunk, flushFlag) {
489504
}
490505
}
491506

492-
self.bytesRead = inputRead;
507+
self.bytesWritten = inputRead;
493508

494509
if (nread >= kMaxLength) {
495510
_close(self);
@@ -546,8 +561,8 @@ function processCallback() {
546561
var availOutAfter = state[0];
547562
var availInAfter = state[1];
548563

549-
var inDelta = (handle.availInBefore - availInAfter);
550-
self.bytesRead += inDelta;
564+
const inDelta = handle.availInBefore - availInAfter;
565+
self.bytesWritten += inDelta;
551566

552567
var have = handle.availOutBefore - availOutAfter;
553568
if (have > 0) {

test/parallel/test-zlib-bytes-read.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ for (const method of [
3333
const comp = zlib[method[0]]();
3434
comp.on('data', function(d) {
3535
compData = Buffer.concat([compData, d]);
36-
assert.strictEqual(this.bytesRead, compWriter.size,
36+
assert.strictEqual(this.bytesWritten, compWriter.size,
3737
`Should get write size on ${method[0]} data.`);
3838
});
3939
comp.on('end', common.mustCall(function() {
40-
assert.strictEqual(this.bytesRead, compWriter.size,
40+
assert.strictEqual(this.bytesWritten, compWriter.size,
4141
`Should get write size on ${method[0]} end.`);
42-
assert.strictEqual(this.bytesRead, expectStr.length,
42+
assert.strictEqual(this.bytesWritten, expectStr.length,
4343
`Should get data size on ${method[0]} end.`);
4444

4545
{
@@ -49,12 +49,12 @@ for (const method of [
4949
const decomp = zlib[method[1]]();
5050
decomp.on('data', function(d) {
5151
decompData = Buffer.concat([decompData, d]);
52-
assert.strictEqual(this.bytesRead, decompWriter.size,
52+
assert.strictEqual(this.bytesWritten, decompWriter.size,
5353
`Should get write size on ${method[0]}/` +
5454
`${method[1]} data.`);
5555
});
5656
decomp.on('end', common.mustCall(function() {
57-
assert.strictEqual(this.bytesRead, compData.length,
57+
assert.strictEqual(this.bytesWritten, compData.length,
5858
`Should get compressed size on ${method[0]}/` +
5959
`${method[1]} end.`);
6060
assert.strictEqual(decompData.toString(), expectStr,
@@ -74,14 +74,16 @@ for (const method of [
7474
const decomp = zlib[method[1]]();
7575
decomp.on('data', function(d) {
7676
decompData = Buffer.concat([decompData, d]);
77-
assert.strictEqual(this.bytesRead, decompWriter.size,
77+
assert.strictEqual(this.bytesWritten, decompWriter.size,
7878
`Should get write size on ${method[0]}/` +
7979
`${method[1]} data.`);
8080
});
8181
decomp.on('end', common.mustCall(function() {
82-
assert.strictEqual(this.bytesRead, compData.length,
82+
assert.strictEqual(this.bytesWritten, compData.length,
8383
`Should get compressed size on ${method[0]}/` +
8484
`${method[1]} end.`);
85+
// Checking legacy name.
86+
assert.strictEqual(this.bytesWritten, this.bytesRead);
8587
assert.strictEqual(decompData.toString(), expectStr,
8688
`Should get original string on ${method[0]}/` +
8789
`${method[1]} end.`);

0 commit comments

Comments
 (0)