-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathparse-data-url.js
402 lines (323 loc) · 10.3 KB
/
parse-data-url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
const removeLeadingAndTrailingHTTPWhitespace = (string) =>
string.replace(/^[ \t\n\r]+/, "").replace(/[ \t\n\r]+$/, "");
const removeTrailingHTTPWhitespace = (string) =>
string.replace(/[ \t\n\r]+$/, "");
const isHTTPWhitespaceChar = (char) =>
char === " " || char === "\t" || char === "\n" || char === "\r";
const solelyContainsHTTPTokenCodePoints = (string) =>
/^[-!#$%&'*+.^_`|~A-Za-z0-9]*$/.test(string);
const soleyContainsHTTPQuotedStringTokenCodePoints = (string) =>
/^[\t\u0020-\u007E\u0080-\u00FF]*$/.test(string);
const asciiLowercase = (string) =>
string.replace(/[A-Z]/g, (l) => l.toLowerCase());
const collectAnHTTPQuotedString = (input, position) => {
let value = "";
// eslint-disable-next-line no-param-reassign
position += 1;
// eslint-disable-next-line no-constant-condition
while (true) {
while (
position < input.length &&
input[position] !== '"' &&
input[position] !== "\\"
) {
value += input[position];
// eslint-disable-next-line no-param-reassign
position += 1;
}
if (position >= input.length) {
break;
}
const quoteOrBackslash = input[position];
// eslint-disable-next-line no-param-reassign
position += 1;
if (quoteOrBackslash === "\\") {
if (position >= input.length) {
value += "\\";
break;
}
value += input[position];
// eslint-disable-next-line no-param-reassign
position += 1;
} else {
break;
}
}
return [value, position];
};
function isASCIIHex(c) {
return (
(c >= 0x30 && c <= 0x39) ||
(c >= 0x41 && c <= 0x46) ||
(c >= 0x61 && c <= 0x66)
);
}
function percentDecodeBytes(input) {
const output = new Uint8Array(input.byteLength);
let outputIndex = 0;
for (let i = 0; i < input.byteLength; ++i) {
const byte = input[i];
if (byte !== 0x25) {
output[outputIndex] = byte;
} else if (
byte === 0x25 &&
(!isASCIIHex(input[i + 1]) || !isASCIIHex(input[i + 2]))
) {
output[outputIndex] = byte;
} else {
output[outputIndex] = parseInt(
String.fromCodePoint(input[i + 1], input[i + 2]),
16,
);
i += 2;
}
outputIndex += 1;
}
return output.slice(0, outputIndex);
}
/**
* A lookup table for atob(), which converts an ASCII character to the
* corresponding six-bit number.
*/
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function atobLookup(chr) {
const index = characters.indexOf(chr);
// Throw exception if character is not in the lookup string; should not be hit in tests
// eslint-disable-next-line no-undefined
return index < 0 ? undefined : index;
}
/**
* Implementation of atob() according to the HTML and Infra specs, except that
* instead of throwing INVALID_CHARACTER_ERR we return null.
*/
function atob(input) {
/* eslint-disable no-bitwise */
// Web IDL requires DOMStrings to just be converted using ECMAScript
// ToString, which in our case amounts to using a template literal.
let data = `${input}`;
// "Remove all ASCII whitespace from data."
data = data.replace(/[ \t\n\f\r]/g, "");
// "If data's length divides by 4 leaving no remainder, then: if data ends
// with one or two U+003D (=) code points, then remove them from data."
if (data.length % 4 === 0) {
data = data.replace(/==?$/, "");
}
// "If data's length divides by 4 leaving a remainder of 1, then return
// failure."
//
// "If data contains a code point that is not one of
//
// U+002B (+)
// U+002F (/)
// ASCII alphanumeric
//
// then return failure."
if (data.length % 4 === 1 || /[^+/0-9A-Za-z]/.test(data)) {
return null;
}
// "Let output be an empty byte sequence."
let output = "";
// "Let buffer be an empty buffer that can have bits appended to it."
//
// We append bits via left-shift and or. accumulatedBits is used to track
// when we've gotten to 24 bits.
let buffer = 0;
let accumulatedBits = 0;
// "Let position be a position variable for data, initially pointing at the
// start of data."
//
// "While position does not point past the end of data:"
for (let i = 0; i < data.length; i++) {
// "Find the code point pointed to by position in the second column of
// Table 1: The Base 64 Alphabet of RFC 4648. Let n be the number given in
// the first cell of the same row.
//
// "Append to buffer the six bits corresponding to n, most significant bit
// first."
//
// atobLookup() implements the table from RFC 4648.
// eslint-disable-next-line no-bitwise
buffer <<= 6;
// eslint-disable-next-line no-bitwise
buffer |= atobLookup(data[i]);
accumulatedBits += 6;
// "If buffer has accumulated 24 bits, interpret them as three 8-bit
// big-endian numbers. Append three bytes with values equal to those
// numbers to output, in the same order, and then empty buffer."
if (accumulatedBits === 24) {
output += String.fromCharCode((buffer & 0xff0000) >> 16);
output += String.fromCharCode((buffer & 0xff00) >> 8);
output += String.fromCharCode(buffer & 0xff);
accumulatedBits = 0;
buffer = 0;
}
// "Advance position by 1."
}
// "If buffer is not empty, it contains either 12 or 18 bits. If it contains
// 12 bits, then discard the last four and interpret the remaining eight as
// an 8-bit big-endian number. If it contains 18 bits, then discard the last
// two and interpret the remaining 16 as two 8-bit big-endian numbers. Append
// the one or two bytes with values equal to those one or two numbers to
// output, in the same order."
if (accumulatedBits === 12) {
buffer >>= 4;
output += String.fromCharCode(buffer);
} else if (accumulatedBits === 18) {
buffer >>= 2;
output += String.fromCharCode((buffer & 0xff00) >> 8);
output += String.fromCharCode(buffer & 0xff);
}
/* eslint-enable no-bitwise */
// "Return output."
return output;
}
export default function parseDataUrl(stringInput) {
let parsedUrl;
try {
parsedUrl = new URL(stringInput);
} catch (error) {
return null;
}
if (parsedUrl.protocol !== "data:") {
return null;
}
parsedUrl.hash = "";
// `5` is value of `'data:'.length`
const input = parsedUrl.toString().substring(5);
let position = 0;
let mediaType = "";
while (position < input.length && input[position] !== ",") {
mediaType += input[position];
position += 1;
}
mediaType = mediaType
.replace(/^[ \t\n\f\r]+/, "")
.replace(/[ \t\n\f\r]+$/, "");
if (position === input.length) {
return null;
}
position += 1;
const encodedBody = input.substring(position);
let body = Buffer.from(percentDecodeBytes(Buffer.from(encodedBody, "utf-8")));
// Can't use /i regexp flag because it isn't restricted to ASCII.
const mimeTypeBase64MatchResult = /(.*); *[Bb][Aa][Ss][Ee]64$/.exec(
mediaType,
);
if (mimeTypeBase64MatchResult) {
const stringBody = body.toString("binary");
const asString = atob(stringBody);
if (asString === null) {
return null;
}
body = Buffer.from(asString, "binary");
[, mediaType] = mimeTypeBase64MatchResult;
}
if (mediaType.startsWith(";")) {
mediaType = `text/plain ${mediaType}`;
}
const result = {
// eslint-disable-next-line no-undefined
type: undefined,
// eslint-disable-next-line no-undefined
subtype: undefined,
parameters: new Map(),
isBase64: Boolean(mimeTypeBase64MatchResult),
body,
};
if (!mediaType) {
return result;
}
const inputMediaType = removeLeadingAndTrailingHTTPWhitespace(mediaType);
let positionMediaType = 0;
let type = "";
while (
positionMediaType < inputMediaType.length &&
inputMediaType[positionMediaType] !== "/"
) {
type += inputMediaType[positionMediaType];
positionMediaType += 1;
}
if (type.length === 0 || !solelyContainsHTTPTokenCodePoints(type)) {
return result;
}
if (positionMediaType >= inputMediaType.length) {
return result;
}
// Skips past "/"
positionMediaType += 1;
let subtype = "";
while (
positionMediaType < inputMediaType.length &&
inputMediaType[positionMediaType] !== ";"
) {
subtype += inputMediaType[positionMediaType];
positionMediaType += 1;
}
subtype = removeTrailingHTTPWhitespace(subtype);
if (subtype.length === 0 || !solelyContainsHTTPTokenCodePoints(subtype)) {
return result;
}
result.type = asciiLowercase(type);
result.subtype = asciiLowercase(subtype);
while (positionMediaType < inputMediaType.length) {
// Skip past ";"
positionMediaType += 1;
while (isHTTPWhitespaceChar(inputMediaType[positionMediaType])) {
positionMediaType += 1;
}
let parameterName = "";
while (
positionMediaType < inputMediaType.length &&
inputMediaType[positionMediaType] !== ";" &&
inputMediaType[positionMediaType] !== "="
) {
parameterName += inputMediaType[positionMediaType];
positionMediaType += 1;
}
parameterName = asciiLowercase(parameterName);
if (positionMediaType < inputMediaType.length) {
if (inputMediaType[positionMediaType] === ";") {
// eslint-disable-next-line no-continue
continue;
}
// Skip past "="
positionMediaType += 1;
}
let parameterValue = "";
if (inputMediaType[positionMediaType] === '"') {
[parameterValue, positionMediaType] = collectAnHTTPQuotedString(
inputMediaType,
positionMediaType,
);
while (
positionMediaType < inputMediaType.length &&
inputMediaType[positionMediaType] !== ";"
) {
positionMediaType += 1;
}
} else {
while (
positionMediaType < inputMediaType.length &&
inputMediaType[positionMediaType] !== ";"
) {
parameterValue += inputMediaType[positionMediaType];
positionMediaType += 1;
}
parameterValue = removeTrailingHTTPWhitespace(parameterValue);
if (parameterValue === "") {
// eslint-disable-next-line no-continue
continue;
}
}
if (
parameterName.length > 0 &&
solelyContainsHTTPTokenCodePoints(parameterName) &&
soleyContainsHTTPQuotedStringTokenCodePoints(parameterValue) &&
!result.parameters.has(parameterName)
) {
result.parameters.set(parameterName, parameterValue);
}
}
return result;
}