@bsv/overlay-express 0.2.1 → 0.3.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.
@@ -23,7 +23,7 @@ import { v4 as uuidv4 } from 'uuid'
23
23
  /**
24
24
  * Knex database migration.
25
25
  */
26
- type Migration = {
26
+ interface Migration {
27
27
  name?: string
28
28
  up: (knex: Knex.Knex) => Promise<void>
29
29
  down?: (knex: Knex.Knex) => Promise<void>
@@ -34,7 +34,7 @@ type Migration = {
34
34
  * Allows running migrations defined in code rather than files.
35
35
  */
36
36
  class InMemoryMigrationSource implements Knex.Knex.MigrationSource<Migration> {
37
- constructor(private migrations: Migration[]) { }
37
+ constructor(private readonly migrations: Migration[]) { }
38
38
 
39
39
  /**
40
40
  * Gets the list of migrations.
@@ -51,7 +51,7 @@ class InMemoryMigrationSource implements Knex.Knex.MigrationSource<Migration> {
51
51
  * @returns The name of the migration
52
52
  */
53
53
  getMigrationName(migration: Migration): string {
54
- return migration.name || `Migration at index ${this.migrations.indexOf(migration)}`
54
+ return typeof migration.name === 'string' ? migration.name : `Migration at index ${this.migrations.indexOf(migration)}`
55
55
  }
56
56
 
57
57
  /**
@@ -59,8 +59,8 @@ class InMemoryMigrationSource implements Knex.Knex.MigrationSource<Migration> {
59
59
  * @param migration - The migration object
60
60
  * @returns Promise resolving to the migration object
61
61
  */
62
- getMigration(migration: Migration): Promise<Knex.Knex.Migration> {
63
- return Promise.resolve(migration)
62
+ async getMigration(migration: Migration): Promise<Knex.Knex.Migration> {
63
+ return await Promise.resolve(migration)
64
64
  }
65
65
  }
66
66
 
@@ -171,7 +171,7 @@ export default class OverlayExpress {
171
171
  * Configures the port on which the server will listen.
172
172
  * @param port - The port number
173
173
  */
174
- configurePort(port: number) {
174
+ configurePort(port: number): void {
175
175
  this.port = port
176
176
  this.logger.log(chalk.blue(`🌐 Server port set to ${port}`))
177
177
  }
@@ -180,7 +180,7 @@ export default class OverlayExpress {
180
180
  * Configures the web user interface
181
181
  * @param config - Web UI configuration options
182
182
  */
183
- configureWebUI(config: UIConfig) {
183
+ configureWebUI(config: UIConfig): void {
184
184
  this.webUIConfig = config
185
185
  this.logger.log(chalk.blue('🖥️ Web UI has been configured.'))
186
186
  }
@@ -189,7 +189,7 @@ export default class OverlayExpress {
189
189
  * Configures the logger to be used by the server.
190
190
  * @param logger - A logger object (e.g., console)
191
191
  */
192
- configureLogger(logger: typeof console) {
192
+ configureLogger(logger: typeof console): void {
193
193
  this.logger = logger
194
194
  this.logger.log(chalk.blue('🔍 Logger has been configured.'))
195
195
  }
@@ -199,7 +199,7 @@ export default class OverlayExpress {
199
199
  * By default, it re-initializes chainTracker as a WhatsOnChain for that network.
200
200
  * @param network - The network ('main' or 'test')
201
201
  */
202
- configureNetwork(network: 'main' | 'test') {
202
+ configureNetwork(network: 'main' | 'test'): void {
203
203
  this.network = network
204
204
  this.chainTracker = new WhatsOnChain(this.network)
205
205
  this.logger.log(chalk.blue(`🔗 Network set to ${network}`))
@@ -210,7 +210,7 @@ export default class OverlayExpress {
210
210
  * If 'scripts only' is used, it implies no full SPV chain tracking in the Engine.
211
211
  * @param chainTracker - An instance of ChainTracker or 'scripts only'
212
212
  */
213
- configureChainTracker(chainTracker: ChainTracker | 'scripts only' = new WhatsOnChain(this.network)) {
213
+ configureChainTracker(chainTracker: ChainTracker | 'scripts only' = new WhatsOnChain(this.network)): void {
214
214
  this.chainTracker = chainTracker
215
215
  this.logger.log(chalk.blue('🔗 ChainTracker has been configured.'))
216
216
  }
@@ -219,7 +219,7 @@ export default class OverlayExpress {
219
219
  * Configures the ARC API key.
220
220
  * @param apiKey - The ARC API key
221
221
  */
222
- configureArcApiKey(apiKey: string) {
222
+ configureArcApiKey(apiKey: string): void {
223
223
  this.arcApiKey = apiKey
224
224
  this.logger.log(chalk.blue('🔑 ARC API key has been configured.'))
225
225
  }
@@ -229,7 +229,7 @@ export default class OverlayExpress {
229
229
  * This is a broad toggle that can be overridden or customized through syncConfiguration.
230
230
  * @param enable - true to enable, false to disable
231
231
  */
232
- configureEnableGASPSync(enable: boolean) {
232
+ configureEnableGASPSync(enable: boolean): void {
233
233
  this.enableGASPSync = enable
234
234
  this.logger.log(chalk.blue(`🔄 GASP synchronization ${enable ? 'enabled' : 'disabled'}.`))
235
235
  }
@@ -238,7 +238,7 @@ export default class OverlayExpress {
238
238
  * Enables or disables verbose request logging.
239
239
  * @param enable - true to enable, false to disable
240
240
  */
241
- configureVerboseRequestLogging(enable: boolean) {
241
+ configureVerboseRequestLogging(enable: boolean): void {
242
242
  this.verboseRequestLogging = enable
243
243
  this.logger.log(chalk.blue(`📝 Verbose request logging ${enable ? 'enabled' : 'disabled'}.`))
244
244
  }
@@ -247,7 +247,7 @@ export default class OverlayExpress {
247
247
  * Configure Knex (SQL) database connection.
248
248
  * @param config - Knex configuration object, or MySQL connection string (e.g. mysql://overlayAdmin:overlay123@mysql:3306/overlay).
249
249
  */
250
- async configureKnex(config: Knex.Knex.Config | string) {
250
+ async configureKnex(config: Knex.Knex.Config | string): Promise<void> {
251
251
  if (typeof config === 'string') {
252
252
  config = {
253
253
  client: 'mysql2',
@@ -262,7 +262,7 @@ export default class OverlayExpress {
262
262
  * Configures the MongoDB database connection.
263
263
  * @param connectionString - MongoDB connection string
264
264
  */
265
- async configureMongo(connectionString: string) {
265
+ async configureMongo(connectionString: string): Promise<void> {
266
266
  const mongoClient = new MongoClient(connectionString)
267
267
  await mongoClient.connect()
268
268
  const db = mongoClient.db(`${this.name}_lookup_services`)
@@ -275,7 +275,7 @@ export default class OverlayExpress {
275
275
  * @param name - The name of the Topic Manager
276
276
  * @param manager - An instance of TopicManager
277
277
  */
278
- configureTopicManager(name: string, manager: TopicManager) {
278
+ configureTopicManager(name: string, manager: TopicManager): void {
279
279
  this.managers[name] = manager
280
280
  this.logger.log(chalk.blue(`🗂️ Configured topic manager ${name}`))
281
281
  }
@@ -285,7 +285,7 @@ export default class OverlayExpress {
285
285
  * @param name - The name of the Lookup Service
286
286
  * @param service - An instance of LookupService
287
287
  */
288
- configureLookupService(name: string, service: LookupService) {
288
+ configureLookupService(name: string, service: LookupService): void {
289
289
  this.services[name] = service
290
290
  this.logger.log(chalk.blue(`🔍 Configured lookup service ${name}`))
291
291
  }
@@ -298,7 +298,7 @@ export default class OverlayExpress {
298
298
  configureLookupServiceWithKnex(
299
299
  name: string,
300
300
  serviceFactory: (knex: Knex.Knex) => { service: LookupService; migrations: Array<Migration> }
301
- ) {
301
+ ): void {
302
302
  this.ensureKnex()
303
303
  const factoryResult = serviceFactory(this.knex as Knex.Knex)
304
304
  this.services[name] = factoryResult.service
@@ -311,7 +311,7 @@ export default class OverlayExpress {
311
311
  * @param name - The name of the Lookup Service
312
312
  * @param serviceFactory - A factory function that creates a LookupService instance using MongoDB
313
313
  */
314
- configureLookupServiceWithMongo(name: string, serviceFactory: (mongoDb: Db) => LookupService) {
314
+ configureLookupServiceWithMongo(name: string, serviceFactory: (mongoDb: Db) => LookupService): void {
315
315
  this.ensureMongo()
316
316
  this.services[name] = serviceFactory(this.mongoDb as Db)
317
317
  this.logger.log(chalk.blue(`🔍 Configured lookup service ${name} with MongoDB`))
@@ -331,7 +331,7 @@ export default class OverlayExpress {
331
331
  * These fields will be respected when we finally build/configure the Engine
332
332
  * in the `configureEngine()` method below.
333
333
  */
334
- configureEngineParams(params: EngineConfig) {
334
+ configureEngineParams(params: EngineConfig): void {
335
335
  this.engineConfig = {
336
336
  ...this.engineConfig,
337
337
  ...params
@@ -346,7 +346,7 @@ export default class OverlayExpress {
346
346
  *
347
347
  * @param autoConfigureShipSlap - Whether to auto-configure SHIP and SLAP services (default: true)
348
348
  */
349
- async configureEngine(autoConfigureShipSlap = true) {
349
+ async configureEngine(autoConfigureShipSlap = true): Promise<void> {
350
350
  this.ensureKnex()
351
351
 
352
352
  if (autoConfigureShipSlap) {
@@ -363,7 +363,7 @@ export default class OverlayExpress {
363
363
 
364
364
  // Construct a default sync configuration, in case the user doesn't want GASP at all:
365
365
  let syncConfig: Record<string, string[] | 'SHIP' | false> = {}
366
- if (this.enableGASPSync === false) {
366
+ if (!this.enableGASPSync) {
367
367
  // For each manager, disable sync
368
368
  for (const managerName of Object.keys(this.managers)) {
369
369
  syncConfig[managerName] = false
@@ -379,19 +379,25 @@ export default class OverlayExpress {
379
379
  this.migrationsToRun = [...KnexStorageMigrations.default, ...this.migrationsToRun]
380
380
 
381
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
- })
382
+ let broadcaster: Broadcaster | undefined
383
+ if (typeof this.arcApiKey === 'string') {
384
+ broadcaster = new ARC(
385
+ // We hard-code some ARC URLs for now, but we should make this configurable later.
386
+ this.network === 'test' ? 'https://arc-test.taal.com' : 'https://arc.taal.com',
387
+ {
388
+ apiKey: this.arcApiKey
389
+ })
387
390
  }
388
391
 
389
392
  // Prepare advertiser if not set by the user
390
393
  let advertiser: Advertiser | undefined = this.engineConfig.advertiser
391
- if (!advertiser) {
392
- advertiser = new DiscoveryServices.LegacyNinjaAdvertiser(
394
+ if (typeof advertiser === 'undefined') {
395
+ advertiser = new DiscoveryServices.WalletAdvertiser(
396
+ this.network,
393
397
  this.privateKey,
394
- 'https://dojo.babbage.systems',
398
+ this.network === 'test' // For now, we hard-code some storage servers. In the future, this needs to be configurable.
399
+ ? 'https://staging-storage.babbage.systems'
400
+ : 'https://storage.babbage.systems',
395
401
  this.hostingURL
396
402
  )
397
403
  }
@@ -436,7 +442,7 @@ export default class OverlayExpress {
436
442
  * Ensures that Knex is configured.
437
443
  * @throws Error if Knex is not configured
438
444
  */
439
- private ensureKnex() {
445
+ private ensureKnex(): void {
440
446
  if (typeof this.knex === 'undefined') {
441
447
  throw new Error('You must configure your SQL database with the .configureKnex() method first!')
442
448
  }
@@ -446,7 +452,7 @@ export default class OverlayExpress {
446
452
  * Ensures that MongoDB is configured.
447
453
  * @throws Error if MongoDB is not configured
448
454
  */
449
- private ensureMongo() {
455
+ private ensureMongo(): void {
450
456
  if (typeof this.mongoDb === 'undefined') {
451
457
  throw new Error('You must configure your MongoDB connection with the .configureMongo() method first!')
452
458
  }
@@ -456,7 +462,7 @@ export default class OverlayExpress {
456
462
  * Ensures that the Overlay Engine is configured.
457
463
  * @throws Error if the Engine is not configured
458
464
  */
459
- private ensureEngine() {
465
+ private ensureEngine(): void {
460
466
  if (typeof this.engine === 'undefined') {
461
467
  throw new Error('You must configure your Overlay Services engine with the .configureEngine() method first!')
462
468
  }
@@ -863,13 +869,13 @@ export default class OverlayExpress {
863
869
  })
864
870
 
865
871
  // The legacy Ninja advertiser has a setLookupEngine method.
866
- if (this.engine?.advertiser instanceof DiscoveryServices.LegacyNinjaAdvertiser) {
872
+ if (this.engine?.advertiser instanceof DiscoveryServices.WalletAdvertiser) {
867
873
  this.logger.log(
868
874
  chalk.cyan(
869
875
  `${this.name} will now advertise with SHIP and SLAP at ${this.hostingURL}`
870
876
  )
871
877
  )
872
- ; (this.engine.advertiser as DiscoveryServices.LegacyNinjaAdvertiser).setLookupEngine(this.engine)
878
+ await this.engine.advertiser.initWithEngine(this.engine)
873
879
  }
874
880
 
875
881
  // Log some info about topic managers and services