@bsv/overlay-express 0.8.2-rc1 → 0.8.2-rc2

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/overlay-express",
3
- "version": "0.5.2",
3
+ "version": "0.8.2-rc2",
4
4
  "type": "commonjs",
5
5
  "description": "BSV Blockchain Overlay Express",
6
6
  "files": [
@@ -11,18 +11,18 @@
11
11
  "LICENSE.txt"
12
12
  ],
13
13
  "scripts": {
14
- "test": "npm run build && jest",
15
- "test:watch": "npm run build && jest --watch",
16
- "test:coverage": "npm run build && jest --coverage",
14
+ "test": "echo \"No tests implemented yet\"",
17
15
  "lint": "ts-standard --fix src/**/*.ts",
16
+ "lint:ci": "ts-standard src/**/*.ts",
18
17
  "build": "tsc -b && tsconfig-to-dual-package tsconfig.cjs.json",
19
18
  "dev": "tsc -b -w",
20
19
  "prepublish": "npm run build",
21
- "doc": "ts2md --inputFilename=mod.ts --outputFilename=API.md --filenameSubstring=API --firstHeadingLevel=1"
20
+ "doc": "ts2md --inputFilename=mod.ts --outputFilename=API.md --filenameSubstring=API --firstHeadingLevel=1",
21
+ "demo": "rm -r dist && npm run build && npx tsx demo-ui.ts"
22
22
  },
23
23
  "repository": {
24
24
  "type": "git",
25
- "url": "git+https://github.com/bitcoin-sv/overlay-express.git"
25
+ "url": "git+https://github.com/bsv-blockchain/overlay-express.git"
26
26
  },
27
27
  "keywords": [
28
28
  "BSV",
@@ -34,9 +34,9 @@
34
34
  "author": "BSV Association",
35
35
  "license": "SEE LICENSE IN LICENSE.txt",
36
36
  "bugs": {
37
- "url": "https://github.com/bitcoin-sv/overlay-express/issues"
37
+ "url": "https://github.com/bsv-blockchain/overlay-express/issues"
38
38
  },
39
- "homepage": "https://github.com/bitcoin-sv/overlay-express#readme",
39
+ "homepage": "https://github.com/bsv-blockchain/overlay-express#readme",
40
40
  "devDependencies": {
41
41
  "@types/express": "^5.0.0",
42
42
  "@types/jest": "^29.5.12",
@@ -48,14 +48,14 @@
48
48
  "typescript": "^5.2.2"
49
49
  },
50
50
  "dependencies": {
51
- "@bsv/overlay": "^0.4.0",
52
- "@bsv/overlay-discovery-services": "^1.3.1",
53
- "@bsv/sdk": "^1.6.1",
51
+ "@bsv/overlay": "^0.5.0-rc1",
52
+ "@bsv/overlay-discovery-services": "^1.5.1",
53
+ "@bsv/sdk": "^1.8.2",
54
54
  "body-parser": "^1.20.3",
55
55
  "chalk": "^5.3.0",
56
- "express": "^4.21.1",
56
+ "express": "^4.21.2",
57
57
  "knex": "^3.1.0",
58
- "mongodb": "^6.11.0",
58
+ "mongodb": "^6.20.0",
59
59
  "uuid": "^11.0.5"
60
60
  }
61
61
  }
