@trainheroic-unofficial/cli 3.5.2 → 3.6.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 (2) hide show
  1. package/dist/cli.mjs +190 -15
  2. package/package.json +3 -3
package/dist/cli.mjs CHANGED
@@ -785,30 +785,200 @@ const sessionTemplateCreateSchema = z.object({
785
785
  });
786
786
  //#endregion
787
787
  //#region ../js/src/http-error.ts
788
- /**
789
- * A final non-2xx response from TrainHeroic. The error deliberately carries only request
790
- * metadata that is safe to send to telemetry: never the path, query string, request body,
791
- * response body, credentials, or session token.
792
- */
788
+ const MAX_REQUEST_KEYS = 50;
789
+ const MAX_RESPONSE_DEPTH = 4;
790
+ const MAX_RESPONSE_KEYS = 20;
791
+ const MAX_RESPONSE_ITEMS = 10;
792
+ const MAX_RESPONSE_NODES = 50;
793
+ const RESPONSE_DIAGNOSTIC_KEYS = /* @__PURE__ */ new Set([
794
+ "code",
795
+ "detail",
796
+ "details",
797
+ "error",
798
+ "error_code",
799
+ "errors",
800
+ "message",
801
+ "reason",
802
+ "status",
803
+ "status_code",
804
+ "success"
805
+ ]);
806
+ const RESPONSE_CONTAINER_KEYS = /* @__PURE__ */ new Set([
807
+ "data",
808
+ "received",
809
+ "response",
810
+ "result"
811
+ ]);
812
+ const RESPONSE_STATUS_KEYS = /* @__PURE__ */ new Set(["status", "status_code"]);
813
+ const RESPONSE_BOOLEAN_KEYS = /* @__PURE__ */ new Set(["success"]);
814
+ const PARAMETER_TYPES = /* @__PURE__ */ new Set([
815
+ 0,
816
+ 1,
817
+ 2,
818
+ 3,
819
+ 4,
820
+ 5,
821
+ 6,
822
+ 7,
823
+ 10,
824
+ 11,
825
+ 12,
826
+ 13,
827
+ 14,
828
+ 18
829
+ ]);
830
+ function safeFieldName(key) {
831
+ if (key === "__proto__" || key === "constructor" || key === "prototype") return "[Redacted key]";
832
+ if (!/^[A-Za-z_][A-Za-z0-9_.-]{0,79}$/.test(key)) return "[Redacted key]";
833
+ return key;
834
+ }
835
+ function boundedEntries(object, limit) {
836
+ const entries = [];
837
+ for (const key of Object.keys(object)) {
838
+ const descriptor = Object.getOwnPropertyDescriptor(object, key);
839
+ entries.push([key, descriptor && "value" in descriptor ? descriptor.value : void 0]);
840
+ if (entries.length >= limit) break;
841
+ }
842
+ return entries;
843
+ }
844
+ function safeRequestValue(key, value) {
845
+ if (key === "is_circuit" && typeof value === "boolean") return value;
846
+ if (["param_1_type", "param_2_type"].includes(key) && typeof value === "number" && PARAMETER_TYPES.has(value)) return value;
847
+ }
848
+ function requestBodySummary(body) {
849
+ if (Array.isArray(body)) return {
850
+ type: "array",
851
+ length: body.length
852
+ };
853
+ if (body && typeof body === "object") {
854
+ const entries = boundedEntries(body, MAX_REQUEST_KEYS).sort(([left], [right]) => left.localeCompare(right));
855
+ const keys = entries.map(([key]) => safeFieldName(key));
856
+ const values = {};
857
+ for (const [key, value] of entries) {
858
+ const safeValue = safeRequestValue(key, value);
859
+ if (safeValue !== void 0) values[safeFieldName(key)] = safeValue;
860
+ }
861
+ return Object.keys(values).length > 0 ? {
862
+ type: "object",
863
+ keys,
864
+ values
865
+ } : {
866
+ type: "object",
867
+ keys
868
+ };
869
+ }
870
+ const primitive = typeof body;
871
+ return primitive === "boolean" || primitive === "number" || primitive === "string" ? { type: primitive } : { type: "other" };
872
+ }
873
+ function sanitizedDiagnosticValue(value, depth, budget, field) {
874
+ if (budget.nodes <= 0) return "[Truncated]";
875
+ budget.nodes -= 1;
876
+ if (value === null) return null;
877
+ if (typeof value === "boolean") return field && RESPONSE_BOOLEAN_KEYS.has(field) ? value : "[Redacted]";
878
+ if (typeof value === "number") return field && RESPONSE_STATUS_KEYS.has(field) && Number.isInteger(value) && value >= 100 && value <= 599 ? value : "[Redacted]";
879
+ if (typeof value === "string") return "[Redacted]";
880
+ if (depth >= MAX_RESPONSE_DEPTH) return "[Truncated]";
881
+ if (Array.isArray(value)) {
882
+ const result = [];
883
+ for (const item of value.slice(0, MAX_RESPONSE_ITEMS)) {
884
+ if (budget.nodes <= 0) {
885
+ result.push("[Truncated]");
886
+ break;
887
+ }
888
+ result.push(sanitizedDiagnosticValue(item, depth + 1, budget));
889
+ }
890
+ return result;
891
+ }
892
+ if (value && typeof value === "object") {
893
+ const result = {};
894
+ for (const [key, item] of boundedEntries(value, MAX_RESPONSE_KEYS)) {
895
+ if (budget.nodes <= 0) {
896
+ result.truncated = "[Truncated]";
897
+ break;
898
+ }
899
+ const safeKey = safeFieldName(key);
900
+ const safeValue = safeRequestValue(key, item);
901
+ if (RESPONSE_DIAGNOSTIC_KEYS.has(key)) result[safeKey] = sanitizedDiagnosticValue(item, depth + 1, budget, key);
902
+ else if (RESPONSE_CONTAINER_KEYS.has(key) && item && typeof item === "object") result[safeKey] = sanitizedDiagnosticValue(item, depth + 1, budget);
903
+ else if (safeValue !== void 0) {
904
+ budget.nodes -= 1;
905
+ result[safeKey] = safeValue;
906
+ }
907
+ }
908
+ return result;
909
+ }
910
+ return `[${typeof value}]`;
911
+ }
912
+ function responseBodyDiagnostics(body, budget, depth = 0) {
913
+ const activeBudget = budget ?? { nodes: MAX_RESPONSE_NODES };
914
+ if (activeBudget.nodes <= 0 || depth >= MAX_RESPONSE_DEPTH) return "[Truncated]";
915
+ if (!body || typeof body !== "object" || Array.isArray(body)) return sanitizedDiagnosticValue(body, depth, activeBudget);
916
+ const object = body;
917
+ const result = {};
918
+ for (const [key, value] of boundedEntries(object, MAX_RESPONSE_KEYS)) {
919
+ if (activeBudget.nodes <= 0) {
920
+ result.truncated = "[Truncated]";
921
+ break;
922
+ }
923
+ if (RESPONSE_DIAGNOSTIC_KEYS.has(key)) {
924
+ result[safeFieldName(key)] = sanitizedDiagnosticValue(value, depth + 1, activeBudget, key);
925
+ continue;
926
+ }
927
+ if (RESPONSE_CONTAINER_KEYS.has(key) && value && typeof value === "object") {
928
+ activeBudget.nodes -= 1;
929
+ const nested = responseBodyDiagnostics(value, activeBudget, depth + 1);
930
+ if (nested === "[Truncated]" || nested && typeof nested === "object" && Object.keys(nested).length > 0) result[safeFieldName(key)] = nested;
931
+ }
932
+ }
933
+ if (Object.keys(result).length > 0) return result;
934
+ return { type: "object" };
935
+ }
936
+ function parseResponseText(text) {
937
+ if (text.length === 0) return text;
938
+ try {
939
+ return JSON.parse(text);
940
+ } catch {
941
+ return text;
942
+ }
943
+ }
944
+ function safeRequestBodySummary(body) {
945
+ try {
946
+ return requestBodySummary(body);
947
+ } catch {
948
+ return { type: "other" };
949
+ }
950
+ }
951
+ function safeResponseBodyDiagnostics(body) {
952
+ try {
953
+ return responseBodyDiagnostics(body);
954
+ } catch {
955
+ return "[Unavailable]";
956
+ }
957
+ }
958
+ /** A final non-2xx response plus bounded, telemetry-safe request and response diagnostics. */
793
959
  var TrainHeroicHttpError = class extends Error {
794
960
  name = "TrainHeroicHttpError";
795
961
  method;
796
962
  status;
797
963
  host;
798
- constructor(method, url, status) {
964
+ requestBody;
965
+ responseBody;
966
+ constructor(method, url, status, diagnostics = {}) {
799
967
  const parsed = new URL(url);
800
968
  const normalizedMethod = method.toUpperCase();
801
969
  super(`TrainHeroic ${normalizedMethod} request failed with HTTP ${status}`);
802
970
  this.method = normalizedMethod;
803
971
  this.status = status;
804
972
  this.host = parsed.host;
973
+ this.requestBody = diagnostics.requestBody === void 0 ? void 0 : safeRequestBodySummary(diagnostics.requestBody);
974
+ this.responseBody = diagnostics.responseBody === void 0 ? void 0 : safeResponseBodyDiagnostics(diagnostics.responseBody);
805
975
  }
806
976
  };
807
977
  /** Call observability hooks without allowing them to change SDK behavior. */
808
- function notifyHttpError(handler, method, url, status) {
978
+ function notifyHttpError(handler, method, url, status, diagnostics = {}) {
809
979
  if (!handler) return;
810
980
  try {
811
- Promise.resolve(handler(new TrainHeroicHttpError(method, url, status))).catch(() => {});
981
+ Promise.resolve(handler(new TrainHeroicHttpError(method, url, status, diagnostics))).catch(() => {});
812
982
  } catch {}
813
983
  }
814
984
  //#endregion
@@ -932,14 +1102,19 @@ var TrainHeroicClient = class {
932
1102
  session = await this.#ensureSession();
933
1103
  res = await this.#send(method, url, session, options.body);
934
1104
  }
935
- if (!res.ok && !options.expectedStatuses?.includes(res.status)) notifyHttpError(this.#onHttpError, method, url, res.status);
936
- const text = await res.text();
937
- let data = text;
938
- if (text.length > 0) try {
939
- data = JSON.parse(text);
940
- } catch {
941
- data = text;
1105
+ const shouldReport = !res.ok && !options.expectedStatuses?.includes(res.status);
1106
+ let text;
1107
+ try {
1108
+ text = await res.text();
1109
+ } catch (error) {
1110
+ if (shouldReport) notifyHttpError(this.#onHttpError, method, url, res.status, { requestBody: options.body });
1111
+ throw error;
942
1112
  }
1113
+ const data = parseResponseText(text);
1114
+ if (shouldReport) notifyHttpError(this.#onHttpError, method, url, res.status, {
1115
+ requestBody: options.body,
1116
+ responseBody: data
1117
+ });
943
1118
  return {
944
1119
  status: res.status,
945
1120
  ok: res.ok,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainheroic-unofficial/cli",
3
- "version": "3.5.2",
3
+ "version": "3.6.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,8 +21,8 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "zod": "^4.4.3",
24
- "@trainheroic-unofficial/dto": "3.5.2",
25
- "@trainheroic-unofficial/js": "3.5.2"
24
+ "@trainheroic-unofficial/dto": "3.6.0",
25
+ "@trainheroic-unofficial/js": "3.6.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/node": "^26.3.0",