Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added websockets for sending logs if available #489

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/logs/*
/.git/*
npm-debug.log
DS_store
.DS_store
cypress
tsconfig.tsbuildinfo
.rollup.cache
Expand Down
85 changes: 58 additions & 27 deletions build/UserALEWebExtension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class Configuration {
this.useraleVersion = null;
this.userId = null;
this.version = null;
this.websocketsEnabled = false;
// Call the initialization method only if it's the first time instantiating
if (Configuration.instance === null) {
this.initialize();
Expand Down Expand Up @@ -1137,19 +1138,25 @@ function sendOnClose(logs, config) {
return;
}
if (logs.length > 0) {
const headers = new Headers();
headers.set("Content-Type", "applicaiton/json;charset=UTF-8");
if (config.authHeader) {
headers.set("Authorization", config.authHeader.toString());
if (config.websocketsEnabled) {
const data = JSON.stringify(logs);
wsock.send(data);
}
else {
const headers = new Headers();
headers.set("Content-Type", "applicaiton/json;charset=UTF-8");
if (config.authHeader) {
headers.set("Authorization", config.authHeader.toString());
}
fetch(config.url, {
keepalive: true,
method: "POST",
headers: headers,
body: JSON.stringify(logs),
}).catch((error) => {
console.error(error);
});
}
fetch(config.url, {
keepalive: true,
method: "POST",
headers: headers,
body: JSON.stringify(logs),
}).catch((error) => {
console.error(error);
});
logs.splice(0); // clear log queue
}
});
Expand All @@ -1163,24 +1170,29 @@ function sendOnClose(logs, config) {
*/
// @todo expose config object to sendLogs replate url with config.url
function sendLogs(logs, config, retries) {
const req = new XMLHttpRequest();
const data = JSON.stringify(logs);
req.open("POST", config.url);
if (config.authHeader) {
req.setRequestHeader("Authorization", typeof config.authHeader === "function"
? config.authHeader()
: config.authHeader);
if (config.websocketsEnabled) {
wsock.send(data);
}
req.setRequestHeader("Content-type", "application/json;charset=UTF-8");
if (config.headers) {
Object.entries(config.headers).forEach(([header, value]) => {
req.setRequestHeader(header, value);
});
else {
const req = new XMLHttpRequest();
req.open("POST", config.url);
if (config.authHeader) {
req.setRequestHeader("Authorization", typeof config.authHeader === "function"
? config.authHeader()
: config.authHeader);
}
req.setRequestHeader("Content-type", "application/json;charset=UTF-8");
if (config.headers) {
Object.entries(config.headers).forEach(([header, value]) => {
req.setRequestHeader(header, value);
});
}
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status !== 200) ;
};
req.send(data);
}
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status !== 200) ;
};
req.send(data);
}

/*
Expand All @@ -1207,10 +1219,12 @@ window.onload = function () {
endLoadTimestamp = Date.now();
};
let started = false;
let wsock;
config.update({
useraleVersion: version$1,
});
initPackager(logs, config);
getWebsocketsEnabled(config);
if (config.autostart) {
setup(config);
}
Expand Down Expand Up @@ -1239,6 +1253,23 @@ function setup(config) {
}, 100);
}
}
/**
* Checks to see if the specified backend URL supporsts Websockets
* and updates the config accordingly
*/
function getWebsocketsEnabled(config) {
wsock = new WebSocket(config.url.replace("http://", "ws://"));
wsock.onerror = () => {
console.log("no websockets detected");
};
wsock.onopen = () => {
console.log("connection established with websockets");
config.websocketsEnabled = true;
};
wsock.onclose = () => {
sendOnClose(logs, config);
};
}
// Export the Userale API
const version = version$1;
/**
Expand Down
85 changes: 58 additions & 27 deletions build/UserALEWebExtension/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class Configuration {
this.useraleVersion = null;
this.userId = null;
this.version = null;
this.websocketsEnabled = false;
// Call the initialization method only if it's the first time instantiating
if (Configuration.instance === null) {
this.initialize();
Expand Down Expand Up @@ -1084,19 +1085,25 @@ function sendOnClose(logs, config) {
return;
}
if (logs.length > 0) {
const headers = new Headers();
headers.set("Content-Type", "applicaiton/json;charset=UTF-8");
if (config.authHeader) {
headers.set("Authorization", config.authHeader.toString());
if (config.websocketsEnabled) {
const data = JSON.stringify(logs);
wsock.send(data);
}
else {
const headers = new Headers();
headers.set("Content-Type", "applicaiton/json;charset=UTF-8");
if (config.authHeader) {
headers.set("Authorization", config.authHeader.toString());
}
fetch(config.url, {
keepalive: true,
method: "POST",
headers: headers,
body: JSON.stringify(logs),
}).catch((error) => {
console.error(error);
});
}
fetch(config.url, {
keepalive: true,
method: "POST",
headers: headers,
body: JSON.stringify(logs),
}).catch((error) => {
console.error(error);
});
logs.splice(0); // clear log queue
}
});
Expand All @@ -1110,24 +1117,29 @@ function sendOnClose(logs, config) {
*/
// @todo expose config object to sendLogs replate url with config.url
function sendLogs(logs, config, retries) {
const req = new XMLHttpRequest();
const data = JSON.stringify(logs);
req.open("POST", config.url);
if (config.authHeader) {
req.setRequestHeader("Authorization", typeof config.authHeader === "function"
? config.authHeader()
: config.authHeader);
if (config.websocketsEnabled) {
wsock.send(data);
}
req.setRequestHeader("Content-type", "application/json;charset=UTF-8");
if (config.headers) {
Object.entries(config.headers).forEach(([header, value]) => {
req.setRequestHeader(header, value);
});
else {
const req = new XMLHttpRequest();
req.open("POST", config.url);
if (config.authHeader) {
req.setRequestHeader("Authorization", typeof config.authHeader === "function"
? config.authHeader()
: config.authHeader);
}
req.setRequestHeader("Content-type", "application/json;charset=UTF-8");
if (config.headers) {
Object.entries(config.headers).forEach(([header, value]) => {
req.setRequestHeader(header, value);
});
}
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status !== 200) ;
};
req.send(data);
}
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status !== 200) ;
};
req.send(data);
}

