Skip to content

Commit

Permalink
feat: support cjs and esm both by tshy (#228)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop Node.js < 18.19.0 support
  • Loading branch information
fengmk2 authored Jan 19, 2025
1 parent 98630aa commit 575864c
Show file tree
Hide file tree
Showing 32 changed files with 3,258 additions and 3,308 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"
]
}
32 changes: 11 additions & 21 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
name: Node.js CI
name: CI

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

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [8, 10, 12, 14, 16, 18]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run lint
- run: npm run ci
- run: npx codecov
Job:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest, windows-latest'
version: '18.19.0, 18, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
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: koajs/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules
coverage
*.log
.tshy*
.eslintcache
dist
File renamed without changes.
80 changes: 47 additions & 33 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,45 @@

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/koajs/session/actions/workflows/nodejs.yml/badge.svg)](https://github.com/koajs/session/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/koajs/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)

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

Simple session middleware for Koa. Defaults to cookie-based sessions and supports external stores.

*Requires Node 8.0.0 or greater for async/await support*
Simple session middleware for Koa. Defaults to cookie-based sessions and supports external stores.

## Installation

```js
$ npm install koa-session
```bash
npm install koa-session
```

## Notice

6.x changed the default cookie key from `koa:sess` to `koa.sess` to ensure `set-cookie` value valid with HTTP spec.[see issue](https://github.com/koajs/session/issues/28). If you want to be compatible with the previous version, you can manually set `config.key` to `koa:sess`.
7.x has a breaking change: drop Node.js < 18.19.0 support. And it support CommonJS and ESM both.

6.x changed the default cookie key from `koa:sess` to `koa.sess` to ensure `set-cookie` value valid with HTTP spec.
[See issue](https://github.com/koajs/session/issues/28).
If you want to be compatible with the previous version, you can manually set `config.key` to `koa:sess`.

## Example

View counter example:
View counter example:

```js
const session = require('koa-session');
const Koa = require('koa');
import Koa from 'koa';
import session from 'koa-session';

const app = new Koa();

app.keys = ['some secret hurr'];
Expand Down Expand Up @@ -70,38 +81,35 @@ console.log('listening on port 3000');

### Options

The cookie name is controlled by the `key` option, which defaults
to "koa.sess". All other options are passed to `ctx.cookies.get()` and
`ctx.cookies.set()` allowing you to control security, domain, path,
and signing among other settings.
The cookie name is controlled by the `key` option, which defaults
to "koa.sess". All other options are passed to `ctx.cookies.get()` and
`ctx.cookies.set()` allowing you to control security, domain, path,
and signing among other settings.

#### Custom `encode/decode` Support

Use `options.encode` and `options.decode` to customize your own encode/decode methods.
Use `options.encode` and `options.decode` to customize your own encode/decode methods.

### Hooks

- `valid()`: valid session value before use it
- `beforeSave()`: hook before save session
- `valid()`: valid session value before use it
- `beforeSave()`: hook before save session

### External Session Stores

The session is stored in a cookie by default, but it has some disadvantages:

- Session is stored on client side unencrypted
- [Browser cookies always have length limits](http://browsercookielimits.squawky.net/)
The session is stored in a cookie by default, but it has some disadvantages:

- Session is stored on client side unencrypted
- [Browser cookies always have length limits](http://browsercookielimits.squawky.net/)

You can store the session content in external stores (Redis, MongoDB or other DBs) by passing `options.store` with three methods (these need to be async functions):

- `get(key, maxAge, { rolling, ctx })`: get session object by key
- `set(key, sess, maxAge, { rolling, changed, ctx })`: set session object for key, with a `maxAge` (in ms)
- `destroy(key, {ctx})`: destroy session for key

- `get(key, maxAge, { rolling, ctx })`: get session object by key
- `set(key, sess, maxAge, { rolling, changed, ctx })`: set session object for key, with a `maxAge` (in ms)
- `destroy(key, {ctx})`: destroy session for key

Once you pass `options.store`, session storage is dependent on your external store -- you can't access the session if your external store is down. **Use external session stores only if necessary, avoid using session as a cache, keep the session lean, and store it in a cookie if possible!**


The way of generating external session id is controlled by the `options.genid(ctx)`, which defaults to `uuid.v4()`.

If you want to add prefix for all external session id, you can use `options.prefix`, it will not work if `options.genid(ctx)` present.
Expand All @@ -125,7 +133,7 @@ External key is used the cookie by default, but you can use `options.externalKey

### Session#isNew

Returns __true__ if the session is new.
Returns **true** if the session is new.

```js
if (this.session.isNew) {
Expand All @@ -137,32 +145,38 @@ if (this.session.isNew) {

### Session#maxAge

Get cookie's maxAge.
Get cookie's maxAge.

### Session#maxAge=

Set cookie's maxAge.
Set cookie's maxAge.

### Session#externalKey

Get session external key, only exist when external session store present.
Get session external key, only exist when external session store present.

### Session#save()

Save this session no matter whether it is populated.
Save this session no matter whether it is populated.

### Session#manuallyCommit()

Session headers are auto committed by default. Use this if `autoCommit` is set to `false`.
Session headers are auto committed by default. Use this if `autoCommit` is set to `false`.

### Destroying a session

To destroy a session simply set it to `null`:
To destroy a session simply set it to `null`:

```js
this.session = null;
```

## License

MIT
[MIT](LICENSE)

## Contributors

[![Contributors](https://contrib.rocks/image?repo=koajs/session)](https://github.com/koajs/session/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).
20 changes: 20 additions & 0 deletions example.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

const Koa = require('koa');
const { createSession } = require('./');

const app = new Koa();

app.keys = [ 'some secret hurr' ];

app.use(createSession(app));

app.use(async (ctx, next) => {
if (ctx.path === '/favicon.ico') return next();

let n = ctx.session.views || 0;
ctx.session.views = ++n;
ctx.body = n + ' views';
});

app.listen(3000);
console.log('listening on port http://localhost:3000');
18 changes: 0 additions & 18 deletions example.js

This file was deleted.

Loading

0 comments on commit 575864c

Please sign in to comment.