base64-arraybuffer-es6
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -5,12 +5,18 @@ /* | ||
* | ||
* Copyright (c) 2017 Brett Zamir, 2012 Niklas von Hertzen | ||
* Copyright (c) 2017-2023 Brett Zamir, 2012 Niklas von Hertzen | ||
* Licensed under the MIT license. | ||
*/ | ||
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; // Use a lookup table to find the index. | ||
/** | ||
* @typedef {number} Integer | ||
*/ | ||
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
// Use a lookup table to find the index. | ||
var lookup = new Uint8Array(256); | ||
for (var i = 0; i < chars.length; i++) { | ||
lookup[chars.codePointAt(i)] = i; | ||
lookup[/** @type {number} */chars.codePointAt(i)] = i; | ||
} | ||
/** | ||
@@ -22,4 +28,2 @@ * @param {ArrayBuffer} arraybuffer | ||
*/ | ||
var encode = function encode(arraybuffer, byteOffset, lngth) { | ||
@@ -30,7 +34,7 @@ if (lngth === null || lngth === undefined) { | ||
var bytes = new Uint8Array(arraybuffer, byteOffset || 0, // Default needed for Safari | ||
var bytes = new Uint8Array(arraybuffer, byteOffset || 0, | ||
// Default needed for Safari | ||
lngth); | ||
var len = bytes.length; | ||
var base64 = ''; | ||
for (var _i = 0; _i < len; _i += 3) { | ||
@@ -42,3 +46,2 @@ base64 += chars[bytes[_i] >> 2]; | ||
} | ||
if (len % 3 === 2) { | ||
@@ -49,5 +52,5 @@ base64 = base64.slice(0, -1) + '='; | ||
} | ||
return base64; | ||
}; | ||
/** | ||
@@ -57,12 +60,12 @@ * @param {string} base64 | ||
*/ | ||
var decode = function decode(base64) { | ||
var len = base64.length; | ||
if (len % 4) { | ||
throw new Error('Bad base64 length: not divisible by four'); | ||
} | ||
var bufferLength = base64.length * 0.75; | ||
var p = 0; | ||
var encoded1, encoded2, encoded3, encoded4; | ||
if (base64[base64.length - 1] === '=') { | ||
bufferLength--; | ||
if (base64[base64.length - 2] === '=') { | ||
@@ -72,11 +75,11 @@ bufferLength--; | ||
} | ||
var arraybuffer = new ArrayBuffer(bufferLength), | ||
bytes = new Uint8Array(arraybuffer); | ||
bytes = new Uint8Array(arraybuffer); | ||
for (var _i2 = 0; _i2 < len; _i2 += 4) { | ||
encoded1 = lookup[base64.codePointAt(_i2)]; | ||
encoded2 = lookup[base64.codePointAt(_i2 + 1)]; | ||
encoded3 = lookup[base64.codePointAt(_i2 + 2)]; | ||
encoded4 = lookup[base64.codePointAt(_i2 + 3)]; | ||
// We know the result will not be undefined, as we have a text | ||
// length divisible by four | ||
encoded1 = lookup[/** @type {number} */base64.codePointAt(_i2)]; | ||
encoded2 = lookup[/** @type {number} */base64.codePointAt(_i2 + 1)]; | ||
encoded3 = lookup[/** @type {number} */base64.codePointAt(_i2 + 2)]; | ||
encoded4 = lookup[/** @type {number} */base64.codePointAt(_i2 + 3)]; | ||
bytes[p++] = encoded1 << 2 | encoded2 >> 4; | ||
@@ -86,3 +89,2 @@ bytes[p++] = (encoded2 & 15) << 4 | encoded3 >> 2; | ||
} | ||
return arraybuffer; | ||
@@ -89,0 +91,0 @@ }; |
{ | ||
"name": "base64-arraybuffer-es6", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Encode/decode base64 data into ArrayBuffers", | ||
@@ -8,2 +8,3 @@ "homepage": "https://github.com/brettz9/base64-arraybuffer", | ||
"type": "module", | ||
"types": "./dist/base64-arraybuffer.d.ts", | ||
"contributors": [ | ||
@@ -40,30 +41,29 @@ { | ||
"devDependencies": { | ||
"@babel/core": "^7.17.4", | ||
"@babel/preset-env": "^7.16.11", | ||
"@babel/core": "^7.21.4", | ||
"@babel/preset-env": "^7.21.4", | ||
"@brettz9/eslint-plugin": "^1.0.4", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"c8": "^7.11.0", | ||
"chai": "^4.3.6", | ||
"eslint": "^8.9.0", | ||
"eslint-config-ash-nazg": "^32.4.0", | ||
"eslint-config-standard": "^16.0.3", | ||
"eslint-plugin-array-func": "^3.1.7", | ||
"eslint-plugin-compat": "^4.0.2", | ||
"@rollup/plugin-babel": "^6.0.3", | ||
"c8": "^7.13.0", | ||
"chai": "^4.3.7", | ||
"eslint": "^8.39.0", | ||
"eslint-config-ash-nazg": "^34.11.1", | ||
"eslint-config-standard": "^17.0.0", | ||
"eslint-plugin-array-func": "^3.1.8", | ||
"eslint-plugin-compat": "^4.1.4", | ||
"eslint-plugin-eslint-comments": "^3.2.0", | ||
"eslint-plugin-html": "^6.2.0", | ||
"eslint-plugin-import": "^2.25.4", | ||
"eslint-plugin-jsdoc": "^37.9.2", | ||
"eslint-plugin-markdown": "^2.2.1", | ||
"eslint-plugin-no-unsanitized": "^4.0.1", | ||
"eslint-plugin-html": "^7.1.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-jsdoc": "^43.1.1", | ||
"eslint-plugin-markdown": "^3.0.0", | ||
"eslint-plugin-no-unsanitized": "^4.0.2", | ||
"eslint-plugin-no-use-extend-native": "^0.5.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^6.0.0", | ||
"eslint-plugin-sonarjs": "^0.11.0", | ||
"eslint-plugin-promise": "^6.1.1", | ||
"eslint-plugin-sonarjs": "^0.19.0", | ||
"eslint-plugin-standard": "^4.1.0", | ||
"eslint-plugin-unicorn": "^41.0.0", | ||
"esm": "^3.2.25", | ||
"http-server": "^14.1.0", | ||
"mocha": "^9.2.0", | ||
"open-cli": "^7.0.1", | ||
"rollup": "2.67.2" | ||
"eslint-plugin-unicorn": "^46.0.0", | ||
"http-server": "^14.1.1", | ||
"mocha": "^10.2.0", | ||
"open-cli": "^7.2.0", | ||
"rollup": "3.21.0" | ||
}, | ||
@@ -75,10 +75,11 @@ "keywords": [ | ||
"scripts": { | ||
"tsc": "tsc", | ||
"start": "http-server -p 8093", | ||
"eslint": "eslint --ext js,md,html .", | ||
"lint": "npm run eslint --", | ||
"build": "rollup -c", | ||
"mocha": "mocha --require esm --require chai/register-assert.js test/*.js", | ||
"mocha": "mocha --require chai/register-assert.js test/*.js", | ||
"test": "npm run eslint && npm run build && c8 npm run mocha", | ||
"browser-test": "npm run eslint && npm run build && open-cli http://127.0.0.1:8093/test/ && npm start" | ||
}, | ||
"readme": "# base64-arraybuffer-es6\n\nEncode/decode base64 data into ArrayBuffers\n\nAn ES6 Modules-based port of [base64-arraybuffer](https://github.com/niklasvh/base64-arraybuffer).\n\nAlso supports `byteOffset` and `length` arguments for partial encoding and\nallows for selective import of the two methods.\n\n## Getting Started\n\nInstall the module with: `npm install base64-arraybuffer-es6`\n\n## API\n\nThe library encodes and decodes base64 to and from ArrayBuffers\n\n - __encode(buffer, byteOffset, length)__ - Encodes `ArrayBuffer` into base64 string\n - __decode(str)__ - Decodes base64 string to `ArrayBuffer`\n\n## License\n\nCopyright (c) 2017-2019 Brett Zamir, 2012 Niklas von Hertzen\nLicensed under the MIT license.\n" | ||
} | ||
} |
@@ -5,6 +5,10 @@ /* | ||
* | ||
* Copyright (c) 2017 Brett Zamir, 2012 Niklas von Hertzen | ||
* Copyright (c) 2017-2023 Brett Zamir, 2012 Niklas von Hertzen | ||
* Licensed under the MIT license. | ||
*/ | ||
/** | ||
* @typedef {number} Integer | ||
*/ | ||
const chars = | ||
@@ -16,3 +20,3 @@ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
for (let i = 0; i < chars.length; i++) { | ||
lookup[chars.codePointAt(i)] = i; | ||
lookup[/** @type {number} */ (chars.codePointAt(i))] = i; | ||
} | ||
@@ -61,2 +65,6 @@ | ||
if (len % 4) { | ||
throw new Error('Bad base64 length: not divisible by four'); | ||
} | ||
let bufferLength = base64.length * 0.75; | ||
@@ -77,6 +85,8 @@ let p = 0; | ||
for (let i = 0; i < len; i += 4) { | ||
encoded1 = lookup[base64.codePointAt(i)]; | ||
encoded2 = lookup[base64.codePointAt(i + 1)]; | ||
encoded3 = lookup[base64.codePointAt(i + 2)]; | ||
encoded4 = lookup[base64.codePointAt(i + 3)]; | ||
// We know the result will not be undefined, as we have a text | ||
// length divisible by four | ||
encoded1 = lookup[/** @type {number} */ (base64.codePointAt(i))]; | ||
encoded2 = lookup[/** @type {number} */ (base64.codePointAt(i + 1))]; | ||
encoded3 = lookup[/** @type {number} */ (base64.codePointAt(i + 2))]; | ||
encoded4 = lookup[/** @type {number} */ (base64.codePointAt(i + 3))]; | ||
@@ -83,0 +93,0 @@ bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13065
27
8
245
0