UNPKG

next-csrf

Version:

CSRF mitigation library for Next.js

1 lines 3.38 kB
{"ast":null,"code":"\"use strict\";\n\nexports.__esModule = true;\nexports.parseRelativeUrl = parseRelativeUrl;\n\nvar _utils = require(\"../../utils\");\n\nvar DUMMY_BASE = new URL(false ? 'http://n' : (0, _utils.getLocationOrigin)());\n/**\n* Parses path-relative urls (e.g. `/hello/world?foo=bar`). If url isn't path-relative\n* (e.g. `./hello`) then at least base must be.\n* Absolute urls are rejected with one exception, in the browser, absolute urls that are on\n* the current origin will be parsed as relative\n*/\n\nfunction parseRelativeUrl(url, base) {\n var resolvedBase = base ? new URL(base, DUMMY_BASE) : DUMMY_BASE;\n\n var _URL = new URL(url, resolvedBase),\n pathname = _URL.pathname,\n searchParams = _URL.searchParams,\n search = _URL.search,\n hash = _URL.hash,\n href = _URL.href,\n origin = _URL.origin,\n protocol = _URL.protocol;\n\n if (origin !== DUMMY_BASE.origin || protocol !== 'http:' && protocol !== 'https:') {\n throw new Error('invariant: invalid relative URL');\n }\n\n return {\n pathname: pathname,\n searchParams: searchParams,\n search: search,\n hash: hash,\n href: href.slice(DUMMY_BASE.origin.length)\n };\n}","map":{"version":3,"sources":["../../../../../next-server/lib/router/utils/parse-relative-url.ts"],"names":["DUMMY_BASE","resolvedBase","base","origin","protocol","pathname","searchParams","search","hash","href"],"mappings":";;;;;AAAA,IAAA,MAAA,GAAA,OAAA,CAAA,aAAA,CAAA;;AAEA,IAAMA,UAAU,GAAG,IAAA,GAAA,CACjB,QAAA,UAAA,GAA6C,CAAA,GAAA,MAAA,CAD/C,iBAC+C,GAD5B,CAAnB;AAIA;;;;;;;AAMO,SAAA,gBAAA,CAAA,GAAA,EAAA,IAAA,EAAsD;AAC3D,MAAMC,YAAY,GAAGC,IAAI,GAAG,IAAA,GAAA,CAAA,IAAA,EAAH,UAAG,CAAH,GAAzB,UAAA;;AAD2D,aAUvD,IAAA,GAAA,CAAA,GAAA,EARJ,YAQI,CAVuD;AAAA,MAErD,QAFqD,QAErD,QAFqD;AAAA,MAErD,YAFqD,QAErD,YAFqD;AAAA,MAErD,MAFqD,QAErD,MAFqD;AAAA,MAErD,IAFqD,QAErD,IAFqD;AAAA,MAErD,IAFqD,QAErD,IAFqD;AAAA,MAErD,MAFqD,QAErD,MAFqD;AAAA,MAErD,QAFqD,QAErD,QAFqD;;AAW3D,MACEC,MAAM,KAAKH,UAAU,CAArBG,MAAAA,IACCC,QAAQ,KAARA,OAAAA,IAAwBA,QAAQ,KAFnC,QAAA,EAGE;AACA,UAAM,IAAA,KAAA,CAAN,iCAAM,CAAN;AAEF;;AAAA,SAAO;AACLC,IAAAA,QADK,EACLA,QADK;AAELC,IAAAA,YAFK,EAELA,YAFK;AAGLC,IAAAA,MAHK,EAGLA,MAHK;AAILC,IAAAA,IAJK,EAILA,IAJK;AAKLC,IAAAA,IAAI,EAAEA,IAAI,CAAJA,KAAAA,CAAWT,UAAU,CAAVA,MAAAA,CALnB,MAKQS;AALD,GAAP;AAOD","sourcesContent":["import { getLocationOrigin } from '../../utils'\n\nconst DUMMY_BASE = new URL(\n typeof window === 'undefined' ? 'http://n' : getLocationOrigin()\n)\n\n/**\n * Parses path-relative urls (e.g. `/hello/world?foo=bar`). If url isn't path-relative\n * (e.g. `./hello`) then at least base must be.\n * Absolute urls are rejected with one exception, in the browser, absolute urls that are on\n * the current origin will be parsed as relative\n */\nexport function parseRelativeUrl(url: string, base?: string) {\n const resolvedBase = base ? new URL(base, DUMMY_BASE) : DUMMY_BASE\n const {\n pathname,\n searchParams,\n search,\n hash,\n href,\n origin,\n protocol,\n } = new URL(url, resolvedBase)\n if (\n origin !== DUMMY_BASE.origin ||\n (protocol !== 'http:' && protocol !== 'https:')\n ) {\n throw new Error('invariant: invalid relative URL')\n }\n return {\n pathname,\n searchParams,\n search,\n hash,\n href: href.slice(DUMMY_BASE.origin.length),\n }\n}\n"]},"metadata":{},"sourceType":"script"}