Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace fast-uri url parser with URL contructor #144

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ fastify.register(require('@fastify/url-data'))

fastify.get('/foo', (req, reply) => {
const urlData = req.urlData()
req.log.info(urlData.path) // '/foo'
req.log.info(urlData.query) // 'a=b&c=d'
req.log.info(urlData.host) // '127.0.0.1'
req.log.info(urlData.pathname) // '/foo'
req.log.info(urlData.search) // '?a=b&c=d'
req.log.info(urlData.hostname) // '127.0.0.1'
req.log.info(urlData.port) // 8080

// if you just need single data:
req.log.info(req.urlData('path')) // '/foo'
req.log.info(req.urlData('pathname')) // '/foo'

reply.send({hello: 'world'})
})
Expand Down
45 changes: 45 additions & 0 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const benchmark = require('benchmark')
const Fastify = require('fastify')
const plugin = require('../plugin')

async function getFastify () {
const fastify = Fastify()
fastify.register(plugin).after((err) => {
if (err) console.error(err)
})

fastify.get('/one', (req, reply) => {
req.urlData()
reply.send()
})

try {
await fastify.listen({ port: 3000 })
} catch (err) {
process.exit(1)
}

return fastify
}

const bench = async () => {
const fastify = await getFastify()

new benchmark.Suite()
.add('urlData', async function () {
await fastify.inject({
method: 'GET',
url: '/one?a=b&c=d#foo'
})
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'))
fastify.close()
})
.run({ async: true })
}

