Skip to content

Commit 4ac6d3c

Browse files
refactor: code
1 parent 78effee commit 4ac6d3c

File tree

7 files changed

+796
-304
lines changed

7 files changed

+796
-304
lines changed

package-lock.json

Lines changed: 291 additions & 295 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@
4141
"webpack": "^4.0.0 || ^5.0.0"
4242
},
4343
"dependencies": {
44-
"data-urls": "^2.0.0",
44+
"abab": "^2.0.4",
4545
"iconv-lite": "^0.6.2",
4646
"loader-utils": "^2.0.0",
4747
"schema-utils": "^2.7.0",
48-
"source-map": "^0.6.1"
48+
"source-map": "^0.6.1",
49+
"whatwg-mimetype": "^2.3.0"
4950
},
5051
"devDependencies": {
5152
"@babel/cli": "^7.10.5",
52-
"@babel/core": "^7.11.1",
53+
"@babel/core": "^7.11.4",
5354
"@babel/preset-env": "^7.11.0",
5455
"@commitlint/cli": "^10.0.0",
5556
"@commitlint/config-conventional": "^10.0.0",
@@ -63,11 +64,11 @@
6364
"eslint-config-prettier": "^6.11.0",
6465
"eslint-plugin-import": "^2.22.0",
6566
"husky": "^4.2.5",
66-
"jest": "^26.4.0",
67-
"lint-staged": "^10.2.11",
67+
"jest": "^26.4.2",
68+
"lint-staged": "^10.2.13",
6869
"memfs": "^3.2.0",
6970
"npm-run-all": "^4.1.5",
70-
"prettier": "^2.0.5",
71+
"prettier": "^2.1.1",
7172
"standard-version": "^9.0.0",
7273
"webpack": "^4.44.1"
7374
},

src/labels-to-names.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const labelToNames = {
2-
'866': 'IBM866',
2+
866: 'IBM866',
33
'unicode-1-1-utf-8': 'UTF-8',
44
'utf-8': 'UTF-8',
55
utf8: 'UTF-8',

src/parse-data-url.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import MIMEType from 'whatwg-mimetype';
2+
import { atob } from 'abab';
3+
4+
function isASCIIHex(c) {
5+
return (
6+
(c >= 0x30 && c <= 0x39) ||
7+
(c >= 0x41 && c <= 0x46) ||
8+
(c >= 0x61 && c <= 0x66)
9+
);
10+
}
11+
12+
function percentDecodeBytes(input) {
13+
const output = new Uint8Array(input.byteLength);
14+
let outputIndex = 0;
15+
16+
for (let i = 0; i < input.byteLength; ++i) {
17+
const byte = input[i];
18+
19+
if (byte !== 0x25) {
20+
output[outputIndex] = byte;
21+
} else if (
22+
byte === 0x25 &&
23+
(!isASCIIHex(input[i + 1]) || !isASCIIHex(input[i + 2]))
24+
) {
25+
output[outputIndex] = byte;
26+
} else {
27+
output[outputIndex] = parseInt(
28+
String.fromCodePoint(input[i + 1], input[i + 2]),
29+
16
30+
);
31+
i += 2;
32+
}
33+
34+
outputIndex += 1;
35+
}
36+
37+
return output.slice(0, outputIndex);
38+
}
39+
40+
export default function parseDataUrl(stringInput) {
41+
let parsedUrl;
42+
43+
try {
44+
parsedUrl = new URL(stringInput);
45+
} catch (error) {
46+
return null;
47+
}
48+
49+
if (parsedUrl.protocol !== 'data:') {
50+
return null;
51+
}
52+
53+
parsedUrl.hash = '';
54+
55+
// `5` is value of `'data:'.length`
56+
const input = parsedUrl.toString().substring(5);
57+
58+
let position = 0;
59+
let mimeType = '';
60+
61+
while (position < input.length && input[position] !== ',') {
62+
mimeType += input[position];
63+
position += 1;
64+
}
65+
66+
mimeType = mimeType.replace(/^[ \t\n\f\r]+/, '').replace(/[ \t\n\f\r]+$/, '');
67+
68+
if (position === input.length) {
69+
return null;
70+
}
71+
72+
position += 1;
73+
74+
const encodedBody = input.substring(position);
75+
76+
let body = Buffer.from(percentDecodeBytes(Buffer.from(encodedBody, 'utf-8')));
77+
78+
// Can't use /i regexp flag because it isn't restricted to ASCII.
79+
const mimeTypeBase64MatchResult = /(.*); *[Bb][Aa][Ss][Ee]64$/.exec(mimeType);
80+
81+
if (mimeTypeBase64MatchResult) {
82+
const stringBody = body.toString('binary');
83+
const asString = atob(stringBody);
84+
85+
if (asString === null) {
86+
return null;
87+
}
88+
89+
body = Buffer.from(asString, 'binary');
90+
91+
[, mimeType] = mimeTypeBase64MatchResult;
92+
}
93+
94+
if (mimeType.startsWith(';')) {
95+
mimeType = `text/plain ${mimeType}`;
96+
}
97+
98+
let mimeTypeRecord;
99+
100+
try {
101+
mimeTypeRecord = new MIMEType(mimeType);
102+
} catch (e) {
103+
mimeTypeRecord = new MIMEType('text/plain;charset=US-ASCII');
104+
}
105+
106+
return { mimeType: mimeTypeRecord, body };
107+
}

src/utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import path from 'path';
22
import urlUtils from 'url';
33

44
import sourceMap from 'source-map';
5-
import parseDataURL from 'data-urls';
65

76
import { decode } from 'iconv-lite';
8-
97
import { urlToRequest } from 'loader-utils';
108

9+
import parseDataURL from './parse-data-url';
1110
import labelsToNames from './labels-to-names';
1211

1312
// Matches only the last occurrence of sourceMappingURL

0 commit comments

Comments
 (0)