Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wsy5555621/e5afa57212a898de1bfd4bffa29dbd04 to your computer and use it in GitHub Desktop.
Save wsy5555621/e5afa57212a898de1bfd4bffa29dbd04 to your computer and use it in GitHub Desktop.

Revisions

  1. @somarlyonks somarlyonks revised this gist Mar 8, 2019. 2 changed files with 96 additions and 5093 deletions.
    96 changes: 96 additions & 0 deletions decode-uri-component.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    (function (exports, require, module, __filename, __dirname, process, global, Buffer) { return function (exports, require, module, __filename, __dirname) { '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);
    }
    };

    }.call(this, exports, require, module, __filename, __dirname); });
    5,093 changes: 0 additions & 5,093 deletions worker.js
    0 additions, 5,093 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
  2. @somarlyonks somarlyonks revised this gist Mar 8, 2019. 2 changed files with 14376 additions and 0 deletions.
    9,283 changes: 9,283 additions & 0 deletions index.js
    9,283 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    5,093 changes: 5,093 additions & 0 deletions worker.js
    5,093 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
  3. @somarlyonks somarlyonks renamed this gist Mar 8, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @somarlyonks somarlyonks created this gist Mar 8, 2019.
    6,431 changes: 6,431 additions & 0 deletions af-appx.worker.ide,min.js
    6,431 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.