Compare commits

...

4 Commits

Author SHA1 Message Date
fiatjaf
cc8e34163d most simple relay pool. 2023-02-08 08:39:59 -03:00
gaodeng
9082953ede fix error event 2023-02-07 06:03:41 -03:00
Luis Miguel
61f397463d nip05 supports uppercase
nip05 says `NIP-05 assumes the <local-part> part will be restricted to the characters a-z0-9-_., case insensitive`

So a lot of people is starting the names with uppercase. See here:

`https://nostr-check.com/.well-known/nostr.json`

So I think we should change the regex to accept lowercase or uppercase.

Another way to do it would be to do a `.toLowerCase` at the beginning, but then we would need to do this search ignoring the case:

```
if (!res?.names?.[name])
```

So maybe for now this is enough?
2023-01-31 10:22:11 -03:00
fiatjaf
312b6fd035 add fast insert-into-sorted-list utils. 2023-01-28 18:07:14 -03:00
8 changed files with 346 additions and 9 deletions

View File

@@ -120,6 +120,22 @@ To use this on Node.js you first must install `websocket-polyfill` and import it
import 'websocket-polyfill'
```
### Interacting with multiple relays
```js
import {pool} from 'nostr-tools'
const p = pool()
["wss://relay.example.com", "wss://relay.example2.com"].forEach(async url => {
let relay = pool.ensureRelay(url)
await relay.connect()
relay.sub(...) // same as above
relay.publish(...) // etc
})
```
### Querying profile data from a NIP-05 address
```js
@@ -195,7 +211,7 @@ let event = {
sendEvent(event)
// on the receiver side
sub.on('event', (event) => {
sub.on('event', event => {
let sender = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1]
pk1 === sender
let plaintext = await nip04.decrypt(sk2, pk1, event.content)

View File

@@ -2,8 +2,7 @@ export * from './keys'
export * from './relay'
export * from './event'
export * from './filter'
export * as fj from './fakejson'
export * from './pool'
export * as nip04 from './nip04'
export * as nip05 from './nip05'
@@ -11,6 +10,9 @@ export * as nip06 from './nip06'
export * as nip19 from './nip19'
export * as nip26 from './nip26'
export * as fj from './fakejson'
export * as utils from './utils'
// monkey patch secp256k1
import * as secp256k1 from '@noble/secp256k1'
import {hmac} from '@noble/hashes/hmac'

View File

@@ -36,7 +36,7 @@ export async function queryProfile(
name = '_'
}
if (!name.match(/^[a-z0-9-_]+$/)) return null
if (!name.match(/^[A-Za-z0-9-_]+$/)) return null
let res = await (
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)

View File

@@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "1.2.0",
"version": "1.2.3",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",

27
pool.ts Normal file
View File

@@ -0,0 +1,27 @@
import {Relay, relayInit} from './relay'
import {normalizeURL} from './utils'
export function pool(defaultRelays: string[] = []) {
return new SimplePool(defaultRelays)
}
class SimplePool {
private _conn: {[url: string]: Relay}
private _knownIds: Set<string> = new Set()
constructor(defaultRelays: string[]) {
this._conn = {}
defaultRelays.forEach(this.ensureRelay)
}
ensureRelay(url: string): Relay {
const nm = normalizeURL(url)
const existing = this._conn[nm]
if (existing) return existing
const hasEventId = (id: string): boolean => this._knownIds.has(id)
const relay = relayInit(nm, hasEventId)
this._conn[nm] = relay
return relay
}
}

View File

@@ -46,7 +46,7 @@ export function relayInit(
var listeners: {
connect: Array<() => void>
disconnect: Array<() => void>
error: Array<() => void>
error: Array<(e: globalThis.Event) => void>
notice: Array<(msg: string) => void>
} = {
connect: [],
@@ -77,9 +77,9 @@ export function relayInit(
setOpen()
resolve()
}
ws.onerror = () => {
listeners.error.forEach(cb => cb())
reject()
ws.onerror = (e: globalThis.Event) => {
listeners.error.forEach(cb => cb(e))
reject(e)
}
ws.onclose = async () => {
listeners.disconnect.forEach(cb => cb())

183
utils.test.js Normal file
View File

@@ -0,0 +1,183 @@
/* eslint-env jest */
const {utils} = require('./lib/nostr.cjs')
const {insertEventIntoAscendingList, insertEventIntoDescendingList} = utils
describe('inserting into a desc sorted list of events', () => {
test('insert into an empty list', async () => {
const list0 = []
expect(
insertEventIntoDescendingList(list0, {id: 'abc', created_at: 10})
).toHaveLength(1)
})
test('insert in the beginning of a list', async () => {
const list0 = [{created_at: 20}, {created_at: 10}]
const list1 = insertEventIntoDescendingList(list0, {
id: 'abc',
created_at: 30
})
expect(list1).toHaveLength(3)
expect(list1[0].id).toBe('abc')
})
test('insert in the beginning of a list with same created_at', async () => {
const list0 = [{created_at: 30}, {created_at: 20}, {created_at: 10}]
const list1 = insertEventIntoDescendingList(list0, {
id: 'abc',
created_at: 30
})
expect(list1).toHaveLength(4)
expect(list1[0].id).toBe('abc')
})
test('insert in the middle of a list', async () => {
const list0 = [
{created_at: 30},
{created_at: 20},
{created_at: 10},
{created_at: 1}
]
const list1 = insertEventIntoDescendingList(list0, {
id: 'abc',
created_at: 15
})
expect(list1).toHaveLength(5)
expect(list1[2].id).toBe('abc')
})
test('insert in the end of a list', async () => {
const list0 = [
{created_at: 20},
{created_at: 20},
{created_at: 20},
{created_at: 20},
{created_at: 10}
]
const list1 = insertEventIntoDescendingList(list0, {
id: 'abc',
created_at: 5
})
expect(list1).toHaveLength(6)
expect(list1.slice(-1)[0].id).toBe('abc')
})
test('insert in the last-to-end of a list with same created_at', async () => {
const list0 = [
{created_at: 20},
{created_at: 20},
{created_at: 20},
{created_at: 20},
{created_at: 10}
]
const list1 = insertEventIntoDescendingList(list0, {
id: 'abc',
created_at: 10
})
expect(list1).toHaveLength(6)
expect(list1.slice(-2)[0].id).toBe('abc')
})
test('do not insert duplicates', async () => {
const list0 = [
{created_at: 20},
{created_at: 20},
{created_at: 10, id: 'abc'}
]
const list1 = insertEventIntoDescendingList(list0, {
id: 'abc',
created_at: 10
})
expect(list1).toHaveLength(3)
})
})
describe('inserting into a asc sorted list of events', () => {
test('insert into an empty list', async () => {
const list0 = []
expect(
insertEventIntoAscendingList(list0, {id: 'abc', created_at: 10})
).toHaveLength(1)
})
test('insert in the beginning of a list', async () => {
const list0 = [{created_at: 10}, {created_at: 20}]
const list1 = insertEventIntoAscendingList(list0, {
id: 'abc',
created_at: 1
})
expect(list1).toHaveLength(3)
expect(list1[0].id).toBe('abc')
})
test('insert in the beginning of a list with same created_at', async () => {
const list0 = [{created_at: 10}, {created_at: 20}, {created_at: 30}]
const list1 = insertEventIntoAscendingList(list0, {
id: 'abc',
created_at: 10
})
expect(list1).toHaveLength(4)
expect(list1[0].id).toBe('abc')
})
test('insert in the middle of a list', async () => {
const list0 = [
{created_at: 10},
{created_at: 20},
{created_at: 30},
{created_at: 40}
]
const list1 = insertEventIntoAscendingList(list0, {
id: 'abc',
created_at: 25
})
expect(list1).toHaveLength(5)
expect(list1[2].id).toBe('abc')
})
test('insert in the end of a list', async () => {
const list0 = [
{created_at: 20},
{created_at: 20},
{created_at: 20},
{created_at: 20},
{created_at: 40}
]
const list1 = insertEventIntoAscendingList(list0, {
id: 'abc',
created_at: 50
})
expect(list1).toHaveLength(6)
expect(list1.slice(-1)[0].id).toBe('abc')
})
test('insert in the last-to-end of a list with same created_at', async () => {
const list0 = [
{created_at: 20},
{created_at: 20},
{created_at: 20},
{created_at: 20},
{created_at: 30}
]
const list1 = insertEventIntoAscendingList(list0, {
id: 'abc',
created_at: 30
})
expect(list1).toHaveLength(6)
expect(list1.slice(-2)[0].id).toBe('abc')
})
test('do not insert duplicates', async () => {
const list0 = [
{created_at: 20},
{created_at: 20},
{created_at: 30, id: 'abc'}
]
const list1 = insertEventIntoAscendingList(list0, {
id: 'abc',
created_at: 30
})
expect(list1).toHaveLength(3)
})
})

109
utils.ts
View File

@@ -1,2 +1,111 @@
import {Event} from './event'
export const utf8Decoder = new TextDecoder('utf-8')
export const utf8Encoder = new TextEncoder()
export function normalizeURL(url: string): string {
let p = new URL(url)
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 = ''
p.searchParams.sort()
p.hash = ''
return p.toString()
}
//
// fast insert-into-sorted-array functions adapted from https://github.com/terrymorse58/fast-sorted-array
//
export function insertEventIntoDescendingList(
sortedArray: Event[],
event: Event
) {
let start = 0
let end = sortedArray.length - 1
let midPoint
let position = start
if (end < 0) {
position = 0
} else if (event.created_at < sortedArray[end].created_at) {
position = end + 1
} else if (event.created_at >= sortedArray[start].created_at) {
position = start
} else
while (true) {
if (end <= start + 1) {
position = end
break
}
midPoint = Math.floor(start + (end - start) / 2)
if (sortedArray[midPoint].created_at > event.created_at) {
start = midPoint
} else if (sortedArray[midPoint].created_at < event.created_at) {
end = midPoint
} else {
// aMidPoint === num
position = midPoint
break
}
}
// insert when num is NOT already in (no duplicates)
if (sortedArray[position]?.id !== event.id) {
return [
...sortedArray.slice(0, position),
event,
...sortedArray.slice(position)
]
}
return sortedArray
}
export function insertEventIntoAscendingList(
sortedArray: Event[],
event: Event
) {
let start = 0
let end = sortedArray.length - 1
let midPoint
let position = start
if (end < 0) {
position = 0
} else if (event.created_at > sortedArray[end].created_at) {
position = end + 1
} else if (event.created_at <= sortedArray[start].created_at) {
position = start
} else
while (true) {
if (end <= start + 1) {
position = end
break
}
midPoint = Math.floor(start + (end - start) / 2)
if (sortedArray[midPoint].created_at < event.created_at) {
start = midPoint
} else if (sortedArray[midPoint].created_at > event.created_at) {
end = midPoint
} else {
// aMidPoint === num
position = midPoint
break
}
}
// insert when num is NOT already in (no duplicates)
if (sortedArray[position]?.id !== event.id) {
return [
...sortedArray.slice(0, position),
event,
...sortedArray.slice(position)
]
}
return sortedArray
}