Skip to content

Commit 45be9f8

Browse files
committed
fix: error
1 parent 5084e26 commit 45be9f8

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

src/protocols/DisconnectionOrigin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export enum DisconnectionOrigin {
22
WEBSOCKET_AUTH_RENEWAL = "websocket/auth-renewal",
33
USER_CONNECTION_CLOSED = "user/connection-closed",
44
NETWORK_ERROR = "network/error",
5+
PAYLOAD_MAX_SIZE_EXCEEDED = "payload/max-size-exceeded",
56
}

src/protocols/WebSocket.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,13 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
116116
});
117117
}
118118

119-
this.client.onopen = () => {
119+
this.client.onopen = async () => {
120120
this.clientConnected();
121121

122122
this.setupPingPong();
123123

124+
await this.getMaxPayloadSize();
125+
124126
return resolve();
125127
};
126128

@@ -241,9 +243,15 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
241243
* @param {Object} payload
242244
*/
243245
send(request: RequestPayload, options: JSONObject = {}) {
246+
if (this.maxPayloadSize !== null && Buffer.byteLength(JSON.stringify(request), 'utf8') > this.maxPayloadSize) {
247+
248+
const error: any= new Error(
249+
`Payload size exceeded the maximum allowed by the server ${this.maxPayloadSize} bytes`
250+
);
251+
252+
this.emit("networkError", { error });
253+
this.clientDisconnected(DisconnectionOrigin.PAYLOAD_MAX_SIZE_EXCEEDED);
244254

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

@@ -351,7 +359,7 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
351359
// If we were waiting for a pong that never occured before the next ping cycle we throw an error
352360
if (this.waitForPong) {
353361
const error: any = new Error(
354-
"Kuzzle does'nt respond to ping. Connection lost."
362+
"Kuzzle doesn't respond to ping. Connection lost."
355363
);
356364
error.status = 503;
357365

@@ -368,10 +376,13 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
368376
}
369377
}, this._pingInterval);
370378
}
379+
/**
380+
* Get the maximum payload size allowed by the server
381+
* Stores the value in `this.maxPayloadSize`
382+
**/
371383
async getMaxPayloadSize() {
372384
return new Promise((resolve, reject) => {
373385
const originalOnMessage = this.client.onmessage;
374-
375386
this.client.onmessage = (payload) => {
376387
try {
377388
const data = JSON.parse(payload.data || payload);

src/utils/browser.js

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)