Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-static

Package Overview
Dependencies
Maintainers
13
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-static - npm Package Compare versions

Comparing version 3.4.0 to 4.0.0

25

index.d.ts

@@ -5,3 +5,3 @@ // Definitions by: Jannik <https://github.com/jannikkeye>

import { FastifyPlugin, FastifyReply, RawServerBase } from 'fastify'
import { FastifyPluginCallback, FastifyReply } from 'fastify';

@@ -11,2 +11,5 @@ declare module "fastify" {

sendFile(filename: string, rootPath?: string): FastifyReply;
download(filepath: string, options?: SendOptions): FastifyReply;
download(filepath: string, filename?: string): FastifyReply;
download(filepath: string, filename?: string, options?: SendOptions): FastifyReply;
}

@@ -35,3 +38,16 @@ }

export interface FastifyStaticOptions {
// Passed on to `send`
interface SendOptions {
acceptRanges?: boolean;
cacheControl?: boolean;
dotfiles?: 'allow' | 'deny' | 'ignore';
etag?: boolean;
extensions?: string[];
immutable?: boolean;
index?: string[];
lastModified?: boolean;
maxAge?: string | number;
}
export interface FastifyStaticOptions extends SendOptions {
root: string | string[];

@@ -45,4 +61,5 @@ prefix?: string;

redirect?: boolean;
wildcard?: boolean | string;
wildcard?: boolean;
list?: boolean | ListOptions;
allowedPath?: (pathName: string, root?: string) => boolean;

@@ -61,4 +78,4 @@ // Passed on to `send`

declare const fastifyStatic: FastifyPlugin<FastifyStaticOptions>
declare const fastifyStatic: FastifyPluginCallback<FastifyStaticOptions>
export default fastifyStatic;

@@ -9,2 +9,3 @@ 'use strict'

const send = require('send')
const contentDisposition = require('content-disposition')
const fp = require('fastify-plugin')

@@ -43,5 +44,7 @@ const util = require('util')

function pumpSendToReply (request, reply, pathname, rootPath, rootPathOffset = 0) {
const options = Object.assign({}, sendOptions)
const allowedPath = opts.allowedPath
function pumpSendToReply (request, reply, pathname, rootPath, rootPathOffset = 0, pumpOptions = {}) {
const options = Object.assign({}, sendOptions, pumpOptions)
if (rootPath) {

@@ -55,2 +58,6 @@ if (Array.isArray(rootPath)) {

if (allowedPath && !allowedPath(pathname, options.root)) {
return reply.callNotFound()
}
const stream = send(request.raw, pathname, options)

@@ -164,5 +171,18 @@ let resolvedFilename

})
fastify.decorateReply('download', function (filePath, fileName, options = {}) {
const { root, ...opts } = typeof fileName === 'object' ? fileName : options
fileName = typeof fileName === 'string' ? fileName : filePath
// Set content disposition header
this.header('content-disposition', contentDisposition(fileName))
pumpSendToReply(this.request, this, filePath, root, 0, opts)
return this
})
}
if (opts.serve !== false) {
if (opts.wildcard && typeof opts.wildcard !== 'boolean') throw new Error('"wildcard" option must be a boolean')
if (opts.wildcard === undefined || opts.wildcard === true) {

@@ -180,3 +200,3 @@ fastify.get(prefix + '*', routeOpts, function (req, reply) {

} else {
const globPattern = typeof opts.wildcard === 'string' ? opts.wildcard : '**/*'
const globPattern = '**/*'

@@ -183,0 +203,0 @@ async function addGlobRoutes (rootPath) {

3

package.json
{
"name": "fastify-static",
"version": "3.4.0",
"version": "4.0.0",
"description": "Plugin for serving static files as fast as possible.",

@@ -32,2 +32,3 @@ "main": "index.js",

"dependencies": {
"content-disposition": "^0.5.3",
"fastify-plugin": "^3.0.0",

@@ -34,0 +35,0 @@ "glob": "^7.1.4",

@@ -54,2 +54,27 @@ # fastify-static

### Sending a file with `content-disposition` header
```js
const fastify = require('fastify')()
const path = require('path')
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/', // optional: default '/'
})
fastify.get('/another/path', function (req, reply) {
return reply.download('myHtml.html', 'custom-filename.html') // sending path.join(__dirname, 'public', 'myHtml.html') directly with custom filename
})
fastify.get('/path/without/cache/control', function (req, reply) {
return reply.sendFile('myHtml.html', { cacheControl: false }) // serving a file disabling cache-control headers
})
fastify.get('/path/without/cache/control', function (req, reply) {
return reply.sendFile('myHtml.html', 'custom-filename.html', { cacheControl: false })
})
```
### Options

@@ -111,2 +136,4 @@

You're able to alter this options when calling `reply.download('filename.html', options)` or `reply.download('filename.html', 'otherfilename.html', options)` on each response to a request.
#### `redirect`

@@ -131,4 +158,3 @@

files in the served folder (`${root}/**/*`), and just creates the routes needed for
those.
If set to a glob `string` pattern, `fastify-static` will use the provided string when globing the filesystem (`${root}/${wildcard}`).
those and it will not serve the newly added file on the filesystem.

@@ -141,2 +167,10 @@ The default options of https://www.npmjs.com/package/glob are applied

#### `allowedPath`
Default: `(pathname, root) => true`
This function allows filtering the served files.
If the function returns `true`, the file will be served.
If the function returns `false`, Fastify's 404 handler will be called.
#### `list`

@@ -266,3 +300,3 @@

disable this, pass the option `{ decorateReply: false }`. If fastify-static is
registers to multiple prefixes in the same route only one can initialize reply
registered to multiple prefixes in the same route only one can initialize reply
decorators.

@@ -269,0 +303,0 @@

import fastify from 'fastify'
import { expectError } from 'tsd'
import fastifyStatic, { FastifyStaticOptions } from '../..'

@@ -28,2 +29,6 @@

expectError<FastifyStaticOptions>({
wlidcard: '**/*'
})
appWithImplicitHttp

@@ -45,2 +50,14 @@ .register(fastifyStatic, options)

})
appWithHttp2.get('/download', (request, reply) => {
reply.download('some-file-name')
})
appWithHttp2.get('/download/1', (request, reply) => {
reply.download('some-file-name', { maxAge: '2 days' })
})
appWithHttp2.get('/download/2', (request, reply) => {
reply.download('some-file-name', 'some-filename' ,{ cacheControl: false, acceptRanges: true })
})
})

@@ -57,3 +74,15 @@

})
multiRootAppWithImplicitHttp.get('/download', (request, reply) => {
reply.download('some-file-name')
})
multiRootAppWithImplicitHttp.get('/download/1', (request, reply) => {
reply.download('some-file-name', { maxAge: '2 days' })
})
multiRootAppWithImplicitHttp.get('/download/2', (request, reply) => {
reply.download('some-file-name', 'some-filename', { cacheControl: false, acceptRanges: true })
})
})

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc