{"version":3,"file":"parse-BrChHTWb.mjs","names":[],"sources":["../src/utils/parse.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { stripComments } from \"./strip-comments\";\n\n// https://github.com/fastify/secure-json-parse\n// https://github.com/hapijs/bourne\nconst suspectProtoRx =\n  /\"(?:_|\\\\u0{2}5[Ff]){2}(?:p|\\\\u0{2}70)(?:r|\\\\u0{2}72)(?:o|\\\\u0{2}6[Ff])(?:t|\\\\u0{2}74)(?:o|\\\\u0{2}6[Ff])(?:_|\\\\u0{2}5[Ff]){2}\"\\s*:/;\nconst suspectConstructorRx =\n  /\"(?:c|\\\\u0063)(?:o|\\\\u006[Ff])(?:n|\\\\u006[Ee])(?:s|\\\\u0073)(?:t|\\\\u0074)(?:r|\\\\u0072)(?:u|\\\\u0075)(?:c|\\\\u0063)(?:t|\\\\u0074)(?:o|\\\\u006[Ff])(?:r|\\\\u0072)\"\\s*:/;\n\nconst JsonSigRx = /^\\s*[\"[{]|^\\s*-?\\d{1,16}(?:\\.\\d{1,17})?(?:E[+-]?\\d+)?\\s*$/i;\n\nfunction jsonParseTransform(key: string, value: any): any {\n  if (\n    key === \"__proto__\" ||\n    (key === \"constructor\" &&\n      value &&\n      typeof value === \"object\" &&\n      \"prototype\" in value)\n  ) {\n    // eslint-disable-next-line no-console\n    console.warn(`Dropping \"${key}\" key to prevent prototype pollution.`);\n    return;\n  }\n  return value;\n}\n\nexport interface Options {\n  strict?: boolean;\n}\n\nexport function parse<T = unknown>(value: any, options: Options = {}): T {\n  if (typeof value !== \"string\") {\n    return value;\n  }\n\n  let stripped = stripComments(value);\n\n  if (\n    stripped[0] === '\"' &&\n    stripped[stripped.length - 1] === '\"' &&\n    !stripped.includes(\"\\\\\")\n  ) {\n    return stripped.slice(1, -1) as T;\n  }\n\n  stripped = stripped.trim();\n\n  if (stripped.length <= 9) {\n    switch (stripped.toLowerCase()) {\n      case \"true\": {\n        return true as T;\n      }\n      case \"false\": {\n        return false as T;\n      }\n      case \"undefined\": {\n        return undefined as T;\n      }\n      case \"null\": {\n        return null as T;\n      }\n      case \"nan\": {\n        return Number.NaN as T;\n      }\n      case \"infinity\": {\n        return Number.POSITIVE_INFINITY as T;\n      }\n      case \"-infinity\": {\n        return Number.NEGATIVE_INFINITY as T;\n      }\n    }\n  }\n\n  if (!JsonSigRx.test(stripped)) {\n    if (options.strict) {\n      throw new Error(\"Invalid JSON\");\n    }\n    return stripped as T;\n  }\n\n  try {\n    if (suspectProtoRx.test(stripped) || suspectConstructorRx.test(stripped)) {\n      if (options.strict) {\n        throw new Error(\"Possible prototype pollution\");\n      }\n      return JSON.parse(stripped, jsonParseTransform);\n    }\n\n    return JSON.parse(stripped);\n  } catch (error) {\n    if (options.strict) {\n      throw error;\n    }\n    return value as T;\n  }\n}\n\nexport function safeParse<T = unknown>(value: any, options: Options = {}): T {\n  return parse<T>(value, { ...options, strict: true });\n}\n"],"mappings":";;;AAsBA,MAAM,iBACJ;AACF,MAAM,uBACJ;AAEF,MAAM,YAAY;AAElB,SAAS,mBAAmB,KAAa,OAAiB;AACxD,KACE,QAAQ,eACP,QAAQ,iBACP,SACA,OAAO,UAAU,YACjB,eAAe,OACjB;AAEA,UAAQ,KAAK,aAAa,IAAI,uCAAuC;AACrE;;AAEF,QAAO;;AAOT,SAAgB,MAAmB,OAAY,UAAmB,EAAE,EAAK;AACvE,KAAI,OAAO,UAAU,SACnB,QAAO;CAGT,IAAI,WAAW,cAAc,MAAM;AAEnC,KACE,SAAS,OAAO,QAChB,SA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