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

  1. Pass the underlying ArrayBuffer: use input.buffer where input is a Uint8Array (mind byteOffset/byteLength for subarrays).
  2. Construct a fresh ArrayBuffer copy: new Uint8Array(otherView).buffer is NOT safe if offset≠0; use otherView.slice().buffer instead.
  3. 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

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


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)