Skip to content

Commit 45b08d8

Browse files
committed
chore: add RELAY_PRIVATE_KEY env var
Signed-off-by: Ricardo Arturo Cabral Mejía <[email protected]>
1 parent 79042e7 commit 45b08d8

File tree

7 files changed

+34
-40
lines changed

7 files changed

+34
-40
lines changed

CONFIGURATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The following environment variables can be set:
77
| Name | Description | Default |
88
|----------------------------------|--------------------------------|------------------------|
99
| RELAY_PORT | Relay's server port | 8008 |
10+
| RELAY_PRIVATE_KEY | Relay's private key in hex | (auto-generated) |
1011
| WORKER_COUNT | Number of workers override | No. of available CPUs |
1112
| DB_HOST | PostgresSQL Hostname | |
1213
| DB_PORT | PostgreSQL Port | 5432 |

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ services:
2424
RR_DB_NAME: nostr_ts_relay
2525
RR_DB_MIN_POOL_SIZE: 16
2626
RR_DB_MAX_POOL_SIZE: 64
27-
RR_DB_ACQUIRE_CONNECTION_TIMEOUT: 60000
27+
RR_DB_ACQUIRE_CONNECTION_TIMEOUT: 10000
2828
# Redis
2929
REDIS_HOST: nostream-cache
3030
REDIS_PORT: 6379

src/adapters/web-socket-adapter.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import ws, { WebSocket } from 'ws'
21
import cluster from 'cluster'
32
import { EventEmitter } from 'stream'
43
import { IncomingMessage as IncomingHttpMessage } from 'http'
4+
import { WebSocket } from 'ws'
55

66
import { ContextMetadata, Factory } from '../@types/base'
77
import { createNoticeMessage, createOutgoingEventMessage } from '../utils/messages'
@@ -21,20 +21,6 @@ import { messageSchema } from '../schemas/message-schema'
2121
import { Settings } from '../@types/settings'
2222
import { SocketAddress } from 'net'
2323

24-
(() => {
25-
(ws as any).Receiver.prototype._write = function _write (chunk: any, _encoding: any, cb: any) {
26-
if (this._opcode === 0x08 && this._state == 0) return cb()
27-
28-
this._bufferedBytes += chunk.length
29-
this._buffers.push(chunk)
30-
try {
31-
this.startLoop(cb)
32-
} catch (error) {
33-
console.error('what in the world', error)
34-
cb(error)
35-
}
36-
}
37-
})()
3824

3925
const debug = createLogger('web-socket-adapter')
4026
const debugHeartbeat = debug.extend('heartbeat')
@@ -68,8 +54,6 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
6854
family: address.indexOf(':') >= 0 ? 'ipv6' : 'ipv4',
6955
})
7056

71-
console.log(`web-socket-adapter: new client ${this.clientId} (${this.getClientAddress()}) - ${(this.webSocketServer as any).webSocketServer.clients.size} total on worker ${process.pid}`)
72-
7357
this.client
7458
.on('error', (error) => {
7559
if (error.name === 'RangeError' && error.message === 'Max payload size exceeded') {
@@ -282,7 +266,5 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
282266

283267
this.removeAllListeners()
284268
this.client.removeAllListeners()
285-
286-
console.error(`web-socket-adapter: disconnected client ${this.clientId} (${this.getClientAddress()}) - ${(this.webSocketServer as any).webSocketServer.clients.size} total on worker ${process.pid}`)
287269
}
288270
}

src/utils/event.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,15 @@ export const identifyEvent = async (event: UnidentifiedEvent): Promise<UnsignedE
180180
}
181181

