{"record":{"id":"46caf817c6f2757a","repo":"dotnet/aspnetcore","slug":"expected-data-to-be-of-type-typeof-this-buffer","errorCode":null,"errorMessage":"Expected data to be of type ${typeof(this._buffer)} but was of type ${typeof(data)}","messagePattern":"Expected data to be of type (.+?) but was of type (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr/src/HttpConnection.ts","lineNumber":762,"sourceCode":"    }\n\n    public send(data: string | ArrayBuffer): Promise<void> {\n        this._bufferData(data);\n        if (!this._transportResult) {\n            this._transportResult = new PromiseSource();\n        }\n        return this._transportResult.promise;\n    }\n\n    public stop(): Promise<void> {\n        this._executing = false;\n        this._sendBufferedData.resolve();\n        return this._sendLoopPromise;\n    }\n\n    private _bufferData(data: string | ArrayBuffer): void {\n        if (this._buffer.length && typeof(this._buffer[0]) !== typeof(data)) {\n            throw new Error(`Expected data to be of type ${typeof(this._buffer)} but was of type ${typeof(data)}`);\n        }\n\n        this._buffer.push(data);\n        this._sendBufferedData.resolve();\n    }\n\n    private async _sendLoop(): Promise<void> {\n        while (true) {\n            await this._sendBufferedData.promise;\n\n            if (!this._executing) {\n                if (this._transportResult) {\n                    this._transportResult.reject(\"Connection stopped.\");\n                }\n\n                break;\n            }\n","sourceCodeStart":744,"sourceCodeEnd":780,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/ts/signalr/src/HttpConnection.ts#L744-L780","documentation":"Thrown by the send-loop's _bufferData helper when a new chunk's type does not match the type of data already buffered. The buffer is homogeneous — once you push a string you must keep pushing strings, and once you push an ArrayBuffer you must keep pushing ArrayBuffers. Mixing types breaks the framing contract with the underlying transport.","triggerScenarios":"Calling `connection.send(...)` / `hubConnection.send` (or stream ingestion) with payloads whose JS type alternates between `string` and `ArrayBuffer`/`Uint8Array` on the same connection instance within the buffered send window.","commonSituations":"Using a JSON hub protocol (text) but occasionally sending a raw byte payload, or switching hub protocols (JsonHubProtocol <-> MessagePackHubProtocol) on an already-established connection, or sending a Uint8Array where the buffer expected a string.","solutions":["Pick one hub protocol per connection and stick to its transfer format (Text -> strings, Binary -> ArrayBuffer).","Encode strings to ArrayBuffer (TextEncoder) only when using a binary protocol, and vice-versa.","Do not reuse a connection across protocol changes; rebuild it with the desired protocol."],"exampleFix":"// before - mixing types on one connection\nconn.send(\"Method\", \"text\");\nconn.send(\"Method\", new Uint8Array([1,2,3]));\n// after - keep the buffer homogeneous for the chosen protocol\nconn.send(\"Method\", \"text\");\nconn.send(\"Method\", \"more-text\");","handlingStrategy":"type-guard","validationCode":"function ensureHomogeneous(buffer: unknown[], next: string | ArrayBuffer): void {\n  if (buffer.length && typeof buffer[0] !== typeof next) {\n    throw new TypeError(\"Refusing to mix string and ArrayBuffer payloads\");\n  }\n}","typeGuard":"function isStringPayload(p: unknown): p is string {\n  return typeof p === \"string\";\n}","tryCatchPattern":null,"preventionTips":["Use a single hub protocol per connection (Text -> strings, Binary -> ArrayBuffer).","Never switch protocols on a live connection.","Encode payloads to match the negotiated transfer format."],"tags":["transport","send","buffer","binary","typescript"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}