| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -197,9 +197,12 @@ function assertCrypto() { | |||
| 197 | 197 | throw new ERR_NO_CRYPTO(); | |
| 198 | 198 | } | |
| 199 | 199 | ||
| 200 | - // Return undefined if there is no match. | ||
| 201 | - // Move the "slow cases" to a separate function to make sure this function gets | ||
| 202 | - // inlined properly. That prioritizes the common case. | ||
| 200 | + /** | ||
| 201 | + * Move the "slow cases" to a separate function to make sure this function gets | ||
| 202 | + * inlined properly. That prioritizes the common case. | ||
| 203 | + * @param {unknown} enc | ||
| 204 | + * @returns {string | undefined} Returns undefined if there is no match. | ||
| 205 | + */ | ||
| 203 | 206 | function normalizeEncoding(enc) { | |
| 204 | 207 | if (enc == null || enc === 'utf8' || enc === 'utf-8') return 'utf8'; | |
| 205 | 208 | return slowCases(enc); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,38 +40,17 @@ const { | |||
| 40 | 40 | flush, | |
| 41 | 41 | } = internalBinding('string_decoder'); | |
| 42 | 42 | const { | |
| 43 | - kIsEncodingSymbol, | ||
| 44 | 43 | encodingsMap, | |
| 45 | - normalizeEncoding: _normalizeEncoding, | ||
| 44 | + normalizeEncoding, | ||
| 46 | 45 | } = require('internal/util'); | |
| 47 | 46 | const { | |
| 48 | 47 | ERR_INVALID_ARG_TYPE, | |
| 49 | 48 | ERR_INVALID_THIS, | |
| 50 | 49 | ERR_UNKNOWN_ENCODING, | |
| 51 | 50 | } = require('internal/errors').codes; | |
| 52 | - const isEncoding = Buffer[kIsEncodingSymbol]; | ||
| 53 | 51 | ||
| 54 | 52 | const kNativeDecoder = Symbol('kNativeDecoder'); | |
| 55 | 53 | ||
| 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 | - | ||
| 75 | 54 | /** | |
| 76 | 55 | * StringDecoder provides an interface for efficiently splitting a series of | |
| 77 | 56 | * buffers into a series of JS strings without breaking apart multi-byte | |
@@ -80,6 +59,9 @@ function normalizeEncoding(enc) { | |||
| 80 | 59 | */ | |
| 81 | 60 | function StringDecoder(encoding) { | |
| 82 | 61 | this.encoding = normalizeEncoding(encoding); | |
| 62 | + if (this.encoding === undefined) { | ||
| 63 | + throw new ERR_UNKNOWN_ENCODING(encoding); | ||
| 64 | + } | ||
| 83 | 65 | this[kNativeDecoder] = Buffer.alloc(kSize); | |
| 84 | 66 | this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding]; | |
| 85 | 67 | } | |
@@ -112,11 +94,9 @@ StringDecoder.prototype.write = function write(buf) { | |||
| 112 | 94 | * @returns {string} | |
| 113 | 95 | */ | |
| 114 | 96 | StringDecoder.prototype.end = function end(buf) { | |
| 115 | - let ret = ''; | ||
| 116 | - if (buf !== undefined) | ||
| 117 | - ret = this.write(buf); | ||
| 97 | + const ret = buf === undefined ? '' : this.write(buf); | ||
| 118 | 98 | if (this[kNativeDecoder][kBufferedBytes] > 0) | |
| 119 | - ret += flush(this[kNativeDecoder]); | ||
| 99 | + return ret + flush(this[kNativeDecoder]); | ||
| 120 | 100 | return ret; | |
| 121 | 101 | }; | |
| 122 | 102 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments