Skip to content

Commit

Permalink
Batching limit fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RNEvok committed Jan 23, 2024
1 parent b8b9eb5 commit 2b02a9b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
20 changes: 11 additions & 9 deletions Connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class Connector {
}
};

private _encryptData(json: any) {
private _encryptData(json: any): {result: string, ok: boolean} {
if (!this._encryption)
return jsonStringifyKeepMeta(json);

Expand Down Expand Up @@ -261,13 +261,13 @@ export class Connector {
// codebudConsoleLog(`Intercepted request ${requestId}`, request);
const timestamp = moment().valueOf();
const encryptedData = this._encryptData({request, requestId, timestamp});
this._socket?.emit(SOCKET_EVENTS_EMIT.SAVE_INTERCEPTED_REQUEST, encryptedData);
encryptedData.ok && this._socket?.emit(SOCKET_EVENTS_EMIT.SAVE_INTERCEPTED_REQUEST, encryptedData.result);
},
onResponse: ({ response, request, requestId }: NetworkInterceptorOnResponsePayload) => {
// codebudConsoleLog(`Intercepted response ${requestId}`, response);
const timestamp = moment().valueOf();
const encryptedData = this._encryptData({response, request, requestId, timestamp});
this._socket?.emit(SOCKET_EVENTS_EMIT.SAVE_INTERCEPTED_RESPONSE, encryptedData);
encryptedData.ok && this._socket?.emit(SOCKET_EVENTS_EMIT.SAVE_INTERCEPTED_RESPONSE, encryptedData.result);
}
});
};
Expand Down Expand Up @@ -373,7 +373,7 @@ export class Connector {

this._sendReduxStateBatchingTimer = setTimeout(() => {
const encryptedData = this._encryptData({state: this._currentReduxStateCopy});
this._socket.emit(SOCKET_EVENTS_EMIT.SAVE_REDUX_STATE_COPY, encryptedData);
encryptedData.ok && this._socket.emit(SOCKET_EVENTS_EMIT.SAVE_REDUX_STATE_COPY, encryptedData.result);
}, batchingTimeMs);
}
}
Expand All @@ -387,14 +387,15 @@ export class Connector {
if (this._socket.connected) {
const timestamp = moment().valueOf();
const actionId = Connector._currentInterceptedReduxActionId++;
this._currentReduxActionsBatch.push({actionId: `RA_${actionId}`, action, timestamp});
const reduxActionData = {actionId: `RA_${actionId}`, action, timestamp};
jsonStringifyKeepMeta(reduxActionData).ok && this._currentReduxActionsBatch.push(reduxActionData);

if (this._sendReduxActionsBatchingTimer)
clearTimeout(this._sendReduxActionsBatchingTimer);

this._sendReduxActionsBatchingTimer = setTimeout(() => {
const encryptedData = this._encryptData({actions: this._currentReduxActionsBatch});
this._socket?.emit(SOCKET_EVENTS_EMIT.SAVE_REDUX_ACTIONS_BATCH, encryptedData);
encryptedData.ok && this._socket?.emit(SOCKET_EVENTS_EMIT.SAVE_REDUX_ACTIONS_BATCH, encryptedData.result);
this._currentReduxActionsBatch = [];
}, batchingTimeMs);
}
Expand All @@ -406,14 +407,15 @@ export class Connector {
if (this._socket.connected) {
const timestamp = moment().valueOf();
const storageActionId = Connector._currentInterceptedStorageActionId++;
this._currentStorageActionsBatch.push({storageActionId: `SA_${storageActionId}`, action, data, timestamp});
const storageActionData = {storageActionId: `SA_${storageActionId}`, action, data, timestamp};
jsonStringifyKeepMeta(storageActionData).ok && this._currentStorageActionsBatch.push(storageActionData);

if (this._sendStorageActionsBatchingTimer)
clearTimeout(this._sendStorageActionsBatchingTimer);

this._sendStorageActionsBatchingTimer = setTimeout(() => {
const encryptedData = this._encryptData({storageActions: this._currentStorageActionsBatch});
this._socket?.emit(SOCKET_EVENTS_EMIT.SAVE_INTERCEPTED_STORAGE_ACTIONS_BATCH, encryptedData);
encryptedData.ok && this._socket?.emit(SOCKET_EVENTS_EMIT.SAVE_INTERCEPTED_STORAGE_ACTIONS_BATCH, encryptedData.result);
this._currentStorageActionsBatch = [];
}, this._storageActionsBatchingTimeMs);
}
Expand Down Expand Up @@ -443,7 +445,7 @@ export class Connector {
const capturedEventId = Connector._currentCapturedEventId++;

const encryptedData = this._encryptData({timestamp, capturedEventId: `UCE_${capturedEventId}`, title, data});
this._socket?.emit(SOCKET_EVENTS_EMIT.CAPTURE_EVENT, encryptedData);
encryptedData.ok && this._socket?.emit(SOCKET_EVENTS_EMIT.CAPTURE_EVENT, encryptedData.result);
}
}

Expand Down
10 changes: 6 additions & 4 deletions encryption/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ export class EncryptionPlugin {
return randomBytes(box.nonceLength);
};

public encryptData(json: any) {
public encryptData(json: any): {result: string, ok: boolean} {
try {
if (!this._sharedKey)
throw new Error("Shared key not generated");

const nonce = this.newNonce();
const messageUint8 = decodeUTF8(jsonStringifyKeepMeta(json));
const jsonStringified = jsonStringifyKeepMeta(json);

const messageUint8 = decodeUTF8(jsonStringified.result);

const encrypted = box.after(messageUint8, nonce, this._sharedKey);

Expand All @@ -26,10 +28,10 @@ export class EncryptionPlugin {
fullMessage.set(encrypted, nonce.length);

const base64FullMessage = encodeBase64(fullMessage);
return base64FullMessage;
return {result: base64FullMessage, ok: jsonStringified.ok};
} catch (e) {
codebudConsoleLog(e);
return JSON.stringify({msg: "Data encryption error"});
return {result: JSON.stringify({msg: "Data encryption error"}), ok: false};
}
};

Expand Down
8 changes: 4 additions & 4 deletions helpers/helperFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const getFormDataMeta = (fData: FormData) => {
}

// Custom JSON.stringify wrapper that keeps as much metadata as possible
export const jsonStringifyKeepMeta = (data: ObjectT<any>) => {
export const jsonStringifyKeepMeta = (data: ObjectT<any>): {result: string, ok: boolean} => {
const dataStringified = JSON.stringify(
data,
function(key, value) {
Expand All @@ -89,10 +89,10 @@ export const jsonStringifyKeepMeta = (data: ObjectT<any>) => {
);

if (payloadSizeValidator(dataStringified))
return dataStringified;
return {result: dataStringified, ok: true};

const message = `Payload data was skipped (${CONFIG.PAYLOAD_LIMITS.MAX_KB_SIZE} Kb limit exceeded)`;
codebudConsoleWarn(message);
return JSON.stringify({message});

return {result: JSON.stringify({message}), ok: false};
}

0 comments on commit 2b02a9b

Please sign in to comment.