@yqg/node
Advanced tools
@@ -13,3 +13,3 @@ import { RequestHandler } from 'express'; | ||
| } | ||
| export default function createHealthCheck(options: Options): HealthCheck; | ||
| export default function createHealthCheck(options?: Options): HealthCheck; | ||
| export {}; |
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| var desc = Object.getOwnPropertyDescriptor(m, k); | ||
| if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
| desc = { enumerable: true, get: function() { return m[k]; } }; | ||
| } | ||
| Object.defineProperty(o, k2, desc); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
| Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
| }) : function(o, v) { | ||
| o["default"] = v; | ||
| }); | ||
| var __importStar = (this && this.__importStar) || function (mod) { | ||
| if (mod && mod.__esModule) return mod; | ||
| var result = {}; | ||
| if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
| __setModuleDefault(result, mod); | ||
| return result; | ||
| }; | ||
@@ -8,7 +28,9 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const socket_io_1 = require("socket.io"); | ||
| const redis_client_1 = __importDefault(require("../tool/redis-client")); | ||
| const redis_client_1 = __importStar(require("../tool/redis-client")); | ||
| function getAdapter(adapterConfig) { | ||
| const { key, ...redisConfig } = adapterConfig; | ||
| const pubClient = (0, redis_client_1.default)(redisConfig); | ||
| return (0, redis_adapter_1.createAdapter)(pubClient, pubClient.duplicate(), { key }); | ||
| const subClient = pubClient.duplicate(); | ||
| [pubClient, subClient].forEach(redis_client_1.bindDefaultErrorHandler); | ||
| return (0, redis_adapter_1.createAdapter)(pubClient, subClient, { key }); | ||
| } | ||
@@ -33,4 +55,8 @@ async function startSocket(server, options) { | ||
| logger.info({ redisConfig }); | ||
| if (!redisConfig || (!redisConfig.host && !redisConfig['cluster.nodes'])) { | ||
| logger.error('invalid redis config, skip redis adapter'); | ||
| return; | ||
| } | ||
| io.adapter(getAdapter({ ...redisConfig, key: adapterKey })); | ||
| } | ||
| exports.default = startSocket; |
@@ -0,1 +1,3 @@ | ||
| /// <reference types="mocha" /> | ||
| /// <reference types="node" /> | ||
| import Redis, { Cluster } from 'ioredis'; | ||
@@ -8,2 +10,3 @@ export declare type RedisConfig = { | ||
| }; | ||
| export declare function bindDefaultErrorHandler(client: NodeJS.EventEmitter): NodeJS.EventEmitter; | ||
| export default function getRedisClient(redisConfig: RedisConfig): Redis | Cluster; |
@@ -26,3 +26,11 @@ "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.bindDefaultErrorHandler = void 0; | ||
| const ioredis_1 = __importStar(require("ioredis")); | ||
| const onError = (err) => { | ||
| console.error(err); | ||
| }; | ||
| function bindDefaultErrorHandler(client) { | ||
| return client.on('error', onError); | ||
| } | ||
| exports.bindDefaultErrorHandler = bindDefaultErrorHandler; | ||
| function getRedisClient(redisConfig) { | ||
@@ -29,0 +37,0 @@ const { password, host, port } = redisConfig; |
@@ -13,3 +13,3 @@ import { RequestHandler } from 'express'; | ||
| } | ||
| export default function createHealthCheck(options: Options): HealthCheck; | ||
| export default function createHealthCheck(options?: Options): HealthCheck; | ||
| export {}; |
| import { createAdapter } from '@socket.io/redis-adapter'; | ||
| import { Server as IoServer } from 'socket.io'; | ||
| import getRedisClient from '../tool/redis-client'; | ||
| import getRedisClient, { bindDefaultErrorHandler } from '../tool/redis-client'; | ||
| function getAdapter(adapterConfig) { | ||
| const { key, ...redisConfig } = adapterConfig; | ||
| const pubClient = getRedisClient(redisConfig); | ||
| return createAdapter(pubClient, pubClient.duplicate(), { key }); | ||
| const subClient = pubClient.duplicate(); | ||
| [pubClient, subClient].forEach(bindDefaultErrorHandler); | ||
| return createAdapter(pubClient, subClient, { key }); | ||
| } | ||
@@ -27,3 +29,7 @@ export default async function startSocket(server, options) { | ||
| logger.info({ redisConfig }); | ||
| if (!redisConfig || (!redisConfig.host && !redisConfig['cluster.nodes'])) { | ||
| logger.error('invalid redis config, skip redis adapter'); | ||
| return; | ||
| } | ||
| io.adapter(getAdapter({ ...redisConfig, key: adapterKey })); | ||
| } |
@@ -0,1 +1,3 @@ | ||
| /// <reference types="mocha" /> | ||
| /// <reference types="node" /> | ||
| import Redis, { Cluster } from 'ioredis'; | ||
@@ -8,2 +10,3 @@ export declare type RedisConfig = { | ||
| }; | ||
| export declare function bindDefaultErrorHandler(client: NodeJS.EventEmitter): NodeJS.EventEmitter; | ||
| export default function getRedisClient(redisConfig: RedisConfig): Redis | Cluster; |
| import Redis, { Cluster } from 'ioredis'; | ||
| const onError = (err) => { | ||
| console.error(err); | ||
| }; | ||
| export function bindDefaultErrorHandler(client) { | ||
| return client.on('error', onError); | ||
| } | ||
| export default function getRedisClient(redisConfig) { | ||
@@ -3,0 +9,0 @@ const { password, host, port } = redisConfig; |
+2
-2
| { | ||
| "name": "@yqg/node", | ||
| "version": "1.8.2", | ||
| "version": "1.8.4", | ||
| "description": "Node Middleware", | ||
@@ -11,3 +11,3 @@ "homepage": "https://www.fintopia.tech", | ||
| "module": "esm/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "types": "esm/index.d.ts", | ||
| "commitTag": true, | ||
@@ -14,0 +14,0 @@ "scripts": { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
71895
3.1%1972
2.71%31
3.33%