@bsv/overlay-express 0.4.4 → 0.5.1

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.
@@ -101,7 +101,7 @@ export default class OverlayExpress {
101
101
  knex: Knex.Knex | undefined = undefined
102
102
 
103
103
  // Knex migrations to run
104
- migrationsToRun: Array<Migration> = []
104
+ migrationsToRun: Migration[] = []
105
105
 
106
106
  // MongoDB database
107
107
  mongoDb: Db | undefined = undefined
@@ -141,7 +141,7 @@ export default class OverlayExpress {
141
141
 
142
142
  // The administrative Bearer token used for the admin routes.
143
143
  // If not passed in, we'll generate a random one.
144
- private adminToken: string
144
+ private readonly adminToken: string
145
145
 
146
146
  /**
147
147
  * Constructs an instance of OverlayExpress.
@@ -299,7 +299,7 @@ export default class OverlayExpress {
299
299
  */
300
300
  configureLookupServiceWithKnex(
301
301
  name: string,
302
- serviceFactory: (knex: Knex.Knex) => { service: LookupService; migrations: Array<Migration> }
302
+ serviceFactory: (knex: Knex.Knex) => { service: LookupService; migrations: Migration[] }
303
303
  ): void {
304
304
  this.ensureKnex()
305
305
  const factoryResult = serviceFactory(this.knex as Knex.Knex)
@@ -485,7 +485,7 @@ export default class OverlayExpress {
485
485
  * Starts the Express server.
486
486
  * Sets up routes and begins listening on the configured port.
487
487
  */
488
- async start() {
488
+ async start(): Promise<void> {
489
489
  this.ensureEngine()
490
490
  this.ensureKnex()
491
491
  const engine = this.engine as Engine
@@ -676,10 +676,10 @@ export default class OverlayExpress {
676
676
  try {
677
677
  // Parse out the topics and construct the tagged BEEF
678
678
  const topicsHeader = req.headers['x-topics']
679
- if (!topicsHeader) {
679
+ if (typeof topicsHeader !== 'string') {
680
680
  throw new Error('Missing x-topics header')
681
681
  }
682
- const topics = JSON.parse(topicsHeader as string)
682
+ const topics = JSON.parse(topicsHeader)
683
683
  const taggedBEEF: TaggedBEEF = {
684
684
  beef: Array.from(req.body as number[]),
685
685
  topics
@@ -730,7 +730,7 @@ export default class OverlayExpress {
730
730
  })
731
731
 
732
732
  // ARC ingest route (only if we have an ARC API key)
733
- if (this.arcApiKey) {
733
+ if (typeof this.arcApiKey === 'string' && this.arcApiKey.length > 0) {
734
734
  this.app.post('/arc-ingest', (req, res) => {
735
735
  ; (async () => {
736
736
  try {
@@ -812,14 +812,16 @@ export default class OverlayExpress {
812
812
  /**
813
813
  * Middleware for checking the admin bearer token.
814
814
  */
815
- const checkAdminAuth = (req: express.Request, res: express.Response, next: express.NextFunction) => {
815
+ const checkAdminAuth = (req: express.Request, res: express.Response, next: express.NextFunction): void => {
816
816
  const authHeader = req.headers['authorization']
817
- if (!authHeader || !authHeader.startsWith('Bearer ')) {
818
- return res.status(401).json({ status: 'error', message: 'Unauthorized: Missing Bearer token' })
817
+ if (typeof authHeader !== 'string' || !authHeader.startsWith('Bearer ')) {
818
+ res.status(401).json({ status: 'error', message: 'Unauthorized: Missing Bearer token' })
819
+ return
819
820
  }
820
821
  const token = authHeader.substring('Bearer '.length)
821
822
  if (token !== this.adminToken) {
822
- return res.status(403).json({ status: 'error', message: 'Forbidden: Invalid Bearer token' })
823
+ res.status(403).json({ status: 'error', message: 'Forbidden: Invalid Bearer token' })
824
+ return
823
825
  }
824
826
  next()
825
827
  }
@@ -871,14 +873,23 @@ export default class OverlayExpress {
871
873
  })
872
874
 
873
875
  /**
874
- * Admin route to evict an outpoint from a lookup service.
876
+ * Admin route to evict an outpoint, either from all services or a specific one.
875
877
  */
876
878
  this.app.post('/admin/evictOutpoint', checkAdminAuth as any, (req, res) => {
877
879
  ; (async () => {
878
880
  try {
879
- const service = engine.lookupServices[req.body.service]
880
- if (typeof service.outputDeleted === 'function') {
881
- await service.outputDeleted(req.body.txid, req.body.outputIndex, req.body.topic)
881
+ if (typeof req.body.service === 'string') {
882
+ const service = engine.lookupServices[req.body.service]
883
+ await service.outputEvicted(req.body.txid, req.body.outputIndex)
884
+ } else {
885
+ const services = Object.values(engine.lookupServices)
886
+ for (let i = 0; i < services.length; i++) {
887
+ try {
888
+ await services[i].outputEvicted(req.body.txid, req.body.outputIndex)
889
+ } catch {
890
+ continue
891
+ }
892
+ }
882
893
  }
883
894
  return res.status(200).json({ status: 'success', message: 'Outpoint evicted' })
884
895
  } catch (error) {