serving memory stored static files?
- Dominant language
- No language data
- Stars
- 68
- Forks
- 8
- Avg merge
- 11h 2m
- Merged PRs (30d)
- 2
Description
I felt that multiple times, I wanted to serve a firstful of static files. LIke favicon.ico, a css, a html, and a frontend js file. They make maybe around 1 - 2 mb. But using fastify-static feels wrong. I just want to serve these files over and over. They never change.
fastify-static is reading every time the file from the disk. everytime it generates the etag.
What I could do, is explicitly loading the files via fs.readFileSync and store them as buffer and just send them directly via the handler.
Of course I simplify my case very much. E.g. @fastify/send also handles etags and stuff. And compression is also not implemented. But well... its a poc.
For comparison the example/server.js in fastify-static.
```js
'use strict'
const fs = require('node:fs')
const path = require('node:path')
const fastify = require('fastify')({ logger: false })
const ondex = fs.readFileSync(path.join(__dirname, '/public/index.html'))
fastify
.register(require('../'), {
// An absolute path containing static files to serve.
root: path.join(__dirname, '/public')
})
.get('/ondex.html', (req, reply) => {
reply
.type('text/html')
.send(ondex)
})
.listen({ port: 3000 }, err => {
if (err) throw err
})
```
index.html (standard fastify-static)
```sh
aras@aras-Lenovo-Legion-5-17ARH05H:~/workspace/fastify-static$ autocannon http://localhost:3000/index.html
Running 10s test @ http://localhost:3000/index.html
10 connections
┌─────────┬──────┬──────┬───────┬──────┬─────────┬─────────┬───────┐
│ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
├─────────┼──────┼──────┼───────┼──────┼─────────┼─────────┼───────┤
│ Latency │ 0 ms │ 0 ms │ 3 ms │ 3 ms │ 0.28 ms │ 0.71 ms │ 24 ms │
└─────────┴──────┴──────┴───────┴──────┴─────────┴─────────┴───────┘
┌───────────┬─────────┬─────────┬────────┬─────────┬─────────┬─────────┬─────────┐
│ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
├───────────┼─────────┼─────────┼────────┼─────────┼─────────┼─────────┼─────────┤
│ Req/Sec │ 6103 │ 6103 │ 9991 │ 10831 │ 9738.6 │ 1271.02 │ 6101 │
├───────────┼─────────┼─────────┼────────┼─────────┼─────────┼─────────┼─────────┤
│ Bytes/Sec │ 3.11 MB │ 3.11 MB │ 5.1 MB │ 5.53 MB │ 4.97 MB │ 648 kB │ 3.11 MB │
└───────────┴─────────┴─────────┴────────┴─────────┴─────────┴─────────┴─────────┘
Req/Bytes counts sampled once per second.
# of samples: 10
97k requests in 10.02s, 49.7 MB read
```
ondex.html (just buffered)
```sh
aras@aras-Lenovo-Legion-5-17ARH05H:~/workspace/fastify-static$ autocannon http://localhost:3000/ondex.html
Running 10s test @ http://localhost:3000/ondex.html
10 connections
┌─────────┬──────┬──────┬───────┬──────┬─────────┬─────────┬───────┐
│ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
├─────────┼──────┼──────┼───────┼──────┼─────────┼─────────┼───────┤
│ Latency │ 0 ms │ 0 ms │ 0 ms │ 0 ms │ 0.01 ms │ 0.12 ms │ 28 ms │
└─────────┴──────┴──────┴───────┴──────┴─────────┴─────────┴───────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬─────────┬─────────┐
│ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼─────────┼─────────┤
│ Req/Sec │ 28239 │ 28239 │ 39263 │ 39519 │ 38208.73 │ 3170.32 │ 28237 │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼─────────┼─────────┤
│ Bytes/Sec │ 10.4 MB │ 10.4 MB │ 14.4 MB │ 14.5 MB │ 14 MB │ 1.16 MB │ 10.4 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴─────────┴─────────┘
Req/Bytes counts sampled once per second.
# of samples: 11
420k requests in 11.01s, 154 MB read
```
Contributor guide
Assessment
This issue has not been assessed yet.