Skip to content

Commit

Permalink
refactor: rename
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyTseng committed Mar 24, 2024
1 parent ec063a7 commit 06164f1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 42 deletions.
6 changes: 3 additions & 3 deletions packages/common/src/interfaces/plugin.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ 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,
next: () => Promise<HandleMessageResult>,
): Promise<HandleMessageResult> | HandleMessageResult;
}

export interface BroadcastMiddleware {
export interface BroadcastPlugin {
broadcast(
ctx: ClientContext,
event: Event,
Expand Down
16 changes: 7 additions & 9 deletions packages/core/__test__/services/plugin-manager.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -47,19 +45,19 @@ 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,
]);
});
});

describe('handleMessage', () => {
it('should call middlewares in order', async () => {
it('should call plugins in order', async () => {
const arr: number[] = [];
pluginManagerService.register(
{
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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(
{
Expand Down
52 changes: 22 additions & 30 deletions packages/core/src/services/plugin-manager.service.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import {
BroadcastMiddleware,
BroadcastPlugin,
ClientContext,
Event,
HandleMessageMiddleware,
HandleMessagePlugin,
HandleMessageResult,
IncomingMessage,
KeysOfUnion,
NostrRelayPlugin,
} 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;
Expand All @@ -34,7 +34,7 @@ export class PluginManagerService {
) => Promise<HandleMessageResult>,
): Promise<HandleMessageResult> {
return this.compose(
this.handleMessageMiddlewares,
this.handleMessagePlugins,
'handleMessage',
next,
ctx,
Expand All @@ -47,13 +47,7 @@ export class PluginManagerService {
event: Event,
next: (ctx: ClientContext, event: Event) => Promise<void>,
): Promise<void> {
return this.compose(
this.broadcastMiddlewares,
'broadcast',
next,
ctx,
event,
);
return this.compose(this.broadcastPlugins, 'broadcast', next, ctx, event);
}

private compose<R>(
Expand All @@ -63,31 +57,29 @@ export class PluginManagerService {
...args: any[]
): Promise<R> {
let index = -1;
async function dispatch(i: number): Promise<R> {
return dispatch(0);
function dispatch(i: number): Promise<R> {
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';
}
}

0 comments on commit 06164f1

Please sign in to comment.