bench()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
"@types/node": "^20.1.0",
"benchmark": "^2.1.4",
"fastify": "^4.0.0-rc.2",
"h2url": "^0.2.0",
"semver": "^7.3.2",
Expand All @@ -40,7 +41,6 @@
"tsd": "~0.30.0"
},
"dependencies": {
"fast-uri": "^2.2.0",
"fastify-plugin": "^4.0.0"
},
"publishConfig": {
Expand Down
8 changes: 4 additions & 4 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict'

const fp = require('fastify-plugin')
const fastUri = require('fast-uri')

function fastifyUrlData (fastify, options, next) {
fastify.decorateRequest('urlData', function (key) {
const scheme = this.headers[':scheme'] ? this.headers[':scheme'] : this.protocol
const host = this.hostname
const path = this.headers[':path'] || this.raw.url
const urlData = fastUri.parse(scheme + '://' + host + path)
if (key) return urlData[key]
return urlData
const url = new URL(scheme + '://' + host + path)

if (key) return url[key]
return url
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you avoid the breaking change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid it, would mean converting the URL to an URIComponent object.
I can map it 1-to-1 and return what we previously had, but there's a s mall caveat where fast-uri previously also had a prop on the URIComponent called userinfo which returned something like this : "user:pass".
By default the URL does not have it, it would require creating ourselves just like as fast-uri does it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind

})
next()
}
Expand Down
84 changes: 42 additions & 42 deletions test/tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const urlHost = 'localhost'
const urlForwardedHost = 'example.com'
const urlPath = '/one'
const urlQuery = 'a=b&c=d'
const httpScheme = 'http'
const httpsScheme = 'https'
const httpScheme = 'http:'
const httpsScheme = 'https:'

test('parses a full URI', (t) => {
t.plan(10)
let port
let port = ''
const fastify = Fastify()

fastify
Expand All @@ -28,24 +28,24 @@ test('parses a full URI', (t) => {

fastify.get(urlPath, (req, reply) => {
const uriData = req.urlData()
t.equal(uriData.host, urlHost)
t.equal(uriData.hostname, urlHost)
t.equal(uriData.port, port)
t.equal(uriData.path, urlPath)
t.equal(uriData.query, urlQuery)
t.equal(uriData.scheme, httpScheme)
t.equal(req.urlData('host'), urlHost)
t.equal(uriData.pathname, urlPath)
t.equal(uriData.search, `?${urlQuery}`)
t.equal(uriData.protocol, httpScheme)
t.equal(req.urlData('hostname'), urlHost)
t.equal(req.urlData('port'), port)
t.equal(req.urlData('path'), urlPath)
t.equal(req.urlData('query'), urlQuery)
t.equal(req.urlData('scheme'), httpScheme)
t.equal(req.urlData('pathname'), urlPath)
t.equal(req.urlData('search'), `?${urlQuery}`)
t.equal(req.urlData('protocol'), httpScheme)
reply.send()
})

fastify.listen({ port: 0 }, (err) => {
fastify.server.unref()
if (err) t.threw(err)

port = fastify.server.address().port
port = fastify.server.address().port.toString()
http
.get(`http://${urlHost}:${port}${urlPath}?${urlQuery}#foo`, () => {})
.on('error', t.threw)
Expand All @@ -59,7 +59,7 @@ test('parses a full URI in HTTP2', { skip: semver.lt(process.versions.node, '8.8

const h2url = require('h2url')

let port
let port = ''
let fastify
try {
fastify = Fastify({
Expand All @@ -82,24 +82,24 @@ test('parses a full URI in HTTP2', { skip: semver.lt(process.versions.node, '8.8

fastify.get(urlPath, (req, reply) => {
const uriData = req.urlData()
t.equal(uriData.host, urlHost)
t.equal(uriData.hostname, urlHost)
t.equal(uriData.port, port)
t.equal(uriData.path, urlPath)
t.equal(uriData.query, urlQuery)
t.equal(uriData.scheme, httpsScheme)
t.equal(req.urlData('host'), urlHost)
t.equal(uriData.pathname, urlPath)
t.equal(uriData.search, `?${urlQuery}`)
t.equal(uriData.protocol, httpsScheme)
t.equal(req.urlData('hostname'), urlHost)
t.equal(req.urlData('port'), port)
t.equal(req.urlData('path'), urlPath)
t.equal(req.urlData('query'), urlQuery)
t.equal(req.urlData('scheme'), httpsScheme)
t.equal(req.urlData('pathname'), urlPath)
t.equal(req.urlData('search'), `?${urlQuery}`)
t.equal(req.urlData('protocol'), httpsScheme)
reply.send()
})

fastify.listen({ port: 0 }, (err) => {
fastify.server.unref()
if (err) t.threw(err)

port = fastify.server.address().port
port = fastify.server.address().port.toString()
h2url.concat({ url: `https://${urlHost}:${port}${urlPath}?${urlQuery}#foo` }).then(() => {})
})

Expand All @@ -108,7 +108,7 @@ test('parses a full URI in HTTP2', { skip: semver.lt(process.versions.node, '8.8

test('parses a full URI using X-Forwarded-Host when trustProxy is set', (t) => {
t.plan(10)
let port
let port = ''
const fastify = Fastify({ trustProxy: true }) // Setting trustProxy true will use X-Forwarded-Host header if set

fastify
Expand All @@ -119,24 +119,24 @@ test('parses a full URI using X-Forwarded-Host when trustProxy is set', (t) => {

fastify.get(urlPath, (req, reply) => {
const uriData = req.urlData()
t.equal(uriData.host, urlForwardedHost)
t.equal(uriData.hostname, urlForwardedHost)
t.equal(uriData.port, port)
t.equal(uriData.path, urlPath)
t.equal(uriData.query, urlQuery)
t.equal(uriData.scheme, httpScheme)
t.equal(req.urlData('host'), urlForwardedHost)
t.equal(uriData.pathname, urlPath)
t.equal(uriData.search, `?${urlQuery}`)
t.equal(uriData.protocol, httpScheme)
t.equal(req.urlData('hostname'), urlForwardedHost)
t.equal(req.urlData('port'), port)
t.equal(req.urlData('path'), urlPath)
t.equal(req.urlData('query'), urlQuery)
t.equal(req.urlData('scheme'), httpScheme)
t.equal(req.urlData('pathname'), urlPath)
t.equal(req.urlData('search'), `?${urlQuery}`)
t.equal(req.urlData('protocol'), httpScheme)
reply.send()
})

fastify.listen({ port: 0 }, (err) => {
fastify.server.unref()
if (err) t.threw(err)

port = fastify.server.address().port
port = fastify.server.address().port.toString()
http
.get(`http://${urlHost}:${port}${urlPath}?${urlQuery}#foo`, { headers: { 'X-Forwarded-Host': `${urlForwardedHost}:${port}` } }, () => {})
.on('error', t.threw)
Expand All @@ -147,7 +147,7 @@ test('parses a full URI using X-Forwarded-Host when trustProxy is set', (t) => {

test('parses a full URI ignoring X-Forwarded-Host when trustProxy is not set', (t) => {
t.plan(10)
let port
let port = ''
const fastify = Fastify()

fastify
Expand All @@ -158,24 +158,24 @@ test('parses a full URI ignoring X-Forwarded-Host when trustProxy is not set', (

fastify.get(urlPath, (req, reply) => {
const uriData = req.urlData()
t.equal(uriData.host, urlHost)
t.equal(uriData.hostname, urlHost)
t.equal(uriData.port, port)
t.equal(uriData.path, urlPath)
t.equal(uriData.query, urlQuery)
t.equal(uriData.scheme, httpScheme)
t.equal(req.urlData('host'), urlHost)
t.equal(uriData.pathname, urlPath)
t.equal(uriData.search, `?${urlQuery}`)
t.equal(uriData.protocol, httpScheme)
t.equal(req.urlData('hostname'), urlHost)
t.equal(req.urlData('port'), port)
t.equal(req.urlData('path'), urlPath)
t.equal(req.urlData('query'), urlQuery)
t.equal(req.urlData('scheme'), httpScheme)
t.equal(req.urlData('pathname'), urlPath)
t.equal(req.urlData('search'), `?${urlQuery}`)
t.equal(req.urlData('protocol'), httpScheme)
reply.send()
})

fastify.listen({ port: 0 }, (err) => {
fastify.server.unref()
if (err) t.threw(err)

port = fastify.server.address().port
port = fastify.server.address().port.toString()
http
.get(`http://${urlHost}:${port}${urlPath}?${urlQuery}#foo`, { headers: { 'X-Forwarded-Host': `${urlForwardedHost}:${port}` } }, () => {})
.on('error', t.threw)
Expand Down
5 changes: 2 additions & 3 deletions types/fastify-url-data.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { FastifyPluginCallback } from 'fastify';
import { URIComponent } from 'fast-uri'

type FastifyUrlData = FastifyPluginCallback

declare module 'fastify' {
interface FastifyRequest {
urlData<K extends keyof URIComponent>(target: K): URIComponent[K]
urlData(): URIComponent
urlData<K extends keyof URL>(target: K): URL[K]
urlData(): URL
}
}

Expand Down
13 changes: 6 additions & 7 deletions types/fastify-url-data.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ const server = fastify();
server.register(urlData)

server.get('/data', (req, reply) => {
console.log(req.urlData)
expectType<string|undefined>(req.urlData().path);
expectType<string|undefined>(req.urlData().host);
expectType<string|number|undefined>(req.urlData().port);
expectType<string|undefined>(req.urlData().query);
expectType<string|''>(req.urlData().pathname);
expectType<string|''>(req.urlData().hostname);
expectType<string|''>(req.urlData().port);
expectType<string|''>(req.urlData().search);

expectType<string|undefined>(req.urlData('path'));
expectType<string|number|undefined>(req.urlData('port'));
expectType<string|''>(req.urlData('pathname'));
expectType<string|''>(req.urlData('port'));

reply.send({msg: 'ok'})
})
Expand Down