Skip to content

Commit

Permalink
add convenience method and fix websocket-less tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Nov 28, 2023
1 parent 5b43c33 commit 69f5dab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/nocap/adapters/default/GeoAdapterDefault/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { fetch } from 'cross-fetch'
const ipArr = this.$.results.get('ipv4')
const ip = ipArr[ipArr?.length-1]
if(!ip)
this.$.finish('geo', { geo: { error: 'No IP address found' }})
this.$.finish('geo', { geo: { error: 'No IP address. Run dns check first.' }})
if(this.config?.auth?.ip_api_key)
endpoint = `https://pro.ip-api.com/json/${ip}?key=${this.config.auth.ip_api_key}`
else
Expand Down
53 changes: 25 additions & 28 deletions packages/nocap/src/classes/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default class {
this.latency = new LatencyHelper(this.session)
this.promises = new DeferredWrapper(this.session, this.timeouts)
this.logger = new Logger(url, this.config.logLevel)
this.customChecks = {}
//
this.SAMPLE_EVENT = SAMPLE_EVENT
//
Expand All @@ -68,15 +69,28 @@ export default class {
}

async checkAll(){
this.logger.debug(`checkAll()`)
this.defaultAdapters()
for(const check of this.checks) {
await this.check(check)
return this.check('all')
}

async check(keys){
if(keys === "all") {
await this.check(this.checks)
return this.results.dump()
}

if(typeof keys === 'string')
return this._check(keys)

if(keys instanceof Array) {
let result = {}
for(const key of keys){
result = { ...result, ...await this._check(key) }
}
return new Promise(resolve => resolve(result))
}
return this.results.dump()
}

async check(key){
async _check(key){
this.logger.debug(`check(${key})`)
this.defaultAdapters()
await this.start(key)
Expand All @@ -87,24 +101,8 @@ export default class {
return result
}

// async _check(keys){
// if(keys === "all")
// return this.check(this.checks)

// if(typeof keys === 'string')
// return this._check(keys)

// if(keys instanceof Array) {
// let result = {}
// for(const key of keys){
// result = { ...result, ...await this.check(key) }
// }
// return new Promise(resolve => resolve(result))
// }
// }

async start(key){
this.logger.debug(`start(${key})`)
this.logger.debug(`${key}: start()`)
const deferred = this.addDeferred(key)
const adapter = this.routeAdapter(key)

Expand Down Expand Up @@ -132,7 +130,7 @@ export default class {
this.promises.get(key).resolve(precheck.result)
}
else {
throw new Error(`start(): precheck rejection for ${key} should not ever get here.`)
throw new Error(`start(): precheck rejection for ${key} should not ever get here: ${JSON.stringify(precheck)}`)
}
deferred.reject()
})
Expand All @@ -152,13 +150,12 @@ export default class {
}

async precheck(key){
this.logger.debug(`${key}: precheck()`)
const deferred = this.addDeferred(`precheck_${key}`)
const needsWebsocket = ['connect', 'read', 'write'].includes(key)
const keyIsConnect = key === 'connect'
const resolvePrecheck = deferred.resolve
const rejectPrecheck = deferred.reject
const connectAttempted = this.promises.reflect('connect').state.isFulfilled
const connectAttempted = this.promises.exists('connect') && this.promises.reflect('connect').state.isFulfilled

const waitForConnection = async () => {
this.logger.debug(`${key}: waitForConnection()`)
Expand All @@ -171,7 +168,7 @@ export default class {
}

const prechecker = async () => {
this.logger.debug(`${key}: prechecker()`)
this.logger.debug(`${key}: prechecker(): needs websocket: ${needsWebsocket}, key is connect: ${keyIsConnect}, connectAttempted: ${connectAttempted}`)
//Doesn't need websocket. Resolve precheck immediately.

if( !needsWebsocket ){
Expand Down Expand Up @@ -516,7 +513,7 @@ export default class {
}

addDeferred(key){
const existingDeferred = this.promises.get(key)
const existingDeferred = this.promises.exists(key)
if(!existingDeferred)
this.promises.add(key, this.config?.[`${key}_timeout`])
return this.promises.get(key)
Expand Down
10 changes: 8 additions & 2 deletions packages/nocap/src/classes/DeferredWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export class DeferredWrapper {
}

reflect(key) {
const promise = this.get(key).promise;
const promise = this.get(key).promise;
if(!promise)
return false
const state = { isFulfilled: false, isRejected: false, isPending: true };
const reflectedPromise = promise
.then(
Expand All @@ -57,8 +59,12 @@ export class DeferredWrapper {
return this.get(key)
}

exists(key){
return this.promises?.[this.session()]?.[key]
}

get(key){
const deferred = this.promises[this.session()]?.[key]
const deferred = this.promises[this.session()][key]
return deferred
}

Expand Down

0 comments on commit 69f5dab

Please sign in to comment.