koa-redis 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
koa-redis 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
koa-redis 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 MIT License
开发语言 C#
操作系统 跨平台
软件类型 开源软件
所属分类 大数据数据存储
开源组织
地区 不详
投 递 者 首席测试
适用人群 未知
收录时间 2021-11-30

软件简介

koa-redis

build status Coveralls David deps David devDeps license code style styled with prettier made with lass

Redis storage for Koa session middleware/cache with Sentinel and Cluster support

NPM

v4.0.0+ now uses ioredis and has support for Sentinel and Cluster!

Table of Contents

Install

npm:

npm install koa-redis

yarn:

yarn add koa-redis

Usage

koa-redis works with koa-generic-session (a generic session middleware for koa).

For more examples, please see the examples folder of koa-generic-session.

Basic

const session = require('koa-generic-session');
const redisStore = require('koa-redis');
const koa = require('koa');

const app = koa();
app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore({
    // Options specified here
  })
}));

app.use(function *() {
  switch (this.path) {
  case '/get':
    get.call(this);
    break;
  case '/remove':
    remove.call(this);
    break;
  case '/regenerate':
    yield regenerate.call(this);
    break;
  }
});

function get() {
  const session = this.session;
  session.count = session.count || 0;
  session.count++;
  this.body = session.count;
}

function remove() {
  this.session = null;
  this.body = 0;
}

function *regenerate() {
  get.call(this);
  yield this.regenerateSession();
  get.call(this);
}

app.listen(8080);

Sentinel

const session = require('koa-generic-session');
const redisStore = require('koa-redis');
const koa = require('koa');

const app = koa();
app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore({
    // Options specified here
    // <https://github.com/luin/ioredis#sentinel>
    sentinels: [
      { host: 'localhost', port: 26379 },
      { host: 'localhost', port: 26380 }
      // ...
    ],
    name: 'mymaster'
  })
}));

// ...

Cluster

const session = require('koa-generic-session');
const redisStore = require('koa-redis');
const koa = require('koa');

const app = koa();
app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore({
    // Options specified here
    // <https://github.com/luin/ioredis#cluster>
    isRedisCluster: true,
    nodes: [
      {
        port: 6380,
        host: '127.0.0.1'
      },
      {
        port: 6381,
        host: '127.0.0.1'
      }
      // ...
    ],
    // <https://github.com/luin/ioredis/blob/master/API.md#new-clusterstartupnodes-options>
    clusterOptions: {
      // ...
      redisOptions: {
        // ...
      }
    }
  })
}));

// ...

Options

  • all ioredis options - Useful things include url, host, port, and path to the server. Defaults to 127.0.0.1:6379
  • db (number) - will run client.select(db) after connection
  • client (object) - supply your own client, all other options are ignored unless duplicate is also supplied
  • duplicate (boolean) - When true, it will run client.duplicate() on the supplied client and use all other options supplied. This is useful if you want to select a different DB for sessions but also want to base from the same client object.
  • serialize - Used to serialize the data that is saved into the store.
  • unserialize - Used to unserialize the data that is fetched from the store.
  • isRedisCluster (boolean) - Used for creating a Redis cluster instance per ioredis Cluster options, if set to true, then a new Redis cluster will be instantiated with new Redis.Cluster(options.nodes, options.clusterOptions) (see Cluster docs for more info).
  • nodes (array) - Conditionally used for creating a Redis cluster instance when isRedisCluster option is true, this is the first argument passed to new Redis.Cluster and contains a list of all the nodes of the cluster ou want to connect to (see Cluster docs for more info).
  • clusterOptions (object) - Conditionally used for created a Redi cluster instance when isRedisCluster option is true, this is the second argument passed to new Redis.Cluster and contains options, such as redisOptions (see Cluster docs for more info).
  • DEPRECATED: old options - auth_pass and pass have been replaced with password, and socket has been replaced with path, however all of these options are backwards compatible.

Events

See the ioredis docs for more info.

Note that as of v4.0.0 the disconnect and warning events are removed as ioredis does not support them. The disconnect event is deprecated, although it is still emitted when end events are emitted.

API

These are some the functions that koa-generic-session uses that you can use manually. You will need to initialize differently than the example above:

const session = require('koa-generic-session');
const redisStore = require('koa-redis')({
  // Options specified here
});
const app = require('koa')();

app.keys = ['keys', 'keykeys'];
app.use(session({
  store: redisStore
}));

module(options)

