Skip to content

Commit

Permalink
feat: support cjs and esm both by tshy (#54)
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

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced comprehensive support for TypeScript in project
configurations.
- Added new cookie management functionalities, including setting,
encryption, and validation.
  - Added support for `Keygrip` class for cryptographic operations.

- **Documentation**
- Updated package name in README files from `egg-cookies` to
`@eggjs/cookies`.
- Adjusted code snippets and URLs in documentation to reflect the new
package name.

- **Tests**
- Enhanced test suite with additional test cases for cookie encryption,
caching, and error handling.
  - Added new test files for `Keygrip` and cookie functionalities.
  
- **Chores**
  - Updated `.gitignore` to include new patterns for ignoring files.
- Improved CI workflow with updated Node.js versions and Codecov token
integration.
- Updated dependencies and scripts in `package.json` to align with the
new package structure and TypeScript support.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Jun 23, 2024
1 parent 2d22d6d commit 12db545
Show file tree
Hide file tree
Showing 27 changed files with 708 additions and 851 deletions.
8 changes: 4 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "eslint-config-egg",
"parserOptions": {
"ecmaVersion": 2017
}
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
68 changes: 0 additions & 68 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

5 changes: 3 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: CI
on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

Expand All @@ -13,4 +12,6 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest'
version: '10, 12, 14, 16, 18, 20'
version: '18.19.0, 18, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ node_modules
coverage
test/ts/report
package-lock.json
.tshy*
.eslintcache
dist
36 changes: 16 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
# egg-cookies
# @eggjs/cookies

[![NPM version][npm-image]][npm-url]
[![build status][ci-image]][ci-url]
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-cookies.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-cookies
[npm-image]: https://img.shields.io/npm/v/@eggjs/cookies.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@eggjs/cookies
[ci-image]: https://github.com/eggjs/egg-cookies/actions/workflows/nodejs.yml/badge.svg
[ci-url]: https://github.com/eggjs/egg-cookies/actions/workflows/nodejs.yml
[codecov-image]: https://codecov.io/gh/eggjs/egg-cookies/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/eggjs/egg-cookies
[download-image]: https://img.shields.io/npm/dm/egg-cookies.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-cookies
[download-image]: https://img.shields.io/npm/dm/@eggjs/cookies.svg?style=flat-square
[download-url]: https://npmjs.org/package/@eggjs/cookies

Extends [pillarjs/cookies](https://github.com/pillarjs/cookies) to adapt koa and egg with some additional features.

## Encrypt

egg-cookies provide an alternative `encrypt` mode like `signed`. An encrypt cookie's value will be encrypted base on keys. Anyone who don't have the keys are unable to know the original cookie's value.
`@eggjs/cookies` provide an alternative `encrypt` mode like `signed`. An encrypt cookie's value will be encrypted base on keys. Anyone who don't have the keys are unable to know the original cookie's value.

```ts
import { Cookies } from '@eggjs/cookies';

```js
const Cookies = require('egg-cookies');
const cookies = new Cookies(ctx, keys[, defaultCookieOptions]);

cookies.set('foo', 'bar', { encrypt: true });
Expand All @@ -32,13 +33,14 @@ cookies.get('foo', { encrypt: true });

## Cookie Length Check

[Browsers all had some limitation in cookie's length](http://browsercookielimits.squawky.net/), so if set a cookie with an extremely long value(> 4093), egg-cookies will emit an `cookieLimitExceed` event. You can listen to this event and record.
[Browsers all had some limitation in cookie's length](http://browsercookielimits.squawky.net/), so if set a cookie with an extremely long value(> 4093), `@eggjs/cookies` will emit an `cookieLimitExceed` event. You can listen to this event and record.

```ts
import { Cookies } from '@eggjs/cookies';

```js
const Cookies = require('egg-cookies');
const cookies = new Cookies(ctx, keys);

cookies.on('cookieLimitExceed', { name, value } => {
cookies.on('cookieLimitExceed', ({ name, value }) => {
// log
});

Expand All @@ -48,15 +50,9 @@ cookies.set('foo', longText);
## License

[MIT](LICENSE)
<!-- GITCONTRIBUTOR_START -->

## Contributors

|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>|[<img src="https://avatars.githubusercontent.com/u/5102113?v=4" width="100px;"/><br/><sub><b>xyeric</b></sub>](https://github.com/xyeric)<br/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[<img src="https://avatars.githubusercontent.com/u/3939959?v=4" width="100px;"/><br/><sub><b>jedmeng</b></sub>](https://github.com/jedmeng)<br/>|[<img src="https://avatars.githubusercontent.com/u/17005098?v=4" width="100px;"/><br/><sub><b>Junyan</b></sub>](https://github.com/Junyan)<br/>|[<img src="https://avatars.githubusercontent.com/u/12657964?v=4" width="100px;"/><br/><sub><b>beliefgp</b></sub>](https://github.com/beliefgp)<br/>|[<img src="https://avatars.githubusercontent.com/u/52845048?v=4" width="100px;"/><br/><sub><b>snapre</b></sub>](https://github.com/snapre)<br/>|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/22520131?v=4" width="100px;"/><br/><sub><b>tang-xy</b></sub>](https://github.com/tang-xy)<br/>|
[<img src="https://avatars.githubusercontent.com/u/1276859?v=4" width="100px;"/><br/><sub><b>ziyunfei</b></sub>](https://github.com/ziyunfei)<br/>|[<img src="https://avatars.githubusercontent.com/u/12656301?v=4" width="100px;"/><br/><sub><b>brizer</b></sub>](https://github.com/brizer)<br/>|[<img src="https://avatars.githubusercontent.com/u/7692708?v=4" width="100px;"/><br/><sub><b>xuezier</b></sub>](https://github.com/xuezier)<br/>|[<img src="https://avatars.githubusercontent.com/u/16103358?v=4" width="100px;"/><br/><sub><b>onlylovermb</b></sub>](https://github.com/onlylovermb)<br/>

This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Mon Feb 19 2024 17:41:40 GMT+0800`.
[![Contributors](https://contrib.rocks/image?repo=eggjs/egg-cookies)](https://github.com/eggjs/egg-cookies/graphs/contributors)

<!-- GITCONTRIBUTOR_END -->
Made with [contributors-img](https://contrib.rocks).
26 changes: 10 additions & 16 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# egg-cookies
# @eggjs/cookies

[![NPM version][npm-image]][npm-url]
[![build status][ci-image]][ci-url]
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-cookies.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-cookies
[npm-image]: https://img.shields.io/npm/v/@eggjs/cookies.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@eggjs/cookies
[ci-image]: https://github.com/eggjs/egg-cookies/actions/workflows/nodejs.yml/badge.svg
[ci-url]: https://github.com/eggjs/egg-cookies/actions/workflows/nodejs.yml
[codecov-image]: https://codecov.io/gh/eggjs/egg-cookies/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/eggjs/egg-cookies
[download-image]: https://img.shields.io/npm/dm/egg-cookies.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-cookies
[download-image]: https://img.shields.io/npm/dm/@eggjs/cookies.svg?style=flat-square
[download-url]: https://npmjs.org/package/@eggjs/cookies

为 egg 提供 cookie 操作的封装。

```js
```ts
ctx.cookies = new Cookies(ctx, keys[, defaultCookieOptions]);

ctx.cookies.get('key', 'value', options);
ctx.cookies.set('key', 'value', options);
```
Expand Down Expand Up @@ -68,15 +69,8 @@ ctx.cookies.set('key', 'value', options);

[MIT](LICENSE)

<!-- GITCONTRIBUTOR_START -->

## 贡献者

|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>|[<img src="https://avatars.githubusercontent.com/u/5102113?v=4" width="100px;"/><br/><sub><b>xyeric</b></sub>](https://github.com/xyeric)<br/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[<img src="https://avatars.githubusercontent.com/u/3939959?v=4" width="100px;"/><br/><sub><b>jedmeng</b></sub>](https://github.com/jedmeng)<br/>|[<img src="https://avatars.githubusercontent.com/u/17005098?v=4" width="100px;"/><br/><sub><b>Junyan</b></sub>](https://github.com/Junyan)<br/>|[<img src="https://avatars.githubusercontent.com/u/12657964?v=4" width="100px;"/><br/><sub><b>beliefgp</b></sub>](https://github.com/beliefgp)<br/>|[<img src="https://avatars.githubusercontent.com/u/52845048?v=4" width="100px;"/><br/><sub><b>snapre</b></sub>](https://github.com/snapre)<br/>|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/22520131?v=4" width="100px;"/><br/><sub><b>tang-xy</b></sub>](https://github.com/tang-xy)<br/>|
[<img src="https://avatars.githubusercontent.com/u/1276859?v=4" width="100px;"/><br/><sub><b>ziyunfei</b></sub>](https://github.com/ziyunfei)<br/>|[<img src="https://avatars.githubusercontent.com/u/12656301?v=4" width="100px;"/><br/><sub><b>brizer</b></sub>](https://github.com/brizer)<br/>|[<img src="https://avatars.githubusercontent.com/u/7692708?v=4" width="100px;"/><br/><sub><b>xuezier</b></sub>](https://github.com/xuezier)<br/>|[<img src="https://avatars.githubusercontent.com/u/16103358?v=4" width="100px;"/><br/><sub><b>onlylovermb</b></sub>](https://github.com/onlylovermb)<br/>
## Contributors

[git-contributor 说明](https://github.com/xudafeng/git-contributor),自动生成时间:`Mon Feb 19 2024 17:41:40 GMT+0800`
[![Contributors](https://contrib.rocks/image?repo=eggjs/egg-cookies)](https://github.com/eggjs/egg-cookies/graphs/contributors)

<!-- GITCONTRIBUTOR_END -->
Made with [contributors-img](https://contrib.rocks).
23 changes: 20 additions & 3 deletions benchmark/index.js → benchmark/index.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

const Benchmark = require('benchmark');
const benchmarks = require('beautify-benchmark');
const Cookies = require('cookies');
const Keygrip = require('keygrip');
const EggCookies = require('..');
const { Cookies: EggCookies } = require('..');

const suite = new Benchmark.Suite();

Expand Down Expand Up @@ -125,6 +123,25 @@ function createCookie() {
return new Cookies(ctx.req, ctx.res, { keys: keygrip });
}

// v3
// node version: v22.3.0, date: Sun Jun 23 2024 17:19:38 GMT+0800 (中国标准时间)
// Starting...
// 12 tests completed.

// create EggCookie x 26,305,561 ops/sec ±3.41% (87 runs sampled)
// create Cookie x 18,373,466 ops/sec ±8.96% (81 runs sampled)
// EggCookies.set with signed x 453,511 ops/sec ±1.37% (97 runs sampled)
// Cookies.set with signed x 442,143 ops/sec ±2.48% (92 runs sampled)
// EggCookies.set without signed x 4,644,441 ops/sec ±2.05% (95 runs sampled)
// Cookies.set without signed x 4,055,903 ops/sec ±0.14% (98 runs sampled)
// EggCookies.set with encrypt x 477,018 ops/sec ±1.10% (97 runs sampled)
// EggCookies.get with signed x 367,708 ops/sec ±0.29% (92 runs sampled)
// Cookies.get with signed x 133,608 ops/sec ±4.24% (87 runs sampled)
// EggCookies.get without signed x 9,233,880 ops/sec ±8.40% (86 runs sampled)
// Cookies.get without signed x 7,135,602 ops/sec ±4.52% (86 runs sampled)
// EggCookies.get with encrypt x 423,227 ops/sec ±2.52% (89 runs sampled)

// v2
// create EggCookie x 6,892,450 ops/sec ±1.19% (85 runs sampled)
// create Cookie x 3,885,528 ops/sec ±1.07% (84 runs sampled)
// EggCookies.set with signed x 87,470 ops/sec ±1.63% (84 runs sampled)
Expand Down
106 changes: 0 additions & 106 deletions index.d.ts

This file was deleted.

Loading

0 comments on commit 12db545

Please sign in to comment.