Skip to content

Commit

Permalink
refactor(curl): use const/let instead of var, strict equal
Browse files Browse the repository at this point in the history
  • Loading branch information
StephaneBour committed Jan 15, 2024
1 parent aab6132 commit f1db108
Showing 1 changed file with 56 additions and 59 deletions.
115 changes: 56 additions & 59 deletions src/curl.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,72 @@
(function () {
var global = window;
if (!global.sense)
global.sense = {};
const global = window;
if (!global.sense)
global.sense = {};

function detectCURL(text) {
// returns true if text matches a curl request
if (!text) return false;
return text.match(/^\s*?curl\s+(-X[A-Z]+)?\s*['"]?.*?['"]?(\s*$|\s+?-d\s*?['"])/);
function detectCURL(text) {
// returns true if text matches a curl request
if (!text) return false;
return text.match(/^\s*?curl\s+(-X[A-Z]+)?\s*['"]?.*?['"]?(\s*$|\s+?-d\s*?['"])/);

}
}

function parseCURL(text) {
var matches = text.match(/^\s*?curl\s+(?:-s\s+)?(-X\s*[A-Z]+)?\s*/);
var ret = {
method: "",
server: "",
endpoint: "",
data: ""
};
if (matches[1]) {
ret.method = matches[1].substring(2).trim(); // strip -X
}
text = text.substring(matches[0].length); // strip everything so far.
if (text.length == 0) return ret;
if (text[0] == '"') {
matches = text.match(/^"([^"]*)"/);
}
else if (text[0] == "'") {
matches = text.match(/^'([^']*)'/);
}
else {
matches = text.match(/^(\S*)/);
}
function parseCURL(text) {
let matches = text.match(/^\s*?curl\s+(?:-s\s+)?(-X\s*[A-Z]+)?\s*/);
const ret = {
method: "",
server: "",
endpoint: "",
data: ""
};
if (matches[1]) {
ret.method = matches[1].substring(2).trim(); // strip -X
}
text = text.substring(matches[0].length); // strip everything so far.
if (text.length === 0) return ret;
if (text[0] === '"') {
matches = text.match(/^"([^"]*)"/);
} else if (text[0] === "'") {
matches = text.match(/^'([^']*)'/);
} else {
matches = text.match(/^(\S*)/);
}

if (!matches) return ret;
var url = matches[1];
if (!matches) return ret;
let url = matches[1];

if (!url.match(/:\/\//)) url = "http://" + url; // inject http as curl does
if (!url.match(/:\/\//)) url = "http://" + url; // inject http as curl does

var urlAnchor = document.createElement("a");
urlAnchor.href = url;
const urlAnchor = document.createElement("a");
urlAnchor.href = url;

ret.server = (urlAnchor.protocol || "http") + "//" + urlAnchor.hostname;
if (urlAnchor.port && urlAnchor.port != 0) ret.server += ":" + urlAnchor.port;
ret.url = (urlAnchor.pathname || "") + (urlAnchor.search || "");
ret.server = (urlAnchor.protocol || "http") + "//" + urlAnchor.hostname;
if (urlAnchor.port && urlAnchor.port !== "0") ret.server += ":" + urlAnchor.port;
ret.url = (urlAnchor.pathname || "") + (urlAnchor.search || "");

text = text.substring(matches[0].length);
text = text.substring(matches[0].length);

// now search for -d
matches = text.match(/.*-d\s*?'/);
if (matches) {
ret.data = text.substring(matches[0].length).replace(/'\s*$/, '');
}
else {
matches = text.match(/.*-d\s*?"/);
if (matches) {
ret.data = text.substring(matches[0].length).replace(/"\s*$/, '');
ret.data = ret.data.replace(/\\(.)/gi, "$1");
}
}
// now search for -d
matches = text.match(/.*-d\s*?'/);
if (matches) {
ret.data = text.substring(matches[0].length).replace(/'\s*$/, '');
} else {
matches = text.match(/.*-d\s*?"/);
if (matches) {
ret.data = text.substring(matches[0].length).replace(/"\s*$/, '');
ret.data = ret.data.replace(/\\(.)/gi, "$1");
}
}

if (ret.data) {
ret.data = ret.data.trim();
}
if (ret.data) {
ret.data = ret.data.trim();
}

return ret;
}
return ret;
}


global.sense.curl = {};
global.sense.curl.parseCURL = parseCURL;
global.sense.curl.detectCURL = detectCURL;
global.sense.curl = {};
global.sense.curl.parseCURL = parseCURL;
global.sense.curl.detectCURL = detectCURL;

})();

0 comments on commit f1db108

Please sign in to comment.