Skip to content

Commit dc926ed

Browse files
committed
added output without headers option and updated base tests
1 parent 889eed5 commit dc926ed

File tree

4 files changed

+271
-178
lines changed

4 files changed

+271
-178
lines changed

packages/nocap/src/classes/Base.js

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,33 @@ export default class {
7272
return this.check('all')
7373
}
7474

75-
async check(keys){
76-
if(keys === "all") {
77-
await this.check(this.checks)
78-
return this.results.raw()
79-
}
80-
81-
if(typeof keys === 'string')
82-
return this._check(keys)
75+
async rawResult(key, raw){
8376

84-
if(keys instanceof Array && keys.length) {
85-
let result = {}
77+
}
78+
79+
async check(keys, raw=true){
80+
let result
81+
if(keys == "all") {
82+
console.log('check all')
83+
return this.check(this.checks)
84+
}
85+
else if(typeof keys === 'string') {
86+
result = await this._check(keys)
87+
}
88+
else if(keys instanceof Array && keys.length) {
8689
for(const key of keys){
87-
result = { ...result, ...await this._check(key) }
90+
await this._check(key)
8891
}
89-
return new Promise(resolve => resolve(result))
92+
result = this.results.raw(keys)
93+
}
94+
else {
95+
return this.throw(`check(${keys}) failed. keys must be string or populated array`)
9096
}
91-
return this.throw(`check(${keys}) failed. keys must be string or populated array`)
97+
return raw? result: this.results.cleanResult(keys, result)
9298
}
9399

94100
async _check(key){
95-
this.logger.debug(`check(${key})`)
101+
this.logger.debug(`${key}: check()`)
96102
this.defaultAdapters()
97103
await this.start(key)
98104
const result = await this.promises.get(key).promise
@@ -121,16 +127,16 @@ export default class {
121127
this.adapters[adapter][`check_${key}`]()
122128
})
123129
.catch((precheck) => {
124-
if(precheck.error === true) {
125-
this.logger.debug(`${key}: precheck rejected with error`)
126-
this.logger.error(`Error in ${key} precheck: ${precheck.error}`)
127-
this.promises.get(key).resolve({ [key]: false, [`${key}Latency`]: -1, ...precheck })
128-
}
129-
else if(key === 'connect' && precheck.error === false && precheck?.result){
130+
if(key === 'connect' && precheck.status == "error" && precheck?.result){
130131
this.logger.debug(`${key}: precheck rejected with cached result`)
131132
this.logger.warn(`Precheck found that connect check was already fulfilled, returning cached result`)
132133
this.promises.get(key).resolve(precheck.result)
133134
}
135+
else if(precheck.status == "error") {
136+
this.logger.debug(`${key}: precheck rejected with error`)
137+
this.logger.err(`Error in ${key} precheck: ${precheck.error}`)
138+
this.promises.get(key).resolve({ [key]: false, [`${key}Latency`]: -1, ...precheck })
139+
}
134140
else {
135141
throw new Error(`start(): precheck rejection for ${key} should not ever get here: ${JSON.stringify(precheck)}`)
136142
}
@@ -149,9 +155,10 @@ export default class {
149155
const adapter_name = this.adapters[adapter_key].constructor.name
150156
const adapters = [ ...new Set( this.results.get('adapters').concat([adapter_name]) ) ]
151157
const checked_at = Date.now()
152-
data.duration = this.latency.duration(key)
153-
this.results.setMany({ checked_at, adapters, [key]: {...data} })
154-
this.promises.get(key).resolve({ url, network, checked_at, adapters, ...data })
158+
data.duration = this.latency.duration(key)
159+
const result = { url, network, checked_at, adapters, [key]: {...data} }
160+
this.results.setMany(result)
161+
this.promises.get(key).resolve(result)
155162
this.on_change()
156163
}
157164

@@ -214,21 +221,7 @@ export default class {
214221
//Websocket is not connecting, key is not connect
215222
if( !keyIsConnect && !this.isConnected()) {
216223
this.logger.debug(`${key}: prechecker(): websocket is not connecting, key is not connect`)
217-
const error = { status: "error", message: `Cannot check ${key}, no active websocket connection to relay` }
218-
if(connectAttempted){
219-
this.logger.debug(`${key}: prechecker(): websocket is not connecting, key is not connect, connectAttempted is true`)
220-
this.logger.warn(`Error in ${key} precheck: ${error.message}`)
221-
return rejectPrecheck(error)
222-
}
223-
const result = await this.check('connect')
224-
if(result.connect){
225-
this.logger.debug(`${key}: prechecker(): websocket is not connecting, key is not connect, connectAttempted is false, connect check succeeded`)
226-
return resolvePrecheck()
227-
}
228-
else {
229-
this.logger.debug(`${key}: prechecker(): websocket is not connecting, key is not connect, connectAttempted is false, connect check failed`)
230-
return rejectPrecheck(error)
231-
}
224+
return rejectPrecheck({ status: "error", message: `Cannot check ${key}, no active websocket connection to relay` })
232225
}
233226
this.logger.debug(`${key}: Made it here without resolving or rejecting precheck. You missed something.`)
234227
}

0 commit comments

Comments
 (0)