Skip to content

Commit

Permalink
feat: support cjs and esm both by tshy
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257
  • Loading branch information
fengmk2 committed Jan 12, 2025
1 parent 1d964fc commit d139402
Show file tree
Hide file tree
Showing 17 changed files with 1,163 additions and 966 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/fixtures
coverage
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
16 changes: 16 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
Job:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
version: '18.19.0, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Any Commit
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Build
run: npm run prepublishOnly --if-present

- run: npx pkg-pr-new publish
13 changes: 13 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Release

on:
push:
branches: [ master ]

jobs:
release:
name: Node.js
uses: eggjs/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
15 changes: 10 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
.DS_Store*
ehthumbs.db
Thumbs.db
node_modules
logs/
npm-debug.log
coverage
node_modules/
coverage/
test/fixtures/**/run
.DS_Store
.tshy*
.eslintcache
dist
package-lock.json
.package-lock.json
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

File renamed without changes.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2025 - present eggjs and the contributors.
Copyright (c) 2013 Jonathan Ong [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 0 additions & 33 deletions Makefile

This file was deleted.

100 changes: 46 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
# Koa Static Cache

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![David deps][david-image]][david-url]

[npm-image]: https://img.shields.io/npm/v/koa-static-cache.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-static-cache
[travis-image]: https://img.shields.io/travis/koajs/static-cache.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/static-cache
[coveralls-image]: https://img.shields.io/coveralls/koajs/static-cache.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/koajs/static-cache?branch=master
[david-image]: https://img.shields.io/david/koajs/static-cache.svg?style=flat-square
[david-url]: https://david-dm.org/koajs/static-cache

Static server for koa.
[![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)
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/@eggjs/koa-static-cache.svg?style=flat)](https://nodejs.org/en/download/)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)

[npm-image]: https://img.shields.io/npm/v/@eggjs/koa-static-cache.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@eggjs/koa-static-cache
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/koa-static-cache.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/koa-static-cache?branch=master
[snyk-image]: https://snyk.io/test/npm/@eggjs/koa-static-cache/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/@eggjs/koa-static-cache
[download-image]: https://img.shields.io/npm/dm/@eggjs/koa-static-cache.svg?style=flat-square
[download-url]: https://npmjs.org/package/@eggjs/koa-static-cache

Static cache middleware for koa.

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

- There is no directory or `index.html` support.
- You may optionally store the data in memory - it streams by default.
- Caches the assets on initialization - you need to restart the process to update the assets.(can turn off with options.preload = false)
- Uses MD5 hash sum as an ETag.
- Uses .gz files if present on disk, like nginx gzip_static module
- Uses `.gz` files if present on disk, like nginx gzip_static module

> Forked from https://github.com/koajs/static-cache, refactor with TypeScript to support CommonJS and ESM both.
## Installation

```js
$ npm install koa-static-cache
```bash
npm install @eggjs/koa-static-cache
```

## API

### staticCache(dir [, options] [, files])
### staticCache([options])

```js
var path = require('path')
var staticCache = require('koa-static-cache')
const path = require('path');
const { staticCache } = require('@eggjs/koa-static-cache');

app.use(staticCache(path.join(__dirname, 'public'), {
maxAge: 365 * 24 * 60 * 60
}))
}));
```

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

### Aliases

For example, if you have this alias object:
For example, if you have this `alias` object:

```js
{
'/favicon.png': '/favicon-32.png'
const options = {
alias: {
'/favicon.png': '/favicon-32.png'
}
}
```

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

```js
var files = {}
const files = {};

// Mount the middleware
app.use(staticCache('/public/js', {}, files))
app.use(staticCache('/public/js', {}, files));

// Add additional files
staticCache('/public/css', {}, files)
staticCache('/public/css', {}, files);
```

The benefit is that you'll have one less function added to the stack as well as doing one hash lookup instead of two.
Expand All @@ -103,50 +109,36 @@ The benefit is that you'll have one less function added to the stack as well as
For example, if you want to change the max age of `/package.json`, you can do the following:

```js
var files = {}
const files = {};

app.use(staticCache('/public', {
maxAge: 60 * 60 * 24 * 365
}, files))
}, files));

files['/package.json'].maxAge = 60 * 60 * 24 * 30
files['/package.json'].maxAge = 60 * 60 * 24 * 30;
```

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

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

```js
var LRU = require('lru-cache')
var files = new LRU({ max: 1000 })
const LRU = require('lru-cache');
const files = new LRU({ max: 1000 });

app.use(staticCache({
dir: '/public',
dynamic: true,
files: files
}))
files,
}));
```

## License

The MIT License (MIT)

Copyright (c) 2013 Jonathan Ong [email protected]
[MIT](LICENSE)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
## Contributors

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
[![Contributors](https://contrib.rocks/image?repo=eggjs/koa-static-cache)](https://github.com/eggjs/koa-static-cache/graphs/contributors)

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Made with [contributors-img](https://contrib.rocks).
Loading

0 comments on commit d139402

Please sign in to comment.