@curveball/session

中文文档
1.0.1发布于 2024/11/07MITES Module近 30 天下载 2137

Session storage using HTTP cookies

原始使用文档
文档目录9 节
  1. Installation
  2. Upgrading from versions 0.5 and below
  3. Getting started
  4. Adding the middleware
  5. Using the session store
  6. Deleting a session
  7. Re-generate a session id.
  8. CSRF token support
  9. API

Curveball Session Middleware

This package adds support for sessions to the Curveball framework.

Features:

  • It's lazy. It will only start a session if there is something in the store.
  • It will also automatically wipe the session data if session data was emptied.
  • It provides features for generating and validating CSRF tokens.

Installation

npm install @curveball/session

Upgrading from versions 0.5 and below

If you are upgrading from a 0.5.x release or earlier, this package introduces a BC break since 0.6.

In 0.5 session data was available in ctx.state.session and ctx.state.sessionId, but this has been moved to ctx.session and ctx.sessionId.

Getting started

Adding the middleware

import session from '@curveball/session';

app.use(session({
  store: 'memory',
});

This will add the in-memory session store to curveball. This store is mostly meant for testing.

Here is another example with more options:

import session from '@curveball/session';

app.use(session({
  store: 'memory',
  cookieName: 'MY_SESSION',
  expiry: 7200,
  cookieOptions: {
    secure: true,
    path: '/',
    sameSite: true,
  },
});
  • cookieName - Updates the name of the HTTP Cookie. It's CB by default.
  • expiry - The number of seconds of inactivity before the session disappears. this is 3600 seconds by default. It only pertains to the longevity of the session in the store, it doesn't influence cookie parameters.
  • cookieOptions - If set, override cookie options from the default. The list of supported options can be found in the documentation of the [cookie package][3].

Using the session store

In your own controllers and middlewares, you can set and update session data via the ctx.session property.

app.use( ctx => {

  // Running this will create the session
  ctx.session = { userId: 5 };
  ctx.response.body = 'Hello world';

});

Deleting a session

To delete an open session, just clear the session data:

app.use( ctx => {

  // Running this will create the session
  ctx.session = null;

});

Re-generate a session id.

If you clear the session id, but there is still data, the middleware will remove the old session and automatically create a new session id:

app.use( ctx => {

  // This will kill the old session and start a new one with the same data.
  ctx.sessionId = null;

});

CSRF token support

To obtain a CSRF token for forms, the middleware provides a getCsrf() function:

app.use( async ctx => {

  // Obtain a CSRF token for HTML forms:
  const csrfToken = await ctx.getCsrf();

});

It's recommended to embed this token in HTML forms as such:

<input type="hidden" name="csrf-token" value="....token goes here" />

Then on POST requests, you can easily validate the token with the validateCsrf function. If the token was incorrect, this will automatically result in a 403 error:

app.use(route.post('/form-submit', ctx => {

  // Throws error if csrf-token was not supplied or incorrect
  ctx.validateCsrf();

}));

API

It's desirable to create your own stores for product usage. Eventually this project will probably add a few more default stores.

Until then, you must implement the following interface:

interface SessionStore {

  set(id: string, values: SessionValues, expire: number): Promise<void>;
  get(id: string): Promise<SessionValues>,
  delete(id: string): Promise<void>,
  newSessionId(): Promise<string>,

}

SessionValues is simply a key->value object. expire is expressed as a unix timestamp.

版本与发布记录

@curveball/session 共发布 22 个版本,首次发布于 2018/09/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.1latest2024/11/072 年前
1.0.02024/01/182 年前
0.10.02023/10/043 年前
0.9.02023/02/143 年前
0.8.12022/10/124 年前
0.8.02022/09/044 年前

关于 @curveball/session 的常见问题

@curveball/session 是什么?

Session storage using HTTP cookies

如何安装 @curveball/session?

在项目目录运行 npm install @curveball/session,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@curveball/kernel(^1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@curveball/session 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/curveball__session 是否存在。

@curveball/session 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@curveball/session 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@curveball/session 可以商用吗?

@curveball/session 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@curveball/session 的下载量有多少?

根据 npm 官方下载统计,@curveball/session 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,137 次,日均约 71 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@curveball/session 还在维护吗?

最新版本 1.0.1 发布于 2024/11/07(2 年前);至今共发布 22 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/09/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。