mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34a1d8db47 | ||
|
|
d3ddd490c2 | ||
|
|
7730e321a5 | ||
|
|
400d132612 | ||
|
|
01880b6fb5 | ||
|
|
e87ffc433c |
@@ -138,6 +138,7 @@
|
|||||||
"valid-typeof": 2,
|
"valid-typeof": 2,
|
||||||
"wrap-iife": [2, "any"],
|
"wrap-iife": [2, "any"],
|
||||||
"yield-star-spacing": [2, "both"],
|
"yield-star-spacing": [2, "both"],
|
||||||
"yoda": [0]
|
"yoda": [0],
|
||||||
|
"no-labels": [0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1
build.js
1
build.js
@@ -7,7 +7,6 @@ const entryPoints = fs
|
|||||||
.filter(
|
.filter(
|
||||||
file =>
|
file =>
|
||||||
file.endsWith('.ts') &&
|
file.endsWith('.ts') &&
|
||||||
file !== 'core.ts' &&
|
|
||||||
file !== 'test-helpers.ts' &&
|
file !== 'test-helpers.ts' &&
|
||||||
file !== 'helpers.ts' &&
|
file !== 'helpers.ts' &&
|
||||||
file !== 'benchmarks.ts' &&
|
file !== 'benchmarks.ts' &&
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.17.4",
|
"version": "2.18.1",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
|
|||||||
4
kinds.ts
4
kinds.ts
@@ -2,12 +2,12 @@ import { NostrEvent, validateEvent } from './pure.ts'
|
|||||||
|
|
||||||
/** Events are **regular**, which means they're all expected to be stored by relays. */
|
/** Events are **regular**, which means they're all expected to be stored by relays. */
|
||||||
export function isRegularKind(kind: number): boolean {
|
export function isRegularKind(kind: number): boolean {
|
||||||
return (1000 <= kind && kind < 10000) || [1, 2, 4, 5, 6, 7, 8, 16, 40, 41, 42, 43, 44].includes(kind)
|
return kind < 10000 && kind !== 0 && kind !== 3
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Events are **replaceable**, which means that, for each combination of `pubkey` and `kind`, only the latest event is expected to (SHOULD) be stored by relays, older versions are expected to be discarded. */
|
/** Events are **replaceable**, which means that, for each combination of `pubkey` and `kind`, only the latest event is expected to (SHOULD) be stored by relays, older versions are expected to be discarded. */
|
||||||
export function isReplaceableKind(kind: number): boolean {
|
export function isReplaceableKind(kind: number): boolean {
|
||||||
return [0, 3].includes(kind) || (10000 <= kind && kind < 20000)
|
return kind === 0 || kind === 3 || (10000 <= kind && kind < 20000)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Events are **ephemeral**, which means they are not expected to be stored by relays. */
|
/** Events are **ephemeral**, which means they are not expected to be stored by relays. */
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { test, expect } from 'bun:test'
|
import { test, expect } from 'bun:test'
|
||||||
import { parse } from './nip27.ts'
|
import { parse } from './nip27.ts'
|
||||||
|
import { NostrEvent } from './core.ts'
|
||||||
|
|
||||||
test('first: parse simple content with 1 url and 1 nostr uri', () => {
|
test('first: parse simple content with 1 url and 1 nostr uri', () => {
|
||||||
const content = `nostr:npub1hpslpc8c5sp3e2nhm2fr7swsfqpys5vyjar5dwpn7e7decps6r8qkcln63 check out my profile:nostr:npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s; and this cool image https://images.com/image.jpg`
|
const content = `nostr:npub1hpslpc8c5sp3e2nhm2fr7swsfqpys5vyjar5dwpn7e7decps6r8qkcln63 check out my profile:nostr:npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s; and this cool image https://images.com/image.jpg`
|
||||||
@@ -75,3 +76,40 @@ test('third: parse complex content with 4 nostr uris and 3 urls', () => {
|
|||||||
{ type: 'url', url: 'https://example.com/docs' },
|
{ type: 'url', url: 'https://example.com/docs' },
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('parse content with hashtags and emoji shortcodes', () => {
|
||||||
|
const event: NostrEvent = {
|
||||||
|
kind: 1,
|
||||||
|
tags: [
|
||||||
|
['emoji', 'star', 'https://example.com/star.png'],
|
||||||
|
['emoji', 'alpaca', 'https://example.com/alpaca.png'],
|
||||||
|
],
|
||||||
|
content:
|
||||||
|
'hey nostr:npub1hpslpc8c5sp3e2nhm2fr7swsfqpys5vyjar5dwpn7e7decps6r8qkcln63 check out :alpaca::alpaca: #alpaca at wss://alpaca.com! :star:',
|
||||||
|
created_at: 1234567890,
|
||||||
|
pubkey: 'dummy',
|
||||||
|
id: 'dummy',
|
||||||
|
sig: 'dummy',
|
||||||
|
}
|
||||||
|
const blocks = Array.from(parse(event))
|
||||||
|
|
||||||
|
expect(blocks).toEqual([
|
||||||
|
{ type: 'text', text: 'hey ' },
|
||||||
|
{ type: 'reference', pointer: { pubkey: 'b861f0e0f8a4031caa77da923f41d04802485184974746b833f67cdce030d0ce' } },
|
||||||
|
{ type: 'text', text: ' check out ' },
|
||||||
|
{ type: 'emoji', shortcode: 'alpaca', url: 'https://example.com/alpaca.png' },
|
||||||
|
{ type: 'emoji', shortcode: 'alpaca', url: 'https://example.com/alpaca.png' },
|
||||||
|
{ type: 'text', text: ' ' },
|
||||||
|
{ type: 'hashtag', value: 'alpaca' },
|
||||||
|
{ type: 'text', text: ' at ' },
|
||||||
|
{ type: 'relay', url: 'wss://alpaca.com/' },
|
||||||
|
{ type: 'text', text: '! ' },
|
||||||
|
{ type: 'emoji', shortcode: 'star', url: 'https://example.com/star.png' },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('emoji shortcodes are treated as text if no event tags', () => {
|
||||||
|
const blocks = Array.from(parse('hello :alpaca:'))
|
||||||
|
|
||||||
|
expect(blocks).toEqual([{ type: 'text', text: 'hello :alpaca:' }])
|
||||||
|
})
|
||||||
|
|||||||
121
nip27.ts
121
nip27.ts
@@ -1,3 +1,4 @@
|
|||||||
|
import { NostrEvent } from './core.ts'
|
||||||
import { AddressPointer, EventPointer, ProfilePointer, decode } from './nip19.ts'
|
import { AddressPointer, EventPointer, ProfilePointer, decode } from './nip19.ts'
|
||||||
|
|
||||||
export type Block =
|
export type Block =
|
||||||
@@ -29,27 +30,67 @@ export type Block =
|
|||||||
type: 'audio'
|
type: 'audio'
|
||||||
url: string
|
url: string
|
||||||
}
|
}
|
||||||
|
| {
|
||||||
|
type: 'emoji'
|
||||||
|
shortcode: string
|
||||||
|
url: string
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type: 'hashtag'
|
||||||
|
value: string
|
||||||
|
}
|
||||||
|
|
||||||
const noCharacter = /\W/m
|
const noCharacter = /\W/m
|
||||||
const noURLCharacter = /\W |\W$|$|,| /m
|
const noURLCharacter = /\W |\W$|$|,| /m
|
||||||
|
const MAX_HASHTAG_LENGTH = 42
|
||||||
|
|
||||||
|
export function* parse(content: string | NostrEvent): Iterable<Block> {
|
||||||
|
let emojis: { type: 'emoji'; shortcode: string; url: string }[] = []
|
||||||
|
if (typeof content !== 'string') {
|
||||||
|
for (let i = 0; i < content.tags.length; i++) {
|
||||||
|
const tag = content.tags[i]
|
||||||
|
if (tag[0] === 'emoji' && tag.length >= 3) {
|
||||||
|
emojis.push({ type: 'emoji', shortcode: tag[1], url: tag[2] })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
content = content.content
|
||||||
|
}
|
||||||
|
|
||||||
export function* parse(content: string): Iterable<Block> {
|
|
||||||
const max = content.length
|
const max = content.length
|
||||||
let prevIndex = 0
|
let prevIndex = 0
|
||||||
let index = 0
|
let index = 0
|
||||||
while (index < max) {
|
mainloop: while (index < max) {
|
||||||
let u = content.indexOf(':', index)
|
const u = content.indexOf(':', index)
|
||||||
if (u === -1) {
|
const h = content.indexOf('#', index)
|
||||||
|
if (u === -1 && h === -1) {
|
||||||
// reached end
|
// reached end
|
||||||
break
|
break mainloop
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content.substring(u - 5, u) === 'nostr') {
|
if (u === -1 || (h >= 0 && h < u)) {
|
||||||
const m = content.substring(u + 60).match(noCharacter)
|
// parse hashtag
|
||||||
|
if (h === 0 || content[h - 1] === ' ') {
|
||||||
|
const m = content.slice(h + 1, h + MAX_HASHTAG_LENGTH).match(noCharacter)
|
||||||
|
const end = m ? h + 1 + m.index! : max
|
||||||
|
yield { type: 'text', text: content.slice(prevIndex, h) }
|
||||||
|
yield { type: 'hashtag', value: content.slice(h + 1, end) }
|
||||||
|
index = end
|
||||||
|
prevIndex = index
|
||||||
|
continue mainloop
|
||||||
|
}
|
||||||
|
|
||||||
|
// ignore this, it is nothing
|
||||||
|
index = h + 1
|
||||||
|
continue mainloop
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise parse things that have an ":"
|
||||||
|
if (content.slice(u - 5, u) === 'nostr') {
|
||||||
|
const m = content.slice(u + 60).match(noCharacter)
|
||||||
const end = m ? u + 60 + m.index! : max
|
const end = m ? u + 60 + m.index! : max
|
||||||
try {
|
try {
|
||||||
let pointer: ProfilePointer | AddressPointer | EventPointer
|
let pointer: ProfilePointer | AddressPointer | EventPointer
|
||||||
let { data, type } = decode(content.substring(u + 1, end))
|
let { data, type } = decode(content.slice(u + 1, end))
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'npub':
|
case 'npub':
|
||||||
@@ -65,89 +106,107 @@ export function* parse(content: string): Iterable<Block> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (prevIndex !== u - 5) {
|
if (prevIndex !== u - 5) {
|
||||||
yield { type: 'text', text: content.substring(prevIndex, u - 5) }
|
yield { type: 'text', text: content.slice(prevIndex, u - 5) }
|
||||||
}
|
}
|
||||||
yield { type: 'reference', pointer }
|
yield { type: 'reference', pointer }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue
|
continue mainloop
|
||||||
} catch (_err) {
|
} catch (_err) {
|
||||||
// ignore this, not a valid nostr uri
|
// ignore this, not a valid nostr uri
|
||||||
index = u + 1
|
index = u + 1
|
||||||
continue
|
continue mainloop
|
||||||
}
|
}
|
||||||
} else if (content.substring(u - 5, u) === 'https' || content.substring(u - 4, u) === 'http') {
|
} else if (content.slice(u - 5, u) === 'https' || content.slice(u - 4, u) === 'http') {
|
||||||
const m = content.substring(u + 4).match(noURLCharacter)
|
const m = content.slice(u + 4).match(noURLCharacter)
|
||||||
const end = m ? u + 4 + m.index! : max
|
const end = m ? u + 4 + m.index! : max
|
||||||
const prefixLen = content[u - 1] === 's' ? 5 : 4
|
const prefixLen = content[u - 1] === 's' ? 5 : 4
|
||||||
try {
|
try {
|
||||||
let url = new URL(content.substring(u - prefixLen, end))
|
let url = new URL(content.slice(u - prefixLen, end))
|
||||||
if (url.hostname.indexOf('.') === -1) {
|
if (url.hostname.indexOf('.') === -1) {
|
||||||
throw new Error('invalid url')
|
throw new Error('invalid url')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevIndex !== u - prefixLen) {
|
if (prevIndex !== u - prefixLen) {
|
||||||
yield { type: 'text', text: content.substring(prevIndex, u - prefixLen) }
|
yield { type: 'text', text: content.slice(prevIndex, u - prefixLen) }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (/\.(png|jpe?g|gif|webp)$/i.test(url.pathname)) {
|
if (/\.(png|jpe?g|gif|webp|heic|svg)$/i.test(url.pathname)) {
|
||||||
yield { type: 'image', url: url.toString() }
|
yield { type: 'image', url: url.toString() }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue
|
continue mainloop
|
||||||
}
|
}
|
||||||
if (/\.(mp4|avi|webm|mkv)$/i.test(url.pathname)) {
|
if (/\.(mp4|avi|webm|mkv|mov)$/i.test(url.pathname)) {
|
||||||
yield { type: 'video', url: url.toString() }
|
yield { type: 'video', url: url.toString() }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue
|
continue mainloop
|
||||||
}
|
}
|
||||||
if (/\.(mp3|aac|ogg|opus)$/i.test(url.pathname)) {
|
if (/\.(mp3|aac|ogg|opus|wav|flac)$/i.test(url.pathname)) {
|
||||||
yield { type: 'audio', url: url.toString() }
|
yield { type: 'audio', url: url.toString() }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue
|
continue mainloop
|
||||||
}
|
}
|
||||||
|
|
||||||
yield { type: 'url', url: url.toString() }
|
yield { type: 'url', url: url.toString() }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue
|
continue mainloop
|
||||||
} catch (_err) {
|
} catch (_err) {
|
||||||
// ignore this, not a valid url
|
// ignore this, not a valid url
|
||||||
index = end + 1
|
index = end + 1
|
||||||
continue
|
continue mainloop
|
||||||
}
|
}
|
||||||
} else if (content.substring(u - 3, u) === 'wss' || content.substring(u - 2, u) === 'ws') {
|
} else if (content.slice(u - 3, u) === 'wss' || content.slice(u - 2, u) === 'ws') {
|
||||||
const m = content.substring(u + 4).match(noURLCharacter)
|
const m = content.slice(u + 4).match(noURLCharacter)
|
||||||
const end = m ? u + 4 + m.index! : max
|
const end = m ? u + 4 + m.index! : max
|
||||||
const prefixLen = content[u - 1] === 's' ? 3 : 2
|
const prefixLen = content[u - 1] === 's' ? 3 : 2
|
||||||
try {
|
try {
|
||||||
let url = new URL(content.substring(u - prefixLen, end))
|
let url = new URL(content.slice(u - prefixLen, end))
|
||||||
if (url.hostname.indexOf('.') === -1) {
|
if (url.hostname.indexOf('.') === -1) {
|
||||||
throw new Error('invalid ws url')
|
throw new Error('invalid ws url')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevIndex !== u - prefixLen) {
|
if (prevIndex !== u - prefixLen) {
|
||||||
yield { type: 'text', text: content.substring(prevIndex, u - prefixLen) }
|
yield { type: 'text', text: content.slice(prevIndex, u - prefixLen) }
|
||||||
}
|
}
|
||||||
yield { type: 'relay', url: url.toString() }
|
yield { type: 'relay', url: url.toString() }
|
||||||
index = end
|
index = end
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
continue
|
continue mainloop
|
||||||
} catch (_err) {
|
} catch (_err) {
|
||||||
// ignore this, not a valid url
|
// ignore this, not a valid url
|
||||||
index = end + 1
|
index = end + 1
|
||||||
continue
|
continue mainloop
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// try to parse an emoji shortcode
|
||||||
|
for (let e = 0; e < emojis.length; e++) {
|
||||||
|
const emoji = emojis[e]
|
||||||
|
if (
|
||||||
|
content[u + emoji.shortcode.length + 1] === ':' &&
|
||||||
|
content.slice(u + 1, u + emoji.shortcode.length + 1) === emoji.shortcode
|
||||||
|
) {
|
||||||
|
// found an emoji
|
||||||
|
if (prevIndex !== u) {
|
||||||
|
yield { type: 'text', text: content.slice(prevIndex, u) }
|
||||||
|
}
|
||||||
|
yield emoji
|
||||||
|
index = u + emoji.shortcode.length + 2
|
||||||
|
prevIndex = index
|
||||||
|
continue mainloop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ignore this, it is nothing
|
// ignore this, it is nothing
|
||||||
index = u + 1
|
index = u + 1
|
||||||
continue
|
continue mainloop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevIndex !== max) {
|
if (prevIndex !== max) {
|
||||||
yield { type: 'text', text: content.substring(prevIndex) }
|
yield { type: 'text', text: content.slice(prevIndex) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
114
nip77.test.ts
Normal file
114
nip77.test.ts
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
import { describe, test, expect } from 'bun:test'
|
||||||
|
import { NegentropySync, NegentropyStorageVector } from './nip77.ts'
|
||||||
|
import { Relay } from './relay.ts'
|
||||||
|
import { NostrEvent } from './core.ts'
|
||||||
|
|
||||||
|
// const RELAY = 'ws://127.0.0.1:10547'
|
||||||
|
const RELAY = 'wss://relay.damus.io'
|
||||||
|
|
||||||
|
describe('NegentropySync', () => {
|
||||||
|
test('syncs events from ' + RELAY, async () => {
|
||||||
|
const relay = await Relay.connect(RELAY)
|
||||||
|
|
||||||
|
const storage = new NegentropyStorageVector()
|
||||||
|
storage.seal()
|
||||||
|
const filter = {
|
||||||
|
authors: ['3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'],
|
||||||
|
kinds: [30617, 30618],
|
||||||
|
}
|
||||||
|
|
||||||
|
let ids1: string[] = []
|
||||||
|
const done1 = Promise.withResolvers<void>()
|
||||||
|
const sync1 = new NegentropySync(relay, storage, filter, {
|
||||||
|
onneed: (id: string) => {
|
||||||
|
ids1.push(id)
|
||||||
|
},
|
||||||
|
onclose: err => {
|
||||||
|
expect(err).toBeUndefined()
|
||||||
|
done1.resolve()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await sync1.start()
|
||||||
|
await done1.promise
|
||||||
|
|
||||||
|
expect(ids1.length).toBeGreaterThan(10)
|
||||||
|
|
||||||
|
sync1.close()
|
||||||
|
|
||||||
|
// fetch events
|
||||||
|
const events1: NostrEvent[] = []
|
||||||
|
const fetched = Promise.withResolvers()
|
||||||
|
const sub = relay.subscribe([{ ids: ids1 }], {
|
||||||
|
onevent(evt) {
|
||||||
|
events1.push(evt)
|
||||||
|
},
|
||||||
|
oneose() {
|
||||||
|
sub.close()
|
||||||
|
fetched.resolve()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
await fetched.promise
|
||||||
|
expect(events1.map(evt => evt.id).sort()).toEqual(ids1.sort())
|
||||||
|
|
||||||
|
// Second sync with local events
|
||||||
|
await relay.connect()
|
||||||
|
|
||||||
|
const storage2 = new NegentropyStorageVector()
|
||||||
|
for (const evt of events1) {
|
||||||
|
storage2.insert(evt.created_at, evt.id)
|
||||||
|
}
|
||||||
|
storage2.seal()
|
||||||
|
|
||||||
|
let ids2: string[] = []
|
||||||
|
let done2 = Promise.withResolvers()
|
||||||
|
const sync2 = new NegentropySync(relay, storage2, filter, {
|
||||||
|
onneed: (id: string) => {
|
||||||
|
ids2.push(id)
|
||||||
|
},
|
||||||
|
onclose: err => {
|
||||||
|
expect(err).toBeUndefined()
|
||||||
|
done2.resolve()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await sync2.start()
|
||||||
|
await done2.promise
|
||||||
|
|
||||||
|
expect(ids2.length).toBe(0)
|
||||||
|
|
||||||
|
sync2.close()
|
||||||
|
|
||||||
|
// third sync with 4 events removed
|
||||||
|
const storage3 = new NegentropyStorageVector()
|
||||||
|
|
||||||
|
// shuffle
|
||||||
|
ids1.sort(() => Math.random() - 0.5)
|
||||||
|
const removedEvents = ids1.slice(0, 1 + Math.floor(Math.random() * ids1.length - 1))
|
||||||
|
for (const evt of events1) {
|
||||||
|
if (!removedEvents.includes(evt.id)) {
|
||||||
|
storage3.insert(evt.created_at, evt.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
storage3.seal()
|
||||||
|
|
||||||
|
let ids3: string[] = []
|
||||||
|
const done3 = Promise.withResolvers()
|
||||||
|
const sync3 = new NegentropySync(relay, storage3, filter, {
|
||||||
|
onneed: (id: string) => {
|
||||||
|
ids3.push(id)
|
||||||
|
},
|
||||||
|
onclose: err => {
|
||||||
|
expect(err).toBeUndefined()
|
||||||
|
done3.resolve()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await sync3.start()
|
||||||
|
await done3.promise
|
||||||
|
|
||||||
|
expect(ids3.sort()).toEqual(removedEvents.sort())
|
||||||
|
|
||||||
|
sync3.close()
|
||||||
|
})
|
||||||
|
})
|
||||||
12
nip77.ts
12
nip77.ts
@@ -537,6 +537,7 @@ export class NegentropySync {
|
|||||||
relay: AbstractRelay
|
relay: AbstractRelay
|
||||||
storage: NegentropyStorageVector
|
storage: NegentropyStorageVector
|
||||||
private neg: Negentropy
|
private neg: Negentropy
|
||||||
|
private filter: Filter
|
||||||
private subscription: Subscription
|
private subscription: Subscription
|
||||||
private onhave?: (id: string) => void
|
private onhave?: (id: string) => void
|
||||||
private onneed?: (id: string) => void
|
private onneed?: (id: string) => void
|
||||||
@@ -557,8 +558,10 @@ export class NegentropySync {
|
|||||||
this.neg = new Negentropy(storage)
|
this.neg = new Negentropy(storage)
|
||||||
this.onhave = params.onhave
|
this.onhave = params.onhave
|
||||||
this.onneed = params.onneed
|
this.onneed = params.onneed
|
||||||
|
this.filter = filter
|
||||||
|
|
||||||
this.subscription = this.relay.prepareSubscription([filter], { label: params.label || 'negentropy' })
|
// we prepare a subscription with an empty filter, but it will not be used
|
||||||
|
this.subscription = this.relay.prepareSubscription([{}], { label: params.label || 'negentropy' })
|
||||||
this.subscription.oncustom = (data: string[]) => {
|
this.subscription.oncustom = (data: string[]) => {
|
||||||
switch (data[0]) {
|
switch (data[0]) {
|
||||||
case 'NEG-MSG': {
|
case 'NEG-MSG': {
|
||||||
@@ -569,6 +572,9 @@ export class NegentropySync {
|
|||||||
const response = this.neg.reconcile(data[2], this.onhave, this.onneed)
|
const response = this.neg.reconcile(data[2], this.onhave, this.onneed)
|
||||||
if (response) {
|
if (response) {
|
||||||
this.relay.send(`["NEG-MSG", "${this.subscription.id}", "${response}"]`)
|
this.relay.send(`["NEG-MSG", "${this.subscription.id}", "${response}"]`)
|
||||||
|
} else {
|
||||||
|
this.close()
|
||||||
|
params.onclose?.()
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('negentropy reconcile error:', error)
|
console.error('negentropy reconcile error:', error)
|
||||||
@@ -591,9 +597,7 @@ export class NegentropySync {
|
|||||||
|
|
||||||
async start(): Promise<void> {
|
async start(): Promise<void> {
|
||||||
const initMsg = this.neg.initiate()
|
const initMsg = this.neg.initiate()
|
||||||
if (initMsg) {
|
this.relay.send(`["NEG-OPEN","${this.subscription.id}",${JSON.stringify(this.filter)},"${initMsg}"]`)
|
||||||
this.relay.send(`["NEG-OPEN","${this.subscription.id}",${initMsg}]`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
close(): void {
|
close(): void {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.17.4",
|
"version": "2.18.1",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
Reference in New Issue
Block a user