mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-10 09:08:50 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf55ad6b5a | ||
|
|
04a46b815c | ||
|
|
165ff44dff | ||
|
|
7bfd23af3c | ||
|
|
3d93ec8446 |
@@ -120,7 +120,7 @@ let event = await relay.get({
|
|||||||
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||||
})
|
})
|
||||||
|
|
||||||
await relay.close()
|
relay.close()
|
||||||
```
|
```
|
||||||
|
|
||||||
To use this on Node.js you first must install `websocket-polyfill` and import it:
|
To use this on Node.js you first must install `websocket-polyfill` and import it:
|
||||||
|
|||||||
10
build.js
10
build.js
@@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const fs = require('fs')
|
||||||
const esbuild = require('esbuild')
|
const esbuild = require('esbuild')
|
||||||
|
|
||||||
let common = {
|
let common = {
|
||||||
@@ -11,11 +12,16 @@ let common = {
|
|||||||
esbuild
|
esbuild
|
||||||
.build({
|
.build({
|
||||||
...common,
|
...common,
|
||||||
outfile: 'lib/nostr.esm.js',
|
outfile: 'lib/esm/nostr.mjs',
|
||||||
format: 'esm',
|
format: 'esm',
|
||||||
packages: 'external'
|
packages: 'external'
|
||||||
})
|
})
|
||||||
.then(() => console.log('esm build success.'))
|
.then(() => {
|
||||||
|
const packageJson = JSON.stringify({ type: 'module' })
|
||||||
|
fs.writeFileSync(`${__dirname}/lib/esm/package.json`, packageJson, 'utf8')
|
||||||
|
|
||||||
|
console.log('esm build success.')
|
||||||
|
})
|
||||||
|
|
||||||
esbuild
|
esbuild
|
||||||
.build({
|
.build({
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.5.0",
|
"version": "1.6.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/fiatjaf/nostr-tools.git"
|
"url": "https://github.com/fiatjaf/nostr-tools.git"
|
||||||
},
|
},
|
||||||
"main": "lib/nostr.cjs.js",
|
"main": "lib/nostr.cjs.js",
|
||||||
"module": "lib/nostr.esm.js",
|
"module": "lib/esm/nostr.mjs",
|
||||||
|
"exports": {
|
||||||
|
"import": "./lib/esm/nostr.mjs",
|
||||||
|
"require": "./lib/nostr.cjs.js"
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/hashes": "1.0.0",
|
"@noble/hashes": "1.0.0",
|
||||||
"@noble/secp256k1": "^1.7.1",
|
"@noble/secp256k1": "^1.7.1",
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ let relays = [
|
|||||||
'wss://nostr.zebedee.cloud/'
|
'wss://nostr.zebedee.cloud/'
|
||||||
]
|
]
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(() => {
|
||||||
await pool.close([
|
pool.close([
|
||||||
...relays,
|
...relays,
|
||||||
'wss://nostr-relay.untethr.me',
|
'wss://nostr-relay.untethr.me',
|
||||||
'wss://offchain.pub',
|
'wss://offchain.pub',
|
||||||
|
|||||||
8
pool.ts
8
pool.ts
@@ -12,13 +12,11 @@ export class SimplePool {
|
|||||||
this._conn = {}
|
this._conn = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
async close(relays: string[]): Promise<void> {
|
close(relays: string[]): void {
|
||||||
await Promise.all(
|
relays.map(url => {
|
||||||
relays.map(async url => {
|
|
||||||
let relay = this._conn[normalizeURL(url)]
|
let relay = this._conn[normalizeURL(url)]
|
||||||
if (relay) await relay.close()
|
if (relay) relay.close()
|
||||||
})
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureRelay(url: string): Promise<Relay> {
|
async ensureRelay(url: string): Promise<Relay> {
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ beforeAll(() => {
|
|||||||
relay.connect()
|
relay.connect()
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(() => {
|
||||||
await relay.close()
|
relay.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('connectivity', () => {
|
test('connectivity', () => {
|
||||||
|
|||||||
11
relay.ts
11
relay.ts
@@ -10,7 +10,7 @@ export type Relay = {
|
|||||||
url: string
|
url: string
|
||||||
status: number
|
status: number
|
||||||
connect: () => Promise<void>
|
connect: () => Promise<void>
|
||||||
close: () => Promise<void>
|
close: () => void
|
||||||
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
||||||
list: (filters: Filter[], opts?: SubscriptionOptions) => Promise<Event[]>
|
list: (filters: Filter[], opts?: SubscriptionOptions) => Promise<Event[]>
|
||||||
get: (filter: Filter, opts?: SubscriptionOptions) => Promise<Event | null>
|
get: (filter: Filter, opts?: SubscriptionOptions) => Promise<Event | null>
|
||||||
@@ -37,7 +37,6 @@ export type SubscriptionOptions = {
|
|||||||
|
|
||||||
export function relayInit(url: string): Relay {
|
export function relayInit(url: string): Relay {
|
||||||
var ws: WebSocket
|
var ws: WebSocket
|
||||||
var resolveClose: () => void
|
|
||||||
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
|
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
|
||||||
var listeners: {
|
var listeners: {
|
||||||
connect: Array<() => void>
|
connect: Array<() => void>
|
||||||
@@ -78,7 +77,6 @@ export function relayInit(url: string): Relay {
|
|||||||
}
|
}
|
||||||
ws.onclose = async () => {
|
ws.onclose = async () => {
|
||||||
listeners.disconnect.forEach(cb => cb())
|
listeners.disconnect.forEach(cb => cb())
|
||||||
resolveClose && resolveClose()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let incomingMessageQueue: string[] = []
|
let incomingMessageQueue: string[] = []
|
||||||
@@ -291,16 +289,13 @@ export function relayInit(url: string): Relay {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
connect,
|
connect,
|
||||||
close(): Promise<void> {
|
close(): void {
|
||||||
listeners = {connect: [], disconnect: [], error: [], notice: []}
|
listeners = {connect: [], disconnect: [], error: [], notice: []}
|
||||||
subListeners = {}
|
subListeners = {}
|
||||||
pubListeners = {}
|
pubListeners = {}
|
||||||
|
|
||||||
if (ws.readyState > 1) return Promise.resolve()
|
if (ws.readyState > 1) return
|
||||||
ws.close()
|
ws.close()
|
||||||
return new Promise<void>(resolve => {
|
|
||||||
resolveClose = resolve
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
get status() {
|
get status() {
|
||||||
return ws?.readyState ?? 3
|
return ws?.readyState ?? 3
|
||||||
|
|||||||
Reference in New Issue
Block a user