Skip to content

Commit

Permalink
refactor: rename domain to hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyTseng committed Aug 12, 2024
1 parent 0e831d7 commit a65e933
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import { Logger } from './logger.interface';
* Options for NostrRelay
*/
export type NostrRelayOptions = {
/**
* Hostname of the Nostr Relay server. If not set, NIP-42 is not enabled.
* More info: https://github.com/nostr-protocol/nips/blob/master/42.md
*/
hostname?: string;
/**
* Domain name of the Nostr Relay server. If not set, NIP-42 is not enabled.
* More info: https://github.com/nostr-protocol/nips/blob/master/42.md
*
* @deprecated Use hostname instead
*/
domain?: string;
/**
Expand Down
26 changes: 13 additions & 13 deletions packages/core/__test__/nostr-relay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('NostrRelay', () => {

beforeEach(() => {
nostrRelay = new NostrRelay({} as EventRepository, {
domain: 'test',
hostname: 'test',
});

client = {
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('NostrRelay', () => {

it('should not cache handle result', async () => {
const nostrRelayWithoutCache = new NostrRelay({} as EventRepository, {
domain: 'test',
hostname: 'test',
eventHandlingResultCacheTtl: 0,
});
const event = { id: 'eventId' } as Event;
Expand Down Expand Up @@ -253,20 +253,20 @@ describe('NostrRelay', () => {
});

it('should handle req successfully if NIP-42 is not enabled and filter contains encrypted direct message kind', async () => {
const nostrRelayWithoutDomain = new NostrRelay({} as EventRepository);
const nostrRelayWithoutHostname = new NostrRelay({} as EventRepository);
const subscriptionId: SubscriptionId = 'subscriptionId';
const filters: Filter[] = [{ kinds: [4] }];
const events = [{ id: 'a', kind: 4 }] as Event[];
const ctx = nostrRelayWithoutDomain['getClientContext'](client);
const ctx = nostrRelayWithoutHostname['getClientContext'](client);

const mockSubscribe = jest
.spyOn(nostrRelayWithoutDomain['subscriptionService'], 'subscribe')
.spyOn(nostrRelayWithoutHostname['subscriptionService'], 'subscribe')
.mockImplementation();
const mockFind = jest
.spyOn(nostrRelayWithoutDomain['eventService'], 'find')
.spyOn(nostrRelayWithoutHostname['eventService'], 'find')
.mockResolvedValue(events);

const result = await nostrRelayWithoutDomain.handleReqMessage(
const result = await nostrRelayWithoutHostname.handleReqMessage(
client,
subscriptionId,
filters,
Expand Down Expand Up @@ -330,11 +330,11 @@ describe('NostrRelay', () => {
);
});

it('should return directly if domain is not set', async () => {
const nostrRelayWithoutDomain = new NostrRelay({} as EventRepository);
it('should return directly if hostname is not set', async () => {
const nostrRelayWithoutHostname = new NostrRelay({} as EventRepository);
const signedEvent = { id: 'eventId' } as Event;

nostrRelayWithoutDomain.handleAuthMessage(client, signedEvent);
nostrRelayWithoutHostname.handleAuthMessage(client, signedEvent);

expect(client.send).toHaveBeenCalledWith(
JSON.stringify([MessageType.OK, signedEvent.id, true, '']),
Expand Down Expand Up @@ -405,10 +405,10 @@ describe('NostrRelay', () => {
});

describe('isAuthorized', () => {
it('should return true if domain is not set', () => {
const nostrRelayWithoutDomain = new NostrRelay({} as EventRepository);
it('should return true if hostname is not set', () => {
const nostrRelayWithoutHostname = new NostrRelay({} as EventRepository);

expect(nostrRelayWithoutDomain.isAuthorized(client)).toBeTruthy();
expect(nostrRelayWithoutHostname.isAuthorized(client)).toBeTruthy();
});

it('should return false if client is not authenticated', () => {
Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/nostr-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class NostrRelay {
private readonly eventHandlingLazyCache:
| LazyCache<EventId, Promise<HandleEventResult>>
| undefined;
private readonly domain?: string;
private readonly hostname?: string;
private readonly pluginManagerService: PluginManagerService;

private readonly clientContexts = new Map<Client, ClientContext>();
Expand All @@ -59,14 +59,17 @@ export class NostrRelay {
) {
this.options = options;

// if hostname is not set, it means that NIP-42 is not enabled
this.hostname = options.hostname ?? options.domain;

const logger = options.logger ?? new ConsoleLoggerService();
logger.setLogLevel(options.logLevel ?? LogLevel.INFO);

this.pluginManagerService = new PluginManagerService();
this.subscriptionService = new SubscriptionService(
this.clientContexts,
logger,
!!options.domain,
!!this.hostname,
);
this.eventService = new EventService(
eventRepository,
Expand All @@ -89,9 +92,6 @@ export class NostrRelay {
ttl: options.eventHandlingResultCacheTtl,
});
}

// if domain is not set, it means that NIP-42 is not enabled
this.domain = options.domain;
}

/**
Expand All @@ -112,7 +112,7 @@ export class NostrRelay {
*/
handleConnection(client: Client): void {
const ctx = this.getClientContext(client);
if (this.domain) {
if (this.hostname) {
ctx.sendMessage(createOutgoingAuthMessage(ctx.id));
}
}
Expand Down Expand Up @@ -283,15 +283,15 @@ export class NostrRelay {
signedEvent: Event,
): HandleAuthMessageResult {
const ctx = this.getClientContext(client);
if (!this.domain) {
if (!this.hostname) {
ctx.sendMessage(createOutgoingOkMessage(signedEvent.id, true));
return { success: true };
}

const validateErrorMsg = EventUtils.isSignedEventValid(
signedEvent,
ctx.id,
this.domain,
this.hostname,
);
if (validateErrorMsg) {
ctx.sendMessage(
Expand All @@ -312,7 +312,7 @@ export class NostrRelay {
* @param client Client instance, usually a WebSocket
*/
isAuthorized(client: Client): boolean {
return this.domain ? !!this.getClientContext(client).pubkey : true;
return this.hostname ? !!this.getClientContext(client).pubkey : true;
}

/**
Expand Down Expand Up @@ -363,7 +363,7 @@ export class NostrRelay {
): Promise<Event[]> {
const events: Event[] = [];
if (
this.domain &&
this.hostname &&
filters.some(filter =>
FilterUtils.hasEncryptedDirectMessageKind(filter),
) &&
Expand All @@ -375,7 +375,7 @@ export class NostrRelay {
}

(await this.eventService.find(filters)).forEach(event => {
if (this.domain && !EventUtils.checkPermission(event, pubkey)) {
if (this.hostname && !EventUtils.checkPermission(event, pubkey)) {
return;
}
events.push(event);
Expand Down

0 comments on commit a65e933

Please sign in to comment.