@fastify/static
Advanced tools
Comparing version
13
index.js
@@ -361,3 +361,3 @@ 'use strict' | ||
setUpHeadAndGet(fastify, routeOpts, route, '/' + file, rootPath) | ||
setUpHeadAndGet(routeOpts, route, '/' + file, rootPath) | ||
@@ -374,6 +374,6 @@ const key = path.posix.basename(route) | ||
const file = '/' + pathname.replace(prefix, '') | ||
setUpHeadAndGet(fastify, routeOpts, pathname, file, rootPath) | ||
setUpHeadAndGet(routeOpts, pathname, file, rootPath) | ||
if (opts.redirect === true) { | ||
setUpHeadAndGet(fastify, routeOpts, pathname.replace(/\/$/, ''), file.replace(/\/$/, ''), rootPath) | ||
setUpHeadAndGet(routeOpts, pathname.replace(/\/$/, ''), file.replace(/\/$/, ''), rootPath) | ||
} | ||
@@ -384,3 +384,3 @@ } | ||
function setUpHeadAndGet (fastify, routeOpts, route, file, rootPath) { | ||
function setUpHeadAndGet (routeOpts, route, file, rootPath) { | ||
const toSetUp = { | ||
@@ -404,2 +404,3 @@ ...routeOpts, | ||
} | ||
function normalizeRoot (root) { | ||
@@ -482,4 +483,2 @@ if (root === undefined) { | ||
const supportedEncodings = ['br', 'gzip', 'deflate'] | ||
function getContentType (path) { | ||
@@ -512,2 +511,4 @@ const type = send.mime.getType(path) || send.mime.default_type | ||
const supportedEncodings = ['br', 'gzip', 'deflate'] | ||
// Adapted from https://github.com/fastify/fastify-compress/blob/665e132fa63d3bf05ad37df3c20346660b71a857/index.js#L451 | ||
@@ -514,0 +515,0 @@ function getEncodingHeader (headers, checked) { |
{ | ||
"name": "@fastify/static", | ||
"version": "6.10.2", | ||
"version": "6.11.0", | ||
"description": "Plugin for serving static files as fast as possible.", | ||
@@ -8,10 +8,11 @@ "main": "index.js", | ||
"scripts": { | ||
"lint": "standard | snazzy", | ||
"lint:fix": "standard --fix", | ||
"unit": "tap test/*.test.js", | ||
"typescript": "tsd", | ||
"test": "npm run lint && npm run unit && npm run typescript", | ||
"example": "node example/server.js", | ||
"coverage": "tap --cov --coverage-report=html test", | ||
"coveralls": "tap test/*test.js test/*/*.test.js --cov" | ||
"coverage": "npm run test:unit -- --coverage-report=html", | ||
"lint": "npm run lint:javascript && npm run lint:typescript", | ||
"lint:javascript": "standard | snazzy", | ||
"lint:fix": "standard --fix && npm run lint:typescript -- --fix", | ||
"lint:typescript": "eslint -c .eslintrc.json types/**/*.d.ts types/**/*.test-d.ts", | ||
"test": "npm run test:unit && npm run test:typescript", | ||
"test:typescript": "tsd", | ||
"test:unit": "tap", | ||
"example": "node example/server.js" | ||
}, | ||
@@ -33,9 +34,9 @@ "repository": { | ||
"dependencies": { | ||
"@fastify/accept-negotiator": "^1.0.0", | ||
"@fastify/send": "^2.0.0", | ||
"content-disposition": "^0.5.3", | ||
"@fastify/accept-negotiator": "^1.0.0", | ||
"fastify-plugin": "^4.0.0", | ||
"glob": "^8.0.1", | ||
"p-limit": "^3.1.0", | ||
"readable-stream": "^4.0.0", | ||
"@fastify/send": "^2.0.0" | ||
"readable-stream": "^4.0.0" | ||
}, | ||
@@ -46,4 +47,4 @@ "devDependencies": { | ||
"@types/node": "^20.1.0", | ||
"@typescript-eslint/eslint-plugin": "^2.29.0", | ||
"@typescript-eslint/parser": "^2.29.0", | ||
"@typescript-eslint/eslint-plugin": "^6.3.0", | ||
"@typescript-eslint/parser": "^6.3.0", | ||
"concat-stream": "^2.0.0", | ||
@@ -60,3 +61,4 @@ "coveralls": "^3.0.4", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.28.0" | ||
"tsd": "^0.28.0", | ||
"typescript": "^5.1.6" | ||
}, | ||
@@ -63,0 +65,0 @@ "tsd": { |
@@ -455,2 +455,21 @@ # @fastify/static | ||
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: | ||
```js | ||
const app = require('fastify')(); | ||
app.register((childContext, _, done) => { | ||
childContext.register(require('@fastify/static'), { | ||
root: path.join(__dirname, 'docs'), // docs is a folder that contains `index.html` and `404.html` | ||
wildcard: false | ||
}); | ||
childContext.setNotFoundHandler((_, reply) => { | ||
return reply.code(404).type('text/html').sendFile('404.html'); | ||
}); | ||
done(); | ||
}, { prefix: 'docs' }); | ||
``` | ||
This code will send the `index.html` for the paths `docs`, `docs/`, and `docs/index.html`. For all other `docs/<undefined-routes>` it will reply with `404.html`. | ||
### Handling Errors | ||
@@ -457,0 +476,0 @@ |
@@ -5,6 +5,6 @@ // Definitions by: Jannik <https://github.com/jannikkeye> | ||
import {FastifyPluginAsync, FastifyRequest, RouteOptions} from 'fastify'; | ||
import { Stats } from 'fs'; | ||
import { FastifyPluginAsync, FastifyRequest, RouteOptions } from 'fastify' | ||
import { Stats } from 'fs' | ||
declare module "fastify" { | ||
declare module 'fastify' { | ||
interface FastifyReply { | ||
@@ -81,3 +81,3 @@ sendFile(filename: string, rootPath?: string): FastifyReply; | ||
export interface FastifyStaticOptions extends SendOptions { | ||
root: string | string[]; | ||
root: string | string[] | URL | URL[]; | ||
prefix?: string; | ||
@@ -112,5 +112,5 @@ prefixAvoidTrailingSlash?: boolean; | ||
export const fastifyStatic: FastifyStaticPlugin; | ||
export const fastifyStatic: FastifyStaticPlugin | ||
export { fastifyStatic as default }; | ||
export { fastifyStatic as default } | ||
} | ||
@@ -117,0 +117,0 @@ |
import fastify, { FastifyInstance, FastifyPluginAsync, FastifyRequest } from 'fastify' | ||
import { Server } from 'http'; | ||
import { Server } from 'http' | ||
import { expectAssignable, expectError, expectType } from 'tsd' | ||
import * as fastifyStaticStar from '..'; | ||
import * as fastifyStaticStar from '..' | ||
import fastifyStatic, { | ||
FastifyStaticOptions, | ||
fastifyStatic as fastifyStaticNamed, | ||
fastifyStatic as fastifyStaticNamed | ||
} from '..' | ||
import fastifyStaticCjsImport = require('..'); | ||
const fastifyStaticCjs = require('..'); | ||
const fastifyStaticCjs = require('..') | ||
const app: FastifyInstance = fastify(); | ||
const app: FastifyInstance = fastify() | ||
app.register(fastifyStatic); | ||
app.register(fastifyStaticNamed); | ||
app.register(fastifyStaticCjs); | ||
app.register(fastifyStaticCjsImport.default); | ||
app.register(fastifyStaticCjsImport.fastifyStatic); | ||
app.register(fastifyStaticStar.default); | ||
app.register(fastifyStaticStar.fastifyStatic); | ||
app.register(fastifyStatic) | ||
app.register(fastifyStaticNamed) | ||
app.register(fastifyStaticCjs) | ||
app.register(fastifyStaticCjsImport.default) | ||
app.register(fastifyStaticCjsImport.fastifyStatic) | ||
app.register(fastifyStaticStar.default) | ||
app.register(fastifyStaticStar.fastifyStatic) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStatic); | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticNamed); | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticCjsImport.default); | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticCjsImport.fastifyStatic); | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticStar.default); | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStatic) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticNamed) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticCjsImport.default) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticCjsImport.fastifyStatic) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>(fastifyStaticStar.default) | ||
expectType<FastifyPluginAsync<FastifyStaticOptions, Server>>( | ||
fastifyStaticStar.fastifyStatic | ||
); | ||
expectType<any>(fastifyStaticCjs); | ||
fastifyStaticStar.fastifyStatic | ||
) | ||
expectType<any>(fastifyStaticCjs) | ||
@@ -57,3 +57,3 @@ const appWithImplicitHttp = fastify() | ||
allowedPath: (pathName: string, root: string, request: FastifyRequest) => { | ||
return true; | ||
return true | ||
}, | ||
@@ -74,3 +74,3 @@ constraints: { | ||
list: { | ||
format: 'json', | ||
format: 'json' | ||
} | ||
@@ -98,6 +98,18 @@ }) | ||
list: { | ||
format: 'html', | ||
format: 'html' | ||
} | ||
}) | ||
expectAssignable<FastifyStaticOptions>({ | ||
root: [''] | ||
}) | ||
expectAssignable<FastifyStaticOptions>({ | ||
root: new URL('') | ||
}) | ||
expectAssignable<FastifyStaticOptions>({ | ||
root: [new URL('')] | ||
}) | ||
appWithImplicitHttp | ||
@@ -129,3 +141,3 @@ .register(fastifyStatic, options) | ||
appWithHttp2.get('/download/2', (request, reply) => { | ||
reply.download('some-file-name', 'some-filename' ,{ cacheControl: false, acceptRanges: true }) | ||
reply.download('some-file-name', 'some-filename', { cacheControl: false, acceptRanges: true }) | ||
}) | ||
@@ -175,4 +187,14 @@ }) | ||
}) | ||
}) | ||
}) | ||
options.root = new URL('') | ||
const URLRootApp = fastify() | ||
URLRootApp.register(fastifyStatic, options) | ||
.after(() => { | ||
URLRootApp.get('/', (request, reply) => { | ||
reply.send('<h1>fastify-static</h1>') | ||
}) | ||
}) | ||
const defaultIndexApp = fastify() | ||
@@ -179,0 +201,0 @@ options.index = 'index.html' |
Sorry, the diff of this file is too big to display
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
453454
0.9%68
6.25%5314
2.11%494
4%18
5.88%