@jrpool/kilotest 61.0.0 → 63.0.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.
package/index.ts CHANGED
@@ -88,6 +88,7 @@ import {response as requestNewTestAPI} from './api/requestNewTest.ts';
88
88
  import {response as orderNewTestAPI} from './api/orderNewTest.ts';
89
89
  import {response as orderRetestAPI} from './api/orderRetest.ts';
90
90
  import {response as awaitTestAPI} from './api/awaitTest.ts';
91
+ import {response as reportDisagreementAPI} from './api/reportDisagreement.ts';
91
92
 
92
93
  // ENVIRONMENT
93
94
 
@@ -190,6 +191,7 @@ const apiRespond: {
190
191
  orderNewTest: ApiResponder;
191
192
  orderRetest: ApiResponder;
192
193
  awaitTest: ApiResponder;
194
+ reportDisagreement: ApiResponder;
193
195
  } = {
194
196
  getReport: getReportAPI,
195
197
  listDiagnoses: listDiagnosesAPI,
@@ -201,7 +203,8 @@ const apiRespond: {
201
203
  requestNewTest: requestNewTestAPI,
202
204
  orderNewTest: orderNewTestAPI,
203
205
  orderRetest: orderRetestAPI,
204
- awaitTest: awaitTestAPI
206
+ awaitTest: awaitTestAPI,
207
+ reportDisagreement: reportDisagreementAPI
205
208
  };
206
209
 
207
210
  // CONSTANTS
@@ -1396,6 +1399,16 @@ const handleRequest = async (request: IncomingMessage, response: ServerResponse)
1396
1399
  setHeaders('application/json', null, 'ultra');
1397
1400
  response.end(JSON.stringify(responseBody));
1398
1401
  }
1402
+ // Otherwise, if the service is to receive a disagreement with a diagnosis:
1403
+ else if (segments[0] === 'reportDisagreement') {
1404
+ const {engineID, ruleID, reason} = postData as {engineID: string; ruleID: string; reason: string};
1405
+ // Get the response body.
1406
+ const responseBody = await apiRespond.reportDisagreement(segments.slice(1).concat(engineID, ruleID, reason));
1407
+ await recordPageMetric('apiOperations', 'reportDisagreement');
1408
+ // Send it.
1409
+ setHeaders('application/json', null, 'ultra');
1410
+ response.end(JSON.stringify(responseBody));
1411
+ }
1399
1412
  // Otherwise, if the service is to order an immediate, automatically approved test:
1400
1413
  else if (segments[0] === 'orderNewTest') {
1401
1414
  const {description, URL, reason} = postData as {description: string; URL: string; reason: string};
@@ -1516,7 +1529,19 @@ export {serve};
1516
1529
 
1517
1530
  // Starts the server using the configured protocol and credentials.
1518
1531
  export const startServer = async () => {
1532
+ // Fail fast on missing required environment variables. THIS_KILOTEST_HOST is read raw
1533
+ // by getThisHost and interpolated into URLs in every response; KEY and CERT are
1534
+ // required only for HTTPS. Other env vars have documented defaults or designed
1535
+ // degradation (see env.example and docs/SERVICE.md) and are deliberately not required.
1536
+ const requiredEnvVars = ['THIS_KILOTEST_HOST'];
1519
1537
  const startProtocol = process.env.PROTOCOL || 'http';
1538
+ if (startProtocol === 'https') {
1539
+ requiredEnvVars.push('KEY', 'CERT');
1540
+ }
1541
+ const missingEnvVars = requiredEnvVars.filter(name => !process.env[name]);
1542
+ if (missingEnvVars.length) {
1543
+ throw new Error(`Missing required environment variable(s): ${missingEnvVars.join(', ')}`);
1544
+ }
1520
1545
  if (startProtocol === 'http') {
1521
1546
  console.log('Starting HTTP server');
1522
1547
  return serve(http, {});
@@ -1532,7 +1557,10 @@ export const startServer = async () => {
1532
1557
  // Runs the server if the module was loaded directly (not required by a test). The starter is a parameter so tests can inject a spy, since ESM module exports cannot be monkey-patched.
1533
1558
  export const runIfMain = (mainModule: unknown, currentModule: unknown, starter: () => Promise<unknown> = startServer) => {
1534
1559
  if (mainModule === currentModule) {
1535
- starter().catch(error => console.log(errorMessage(error)));
1560
+ starter().catch(error => {
1561
+ console.log(errorMessage(error));
1562
+ process.exitCode = 1;
1563
+ });
1536
1564
  }
1537
1565
  };
1538
1566