Why Nostr? What is Njump?
2025-03-26 21:01:13
in reply to

hodlbod on Nostr: Yeah, the tricky part seems to be intercepting send messages so that you can buffer ...

Yeah, the tricky part seems to be intercepting send messages so that you can buffer them and re-send after authentication (or remove reqs that have been closed before they were sent while you were waiting for auth). Here's my (half-baked) policy for that:

```
export const socketPolicyDeferOnAuth = (socket: Socket) => {
const send$ = socket.send$
const buffer: ClientMessage[] = []
const authState = new AuthState(socket)
const okStatuses = [AuthStatus.None, AuthStatus.Ok]
// Defer sending certain messages when we're not authenticated
socket.send$ = send$.pipe(
filter(message => {
// Always allow sending auth
if (isClientAuth(message)) return true
// Always allow sending join requests
if (isClientEvent(message) && message[1].kind === AUTH_JOIN) return true
// If we're not ok, remove the message and save it for later
if (!okStatuses.includes(authState.status)) {
buffer.push(message)
return false
}
return true
}),
)
// Send buffered messages when we get successful auth
return authState.subscribe((status: AuthStatus) => {
if (okStatuses.includes(status)) {
const reqs = new Set(buffer.filter(isClientReq).map(nth(1)))
const closed = new Set(buffer.filter(isClientClose).map(nth(1)))
for (const message of buffer.splice(0)) {
// Skip requests that were closed before they were sent
if (isClientReq(message) && closed.has(message[1])) continue
// Skip closes for requests that were never sent
if (isClientClose(message) && reqs.has(message[1])) continue
socket.send(message)
}
}
})
}
```
Author Public Key
npub1jlrs53pkdfjnts29kveljul2sm0actt6n8dxrrzqcersttvcuv3qdjynqn