Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 10 additions & 158 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,159 +16,31 @@ jobs:
strategy:
matrix:
name:
- Node.js 0.8
- Node.js 0.10
- Node.js 0.12
- io.js 1.x
- io.js 2.x
- io.js 3.x
- Node.js 4.x
- Node.js 5.x
- Node.js 6.x
- Node.js 7.x
- Node.js 8.x
- Node.js 9.x
- Node.js 10.x
- Node.js 11.x
- Node.js 12.x
- Node.js 13.x
- Node.js 14.x
- Node.js 15.x
- Node.js 16.x
- Node.js 17.x
- Node.js 18.x
- Node.js 19.x
- Node.js 20.x
- Node.js 21.x
- Node.js 22.x

include:
- name: Node.js 0.8
node-version: "0.8"
npm-i: [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser nyc

- name: Node.js 0.10
node-version: "0.10"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 0.12
node-version: "0.12"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: io.js 1.x
node-version: "1.8"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: io.js 2.x
node-version: "2.5"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: io.js 3.x
node-version: "3.3"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 4.x
node-version: "4.9"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 5.x
node-version: "5.12"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 6.x
node-version: "6.17"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 7.x
node-version: "7.10"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 8.x
node-version: "8.17"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 9.x
node-version: "9.11"
npm-i: [email protected] [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 10.x
node-version: "10.24"
npm-i: [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 11.x
node-version: "11.15"
npm-i: [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 12.x
node-version: "12.22"
npm-i: [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 13.x
node-version: "13.14"
npm-i: [email protected]
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 14.x
node-version: "14.21"
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 15.x
node-version: "15.14"
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 16.x
node-version: "16.19"
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 17.x
node-version: "17.9"
npm-rm: beautify-benchmark benchmark fast-url-parser
node-version: "16.x"

- name: Node.js 18.x
node-version: "18.13"
npm-rm: beautify-benchmark benchmark fast-url-parser

- name: Node.js 19.x
node-version: "19.6"
node-version: "18.x"

- name: Node.js 20.x
node-version: "20.12"

- name: Node.js 21.x
node-version: "21.7"
node-version: "20.x"

- name: Node.js 22.x
node-version: "22.0"
node-version: "22.x"

steps:
- uses: actions/checkout@v4

- name: Install Node.js ${{ matrix.node-version }}
shell: bash -eo pipefail -l {0}
run: |
nvm install --default ${{ matrix.node-version }}
if [[ "${{ matrix.node-version }}" == 0.* && "$(cut -d. -f2 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then
nvm install --alias=npm 0.10
nvm use ${{ matrix.node-version }}
sed -i '1s;^.*$;'"$(printf '#!%q' "$(nvm which npm)")"';' "$(readlink -f "$(which npm)")"
npm config set strict-ssl false
fi
dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH"
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Configure npm
run: |
Expand All @@ -178,26 +50,6 @@ jobs:
npm config set shrinkwrap false
fi

- name: Remove npm module(s) ${{ matrix.npm-rm }}
run: npm rm --silent --save-dev ${{ matrix.npm-rm }}
if: matrix.npm-rm != ''

- name: Install npm module(s) ${{ matrix.npm-i }}
run: npm install --save-dev ${{ matrix.npm-i }}
if: matrix.npm-i != ''

- name: Setup Node.js version-specific dependencies
shell: bash
run: |
# eslint for linting
# - remove on Node.js < 12
if [[ "$(cut -d. -f1 <<< "${{ matrix.node-version }}")" -lt 12 ]]; then
node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \
grep -E '^eslint(-|$)' | \
sort -r | \
xargs -n1 npm rm --silent --save-dev
fi

- name: Install Node.js dependencies
run: npm install

Expand All @@ -208,7 +60,7 @@ jobs:
echo "node@$(node -v)"
echo "npm@$(npm -v)"
npm -s ls ||:
(npm -s ls --depth=0 ||:) | awk -F'[ @]' 'NR>1 && $2 { print "::set-output name=" $2 "::" $3 }'
(npm -s ls --depth=0 ||:) | awk -F'[ @]' 'NR>1 && $2 { print $2 "=" $3 >> ENVIRON["GITHUB_OUTPUT"] }'

- name: Run tests
shell: bash
Expand All @@ -224,7 +76,7 @@ jobs:
run: npm run lint

- name: Collect code coverage
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@v2
if: steps.list_env.outputs.nyc != ''
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -238,7 +90,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Upload code coverage
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.github_token }}
parallel-finished: true
73 changes: 62 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/

var url = require('url')
var parse = url.parse // eslint-disable-line
var Url = url.Url

/**
Expand All @@ -24,6 +23,54 @@ var Url = url.Url
module.exports = parseurl
module.exports.original = originalurl

function parseUrlString (str) {
if (typeof str !== 'string') {
return {}
}

str = str.trim()

var result = Url !== undefined ? new Url() : {}
var parsedUrl

try {
var isRelative = str.charCodeAt(0) === 0x2f /* / */ || !str.includes('://')
parsedUrl = isRelative ? new URL(str, 'http://localhost') : new URL(str)
} catch (err) {
result.href = str
result.path = str
result.pathname = str
return result
}

result.href = str
result.path = parsedUrl.pathname + parsedUrl.search
result.pathname = parsedUrl.pathname
result.search = parsedUrl.search || null
result.query = parsedUrl.search ? parsedUrl.search.substring(1) : null
result.hash = parsedUrl.hash || null

if (parsedUrl.protocol && str.includes('://')) {
result.protocol = parsedUrl.protocol
result.host = parsedUrl.host
result.hostname = parsedUrl.hostname
result.port = parsedUrl.port || null
result.auth =
parsedUrl.username || parsedUrl.password
? (parsedUrl.username || '') +
(parsedUrl.password ? ':' + parsedUrl.password : '')
: null
} else {
result.protocol = null
result.host = null
result.hostname = null
result.port = null
result.auth = null
}

return result
}

/**
* Parse the `req` url with memoization.
*
Expand Down Expand Up @@ -52,7 +99,7 @@ function parseurl (req) {
parsed._raw = url

return (req._parsedUrl = parsed)
};
}

/**
* Parse the `req` original url with fallback and memoization.
Expand Down Expand Up @@ -82,7 +129,7 @@ function originalurl (req) {
parsed._raw = url

return (req._parsedOriginalUrl = parsed)
};
}

/**
* Parse the `str` url with fast-path short-cut.
Expand All @@ -94,7 +141,7 @@ function originalurl (req) {

function fastparse (str) {
if (typeof str !== 'string' || str.charCodeAt(0) !== 0x2f /* / */) {
return parse(str)
return parseUrlString(str)
}

var pathname = str
Expand All @@ -106,28 +153,30 @@ function fastparse (str) {
// And unrolls it into a for loop
for (var i = 1; i < str.length; i++) {
switch (str.charCodeAt(i)) {
case 0x3f: /* ? */
case 0x3f /* ? */:
if (search === null) {
pathname = str.substring(0, i)
query = str.substring(i + 1)
search = str.substring(i)
}
break
case 0x23 /* # */:
if (search === null) {
pathname = str.substring(0, i)
}
return parseUrlString(str)
case 0x09: /* \t */
case 0x0a: /* \n */
case 0x0c: /* \f */
case 0x0d: /* \r */
case 0x20: /* */
case 0x23: /* # */
case 0xa0:
case 0xfeff:
return parse(str)
return parseUrlString(str)
}
}

var url = Url !== undefined
? new Url()
: {}
var url = Url !== undefined ? new Url() : {}

url.path = str
url.href = str
Expand All @@ -151,8 +200,10 @@ function fastparse (str) {
*/

function fresh (url, parsedUrl) {
return typeof parsedUrl === 'object' &&
return (
typeof parsedUrl === 'object' &&
parsedUrl !== null &&
(Url === undefined || parsedUrl instanceof Url) &&
parsedUrl._raw === url
)
}