Initialize the Redis connection with the optionally provided options (see above). The variable session below references this.

session.get(sid)

Generator that gets a session by ID. Returns parsed JSON is exists, null if it does not exist, and nothing upon error.

session.set(sid, sess, ttl)

Generator that sets a JSON session by ID with an optional time-to-live (ttl) in milliseconds. Yields ioredis's client.set() or client.setex().

session.destroy(sid)

Generator that destroys a session (removes it from Redis) by ID. Tields ioredis's client.del().

session.quit()

Generator that stops a Redis session after everything in the queue has completed. Yields ioredis's client.quit().

session.end()

Alias to session.quit(). It is not safe to use the real end function, as it cuts off the queue.

session.status

String giving the connection status updated using client.status.

session.connected

Boolean giving the connection status updated using client.status after any of the events above is fired.

session.client

Direct access to the ioredis client object.

Benchmark

Server Transaction rate Response time
connect without session 6763.56 trans/sec 0.01 secs
koa without session 5684.75 trans/sec 0.01 secs
connect with session 2759.70 trans/sec 0.02 secs
koa with session 2355.38 trans/sec 0.02 secs

Detailed benchmark report here

Testing

  1. Start a Redis server on localhost:6379. You can use redis-windows if you are on Windows or just want a quick VM-based server.
  2. Clone the repository and run npm i in it (Windows should work fine).
  3. If you want to see debug output, turn on the prompt's DEBUG flag.
  4. Run npm test to run the tests and generate coverage. To run the tests without generating coverage, run npm run-script test-only.

License

MIT © dead_horse

Contributors

Name Website
dead_horse
Nick Baugh http://niftylettuce.com/

展开阅读全文

代码

评论

