basic NIP-AD implementation.

This commit is contained in:
fiatjaf
2026-08-19 17:25:30 -03:00
parent a2fd116f81
commit 70eabad612
3 changed files with 55 additions and 2 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@nostr/tools", "name": "@nostr/tools",
"version": "2.24.2", "version": "2.24.3",
"exports": { "exports": {
".": "./index.ts", ".": "./index.ts",
"./core": "./core.ts", "./core": "./core.ts",
+47
View File
@@ -0,0 +1,47 @@
import { Filter } from './filter.ts'
export type WebAddressPointer = {
filter: Filter
relays?: string[]
}
/**
* NIP-AD regex. Matches a web URL with a path that may have a Nostr counterpart.
*
* - 0: full match
* - 1: domain
* - 2: path
*/
export const AD_REGEX = /^(?:https?:\/\/)?((?:[\w-]+\.)+[\w-]+)(\/[^\s]*)?$/
export const isWebAddress = (value?: string | null): value is string => AD_REGEX.test(value || '')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let _fetch: any
try {
_fetch = fetch
} catch (_) {
null
}
export function useFetchImplementation(fetchImplementation: unknown) {
_fetch = fetchImplementation
}
export async function queryWebAddress(url: string): Promise<WebAddressPointer | null> {
const match = url.match(AD_REGEX)
if (!match) return null
const [, domain, path = '/'] = match
try {
const res = await _fetch(`https://${domain}/.well-known/nostr.json?path=${path}`)
if (res.status !== 200) {
throw Error('Wrong response code')
}
const json = await res.json()
return json[path] || null
} catch (_) {
return null
}
}
+7 -1
View File
@@ -1,7 +1,7 @@
{ {
"type": "module", "type": "module",
"name": "nostr-tools", "name": "nostr-tools",
"version": "2.24.2", "version": "2.24.3",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",
@@ -258,6 +258,12 @@
"require": "./lib/cjs/nip98.js", "require": "./lib/cjs/nip98.js",
"types": "./lib/types/nip98.d.ts" "types": "./lib/types/nip98.d.ts"
}, },
"./nipad": {
"source": "./nipad.ts",
"import": "./lib/esm/nipad.js",
"require": "./lib/cjs/nipad.js",
"types": "./lib/types/nipad.d.ts"
},
"./nip99": { "./nip99": {
"source": "./nip99.ts", "source": "./nip99.ts",
"import": "./lib/esm/nip99.js", "import": "./lib/esm/nip99.js",