Skip to content

Commit

Permalink
refactor(common): replace enum with const (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyTseng authored Feb 18, 2024
1 parent 6d64f16 commit 42a4536
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 101 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/client-context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { randomUUID } from 'crypto';
import { LRUCache } from 'lru-cache';
import { ClientReadyState } from './enums/client.enum';
import { ClientReadyState } from './constants';
import { Client } from './interfaces/client.interface';
import { Filter } from './interfaces/filter.interface';
import { OutgoingMessage } from './interfaces/message.interface';
Expand Down
11 changes: 11 additions & 0 deletions packages/common/src/constants/client.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Same as WebSocket readyState
*/
export const ClientReadyState = {
CONNECTING: 0,
OPEN: 1,
CLOSING: 2,
CLOSED: 3,
} as const;
export type ClientReadyState =
(typeof ClientReadyState)[keyof typeof ClientReadyState];
67 changes: 67 additions & 0 deletions packages/common/src/constants/event.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
export const EventType = {
/**
* Regular event
*/
REGULAR: 'REGULAR',
/**
* Replaceable event
*/
REPLACEABLE: 'REPLACEABLE',
/**
* Ephemeral event
*/
EPHEMERAL: 'EPHEMERAL',
/**
* Parameterized replaceable event
*/
PARAMETERIZED_REPLACEABLE: 'PARAMETERIZED_REPLACEABLE',
} as const;
export type EventType = (typeof EventType)[keyof typeof EventType];

/**
* Some special event kinds
*/
export const EventKind = {
SET_METADATA: 0,
TEXT_NOTE: 1,
RECOMMEND_SERVER: 2,
CONTACT_LIST: 3,
ENCRYPTED_DIRECT_MESSAGE: 4,
DELETION: 5,
// Channel
CHANNEL_CREATION: 40,
CHANNEL_METADATA: 41,
CHANNEL_MESSAGE: 42,
CHANNEL_HIDE_MESSAGE: 43,
CHANNEL_MUTE_USER: 44,
CHANNEL_RESERVE_FIRST: 45,
CHANNEL_RESERVE_LAST: 49,
// Regular Events
REGULAR_FIRST: 1000,
REGULAR_LAST: 9999,
// Replaceable Events
REPLACEABLE_FIRST: 10000,
REPLACEABLE_LAST: 19999,
// Ephemeral Events
EPHEMERAL_FIRST: 20000,
AUTHENTICATION: 22242,
EPHEMERAL_LAST: 29999,
// Parameterized Replaceable Events
PARAMETERIZED_REPLACEABLE_FIRST: 30000,
LONG_FORM_CONTENT: 30023,
PARAMETERIZED_REPLACEABLE_LAST: 39999,
};
export type EventKind = number;

export const TagName = {
EVENT_COORDINATES: 'a',
EVENT: 'e',
PUBKEY: 'p',
D: 'd',
NONCE: 'nonce',
EXPIRATION: 'expiration',
DELEGATION: 'delegation',
RELAY: 'relay',
CHALLENGE: 'challenge',
} as const;
export type TagName = (typeof TagName)[keyof typeof TagName];
4 changes: 4 additions & 0 deletions packages/common/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './client.const';
export * from './event.const';
export * from './logger.const';
export * from './message.const';
7 changes: 7 additions & 0 deletions packages/common/src/constants/logger.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const LogLevel = {
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
} as const;
export type LogLevel = (typeof LogLevel)[keyof typeof LogLevel];
11 changes: 11 additions & 0 deletions packages/common/src/constants/message.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const MessageType = {
REQ: 'REQ',
EVENT: 'EVENT',
CLOSE: 'CLOSE',
AUTH: 'AUTH',
EOSE: 'EOSE',
OK: 'OK',
NOTICE: 'NOTICE',
CLOSED: 'CLOSED',
} as const;
export type MessageType = (typeof MessageType)[keyof typeof MessageType];
9 changes: 0 additions & 9 deletions packages/common/src/enums/client.enum.ts

This file was deleted.

64 changes: 0 additions & 64 deletions packages/common/src/enums/event.enum.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/common/src/enums/index.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/common/src/enums/log.enum.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './client-context';
export * from './enums';
export * from './constants';
export * from './errors';
export * from './interfaces';
export * from './services';
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/interfaces/client.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientReadyState } from '../enums';
import { ClientReadyState } from '../constants';

/**
* Client interface. Usually a WebSocket.
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/interfaces/logger.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogLevel } from '../enums';
import { LogLevel } from '../constants';

export interface Logger {
setLogLevel(level: LogLevel): void;
Expand Down
13 changes: 1 addition & 12 deletions packages/common/src/interfaces/message.interface.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { MessageType } from '../constants';
import { EventId, SubscriptionId } from './common.interface';
import { Event } from './event.interface';
import { Filter } from './filter.interface';

export const MessageType = {
REQ: 'REQ',
EVENT: 'EVENT',
CLOSE: 'CLOSE',
AUTH: 'AUTH',
EOSE: 'EOSE',
OK: 'OK',
NOTICE: 'NOTICE',
CLOSED: 'CLOSED',
} as const;
export type TMessageType = (typeof MessageType)[keyof typeof MessageType];

export type IncomingMessage =
| IncomingEventMessage
| IncomingReqMessage
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/services/console-logger.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogLevel } from '../enums';
import { LogLevel } from '../constants';
import { Logger } from '../interfaces';

export class ConsoleLoggerService implements Logger {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/utils/event.util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventKind, EventType, TagName } from '../enums';
import { EventKind, EventType, TagName } from '../constants';
import { Event, Filter, Tag } from '../interfaces';
import { schnorrVerify, sha256 } from './crypto.util';
import { countPowDifficulty } from './proof-of-work.util';
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/utils/filter.util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventKind } from '../enums';
import { EventKind } from '../constants';
import { Filter } from '../interfaces';

export class FilterUtils {
Expand Down

0 comments on commit 42a4536

Please sign in to comment.