Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Websocket propper error when the payload size is too big #756

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/protocols/DisconnectionOrigin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum DisconnectionOrigin {
WEBSOCKET_AUTH_RENEWAL = "websocket/auth-renewal",
USER_CONNECTION_CLOSED = "user/connection-closed",
NETWORK_ERROR = "network/error",
PAYLOAD_MAX_SIZE_EXCEEDED = "payload/max-size-exceeded",
}
56 changes: 54 additions & 2 deletions src/protocols/WebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { JSONObject } from "../types";
import { RequestPayload } from "../types/RequestPayload";
import HttpProtocol from "./Http";
import { DisconnectionOrigin } from "./DisconnectionOrigin";
import { parseSize } from "../utils/parseSize";

/**
* WebSocket protocol used to connect to a Kuzzle server.
Expand All @@ -15,6 +16,7 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
private options: any;
private client: any;
private lasturl: any;
private maxPayloadSize: number;
private ping: any;
private waitForPong: boolean;
private pingIntervalId: ReturnType<typeof setInterval>;
Expand Down Expand Up @@ -77,6 +79,7 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
typeof options.pingInterval === "number" ? options.pingInterval : 2000;
this.client = null;
this.lasturl = null;
this.maxPayloadSize = null;
}

/**
Expand Down Expand Up @@ -113,11 +116,13 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
});
}

this.client.onopen = () => {
this.client.onopen = async () => {
this.clientConnected();

this.setupPingPong();

await this.getMaxPayloadSize();

return resolve();
};

Expand Down Expand Up @@ -238,6 +243,20 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
* @param {Object} payload
*/
send(request: RequestPayload, options: JSONObject = {}) {
if (
this.maxPayloadSize !== null &&
Buffer.byteLength(JSON.stringify(request), "utf8") > this.maxPayloadSize
) {
const error: any = new Error(
`Payload size exceeded the maximum allowed by the server ${this.maxPayloadSize} bytes`
);

this.emit("networkError", { error });
this.clientDisconnected(DisconnectionOrigin.PAYLOAD_MAX_SIZE_EXCEEDED);

return;
}

if (!this.client || this.client.readyState !== this.client.OPEN) {
return;
}
Expand Down Expand Up @@ -342,7 +361,7 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
// If we were waiting for a pong that never occured before the next ping cycle we throw an error
if (this.waitForPong) {
const error: any = new Error(
"Kuzzle does'nt respond to ping. Connection lost."
"Kuzzle doesn't respond to ping. Connection lost."
);
error.status = 503;

Expand All @@ -359,4 +378,37 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
}
}, this._pingInterval);
}
/**
* Get the maximum payload size allowed by the server
* Stores the value in `this.maxPayloadSize`
**/
async getMaxPayloadSize() {
return new Promise((resolve, reject) => {
const originalOnMessage = this.client.onmessage;
this.client.onmessage = (payload) => {
try {
const data = JSON.parse(payload.data || payload);

// Check if the message corresponds to the `getMaxPayloadSize` response
if (
data.result &&
data.result.server &&
data.result.server.maxRequestSize
) {
this.maxPayloadSize = parseSize(data.result.server.maxRequestSize);

// Restore the original `onmessage` handler
this.client.onmessage = originalOnMessage;
resolve(this.maxPayloadSize);
return;
}
} catch (error) {
reject(error);
}
};

// Send the request
this.send({ action: "getConfig", controller: "server" });
});
}
}
39 changes: 39 additions & 0 deletions src/utils/parseSize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function parseSize(size) {
const units = {
b: 1, // Bytes
kb: 1024, // Kilobytes
mb: 1024 * 1024, // Megabytes
// eslint-disable-next-line sort-keys
gb: 1024 * 1024 * 1024, // Gigabytes
};

if (typeof size !== "string") {
throw new Error(
`Invalid size input: expected a string, got ${typeof size}`
);
}

// Extract numeric value and unit
const match = /^(\d+(?:\.\d+)?)(b|kb|mb|gb)?$/i.exec(size.trim());
if (!match) {
throw new Error(
`Invalid size format: "${size}". Expected formats like "2MB", "500KB", "3GB", etc.`
);
}

const value = parseFloat(match[1]); // Get the numeric part
const unit = (match[2] || "b").toLowerCase(); // Default to bytes if no unit is provided

if (!units[unit]) {
throw new Error(
`Unknown size unit: "${unit}". Allowed units are B, KB, MB, GB.`
);
}

// Convert to bytes
return value * units[unit];
}

module.exports = {
parseSize,
};
Loading