nip77: adds wrapper for negentropy and fallback for yieldThread MessageChannel

This commit is contained in:
max-gy
2025-10-14 20:00:13 +02:00
committed by fiatjaf_
parent 1e0f393268
commit e19db61bec
5 changed files with 158 additions and 12 deletions

21
nip77.ts Normal file
View File

@@ -0,0 +1,21 @@
import { Filter } from './filter.ts'
import { AbstractRelay } from './relay.ts'
function sendNegentropyToRelay(open: boolean, relay: AbstractRelay, msg: string, filters: Filter[], subscriptionId?: any): void {
const subId = subscriptionId || Math.random().toString(36).slice(2, 10)
const parts: any[] = [open ? 'NEG-OPEN' : 'NEG-MSG', subId, ...filters, msg]
relay.send(JSON.stringify(parts))
}
export function sendNegentropyMessage(relay: AbstractRelay, msg: string, filters: Filter[], subscriptionId?: any): void {
sendNegentropyToRelay(false, relay, msg, filters, subscriptionId)
}
export function openNegentropyWithMessage(relay: AbstractRelay, msg: string, filters: Filter[], subscriptionId?: any): void {
sendNegentropyToRelay(true, relay, msg, filters, subscriptionId)
}
export function closeNegentropy(relay: AbstractRelay, subscriptionId: string): void {
const request = '["NEG-CLOSE","' + subscriptionId + '"]'
relay.send(request)
}