dotnet/aspnetcore · error

Cannot create a JSStreamReference from the value '${streamRe

Error message

Cannot create a JSStreamReference from the value '${streamReference}' as it doesn't have a byteLength.

What it means

Thrown by createJSStreamReference() in Microsoft.JSInterop.ts (line 203) when the supplied value is a typed-array-like object (it has streamReference.buffer instanceof ArrayBuffer) but lacks a byteLength property. The stream reference needs byteLength to tell .NET how many bytes to pull; without it the transfer cannot be sized, so the function refuses.

Source

Thrown at src/JSInterop/Microsoft.JSInterop.JS/src/src/Microsoft.JSInterop.ts:203

   *
   * @param streamReference The ArrayBufferView or Blob used to create the JavaScript stream reference.
   * @returns The JavaScript data reference (this will be the same instance as the given object).
   * @throws Error if the given value is not an Object or doesn't have a valid byteLength.
   */
  export function createJSStreamReference(streamReference: ArrayBuffer | ArrayBufferView | Blob | any): any {
      let length = -1;

      // If we're given a raw Array Buffer, we interpret it as a `Uint8Array` as
      // ArrayBuffers' aren't directly readable.
      if (streamReference instanceof ArrayBuffer) {
          streamReference = new Uint8Array(streamReference);
      }

      if (streamReference instanceof Blob) {
          length = streamReference.size;
      } else if (streamReference.buffer instanceof ArrayBuffer) {
          if (streamReference.byteLength === undefined) {
              throw new Error(`Cannot create a JSStreamReference from the value '${streamReference}' as it doesn't have a byteLength.`);
          }

          length = streamReference.byteLength;
      } else {
          throw new Error("Supplied value is not a typed array or blob.");
      }

      const result: any = {
          [jsStreamReferenceLengthKey]: length
      };

      try {
          const jsObjectReference = createJSObjectReference(streamReference);
          result[jsObjectIdKey] = jsObjectReference[jsObjectIdKey];
      } catch (error) {
          throw new Error(`Cannot create a JSStreamReference from the value '${streamReference}'.`);
      }

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Pass a real typed array (e.g., new Uint8Array(...)) or a Blob — these always carry byteLength/size.
  2. If wrapping data, expose a byteLength getter that returns the underlying view's length.
  3. Copy your custom buffer into a Uint8Array before creating the stream reference.
  4. Avoid Proxy/wrapper layers around typed arrays passed to interop.

Example fix

// before
const fake = Object.assign(Object.create(Uint8Array.prototype), { buffer: someBuffer });
DotNet.createJSStreamReference(fake); // throws — no byteLength

// after
const view = new Uint8Array(someBuffer);
DotNet.createJSStreamReference(view);
Defensive patterns

Strategy: type-guard

Validate before calling

function hasByteLength(v: any): boolean {
  return v && v.buffer instanceof ArrayBuffer && typeof v.byteLength === 'number';
}

Type guard

function isTypedArrayLike(v: any): v is ArrayBufferView {
  return v && v.buffer instanceof ArrayBuffer && typeof v.byteLength === 'number';
}

Prevention

When it happens

Trigger: Calling createJSStreamReference with an object whose [[Prototype]] gives it a buffer but on which byteLength is undefined — e.g., a custom wrapper around an ArrayBufferView, or a partially-constructed DataView/typed array before its byteLength is set.

Common situations: Passing a custom Buffer-like object instead of a real typed array (Uint8Array, Int32Array, etc.) or Blob. Object created via Object.create(Uint8Array.prototype) without setting byteLength. A polyfilled/shimmed typed array missing the property. Proxy-wrapped typed arrays that do not expose byteLength.

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/cb78c7f74abc13bf. Report an issue: GitHub.