Skip to content

Commit

Permalink
fix finish pattern in nocapd, update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Feb 11, 2024
1 parent c6dbc0f commit 8ea16fd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
2 changes: 1 addition & 1 deletion apps/nocapd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@nostr-fetch/adapter-nostr-tools": "0.14.1",
"@nostrwatch/controlflow": "^0.0.2",
"@nostrwatch/logger": "^0.0.3",
"@nostrwatch/nocap": "^0.1.9",
"@nostrwatch/nocap": "^0.1.10",
"@nostrwatch/nwcache": "^0.0.2",
"@nostrwatch/publisher": "^0.1.1",
"@nostrwatch/seed": "^0.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/nocap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostrwatch/nocap",
"version": "0.1.9",
"version": "0.1.10",
"main": "src/index.js",
"type": "module",
"license": "MIT",
Expand Down
38 changes: 14 additions & 24 deletions packages/nocap/src/classes/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,38 +168,27 @@ export default class {
}

/**
* maybeTimeoutReject
* Creates a reject function for a timeout scenario
* maybe_timeout
* Creates a resolve function for a timeout scenario
*
* @private
* @param {string} key - The key associated with the timeout
* @returns {Function} - The reject function
*/
maybeTimeoutReject(key){
return (reject) => {
maybe_timeout(key){
return (resolve, reject) => {
const message = `${key} check timed out (after ${this.config.timeout[key]}ms}`
this.logger.debug(message)
return reject({ data: {}, duration: -1, status: "error", message})
const data = this.isWebsocketKey(key)? false: {}
if(key === 'connect' && this.config.rejectOnConnectFailure){
return reject({ data, duration: -1, status: "error", message})
}
else {
return this.finish( key, { data, duration: -1, status: "error", message})
}
}
}

/**
* maybeTimeoutResolve
* Creates a resolve function for a timeout scenario
*
* @private
* @param {string} key - The key associated with the timeout
* @returns {Function} - The reject function
*/
maybeTimeoutResolve(key){
return (resolve) => {
if(this.isWebsocketKey(key))
return resolve({ data: false, duration: -1, status: "error", message: `Websocket connection to relay timed out (after ${this.config.timeout[key]}ms}` })
else
return resolve({ data: {}, duration: -1, status: "error", message: `${key} check timed out (after ${this.config.timeout[key]}ms}` })
}
}

/**
* start
* Creates deferred promise for check (key), validates check (key), validates adapter for given check (key), performs a precheck, handles pre-check results, calls check in the corresponding adapter and returns the deferred's promise.
Expand All @@ -211,7 +200,7 @@ export default class {
*/
async start(key){
this.logger.debug(`${key}: start()`)
const checkDeferred = await this.addDeferred(key, this.maybeTimeoutReject(key))
const checkDeferred = await this.addDeferred(key, this.maybe_timeout(key))
const adapter = this.routeAdapter(key)

if( typeof key !== 'string')
Expand Down Expand Up @@ -310,7 +299,8 @@ export default class {
result.adapters = [ ...new Set( this.results.get('adapters').concat([adapter_name]) ) ]
result.checked_at = Date.now()
result.checked_by = this.config.checked_by
data.duration = this.latency.duration(key)
if(!data?.duration)
data.duration = this.latency.duration(key)
result[key] = { ...data }
return result
}
Expand Down
4 changes: 2 additions & 2 deletions packages/nocap/src/classes/DeferredWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export class DeferredWrapper {
this.timeout.create(key, timeout, () => {
if(timeoutCb instanceof Function) {
try {
timeoutCb(deferred.reject)
timeoutCb(deferred.resolve, deferred.reject)
}
catch(e) { this.logger.error(`error in timeout callback for ${key}: ${e.message}` ) }
}
else {
this.reject(key, { status: "error", message: `timeout of ${timeout}ms exceeded for ${key}` })
this.resolve(key, { status: "error", message: `timeout of ${timeout}ms exceeded for ${key}` })
}
})
return deferred
Expand Down
1 change: 1 addition & 0 deletions packages/nocap/src/interfaces/ConfigInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const ConfigDefaults = {
},
tor: {},
adapterOptions: {},
rejectOnConnectFailure: false
}

/**
Expand Down

0 comments on commit 8ea16fd

Please sign in to comment.