Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: fastify/fastify-static
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: b77ca2cb637f509f475c96d0a100b816e0234a92
Choose a base ref
...
head repository: fastify/fastify-static
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f1a248b152f2193c556698a749159a1173124462
Choose a head ref
  • 8 commits
  • 5 files changed
  • 6 contributors

Commits on Oct 29, 2023

  1. Bumped v6.12.0

    Eomm committed Oct 29, 2023
    Copy the full SHA
    37056ce View commit details

Commits on Dec 18, 2023

  1. build(deps-dev): bump tsd from 0.29.0 to 0.30.0 (#424)

    Bumps [tsd](https://github.com/tsdjs/tsd) from 0.29.0 to 0.30.0.
    - [Release notes](https://github.com/tsdjs/tsd/releases)
    - [Commits](tsdjs/tsd@v0.29.0...v0.30.0)
    
    ---
    updated-dependencies:
    - dependency-name: tsd
      dependency-type: direct:development
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Dec 18, 2023
    Copy the full SHA
    e6f6124 View commit details

Commits on Jan 13, 2024

  1. Copy the full SHA
    1d788e4 View commit details

Commits on Jan 14, 2024

  1. docs(readme): replace fastify.io links with fastify.dev (#426)

    * docs(readme): replace `fastify.io` links with `fastify.dev`
    
    * docs(readme): remove `www.` from `fastify.dev` urls
    Fdawgs authored Jan 14, 2024
    Copy the full SHA
    5de6d94 View commit details

Commits on Jan 25, 2024

  1. chore(package): fix repository url (#429)

    Signed-off-by: Frazer Smith <frazer.dev@outlook.com>
    Fdawgs authored Jan 25, 2024
    Copy the full SHA
    a34d6a2 View commit details

Commits on Jan 26, 2024

  1. Copy the full SHA
    bcf5664 View commit details

Commits on Feb 1, 2024

  1. Upgrade glob to v10 (#406)

    * update glob
    
    * use posix path
    
    * don't use replaceAll
    
    * use posix true
    
    * use posix.join
    
    * remove redundant replace
    
    * add back check
    
    * fix file paths on windows
    
    * normalize path after it's joined
    
    * remove console.log
    
    * bump
    
    * move glob pattern down
    
    * lessen split-joins
    
    * Revert "lessen split-joins"
    
    This reverts commit 32f875d.
    
    * move order
    
    * remove redundant split-join
    
    * upgrade to glob v9
    
    * Revert "upgrade to glob v9"
    
    This reverts commit aad0548.
    
    * use one replace
    gurgunday authored Feb 1, 2024
    Copy the full SHA
    9e56e07 View commit details
  2. Bumped v7.0.0

    Signed-off-by: Matteo Collina <hello@matteocollina.com>
    mcollina committed Feb 1, 2024
    Copy the full SHA
    f1a248b View commit details
Showing with 35 additions and 24 deletions.
  1. +3 −3 README.md
  2. +8 −12 index.js
  3. +4 −4 package.json
  4. +9 −2 types/index.d.ts
  5. +11 −3 types/index.test-d.ts
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -124,7 +124,7 @@ A URL path prefix used to create a virtual mount path for the static directory.
Default: `{}`

Constraints that will be added to registered routes. See Fastify's documentation for
[route constraints](https://www.fastify.io/docs/latest/Reference/Routes/#constraints).
[route constraints](https://fastify.dev/docs/latest/Reference/Routes/#constraints).

#### `prefixAvoidTrailingSlash`

@@ -450,7 +450,7 @@ decorators.

If a request matches the URL `prefix` but a file cannot be found for the
request, Fastify's 404 handler will be called. You can set a custom 404
handler with [`fastify.setNotFoundHandler()`](https://www.fastify.io/docs/latest/Reference/Server/#setnotfoundhandler).
handler with [`fastify.setNotFoundHandler()`](https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler).

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:

@@ -475,7 +475,7 @@ This code will send the `index.html` for the paths `docs`, `docs/`, and `docs/in

If an error occurs while trying to send a file, the error will be passed
to Fastify's error handler. You can set a custom error handler with
[`fastify.setErrorHandler()`](https://www.fastify.io/docs/latest/Reference/Server/#seterrorhandler).
[`fastify.setErrorHandler()`](https://fastify.dev/docs/latest/Reference/Server/#seterrorhandler).

### Payload `stream.filename`

20 changes: 8 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -4,19 +4,14 @@ const { PassThrough } = require('node:stream')
const path = require('node:path')
const { fileURLToPath } = require('node:url')
const { statSync } = require('node:fs')
const { promisify } = require('node:util')
const glob = require('glob')
const globPromise = promisify(glob)
const { glob } = require('glob')
const fp = require('fastify-plugin')
const send = require('@fastify/send')
const encodingNegotiator = require('@fastify/accept-negotiator')
const contentDisposition = require('content-disposition')

const dirList = require('./lib/dirList')

const winSeparatorRegex = new RegExp(`\\${path.win32.sep}`, 'gu')
const backslashRegex = /\\/gu
const startForwardSlashRegex = /^\//u
const endForwardSlashRegex = /\/$/u
const doubleForwardSlashRegex = /\/\//gu
const asteriskRegex = /\*/gu
@@ -129,19 +124,20 @@ async function fastifyStatic (fastify, opts) {
})
}
} else {
const globPattern = '**/**'
const indexes = opts.index === undefined ? ['index.html'] : [].concat(opts.index)
const indexDirs = new Map()
const routes = new Set()
const globPattern = '**/**'

const roots = Array.isArray(sendOptions.root) ? sendOptions.root : [sendOptions.root]
for (let i = 0; i < roots.length; ++i) {
const rootPath = roots[i]
const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), { nodir: true, dot: opts.serveDotFiles })
const indexes = opts.index === undefined ? ['index.html'] : [].concat(opts.index)
const posixRootPath = rootPath.split(path.win32.sep).join(path.posix.sep)
const files = await glob(`${posixRootPath}/${globPattern}`, { follow: true, nodir: true, dot: opts.serveDotFiles })

for (let i = 0; i < files.length; ++i) {
const file = files[i].replace(rootPath.replace(backslashRegex, '/'), '')
.replace(startForwardSlashRegex, '')
const file = files[i].split(path.win32.sep).join(path.posix.sep)
.replace(`${posixRootPath}/`, '')
const route = (prefix + file).replace(doubleForwardSlashRegex, '/')

if (routes.has(route)) {
@@ -150,7 +146,7 @@ async function fastifyStatic (fastify, opts) {

routes.add(route)

setUpHeadAndGet(routeOpts, route, '/' + file, rootPath)
setUpHeadAndGet(routeOpts, route, `/${file}`, rootPath)

const key = path.posix.basename(route)
if (indexes.includes(key) && !indexDirs.has(key)) {
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fastify/static",
"version": "6.11.2",
"version": "7.0.0",
"description": "Plugin for serving static files as fast as possible.",
"main": "index.js",
"type": "commonjs",
@@ -18,7 +18,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/fastify/fastify-static.git"
"url": "git+https://github.com/fastify/fastify-static.git"
},
"keywords": [
"fastify",
@@ -35,7 +35,7 @@
"@fastify/send": "^2.0.0",
"content-disposition": "^0.5.3",
"fastify-plugin": "^4.0.0",
"glob": "^8.0.1",
"glob": "^10.3.4",
"p-limit": "^3.1.0"
},
"devDependencies": {
@@ -55,7 +55,7 @@
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tap": "^16.0.0",
"tsd": "^0.29.0",
"tsd": "^0.30.0",
"typescript": "^5.1.6"
},
"tsd": {
11 changes: 9 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
// Leo <https://github.com/leomelzer>
/// <reference types="node" />

import { FastifyPluginAsync, FastifyRequest, RouteOptions } from 'fastify'
import { FastifyPluginAsync, FastifyReply, FastifyRequest, RouteOptions } from 'fastify'
import { Stats } from 'fs'

declare module 'fastify' {
@@ -19,6 +19,13 @@ declare module 'fastify' {
type FastifyStaticPlugin = FastifyPluginAsync<NonNullable<fastifyStatic.FastifyStaticOptions>>;

declare namespace fastifyStatic {
export interface SetHeadersResponse {
getHeader: FastifyReply['getHeader'];
setHeader: FastifyReply['header'];
readonly filename: string;
statusCode: number;
}

export interface ExtendedInformation {
fileCount: number;
totalFileCount: number;
@@ -83,7 +90,7 @@ declare namespace fastifyStatic {
serve?: boolean;
decorateReply?: boolean;
schemaHide?: boolean;
setHeaders?: (...args: any[]) => void;
setHeaders?: (res: SetHeadersResponse, path: string, stat: Stats) => void;
redirect?: boolean;
wildcard?: boolean;
list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
14 changes: 11 additions & 3 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest } from 'fastify'
import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest, FastifyReply } from 'fastify'
import { Server } from 'http'
import { Stats } from 'fs'
import { expectAssignable, expectError, expectType } from 'tsd'
import * as fastifyStaticStar from '..'
import fastifyStatic, {
@@ -49,8 +50,15 @@ const options: FastifyStaticOptions = {
serve: true,
wildcard: true,
list: false,
setHeaders: (res: any, pathName: any) => {
res.setHeader('test', pathName)
setHeaders: (res, path, stat) => {
expectType<string>(res.filename)
expectType<number>(res.statusCode)
expectType<ReturnType<FastifyReply['getHeader']>>(res.getHeader('X-Test'))
res.setHeader('X-Test', 'string')

expectType<string>(path)

expectType<Stats>(stat)
},
preCompressed: false,
allowedPath: (pathName: string, root: string, request: FastifyRequest) => {