@fastify/static 6.1.0 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/index.js +4 -2
- package/lib/dirList.js +10 -5
- package/package.json +1 -1
- package/test/dir-list.test.js +84 -0
- package/test/static-dotfiles/.aaa +0 -0
- package/test/static-dotfiles/dir/index.html +0 -0
- package/test/static-dotfiles/test.txt +0 -0
package/README.md
CHANGED
|
@@ -190,7 +190,9 @@ If set, it provide the directory list calling the directory path.
|
|
|
190
190
|
|
|
191
191
|
Default response is json.
|
|
192
192
|
|
|
193
|
-
Note:
|
|
193
|
+
Note:
|
|
194
|
+
- Multi-root is not supported within the `list` option.
|
|
195
|
+
- If `dotfiles` option value is `deny` or `ignore`, dotfiles will be excluded.
|
|
194
196
|
|
|
195
197
|
**Example:**
|
|
196
198
|
|
package/index.js
CHANGED
|
@@ -161,7 +161,8 @@ async function fastifyStatic (fastify, opts) {
|
|
|
161
161
|
dir: path,
|
|
162
162
|
options: opts.list,
|
|
163
163
|
route: pathname,
|
|
164
|
-
prefix
|
|
164
|
+
prefix,
|
|
165
|
+
dotfiles: opts.dotfiles
|
|
165
166
|
}).catch((err) => reply.send(err))
|
|
166
167
|
return
|
|
167
168
|
}
|
|
@@ -217,7 +218,8 @@ async function fastifyStatic (fastify, opts) {
|
|
|
217
218
|
dir: dirList.path(opts.root, pathname),
|
|
218
219
|
options: opts.list,
|
|
219
220
|
route: pathname,
|
|
220
|
-
prefix
|
|
221
|
+
prefix,
|
|
222
|
+
dotfiles: opts.dotfiles
|
|
221
223
|
}).catch((err) => reply.send(err))
|
|
222
224
|
return
|
|
223
225
|
}
|
package/lib/dirList.js
CHANGED
|
@@ -8,12 +8,16 @@ const dirList = {
|
|
|
8
8
|
/**
|
|
9
9
|
* get files and dirs from dir, or error
|
|
10
10
|
* @param {string} dir full path fs dir
|
|
11
|
-
* @param {
|
|
11
|
+
* @param {ListOptions} options
|
|
12
|
+
* @param {string} dotfiles
|
|
12
13
|
* note: can't use glob because don't get error on non existing dir
|
|
13
14
|
*/
|
|
14
|
-
list: async function (dir, options) {
|
|
15
|
+
list: async function (dir, options, dotfiles) {
|
|
15
16
|
const entries = { dirs: [], files: [] }
|
|
16
|
-
|
|
17
|
+
let files = await fs.readdir(dir)
|
|
18
|
+
if (dotfiles === 'deny' || dotfiles === 'ignore') {
|
|
19
|
+
files = files.filter(f => f.charAt(0) !== '.')
|
|
20
|
+
}
|
|
17
21
|
if (files.length < 1) {
|
|
18
22
|
return entries
|
|
19
23
|
}
|
|
@@ -97,11 +101,12 @@ const dirList = {
|
|
|
97
101
|
* @param {string} dir full path fs dir
|
|
98
102
|
* @param {ListOptions} options
|
|
99
103
|
* @param {string} route request route
|
|
104
|
+
* @param {string} dotfiles
|
|
100
105
|
*/
|
|
101
|
-
send: async function ({ reply, dir, options, route, prefix }) {
|
|
106
|
+
send: async function ({ reply, dir, options, route, prefix, dotfiles }) {
|
|
102
107
|
let entries
|
|
103
108
|
try {
|
|
104
|
-
entries = await dirList.list(dir, options)
|
|
109
|
+
entries = await dirList.list(dir, options, dotfiles)
|
|
105
110
|
} catch (error) {
|
|
106
111
|
return reply.callNotFound()
|
|
107
112
|
}
|
package/package.json
CHANGED
package/test/dir-list.test.js
CHANGED
|
@@ -636,6 +636,90 @@ t.test('serve a non existent dir and get error', t => {
|
|
|
636
636
|
})
|
|
637
637
|
})
|
|
638
638
|
|
|
639
|
+
t.test('dir list with dotfiles allow option', t => {
|
|
640
|
+
t.plan(2)
|
|
641
|
+
|
|
642
|
+
const options = {
|
|
643
|
+
root: path.join(__dirname, '/static-dotfiles'),
|
|
644
|
+
prefix: '/public',
|
|
645
|
+
dotfiles: 'allow',
|
|
646
|
+
index: false,
|
|
647
|
+
list: true
|
|
648
|
+
}
|
|
649
|
+
const route = '/public/'
|
|
650
|
+
const content = { dirs: ['dir'], files: ['.aaa', 'test.txt'] }
|
|
651
|
+
|
|
652
|
+
helper.arrange(t, options, (url) => {
|
|
653
|
+
t.test(route, t => {
|
|
654
|
+
t.plan(3)
|
|
655
|
+
simple.concat({
|
|
656
|
+
method: 'GET',
|
|
657
|
+
url: url + route
|
|
658
|
+
}, (err, response, body) => {
|
|
659
|
+
t.error(err)
|
|
660
|
+
t.equal(response.statusCode, 200)
|
|
661
|
+
t.equal(body.toString(), JSON.stringify(content))
|
|
662
|
+
})
|
|
663
|
+
})
|
|
664
|
+
})
|
|
665
|
+
})
|
|
666
|
+
|
|
667
|
+
t.test('dir list with dotfiles deny option', t => {
|
|
668
|
+
t.plan(2)
|
|
669
|
+
|
|
670
|
+
const options = {
|
|
671
|
+
root: path.join(__dirname, '/static-dotfiles'),
|
|
672
|
+
prefix: '/public',
|
|
673
|
+
dotfiles: 'deny',
|
|
674
|
+
index: false,
|
|
675
|
+
list: true
|
|
676
|
+
}
|
|
677
|
+
const route = '/public/'
|
|
678
|
+
const content = { dirs: ['dir'], files: ['test.txt'] }
|
|
679
|
+
|
|
680
|
+
helper.arrange(t, options, (url) => {
|
|
681
|
+
t.test(route, t => {
|
|
682
|
+
t.plan(3)
|
|
683
|
+
simple.concat({
|
|
684
|
+
method: 'GET',
|
|
685
|
+
url: url + route
|
|
686
|
+
}, (err, response, body) => {
|
|
687
|
+
t.error(err)
|
|
688
|
+
t.equal(response.statusCode, 200)
|
|
689
|
+
t.equal(body.toString(), JSON.stringify(content))
|
|
690
|
+
})
|
|
691
|
+
})
|
|
692
|
+
})
|
|
693
|
+
})
|
|
694
|
+
|
|
695
|
+
t.test('dir list with dotfiles ignore option', t => {
|
|
696
|
+
t.plan(2)
|
|
697
|
+
|
|
698
|
+
const options = {
|
|
699
|
+
root: path.join(__dirname, '/static-dotfiles'),
|
|
700
|
+
prefix: '/public',
|
|
701
|
+
dotfiles: 'ignore',
|
|
702
|
+
index: false,
|
|
703
|
+
list: true
|
|
704
|
+
}
|
|
705
|
+
const route = '/public/'
|
|
706
|
+
const content = { dirs: ['dir'], files: ['test.txt'] }
|
|
707
|
+
|
|
708
|
+
helper.arrange(t, options, (url) => {
|
|
709
|
+
t.test(route, t => {
|
|
710
|
+
t.plan(3)
|
|
711
|
+
simple.concat({
|
|
712
|
+
method: 'GET',
|
|
713
|
+
url: url + route
|
|
714
|
+
}, (err, response, body) => {
|
|
715
|
+
t.error(err)
|
|
716
|
+
t.equal(response.statusCode, 200)
|
|
717
|
+
t.equal(body.toString(), JSON.stringify(content))
|
|
718
|
+
})
|
|
719
|
+
})
|
|
720
|
+
})
|
|
721
|
+
})
|
|
722
|
+
|
|
639
723
|
t.test('dir list error', t => {
|
|
640
724
|
t.plan(7)
|
|
641
725
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|