Skip to content

Commit

Permalink
Merge pull request hoppscotch#88 from nelsontky/feat/include-cookies
Browse files Browse the repository at this point in the history
feat: include cookies
  • Loading branch information
AndrewBastin authored Oct 5, 2021
2 parents 83409b3 + 02a8dae commit 111ba43
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"permissions": [
"storage",
"tabs",
"cookies",
"<all_urls>"
],
"applications": {
Expand Down
70 changes: 68 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,60 @@ const convertDataURLToBlob = (dataurl: string) => {
return blob;
}

const parseCookieString: (str: string) => { [property: string]: string } = (
str
) =>
str
.split(";")
.map((value) => value.split("="))
.reduce((acc, curr) => {
if (!!curr[0] && !!curr[1]) {
acc[decodeURIComponent(curr[0].trim())] = decodeURIComponent(
curr[1].trim()
);
}
return acc;
}, {} as { [property: string]: string });

// keep track of the cookies we have to delete after the request is made
let cookiesToDelete: { url?: string; cookies?: string[] } = {};
const removeRequestCookies: () => Promise<void> = async () => {
if (!!cookiesToDelete.url && !!cookiesToDelete.cookies) {
for (const name of cookiesToDelete.cookies) {
await chrome.cookies.remove({
url: cookiesToDelete.url,
name,
});
}

cookiesToDelete = {};
}
};

const processRequestCookies: (reqConfig: any) => Promise<AxiosRequestConfig> =
async (reqConfig) => {

const cookie = Object.entries(reqConfig.headers || {}).find(
([header]) => header.toLowerCase() === "cookie"
);

if (!!cookie && !!reqConfig.url && typeof cookie[1] === "string") {
cookiesToDelete = { url: reqConfig.url, cookies: [] };
const parsedCookies = parseCookieString(cookie[1]);

for (const [name, value] of Object.entries(parsedCookies)) {
await chrome.cookies.set({
url: reqConfig.url,
name,
value,
});
cookiesToDelete.cookies.push(name);
}
}

return reqConfig;
};

const processRequestFormData: (reqConfig: any) => AxiosRequestConfig = (reqConfig) => {
if (reqConfig.formData || reqConfig.formFiles) {
const form = new FormData();
Expand All @@ -94,6 +148,13 @@ const processRequestFormData: (reqConfig: any) => AxiosRequestConfig = (reqConfi
return reqConfig as AxiosRequestConfig;
}

const processRequest: (reqConfig: any) => Promise<AxiosRequestConfig> = async (
reqConfig
) => {
await processRequestCookies(reqConfig);
return processRequestFormData(reqConfig);
};

function bufferToBase64(buffer: any) {
return btoa(new Uint8Array(buffer).reduce((data, byte)=> {
return data + String.fromCharCode(byte);
Expand All @@ -102,9 +163,11 @@ function bufferToBase64(buffer: any) {

const handleSendRequestMessage = async (config: any) => {
try {
const processedConfig = await processRequest(config);

if (config.wantsBinary) {
const r = await axios({
...processRequestFormData(config),
...processedConfig,
cancelToken: cancelSource.token,
responseType: 'arraybuffer'
});
Expand All @@ -126,7 +189,7 @@ const handleSendRequestMessage = async (config: any) => {
};
} else {
const res = await axios({
...processRequestFormData(config),
...processedConfig,

cancelToken: cancelSource.token,

Expand Down Expand Up @@ -173,6 +236,9 @@ const handleSendRequestMessage = async (config: any) => {
error: errorToObject(e)
}
};
} finally {
// remove the cookies set for this request
await removeRequestCookies();
}
}

Expand Down

0 comments on commit 111ba43

Please sign in to comment.