@bsv/overlay-express 2.4.2 → 2.4.9

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 (135) hide show
  1. package/LICENSE.txt +57 -27
  2. package/README.md +141 -56
  3. package/dist/cjs/mod.d.ts +11 -0
  4. package/dist/cjs/mod.d.ts.map +1 -0
  5. package/dist/cjs/mod.js.map +1 -1
  6. package/dist/cjs/package.json +26 -13
  7. package/dist/cjs/src/ArcadeProvider.d.ts +37 -0
  8. package/dist/cjs/src/ArcadeProvider.d.ts.map +1 -0
  9. package/dist/cjs/src/ArcadeProvider.js.map +1 -1
  10. package/dist/cjs/src/BanAwareDiscoveryStorage.d.ts +36 -0
  11. package/dist/cjs/src/BanAwareDiscoveryStorage.d.ts.map +1 -0
  12. package/dist/cjs/src/BanAwareDiscoveryStorage.js +5 -4
  13. package/dist/cjs/src/BanAwareDiscoveryStorage.js.map +1 -1
  14. package/dist/cjs/src/BanAwareLookupWrapper.d.ts +31 -0
  15. package/dist/cjs/src/BanAwareLookupWrapper.d.ts.map +1 -0
  16. package/dist/cjs/src/BanAwareLookupWrapper.js +3 -2
  17. package/dist/cjs/src/BanAwareLookupWrapper.js.map +1 -1
  18. package/dist/cjs/src/BanAwareTopicManager.d.ts +28 -0
  19. package/dist/cjs/src/BanAwareTopicManager.d.ts.map +1 -0
  20. package/dist/cjs/src/BanAwareTopicManager.js +3 -2
  21. package/dist/cjs/src/BanAwareTopicManager.js.map +1 -1
  22. package/dist/cjs/src/BanService.d.ts +81 -0
  23. package/dist/cjs/src/BanService.d.ts.map +1 -0
  24. package/dist/cjs/src/BanService.js.map +1 -1
  25. package/dist/cjs/src/ChaintracksProvider.d.ts +29 -0
  26. package/dist/cjs/src/ChaintracksProvider.d.ts.map +1 -0
  27. package/dist/cjs/src/ChaintracksProvider.js +1 -1
  28. package/dist/cjs/src/ChaintracksProvider.js.map +1 -1
  29. package/dist/cjs/src/JanitorService.d.ts +128 -0
  30. package/dist/cjs/src/JanitorService.d.ts.map +1 -0
  31. package/dist/cjs/src/JanitorService.js +108 -37
  32. package/dist/cjs/src/JanitorService.js.map +1 -1
  33. package/dist/cjs/src/OverlayExpress.d.ts +466 -0
  34. package/dist/cjs/src/OverlayExpress.d.ts.map +1 -0
  35. package/dist/cjs/src/OverlayExpress.js +307 -148
  36. package/dist/cjs/src/OverlayExpress.js.map +1 -1
  37. package/dist/cjs/src/OverlayMonitor.d.ts +197 -0
  38. package/dist/cjs/src/OverlayMonitor.d.ts.map +1 -0
  39. package/dist/cjs/src/OverlayMonitor.js +6 -4
  40. package/dist/cjs/src/OverlayMonitor.js.map +1 -1
  41. package/dist/cjs/src/ProviderChainBroadcaster.d.ts +18 -0
  42. package/dist/cjs/src/ProviderChainBroadcaster.d.ts.map +1 -0
  43. package/dist/cjs/src/ProviderChainBroadcaster.js +7 -3
  44. package/dist/cjs/src/ProviderChainBroadcaster.js.map +1 -1
  45. package/dist/cjs/src/ReorgStream.d.ts +74 -0
  46. package/dist/cjs/src/ReorgStream.d.ts.map +1 -0
  47. package/dist/cjs/src/ReorgStream.js +14 -6
  48. package/dist/cjs/src/ReorgStream.js.map +1 -1
  49. package/dist/cjs/src/generalGuide.md.d.ts +3 -0
  50. package/dist/cjs/src/generalGuide.md.d.ts.map +1 -0
  51. package/dist/cjs/src/generalGuide.md.js.map +1 -1
  52. package/dist/cjs/src/makeUserInterface.d.ts +22 -0
  53. package/dist/cjs/src/makeUserInterface.d.ts.map +1 -0
  54. package/dist/cjs/src/makeUserInterface.js +4 -4
  55. package/dist/cjs/src/makeUserInterface.js.map +1 -1
  56. package/dist/cjs/src/security/edgePolicy.d.ts +57 -0
  57. package/dist/cjs/src/security/edgePolicy.d.ts.map +1 -0
  58. package/dist/cjs/src/security/edgePolicy.js +379 -0
  59. package/dist/cjs/src/security/edgePolicy.js.map +1 -0
  60. package/dist/esm/mod.js.map +1 -1
  61. package/dist/esm/src/ArcadeProvider.js.map +1 -1
  62. package/dist/esm/src/BanAwareDiscoveryStorage.js +5 -4
  63. package/dist/esm/src/BanAwareDiscoveryStorage.js.map +1 -1
  64. package/dist/esm/src/BanAwareLookupWrapper.js +3 -2
  65. package/dist/esm/src/BanAwareLookupWrapper.js.map +1 -1
  66. package/dist/esm/src/BanAwareTopicManager.js +3 -2
  67. package/dist/esm/src/BanAwareTopicManager.js.map +1 -1
  68. package/dist/esm/src/ChaintracksProvider.js +1 -1
  69. package/dist/esm/src/ChaintracksProvider.js.map +1 -1
  70. package/dist/esm/src/JanitorService.js +84 -8
  71. package/dist/esm/src/JanitorService.js.map +1 -1
  72. package/dist/esm/src/OverlayExpress.js +290 -134
  73. package/dist/esm/src/OverlayExpress.js.map +1 -1
  74. package/dist/esm/src/ProviderChainBroadcaster.js +7 -3
  75. package/dist/esm/src/ProviderChainBroadcaster.js.map +1 -1
  76. package/dist/esm/src/ReorgStream.js +14 -6
  77. package/dist/esm/src/ReorgStream.js.map +1 -1
  78. package/dist/esm/src/makeUserInterface.js +3 -4
  79. package/dist/esm/src/makeUserInterface.js.map +1 -1
  80. package/dist/esm/src/security/edgePolicy.js +377 -0
  81. package/dist/esm/src/security/edgePolicy.js.map +1 -0
  82. package/dist/types/mod.d.ts +1 -1
  83. package/dist/types/mod.d.ts.map +1 -1
  84. package/dist/types/src/ArcadeProvider.d.ts.map +1 -1
  85. package/dist/types/src/BanAwareDiscoveryStorage.d.ts.map +1 -1
  86. package/dist/types/src/BanAwareLookupWrapper.d.ts.map +1 -1
  87. package/dist/types/src/BanAwareTopicManager.d.ts.map +1 -1
  88. package/dist/types/src/BanService.d.ts.map +1 -1
  89. package/dist/types/src/ChaintracksProvider.d.ts.map +1 -1
  90. package/dist/types/src/JanitorService.d.ts +14 -1
  91. package/dist/types/src/JanitorService.d.ts.map +1 -1
  92. package/dist/types/src/OverlayExpress.d.ts +48 -2
  93. package/dist/types/src/OverlayExpress.d.ts.map +1 -1
  94. package/dist/types/src/OverlayMonitor.d.ts.map +1 -1
  95. package/dist/types/src/ProviderChainBroadcaster.d.ts.map +1 -1
  96. package/dist/types/src/ReorgStream.d.ts.map +1 -1
  97. package/dist/types/src/generalGuide.md.d.ts +1 -1
  98. package/dist/types/src/generalGuide.md.d.ts.map +1 -1
  99. package/dist/types/src/makeUserInterface.d.ts +1 -2
  100. package/dist/types/src/makeUserInterface.d.ts.map +1 -1
  101. package/dist/types/src/security/edgePolicy.d.ts +57 -0
  102. package/dist/types/src/security/edgePolicy.d.ts.map +1 -0
  103. package/package.json +47 -25
  104. package/dist/cjs/tsconfig.cjs.tsbuildinfo +0 -1
  105. package/dist/esm/tsconfig.esm.tsbuildinfo +0 -1
  106. package/dist/types/tsconfig.types.tsbuildinfo +0 -1
  107. package/mod.ts +0 -47
  108. package/src/ArcadeProvider.ts +0 -232
  109. package/src/BanAwareDiscoveryStorage.ts +0 -91
  110. package/src/BanAwareLookupWrapper.ts +0 -95
  111. package/src/BanAwareTopicManager.ts +0 -87
  112. package/src/BanService.ts +0 -154
  113. package/src/ChaintracksProvider.ts +0 -79
  114. package/src/JanitorService.ts +0 -423
  115. package/src/OverlayExpress.ts +0 -2479
  116. package/src/OverlayMonitor.ts +0 -817
  117. package/src/ProviderChainBroadcaster.ts +0 -108
  118. package/src/ReorgStream.ts +0 -221
  119. package/src/__tests__/BanAwareDiscoveryStorage.test.ts +0 -131
  120. package/src/__tests__/BanAwareLookupWrapper.test.ts +0 -255
  121. package/src/__tests__/BanAwareTopicManager.test.ts +0 -132
  122. package/src/__tests__/BanService.test.ts +0 -310
  123. package/src/__tests__/InMemoryMigrationSource.test.ts +0 -11
  124. package/src/__tests__/JanitorService.test.ts +0 -941
  125. package/src/__tests__/OverlayExpress.test.ts +0 -1300
  126. package/src/__tests__/OverlayMonitor.test.ts +0 -255
  127. package/src/__tests__/OverlayProviders.live.test.ts +0 -43
  128. package/src/__tests__/OverlayProviders.test.ts +0 -123
  129. package/src/__tests__/README.md +0 -105
  130. package/src/__tests__/ReorgStream.test.ts +0 -66
  131. package/src/__tests__/makeUserInterface.test.ts +0 -193
  132. package/src/__tests__/setup.ts +0 -36
  133. package/src/auth-express-middleware.d.ts +0 -16
  134. package/src/generalGuide.md.ts +0 -74
  135. package/src/makeUserInterface.ts +0 -1224
@@ -45,8 +45,8 @@ const mongodb_1 = require("mongodb");
45
45
  const makeUserInterface_js_1 = __importDefault(require("./makeUserInterface.js"));
46
46
  const DiscoveryServices = __importStar(require("@bsv/overlay-discovery-services"));
47
47
  const chalk_1 = __importDefault(require("chalk"));
48
- const node_util_1 = __importDefault(require("node:util"));
49
48
  const uuid_1 = require("uuid");
49
+ const node_crypto_1 = require("node:crypto");
50
50
  const JanitorService_js_1 = require("./JanitorService.js");
51
51
  const BanService_js_1 = require("./BanService.js");
52
52
  const BanAwareLookupWrapper_js_1 = require("./BanAwareLookupWrapper.js");
@@ -58,6 +58,7 @@ const auth_express_middleware_1 = require("@bsv/auth-express-middleware");
58
58
  const ArcadeProvider_js_1 = require("./ArcadeProvider.js");
59
59
  const ProviderChainBroadcaster_js_1 = require("./ProviderChainBroadcaster.js");
60
60
  const ChaintracksProvider_js_1 = require("./ChaintracksProvider.js");
61
+ const edgePolicy_js_1 = require("./security/edgePolicy.js");
61
62
  /**
62
63
  * In-memory migration source for Knex migrations.
63
64
  * Allows running migrations defined in code rather than files.
@@ -71,7 +72,7 @@ class InMemoryMigrationSource {
71
72
  * @param loadExtensions - Array of file extensions to filter by (not used here)
72
73
  * @returns Promise resolving to the array of migrations
73
74
  */
74
- async getMigrations(loadExtensions) {
75
+ async getMigrations(_loadExtensions) {
75
76
  return this.migrations;
76
77
  }
77
78
  /**
@@ -91,13 +92,33 @@ class InMemoryMigrationSource {
91
92
  return await Promise.resolve(migration);
92
93
  }
93
94
  }
95
+ class PublicRequestError extends Error {
96
+ constructor(message) {
97
+ super(message);
98
+ this.name = 'PublicRequestError';
99
+ }
100
+ }
101
+ function publicErrorMessage(error, fallback = 'Request could not be processed') {
102
+ return error instanceof PublicRequestError ? error.message : fallback;
103
+ }
104
+ function secretMatches(provided, expected) {
105
+ const providedDigest = (0, node_crypto_1.createHash)('sha256').update(provided, 'utf8').digest();
106
+ const expectedDigest = (0, node_crypto_1.createHash)('sha256').update(expected, 'utf8').digest();
107
+ return (0, node_crypto_1.timingSafeEqual)(providedDigest, expectedDigest);
108
+ }
94
109
  function parseTopicsHeader(header) {
95
110
  const value = header.trim();
96
- const parsed = value.startsWith('[')
97
- ? JSON.parse(value)
98
- : value.split(',').map(topic => topic.trim());
111
+ let parsed;
112
+ try {
113
+ parsed = value.startsWith('[')
114
+ ? JSON.parse(value)
115
+ : value.split(',').map(topic => topic.trim());
116
+ }
117
+ catch {
118
+ throw new PublicRequestError('Invalid x-topics header: expected a comma-separated list or JSON string array');
119
+ }
99
120
  if (!Array.isArray(parsed) || parsed.some(topic => typeof topic !== 'string' || topic.length === 0)) {
100
- throw new TypeError('Invalid x-topics header: expected a comma-separated list or JSON string array');
121
+ throw new PublicRequestError('Invalid x-topics header: expected a comma-separated list or JSON string array');
101
122
  }
102
123
  return parsed;
103
124
  }
@@ -172,7 +193,8 @@ class OverlayExpress {
172
193
  this.janitorConfig = {
173
194
  requestTimeoutMs: 10000, // 10 seconds
174
195
  hostDownRevokeScore: 3,
175
- autoBanOnRemoval: true
196
+ autoBanOnRemoval: true,
197
+ allowPrivateHosts: false
176
198
  };
177
199
  // Health endpoint configuration
178
200
  this.healthConfig = {
@@ -183,6 +205,22 @@ class OverlayExpress {
183
205
  this.healthChecks = [];
184
206
  // Lifecycle marker for readiness/liveness reporting
185
207
  this.isListening = false;
208
+ this.edgePolicyConfig = {
209
+ environmentPrefix: 'OVERLAY',
210
+ jsonBodyLimitBytes: 8 * 1024 * 1024,
211
+ binaryBodyLimitBytes: 64 * 1024 * 1024,
212
+ maxConcurrentRequests: 200,
213
+ http: {
214
+ requestTimeoutMs: 2 * 60 * 1000,
215
+ headersTimeoutMs: 15000,
216
+ keepAliveTimeoutMs: 5000,
217
+ socketTimeoutMs: 2 * 60 * 1000,
218
+ maxRequestsPerSocket: 1000
219
+ },
220
+ securityHeaders: {
221
+ contentSecurityPolicy: "default-src 'none'; script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; img-src 'self' data: https://bsvblockchain.org; connect-src 'self' https:; font-src 'self' https://cdn.jsdelivr.net; base-uri 'none'; form-action 'none'; frame-ancestors 'none'"
222
+ }
223
+ };
186
224
  this.app = (0, express_1.default)();
187
225
  this.logger.log(chalk_1.default.green.bold(`${name} constructed`));
188
226
  this.adminToken = adminToken !== null && adminToken !== void 0 ? adminToken : (0, uuid_1.v4)(); // generate random if not provided
@@ -215,6 +253,7 @@ class OverlayExpress {
215
253
  * - requestTimeoutMs: Timeout for health check requests (default: 10000ms)
216
254
  * - hostDownRevokeScore: Number of consecutive failures before deleting output (default: 3)
217
255
  * - autoBanOnRemoval: Whether to auto-ban domains when removed by janitor (default: true)
256
+ * - allowPrivateHosts: Permit private/HTTP targets for isolated local development (default: false)
218
257
  */
219
258
  configureJanitor(config) {
220
259
  this.janitorConfig = {
@@ -233,6 +272,30 @@ class OverlayExpress {
233
272
  };
234
273
  this.logger.log(chalk_1.default.blue('Health reporting has been configured.'));
235
274
  }
275
+ /**
276
+ * Configures explicit browser origins and bounded request/resource policy.
277
+ * Public cross-origin access is the default; pass allowedOrigins or set
278
+ * OVERLAY_CORS_MODE=allowlist to restrict browser callers.
279
+ */
280
+ configureEdgePolicy(config) {
281
+ const { http, securityHeaders: headerConfig, ...topLevel } = config;
282
+ const definedTopLevel = Object.fromEntries(Object.entries(topLevel).filter(([, value]) => value !== undefined));
283
+ const definedHttp = Object.fromEntries(Object.entries(http !== null && http !== void 0 ? http : {}).filter(([, value]) => value !== undefined));
284
+ const definedHeaders = Object.fromEntries(Object.entries(headerConfig !== null && headerConfig !== void 0 ? headerConfig : {}).filter(([, value]) => value !== undefined));
285
+ this.edgePolicyConfig = {
286
+ ...this.edgePolicyConfig,
287
+ ...definedTopLevel,
288
+ http: {
289
+ ...this.edgePolicyConfig.http,
290
+ ...definedHttp
291
+ },
292
+ securityHeaders: {
293
+ ...this.edgePolicyConfig.securityHeaders,
294
+ ...definedHeaders
295
+ }
296
+ };
297
+ this.logger.log(chalk_1.default.blue('HTTP edge policy has been configured.'));
298
+ }
236
299
  /**
237
300
  * Registers an application-specific health check.
238
301
  */
@@ -256,6 +319,15 @@ class OverlayExpress {
256
319
  this.adminIdentityKey = identityKey;
257
320
  this.logger.log(chalk_1.default.blue('Admin identity key has been configured.'));
258
321
  }
322
+ /**
323
+ * Configures BRC-103 session storage for the administrative mutual-auth
324
+ * middleware. Horizontally scaled services should supply a shared
325
+ * AsyncSessionManager rather than use the default process-local store.
326
+ */
327
+ configureAuthSessionManager(sessionManager) {
328
+ this.authSessionManager = sessionManager;
329
+ this.logger.log(chalk_1.default.blue('BSV authentication session manager has been configured.'));
330
+ }
259
331
  /**
260
332
  * Configures the logger to be used by the server.
261
333
  * @param logger - A logger object (e.g., console)
@@ -767,7 +839,8 @@ class OverlayExpress {
767
839
  requestTimeoutMs: this.janitorConfig.requestTimeoutMs,
768
840
  hostDownRevokeScore: this.janitorConfig.hostDownRevokeScore,
769
841
  banService: this.banService,
770
- autoBanOnRemoval: this.janitorConfig.autoBanOnRemoval
842
+ autoBanOnRemoval: this.janitorConfig.autoBanOnRemoval,
843
+ allowPrivateHosts: this.janitorConfig.allowPrivateHosts
771
844
  });
772
845
  }
773
846
  /** Ban a domain and remove all its SHIP/SLAP records from MongoDB. */
@@ -838,11 +911,12 @@ class OverlayExpress {
838
911
  async runHealthCheck(definition) {
839
912
  var _a, _b;
840
913
  const startedAt = Date.now();
914
+ let timeout;
841
915
  try {
842
916
  const result = ((_a = (await Promise.race([
843
917
  Promise.resolve(definition.handler()),
844
918
  new Promise((_, reject) => {
845
- setTimeout(() => reject(new Error(`Timed out after ${this.healthConfig.timeoutMs}ms`)), this.healthConfig.timeoutMs);
919
+ timeout = setTimeout(() => reject(new Error(`Timed out after ${this.healthConfig.timeoutMs}ms`)), this.healthConfig.timeoutMs);
846
920
  })
847
921
  ]))) !== null && _a !== void 0 ? _a : {});
848
922
  return {
@@ -856,18 +930,28 @@ class OverlayExpress {
856
930
  };
857
931
  }
858
932
  catch (error) {
933
+ this.logger.error({
934
+ operation: 'overlay.health_check',
935
+ check: definition.name,
936
+ error
937
+ });
859
938
  return {
860
939
  name: definition.name,
861
940
  scope: definition.scope,
862
941
  critical: definition.critical,
863
942
  status: 'error',
864
- message: error instanceof Error ? error.message : 'Unknown health-check error',
943
+ message: 'Health check failed',
865
944
  durationMs: Date.now() - startedAt
866
945
  };
867
946
  }
947
+ finally {
948
+ if (timeout !== undefined)
949
+ clearTimeout(timeout);
950
+ }
868
951
  }
869
952
  async collectHealthReport(mode) {
870
- var _a, _b, _c;
953
+ var _a;
954
+ var _b, _c;
871
955
  const definitions = [
872
956
  {
873
957
  name: 'process',
@@ -902,7 +986,8 @@ class OverlayExpress {
902
986
  scope: 'ready',
903
987
  critical: true,
904
988
  handler: async () => {
905
- var _a, _b, _c;
989
+ var _a, _b;
990
+ var _c;
906
991
  if (this.knex === undefined) {
907
992
  throw new TypeError('Knex is not configured');
908
993
  }
@@ -936,8 +1021,8 @@ class OverlayExpress {
936
1021
  for (const check of this.healthChecks) {
937
1022
  definitions.push({
938
1023
  name: check.name,
939
- scope: (_a = check.scope) !== null && _a !== void 0 ? _a : 'ready',
940
- critical: (_b = check.critical) !== null && _b !== void 0 ? _b : false,
1024
+ scope: (_b = check.scope) !== null && _b !== void 0 ? _b : 'ready',
1025
+ critical: (_c = check.critical) !== null && _c !== void 0 ? _c : false,
941
1026
  handler: check.handler
942
1027
  });
943
1028
  }
@@ -971,14 +1056,18 @@ class OverlayExpress {
971
1056
  advertisableFQDN: this.advertisableFQDN,
972
1057
  port: this.port,
973
1058
  network: this.network,
974
- startedAt: (_c = this.startTime) === null || _c === void 0 ? void 0 : _c.toISOString(),
1059
+ startedAt: (_a = this.startTime) === null || _a === void 0 ? void 0 : _a.toISOString(),
975
1060
  uptimeMs: this.startTime === undefined ? 0 : Date.now() - this.startTime.getTime(),
976
1061
  topicManagerCount: Object.keys(this.managers).length,
977
1062
  lookupServiceCount: Object.keys(this.services).length
978
1063
  },
979
1064
  checks: this.healthConfig.includeDetails
980
1065
  ? checks
981
- : checks.map(({ details, ...check }) => check),
1066
+ : checks.map(check => {
1067
+ const publicCheck = { ...check };
1068
+ delete publicCheck.details;
1069
+ return publicCheck;
1070
+ }),
982
1071
  context
983
1072
  };
984
1073
  return report;
@@ -986,20 +1075,83 @@ class OverlayExpress {
986
1075
  /**
987
1076
  * Renders a request or response body for verbose logging, truncating overly long payloads.
988
1077
  */
989
- formatBodyForLog(body, tooLongLabel, okPrefix) {
990
- let bodyString;
991
- if (typeof body === 'object') {
992
- bodyString = JSON.stringify(body, null, 2);
1078
+ formatBodyForLog(body, okPrefix) {
1079
+ if (Buffer.isBuffer(body)) {
1080
+ return chalk_1.default.green(`${okPrefix} binary body (${(0, overlay_1.serializeLogValue)(body.byteLength)} bytes)`);
993
1081
  }
994
- else if (Buffer.isBuffer(body)) {
995
- bodyString = body.toString('utf8');
1082
+ if (typeof body === 'string') {
1083
+ return chalk_1.default.green(`${okPrefix} string body (${(0, overlay_1.serializeLogValue)(Buffer.byteLength(body, 'utf8'))} bytes)`);
996
1084
  }
997
- else {
998
- bodyString = String(body);
1085
+ if (body != null && typeof body === 'object') {
1086
+ const keys = Array.isArray(body) ? body.length : Object.keys(body).length;
1087
+ return chalk_1.default.green(`${okPrefix} structured body (${(0, overlay_1.serializeLogValue)(keys)} top-level item(s))`);
1088
+ }
1089
+ return chalk_1.default.green(`${okPrefix} type=${(0, overlay_1.serializeLogValue)(typeof body)}`);
1090
+ }
1091
+ redactHeadersForLog(headers) {
1092
+ const sensitiveHeader = /authorization|cookie|token|secret|payment|signature|nonce/i;
1093
+ return Object.fromEntries(Object.entries(headers).map(([name, value]) => [
1094
+ name,
1095
+ sensitiveHeader.test(name) ? '[REDACTED]' : value
1096
+ ]));
1097
+ }
1098
+ arcCallbackRequestToken(req) {
1099
+ const authorization = req.headers.authorization;
1100
+ const headerToken = Array.isArray(authorization) ? authorization[0] : authorization;
1101
+ if (typeof headerToken === 'string' && headerToken.startsWith('Bearer ')) {
1102
+ return headerToken.slice('Bearer '.length);
1103
+ }
1104
+ return headerToken;
1105
+ }
1106
+ arcCallbackAuthorized(req) {
1107
+ if (typeof this.arcCallbackToken !== 'string' || this.arcCallbackToken.length === 0) {
1108
+ return true;
1109
+ }
1110
+ const callbackHeader = req.headers['x-callback-token'];
1111
+ const callbackToken = Array.isArray(callbackHeader) ? callbackHeader[0] : callbackHeader;
1112
+ return (this.arcCallbackRequestToken(req) === this.arcCallbackToken ||
1113
+ callbackToken === this.arcCallbackToken);
1114
+ }
1115
+ async processArcIngest(engine, req, res) {
1116
+ if (!this.arcCallbackAuthorized(req)) {
1117
+ return res.status(401).json({ status: 'error', message: 'Unauthorized callback' });
1118
+ }
1119
+ const { txid, merklePath: merklePathHex, blockHeight, txStatus, extraInfo, competingTxs, topic } = req.body;
1120
+ if (typeof txid !== 'string' || txid === '') {
1121
+ throw new PublicRequestError('Provider callback is missing txid');
1122
+ }
1123
+ if ((0, ArcadeProvider_js_1.isTerminalArcStatus)(txStatus, extraInfo)) {
1124
+ const report = await engine.evictAppliedTransaction(txid, {
1125
+ topic: typeof topic === 'string' ? topic : undefined,
1126
+ reason: `${txStatus !== null && txStatus !== void 0 ? txStatus : ''} ${extraInfo !== null && extraInfo !== void 0 ? extraInfo : ''}`.trim()
1127
+ });
1128
+ this.logger.warn({
1129
+ operation: 'overlay.provider_callback',
1130
+ outcome: 'terminal_evicted',
1131
+ txid,
1132
+ txStatus,
1133
+ competingTxs,
1134
+ report
1135
+ });
1136
+ return res.status(200).json({
1137
+ status: 'success',
1138
+ message: 'Terminal transaction status processed',
1139
+ data: { ...report, txStatus, competingTxs }
1140
+ });
1141
+ }
1142
+ if (typeof merklePathHex !== 'string' || merklePathHex === '') {
1143
+ return res
1144
+ .status(202)
1145
+ .json({ status: 'success', message: 'Transaction status received without proof' });
999
1146
  }
1000
- return bodyString.length > 280
1001
- ? chalk_1.default.yellow(`(${tooLongLabel}, length: ${String(bodyString.length)} characters)`)
1002
- : chalk_1.default.green(`${okPrefix}\n${bodyString}`);
1147
+ await engine.handleNewMerkleProof(txid, sdk_1.MerklePath.fromHex(merklePathHex), blockHeight);
1148
+ this.logger.log({
1149
+ operation: 'overlay.provider_callback',
1150
+ outcome: 'proof_ingested',
1151
+ txid,
1152
+ blockHeight
1153
+ });
1154
+ return res.status(200).json({ status: 'success', message: 'Transaction status updated' });
1003
1155
  }
1004
1156
  /**
1005
1157
  * Installs middleware that verbosely logs incoming requests and outgoing responses.
@@ -1008,13 +1160,11 @@ class OverlayExpress {
1008
1160
  this.app.use((req, res, next) => {
1009
1161
  const startTime = Date.now();
1010
1162
  // Log incoming request details
1011
- this.logger.log(chalk_1.default.magenta.bold(`Incoming Request: ${String(req.method)} ${String(req.originalUrl)}`));
1012
- // Pretty-print headers
1013
- this.logger.log(chalk_1.default.cyan('Headers:'));
1014
- this.logger.log(node_util_1.default.inspect(req.headers, { colors: true, depth: null }));
1163
+ this.logger.log(chalk_1.default.magenta.bold(`Incoming Request: method=${(0, overlay_1.serializeLogValue)(req.method)} url=${(0, overlay_1.serializeLogValue)(req.originalUrl)}`));
1164
+ this.logger.log(chalk_1.default.cyan(`Headers: ${(0, overlay_1.serializeLogValue)(this.redactHeadersForLog(req.headers))}`));
1015
1165
  // Handle request body
1016
1166
  if (req.body != null && Object.keys(req.body).length > 0) {
1017
- this.logger.log(this.formatBodyForLog(req.body, 'Body too long to display', 'Request Body:'));
1167
+ this.logger.log(this.formatBodyForLog(req.body, 'Request Body:'));
1018
1168
  }
1019
1169
  // Intercept the res.send method to log responses
1020
1170
  const originalSend = res.send;
@@ -1026,12 +1176,11 @@ class OverlayExpress {
1026
1176
  // Log outgoing response details after the response is finished
1027
1177
  res.on('finish', () => {
1028
1178
  const duration = Date.now() - startTime;
1029
- this.logger.log(chalk_1.default.magenta.bold(`Outgoing Response: ${String(req.method)} ${String(req.originalUrl)} - Status: ${String(res.statusCode)} - Duration: ${String(duration)}ms`));
1030
- this.logger.log(chalk_1.default.cyan('Response Headers:'));
1031
- this.logger.log(node_util_1.default.inspect(res.getHeaders(), { colors: true, depth: null }));
1179
+ this.logger.log(chalk_1.default.magenta.bold(`Outgoing Response: method=${(0, overlay_1.serializeLogValue)(req.method)} url=${(0, overlay_1.serializeLogValue)(req.originalUrl)} status=${(0, overlay_1.serializeLogValue)(res.statusCode)} durationMs=${(0, overlay_1.serializeLogValue)(duration)}`));
1180
+ this.logger.log(chalk_1.default.cyan(`Response Headers: ${(0, overlay_1.serializeLogValue)(this.redactHeadersForLog(res.getHeaders()))}`));
1032
1181
  // Handle response body
1033
1182
  if (responseBody != null) {
1034
- this.logger.log(this.formatBodyForLog(responseBody, 'Response body too long to display', 'Response Body:'));
1183
+ this.logger.log(this.formatBodyForLog(responseBody, 'Response Body:'));
1035
1184
  }
1036
1185
  });
1037
1186
  next();
@@ -1045,25 +1194,30 @@ class OverlayExpress {
1045
1194
  const engine = this.ensureEngine();
1046
1195
  const knex = this.ensureKnex();
1047
1196
  this.startTime = new Date();
1048
- this.app.use(body_parser_1.default.json({ limit: '1gb', type: 'application/json' }));
1049
- this.app.use(body_parser_1.default.raw({ limit: '1gb', type: 'application/octet-stream' }));
1197
+ const edgePolicy = this.edgePolicyConfig;
1198
+ this.app.disable('x-powered-by');
1199
+ this.app.use((0, edgePolicy_js_1.securityHeaders)({
1200
+ environmentPrefix: edgePolicy.environmentPrefix,
1201
+ ...edgePolicy.securityHeaders
1202
+ }));
1203
+ this.app.use((0, edgePolicy_js_1.corsPolicy)({
1204
+ environmentPrefix: edgePolicy.environmentPrefix,
1205
+ allowedOrigins: edgePolicy.allowedOrigins,
1206
+ methods: ['GET', 'POST', 'OPTIONS']
1207
+ }));
1208
+ this.app.use((0, edgePolicy_js_1.concurrencyLimit)(edgePolicy.environmentPrefix, edgePolicy.maxConcurrentRequests));
1209
+ this.app.use(body_parser_1.default.json({
1210
+ limit: (0, edgePolicy_js_1.readBodyLimitBytes)(`${edgePolicy.environmentPrefix}_JSON`, edgePolicy.jsonBodyLimitBytes),
1211
+ type: 'application/json'
1212
+ }));
1213
+ this.app.use(body_parser_1.default.raw({
1214
+ limit: (0, edgePolicy_js_1.readBodyLimitBytes)(`${edgePolicy.environmentPrefix}_BINARY`, edgePolicy.binaryBodyLimitBytes),
1215
+ type: 'application/octet-stream'
1216
+ }));
1217
+ this.app.use(edgePolicy_js_1.bodyParserErrorHandler);
1050
1218
  if (this.verboseRequestLogging) {
1051
1219
  this.setupVerboseRequestLogging();
1052
1220
  }
1053
- // Enable CORS
1054
- this.app.use((req, res, next) => {
1055
- res.header('Access-Control-Allow-Origin', '*');
1056
- res.header('Access-Control-Allow-Headers', '*');
1057
- res.header('Access-Control-Allow-Methods', '*');
1058
- res.header('Access-Control-Expose-Headers', '*');
1059
- res.header('Access-Control-Allow-Private-Network', 'true');
1060
- if (req.method === 'OPTIONS') {
1061
- res.sendStatus(200);
1062
- }
1063
- else {
1064
- next();
1065
- }
1066
- });
1067
1221
  // Serve a static documentation site or user interface
1068
1222
  this.app.get('/', (req, res) => {
1069
1223
  res.set('content-type', 'text/html');
@@ -1079,9 +1233,10 @@ class OverlayExpress {
1079
1233
  const report = await this.collectHealthReport('live');
1080
1234
  return res.status(report.live ? 200 : 503).json(report);
1081
1235
  })().catch((error) => {
1236
+ this.logger.error({ operation: 'overlay.health_live', error });
1082
1237
  res.status(500).json({
1083
1238
  status: 'error',
1084
- message: error instanceof Error ? error.message : 'Unexpected error'
1239
+ message: 'Health report unavailable'
1085
1240
  });
1086
1241
  });
1087
1242
  });
@@ -1091,9 +1246,10 @@ class OverlayExpress {
1091
1246
  const report = await this.collectHealthReport('ready');
1092
1247
  return res.status(report.ready ? 200 : 503).json(report);
1093
1248
  })().catch((error) => {
1249
+ this.logger.error({ operation: 'overlay.health_ready', error });
1094
1250
  res.status(500).json({
1095
1251
  status: 'error',
1096
- message: error instanceof Error ? error.message : 'Unexpected error'
1252
+ message: 'Health report unavailable'
1097
1253
  });
1098
1254
  });
1099
1255
  });
@@ -1103,9 +1259,10 @@ class OverlayExpress {
1103
1259
  const report = await this.collectHealthReport('full');
1104
1260
  return res.status(report.ready ? 200 : 503).json(report);
1105
1261
  })().catch((error) => {
1262
+ this.logger.error({ operation: 'overlay.health_full', error });
1106
1263
  res.status(500).json({
1107
1264
  status: 'error',
1108
- message: error instanceof Error ? error.message : 'Unexpected error'
1265
+ message: 'Health report unavailable'
1109
1266
  });
1110
1267
  });
1111
1268
  });
@@ -1120,7 +1277,7 @@ class OverlayExpress {
1120
1277
  catch (error) {
1121
1278
  return res.status(400).json({
1122
1279
  status: 'error',
1123
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1280
+ message: publicErrorMessage(error)
1124
1281
  });
1125
1282
  }
1126
1283
  })().catch(() => {
@@ -1140,7 +1297,7 @@ class OverlayExpress {
1140
1297
  catch (error) {
1141
1298
  return res.status(400).json({
1142
1299
  status: 'error',
1143
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1300
+ message: publicErrorMessage(error)
1144
1301
  });
1145
1302
  }
1146
1303
  })().catch(() => {
@@ -1163,7 +1320,7 @@ class OverlayExpress {
1163
1320
  catch (error) {
1164
1321
  return res.status(400).json({
1165
1322
  status: 'error',
1166
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1323
+ message: publicErrorMessage(error)
1167
1324
  });
1168
1325
  }
1169
1326
  })().catch(() => {
@@ -1185,7 +1342,7 @@ class OverlayExpress {
1185
1342
  catch (error) {
1186
1343
  return res.status(400).json({
1187
1344
  status: 'error',
1188
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1345
+ message: publicErrorMessage(error)
1189
1346
  });
1190
1347
  }
1191
1348
  })().catch(() => {
@@ -1204,12 +1361,12 @@ class OverlayExpress {
1204
1361
  const topicsHeader = req.headers['x-topics'];
1205
1362
  const includesOffChain = req.headers['x-includes-off-chain-values'] === 'true';
1206
1363
  if (typeof topicsHeader !== 'string') {
1207
- throw new TypeError('Missing x-topics header');
1364
+ throw new PublicRequestError('Missing x-topics header');
1208
1365
  }
1209
1366
  const topics = parseTopicsHeader(topicsHeader);
1210
1367
  const body = req.body;
1211
1368
  if (body == null || typeof body[Symbol.iterator] !== 'function' || body.length === 0) {
1212
- throw new TypeError('Missing or empty BEEF body');
1369
+ throw new PublicRequestError('Missing or empty BEEF body');
1213
1370
  }
1214
1371
  let offChainValues;
1215
1372
  let beef = Array.from(body);
@@ -1235,10 +1392,10 @@ class OverlayExpress {
1235
1392
  }
1236
1393
  }
1237
1394
  catch (error) {
1238
- console.error(chalk_1.default.red('Error in /submit:'), error);
1395
+ this.logger.error(chalk_1.default.red(`Error in /submit: error=${(0, overlay_1.serializeErrorForLog)(error)}`));
1239
1396
  return res.status(400).json({
1240
1397
  status: 'error',
1241
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1398
+ message: publicErrorMessage(error)
1242
1399
  });
1243
1400
  }
1244
1401
  })().catch(() => {
@@ -1297,10 +1454,10 @@ class OverlayExpress {
1297
1454
  return res.status(200).send(Buffer.from(writer.toArray()));
1298
1455
  }
1299
1456
  catch (error) {
1300
- console.error(chalk_1.default.red('Error in /lookup:'), error);
1457
+ this.logger.error(chalk_1.default.red(`Error in /lookup: error=${(0, overlay_1.serializeErrorForLog)(error)}`));
1301
1458
  return res.status(400).json({
1302
1459
  status: 'error',
1303
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1460
+ message: publicErrorMessage(error)
1304
1461
  });
1305
1462
  }
1306
1463
  })().catch(() => {
@@ -1317,63 +1474,13 @@ class OverlayExpress {
1317
1474
  ;
1318
1475
  (async () => {
1319
1476
  try {
1320
- if (typeof this.arcCallbackToken === 'string' && this.arcCallbackToken.length > 0) {
1321
- const authorization = req.headers.authorization;
1322
- const headerToken = Array.isArray(authorization) ? authorization[0] : authorization;
1323
- const xCallbackToken = req.headers['x-callback-token'];
1324
- const callbackToken = Array.isArray(xCallbackToken) ? xCallbackToken[0] : xCallbackToken;
1325
- const bearerToken = typeof headerToken === 'string' && headerToken.startsWith('Bearer ')
1326
- ? headerToken.slice('Bearer '.length)
1327
- : headerToken;
1328
- if (bearerToken !== this.arcCallbackToken && callbackToken !== this.arcCallbackToken) {
1329
- return res.status(401).json({ status: 'error', message: 'Unauthorized callback' });
1330
- }
1331
- }
1332
- const { txid, merklePath: merklePathHex, blockHeight, txStatus, extraInfo, competingTxs, topic } = req.body;
1333
- if (typeof txid !== 'string' || txid === '') {
1334
- throw new TypeError('Provider callback is missing txid');
1335
- }
1336
- if ((0, ArcadeProvider_js_1.isTerminalArcStatus)(txStatus, extraInfo)) {
1337
- const report = await engine.evictAppliedTransaction(txid, {
1338
- topic: typeof topic === 'string' ? topic : undefined,
1339
- reason: `${txStatus !== null && txStatus !== void 0 ? txStatus : ''} ${extraInfo !== null && extraInfo !== void 0 ? extraInfo : ''}`.trim()
1340
- });
1341
- this.logger.warn({
1342
- operation: 'overlay.provider_callback',
1343
- outcome: 'terminal_evicted',
1344
- txid,
1345
- txStatus,
1346
- competingTxs,
1347
- report
1348
- });
1349
- return res.status(200).json({
1350
- status: 'success',
1351
- message: 'Terminal transaction status processed',
1352
- data: {
1353
- ...report,
1354
- txStatus,
1355
- competingTxs
1356
- }
1357
- });
1358
- }
1359
- if (typeof merklePathHex !== 'string' || merklePathHex === '') {
1360
- return res.status(202).json({ status: 'success', message: 'Transaction status received without proof' });
1361
- }
1362
- const merklePath = sdk_1.MerklePath.fromHex(merklePathHex);
1363
- await engine.handleNewMerkleProof(txid, merklePath, blockHeight);
1364
- this.logger.log({
1365
- operation: 'overlay.provider_callback',
1366
- outcome: 'proof_ingested',
1367
- txid,
1368
- blockHeight
1369
- });
1370
- return res.status(200).json({ status: 'success', message: 'Transaction status updated' });
1477
+ return await this.processArcIngest(engine, req, res);
1371
1478
  }
1372
1479
  catch (error) {
1373
- console.error(chalk_1.default.red('Error in /arc-ingest:'), error);
1480
+ this.logger.error(chalk_1.default.red(`Error in /arc-ingest: error=${(0, overlay_1.serializeErrorForLog)(error)}`));
1374
1481
  return res.status(400).json({
1375
1482
  status: 'error',
1376
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1483
+ message: publicErrorMessage(error)
1377
1484
  });
1378
1485
  }
1379
1486
  })().catch(() => {
@@ -1401,7 +1508,7 @@ class OverlayExpress {
1401
1508
  console.error(chalk_1.default.red('Error in /requestSyncResponse:'), error);
1402
1509
  return res.status(400).json({
1403
1510
  status: 'error',
1404
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1511
+ message: publicErrorMessage(error)
1405
1512
  });
1406
1513
  }
1407
1514
  })().catch(() => {
@@ -1423,7 +1530,7 @@ class OverlayExpress {
1423
1530
  console.error(chalk_1.default.red('Error in /requestForeignGASPNode:'), error);
1424
1531
  return res.status(400).json({
1425
1532
  status: 'error',
1426
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1533
+ message: publicErrorMessage(error)
1427
1534
  });
1428
1535
  }
1429
1536
  })().catch(() => {
@@ -1442,7 +1549,7 @@ class OverlayExpress {
1442
1549
  const readBasmTopic = (req) => {
1443
1550
  const header = req.headers['x-bsv-topic'];
1444
1551
  if (typeof header !== 'string' || header.length === 0) {
1445
- throw new TypeError('Missing x-bsv-topic header');
1552
+ throw new PublicRequestError('Missing x-bsv-topic header');
1446
1553
  }
1447
1554
  return header;
1448
1555
  };
@@ -1464,7 +1571,7 @@ class OverlayExpress {
1464
1571
  console.error(chalk_1.default.red(`Error in ${path}:`), error);
1465
1572
  return res.status(400).json({
1466
1573
  status: 'error',
1467
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1574
+ message: publicErrorMessage(error)
1468
1575
  });
1469
1576
  }
1470
1577
  })().catch(() => {
@@ -1474,7 +1581,7 @@ class OverlayExpress {
1474
1581
  };
1475
1582
  const requireTxids = (value) => {
1476
1583
  if (!Array.isArray(value) || !value.every(txid => typeof txid === 'string')) {
1477
- throw new TypeError('txids must be an array of strings');
1584
+ throw new PublicRequestError('txids must be an array of strings');
1478
1585
  }
1479
1586
  return value;
1480
1587
  };
@@ -1507,6 +1614,7 @@ class OverlayExpress {
1507
1614
  if (this.serverWallet !== undefined) {
1508
1615
  const bsvAuth = (0, auth_express_middleware_1.createAuthMiddleware)({
1509
1616
  wallet: this.serverWallet,
1617
+ sessionManager: this.authSessionManager,
1510
1618
  allowUnauthenticated: true
1511
1619
  });
1512
1620
  this.app.use(bsvAuth);
@@ -1533,7 +1641,7 @@ class OverlayExpress {
1533
1641
  const authHeader = req.headers.authorization;
1534
1642
  if (typeof authHeader === 'string' && authHeader.startsWith('Bearer ')) {
1535
1643
  const token = authHeader.substring('Bearer '.length);
1536
- if (token === this.adminToken) {
1644
+ if (secretMatches(token, this.adminToken)) {
1537
1645
  next();
1538
1646
  return;
1539
1647
  }
@@ -1561,13 +1669,14 @@ class OverlayExpress {
1561
1669
  this.app.get('/admin/stats', checkAdminAuth, (req, res) => {
1562
1670
  ;
1563
1671
  (async () => {
1564
- var _a, _b, _c;
1672
+ var _a, _b;
1673
+ var _c;
1565
1674
  try {
1566
1675
  const db = this.ensureMongo();
1567
1676
  const [shipCount, slapCount, banStats] = await Promise.all([
1568
1677
  db.collection('shipRecords').countDocuments(),
1569
1678
  db.collection('slapRecords').countDocuments(),
1570
- (_b = (_a = this.banService) === null || _a === void 0 ? void 0 : _a.getStats()) !== null && _b !== void 0 ? _b : { domainBans: 0, outpointBans: 0, totalBans: 0 }
1679
+ (_c = (_a = this.banService) === null || _a === void 0 ? void 0 : _a.getStats()) !== null && _c !== void 0 ? _c : { domainBans: 0, outpointBans: 0, totalBans: 0 }
1571
1680
  ]);
1572
1681
  return res.status(200).json({
1573
1682
  status: 'success',
@@ -1575,7 +1684,7 @@ class OverlayExpress {
1575
1684
  nodeName: this.name,
1576
1685
  network: this.network,
1577
1686
  uptime: this.startTime != null ? Date.now() - this.startTime.getTime() : 0,
1578
- startedAt: (_c = this.startTime) === null || _c === void 0 ? void 0 : _c.toISOString(),
1687
+ startedAt: (_b = this.startTime) === null || _b === void 0 ? void 0 : _b.toISOString(),
1579
1688
  shipRecordCount: shipCount,
1580
1689
  slapRecordCount: slapCount,
1581
1690
  bannedDomains: banStats.domainBans,
@@ -1592,7 +1701,7 @@ class OverlayExpress {
1592
1701
  catch (error) {
1593
1702
  return res.status(400).json({
1594
1703
  status: 'error',
1595
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1704
+ message: publicErrorMessage(error)
1596
1705
  });
1597
1706
  }
1598
1707
  })().catch(() => {
@@ -1635,7 +1744,7 @@ class OverlayExpress {
1635
1744
  catch (error) {
1636
1745
  return res.status(400).json({
1637
1746
  status: 'error',
1638
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1747
+ message: publicErrorMessage(error)
1639
1748
  });
1640
1749
  }
1641
1750
  })().catch(() => {
@@ -1678,7 +1787,7 @@ class OverlayExpress {
1678
1787
  catch (error) {
1679
1788
  return res.status(400).json({
1680
1789
  status: 'error',
1681
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1790
+ message: publicErrorMessage(error)
1682
1791
  });
1683
1792
  }
1684
1793
  })().catch(() => {
@@ -1704,7 +1813,7 @@ class OverlayExpress {
1704
1813
  catch (error) {
1705
1814
  return res.status(400).json({
1706
1815
  status: 'error',
1707
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1816
+ message: publicErrorMessage(error)
1708
1817
  });
1709
1818
  }
1710
1819
  })().catch(() => {
@@ -1736,7 +1845,7 @@ class OverlayExpress {
1736
1845
  catch (error) {
1737
1846
  return res.status(400).json({
1738
1847
  status: 'error',
1739
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1848
+ message: publicErrorMessage(error)
1740
1849
  });
1741
1850
  }
1742
1851
  })().catch(() => {
@@ -1766,7 +1875,7 @@ class OverlayExpress {
1766
1875
  catch (error) {
1767
1876
  return res.status(400).json({
1768
1877
  status: 'error',
1769
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1878
+ message: publicErrorMessage(error)
1770
1879
  });
1771
1880
  }
1772
1881
  })().catch(() => {
@@ -1791,7 +1900,7 @@ class OverlayExpress {
1791
1900
  catch (error) {
1792
1901
  return res.status(400).json({
1793
1902
  status: 'error',
1794
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1903
+ message: publicErrorMessage(error)
1795
1904
  });
1796
1905
  }
1797
1906
  })().catch(() => {
@@ -1833,7 +1942,7 @@ class OverlayExpress {
1833
1942
  catch (error) {
1834
1943
  return res.status(400).json({
1835
1944
  status: 'error',
1836
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1945
+ message: publicErrorMessage(error)
1837
1946
  });
1838
1947
  }
1839
1948
  })().catch(() => {
@@ -1854,7 +1963,7 @@ class OverlayExpress {
1854
1963
  console.error(chalk_1.default.red('Error in /admin/syncAdvertisements:'), error);
1855
1964
  return res.status(400).json({
1856
1965
  status: 'error',
1857
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1966
+ message: publicErrorMessage(error)
1858
1967
  });
1859
1968
  }
1860
1969
  })().catch(() => {
@@ -1878,7 +1987,7 @@ class OverlayExpress {
1878
1987
  console.error(chalk_1.default.red('Error in /admin/startGASPSync:'), error);
1879
1988
  return res.status(400).json({
1880
1989
  status: 'error',
1881
- message: error instanceof Error ? error.message : 'An unknown error occurred'
1990
+ message: publicErrorMessage(error)
1882
1991
  });
1883
1992
  }
1884
1993
  })().catch(() => {
@@ -1965,7 +2074,7 @@ class OverlayExpress {
1965
2074
  console.error(chalk_1.default.red('Error in /admin/evictOutpoint:'), error);
1966
2075
  return res.status(400).json({
1967
2076
  status: 'error',
1968
- message: error instanceof Error ? error.message : 'An unknown error occurred'
2077
+ message: publicErrorMessage(error)
1969
2078
  });
1970
2079
  }
1971
2080
  })().catch(() => {
@@ -1990,7 +2099,7 @@ class OverlayExpress {
1990
2099
  console.error(chalk_1.default.red('Error in /admin/janitor:'), error);
1991
2100
  return res.status(400).json({
1992
2101
  status: 'error',
1993
- message: error instanceof Error ? error.message : 'An unknown error occurred'
2102
+ message: publicErrorMessage(error)
1994
2103
  });
1995
2104
  }
1996
2105
  })().catch(() => {
@@ -2008,7 +2117,7 @@ class OverlayExpress {
2008
2117
  this.logger.log(chalk_1.default.green('Knex migrations run'), result);
2009
2118
  // 404 handler for all other routes
2010
2119
  this.app.use((req, res) => {
2011
- this.logger.log(chalk_1.default.red('404 Not Found:'), req.url);
2120
+ this.logger.log(chalk_1.default.red(`404 Not Found: url=${(0, overlay_1.serializeLogValue)(req.url)}`));
2012
2121
  res.status(404).json({
2013
2122
  status: 'error',
2014
2123
  code: 'ERR_ROUTE_NOT_FOUND',
@@ -2017,10 +2126,57 @@ class OverlayExpress {
2017
2126
  });
2018
2127
  await this.runStartupSync();
2019
2128
  // Start listening on the configured port
2020
- this.app.listen(this.port, () => {
2129
+ this.server = this.app.listen(this.port, () => {
2021
2130
  this.isListening = true;
2022
2131
  this.logger.log(chalk_1.default.green.bold(`${this.name} is ready and listening on local port ${this.port}`));
2023
2132
  });
2133
+ (0, edgePolicy_js_1.configureHttpServer)(this.server, edgePolicy.environmentPrefix, edgePolicy.http);
2134
+ }
2135
+ /**
2136
+ * Stops new HTTP work, background synchronization, and database clients.
2137
+ *
2138
+ * The operation is idempotent so multiple signal handlers or embedding
2139
+ * runtimes can share shutdown ownership safely.
2140
+ */
2141
+ async close() {
2142
+ var _a;
2143
+ (_a = this.closePromise) !== null && _a !== void 0 ? _a : (this.closePromise = this.closeResources());
2144
+ await this.closePromise;
2145
+ }
2146
+ async closeResources() {
2147
+ var _a, _b, _c;
2148
+ var _d, _e;
2149
+ this.isListening = false;
2150
+ if (this.basmBlockPollTimer !== undefined) {
2151
+ clearInterval(this.basmBlockPollTimer);
2152
+ this.basmBlockPollTimer = undefined;
2153
+ }
2154
+ if (this.unprovenMaintenanceTimer !== undefined) {
2155
+ clearInterval(this.unprovenMaintenanceTimer);
2156
+ this.unprovenMaintenanceTimer = undefined;
2157
+ }
2158
+ (_a = this.reorgAdapter) === null || _a === void 0 ? void 0 : _a.stop();
2159
+ this.reorgAdapter = undefined;
2160
+ const server = this.server;
2161
+ this.server = undefined;
2162
+ const closeServer = server === undefined
2163
+ ? Promise.resolve()
2164
+ : new Promise((resolve, reject) => {
2165
+ server.close(error => {
2166
+ if (error !== undefined)
2167
+ reject(error);
2168
+ else
2169
+ resolve();
2170
+ });
2171
+ });
2172
+ await closeServer;
2173
+ await Promise.all([
2174
+ (_d = (_b = this.knex) === null || _b === void 0 ? void 0 : _b.destroy()) !== null && _d !== void 0 ? _d : Promise.resolve(),
2175
+ (_e = (_c = this.mongoClient) === null || _c === void 0 ? void 0 : _c.close()) !== null && _e !== void 0 ? _e : Promise.resolve()
2176
+ ]);
2177
+ this.knex = undefined;
2178
+ this.mongoClient = undefined;
2179
+ this.mongoDb = undefined;
2024
2180
  }
2025
2181
  /**
2026
2182
  * Runs the post-listen startup work: advertiser init, advertisement sync,
@@ -2129,23 +2285,26 @@ class OverlayExpress {
2129
2285
  }
2130
2286
  /** Revalidate recent BASM anchors against the chain tracker, reconciling any reorg. */
2131
2287
  async revalidateBASMAnchors() {
2132
- var _a, _b;
2288
+ var _a;
2289
+ var _b;
2133
2290
  try {
2134
- const depth = (_a = this.engineConfig.reorgScanDepth) !== null && _a !== void 0 ? _a : this.reorgScanDepth;
2135
- await ((_b = this.engine) === null || _b === void 0 ? void 0 : _b.revalidateRecentAnchors(depth));
2291
+ const depth = (_b = this.engineConfig.reorgScanDepth) !== null && _b !== void 0 ? _b : this.reorgScanDepth;
2292
+ await ((_a = this.engine) === null || _a === void 0 ? void 0 : _a.revalidateRecentAnchors(depth));
2136
2293
  }
2137
2294
  catch (e) {
2138
2295
  console.error(chalk_1.default.red('Failed to revalidate BASM anchors'), e);
2139
2296
  }
2140
2297
  }
2141
2298
  startUnprovenMaintenance() {
2142
- var _a, _b, _c;
2143
- const intervalMs = (_a = this.engineConfig.unprovenMaintenanceIntervalMs) !== null && _a !== void 0 ? _a : this.unprovenMaintenanceIntervalMs;
2299
+ var _a, _b;
2300
+ var _c;
2301
+ const intervalMs = (_c = this.engineConfig.unprovenMaintenanceIntervalMs) !== null && _c !== void 0 ? _c : this.unprovenMaintenanceIntervalMs;
2144
2302
  if (intervalMs <= 0)
2145
2303
  return;
2146
2304
  const run = () => {
2147
2305
  void (async () => {
2148
- var _a, _b;
2306
+ var _a;
2307
+ var _b;
2149
2308
  try {
2150
2309
  const report = await ((_a = this.engine) === null || _a === void 0 ? void 0 : _a.maintainUnprovenTransactions({
2151
2310
  thresholdBlocks: (_b = this.engineConfig.unprovenEvictionBlocks) !== null && _b !== void 0 ? _b : this.unprovenEvictionBlocks,
@@ -2160,7 +2319,7 @@ class OverlayExpress {
2160
2319
  };
2161
2320
  run();
2162
2321
  this.unprovenMaintenanceTimer = setInterval(run, intervalMs);
2163
- (_c = (_b = this.unprovenMaintenanceTimer).unref) === null || _c === void 0 ? void 0 : _c.call(_b);
2322
+ (_b = (_a = this.unprovenMaintenanceTimer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
2164
2323
  }
2165
2324
  }
2166
2325
  exports.default = OverlayExpress;