Skip to content

Commit

Permalink
chore: remove handlebars
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Aug 21, 2024
1 parent 95428ba commit 6f6a492
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 105 deletions.
35 changes: 12 additions & 23 deletions example/server-dir-list.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
'use strict'

const path = require('node:path')
const Handlebars = require('handlebars')

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

// Handlebar template for listing files and directories.
const template = `
<html>
<body>
dirs
<ul>
{{#dirs}}
<li><a href="{{href}}">{{name}}</a></li>
{{/dirs}}
</ul>
list
<ul>
{{#files}}
<li><a href="{{href}}" target="_blank">{{name}}</a></li>
{{/files}}
</ul>
</body>
</html>
const renderer = (dirs, files) => {
return `
<html><body>
<ul>
${dirs.map(dir => `<li><a href="${dir.href}">${dir.name}</a></li>`).join('\n ')}
</ul>
<ul>
${files.map(file => `<li><a href="${file.href}" target="_blank">${file.name}</a></li>`).join('\n ')}
</ul>
</body></html>
`
const handlebarTemplate = Handlebars.compile(template)
}

fastify
.register(require('..'), {
Expand All @@ -41,7 +30,7 @@ fastify
// A list of filenames that trigger a directory list response.
names: ['index', 'index.html', 'index.htm', '/'],
// You can provide your own render method as needed.
render: (dirs, files) => handlebarTemplate({ dirs, files })
renderer
}
})
.listen({ port: 3000 }, err => {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"concat-stream": "^2.0.0",
"coveralls": "^3.1.1",
"fastify": "^5.0.0-alpha.3",
"handlebars": "^4.7.8",
"pino": "^9.1.0",
"proxyquire": "^2.1.3",
"simple-get": "^4.0.1",
Expand Down
111 changes: 30 additions & 81 deletions test/dir-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,51 +200,15 @@ t.test('dir list, custom options with empty array index', t => {
})

t.test('dir list html format', t => {
t.plan(6)

// render html in 2 ways: one with handlebars and one with template string

const Handlebars = require('handlebars')
const source = `
<html><body>
<ul>
{{#dirs}}
<li><a href="{{href}}">{{name}}</a></li>
{{/dirs}}
</ul>
<ul>
{{#files}}
<li><a href="{{href}}" target="_blank">{{name}}</a></li>
{{/files}}
</ul>
</body></html>
`
const handlebarTemplate = Handlebars.compile(source)
const templates = [
{
render: (dirs, files) => {
return handlebarTemplate({ dirs, files })
},
output: `
<html><body>
<ul>
<li><a href="/public/deep">deep</a></li>
<li><a href="/public/shallow">shallow</a></li>
</ul>
<ul>
<li><a href="/public/.example" target="_blank">.example</a></li>
<li><a href="/public/100%25.txt" target="_blank">100%.txt</a></li>
<li><a href="/public/a%20.md" target="_blank">a .md</a></li>
<li><a href="/public/foo.html" target="_blank">foo.html</a></li>
<li><a href="/public/foobar.html" target="_blank">foobar.html</a></li>
<li><a href="/public/index.css" target="_blank">index.css</a></li>
<li><a href="/public/index.html" target="_blank">index.html</a></li>
</ul>
</body></html>
`
},
t.plan(3)

{
const options = {
root: path.join(__dirname, '/static'),
prefix: '/public',
index: false,
list: {
format: 'html',
names: ['index', 'index.htm'],
render: (dirs, files) => {
return `
<html><body>
Expand All @@ -256,8 +220,24 @@ t.test('dir list html format', t => {
</ul>
</body></html>
`
},
output: `
}
}
}
const routes = ['/public/index.htm', '/public/index']

// check all routes by names

helper.arrange(t, options, (url) => {
for (const route of routes) {
t.test(route, t => {
t.plan(3)
simple.concat({
method: 'GET',
url: url + route
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), `
<html><body>
<ul>
<li><a href="/public/deep">deep</a></li>
Expand All @@ -273,42 +253,11 @@ t.test('dir list html format', t => {
<li><a href="/public/index.html" target="_blank">index.html</a></li>
</ul>
</body></html>
`
}

]

for (const template of templates) {
const options = {
root: path.join(__dirname, '/static'),
prefix: '/public',
index: false,
list: {
format: 'html',
names: ['index', 'index.htm'],
render: template.render
}
}
const routes = ['/public/index.htm', '/public/index']

// check all routes by names

helper.arrange(t, options, (url) => {
for (const route of routes) {
t.test(route, t => {
t.plan(3)
simple.concat({
method: 'GET',
url: url + route
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), template.output)
})
`)
})
}
})
}
})
}
})
})

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

0 comments on commit 6f6a492

Please sign in to comment.