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

fastify-static

Package Overview
Dependencies
Maintainers
8
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.0.1 to 3.1.0

example/public/images/sample.jpg

21

index.d.ts

@@ -13,2 +13,22 @@ // Definitions by: Jannik <https://github.com/jannikkeye>

interface ListDir {
href: string;
name: string;
}
interface ListFile {
href: string;
name: string;
}
interface ListRender {
(dirs: ListDir[], files: ListFile[]): string;
}
interface ListOptions {
format: 'json' | 'html';
names: string[];
render: ListRender;
}
export interface FastifyStaticOptions {

@@ -24,2 +44,3 @@ root: string;

wildcard?: boolean | string;
list?: boolean | ListOptions;

@@ -26,0 +47,0 @@ // Passed on to `send`

20

index.js

@@ -8,7 +8,7 @@ 'use strict'

const glob = require('glob')
const send = require('send')
const fp = require('fastify-plugin')
const dirList = require('./lib/dirList')
function fastifyStatic (fastify, opts, next) {

@@ -24,2 +24,7 @@ const error = checkRootPathForErrors(fastify, opts.root)

const invalidDirListOpts = dirList.validateOptions(opts.list)
if (invalidDirListOpts) {
return next(invalidDirListOpts)
}
const sendOptions = {

@@ -88,3 +93,7 @@ root: opts.root,

stream.on('directory', function (res, path) {
stream.on('directory', function (_, path) {
if (opts.list) {
return dirList.send({ reply, dir: path, options: opts.list, route: pathname })
}
if (opts.redirect === true) {

@@ -102,2 +111,6 @@ /* eslint node/no-deprecated-api: "off" */

if (err.code === 'ENOENT') {
// if file exists, send real file, otherwise send dir list if name match
if (opts.list && dirList.handle(pathname, opts.list)) {
return dirList.send({ reply, dir: dirList.path(opts.root, pathname), options: opts.list, route: pathname })
}
return reply.callNotFound()

@@ -121,2 +134,3 @@ }

}
// Set the schema hide property if defined in opts or true by default

@@ -123,0 +137,0 @@ const schema = { schema: { hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true } }

7

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

@@ -43,4 +43,5 @@ "main": "index.js",

"eslint-plugin-typescript": "^0.14.0",
"fastify": "^3.0.0-rc.4",
"fastify": "^3.0.0-rc.5",
"fastify-compress": "^3.0.0",
"handlebars": "^4.7.6",
"pre-commit": "^1.2.2",

@@ -52,3 +53,3 @@ "proxyquire": "^2.1.0",

"tap": "^14.10.7",
"tsd": "^0.11.0",
"tsd": "^0.12.0",
"typescript": "^3.5.2"

@@ -55,0 +56,0 @@ },

@@ -135,2 +135,118 @@ # fastify-static

#### `list`
Default: `undefined`
If set, it provide the directory list calling the directory path.
Default response is json.
**Example:**
```js
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/',
list: true
})
```
Request
```bash
GET .../public
```
Response
```json
{ "dirs": ["dir1", "dir2"], "files": ["file1.png", "file2.txt"] }
```
#### `list.format`
Default: `json`
Options: `html`, `json`
Directory list can be also in `html` format; in that case, `list.render` function is required.
**Example:**
```js
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/',
list: {
format: 'html',
render: (dirs, files) => {
return `
<html><body>
<ul>
${dirs.map(dir => `<li><a href="${dir.href}">${dir.name}</a></li>`).join('\n ')}
</ul>
<ul>
${files.map(file => `<li><a href="${file.href}" target="_blank">${file.name}</a></li>`).join('\n ')}
</ul>
</body></html>
`
},
}
})
```
Request
```bash
GET .../public
```
Response
```html
<html><body>
<ul>
<li><a href="/dir1">dir1</a></li>
<li><a href="/dir1">dir2</a></li>
</ul>
<ul>
<li><a href="/foo.html" target="_blank">foo.html</a></li>
<li><a href="/foobar.html" target="_blank">foobar.html</a></li>
<li><a href="/index.css" target="_blank">index.css</a></li>
<li><a href="/index.html" target="_blank">index.html</a></li>
</ul>
</body></html>
```
#### `list.names`
Default: `['']`
Directory list can respond to different routes, declared in `list.names` options.
Note: if a file with the same name exists, the actual file is sent.
**Example:**
```js
fastify.register(require('fastify-static'), {
root: path.join(__dirname, '/static'),
prefix: '/public',
prefixAvoidTrailingSlash: true,
list: {
format: 'json',
names: ['index', 'index.json', '/']
}
})
```
Dir list respond with the same content to
```bash
GET .../public
GET .../public/
GET .../public/index
GET .../public/index.json
```
#### Disable serving

@@ -137,0 +253,0 @@

@@ -22,5 +22,6 @@ import fastify from 'fastify'

wildcard: true,
list: false,
setHeaders: (res: any, pathName: any) => {
res.setHeader('test', pathName)
}
},
}

@@ -27,0 +28,0 @@

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