From 39cfc5c09e79f5486b13985641c9814ad815679f Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 16 Dec 2023 11:00:46 -0300 Subject: [PATCH] cleanup nip-11. --- nip11.test.ts | 34 ++++++++---------------- nip11.ts | 71 ++++++++++++++++----------------------------------- 2 files changed, 33 insertions(+), 72 deletions(-) diff --git a/nip11.test.ts b/nip11.test.ts index ec89689..f52cefc 100644 --- a/nip11.test.ts +++ b/nip11.test.ts @@ -1,27 +1,15 @@ -import {Nip11} from './nip11' -const requestRelayInfos = Nip11.requestRelayInfos +import fetch from 'node-fetch' +import { useFetchImplementation, fetchRelayInformation } from './nip11' -describe('requesting Relay infos as for NIP11', () => { - test('testing damus relay', async () => { - 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 - ] +describe('requesting relay as for NIP11', () => { + useFetchImplementation(fetch) - const test_relay = 'https://relay.nostr.nu' - const relay_infos = await requestRelayInfos(test_relay) - const relay_name = relay_infos.name - const relay_description = relay_infos.description - const fees = relay_infos.fees - const admission = fees?.admission - const supported_nips = relay_infos.supported_nips - 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) + test('testing a relay', async () => { + const info = await fetchRelayInformation('wss://atlas.nostr.land') + expect(info.name).toEqual('nostr.land') + expect(info.description).toEqual('nostr.land family of relays (us-or-01)') + expect(info.fees).toBeTruthy() + expect(info.supported_nips).toEqual([1, 2, 4, 9, 11, 12, 16, 20, 22, 28, 33, 40]) + expect(info.software).toEqual('custom') }) }) diff --git a/nip11.ts b/nip11.ts index 7a8c6d5..e855111 100644 --- a/nip11.ts +++ b/nip11.ts @@ -1,39 +1,19 @@ -// #85 I created an implementation of each of the different -// types described in the NIP11. -import fetch from 'node-fetch' +var _fetch: any -export namespace Nip11 { - export interface requestRelayInfos< - N extends string[], - A extends boolean, - P extends boolean - > { - (relay_addr: string): Promise> - } +try { + _fetch = fetch +} catch {} - // I wanted to use an enum, but eslint is giving me - // problems! +export function useFetchImplementation(fetchImplementation: any) { + _fetch = fetchImplementation +} - // export enum headers_accept { - // nostr_json = 'application/nostr+json' - // } - - export const requestRelayInfos: requestRelayInfos = ( - 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 = - (await response.json()) as RelayInfosTemplate - res(relayInfos) - } catch (error) { - rej(error) - } +export async function fetchRelayInformation(url: string) { + return (await ( + await fetch(url.replace('ws://', 'http://').replace('wss://', 'https://'), { + headers: { Accept: 'application/nostr+json' }, }) - } + ).json()) as RelayInformation } /** @@ -60,13 +40,13 @@ export namespace Nip11 { * @param software identifying relay software URL * @param version string version identifier */ -export interface RelayInfosTemplate { +export interface BasicRelayInformation { // string identifying relay name: string description: string pubkey: string contact: string - supported_nips: N + supported_nips: number[] software: string version: string // limitation?: Limitations @@ -124,7 +104,7 @@ export interface RelayInfosTemplate { * @param payment_required this relay requires payment * before a new connection may perform any action. */ -export interface Limitations { +export interface Limitations { max_message_length: number max_subscription: number max_filters: number @@ -134,19 +114,16 @@ export interface Limitations { max_event_tags: number max_content_length: number min_pow_difficulty: number - auth_required: A - payment_required: P + auth_required: boolean + payment_required: boolean } -type range = [L, H] -type anyRange = range -type genericKinds = (number | anyRange)[] -interface RetentionDetails { - kinds: K +interface RetentionDetails { + kinds: (number | number[])[] time?: number | null count?: number | null } -type AnyRetentionDetails = RetentionDetails +type AnyRetentionDetails = RetentionDetails /** * ### Event Retention @@ -300,13 +277,9 @@ export interface Icon { icon: string } -export type RelayInfos< - N extends string[], - A extends boolean, - P extends boolean -> = RelayInfosTemplate & +export type RelayInformation = BasicRelayInformation & Partial & { - limitation?: Partial> + limitation?: Partial } & Partial & Partial & Partial &