@@ -40,38 +40,17 @@ const {
4040 flush,
4141} = internalBinding ( 'string_decoder' ) ;
4242const {
43- kIsEncodingSymbol,
4443 encodingsMap,
45- normalizeEncoding : _normalizeEncoding ,
44+ normalizeEncoding,
4645} = require ( 'internal/util' ) ;
4746const {
4847 ERR_INVALID_ARG_TYPE ,
4948 ERR_INVALID_THIS ,
5049 ERR_UNKNOWN_ENCODING ,
5150} = require ( 'internal/errors' ) . codes ;
52- const isEncoding = Buffer [ kIsEncodingSymbol ] ;
5351
5452const kNativeDecoder = Symbol ( 'kNativeDecoder' ) ;
5553
56- // Do not cache `Buffer.isEncoding` when checking encoding names as some
57- // modules monkey-patch it to support additional encodings
58- /**
59- * Normalize encoding notation
60- * @param {string } enc
61- * @returns {"utf8" | "utf16le" | "hex" | "ascii"
62- * | "base64" | "latin1" | "base64url"}
63- * @throws {TypeError } Throws an error when encoding is invalid
64- */
65- function normalizeEncoding ( enc ) {
66- const nenc = _normalizeEncoding ( enc ) ;
67- if ( nenc === undefined ) {
68- if ( Buffer . isEncoding === isEncoding || ! Buffer . isEncoding ( enc ) )
69- throw new ERR_UNKNOWN_ENCODING ( enc ) ;
70- return enc ;
71- }
72- return nenc ;
73- }
74-
7554/**
7655 * StringDecoder provides an interface for efficiently splitting a series of
7756 * buffers into a series of JS strings without breaking apart multi-byte
@@ -80,6 +59,9 @@ function normalizeEncoding(enc) {
8059 */
8160function StringDecoder ( encoding ) {
8261 this . encoding = normalizeEncoding ( encoding ) ;
62+ if ( this . encoding === undefined ) {
63+ throw new ERR_UNKNOWN_ENCODING ( encoding ) ;
64+ }
8365 this [ kNativeDecoder ] = Buffer . alloc ( kSize ) ;
8466 this [ kNativeDecoder ] [ kEncodingField ] = encodingsMap [ this . encoding ] ;
8567}
@@ -112,9 +94,7 @@ StringDecoder.prototype.write = function write(buf) {
11294 * @returns {string }
11395 */
11496StringDecoder . prototype . end = function end ( buf ) {
115- let ret = '' ;
116- if ( buf !== undefined )
117- ret = this . write ( buf ) ;
97+ let ret = buf === undefined ? '' : this . write ( buf ) ;
11898 if ( this [ kNativeDecoder ] [ kBufferedBytes ] > 0 )
11999 ret += flush ( this [ kNativeDecoder ] ) ;
120100 return ret ;
0 commit comments