点击引领话题📣 发布并加入讨论🔥
暂无内容
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
lodash输入验证错误漏洞
原型污染
lodash是一款开源的JavaScript实用程序库。 lodash 4.17.15及之前版本中存在输入验证错误漏洞。远程攻击者可借助'merge'、'mergeWith'和'defaultsDeep'函数利用该漏洞在系统上执行任意代码。
CVE-2020-8203 MPS-2020-15679
2022-08-08 19:28
Npm Ini 资源管理错误漏洞
拒绝服务
Npm Ini是美国Npm公司的一个基于Javascript的用于解析和序列化Ini格式文件的代码库。 Npm ini before 1.3.6 存在资源管理错误漏洞,该漏洞允许攻击者可利用该漏洞向应用程序提交恶意的INI文件,该应用程序将用INI解析该文件。这可以根据上下文进一步加以利用。
CVE-2020-7788 MPS-2020-17544
2022-08-08 19:28
lodash 命令注入漏洞
代码注入
lodash是一个提供模块化、性能和附加功能的现代 JavaScript 实用程序库。 4.17.21 之前的 Lodash 版本容易通过模板函数进行命令注入。
CVE-2021-23337 MPS-2021-2638
2022-08-08 19:28
handlebars 安全漏洞
原型污染
handlebars是一款语义化的Web模板系统。 handlebars 4.7.7之前版本存在安全漏洞,该漏洞源于当选择某些编译选项来编译来自不可信源的模板时,容易受到原型污染的影响。
CVE-2021-23383 MPS-2021-6180
2022-08-08 19:28
trim-newlines 安全漏洞
拒绝服务
trim-newlines是一个修改换行符的npm包。 trim-newlines 存在安全漏洞,该漏洞源于应用于Node.js在3.0.1与4.0.1版本及之前版本中.end()方法存在相关问题。
CVE-2021-33623 MPS-2021-7398
2022-08-08 19:28
nodejs 资源管理错误漏洞
拒绝服务
nodejs是是一个基于ChromeV8引擎的JavaScript运行环境通过对Chromev8引擎进行了封装以及使用事件驱动和非阻塞IO的应用让Javascript开发高性能的后台应用成为了可能。 nodejs-glob-parent 存在安全漏洞,该漏洞源于正则表达式拒绝服务。
CVE-2020-28469 MPS-2021-7827
2022-08-08 19:28
conventional-commits-parser 存在ReDoS漏洞
ReDoS
常规提交解析器是 Parse 原始常规提交。由于缺少清理,此软件包的受影响版本容易受到正则表达式拒绝服务 (ReDoS) 的攻击。
MPS-2022-13608
2022-08-08 19:28
ioredis 存在原型污染漏洞
原型污染
ioredis 是 Node.js 的 Redis 客户端。此软件包的受影响版本容易受到原型污染。
MPS-2022-13789
2022-08-08 19:28
kind-of注入漏洞
将资源暴露给错误范围
kind-of是一款JavaScript类型检查软件包。 kind-of v6.0.2版本中的index.js文件的‘ctorName’函数存在注入漏洞,攻击者可利用该漏洞覆盖内部属性,操纵类型检查的结果。
CVE-2019-20149 MPS-2019-17164
2022-08-08 19:28
Components trim 安全漏洞
拒绝服务
Components trim是Components团队的一个用于去除字符串两端空格的 Npm 代码库。 Service trim 所有版本存在安全漏洞,该漏洞源于容易受到通过trim()的正则表达式拒绝服务的攻击。
CVE-2020-7753 MPS-2020-14926
2022-08-08 19:28
npm dot-prop 安全漏洞
原型污染
4.2.1 之前的 dot-prop npm 包版本和 5.1.1 之前的 5.x 版本中的原型污染漏洞允许攻击者向 JavaScript 语言构造(例如对象)添加任意属性。
CVE-2020-8116 MPS-2020-1734
2022-08-08 19:28
yargs-parser 输入验证错误漏洞
输入验证不恰当
yargs-parser是一款选项解析器。 yargs-parser 13.1.2之前版本、14.0.0及之后版本(15.0.1版本已修复)和16.0.0及之后版本(18.1.1版本已修复)中存在输入验证错误漏洞。攻击者可借助‘__proto__’payload利用该漏洞添加或修改Object.prototype属性。
CVE-2020-7608 MPS-2020-4006
2022-08-08 19:28
pac-resolver 安全漏洞
pac-resolver是一个从PAC 文件生成异步解析器函数。 pac-resolver 5.0.0之前版本存在安全漏洞,该漏洞源于应用不安全的 PAC 文件处理,当与不受信任的输入一起使用时可能会发生这种情况。
CVE-2021-23406 MPS-2021-17857
2022-08-08 19:28
lodash 存在拒绝服务漏洞
拒绝服务
lodash 是一个现代 JavaScript 实用程序库,提供模块化、性能和附加功能。此软件包的受影响版本容易通过 setWith 和 set 函数受到原型污染。
MPS-2022-13842
2022-08-08 19:28
mocha 存在不正确的正则表达式漏洞
不正确的正则表达式
mocha 是一个用于 node.js 和浏览器的 javascript 测试框架。此软件包的受影响版本容易受到正则表达式拒绝服务 (ReDoS) 的攻击。
MPS-2022-13886
2022-08-08 19:28
Npm netmask 输入验证错误漏洞
输入验证不恰当
Npm netmask是美国Npm公司的一个应用软件。Netmask类解析并了解IPv4 CIDR块,以便可以对其进行探索和比较。 netmask package before 2.0.1 for Node.js 存在输入验证错误漏洞,攻击者可利用该漏洞绕过基于IP地址的访问控制。
CVE-2021-29418 MPS-2021-3762
2022-08-08 19:28
diff 存在拒绝服务漏洞
diff 是一个 javascript 文本差异实现。此软件包的受影响版本容易受到正则表达式拒绝服务 (ReDoS) 的攻击。
MPS-2022-12883
2022-08-08 19:28
acorn 存在拒绝服务漏洞
拒绝服务
acorn 是一个用 JavaScript 编写的小巧、快速的 JavaScript 解析器。此软件包的受影响版本通过 /[x-\ud800]/u 形式的正则表达式容易受到正则表达式拒绝服务 (ReDoS) 的攻击,这会导致解析器进入无限循环。
MPS-2022-13525
2022-08-08 19:28
Growl命令执行漏洞
命令注入
Growl是一套支持Node.js的通知系统。 Growl 1.10.2之前版本中存在安全漏洞,该漏洞源于在将输入传递到shell命令之前,程序未能正确的对其进行过滤。攻击者可利用该漏洞执行任意命令。
CVE-2017-16042 MPS-2018-7026
2022-08-08 19:28
extend module 输入验证错误漏洞
输入验证不恰当
extend module是一个jQuery的经典extend()方法的端口。 deep-extend node模块0.5.0及之前版本中的‘utilities’函数存在输入验证错误漏洞。攻击者可利用该漏洞造成服务器崩溃或返回500。
CVE-2018-3750 MPS-2018-8705
2022-08-08 19:28
没有更多内容
加载失败,请刷新页面
点击加载更多
加载中
下一页
0 评论
0 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部