mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
7 Commits
v2.18.0
...
de7d459f6f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de7d459f6f | ||
|
|
21ec5bb2dc | ||
|
|
e959409c14 | ||
|
|
8a76c4e329 | ||
|
|
34a1d8db47 | ||
|
|
d3ddd490c2 | ||
|
|
7730e321a5 |
@@ -14,14 +14,17 @@ import { alwaysTrue } from './helpers.ts'
|
|||||||
|
|
||||||
export type SubCloser = { close: (reason?: string) => void }
|
export type SubCloser = { close: (reason?: string) => void }
|
||||||
|
|
||||||
export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {}
|
export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {
|
||||||
|
// automaticallyAuth takes a relay URL and should return null
|
||||||
|
// in case that relay shouldn't be authenticated against
|
||||||
|
// or a function to sign the AUTH event template otherwise (that function may still throw in case of failure)
|
||||||
|
automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
||||||
|
}
|
||||||
|
|
||||||
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
||||||
maxWait?: number
|
maxWait?: number
|
||||||
onclose?: (reasons: string[]) => void
|
onclose?: (reasons: string[]) => void
|
||||||
onauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
onauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||||
// Deprecated: use onauth instead
|
|
||||||
doauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
|
||||||
id?: string
|
id?: string
|
||||||
label?: string
|
label?: string
|
||||||
}
|
}
|
||||||
@@ -34,6 +37,7 @@ export class AbstractSimplePool {
|
|||||||
public verifyEvent: Nostr['verifyEvent']
|
public verifyEvent: Nostr['verifyEvent']
|
||||||
public enablePing: boolean | undefined
|
public enablePing: boolean | undefined
|
||||||
public enableReconnect: boolean | ((filters: Filter[]) => Filter[]) | undefined
|
public enableReconnect: boolean | ((filters: Filter[]) => Filter[]) | undefined
|
||||||
|
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
||||||
public trustedRelayURLs: Set<string> = new Set()
|
public trustedRelayURLs: Set<string> = new Set()
|
||||||
|
|
||||||
private _WebSocket?: typeof WebSocket
|
private _WebSocket?: typeof WebSocket
|
||||||
@@ -43,6 +47,7 @@ export class AbstractSimplePool {
|
|||||||
this._WebSocket = opts.websocketImplementation
|
this._WebSocket = opts.websocketImplementation
|
||||||
this.enablePing = opts.enablePing
|
this.enablePing = opts.enablePing
|
||||||
this.enableReconnect = opts.enableReconnect
|
this.enableReconnect = opts.enableReconnect
|
||||||
|
this.automaticallyAuth = opts.automaticallyAuth
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
|
async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
|
||||||
@@ -64,6 +69,14 @@ export class AbstractSimplePool {
|
|||||||
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
||||||
this.relays.set(url, relay)
|
this.relays.set(url, relay)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.automaticallyAuth) {
|
||||||
|
const authSignerFn = this.automaticallyAuth(url)
|
||||||
|
if (authSignerFn) {
|
||||||
|
relay.onauth = authSignerFn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await relay.connect()
|
await relay.connect()
|
||||||
|
|
||||||
return relay
|
return relay
|
||||||
@@ -77,8 +90,6 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
|
||||||
|
|
||||||
const request: { url: string; filter: Filter }[] = []
|
const request: { url: string; filter: Filter }[] = []
|
||||||
for (let i = 0; i < relays.length; i++) {
|
for (let i = 0; i < relays.length; i++) {
|
||||||
const url = normalizeURL(relays[i])
|
const url = normalizeURL(relays[i])
|
||||||
@@ -91,8 +102,6 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribeMany(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
subscribeMany(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
|
||||||
|
|
||||||
const request: { url: string; filter: Filter }[] = []
|
const request: { url: string; filter: Filter }[] = []
|
||||||
const uniqUrls: string[] = []
|
const uniqUrls: string[] = []
|
||||||
for (let i = 0; i < relays.length; i++) {
|
for (let i = 0; i < relays.length; i++) {
|
||||||
@@ -107,8 +116,6 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {
|
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
|
||||||
|
|
||||||
const grouped = new Map<string, Filter[]>()
|
const grouped = new Map<string, Filter[]>()
|
||||||
for (const req of requests) {
|
for (const req of requests) {
|
||||||
const { url, filter } = req
|
const { url, filter } = req
|
||||||
@@ -221,10 +228,8 @@ export class AbstractSimplePool {
|
|||||||
subscribeEose(
|
subscribeEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth'>,
|
||||||
): SubCloser {
|
): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
|
||||||
|
|
||||||
const subcloser = this.subscribe(relays, filter, {
|
const subcloser = this.subscribe(relays, filter, {
|
||||||
...params,
|
...params,
|
||||||
oneose() {
|
oneose() {
|
||||||
@@ -237,10 +242,8 @@ export class AbstractSimplePool {
|
|||||||
subscribeManyEose(
|
subscribeManyEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth'>,
|
||||||
): SubCloser {
|
): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
|
||||||
|
|
||||||
const subcloser = this.subscribeMany(relays, filter, {
|
const subcloser = this.subscribeMany(relays, filter, {
|
||||||
...params,
|
...params,
|
||||||
oneose() {
|
oneose() {
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ export class AbstractRelay {
|
|||||||
|
|
||||||
public onclose: (() => void) | null = null
|
public onclose: (() => void) | null = null
|
||||||
public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`)
|
public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`)
|
||||||
|
public onauth: undefined | ((evt: EventTemplate) => Promise<VerifiedEvent>)
|
||||||
|
|
||||||
public baseEoseTimeout: number = 4400
|
public baseEoseTimeout: number = 4400
|
||||||
public connectionTimeout: number = 4400
|
public connectionTimeout: number = 4400
|
||||||
@@ -158,12 +159,14 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
clearTimeout(this.connectionTimeoutHandle)
|
clearTimeout(this.connectionTimeoutHandle)
|
||||||
this._connected = true
|
this._connected = true
|
||||||
|
|
||||||
|
const isReconnection = this.reconnectAttempts > 0
|
||||||
this.reconnectAttempts = 0
|
this.reconnectAttempts = 0
|
||||||
|
|
||||||
// resubscribe to all open subscriptions
|
// resubscribe to all open subscriptions
|
||||||
for (const sub of this.openSubs.values()) {
|
for (const sub of this.openSubs.values()) {
|
||||||
sub.eosed = false
|
sub.eosed = false
|
||||||
if (typeof this.enableReconnect === 'function') {
|
if (isReconnection && typeof this.enableReconnect === 'function') {
|
||||||
sub.filters = this.enableReconnect(sub.filters)
|
sub.filters = this.enableReconnect(sub.filters)
|
||||||
}
|
}
|
||||||
sub.fire()
|
sub.fire()
|
||||||
@@ -205,14 +208,17 @@ export class AbstractRelay {
|
|||||||
private async waitForDummyReq() {
|
private async waitForDummyReq() {
|
||||||
return new Promise((resolve, _) => {
|
return new Promise((resolve, _) => {
|
||||||
// make a dummy request with expected empty eose reply
|
// make a dummy request with expected empty eose reply
|
||||||
// ["REQ", "_", {"ids":["aaaa...aaaa"]}]
|
// ["REQ", "_", {"ids":["aaaa...aaaa"], "limit": 0}]
|
||||||
const sub = this.subscribe([{ ids: ['a'.repeat(64)] }], {
|
const sub = this.subscribe(
|
||||||
oneose: () => {
|
[{ ids: ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], limit: 0 }],
|
||||||
sub.close()
|
{
|
||||||
resolve(true)
|
oneose: () => {
|
||||||
|
sub.close()
|
||||||
|
resolve(true)
|
||||||
|
},
|
||||||
|
eoseTimeout: this.pingTimeout + 1000,
|
||||||
},
|
},
|
||||||
eoseTimeout: this.pingTimeout + 1000,
|
)
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,6 +344,9 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
case 'AUTH': {
|
case 'AUTH': {
|
||||||
this.challenge = data[1] as string
|
this.challenge = data[1] as string
|
||||||
|
if (this.onauth) {
|
||||||
|
this.auth(this.onauth)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.18.0",
|
"version": "2.19.0",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ test('kind classification', () => {
|
|||||||
expect(classifyKind(30000)).toBe('parameterized')
|
expect(classifyKind(30000)).toBe('parameterized')
|
||||||
expect(classifyKind(39999)).toBe('parameterized')
|
expect(classifyKind(39999)).toBe('parameterized')
|
||||||
expect(classifyKind(40000)).toBe('unknown')
|
expect(classifyKind(40000)).toBe('unknown')
|
||||||
expect(classifyKind(255)).toBe('unknown')
|
expect(classifyKind(255)).toBe('regular')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('kind type guard', () => {
|
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. */
|
/** 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. */
|
||||||
|
|||||||
@@ -107,3 +107,9 @@ test('parse content with hashtags and emoji shortcodes', () => {
|
|||||||
{ type: 'emoji', shortcode: 'star', url: 'https://example.com/star.png' },
|
{ 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) }
|
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 mainloop
|
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 mainloop
|
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
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.18.0",
|
"version": "2.19.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import { describe, test, expect } from 'bun:test'
|
import { describe, test, expect } from 'bun:test'
|
||||||
import { buildEvent } from './test-helpers.ts'
|
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'
|
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 => ('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])
|
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 {
|
try {
|
||||||
if (url.indexOf('://') === -1) url = 'wss://' + url
|
if (url.indexOf('://') === -1) url = 'wss://' + url
|
||||||
let p = new URL(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, '/')
|
p.pathname = p.pathname.replace(/\/+/g, '/')
|
||||||
if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)
|
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 = ''
|
if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) p.port = ''
|
||||||
|
|||||||
Reference in New Issue
Block a user