@@ -0,0 +1,215 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.JanitorService = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ /**
9
+ * JanitorService runs a single pass of health checks on SHIP and SLAP outputs.
10
+ * It validates domain names and checks /health endpoints to ensure services are operational.
11
+ *
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).
16
+ */
17
+ class JanitorService {
18
+ constructor(config) {
19
+ var _a, _b, _c;
20
+ this.mongoDb = config.mongoDb;
21
+ 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
23
+ this.hostDownRevokeScore = (_c = config.hostDownRevokeScore) !== null && _c !== void 0 ? _c : 3;
24
+ }
25
+ /**
26
+ * Runs a single pass of health checks on all SHIP and SLAP outputs
27
+ */
28
+ async run() {
29
+ this.logger.log(chalk_1.default.blue('🧹 Running janitor health checks...'));
30
+ 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'));
36
+ }
37
+ catch (error) {
38
+ this.logger.error(chalk_1.default.red('❌ Error during health checks:'), error);
39
+ throw error;
40
+ }
41
+ }
42
+ /**
43
+ * Checks all outputs for a specific topic
44
+ */
45
+ async checkTopicOutputs(collectionName) {
46
+ try {
47
+ const collection = this.mongoDb.collection(collectionName);
48
+ const outputs = await collection.find({}).toArray();
49
+ this.logger.log(chalk_1.default.cyan(`🔍 Checking ${outputs.length} ${collectionName.toUpperCase()} outputs...`));
50
+ for (const output of outputs) {
51
+ await this.checkOutput(output, collection);
52
+ }
53
+ }
54
+ catch (error) {
55
+ this.logger.error(chalk_1.default.red(`❌ Error checking ${collectionName.toUpperCase()} outputs:`), error);
56
+ }
57
+ }
58
+ /**
59
+ * Checks a single output for health
60
+ */
61
+ async checkOutput(output, collection) {
62
+ 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) {
77
+ await this.handleHealthyOutput(output, collection);
78
+ }
79
+ else {
80
+ await this.handleUnhealthyOutput(output, collection);
81
+ }
82
+ }
83
+ 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(() => { });
86
+ }
87
+ }
88
+ /**
89
+ * Extracts URL from output record
90
+ */
91
+ extractURLFromOutput(output) {
92
+ try {
93
+ // SHIP and SLAP outputs typically have a domain field or URL field
94
+ // Check for common field names
95
+ if (typeof output.domain === 'string') {
96
+ return output.domain;
97
+ }
98
+ if (typeof output.url === 'string') {
99
+ return output.url;
100
+ }
101
+ if (typeof output.serviceURL === 'string') {
102
+ return output.serviceURL;
103
+ }
104
+ // SLAP outputs may have a protocols array with URLs
105
+ if (Array.isArray(output.protocols) && output.protocols.length > 0) {
106
+ const httpsProtocol = output.protocols.find((p) => typeof p === 'string' && p.startsWith('https://'));
107
+ if (httpsProtocol !== undefined) {
108
+ return httpsProtocol;
109
+ }
110
+ }
111
+ return null;
112
+ }
113
+ catch {
114
+ return null;
115
+ }
116
+ }
117
+ /**
118
+ * Validates if a string is a valid domain name
119
+ */
120
+ isValidDomain(url) {
121
+ try {
122
+ // Parse the URL
123
+ const parsedURL = new URL(url.startsWith('http') ? url : `https://${url}`);
124
+ // Check if hostname is valid
125
+ const hostname = parsedURL.hostname;
126
+ // Basic domain validation
127
+ 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
+ const localhostRegex = /^localhost$/i;
129
+ const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;
130
+ return domainRegex.test(hostname) || localhostRegex.test(hostname) || ipv4Regex.test(hostname);
131
+ }
132
+ catch {
133
+ return false;
134
+ }
135
+ }
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
+ /**
175
+ * Handles a healthy output by decrementing its down counter
176
+ */
177
+ async handleHealthyOutput(output, collection) {
178
+ try {
179
+ const currentDown = typeof output.down === 'number' ? output.down : 0;
180
+ if (currentDown > 0) {
181
+ // Decrement the down counter
182
+ 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
+ }
185
+ }
186
+ catch (error) {
187
+ this.logger.error(chalk_1.default.red(`❌ Error handling healthy output ${String(output.txid)}:${String(output.outputIndex)}:`), error);
188
+ }
189
+ }
190
+ /**
191
+ * Handles an unhealthy output by incrementing its down counter and deleting if threshold is reached
192
+ */
193
+ async handleUnhealthyOutput(output, collection) {
194
+ try {
195
+ const currentDown = typeof output.down === 'number' ? output.down : 0;
196
+ 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
+ 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})`));
201
+ await collection.deleteOne({ _id: output._id });
202
+ this.logger.log(chalk_1.default.green(`✅ Successfully deleted output ${String(output.txid)}:${String(output.outputIndex)}`));
203
+ }
204
+ else {
205
+ // Increment the down counter
206
+ await collection.updateOne({ _id: output._id }, { $inc: { down: 1 } });
207
+ }
208
+ }
209
+ catch (error) {
210
+ this.logger.error(chalk_1.default.red(`❌ Error handling unhealthy output ${String(output.txid)}:${String(output.outputIndex)}:`), error);
211
+ }
212
+ }
213
+ }
214
+ exports.JanitorService = JanitorService;
215
+ //# sourceMappingURL=JanitorService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JanitorService.js","sourceRoot":"","sources":["../../../src/JanitorService.ts"],"names":[],"mappings":";;;;;;AACA,kDAAyB;AAYzB;;;;;;;;GAQG;AACH,MAAa,cAAc;IAMzB,YAAa,MAAqB;;QAChC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAA;QAC7B,IAAI,CAAC,MAAM,GAAG,MAAA,MAAM,CAAC,MAAM,mCAAI,OAAO,CAAA;QACtC,IAAI,CAAC,gBAAgB,GAAG,MAAA,MAAM,CAAC,gBAAgB,mCAAI,KAAK,CAAA,CAAC,sBAAsB;QAC/E,IAAI,CAAC,mBAAmB,GAAG,MAAA,MAAM,CAAC,mBAAmB,mCAAI,CAAC,CAAA;IAC5D,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAG;QACP,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,eAAK,CAAC,IAAI,CAAC,qCAAqC,CAAC,CAAC,CAAA;QAElE,IAAI,CAAC;YACH,qBAAqB;YACrB,MAAM,IAAI,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAA;YAE3C,qBAAqB;YACrB,MAAM,IAAI,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAA;YAE3C,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,eAAK,CAAC,KAAK,CAAC,oCAAoC,CAAC,CAAC,CAAA;QACpE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,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47,7 @@ const DiscoveryServices = __importStar(require("@bsv/overlay-discovery-services"
47
47
  const chalk_1 = __importDefault(require("chalk"));
48
48
  const util_1 = __importDefault(require("util"));
49
49
  const uuid_1 = require("uuid");
50
+ const JanitorService_js_1 = require("./JanitorService.js");
50
51
  /**
51
52
  * In-memory migration source for Knex migrations.
52
53
  * Allows running migrations defined in code rather than files.
@@ -103,18 +104,18 @@ class OverlayExpress {
103
104
  // Logger (defaults to console)
104
105
  this.logger = console;
105
106
  // Knex (SQL) database
106
- this.knex = undefined;
107
+ this.knex = {};
107
108
  // Knex migrations to run
108
109
  this.migrationsToRun = [];
109
110
  // MongoDB database
110
- this.mongoDb = undefined;
111
+ this.mongoDb = {};
111
112
  // Network ('main' or 'test')
112
113
  this.network = 'main';
113
114
  // If no custom ChainTracker is configured, default is a WhatsOnChain instance
114
115
  // (We keep a property for it, so we can pass it to Engine)
115
116
  this.chainTracker = new sdk_1.WhatsOnChain(this.network);
116
117
  // The Overlay Engine
117
- this.engine = undefined;
118
+ this.engine = {};
118
119
  // Configured Topic Managers
119
120
  this.managers = {};
120
121
  // Configured Lookup Services
@@ -131,9 +132,14 @@ class OverlayExpress {
131
132
  // Additional advanced engine config (these map to Engine constructor parameters).
132
133
  // Default to undefined or default values that are used in the Engine if not specified.
133
134
  this.engineConfig = {};
135
+ // Configuration for the janitor service
136
+ this.janitorConfig = {
137
+ requestTimeoutMs: 10000, // 10 seconds
138
+ hostDownRevokeScore: 3
139
+ };
134
140
  this.app = (0, express_1.default)();
135
141
  this.logger.log(chalk_1.default.green.bold(`${name} constructed 🎉`));
136
- this.adminToken = adminToken || (0, uuid_1.v4)(); // generate random if not provided
142
+ this.adminToken = adminToken !== null && adminToken !== void 0 ? adminToken : (0, uuid_1.v4)(); // generate random if not provided
137
143
  }
138
144
  /**
139
145
  * Returns the current admin token in case you need to programmatically retrieve or display it.
@@ -157,6 +163,19 @@ class OverlayExpress {
157
163
  this.webUIConfig = config;
158
164
  this.logger.log(chalk_1.default.blue('🖥️ Web UI has been configured.'));
159
165
  }
166
+ /**
167
+ * Configures the janitor service parameters
168
+ * @param config - Janitor configuration options
169
+ * - requestTimeoutMs: Timeout for health check requests (default: 10000ms)
170
+ * - hostDownRevokeScore: Number of consecutive failures before deleting output (default: 3)
171
+ */
172
+ configureJanitor(config) {
173
+ this.janitorConfig = {
174
+ ...this.janitorConfig,
175
+ ...config
176
+ };
177
+ this.logger.log(chalk_1.default.blue('🧹 Janitor service has been configured.'));
178
+ }
160
179
  /**
161
180
  * Configures the logger to be used by the server.
162
181
  * @param logger - A logger object (e.g., console)
@@ -303,7 +322,7 @@ class OverlayExpress {
303
322
  * @param autoConfigureShipSlap - Whether to auto-configure SHIP and SLAP services (default: true)
304
323
  */
305
324
  async configureEngine(autoConfigureShipSlap = true) {
306
- var _a, _b, _c, _d, _e;
325
+ var _a, _b, _c, _d, _e, _f, _g;
307
326
  this.ensureKnex();
308
327
  if (autoConfigureShipSlap) {
309
328
  // Auto-configure SHIP and SLAP services
@@ -322,7 +341,7 @@ class OverlayExpress {
322
341
  }
323
342
  else {
324
343
  // If the user provided a syncConfiguration, use that. Otherwise default to an empty object.
325
- syncConfig = this.engineConfig.syncConfiguration || {};
344
+ syncConfig = (_a = this.engineConfig.syncConfiguration) !== null && _a !== void 0 ? _a : {};
326
345
  }
327
346
  // Build the actual Storage
328
347
  const storage = new overlay_1.KnexStorage(this.knex);
@@ -361,7 +380,7 @@ class OverlayExpress {
361
380
  `https://${this.advertisableFQDN}`,
362
381
  // shipTrackers
363
382
  this.network === 'test'
364
- ? ((_a = this.engineConfig.shipTrackers) !== null && _a !== void 0 ? _a : sdk_1.DEFAULT_TESTNET_SLAP_TRACKERS)
383
+ ? ((_b = this.engineConfig.shipTrackers) !== null && _b !== void 0 ? _b : sdk_1.DEFAULT_TESTNET_SLAP_TRACKERS)
365
384
  : this.engineConfig.shipTrackers,
366
385
  // slapTrackers
367
386
  Array.isArray(this.engineConfig.slapTrackers)
@@ -376,15 +395,17 @@ class OverlayExpress {
376
395
  // syncConfiguration
377
396
  syncConfig,
378
397
  // logTime
379
- (_b = this.engineConfig.logTime) !== null && _b !== void 0 ? _b : false,
398
+ (_c = this.engineConfig.logTime) !== null && _c !== void 0 ? _c : false,
380
399
  // logPrefix
381
- (_c = this.engineConfig.logPrefix) !== null && _c !== void 0 ? _c : '[OVERLAY_ENGINE] ',
400
+ (_d = this.engineConfig.logPrefix) !== null && _d !== void 0 ? _d : '[OVERLAY_ENGINE] ',
382
401
  // throwOnBroadcastFailure
383
- (_d = this.engineConfig.throwOnBroadcastFailure) !== null && _d !== void 0 ? _d : false,
402
+ (_e = this.engineConfig.throwOnBroadcastFailure) !== null && _e !== void 0 ? _e : false,
384
403
  // overlayBroadcastFacilitator
385
- (_e = this.engineConfig.overlayBroadcastFacilitator) !== null && _e !== void 0 ? _e : new sdk_1.HTTPSOverlayBroadcastFacilitator(),
404
+ (_f = this.engineConfig.overlayBroadcastFacilitator) !== null && _f !== void 0 ? _f : new sdk_1.HTTPSOverlayBroadcastFacilitator(),
386
405
  // logger
387
- this.logger);
406
+ this.logger,
407
+ // suppressDefaultSyncAdvertisements
408
+ (_g = this.engineConfig.suppressDefaultSyncAdvertisements) !== null && _g !== void 0 ? _g : true);
388
409
  this.logger.log(chalk_1.default.green('🚀 Engine has been configured.'));
