@hexagon/base64
Advanced tools
Comparing version 1.1.27 to 1.1.28
@@ -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=>{const 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);const base64={};base64.toArrayBuffer=(data,urlMode)=>{const len=data.length;let bufferLength=data.length*.75,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)=>{const bytes=new Uint8Array(arrBuf);let i,result="";const len=bytes.length,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}); | ||
(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=>{const lookupTemp=typeof Uint8Array==="undefined"?[]:new Uint8Array(256);const len=chars.length;for(let i=0;i<len;i++){lookupTemp[target.charCodeAt(i)]=i}return lookupTemp},lookup=genLookup(chars),lookupUrl=genLookup(charsUrl);const base64UrlPattern=/^[-A-Za-z0-9\-_]*$/;const base64Pattern=/^[-A-Za-z0-9+/]*={0,3}$/;const base64={};base64.toArrayBuffer=(data,urlMode)=>{const len=data.length;let bufferLength=data.length*.75,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)=>{const bytes=new Uint8Array(arrBuf);let i,result="";const len=bytes.length,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]}const remainder=len%3;if(remainder===2){result=result.substring(0,result.length-1)+(urlMode?"":"=")}else if(remainder===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{return urlMode?base64UrlPattern.test(encoded):base64Pattern.test(encoded)}catch(_e){return false}};base64.base64=base64;return base64}); |
{ | ||
"name": "@hexagon/base64", | ||
"version": "1.1.27", | ||
"version": "1.1.28", | ||
"description": "Base64 and base64url to string or arraybuffer, and back. Node, Deno or browser.", | ||
@@ -13,3 +13,3 @@ "author": "Hexagon <github.com/hexagon>", | ||
], | ||
"homepage": "https://hexagon.github.io/base64", | ||
"homepage": "https://base64.56k.guru", | ||
"repository": { | ||
@@ -50,4 +50,3 @@ "type": "git", | ||
"build:typings": "tsc", | ||
"build:cleanup": "(rm dist/base64.mjs || del dist\\base64.mjs)", | ||
"build:docs": "(rm -rf docs/* || rd /S /Q docs) && jsdoc -c .jsdoc.json" | ||
"build:cleanup": "(rm dist/base64.mjs || del dist\\base64.mjs)" | ||
}, | ||
@@ -71,3 +70,3 @@ "type": "module", | ||
"rollup": "^3.27.2", | ||
"typescript": "^5.1.6", | ||
"typescript": "^5.2.2", | ||
"uglify-js": "^3.17.4", | ||
@@ -74,0 +73,0 @@ "uvu": "^0.5.6" |
114
README.md
@@ -22,80 +22,4 @@ <p align="center"> | ||
## Installation | ||
### Node.js | ||
```npm install @hexagon/base64 --save``` | ||
JavaScript | ||
```javascript | ||
// ESM Import ... | ||
import base64 from "@hexagon/base64"; | ||
// ... or CommonJS Require | ||
const base64 = require("@hexagon/base64"); | ||
``` | ||
TypeScript | ||
*Note that only default export is available in Node.js TypeScript, as the commonjs module is used internally.* | ||
```typescript | ||
import base64 from "@hexagon/base64"; | ||
// ... | ||
``` | ||
### Deno | ||
JavaScript | ||
```javascript | ||
import base64 from "https://deno.land/x/b64@1.1.27/src/base64.js"; | ||
// ... | ||
``` | ||
TypeScript | ||
```typescript | ||
import { base64 } from "https://deno.land/x/b64@1.1.27/src/base64.js"; | ||
// ... | ||
``` | ||
### Browser | ||
#### Manual | ||
* Download latest [zipball](https://github.com/Hexagon/base64/archive/refs/heads/master.zip) | ||
* Unpack | ||
* Grab ```base64.min.js``` (UMD and standalone) or ```base64.min.mjs``` (ES-module) from the [dist/](/dist) folder | ||
#### CDN | ||
To use as a [UMD](https://github.com/umdjs/umd)-module (stand alone, [RequireJS](https://requirejs.org/) etc.) | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/@hexagon/base64@1/dist/base64.min.js"></script> | ||
``` | ||
To use as an [ES-module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) | ||
```html | ||
<script type="module"> | ||
import base64 from "https://cdn.jsdelivr.net/npm/@hexagon/base64@1/dist/base64.min.mjs"; | ||
// ... see usage section ... | ||
</script> | ||
``` | ||
## Documentation | ||
Full documentation available at [hexagon.github.io/base64](https://hexagon.github.io/base64/). | ||
### Examples | ||
Assuming you have imported base64 as described under 'Installation'. | ||
```javascript | ||
// Encode string as regular base64 | ||
@@ -110,34 +34,16 @@ const example1enc = base64.fromString("Hellö Wörld, how are you doing today?!"); | ||
// > Hellö Wörld, how are you doing today?! | ||
``` | ||
// Encode string as base64url (setting the second parameter to true gives base64url) | ||
const example2enc = base64.fromString("Hellö Wörld, how are you doing today?!", true); | ||
console.log(example2enc); | ||
// > SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ | ||
Full documentation available at [base64.56k.guru](https://base64.56k.guru) | ||
// Decode string as base64url (setting the second parameter to true takes base64url) | ||
const example2dec = base64.toString("SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ", true); | ||
console.log(example2dec); | ||
// > Hellö Wörld, how are you doing today?! | ||
## Quick Installation | ||
// Check if string is base64url (setting the second parameter to true validates base64url) | ||
const example3valid = base64.validate("SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ", true); | ||
console.log(example3valid); | ||
// > true | ||
Node.js: `npm install @hexagon/base64 --save` | ||
// Check if string is base64 | ||
const example4valid = base64.validate("SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk_IQ"); | ||
console.log(example4valid); | ||
// > false | ||
Deno: `import base64 from "https://deno.land/x/b64@1.1.28/src/base64.js";` | ||
// Check if string is base64 | ||
const example5valid = base64.validate("SGVsbMO2IFfDtnJsZCwgaG93IGFyZSB5b3UgZG9pbmcgdG9kYXk/IQ=="); | ||
console.log(example5valid); | ||
// > true | ||
For browser/cdn usage, refer to the documentation. | ||
``` | ||
### Quick API | ||
### Full API | ||
The library encodes and decodes base64/base64url to ArrayBuffers and vice-versa | ||
- __fromArrayBuffer(buffer, urlMode)__ - Encodes `ArrayBuffer` into base64 or base64url if urlMode(optional) is true | ||
@@ -153,4 +59,8 @@ - __toArrayBuffer(str, urlMode)__ - Decodes base64url string (or base64url string if urlMode is true) to `ArrayBuffer` | ||
See [Contribution Guide](/CONTRIBUTING.md) | ||
See [Contribution Guide](https://base64.56k.guru/contributing.html) | ||
## Donations | ||
If you found this library helpful and wish to support its development, consider making a donation through [Hexagon's GitHub Sponsors page](https://github.com/sponsors/hexagon). Your generosity ensures the library's continued development and maintenance. | ||
### Contributors | ||
@@ -157,0 +67,0 @@ |
@@ -38,3 +38,4 @@ /* ------------------------------------------------------------------------------------ | ||
const lookupTemp = typeof Uint8Array === "undefined" ? [] : new Uint8Array(256); | ||
for (let i = 0; i < chars.length; i++) { | ||
const len = chars.length; | ||
for (let i = 0; i < len; i++) { | ||
lookupTemp[target.charCodeAt(i)] = i; | ||
@@ -50,2 +51,8 @@ } | ||
/** | ||
* Pre-calculated regexes for validating base64 and base64url | ||
*/ | ||
const base64UrlPattern = /^[-A-Za-z0-9\-_]*$/; | ||
const base64Pattern = /^[-A-Za-z0-9+/]*={0,3}$/; | ||
/** | ||
* @namespace base64 | ||
@@ -102,3 +109,3 @@ */ | ||
/** | ||
* Convenience function for converting base64 encoded string to an ArrayBuffer instance | ||
* Convenience function for creating a base64 encoded string from an ArrayBuffer instance | ||
* @public | ||
@@ -127,5 +134,6 @@ * | ||
if (len % 3 === 2) { | ||
const remainder = len % 3; | ||
if (remainder === 2) { | ||
result = result.substring(0, result.length - 1) + (urlMode ? "" : "="); | ||
} else if (len % 3 === 1) { | ||
} else if (remainder === 1) { | ||
result = result.substring(0, result.length - 2) + (urlMode ? "" : "=="); | ||
@@ -135,2 +143,3 @@ } | ||
return result; | ||
}; | ||
@@ -178,7 +187,3 @@ | ||
try { | ||
if (urlMode) { | ||
return /^[-A-Za-z0-9\-_]*$/.test(encoded); | ||
} else { | ||
return /^[-A-Za-z0-9+/]*={0,3}$/.test(encoded); | ||
} | ||
return urlMode ? base64UrlPattern.test(encoded) : base64Pattern.test(encoded); | ||
} catch (_e) { | ||
@@ -185,0 +190,0 @@ return false; |
@@ -13,3 +13,3 @@ export default base64; | ||
/** | ||
* Convenience function for converting base64 encoded string to an ArrayBuffer instance | ||
* Convenience function for creating a base64 encoded string from an ArrayBuffer instance | ||
* @public | ||
@@ -16,0 +16,0 @@ * |
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
373
31309
70