@fastify/static 6.6.1 → 6.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/index.js +22 -21
- package/package.json +1 -1
package/LICENSE
CHANGED
package/index.js
CHANGED
|
@@ -349,13 +349,8 @@ async function fastifyStatic (fastify, opts) {
|
|
|
349
349
|
continue
|
|
350
350
|
}
|
|
351
351
|
routes.add(route)
|
|
352
|
-
fastify.head(route, routeOpts, function (req, reply) {
|
|
353
|
-
pumpSendToReply(req, reply, '/' + file, rootPath)
|
|
354
|
-
})
|
|
355
352
|
|
|
356
|
-
fastify
|
|
357
|
-
pumpSendToReply(req, reply, '/' + file, rootPath)
|
|
358
|
-
})
|
|
353
|
+
setUpHeadAndGet(fastify, routeOpts, route, '/' + file, rootPath)
|
|
359
354
|
|
|
360
355
|
const key = path.posix.basename(route)
|
|
361
356
|
if (indexes.includes(key) && !indexDirs.has(key)) {
|
|
@@ -367,28 +362,34 @@ async function fastifyStatic (fastify, opts) {
|
|
|
367
362
|
for (const [dirname, rootPath] of indexDirs.entries()) {
|
|
368
363
|
const pathname = dirname + (dirname.endsWith('/') ? '' : '/')
|
|
369
364
|
const file = '/' + pathname.replace(prefix, '')
|
|
370
|
-
|
|
371
|
-
fastify.head(pathname, routeOpts, function (req, reply) {
|
|
372
|
-
pumpSendToReply(req, reply, file, rootPath)
|
|
373
|
-
})
|
|
374
|
-
|
|
375
|
-
fastify.get(pathname, routeOpts, function (req, reply) {
|
|
376
|
-
pumpSendToReply(req, reply, file, rootPath)
|
|
377
|
-
})
|
|
365
|
+
setUpHeadAndGet(fastify, routeOpts, pathname, file, rootPath)
|
|
378
366
|
|
|
379
367
|
if (opts.redirect === true) {
|
|
380
|
-
fastify
|
|
381
|
-
pumpSendToReply(req, reply, file.replace(/\/$/, ''), rootPath)
|
|
382
|
-
})
|
|
383
|
-
fastify.get(pathname.replace(/\/$/, ''), routeOpts, function (req, reply) {
|
|
384
|
-
pumpSendToReply(req, reply, file.replace(/\/$/, ''), rootPath)
|
|
385
|
-
})
|
|
368
|
+
setUpHeadAndGet(fastify, routeOpts, pathname.replace(/\/$/, ''), file.replace(/\/$/, ''), rootPath)
|
|
386
369
|
}
|
|
387
370
|
}
|
|
388
371
|
}
|
|
389
372
|
}
|
|
390
|
-
}
|
|
391
373
|
|
|
374
|
+
function setUpHeadAndGet (fastify, routeOpts, route, file, rootPath) {
|
|
375
|
+
const toSetUp = {
|
|
376
|
+
...routeOpts,
|
|
377
|
+
method: ['HEAD', 'GET'],
|
|
378
|
+
url: route,
|
|
379
|
+
handler: serveFileHandler
|
|
380
|
+
}
|
|
381
|
+
toSetUp.config = toSetUp.config || {}
|
|
382
|
+
toSetUp.config.file = file
|
|
383
|
+
toSetUp.config.rootPath = rootPath
|
|
384
|
+
fastify.route(toSetUp)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function serveFileHandler (req, reply) {
|
|
388
|
+
const file = req.routeConfig.file
|
|
389
|
+
const rootPath = req.routeConfig.rootPath
|
|
390
|
+
pumpSendToReply(req, reply, file, rootPath)
|
|
391
|
+
}
|
|
392
|
+
}
|
|
392
393
|
function checkRootPathForErrors (fastify, rootPath) {
|
|
393
394
|
if (rootPath === undefined) {
|
|
394
395
|
throw new Error('"root" option is required')
|