@fastify/static 6.10.0 → 6.10.1

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.
Binary file
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const path = require('path')
4
+ const url = require('url')
4
5
  const statSync = require('fs').statSync
5
6
  const { PassThrough } = require('readable-stream')
6
7
  const glob = require('glob')
@@ -14,6 +15,7 @@ const encodingNegotiator = require('@fastify/accept-negotiator')
14
15
  const dirList = require('./lib/dirList')
15
16
 
16
17
  async function fastifyStatic (fastify, opts) {
18
+ opts.root = normalizeRoot(opts.root)
17
19
  checkRootPathForErrors(fastify, opts.root)
18
20
 
19
21
  const setHeaders = opts.setHeaders
@@ -394,6 +396,29 @@ async function fastifyStatic (fastify, opts) {
394
396
  pumpSendToReply(req, reply, file, rootPath)
395
397
  }
396
398
  }
399
+ function normalizeRoot (root) {
400
+ if (root === undefined) {
401
+ return root
402
+ }
403
+ if (root instanceof URL && root.protocol === 'file:') {
404
+ return url.fileURLToPath(root)
405
+ }
406
+ if (Array.isArray(root)) {
407
+ const result = []
408
+ for (let i = 0, il = root.length; i < il; ++i) {
409
+ if (root[i] instanceof URL && root[i].protocol === 'file:') {
410
+ result.push(url.fileURLToPath(root[i]))
411
+ } else {
412
+ result.push(root[i])
413
+ }
414
+ }
415
+
416
+ return result
417
+ }
418
+
419
+ return root
420
+ }
421
+
397
422
  function checkRootPathForErrors (fastify, rootPath) {
398
423
  if (rootPath === undefined) {
399
424
  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.10.0",
3
+ "version": "6.10.1",
4
4
  "description": "Plugin for serving static files as fast as possible.",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -3756,3 +3756,50 @@ t.test(
3756
3756
  t.end()
3757
3757
  }
3758
3758
  )
3759
+ t.test(
3760
+ 'converts URL to path',
3761
+ async (t) => {
3762
+ t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
3763
+ const pluginOptions = {
3764
+ root: url.pathToFileURL(path.join(__dirname, '/static'))
3765
+ }
3766
+
3767
+ const fastify = Fastify()
3768
+
3769
+ fastify.register(fastifyStatic, pluginOptions)
3770
+ const response = await fastify.inject({
3771
+ method: 'GET',
3772
+ url: 'foobar.html',
3773
+ headers: {
3774
+ 'accept-encoding': '*, *'
3775
+ }
3776
+ })
3777
+ genericResponseChecks(t, response)
3778
+ t.equal(response.statusCode, 200)
3779
+ t.same(response.body, foobarContent)
3780
+ }
3781
+ )
3782
+
3783
+ t.test(
3784
+ 'converts array of URLs to path, contains string path',
3785
+ async (t) => {
3786
+ t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
3787
+ const pluginOptions = {
3788
+ root: [url.pathToFileURL(path.join(__dirname, '/static')), path.join(__dirname, 'static-dotfiles'), url.pathToFileURL(path.join(__dirname, '/static-pre-compressed'))]
3789
+ }
3790
+
3791
+ const fastify = Fastify()
3792
+
3793
+ fastify.register(fastifyStatic, pluginOptions)
3794
+ const response = await fastify.inject({
3795
+ method: 'GET',
3796
+ url: 'foobar.html',
3797
+ headers: {
3798
+ 'accept-encoding': '*, *'
3799
+ }
3800
+ })
3801
+ genericResponseChecks(t, response)
3802
+ t.equal(response.statusCode, 200)
3803
+ t.same(response.body, foobarContent)
3804
+ }
3805
+ )