1
1
# Koa Static Cache
2
2
3
3
[ ![ NPM version] [ npm-image ]] [ npm-url ]
4
- [ ![ build status] [ travis-image ]] [ travis-url ]
5
- [ ![ Test coverage] [ coveralls-image ]] [ coveralls-url ]
6
- [ ![ David deps] [ david-image ]] [ david-url ]
7
-
8
- [ npm-image ] : https://img.shields.io/npm/v/koa-static-cache.svg?style=flat-square
9
- [ npm-url ] : https://npmjs.org/package/koa-static-cache
10
- [ travis-image ] : https://img.shields.io/travis/koajs/static-cache.svg?style=flat-square
11
- [ travis-url ] : https://travis-ci.org/koajs/static-cache
12
- [ coveralls-image ] : https://img.shields.io/coveralls/koajs/static-cache.svg?style=flat-square
13
- [ coveralls-url ] : https://coveralls.io/r/koajs/static-cache?branch=master
14
- [ david-image ] : https://img.shields.io/david/koajs/static-cache.svg?style=flat-square
15
- [ david-url ] : https://david-dm.org/koajs/static-cache
16
-
17
- Static server for koa.
4
+ [ ![ Node.js CI] ( https://github.com/eggjs/koa-static-cache/actions/workflows/nodejs.yml/badge.svg )] ( https://github.com/eggjs/koa-static-cache/actions/workflows/nodejs.yml )
5
+ [ ![ Test coverage] [ codecov-image ]] [ codecov-url ]
6
+ [ ![ Known Vulnerabilities] [ snyk-image ]] [ snyk-url ]
7
+ [ ![ npm download] [ download-image ]] [ download-url ]
8
+ [ ![ Node.js Version] ( https://img.shields.io/node/v/@eggjs/koa-static-cache.svg?style=flat )] ( https://nodejs.org/en/download/ )
9
+ [ ![ PRs Welcome] ( https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square )] ( https://makeapullrequest.com )
10
+
11
+ [ npm-image ] : https://img.shields.io/npm/v/@eggjs/koa-static-cache.svg?style=flat-square
12
+ [ npm-url ] : https://npmjs.org/package/@eggjs/koa-static-cache
13
+ [ codecov-image ] : https://img.shields.io/codecov/c/github/eggjs/koa-static-cache.svg?style=flat-square
14
+ [ codecov-url ] : https://codecov.io/github/eggjs/koa-static-cache?branch=master
15
+ [ snyk-image ] : https://snyk.io/test/npm/@eggjs/koa-static-cache/badge.svg?style=flat-square
16
+ [ snyk-url ] : https://snyk.io/test/npm/@eggjs/koa-static-cache
17
+ [ download-image ] : https://img.shields.io/npm/dm/@eggjs/koa-static-cache.svg?style=flat-square
18
+ [ download-url ] : https://npmjs.org/package/@eggjs/koa-static-cache
19
+
20
+ Static cache middleware for koa.
18
21
19
22
Differences between this library and other libraries such as [ static] ( https://github.com/koajs/static ) :
20
23
21
24
- There is no directory or ` index.html ` support.
22
25
- You may optionally store the data in memory - it streams by default.
23
26
- Caches the assets on initialization - you need to restart the process to update the assets.(can turn off with options.preload = false)
24
27
- Uses MD5 hash sum as an ETag.
25
- - Uses .gz files if present on disk, like nginx gzip_static module
28
+ - Uses ` .gz ` files if present on disk, like nginx gzip_static module
29
+
30
+ > Forked from https://github.com/koajs/static-cache , refactor with TypeScript to support CommonJS and ESM both.
26
31
27
32
## Installation
28
33
29
- ``` js
30
- $ npm install koa- static - cache
34
+ ``` bash
35
+ npm install @eggjs/ koa-static-cache
31
36
```
32
37
33
38
## API
34
39
35
- ### staticCache(dir [ , options] [ , files ] )
40
+ ### staticCache([ options] )
36
41
37
42
``` js
38
- var path = require (' path' )
39
- var staticCache = require (' koa-static-cache' )
43
+ const path = require (' path' );
44
+ const { staticCache } = require (' @eggjs/ koa-static-cache' );
40
45
41
46
app .use (staticCache (path .join (__dirname , ' public' ), {
42
47
maxAge: 365 * 24 * 60 * 60
43
- }))
48
+ }));
44
49
```
45
50
46
- - ` dir ` (str) - the directory you wish to serve, priority than ` options.dir ` .
47
51
- ` options.dir ` (str) - the directory you wish to serve, default to ` process.cwd ` .
48
52
- ` options.maxAge ` (int) - cache control max age for the files, ` 0 ` by default.
49
53
- ` options.cacheControl ` (str) - optional cache control header. Overrides ` options.maxAge ` .
@@ -56,14 +60,16 @@ app.use(staticCache(path.join(__dirname, 'public'), {
56
60
- ` options.filter ` (function | array) - filter files at init dir, for example - skip non build (source) files. If array set - allow only listed files
57
61
- ` options.preload ` (bool) - caches the assets on initialization or not, default to ` true ` . always work together with ` options.dynamic ` .
58
62
- ` options.files ` (obj) - optional files object. See below.
59
- - ` files ` (obj) - optional files object. See below.
63
+
60
64
### Aliases
61
65
62
- For example, if you have this alias object:
66
+ For example, if you have this ` alias ` object:
63
67
64
68
``` js
65
- {
66
- ' /favicon.png' : ' /favicon-32.png'
69
+ const options = {
70
+ alias: {
71
+ ' /favicon.png' : ' /favicon-32.png'
72
+ }
67
73
}
68
74
```
69
75
@@ -87,13 +93,13 @@ app.use(staticCache('/public/css'))
87
93
You can do this:
88
94
89
95
``` js
90
- var files = {}
96
+ const files = {};
91
97
92
98
// Mount the middleware
93
- app .use (staticCache (' /public/js' , {}, files))
99
+ app .use (staticCache (' /public/js' , {}, files));
94
100
95
101
// Add additional files
96
- staticCache (' /public/css' , {}, files)
102
+ staticCache (' /public/css' , {}, files);
97
103
```
98
104
99
105
The benefit is that you'll have one less function added to the stack as well as doing one hash lookup instead of two.
@@ -103,50 +109,36 @@ The benefit is that you'll have one less function added to the stack as well as
103
109
For example, if you want to change the max age of ` /package.json ` , you can do the following:
104
110
105
111
``` js
106
- var files = {}
112
+ const files = {};
107
113
108
114
app .use (staticCache (' /public' , {
109
115
maxAge: 60 * 60 * 24 * 365
110
- }, files))
116
+ }, files));
111
117
112
- files[' /package.json' ].maxAge = 60 * 60 * 24 * 30
118
+ files[' /package.json' ].maxAge = 60 * 60 * 24 * 30 ;
113
119
```
114
120
115
121
#### Using a LRU cache to avoid OOM when dynamic mode enabled
116
122
117
123
You can pass in a lru cache instance which has tow methods: ` get(key) ` and ` set(key, value) ` .
118
124
119
125
``` js
120
- var LRU = require (' lru-cache' )
121
- var files = new LRU ({ max: 1000 })
126
+ const LRU = require (' lru-cache' );
127
+ const files = new LRU ({ max: 1000 });
122
128
123
129
app .use (staticCache ({
124
130
dir: ' /public' ,
125
131
dynamic: true ,
126
- files: files
127
- }))
132
+ files,
133
+ }));
128
134
```
129
135
130
136
## License
131
137
132
- The MIT License (MIT)
133
-
134
- Copyright (c) 2013 Jonathan Ong
[email protected]
138
+ [ MIT] ( LICENSE )
135
139
136
- Permission is hereby granted, free of charge, to any person obtaining a copy
137
- of this software and associated documentation files (the "Software"), to deal
138
- in the Software without restriction, including without limitation the rights
139
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
140
- copies of the Software, and to permit persons to whom the Software is
141
- furnished to do so, subject to the following conditions:
140
+ ## Contributors
142
141
143
- The above copyright notice and this permission notice shall be included in
144
- all copies or substantial portions of the Software.
142
+ [ ![ Contributors] ( https://contrib.rocks/image?repo=eggjs/koa-static-cache )] ( https://github.com/eggjs/koa-static-cache/graphs/contributors )
145
143
146
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
147
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
148
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
149
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
150
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
151
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
152
- THE SOFTWARE.
144
+ Made with [ contributors-img] ( https://contrib.rocks ) .
0 commit comments