Skip to content

Commit 9daafb3

Browse files
authoredDec 27, 2023
fix: avoid deprecation message of abab package (#228)
1 parent fdbdeab commit 9daafb3

File tree

3 files changed

+3360
-2699
lines changed

3 files changed

+3360
-2699
lines changed
 

‎package-lock.json

+3,249-2,696
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"webpack": "^5.72.1"
4545
},
4646
"dependencies": {
47-
"abab": "^2.0.6",
4847
"iconv-lite": "^0.6.3",
4948
"source-map-js": "^1.0.2"
5049
},

‎src/parse-data-url.js

+111-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { atob } from "abab";
2-
31
const removeLeadingAndTrailingHTTPWhitespace = (string) =>
42
string.replace(/^[ \t\n\r]+/, "").replace(/[ \t\n\r]+$/, "");
53

@@ -98,6 +96,117 @@ function percentDecodeBytes(input) {
9896
return output.slice(0, outputIndex);
9997
}
10098

99+
/**
100+
* A lookup table for atob(), which converts an ASCII character to the
101+
* corresponding six-bit number.
102+
*/
103+
104+
const characters =
105+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
106+
107+
function atobLookup(chr) {
108+
const index = characters.indexOf(chr);
109+
// Throw exception if character is not in the lookup string; should not be hit in tests
110+
// eslint-disable-next-line no-undefined
111+
return index < 0 ? undefined : index;
112+
}
113+
114+
/**
115+
* Implementation of atob() according to the HTML and Infra specs, except that
116+
* instead of throwing INVALID_CHARACTER_ERR we return null.
117+
*/
118+
function atob(input) {
119+
/* eslint-disable no-bitwise */
120+
// Web IDL requires DOMStrings to just be converted using ECMAScript
121+
// ToString, which in our case amounts to using a template literal.
122+
let data = `${input}`;
123+
124+
// "Remove all ASCII whitespace from data."
125+
data = data.replace(/[ \t\n\f\r]/g, "");
126+
127+
// "If data's length divides by 4 leaving no remainder, then: if data ends
128+
// with one or two U+003D (=) code points, then remove them from data."
129+
if (data.length % 4 === 0) {
130+
data = data.replace(/==?$/, "");
131+
}
132+
133+
// "If data's length divides by 4 leaving a remainder of 1, then return
134+
// failure."
135+
//
136+
// "If data contains a code point that is not one of
137+
//
138+
// U+002B (+)
139+
// U+002F (/)
140+
// ASCII alphanumeric
141+
//
142+
// then return failure."
143+
if (data.length % 4 === 1 || /[^+/0-9A-Za-z]/.test(data)) {
144+
return null;
145+
}
146+
147+
// "Let output be an empty byte sequence."
148+
let output = "";
149+
150+
// "Let buffer be an empty buffer that can have bits appended to it."
151+
//
152+
// We append bits via left-shift and or. accumulatedBits is used to track
153+
// when we've gotten to 24 bits.
154+
let buffer = 0;
155+
let accumulatedBits = 0;
156+
157+
// "Let position be a position variable for data, initially pointing at the
158+
// start of data."
159+
//
160+
// "While position does not point past the end of data:"
161+
for (let i = 0; i < data.length; i++) {
162+
// "Find the code point pointed to by position in the second column of
163+
// Table 1: The Base 64 Alphabet of RFC 4648. Let n be the number given in
164+
// the first cell of the same row.
165+
//
166+
// "Append to buffer the six bits corresponding to n, most significant bit
167+
// first."
168+
//
169+
// atobLookup() implements the table from RFC 4648.
170+
// eslint-disable-next-line no-bitwise
171+
buffer <<= 6;
172+
173+
// eslint-disable-next-line no-bitwise
174+
buffer |= atobLookup(data[i]);
175+
accumulatedBits += 6;
176+
177+
// "If buffer has accumulated 24 bits, interpret them as three 8-bit
178+
// big-endian numbers. Append three bytes with values equal to those
179+
// numbers to output, in the same order, and then empty buffer."
180+
if (accumulatedBits === 24) {
181+
output += String.fromCharCode((buffer & 0xff0000) >> 16);
182+
output += String.fromCharCode((buffer & 0xff00) >> 8);
183+
output += String.fromCharCode(buffer & 0xff);
184+
accumulatedBits = 0;
185+
buffer = 0;
186+
}
187+
// "Advance position by 1."
188+
}
189+
190+
// "If buffer is not empty, it contains either 12 or 18 bits. If it contains
191+
// 12 bits, then discard the last four and interpret the remaining eight as
192+
// an 8-bit big-endian number. If it contains 18 bits, then discard the last
193+
// two and interpret the remaining 16 as two 8-bit big-endian numbers. Append
194+
// the one or two bytes with values equal to those one or two numbers to
195+
// output, in the same order."
196+
if (accumulatedBits === 12) {
197+
buffer >>= 4;
198+
output += String.fromCharCode(buffer);
199+
} else if (accumulatedBits === 18) {
200+
buffer >>= 2;
201+
output += String.fromCharCode((buffer & 0xff00) >> 8);
202+
output += String.fromCharCode(buffer & 0xff);
203+
}
204+
/* eslint-enable no-bitwise */
205+
206+
// "Return output."
207+
return output;
208+
}
209+
101210
export default function parseDataUrl(stringInput) {
102211
let parsedUrl;
103212

0 commit comments

Comments
 (0)
Please sign in to comment.