diff --git a/config.yml b/config.yml deleted file mode 100644 index 825ceff..0000000 --- a/config.yml +++ /dev/null @@ -1,14 +0,0 @@ -nipsTotal: 40 -millis: - connectTimeout: 15000 - readTimeout: 15000 - writeTimeout: 15000 - clearTimeoutBuffer: 1000 -testEvent: - id: '41ce9bc50da77dda5542f020370ecc2b056d8f2be93c1cedf1bf57efcab095b0' - pubkey: '5a462fa6044b4b8da318528a6987a45e3adf832bd1c64bd6910eacfecdf07541' - created_at: 1640305962 - kind: 1 - tags: [] - content: 'running branle' - sig: '08e6303565e9282f32bed41eee4136f45418f366c0ec489ef4f90d13de1b3b9fb45e14c74f926441f8155236fb2f6fef5b48a5c52b19298a0585a2c06afe39ed' diff --git a/package.json b/package.json index fa57a02..8228364 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,9 @@ "test": "tape test/*.js" }, "dependencies": { + "alby": "1.0.1", + "js-yaml": "4.1.0", + "node-fetch": "3.3.0", "nostr": "0.2.5", "nostr-relay-inspector": "0.0.3", "tape": "5.6.1", diff --git a/src/inspector.js b/src/inspector.js index a9e244f..b0015f4 100644 --- a/src/inspector.js +++ b/src/inspector.js @@ -1,9 +1,10 @@ /* eslint-disable */ -import config from '../config.yml' import Observation from './observation.js' import { Relay } from 'nostr' import crypto from 'crypto' import {Result, Opts, Inbox, Timeout, Info} from './types.js' +import config from '../config.js' +import { isJson } from './util.js' export default function Inspector(relay, opts={}) { @@ -43,6 +44,9 @@ Inspector.prototype.run = async function() { return this } +Inspector.prototype.close = async function() { + this.relay.close() +} Inspector.prototype.setup = function(opts){ @@ -110,13 +114,9 @@ Inspector.prototype.getInfoRemote = async function(){ headers = { "Accept": "application/nostr+json", } - - + let res = await fetch(`https://${url.hostname}/`, { method: 'GET', headers: headers}) - .then(response => { - try { JSON.parse(JSON.stringify(response)) } catch (e) { return false; } - return response.json() - }) + .then(response => isJson(response) ? response.json().catch() : false ) .catch(err => this.cbcall('error', err)) ; if(this.opts.debug) @@ -128,16 +128,18 @@ Inspector.prototype.getInfoRemote = async function(){ Inspector.prototype.getIdentities = async function() { const url = new URL(this.relay.url) + try { + let res = await fetch(`https://${url.hostname}/.well-known/nostr.json`) + .then(response => isJson(response) ? response.json().catch() : false) + .catch(); + if(this.opts.debug) + console.log(`https://${url.hostname}/`, 'check_nip_5', res) - let res = await fetch(`https://${url.hostname}/.well-known/nostr.json`) - .then(response => response.json()) - .catch(err => console.log(err)); - - if(this.opts.debug) - console.log(`https://${url.hostname}/`, 'check_nip_5', res) - - return res && Object.prototype.hasOwnProperty.call(res, 'names') ? res.names : false + return res && Object.prototype.hasOwnProperty.call(res, 'names') ? res.names : false + } catch(e) { + "" + } } Inspector.prototype.checkLatency = function(){ @@ -278,10 +280,12 @@ Inspector.prototype.handle_event = function(subid, event) { */ Inspector.prototype.on_open = async function(e) { - if(this.opts.debug) console.log(this.relay.url, "on_open") + if(this.opts.debug) + console.log(this.relay.url, "on_open") //debug.info(url, "OPEN") - if(this.opts.debug) console.dir(this) + if(this.opts.debug) + console.dir(this) setTimeout( () => { clearTimeout(this.timeout.connect) diff --git a/src/types.js b/src/types.js index c233877..7e18150 100644 --- a/src/types.js +++ b/src/types.js @@ -1,4 +1,4 @@ -import config from '../config.yml' +import config from '../config.js' export const Result = { uri: "", diff --git a/src/util.js b/src/util.js index e69de29..c3ef4a9 100644 --- a/src/util.js +++ b/src/util.js @@ -0,0 +1,8 @@ +export const isJson = function(str) { + try { + JSON.parse(str); + } catch (e) { + return false; + } + return true; +} \ No newline at end of file