Skip to content
This repository was archived by the owner on Sep 24, 2024. It is now read-only.

Commit f19cb23

Browse files
Handle invalid SDK key
1 parent 9d10548 commit f19cb23

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/client/client.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ document.addEventListener("readystatechange", () => {
3636
payloadSizeDisplay.textContent = "{measure and fill json size in KB}";
3737
dataFileContentDisplay.textContent = "{fill json}";
3838
});
39-
4039
manualPullButton.addEventListener("click", e => {
4140
e.preventDefault();
4241
socket.emit("datafile-pull", sdkKey, socketId);
4342
});
44-
4543
sdkKeyField.addEventListener("change", e => {
4644
console.log("Ignoring SDK", sdkKey);
4745
socket.emit("ignoring-sdk-key", sdkKey);
@@ -61,15 +59,13 @@ const updatePayloadSize = obj => {
6159

6260
payloadSizeDisplay.textContent = `${kb} KB`;
6361
};
64-
6562
const updateJsonDisplay = obj => {
6663
const cleaned = JSON.stringify(obj, null, 2);
6764

6865
dataFileContentDisplay.textContent = cleaned.replace(/^[\t ]*"[^:\n\r]+(?<!\\)":/gm, function (match) {
6966
return match.replace(/"/g, "");
7067
});
7168
};
72-
7369
const getSizeInBytes = obj => {
7470
let str;
7571
if (typeof obj === 'string') {

src/server/server.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ const io = require("socket.io")(serverPort, {
1919
const fs = require('fs');
2020
const readDataFileFromDisk = sdkKey => {
2121
console.log("Reading datafile from disk", sdkKey);
22-
return JSON.parse(fs.readFileSync(`./datafiles/${sdkKey}.json`).toString());
22+
const path = `./datafiles/${sdkKey}.json`;
23+
if (!fs.existsSync(path)) {
24+
return;
25+
}
26+
const buffer = fs.readFileSync(path);
27+
if (!buffer) {
28+
return;
29+
}
30+
return JSON.parse(buffer.toString());
2331
};
2432

2533
const sendDataFile = (dataFile, toClientId, toAllClientsWithSdkKey) => {
@@ -49,7 +57,11 @@ io.on("connection", s => {
4957
if (isNullOrWhitespace(sdkKey)) {
5058
return;
5159
}
52-
sendDataFile(readDataFileFromDisk(sdkKey), socketId, null);
60+
const dataFile =readDataFileFromDisk(sdkKey);
61+
if (!dataFile) {
62+
return;
63+
}
64+
sendDataFile(dataFile, socketId, null);
5365
});
5466
});
5567

0 commit comments

Comments
 (0)