All files / x RedisCookieJar.ts

100% Statements 4/4
50% Branches 1/2
100% Functions 1/1
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                  1x                                   1x     1x     1x            
import Redis from 'ioredis'
import { CookieJar } from 'tough-cookie'
import { isPlainObject } from '../utils'
 
// @ts-ignore
import _RedisCookieStore from 'tough-cookie-redisstore'
const RedisCookieStore: new (
  cookieKey: string,
  options?: { redis: Redis.Redis } | Redis.RedisOptions,
) => any = _RedisCookieStore
 
export interface RedisCookieJarOptions extends CookieJar.Options {
  /**
   * 唯一的键名。
   */
  key: string
 
  /**
   * Redis 实例或选项,内部使用 `ioredis`。
   */
  redis: Redis.Redis | Redis.RedisOptions
}
 
/**
 * 使用 Redis 作为 Cookie Jar。
 */
export class RedisCookieJar extends CookieJar {
  private static prefix = '@@@@COOKIE@@@@'
 
  constructor(options: RedisCookieJarOptions) {
    const redis: Redis.Redis = isPlainObject(options.redis)
      ? new Redis(options.redis as any)
      : (options.redis as any)
    super(
      new RedisCookieStore(`${RedisCookieJar.prefix}${options.key}`, { redis }),
      options,
    )
  }
}