aboutsummaryrefslogtreecommitdiff
path: root/node_modules/decode-uri-component/index.js
diff options
context:
space:
mode:
authorDimitri Staessens <dimitri@ouroboros.rocks>2019-10-06 21:37:45 +0200
committerDimitri Staessens <dimitri@ouroboros.rocks>2019-10-06 21:37:45 +0200
commit3c51c3be85bb0d1bdb87ea0d6632f1c256912f27 (patch)
treec7ccc8279b12c4f7bdbbb4270d617e48f51722e4 /node_modules/decode-uri-component/index.js
parent412c104bebc507bea9c94fd53b5bdc4b64cbfe31 (diff)
downloadwebsite-3c51c3be85bb0d1bdb87ea0d6632f1c256912f27.tar.gz
website-3c51c3be85bb0d1bdb87ea0d6632f1c256912f27.zip
build: Add some required modules for node
Diffstat (limited to 'node_modules/decode-uri-component/index.js')
-rw-r--r--node_modules/decode-uri-component/index.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/node_modules/decode-uri-component/index.js b/node_modules/decode-uri-component/index.js
new file mode 100644
index 0000000..691499b
--- /dev/null
+++ b/node_modules/decode-uri-component/index.js
@@ -0,0 +1,94 @@
+'use strict';
+var token = '%[a-f0-9]{2}';
+var singleMatcher = new RegExp(token, 'gi');
+var multiMatcher = new RegExp('(' + token + ')+', 'gi');
+
+function decodeComponents(components, split) {
+ try {
+ // Try to decode the entire string first
+ return decodeURIComponent(components.join(''));
+ } catch (err) {
+ // Do nothing
+ }
+
+ if (components.length === 1) {
+ return components;
+ }
+
+ split = split || 1;
+
+ // Split the array in 2 parts
+ var left = components.slice(0, split);
+ var right = components.slice(split);
+
+ return Array.prototype.concat.call([], decodeComponents(left), decodeComponents(right));
+}
+
+function decode(input) {
+ try {
+ return decodeURIComponent(input);
+ } catch (err) {
+ var tokens = input.match(singleMatcher);
+
+ for (var i = 1; i < tokens.length; i++) {
+ input = decodeComponents(tokens, i).join('');
+
+ tokens = input.match(singleMatcher);
+ }
+
+ return input;
+ }
+}
+
+function customDecodeURIComponent(input) {
+ // Keep track of all the replacements and prefill the map with the `BOM`
+ var replaceMap = {
+ '%FE%FF': '\uFFFD\uFFFD',
+ '%FF%FE': '\uFFFD\uFFFD'
+ };
+
+ var match = multiMatcher.exec(input);
+ while (match) {
+ try {
+ // Decode as big chunks as possible
+ replaceMap[match[0]] = decodeURIComponent(match[0]);
+ } catch (err) {
+ var result = decode(match[0]);
+
+ if (result !== match[0]) {
+ replaceMap[match[0]] = result;
+ }
+ }
+
+ match = multiMatcher.exec(input);
+ }
+
+ // Add `%C2` at the end of the map to make sure it does not replace the combinator before everything else
+ replaceMap['%C2'] = '\uFFFD';
+
+ var entries = Object.keys(replaceMap);
+
+ for (var i = 0; i < entries.length; i++) {
+ // Replace all decoded components
+ var key = entries[i];
+ input = input.replace(new RegExp(key, 'g'), replaceMap[key]);
+ }
+
+ return input;
+}
+
+module.exports = function (encodedURI) {
+ if (typeof encodedURI !== 'string') {
+ throw new TypeError('Expected `encodedURI` to be of type `string`, got `' + typeof encodedURI + '`');
+ }
+
+ try {
+ encodedURI = encodedURI.replace(/\+/g, ' ');
+
+ // Try the built in decoder first
+ return decodeURIComponent(encodedURI);
+ } catch (err) {
+ // Fallback to a more advanced decoder
+ return customDecodeURIComponent(encodedURI);
+ }
+};