@hexagon/base64
Advanced tools
Comparing version 1.0.20 to 1.1.21
@@ -1,1 +0,1 @@ | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.base64=factory())})(this,function(){"use strict";const chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",charsUrl="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",genLookup=target=>{let lookupTemp=typeof Uint8Array==="undefined"?[]:new Uint8Array(256);for(let i=0;i<chars.length;i++){lookupTemp[target.charCodeAt(i)]=i}return lookupTemp},lookup=genLookup(chars),lookupUrl=genLookup(charsUrl);let base64={};base64.toArrayBuffer=(data,urlMode)=>{let bufferLength=data.length*.75,len=data.length,i,p=0,encoded1,encoded2,encoded3,encoded4;if(data[data.length-1]==="="){bufferLength--;if(data[data.length-2]==="="){bufferLength--}}const arraybuffer=new ArrayBuffer(bufferLength),bytes=new Uint8Array(arraybuffer),target=urlMode?lookupUrl:lookup;for(i=0;i<len;i+=4){encoded1=target[data.charCodeAt(i)];encoded2=target[data.charCodeAt(i+1)];encoded3=target[data.charCodeAt(i+2)];encoded4=target[data.charCodeAt(i+3)];bytes[p++]=encoded1<<2|encoded2>>4;bytes[p++]=(encoded2&15)<<4|encoded3>>2;bytes[p++]=(encoded3&3)<<6|encoded4&63}return arraybuffer};base64.fromArrayBuffer=(arrBuf,urlMode)=>{let bytes=new Uint8Array(arrBuf),i,len=bytes.length,result="",target=urlMode?charsUrl:chars;for(i=0;i<len;i+=3){result+=target[bytes[i]>>2];result+=target[(bytes[i]&3)<<4|bytes[i+1]>>4];result+=target[(bytes[i+1]&15)<<2|bytes[i+2]>>6];result+=target[bytes[i+2]&63]}if(len%3===2){result=result.substring(0,result.length-1)+(urlMode?"":"=")}else if(len%3===1){result=result.substring(0,result.length-2)+(urlMode?"":"==")}return result};base64.toString=(str,urlMode)=>{return(new TextDecoder).decode(base64.toArrayBuffer(str,urlMode))};base64.fromString=(str,urlMode)=>{return base64.fromArrayBuffer((new TextEncoder).encode(str),urlMode)};base64.base64=base64;return base64}); | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.base64=factory())})(this,function(){"use strict";const chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",charsUrl="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",genLookup=target=>{let lookupTemp=typeof Uint8Array==="undefined"?[]:new Uint8Array(256);for(let i=0;i<chars.length;i++){lookupTemp[target.charCodeAt(i)]=i}return lookupTemp},lookup=genLookup(chars),lookupUrl=genLookup(charsUrl);let base64={};base64.toArrayBuffer=(data,urlMode)=>{let bufferLength=data.length*.75,len=data.length,i,p=0,encoded1,encoded2,encoded3,encoded4;if(data[data.length-1]==="="){bufferLength--;if(data[data.length-2]==="="){bufferLength--}}const arraybuffer=new ArrayBuffer(bufferLength),bytes=new Uint8Array(arraybuffer),target=urlMode?lookupUrl:lookup;for(i=0;i<len;i+=4){encoded1=target[data.charCodeAt(i)];encoded2=target[data.charCodeAt(i+1)];encoded3=target[data.charCodeAt(i+2)];encoded4=target[data.charCodeAt(i+3)];bytes[p++]=encoded1<<2|encoded2>>4;bytes[p++]=(encoded2&15)<<4|encoded3>>2;bytes[p++]=(encoded3&3)<<6|encoded4&63}return arraybuffer};base64.fromArrayBuffer=(arrBuf,urlMode)=>{let bytes=new Uint8Array(arrBuf),i,len=bytes.length,result="",target=urlMode?charsUrl:chars;for(i=0;i<len;i+=3){result+=target[bytes[i]>>2];result+=target[(bytes[i]&3)<<4|bytes[i+1]>>4];result+=target[(bytes[i+1]&15)<<2|bytes[i+2]>>6];result+=target[bytes[i+2]&63]}if(len%3===2){result=result.substring(0,result.length-1)+(urlMode?"":"=")}else if(len%3===1){result=result.substring(0,result.length-2)+(urlMode?"":"==")}return result};base64.toString=(str,urlMode)=>{return(new TextDecoder).decode(base64.toArrayBuffer(str,urlMode))};base64.fromString=(str,urlMode)=>{return base64.fromArrayBuffer((new TextEncoder).encode(str),urlMode)};base64.validate=(encoded,urlMode)=>{if(!(typeof encoded==="string"||encoded instanceof String)){return false}try{if(urlMode){return/^[-A-Za-z0-9\-_]*$/.test(encoded)}else{return/^[-A-Za-z0-9+/]*={0,3}$/.test(encoded)}}catch(_e){return false}};base64.base64=base64;return base64}); |
{ | ||
"name": "@hexagon/base64", | ||
"version": "1.0.20", | ||
"version": "1.1.21", | ||
"description": "Base64 and base64url to string or arraybuffer, and back. Node, Deno or browser.", | ||
@@ -5,0 +5,0 @@ "author": "Hexagon <github.com/hexagon>", |
@@ -8,3 +8,3 @@ <p align="center"> | ||
Base64 and base64url to string or arraybuffer, and back. Works in Node, Deno or browser. | ||
Encode, decode and validate base64 and base64url to string or arraybuffer, and back. Works in Node, Deno or browser. | ||
@@ -17,2 +17,3 @@ [![Node.js CI](https://github.com/Hexagon/base64/actions/workflows/node.js.yml/badge.svg)](https://github.com/Hexagon/base64/actions/workflows/node.js.yml) [![Deno CI](https://github.com/Hexagon/base64/actions/workflows/deno.yml/badge.svg)](https://github.com/Hexagon/base64/actions/workflows/deno.yml) | ||
* Convert to/from string or arraybuffer | ||
* Validate / identify base64 and base64url | ||
* Works in Node.js >=4.0 (both require and import). | ||
@@ -120,2 +121,18 @@ * Works in Deno >=1.16. | ||
// > Hellö Wörld, how are you doing today?! | ||
// Check if string is base64url (setting the second parameter to true validates base64url) | ||
const example3valid = base64.validate("SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ", true); | ||
console.log(example3valid); | ||
// > true | ||
// Check if string is base64 | ||
const example4valid = base64.validate("SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ"); | ||
console.log(example4valid); | ||
// > false | ||
// Check if string is base64 | ||
const example5valid = base64.validate("SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk/IQ=="); | ||
console.log(example5valid); | ||
// > true | ||
``` | ||
@@ -127,13 +144,9 @@ | ||
- __fromArrayBuffer(buffer)__ - Encodes `ArrayBuffer` into base64 string | ||
- __toArrayBuffer(str)__ - Decodes base64 string to `ArrayBuffer` | ||
- __fromArrayBuffer(buffer, urlMode)__ - Encodes `ArrayBuffer` into base64 or base64url if urlMode (optional) is true | ||
- __toArrayBuffer(str, urlMode)__ - Decodes base64url string (or base64url string if urlMode is true) to `ArrayBuffer` | ||
- __fromArrayBuffer(buffer, true)__ - Encodes `ArrayBuffer` into base64url string | ||
- __toArrayBuffer(str, true)__ - Decodes base64url string to `ArrayBuffer` | ||
- __fromString(str)__ - Encodes `String` into base64 string | ||
- __toString(str)__ - Decodes base64 string to `String` | ||
- __fromString(buffer, true)__ - Encodes `String` into base64url string | ||
- __toString(str, true)__ - Decodes base64url string to `String` | ||
- __validate(str, urlMode)__ - Returns true if `String` str is valid base64/base64 dependending on urlMode | ||
@@ -140,0 +153,0 @@ ## Contributing |
@@ -150,4 +150,30 @@ /* ------------------------------------------------------------------------------------ | ||
/** | ||
* Function to validate base64 | ||
* @public | ||
* @param {string} encoded - Base64 or Base64url encoded data | ||
* @param {boolean} [urlMode] - If set to true, base64url will be expected | ||
* @returns {boolean} - Valid base64/base64url? | ||
*/ | ||
base64.validate = (encoded, urlMode) => { | ||
// Bail out if not string | ||
if (!(typeof encoded === "string" || encoded instanceof String)) { | ||
return false; | ||
} | ||
// Go on validate | ||
try { | ||
if (urlMode) { | ||
return /^[-A-Za-z0-9\-_]*$/.test(encoded); | ||
} else { | ||
return /^[-A-Za-z0-9+/]*={0,3}$/.test(encoded); | ||
} | ||
} catch (_e) { | ||
return false; | ||
} | ||
}; | ||
base64.base64 = base64; | ||
export default base64; | ||
export { base64 }; |
@@ -39,3 +39,11 @@ export default base64; | ||
export function fromString(str: string, urlMode?: boolean): string; | ||
/** | ||
* Function to validate base64 | ||
* @public | ||
* @param {string} encoded - Base64 or Base64url encoded data | ||
* @param {boolean} [urlMode] - If set to true, base64url will be expected | ||
* @returns {boolean} - Valid base64/base64url? | ||
*/ | ||
export function validate(encoded: string, urlMode?: boolean): boolean; | ||
export { base64 }; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32141
355
160