Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ are applied for getting the file list.

This option cannot be `false` if `redirect` is `true` and `ignoreTrailingSlash` is `true`.

#### `globIgnore`

Default: `undefined`

This is passed to [`glob`](https://www.npmjs.com/package/glob)
as the `ignore` option. It can be used to ignore files or directories
when using the `wildcard: false` option.

#### `allowedPath`

Default: `(pathName, root, request) => true`
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async function fastifyStatic (fastify, opts) {
rootPath = rootPath.split(path.win32.sep).join(path.posix.sep)
!rootPath.endsWith('/') && (rootPath += '/')
const files = await glob('**/**', {
cwd: rootPath, absolute: false, follow: true, nodir: true, dot: opts.serveDotFiles
cwd: rootPath, absolute: false, follow: true, nodir: true, dot: opts.serveDotFiles, ignore: opts.globIgnore
})

for (let file of files) {
Expand Down
1 change: 1 addition & 0 deletions test/static-filtered/bar.private
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
3 changes: 3 additions & 0 deletions test/static-filtered/deep/path/to/baz.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<html>
<body>baz</body>
</html>
1 change: 1 addition & 0 deletions test/static-filtered/deep/path/to/baz.private
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
baz
3 changes: 3 additions & 0 deletions test/static-filtered/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<html>
<body>index2</body>
</html>
74 changes: 74 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3669,3 +3669,77 @@ test('register /static/ with custom log level', async t => {
t.assert.deepStrictEqual(response2.status, 304)
})
})

test('register with wildcard false and globIgnore', async t => {
t.plan(5)

const indexContent = fs
.readFileSync('./test/static-filtered/index.html')
.toString('utf8')

const deepContent = fs
.readFileSync('./test/static-filtered/deep/path/to/baz.html')
.toString('utf8')

const pluginOptions = {
root: path.join(__dirname, '/static-filtered'),
wildcard: false,
globIgnore: ['**/*.private']
}
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

t.after(() => fastify.close())

await fastify.listen({ port: 0 })

fastify.server.unref()

await t.test('/index.html', async t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)

const response = await fetch('http://localhost:' + fastify.server.address().port + '/index.html')
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), indexContent)
genericResponseChecks(t, response)
})

await t.test('/bar.private', async t => {
t.plan(2)

const response = await fetch('http://localhost:' + fastify.server.address().port + '/bar.private')
t.assert.ok(!response.ok)
t.assert.deepStrictEqual(response.status, 404)
await response.text()
})

await t.test('/', async (t) => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)

const response = await fetch('http://localhost:' + fastify.server.address().port)
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), indexContent)
genericResponseChecks(t, response)
})

await t.test('/deep/path/to/baz.html', async (t) => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)

const response = await fetch('http://localhost:' + fastify.server.address().port + '/deep/path/to/baz.html')
t.assert.ok(response.ok)
t.assert.deepStrictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), deepContent)
genericResponseChecks(t, response)
})

await t.test('/deep/path/to/baz.private', async (t) => {
t.plan(2)

const response = await fetch('http://localhost:' + fastify.server.address().port + '/deep/path/to/baz.private')
t.assert.ok(!response.ok)
t.assert.deepStrictEqual(response.status, 404)
await response.text()
})
})
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ declare namespace fastifyStatic {
setHeaders?: (res: SetHeadersResponse, path: string, stat: Stats) => void;
redirect?: boolean;
wildcard?: boolean;
globIgnore?: string[];
list?: boolean | ListOptionsJsonFormat | ListOptionsHtmlFormat;
allowedPath?: (pathName: string, root: string, request: FastifyRequest) => boolean;
/**
Expand Down
1 change: 1 addition & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const options: FastifyStaticOptions = {
schemaHide: true,
serve: true,
wildcard: true,
globIgnore: ['**/*.private'],
list: false,
setHeaders: (res, path, stat) => {
expectType<string>(res.filename)
Expand Down
Loading