Skip to content

Commit 49fd9c6

Browse files
addaleaxBridgeAR
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: nodejs#19414 Refs: nodejs#8874 Refs: nodejs#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 cc6abc6 commit 49fd9c6

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
@@ -979,6 +979,16 @@ Type: Runtime
979979
This was an undocumented helper function not intended for use outside Node.js
980980
core and obsoleted by the removal of NPN (Next Protocol Negotiation) support.
981981
982+
<a id="DEP0108"></a>
983+
### DEP0108: zlib.bytesRead
984+
985+
Type: Documentation-only
986+
987+
Deprecated alias for [`zlib.bytesWritten`][]. This original name was chosen
988+
because it also made sense to interpret the value as the number of bytes
989+
read by the engine, but is inconsistent with other streams in Node.js that
990+
expose values under these names.
991+
982992
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
983993
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
984994
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
@@ -1058,6 +1068,7 @@ core and obsoleted by the removal of NPN (Next Protocol Negotiation) support.
10581068
[`util.types`]: util.html#util_util_types
10591069
[`util`]: util.html
10601070
[`worker.exitedAfterDisconnect`]: cluster.html#cluster_worker_exitedafterdisconnect
1071+
[`zlib.bytesWritten`]: zlib.html#zlib_zlib_byteswritten
10611072
[alloc]: buffer.html#buffer_class_method_buffer_alloc_size_fill_encoding
10621073
[alloc_unsafe_size]: buffer.html#buffer_class_method_buffer_allocunsafe_size
10631074
[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
@@ -288,7 +288,7 @@ function Zlib(opts, mode) {
288288
}
289289
}
290290
Transform.call(this, opts);
291-
this.bytesRead = 0;
291+
this.bytesWritten = 0;
292292
this._handle = new binding.Zlib(mode);
293293
this._handle.jsref = this; // Used by processCallback() and zlibOnError()
294294
this._handle.onerror = zlibOnError;
@@ -327,6 +327,21 @@ Object.defineProperty(Zlib.prototype, '_closed', {
327327
}
328328
});
329329

330+
// `bytesRead` made sense as a name when looking from the zlib engine's
331+
// perspective, but it is inconsistent with all other streams exposed by Node.js
332+
// that have this concept, where it stands for the number of bytes read
333+
// *from* the stream (that is, net.Socket/tls.Socket & file system streams).
334+
Object.defineProperty(Zlib.prototype, 'bytesRead', {
335+
configurable: true,
336+
enumerable: true,
337+
get() {
338+
return this.bytesWritten;
339+
},
340+
set(value) {
341+
this.bytesWritten = value;
342+
}
343+
});
344+
330345
Zlib.prototype.params = function params(level, strategy, callback) {
331346
checkRangesOrGetDefault(level, 'level', Z_MIN_LEVEL, Z_MAX_LEVEL);
332347
checkRangesOrGetDefault(strategy, 'strategy', Z_DEFAULT_STRATEGY, Z_FIXED);
@@ -501,7 +516,7 @@ function processChunkSync(self, chunk, flushFlag) {
501516
}
502517
}
503518

504-
self.bytesRead = inputRead;
519+
self.bytesWritten = inputRead;
505520

506521
if (nread >= kMaxLength) {
507522
_close(self);
@@ -558,8 +573,8 @@ function processCallback() {
558573
var availOutAfter = state[0];
559574
var availInAfter = state[1];
560575

561-
var inDelta = (handle.availInBefore - availInAfter);
562-
self.bytesRead += inDelta;
576+
const inDelta = handle.availInBefore - availInAfter;
577+
self.bytesWritten += inDelta;
563578

564579
var have = handle.availOutBefore - availOutAfter;
565580
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)