|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | const path = require('path')
|
4 |
| -const url = require('url') |
5 | 4 | const statSync = require('fs').statSync
|
6 | 5 | const { PassThrough } = require('readable-stream')
|
7 | 6 | const glob = require('glob')
|
@@ -152,9 +151,7 @@ async function fastifyStatic (fastify, opts) {
|
152 | 151 | }
|
153 | 152 |
|
154 | 153 | if (opts.redirect === true) {
|
155 |
| - /* eslint node/no-deprecated-api: "off" */ |
156 |
| - const parsed = url.parse(request.raw.url) |
157 |
| - reply.redirect(301, parsed.pathname + '/' + (parsed.search || '')) |
| 154 | + reply.redirect(301, getRedirectUrl(request.raw.url)) |
158 | 155 | } else {
|
159 | 156 | reply.callNotFound()
|
160 | 157 | }
|
@@ -275,9 +272,7 @@ async function fastifyStatic (fastify, opts) {
|
275 | 272 | })
|
276 | 273 | if (opts.redirect === true && prefix !== opts.prefix) {
|
277 | 274 | fastify.get(opts.prefix, routeOpts, function (req, reply) {
|
278 |
| - /* eslint node/no-deprecated-api: "off" */ |
279 |
| - const parsed = url.parse(req.raw.url) |
280 |
| - reply.redirect(301, parsed.pathname + '/' + (parsed.search || '')) |
| 275 | + reply.redirect(301, getRedirectUrl(req.raw.url)) |
281 | 276 | })
|
282 | 277 | }
|
283 | 278 | } else {
|
@@ -436,6 +431,11 @@ function getEncodingExtension (encoding) {
|
436 | 431 | }
|
437 | 432 | }
|
438 | 433 |
|
| 434 | +function getRedirectUrl (url) { |
| 435 | + const parsed = new URL(url, 'http://localhost.com/') |
| 436 | + return parsed.pathname + (parsed.pathname[parsed.pathname.length - 1] !== '/' ? '/' : '') + (parsed.search || '') |
| 437 | +} |
| 438 | + |
439 | 439 | module.exports = fp(fastifyStatic, {
|
440 | 440 | fastify: '3.x',
|
441 | 441 | name: 'fastify-static'
|
|
0 commit comments