@bsv/overlay-express 0.1.13 → 0.2.0

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.
@@ -1,13 +1,24 @@
1
1
  import express from 'express'
2
2
  import bodyParser from 'body-parser'
3
- import { Engine, KnexStorage, LookupService, TopicManager, KnexStorageMigrations } from '@bsv/overlay'
4
- import { ARC, ChainTracker, MerklePath, STEAK, TaggedBEEF, WhatsOnChain } from '@bsv/sdk'
3
+ import { Engine, KnexStorage, LookupService, TopicManager, KnexStorageMigrations, Advertiser } from '@bsv/overlay'
4
+ import {
5
+ ARC,
6
+ ChainTracker,
7
+ MerklePath,
8
+ STEAK,
9
+ TaggedBEEF,
10
+ WhatsOnChain,
11
+ Broadcaster,
12
+ OverlayBroadcastFacilitator,
13
+ HTTPSOverlayBroadcastFacilitator
14
+ } from '@bsv/sdk'
5
15
  import Knex from 'knex'
6
16
  import { MongoClient, Db } from 'mongodb'
7
17
  import makeUserInterface, { type UIConfig } from './makeUserInterface.js'
8
18
  import * as DiscoveryServices from '@bsv/overlay-discovery-services'
9
19
  import chalk from 'chalk'
10
20
  import util from 'util'
21
+ import { v4 as uuidv4 } from 'uuid'
11
22
 
12
23
  /**
13
24
  * Knex database migration.
@@ -53,6 +64,22 @@ class InMemoryMigrationSource implements Knex.Knex.MigrationSource<Migration> {
53
64
  }
54
65
  }
55
66
 
67
+ /**
68
+ * Configuration options that map to Engine constructor parameters.
69
+ */
70
+ export interface EngineConfig {
71
+ chainTracker?: ChainTracker | 'scripts only'
72
+ shipTrackers?: string[]
73
+ slapTrackers?: string[]
74
+ broadcaster?: Broadcaster
75
+ advertiser?: Advertiser
76
+ syncConfiguration?: Record<string, string[] | 'SHIP' | false>
77
+ logTime?: boolean
78
+ logPrefix?: string
79
+ throwOnBroadcastFailure?: boolean
80
+ overlayBroadcastFacilitator?: OverlayBroadcastFacilitator
81
+ }
82
+
56
83
  /**
57
84
  * OverlayExpress class provides an Express-based server for hosting Overlay Services.
58
85
  * It allows configuration of various components like databases, topic managers, and lookup services.
@@ -80,8 +107,9 @@ export default class OverlayExpress {
80
107
  // Network ('main' or 'test')
81
108
  network: 'main' | 'test' = 'main'
82
109
 
83
- // Chain tracker
84
- chainTracker: ChainTracker = new WhatsOnChain(this.network)
110
+ // If no custom ChainTracker is configured, default is a WhatsOnChain instance
111
+ // (We keep a property for it, so we can pass it to Engine)
112
+ chainTracker: ChainTracker | 'scripts only' = new WhatsOnChain(this.network)
85
113
 
86
114
  // The Overlay Engine
87
115
  engine: Engine | undefined = undefined
@@ -93,6 +121,7 @@ export default class OverlayExpress {
93
121
  services: Record<string, LookupService> = {}
94
122
 
95
123
  // Enable GASP Sync
124
+ // (We allow an on/off toggle, but also can do advanced custom sync config below)
96
125
  enableGASPSync: boolean = true
97
126
 
98
127
  // ARC API Key
@@ -104,19 +133,38 @@ export default class OverlayExpress {
104
133
  // Web UI configuration
105
134
  webUIConfig: UIConfig = {}
106
135
 
136
+ // Additional advanced engine config (these map to Engine constructor parameters).
137
+ // Default to undefined or default values that are used in the Engine if not specified.
138
+ engineConfig: EngineConfig = {}
139
+
140
+ // The administrative Bearer token used for the admin routes.
141
+ // If not passed in, we'll generate a random one.
142
+ private adminToken: string
143
+
107
144
  /**
108
145
  * Constructs an instance of OverlayExpress.
109
146
  * @param name - The name of the service
110
147
  * @param privateKey - Private key used for signing advertisements
111
148
  * @param hostingURL - The public URL where this service is hosted
149
+ * @param adminToken - Optional. An administrative Bearer token used to protect admin routes.
150
+ * If not provided, a random token will be generated at runtime.
112
151
  */
113
152
  constructor(
114
153
  public name: string,
115
154
  public privateKey: string,
116
- public hostingURL: string
155
+ public hostingURL: string,
156
+ adminToken?: string
117
157
  ) {
118
158
  this.app = express()
119
159
  this.logger.log(chalk.green.bold(`${name} constructed 🎉`))
160
+ this.adminToken = adminToken || uuidv4() // generate random if not provided
161
+ }
162
+
163
+ /**
164
+ * Returns the current admin token in case you need to programmatically retrieve or display it.
165
+ */
166
+ getAdminToken(): string {
167
+ return this.adminToken
120
168
  }
121
169
 
122
170
  /**
@@ -148,6 +196,7 @@ export default class OverlayExpress {
148
196
 
149
197
  /**
150
198
  * Configures the BSV Blockchain network to be used ('main' or 'test').
199
+ * By default, it re-initializes chainTracker as a WhatsOnChain for that network.
151
200
  * @param network - The network ('main' or 'test')
152
201
  */
153
202
  configureNetwork(network: 'main' | 'test') {
@@ -158,9 +207,10 @@ export default class OverlayExpress {
158
207
 
159
208
  /**
160
209
  * Configures the ChainTracker to be used.
161
- * @param chainTracker - An instance of ChainTracker
210
+ * If 'scripts only' is used, it implies no full SPV chain tracking in the Engine.
211
+ * @param chainTracker - An instance of ChainTracker or 'scripts only'
162
212
  */
163
- configureChainTracker(chainTracker: ChainTracker = new WhatsOnChain(this.network)) {
213
+ configureChainTracker(chainTracker: ChainTracker | 'scripts only' = new WhatsOnChain(this.network)) {
164
214
  this.chainTracker = chainTracker
165
215
  this.logger.log(chalk.blue('🔗 ChainTracker has been configured.'))
166
216
  }
@@ -175,7 +225,8 @@ export default class OverlayExpress {
175
225
  }
176
226
 
177
227
  /**
178
- * Enables or disables GASP synchronization.
228
+ * Enables or disables GASP synchronization (high-level setting).
229
+ * This is a broad toggle that can be overridden or customized through syncConfiguration.
179
230
  * @param enable - true to enable, false to disable
180
231
  */
181
232
  configureEnableGASPSync(enable: boolean) {
@@ -193,7 +244,7 @@ export default class OverlayExpress {
193
244
  }
194
245
 
195
246
  /**
196
- * Configures the Knex (SQL) database connection.
247
+ * Configure Knex (SQL) database connection.
197
248
  * @param config - Knex configuration object, or MySQL connection string (e.g. mysql://overlayAdmin:overlay123@mysql:3306/overlay).
198
249
  */
199
250
  async configureKnex(config: Knex.Knex.Config | string) {
@@ -267,7 +318,32 @@ export default class OverlayExpress {
267
318
  }
268
319
 
269
320
  /**
270
- * Configures the Overlay Engine.
321
+ * Advanced configuration method for setting or overriding any
322
+ * Engine constructor parameters via an EngineConfig object.
323
+ *
324
+ * Example usage:
325
+ * configureEngineParams({
326
+ * logTime: true,
327
+ * throwOnBroadcastFailure: true,
328
+ * overlayBroadcastFacilitator: new MyCustomFacilitator()
329
+ * })
330
+ *
331
+ * These fields will be respected when we finally build/configure the Engine
332
+ * in the `configureEngine()` method below.
333
+ */
334
+ configureEngineParams(params: EngineConfig) {
335
+ this.engineConfig = {
336
+ ...this.engineConfig,
337
+ ...params
338
+ }
339
+ this.logger.log(chalk.blue('⚙️ Advanced Engine configuration params have been updated.'))
340
+ }
341
+
342
+ /**
343
+ * Configures the Overlay Engine itself.
344
+ * By default, auto-configures SHIP and SLAP unless autoConfigureShipSlap = false
345
+ * Then it merges in any advanced engine config from `this.engineConfig`.
346
+ *
271
347
  * @param autoConfigureShipSlap - Whether to auto-configure SHIP and SLAP services (default: true)
272
348
  */
273
349
  async configureEngine(autoConfigureShipSlap = true) {
@@ -285,32 +361,74 @@ export default class OverlayExpress {
285
361
  ))
286
362
  }
287
363
 
288
- // Sync configuration
289
- let syncConfig: Record<string, false> = {}
290
- if (!this.enableGASPSync) {
291
- for (const manager of Object.keys(this.managers)) {
292
- syncConfig[manager] = false
364
+ // Construct a default sync configuration, in case the user doesn't want GASP at all:
365
+ let syncConfig: Record<string, string[] | 'SHIP' | false> = {}
366
+ if (this.enableGASPSync === false) {
367
+ // For each manager, disable sync
368
+ for (const managerName of Object.keys(this.managers)) {
369
+ syncConfig[managerName] = false
293
370
  }
371
+ } else {
372
+ // If the user provided a syncConfiguration, use that. Otherwise default to an empty object.
373
+ syncConfig = this.engineConfig.syncConfiguration || {}
294
374
  }
295
375
 
376
+ // Build the actual Storage
296
377
  const storage = new KnexStorage(this.knex as Knex.Knex)
297
378
  // Include the KnexStorage migrations
298
379
  this.migrationsToRun = [...KnexStorageMigrations.default, ...this.migrationsToRun]
299
380
 
381
+ // Prepare broadcaster if arcApiKey is set
382
+ let broadcaster: Broadcaster | undefined = undefined
383
+ if (this.arcApiKey) {
384
+ broadcaster = new ARC('https://arc.taal.com', {
385
+ apiKey: this.arcApiKey
386
+ })
387
+ }
388
+
389
+ // Prepare advertiser if not set by the user
390
+ let advertiser: Advertiser | undefined = this.engineConfig.advertiser
391
+ if (!advertiser) {
392
+ advertiser = new DiscoveryServices.LegacyNinjaAdvertiser(
393
+ this.privateKey,
394
+ 'https://dojo.babbage.systems',
395
+ this.hostingURL
396
+ )
397
+ }
398
+
399
+ // Construct the Engine with any advanced config overrides. Fallback to defaults.
300
400
  this.engine = new Engine(
301
401
  this.managers,
302
402
  this.services,
303
403
  storage,
304
- this.chainTracker,
404
+ // chainTracker
405
+ typeof this.engineConfig.chainTracker !== 'undefined'
406
+ ? this.engineConfig.chainTracker
407
+ : this.chainTracker,
408
+ // hostingURL
305
409
  this.hostingURL,
306
- undefined,
307
- undefined,
308
- this.arcApiKey ? new ARC('https://arc.taal.com', {
309
- apiKey: this.arcApiKey
310
- }) : undefined,
311
- new DiscoveryServices.LegacyNinjaAdvertiser(this.privateKey, 'https://dojo.babbage.systems', this.hostingURL),
312
- syncConfig
410
+ // shipTrackers
411
+ this.engineConfig.shipTrackers,
412
+ // slapTrackers
413
+ this.engineConfig.slapTrackers,
414
+ // broadcaster
415
+ broadcaster ?? this.engineConfig.broadcaster,
416
+ // advertiser
417
+ advertiser,
418
+ // syncConfiguration
419
+ syncConfig,
420
+ // logTime
421
+ this.engineConfig.logTime ?? false,
422
+ // logPrefix
423
+ this.engineConfig.logPrefix ?? '[OVERLAY_ENGINE] ',
424
+ // throwOnBroadcastFailure
425
+ this.engineConfig.throwOnBroadcastFailure ?? false,
426
+ // overlayBroadcastFacilitator
427
+ this.engineConfig.overlayBroadcastFacilitator ?? new HTTPSOverlayBroadcastFacilitator(),
428
+ // logger
429
+ this.logger
313
430
  )
431
+
314
432
  this.logger.log(chalk.green('🚀 Engine has been configured.'))
315
433
  }
316
434
 
@@ -320,7 +438,7 @@ export default class OverlayExpress {
320
438
  */
321
439
  private ensureKnex() {
322
440
  if (typeof this.knex === 'undefined') {
323
- throw new Error('You\'ll need to configure your SQL database with the .configureKnex() method first!')
441
+ throw new Error('You must configure your SQL database with the .configureKnex() method first!')
324
442
  }
325
443
  }
326
444
 
@@ -330,7 +448,7 @@ export default class OverlayExpress {
330
448
  */
331
449
  private ensureMongo() {
332
450
  if (typeof this.mongoDb === 'undefined') {
333
- throw new Error('You\'ll need to configure your MongoDB connection with the .configureMongo() method first!')
451
+ throw new Error('You must configure your MongoDB connection with the .configureMongo() method first!')
334
452
  }
335
453
  }
336
454
 
@@ -340,7 +458,7 @@ export default class OverlayExpress {
340
458
  */
341
459
  private ensureEngine() {
342
460
  if (typeof this.engine === 'undefined') {
343
- throw new Error('You\'ll need to configure your Overlay Services engine with the .configureEngine() method first!')
461
+ throw new Error('You must configure your Overlay Services engine with the .configureEngine() method first!')
344
462
  }
345
463
  }
346
464
 
@@ -359,76 +477,75 @@ export default class OverlayExpress {
359
477
 
360
478
  if (this.verboseRequestLogging) {
361
479
  this.app.use((req, res, next) => {
362
- const startTime = Date.now();
480
+ const startTime = Date.now()
363
481
 
364
482
  // Log incoming request details
365
- this.logger.log(chalk.magenta.bold(`📥 Incoming Request: ${req.method} ${req.originalUrl}`));
366
-
483
+ this.logger.log(chalk.magenta.bold(`📥 Incoming Request: ${req.method} ${req.originalUrl}`))
367
484
  // Pretty-print headers
368
- this.logger.log(chalk.cyan(`Headers:`));
369
- this.logger.log(util.inspect(req.headers, { colors: true, depth: null }));
485
+ this.logger.log(chalk.cyan(`Headers:`))
486
+ this.logger.log(util.inspect(req.headers, { colors: true, depth: null }))
370
487
 
371
488
  // Handle request body
372
489
  if (req.body && Object.keys(req.body).length > 0) {
373
- let bodyContent;
374
- let bodyString;
490
+ let bodyContent
491
+ let bodyString
375
492
  if (typeof req.body === 'object') {
376
- bodyString = JSON.stringify(req.body, null, 2);
493
+ bodyString = JSON.stringify(req.body, null, 2)
377
494
  } else if (Buffer.isBuffer(req.body)) {
378
- bodyString = req.body.toString('utf8');
495
+ bodyString = req.body.toString('utf8')
379
496
  } else {
380
- bodyString = req.body.toString();
497
+ bodyString = req.body.toString()
381
498
  }
382
499
  if (bodyString.length > 280) {
383
- bodyContent = chalk.yellow(`(Body too long to display, length: ${bodyString.length} characters)`);
500
+ bodyContent = chalk.yellow(`(Body too long to display, length: ${bodyString.length} characters)`)
384
501
  } else {
385
- bodyContent = chalk.green(`Request Body:\n${bodyString}`);
502
+ bodyContent = chalk.green(`Request Body:\n${bodyString}`)
386
503
  }
387
- this.logger.log(bodyContent);
504
+ this.logger.log(bodyContent)
388
505
  }
389
506
 
390
- // Intercept the res.send method
391
- const originalSend = res.send;
392
- let responseBody: any;
507
+ // Intercept the res.send method to log responses
508
+ const originalSend = res.send
509
+ let responseBody: any
393
510
 
394
511
  res.send = function (body?: any): any {
395
- responseBody = body;
396
- return originalSend.call(this, body);
397
- };
512
+ responseBody = body
513
+ return originalSend.call(this, body)
514
+ }
398
515
 
399
516
  // Log outgoing response details after the response is finished
400
517
  res.on('finish', () => {
401
- const duration = Date.now() - startTime;
518
+ const duration = Date.now() - startTime
402
519
  this.logger.log(
403
520
  chalk.magenta.bold(
404
521
  `📤 Outgoing Response: ${req.method} ${req.originalUrl} - Status: ${res.statusCode} - Duration: ${duration}ms`
405
522
  )
406
- );
407
- this.logger.log(chalk.cyan(`Response Headers:`));
408
- this.logger.log(util.inspect(res.getHeaders(), { colors: true, depth: null }));
523
+ )
524
+ this.logger.log(chalk.cyan(`Response Headers:`))
525
+ this.logger.log(util.inspect(res.getHeaders(), { colors: true, depth: null }))
409
526
 
410
527
  // Handle response body
411
528
  if (responseBody) {
412
- let bodyContent;
413
- let bodyString;
529
+ let bodyContent
530
+ let bodyString
414
531
  if (typeof responseBody === 'object') {
415
- bodyString = JSON.stringify(responseBody, null, 2);
532
+ bodyString = JSON.stringify(responseBody, null, 2)
416
533
  } else if (Buffer.isBuffer(responseBody)) {
417
- bodyString = responseBody.toString('utf8');
534
+ bodyString = responseBody.toString('utf8')
418
535
  } else {
419
- bodyString = responseBody.toString();
536
+ bodyString = responseBody.toString()
420
537
  }
421
538
  if (bodyString.length > 280) {
422
- bodyContent = chalk.yellow(`(Response body too long to display, length: ${bodyString.length} characters)`);
539
+ bodyContent = chalk.yellow(`(Response body too long to display, length: ${bodyString.length} characters)`)
423
540
  } else {
424
- bodyContent = chalk.green(`Response Body:\n${bodyString}`);
541
+ bodyContent = chalk.green(`Response Body:\n${bodyString}`)
425
542
  }
426
- this.logger.log(bodyContent);
543
+ this.logger.log(bodyContent)
427
544
  }
428
- });
545
+ })
429
546
 
430
- next();
431
- });
547
+ next()
548
+ })
432
549
  }
433
550
 
434
551
  // Enable CORS
@@ -453,7 +570,7 @@ export default class OverlayExpress {
453
570
 
454
571
  // List hosted topic managers and lookup services
455
572
  this.app.get('/listTopicManagers', (_, res) => {
456
- (async () => {
573
+ ; (async () => {
457
574
  try {
458
575
  const result = await engine.listTopicManagers()
459
576
  return res.status(200).json(result)
@@ -473,7 +590,7 @@ export default class OverlayExpress {
473
590
  })
474
591
 
475
592
  this.app.get('/listLookupServiceProviders', (_, res) => {
476
- (async () => {
593
+ ; (async () => {
477
594
  try {
478
595
  const result = await engine.listLookupServiceProviders()
479
596
  return res.status(200).json(result)
@@ -493,7 +610,7 @@ export default class OverlayExpress {
493
610
 
494
611
  // Host documentation for the services
495
612
  this.app.get('/getDocumentationForTopicManager', (req, res) => {
496
- (async () => {
613
+ ; (async () => {
497
614
  try {
498
615
  const manager = req.query.manager as string
499
616
  const result = await engine.getDocumentationForTopicManager(manager)
@@ -514,7 +631,7 @@ export default class OverlayExpress {
514
631
  })
515
632
 
516
633
  this.app.get('/getDocumentationForLookupServiceProvider', (req, res) => {
517
- (async () => {
634
+ ; (async () => {
518
635
  try {
519
636
  const lookupService = req.query.lookupService as string
520
637
  const result = await engine.getDocumentationForLookupServiceProvider(lookupService)
@@ -535,7 +652,7 @@ export default class OverlayExpress {
535
652
 
536
653
  // Submit transactions and facilitate lookup requests
537
654
  this.app.post('/submit', (req, res) => {
538
- (async () => {
655
+ ; (async () => {
539
656
  try {
540
657
  // Parse out the topics and construct the tagged BEEF
541
658
  const topicsHeader = req.headers['x-topics']
@@ -548,8 +665,7 @@ export default class OverlayExpress {
548
665
  topics
549
666
  }
550
667
 
551
- // Using a callback function, we can just return once our steak is ready
552
- // instead of having to wait for all the broadcasts to occur.
668
+ // Using a callback function, we can return once the STEAK is ready
553
669
  await engine.submit(taggedBEEF, (steak: STEAK) => {
554
670
  return res.status(200).json(steak)
555
671
  })
@@ -569,7 +685,7 @@ export default class OverlayExpress {
569
685
  })
570
686
 
571
687
  this.app.post('/lookup', (req, res) => {
572
- (async () => {
688
+ ; (async () => {
573
689
  try {
574
690
  const result = await engine.lookup(req.body)
575
691
  return res.status(200).json(result)
@@ -588,10 +704,10 @@ export default class OverlayExpress {
588
704
  })
589
705
  })
590
706
 
707
+ // ARC ingest route (only if we have an ARC API key)
591
708
  if (this.arcApiKey) {
592
- // Route for ARC ingest
593
709
  this.app.post('/arc-ingest', (req, res) => {
594
- (async () => {
710
+ ; (async () => {
595
711
  try {
596
712
  const { txid, merklePath: merklePathHex, blockHeight } = req.body
597
713
  const merklePath = MerklePath.fromHex(merklePathHex)
@@ -615,10 +731,10 @@ export default class OverlayExpress {
615
731
  this.logger.warn(chalk.yellow('⚠️ Disabling ARC because no ARC API key was provided.'))
616
732
  }
617
733
 
734
+ // GASP sync routes if enabled
618
735
  if (this.enableGASPSync) {
619
- // Routes for GASP synchronization
620
736
  this.app.post('/requestSyncResponse', (req, res) => {
621
- (async () => {
737
+ ; (async () => {
622
738
  try {
623
739
  const topic = req.headers['x-bsv-topic'] as string
624
740
  const response = await engine.provideForeignSyncResponse(req.body, topic)
@@ -637,8 +753,9 @@ export default class OverlayExpress {
637
753
  })
638
754
  })
639
755
  })
756
+
640
757
  this.app.post('/requestForeignGASPNode', (req, res) => {
641
- (async () => {
758
+ ; (async () => {
642
759
  try {
643
760
  const { graphID, txid, outputIndex } = req.body
644
761
  const response = await engine.provideForeignGASPNode(graphID, txid, outputIndex)
@@ -661,6 +778,73 @@ export default class OverlayExpress {
661
778
  this.logger.warn(chalk.yellow('⚠️ GASP sync is disabled.'))
662
779
  }
663
780
 
781
+ /**
782
+ * ============== ADMIN ROUTES ==============
783
+ * These routes expose advanced engine operations
784
+ * and require a valid Bearer token for access.
785
+ */
786
+
787
+ /**
788
+ * Middleware for checking the admin bearer token.
789
+ */
790
+ const checkAdminAuth = (req: express.Request, res: express.Response, next: express.NextFunction) => {
791
+ const authHeader = req.headers['authorization']
792
+ if (!authHeader || !authHeader.startsWith('Bearer ')) {
793
+ return res.status(401).json({ status: 'error', message: 'Unauthorized: Missing Bearer token' })
794
+ }
795
+ const token = authHeader.substring('Bearer '.length)
796
+ if (token !== this.adminToken) {
797
+ return res.status(403).json({ status: 'error', message: 'Forbidden: Invalid Bearer token' })
798
+ }
799
+ next()
800
+ }
801
+
802
+ /**
803
+ * Admin route to manually sync advertisements, calling `engine.syncAdvertisements()`.
804
+ */
805
+ this.app.post('/admin/syncAdvertisements', checkAdminAuth as any, (req, res) => {
806
+ ; (async () => {
807
+ try {
808
+ await engine.syncAdvertisements()
809
+ return res.status(200).json({ status: 'success', message: 'Advertisements synced successfully' })
810
+ } catch (error) {
811
+ console.error(chalk.red('❌ Error in /admin/syncAdvertisements:'), error)
812
+ return res.status(400).json({
813
+ status: 'error',
814
+ message: error instanceof Error ? error.message : 'An unknown error occurred'
815
+ })
816
+ }
817
+ })().catch(() => {
818
+ res.status(500).json({
819
+ status: 'error',
820
+ message: 'Unexpected error'
821
+ })
822
+ })
823
+ })
824
+
825
+ /**
826
+ * Admin route to manually start GASP sync, calling `engine.startGASPSync()`.
827
+ */
828
+ this.app.post('/admin/startGASPSync', checkAdminAuth as any, (req, res) => {
829
+ ; (async () => {
830
+ try {
831
+ await engine.startGASPSync()
832
+ return res.status(200).json({ status: 'success', message: 'GASP sync started and completed' })
833
+ } catch (error) {
834
+ console.error(chalk.red('❌ Error in /admin/startGASPSync:'), error)
835
+ return res.status(400).json({
836
+ status: 'error',
837
+ message: error instanceof Error ? error.message : 'An unknown error occurred'
838
+ })
839
+ }
840
+ })().catch(() => {
841
+ res.status(500).json({
842
+ status: 'error',
843
+ message: 'Unexpected error'
844
+ })
845
+ })
846
+ })
847
+
664
848
  // Automatically handle migrations
665
849
  const migrationSource = new InMemoryMigrationSource(this.migrationsToRun)
666
850
  const result = await knex.migrate.latest({
@@ -676,21 +860,32 @@ export default class OverlayExpress {
676
860
  code: 'ERR_ROUTE_NOT_FOUND',
677
861
  description: 'Route not found.'
678
862
  })
679
- });
863
+ })
680
864
 
681
865
  // The legacy Ninja advertiser has a setLookupEngine method.
682
- this.logger.log(chalk.cyan(`${this.name} will now advertise with SHIP and SLAP at ${this.hostingURL}`))
866
+ if (this.engine?.advertiser instanceof DiscoveryServices.LegacyNinjaAdvertiser) {
867
+ this.logger.log(
868
+ chalk.cyan(
869
+ `${this.name} will now advertise with SHIP and SLAP at ${this.hostingURL}`
870
+ )
871
+ )
872
+ ; (this.engine.advertiser as DiscoveryServices.LegacyNinjaAdvertiser).setLookupEngine(this.engine)
873
+ }
874
+
875
+ // Log some info about topic managers and services
683
876
  const numTopicManagers = Object.keys(this.managers).length
684
877
  const numLookupServices = Object.keys(this.services).length
685
878
  this.logger.log(chalk.blue(`Topic Managers: ${numTopicManagers}`))
686
- this.logger.log(chalk.blue(`Lookup Services: ${numLookupServices}`));
879
+ this.logger.log(chalk.blue(`Lookup Services: ${numLookupServices}`))
880
+
881
+ // Attempt to sync advertisements
687
882
  try {
688
- (this.engine?.advertiser as DiscoveryServices.LegacyNinjaAdvertiser).setLookupEngine(this.engine!)
689
883
  await this.engine?.syncAdvertisements()
690
884
  } catch (e) {
691
885
  this.logger.log(chalk.red('❌ Error syncing advertisements:'), e)
692
886
  }
693
887
 
888
+ // Attempt to do GASP sync if enabled
694
889
  if (this.enableGASPSync) {
695
890
  try {
696
891
  this.logger.log(chalk.green('Starting GASP sync...'))
@@ -705,7 +900,7 @@ export default class OverlayExpress {
705
900
 
706
901
  // Start listening on the configured port
707
902
  this.app.listen(this.port, async () => {
708
- this.logger.log(chalk.green.bold(`🎧 ${this.name} is ready and listening on local port ${this.port}`));
903
+ this.logger.log(chalk.green.bold(`🎧 ${this.name} is ready and listening on local port ${this.port}`))
709
904
  })
710
905
  }
711
906
  }