/*
Expand All @@ -1154,10 +1166,12 @@ window.onload = function () {
endLoadTimestamp = Date.now();
};
let started = false;
let wsock;
config.update({
useraleVersion: version,
});
initPackager(logs, config);
getWebsocketsEnabled(config);
if (config.autostart) {
setup(config);
}
Expand Down Expand Up @@ -1186,6 +1200,23 @@ function setup(config) {
}, 100);
}
}
/**
* Checks to see if the specified backend URL supporsts Websockets
* and updates the config accordingly
*/
function getWebsocketsEnabled(config) {
wsock = new WebSocket(config.url.replace("http://", "ws://"));
wsock.onerror = () => {
console.log("no websockets detected");
};
wsock.onopen = () => {
console.log("connection established with websockets");
config.websocketsEnabled = true;
};
wsock.onclose = () => {
sendOnClose(logs, config);
};
}
/**
* Updates the current configuration
* object with the provided values.
Expand Down
85 changes: 58 additions & 27 deletions build/UserALEWebExtension/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class Configuration {
this.useraleVersion = null;
this.userId = null;
this.version = null;
this.websocketsEnabled = false;
// Call the initialization method only if it's the first time instantiating
if (Configuration.instance === null) {
this.initialize();
Expand Down Expand Up @@ -1084,19 +1085,25 @@ function sendOnClose(logs, config) {
return;
}
if (logs.length > 0) {
const headers = new Headers();
headers.set("Content-Type", "applicaiton/json;charset=UTF-8");
if (config.authHeader) {
headers.set("Authorization", config.authHeader.toString());
if (config.websocketsEnabled) {
const data = JSON.stringify(logs);
wsock.send(data);
}
else {
const headers = new Headers();
headers.set("Content-Type", "applicaiton/json;charset=UTF-8");
if (config.authHeader) {
headers.set("Authorization", config.authHeader.toString());
}
fetch(config.url, {
keepalive: true,
method: "POST",
headers: headers,
body: JSON.stringify(logs),
}).catch((error) => {
console.error(error);
});
}
fetch(config.url, {
keepalive: true,
method: "POST",
headers: headers,
body: JSON.stringify(logs),
}).catch((error) => {
console.error(error);
});
logs.splice(0); // clear log queue
}
});
Expand All @@ -1110,24 +1117,29 @@ function sendOnClose(logs, config) {
*/
// @todo expose config object to sendLogs replate url with config.url
function sendLogs(logs, config, retries) {
const req = new XMLHttpRequest();
const data = JSON.stringify(logs);
req.open("POST", config.url);
if (config.authHeader) {
req.setRequestHeader("Authorization", typeof config.authHeader === "function"
? config.authHeader()
: config.authHeader);
if (config.websocketsEnabled) {
wsock.send(data);
}
req.setRequestHeader("Content-type", "application/json;charset=UTF-8");
if (config.headers) {
Object.entries(config.headers).forEach(([header, value]) => {
req.setRequestHeader(header, value);
});
else {
const req = new XMLHttpRequest();
req.open("POST", config.url);
if (config.authHeader) {
req.setRequestHeader("Authorization", typeof config.authHeader === "function"
? config.authHeader()
: config.authHeader);
}
req.setRequestHeader("Content-type", "application/json;charset=UTF-8");
if (config.headers) {
Object.entries(config.headers).forEach(([header, value]) => {
req.setRequestHeader(header, value);
});
}
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status !== 200) ;
};
req.send(data);
}
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status !== 200) ;
};
req.send(data);
}

/*
Expand All @@ -1154,10 +1166,12 @@ window.onload = function () {
endLoadTimestamp = Date.now();
};
let started = false;
let wsock;
config.update({
useraleVersion: version,
});
initPackager(logs, config);
getWebsocketsEnabled(config);
if (config.autostart) {
setup(config);
}
Expand Down Expand Up @@ -1186,6 +1200,23 @@ function setup(config) {
}, 100);
}
}
/**
* Checks to see if the specified backend URL supporsts Websockets
* and updates the config accordingly
*/
function getWebsocketsEnabled(config) {
wsock = new WebSocket(config.url.replace("http://", "ws://"));
wsock.onerror = () => {
console.log("no websockets detected");
};
wsock.onopen = () => {
console.log("connection established with websockets");
config.websocketsEnabled = true;
};
wsock.onclose = () => {
sendOnClose(logs, config);
};
}
/**
* Updates the current configuration
* object with the provided values.
Expand Down
Loading
Loading