@eggjs/redis 3.0.0 → 4.0.0-beta.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +169 -124
  2. package/dist/agent.d.ts +2 -0
  3. package/dist/agent.js +3 -0
  4. package/dist/app.d.ts +2 -0
  5. package/dist/app.js +3 -0
  6. package/dist/{commonjs/config → config}/config.default.d.ts +2 -2
  7. package/dist/{esm/config → config}/config.default.js +1 -1
  8. package/dist/index.d.ts +1 -0
  9. package/dist/{esm/index.js → index.js} +2 -2
  10. package/dist/{commonjs/lib → lib}/redis.d.ts +2 -2
  11. package/dist/lib/redis.js +76 -0
  12. package/dist/types.d.ts +14 -0
  13. package/dist/{esm/types.js → types.js} +1 -1
  14. package/package.json +49 -69
  15. package/dist/commonjs/agent.d.ts +0 -2
  16. package/dist/commonjs/agent.js +0 -5
  17. package/dist/commonjs/app.d.ts +0 -2
  18. package/dist/commonjs/app.js +0 -5
  19. package/dist/commonjs/config/config.default.js +0 -55
  20. package/dist/commonjs/index.d.ts +0 -1
  21. package/dist/commonjs/index.js +0 -4
  22. package/dist/commonjs/lib/redis.js +0 -87
  23. package/dist/commonjs/package.json +0 -3
  24. package/dist/commonjs/types.d.ts +0 -11
  25. package/dist/commonjs/types.js +0 -3
  26. package/dist/esm/agent.d.ts +0 -2
  27. package/dist/esm/agent.js +0 -3
  28. package/dist/esm/app.d.ts +0 -2
  29. package/dist/esm/app.js +0 -3
  30. package/dist/esm/config/config.default.d.ts +0 -59
  31. package/dist/esm/index.d.ts +0 -1
  32. package/dist/esm/lib/redis.d.ts +0 -6
  33. package/dist/esm/lib/redis.js +0 -80
  34. package/dist/esm/package.json +0 -3
  35. package/dist/esm/types.d.ts +0 -11
  36. package/dist/package.json +0 -4
  37. package/src/agent.ts +0 -3
  38. package/src/app.ts +0 -3
  39. package/src/config/config.default.ts +0 -111
  40. package/src/index.ts +0 -1
  41. package/src/lib/redis.ts +0 -92
  42. package/src/types.ts +0 -14
  43. package/src/typings/index.d.ts +0 -4
