@bsv/overlay-express 2.4.1 → 2.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/LICENSE.txt +57 -27
  2. package/README.md +141 -56
  3. package/dist/cjs/mod.d.ts +11 -0
  4. package/dist/cjs/mod.d.ts.map +1 -0
  5. package/dist/cjs/mod.js.map +1 -1
  6. package/dist/cjs/package.json +36 -23
  7. package/dist/cjs/src/ArcadeProvider.d.ts +37 -0
  8. package/dist/cjs/src/ArcadeProvider.d.ts.map +1 -0
  9. package/dist/cjs/src/ArcadeProvider.js.map +1 -1
  10. package/dist/cjs/src/BanAwareDiscoveryStorage.d.ts +36 -0
  11. package/dist/cjs/src/BanAwareDiscoveryStorage.d.ts.map +1 -0
  12. package/dist/cjs/src/BanAwareDiscoveryStorage.js +5 -4
  13. package/dist/cjs/src/BanAwareDiscoveryStorage.js.map +1 -1
  14. package/dist/cjs/src/BanAwareLookupWrapper.d.ts +31 -0
  15. package/dist/cjs/src/BanAwareLookupWrapper.d.ts.map +1 -0
  16. package/dist/cjs/src/BanAwareLookupWrapper.js +3 -2
  17. package/dist/cjs/src/BanAwareLookupWrapper.js.map +1 -1
  18. package/dist/cjs/src/BanAwareTopicManager.d.ts +28 -0
  19. package/dist/cjs/src/BanAwareTopicManager.d.ts.map +1 -0
  20. package/dist/cjs/src/BanAwareTopicManager.js +3 -2
  21. package/dist/cjs/src/BanAwareTopicManager.js.map +1 -1
  22. package/dist/cjs/src/BanService.d.ts +81 -0
  23. package/dist/cjs/src/BanService.d.ts.map +1 -0
  24. package/dist/cjs/src/BanService.js.map +1 -1
  25. package/dist/cjs/src/ChaintracksProvider.d.ts +29 -0
  26. package/dist/cjs/src/ChaintracksProvider.d.ts.map +1 -0
  27. package/dist/cjs/src/ChaintracksProvider.js +1 -1
  28. package/dist/cjs/src/ChaintracksProvider.js.map +1 -1
  29. package/dist/cjs/src/JanitorService.d.ts +128 -0
  30. package/dist/cjs/src/JanitorService.d.ts.map +1 -0
  31. package/dist/cjs/src/JanitorService.js +108 -37
  32. package/dist/cjs/src/JanitorService.js.map +1 -1
  33. package/dist/cjs/src/OverlayExpress.d.ts +466 -0
  34. package/dist/cjs/src/OverlayExpress.d.ts.map +1 -0
  35. package/dist/cjs/src/OverlayExpress.js +337 -160
  36. package/dist/cjs/src/OverlayExpress.js.map +1 -1
  37. package/dist/cjs/src/OverlayMonitor.d.ts +197 -0
  38. package/dist/cjs/src/OverlayMonitor.d.ts.map +1 -0
  39. package/dist/cjs/src/OverlayMonitor.js +6 -4
  40. package/dist/cjs/src/OverlayMonitor.js.map +1 -1
  41. package/dist/cjs/src/ProviderChainBroadcaster.d.ts +18 -0
  42. package/dist/cjs/src/ProviderChainBroadcaster.d.ts.map +1 -0
  43. package/dist/cjs/src/ProviderChainBroadcaster.js +7 -3
  44. package/dist/cjs/src/ProviderChainBroadcaster.js.map +1 -1
  45. package/dist/cjs/src/ReorgStream.d.ts +74 -0
  46. package/dist/cjs/src/ReorgStream.d.ts.map +1 -0
  47. package/dist/cjs/src/ReorgStream.js +14 -6
  48. package/dist/cjs/src/ReorgStream.js.map +1 -1
  49. package/dist/cjs/src/generalGuide.md.d.ts +3 -0
  50. package/dist/cjs/src/generalGuide.md.d.ts.map +1 -0
  51. package/dist/cjs/src/generalGuide.md.js +1 -1
  52. package/dist/cjs/src/generalGuide.md.js.map +1 -1
  53. package/dist/cjs/src/makeUserInterface.d.ts +22 -0
  54. package/dist/cjs/src/makeUserInterface.d.ts.map +1 -0
  55. package/dist/cjs/src/makeUserInterface.js +4 -4
  56. package/dist/cjs/src/makeUserInterface.js.map +1 -1
  57. package/dist/cjs/src/security/edgePolicy.d.ts +57 -0
  58. package/dist/cjs/src/security/edgePolicy.d.ts.map +1 -0
  59. package/dist/cjs/src/security/edgePolicy.js +379 -0
  60. package/dist/cjs/src/security/edgePolicy.js.map +1 -0
  61. package/dist/esm/mod.js.map +1 -1
  62. package/dist/esm/src/ArcadeProvider.js.map +1 -1
  63. package/dist/esm/src/BanAwareDiscoveryStorage.js +5 -4
  64. package/dist/esm/src/BanAwareDiscoveryStorage.js.map +1 -1
  65. package/dist/esm/src/BanAwareLookupWrapper.js +3 -2
  66. package/dist/esm/src/BanAwareLookupWrapper.js.map +1 -1
  67. package/dist/esm/src/BanAwareTopicManager.js +3 -2
  68. package/dist/esm/src/BanAwareTopicManager.js.map +1 -1
  69. package/dist/esm/src/ChaintracksProvider.js +1 -1
  70. package/dist/esm/src/ChaintracksProvider.js.map +1 -1
  71. package/dist/esm/src/JanitorService.js +84 -8
  72. package/dist/esm/src/JanitorService.js.map +1 -1
  73. package/dist/esm/src/OverlayExpress.js +319 -146
  74. package/dist/esm/src/OverlayExpress.js.map +1 -1
  75. package/dist/esm/src/ProviderChainBroadcaster.js +7 -3
  76. package/dist/esm/src/ProviderChainBroadcaster.js.map +1 -1
  77. package/dist/esm/src/ReorgStream.js +14 -6
  78. package/dist/esm/src/ReorgStream.js.map +1 -1
  79. package/dist/esm/src/generalGuide.md.js +1 -1
  80. package/dist/esm/src/makeUserInterface.js +3 -4
  81. package/dist/esm/src/makeUserInterface.js.map +1 -1
  82. package/dist/esm/src/security/edgePolicy.js +377 -0
  83. package/dist/esm/src/security/edgePolicy.js.map +1 -0
  84. package/dist/types/mod.d.ts +1 -1
  85. package/dist/types/mod.d.ts.map +1 -1
  86. package/dist/types/src/ArcadeProvider.d.ts.map +1 -1
  87. package/dist/types/src/BanAwareDiscoveryStorage.d.ts.map +1 -1
  88. package/dist/types/src/BanAwareLookupWrapper.d.ts.map +1 -1
  89. package/dist/types/src/BanAwareTopicManager.d.ts.map +1 -1
  90. package/dist/types/src/BanService.d.ts.map +1 -1
  91. package/dist/types/src/ChaintracksProvider.d.ts.map +1 -1
  92. package/dist/types/src/JanitorService.d.ts +14 -1
  93. package/dist/types/src/JanitorService.d.ts.map +1 -1
  94. package/dist/types/src/OverlayExpress.d.ts +48 -2
  95. package/dist/types/src/OverlayExpress.d.ts.map +1 -1
  96. package/dist/types/src/OverlayMonitor.d.ts.map +1 -1
  97. package/dist/types/src/ProviderChainBroadcaster.d.ts.map +1 -1
  98. package/dist/types/src/ReorgStream.d.ts.map +1 -1
  99. package/dist/types/src/generalGuide.md.d.ts +1 -1
  100. package/dist/types/src/generalGuide.md.d.ts.map +1 -1
  101. package/dist/types/src/makeUserInterface.d.ts +1 -2
  102. package/dist/types/src/makeUserInterface.d.ts.map +1 -1
  103. package/dist/types/src/security/edgePolicy.d.ts +57 -0
  104. package/dist/types/src/security/edgePolicy.d.ts.map +1 -0
  105. package/package.json +51 -29
  106. package/dist/cjs/tsconfig.cjs.tsbuildinfo +0 -1
  107. package/dist/esm/tsconfig.esm.tsbuildinfo +0 -1
  108. package/dist/types/tsconfig.types.tsbuildinfo +0 -1
  109. package/mod.ts +0 -47
  110. package/src/ArcadeProvider.ts +0 -232
  111. package/src/BanAwareDiscoveryStorage.ts +0 -91
  112. package/src/BanAwareLookupWrapper.ts +0 -95
  113. package/src/BanAwareTopicManager.ts +0 -87
  114. package/src/BanService.ts +0 -154
  115. package/src/ChaintracksProvider.ts +0 -79
  116. package/src/JanitorService.ts +0 -423
  117. package/src/OverlayExpress.ts +0 -2456
  118. package/src/OverlayMonitor.ts +0 -817
  119. package/src/ProviderChainBroadcaster.ts +0 -108
  120. package/src/ReorgStream.ts +0 -221
  121. package/src/__tests__/BanAwareDiscoveryStorage.test.ts +0 -131
  122. package/src/__tests__/BanAwareLookupWrapper.test.ts +0 -255
  123. package/src/__tests__/BanAwareTopicManager.test.ts +0 -132
  124. package/src/__tests__/BanService.test.ts +0 -310
  125. package/src/__tests__/InMemoryMigrationSource.test.ts +0 -11
  126. package/src/__tests__/JanitorService.test.ts +0 -941
  127. package/src/__tests__/OverlayExpress.test.ts +0 -1142
  128. package/src/__tests__/OverlayMonitor.test.ts +0 -255
  129. package/src/__tests__/OverlayProviders.live.test.ts +0 -39
  130. package/src/__tests__/OverlayProviders.test.ts +0 -123
  131. package/src/__tests__/README.md +0 -105
  132. package/src/__tests__/ReorgStream.test.ts +0 -66
  133. package/src/__tests__/makeUserInterface.test.ts +0 -193
  134. package/src/__tests__/setup.ts +0 -36
  135. package/src/auth-express-middleware.d.ts +0 -16
  136. package/src/generalGuide.md.ts +0 -74
  137. package/src/makeUserInterface.ts +0 -1224
