Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove handlebars #464

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -43,7 +43,6 @@
"concat-stream": "^2.0.0",
"eslint": "^9.9.0",
"fastify": "^5.0.0-alpha.3",
"handlebars": "^4.7.8",
"neostandard": "^0.11.3",
"pino": "^9.1.0",
"proxyquire": "^2.1.3",
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