@fastify/static 6.4.1 → 6.5.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/.eslintrc ADDED
@@ -0,0 +1 @@
1
+ { "extends": "standard" }
package/README.md CHANGED
@@ -11,7 +11,7 @@ Please refer to [this branch](https://github.com/fastify/fastify-static/tree/1.x
11
11
 
12
12
  ## Install
13
13
 
14
- `npm install --save @fastify/static`
14
+ `npm i @fastify/static`
15
15
 
16
16
  ## Usage
17
17
 
@@ -169,9 +169,9 @@ with `ignoreTrailingSlash` set to `true`.
169
169
 
170
170
  #### `allowedPath`
171
171
 
172
- Default: `(pathname, root) => true`
172
+ Default: `(pathName, root, request) => true`
173
173
 
174
- This function allows filtering the served files.
174
+ This function allows filtering the served files. Also, with the help of the request object a more complex path authentication is possible.
175
175
  If the function returns `true`, the file will be served.
176
176
  If the function returns `false`, Fastify's 404 handler will be called.
177
177
 
package/index.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Leo <https://github.com/leomelzer>
3
3
  /// <reference types="node" />
4
4
 
5
- import { FastifyPluginCallback, FastifyReply } from 'fastify';
5
+ import { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify';
6
6
  import { Stats } from 'fs';
7
7
 
8
8
  declare module "fastify" {
@@ -86,7 +86,7 @@ declare namespace fastifyStatic {
86
86
  redirect?: boolean;
87
87
  wildcard?: boolean;
88
88
  list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
89
- allowedPath?: (pathName: string, root?: string) => boolean;
89
+ allowedPath?: (pathName: string, root: string, request: FastifyRequest) => boolean;
90
90
  /**
91
91
  * @description
92
92
  * Opt-in to looking for pre-compressed files
package/index.js CHANGED
@@ -72,7 +72,7 @@ async function fastifyStatic (fastify, opts) {
72
72
  }
73
73
  }
74
74
 
75
- if (allowedPath && !allowedPath(pathname, options.root)) {
75
+ if (allowedPath && !allowedPath(pathname, options.root, request)) {
76
76
  return reply.callNotFound()
77
77
  }
78
78
 
package/lib/sdf/wqer ADDED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "6.4.1",
3
+ "version": "6.5.0",
4
4
  "description": "Plugin for serving static files as fast as possible.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -973,7 +973,7 @@ t.test('sendFile disabled', (t) => {
973
973
  })
974
974
  })
975
975
 
976
- t.test('allowedPath option', (t) => {
976
+ t.test('allowedPath option - pathname', (t) => {
977
977
  t.plan(3)
978
978
 
979
979
  const pluginOptions = {
@@ -1014,6 +1014,47 @@ t.test('allowedPath option', (t) => {
1014
1014
  })
1015
1015
  })
1016
1016
 
1017
+ t.test('allowedPath option - request', (t) => {
1018
+ t.plan(3)
1019
+
1020
+ const pluginOptions = {
1021
+ root: path.join(__dirname, '/static'),
1022
+ allowedPath: (pathName, root, request) => request.query.key === 'temporaryKey'
1023
+ }
1024
+ const fastify = Fastify()
1025
+ fastify.register(fastifyStatic, pluginOptions)
1026
+ fastify.listen({ port: 0 }, (err) => {
1027
+ t.error(err)
1028
+
1029
+ fastify.server.unref()
1030
+
1031
+ t.test('/foobar.html not found', (t) => {
1032
+ t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
1033
+ simple.concat({
1034
+ method: 'GET',
1035
+ url: 'http://localhost:' + fastify.server.address().port + '/foobar.html',
1036
+ followRedirect: false
1037
+ }, (err, response, body) => {
1038
+ t.error(err)
1039
+ t.equal(response.statusCode, 404)
1040
+ genericErrorResponseChecks(t, response)
1041
+ })
1042
+ })
1043
+
1044
+ t.test('/index.css found', (t) => {
1045
+ t.plan(2)
1046
+ simple.concat({
1047
+ method: 'GET',
1048
+ url: 'http://localhost:' + fastify.server.address().port + '/index.css?key=temporaryKey',
1049
+ followRedirect: false
1050
+ }, (err, response, body) => {
1051
+ t.error(err)
1052
+ t.equal(response.statusCode, 200)
1053
+ })
1054
+ })
1055
+ })
1056
+ })
1057
+
1017
1058
  t.test('download', (t) => {
1018
1059
  t.plan(7)
1019
1060
 
@@ -1,4 +1,4 @@
1
- import fastify, { FastifyInstance, FastifyPluginCallback } from 'fastify'
1
+ import fastify, { FastifyInstance, FastifyPluginCallback, FastifyRequest } from 'fastify'
2
2
  import { Server } from 'http';
3
3
  import { expectAssignable, expectError, expectType } from 'tsd'
4
4
  import * as fastifyStaticStar from '../..';
@@ -52,7 +52,10 @@ const options: FastifyStaticOptions = {
52
52
  setHeaders: (res: any, pathName: any) => {
53
53
  res.setHeader('test', pathName)
54
54
  },
55
- preCompressed: false
55
+ preCompressed: false,
56
+ allowedPath: (pathName: string, root: string, request: FastifyRequest) => {
57
+ return true;
58
+ }
56
59
  }
57
60
 
58
61
  expectError<FastifyStaticOptions>({