Skip to content

Commit b77239a

Browse files
committed
chore: tidy up
1 parent faa32c3 commit b77239a

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

src/handlers/event-message-handler.ts

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,12 @@ export class EventMessageHandler implements IMessageHandler {
6363
protected canAcceptEvent(event: Event): string | undefined {
6464
const now = Math.floor(Date.now()/1000)
6565
const limits = this.settings().limits.event
66-
if (limits.createdAt.maxPositiveDelta > 0) {
67-
if (event.created_at > now + limits.createdAt.maxPositiveDelta) {
68-
return `rejected: created_at is more than ${limits.createdAt.maxPositiveDelta} seconds in the future`
69-
}
66+
if (limits.createdAt.maxPositiveDelta > 0 && event.created_at > now + limits.createdAt.maxPositiveDelta) {
67+
return `rejected: created_at is more than ${limits.createdAt.maxPositiveDelta} seconds in the future`
7068
}
7169

72-
if (limits.createdAt.maxNegativeDelta > 0) {
73-
if (event.created_at < now - limits.createdAt.maxNegativeDelta) {
74-
return `rejected: created_at is more than ${limits.createdAt.maxNegativeDelta} seconds in the past`
75-
}
70+
if (limits.createdAt.maxNegativeDelta > 0 && event.created_at < now - limits.createdAt.maxNegativeDelta) {
71+
return `rejected: created_at is more than ${limits.createdAt.maxNegativeDelta} seconds in the past`
7672
}
7773

7874
if (limits.eventId.minLeadingZeroBits > 0) {
@@ -89,33 +85,31 @@ export class EventMessageHandler implements IMessageHandler {
8985
}
9086
}
9187

92-
if (limits.pubkey.whitelist.length > 0) {
93-
if (!limits.pubkey.whitelist.some((prefix) => event.pubkey.startsWith(prefix))) {
94-
return 'blocked: pubkey not allowed'
95-
}
88+
if (
89+
limits.pubkey.whitelist.length > 0
90+
&& !limits.pubkey.whitelist.some((prefix) => event.pubkey.startsWith(prefix))
91+
) {
92+
return 'blocked: pubkey not allowed'
9693
}
9794

98-
if (limits.pubkey.blacklist.length > 0) {
99-
if (limits.pubkey.blacklist.some((prefix) => event.pubkey.startsWith(prefix))) {
100-
return 'blocked: pubkey not allowed'
101-
}
95+
if (
96+
limits.pubkey.blacklist.length > 0
97+
&& limits.pubkey.blacklist.some((prefix) => event.pubkey.startsWith(prefix))
98+
) {
99+
return 'blocked: pubkey not allowed'
102100
}
103101

104102
const isEventKindMatch = (item: EventKinds | EventKindsRange) =>
105103
typeof item === 'number'
106104
? item === event.kind
107105
: event.kind >= item[0] && event.kind <= item[1]
108106

109-
if (limits.kind.whitelist.length > 0) {
110-
if (!limits.kind.whitelist.some(isEventKindMatch)) {
111-
return `blocked: event kind ${event.kind} not allowed`
112-
}
107+
if (limits.kind.whitelist.length > 0 && !limits.kind.whitelist.some(isEventKindMatch)) {
108+
return `blocked: event kind ${event.kind} not allowed`
113109
}
114110

115-
if (limits.kind.blacklist.length > 0) {
116-
if (limits.kind.blacklist.some(isEventKindMatch)) {
117-
return `blocked: event kind ${event.kind} not allowed`
118-
}
111+
if (limits.kind.blacklist.length > 0 && limits.kind.blacklist.some(isEventKindMatch)) {
112+
return `blocked: event kind ${event.kind} not allowed`
119113
}
120114
}
121115

@@ -152,13 +146,10 @@ export class EventMessageHandler implements IMessageHandler {
152146
)
153147
}
154148

155-
const hits = await Promise.all(
156-
rateLimits
157-
.map(async (rateLimit) => ({ ...rateLimit, active: await hit(rateLimit) }))
158-
)
149+
const hits = await Promise.all(rateLimits.map(hit))
159150

160151
debug('rate limit check %s: %o', event.pubkey, hits)
161152

162-
return hits.some(({ active }) => active)
153+
return hits.some((active) => active)
163154
}
164155
}

src/handlers/subscribe-message-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class SubscribeMessageHandler implements IMessageHandler, IAbortable {
3333

3434
public async handleMessage(message: SubscribeMessage): Promise<void> {
3535
debug('received message: %o', message)
36-
const subscriptionId = message[1] as SubscriptionId
36+
const subscriptionId = message[1]
3737
const filters = uniqWith(equals, message.slice(2)) as SubscriptionFilter[]
3838

3939
const reason = this.canSubscribe(subscriptionId, filters)

src/repositories/event-repository.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ export class EventRepository implements IEventRepository {
7373
evolve({
7474
exact: (pubkeys: string[]) =>
7575
tableFields.forEach((tableField) =>
76-
void bd.orWhereIn(tableField, pubkeys.map(toBuffer))
76+
bd.orWhereIn(tableField, pubkeys.map(toBuffer))
7777
),
7878
even: forEach((prefix: string) =>
7979
tableFields.forEach((tableField) =>
80-
void bd.orWhereRaw(
80+
bd.orWhereRaw(
8181
`substring("${tableField}" from 1 for ?) = ?`,
8282
[prefix.length >> 1, toBuffer(prefix)]
8383
)
8484
)
8585
),
8686
odd: forEach((prefix: string) =>
8787
tableFields.forEach((tableField) =>
88-
void bd.orWhereRaw(
88+
bd.orWhereRaw(
8989
`substring("${tableField}" from 1 for ?) BETWEEN ? AND ?`,
9090
[
9191
(prefix.length >> 1) + 1,

0 commit comments

Comments
 (0)