Skip to content

Commit

Permalink
chore: lint and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-mauran committed Nov 20, 2024
1 parent f278340 commit 8f34d79
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions src/protocols/WebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,11 @@ 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(
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`
);

Expand Down Expand Up @@ -379,35 +381,35 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
/**
* 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;
}

// If not related to `getMaxPayloadSize`, pass to the original handler
if (originalOnMessage) {
originalOnMessage(payload);
}
} catch (error) {
reject(error);
}
};
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;
}

Check failure on line 405 in src/protocols/WebSocket.ts

View workflow job for this annotation

GitHub Actions / Lint (18)

Delete `⏎`

Check failure on line 405 in src/protocols/WebSocket.ts

View workflow job for this annotation

GitHub Actions / Lint (20)

Delete `⏎`
} catch (error) {
reject(error);
}
};

// Send the request
this.send({ controller: "server", action: "getConfig" });
// Send the request
this.send({ action: "getConfig", controller: "server" });
});
}
}

0 comments on commit 8f34d79

Please sign in to comment.