Skip to content

Commit

Permalink
fix: error
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-mauran committed Nov 20, 2024
1 parent 5084e26 commit 45be9f8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
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",
}
21 changes: 16 additions & 5 deletions src/protocols/WebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,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 @@ -241,9 +243,15 @@ 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);

if (this.maxPayloadSize && Buffer.byteLength(JSON.stringify(request), 'utf8') > this.maxPayloadSize) {
this.clientNetworkError(new Error('Payload size exceeds the maximum allowed size of ' + this.maxPayloadSize));
return;
}

Expand Down Expand Up @@ -351,7 +359,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 @@ -368,10 +376,13 @@ 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);
Expand Down
40 changes: 40 additions & 0 deletions src/utils/browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45be9f8

Please sign in to comment.