Skip to content

Type.fromBuffer() behaves differently when given the original toBuffer() result vs. a Uint8Array copy of the same bytes. #506

@simensgreen

Description

@simensgreen

avsc: fromBuffer yields wrong result for Uint8Array copy of toBuffer output (browser)

Environment

  • Runtime: Browser (Vite bundled)
  • Browser: Microsoft Edge
  • avsc: ^5.7.9
  • buffer: ^6.0.3 (polyfill)
  • Bundler: Vite ^7.3.1

Description

Type.fromBuffer() behaves differently when given the original toBuffer() result vs. a Uint8Array copy of the same bytes. The same byte sequence decodes correctly from the original buffer but produces wrong output from the copy.

Steps to reproduce

import { Type } from "avsc/etc/browser/avsc"

const strArr = Type.forSchema({ type: "array", items: "string" })
const buf1 = strArr.toBuffer(["a", "b", "c"])
const buf2 = new Uint8Array([...buf1])

const left = strArr.fromBuffer(buf1).join("")
const right = strArr.fromBuffer(buf2).join("")

console.log(left, right) // "abc" "979899"

Expected

left === right → both "abc"

Actual

  • left === "abc"
  • right === "979899"

The string "979899" corresponds to ASCII codes 97, 98, 99 ('a', 'b', 'c') interpreted as decimal strings: "97" + "98" + "99".

Additional notes

  • Using the original buf1 in fromBuffer works as expected.
  • Creating a fresh buffer with strArr.toBuffer(["a", "b", "c"]) and decoding it also works correctly.
  • The issue appears only when decoding a Uint8Array copy of the original buffer.
  • Diagnostic output:
    buf1.constructor.name: Buffer
    buf2.constructor.name: Uint8Array
    buf1 instanceof Buffer: true
    buf2 instanceof Buffer: false
    buf1 instanceof Uint8Array: true
    buf2 instanceof Uint8Array: true
    

Possible cause

Different code paths for Buffer vs. plain Uint8Array in avsc’s internal decoding (e.g. instanceof Buffer checks or different handling of .buffer / ArrayBuffer views).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions