Skip to content
Closed
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
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ send.mime.default_type = 'application/octet-stream'

/** @type {import("fastify").FastifyPluginAsync<import("./types").FastifyStaticOptions>} */
async function fastifyStatic (fastify, opts) {
opts.root = normalizeRoot(opts.root)
checkRootPathForErrors(fastify, opts.root)
if (opts.serve !== false) {
opts.root = normalizeRoot(opts.root)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dipali127
Why should normalizeRoot also be skipped?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this scenario we are not using root if I'm not wrong

checkRootPathForErrors(fastify, opts.root)
}

const setHeaders = opts.setHeaders
if (setHeaders !== undefined && typeof setHeaders !== 'function') {
Expand Down
11 changes: 11 additions & 0 deletions test/root/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test file</title>
</head>
<body>
<h1>hello fastify</h1>
</body>
</html>
39 changes: 39 additions & 0 deletions test/serve-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

const path = require('node:path')
const assert = require('node:assert')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifyStatic = require('../index.js')

test('should not serve static files when serve is false', async t => {
const fastify = Fastify()
fastify.register(fastifyStatic, {
serve: false
})

t.after(() => fastify.close())
await fastify.listen({ port: 0 })
fastify.server.unref()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fastify.server.unref()
t.after(() => fastify.close())


const res = await fetch('http://localhost:' + fastify.server.address().port + '/public/example.html')
assert.strictEqual(res.status, 404)
})

test('should serve static files when serve is true', async t => {
const fastify = Fastify()
fastify.register(fastifyStatic, {
root: path.join(__dirname, 'root'),
prefix: '/public/'
})

t.after(() => fastify.close())
await fastify.listen({ port: 0 })
fastify.server.unref()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fastify.server.unref()
t.after(() => fastify.close())


const res = await fetch('http://localhost:' + fastify.server.address().port + '/public/example.html')
assert.strictEqual(res.status, 200)

const content = await res.text()
assert.ok(content.includes('hello'), 'File content should contain "hello"')
})
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ declare namespace fastifyStatic {
}

export interface FastifyStaticOptions extends SendOptions {
root: string | string[] | URL | URL[];
root?: string | string[] | URL | URL[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boring to write, but we need something like:

export interface FastifyStaticOptions extends SendOptions {
  ...stuff
} & ({
  serve: false,
} | {
  serve: true,
  root: string | string[] | URL | URL[];
})

prefix?: string;
prefixAvoidTrailingSlash?: boolean;
serve?: boolean;
Expand Down
Loading