389
410
  }
390
411
  /**
@@ -430,12 +451,12 @@ class OverlayExpress {
430
451
  this.app.use((req, res, next) => {
431
452
  const startTime = Date.now();
432
453
  // Log incoming request details
433
- this.logger.log(chalk_1.default.magenta.bold(`📥 Incoming Request: ${req.method} ${req.originalUrl}`));
454
+ this.logger.log(chalk_1.default.magenta.bold(`📥 Incoming Request: ${String(req.method)} ${String(req.originalUrl)}`));
434
455
  // Pretty-print headers
435
- this.logger.log(chalk_1.default.cyan(`Headers:`));
456
+ this.logger.log(chalk_1.default.cyan('Headers:'));
436
457
  this.logger.log(util_1.default.inspect(req.headers, { colors: true, depth: null }));
437
458
  // Handle request body
438
- if (req.body && Object.keys(req.body).length > 0) {
459
+ if (req.body != null && Object.keys(req.body).length > 0) {
439
460
  let bodyContent;
440
461
  let bodyString;
441
462
  if (typeof req.body === 'object') {
@@ -445,13 +466,13 @@ class OverlayExpress {
445
466
  bodyString = req.body.toString('utf8');
446
467
  }
447
468
  else {
448
- bodyString = req.body.toString();
469
+ bodyString = String(req.body);
449
470
  }
450
471
  if (bodyString.length > 280) {
451
- bodyContent = chalk_1.default.yellow(`(Body too long to display, length: ${bodyString.length} characters)`);
472
+ bodyContent = chalk_1.default.yellow(`(Body too long to display, length: ${String(bodyString.length)} characters)`);
452
473
  }
453
474
  else {
454
- bodyContent = chalk_1.default.green(`Request Body:\n${bodyString}`);
475
+ bodyContent = chalk_1.default.green(`Request Body:\n${String(bodyString)}`);
455
476
  }
456
477
  this.logger.log(bodyContent);
457
478
  }
@@ -465,11 +486,11 @@ class OverlayExpress {
465
486
  // Log outgoing response details after the response is finished
466
487
  res.on('finish', () => {
467
488
  const duration = Date.now() - startTime;
468
- this.logger.log(chalk_1.default.magenta.bold(`📤 Outgoing Response: ${req.method} ${req.originalUrl} - Status: ${res.statusCode} - Duration: ${duration}ms`));
469
- this.logger.log(chalk_1.default.cyan(`Response Headers:`));
489
+ this.logger.log(chalk_1.default.magenta.bold(`📤 Outgoing Response: ${String(req.method)} ${String(req.originalUrl)} - Status: ${String(res.statusCode)} - Duration: ${String(duration)}ms`));
490
+ this.logger.log(chalk_1.default.cyan('Response Headers:'));
470
491
  this.logger.log(util_1.default.inspect(res.getHeaders(), { colors: true, depth: null }));
471
492
  // Handle response body
472
- if (responseBody) {
493
+ if (responseBody != null) {
473
494
  let bodyContent;
474
495
  let bodyString;
475
496
  if (typeof responseBody === 'object') {
@@ -478,14 +499,17 @@ class OverlayExpress {
478
499
  else if (Buffer.isBuffer(responseBody)) {
479
500
  bodyString = responseBody.toString('utf8');
480
501
  }
502
+ else if (typeof responseBody === 'string') {
503
+ bodyString = responseBody;
504
+ }
481
505
  else {
482
- bodyString = responseBody.toString();
506
+ bodyString = String(responseBody);
483
507
  }
484
508
  if (bodyString.length > 280) {
485
- bodyContent = chalk_1.default.yellow(`(Response body too long to display, length: ${bodyString.length} characters)`);
509
+ bodyContent = chalk_1.default.yellow(`(Response body too long to display, length: ${String(bodyString.length)} characters)`);
486
510
  }
487
511
  else {
488
- bodyContent = chalk_1.default.green(`Response Body:\n${bodyString}`);
512
+ bodyContent = chalk_1.default.green(`Response Body:\n${String(bodyString)}`);
489
513
  }
490
514
  this.logger.log(bodyContent);
491
515
  }
@@ -512,6 +536,10 @@ class OverlayExpress {
512
536
  res.set('content-type', 'text/html');
513
537
  res.send((0, makeUserInterface_js_1.default)(this.webUIConfig));
514
538
  });
539
+ // Serve a health check endpoint
540
+ this.app.get('/health', (_, res) => {
541
+ res.status(200).json({ status: 'ok' });
542
+ });
515
543
  // List hosted topic managers and lookup services
516
544
  this.app.get('/listTopicManagers', (_, res) => {
517
545
  ;
@@ -629,7 +657,7 @@ class OverlayExpress {
629
657
  const steak = await engine.submit(taggedBEEF, (steak) => {
630
658
  responseSent = true;
631
659
  return res.status(200).json(steak);
632
- });
660
+ }, 'current-tx', offChainValues);
633
661
  if (!responseSent) {
634
662
  res.status(200).json(steak);
635
663
  }
@@ -652,11 +680,52 @@ class OverlayExpress {
652
680
  ;
653
681
  (async () => {
654
682
  try {
655
- const result = await engine.lookup(req.body);
656
- return res.status(200).json(result);
683
+ // Check for aggregation header to determine response format
684
+ const aggregationHeader = req.headers['x-aggregation'];
685
+ const shouldReturnBinary = aggregationHeader === 'yes';
686
+ // Validate request body structure
687
+ const lookupRequest = req.body;
688
+ if (typeof lookupRequest.service !== 'string' || lookupRequest.query === undefined) {
689
+ return res.status(400).json({
690
+ status: 'error',
691
+ message: 'Invalid request: body must contain "service" (string) and "query" fields'
692
+ });
693
+ }
694
+ const result = await engine.lookup(lookupRequest);
695
+ if (!shouldReturnBinary) {
696
+ // Return JSON response (default behavior)
697
+ return res.status(200).json(result);
698
+ }
699
+ const beef = new sdk_1.Beef();
700
+ const outputs = result.outputs;
701
+ // Serialize in the format expected by LookupResolver
702
+ const writer = new sdk_1.Utils.Writer();
703
+ // Write number of outpoints
704
+ writer.writeVarIntNum(outputs.length);
705
+ // Write each outpoint data
706
+ for (const output of outputs) {
707
+ const tx = sdk_1.Transaction.fromBEEF(output.beef);
708
+ // Write txid (32 bytes)
709
+ writer.write(tx.id());
710
+ // Write outputIndex
711
+ writer.writeVarIntNum(output.outputIndex);
712
+ // Write context length and data
713
+ if ((output.context != null) && output.context.length > 0) {
714
+ writer.writeVarIntNum(output.context.length);
715
+ writer.write(output.context);
716
+ }
717
+ else {
718
+ writer.writeVarIntNum(0);
719
+ }
720
+ beef.mergeTransaction(tx);
721
+ }
722
+ // Write the beef data
723
+ writer.write(beef.toBinary());
724
+ res.setHeader('Content-Type', 'application/octet-stream');
725
+ return res.status(200).send(Buffer.from(writer.toArray()));
657
726
  }
658
727
  catch (error) {
659
- console.error(chalk_1.default.red('❌ Error in /lookup:'), error);
728
+ console.error(chalk_1.default.red('Error in /lookup:'), error);
660
729
  return res.status(400).json({
661
730
  status: 'error',
662
731
  message: error instanceof Error ? error.message : 'An unknown error occurred'
@@ -757,7 +826,7 @@ class OverlayExpress {
757
826
  * Middleware for checking the admin bearer token.
758
827
  */
759
828
  const checkAdminAuth = (req, res, next) => {
760
- const authHeader = req.headers['authorization'];
829
+ const authHeader = req.headers.authorization;
761
830
  if (typeof authHeader !== 'string' || !authHeader.startsWith('Bearer ')) {
762
831
  res.status(401).json({ status: 'error', message: 'Unauthorized: Missing Bearer token' });
763
832
  return;
@@ -855,6 +924,37 @@ class OverlayExpress {
855
924
  });
856
925
  });
857
926
  });
927
+ /**
928
+ * Admin route to run the janitor service.
929
+ */
930
+ this.app.post('/admin/janitor', checkAdminAuth, (req, res) => {
931
+ ;
932
+ (async () => {
933
+ try {
934
+ this.ensureMongo();
935
+ const janitor = new JanitorService_js_1.JanitorService({
936
+ mongoDb: this.mongoDb,
937
+ logger: this.logger,
938
+ requestTimeoutMs: this.janitorConfig.requestTimeoutMs,
939
+ hostDownRevokeScore: this.janitorConfig.hostDownRevokeScore
940
+ });
941
+ await janitor.run();
942
+ return res.status(200).json({ status: 'success', message: 'Janitor run completed' });
943
+ }
944
+ catch (error) {
945
+ console.error(chalk_1.default.red('❌ Error in /admin/janitor:'), error);
946
+ return res.status(400).json({
947
+ status: 'error',
948
+ message: error instanceof Error ? error.message : 'An unknown error occurred'
949
+ });
950
+ }
951
+ })().catch(() => {
952
+ res.status(500).json({
953
+ status: 'error',
954
+ message: 'Unexpected error'
955
+ });
956
+ });
957
+ });
858
958
  // Automatically handle migrations
859
959
  const migrationSource = new InMemoryMigrationSource(this.migrationsToRun);
860
960
  const result = await knex.migrate.latest({
@@ -902,7 +1002,7 @@ class OverlayExpress {
902
1002
  this.logger.log(chalk_1.default.yellow(`${this.name} will not sync because GASP has been disabled.`));
903
1003
  }
904
1004
  // Start listening on the configured port
905
- this.app.listen(this.port, async () => {
1005
+ this.app.listen(this.port, () => {
906
1006
  this.logger.log(chalk_1.default.green.bold(`🎧 ${this.name} is ready and listening on local port ${this.port}`));
907
1007
  });
908
1008
  }