Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Aug 29, 2020
1 parent 78effee commit 4ac6d3c
Show file tree
Hide file tree
Showing 7 changed files with 796 additions and 304 deletions.
586 changes: 291 additions & 295 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@
"webpack": "^4.0.0 || ^5.0.0"
},
"dependencies": {
"data-urls": "^2.0.0",
"abab": "^2.0.4",
"iconv-lite": "^0.6.2",
"loader-utils": "^2.0.0",
"schema-utils": "^2.7.0",
"source-map": "^0.6.1"
"source-map": "^0.6.1",
"whatwg-mimetype": "^2.3.0"
},
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.11.1",
"@babel/core": "^7.11.4",
"@babel/preset-env": "^7.11.0",
"@commitlint/cli": "^10.0.0",
"@commitlint/config-conventional": "^10.0.0",
Expand All @@ -63,11 +64,11 @@
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"husky": "^4.2.5",
"jest": "^26.4.0",
"lint-staged": "^10.2.11",
"jest": "^26.4.2",
"lint-staged": "^10.2.13",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"prettier": "^2.1.1",
"standard-version": "^9.0.0",
"webpack": "^4.44.1"
},
Expand Down
2 changes: 1 addition & 1 deletion src/labels-to-names.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const labelToNames = {
'866': 'IBM866',
866: 'IBM866',
'unicode-1-1-utf-8': 'UTF-8',
'utf-8': 'UTF-8',
utf8: 'UTF-8',
Expand Down
107 changes: 107 additions & 0 deletions src/parse-data-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import MIMEType from 'whatwg-mimetype';
import { atob } from 'abab';

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);
}

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 mimeType = '';

while (position < input.length && input[position] !== ',') {
mimeType += input[position];
position += 1;
}

mimeType = mimeType.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(mimeType);

if (mimeTypeBase64MatchResult) {
const stringBody = body.toString('binary');
const asString = atob(stringBody);

if (asString === null) {
return null;
}

body = Buffer.from(asString, 'binary');

[, mimeType] = mimeTypeBase64MatchResult;
}

if (mimeType.startsWith(';')) {
mimeType = `text/plain ${mimeType}`;
}

let mimeTypeRecord;

try {
mimeTypeRecord = new MIMEType(mimeType);
} catch (e) {
mimeTypeRecord = new MIMEType('text/plain;charset=US-ASCII');
}

return { mimeType: mimeTypeRecord, body };
}
3 changes: 1 addition & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import path from 'path';
import urlUtils from 'url';

import sourceMap from 'source-map';
import parseDataURL from 'data-urls';

import { decode } from 'iconv-lite';

import { urlToRequest } from 'loader-utils';

import parseDataURL from './parse-data-url';
import labelsToNames from './labels-to-names';

// Matches only the last occurrence of sourceMappingURL
Expand Down
Loading

0 comments on commit 4ac6d3c

Please sign in to comment.