From c9c62deee7ea3a7016211be63269a10b4e6ff763 Mon Sep 17 00:00:00 2001 From: dicearr Date: Mon, 23 Oct 2017 09:28:52 +0200 Subject: [PATCH 1/4] doc: howto decode buffers extending from Writable Improved stream documentation with an example of how to decode buffers to strings within a custom Writable. Fixes: https://github.com/nodejs/node/issues/15369 --- doc/api/stream.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/doc/api/stream.md b/doc/api/stream.md index d2af5cd93370bf..ae3dc7771aa305 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1510,6 +1510,39 @@ class MyWritable extends Writable { } ``` +#### Decoding buffers in a Writable Stream + +Decoding buffers is a common task, for instance, when using transformers whose +input is a string. This is not a trivial process when using multi-byte +characters encoding. Implement it within [Writable][] implies some performance +regressions. The following is an example of how to achieve that extending +[Writable][]. + +```js +const { Writable } = require('stream') +const { StringDecoder } = require('string_decoder') + +class StringWritable extends Writable { + constructor (options) { + super(options) + const state = this._writableState + this._decoder = new StringDecoder(state.defaultEncoding) + this._data = '' + } + _write (chunk, encoding, callback) { + if (encoding === 'buffer') { + chunk = this._decoder.write(chunk) + } + this._data += chunk + callback() + } + _final (callback) { + this._data += this._decoder.end() + callback() + } +} +``` + ### Implementing a Readable Stream The `stream.Readable` class is extended to implement a [Readable][] stream. From 0acc2a38993645721eec0068045c62f9bf2a8d62 Mon Sep 17 00:00:00 2001 From: dicearr Date: Tue, 24 Oct 2017 09:22:41 +0200 Subject: [PATCH 2/4] fix: linting errors in example code --- doc/api/stream.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/doc/api/stream.md b/doc/api/stream.md index ae3dc7771aa305..9db5261bec3348 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1519,26 +1519,26 @@ regressions. The following is an example of how to achieve that extending [Writable][]. ```js -const { Writable } = require('stream') -const { StringDecoder } = require('string_decoder') +const { Writable } = require('stream'); +const { StringDecoder } = require('string_decoder'); class StringWritable extends Writable { - constructor (options) { - super(options) - const state = this._writableState - this._decoder = new StringDecoder(state.defaultEncoding) - this._data = '' + constructor(options) { + super(options); + const state = this._writableState; + this._decoder = new StringDecoder(state.defaultEncoding); + this._data = ''; } - _write (chunk, encoding, callback) { + _write(chunk, encoding, callback) { if (encoding === 'buffer') { - chunk = this._decoder.write(chunk) + chunk = this._decoder.write(chunk); } - this._data += chunk - callback() + this._data += chunk; + callback(); } - _final (callback) { - this._data += this._decoder.end() - callback() + _final(callback) { + this._data += this._decoder.end(); + callback(); } } ``` From 14140ccc32d6a9cb000b08f6459c23958db1d5fc Mon Sep 17 00:00:00 2001 From: dicearr Date: Mon, 30 Oct 2017 14:52:28 +0100 Subject: [PATCH 3/4] fix: example usage and text changes --- doc/api/stream.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/api/stream.md b/doc/api/stream.md index 9db5261bec3348..e59cb2ebe4bcf4 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1514,9 +1514,8 @@ class MyWritable extends Writable { Decoding buffers is a common task, for instance, when using transformers whose input is a string. This is not a trivial process when using multi-byte -characters encoding. Implement it within [Writable][] implies some performance -regressions. The following is an example of how to achieve that extending -[Writable][]. +characters encoding, such as UTF-8. The following example shows how to decode +multi-byte strings using `StringDecoder` and [Writable][]. ```js const { Writable } = require('stream'); @@ -1541,6 +1540,15 @@ class StringWritable extends Writable { callback(); } } + +const euro = [[0xE2, 0x82], [0xAC]].map(Buffer.from); +const w = new StringWritable(); + +w.write('currency: '); +w.write(euro[0]); +w.end(euro[1]); + +console.log(w._data); // currency: € ``` ### Implementing a Readable Stream From 829d35606d7374b834f38c323281c82743e46e68 Mon Sep 17 00:00:00 2001 From: dicearr Date: Tue, 31 Oct 2017 10:08:00 +0100 Subject: [PATCH 4/4] fix: renamed _data into data --- doc/api/stream.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/api/stream.md b/doc/api/stream.md index e59cb2ebe4bcf4..02ffa645a48ecc 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -1526,17 +1526,17 @@ class StringWritable extends Writable { super(options); const state = this._writableState; this._decoder = new StringDecoder(state.defaultEncoding); - this._data = ''; + this.data = ''; } _write(chunk, encoding, callback) { if (encoding === 'buffer') { chunk = this._decoder.write(chunk); } - this._data += chunk; + this.data += chunk; callback(); } _final(callback) { - this._data += this._decoder.end(); + this.data += this._decoder.end(); callback(); } } @@ -1548,7 +1548,7 @@ w.write('currency: '); w.write(euro[0]); w.end(euro[1]); -console.log(w._data); // currency: € +console.log(w.data); // currency: € ``` ### Implementing a Readable Stream