@bsv/overlay-express 2.0.1 → 2.1.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.
Files changed (52) hide show
  1. package/dist/cjs/mod.js +7 -1
  2. package/dist/cjs/mod.js.map +1 -1
  3. package/dist/cjs/package.json +4 -2
  4. package/dist/cjs/src/BanAwareLookupWrapper.js +76 -0
  5. package/dist/cjs/src/BanAwareLookupWrapper.js.map +1 -0
  6. package/dist/cjs/src/BanService.js +118 -0
  7. package/dist/cjs/src/BanService.js.map +1 -0
  8. package/dist/cjs/src/JanitorService.js +206 -93
  9. package/dist/cjs/src/JanitorService.js.map +1 -1
  10. package/dist/cjs/src/OverlayExpress.js +522 -71
  11. package/dist/cjs/src/OverlayExpress.js.map +1 -1
  12. package/dist/cjs/src/makeUserInterface.js +889 -217
  13. package/dist/cjs/src/makeUserInterface.js.map +1 -1
  14. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  15. package/dist/esm/mod.js +4 -1
  16. package/dist/esm/mod.js.map +1 -1
  17. package/dist/esm/src/BanAwareLookupWrapper.js +78 -0
  18. package/dist/esm/src/BanAwareLookupWrapper.js.map +1 -0
  19. package/dist/esm/src/BanService.js +115 -0
  20. package/dist/esm/src/BanService.js.map +1 -0
  21. package/dist/esm/src/JanitorService.js +199 -92
  22. package/dist/esm/src/JanitorService.js.map +1 -1
  23. package/dist/esm/src/OverlayExpress.js +528 -72
  24. package/dist/esm/src/OverlayExpress.js.map +1 -1
  25. package/dist/esm/src/makeUserInterface.js +889 -217
  26. package/dist/esm/src/makeUserInterface.js.map +1 -1
  27. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  28. package/dist/types/mod.d.ts +4 -1
  29. package/dist/types/mod.d.ts.map +1 -1
  30. package/dist/types/src/BanAwareLookupWrapper.d.ts +31 -0
  31. package/dist/types/src/BanAwareLookupWrapper.d.ts.map +1 -0
  32. package/dist/types/src/BanService.d.ts +81 -0
  33. package/dist/types/src/BanService.d.ts.map +1 -0
  34. package/dist/types/src/JanitorService.d.ts +69 -13
  35. package/dist/types/src/JanitorService.d.ts.map +1 -1
  36. package/dist/types/src/OverlayExpress.d.ts +28 -4
  37. package/dist/types/src/OverlayExpress.d.ts.map +1 -1
  38. package/dist/types/src/makeUserInterface.d.ts +2 -0
  39. package/dist/types/src/makeUserInterface.d.ts.map +1 -1
  40. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  41. package/mod.ts +4 -1
  42. package/package.json +4 -2
  43. package/src/BanAwareLookupWrapper.ts +95 -0
  44. package/src/BanService.ts +154 -0
  45. package/src/JanitorService.ts +277 -101
  46. package/src/OverlayExpress.ts +575 -76
  47. package/src/__tests__/BanAwareLookupWrapper.test.ts +255 -0
  48. package/src/__tests__/BanService.test.ts +310 -0
  49. package/src/__tests__/JanitorService.test.ts +342 -5
  50. package/src/__tests__/OverlayExpress.test.ts +19 -6
  51. package/src/__tests__/makeUserInterface.test.ts +4 -3
  52. package/src/makeUserInterface.ts +892 -217
@@ -298,7 +298,6 @@ describe('JanitorService', () => {
298
298
  { _id: '123' },
299
299
  { $inc: { down: 1 } }
300
300
  )
301
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('Invalid domain'))
302
301
  })
303
302
 
304
303
  it('should accept localhost', async () => {
@@ -388,7 +387,6 @@ describe('JanitorService', () => {
388
387
  { _id: '123' },
389
388
  { $inc: { down: -1 } }
390
389
  )
391
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('is healthy'))
392
390
  })
393
391
 
394
392
  it('should not decrement when already at 0', async () => {
@@ -447,7 +445,6 @@ describe('JanitorService', () => {
447
445
  { _id: '123' },
448
446
  { $inc: { down: 1 } }
449
447
  )
450
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('is unhealthy'))
451
448
  })
452
449
 
453
450
  it('should delete output when down count reaches threshold', async () => {
@@ -476,7 +473,7 @@ describe('JanitorService', () => {
476
473
  await janitor.run()
477
474
 
478
475
  expect(mockCollection.deleteOne).toHaveBeenCalledWith({ _id: '123' })
479
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('Deleting output'))
476
+ expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('Removing output'))
480
477
  })
481
478
 
482
479
  it('should handle fetch timeout', async () => {
@@ -504,7 +501,6 @@ describe('JanitorService', () => {
504
501
 
505
502
  await janitor.run()
506
503
 
507
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('timeout'))
508
504
  expect(mockCollection.updateOne).toHaveBeenCalledWith(
509
505
  { _id: '123' },
510
506
  { $inc: { down: 1 } }
@@ -601,4 +597,345 @@ describe('JanitorService', () => {
601
597
  )
602
598
  })
603
599
  })
600
+
601
+ describe('checkHost', () => {
602
+ it('should return healthy when health endpoint returns ok', async () => {
603
+ ;(global.fetch as jest.Mock<any>).mockResolvedValue({
604
+ ok: true,
605
+ status: 200,
606
+ json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
607
+ })
608
+
609
+ const janitor = new JanitorService({
610
+ mongoDb: mockDb,
611
+ logger: mockLogger
612
+ })
613
+
614
+ const result = await janitor.checkHost('https://example.com')
615
+
616
+ expect(result.healthy).toBe(true)
617
+ expect(result.statusCode).toBe(200)
618
+ expect(result.responseTimeMs).toBeGreaterThanOrEqual(0)
619
+ })
620
+
621
+ it('should return unhealthy for invalid domain', async () => {
622
+ const janitor = new JanitorService({
623
+ mongoDb: mockDb,
624
+ logger: mockLogger
625
+ })
626
+
627
+ const result = await janitor.checkHost('invalid domain!@#')
628
+
629
+ expect(result.healthy).toBe(false)
630
+ expect(result.error).toBe('Invalid domain')
631
+ })
632
+
633
+ it('should return unhealthy when health endpoint returns non-ok status', async () => {
634
+ ;(global.fetch as jest.Mock<any>).mockResolvedValue({
635
+ ok: false,
636
+ status: 503
637
+ })
638
+
639
+ const janitor = new JanitorService({
640
+ mongoDb: mockDb,
641
+ logger: mockLogger
642
+ })
643
+
644
+ const result = await janitor.checkHost('https://example.com')
645
+
646
+ expect(result.healthy).toBe(false)
647
+ expect(result.statusCode).toBe(503)
648
+ expect(result.error).toBe('HTTP 503')
649
+ })
650
+
651
+ it('should return unhealthy when response does not have status ok', async () => {
652
+ ;(global.fetch as jest.Mock<any>).mockResolvedValue({
653
+ ok: true,
654
+ status: 200,
655
+ json: jest.fn<any>().mockResolvedValue({ status: 'error' })
656
+ })
657
+
658
+ const janitor = new JanitorService({
659
+ mongoDb: mockDb,
660
+ logger: mockLogger
661
+ })
662
+
663
+ const result = await janitor.checkHost('https://example.com')
664
+
665
+ expect(result.healthy).toBe(false)
666
+ expect(result.error).toBe('Unexpected response')
667
+ })
668
+
669
+ it('should handle timeout (AbortError)', async () => {
670
+ const abortError = new Error('Aborted')
671
+ abortError.name = 'AbortError'
672
+ ;(global.fetch as jest.Mock<any>).mockRejectedValue(abortError)
673
+
674
+ const janitor = new JanitorService({
675
+ mongoDb: mockDb,
676
+ logger: mockLogger,
677
+ requestTimeoutMs: 100
678
+ })
679
+
680
+ const result = await janitor.checkHost('https://example.com')
681
+
682
+ expect(result.healthy).toBe(false)
683
+ expect(result.error).toBe('Timeout')
684
+ })
685
+
686
+ it('should handle network errors', async () => {
687
+ ;(global.fetch as jest.Mock<any>).mockRejectedValue(new Error('ECONNREFUSED'))
688
+
689
+ const janitor = new JanitorService({
690
+ mongoDb: mockDb,
691
+ logger: mockLogger
692
+ })
693
+
694
+ const result = await janitor.checkHost('https://example.com')
695
+
696
+ expect(result.healthy).toBe(false)
697
+ expect(result.error).toBe('ECONNREFUSED')
698
+ })
699
+
700
+ it('should prepend https:// to domains without protocol', async () => {
701
+ ;(global.fetch as jest.Mock<any>).mockResolvedValue({
702
+ ok: true,
703
+ status: 200,
704
+ json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
705
+ })
706
+
707
+ const janitor = new JanitorService({
708
+ mongoDb: mockDb,
709
+ logger: mockLogger
710
+ })
711
+
712
+ await janitor.checkHost('example.com')
713
+
714
+ expect(global.fetch).toHaveBeenCalledWith(
715
+ 'https://example.com/health',
716
+ expect.any(Object)
717
+ )
718
+ })
719
+ })
720
+
721
+ describe('getHealthStatus', () => {
722
+ it('should return health status for ship and slap records', async () => {
723
+ const shipOutput = {
724
+ txid: 'ship-tx',
725
+ outputIndex: 0,
726
+ domain: 'https://ship.example.com',
727
+ topic: 'tm_ship',
728
+ identityKey: 'key1',
729
+ createdAt: new Date(),
730
+ down: 1
731
+ }
732
+ const slapOutput = {
733
+ txid: 'slap-tx',
734
+ outputIndex: 1,
735
+ domain: 'https://slap.example.com',
736
+ service: 'ls_slap',
737
+ identityKey: 'key2',
738
+ createdAt: new Date(),
739
+ down: 0
740
+ }
741
+
742
+ // First call returns ship records, second returns slap records
743
+ let callCount = 0
744
+ mockCollection.find.mockImplementation(() => ({
745
+ toArray: jest.fn<any>().mockResolvedValue(callCount++ === 0 ? [shipOutput] : [slapOutput])
746
+ }))
747
+
748
+ const janitor = new JanitorService({
749
+ mongoDb: mockDb,
750
+ logger: mockLogger
751
+ })
752
+
753
+ const result = await janitor.getHealthStatus()
754
+
755
+ expect(result.ship).toHaveLength(1)
756
+ expect(result.ship[0].txid).toBe('ship-tx')
757
+ expect(result.ship[0].downCount).toBe(1)
758
+ expect(result.slap).toHaveLength(1)
759
+ expect(result.slap[0].txid).toBe('slap-tx')
760
+ expect(result.slap[0].downCount).toBe(0)
761
+ })
762
+
763
+ it('should return empty arrays when no records exist', async () => {
764
+ const janitor = new JanitorService({
765
+ mongoDb: mockDb,
766
+ logger: mockLogger
767
+ })
768
+
769
+ const result = await janitor.getHealthStatus()
770
+
771
+ expect(result.ship).toEqual([])
772
+ expect(result.slap).toEqual([])
773
+ })
774
+ })
775
+
776
+ describe('ban service integration', () => {
777
+ it('should auto-ban domain and outpoint when threshold is reached', async () => {
778
+ const mockBanService = {
779
+ banDomain: jest.fn<any>().mockResolvedValue(undefined),
780
+ banOutpoint: jest.fn<any>().mockResolvedValue(undefined)
781
+ }
782
+
783
+ const mockOutput = {
784
+ _id: '123',
785
+ txid: 'abc123',
786
+ outputIndex: 0,
787
+ domain: 'https://dead-host.com',
788
+ down: 2
789
+ }
790
+
791
+ mockCollection.find.mockReturnValue({
792
+ toArray: jest.fn<any>().mockResolvedValue([mockOutput])
793
+ })
794
+
795
+ ;(global.fetch as jest.Mock<any>).mockResolvedValue({ ok: false })
796
+
797
+ const janitor = new JanitorService({
798
+ mongoDb: mockDb,
799
+ logger: mockLogger,
800
+ hostDownRevokeScore: 3,
801
+ banService: mockBanService as any
802
+ })
803
+
804
+ await janitor.run()
805
+
806
+ expect(mockCollection.deleteOne).toHaveBeenCalledWith({ _id: '123' })
807
+ expect(mockBanService.banOutpoint).toHaveBeenCalledWith(
808
+ 'abc123',
809
+ 0,
810
+ expect.stringContaining('Auto-banned'),
811
+ 'https://dead-host.com'
812
+ )
813
+ expect(mockBanService.banDomain).toHaveBeenCalledWith(
814
+ 'https://dead-host.com',
815
+ expect.stringContaining('Auto-banned')
816
+ )
817
+ })
818
+
819
+ it('should not auto-ban when autoBanOnRemoval is false', async () => {
820
+ const mockBanService = {
821
+ banDomain: jest.fn<any>().mockResolvedValue(undefined),
822
+ banOutpoint: jest.fn<any>().mockResolvedValue(undefined)
823
+ }
824
+
825
+ const mockOutput = {
826
+ _id: '123',
827
+ txid: 'abc123',
828
+ outputIndex: 0,
829
+ domain: 'https://dead-host.com',
830
+ down: 2
831
+ }
832
+
833
+ mockCollection.find.mockReturnValue({
834
+ toArray: jest.fn<any>().mockResolvedValue([mockOutput])
835
+ })
836
+
837
+ ;(global.fetch as jest.Mock<any>).mockResolvedValue({ ok: false })
838
+
839
+ const janitor = new JanitorService({
840
+ mongoDb: mockDb,
841
+ logger: mockLogger,
842
+ hostDownRevokeScore: 3,
843
+ banService: mockBanService as any,
844
+ autoBanOnRemoval: false
845
+ })
846
+
847
+ await janitor.run()
848
+
849
+ expect(mockCollection.deleteOne).toHaveBeenCalledWith({ _id: '123' })
850
+ expect(mockBanService.banOutpoint).not.toHaveBeenCalled()
851
+ expect(mockBanService.banDomain).not.toHaveBeenCalled()
852
+ })
853
+
854
+ it('should not ban domain when domain is unknown', async () => {
855
+ const mockBanService = {
856
+ banDomain: jest.fn<any>().mockResolvedValue(undefined),
857
+ banOutpoint: jest.fn<any>().mockResolvedValue(undefined)
858
+ }
859
+
860
+ const mockOutput = {
861
+ _id: '123',
862
+ txid: 'abc123',
863
+ outputIndex: 0,
864
+ down: 2
865
+ // no domain field
866
+ }
867
+
868
+ mockCollection.find.mockReturnValue({
869
+ toArray: jest.fn<any>().mockResolvedValue([mockOutput])
870
+ })
871
+
872
+ ;(global.fetch as jest.Mock<any>).mockResolvedValue({ ok: false })
873
+
874
+ const janitor = new JanitorService({
875
+ mongoDb: mockDb,
876
+ logger: mockLogger,
877
+ hostDownRevokeScore: 3,
878
+ banService: mockBanService as any
879
+ })
880
+
881
+ await janitor.run()
882
+
883
+ expect(mockBanService.banOutpoint).toHaveBeenCalled()
884
+ expect(mockBanService.banDomain).not.toHaveBeenCalled()
885
+ })
886
+ })
887
+
888
+ describe('run report', () => {
889
+ it('should return a JanitorReport with summary', async () => {
890
+ const mockOutput = {
891
+ _id: '123',
892
+ txid: 'abc123',
893
+ outputIndex: 0,
894
+ domain: 'https://example.com',
895
+ down: 0
896
+ }
897
+
898
+ mockCollection.find.mockReturnValue({
899
+ toArray: jest.fn<any>().mockResolvedValue([mockOutput])
900
+ })
901
+
902
+ ;(global.fetch as jest.Mock<any>).mockResolvedValue({
903
+ ok: true,
904
+ status: 200,
905
+ json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
906
+ })
907
+
908
+ const janitor = new JanitorService({
909
+ mongoDb: mockDb,
910
+ logger: mockLogger
911
+ })
912
+
913
+ const report = await janitor.run()
914
+
915
+ expect(report.startedAt).toBeInstanceOf(Date)
916
+ expect(report.completedAt).toBeInstanceOf(Date)
917
+ expect(report.durationMs).toBeGreaterThanOrEqual(0)
918
+ expect(report.summary.totalChecked).toBeGreaterThan(0)
919
+ expect(report.summary.removed).toBe(0)
920
+ expect(report.summary.banned).toBe(0)
921
+ })
922
+
923
+ it('should handle collection errors gracefully and return empty report', async () => {
924
+ ;(mockDb as any).collection = jest.fn().mockImplementation(() => {
925
+ throw new Error('Database error')
926
+ })
927
+
928
+ const janitor = new JanitorService({
929
+ mongoDb: mockDb,
930
+ logger: mockLogger
931
+ })
932
+
933
+ // checkTopicOutputs catches errors internally, so run() returns an empty report
934
+ const report = await janitor.run()
935
+ expect(report.summary.totalChecked).toBe(0)
936
+ expect(report.shipResults).toEqual([])
937
+ expect(report.slapResults).toEqual([])
938
+ expect(mockLogger.error).toHaveBeenCalled()
939
+ })
940
+ })
604
941
  })
@@ -12,6 +12,19 @@ jest.mock('@bsv/overlay')
12
12
  jest.mock('@bsv/sdk')
13
13
  jest.mock('@bsv/overlay-discovery-services')
14
14
 
15
+ /** Creates a mock MongoDB Db object with a collection stub that supports BanService */
16
+ function createMockDbValue (): Record<string, any> {
17
+ const mockCollection = {
18
+ createIndex: jest.fn<any>().mockResolvedValue(undefined),
19
+ find: jest.fn<any>().mockReturnValue({ sort: jest.fn<any>().mockReturnValue({ toArray: jest.fn<any>().mockResolvedValue([]) }), toArray: jest.fn<any>().mockResolvedValue([]) }),
20
+ findOne: jest.fn<any>().mockResolvedValue(null),
21
+ updateOne: jest.fn<any>().mockResolvedValue({}),
22
+ deleteOne: jest.fn<any>().mockResolvedValue({}),
23
+ countDocuments: jest.fn<any>().mockResolvedValue(0)
24
+ }
25
+ return { collection: jest.fn<any>().mockReturnValue(mockCollection) }
26
+ }
27
+
15
28
  describe('OverlayExpress', () => {
16
29
  let overlayExpress: OverlayExpress
17
30
 
@@ -257,7 +270,7 @@ describe('OverlayExpress', () => {
257
270
  it('should configure MongoDB connection', async () => {
258
271
  // @ts-expect-error - Mock resolved value
259
272
  const mockConnect = jest.fn().mockResolvedValue(undefined)
260
- const mockDb = jest.fn().mockReturnValue({})
273
+ const mockDb = jest.fn().mockReturnValue(createMockDbValue())
261
274
  const mockClient = {
262
275
  connect: mockConnect,
263
276
  db: mockDb
@@ -373,7 +386,7 @@ describe('OverlayExpress', () => {
373
386
  beforeEach(async () => {
374
387
  // @ts-expect-error - Mock resolved value
375
388
  const mockConnect = jest.fn().mockResolvedValue(undefined)
376
- const mockDb = jest.fn().mockReturnValue({})
389
+ const mockDb = jest.fn().mockReturnValue(createMockDbValue())
377
390
  const mockClient = {
378
391
  connect: mockConnect,
379
392
  db: mockDb
@@ -452,7 +465,7 @@ describe('OverlayExpress', () => {
452
465
 
453
466
  // @ts-expect-error - Mock resolved value
454
467
  const mockConnect = jest.fn().mockResolvedValue(undefined)
455
- const mockDb = jest.fn().mockReturnValue({})
468
+ const mockDb = jest.fn().mockReturnValue(createMockDbValue())
456
469
  const mockClient = {
457
470
  connect: mockConnect,
458
471
  db: mockDb
@@ -504,7 +517,7 @@ describe('OverlayExpress', () => {
504
517
 
505
518
  // @ts-expect-error - Mock resolved value
506
519
  const mockConnect = jest.fn().mockResolvedValue(undefined)
507
- const mockDb = jest.fn().mockReturnValue({})
520
+ const mockDb = jest.fn().mockReturnValue(createMockDbValue())
508
521
  const mockClient = {
509
522
  connect: mockConnect,
510
523
  db: mockDb
@@ -568,7 +581,7 @@ describe('OverlayExpress', () => {
568
581
 
569
582
  // @ts-expect-error - Mock resolved value
570
583
  const mockConnect = jest.fn().mockResolvedValue(undefined)
571
- const mockDb = jest.fn().mockReturnValue({})
584
+ const mockDb = jest.fn().mockReturnValue(createMockDbValue())
572
585
  const mockClient = {
573
586
  connect: mockConnect,
574
587
  db: mockDb
@@ -667,7 +680,7 @@ describe('OverlayExpress', () => {
667
680
 
668
681
  // @ts-expect-error - Mock return value
669
682
  const mockConnect = jest.fn().mockResolvedValue(undefined)
670
- const mockDb = jest.fn().mockReturnValue({})
683
+ const mockDb = jest.fn().mockReturnValue(createMockDbValue())
671
684
  const mockClient = {
672
685
  connect: mockConnect,
673
686
  db: mockDb
@@ -47,9 +47,10 @@ describe('makeUserInterface', () => {
47
47
  }
48
48
  const html = makeUserInterface(config)
49
49
 
50
- expect(html).toContain('https://custom-host.com/listTopicManagers')
51
- expect(html).toContain('https://custom-host.com/listLookupServiceProviders')
52
- expect(html).toContain('https://custom-host.com/getDocumentationForTopicManager')
50
+ // HOST is set as a JS variable; URLs are built at runtime in the browser
51
+ expect(html).toContain("const HOST = 'https://custom-host.com'")
52
+ expect(html).toContain("HOST + '/listTopicManagers'")
53
+ expect(html).toContain('/getDocumentationForTopicManager')
53
54
  })
54
55
 
55
56
  it('should use custom favicon URL', () => {