-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement cookie management for the Node.js client
When setting the `withCredentials` option to `true`, the Node.js client will now include the cookies in the HTTP requests, making it easier to use it with cookie-based sticky sessions. Related: socketio/socket.io#3812
- Loading branch information
1 parent
7195c0f
commit 5fc88a6
Showing
5 changed files
with
219 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,5 @@ export function XHR(opts) { | |
} catch (e) {} | ||
} | ||
} | ||
|
||
export function createCookieJar() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,102 @@ | ||
import * as XMLHttpRequestModule from "xmlhttprequest-ssl"; | ||
|
||
export const XHR = XMLHttpRequestModule.default || XMLHttpRequestModule; | ||
|
||
export function createCookieJar() { | ||
return new CookieJar(); | ||
} | ||
|
||
interface Cookie { | ||
name: string; | ||
value: string; | ||
expires?: Date; | ||
} | ||
|
||
/** | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie | ||
*/ | ||
export function parse(setCookieString: string): Cookie { | ||
const parts = setCookieString.split("; "); | ||
const i = parts[0].indexOf("="); | ||
|
||
if (i === -1) { | ||
return; | ||
} | ||
|
||
const name = parts[0].substring(0, i).trim(); | ||
|
||
if (!name.length) { | ||
return; | ||
} | ||
|
||
let value = parts[0].substring(i + 1).trim(); | ||
|
||
if (value.charCodeAt(0) === 0x22) { | ||
// remove double quotes | ||
value = value.slice(1, -1); | ||
} | ||
|
||
const cookie: Cookie = { | ||
name, | ||
value, | ||
}; | ||
|
||
for (let j = 1; j < parts.length; j++) { | ||
const subParts = parts[j].split("="); | ||
if (subParts.length !== 2) { | ||
continue; | ||
} | ||
const key = subParts[0].trim(); | ||
const value = subParts[1].trim(); | ||
switch (key) { | ||
case "Expires": | ||
cookie.expires = new Date(value); | ||
break; | ||
case "Max-Age": | ||
const expiration = new Date(); | ||
expiration.setUTCSeconds( | ||
expiration.getUTCSeconds() + parseInt(value, 10) | ||
); | ||
cookie.expires = expiration; | ||
break; | ||
default: | ||
// ignore other keys | ||
} | ||
} | ||
|
||
return cookie; | ||
} | ||
|
||
export class CookieJar { | ||
private cookies = new Map<string, Cookie>(); | ||
|
||
public parseCookies(xhr: any) { | ||
const values = xhr.getResponseHeader("set-cookie"); | ||
if (!values) { | ||
return; | ||
} | ||
values.forEach((value) => { | ||
const parsed = parse(value); | ||
if (parsed) { | ||
this.cookies.set(parsed.name, parsed); | ||
} | ||
}); | ||
} | ||
|
||
public addCookies(xhr: any) { | ||
const cookies = []; | ||
|
||
this.cookies.forEach((cookie, name) => { | ||
if (cookie.expires?.getTime() < Date.now()) { | ||
this.cookies.delete(name); | ||
} else { | ||
cookies.push(`${name}=${cookie.value}`); | ||
} | ||
}); | ||
|
||
if (cookies.length) { | ||
xhr.setDisableHeaderCheck(true); | ||
xhr.setRequestHeader("cookie", cookies.join("; ")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters