{"record":{"id":"cb78c7f74abc13bf","repo":"dotnet/aspnetcore","slug":"cannot-create-a-jsstreamreference-from-the-value","errorCode":null,"errorMessage":"Cannot create a JSStreamReference from the value '${streamReference}' as it doesn't have a byteLength.","messagePattern":"Cannot create a JSStreamReference from the value '(.+?)' as it doesn't have a byteLength\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/JSInterop/Microsoft.JSInterop.JS/src/src/Microsoft.JSInterop.ts","lineNumber":203,"sourceCode":"   *\n   * @param streamReference The ArrayBufferView or Blob used to create the JavaScript stream reference.\n   * @returns The JavaScript data reference (this will be the same instance as the given object).\n   * @throws Error if the given value is not an Object or doesn't have a valid byteLength.\n   */\n  export function createJSStreamReference(streamReference: ArrayBuffer | ArrayBufferView | Blob | any): any {\n      let length = -1;\n\n      // If we're given a raw Array Buffer, we interpret it as a `Uint8Array` as\n      // ArrayBuffers' aren't directly readable.\n      if (streamReference instanceof ArrayBuffer) {\n          streamReference = new Uint8Array(streamReference);\n      }\n\n      if (streamReference instanceof Blob) {\n          length = streamReference.size;\n      } else if (streamReference.buffer instanceof ArrayBuffer) {\n          if (streamReference.byteLength === undefined) {\n              throw new Error(`Cannot create a JSStreamReference from the value '${streamReference}' as it doesn't have a byteLength.`);\n          }\n\n          length = streamReference.byteLength;\n      } else {\n          throw new Error(\"Supplied value is not a typed array or blob.\");\n      }\n\n      const result: any = {\n          [jsStreamReferenceLengthKey]: length\n      };\n\n      try {\n          const jsObjectReference = createJSObjectReference(streamReference);\n          result[jsObjectIdKey] = jsObjectReference[jsObjectIdKey];\n      } catch (error) {\n          throw new Error(`Cannot create a JSStreamReference from the value '${streamReference}'.`);\n      }\n","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/JSInterop/Microsoft.JSInterop.JS/src/src/Microsoft.JSInterop.ts#L185-L221","documentation":"Thrown by DotNet.createJSStreamReference() when the supplied value is a typed-array-like object (its .buffer is an ArrayBuffer) but its byteLength property is undefined. byteLength is required to tell .NET the stream's size up front.","triggerScenarios":"Passing a DataView or typed array whose byteLength has been deleted, or a custom ArrayBufferView-shaped object missing byteLength. Normal Uint8Array/Int32Array/etc. always have byteLength, so this only fires with malformed or polyfilled typed arrays.","commonSituations":"Using a mock or polyfill that doesn't fully implement the TypedArray spec; passing a hand-rolled object that quacks like an ArrayBufferView but omits byteLength; an older transpiler/runtime stripping the property.","solutions":["Use a standard typed array (Uint8Array, Int32Array) which always carries byteLength.","If wrapping custom data, construct a Uint8Array copy: new Uint8Array(yourBytes).","Use a Blob if you only have raw bytes: new Blob([data])."],"exampleFix":"// before\nconst ref = createJSStreamReference({ buffer: someArrayBuffer }); // no byteLength\n\n// after\nconst ref = createJSStreamReference(new Uint8Array(someArrayBuffer));","handlingStrategy":"type-guard","validationCode":"function hasByteLength(v) { return v != null && typeof v.byteLength === 'number'; }\nif (v.buffer instanceof ArrayBuffer && !hasByteLength(v)) { throw new TypeError('Typed array missing byteLength'); }","typeGuard":"function isTypedArrayWithByteLength(v: unknown): v is ArrayBufferView { return v != null && (v as any).buffer instanceof ArrayBuffer && typeof (v as any).byteLength === 'number'; }","tryCatchPattern":"try { return DotNet.createJSStreamReference(value); } catch (e) { throw new Error('Pass a standard typed array or Blob for stream references.', { cause: e }); }","preventionTips":["Always pass standard typed arrays (Uint8Array, Int32Array) which guarantee byteLength.","Avoid polyfills or wrappers that strip byteLength from ArrayBufferView objects.","Prefer Blob for opaque byte payloads."],"tags":["js-interop","blazor","stream","typed-array","serialization"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}