Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
typings: add typing for internal/string_decoder
  • Loading branch information
Ayase-252 committed Apr 13, 2021
commit cd2230b9890d2ebccddcd9d1372e0a3f7bb89f54
25 changes: 25 additions & 0 deletions lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ const kNativeDecoder = Symbol('kNativeDecoder');

// Do not cache `Buffer.isEncoding` when checking encoding names as some
// modules monkey-patch it to support additional encodings
/**
*
* @param {string} enc
* @returns {"utf8" | "utf16le" | "hex" | "ascii" | "base64" | "latin1" | "base64url"}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @returns {"utf8" | "utf16le" | "hex" | "ascii" | "base64" | "latin1" | "base64url"}
* @returns {"utf8" | "utf16le" | "hex" | "ascii"
* | "base64" | "latin1" | "base64url"}

What about this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it works

*/
function normalizeEncoding(enc) {
const nenc = internalUtil.normalizeEncoding(enc);
if (nenc === undefined) {
Expand All @@ -68,12 +73,21 @@ for (let i = 0; i < encodings.length; ++i)
// StringDecoder provides an interface for efficiently splitting a series of
// buffers into a series of JS strings without breaking apart multi-byte
// characters.
/**
*
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the above description inside the comment?

* @param {string} encoding
Comment thread
jasnell marked this conversation as resolved.
Outdated
*/
function StringDecoder(encoding) {
this.encoding = normalizeEncoding(encoding);
this[kNativeDecoder] = Buffer.alloc(kSize);
this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding];
}

/**
*
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*
* Returns a decoded string, omitting any incomplete multibyte
* characters at the end of the Buffer, or TypedArray, or DataView

* @param {string | Buffer | TypedArray | DataView} buf
* @returns {string}
*/
StringDecoder.prototype.write = function write(buf) {
if (typeof buf === 'string')
return buf;
Expand All @@ -84,6 +98,11 @@ StringDecoder.prototype.write = function write(buf) {
return decode(this[kNativeDecoder], buf);
};

/**
*
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*
* Returns any remaining input stored in the internal buffer as a string.
* After end() is called, the stringDecoder object can be reused for new input.

* @param {string | Buffer | TypedArray | DataView} buf
Comment thread
jasnell marked this conversation as resolved.
Outdated
* @returns {string}
*/
StringDecoder.prototype.end = function end(buf) {
let ret = '';
if (buf !== undefined)
Expand All @@ -94,6 +113,12 @@ StringDecoder.prototype.end = function end(buf) {
};

/* Everything below this line is undocumented legacy stuff. */
/**
*
* @param {string | Buffer | TypedArray | DataView} buf
* @param {number} offset
* @returns {string}
*/
StringDecoder.prototype.text = function text(buf, offset) {
this[kNativeDecoder][kMissingBytes] = 0;
this[kNativeDecoder][kBufferedBytes] = 0;
Expand Down