UNPKG

@foxify/http

Version:
83 lines (76 loc) 2.55 kB
/** * (The MIT License) * * Copyright (c) 2014 Jonathan Ong <me@jongleberry.com> * Copyright (c) 2014-2017 Douglas Christopher Wilson <doug@somethingdoug.com> * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * 'Software'), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * This module is modified and optimized for this project specifically */ import { parse, UrlWithStringQuery } from "url"; /** * Parse the `url` with fast-path short-cut. * * @param {String} url * @return {UrlWithStringQuery} * @public */ export default function parseUrl(url: string): UrlWithStringQuery { /* / */ if (url.charCodeAt(0) !== 0x2f) return parse(url); let pathname = url; let query = null; let search = null; // This takes the regexp from https://github.com/joyent/node/pull/7878 // Which is /^(\/[^?#\s]*)(\?[^#\s]*)?$/ // And unrolls it into a for loop for (let i = 1; i < url.length; i++) { switch (url.charCodeAt(i)) { case 0x3f: /* ? */ if (search === null) { pathname = url.substring(0, i); query = url.substring(i + 1); search = url.substring(i); } break; case 0x09: /* \t */ case 0x0a: /* \n */ case 0x0c: /* \f */ case 0x0d: /* \r */ case 0x20: /* */ case 0x23: /* # */ case 0xa0: case 0xfeff: return parse(url); default: break; } } // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return { pathname, query, search, href: url, path: url, } as never; }