diff --git a/packages/common/src/client-context.ts b/packages/common/src/client-context.ts index f39da825..4c4be04a 100644 --- a/packages/common/src/client-context.ts +++ b/packages/common/src/client-context.ts @@ -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'; diff --git a/packages/common/src/constants/client.const.ts b/packages/common/src/constants/client.const.ts new file mode 100644 index 00000000..70aa50f7 --- /dev/null +++ b/packages/common/src/constants/client.const.ts @@ -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]; diff --git a/packages/common/src/constants/event.const.ts b/packages/common/src/constants/event.const.ts new file mode 100644 index 00000000..3c1b35ea --- /dev/null +++ b/packages/common/src/constants/event.const.ts @@ -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]; diff --git a/packages/common/src/constants/index.ts b/packages/common/src/constants/index.ts new file mode 100644 index 00000000..01c5ca94 --- /dev/null +++ b/packages/common/src/constants/index.ts @@ -0,0 +1,4 @@ +export * from './client.const'; +export * from './event.const'; +export * from './logger.const'; +export * from './message.const'; diff --git a/packages/common/src/constants/logger.const.ts b/packages/common/src/constants/logger.const.ts new file mode 100644 index 00000000..34ce3678 --- /dev/null +++ b/packages/common/src/constants/logger.const.ts @@ -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]; diff --git a/packages/common/src/constants/message.const.ts b/packages/common/src/constants/message.const.ts new file mode 100644 index 00000000..426fbce1 --- /dev/null +++ b/packages/common/src/constants/message.const.ts @@ -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]; diff --git a/packages/common/src/enums/client.enum.ts b/packages/common/src/enums/client.enum.ts deleted file mode 100644 index 9421ede1..00000000 --- a/packages/common/src/enums/client.enum.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Same as WebSocket readyState - */ -export enum ClientReadyState { - CONNECTING = 0, - OPEN = 1, - CLOSING = 2, - CLOSED = 3, -} diff --git a/packages/common/src/enums/event.enum.ts b/packages/common/src/enums/event.enum.ts deleted file mode 100644 index 8953b435..00000000 --- a/packages/common/src/enums/event.enum.ts +++ /dev/null @@ -1,64 +0,0 @@ -export enum EventType { - /** - * Regular event - */ - REGULAR = 'REGULAR', - /** - * Replaceable event - */ - REPLACEABLE = 'REPLACEABLE', - /** - * Ephemeral event - */ - EPHEMERAL = 'EPHEMERAL', - /** - * Parameterized replaceable event - */ - PARAMETERIZED_REPLACEABLE = 'PARAMETERIZED_REPLACEABLE', -} - -/** - * Some special event kinds - */ -export enum 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 enum TagName { - EVENT_COORDINATES = 'a', - EVENT = 'e', - PUBKEY = 'p', - D = 'd', - NONCE = 'nonce', - EXPIRATION = 'expiration', - DELEGATION = 'delegation', - RELAY = 'relay', - CHALLENGE = 'challenge', -} diff --git a/packages/common/src/enums/index.ts b/packages/common/src/enums/index.ts deleted file mode 100644 index f0511a29..00000000 --- a/packages/common/src/enums/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './client.enum'; -export * from './event.enum'; -export * from './log.enum'; diff --git a/packages/common/src/enums/log.enum.ts b/packages/common/src/enums/log.enum.ts deleted file mode 100644 index ee27ada0..00000000 --- a/packages/common/src/enums/log.enum.ts +++ /dev/null @@ -1,6 +0,0 @@ -export enum LogLevel { - DEBUG = 0, - INFO = 1, - WARN = 2, - ERROR = 3, -} diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 3a572690..839bfb0f 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -1,5 +1,5 @@ export * from './client-context'; -export * from './enums'; +export * from './constants'; export * from './errors'; export * from './interfaces'; export * from './services'; diff --git a/packages/common/src/interfaces/client.interface.ts b/packages/common/src/interfaces/client.interface.ts index 61a49b3e..cf81b76f 100644 --- a/packages/common/src/interfaces/client.interface.ts +++ b/packages/common/src/interfaces/client.interface.ts @@ -1,4 +1,4 @@ -import { ClientReadyState } from '../enums'; +import { ClientReadyState } from '../constants'; /** * Client interface. Usually a WebSocket. diff --git a/packages/common/src/interfaces/logger.interface.ts b/packages/common/src/interfaces/logger.interface.ts index c51d5739..1ee5a4ef 100644 --- a/packages/common/src/interfaces/logger.interface.ts +++ b/packages/common/src/interfaces/logger.interface.ts @@ -1,4 +1,4 @@ -import { LogLevel } from '../enums'; +import { LogLevel } from '../constants'; export interface Logger { setLogLevel(level: LogLevel): void; diff --git a/packages/common/src/interfaces/message.interface.ts b/packages/common/src/interfaces/message.interface.ts index f159c418..6e48b94a 100644 --- a/packages/common/src/interfaces/message.interface.ts +++ b/packages/common/src/interfaces/message.interface.ts @@ -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 diff --git a/packages/common/src/services/console-logger.service.ts b/packages/common/src/services/console-logger.service.ts index 7487501a..04041d2d 100644 --- a/packages/common/src/services/console-logger.service.ts +++ b/packages/common/src/services/console-logger.service.ts @@ -1,4 +1,4 @@ -import { LogLevel } from '../enums'; +import { LogLevel } from '../constants'; import { Logger } from '../interfaces'; export class ConsoleLoggerService implements Logger { diff --git a/packages/common/src/utils/event.util.ts b/packages/common/src/utils/event.util.ts index bc9450fe..f1ad0c3c 100644 --- a/packages/common/src/utils/event.util.ts +++ b/packages/common/src/utils/event.util.ts @@ -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'; diff --git a/packages/common/src/utils/filter.util.ts b/packages/common/src/utils/filter.util.ts index 0fcac486..96797502 100644 --- a/packages/common/src/utils/filter.util.ts +++ b/packages/common/src/utils/filter.util.ts @@ -1,4 +1,4 @@ -import { EventKind } from '../enums'; +import { EventKind } from '../constants'; import { Filter } from '../interfaces'; export class FilterUtils {