[RFE] @std/http/file_server - Allow to modify file contents on-the-fly
- Dominant language
- TypeScript
- Stars
- 3.6k
- Forks
- 681
- PR merge metrics
- No merged PRs in 30d
Description
**Is your feature request related to a problem? Please describe.**
It would be nice (i would like tobe able) if we could modify the file contents of certain files served "On-The-Fly", eg, before actual sending of the contents.
Excuse me for still being a total noob wrt `deno`, `TypeScript`, et all...
I would like to automatically inject, for example the below, inside the `` tag of html files that will be served:
-
```html
{ "polyfillEnable": ["typescript", "css-modules"] }
{
"imports": {
// This is just an example ofcourse, replace or add to your needs...
"vue": "https://ga.jspm.io/npm:vue@3.5.13/dist/vue.esm-browser.prod.js"
}
}
```
-
```html
blabla
...
```
**Describe the solution you'd like**
Somekind of hook/callback we could add to the function call, that modifies the text-data from the file before that data is send out, depending on file type and name.
-
```ts
// myFilter.mts
interface FilterParams {
fname: string,
fext: string,
fdata: Array; // One string per \n delimited line fe...
};
export const filter =
(resp:FilterParams): string[] => {
if (resp.fname === 'index'
&& resp.fext === "html"
) {
/*
Do some processing on resp.fdata
1. Find the 'head' element
2. Insert some html before the first script tag using Array() functionality.
3. Optionally add some SRI to respective tags, or some CSP.
*/
}
// resp.fdata will now hold the altered or unaltered file data.
return resp.fdata;
};
```
1. Usage inside a module:
```ts
// serve.ts
import { filter } from "./myFilter.mts";
import { serveDir } from "@std/http/file-server";
// Something like this, i don't know how to use this yet but you get the idea...
Deno.serve({
serveDir(req),
filter
});
```
1. Or from command line:
```console
$ file-server --filter=./myFilter.mts ./src
```
The above would process each request and serve the file(s) that is/are requested
- unaltered **UNLESS** it is intercepted and changed by the `filter`.
**Describe alternatives you've considered**
No idea yet, as i'm still in the process of reading `deno` docs and learning `TypeScript` 🤷♀
If what i describe is already possible please show me the way to do it with an example and if possible add it to the docs. 😉
Contributor guide
Assessment
This issue has not been assessed yet.