@fastify/static 5.0.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.
Files changed (46) hide show
  1. package/.gitattributes +5 -0
  2. package/.github/dependabot.yml +13 -0
  3. package/.github/stale.yml +21 -0
  4. package/.github/workflows/ci.yml +46 -0
  5. package/LICENSE +21 -0
  6. package/README.md +440 -0
  7. package/example/public/images/sample.jpg +0 -0
  8. package/example/public/index.css +9 -0
  9. package/example/public/index.html +11 -0
  10. package/example/public/index.js +8 -0
  11. package/example/public2/test.css +4 -0
  12. package/example/public2/test.html +8 -0
  13. package/example/server-compress.js +15 -0
  14. package/example/server-dir-list.js +49 -0
  15. package/example/server.js +13 -0
  16. package/index.d.ts +98 -0
  17. package/index.js +509 -0
  18. package/lib/dirList.js +199 -0
  19. package/package.json +71 -0
  20. package/test/dir-list.test.js +675 -0
  21. package/test/static/.example +1 -0
  22. package/test/static/a .md +1 -0
  23. package/test/static/deep/path/for/test/index.html +5 -0
  24. package/test/static/deep/path/for/test/purpose/foo.html +5 -0
  25. package/test/static/foo.html +3 -0
  26. package/test/static/foobar.html +3 -0
  27. package/test/static/index.css +0 -0
  28. package/test/static/index.html +5 -0
  29. package/test/static/shallow/sample.jpg +0 -0
  30. package/test/static-pre-compressed/all-three.html +5 -0
  31. package/test/static-pre-compressed/all-three.html.br +0 -0
  32. package/test/static-pre-compressed/all-three.html.gz +0 -0
  33. package/test/static-pre-compressed/dir/index.html +5 -0
  34. package/test/static-pre-compressed/dir/index.html.br +0 -0
  35. package/test/static-pre-compressed/empty/.gitkeep +0 -0
  36. package/test/static-pre-compressed/gzip-only.html +3 -0
  37. package/test/static-pre-compressed/gzip-only.html.gz +0 -0
  38. package/test/static-pre-compressed/index.html +5 -0
  39. package/test/static-pre-compressed/index.html.br +0 -0
  40. package/test/static-pre-compressed/sample.jpg +0 -0
  41. package/test/static-pre-compressed/sample.jpg.br +0 -0
  42. package/test/static-pre-compressed/uncompressed.html +3 -0
  43. package/test/static.test.js +3482 -0
  44. package/test/static2/bar.html +3 -0
  45. package/test/static2/index.html +3 -0
  46. package/test/types/index.ts +105 -0
@@ -0,0 +1,3 @@
1
+ <html>
2
+ <body>bar</body>
3
+ </html>
@@ -0,0 +1,3 @@
1
+ <html>
2
+ <body>index2</body>
3
+ </html>
@@ -0,0 +1,105 @@
1
+ import fastify from 'fastify'
2
+ import { expectError } from 'tsd'
3
+ import fastifyStatic, { FastifyStaticOptions } from '../..'
4
+
5
+ const appWithImplicitHttp = fastify()
6
+ const options: FastifyStaticOptions = {
7
+ acceptRanges: true,
8
+ cacheControl: true,
9
+ decorateReply: true,
10
+ dotfiles: 'allow',
11
+ etag: true,
12
+ extensions: ['.js'],
13
+ immutable: true,
14
+ index: ['1'],
15
+ lastModified: true,
16
+ maxAge: '',
17
+ prefix: '',
18
+ prefixAvoidTrailingSlash: false,
19
+ root: '',
20
+ schemaHide: true,
21
+ serve: true,
22
+ wildcard: true,
23
+ list: false,
24
+ setHeaders: (res: any, pathName: any) => {
25
+ res.setHeader('test', pathName)
26
+ },
27
+ preCompressed: false
28
+ }
29
+
30
+ expectError<FastifyStaticOptions>({
31
+ wlidcard: '**/*'
32
+ })
33
+
34
+ appWithImplicitHttp
35
+ .register(fastifyStatic, options)
36
+ .after(() => {
37
+ appWithImplicitHttp.get('/', (request, reply) => {
38
+ reply.sendFile('some-file-name')
39
+ })
40
+ })
41
+
42
+ const appWithHttp2 = fastify({ http2: true })
43
+
44
+ appWithHttp2
45
+ .register(fastifyStatic, options)
46
+ .after(() => {
47
+ appWithHttp2.get('/', (request, reply) => {
48
+ reply.sendFile('some-file-name')
49
+ })
50
+
51
+ appWithHttp2.get('/download', (request, reply) => {
52
+ reply.download('some-file-name')
53
+ })
54
+
55
+ appWithHttp2.get('/download/1', (request, reply) => {
56
+ reply.download('some-file-name', { maxAge: '2 days' })
57
+ })
58
+
59
+ appWithHttp2.get('/download/2', (request, reply) => {
60
+ reply.download('some-file-name', 'some-filename' ,{ cacheControl: false, acceptRanges: true })
61
+ })
62
+ })
63
+
64
+ const multiRootAppWithImplicitHttp = fastify()
65
+ options.root = ['']
66
+
67
+ multiRootAppWithImplicitHttp
68
+ .register(fastifyStatic, options)
69
+ .after(() => {
70
+ multiRootAppWithImplicitHttp.get('/', (request, reply) => {
71
+ reply.sendFile('some-file-name')
72
+ })
73
+
74
+ multiRootAppWithImplicitHttp.get('/', (request, reply) => {
75
+ reply.sendFile('some-file-name', { cacheControl: false, acceptRanges: true })
76
+ })
77
+
78
+ multiRootAppWithImplicitHttp.get('/', (request, reply) => {
79
+ reply.sendFile('some-file-name', 'some-root-name', { cacheControl: false, acceptRanges: true })
80
+ })
81
+
82
+ multiRootAppWithImplicitHttp.get('/download', (request, reply) => {
83
+ reply.download('some-file-name')
84
+ })
85
+
86
+ multiRootAppWithImplicitHttp.get('/download/1', (request, reply) => {
87
+ reply.download('some-file-name', { maxAge: '2 days' })
88
+ })
89
+
90
+ multiRootAppWithImplicitHttp.get('/download/2', (request, reply) => {
91
+ reply.download('some-file-name', 'some-filename', { cacheControl: false, acceptRanges: true })
92
+ })
93
+ })
94
+
95
+ const noIndexApp = fastify()
96
+ options.root = ''
97
+ options.index = false
98
+
99
+ noIndexApp
100
+ .register(fastifyStatic, options)
101
+ .after(() => {
102
+ noIndexApp.get('/', (request, reply) => {
103
+ reply.send('<h1>fastify-static</h1>')
104
+ })
105
+ })