@fastify/static 6.6.1 → 6.8.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.
Files changed (3) hide show
  1. package/LICENSE +1 -1
  2. package/index.js +32 -28
  3. package/package.json +2 -2
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2018 Fastify
3
+ Copyright (c) 2017-2023 Fastify
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/index.js CHANGED
@@ -27,6 +27,10 @@ async function fastifyStatic (fastify, opts) {
27
27
  throw invalidDirListOpts
28
28
  }
29
29
 
30
+ if (opts.dotfiles === undefined) {
31
+ opts.dotfiles = 'allow'
32
+ }
33
+
30
34
  const sendOptions = {
31
35
  root: opts.root,
32
36
  acceptRanges: opts.acceptRanges,
@@ -37,8 +41,7 @@ async function fastifyStatic (fastify, opts) {
37
41
  immutable: opts.immutable,
38
42
  index: opts.index,
39
43
  lastModified: opts.lastModified,
40
- maxAge: opts.maxAge,
41
- serveDotFiles: opts.serveDotFiles ?? false
44
+ maxAge: opts.maxAge
42
45
  }
43
46
 
44
47
  const allowedPath = opts.allowedPath
@@ -337,7 +340,7 @@ async function fastifyStatic (fastify, opts) {
337
340
  const winSeparatorRegex = new RegExp(`\\${path.win32.sep}`, 'g')
338
341
 
339
342
  for (const rootPath of Array.isArray(sendOptions.root) ? sendOptions.root : [sendOptions.root]) {
340
- const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), { nodir: true, dot: sendOptions.serveDotFiles })
343
+ const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), { nodir: true, dot: opts.serveDotFiles })
341
344
  const indexes = typeof opts.index === 'undefined' ? ['index.html'] : [].concat(opts.index)
342
345
 
343
346
  for (let file of files) {
@@ -349,13 +352,8 @@ async function fastifyStatic (fastify, opts) {
349
352
  continue
350
353
  }
351
354
  routes.add(route)
352
- fastify.head(route, routeOpts, function (req, reply) {
353
- pumpSendToReply(req, reply, '/' + file, rootPath)
354
- })
355
355
 
356
- fastify.get(route, routeOpts, function (req, reply) {
357
- pumpSendToReply(req, reply, '/' + file, rootPath)
358
- })
356
+ setUpHeadAndGet(fastify, routeOpts, route, '/' + file, rootPath)
359
357
 
360
358
  const key = path.posix.basename(route)
361
359
  if (indexes.includes(key) && !indexDirs.has(key)) {
@@ -367,28 +365,34 @@ async function fastifyStatic (fastify, opts) {
367
365
  for (const [dirname, rootPath] of indexDirs.entries()) {
368
366
  const pathname = dirname + (dirname.endsWith('/') ? '' : '/')
369
367
  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
- })
368
+ setUpHeadAndGet(fastify, routeOpts, pathname, file, rootPath)
378
369
 
379
370
  if (opts.redirect === true) {
380
- fastify.head(pathname.replace(/\/$/, ''), routeOpts, function (req, reply) {
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
- })
371
+ setUpHeadAndGet(fastify, routeOpts, pathname.replace(/\/$/, ''), file.replace(/\/$/, ''), rootPath)
386
372
  }
387
373
  }
388
374
  }
389
375
  }
390
- }
391
376
 
377
+ function setUpHeadAndGet (fastify, routeOpts, route, file, rootPath) {
378
+ const toSetUp = {
379
+ ...routeOpts,
380
+ method: ['HEAD', 'GET'],
381
+ url: route,
382
+ handler: serveFileHandler
383
+ }
384
+ toSetUp.config = toSetUp.config || {}
385
+ toSetUp.config.file = file
386
+ toSetUp.config.rootPath = rootPath
387
+ fastify.route(toSetUp)
388
+ }
389
+
390
+ function serveFileHandler (req, reply) {
391
+ const file = req.routeConfig.file
392
+ const rootPath = req.routeConfig.rootPath
393
+ pumpSendToReply(req, reply, file, rootPath)
394
+ }
395
+ }
392
396
  function checkRootPathForErrors (fastify, rootPath) {
393
397
  if (rootPath === undefined) {
394
398
  throw new Error('"root" option is required')
@@ -446,12 +450,12 @@ function checkPath (fastify, rootPath) {
446
450
  const supportedEncodings = ['br', 'gzip', 'deflate']
447
451
 
448
452
  function getContentType (path) {
449
- const type = send.mime.lookup(path)
450
- const charset = send.mime.charsets.lookup(type)
451
- if (!charset) {
453
+ const type = send.mime.getType(path)
454
+
455
+ if (!send.isUtf8MimeType(type)) {
452
456
  return type
453
457
  }
454
- return `${type}; charset=${charset}`
458
+ return `${type}; charset=UTF-8`
455
459
  }
456
460
 
457
461
  function findIndexFile (pathname, root, indexFiles = ['index.html']) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "6.6.1",
3
+ "version": "6.8.0",
4
4
  "description": "Plugin for serving static files as fast as possible.",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -35,7 +35,7 @@
35
35
  "glob": "^8.0.1",
36
36
  "p-limit": "^3.1.0",
37
37
  "readable-stream": "^4.0.0",
38
- "@fastify/send": "^1.0.0"
38
+ "@fastify/send": "^2.0.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@fastify/compress": "^6.0.0",