@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
package/dist/cjs/mod.js CHANGED
@@ -3,7 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.default = void 0;
6
+ exports.JanitorService = exports.BanAwareLookupWrapper = exports.BanService = exports.default = void 0;
7
7
  var OverlayExpress_js_1 = require("./src/OverlayExpress.js");
8
8
  Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(OverlayExpress_js_1).default; } });
9
+ var BanService_js_1 = require("./src/BanService.js");
10
+ Object.defineProperty(exports, "BanService", { enumerable: true, get: function () { return BanService_js_1.BanService; } });
11
+ var BanAwareLookupWrapper_js_1 = require("./src/BanAwareLookupWrapper.js");
12
+ Object.defineProperty(exports, "BanAwareLookupWrapper", { enumerable: true, get: function () { return BanAwareLookupWrapper_js_1.BanAwareLookupWrapper; } });
13
+ var JanitorService_js_1 = require("./src/JanitorService.js");
14
+ Object.defineProperty(exports, "JanitorService", { enumerable: true, get: function () { return JanitorService_js_1.JanitorService; } });
9
15
  //# sourceMappingURL=mod.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mod.js","sourceRoot":"","sources":["../../mod.ts"],"names":[],"mappings":";;;;;;AAAA,6DAAiD;AAAxC,6HAAA,OAAO,OAAA"}
1
+ {"version":3,"file":"mod.js","sourceRoot":"","sources":["../../mod.ts"],"names":[],"mappings":";;;;;;AAAA,6DAAiD;AAAxC,6HAAA,OAAO,OAAA;AAChB,qDAAmE;AAA1D,2GAAA,UAAU,OAAA;AACnB,2EAAsE;AAA7D,iIAAA,qBAAqB,OAAA;AAC9B,6DAAuH;AAA9G,mHAAA,cAAc,OAAA"}
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/overlay-express",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "type": "commonjs",
5
5
  "description": "BSV Blockchain Overlay Express",
6
6
  "files": [
@@ -50,9 +50,11 @@
50
50
  "typescript": "^5.2.2"
51
51
  },
52
52
  "dependencies": {
53
- "@bsv/overlay": "^2.0.1",
53
+ "@bsv/auth-express-middleware": "^2.0.2",
54
+ "@bsv/overlay": "^2.0.2",
54
55
  "@bsv/overlay-discovery-services": "^2.0.2",
55
56
  "@bsv/sdk": "^2.0.4",
57
+ "@bsv/wallet-toolbox-client": "^2.0.20",
56
58
  "body-parser": "^2.2.2",
57
59
  "chalk": "^5.6.2",
58
60
  "express": "^5.2.1",
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BanAwareLookupWrapper = void 0;
4
+ const sdk_1 = require("@bsv/sdk");
5
+ /**
6
+ * Wraps a SHIP or SLAP LookupService to intercept outputAdmittedByTopic calls
7
+ * and block outputs whose domain or outpoint appears in the persistent ban list.
8
+ *
9
+ * This prevents GASP from re-syncing stale or banned tokens that were previously
10
+ * removed by the Janitor or an admin.
11
+ */
12
+ class BanAwareLookupWrapper {
13
+ constructor(wrapped, banService, protocol, logger = console) {
14
+ this.wrapped = wrapped;
15
+ this.banService = banService;
16
+ this.protocol = protocol;
17
+ this.logger = logger;
18
+ this.admissionMode = wrapped.admissionMode;
19
+ this.spendNotificationMode = wrapped.spendNotificationMode;
20
+ }
21
+ /**
22
+ * Intercepts admission to check the ban list before delegating to the wrapped service.
23
+ * If the output's domain or outpoint is banned, the admission is silently blocked.
24
+ */
25
+ async outputAdmittedByTopic(payload) {
26
+ if (payload.mode === 'locking-script') {
27
+ const { txid, outputIndex, lockingScript } = payload;
28
+ // Check if the specific outpoint is banned
29
+ if (await this.banService.isOutpointBanned(txid, outputIndex)) {
30
+ this.logger.log(`[BAN] Blocked banned outpoint ${txid}.${outputIndex} from ${this.protocol}`);
31
+ return;
32
+ }
33
+ // Try to parse the domain from PushDrop fields and check domain ban
34
+ try {
35
+ const result = sdk_1.PushDrop.decode(lockingScript);
36
+ if (result.fields.length >= 3) {
37
+ const domain = sdk_1.Utils.toUTF8(result.fields[2]);
38
+ if (await this.banService.isDomainBanned(domain)) {
39
+ this.logger.log(`[BAN] Blocked banned domain ${domain} from ${this.protocol} (${txid}.${outputIndex})`);
40
+ return;
41
+ }
42
+ }
43
+ }
44
+ catch {
45
+ // If we can't parse PushDrop fields, let it through to the wrapped service
46
+ // which will apply its own validation
47
+ }
48
+ }
49
+ // Delegate to the wrapped service
50
+ return await this.wrapped.outputAdmittedByTopic(payload);
51
+ }
52
+ async outputSpent(payload) {
53
+ if (typeof this.wrapped.outputSpent === 'function') {
54
+ return await this.wrapped.outputSpent(payload);
55
+ }
56
+ }
57
+ async outputNoLongerRetainedInHistory(txid, outputIndex, topic) {
58
+ if (typeof this.wrapped.outputNoLongerRetainedInHistory === 'function') {
59
+ return await this.wrapped.outputNoLongerRetainedInHistory(txid, outputIndex, topic);
60
+ }
61
+ }
62
+ async outputEvicted(txid, outputIndex) {
63
+ return await this.wrapped.outputEvicted(txid, outputIndex);
64
+ }
65
+ async lookup(question) {
66
+ return await this.wrapped.lookup(question);
67
+ }
68
+ async getDocumentation() {
69
+ return await this.wrapped.getDocumentation();
70
+ }
71
+ async getMetaData() {
72
+ return await this.wrapped.getMetaData();
73
+ }
74
+ }
75
+ exports.BanAwareLookupWrapper = BanAwareLookupWrapper;
76
+ //# sourceMappingURL=BanAwareLookupWrapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BanAwareLookupWrapper.js","sourceRoot":"","sources":["../../../src/BanAwareLookupWrapper.ts"],"names":[],"mappings":";;;AASA,kCAA0D;AAG1D;;;;;;GAMG;AACH,MAAa,qBAAqB;IAIhC,YACmB,OAAsB,EACtB,UAAsB,EACtB,QAAyB,EACzB,SAAyB,OAAO;QAHhC,YAAO,GAAP,OAAO,CAAe;QACtB,eAAU,GAAV,UAAU,CAAY;QACtB,aAAQ,GAAR,QAAQ,CAAiB;QACzB,WAAM,GAAN,MAAM,CAA0B;QAEjD,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,CAAA;QAC1C,IAAI,CAAC,qBAAqB,GAAG,OAAO,CAAC,qBAAqB,CAAA;IAC5D,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,qBAAqB,CAAE,OAA8B;QACzD,IAAI,OAAO,CAAC,IAAI,KAAK,gBAAgB,EAAE,CAAC;YACtC,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,aAAa,EAAE,GAAG,OAAO,CAAA;YAEpD,2CAA2C;YAC3C,IAAI,MAAM,IAAI,CAAC,UAAU,CAAC,gBAAgB,CAAC,IAAI,EAAE,WAAW,CAAC,EAAE,CAAC;gBAC9D,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,iCAAiC,IAAI,IAAI,WAAW,SAAS,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAA;gBAC7F,OAAM;YACR,CAAC;YAED,oEAAoE;YACpE,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,cAAQ,CAAC,MAAM,CAAC,a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AC,MAAM,CAAE,QAAwB;QACpC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;IAC5C,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,CAAA;IAC9C,CAAC;IAED,KAAK,CAAC,WAAW;QACf,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAAA;IACzC,CAAC;CACF;AA3ED,sDA2EC"}
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BanService = void 0;
4
+ /**
5
+ * BanService provides a persistent ban list stored in MongoDB.
6
+ *
7
+ * When the Janitor or an admin removes a SHIP/SLAP token, the associated
8
+ * domain or outpoint can be added to the ban list. The BanAwareLookupWrapper
9
+ * checks this list before admitting new outputs, preventing GASP from
10
+ * re-syncing previously removed stale tokens.
11
+ */
12
+ class BanService {
13
+ constructor(db) {
14
+ this.bans = db.collection('bannedRecords');
15
+ }
16
+ /**
17
+ * Sanitizes a value for use in MongoDB queries, preventing NoSQL injection.
18
+ * Ensures the value is a plain string and rejects objects/arrays that could
19
+ * contain MongoDB operators like $ne, $gt, etc.
20
+ */
21
+ sanitize(value) {
22
+ if (typeof value !== 'string') {
23
+ throw new Error('Invalid input: expected a string value');
24
+ }
25
+ return value;
26
+ }
27
+ /**
28
+ * Creates indexes for efficient ban lookups.
29
+ */
30
+ async ensureIndexes() {
31
+ await this.bans.createIndex({ type: 1, value: 1 }, { unique: true });
32
+ await this.bans.createIndex({ bannedAt: -1 });
33
+ }
34
+ /**
35
+ * Bans a domain, preventing any SHIP/SLAP tokens referencing it from being stored.
36
+ */
37
+ async banDomain(domain, reason, bannedBy) {
38
+ const safeDomain = this.sanitize(domain);
39
+ await this.bans.updateOne({ type: 'domain', value: safeDomain }, {
40
+ $set: {
41
+ type: 'domain',
42
+ value: safeDomain,
43
+ reason: reason !== null && reason !== void 0 ? reason : 'Manually banned',
44
+ bannedAt: new Date(),
45
+ bannedBy
46
+ }
47
+ }, { upsert: true });
48
+ }
49
+ /**
50
+ * Removes a domain ban.
51
+ */
52
+ async unbanDomain(domain) {
53
+ await this.bans.deleteOne({ type: 'domain', value: this.sanitize(domain) });
54
+ }
55
+ /**
56
+ * Checks if a domain is banned.
57
+ */
58
+ async isDomainBanned(domain) {
59
+ const record = await this.bans.findOne({ type: 'domain', value: this.sanitize(domain) });
60
+ return record !== null;
61
+ }
62
+ /**
63
+ * Bans a specific outpoint (txid.outputIndex), preventing it from being re-admitted.
64
+ */
65
+ async banOutpoint(txid, outputIndex, reason, domain, bannedBy) {
66
+ const value = `${this.sanitize(txid)}.${Number(outputIndex)}`;
67
+ await this.bans.updateOne({ type: 'outpoint', value }, {
68
+ $set: {
69
+ type: 'outpoint',
70
+ value,
71
+ domain: domain !== undefined ? this.sanitize(domain) : undefined,
72
+ reason: reason !== null && reason !== void 0 ? reason : 'Manually banned',
73
+ bannedAt: new Date(),
74
+ bannedBy
75
+ }
76
+ }, { upsert: true });
77
+ }
78
+ /**
79
+ * Removes an outpoint ban.
80
+ */
81
+ async unbanOutpoint(txid, outputIndex) {
82
+ const value = `${this.sanitize(txid)}.${Number(outputIndex)}`;
83
+ await this.bans.deleteOne({ type: 'outpoint', value });
84
+ }
85
+ /**
86
+ * Checks if a specific outpoint is banned.
87
+ */
88
+ async isOutpointBanned(txid, outputIndex) {
89
+ const value = `${this.sanitize(txid)}.${Number(outputIndex)}`;
90
+ const record = await this.bans.findOne({ type: 'outpoint', value });
91
+ return record !== null;
92
+ }
93
+ /**
94
+ * Lists all bans, optionally filtered by type.
95
+ */
96
+ async listBans(type) {
97
+ const query = typeof type === 'string' ? { type } : {};
98
+ return await this.bans.find(query).sort({ bannedAt: -1 }).toArray();
99
+ }
100
+ /**
101
+ * Removes a ban by type and value.
102
+ */
103
+ async removeBan(type, value) {
104
+ await this.bans.deleteOne({ type: this.sanitize(type), value: this.sanitize(value) });
105
+ }
106
+ /**
107
+ * Returns ban statistics.
108
+ */
109
+ async getStats() {
110
+ const [domainBans, outpointBans] = await Promise.all([
111
+ this.bans.countDocuments({ type: 'domain' }),
112
+ this.bans.countDocuments({ type: 'outpoint' })
113
+ ]);
114
+ return { domainBans, outpointBans, totalBans: domainBans + outpointBans };
115
+ }
116
+ }
117
+ exports.BanService = BanService;
118
+ //# sourceMappingURL=BanService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BanService.js","sourceRoot":"","sources":["../../../src/BanService.ts"],"names":[],"mappings":";;;AAoBA;;;;;;;GAOG;AACH,MAAa,UAAU;IAGrB,YAAa,EAAM;QACjB,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC,UAAU,CAAe,eAAe,CAAC,CAAA;IAC1D,CAAC;IAED;;;;OAIG;IACK,QAAQ,CAAE,KAAc;QAC9B,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAA;QAC3D,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,aAAa;QACjB,MAAM,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAA;QACpE,MAAM,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,CAAA;IAC/C,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,SAAS,CAAE,MAAc,EAAE,MAAe,EAAE,QAAiB;QACjE,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;QACxC,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,CACvB,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,EAA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,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAA0B,EAAE,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IAChH,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,QAAQ;QACZ,MAAM,CAAC,UAAU,EAAE,YAAY,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;YACnD,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;YAC5C,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;SAC/C,CAAC,CAAA;QACF,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,SAAS,EAAE,UAAU,GAAG,YAAY,EAAE,CAAA;IAC3E,CAAC;CACF;AA7HD,gCA6HC"}
@@ -6,92 +6,239 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.JanitorService = void 0;
7
7
  const chalk_1 = __importDefault(require("chalk"));
8
8
  /**
9
- * JanitorService runs a single pass of health checks on SHIP and SLAP outputs.
9
+ * JanitorService runs health checks on SHIP and SLAP outputs.
10
10
  * It validates domain names and checks /health endpoints to ensure services are operational.
11
11
  *
12
12
  * When a service is down, it increments a "down" counter. When healthy, it decrements.
13
- * If the down counter reaches HOST_DOWN_REVOKE_SCORE, it deletes the output from the database.
14
- *
15
- * This service is designed to be run periodically via external schedulers (e.g., cron, docker-compose).
13
+ * If the down counter reaches hostDownRevokeScore, the output is deleted and optionally
14
+ * the domain is added to the persistent ban list to prevent GASP re-sync.
16
15
  */
17
16
  class JanitorService {
18
17
  constructor(config) {
19
- var _a, _b, _c;
18
+ var _a, _b, _c, _d;
20
19
  this.mongoDb = config.mongoDb;
21
20
  this.logger = (_a = config.logger) !== null && _a !== void 0 ? _a : console;
22
- this.requestTimeoutMs = (_b = config.requestTimeoutMs) !== null && _b !== void 0 ? _b : 10000; // Default: 10 seconds
21
+ this.requestTimeoutMs = (_b = config.requestTimeoutMs) !== null && _b !== void 0 ? _b : 10000;
23
22
  this.hostDownRevokeScore = (_c = config.hostDownRevokeScore) !== null && _c !== void 0 ? _c : 3;
23
+ this.banService = config.banService;
24
+ this.autoBanOnRemoval = (_d = config.autoBanOnRemoval) !== null && _d !== void 0 ? _d : true;
24
25
  }
25
26
  /**
26
- * Runs a single pass of health checks on all SHIP and SLAP outputs
27
+ * Runs a full pass of health checks on all SHIP and SLAP outputs.
28
+ * Returns a detailed report of the results.
27
29
  */
28
30
  async run() {
29
- this.logger.log(chalk_1.default.blue('🧹 Running janitor health checks...'));
31
+ const startedAt = new Date();
32
+ this.logger.log(chalk_1.default.blue('Running janitor health checks...'));
33
+ let shipResults = [];
34
+ let slapResults = [];
35
+ let removed = 0;
36
+ let banned = 0;
30
37
  try {
31
- // Check SHIP outputs
32
- await this.checkTopicOutputs('shipRecords');
33
- // Check SLAP outputs
34
- await this.checkTopicOutputs('slapRecords');
35
- this.logger.log(chalk_1.default.green('🧹 Janitor health checks completed'));
38
+ const shipCheckResult = await this.checkTopicOutputs('shipRecords', 'topic');
39
+ shipResults = shipCheckResult.results;
40
+ removed += shipCheckResult.removed;
41
+ banned += shipCheckResult.banned;
42
+ const slapCheckResult = await this.checkTopicOutputs('slapRecords', 'service');
43
+ slapResults = slapCheckResult.results;
44
+ removed += slapCheckResult.removed;
45
+ banned += slapCheckResult.banned;
46
+ this.logger.log(chalk_1.default.green('Janitor health checks completed'));
36
47
  }
37
48
  catch (error) {
38
- this.logger.error(chalk_1.default.red('❌ Error during health checks:'), error);
49
+ this.logger.error(chalk_1.default.red('Error during health checks:'), error);
39
50
  throw error;
40
51
  }
52
+ const completedAt = new Date();
53
+ const allResults = [...shipResults, ...slapResults];
54
+ return {
55
+ startedAt,
56
+ completedAt,
57
+ durationMs: completedAt.getTime() - startedAt.getTime(),
58
+ shipResults,
59
+ slapResults,
60
+ summary: {
61
+ totalChecked: allResults.length,
62
+ healthy: allResults.filter(r => r.healthy).length,
63
+ unhealthy: allResults.filter(r => !r.healthy).length,
64
+ removed,
65
+ banned
66
+ }
67
+ };
68
+ }
69
+ /**
70
+ * Checks a single URL's health endpoint. Used by the admin dashboard for on-demand checks.
71
+ */
72
+ async checkHost(url) {
73
+ var _a, _b;
74
+ const startTime = Date.now();
75
+ if (!this.isValidDomain(url)) {
76
+ return {
77
+ healthy: false,
78
+ responseTimeMs: Date.now() - startTime,
79
+ error: 'Invalid domain'
80
+ };
81
+ }
82
+ try {
83
+ const fullURL = url.startsWith('http') ? url : `https://${url}`;
84
+ const healthURL = new URL('/health', fullURL).toString();
85
+ const controller = new AbortController();
86
+ const timeout = setTimeout(() => controller.abort(), this.requestTimeoutMs);
87
+ try {
88
+ const response = await fetch(healthURL, {
89
+ method: 'GET',
90
+ signal: controller.signal,
91
+ headers: { Accept: 'application/json' }
92
+ });
93
+ clearTimeout(timeout);
94
+ const responseTimeMs = Date.now() - startTime;
95
+ if (!response.ok) {
96
+ return { healthy: false, responseTimeMs, statusCode: response.status, error: `HTTP ${response.status}` };
97
+ }
98
+ const data = await response.json();
99
+ const healthy = (data === null || data === void 0 ? void 0 : data.status) === 'ok';
100
+ return { healthy, responseTimeMs, statusCode: response.status, error: healthy ? undefined : 'Unexpected response' };
101
+ }
102
+ catch (error) {
103
+ clearTimeout(timeout);
104
+ const responseTimeMs = Date.now() - startTime;
105
+ if (error.name === 'AbortError') {
106
+ return { healthy: false, responseTimeMs, error: 'Timeout' };
107
+ }
108
+ return { healthy: false, responseTimeMs, error: (_a = error.message) !== null && _a !== void 0 ? _a : 'Connection failed' };
109
+ }
110
+ }
111
+ catch (error) {
112
+ return { healthy: false, responseTimeMs: Date.now() - startTime, error: (_b = error.message) !== null && _b !== void 0 ? _b : 'Invalid URL' };
113
+ }
114
+ }
115
+ /**
116
+ * Gets health status for all records without modifying them.
117
+ * Used by the dashboard to display current state.
118
+ */
119
+ async getHealthStatus() {
120
+ const shipCollection = this.mongoDb.collection('shipRecords');
121
+ const slapCollection = this.mongoDb.collection('slapRecords');
122
+ const [shipOutputs, slapOutputs] = await Promise.all([
123
+ shipCollection.find({}).toArray(),
124
+ slapCollection.find({}).toArray()
125
+ ]);
126
+ const shipResults = shipOutputs.map(output => {
127
+ var _a;
128
+ return ({
129
+ txid: output.txid,
130
+ outputIndex: output.outputIndex,
131
+ domain: (_a = this.extractURLFromOutput(output)) !== null && _a !== void 0 ? _a : 'unknown',
132
+ topic: output.topic,
133
+ identityKey: output.identityKey,
134
+ createdAt: output.createdAt,
135
+ healthy: true, // Will be updated if health check is run
136
+ downCount: typeof output.down === 'number' ? output.down : 0
137
+ });
138
+ });
139
+ const slapResults = slapOutputs.map(output => {
140
+ var _a;
141
+ return ({
142
+ txid: output.txid,
143
+ outputIndex: output.outputIndex,
144
+ domain: (_a = this.extractURLFromOutput(output)) !== null && _a !== void 0 ? _a : 'unknown',
145
+ service: output.service,
146
+ identityKey: output.identityKey,
147
+ createdAt: output.createdAt,
148
+ healthy: true,
149
+ downCount: typeof output.down === 'number' ? output.down : 0
150
+ });
151
+ });
152
+ return { ship: shipResults, slap: slapResults };
41
153
  }
42
154
  /**
43
- * Checks all outputs for a specific topic
155
+ * Checks all outputs for a specific collection and returns results.
44
156
  */
45
- async checkTopicOutputs(collectionName) {
157
+ async checkTopicOutputs(collectionName, typeField) {
158
+ const results = [];
159
+ let removed = 0;
160
+ let banned = 0;
46
161
  try {
47
162
  const collection = this.mongoDb.collection(collectionName);
48
163
  const outputs = await collection.find({}).toArray();
49
- this.logger.log(chalk_1.default.cyan(`🔍 Checking ${outputs.length} ${collectionName.toUpperCase()} outputs...`));
164
+ this.logger.log(chalk_1.default.cyan(`Checking ${outputs.length} ${collectionName} outputs...`));
50
165
  for (const output of outputs) {
51
- await this.checkOutput(output, collection);
166
+ const result = await this.checkOutput(output, collection, typeField);
167
+ results.push(result);
168
+ if (result.error === 'REMOVED') {
169
+ removed++;
170
+ }
171
+ }
172
+ // Count auto-bans that happened during this run
173
+ if (this.banService !== undefined && this.autoBanOnRemoval) {
174
+ banned = removed; // Each removal triggers a ban
52
175
  }
53
176
  }
54
177
  catch (error) {
55
- this.logger.error(chalk_1.default.red(`❌ Error checking ${collectionName.toUpperCase()} outputs:`), error);
178
+ this.logger.error(chalk_1.default.red(`Error checking ${collectionName} outputs:`), error);
56
179
  }
180
+ return { results, removed, banned };
57
181
  }
58
182
  /**
59
- * Checks a single output for health
183
+ * Checks a single output for health and returns the result.
60
184
  */
61
- async checkOutput(output, collection) {
185
+ async checkOutput(output, collection, typeField) {
186
+ var _a, _b;
187
+ const domain = (_a = this.extractURLFromOutput(output)) !== null && _a !== void 0 ? _a : 'unknown';
188
+ const baseResult = {
189
+ txid: output.txid,
190
+ outputIndex: output.outputIndex,
191
+ domain,
192
+ identityKey: output.identityKey,
193
+ createdAt: output.createdAt,
194
+ healthy: false,
195
+ downCount: typeof output.down === 'number' ? output.down : 0
196
+ };
197
+ if (typeField === 'topic') {
198
+ baseResult.topic = output.topic;
199
+ }
200
+ else {
201
+ baseResult.service = output.service;
202
+ }
62
203
  try {
63
- // Extract the URL from the output
64
- const url = this.extractURLFromOutput(output);
65
- if (url === null) {
66
- return; // Skip if no valid URL
67
- }
68
- // Validate domain
69
- if (!this.isValidDomain(url)) {
70
- this.logger.log(chalk_1.default.yellow(`⚠️ Invalid domain for output ${String(output.txid)}:${String(output.outputIndex)}: ${url}`));
71
- await this.handleUnhealthyOutput(output, collection);
72
- return;
73
- }
74
- // Check health endpoint
75
- const isHealthy = await this.checkHealthEndpoint(url);
76
- if (isHealthy) {
204
+ if (domain === 'unknown') {
205
+ baseResult.error = 'No URL found';
206
+ await this.handleUnhealthyOutput(output, collection, domain);
207
+ return baseResult;
208
+ }
209
+ if (!this.isValidDomain(domain)) {
210
+ baseResult.error = 'Invalid domain';
211
+ await this.handleUnhealthyOutput(output, collection, domain);
212
+ return baseResult;
213
+ }
214
+ const healthResult = await this.checkHost(domain);
215
+ baseResult.healthy = healthResult.healthy;
216
+ baseResult.responseTimeMs = healthResult.responseTimeMs;
217
+ baseResult.statusCode = healthResult.statusCode;
218
+ baseResult.error = healthResult.error;
219
+ if (healthResult.healthy) {
77
220
  await this.handleHealthyOutput(output, collection);
221
+ baseResult.downCount = Math.max(0, baseResult.downCount - 1);
78
222
  }
79
223
  else {
80
- await this.handleUnhealthyOutput(output, collection);
224
+ const wasRemoved = await this.handleUnhealthyOutput(output, collection, domain);
225
+ baseResult.downCount++;
226
+ if (wasRemoved) {
227
+ baseResult.error = 'REMOVED';
228
+ }
81
229
  }
82
230
  }
83
231
  catch (error) {
84
- this.logger.error(chalk_1.default.red(`❌ Error checking output ${String(output.txid)}:${String(output.outputIndex)}:`), error);
85
- await this.handleUnhealthyOutput(output, collection).catch(() => { });
232
+ baseResult.error = (_b = error.message) !== null && _b !== void 0 ? _b : 'Check failed';
233
+ await this.handleUnhealthyOutput(output, collection, domain).catch(() => { });
86
234
  }
235
+ return baseResult;
87
236
  }
88
237
  /**
89
238
  * Extracts URL from output record
90
239
  */
91
240
  extractURLFromOutput(output) {
92
241
  try {
93
- // SHIP and SLAP outputs typically have a domain field or URL field
94
- // Check for common field names
95
242
  if (typeof output.domain === 'string') {
96
243
  return output.domain;
97
244
  }
@@ -101,7 +248,6 @@ class JanitorService {
101
248
  if (typeof output.serviceURL === 'string') {
102
249
  return output.serviceURL;
103
250
  }
104
- // SLAP outputs may have a protocols array with URLs
105
251
  if (Array.isArray(output.protocols) && output.protocols.length > 0) {
106
252
  const httpsProtocol = output.protocols.find((p) => typeof p === 'string' && p.startsWith('https://'));
107
253
  if (httpsProtocol !== undefined) {
@@ -119,11 +265,8 @@ class JanitorService {
119
265
  */
120
266
  isValidDomain(url) {
121
267
  try {
122
- // Parse the URL
123
268
  const parsedURL = new URL(url.startsWith('http') ? url : `https://${url}`);
124
- // Check if hostname is valid
125
269
  const hostname = parsedURL.hostname;
126
- // Basic domain validation
127
270
  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;
128
271
  const localhostRegex = /^localhost$/i;
129
272
  const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;
@@ -133,44 +276,6 @@ class JanitorService {
133
276
  return false;
134
277
  }
135
278
  }
136
- /**
137
- * Checks the /health endpoint of a service
138
- */
139
- async checkHealthEndpoint(url) {
140
- try {
141
- // Ensure URL has protocol
142
- const fullURL = url.startsWith('http') ? url : `https://${url}`;
143
- const healthURL = new URL('/health', fullURL).toString();
144
- // Create AbortController for timeout
145
- const controller = new AbortController();
146
- const timeout = setTimeout(() => controller.abort(), this.requestTimeoutMs);
147
- try {
148
- const response = await fetch(healthURL, {
149
- method: 'GET',
150
- signal: controller.signal,
151
- headers: {
152
- Accept: 'application/json'
153
- }
154
- });
155
- clearTimeout(timeout);
156
- if (!response.ok) {
157
- return false;
158
- }
159
- const data = await response.json();
160
- return (data === null || data === void 0 ? void 0 : data.status) === 'ok';
161
- }
162
- catch (error) {
163
- clearTimeout(timeout);
164
- if (error.name === 'AbortError') {
165
- this.logger.log(chalk_1.default.yellow(`⏱️ Health check timeout for ${healthURL}`));
166
- }
167
- return false;
168
- }
169
- }
170
- catch (error) {
171
- return false;
172
- }
173
- }
174
279
  /**
175
280
  * Handles a healthy output by decrementing its down counter
176
281
  */
@@ -178,36 +283,44 @@ class JanitorService {
178
283
  try {
179
284
  const currentDown = typeof output.down === 'number' ? output.down : 0;
180
285
  if (currentDown > 0) {
181
- // Decrement the down counter
182
286
  await collection.updateOne({ _id: output._id }, { $inc: { down: -1 } });
183
- this.logger.log(chalk_1.default.green(`✅ Output ${String(output.txid)}:${String(output.outputIndex)} is healthy (down: ${currentDown} -> ${currentDown - 1})`));
184
287
  }
185
288
  }
186
289
  catch (error) {
187
- this.logger.error(chalk_1.default.red(`❌ Error handling healthy output ${String(output.txid)}:${String(output.outputIndex)}:`), error);
290
+ this.logger.error(chalk_1.default.red(`Error handling healthy output ${String(output.txid)}:${String(output.outputIndex)}:`), error);
188
291
  }
189
292
  }
190
293
  /**
191
- * Handles an unhealthy output by incrementing its down counter and deleting if threshold is reached
294
+ * Handles an unhealthy output by incrementing its down counter.
295
+ * If the threshold is reached, deletes the record and optionally bans the domain.
296
+ * Returns true if the record was removed.
192
297
  */
193
- async handleUnhealthyOutput(output, collection) {
298
+ async handleUnhealthyOutput(output, collection, domain) {
194
299
  try {
195
300
  const currentDown = typeof output.down === 'number' ? output.down : 0;
196
301
  const newDown = currentDown + 1;
197
- this.logger.log(chalk_1.default.yellow(`⚠️ Output ${String(output.txid)}:${String(output.outputIndex)} is unhealthy (down: ${currentDown} -> ${newDown})`));
198
- // Check if we should delete the record
199
302
  if (newDown >= this.hostDownRevokeScore) {
200
- this.logger.log(chalk_1.default.red(`🚫 Deleting output ${String(output.txid)}:${String(output.outputIndex)} (down count: ${newDown} >= ${this.hostDownRevokeScore})`));
303
+ this.logger.log(chalk_1.default.red(`Removing output ${String(output.txid)}:${String(output.outputIndex)} (down: ${newDown} >= ${this.hostDownRevokeScore})`));
201
304
  await collection.deleteOne({ _id: output._id });
202
- this.logger.log(chalk_1.default.green(`✅ Successfully deleted output ${String(output.txid)}:${String(output.outputIndex)}`));
305
+ // Auto-ban the domain and outpoint to prevent GASP re-sync
306
+ if (this.banService !== undefined && this.autoBanOnRemoval) {
307
+ const txid = output.txid;
308
+ const outputIndex = output.outputIndex;
309
+ await this.banService.banOutpoint(txid, outputIndex, `Auto-banned by janitor: host down ${newDown} consecutive checks`, domain);
310
+ if (typeof domain === 'string' && domain !== 'unknown') {
311
+ await this.banService.banDomain(domain, `Auto-banned by janitor: host unresponsive after ${newDown} checks`);
312
+ }
313
+ }
314
+ return true;
203
315
  }
204
316
  else {
205
- // Increment the down counter
206
317
  await collection.updateOne({ _id: output._id }, { $inc: { down: 1 } });
318
+ return false;
207
319
  }
208
320
  }
209
321
  catch (error) {
210
- this.logger.error(chalk_1.default.red(`❌ Error handling unhealthy output ${String(output.txid)}:${String(output.outputIndex)}:`), error);
322
+ this.logger.error(chalk_1.default.red(`Error handling unhealthy output ${String(output.txid)}:${String(output.outputIndex)}:`), error);
323
+ return false;
211
324
  }
212
325
  }
213
326
  }