From cd4c922d92cc3ee877c307f5ab9a48d39e233f2f Mon Sep 17 00:00:00 2001 From: mdbase release automation Date: Sat, 28 Nov 2026 23:41:21 +1100 Subject: [PATCH] Encode short ASCII text directly in SDK CBOR frames --- changelog.d/sdk-short-text-encoding.md | 3 +++ runtime/packages/sdk/src/cbor.ts | 8 ++++++++ runtime/packages/sdk/test/cbor-secrets.test.ts | 2 +- runtime/packages/sdk/test/cbor.test.ts | 8 ++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 changelog.d/sdk-short-text-encoding.md diff --git a/changelog.d/sdk-short-text-encoding.md b/changelog.d/sdk-short-text-encoding.md new file mode 100644 index 000000000..b92684870 --- /dev/null +++ b/changelog.d/sdk-short-text-encoding.md @@ -0,0 +1,3 @@ +## Changed + +- The TypeScript SDK serializes short ASCII record fields directly into query and sync frames, reducing allocation overhead. diff --git a/runtime/packages/sdk/src/cbor.ts b/runtime/packages/sdk/src/cbor.ts index 77340ed00..21ce4df20 100644 --- a/runtime/packages/sdk/src/cbor.ts +++ b/runtime/packages/sdk/src/cbor.ts @@ -103,6 +103,13 @@ class Writer { this.len += b.length; } + /** For short ASCII text, the UTF-8 length fits in the initial CBOR byte. */ + shortAscii(s: string): void { + this.ensure(1 + s.length); + this.buf[this.len++] = 0x60 | s.length; + for (let i = 0; i < s.length; i++) this.buf[this.len++] = s.charCodeAt(i); + } + head(major: number, arg: number | bigint): void { const m = major << 5; if (typeof arg === "number" && arg < 24) { @@ -189,6 +196,7 @@ function encodeMap(w: Writer, m: CborMap, depth: number): void { } function encodeText(w: Writer, s: string): void { + if (s.length < 24 && !/[^\x00-\x7f]/u.test(s)) return w.shortAscii(s); if (!isWellFormed(s)) throw new CborError("utf8", "text is not well-formed UTF-16"); const b = textEncoder.encode(s); try { diff --git a/runtime/packages/sdk/test/cbor-secrets.test.ts b/runtime/packages/sdk/test/cbor-secrets.test.ts index f519a278c..7dc8da68f 100644 --- a/runtime/packages/sdk/test/cbor-secrets.test.ts +++ b/runtime/packages/sdk/test/cbor-secrets.test.ts @@ -12,7 +12,7 @@ describe("secret-bearing CBOR scratch lifetime", () => { temporary.push(bytes); return bytes; }); - const input = new Map([["key", "value"]]); + const input = new Map([["clé", "valeur privée"]]); const output = encodeSecret(input); expect(decode(output)).toEqual(input); expect(temporary).toHaveLength(2); diff --git a/runtime/packages/sdk/test/cbor.test.ts b/runtime/packages/sdk/test/cbor.test.ts index da04fe80b..dd21b9d26 100644 --- a/runtime/packages/sdk/test/cbor.test.ts +++ b/runtime/packages/sdk/test/cbor.test.ts @@ -48,6 +48,14 @@ describe("mdb-cbor/1", () => { expect(() => encode("\ud800")).toThrow(CborError); }); + it("uses the UTF-8 byte count at the short-text boundary", () => { + for (const text of ["", "\0\x7f", "x".repeat(23), "x".repeat(24), "x".repeat(22) + "é", "😀"]) { + const utf8 = new TextEncoder().encode(text); + const header = utf8.length < 24 ? [0x60 + utf8.length] : [0x78, utf8.length]; + expect(encode(text)).toEqual(Uint8Array.from([...header, ...utf8])); + } + }); + it("bounds depth", () => { const b = new Uint8Array(130).fill(0x81); b[129] = 0;