@fastify/static 6.12.0 → 7.0.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/README.md CHANGED
@@ -124,7 +124,7 @@ A URL path prefix used to create a virtual mount path for the static directory.
124
124
  Default: `{}`
125
125
 
126
126
  Constraints that will be added to registered routes. See Fastify's documentation for
127
- [route constraints](https://www.fastify.io/docs/latest/Reference/Routes/#constraints).
127
+ [route constraints](https://fastify.dev/docs/latest/Reference/Routes/#constraints).
128
128
 
129
129
  #### `prefixAvoidTrailingSlash`
130
130
 
@@ -450,7 +450,7 @@ decorators.
450
450
 
451
451
  If a request matches the URL `prefix` but a file cannot be found for the
452
452
  request, Fastify's 404 handler will be called. You can set a custom 404
453
- handler with [`fastify.setNotFoundHandler()`](https://www.fastify.io/docs/latest/Reference/Server/#setnotfoundhandler).
453
+ handler with [`fastify.setNotFoundHandler()`](https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler).
454
454
 
455
455
  When registering `@fastify/static` within an encapsulated context, the `wildcard` option may need to be set to `false` in order to support index resolution and nested not-found-handler:
456
456
 
@@ -475,7 +475,7 @@ This code will send the `index.html` for the paths `docs`, `docs/`, and `docs/in
475
475
 
476
476
  If an error occurs while trying to send a file, the error will be passed
477
477
  to Fastify's error handler. You can set a custom error handler with
478
- [`fastify.setErrorHandler()`](https://www.fastify.io/docs/latest/Reference/Server/#seterrorhandler).
478
+ [`fastify.setErrorHandler()`](https://fastify.dev/docs/latest/Reference/Server/#seterrorhandler).
479
479
 
480
480
  ### Payload `stream.filename`
481
481
 
package/index.js CHANGED
@@ -4,9 +4,7 @@ const { PassThrough } = require('node:stream')
4
4
  const path = require('node:path')
5
5
  const { fileURLToPath } = require('node:url')
6
6
  const { statSync } = require('node:fs')
7
- const { promisify } = require('node:util')
8
- const glob = require('glob')
9
- const globPromise = promisify(glob)
7
+ const { glob } = require('glob')
10
8
  const fp = require('fastify-plugin')
11
9
  const send = require('@fastify/send')
12
10
  const encodingNegotiator = require('@fastify/accept-negotiator')
@@ -14,9 +12,6 @@ const contentDisposition = require('content-disposition')
14
12
 
15
13
  const dirList = require('./lib/dirList')
16
14
 
17
- const winSeparatorRegex = new RegExp(`\\${path.win32.sep}`, 'gu')
18
- const backslashRegex = /\\/gu
19
- const startForwardSlashRegex = /^\//u
20
15
  const endForwardSlashRegex = /\/$/u
21
16
  const doubleForwardSlashRegex = /\/\//gu
22
17
  const asteriskRegex = /\*/gu
@@ -129,19 +124,20 @@ async function fastifyStatic (fastify, opts) {
129
124
  })
130
125
  }
131
126
  } else {
132
- const globPattern = '**/**'
127
+ const indexes = opts.index === undefined ? ['index.html'] : [].concat(opts.index)
133
128
  const indexDirs = new Map()
134
129
  const routes = new Set()
130
+ const globPattern = '**/**'
135
131
 
136
132
  const roots = Array.isArray(sendOptions.root) ? sendOptions.root : [sendOptions.root]
137
133
  for (let i = 0; i < roots.length; ++i) {
138
134
  const rootPath = roots[i]
139
- const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), { nodir: true, dot: opts.serveDotFiles })
140
- const indexes = opts.index === undefined ? ['index.html'] : [].concat(opts.index)
135
+ const posixRootPath = rootPath.split(path.win32.sep).join(path.posix.sep)
136
+ const files = await glob(`${posixRootPath}/${globPattern}`, { follow: true, nodir: true, dot: opts.serveDotFiles })
141
137
 
142
138
  for (let i = 0; i < files.length; ++i) {
143
- const file = files[i].replace(rootPath.replace(backslashRegex, '/'), '')
144
- .replace(startForwardSlashRegex, '')
139
+ const file = files[i].split(path.win32.sep).join(path.posix.sep)
140
+ .replace(`${posixRootPath}/`, '')
145
141
  const route = (prefix + file).replace(doubleForwardSlashRegex, '/')
146
142
 
147
143
  if (routes.has(route)) {
@@ -150,7 +146,7 @@ async function fastifyStatic (fastify, opts) {
150
146
 
151
147
  routes.add(route)
152
148
 
153
- setUpHeadAndGet(routeOpts, route, '/' + file, rootPath)
149
+ setUpHeadAndGet(routeOpts, route, `/${file}`, rootPath)
154
150
 
155
151
  const key = path.posix.basename(route)
156
152
  if (indexes.includes(key) && !indexDirs.has(key)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "6.12.0",
3
+ "version": "7.0.0",
4
4
  "description": "Plugin for serving static files as fast as possible.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",
21
- "url": "https://github.com/fastify/fastify-static.git"
21
+ "url": "git+https://github.com/fastify/fastify-static.git"
22
22
  },
23
23
  "keywords": [
24
24
  "fastify",
@@ -35,7 +35,7 @@
35
35
  "@fastify/send": "^2.0.0",
36
36
  "content-disposition": "^0.5.3",
37
37
  "fastify-plugin": "^4.0.0",
38
- "glob": "^8.0.1",
38
+ "glob": "^10.3.4",
39
39
  "p-limit": "^3.1.0"
40
40
  },
41
41
  "devDependencies": {
@@ -55,7 +55,7 @@
55
55
  "snazzy": "^9.0.0",
56
56
  "standard": "^17.0.0",
57
57
  "tap": "^16.0.0",
58
- "tsd": "^0.29.0",
58
+ "tsd": "^0.30.0",
59
59
  "typescript": "^5.1.6"
60
60
  },
61
61
  "tsd": {
package/types/index.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Leo <https://github.com/leomelzer>
3
3
  /// <reference types="node" />
4
4
 
5
- import { FastifyPluginAsync, FastifyRequest, RouteOptions } from 'fastify'
5
+ import { FastifyPluginAsync, FastifyReply, FastifyRequest, RouteOptions } from 'fastify'
6
6
  import { Stats } from 'fs'
7
7
 
8
8
  declare module 'fastify' {
@@ -19,6 +19,13 @@ declare module 'fastify' {
19
19
  type FastifyStaticPlugin = FastifyPluginAsync<NonNullable<fastifyStatic.FastifyStaticOptions>>;
20
20
 
21
21
  declare namespace fastifyStatic {
22
+ export interface SetHeadersResponse {
23
+ getHeader: FastifyReply['getHeader'];
24
+ setHeader: FastifyReply['header'];
25
+ readonly filename: string;
26
+ statusCode: number;
27
+ }
28
+
22
29
  export interface ExtendedInformation {
23
30
  fileCount: number;
24
31
  totalFileCount: number;
@@ -83,7 +90,7 @@ declare namespace fastifyStatic {
83
90
  serve?: boolean;
84
91
  decorateReply?: boolean;
85
92
  schemaHide?: boolean;
86
- setHeaders?: (...args: any[]) => void;
93
+ setHeaders?: (res: SetHeadersResponse, path: string, stat: Stats) => void;
87
94
  redirect?: boolean;
88
95
  wildcard?: boolean;
89
96
  list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
@@ -1,5 +1,6 @@
1
- import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest } from 'fastify'
1
+ import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest, FastifyReply } from 'fastify'
2
2
  import { Server } from 'http'
3
+ import { Stats } from 'fs'
3
4
  import { expectAssignable, expectError, expectType } from 'tsd'
4
5
  import * as fastifyStaticStar from '..'
5
6
  import fastifyStatic, {
@@ -49,8 +50,15 @@ const options: FastifyStaticOptions = {
49
50
  serve: true,
50
51
  wildcard: true,
51
52
  list: false,
52
- setHeaders: (res: any, pathName: any) => {
53
- res.setHeader('test', pathName)
53
+ setHeaders: (res, path, stat) => {
54
+ expectType<string>(res.filename)
55
+ expectType<number>(res.statusCode)
56
+ expectType<ReturnType<FastifyReply['getHeader']>>(res.getHeader('X-Test'))
57
+ res.setHeader('X-Test', 'string')
58
+
59
+ expectType<string>(path)
60
+
61
+ expectType<Stats>(stat)
54
62
  },
55
63
  preCompressed: false,
56
64
  allowedPath: (pathName: string, root: string, request: FastifyRequest) => {