js-encoding-utils
Advanced tools
Comparing version 0.3.6 to 0.4.0
@@ -8,10 +8,3 @@ "use strict"; | ||
}); | ||
exports.encodeBase64 = encodeBase64; | ||
exports.decodeBase64 = decodeBase64; | ||
exports.encodeBase64Url = encodeBase64Url; | ||
exports.decodeBase64Url = decodeBase64Url; | ||
exports.arrayBufferToHexString = arrayBufferToHexString; | ||
exports.hexStringToArrayBuffer = hexStringToArrayBuffer; | ||
exports.arrayBufferToString = arrayBufferToString; | ||
exports.stringToArrayBuffer = stringToArrayBuffer; | ||
exports.stringToArrayBuffer = exports.arrayBufferToString = exports.hexStringToArrayBuffer = exports.arrayBufferToHexString = exports.decodeBase64Url = exports.encodeBase64Url = exports.decodeBase64 = exports.encodeBase64 = void 0; | ||
@@ -29,3 +22,3 @@ var env = _interopRequireWildcard(require("./env.js")); | ||
*/ | ||
function encodeBase64(data) { | ||
var encodeBase64 = function encodeBase64(data) { | ||
var str = ''; | ||
@@ -35,3 +28,3 @@ if (typeof data === 'string') str = data;else str = arrayBufferToString(data); | ||
return btoa(str); | ||
} | ||
}; | ||
/** | ||
@@ -44,3 +37,5 @@ * Decode Base64 to Uint8Array | ||
function decodeBase64(str) { | ||
exports.encodeBase64 = encodeBase64; | ||
var decodeBase64 = function decodeBase64(str) { | ||
var atob = env.getEnvAtob(); | ||
@@ -50,3 +45,3 @@ var binary = atob(str); | ||
return getAsciiIfAscii(data); | ||
} | ||
}; | ||
/** | ||
@@ -59,3 +54,5 @@ * if input data is an ArrayBuffer or TypedArray, it would be returned as Uint8Array | ||
function sanitizeTypedArrayAndArrayBuffer(data) { | ||
exports.decodeBase64 = decodeBase64; | ||
var sanitizeTypedArrayAndArrayBuffer = function sanitizeTypedArrayAndArrayBuffer(data) { | ||
if (data instanceof Uint8Array) return data; | ||
@@ -70,3 +67,3 @@ | ||
} else throw new Error('Input must be an ArrayBuffer or a TypedArray'); | ||
} | ||
}; | ||
/** | ||
@@ -79,3 +76,3 @@ * Check if the given Uint8Array can be expressed in Ascii Text | ||
function getAsciiIfAscii(data) { | ||
var getAsciiIfAscii = function getAsciiIfAscii(data) { | ||
if (data instanceof Uint8Array) { | ||
@@ -103,3 +100,3 @@ var flag = true; | ||
} else throw new Error('Input data must be an Uint8Array'); | ||
} | ||
}; | ||
/** | ||
@@ -112,5 +109,5 @@ * Encode ArrayBuffer or TypedArray to base64url string | ||
function encodeBase64Url(data) { | ||
var encodeBase64Url = function encodeBase64Url(data) { | ||
return encodeBase64(data).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); | ||
} | ||
}; | ||
/** | ||
@@ -123,3 +120,5 @@ * Decode Base64Url string to Uint8Array | ||
function decodeBase64Url(str) { | ||
exports.encodeBase64Url = encodeBase64Url; | ||
var decodeBase64Url = function decodeBase64Url(str) { | ||
if (typeof str !== 'string') throw new Error('Input must be string'); | ||
@@ -129,3 +128,3 @@ str = str.replace(/-/g, '+').replace(/_/g, '/'); // str = str + "=".repeat(str.length % 4); // this sometimes causes error... | ||
return decodeBase64(str); | ||
} | ||
}; | ||
/** | ||
@@ -138,3 +137,5 @@ * Encode ArrayBuffer or TypedArray to hex string | ||
function arrayBufferToHexString(data) { | ||
exports.decodeBase64Url = decodeBase64Url; | ||
var arrayBufferToHexString = function arrayBufferToHexString(data) { | ||
var arr = sanitizeTypedArrayAndArrayBuffer(data); | ||
@@ -150,3 +151,3 @@ var hexStr = ''; | ||
return hexStr; | ||
} | ||
}; | ||
/** | ||
@@ -159,3 +160,5 @@ * Decode hex string to Uint8Array | ||
function hexStringToArrayBuffer(str) { | ||
exports.arrayBufferToHexString = arrayBufferToHexString; | ||
var hexStringToArrayBuffer = function hexStringToArrayBuffer(str) { | ||
if (!str || !(typeof str === 'string')) throw new Error('Input arg must be a non-null string'); | ||
@@ -170,3 +173,3 @@ var arr = []; | ||
return new Uint8Array(arr); | ||
} | ||
}; | ||
/** | ||
@@ -179,6 +182,8 @@ * Encode ArrayBuffer or TypedArray to string with code (like output of legacy atob) | ||
function arrayBufferToString(data) { | ||
exports.hexStringToArrayBuffer = hexStringToArrayBuffer; | ||
var arrayBufferToString = function arrayBufferToString(data) { | ||
var bytes = sanitizeTypedArrayAndArrayBuffer(data); | ||
return String.fromCharCode.apply(null, bytes); | ||
} | ||
}; | ||
/** | ||
@@ -191,3 +196,5 @@ * Decode string with code (like output of legacy atob) to Uint8Array | ||
function stringToArrayBuffer(str) { | ||
exports.arrayBufferToString = arrayBufferToString; | ||
var stringToArrayBuffer = function stringToArrayBuffer(str) { | ||
if (!str || !(typeof str === 'string')) throw new Error('Input arg must be a non-null string'); | ||
@@ -201,2 +208,4 @@ var bytes = new Uint8Array(str.length); | ||
return bytes; | ||
} | ||
}; | ||
exports.stringToArrayBuffer = stringToArrayBuffer; |
@@ -6,4 +6,3 @@ "use strict"; | ||
}); | ||
exports.getEnvBtoa = getEnvBtoa; | ||
exports.getEnvAtob = getEnvAtob; | ||
exports.getEnvAtob = exports.getEnvBtoa = void 0; | ||
@@ -14,12 +13,16 @@ /** | ||
*/ | ||
function getEnvBtoa() { | ||
var getEnvBtoa = function getEnvBtoa() { | ||
if (typeof window !== 'undefined') return window.btoa; // browser | ||
else return nodeBtoa; // node | ||
} | ||
}; | ||
function getEnvAtob() { | ||
exports.getEnvBtoa = getEnvBtoa; | ||
var getEnvAtob = function getEnvAtob() { | ||
if (typeof window !== 'undefined') return window.atob; // browser | ||
else return nodeAtob; // node | ||
} | ||
}; | ||
exports.getEnvAtob = getEnvAtob; | ||
var nodeBtoa = function nodeBtoa(str) { | ||
@@ -26,0 +29,0 @@ if (typeof Buffer !== 'undefined') { |
@@ -8,4 +8,3 @@ "use strict"; | ||
}); | ||
exports.pemToBin = pemToBin; | ||
exports.binToPem = binToPem; | ||
exports.binToPem = exports.pemToBin = void 0; | ||
@@ -30,6 +29,6 @@ var encoder = _interopRequireWildcard(require("./encoder.js")); | ||
function pemToBin(keydataB64Pem) { | ||
var pemToBin = function pemToBin(keydataB64Pem) { | ||
var keydataB64 = dearmorPem(keydataB64Pem); | ||
return encoder.decodeBase64(keydataB64); | ||
} | ||
}; | ||
/** | ||
@@ -43,6 +42,8 @@ * Convert ArrayBuffer or TypedArray to PEM armored string with a specified type | ||
function binToPem(keydata, type) { | ||
exports.pemToBin = pemToBin; | ||
var binToPem = function binToPem(keydata, type) { | ||
var keydataB64 = encoder.encodeBase64(keydata); | ||
return formatAsPem(keydataB64, type); | ||
} | ||
}; | ||
/** | ||
@@ -56,3 +57,5 @@ * Armor the given Base64 string and return PEM formatted string | ||
function formatAsPem(str, type) { | ||
exports.binToPem = binToPem; | ||
var formatAsPem = function formatAsPem(str, type) { | ||
if (!str || !(typeof str === 'string')) throw new Error('Input arg must be a non-null string'); | ||
@@ -71,3 +74,3 @@ if (!type || !(typeof type === 'string')) throw new Error('Input arg must be a non-null string'); | ||
return finalString; | ||
} | ||
}; | ||
/** | ||
@@ -80,3 +83,3 @@ * Dearmor the given PEM string and return Base64 string | ||
function dearmorPem(str) { | ||
var dearmorPem = function dearmorPem(str) { | ||
if (!str || !(typeof str === 'string')) throw new Error('Input arg must be a non-null string'); // const beginRegExp = RegExp('^-----[\s]*BEGIN[^-]*KEY-----$', 'gm'); | ||
@@ -95,2 +98,2 @@ // const endRegExp = RegExp('^-----[\s]*END[^-]*KEY-----$', 'gm'); | ||
} | ||
} | ||
}; |
@@ -1,1 +0,1 @@ | ||
!function(r,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.jseu=n():r.jseu=n()}(this,function(){return function(r){var n={};function t(e){if(n[e])return n[e].exports;var o=n[e]={i:e,l:!1,exports:{}};return r[e].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=r,t.c=n,t.d=function(r,n,e){t.o(r,n)||Object.defineProperty(r,n,{enumerable:!0,get:e})},t.r=function(r){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})},t.t=function(r,n){if(1&n&&(r=t(r)),8&n)return r;if(4&n&&"object"==typeof r&&r&&r.__esModule)return r;var e=Object.create(null);if(t.r(e),Object.defineProperty(e,"default",{enumerable:!0,value:r}),2&n&&"string"!=typeof r)for(var o in r)t.d(e,o,function(n){return r[n]}.bind(null,o));return e},t.n=function(r){var n=r&&r.__esModule?function(){return r.default}:function(){return r};return t.d(n,"a",n),n},t.o=function(r,n){return Object.prototype.hasOwnProperty.call(r,n)},t.p="/home/circleci/repo/dist",t(t.s=0)}([function(r,n,t){r.exports=t(1)},function(r,n,t){"use strict";t.r(n);var e={};t.r(e),t.d(e,"encodeBase64",function(){return i}),t.d(e,"decodeBase64",function(){return a}),t.d(e,"encodeBase64Url",function(){return p}),t.d(e,"decodeBase64Url",function(){return s}),t.d(e,"arrayBufferToHexString",function(){return d}),t.d(e,"hexStringToArrayBuffer",function(){return l}),t.d(e,"arrayBufferToString",function(){return y}),t.d(e,"stringToArrayBuffer",function(){return g});var o={};t.r(o),t.d(o,"pemToBin",function(){return w}),t.d(o,"binToPem",function(){return E});var u=function(r){if("undefined"!=typeof Buffer){var n=Object.prototype.toString.call(r).slice(8,-1);return(Buffer.isBuffer(r)?r:["ArrayBuffer","TypedArray","Uint8Array","Int8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array"].indexOf(n)>=0?Buffer.from(r):Buffer.from(r.toString(),"binary")).toString("base64")}throw new Error("UnsupportedEnvironment")},f=function(r){if("undefined"!=typeof Buffer)return Buffer.from(r,"base64").toString("binary");throw new Error("UnsupportedEnvironment")};function i(r){var n="";return n="string"==typeof r?r:y(r),("undefined"!=typeof window?window.btoa:u)(n)}function a(r){return function(r){if(r instanceof Uint8Array){for(var n=!0,t=0;t<r.length;t++)if(r[t]>126||r[t]<32&&13!==r[t]&&10!==r[t]){n=!1;break}var e=null;if(n){e="";for(var o=0;o<r.length;o++)e+=String.fromCharCode(r[o])}else e=r;return e}throw new Error("Input data must be an Uint8Array")}(g(("undefined"!=typeof window?window.atob:f)(r)))}function c(r){if(r instanceof Uint8Array)return r;if(ArrayBuffer.isView(r)&&void 0!==r.buffer)return new Uint8Array(r.buffer);if(r instanceof ArrayBuffer)return new Uint8Array(r);throw new Error("Input must be an ArrayBuffer or a TypedArray")}function p(r){return i(r).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function s(r){if("string"!=typeof r)throw new Error("Input must be string");return a(r=r.replace(/-/g,"+").replace(/_/g,"/"))}function d(r){for(var n=c(r),t="",e=0;e<n.length;e++){var o=(255&n[e]).toString(16);t+=o=1===o.length?"0".concat(o):o}return t}function l(r){if(!r||"string"!=typeof r)throw new Error("Input arg must be a non-null string");for(var n=[],t=r.length,e=0;e<t;e+=2)n.push(parseInt(r.substr(e,2),16));return new Uint8Array(n)}function y(r){var n=c(r);return String.fromCharCode.apply(null,n)}function g(r){if(!r||"string"!=typeof r)throw new Error("Input arg must be a non-null string");for(var n=new Uint8Array(r.length),t=0;t<r.length;t++)n[t]=r.charCodeAt(t);return n}var b={public:"PUBLIC KEY",private:"PRIVATE KEY",encryptedPrivate:"ENCRYPTED PRIVATE KEY",certificate:"CERTIFICATE",certRequest:"CERTIFICATE REQUEST"};function w(r){return a(function(r){if(!r||"string"!=typeof r)throw new Error("Input arg must be a non-null string");var n=RegExp("^-----[s]*BEGIN[^-]*-----$","gm"),t=RegExp("^-----[s]*END[^-]*-----$","gm");try{var e=r.split(n)[1].split(t)[0];return e=e.replace(/\r?\n/g,"")}catch(r){throw new Error("Invalid format as PEM")}}(r))}function E(r,n){return function(r,n){if(!r||"string"!=typeof r)throw new Error("Input arg must be a non-null string");if(!n||"string"!=typeof n)throw new Error("Input arg must be a non-null string");if(Object.keys(b).indexOf(n)<0)throw new Error("Unsupported type");var t=b[n],e="-----BEGIN ".concat(t,"-----\n");for(;r.length>0;)e+="".concat(r.substring(0,64),"\n"),r=r.substring(64);return e="".concat(e,"-----END ").concat(t,"-----")}(i(r),n)}t.d(n,"encoder",function(){return e}),t.d(n,"formatter",function(){return o});n.default={encoder:e,formatter:o}}])}); | ||
!function(r,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.jseu=n():r.jseu=n()}(this,function(){return function(r){var n={};function t(e){if(n[e])return n[e].exports;var o=n[e]={i:e,l:!1,exports:{}};return r[e].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=r,t.c=n,t.d=function(r,n,e){t.o(r,n)||Object.defineProperty(r,n,{enumerable:!0,get:e})},t.r=function(r){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})},t.t=function(r,n){if(1&n&&(r=t(r)),8&n)return r;if(4&n&&"object"==typeof r&&r&&r.__esModule)return r;var e=Object.create(null);if(t.r(e),Object.defineProperty(e,"default",{enumerable:!0,value:r}),2&n&&"string"!=typeof r)for(var o in r)t.d(e,o,function(n){return r[n]}.bind(null,o));return e},t.n=function(r){var n=r&&r.__esModule?function(){return r.default}:function(){return r};return t.d(n,"a",n),n},t.o=function(r,n){return Object.prototype.hasOwnProperty.call(r,n)},t.p="/home/circleci/repo/dist",t(t.s=0)}([function(r,n,t){r.exports=t(1)},function(r,n,t){"use strict";t.r(n);var e={};t.r(e),t.d(e,"encodeBase64",function(){return i}),t.d(e,"decodeBase64",function(){return a}),t.d(e,"encodeBase64Url",function(){return s}),t.d(e,"decodeBase64Url",function(){return d}),t.d(e,"arrayBufferToHexString",function(){return l}),t.d(e,"hexStringToArrayBuffer",function(){return y}),t.d(e,"arrayBufferToString",function(){return g}),t.d(e,"stringToArrayBuffer",function(){return b});var o={};t.r(o),t.d(o,"pemToBin",function(){return E}),t.d(o,"binToPem",function(){return m});var u=function(r){if("undefined"!=typeof Buffer){var n=Object.prototype.toString.call(r).slice(8,-1);return(Buffer.isBuffer(r)?r:["ArrayBuffer","TypedArray","Uint8Array","Int8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array"].indexOf(n)>=0?Buffer.from(r):Buffer.from(r.toString(),"binary")).toString("base64")}throw new Error("UnsupportedEnvironment")},f=function(r){if("undefined"!=typeof Buffer)return Buffer.from(r,"base64").toString("binary");throw new Error("UnsupportedEnvironment")},i=function(r){var n="";return n="string"==typeof r?r:g(r),("undefined"!=typeof window?window.btoa:u)(n)},a=function(r){var n=("undefined"!=typeof window?window.atob:f)(r),t=b(n);return p(t)},c=function(r){if(r instanceof Uint8Array)return r;if(ArrayBuffer.isView(r)&&void 0!==r.buffer)return new Uint8Array(r.buffer);if(r instanceof ArrayBuffer)return new Uint8Array(r);throw new Error("Input must be an ArrayBuffer or a TypedArray")},p=function(r){if(r instanceof Uint8Array){for(var n=!0,t=0;t<r.length;t++)if(r[t]>126||r[t]<32&&13!==r[t]&&10!==r[t]){n=!1;break}var e=null;if(n){e="";for(var o=0;o<r.length;o++)e+=String.fromCharCode(r[o])}else e=r;return e}throw new Error("Input data must be an Uint8Array")},s=function(r){return i(r).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")},d=function(r){if("string"!=typeof r)throw new Error("Input must be string");return r=r.replace(/-/g,"+").replace(/_/g,"/"),a(r)},l=function(r){for(var n=c(r),t="",e=0;e<n.length;e++){var o=(255&n[e]).toString(16);t+=o=1===o.length?"0".concat(o):o}return t},y=function(r){if(!r||"string"!=typeof r)throw new Error("Input arg must be a non-null string");for(var n=[],t=r.length,e=0;e<t;e+=2)n.push(parseInt(r.substr(e,2),16));return new Uint8Array(n)},g=function(r){var n=c(r);return String.fromCharCode.apply(null,n)},b=function(r){if(!r||"string"!=typeof r)throw new Error("Input arg must be a non-null string");for(var n=new Uint8Array(r.length),t=0;t<r.length;t++)n[t]=r.charCodeAt(t);return n},w={public:"PUBLIC KEY",private:"PRIVATE KEY",encryptedPrivate:"ENCRYPTED PRIVATE KEY",certificate:"CERTIFICATE",certRequest:"CERTIFICATE REQUEST"},E=function(r){var n=h(r);return a(n)},m=function(r,n){var t=i(r);return v(t,n)},v=function(r,n){if(!r||"string"!=typeof r)throw new Error("Input arg must be a non-null string");if(!n||"string"!=typeof n)throw new Error("Input arg must be a non-null string");if(Object.keys(w).indexOf(n)<0)throw new Error("Unsupported type");for(var t=w[n],e="-----BEGIN ".concat(t,"-----\n");r.length>0;)e+="".concat(r.substring(0,64),"\n"),r=r.substring(64);return e="".concat(e,"-----END ").concat(t,"-----")},h=function(r){if(!r||"string"!=typeof r)throw new Error("Input arg must be a non-null string");var n=RegExp("^-----[s]*BEGIN[^-]*-----$","gm"),t=RegExp("^-----[s]*END[^-]*-----$","gm");try{var e=r.split(n)[1].split(t)[0];return e=e.replace(/\r?\n/g,"")}catch(r){throw new Error("Invalid format as PEM")}};t.d(n,"encoder",function(){return e}),t.d(n,"formatter",function(){return o});n.default={encoder:e,formatter:o}}])}); |
{ | ||
"name": "js-encoding-utils", | ||
"version": "0.3.6", | ||
"version": "0.4.0", | ||
"description": "Miscellaneous Encoding Utilities for Crypto-related Objects in JavaScript", | ||
@@ -44,12 +44,12 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@babel/cli": "7.4.3", | ||
"@babel/core": "7.4.3", | ||
"@babel/plugin-transform-regenerator": "7.4.3", | ||
"@babel/plugin-transform-runtime": "7.4.3", | ||
"@babel/polyfill": "7.4.3", | ||
"@babel/preset-env": "7.4.3", | ||
"@babel/register": "7.4.0", | ||
"@babel/cli": "7.4.4", | ||
"@babel/core": "7.4.4", | ||
"@babel/plugin-transform-regenerator": "7.4.4", | ||
"@babel/plugin-transform-runtime": "7.4.4", | ||
"@babel/polyfill": "7.4.4", | ||
"@babel/preset-env": "7.4.4", | ||
"@babel/register": "7.4.4", | ||
"babel-eslint": "10.0.1", | ||
"babel-loader": "8.0.5", | ||
"babel-plugin-istanbul": "5.1.1", | ||
"babel-plugin-istanbul": "5.1.4", | ||
"babel-plugin-transform-remove-console": "6.9.4", | ||
@@ -60,4 +60,4 @@ "can-npm-publish": "1.3.1", | ||
"cross-env": "5.2.0", | ||
"jsdom": "14.0.0", | ||
"karma": "4.0.1", | ||
"jsdom": "15.0.0", | ||
"karma": "4.1.0", | ||
"karma-chrome-launcher": "2.2.0", | ||
@@ -70,7 +70,7 @@ "karma-cli": "2.0.0", | ||
"lcov-result-merger": "^3.1.0", | ||
"mocha": "6.1.2", | ||
"mocha": "6.1.4", | ||
"mocha-sinon": "2.1.0", | ||
"nyc": "13.3.0", | ||
"webpack": "4.29.6", | ||
"webpack-cli": "3.3.0", | ||
"nyc": "14.1.0", | ||
"webpack": "4.30.0", | ||
"webpack-cli": "3.3.1", | ||
"webpack-common-shake": "2.1.0", | ||
@@ -77,0 +77,0 @@ "webpack-merge": "4.2.1" |
22625
276