diff --git a/src/protocols/DisconnectionOrigin.ts b/src/protocols/DisconnectionOrigin.ts index 6d0530bf..05600c67 100644 --- a/src/protocols/DisconnectionOrigin.ts +++ b/src/protocols/DisconnectionOrigin.ts @@ -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", } diff --git a/src/protocols/WebSocket.ts b/src/protocols/WebSocket.ts index b116283a..fc7b4f5f 100644 --- a/src/protocols/WebSocket.ts +++ b/src/protocols/WebSocket.ts @@ -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(); }; @@ -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; } @@ -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; @@ -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); diff --git a/src/utils/browser.js b/src/utils/browser.js new file mode 100644 index 00000000..3d81f11b --- /dev/null +++ b/src/utils/browser.js @@ -0,0 +1,40 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.isBrowser = exports.getBrowserWindow = void 0; +function getBrowserWindow() { + let windowObject; + try { + windowObject || (windowObject = globalThis.window); + if (windowObject) { + return windowObject; + } + } + catch { + // Check next variable + } + try { + windowObject || (windowObject = global.window); + if (windowObject) { + return windowObject; + } + } + catch { + // Check next variable + } + try { + windowObject || (windowObject = window); + if (windowObject) { + return windowObject; + } + } + catch { + // return undefined + } +} +exports.getBrowserWindow = getBrowserWindow; +function isBrowser() { + const window = getBrowserWindow(); + return window !== undefined && window !== null && typeof window === "object"; +} +exports.isBrowser = isBrowser; +//# sourceMappingURL=browser.js.map \ No newline at end of file