Skip to content

Commit

Permalink
Merge pull request #10 from dskvr/hotfix/error-handling
Browse files Browse the repository at this point in the history
stability
  • Loading branch information
dskvr authored Dec 20, 2022
2 parents 092a7ff + 6363f82 commit b2ad5f1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
14 changes: 0 additions & 14 deletions config.yml

This file was deleted.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 21 additions & 17 deletions src/inspector.js
Original file line number Diff line number Diff line change
@@ -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={})
{
Expand Down Expand Up @@ -43,6 +44,9 @@ Inspector.prototype.run = async function() {
return this
}

Inspector.prototype.close = async function() {
this.relay.close()
}

Inspector.prototype.setup = function(opts){

Expand Down Expand Up @@ -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)
Expand All @@ -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(){
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import config from '../config.yml'
import config from '../config.js'

export const Result = {
uri: "",
Expand Down
8 changes: 8 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const isJson = function(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}

0 comments on commit b2ad5f1

Please sign in to comment.