Minor net2 cleanup

This commit is contained in:
Jon Staab
2025-03-24 13:35:43 -07:00
parent b985003333
commit d7b74d2c7e
6 changed files with 39 additions and 24 deletions
+1 -7
View File
@@ -30,7 +30,7 @@ export type AuthResult = {
}
export enum AuthStateEventType {
Status = "auth:state:event:status",
Status = "auth:event:status",
}
export type AuthStateEvents = {
@@ -72,17 +72,11 @@ export class AuthState extends (EventEmitter as new () => TypedEmitter<AuthState
this.setStatus(AuthStatus.Requested)
}
}),
)
this._unsubscribers.push(
on(socket, SocketEventType.Enqueue, (message: RelayMessage) => {
if (isClientAuth(message)) {
this.setStatus(AuthStatus.PendingResponse)
}
}),
)
this._unsubscribers.push(
on(socket, SocketEventType.Status, (status: SocketStatus) => {
if (status === SocketStatus.Closed) {
this.challenge = undefined
+20 -10
View File
@@ -24,29 +24,32 @@ export type DiffEvents = {
[DiffEventType.Close]: () => void
}
export type DiffOptions = {
filter: Filter
events: SignedEvent[]
adapter: AbstractAdapter
on?: Partial<DiffEvents>
}
export class Diff extends (EventEmitter as new () => TypedEmitter<DiffEvents>) {
_id = `NEG-${randomId().slice(0, 8)}`
_unsubscriber: () => void
_closed = false
constructor(
readonly adapter: AbstractAdapter,
readonly events: SignedEvent[],
readonly filter: Filter,
) {
constructor(readonly options: DiffOptions) {
super()
const storage = new NegentropyStorageVector()
const neg = new Negentropy(storage, 50_000)
for (const event of events) {
for (const event of this.options.events) {
storage.insert(event.created_at, event.id)
}
storage.seal()
this._unsubscriber = on(
adapter,
this.options.adapter,
AdapterEventType.Receive,
async (message: RelayMessage, url: string) => {
if (isRelayNegMsg(message)) {
@@ -58,7 +61,7 @@ export class Diff extends (EventEmitter as new () => TypedEmitter<DiffEvents>) {
this.emit(DiffEventType.Message, {have, need}, url)
if (newMsg) {
adapter.send([RelayMessageType.NegMsg, this._id, newMsg])
this.options.adapter.send([RelayMessageType.NegMsg, this._id, newMsg])
} else {
this.close()
}
@@ -75,15 +78,22 @@ export class Diff extends (EventEmitter as new () => TypedEmitter<DiffEvents>) {
},
)
// Register listeners
if (this.options.on) {
for (const [k, listener] of Object.entries(this.options.on)) {
this.on(k as keyof DiffEvents, listener)
}
}
neg.initiate().then((msg: string) => {
adapter.send([ClientMessageType.NegOpen, this._id, filter, msg])
this.options.adapter.send([ClientMessageType.NegOpen, this._id, this.options.filter, msg])
})
}
close() {
if (this._closed) return
this.adapter.send([ClientMessageType.NegClose, this._id])
this.options.adapter.send([ClientMessageType.NegClose, this._id])
this.emit(DiffEventType.Close)
this.removeAllListeners()
this._unsubscriber()
+11
View File
@@ -0,0 +1,11 @@
export * from "./adapter.js"
export * from "./auth.js"
export * from "./diff.js"
export * from "./message.js"
export * from "./negentropy.js"
export * from "./policy.js"
export * from "./pool.js"
export * from "./publish.js"
export * from "./socket.js"
export * from "./subscribe.js"
export * from "./tracker.js"
+3 -3
View File
@@ -25,7 +25,7 @@ export type PublishOptions = {
adapter: AbstractAdapter
event: SignedEvent
timeout?: number
events?: Partial<PublishEvents>
on?: Partial<PublishEvents>
}
export class Publish extends (EventEmitter as new () => TypedEmitter<PublishEvents>) {
@@ -63,8 +63,8 @@ export class Publish extends (EventEmitter as new () => TypedEmitter<PublishEven
)
// Register handlers
if (this.options.events) {
for (const [k, listener] of Object.entries(this.options.events)) {
if (this.options.on) {
for (const [k, listener] of Object.entries(this.options.on)) {
this.on(k as keyof PublishEvents, listener)
}
}
+3 -3
View File
@@ -34,7 +34,7 @@ export type SubscriptionOptions = {
timeout?: number
tracker?: Tracker
verifyEvent?: (event: SignedEvent) => boolean
events?: Partial<SubscriptionEvents>
on?: Partial<SubscriptionEvents>
}
export class Subscription extends (EventEmitter as new () => TypedEmitter<SubscriptionEvents>) {
@@ -102,8 +102,8 @@ export class Subscription extends (EventEmitter as new () => TypedEmitter<Subscr
}
// Register listeners
if (this.options.events) {
for (const [k, listener] of Object.entries(this.options.events)) {
if (this.options.on) {
for (const [k, listener] of Object.entries(this.options.on)) {
this.on(k as keyof SubscriptionEvents, listener)
}
}