summaryrefslogtreecommitdiff
path: root/lib/internal/util.js
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2016-06-07 16:03:27 -0700
committerJames M Snell <jasnell@gmail.com>2016-06-21 09:28:38 -0700
commit6dd093da261bc12fe5c2d2669eaca96ef4583f10 (patch)
treec5e59fdade5983d4b13279aafd5329b5573dd49d /lib/internal/util.js
parent193afef3b03ecb65c4905843f4bca7b5d7b2ea31 (diff)
downloadnode-new-6dd093da261bc12fe5c2d2669eaca96ef4583f10.tar.gz
buffer,string_decoder: consolidate encoding validation logic
Buffer.isEncoding and string_decoder.normalizeEncoding shared quite a bit of logic. This moves the primary logic into internal/util. The userland modules that monkey patch Buffer.isEncoding should still work. PR-URL: https://github.com/nodejs/node/pull/7207 Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'lib/internal/util.js')
-rw-r--r--lib/internal/util.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/internal/util.js b/lib/internal/util.js
index a0c2412dce..8e7e51aab9 100644
--- a/lib/internal/util.js
+++ b/lib/internal/util.js
@@ -92,3 +92,32 @@ exports.assertCrypto = function(exports) {
if (noCrypto)
throw new Error('Node.js is not compiled with openssl crypto support');
};
+
+exports.kIsEncodingSymbol = Symbol('node.isEncoding');
+exports.normalizeEncoding = function normalizeEncoding(enc) {
+ if (!enc) return 'utf8';
+ var low;
+ for (;;) {
+ switch (enc) {
+ case 'utf8':
+ case 'utf-8':
+ return 'utf8';
+ case 'ucs2':
+ case 'utf16le':
+ case 'ucs-2':
+ case 'utf-16le':
+ return 'utf16le';
+ case 'binary':
+ return 'latin1';
+ case 'base64':
+ case 'ascii':
+ case 'latin1':
+ case 'hex':
+ return enc;
+ default:
+ if (low) return; // undefined
+ enc = ('' + enc).toLowerCase();
+ low = true;
+ }
+ }
+};