lukeed / sirv

An optimized middleware & CLI application for serving static files~!
MIT License
1.07k stars 58 forks source link

Support for "." in pathnames #132 #133

Open patrikpessi opened 2 years ago

patrikpessi commented 2 years ago

132

lukeed commented 2 years ago

This is just allowing numbers with periods, specific to your use case

patrikpessi commented 2 years ago

I wouldn't say this is specific for my project since some packages (eg. express) seem to handle such routes aswell. Working example using express:

const fs = require("fs")
const express = require("express")
const app = express()

app.use(express.static("public"))

app.get("*", (req, res) => {
    const html = fs.readFileSync("public/index.html")
    res.send(html.toString())
})

app.listen(3000)
lukeed commented 2 years ago

Right, I’m not saying the need is specific to you (should be done) but this fix is specifically only allowing paths with dot-separated numbers, which is your use case.

patrikpessi commented 2 years ago

I tested it on regexr and it should also allow paths with word characters aswell. Here are the tests I used. Please let me know if there is some edge case here that I'm missing?

lukeed commented 2 years ago

It stops matching URLs like /foo/bar.mp3 and /foo/bar.txt.mp3 because there's a digit in the final extension segment. This is what I meant originally – the RegExp is now specifically targeting digits, which is your use case

patrikpessi commented 2 years ago

Updated the regex for better file name/extension matching Here is another link to tests used for the regex: https://regexr.com/6fv5m

patrikpessi commented 2 years ago

@lukeed Toughts?