From 06164f159d2c4caa7a0e7058ea83cc1fcf39822e Mon Sep 17 00:00:00 2001 From: codytseng Date: Sun, 24 Mar 2024 13:43:16 +0800 Subject: [PATCH] refactor: rename --- .../common/src/interfaces/plugin.interface.ts | 6 +-- .../services/plugin-manager.service.spec.ts | 16 +++--- .../src/services/plugin-manager.service.ts | 52 ++++++++----------- 3 files changed, 32 insertions(+), 42 deletions(-) diff --git a/packages/common/src/interfaces/plugin.interface.ts b/packages/common/src/interfaces/plugin.interface.ts index 0e5e0cd7..df52f622 100644 --- a/packages/common/src/interfaces/plugin.interface.ts +++ b/packages/common/src/interfaces/plugin.interface.ts @@ -3,9 +3,9 @@ import { Event } from './event.interface'; import { HandleMessageResult } from './handle-result.interface'; import { IncomingMessage } from './message.interface'; -export type NostrRelayPlugin = HandleMessageMiddleware | BroadcastMiddleware; +export type NostrRelayPlugin = HandleMessagePlugin | BroadcastPlugin; -export interface HandleMessageMiddleware { +export interface HandleMessagePlugin { handleMessage( ctx: ClientContext, message: IncomingMessage, @@ -13,7 +13,7 @@ export interface HandleMessageMiddleware { ): Promise | HandleMessageResult; } -export interface BroadcastMiddleware { +export interface BroadcastPlugin { broadcast( ctx: ClientContext, event: Event, diff --git a/packages/core/__test__/services/plugin-manager.service.spec.ts b/packages/core/__test__/services/plugin-manager.service.spec.ts index 42f86255..a67aa31a 100644 --- a/packages/core/__test__/services/plugin-manager.service.spec.ts +++ b/packages/core/__test__/services/plugin-manager.service.spec.ts @@ -27,10 +27,8 @@ describe('PluginManagerService', () => { pluginManagerService.register(plugin); - expect(pluginManagerService['handleMessageMiddlewares']).toEqual([ - plugin, - ]); - expect(pluginManagerService['broadcastMiddlewares']).toEqual([plugin]); + expect(pluginManagerService['handleMessagePlugins']).toEqual([plugin]); + expect(pluginManagerService['broadcastPlugins']).toEqual([plugin]); }); it('should register plugins', () => { @@ -47,11 +45,11 @@ describe('PluginManagerService', () => { pluginManagerService.register(plugin1, plugin2).register(plugin3); - expect(pluginManagerService['handleMessageMiddlewares']).toEqual([ + expect(pluginManagerService['handleMessagePlugins']).toEqual([ plugin1, plugin3, ]); - expect(pluginManagerService['broadcastMiddlewares']).toEqual([ + expect(pluginManagerService['broadcastPlugins']).toEqual([ plugin2, plugin3, ]); @@ -59,7 +57,7 @@ describe('PluginManagerService', () => { }); describe('handleMessage', () => { - it('should call middlewares in order', async () => { + it('should call plugins in order', async () => { const arr: number[] = []; pluginManagerService.register( { @@ -95,7 +93,7 @@ describe('PluginManagerService', () => { expect(mockNext).toHaveBeenCalledWith(ctx, {}); }); - it('should directly return if middleware does not call next', async () => { + it('should directly return if plugin does not call next', async () => { pluginManagerService.register({ handleMessage: async () => { return { messageType: 'EVENT', success: false }; @@ -132,7 +130,7 @@ describe('PluginManagerService', () => { }); describe('broadcast', () => { - it('should call middlewares in order', async () => { + it('should call plugins in order', async () => { const arr: number[] = []; pluginManagerService.register( { diff --git a/packages/core/src/services/plugin-manager.service.ts b/packages/core/src/services/plugin-manager.service.ts index 1864331b..a6dcf5ea 100644 --- a/packages/core/src/services/plugin-manager.service.ts +++ b/packages/core/src/services/plugin-manager.service.ts @@ -1,8 +1,8 @@ import { - BroadcastMiddleware, + BroadcastPlugin, ClientContext, Event, - HandleMessageMiddleware, + HandleMessagePlugin, HandleMessageResult, IncomingMessage, KeysOfUnion, @@ -10,16 +10,16 @@ import { } from '@nostr-relay/common'; export class PluginManagerService { - private readonly handleMessageMiddlewares: HandleMessageMiddleware[] = []; - private readonly broadcastMiddlewares: BroadcastMiddleware[] = []; + private readonly handleMessagePlugins: HandleMessagePlugin[] = []; + private readonly broadcastPlugins: BroadcastPlugin[] = []; register(...plugins: NostrRelayPlugin[]): PluginManagerService { plugins.forEach(plugin => { - if (this.hasHandleMessageMiddleware(plugin)) { - this.handleMessageMiddlewares.push(plugin); + if (this.hasHandleMessagePlugin(plugin)) { + this.handleMessagePlugins.push(plugin); } - if (this.hasBroadcastMiddleware(plugin)) { - this.broadcastMiddlewares.push(plugin); + if (this.hasBroadcastPlugin(plugin)) { + this.broadcastPlugins.push(plugin); } }); return this; @@ -34,7 +34,7 @@ export class PluginManagerService { ) => Promise, ): Promise { return this.compose( - this.handleMessageMiddlewares, + this.handleMessagePlugins, 'handleMessage', next, ctx, @@ -47,13 +47,7 @@ export class PluginManagerService { event: Event, next: (ctx: ClientContext, event: Event) => Promise, ): Promise { - return this.compose( - this.broadcastMiddlewares, - 'broadcast', - next, - ctx, - event, - ); + return this.compose(this.broadcastPlugins, 'broadcast', next, ctx, event); } private compose( @@ -63,31 +57,29 @@ export class PluginManagerService { ...args: any[] ): Promise { let index = -1; - async function dispatch(i: number): Promise { + return dispatch(0); + function dispatch(i: number): Promise { if (i <= index) { - throw new Error('next() called multiple times'); + return Promise.reject(new Error('next() called multiple times')); } index = i; - const middleware = plugins[i]?.[funcName]; - if (!middleware) { + const fn = plugins[i]?.[funcName]; + if (!fn) { return next(...args); } - return middleware(...args, dispatch.bind(null, i + 1)); + return Promise.resolve(fn(...args, dispatch.bind(null, i + 1))); } - return dispatch(0); } - private hasHandleMessageMiddleware( + private hasHandleMessagePlugin( plugin: NostrRelayPlugin, - ): plugin is HandleMessageMiddleware { - return ( - typeof (plugin as HandleMessageMiddleware).handleMessage === 'function' - ); + ): plugin is HandleMessagePlugin { + return typeof (plugin as HandleMessagePlugin).handleMessage === 'function'; } - private hasBroadcastMiddleware( + private hasBroadcastPlugin( plugin: NostrRelayPlugin, - ): plugin is BroadcastMiddleware { - return typeof (plugin as BroadcastMiddleware).broadcast === 'function'; + ): plugin is BroadcastPlugin { + return typeof (plugin as BroadcastPlugin).broadcast === 'function'; } }