Skip to content

Commit

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

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

## Release Notes

- **Package Upgrade**
	- Renamed package from `egg-session` to `@eggjs/session`
	- Updated Node.js compatibility to version 18.19.0+

- **New Features**
	- Enhanced session configuration with improved type safety
	- Added support for more granular session management
	- Improved logging and security configurations

- **Breaking Changes**
	- Dropped support for Node.js versions below 18.19.0
	- Migrated from generator functions to async/await syntax
	- Updated session middleware and configuration structure

- **Performance**
	- Updated dependencies, including `koa-session` to version 7.0.2
	- Optimized session store handling

- **Security**
	- Strengthened default session configurations
	- Added warnings for potential security risks in session settings
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
fengmk2 authored Jan 19, 2025
1 parent f57f24e commit b1a96e5
Showing 36 changed files with 397 additions and 276 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
test/fixtures
coverage
__snapshots__
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "eslint-config-egg"
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
3 changes: 1 addition & 2 deletions .github/workflows/node.yml → .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ jobs:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test-mysql.yml@master
with:
os: 'ubuntu-latest'
version: '14, 16, 18, 20, 22'
version: '18.19.0, 18, 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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -3,5 +3,9 @@ npm-debug.log
node_modules/
coverage/
test/fixtures/**/run
dump.rdb
.DS_Store
.tshy*
.eslintcache
dist
package-lock.json
.package-lock.json
File renamed without changes.
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
# egg-session
# @eggjs/session

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/eggjs/session/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/session/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-session.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-session
[codecov-image]: https://codecov.io/github/eggjs/egg-session/coverage.svg?branch=master
[codecov-url]: https://codecov.io/github/eggjs/egg-session?branch=master
[download-image]: https://img.shields.io/npm/dm/egg-session.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-session
[![Node.js Version](https://img.shields.io/node/v/@eggjs/session.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)
![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/eggjs/session)

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

Session plugin for egg, based on [koa-session](https://github.com/koajs/session).

## Install

```bash
npm i egg-session --save
npm i @eggjs/session
```

## Usage
@@ -118,7 +125,7 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).

## License

[MIT](https://github.com/eggjs/egg-session/blob/master/LICENSE)
[MIT](LICENSE)

## Contributors

12 changes: 12 additions & 0 deletions __snapshots__/session.test.ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports['test/app/middlewares/session.test.js sessionStore should keep session config stable 1'] = {
"key": "EGG_SESS",
"maxAge": 86400000,
"autoCommit": true,
"overwrite": true,
"httpOnly": true,
"signed": true,
"rolling": false,
"renew": false,
"logValue": true,
"store": {}
}
19 changes: 0 additions & 19 deletions app.js

This file was deleted.

55 changes: 0 additions & 55 deletions app/extend/application.js

This file was deleted.

3 changes: 0 additions & 3 deletions app/middleware/session.js

This file was deleted.

10 changes: 0 additions & 10 deletions config/config.default.js

This file was deleted.

92 changes: 68 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,92 @@
{
"name": "egg-session",
"name": "@eggjs/session",
"version": "3.3.0",
"publishConfig": {
"access": "public"
},
"description": "session plugin for egg",
"eggPlugin": {
"name": "session"
"name": "session",
"exports": {
"import": "./dist/esm",
"require": "./dist/commonjs",
"typescript": "./src"
}
},
"files": [
"config",
"app",
"app.js"
],
"repository": {
"type": "git",
"url": "git@github.com:eggjs/session.git"
},
"author": "dead_horse",
"keywords": [
"egg",
"egg-plugin",
"eggPlugin",
"session",
"cookie"
],
"engines": {
"node": ">= 18.19.0"
},
"dependencies": {
"koa-session": "^6.0.0"
"@eggjs/core": "^6.2.13",
"koa-session": "^7.0.2",
"zod": "^3.24.1"
},
"devDependencies": {
"egg": "^3.17.5",
"egg-bin": "6",
"egg-mock": "5",
"egg-redis": "^1.0.1",
"@arethetypeswrong/cli": "^0.17.1",
"@eggjs/bin": "7",
"@eggjs/mock": "^6.0.5",
"@eggjs/supertest": "^8.2.0",
"@eggjs/tsconfig": "1",
"@types/mocha": "10",
"@types/node": "22",
"egg": "^4.0.1",
"egg-redis": "^2.6.0",
"eslint": "8",
"eslint-config-egg": "12",
"mz-modules": "^2.0.0",
"supertest": "^3.0.0"
},
"engines": {
"node": ">=8.0.0"
"eslint-config-egg": "14",
"rimraf": "6",
"snap-shot-it": "^7.9.10",
"tshy": "3",
"tshy-after": "1",
"typescript": "5"
},
"scripts": {
"lint": "eslint .",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test --ts false",
"cov": "egg-bin cov --ts false",
"ci": "npm run lint && npm run cov"
"lint": "eslint --cache src test --ext .ts",
"pretest": "npm run clean && npm run lint -- --fix",
"test": "egg-bin test",
"test:snapshot:update": "SNAPSHOT_UPDATE=1 egg-bin test",
"preci": "npm run clean && npm run lint",
"ci": "egg-bin cov",
"postci": "npm run prepublishOnly && npm run clean",
"clean": "rimraf dist",
"prepublishOnly": "tshy && tshy-after && attw --pack"
},
"type": "module",
"tshy": {
"exports": {
".": "./src/index.ts",
"./package.json": "./package.json"
}
},
"exports": {
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
},
"./package.json": "./package.json"
},
"author": "dead_horse"
"files": [
"dist",
"src"
],
"types": "./dist/commonjs/index.d.ts",
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js"
}
30 changes: 30 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ILifecycleBoot, EggCore } from '@eggjs/core';
import { SessionConfig } from './config/config.default.js';

export default class AppBoot implements ILifecycleBoot {
private readonly app;

constructor(app: EggCore) {
this.app = app;
}

configWillLoad() {
const app = this.app;
SessionConfig.parse(app.config.session);

if (!app.config.session.httpOnly) {
app.coreLogger.warn('[@eggjs/session]: please set `config.session.httpOnly` to true. It is very dangerous if session can read by client JavaScript.');
}
app.config.coreMiddleware.push('session');
// listen on session's events
app.on('session:missed', ({ ctx, key }) => {
ctx.coreLogger.warn('[session][missed] key(%s)', key);
});
app.on('session:expired', ({ ctx, key, value }) => {
ctx.coreLogger.warn('[session][expired] key(%s) value(%j)', key, app.config.session.logValue ? value : '');
});
app.on('session:invalid', ({ ctx, key, value }) => {
ctx.coreLogger.warn('[session][invalid] key(%s) value(%j)', key, app.config.session.logValue ? value : '');
});
}
}
Loading

0 comments on commit b1a96e5

Please sign in to comment.