@fastify/static 6.6.0 → 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 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
@@ -4,7 +4,7 @@ const path = require('path')
4
4
  const statSync = require('fs').statSync
5
5
  const { PassThrough } = require('readable-stream')
6
6
  const glob = require('glob')
7
- const send = require('send')
7
+ const send = require('@fastify/send')
8
8
  const contentDisposition = require('content-disposition')
9
9
  const fp = require('fastify-plugin')
10
10
  const util = require('util')
@@ -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.get(route, routeOpts, function (req, reply) {
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.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
- })
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')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "6.6.0",
3
+ "version": "6.7.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
- "send": "^0.18.0"
38
+ "@fastify/send": "^1.0.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@fastify/compress": "^6.0.0",
@@ -54,7 +54,7 @@
54
54
  "snazzy": "^9.0.0",
55
55
  "standard": "^17.0.0",
56
56
  "tap": "^16.0.0",
57
- "tsd": "^0.24.1"
57
+ "tsd": "^0.25.0"
58
58
  },
59
59
  "tsd": {
60
60
  "directory": "test/types"
@@ -1336,7 +1336,7 @@ t.test('send options', (t) => {
1336
1336
  }
1337
1337
  const fastify = Fastify({ logger: false })
1338
1338
  const fastifyStatic = require('proxyquire')('../', {
1339
- send: function sendStub (req, pathName, options) {
1339
+ '@fastify/send': function sendStub (req, pathName, options) {
1340
1340
  t.equal(pathName, '/index.html')
1341
1341
  t.equal(options.root, path.join(__dirname, '/static'))
1342
1342
  t.equal(options.acceptRanges, 'acceptRanges')