@@ -51,6 +51,14 @@ const kNativeDecoder = Symbol('kNativeDecoder');
5151
5252// Do not cache `Buffer.isEncoding` when checking encoding names as some
5353// modules monkey-patch it to support additional encodings
54+ /**
55+ * Normalize encoding notation
56+ *
57+ * @param {string} enc
58+ * @returns {"utf8" | "utf16le" | "hex" | "ascii"
59+ * | "base64" | "latin1" | "base64url"}
60+ * @throws {TypeError} Throws an error when encoding is invalid
61+ */
5462function normalizeEncoding(enc) {
5563 const nenc = internalUtil.normalizeEncoding(enc);
5664 if (nenc === undefined) {
@@ -65,15 +73,27 @@ const encodingsMap = {};
6573for (let i = 0; i < encodings.length; ++i)
6674 encodingsMap[encodings[i]] = i;
6775
68- // StringDecoder provides an interface for efficiently splitting a series of
69- // buffers into a series of JS strings without breaking apart multi-byte
70- // characters.
76+ /**
77+ * StringDecoder provides an interface for efficiently splitting a series of
78+ * buffers into a series of JS strings without breaking apart multi-byte
79+ * characters.
80+ *
81+ * @param {string} [encoding=utf-8]
82+ */
7183function StringDecoder(encoding) {
7284 this.encoding = normalizeEncoding(encoding);
7385 this[kNativeDecoder] = Buffer.alloc(kSize);
7486 this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding];
7587}
7688
89+ /**
90+ * Returns a decoded string, omitting any incomplete multi-bytes
91+ * characters at the end of the Buffer, or TypedArray, or DataView
92+ *
93+ * @param {string | Buffer | TypedArray | DataView} buf
94+ * @returns {string}
95+ * @throws {TypeError} Throws when buf is not in one of supported types
96+ */
7797StringDecoder.prototype.write = function write(buf) {
7898 if (typeof buf === 'string')
7999 return buf;
@@ -84,6 +104,14 @@ StringDecoder.prototype.write = function write(buf) {
84104 return decode(this[kNativeDecoder], buf);
85105};
86106
107+ /**
108+ * Returns any remaining input stored in the internal buffer as a string.
109+ * After end() is called, the stringDecoder object can be reused for new
110+ * input.
111+ *
112+ * @param {string | Buffer | TypedArray | DataView} [buf]
113+ * @returns {string}
114+ */
87115StringDecoder.prototype.end = function end(buf) {
88116 let ret = '';
89117 if (buf !== undefined)
@@ -94,6 +122,12 @@ StringDecoder.prototype.end = function end(buf) {
94122};
95123
96124/* Everything below this line is undocumented legacy stuff. */
125+ /**
126+ *
127+ * @param {string | Buffer | TypedArray | DataView} buf
128+ * @param {number} offset
129+ * @returns {string}
130+ */
97131StringDecoder.prototype.text = function text(buf, offset) {
98132 this[kNativeDecoder][kMissingBytes] = 0;
99133 this[kNativeDecoder][kBufferedBytes] = 0;
0 commit comments