rollup-plugin-sourcemaps2
Version:
Rollup plugin for grabbing source maps from sourceMappingURLs
1 lines • 5.74 kB
Source Map (JSON)
{"version":3,"file":"decode-uri-component.cjs","sources":["../src/decode-uri-component.ts"],"sourcesContent":["const token = '%[a-f0-9]{2}';\nconst singleMatcher = new RegExp(`(${token})|([^%]+?)`, 'gi');\nconst multiMatcher = new RegExp(`(${token})+`, 'gi');\n\nfunction decodeComponents(components: RegExpMatchArray | [], split?: number): string[] {\n let splitAt = split;\n\n try {\n // Try to decode the entire string first\n return [decodeURIComponent(components.join(''))];\n } catch {\n // Do nothing\n }\n\n if (components.length === 1) {\n return components;\n }\n\n splitAt = splitAt ?? 1;\n\n // Split the array in 2 parts\n const left = components.slice(0, splitAt) as RegExpMatchArray;\n const right = components.slice(splitAt) as RegExpMatchArray;\n\n return Array.prototype.concat.call(\n [],\n decodeComponents(left),\n decodeComponents(right),\n ) as string[];\n}\n\nfunction decode(input: string): string {\n let localInput = input;\n\n try {\n return decodeURIComponent(localInput);\n } catch {\n let tokens = RegExp(singleMatcher).exec(localInput) || [];\n\n for (let i = 1; i < tokens.length; i++) {\n localInput = decodeComponents(tokens, i).join('');\n\n tokens = RegExp(singleMatcher).exec(localInput) || [];\n }\n\n return localInput;\n }\n}\n\nfunction customDecodeURIComponent(input: string): string {\n let localInput = input;\n\n // Keep track of all the replacements and prefill the map with the `BOM`\n const replaceMap: Record<string, string> = {\n '%FE%FF': '\\uFFFD\\uFFFD',\n '%FF%FE': '\\uFFFD\\uFFFD',\n };\n\n let match = multiMatcher.exec(localInput);\n while (match) {\n try {\n // Decode as big chunks as possible\n replaceMap[match[0]] = decodeURIComponent(match[0]);\n } catch {\n const result = decode(match[0]);\n\n if (result !== match[0]) {\n replaceMap[match[0]] = result;\n }\n }\n\n match = multiMatcher.exec(localInput);\n }\n\n // Add `%C2` at the end of the map to make sure it does not replace the combinator before everything else\n replaceMap['%C2'] = '\\uFFFD';\n\n const entries = Object.keys(replaceMap);\n\n for (const key of entries) {\n // Replace all decoded components\n localInput = localInput.replace(new RegExp(key, 'g'), replaceMap[key]);\n }\n\n return localInput;\n}\n\nexport default function decodeUriComponent(encodedURI: string): string {\n if (typeof encodedURI !== 'string') {\n throw new TypeError(\n `Expected \\`encodedURI\\` to be of type \\`string\\`, got \\`${typeof encodedURI}\\``,\n );\n }\n\n try {\n // Try the built in decoder first\n return decodeURIComponent(encodedURI);\n } catch {\n // Fallback to a more advanced decoder\n return customDecodeURIComponent(encodedURI);\n }\n}\n"],"names":["token","singleMatcher","RegExp","multiMatcher","decodeComponents","components","split","splitAt","decodeURIComponent","join","length","left","slice","right","Array","prototype","concat","call","decode","input","localInput","tokens","exec","i","customDecodeURIComponent","replaceMap","match","result","entries","Object","keys","key","replace","decodeUriComponent","encodedURI","TypeError"],"mappings":";;AAAA,MAAMA,KAAAA,GAAQ,cAAA;AACd,MAAMC,aAAAA,GAAgB,IAAIC,MAAAA,CAAO,CAAC,CAAC,EAAEF,KAAAA,CAAM,UAAU,CAAC,EAAE,IAAA,CAAA;AACxD,MAAMG,YAAAA,GAAe,IAAID,MAAAA,CAAO,CAAC,CAAC,EAAEF,KAAAA,CAAM,EAAE,CAAC,EAAE,IAAA,CAAA;AAE/C,SAASI,gBAAAA,CAAiBC,UAAiC,EAAEC,KAAc,EAAA;AACzE,IAAA,IAAIC,OAAAA,GAAUD,KAAAA;IAEd,IAAI;;QAEF,OAAO;YAACE,kBAAAA,CAAmBH,UAAAA,CAAWI,IAAI,CAAC,EAAA,CAAA;AAAK,SAAA;AAClD,IAAA,CAAA,CAAE,OAAM;;AAER,IAAA;IAEA,IAAIJ,UAAAA,CAAWK,MAAM,KAAK,CAAA,EAAG;QAC3B,OAAOL,UAAAA;AACT,IAAA;AAEAE,IAAAA,OAAAA,GAAUA,OAAAA,IAAW,CAAA;;AAGrB,IAAA,MAAMI,IAAAA,GAAON,UAAAA,CAAWO,KAAK,CAAC,CAAA,EAAGL,OAAAA,CAAAA;IACjC,MAAMM,KAAAA,GAA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