Skip to content
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
5 changes: 4 additions & 1 deletion src/static_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ export default class StaticMiddleware {
* ```
*/
constructor(publicPath: string, config: AssetsConfig) {
const { dotFiles, ...rest } = config

this.#sendFile = staticServer(publicPath, {
...config,
...rest,
dotfiles: dotFiles,
fallthrough: true,
setHeaders: (res, path, stats) => {
const headers = res.parent!.getHeaders()
Expand Down
48 changes: 48 additions & 0 deletions tests/static_middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,54 @@ test.group('Serve Static', (group) => {
assert.equal(text, 'Route not found')
})

test('serve files inside dotfile directories when dotFiles is allow', async ({ assert }) => {
await fs.outputFile(
join(BASE_PATH, 'public/.well-known/apple-developer-merchantid-domain-association'),
'merchant-id-content'
)

const server = createServer(async (req, res) => {
const serveStatic = new StaticMiddleware(
join(BASE_PATH, 'public'),
defineConfig({ dotFiles: 'allow' })
)

const request = new RequestFactory().merge({ req, res }).create()
const response = new ResponseFactory().merge({ req, res }).create()
const ctx = new HttpContextFactory().merge({ request, response }).create()

await serveStatic.handle(ctx, () => {
ctx.response.status(404).send('404')
ctx.response.finish()
})
})

const res = await supertest(server)
.get('/.well-known/apple-developer-merchantid-domain-association')
.expect(200)

assert.equal(Buffer.from(res.body).toString(), 'merchant-id-content')
})

test('ignore dotfiles by default', async () => {
await fs.outputFile(join(BASE_PATH, 'public/.env'), 'SECRET=123')

const server = createServer(async (req, res) => {
const serveStatic = new StaticMiddleware(join(BASE_PATH, 'public'), defineConfig({}))

const request = new RequestFactory().merge({ req, res }).create()
const response = new ResponseFactory().merge({ req, res }).create()
const ctx = new HttpContextFactory().merge({ request, response }).create()

await serveStatic.handle(ctx, () => {
ctx.response.status(404).send('404')
ctx.response.finish()
})
})

await supertest(server).get('/.env').expect(404)
})

test('set headers defined via config', async ({ assert }) => {
await fs.outputFile(join(BASE_PATH, 'public/style.css'), 'body { background: #000 }')

Expand Down
Loading