UNPKG

next-csrf

Version:

CSRF mitigation library for Next.js

1 lines 3.04 kB
{"ast":null,"code":"\"use strict\";\n\nexports.__esModule = true;\nexports.parseRelativeUrl = parseRelativeUrl;\n\nvar _utils = require(\"../../utils\");\n\nconst DUMMY_BASE = new URL(true ? '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 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\n if (origin !== DUMMY_BASE.origin || protocol !== 'http:' && protocol !== 'https:') {\n throw new Error('invariant: invalid relative URL');\n }\n\n return {\n pathname,\n searchParams,\n search,\n 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","href"],"mappings":";;;;;AAAA,IAAA,MAAA,GAAA,OAAA,CAAA,aAAA,CAAA;;AAEA,MAAMA,UAAU,GAAG,IAAA,GAAA,CACjB,OAAA,UAAA,GAA6C,CAAA,GAAA,MAAA,CAD/C,iBAC+C,GAD5B,CAAnB;AAIA;;;;;;;AAMO,SAAA,gBAAA,CAAA,GAAA,EAAA,IAAA,EAAsD;AAC3D,QAAMC,YAAY,GAAGC,IAAI,GAAG,IAAA,GAAA,CAAA,IAAA,EAAH,UAAG,CAAH,GAAzB,UAAA;AACA,QAAM;AAAA,IAAA,QAAA;AAAA,IAAA,YAAA;AAAA,IAAA,MAAA;AAAA,IAAA,IAAA;AAAA,IAAA,IAAA;AAAA,IAAA,MAAA;AAAA,IAAA;AAAA,MAQF,IAAA,GAAA,CAAA,GAAA,EARJ,YAQI,CARJ;;AASA,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;AAAA,IAAA,QAAA;AAAA,IAAA,YAAA;AAAA,IAAA,MAAA;AAAA,IAAA,IAAA;AAKLC,IAAAA,IAAI,EAAEA,IAAI,CAAJA,KAAAA,CAAWL,UAAU,CAAVA,MAAAA,CALnB,MAKQK;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"}