@cadenza.io/service 2.17.73 → 2.17.75

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/dist/index.mjs CHANGED
@@ -3612,11 +3612,12 @@ var RestController = class _RestController {
3612
3612
  * @returns {Promise<any>} A promise that resolves to the parsed response data if the request is successful.
3613
3613
  * @throws {Error} Throws an error if the request fails due to issues such as timeout or other unexpected errors.
3614
3614
  */
3615
- this.fetchDataWithTimeout = async function(url, requestInit, timeoutMs) {
3615
+ this.fetchDataWithTimeout = async (url, requestInit, timeoutMs) => {
3616
3616
  const signal = AbortSignal.timeout(timeoutMs);
3617
3617
  try {
3618
3618
  const response = await fetch(url, { ...requestInit, signal });
3619
- return await response.json();
3619
+ const parsedResponse = await this.parseFetchResponse(response);
3620
+ return parsedResponse.data;
3620
3621
  } catch (error) {
3621
3622
  if (error?.name === "AbortError") {
3622
3623
  CadenzaService.log(
@@ -3682,7 +3683,11 @@ var RestController = class _RestController {
3682
3683
  }
3683
3684
  console.log("Service inserted...");
3684
3685
  const app = express();
3685
- app.use(bodyParser.json());
3686
+ app.use(
3687
+ bodyParser.json({
3688
+ limit: this.resolveJsonBodyLimit()
3689
+ })
3690
+ );
3686
3691
  switch (ctx.__securityProfile) {
3687
3692
  case "low":
3688
3693
  app.use(helmet());
@@ -3855,6 +3860,32 @@ var RestController = class _RestController {
3855
3860
  )
3856
3861
  );
3857
3862
  });
3863
+ app.use(
3864
+ (error, _req, res, next) => {
3865
+ if (!error) {
3866
+ next();
3867
+ return;
3868
+ }
3869
+ const statusCode = typeof error.statusCode === "number" ? error.statusCode : typeof error.status === "number" ? error.status : error.type === "entity.too.large" ? 413 : 500;
3870
+ const message = error.type === "entity.too.large" ? `Request payload exceeded REST body limit ${this.resolveJsonBodyLimit()}.` : this.getErrorMessage(error);
3871
+ CadenzaService.log(
3872
+ "REST request failed before route completion.",
3873
+ {
3874
+ error: message,
3875
+ type: error.type,
3876
+ statusCode,
3877
+ path: _req?.path,
3878
+ method: _req?.method
3879
+ },
3880
+ statusCode >= 500 ? "error" : "warning"
3881
+ );
3882
+ res.status(statusCode).json({
3883
+ __status: "error",
3884
+ errored: true,
3885
+ __error: message
3886
+ });
3887
+ }
3888
+ );
3858
3889
  return true;
3859
3890
  },
3860
3891
  "Starts REST server and initiates meta-handling"
@@ -4393,6 +4424,42 @@ var RestController = class _RestController {
4393
4424
  return String(error);
4394
4425
  }
4395
4426
  }
4427
+ resolveJsonBodyLimit() {
4428
+ const configuredLimit = process.env.CADENZA_REST_BODY_LIMIT?.trim();
4429
+ return configuredLimit && configuredLimit.length > 0 ? configuredLimit : "10mb";
4430
+ }
4431
+ async parseFetchResponse(response) {
4432
+ const contentType = response.headers.get("content-type") ?? "";
4433
+ const rawText = await response.text();
4434
+ const headers = Object.fromEntries(response.headers.entries());
4435
+ if (rawText.length === 0) {
4436
+ return {
4437
+ ok: response.ok,
4438
+ status: response.status,
4439
+ statusText: response.statusText,
4440
+ headers,
4441
+ data: {}
4442
+ };
4443
+ }
4444
+ if (!contentType.toLowerCase().includes("application/json")) {
4445
+ throw new Error(
4446
+ `Expected JSON response from ${response.url ?? "remote service"} but received ${contentType || "unknown content type"} (HTTP ${response.status}). Body preview: ${rawText.slice(0, 200)}`
4447
+ );
4448
+ }
4449
+ try {
4450
+ return {
4451
+ ok: response.ok,
4452
+ status: response.status,
4453
+ statusText: response.statusText,
4454
+ headers,
4455
+ data: JSON.parse(rawText)
4456
+ };
4457
+ } catch (error) {
4458
+ throw new Error(
4459
+ `Failed to parse JSON response from ${response.url ?? "remote service"} (HTTP ${response.status}). Body preview: ${rawText.slice(0, 200)}. Parse error: ${this.getErrorMessage(error)}`
4460
+ );
4461
+ }
4462
+ }
4396
4463
  resolveDelegationTimeoutMs(ctx) {
4397
4464
  const syncing = ctx?.__syncing === true || ctx?.__metadata?.__syncing === true || Array.isArray(ctx?.joinedContexts) && ctx.joinedContexts.some(
4398
4465
  (joinedCtx) => joinedCtx?.__syncing === true || joinedCtx?.__metadata?.__syncing === true
@@ -9700,7 +9767,7 @@ var GraphSyncController = class _GraphSyncController {
9700
9767
  });
9701
9768
  const signalName = resolveSignalNameFromSyncContext(ctx);
9702
9769
  if (!signalName) {
9703
- return true;
9770
+ return false;
9704
9771
  }
9705
9772
  const signalObservers = CadenzaService.signalBroker.signalObservers;
9706
9773
  if (!signalObservers?.has(signalName)) {