@wooksjs/http-static 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -0
- package/dist/index.cjs +1397 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.mjs +1395 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Wooks Serve File
|
|
2
|
+
|
|
3
|
+
**!!! This is work-in-progress library, breaking changes are expected !!!**
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<img src="../../logo.png" width="128px"><br>
|
|
7
|
+
<a href="https://github.com/prostojs/wooks/blob/main/LICENSE">
|
|
8
|
+
<img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" />
|
|
9
|
+
</a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
Wooks Serve File is composable static file server for [@wooksjs/event-http](https://github.com/wooksjs/wooksjs/tree/main/packages/event-http).
|
|
14
|
+
|
|
15
|
+
`serveFile` returns a readable stream and prepares all the neccessary response headers (like content-length, content-type etc).
|
|
16
|
+
|
|
17
|
+
- ✅ returns a readable stream
|
|
18
|
+
- ✅ prepares all the neccessary response headers (like content-length, content-type etc)
|
|
19
|
+
- ✅ can handle etag
|
|
20
|
+
- ✅ can handle ranges
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
`npm install @wooksjs/http-static`
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
import { serveFile } from '@wooksjs/http-static'
|
|
31
|
+
// ...
|
|
32
|
+
serveFile(filePath, options)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**serveFile options**
|
|
36
|
+
```ts
|
|
37
|
+
{
|
|
38
|
+
// Any header to add
|
|
39
|
+
headers?: Record<string, string>,
|
|
40
|
+
|
|
41
|
+
// Cache-Control header
|
|
42
|
+
cacheControl?: TCacheControl,
|
|
43
|
+
|
|
44
|
+
// Expires header
|
|
45
|
+
expires?: Date | string | number,
|
|
46
|
+
|
|
47
|
+
// when true a header "Pragma: no-cache" will be added
|
|
48
|
+
pragmaNoCache?: boolean,
|
|
49
|
+
|
|
50
|
+
// the base directory path
|
|
51
|
+
baseDir?: string,
|
|
52
|
+
|
|
53
|
+
// default extension will be added to the filePath
|
|
54
|
+
defaultExt?: string,
|
|
55
|
+
|
|
56
|
+
// when true lists files in directory
|
|
57
|
+
listDirectory?: boolean,
|
|
58
|
+
|
|
59
|
+
// put 'index.html'
|
|
60
|
+
// to automatically serve it from the folder
|
|
61
|
+
index?: string,
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Built-in file server example:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
import { useRouteParams } from 'wooks'
|
|
69
|
+
import { serveFile } from '@wooksjs/http-static'
|
|
70
|
+
app.get('static/*', () => {
|
|
71
|
+
const { get } = useRouteParams()
|
|
72
|
+
return serveFile(get('*'), { cacheControl: { maxAge: '10m' } })
|
|
73
|
+
})
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`cacheControl` here is the same object as used in `useSetCacheControl().setCacheControl({ ... })` from `@wooksjs/composables`
|
|
77
|
+
|