diff --git a/lib/encoding.js b/lib/encoding.js index ebb4820..7ed4116 100644 --- a/lib/encoding.js +++ b/lib/encoding.js @@ -2,7 +2,36 @@ var Enc = module.exports; -Enc.bufToHex = function toHex(u8) { +Enc.base64ToBuf = function (str) { + // always convert from urlsafe base64, just in case + //return Buffer.from(Enc.urlBase64ToBase64(str)).toString('base64'); + // node handles urlBase64 automatically + return Buffer.from(str, 'base64'); +}; + +Enc.base64ToHex = function (b64) { + return Enc.bufToHex(Enc.base64ToBuf(b64)); +}; + +Enc.bufToBase64 = function (u8) { + // we want to maintain api compatability with browser APIs, + // so we assume that this could be a Uint8Array + return Buffer.from(u8).toString('base64'); +}; + +/* +Enc.bufToUint8 = function bufToUint8(buf) { + return new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)); +}; +*/ + +Enc.bufToUrlBase64 = function (u8) { + return Enc.bufToBase64(u8) + .replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); +}; + + +Enc.bufToHex = function (u8) { var hex = []; var i, h; var len = (u8.byteLength || u8.length); @@ -24,7 +53,7 @@ Enc.hexToBuf = function (hex) { return Buffer.from(hex, 'hex'); }; -Enc.numToHex = function numToHex(d) { +Enc.numToHex = function (d) { d = d.toString(16); if (d.length % 2) { return '0' + d; @@ -32,37 +61,17 @@ Enc.numToHex = function numToHex(d) { return d; }; -Enc.base64ToHex = function base64ToHex(b64) { - return Enc.bufToHex(Enc.base64ToBuf(b64)); -}; - -Enc.bufToBase64 = function toHex(u8) { - // we want to maintain api compatability with browser APIs, - // so we assume that this could be a Uint8Array - return Buffer.from(u8).toString('base64'); -}; - /* -Enc.bufToUint8 = function bufToUint8(buf) { - return new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)); +Enc.strToBase64 = function (str) { + // node automatically can tell the difference + // between uc2 (utf-8) strings and binary strings + // so we don't have to re-encode the strings + return Buffer.from(str).toString('base64'); }; */ -Enc.bufToUrlBase64 = function toHex(u8) { - return Enc.bufToBase64(u8) - .replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); -}; - -Enc.strToHex = function strToHex(str) { - return Buffer.from(str).toString('hex'); -}; - -Enc.strToBuf = function strToBuf(str) { - return Buffer.from(str); -}; - /* -Enc.strToBin = function strToBin(str) { +Enc.strToBin = function (str) { var escstr = encodeURIComponent(str); // replaces any uri escape sequence, such as %0A, // with binary escape, such as 0x0A @@ -74,17 +83,16 @@ Enc.strToBin = function strToBin(str) { }; */ -/* -Enc.strToBase64 = function strToBase64(str) { - // node automatically can tell the difference - // between uc2 (utf-8) strings and binary strings - // so we don't have to re-encode the strings - return Buffer.from(str).toString('base64'); +Enc.strToBuf = function (str) { + return Buffer.from(str); +}; + +Enc.strToHex = function (str) { + return Buffer.from(str).toString('hex'); }; -*/ /* -Enc.urlBase64ToBase64 = function urlsafeBase64ToBase64(str) { +Enc.urlBase64ToBase64 = function (str) { var r = str % 4; if (2 === r) { str += '=='; @@ -94,10 +102,3 @@ Enc.urlBase64ToBase64 = function urlsafeBase64ToBase64(str) { return str.replace(/-/g, '+').replace(/_/g, '/'); }; */ - -Enc.base64ToBuf = function base64ToBuf(str) { - // always convert from urlsafe base64, just in case - //return Buffer.from(Enc.urlBase64ToBase64(str)).toString('base64'); - // node handles urlBase64 automatically - return Buffer.from(str, 'base64'); -};