Skip to content

Commit 39e89ea

Browse files
authored
chore: remove handlebars (#464)
Signed-off-by: Aras Abbasi <[email protected]>
1 parent cccd58f commit 39e89ea

File tree

3 files changed

+42
-105
lines changed

3 files changed

+42
-105
lines changed

example/server-dir-list.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
11
'use strict'
22

33
const path = require('node:path')
4-
const Handlebars = require('handlebars')
54

65
const fastify = require('fastify')({ logger: { level: 'trace' } })
76

8-
// Handlebar template for listing files and directories.
9-
const template = `
10-
<html>
11-
<body>
12-
dirs
13-
<ul>
14-
{{#dirs}}
15-
<li><a href="{{href}}">{{name}}</a></li>
16-
{{/dirs}}
17-
</ul>
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>
2817
`
29-
const handlebarTemplate = Handlebars.compile(template)
18+
}
3019

3120
fastify
3221
.register(require('..'), {
@@ -41,7 +30,7 @@ fastify
4130
// A list of filenames that trigger a directory list response.
4231
names: ['index', 'index.html', 'index.htm', '/'],
4332
// You can provide your own render method as needed.
44-
render: (dirs, files) => handlebarTemplate({ dirs, files })
33+
renderer
4534
}
4635
})
4736
.listen({ port: 3000 }, err => {

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
"concat-stream": "^2.0.0",
4444
"eslint": "^9.9.0",
4545
"fastify": "^5.0.0-alpha.3",
46-
"handlebars": "^4.7.8",
4746
"neostandard": "^0.11.3",
4847
"pino": "^9.1.0",
4948
"proxyquire": "^2.1.3",

test/dir-list.test.js

Lines changed: 30 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -200,51 +200,15 @@ t.test('dir list, custom options with empty array index', t => {
200200
})
201201

202202
t.test('dir list html format', t => {
203-
t.plan(6)
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)
246204

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'],
248212
render: (dirs, files) => {
249213
return `
250214
<html><body>
@@ -256,8 +220,24 @@ t.test('dir list html format', t => {
256220
</ul>
257221
</body></html>
258222
`
259-
},
260-
output: `
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(), `
261241
<html><body>
262242
<ul>
263243
<li><a href="/public/deep">deep</a></li>
@@ -273,42 +253,11 @@ t.test('dir list html format', t => {
273253
<li><a href="/public/index.html" target="_blank">index.html</a></li>
274254
</ul>
275255
</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+
`)
308257
})
309-
}
310-
})
311-
}
258+
})
259+
}
260+
})
312261
})
313262

314263
t.test('dir list href nested structure', t => {

0 commit comments

Comments
 (0)