rollup-plugin-sourcemaps2
Version:
Rollup plugin for grabbing source maps from sourceMappingURLs
1 lines • 5.66 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,KAAQ,GAAA,cAAA;AACd,MAAMC,aAAAA,GAAgB,IAAIC,MAAO,CAAA,CAAC,CAAC,EAAEF,KAAAA,CAAM,UAAU,CAAC,EAAE,IAAA,CAAA;AACxD,MAAMG,YAAAA,GAAe,IAAID,MAAO,CAAA,CAAC,CAAC,EAAEF,KAAAA,CAAM,EAAE,CAAC,EAAE,IAAA,CAAA;AAE/C,SAASI,gBAAAA,CAAiBC,UAAiC,EAAEC,KAAc,EAAA;AACzE,IAAA,IAAIC,OAAUD,GAAAA,KAAAA;IAEd,IAAI;;QAEF,OAAO;YAACE,kBAAmBH,CAAAA,UAAAA,CAAWI,IAAI,CAAC,EAAA,CAAA;AAAK,SAAA;AAClD,KAAA,CAAE,OAAM;;AAER;IAEA,IAAIJ,UAAAA,CAAWK,MAAM,KAAK,CAAG,EAAA;QAC3B,OAAOL,UAAAA;AACT;AAEAE,IAAAA,OAAAA,GAAUA,OAAW,IAAA,CAAA;;AAGrB,IAAA,MAAMI,IAAON,GAAAA,UAAAA,CAAWO,KAAK,CAAC,CAAGL,EAAAA,OAAAA,CAAAA;IACjC,MAAMM,KAAAA,GAAQR,UAAWO,CAAAA,KAA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