Skip to content

Commit b0524d9

Browse files
authored
feat: support cjs and esm both by tshy (#1)
BREAKING CHANGE: drop Node.js < 18.19.0 support part of eggjs/egg#3644 eggjs/egg#5257 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added static cache middleware for Koa. - Introduced TypeScript support for the package. - Implemented comprehensive configuration for package management. - **Infrastructure** - Updated GitHub Actions workflows for CI/CD. - Added ESLint configuration. - Updated project build and testing configurations. - **Documentation** - Refreshed README with new badges and installation instructions. - Updated package description and licensing. - **Maintenance** - Upgraded Node.js engine support to version 18.19.0+. - Migrated package to `@eggjs/koa-static-cache`. - Removed legacy Travis CI configuration. - Added new TypeScript configuration file. - Removed unnecessary files and configurations, streamlining the project structure. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 1d964fc commit b0524d9

17 files changed

+1178
-966
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test/fixtures
2+
coverage

.eslintrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": [
3+
"eslint-config-egg/typescript",
4+
"eslint-config-egg/lib/rules/enforce-node-prefix"
5+
]
6+
}

.github/workflows/nodejs.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
Job:
11+
name: Node.js
12+
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
13+
with:
14+
version: '18.19.0, 20, 22'
15+
secrets:
16+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/pkg.pr.new.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Publish Any Commit
2+
on: [push, pull_request]
3+
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
8+
steps:
9+
- name: Checkout code
10+
uses: actions/checkout@v4
11+
12+
- run: corepack enable
13+
- uses: actions/setup-node@v4
14+
with:
15+
node-version: 20
16+
17+
- name: Install dependencies
18+
run: npm install
19+
20+
- name: Build
21+
run: npm run prepublishOnly --if-present
22+
23+
- run: npx pkg-pr-new publish

.github/workflows/release.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
7+
jobs:
8+
release:
9+
name: Node.js
10+
uses: eggjs/github-actions/.github/workflows/node-release.yml@master
11+
secrets:
12+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
13+
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}

.gitignore

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
.DS_Store*
2-
ehthumbs.db
3-
Thumbs.db
4-
node_modules
1+
logs/
52
npm-debug.log
6-
coverage
3+
node_modules/
4+
coverage/
5+
test/fixtures/**/run
6+
.DS_Store
7+
.tshy*
8+
.eslintcache
9+
dist
10+
package-lock.json
11+
.package-lock.json

.travis.yml

-8
This file was deleted.

HISTORY.md CHANGELOG.md

File renamed without changes.

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2025 - present eggjs and the contributors.
4+
Copyright (c) 2013 Jonathan Ong [email protected]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

Makefile

-33
This file was deleted.

README.md

+46-54
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,53 @@
11
# Koa Static Cache
22

33
[![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.
1821

1922
Differences between this library and other libraries such as [static](https://github.com/koajs/static):
2023

2124
- There is no directory or `index.html` support.
2225
- You may optionally store the data in memory - it streams by default.
2326
- Caches the assets on initialization - you need to restart the process to update the assets.(can turn off with options.preload = false)
2427
- 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.
2631
2732
## Installation
2833

29-
```js
30-
$ npm install koa-static-cache
34+
```bash
35+
npm install @eggjs/koa-static-cache
3136
```
3237

3338
## API
3439

35-
### staticCache(dir [, options] [, files])
40+
### staticCache([options])
3641

3742
```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');
4045

4146
app.use(staticCache(path.join(__dirname, 'public'), {
4247
maxAge: 365 * 24 * 60 * 60
43-
}))
48+
}));
4449
```
4550

46-
- `dir` (str) - the directory you wish to serve, priority than `options.dir`.
4751
- `options.dir` (str) - the directory you wish to serve, default to `process.cwd`.
4852
- `options.maxAge` (int) - cache control max age for the files, `0` by default.
4953
- `options.cacheControl` (str) - optional cache control header. Overrides `options.maxAge`.
@@ -56,14 +60,16 @@ app.use(staticCache(path.join(__dirname, 'public'), {
5660
- `options.filter` (function | array) - filter files at init dir, for example - skip non build (source) files. If array set - allow only listed files
5761
- `options.preload` (bool) - caches the assets on initialization or not, default to `true`. always work together with `options.dynamic`.
5862
- `options.files` (obj) - optional files object. See below.
59-
- `files` (obj) - optional files object. See below.
63+
6064
### Aliases
6165

62-
For example, if you have this alias object:
66+
For example, if you have this `alias` object:
6367

6468
```js
65-
{
66-
'/favicon.png': '/favicon-32.png'
69+
const options = {
70+
alias: {
71+
'/favicon.png': '/favicon-32.png'
72+
}
6773
}
6874
```
6975

@@ -87,13 +93,13 @@ app.use(staticCache('/public/css'))
8793
You can do this:
8894

8995
```js
90-
var files = {}
96+
const files = {};
9197

9298
// Mount the middleware
93-
app.use(staticCache('/public/js', {}, files))
99+
app.use(staticCache('/public/js', {}, files));
94100

95101
// Add additional files
96-
staticCache('/public/css', {}, files)
102+
staticCache('/public/css', {}, files);
97103
```
98104

99105
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
103109
For example, if you want to change the max age of `/package.json`, you can do the following:
104110

105111
```js
106-
var files = {}
112+
const files = {};
107113

108114
app.use(staticCache('/public', {
109115
maxAge: 60 * 60 * 24 * 365
110-
}, files))
116+
}, files));
111117

112-
files['/package.json'].maxAge = 60 * 60 * 24 * 30
118+
files['/package.json'].maxAge = 60 * 60 * 24 * 30;
113119
```
114120

115121
#### Using a LRU cache to avoid OOM when dynamic mode enabled
116122

117123
You can pass in a lru cache instance which has tow methods: `get(key)` and `set(key, value)`.
118124

119125
```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 });
122128

123129
app.use(staticCache({
124130
dir: '/public',
125131
dynamic: true,
126-
files: files
127-
}))
132+
files,
133+
}));
128134
```
129135

130136
## License
131137

132-
The MIT License (MIT)
133-
134-
Copyright (c) 2013 Jonathan Ong [email protected]
138+
[MIT](LICENSE)
135139

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
142141

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)
145143

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

Comments
 (0)