@@ -1,111 +0,0 @@
1
- import type { RedisOptions, ClusterOptions } from 'ioredis';
2
-
3
- export interface RedisClientOptions extends RedisOptions {
4
- /**
5
- * Whether to enable weakDependent mode, the redis client start will not block the application start
6
- *
7
- * Default to `undefined`
8
- */
9
- weakDependent?: boolean;
10
- }
11
-
12
- export interface RedisClusterOptions extends ClusterOptions {
13
- cluster: true;
14
- nodes: RedisClientOptions[];
15
- }
16
-
17
- export interface RedisConfig {
18
- /**
19
- * Default redis client config
20
- *
21
- * Default to `{}`
22
- */
23
- default: RedisClientOptions;
24
- /**
25
- * Single Redis or Cluster Redis config
26
- */
27
- client?: RedisClientOptions | RedisClusterOptions;
28
- /**
29
- * Multi Redis config
30
- */
31
- clients?: Record<string, RedisClientOptions>;
32
- /**
33
- * redis client will try to use TIME command to detect client is ready or not
34
- * if your redis server not support TIME command, please set this config to false
35
- * see https://redis.io/commands/time
36
- *
37
- * Default to `true`
38
- */
39
- supportTimeCommand: boolean;
40
- /**
41
- * Whether to enable redis for `app`
42
- *
43
- * Default to `true`
44
- */
45
- app: boolean;
46
- /**
47
- * Whether to enable redis for `agent`
48
- *
49
- * Default to `false`
50
- */
51
- agent: boolean;
52
- /**
53
- * Customize iovalkey version, only set when you needed
54
- *
55
- * Default to `undefined`, which means using the built-in ioredis
56
- */
57
- Redis?: any;
58
- }
59
-
60
- export default {
61
- redis: {
62
- default: {},
63
- app: true,
64
- agent: false,
65
- supportTimeCommand: true,
66
- // Single Redis
67
- // client: {
68
- // host: 'host',
69
- // port: 'port',
70
- // family: 'user',
71
- // password: 'password',
72
- // db: 'db',
73
- // },
74
- //
75
- // Cluster Redis
76
- // client: {
77
- // cluster: true,
78
- // nodes: [{
79
- // host: 'host',
80
- // port: 'port',
81
- // family: 'user',
82
- // password: 'password',
83
- // db: 'db',
84
- // }, {
85
- // host: 'host',
86
- // port: 'port',
87
- // family: 'user',
88
- // password: 'password',
89
- // db: 'db',
90
- // },
91
- // ]},
92
- //
93
- // Multi Redis
94
- // clients: {
95
- // instance1: {
96
- // host: 'host',
97
- // port: 'port',
98
- // family: 'user',
99
- // password: 'password',
100
- // db: 'db',
101
- // },
102
- // instance2: {
103
- // host: 'host',
104
- // port: 'port',
105
- // family: 'user',
106
- // password: 'password',
107
- // db: 'db',
108
- // },
109
- // },
110
- } as RedisConfig,
111
- };
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- import './types.js';
package/src/lib/redis.ts DELETED
@@ -1,92 +0,0 @@
1
- import assert from 'node:assert';
2
- import { once } from 'node:events';
3
- import { Redis } from 'ioredis';
4
- import type { ILifecycleBoot, EggCore } from '@eggjs/core';
5
- import type { RedisClusterOptions, RedisClientOptions } from '../config/config.default.js';
6
-
7
- export class RedisBoot implements ILifecycleBoot {
8
- constructor(private readonly app: EggCore) {
9
- // empty
10
- }
11
- async didLoad() {
12
- const app = this.app;
13
- if (app.type === 'application' && app.config.redis.app) {
14
- app.addSingleton('redis', createClient);
15
- } else if (app.type === 'agent' && app.config.redis.agent) {
16
- app.addSingleton('redis', createClient);
17
- }
18
- }
19
- }
20
-
21
- let count = 0;
22
- function createClient(options: RedisClusterOptions | RedisClientOptions, app: EggCore) {
23
- const RedisClass: typeof Redis = app.config.redis.Redis ?? Redis;
24
- let client;
25
-
26
- if ('cluster' in options && options.cluster === true) {
27
- const config = options as RedisClusterOptions;
28
- assert(config.nodes && config.nodes.length !== 0, '[@eggjs/redis] cluster nodes configuration is required when use cluster redis');
29
-
30
- config.nodes.forEach(client => {
31
- assert(client.host && client.port, `[@eggjs/redis] 'host: ${client.host}', 'port: ${client.port}' are required on config`);
32
- });
33
- app.coreLogger.info('[@eggjs/redis] cluster connecting');
34
- client = new RedisClass.Cluster(config.nodes, config as any);
35
- } else if ('sentinels' in options && options.sentinels) {
36
- const config = options as RedisClientOptions;
37
- assert(config.sentinels && config.sentinels.length !== 0, '[@eggjs/redis] sentinels configuration is required when use redis sentinel');
38
-
39
- config.sentinels.forEach(sentinel => {
40
- assert(sentinel.host && sentinel.port,
41
- `[@eggjs/redis] 'host: ${sentinel.host}', 'port: ${sentinel.port}' are required on config`);
42
- });
43
-
44
- const mask = config.password ? '***' : config.password;
45
- assert(config.name && config.password !== undefined && config.db !== undefined,
46
- `[@eggjs/redis] 'name of master: ${config.name}', 'password: ${mask}', 'db: ${config.db}' are required on config`);
47
-
48
- app.coreLogger.info('[@eggjs/redis] sentinel connecting start');
49
- client = new RedisClass(config as any);
50
- } else {
51
- const config = options as RedisClientOptions;
52
- const mask = config.password ? '***' : config.password;
53
- assert((config.host && config.port && config.password !== undefined && config.db !== undefined) || config.path,
54
- `[@eggjs/redis] 'host: ${config.host}', 'port: ${config.port}', 'password: ${mask}', 'db: ${config.db}' or 'path:${config.path}' are required on config`);
55
- if (config.host) {
56
- app.coreLogger.info('[@eggjs/redis] server connecting redis://:***@%s:%s/%s',
57
- config.host, config.port, config.db);
58
- } else {
59
- app.coreLogger.info('[@eggjs/redis] server connecting %s',
60
- config.path || config);
61
- }
62
-
63
- client = new RedisClass(config as any);
64
- }
65
-
66
- client.on('connect', () => {
67
- app.coreLogger.info('[@eggjs/redis] client connect success');
68
- });
69
- client.on('error', err => {
70
- app.coreLogger.error('[@eggjs/redis] client error: %s', err);
71
- app.coreLogger.error(err);
72
- });
73
-
74
- const index = count++;
75
- app.lifecycle.registerBeforeStart(async () => {
76
- if ('weakDependent' in options && options.weakDependent) {
77
- app.coreLogger.info(`[@eggjs/redis] instance[${index}] is weak dependent and won't block app start`);
78
- client.once('ready', () => {
79
- app.coreLogger.info(`[@eggjs/redis] instance[${index}] status OK`);
80
- });
81
- return;
82
- }
83
-
84
- await Promise.race([
85
- once(client, 'ready'),
86
- once(client, 'error'),
87
- ]);
88
- app.coreLogger.info(`[@eggjs/redis] instance[${index}] status OK, client ready`);
89
- }, `[@eggjs/redis] instance[${index}] start check`);
90
-
91
- return client;
92
- }
package/src/types.ts DELETED
@@ -1,14 +0,0 @@
1
- import type { Singleton } from '@eggjs/core';
2
- import type { Redis } from 'ioredis';
3
- import type { RedisConfig } from './config/config.default.js';
4
-
5
- declare module '@eggjs/core' {
6
- // add EggAppConfig overrides types
7
- interface EggAppConfig {
8
- redis: RedisConfig;
9
- }
10
-
11
- interface EggCore {
12
- redis: Redis & Singleton<Redis>;
13
- }
14
- }
@@ -1,4 +0,0 @@
1
- // make sure to import egg typings and let typescript know about it
2
- // @see https://github.com/whxaxes/blog/issues/11
3
- // and https://www.typescriptlang.org/docs/handbook/declaration-merging.html
4
- import 'egg';