diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 463d308..0000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,61 +0,0 @@ -# Welcome to base64 contributing guide - -## New contributor guide - -To get an overview of the project, read the [README](README.md). Here are some resources to help you get started with open source contributions: - -- [Set up Git](https://docs.github.com/en/get-started/quickstart/set-up-git) -- [GitHub flow](https://docs.github.com/en/get-started/quickstart/github-flow) -- [Collaborating with pull requests](https://docs.github.com/en/github/collaborating-with-pull-requests) - -## Getting started - -### Issues - -#### Create a new issue - -If you spot a problem with base64, [search if an issue already exists](https://docs.github.com/en/github/searching-for-information-on-github/searching-on-github/searching-issues-and-pull-requests#search-by-the-title-body-or-comments). If a related issue doesn't exist, you can open a new issue using a relevant [issue form](https://github.com/hexagon/base64/issues/new/choose). - -#### Solve an issue - -Scan through our [existing issues](https://github.com/hexagon/base64/issues) to find one that interests you. You can narrow down the search using `labels` as filters. If you find an issue to work on, make a note in the comments so er van assign it to you. Then you are welcome to open a PR with a fix. - -### Make Changes - -#### Setting up the environment - -Se recommend using VS Code with eslint extensions, which will automatically check your code against THE defined rules as you write it. - -1. Fork the repository. -- Using GitHub Desktop: - - [Getting started with GitHub Desktop](https://docs.github.com/en/desktop/installing-and-configuring-github-desktop/getting-started-with-github-desktop) will guide you through setting up Desktop. - - Once Desktop is set up, you can use it to [fork the repo](https://docs.github.com/en/desktop/contributing-and-collaborating-using-github-desktop/cloning-and-forking-repositories-from-github-desktop)! - -- Using the command line: - - [Fork the repo](https://docs.github.com/en/github/getting-started-with-github/fork-a-repo#fork-an-example-repository) so that you can make your changes without affecting the original project until you're ready to merge them. - -3. Install or update to **Node.js v16**. - -3. Create a working branch ```feature/my-cool-feature``` or ```bugfix/issue-14``` and start with your changes! - -### Commit your update - -Please run ```npm run build``` before committing, to update the dist-files, and to make sure every test and check passes. If you make changes to any function Interface, or to JSDoc in general, you should also run ```npm run build:docs``` to update the generated documentation. - -See [package.json](/package.json) scripts section for all available scripts. - -Then, commit the changes once you are happy with them. - -### Pull Request - -When you're finished with the changes, create a pull request, also known as a PR. -- Don't forget to [link PR to issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) if you are solving one. -- Enable the checkbox to [allow maintainer edits](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork) so the branch can be updated for a merge. -Once you submit your PR, a team member will review your proposal. We may ask questions or request for additional information. -- We may ask for changes to be made before a PR can be merged, either using [suggested changes](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/incorporating-feedback-in-your-pull-request) or pull request comments. You can apply suggested changes directly through the UI. You can make any other changes in your fork, then commit them to your branch. -- As you update your PR and apply changes, mark each conversation as [resolved](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/commenting-on-a-pull-request#resolving-conversations). -- If you run into any merge issues, checkout this [git tutorial](https://lab.github.com/githubtraining/managing-merge-conflicts) to help you resolve merge conflicts and other issues. - -### Success! - -This guide is based on [GitHub Docs CONTRIBUTING.md](https://github.com/github/docs/blob/main/CONTRIBUTING.md) diff --git a/README.md b/README.md index 22dee78..46eb1a1 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Full documentation available at [base64.56k.guru](https://base64.56k.guru) Node.js: `npm install @hexagon/base64 --save` -Deno: `import base64 from "https://deno.land/x/b64@1.1.27/src/base64.js";` +Deno: `import base64 from "https://deno.land/x/b64@1.1.28/src/base64.js";` For browser/cdn usage, refer to the documentation. diff --git a/dist/base64.cjs b/dist/base64.cjs index c499830..a3276d4 100644 --- a/dist/base64.cjs +++ b/dist/base64.cjs @@ -41,7 +41,8 @@ genLookup = (target) => { 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; } return lookupTemp; @@ -51,6 +52,12 @@ lookup = genLookup(chars), lookupUrl = genLookup(charsUrl); + /** + * Pre-calculated regexes for validating base64 and base64url + */ + const base64UrlPattern = /^[-A-Za-z0-9\-_]*$/; + const base64Pattern = /^[-A-Za-z0-9+/]*={0,3}$/; + /** * @namespace base64 */ @@ -103,7 +110,7 @@ }; /** - * Convenience function for converting base64 encoded string to an ArrayBuffer instance + * Convenience function for creating a base64 encoded string from an ArrayBuffer instance * @public * * @param {ArrayBuffer} arrBuf - ArrayBuffer to be encoded @@ -127,13 +134,15 @@ result += target[bytes[i + 2] & 63]; } - 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 ? "" : "=="); } return result; + }; /** @@ -176,11 +185,7 @@ // Go on validate 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) { return false; } diff --git a/dist/base64.min.js b/dist/base64.min.js index 7b5609f..361148c 100644 --- a/dist/base64.min.js +++ b/dist/base64.min.js @@ -1 +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{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>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>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}); \ No newline at end of file +(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{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>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>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}); \ No newline at end of file diff --git a/dist/base64.min.js.map b/dist/base64.min.js.map index ba00181..f119264 100644 --- a/dist/base64.min.js.map +++ b/dist/base64.min.js.map @@ -1 +1 @@ -{"version":3,"sources":["dist/base64.cjs"],"names":["global","factory","exports","module","define","amd","globalThis","self","base64","this","chars","charsUrl","genLookup","lookupTemp","Uint8Array","let","i","length","target","charCodeAt","lookup","lookupUrl","toArrayBuffer","data","urlMode","len","bufferLength","p","encoded1","encoded2","encoded3","encoded4","arraybuffer","ArrayBuffer","bytes","fromArrayBuffer","arrBuf","result","substring","toString","str","TextDecoder","decode","fromString","TextEncoder","encode","validate","encoded","String","test","_e"],"mappings":"CAAA,SAAWA,OAAQC,SAClB,OAAOC,UAAY,UAAY,OAAOC,SAAW,YAAcA,OAAOD,QAAUD,QAAQ,EACxF,OAAOG,SAAW,YAAcA,OAAOC,IAAMD,OAAOH,OAAO,GAC1DD,OAAS,OA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No newline at end of file +{"version":3,"sources":["dist/base64.cjs"],"names":["global","factory","exports","module","define","amd","globalThis","self","base64","this","chars","charsUrl","genLookup","lookupTemp","Uint8Array","len","length","let","i","target","charCodeAt","lookup","lookupUrl","base64UrlPattern","base64Pattern","toArrayBuffer","data","urlMode","bufferLength","p","encoded1","encoded2","encoded3","encoded4","arraybuffer","ArrayBuffer","bytes","fromArrayBuffer","arrBuf","result","remainder","substring","toString","str","TextDecoder","decode","fromString","TextEncoder","encode","validate","encoded","String","test","_e"],"mappings":"CAAA,SAAWA,OAAQC,SAClB,OAAOC,UAAY,UAAY,OAAOC,SAAW,YAAcA,OAAOD,QAAUD,QAAQ,EACxF,OAAOG,SAAW,YAAcA,OAAOC,IAAMD,OAAOH,OAAO,GAC1DD,OAAS,OAAOM,aAAe,YAAcA,WAAaN,QAAUO,KAAMP,OAAOQ,OAASP,QAAQ,EACnG,GAAEQ,KAAM,WAAe,aA8BvB,MAECC,MAAQ,mEAGRC,SAAW,mEAEXC,UAAY,SACX,MAAMC,WAAa,OAAOC,aAAe,YAAc,GAAK,IAAIA,WAAW,GAAG,EAC9E,MAAMC,IAAML,MAAMM,OAClB,IAAKC,IAAIC,EAAI,EAAGA,EAAIH,IAAKG,CAAC,GAAI,CAC7BL,WAAWM,OAAOC,WAAWF,CAAC,GAAKA,CACpC,CACA,OAAOL,UACR,EAGAQ,OAAST,UAAUF,KAAK,EACxBY,UAAYV,UAAUD,QAAQ,EAK/B,MAAMY,iBAAmB,qBACzB,MAAMC,cAAgB,0BAKtB,MAAMhB,OAAS,GAUfA,OAAOiB,cAAgB,CAACC,KAAMC,WAC7B,MACCZ,IAAMW,KAAKV,OACZC,IAAIW,aAAeF,KAAKV,OAAS,IAChCE,EACAW,EAAI,EACJC,SACAC,SACAC,SACAC,SAED,GAAIP,KAAKA,KAAKV,OAAS,KAAO,IAAK,CAClCY,YAAY,GACZ,GAAIF,KAAKA,KAAKV,OAAS,KAAO,IAAK,CAClCY,YAAY,EACb,CACD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AAK,KACnE,CAEA,OAAOY,MAER,EAUA/B,OAAOkC,SAAW,CAACC,IAAKhB,WACvB,OAAO,IAAIiB,aAAcC,OAAOrC,OAAOiB,cAAckB,IAAKhB,OAAO,CAAC,CACnE,EAUAnB,OAAOsC,WAAa,CAACH,IAAKhB,WACzB,OAAOnB,OAAO6B,iBAAgB,IAAIU,aAAcC,OAAOL,GAAG,EAAGhB,OAAO,CACrE,EASAnB,OAAOyC,SAAW,CAACC,QAASvB,WAG3B,GAAI,EAAE,OAAOuB,UAAY,UAAYA,mBAAmBC,QAAS,CAChE,OAAO,KACR,CAGA,IACC,OAAOxB,QAAUJ,iBAAiB6B,KAAKF,OAAO,EAAI1B,cAAc4B,KAAKF,OAAO,CAG7E,CAFE,MAAOG,IACR,OAAO,KACR,CACD,EAEA7C,OAAOA,OAASA,OAEhB,OAAOA,MAEP,CAAC"} \ No newline at end of file diff --git a/dist/base64.min.mjs b/dist/base64.min.mjs index 35d5538..7fa07b5 100644 --- a/dist/base64.min.mjs +++ b/dist/base64.min.mjs @@ -1 +1 @@ -const chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",charsUrl="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",genLookup=target=>{const lookupTemp=typeof Uint8Array==="undefined"?[]:new Uint8Array(256);for(let i=0;i{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>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>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;export{base64,base64 as default}; \ No newline at end of file +const chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",charsUrl="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",genLookup=target=>{const lookupTemp=typeof Uint8Array==="undefined"?[]:new Uint8Array(256);const len=chars.length;for(let i=0;i{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>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>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;export{base64,base64 as default}; \ No newline at end of file diff --git a/dist/base64.min.mjs.map b/dist/base64.min.mjs.map index fe2fb37..f19f5ff 100644 --- a/dist/base64.min.mjs.map +++ b/dist/base64.min.mjs.map @@ -1 +1 @@ -{"version":3,"sources":["dist/base64.mjs"],"names":["chars","charsUrl","genLookup","lookupTemp","Uint8Array","let","i","length","target","charCodeAt","lookup","lookupUrl","base64","toArrayBuffer","data","urlMode","len","bufferLength","p","encoded1","encoded2","encoded3","encoded4","arraybuffer","ArrayBuffer","bytes","fromArrayBuffer","arrBuf","result","substring","toString","str","TextDecoder","decode","fromString","TextEncoder","encode","validate","encoded","String","test","_e"],"mappings":"AA4BA,MAECA,MAAQ,mEAGRC,SAAW,mEAEXC,UAAY,SACX,MAAMC,WAAa,OAAOC,aAAe,YAAc,GAAK,IAAIA,WAAW,GAAG,EAC9E,IAAKC,IAAIC,EAAI,EAAGA,EAAIN,MAAMO,OAAQD,CAAC,GAAI,CACtCH,WAAWK,OAAOC,WAAWH,CAAC,GAAKA,CACpC,CACA,OAAOH,UACR,EAGAO,OAASR,UAAUF,KAAK,EACxBW,UAAYT,UAAUD,QAAQ,EAK/B,MAAMW,OAAS,GAUfA,OAAOC,cAAgB,CAACC,KAAMC,WAC7B,MACCC,IAAMF,KAAKP,OACZF,IAAIY,aAAeH,KAAKP,OAAS,IAChCD,EACAY,EAAI,EACJC,SACAC,SACAC,SACAC,SAED,GAAIR,KAAKA,KAAKP,OAAS,KAAO,IAAK,C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OAASA,OAAOC,UAAU,EAAGD,OAAOrB,OAAS,CAAC,GAAKQ,QAAU,GAAK,KACnE,CAEA,OAAOa,MACR,EAUAhB,OAAOkB,SAAW,CAACC,IAAKhB,WACvB,OAAO,IAAIiB,aAAcC,OAAOrB,OAAOC,cAAckB,IAAKhB,OAAO,CAAC,CACnE,EAUAH,OAAOsB,WAAa,CAACH,IAAKhB,WACzB,OAAOH,OAAOc,iBAAgB,IAAIS,aAAcC,OAAOL,GAAG,EAAGhB,OAAO,CACrE,EASAH,OAAOyB,SAAW,CAACC,QAASvB,WAG3B,GAAI,EAAE,OAAOuB,UAAY,UAAYA,mBAAmBC,QAAS,CAChE,OAAO,KACR,CAGA,IACC,GAAIxB,QAAS,CACZ,MAAO,qBAAqByB,KAAKF,OAAO,CACzC,KAAO,CACN,MAAO,0BAA0BE,KAAKF,OAAO,CAC9C,CAGD,CAFE,MAAOG,IACR,OAAO,KACR,CACD,EAEA7B,OAAOA,OAASA,cAEPA,OAAQA,iBAAmB"} \ No newline at end of file +{"version":3,"sources":["dist/base64.mjs"],"names":["chars","charsUrl","genLookup","lookupTemp","Uint8Array","len","length","let","i","target","charCodeAt","lookup","lookupUrl","base64UrlPattern","base64Pattern","base64","toArrayBuffer","data","urlMode","bufferLength","p","encoded1","encoded2","encoded3","encoded4","arraybuffer","ArrayBuffer","bytes","fromArrayBuffer","arrBuf","result","remainder","substring","toString","str","TextDecoder","decode","fromString","TextEncoder","encode","validate","encoded","String","test","_e"],"mappings":"AA4BA,MAECA,MAAQ,mEAGRC,SAAW,mEAEXC,UAAY,SACX,MAAMC,WAAa,OAAOC,aAAe,YAAc,GAAK,IAAIA,WAAW,GAAG,EAC9E,MAAMC,IAAML,MAAMM,OAClB,IAAKC,IAAIC,EAAI,EAAGA,EAAIH,IAAKG,CAAC,GAAI,CAC7BL,WAAWM,OAAOC,WAAWF,CAAC,GAAKA,CACpC,CACA,OAAOL,UACR,EAGAQ,OAAST,UAAUF,KAAK,EACxBY,UAAYV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AUrB,QAASkB,MAAMnB,EAAI,GAAK,KAAO,EAAMmB,MAAMnB,EAAI,IAAM,GAC/DsB,QAAUrB,OAAOkB,MAAMnB,EAAI,GAAK,GACjC,CAEA,MAAMuB,UAAY1B,IAAM,EACxB,GAAI0B,YAAc,EAAG,CACpBD,OAASA,OAAOE,UAAU,EAAGF,OAAOxB,OAAS,CAAC,GAAKY,QAAU,GAAK,IACnE,MAAO,GAAIa,YAAc,EAAG,CAC3BD,OAASA,OAAOE,UAAU,EAAGF,OAAOxB,OAAS,CAAC,GAAKY,QAAU,GAAK,KACnE,CAEA,OAAOY,MAER,EAUAf,OAAOkB,SAAW,CAACC,IAAKhB,WACvB,OAAO,IAAIiB,aAAcC,OAAOrB,OAAOC,cAAckB,IAAKhB,OAAO,CAAC,CACnE,EAUAH,OAAOsB,WAAa,CAACH,IAAKhB,WACzB,OAAOH,OAAOa,iBAAgB,IAAIU,aAAcC,OAAOL,GAAG,EAAGhB,OAAO,CACrE,EASAH,OAAOyB,SAAW,CAACC,QAASvB,WAG3B,GAAI,EAAE,OAAOuB,UAAY,UAAYA,mBAAmBC,QAAS,CAChE,OAAO,KACR,CAGA,IACC,OAAOxB,QAAUL,iBAAiB8B,KAAKF,OAAO,EAAI3B,cAAc6B,KAAKF,OAAO,CAG7E,CAFE,MAAOG,IACR,OAAO,KACR,CACD,EAEA7B,OAAOA,OAASA,cAEPA,OAAQA,iBAAmB"} \ No newline at end of file diff --git a/docs/contributing.md b/docs/contributing.md index 4cc4355..33a7f68 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -48,7 +48,7 @@ We recommend using VS Code with eslint extensions. It will automatically check y ### Commit your update -Please run ```npm run build``` before committing, to update the dist-files, and to make sure that every test and check passes. If you make changes to any function Interface, or to JSDoc in general, you should also run ```npm run build:docs``` to update the generated documentation. +Please run ```npm run build``` before committing, to update the dist-files, and to make sure that every test and check passes. See [package.json](/package.json) scripts section for all available scripts. diff --git a/docs/installation.md b/docs/installation.md index 61e6492..077ef95 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -40,14 +40,14 @@ import base64 from "@hexagon/base64"; JavaScript: ```javascript -import base64 from "https://deno.land/x/b64@1.1.27/src/base64.js"; +import base64 from "https://deno.land/x/b64@1.1.28/src/base64.js"; // ... ``` TypeScript: ``` -import { base64 } from "https://deno.land/x/b64@1.1.27/src/base64.js"; +import { base64 } from "https://deno.land/x/b64@1.1.28/src/base64.js"; // ... ``` diff --git a/package-lock.json b/package-lock.json index 8214b5f..dc2e059 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,19 @@ { "name": "@hexagon/base64", - "version": "1.1.27", + "version": "1.1.28", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@hexagon/base64", - "version": "1.1.27", + "version": "1.1.28", "license": "MIT", "devDependencies": { "c8": "^8.0.1", "eslint": "^8.46.0", "jsdoc": "^4.0.2", "rollup": "^3.27.2", - "typescript": "^5.1.6", + "typescript": "^5.2.2", "uglify-js": "^3.17.4", "uvu": "^0.5.6" } @@ -28,9 +28,9 @@ } }, "node_modules/@babel/parser": { - "version": "7.22.10", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.22.10.tgz", - "integrity": "sha512-lNbdGsQb9ekfsnjFGhEiF4hfFqGgfOP3H3d27re3n+CGhNuTSUEQdfWk556sTLNTloczcdM5TYF2LhzmDQKyvQ==", + "version": "7.22.16", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.22.16.tgz", + "integrity": "sha512-+gPfKv8UWeKKeJTUxe59+OobVcrYHETCsORl61EmSkmgymguYk/X5bp7GuUIXaFsc6y++v8ZxPsLSSuujqDphA==", "dev": true, "bin": { "parser": "bin/babel-parser.js" @@ -61,9 +61,9 @@ } }, "node_modules/@eslint-community/regexpp": { - "version": "4.6.2", - "resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.6.2.tgz", - "integrity": "sha512-pPTNuaAG3QMH+buKyBIGJs3g/S5y0caxw0ygM3YyE6yJFySwiGGSzA+mM3KJ8QQvzeLh3blwgSonkFjgQdxzMw==", + "version": "4.8.1", + "resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.8.1.tgz", + "integrity": "sha512-PWiOzLIUAjN/w5K17PoF4n6sKBw0gqLHPhywmYHP4t1VFQQVYeb1yWsJwnMVEMl3tUHME7X/SJPZLmtG7XBDxQ==", "dev": true, "engines": { "node": "^12.0.0 || ^14.0.0 || >=16.0.0" @@ -93,18 +93,18 @@ } }, "node_modules/@eslint/js": { - "version": "8.47.0", - "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.47.0.tgz", - "integrity": "sha512-P6omY1zv5MItm93kLM8s2vr1HICJH8v0dvddDhysbIuZ+vcjOHg5Zbkf1mTkcmi2JA9oBG2anOkRnW8WJTS8Og==", + "version": "8.49.0", + "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.49.0.tgz", + "integrity": "sha512-1S8uAY/MTJqVx0SC4epBq+N2yhuwtNwLbJYNZyhL2pO1ZVKn5HFXav5T41Ryzy9K9V7ZId2JB2oy/W4aCd9/2w==", "dev": true, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" } }, "node_modules/@humanwhocodes/config-array": { - "version": "0.11.10", - "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.10.tgz", - "integrity": "sha512-KVVjQmNUepDVGXNuoRRdmmEjruj0KfiGSbS8LVc12LMsWDQzRXJ0qdhN8L8uUigKpfEHRhlaQFY0ib1tnUbNeQ==", + "version": "0.11.11", + "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.11.tgz", + "integrity": "sha512-N2brEuAadi0CcdeMXUkhbZB84eskAc8MEX1By6qEchoVywSgXPIjou4rYsl0V3Hj0ZnuGycGCjdNgockbzeWNA==", "dev": true, "dependencies": { "@humanwhocodes/object-schema": "^1.2.1", @@ -222,9 +222,9 @@ "dev": true }, "node_modules/@types/linkify-it": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-3.0.2.tgz", - "integrity": "sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-3.0.3.tgz", + "integrity": "sha512-pTjcqY9E4nOI55Wgpz7eiI8+LzdYnw3qxXCfHyBDdPbYvbyLgWLJGh8EdPvqawwMK1Uo1794AUkkR38Fr0g+2g==", "dev": true }, "node_modules/@types/markdown-it": { @@ -543,16 +543,16 @@ } }, "node_modules/eslint": { - "version": "8.47.0", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.47.0.tgz", - "integrity": "sha512-spUQWrdPt+pRVP1TTJLmfRNJJHHZryFmptzcafwSvHsceV81djHOdnEeDmkdotZyLNjDhrOasNK8nikkoG1O8Q==", + "version": "8.49.0", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.49.0.tgz", + "integrity": "sha512-jw03ENfm6VJI0jA9U+8H5zfl5b+FvuU3YYvZRdZHOlU2ggJkxrlkJH4HcDrZpj6YwD8kuYqvQM8LyesoazrSOQ==", "dev": true, "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", "@eslint-community/regexpp": "^4.6.1", "@eslint/eslintrc": "^2.1.2", - "@eslint/js": "^8.47.0", - "@humanwhocodes/config-array": "^0.11.10", + "@eslint/js": "8.49.0", + "@humanwhocodes/config-array": "^0.11.11", "@humanwhocodes/module-importer": "^1.0.1", "@nodelib/fs.walk": "^1.2.8", "ajv": "^6.12.4", @@ -739,22 +739,23 @@ } }, "node_modules/flat-cache": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-3.0.4.tgz", - "integrity": "sha512-dm9s5Pw7Jc0GvMYbshN6zchCA9RgQlzzEZX3vylR9IqFfS8XciblUXOKfW6SiuJ0e13eDYZoZV5wdrev7P3Nwg==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-3.1.0.tgz", + "integrity": "sha512-OHx4Qwrrt0E4jEIcI5/Xb+f+QmJYNj2rrK8wiIdQOIrB9WrrJL8cjZvXdXuBTkkEwEqLycb5BeZDV1o2i9bTew==", "dev": true, "dependencies": { - "flatted": "^3.1.0", + "flatted": "^3.2.7", + "keyv": "^4.5.3", "rimraf": "^3.0.2" }, "engines": { - "node": "^10.12.0 || >=12.0.0" + "node": ">=12.0.0" } }, "node_modules/flatted": { - "version": "3.2.7", - "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz", - "integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==", + "version": "3.2.9", + "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.9.tgz", + "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==", "dev": true }, "node_modules/foreground-child": { @@ -777,9 +778,9 @@ "dev": true }, "node_modules/fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", "dev": true, "hasInstallScript": true, "optional": true, @@ -1063,6 +1064,12 @@ "node": ">=8" } }, + "node_modules/json-buffer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", + "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==", + "dev": true + }, "node_modules/json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", @@ -1075,6 +1082,15 @@ "integrity": "sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==", "dev": true }, + "node_modules/keyv": { + "version": "4.5.3", + "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.3.tgz", + "integrity": "sha512-QCiSav9WaX1PgETJ+SpNnx2PRRapJ/oRSXM4VO5OGYGSjrxbKPVFVhB3l2OCbLCk329N8qyAtsJjSjvVBWzEug==", + "dev": true, + "dependencies": { + "json-buffer": "3.0.1" + } + }, "node_modules/klaw": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/klaw/-/klaw-3.0.0.tgz", @@ -1444,9 +1460,9 @@ } }, "node_modules/rollup": { - "version": "3.28.0", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-3.28.0.tgz", - "integrity": "sha512-d7zhvo1OUY2SXSM6pfNjgD5+d0Nz87CUp4mt8l/GgVP3oBsPwzNvSzyu1me6BSG9JIgWNTVcafIXBIyM8yQ3yw==", + "version": "3.29.2", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-3.29.2.tgz", + "integrity": "sha512-CJouHoZ27v6siztc21eEQGo0kIcE5D1gVPA571ez0mMYb25LGYGKnVNXpEj5MGlepmDWGXNjDB5q7uNiPHC11A==", "dev": true, "bin": { "rollup": "dist/bin/rollup" @@ -1631,9 +1647,9 @@ } }, "node_modules/typescript": { - "version": "5.1.6", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", - "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -1815,9 +1831,9 @@ "dev": true }, "@babel/parser": { - "version": "7.22.10", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.22.10.tgz", - "integrity": "sha512-lNbdGsQb9ekfsnjFGhEiF4hfFqGgfOP3H3d27re3n+CGhNuTSUEQdfWk556sTLNTloczcdM5TYF2LhzmDQKyvQ==", + "version": "7.22.16", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.22.16.tgz", + "integrity": "sha512-+gPfKv8UWeKKeJTUxe59+OobVcrYHETCsORl61EmSkmgymguYk/X5bp7GuUIXaFsc6y++v8ZxPsLSSuujqDphA==", "dev": true }, "@bcoe/v8-coverage": { @@ -1836,9 +1852,9 @@ } }, "@eslint-community/regexpp": { - "version": "4.6.2", - "resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.6.2.tgz", - "integrity": "sha512-pPTNuaAG3QMH+buKyBIGJs3g/S5y0caxw0ygM3YyE6yJFySwiGGSzA+mM3KJ8QQvzeLh3blwgSonkFjgQdxzMw==", + "version": "4.8.1", + "resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.8.1.tgz", + "integrity": "sha512-PWiOzLIUAjN/w5K17PoF4n6sKBw0gqLHPhywmYHP4t1VFQQVYeb1yWsJwnMVEMl3tUHME7X/SJPZLmtG7XBDxQ==", "dev": true }, "@eslint/eslintrc": { @@ -1859,15 +1875,15 @@ } }, "@eslint/js": { - "version": "8.47.0", - "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.47.0.tgz", - "integrity": "sha512-P6omY1zv5MItm93kLM8s2vr1HICJH8v0dvddDhysbIuZ+vcjOHg5Zbkf1mTkcmi2JA9oBG2anOkRnW8WJTS8Og==", + "version": "8.49.0", + "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.49.0.tgz", + "integrity": "sha512-1S8uAY/MTJqVx0SC4epBq+N2yhuwtNwLbJYNZyhL2pO1ZVKn5HFXav5T41Ryzy9K9V7ZId2JB2oy/W4aCd9/2w==", "dev": true }, "@humanwhocodes/config-array": { - "version": "0.11.10", - "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.10.tgz", - "integrity": "sha512-KVVjQmNUepDVGXNuoRRdmmEjruj0KfiGSbS8LVc12LMsWDQzRXJ0qdhN8L8uUigKpfEHRhlaQFY0ib1tnUbNeQ==", + "version": "0.11.11", + "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.11.tgz", + "integrity": "sha512-N2brEuAadi0CcdeMXUkhbZB84eskAc8MEX1By6qEchoVywSgXPIjou4rYsl0V3Hj0ZnuGycGCjdNgockbzeWNA==", "dev": true, "requires": { "@humanwhocodes/object-schema": "^1.2.1", @@ -1957,9 +1973,9 @@ "dev": true }, "@types/linkify-it": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-3.0.2.tgz", - "integrity": "sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-3.0.3.tgz", + "integrity": "sha512-pTjcqY9E4nOI55Wgpz7eiI8+LzdYnw3qxXCfHyBDdPbYvbyLgWLJGh8EdPvqawwMK1Uo1794AUkkR38Fr0g+2g==", "dev": true }, "@types/markdown-it": { @@ -2201,16 +2217,16 @@ "dev": true }, "eslint": { - "version": "8.47.0", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.47.0.tgz", - "integrity": "sha512-spUQWrdPt+pRVP1TTJLmfRNJJHHZryFmptzcafwSvHsceV81djHOdnEeDmkdotZyLNjDhrOasNK8nikkoG1O8Q==", + "version": "8.49.0", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.49.0.tgz", + "integrity": "sha512-jw03ENfm6VJI0jA9U+8H5zfl5b+FvuU3YYvZRdZHOlU2ggJkxrlkJH4HcDrZpj6YwD8kuYqvQM8LyesoazrSOQ==", "dev": true, "requires": { "@eslint-community/eslint-utils": "^4.2.0", "@eslint-community/regexpp": "^4.6.1", "@eslint/eslintrc": "^2.1.2", - "@eslint/js": "^8.47.0", - "@humanwhocodes/config-array": "^0.11.10", + "@eslint/js": "8.49.0", + "@humanwhocodes/config-array": "^0.11.11", "@humanwhocodes/module-importer": "^1.0.1", "@nodelib/fs.walk": "^1.2.8", "ajv": "^6.12.4", @@ -2349,19 +2365,20 @@ } }, "flat-cache": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-3.0.4.tgz", - "integrity": "sha512-dm9s5Pw7Jc0GvMYbshN6zchCA9RgQlzzEZX3vylR9IqFfS8XciblUXOKfW6SiuJ0e13eDYZoZV5wdrev7P3Nwg==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-3.1.0.tgz", + "integrity": "sha512-OHx4Qwrrt0E4jEIcI5/Xb+f+QmJYNj2rrK8wiIdQOIrB9WrrJL8cjZvXdXuBTkkEwEqLycb5BeZDV1o2i9bTew==", "dev": true, "requires": { - "flatted": "^3.1.0", + "flatted": "^3.2.7", + "keyv": "^4.5.3", "rimraf": "^3.0.2" } }, "flatted": { - "version": "3.2.7", - "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz", - "integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==", + "version": "3.2.9", + "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.9.tgz", + "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==", "dev": true }, "foreground-child": { @@ -2381,9 +2398,9 @@ "dev": true }, "fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", "dev": true, "optional": true }, @@ -2596,6 +2613,12 @@ } } }, + "json-buffer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", + "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==", + "dev": true + }, "json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", @@ -2608,6 +2631,15 @@ "integrity": "sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==", "dev": true }, + "keyv": { + "version": "4.5.3", + "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.3.tgz", + "integrity": "sha512-QCiSav9WaX1PgETJ+SpNnx2PRRapJ/oRSXM4VO5OGYGSjrxbKPVFVhB3l2OCbLCk329N8qyAtsJjSjvVBWzEug==", + "dev": true, + "requires": { + "json-buffer": "3.0.1" + } + }, "klaw": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/klaw/-/klaw-3.0.0.tgz", @@ -2869,9 +2901,9 @@ } }, "rollup": { - "version": "3.28.0", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-3.28.0.tgz", - "integrity": "sha512-d7zhvo1OUY2SXSM6pfNjgD5+d0Nz87CUp4mt8l/GgVP3oBsPwzNvSzyu1me6BSG9JIgWNTVcafIXBIyM8yQ3yw==", + "version": "3.29.2", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-3.29.2.tgz", + "integrity": "sha512-CJouHoZ27v6siztc21eEQGo0kIcE5D1gVPA571ez0mMYb25LGYGKnVNXpEj5MGlepmDWGXNjDB5q7uNiPHC11A==", "dev": true, "requires": { "fsevents": "~2.3.2" @@ -2993,9 +3025,9 @@ "dev": true }, "typescript": { - "version": "5.1.6", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", - "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", "dev": true }, "uc.micro": { diff --git a/package.json b/package.json index 9015f81..cb62241 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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.", "author": "Hexagon ", "contributors": [ @@ -65,7 +65,7 @@ "eslint": "^8.46.0", "jsdoc": "^4.0.2", "rollup": "^3.27.2", - "typescript": "^5.1.6", + "typescript": "^5.2.2", "uglify-js": "^3.17.4", "uvu": "^0.5.6" }, diff --git a/src/base64.js b/src/base64.js index c7832c8..449e9d2 100644 --- a/src/base64.js +++ b/src/base64.js @@ -35,7 +35,8 @@ const genLookup = (target) => { 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; } return lookupTemp; @@ -45,6 +46,12 @@ const lookup = genLookup(chars), lookupUrl = genLookup(charsUrl); +/** + * Pre-calculated regexes for validating base64 and base64url + */ +const base64UrlPattern = /^[-A-Za-z0-9\-_]*$/; +const base64Pattern = /^[-A-Za-z0-9+/]*={0,3}$/; + /** * @namespace base64 */ @@ -97,7 +104,7 @@ base64.toArrayBuffer = (data, urlMode) => { }; /** - * Convenience function for converting base64 encoded string to an ArrayBuffer instance + * Convenience function for creating a base64 encoded string from an ArrayBuffer instance * @public * * @param {ArrayBuffer} arrBuf - ArrayBuffer to be encoded @@ -121,13 +128,15 @@ base64.fromArrayBuffer = (arrBuf, urlMode) => { result += target[bytes[i + 2] & 63]; } - 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 ? "" : "=="); } return result; + }; /** @@ -170,11 +179,7 @@ base64.validate = (encoded, urlMode) => { // Go on validate 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) { return false; } diff --git a/types/base64.d.ts b/types/base64.d.ts index b7986e6..17f8343 100644 --- a/types/base64.d.ts +++ b/types/base64.d.ts @@ -10,7 +10,7 @@ export namespace base64 { */ export function toArrayBuffer(data: string, urlMode?: boolean): ArrayBuffer; /** - * Convenience function for converting base64 encoded string to an ArrayBuffer instance + * Convenience function for creating a base64 encoded string from an ArrayBuffer instance * @public * * @param {ArrayBuffer} arrBuf - ArrayBuffer to be encoded