@@ -1,79 +0,0 @@
1
- import { ChainTracker } from '@bsv/sdk'
2
-
3
- export interface ChaintracksProviderConfig {
4
- apiPrefix?: string
5
- fetch?: typeof fetch
6
- }
7
-
8
- export interface ChaintracksHeader {
9
- height: number
10
- hash: string
11
- merkleRoot: string
12
- }
13
-
14
- function trimUrl (url: string): string {
15
- let trimmed = url
16
- while (trimmed.endsWith('/')) trimmed = trimmed.slice(0, -1)
17
- return trimmed
18
- }
19
-
20
- function trimPrefix (prefix: string): string {
21
- if (prefix === '') return ''
22
- let trimmed = prefix.startsWith('/') ? prefix : `/${prefix}`
23
- while (trimmed.endsWith('/')) trimmed = trimmed.slice(0, -1)
24
- return trimmed
25
- }
26
-
27
- /**
28
- * Minimal go-chaintracks HTTP client for Overlay Express.
29
- *
30
- * It intentionally implements the SDK ChainTracker surface plus header lookup,
31
- * which is enough for proof validation, BASM headers, and reorg stream URL
32
- * construction without depending on wallet-toolbox client export timing.
33
- */
34
- export class ChaintracksProvider implements ChainTracker {
35
- private readonly baseUrl: string
36
- private readonly fetcher: typeof fetch
37
-
38
- constructor (url: string, config: ChaintracksProviderConfig = {}) {
39
- this.baseUrl = `${trimUrl(url)}${trimPrefix(config.apiPrefix ?? '/chaintracks/v2')}`
40
- this.fetcher = config.fetch ?? fetch
41
- }
42
-
43
- async currentHeight (): Promise<number> {
44
- const response = await this.getJson<{ height: number }>('/height')
45
- return response.height
46
- }
47
-
48
- async isValidRootForHeight (root: string, height: number): Promise<boolean> {
49
- const header = await this.findHeaderForHeight(height)
50
- return header !== undefined && header.merkleRoot === root
51
- }
52
-
53
- async findHeaderForHeight (height: number): Promise<ChaintracksHeader | undefined> {
54
- return await this.getJsonOrUndefined<ChaintracksHeader>(`/header/height/${height}`)
55
- }
56
-
57
- reorgStreamUrl (): string {
58
- return `${this.baseUrl}/reorg/stream`
59
- }
60
-
61
- private async getJson<T>(path: string): Promise<T> {
62
- const value = await this.getJsonOrUndefined<T>(path)
63
- if (value === undefined) {
64
- throw new Error(`Chaintracks returned no value for ${path}`)
65
- }
66
- return value
67
- }
68
-
69
- private async getJsonOrUndefined<T>(path: string): Promise<T | undefined> {
70
- const response = await this.fetcher(`${this.baseUrl}${path}`, {
71
- headers: { Accept: 'application/json' }
72
- })
73
- if (response.status === 404) return undefined
74
- if (!response.ok) {
75
- throw new Error(`Chaintracks request failed for ${path}: ${response.status} ${response.statusText}`)
76
- }
77
- return await response.json() as T
78
- }
79
- }
@@ -1,423 +0,0 @@
1
- import { Db } from 'mongodb'
2
- import chalk from 'chalk'
3
- import { BanService } from './BanService.js'
4
-
5
- /**
6
- * Configuration for the Janitor Service
7
- */
8
- export interface JanitorConfig {
9
- mongoDb: Db
10
- logger?: typeof console
11
- requestTimeoutMs?: number
12
- hostDownRevokeScore?: number
13
- /** Optional BanService for persisting bans when hosts are removed */
14
- banService?: BanService
15
- /** Whether to auto-ban domains when they exceed the down threshold (default: true) */
16
- autoBanOnRemoval?: boolean
17
- }
18
-
19
- /**
20
- * Health status of a single record
21
- */
22
- export interface HostHealthResult {
23
- txid: string
24
- outputIndex: number
25
- domain: string
26
- topic?: string
27
- service?: string
28
- identityKey?: string
29
- createdAt?: Date
30
- healthy: boolean
31
- downCount: number
32
- responseTimeMs?: number
33
- statusCode?: number
34
- error?: string
35
- }
36
-
37
- /**
38
- * Summary report from a janitor run
39
- */
40
- export interface JanitorReport {
41
- startedAt: Date
42
- completedAt: Date
43
- durationMs: number
44
- shipResults: HostHealthResult[]
45
- slapResults: HostHealthResult[]
46
- summary: {
47
- totalChecked: number
48
- healthy: number
49
- unhealthy: number
50
- removed: number
51
- banned: number
52
- }
53
- }
54
-
55
- /**
56
- * JanitorService runs health checks on SHIP and SLAP outputs.
57
- * It validates domain names and checks /health endpoints to ensure services are operational.
58
- *
59
- * When a service is down, it increments a "down" counter. When healthy, it decrements.
60
- * If the down counter reaches hostDownRevokeScore, the output is deleted and optionally
61
- * the domain is added to the persistent ban list to prevent GASP re-sync.
62
- */
63
- export class JanitorService {
64
- private readonly mongoDb: Db
65
- private readonly logger: typeof console
66
- private readonly requestTimeoutMs: number
67
- private readonly hostDownRevokeScore: number
68
- private readonly banService?: BanService
69
- private readonly autoBanOnRemoval: boolean
70
-
71
- constructor (config: JanitorConfig) {
72
- this.mongoDb = config.mongoDb
73
- this.logger = config.logger ?? console
74
- this.requestTimeoutMs = config.requestTimeoutMs ?? 10000
75
- this.hostDownRevokeScore = config.hostDownRevokeScore ?? 3
76
- this.banService = config.banService
77
- this.autoBanOnRemoval = config.autoBanOnRemoval ?? true
78
- }
79
-
80
- /**
81
- * Runs a full pass of health checks on all SHIP and SLAP outputs.
82
- * Returns a detailed report of the results.
83
- */
84
- async run (): Promise<JanitorReport> {
85
- const startedAt = new Date()
86
- this.logger.log(chalk.blue('Running janitor health checks...'))
87
-
88
- let shipResults: HostHealthResult[] = []
89
- let slapResults: HostHealthResult[] = []
90
- let removed = 0
91
- let banned = 0
92
-
93
- try {
94
- const shipCheckResult = await this.checkTopicOutputs('shipRecords', 'topic')
95
- shipResults = shipCheckResult.results
96
- removed += shipCheckResult.removed
97
- banned += shipCheckResult.banned
98
-
99
- const slapCheckResult = await this.checkTopicOutputs('slapRecords', 'service')
100
- slapResults = slapCheckResult.results
101
- removed += slapCheckResult.removed
102
- banned += slapCheckResult.banned
103
-
104
- this.logger.log(chalk.green('Janitor health checks completed'))
105
- } catch (error) {
106
- this.logger.error(chalk.red('Error during health checks:'), error)
107
- throw error
108
- }
109
-
110
- const completedAt = new Date()
111
- const allResults = [...shipResults, ...slapResults]
112
- return {
113
- startedAt,
114
- completedAt,
115
- durationMs: completedAt.getTime() - startedAt.getTime(),
116
- shipResults,
117
- slapResults,
118
- summary: {
119
- totalChecked: allResults.length,
120
- healthy: allResults.filter(r => r.healthy).length,
121
- unhealthy: allResults.filter(r => !r.healthy).length,
122
- removed,
123
- banned
124
- }
125
- }
126
- }
127
-
128
- /**
129
- * Checks a single URL's health endpoint. Used by the admin dashboard for on-demand checks.
130
- */
131
- async checkHost (url: string): Promise<{ healthy: boolean, responseTimeMs: number, statusCode?: number, error?: string }> {
132
- const startTime = Date.now()
133
-
134
- if (!this.isValidDomain(url)) {
135
- return {
136
- healthy: false,
137
- responseTimeMs: Date.now() - startTime,
138
- error: 'Invalid domain'
139
- }
140
- }
141
-
142
- try {
143
- const fullURL = url.startsWith('http') ? url : `https://${url}`
144
- const healthURL = new URL('/health', fullURL).toString()
145
-
146
- const controller = new AbortController()
147
- const timeout = setTimeout(() => controller.abort(), this.requestTimeoutMs)
148
-
149
- try {
150
- const response = await fetch(healthURL, {
151
- method: 'GET',
152
- signal: controller.signal,
153
- headers: { Accept: 'application/json' }
154
- })
155
-
156
- clearTimeout(timeout)
157
- const responseTimeMs = Date.now() - startTime
158
-
159
- if (!response.ok) {
160
- return { healthy: false, responseTimeMs, statusCode: response.status, error: `HTTP ${response.status}` }
161
- }
162
-
163
- const data = await response.json()
164
- const healthy = (data?.status === 'ok' && data?.ready !== false) || (data?.ready === true && data?.live !== false)
165
- return { healthy, responseTimeMs, statusCode: response.status, error: healthy ? undefined : 'Unexpected response' }
166
- } catch (error: any) {
167
- clearTimeout(timeout)
168
- const responseTimeMs = Date.now() - startTime
169
- if (error.name === 'AbortError') {
170
- return { healthy: false, responseTimeMs, error: 'Timeout' }
171
- }
172
- return { healthy: false, responseTimeMs, error: error.message ?? 'Connection failed' }
173
- }
174
- } catch (error: any) {
175
- return { healthy: false, responseTimeMs: Date.now() - startTime, error: error.message ?? 'Invalid URL' }
176
- }
177
- }
178
-
179
- /**
180
- * Gets health status for all records without modifying them.
181
- * Used by the dashboard to display current state.
182
- */
183
- async getHealthStatus (): Promise<{ ship: HostHealthResult[], slap: HostHealthResult[] }> {
184
- const shipCollection = this.mongoDb.collection('shipRecords')
185
- const slapCollection = this.mongoDb.collection('slapRecords')
186
-
187
- const [shipOutputs, slapOutputs] = await Promise.all([
188
- shipCollection.find({}).toArray(),
189
- slapCollection.find({}).toArray()
190
- ])
191
-
192
- const shipResults: HostHealthResult[] = shipOutputs.map(output => ({
193
- txid: output.txid as string,
194
- outputIndex: output.outputIndex as number,
195
- domain: this.extractURLFromOutput(output) ?? 'unknown',
196
- topic: output.topic as string,
197
- identityKey: output.identityKey as string,
198
- createdAt: output.createdAt as Date,
199
- healthy: true, // Will be updated if health check is run
200
- downCount: typeof output.down === 'number' ? output.down : 0
201
- }))
202
-
203
- const slapResults: HostHealthResult[] = slapOutputs.map(output => ({
204
- txid: output.txid as string,
205
- outputIndex: output.outputIndex as number,
206
- domain: this.extractURLFromOutput(output) ?? 'unknown',
207
- service: output.service as string,
208
- identityKey: output.identityKey as string,
209
- createdAt: output.createdAt as Date,
210
- healthy: true,
211
- downCount: typeof output.down === 'number' ? output.down : 0
212
- }))
213
-
214
- return { ship: shipResults, slap: slapResults }
215
- }
216
-
217
- /**
218
- * Checks all outputs for a specific collection and returns results.
219
- */
220
- private async checkTopicOutputs (
221
- collectionName: string,
222
- typeField: 'topic' | 'service'
223
- ): Promise<{ results: HostHealthResult[], removed: number, banned: number }> {
224
- const results: HostHealthResult[] = []
225
- let removed = 0
226
- let banned = 0
227
-
228
- try {
229
- const collection = this.mongoDb.collection(collectionName)
230
- const outputs = await collection.find({}).toArray()
231
-
232
- this.logger.log(chalk.cyan(`Checking ${outputs.length} ${collectionName} outputs...`))
233
-
234
- for (const output of outputs) {
235
- const result = await this.checkOutput(output, collection, typeField)
236
- results.push(result)
237
- if (result.error === 'REMOVED') {
238
- removed++
239
- }
240
- }
241
-
242
- // Count auto-bans that happened during this run
243
- if (this.banService !== undefined && this.autoBanOnRemoval) {
244
- banned = removed // Each removal triggers a ban
245
- }
246
- } catch (error) {
247
- this.logger.error(chalk.red(`Error checking ${collectionName} outputs:`), error)
248
- }
249
-
250
- return { results, removed, banned }
251
- }
252
-
253
- /**
254
- * Checks a single output for health and returns the result.
255
- */
256
- private async checkOutput (
257
- output: Record<string, any>,
258
- collection: any,
259
- typeField: 'topic' | 'service'
260
- ): Promise<HostHealthResult> {
261
- const domain = this.extractURLFromOutput(output) ?? 'unknown'
262
- const baseResult: HostHealthResult = {
263
- txid: output.txid as string,
264
- outputIndex: output.outputIndex as number,
265
- domain,
266
- identityKey: output.identityKey as string,
267
- createdAt: output.createdAt as Date,
268
- healthy: false,
269
- downCount: typeof output.down === 'number' ? output.down : 0
270
- }
271
-
272
- if (typeField === 'topic') {
273
- baseResult.topic = output.topic as string
274
- } else {
275
- baseResult.service = output.service as string
276
- }
277
-
278
- try {
279
- if (domain === 'unknown') {
280
- baseResult.error = 'No URL found'
281
- await this.handleUnhealthyOutput(output, collection, domain)
282
- return baseResult
283
- }
284
-
285
- if (!this.isValidDomain(domain)) {
286
- baseResult.error = 'Invalid domain'
287
- await this.handleUnhealthyOutput(output, collection, domain)
288
- return baseResult
289
- }
290
-
291
- const healthResult = await this.checkHost(domain)
292
- baseResult.healthy = healthResult.healthy
293
- baseResult.responseTimeMs = healthResult.responseTimeMs
294
- baseResult.statusCode = healthResult.statusCode
295
- baseResult.error = healthResult.error
296
-
297
- if (healthResult.healthy) {
298
- await this.handleHealthyOutput(output, collection)
299
- baseResult.downCount = Math.max(0, baseResult.downCount - 1)
300
- } else {
301
- const wasRemoved = await this.handleUnhealthyOutput(output, collection, domain)
302
- baseResult.downCount++
303
- if (wasRemoved) {
304
- baseResult.error = 'REMOVED'
305
- }
306
- }
307
- } catch (error: any) {
308
- baseResult.error = error.message ?? 'Check failed'
309
- await this.handleUnhealthyOutput(output, collection, domain).catch(() => {})
310
- }
311
-
312
- return baseResult
313
- }
314
-
315
- /**
316
- * Extracts URL from output record
317
- */
318
- private extractURLFromOutput (output: Record<string, any>): string | null {
319
- try {
320
- if (typeof output.domain === 'string') {
321
- return output.domain
322
- }
323
- if (typeof output.url === 'string') {
324
- return output.url
325
- }
326
- if (typeof output.serviceURL === 'string') {
327
- return output.serviceURL
328
- }
329
- if (Array.isArray(output.protocols) && output.protocols.length > 0) {
330
- const httpsProtocol = output.protocols.find((p: any) =>
331
- typeof p === 'string' && p.startsWith('https://')
332
- )
333
- if (httpsProtocol !== undefined) {
334
- return httpsProtocol
335
- }
336
- }
337
- return null
338
- } catch {
339
- return null
340
- }
341
- }
342
-
343
- /**
344
- * Validates if a string is a valid domain name
345
- */
346
- private isValidDomain (url: string): boolean {
347
- try {
348
- const parsedURL = new URL(url.startsWith('http') ? url : `https://${url}`)
349
- const hostname = parsedURL.hostname
350
- const domainRegex = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/i
351
- const localhostRegex = /^localhost$/i
352
- const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/
353
- return domainRegex.test(hostname) || localhostRegex.test(hostname) || ipv4Regex.test(hostname)
354
- } catch {
355
- return false
356
- }
357
- }
358
-
359
- /**
360
- * Handles a healthy output by decrementing its down counter
361
- */
362
- private async handleHealthyOutput (output: Record<string, any>, collection: any): Promise<void> {
363
- try {
364
- const currentDown = typeof output.down === 'number' ? output.down : 0
365
- if (currentDown > 0) {
366
- await collection.updateOne(
367
- { _id: output._id },
368
- { $inc: { down: -1 } }
369
- )
370
- }
371
- } catch (error) {
372
- this.logger.error(chalk.red(`Error handling healthy output ${String(output.txid)}:${String(output.outputIndex)}:`), error)
373
- }
374
- }
375
-
376
- /**
377
- * Handles an unhealthy output by incrementing its down counter.
378
- * If the threshold is reached, deletes the record and optionally bans the domain.
379
- * Returns true if the record was removed.
380
- */
381
- private async handleUnhealthyOutput (output: Record<string, any>, collection: any, domain?: string): Promise<boolean> {
382
- try {
383
- const currentDown = typeof output.down === 'number' ? output.down : 0
384
- const newDown = currentDown + 1
385
-
386
- if (newDown >= this.hostDownRevokeScore) {
387
- this.logger.log(chalk.red(`Removing output ${String(output.txid)}:${String(output.outputIndex)} (down: ${newDown} >= ${this.hostDownRevokeScore})`))
388
- await collection.deleteOne({ _id: output._id })
389
-
390
- // Auto-ban the domain and outpoint to prevent GASP re-sync
391
- if (this.banService !== undefined && this.autoBanOnRemoval) {
392
- const txid = output.txid as string
393
- const outputIndex = output.outputIndex as number
394
-
395
- await this.banService.banOutpoint(
396
- txid,
397
- outputIndex,
398
- `Auto-banned by janitor: host down ${newDown} consecutive checks`,
399
- domain
400
- )
401
-
402
- if (typeof domain === 'string' && domain !== 'unknown') {
403
- await this.banService.banDomain(
404
- domain,
405
- `Auto-banned by janitor: host unresponsive after ${newDown} checks`
406
- )
407
- }
408
- }
409
-
410
- return true
411
- } else {
412
- await collection.updateOne(
413
- { _id: output._id },
414
- { $inc: { down: 1 } }
415
- )
416
- return false
417
- }
418
- } catch (error) {
419
- this.logger.error(chalk.red(`Error handling unhealthy output ${String(output.txid)}:${String(output.outputIndex)}:`), error)
420
- return false
421
- }
422
- }
423
- }