dotnet/aspnetcore · error · Error
Invalid input for MessagePack hub protocol. Expected an…
Error message
Invalid input for MessagePack hub protocol. Expected an ArrayBuffer.
What it means
Thrown by MessagePackHubProtocol.parseMessages if the input is not an ArrayBuffer. The HubProtocol interface nominally allows string, but the MessagePack implementation requires binary; isArrayBuffer(input) returns false for typed arrays, SharedArrayBuffer, ArrayBufferView (e.g. Uint8Array), Node Buffers, or strings.
Solutions
- Pass the underlying ArrayBuffer: use input.buffer where input is a Uint8Array (mind byteOffset/byteLength for subarrays).
- Construct a fresh ArrayBuffer copy: new Uint8Array(otherView).buffer is NOT safe if offset≠0; use otherView.slice().buffer instead.
- If you have a string, you are using the wrong protocol — use the JsonHubProtocol instead.
Example fix
// before protocol.parseMessages(uint8ArrayFromTransport, logger); // isArrayBuffer=false → throws // after — hand over the true ArrayBuffer const ab = uint8Array.byteOffset === 0 ? uint8Array.buffer : uint8Array.slice().buffer; protocol.parseMessages(ab, logger);
Defensive patterns
Strategy: type-guard
Validate before calling
function toArrayBuffer(input: ArrayBuffer | ArrayBufferView): ArrayBuffer {
if (input instanceof ArrayBuffer) return input;
const view = input as ArrayBufferView;
if (view.byteOffset === 0 && view.byteLength === view.buffer.byteLength) return view.buffer;
return (input as Uint8Array).slice().buffer;
}
// usage:
protocol.parseMessages(toArrayBuffer(received), logger); Type guard
function isArrayBufferLike(v: unknown): v is ArrayBuffer {
return v instanceof ArrayBuffer;
} Try / catch
try {
protocol.parseMessages(input as ArrayBuffer, logger);
} catch (e) {
if (/Expected an ArrayBuffer/.test((e as Error).message)) {
protocol.parseMessages(toArrayBuffer(input as any), logger);
} else throw e;
} Prevention
- Normalize all incoming bytes to ArrayBuffer at the transport boundary.
- Use JsonHubProtocol if your data is text.
- Never pass Uint8Array/DataView/Node Buffer directly to MessagePackHubProtocol.
When it happens
Trigger: Calling parseMessages with a Uint8Array, a DataView, a Node.js Buffer, a string, or any ArrayBufferView instead of a true ArrayBuffer. Most commonly hit by code that obtains message bytes from a source that yields a view rather than the underlying buffer.
Common situations: Integrating the MessagePack protocol into a custom transport that yields Uint8Array chunks; feeding decrypted/decompressed data held in a Uint8Array; porting code from the JSON protocol where string was acceptable.
Related errors
- Cannot read message size.
- could not find global
- Error reading length header.
- Error reading MessagePack data.
- Error writing MessagePack data.
AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11).
Data as JSON: /api/errors/842fff606255ff97.
Report an issue: GitHub.
Appendix: source
Thrown at src/SignalR/clients/ts/signalr-protocol-msgpack/src/MessagePackHubProtocol.ts:76
messagePackOptions.extensionCodec,
messagePackOptions.context,
messagePackOptions.maxStrLength,
messagePackOptions.maxBinLength,
messagePackOptions.maxArrayLength,
messagePackOptions.maxMapLength,
messagePackOptions.maxExtLength,
);
}
/** Creates an array of HubMessage objects from the specified serialized representation.
*
* @param {ArrayBuffer} input An ArrayBuffer containing the serialized representation.
* @param {ILogger} logger A logger that will be used to log messages that occur during parsing.
*/
public parseMessages(input: ArrayBuffer, logger: ILogger): HubMessage[] {
// The interface does allow "string" to be passed in, but this implementation does not. So let's throw a useful error.
if (!(isArrayBuffer(input))) {
throw new Error("Invalid input for MessagePack hub protocol. Expected an ArrayBuffer.");
}
if (logger === null) {
logger = NullLogger.instance;
}
const messages = BinaryMessageFormat.parse(input);
const hubMessages = [];
for (const message of messages) {
const parsedMessage = this._parseMessage(message, logger);
// Can be null for an unknown message. Unknown message is logged in parseMessage
if (parsedMessage) {
hubMessages.push(parsedMessage);
}
}
return hubMessages;View on GitHub (pinned to 3600ca084e)