mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 08:38:50 +00:00
Compare commits
6 Commits
01880b6fb5
...
v2.18.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e959409c14 | ||
|
|
8a76c4e329 | ||
|
|
34a1d8db47 | ||
|
|
d3ddd490c2 | ||
|
|
7730e321a5 | ||
|
|
400d132612 |
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nostr/tools",
|
||||
"version": "2.18.0",
|
||||
"version": "2.18.2",
|
||||
"exports": {
|
||||
".": "./index.ts",
|
||||
"./core": "./core.ts",
|
||||
|
||||
@@ -18,7 +18,7 @@ test('kind classification', () => {
|
||||
expect(classifyKind(30000)).toBe('parameterized')
|
||||
expect(classifyKind(39999)).toBe('parameterized')
|
||||
expect(classifyKind(40000)).toBe('unknown')
|
||||
expect(classifyKind(255)).toBe('unknown')
|
||||
expect(classifyKind(255)).toBe('regular')
|
||||
})
|
||||
|
||||
test('kind type guard', () => {
|
||||
|
||||
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. */
|
||||
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. */
|
||||
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. */
|
||||
|
||||
@@ -107,3 +107,9 @@ test('parse content with hashtags and emoji shortcodes', () => {
|
||||
{ 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:' }])
|
||||
})
|
||||
|
||||
6
nip27.ts
6
nip27.ts
@@ -131,19 +131,19 @@ export function* parse(content: string | NostrEvent): Iterable<Block> {
|
||||
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() }
|
||||
index = end
|
||||
prevIndex = index
|
||||
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() }
|
||||
index = end
|
||||
prevIndex = index
|
||||
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() }
|
||||
index = end
|
||||
prevIndex = index
|
||||
|
||||
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
|
||||
storage: NegentropyStorageVector
|
||||
private neg: Negentropy
|
||||
private filter: Filter
|
||||
private subscription: Subscription
|
||||
private onhave?: (id: string) => void
|
||||
private onneed?: (id: string) => void
|
||||
@@ -557,8 +558,10 @@ export class NegentropySync {
|
||||
this.neg = new Negentropy(storage)
|
||||
this.onhave = params.onhave
|
||||
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[]) => {
|
||||
switch (data[0]) {
|
||||
case 'NEG-MSG': {
|
||||
@@ -569,6 +572,9 @@ export class NegentropySync {
|
||||
const response = this.neg.reconcile(data[2], this.onhave, this.onneed)
|
||||
if (response) {
|
||||
this.relay.send(`["NEG-MSG", "${this.subscription.id}", "${response}"]`)
|
||||
} else {
|
||||
this.close()
|
||||
params.onclose?.()
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('negentropy reconcile error:', error)
|
||||
@@ -591,9 +597,7 @@ export class NegentropySync {
|
||||
|
||||
async start(): Promise<void> {
|
||||
const initMsg = this.neg.initiate()
|
||||
if (initMsg) {
|
||||
this.relay.send(`["NEG-OPEN","${this.subscription.id}",${initMsg}]`)
|
||||
}
|
||||
this.relay.send(`["NEG-OPEN","${this.subscription.id}",${JSON.stringify(this.filter)},"${initMsg}"]`)
|
||||
}
|
||||
|
||||
close(): void {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"type": "module",
|
||||
"name": "nostr-tools",
|
||||
"version": "2.18.0",
|
||||
"version": "2.18.2",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import { describe, test, expect } from 'bun:test'
|
||||
import { buildEvent } from './test-helpers.ts'
|
||||
import { Queue, insertEventIntoAscendingList, insertEventIntoDescendingList, binarySearch } from './utils.ts'
|
||||
import {
|
||||
Queue,
|
||||
insertEventIntoAscendingList,
|
||||
insertEventIntoDescendingList,
|
||||
binarySearch,
|
||||
normalizeURL,
|
||||
} from './utils.ts'
|
||||
|
||||
import type { Event } from './core.ts'
|
||||
|
||||
@@ -263,3 +269,43 @@ test('binary search', () => {
|
||||
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('a' < b ? -1 : 'a' === b ? 0 : 1))).toEqual([0, true])
|
||||
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('[' < b ? -1 : '[' === b ? 0 : 1))).toEqual([0, false])
|
||||
})
|
||||
|
||||
describe('normalizeURL', () => {
|
||||
test('normalizes wss:// URLs', () => {
|
||||
expect(normalizeURL('wss://example.com')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('wss://example.com/')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('wss://example.com//path')).toBe('wss://example.com/path')
|
||||
expect(normalizeURL('wss://example.com:443')).toBe('wss://example.com/')
|
||||
})
|
||||
|
||||
test('normalizes https:// URLs', () => {
|
||||
expect(normalizeURL('https://example.com')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('https://example.com/')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('http://example.com//path')).toBe('ws://example.com/path')
|
||||
})
|
||||
|
||||
test('normalizes ws:// URLs', () => {
|
||||
expect(normalizeURL('ws://example.com')).toBe('ws://example.com/')
|
||||
expect(normalizeURL('ws://example.com/')).toBe('ws://example.com/')
|
||||
expect(normalizeURL('ws://example.com//path')).toBe('ws://example.com/path')
|
||||
expect(normalizeURL('ws://example.com:80')).toBe('ws://example.com/')
|
||||
})
|
||||
|
||||
test('adds wss:// to URLs without scheme', () => {
|
||||
expect(normalizeURL('example.com')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('example.com/')).toBe('wss://example.com/')
|
||||
expect(normalizeURL('example.com//path')).toBe('wss://example.com/path')
|
||||
})
|
||||
|
||||
test('handles query parameters', () => {
|
||||
expect(normalizeURL('wss://example.com?z=1&a=2')).toBe('wss://example.com/?a=2&z=1')
|
||||
})
|
||||
|
||||
test('removes hash', () => {
|
||||
expect(normalizeURL('wss://example.com#hash')).toBe('wss://example.com/')
|
||||
})
|
||||
|
||||
test('throws on invalid URL', () => {
|
||||
expect(() => normalizeURL('http://')).toThrow('Invalid URL: http://')
|
||||
})
|
||||
})
|
||||
|
||||
2
utils.ts
2
utils.ts
@@ -9,6 +9,8 @@ export function normalizeURL(url: string): string {
|
||||
try {
|
||||
if (url.indexOf('://') === -1) url = 'wss://' + url
|
||||
let p = new URL(url)
|
||||
if (p.protocol === 'http:') p.protocol = 'ws:'
|
||||
else if (p.protocol === 'https:') p.protocol = 'wss:'
|
||||
p.pathname = p.pathname.replace(/\/+/g, '/')
|
||||
if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)
|
||||
if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) p.port = ''
|
||||
|
||||
Reference in New Issue
Block a user