@byollm/protocol 0.1.0-alpha.86 → 0.1.0-alpha.87

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/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  > [!WARNING]
2
- > **Alpha (`0.1.0-alpha.86`) — under active development. Don't use this yet.**
2
+ > **Alpha (`0.1.0-alpha.87`) — under active development. Don't use this yet.**
3
3
  >
4
4
  > Install it deliberately: `npm install @byollm/protocol@alpha`.
5
5
  >
package/dist/index.js CHANGED
@@ -632,6 +632,83 @@ function verifyGrant(input) {
632
632
  ) ? null : "bad-signature";
633
633
  }
634
634
 
635
+ // src/json-size.ts
636
+ function jsonLength(value) {
637
+ try {
638
+ return count(value, /* @__PURE__ */ new Set());
639
+ } catch {
640
+ return void 0;
641
+ }
642
+ }
643
+ var OutOfDomain = class extends Error {
644
+ };
645
+ function bail() {
646
+ throw new OutOfDomain("not a JSON value");
647
+ }
648
+ function count(value, seen) {
649
+ if (value === null) return 4;
650
+ switch (typeof value) {
651
+ case "boolean":
652
+ return value ? 4 : 5;
653
+ // true / false
654
+ case "number":
655
+ return Number.isFinite(value) ? String(value).length : 4;
656
+ case "string":
657
+ return stringLength(value);
658
+ case "object":
659
+ break;
660
+ default:
661
+ return bail();
662
+ }
663
+ const object = value;
664
+ if (seen.has(object)) return bail();
665
+ seen.add(object);
666
+ try {
667
+ if (Array.isArray(object)) {
668
+ let total2 = 2 + Math.max(0, object.length - 1);
669
+ for (const element of object) {
670
+ total2 += element === void 0 || typeof element === "function" ? 4 : count(element, seen);
671
+ }
672
+ return total2;
673
+ }
674
+ if ("toJSON" in object) return bail();
675
+ const proto = Object.getPrototypeOf(object);
676
+ if (proto !== Object.prototype) return bail();
677
+ let total = 2;
678
+ let first = true;
679
+ for (const [key, entry] of Object.entries(object)) {
680
+ if (entry === void 0 || typeof entry === "function") continue;
681
+ total += (first ? 0 : 1) + stringLength(key) + 1 + count(entry, seen);
682
+ first = false;
683
+ }
684
+ return total;
685
+ } finally {
686
+ seen.delete(object);
687
+ }
688
+ }
689
+ function stringLength(text) {
690
+ let total = 2;
691
+ for (let index = 0; index < text.length; index += 1) {
692
+ const code = text.charCodeAt(index);
693
+ if (code === 34 || code === 92) {
694
+ total += 2;
695
+ } else if (code < 32) {
696
+ total += code === 8 || code === 9 || code === 10 || code === 12 || code === 13 ? 2 : 6;
697
+ } else if (code >= 55296 && code <= 57343) {
698
+ const paired = code <= 56319 && index + 1 < text.length && text.charCodeAt(index + 1) >= 56320 && text.charCodeAt(index + 1) <= 57343;
699
+ if (paired) {
700
+ total += 2;
701
+ index += 1;
702
+ } else {
703
+ total += 6;
704
+ }
705
+ } else {
706
+ total += 1;
707
+ }
708
+ }
709
+ return total;
710
+ }
711
+
635
712
  // src/kinds.ts
636
713
  import { z as z6 } from "zod";
637
714
  var PAYLOAD_LIMITS = Object.freeze({
@@ -902,6 +979,8 @@ var SizeClass = z7.enum(["small", "medium", "large", "unbounded"]);
902
979
  var SIZE_CLASSES = Object.freeze(SizeClass.options);
903
980
  var MAX_ENVELOPE_BYTES = 10 * 1024 * 1024;
904
981
  function envelopeBytes(envelope) {
982
+ const counted = jsonLength(envelope);
983
+ if (counted !== void 0) return counted;
905
984
  const serialised = JSON.stringify(envelope);
906
985
  return serialised === void 0 ? 0 : serialised.length;
907
986
  }