removed useless code
Massimo Melina committed
Nov 5, 2023 at 19:22 UTC
1c0e2f8dc43975e340703686baca29abf40705b2
1 file changed
+27
-37
src/selfCheck.ts
+27
-37
@@ -6,13 +6,9 @@ import _ from 'lodash'
6
import { haveTimeout } from './cross'
7
import { httpString } from './util-http'
8
9
-let selfCheckMiddlewareEnabled = false
9
const CHECK_URL = SPECIAL_URI + 'self-check'
11
-export const selfCheckMiddleware: Middleware = (ctx, next) => { // koa format
12
- if (!selfCheckMiddlewareEnabled || !ctx.url.startsWith(CHECK_URL))
13
- return next()
14
- ctx.body = 'HFS'
15
-}
10
+export const selfCheckMiddleware: Middleware = (ctx, next) => // koa format
11
+ ctx.url.startsWith(CHECK_URL) ? ctx.body = 'HFS' : next()
12
13
export async function selfCheck(url: string) {
14
interface PortScannerService {
@@ -26,39 +22,33 @@ export async function selfCheck(url: string) {
22
}
23
const prjInfo = await getProjectInfo()
24
console.log(`checking server ${url}`)
29
- selfCheckMiddlewareEnabled = true
30
- try {
31
- const parsed = new URL(url)
32
- const family = !isIP(parsed.hostname) ? undefined : isIPv6(parsed.hostname) ? 6 : 4
33
- for (const services of _.chunk(_.shuffle<PortScannerService>(prjInfo.selfCheckServices), 2)) {
34
- try {
35
- return await Promise.any(services.map(async (svc) => {
36
- if (!svc.url || svc.type) throw 'unsupported ' + svc.type // only default type supported for now
37
- let { url: serviceUrl, body, regexpSuccess, regexpFailure, ...rest } = svc
38
- const service = new URL(serviceUrl).hostname
39
- console.log('trying external service', service)
40
- body = applySymbols(body)
41
- serviceUrl = applySymbols(serviceUrl)!
42
- const res = await haveTimeout(6_000, httpString(serviceUrl, { family, ...rest, body }))
43
- const success = new RegExp(regexpSuccess).test(res)
44
- const failure = new RegExp(regexpFailure).test(res)
45
- if (success === failure) throw 'inconsistent: ' + service + ': ' + res // this result cannot be trusted
46
- console.debug(service, 'responded', success)
47
- return { success, service, url }
48
- }))
49
- }
50
- catch (e: any) {
51
- console.debug(e?.errors?.map(String) || e?.cause || String(e))
52
- }
25
+ const parsed = new URL(url)
26
+ const family = !isIP(parsed.hostname) ? undefined : isIPv6(parsed.hostname) ? 6 : 4
27
+ for (const services of _.chunk(_.shuffle<PortScannerService>(prjInfo.selfCheckServices), 2)) {
28
+ try {
29
+ return await Promise.any(services.map(async (svc) => {
30
+ if (!svc.url || svc.type) throw 'unsupported ' + svc.type // only default type supported for now
31
+ let { url: serviceUrl, body, regexpSuccess, regexpFailure, ...rest } = svc
32
+ const service = new URL(serviceUrl).hostname
33
+ console.log('trying external service', service)
34
+ body = applySymbols(body)
35
+ serviceUrl = applySymbols(serviceUrl)!
36
+ const res = await haveTimeout(6_000, httpString(serviceUrl, { family, ...rest, body }))
37
+ const success = new RegExp(regexpSuccess).test(res)
38
+ const failure = new RegExp(regexpFailure).test(res)
39
+ if (success === failure) throw 'inconsistent: ' + service + ': ' + res // this result cannot be trusted
40
+ console.debug(service, 'responded', success)
41
+ return { success, service, url }
42
+ }))
43
}
54
-
55
- function applySymbols(s?: string) {
56
- return s?.replace('$IP', parsed.hostname)
57
- .replace('$PORT', parsed.port || (parsed.protocol === 'https:' ? '443' : '80'))
58
- .replace('$URL', url.replace(/\/$/, '') + CHECK_URL)
44
+ catch (e: any) {
45
+ console.debug(e?.errors?.map(String) || e?.cause || String(e))
46
}
47
}
61
- finally {
62
- selfCheckMiddlewareEnabled = false
48
+
49
+ function applySymbols(s?: string) {
50
+ return s?.replace('$IP', parsed.hostname)
51
+ .replace('$PORT', parsed.port || (parsed.protocol === 'https:' ? '443' : '80'))
52
+ .replace('$URL', url.replace(/\/$/, '') + CHECK_URL)
53
}
54
}