在打字稿中从 http-cookie-agent 访问 cookie
Access cookie from http-cookie-agent in typescript
我正在尝试从 axios 访问 cookie。我无法使用包装器,但可以直接使用 http-cookie-agent
。
我可以设置 cookie,但我无法再次检索它。
cookie 是这样设置的:
export function setupAxios(
basePath: string,
config?: { initialCookie?: string | Cookie }
) {
const jar = new CookieJar();
if (config) {
const { initialCookie } = config;
if (typeof initialCookie !== "undefined") {
jar.setCookieSync(initialCookie, basePath);
}
}
const httpsCookieAgent = new HttpsCookieAgent({
rejectUnauthorized: false,
cookies: { jar },
});
const axios = Axios.create({
withCredentials: true,
timeout: 1000,
httpsAgent: httpsCookieAgent,
});
return axios;
}
这样称呼:export const axios = setupAxios(basePath);
我有一个函数 clearCookies
,我在其中获取代理:
export function clearCookies() {
const httpsAgent = axios.defaults.httpsAgent as typeof HttpsCookieAgent;
console.log(httpsAgent);
>>> const options = httpsAgent.[Symbol(cookieOptions)] as CookieAgentOptions; <<< This I don't know how to accomplish.
const jar = options.cookies?.jar;
jar?.removeAllCookiesSync();
}
我可以注销字典了:
CookieAgent {
_events: [Object: null prototype] {
free: [Function (anonymous)],
newListener: [Function: maybeEnableKeylog]
},
_eventsCount: 2,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object: null prototype] { rejectUnauthorized: false, path: null },
requests: [Object: null prototype] {},
sockets: [Object: null prototype] {
'example.com:443::::::::false:::::::::::::': [ [TLSSocket] ]
},
freeSockets: [Object: null prototype] {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'lifo',
maxTotalSockets: Infinity,
totalSocketCount: 1,
maxCachedSessions: 100,
_sessionCache: {
map: {
'example.com:443::::::::false:::::::::::::': <Buffer 30 82 03 1f 02 01 01 02 02 03 04 04 02 13 02 04 20 aa 87 c2 15 2c 23 f2 11 6e f0 fe ce d5 b1 a3 b4 dd 53 01 08 76 ce c0 5e 7e 18 fe cd 11 2d 7c 89 04 ... 753 more bytes>
},
list: [ 'example.com:443::::::::false:::::::::::::' ]
},
[Symbol(kCapture)]: false,
[Symbol(cookieOptions)]: {
jar: CookieJar {
rejectPublicSuffixes: true,
enableLooseMode: false,
allowSpecialUseDomain: false,
store: { idx: {
'example.com': {
'/': {
id0: Cookie="id0=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpIjoiMTI3ZTA4NjYtNmJjZi00ZDAxLThjZTItNDRkNDg1OTI4MTRjIiwibCI6IjIwZDIxMzg4LWNjNzctNGM4Ni04N2JkLTRiNjE1NzU5OGEwYSJ9.SvP-cnS8docWhAGPKHm8YOjICmYp8BjWFFr-gv75WJE; Expires=Mon, 20 Jun 2022 19:00:59 GMT; Path=/; Secure; HttpOnly; hostOnly=true; aAge=9ms; cAge=352ms"
}
}
} },
prefixSecurity: 'silent',
_cloneSync: [Function (anonymous)],
_importCookiesSync: [Function (anonymous)],
getCookiesSync: [Function (anonymous)],
getCookieStringSync: [Function (anonymous)],
getSetCookieStringsSync: [Function (anonymous)],
removeAllCookiesSync: [Function (anonymous)],
setCookieSync: [Function (anonymous)],
serializeSync: [Function (anonymous)]
}
}
}
cookie 明明在那里,但我无法取回它。我已经导入
import { HttpsCookieAgent, CookieAgentOptions, CookieOptions } from "http-cookie-agent/http";
如果我能够设置 CookieAgentOptions
,我就可以访问 cookie(至少根据 vscode
)。但我不知道如何编写访问的代码
[Symbol(cookieOptions)]
httpsAgent.defaultPort
工作正常并得到 443,但我该如何处理符号?
循环的想法让我走上了正确的轨道。
我还必须删除类型转换。现在您可以访问 cookie
例如这样:
export function clearCookies() {
const httpsAgent = axios.defaults.httpsAgent;
for (let sym of Object.getOwnPropertySymbols(httpsAgent)) {
if (sym.toString() === "Symbol(cookieOptions)") {
const jar = httpsAgent[sym].jar as CookieJar;
jar.removeAllCookiesSync();
}
}
}
不过,我更喜欢直接访问符号的方法。
我正在尝试从 axios 访问 cookie。我无法使用包装器,但可以直接使用 http-cookie-agent
。
我可以设置 cookie,但我无法再次检索它。
cookie 是这样设置的:
export function setupAxios(
basePath: string,
config?: { initialCookie?: string | Cookie }
) {
const jar = new CookieJar();
if (config) {
const { initialCookie } = config;
if (typeof initialCookie !== "undefined") {
jar.setCookieSync(initialCookie, basePath);
}
}
const httpsCookieAgent = new HttpsCookieAgent({
rejectUnauthorized: false,
cookies: { jar },
});
const axios = Axios.create({
withCredentials: true,
timeout: 1000,
httpsAgent: httpsCookieAgent,
});
return axios;
}
这样称呼:export const axios = setupAxios(basePath);
我有一个函数 clearCookies
,我在其中获取代理:
export function clearCookies() {
const httpsAgent = axios.defaults.httpsAgent as typeof HttpsCookieAgent;
console.log(httpsAgent);
>>> const options = httpsAgent.[Symbol(cookieOptions)] as CookieAgentOptions; <<< This I don't know how to accomplish.
const jar = options.cookies?.jar;
jar?.removeAllCookiesSync();
}
我可以注销字典了:
CookieAgent {
_events: [Object: null prototype] {
free: [Function (anonymous)],
newListener: [Function: maybeEnableKeylog]
},
_eventsCount: 2,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object: null prototype] { rejectUnauthorized: false, path: null },
requests: [Object: null prototype] {},
sockets: [Object: null prototype] {
'example.com:443::::::::false:::::::::::::': [ [TLSSocket] ]
},
freeSockets: [Object: null prototype] {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
scheduling: 'lifo',
maxTotalSockets: Infinity,
totalSocketCount: 1,
maxCachedSessions: 100,
_sessionCache: {
map: {
'example.com:443::::::::false:::::::::::::': <Buffer 30 82 03 1f 02 01 01 02 02 03 04 04 02 13 02 04 20 aa 87 c2 15 2c 23 f2 11 6e f0 fe ce d5 b1 a3 b4 dd 53 01 08 76 ce c0 5e 7e 18 fe cd 11 2d 7c 89 04 ... 753 more bytes>
},
list: [ 'example.com:443::::::::false:::::::::::::' ]
},
[Symbol(kCapture)]: false,
[Symbol(cookieOptions)]: {
jar: CookieJar {
rejectPublicSuffixes: true,
enableLooseMode: false,
allowSpecialUseDomain: false,
store: { idx: {
'example.com': {
'/': {
id0: Cookie="id0=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpIjoiMTI3ZTA4NjYtNmJjZi00ZDAxLThjZTItNDRkNDg1OTI4MTRjIiwibCI6IjIwZDIxMzg4LWNjNzctNGM4Ni04N2JkLTRiNjE1NzU5OGEwYSJ9.SvP-cnS8docWhAGPKHm8YOjICmYp8BjWFFr-gv75WJE; Expires=Mon, 20 Jun 2022 19:00:59 GMT; Path=/; Secure; HttpOnly; hostOnly=true; aAge=9ms; cAge=352ms"
}
}
} },
prefixSecurity: 'silent',
_cloneSync: [Function (anonymous)],
_importCookiesSync: [Function (anonymous)],
getCookiesSync: [Function (anonymous)],
getCookieStringSync: [Function (anonymous)],
getSetCookieStringsSync: [Function (anonymous)],
removeAllCookiesSync: [Function (anonymous)],
setCookieSync: [Function (anonymous)],
serializeSync: [Function (anonymous)]
}
}
}
cookie 明明在那里,但我无法取回它。我已经导入
import { HttpsCookieAgent, CookieAgentOptions, CookieOptions } from "http-cookie-agent/http";
如果我能够设置 CookieAgentOptions
,我就可以访问 cookie(至少根据 vscode
)。但我不知道如何编写访问的代码
[Symbol(cookieOptions)]
httpsAgent.defaultPort
工作正常并得到 443,但我该如何处理符号?
循环的想法让我走上了正确的轨道。 我还必须删除类型转换。现在您可以访问 cookie 例如这样:
export function clearCookies() {
const httpsAgent = axios.defaults.httpsAgent;
for (let sym of Object.getOwnPropertySymbols(httpsAgent)) {
if (sym.toString() === "Symbol(cookieOptions)") {
const jar = httpsAgent[sym].jar as CookieJar;
jar.removeAllCookiesSync();
}
}
}
不过,我更喜欢直接访问符号的方法。