@fastify/static 8.0.0-pre.fv5.1 → 8.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.
- package/eslint.config.js +6 -0
- package/example/server-dir-list.js +12 -23
- package/package.json +12 -25
- package/test/dir-list.test.js +30 -81
- package/types/index.d.ts +3 -3
- package/types/index.test-d.ts +1 -1
- package/tsconfig.eslint.json +0 -10
package/eslint.config.js
ADDED
|
@@ -1,32 +1,21 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const path = require('node:path')
|
|
4
|
-
const Handlebars = require('handlebars')
|
|
5
4
|
|
|
6
5
|
const fastify = require('fastify')({ logger: { level: 'trace' } })
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
<html>
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
list
|
|
20
|
-
|
|
21
|
-
<ul>
|
|
22
|
-
{{#files}}
|
|
23
|
-
<li><a href="{{href}}" target="_blank">{{name}}</a></li>
|
|
24
|
-
{{/files}}
|
|
25
|
-
</ul>
|
|
26
|
-
</body>
|
|
27
|
-
</html>
|
|
7
|
+
const renderer = (dirs, files) => {
|
|
8
|
+
return `
|
|
9
|
+
<html><body>
|
|
10
|
+
<ul>
|
|
11
|
+
${dirs.map(dir => `<li><a href="${dir.href}">${dir.name}</a></li>`).join('\n ')}
|
|
12
|
+
</ul>
|
|
13
|
+
<ul>
|
|
14
|
+
${files.map(file => `<li><a href="${file.href}" target="_blank">${file.name}</a></li>`).join('\n ')}
|
|
15
|
+
</ul>
|
|
16
|
+
</body></html>
|
|
28
17
|
`
|
|
29
|
-
|
|
18
|
+
}
|
|
30
19
|
|
|
31
20
|
fastify
|
|
32
21
|
.register(require('..'), {
|
|
@@ -41,7 +30,7 @@ fastify
|
|
|
41
30
|
// A list of filenames that trigger a directory list response.
|
|
42
31
|
names: ['index', 'index.html', 'index.htm', '/'],
|
|
43
32
|
// You can provide your own render method as needed.
|
|
44
|
-
|
|
33
|
+
renderer
|
|
45
34
|
}
|
|
46
35
|
})
|
|
47
36
|
.listen({ port: 3000 }, err => {
|
package/package.json
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/static",
|
|
3
|
-
"version": "8.0.0
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"description": "Plugin for serving static files as fast as possible.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "commonjs",
|
|
7
7
|
"types": "types/index.d.ts",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"coverage": "npm run test:unit -- --coverage-report=html",
|
|
10
|
-
"lint": "
|
|
11
|
-
"lint:
|
|
12
|
-
"lint:fix": "standard --fix && npm run lint:typescript -- --fix",
|
|
13
|
-
"lint:typescript": "eslint -c .eslintrc.json types/**/*.d.ts types/**/*.test-d.ts",
|
|
10
|
+
"lint": "eslint",
|
|
11
|
+
"lint:fix": "eslint --fix",
|
|
14
12
|
"test": "npm run test:unit && npm run test:typescript",
|
|
15
13
|
"test:typescript": "tsd",
|
|
16
14
|
"test:unit": "tap",
|
|
@@ -31,41 +29,30 @@
|
|
|
31
29
|
},
|
|
32
30
|
"homepage": "https://github.com/fastify/fastify-static",
|
|
33
31
|
"dependencies": {
|
|
34
|
-
"@fastify/accept-negotiator": "^2.0.0
|
|
32
|
+
"@fastify/accept-negotiator": "^2.0.0",
|
|
35
33
|
"@fastify/send": "^3.1.0",
|
|
36
34
|
"content-disposition": "^0.5.4",
|
|
37
|
-
"fastify-plugin": "^5.0.0
|
|
35
|
+
"fastify-plugin": "^5.0.0",
|
|
38
36
|
"fastq": "^1.17.1",
|
|
39
|
-
"glob": "^
|
|
37
|
+
"glob": "^11.0.0"
|
|
40
38
|
},
|
|
41
39
|
"devDependencies": {
|
|
42
|
-
"@fastify/compress": "^8.0.0
|
|
40
|
+
"@fastify/compress": "^8.0.0",
|
|
43
41
|
"@fastify/pre-commit": "^2.1.0",
|
|
44
|
-
"@types/node": "^
|
|
45
|
-
"@typescript-eslint/eslint-plugin": "^7.3.1",
|
|
46
|
-
"@typescript-eslint/parser": "^7.3.1",
|
|
42
|
+
"@types/node": "^22.0.0",
|
|
47
43
|
"concat-stream": "^2.0.0",
|
|
48
|
-
"
|
|
49
|
-
"fastify": "^5.0.0-alpha.
|
|
50
|
-
"
|
|
44
|
+
"eslint": "^9.9.0",
|
|
45
|
+
"fastify": "^5.0.0-alpha.4",
|
|
46
|
+
"neostandard": "^0.11.3",
|
|
51
47
|
"pino": "^9.1.0",
|
|
52
48
|
"proxyquire": "^2.1.3",
|
|
53
49
|
"simple-get": "^4.0.1",
|
|
54
|
-
"snazzy": "^9.0.0",
|
|
55
|
-
"standard": "^17.1.0",
|
|
56
50
|
"tap": "^18.7.1",
|
|
57
|
-
"tsd": "^0.31.0"
|
|
58
|
-
"typescript": "^5.4.3"
|
|
51
|
+
"tsd": "^0.31.0"
|
|
59
52
|
},
|
|
60
53
|
"tsd": {
|
|
61
54
|
"directory": "test/types"
|
|
62
55
|
},
|
|
63
|
-
"eslintConfig": {
|
|
64
|
-
"rules": {
|
|
65
|
-
"no-unused-vars": "off",
|
|
66
|
-
"@typescript-eslint/no-unused-vars": "error"
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
56
|
"publishConfig": {
|
|
70
57
|
"access": "public"
|
|
71
58
|
}
|
package/test/dir-list.test.js
CHANGED
|
@@ -200,51 +200,15 @@ t.test('dir list, custom options with empty array index', t => {
|
|
|
200
200
|
})
|
|
201
201
|
|
|
202
202
|
t.test('dir list html format', t => {
|
|
203
|
-
t.plan(
|
|
204
|
-
|
|
205
|
-
// render html in 2 ways: one with handlebars and one with template string
|
|
206
|
-
|
|
207
|
-
const Handlebars = require('handlebars')
|
|
208
|
-
const source = `
|
|
209
|
-
<html><body>
|
|
210
|
-
<ul>
|
|
211
|
-
{{#dirs}}
|
|
212
|
-
<li><a href="{{href}}">{{name}}</a></li>
|
|
213
|
-
{{/dirs}}
|
|
214
|
-
</ul>
|
|
215
|
-
<ul>
|
|
216
|
-
{{#files}}
|
|
217
|
-
<li><a href="{{href}}" target="_blank">{{name}}</a></li>
|
|
218
|
-
{{/files}}
|
|
219
|
-
</ul>
|
|
220
|
-
</body></html>
|
|
221
|
-
`
|
|
222
|
-
const handlebarTemplate = Handlebars.compile(source)
|
|
223
|
-
const templates = [
|
|
224
|
-
{
|
|
225
|
-
render: (dirs, files) => {
|
|
226
|
-
return handlebarTemplate({ dirs, files })
|
|
227
|
-
},
|
|
228
|
-
output: `
|
|
229
|
-
<html><body>
|
|
230
|
-
<ul>
|
|
231
|
-
<li><a href="/public/deep">deep</a></li>
|
|
232
|
-
<li><a href="/public/shallow">shallow</a></li>
|
|
233
|
-
</ul>
|
|
234
|
-
<ul>
|
|
235
|
-
<li><a href="/public/.example" target="_blank">.example</a></li>
|
|
236
|
-
<li><a href="/public/100%25.txt" target="_blank">100%.txt</a></li>
|
|
237
|
-
<li><a href="/public/a%20.md" target="_blank">a .md</a></li>
|
|
238
|
-
<li><a href="/public/foo.html" target="_blank">foo.html</a></li>
|
|
239
|
-
<li><a href="/public/foobar.html" target="_blank">foobar.html</a></li>
|
|
240
|
-
<li><a href="/public/index.css" target="_blank">index.css</a></li>
|
|
241
|
-
<li><a href="/public/index.html" target="_blank">index.html</a></li>
|
|
242
|
-
</ul>
|
|
243
|
-
</body></html>
|
|
244
|
-
`
|
|
245
|
-
},
|
|
203
|
+
t.plan(3)
|
|
246
204
|
|
|
247
|
-
|
|
205
|
+
const options = {
|
|
206
|
+
root: path.join(__dirname, '/static'),
|
|
207
|
+
prefix: '/public',
|
|
208
|
+
index: false,
|
|
209
|
+
list: {
|
|
210
|
+
format: 'html',
|
|
211
|
+
names: ['index', 'index.htm'],
|
|
248
212
|
render: (dirs, files) => {
|
|
249
213
|
return `
|
|
250
214
|
<html><body>
|
|
@@ -256,8 +220,24 @@ t.test('dir list html format', t => {
|
|
|
256
220
|
</ul>
|
|
257
221
|
</body></html>
|
|
258
222
|
`
|
|
259
|
-
}
|
|
260
|
-
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
const routes = ['/public/index.htm', '/public/index']
|
|
227
|
+
|
|
228
|
+
// check all routes by names
|
|
229
|
+
|
|
230
|
+
helper.arrange(t, options, (url) => {
|
|
231
|
+
for (const route of routes) {
|
|
232
|
+
t.test(route, t => {
|
|
233
|
+
t.plan(3)
|
|
234
|
+
simple.concat({
|
|
235
|
+
method: 'GET',
|
|
236
|
+
url: url + route
|
|
237
|
+
}, (err, response, body) => {
|
|
238
|
+
t.error(err)
|
|
239
|
+
t.equal(response.statusCode, 200)
|
|
240
|
+
t.equal(body.toString(), `
|
|
261
241
|
<html><body>
|
|
262
242
|
<ul>
|
|
263
243
|
<li><a href="/public/deep">deep</a></li>
|
|
@@ -273,42 +253,11 @@ t.test('dir list html format', t => {
|
|
|
273
253
|
<li><a href="/public/index.html" target="_blank">index.html</a></li>
|
|
274
254
|
</ul>
|
|
275
255
|
</body></html>
|
|
276
|
-
`
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
]
|
|
280
|
-
|
|
281
|
-
for (const template of templates) {
|
|
282
|
-
const options = {
|
|
283
|
-
root: path.join(__dirname, '/static'),
|
|
284
|
-
prefix: '/public',
|
|
285
|
-
index: false,
|
|
286
|
-
list: {
|
|
287
|
-
format: 'html',
|
|
288
|
-
names: ['index', 'index.htm'],
|
|
289
|
-
render: template.render
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
const routes = ['/public/index.htm', '/public/index']
|
|
293
|
-
|
|
294
|
-
// check all routes by names
|
|
295
|
-
|
|
296
|
-
helper.arrange(t, options, (url) => {
|
|
297
|
-
for (const route of routes) {
|
|
298
|
-
t.test(route, t => {
|
|
299
|
-
t.plan(3)
|
|
300
|
-
simple.concat({
|
|
301
|
-
method: 'GET',
|
|
302
|
-
url: url + route
|
|
303
|
-
}, (err, response, body) => {
|
|
304
|
-
t.error(err)
|
|
305
|
-
t.equal(response.statusCode, 200)
|
|
306
|
-
t.equal(body.toString(), template.output)
|
|
307
|
-
})
|
|
256
|
+
`)
|
|
308
257
|
})
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
258
|
+
})
|
|
259
|
+
}
|
|
260
|
+
})
|
|
312
261
|
})
|
|
313
262
|
|
|
314
263
|
t.test('dir list href nested structure', t => {
|
package/types/index.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ declare module 'fastify' {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
type FastifyStaticPlugin = FastifyPluginAsync<NonNullable<fastifyStatic.FastifyStaticOptions
|
|
19
|
+
type FastifyStaticPlugin = FastifyPluginAsync<NonNullable<fastifyStatic.FastifyStaticOptions>>
|
|
20
20
|
|
|
21
21
|
declare namespace fastifyStatic {
|
|
22
22
|
export interface SetHeadersResponse {
|
|
@@ -119,6 +119,6 @@ declare namespace fastifyStatic {
|
|
|
119
119
|
export { fastifyStatic as default }
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
declare function fastifyStatic(...params: Parameters<FastifyStaticPlugin>): ReturnType<FastifyStaticPlugin
|
|
122
|
+
declare function fastifyStatic (...params: Parameters<FastifyStaticPlugin>): ReturnType<FastifyStaticPlugin>
|
|
123
123
|
|
|
124
|
-
export = fastifyStatic
|
|
124
|
+
export = fastifyStatic
|
package/types/index.test-d.ts
CHANGED
|
@@ -8,7 +8,7 @@ import fastifyStatic, {
|
|
|
8
8
|
fastifyStatic as fastifyStaticNamed
|
|
9
9
|
} from '..'
|
|
10
10
|
|
|
11
|
-
import fastifyStaticCjsImport = require('..')
|
|
11
|
+
import fastifyStaticCjsImport = require('..')
|
|
12
12
|
const fastifyStaticCjs = require('..')
|
|
13
13
|
|
|
14
14
|
const app: FastifyInstance = fastify()
|