182182
export const getPrivateKeyFromSecret =
183-
(secret: string) => (publicKey: Pubkey | Buffer): string => {
183+
(secret: string) => (data: string | Buffer): string => {
184+
if (process.env.RELAY_PRIVATE_KEY) {
185+
return process.env.RELAY_PRIVATE_KEY
186+
}
187+
184188
const hmac = createHmac('sha256', secret)
185-
hmac.update(typeof publicKey === 'string' ? Buffer.from(publicKey, 'hex') : publicKey)
189+
190+
hmac.update(typeof data === 'string' ? Buffer.from(data) : data)
191+
186192
return hmac.digest().toString('hex')
187193
}
188194

test/unit/factories/websocket-adapter-factory.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,30 @@ import WebSocket from 'ws'
55

66
import { IEventRepository, IUserRepository } from '../../../src/@types/repositories'
77
import { IWebSocketServerAdapter } from '../../../src/@types/adapters'
8+
import { SettingsStatic } from '../../../src/utils/settings'
89
import { WebSocketAdapter } from '../../../src/adapters/web-socket-adapter'
910
import { webSocketAdapterFactory } from '../../../src/factories/websocket-adapter-factory'
1011

1112
describe('webSocketAdapterFactory', () => {
1213
let onStub: Sinon.SinonStub
14+
let createSettingsStub: Sinon.SinonStub
1315

1416
beforeEach(() => {
1517
onStub = Sinon.stub()
18+
createSettingsStub = Sinon.stub(SettingsStatic, 'createSettings')
1619
})
1720

1821
afterEach(() => {
22+
createSettingsStub.restore()
1923
onStub.reset()
2024
})
2125

2226
it('returns a WebSocketAdapter', () => {
27+
createSettingsStub.returns({
28+
network: {
29+
remoteIpHeader: 'remoteIpHeader',
30+
},
31+
})
2332
const eventRepository: IEventRepository = {} as any
2433
const userRepository: IUserRepository = {} as any
2534

test/unit/factories/worker-factory.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,37 @@ import Sinon from 'sinon'
44
import * as databaseClientModule from '../../../src/database/client'
55

66
import { AppWorker } from '../../../src/app/worker'
7+
import { SettingsStatic } from '../../../src/utils/settings'
78
import { workerFactory } from '../../../src/factories/worker-factory'
89

910

1011
describe('workerFactory', () => {
12+
let createSettingsStub: Sinon.SinonStub
1113
let getMasterDbClientStub: Sinon.SinonStub
1214
let getReadReplicaDbClientStub: Sinon.SinonStub
1315

1416
beforeEach(() => {
17+
createSettingsStub = Sinon.stub(SettingsStatic, 'createSettings')
1518
getMasterDbClientStub = Sinon.stub(databaseClientModule, 'getMasterDbClient')
1619
getReadReplicaDbClientStub = Sinon.stub(databaseClientModule, 'getReadReplicaDbClient')
1720
})
1821

1922
afterEach(() => {
20-
getMasterDbClientStub.restore()
2123
getReadReplicaDbClientStub.restore()
24+
getMasterDbClientStub.restore()
25+
createSettingsStub.restore()
2226
})
2327

2428
it('returns an AppWorker', () => {
29+
createSettingsStub.returns({
30+
info: {
31+
relay_url: 'url',
32+
},
33+
network: {
34+
35+
},
36+
})
37+
2538
const worker = workerFactory()
2639
expect(worker).to.be.an.instanceOf(AppWorker)
2740
worker.close()

test/unit/handlers/subscribe-message-handler.spec.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,6 @@ describe('SubscribeMessageHandler', () => {
177177
)
178178
})
179179

180-
it('ends event stream if aborted', async () => {
181-
isClientSubscribedToEventStub.returns(always(true))
182-
183-
const abort = () => (handler as IAbortable).abort()
184-
const fetch = () => (handler as any).fetchAndSend(subscriptionId, filters)
185-
186-
const promise = fetch()
187-
188-
const closeSpy = sandbox.spy()
189-
stream.once('close', closeSpy)
190-
191-
abort()
192-
193-
await expect(promise).to.eventually.be.rejectedWith(Error, 'The operation was aborted')
194-
expect(closeSpy).to.have.been.called
195-
})
196-
197180
it('ends event stream if error occurs', async () => {
198181
const error = new Error('mistakes were made')
199182
isClientSubscribedToEventStub.returns(always(true))

0 commit comments

Comments
 (0)