mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-10 09:08:50 +00:00
cleanup nip-11.
This commit is contained in:
@@ -1,27 +1,15 @@
|
|||||||
import {Nip11} from './nip11'
|
import fetch from 'node-fetch'
|
||||||
const requestRelayInfos = Nip11.requestRelayInfos
|
import { useFetchImplementation, fetchRelayInformation } from './nip11'
|
||||||
|
|
||||||
describe('requesting Relay infos as for NIP11', () => {
|
describe('requesting relay as for NIP11', () => {
|
||||||
test('testing damus relay', async () => {
|
useFetchImplementation(fetch)
|
||||||
const expected_relay_name = 'relay.nostr.nu'
|
|
||||||
const expected_relay_description =
|
|
||||||
'A nostr relay build by Edward Hollander.'
|
|
||||||
const expected_supported_nips = [
|
|
||||||
1, 2, 4, 9, 11, 12, 15, 16, 20, 22, 26, 28, 33, 40
|
|
||||||
]
|
|
||||||
|
|
||||||
const test_relay = 'https://relay.nostr.nu'
|
test('testing a relay', async () => {
|
||||||
const relay_infos = await requestRelayInfos(test_relay)
|
const info = await fetchRelayInformation('wss://atlas.nostr.land')
|
||||||
const relay_name = relay_infos.name
|
expect(info.name).toEqual('nostr.land')
|
||||||
const relay_description = relay_infos.description
|
expect(info.description).toEqual('nostr.land family of relays (us-or-01)')
|
||||||
const fees = relay_infos.fees
|
expect(info.fees).toBeTruthy()
|
||||||
const admission = fees?.admission
|
expect(info.supported_nips).toEqual([1, 2, 4, 9, 11, 12, 16, 20, 22, 28, 33, 40])
|
||||||
const supported_nips = relay_infos.supported_nips
|
expect(info.software).toEqual('custom')
|
||||||
const admission_condition = Array.isArray(admission)
|
|
||||||
expect(relay_name).toBe(expected_relay_name)
|
|
||||||
expect(relay_description).toBe(expected_relay_description)
|
|
||||||
expect(fees).toBeTruthy()
|
|
||||||
expect(admission_condition).toBeTruthy()
|
|
||||||
expect(supported_nips).toMatchObject(expected_supported_nips)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
71
nip11.ts
71
nip11.ts
@@ -1,39 +1,19 @@
|
|||||||
// #85 I created an implementation of each of the different
|
var _fetch: any
|
||||||
// types described in the NIP11.
|
|
||||||
import fetch from 'node-fetch'
|
|
||||||
|
|
||||||
export namespace Nip11 {
|
try {
|
||||||
export interface requestRelayInfos<
|
_fetch = fetch
|
||||||
N extends string[],
|
} catch {}
|
||||||
A extends boolean,
|
|
||||||
P extends boolean
|
|
||||||
> {
|
|
||||||
(relay_addr: string): Promise<RelayInfos<N, A, P>>
|
|
||||||
}
|
|
||||||
|
|
||||||
// I wanted to use an enum, but eslint is giving me
|
export function useFetchImplementation(fetchImplementation: any) {
|
||||||
// problems!
|
_fetch = fetchImplementation
|
||||||
|
}
|
||||||
|
|
||||||
// export enum headers_accept {
|
export async function fetchRelayInformation(url: string) {
|
||||||
// nostr_json = 'application/nostr+json'
|
return (await (
|
||||||
// }
|
await fetch(url.replace('ws://', 'http://').replace('wss://', 'https://'), {
|
||||||
|
headers: { Accept: 'application/nostr+json' },
|
||||||
export const requestRelayInfos: requestRelayInfos<any, any, any> = (
|
|
||||||
relay_addr: string
|
|
||||||
) => {
|
|
||||||
return new Promise(async (res, rej) => {
|
|
||||||
try {
|
|
||||||
const accept = 'application/nostr+json'
|
|
||||||
const init = {headers: {accept}}
|
|
||||||
const response = await fetch(relay_addr, init)
|
|
||||||
const relayInfos: RelayInfosTemplate<any> =
|
|
||||||
(await response.json()) as RelayInfosTemplate<any>
|
|
||||||
res(relayInfos)
|
|
||||||
} catch (error) {
|
|
||||||
rej(error)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
).json()) as RelayInformation
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,13 +40,13 @@ export namespace Nip11 {
|
|||||||
* @param software identifying relay software URL
|
* @param software identifying relay software URL
|
||||||
* @param version string version identifier
|
* @param version string version identifier
|
||||||
*/
|
*/
|
||||||
export interface RelayInfosTemplate<N extends string[]> {
|
export interface BasicRelayInformation {
|
||||||
// string identifying relay
|
// string identifying relay
|
||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
pubkey: string
|
pubkey: string
|
||||||
contact: string
|
contact: string
|
||||||
supported_nips: N
|
supported_nips: number[]
|
||||||
software: string
|
software: string
|
||||||
version: string
|
version: string
|
||||||
// limitation?: Limitations<A, P>
|
// limitation?: Limitations<A, P>
|
||||||
@@ -124,7 +104,7 @@ export interface RelayInfosTemplate<N extends string[]> {
|
|||||||
* @param payment_required this relay requires payment
|
* @param payment_required this relay requires payment
|
||||||
* before a new connection may perform any action.
|
* before a new connection may perform any action.
|
||||||
*/
|
*/
|
||||||
export interface Limitations<A extends boolean, P extends boolean> {
|
export interface Limitations {
|
||||||
max_message_length: number
|
max_message_length: number
|
||||||
max_subscription: number
|
max_subscription: number
|
||||||
max_filters: number
|
max_filters: number
|
||||||
@@ -134,19 +114,16 @@ export interface Limitations<A extends boolean, P extends boolean> {
|
|||||||
max_event_tags: number
|
max_event_tags: number
|
||||||
max_content_length: number
|
max_content_length: number
|
||||||
min_pow_difficulty: number
|
min_pow_difficulty: number
|
||||||
auth_required: A
|
auth_required: boolean
|
||||||
payment_required: P
|
payment_required: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
type range<L extends number, H extends number> = [L, H]
|
interface RetentionDetails {
|
||||||
type anyRange = range<any, any>
|
kinds: (number | number[])[]
|
||||||
type genericKinds = (number | anyRange)[]
|
|
||||||
interface RetentionDetails<K extends genericKinds> {
|
|
||||||
kinds: K
|
|
||||||
time?: number | null
|
time?: number | null
|
||||||
count?: number | null
|
count?: number | null
|
||||||
}
|
}
|
||||||
type AnyRetentionDetails = RetentionDetails<any>
|
type AnyRetentionDetails = RetentionDetails
|
||||||
/**
|
/**
|
||||||
* ### Event Retention
|
* ### Event Retention
|
||||||
|
|
||||||
@@ -300,13 +277,9 @@ export interface Icon {
|
|||||||
icon: string
|
icon: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RelayInfos<
|
export type RelayInformation = BasicRelayInformation &
|
||||||
N extends string[],
|
|
||||||
A extends boolean,
|
|
||||||
P extends boolean
|
|
||||||
> = RelayInfosTemplate<N> &
|
|
||||||
Partial<Retention> & {
|
Partial<Retention> & {
|
||||||
limitation?: Partial<Limitations<A, P>>
|
limitation?: Partial<Limitations>
|
||||||
} & Partial<ContentLimitations> &
|
} & Partial<ContentLimitations> &
|
||||||
Partial<CommunityPreferences> &
|
Partial<CommunityPreferences> &
|
||||||
Partial<PayToRelay> &
|
Partial<PayToRelay> &
|
||||||
|
|||||||
Reference in New Issue
Block a user