@vex-chat/cli 0.2.0 → 0.3.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/package.json +2 -2
  2. package/src/vex-chat.js +57 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vex-chat/cli",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Terminal client for vex-chat.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -31,7 +31,7 @@
31
31
  "dependencies": {
32
32
  "better-sqlite3": "11.10.0",
33
33
  "msgpackr": "^1.11.9",
34
- "@vex-chat/libvex": "^7.1.1"
34
+ "@vex-chat/libvex": "^7.1.2"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/node": "^25.6.0",
package/src/vex-chat.js CHANGED
@@ -597,16 +597,68 @@ function getClientBearerToken(client) {
597
597
  return match?.[1] ?? null;
598
598
  }
599
599
 
600
+ function responseBodyMessage(data) {
601
+ if (!data || typeof data !== "object" || ArrayBuffer.isView(data)) {
602
+ return null;
603
+ }
604
+ if (data instanceof ArrayBuffer) {
605
+ return null;
606
+ }
607
+ const error = data.error;
608
+ if (typeof error === "string") return error;
609
+ if (error && typeof error === "object") {
610
+ const nestedMessage = error.message;
611
+ if (typeof nestedMessage === "string") return nestedMessage;
612
+ }
613
+ const message = data.message;
614
+ return typeof message === "string" ? message : null;
615
+ }
616
+
617
+ function bytesFromResponseBody(data) {
618
+ if (ArrayBuffer.isView(data)) {
619
+ return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
620
+ }
621
+ if (data instanceof ArrayBuffer) {
622
+ return new Uint8Array(data);
623
+ }
624
+ return null;
625
+ }
626
+
627
+ function decodeBinaryResponseBody(data) {
628
+ const bytes = bytesFromResponseBody(data);
629
+ if (!bytes) return null;
630
+ try {
631
+ return unpack(bytes);
632
+ } catch {
633
+ // Express JSON error bodies also arrive as ArrayBuffer with fetch.
634
+ }
635
+ const text = new TextDecoder("utf-8", { fatal: false })
636
+ .decode(bytes)
637
+ .trim();
638
+ if (!text) return null;
639
+ if (text.startsWith("{") || text.startsWith("[")) {
640
+ try {
641
+ return JSON.parse(text);
642
+ } catch {
643
+ return text;
644
+ }
645
+ }
646
+ return text;
647
+ }
648
+
600
649
  function errorResponseMessage(err) {
601
650
  const data = err?.response?.data;
602
- if (data && typeof data === "object" && !ArrayBuffer.isView(data)) {
603
- const message = data.error ?? data.message;
604
- if (typeof message === "string") return message;
605
- }
651
+ const binaryBody = decodeBinaryResponseBody(data);
652
+ const binaryMessage = responseBodyMessage(binaryBody);
653
+ if (binaryMessage) return binaryMessage;
654
+ if (typeof binaryBody === "string") return binaryBody;
655
+ const objectMessage = responseBodyMessage(data);
656
+ if (objectMessage) return objectMessage;
606
657
  if (typeof data === "string") {
607
658
  try {
608
659
  const parsed = JSON.parse(data);
609
- if (typeof parsed?.error === "string") return parsed.error;
660
+ const jsonMessage = responseBodyMessage(parsed);
661
+ if (jsonMessage) return jsonMessage;
610
662
  } catch {
611
663
  return data;
612
664
  }