typecript中的http-cookie-agent访问cookie

发布于 2025-01-31 18:09:36 字数 3785 浏览 5 评论 0原文

我正在尝试从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;
}

这样称为:导出const axios = setUpaxios(basepath);

我有一个函数clear> clear> clear> clear> clear> clear> clear> clear> clear> clear> clear> clear> clear> clear

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显然在那里,但是我无法检索它。 I have imported

import { HttpsCookieAgent, CookieAgentOptions, CookieOptions } from "http-cookie-agent/http";

and if I'd be able to set the CookieAgentOptions I could access cookie(至少根据vscode)。但是我不知道如何编写访问的代码 [符号(cookieoptions)]

httpsagent.defaultport工作正常并让我443,但是我如何处理符号?

I am trying to access a cookie from axios. I am unable to use the wrapper but can use http-cookie-agent directly.

I can set the cookie, but I am unable to retrieve it again.

The cookie is set like this:

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;
}

which is called like this: export const axios = setupAxios(basePath);

I have a function clearCookies in which I get the agent:

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();
}

I can log out the dictionary:

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)]
    }
  }
}

The cookie is clearly there, I am however unable to retrieve it. I have imported

import { HttpsCookieAgent, CookieAgentOptions, CookieOptions } from "http-cookie-agent/http";

and if I'd be able to set the CookieAgentOptions I could access the cookie (at least according to vscode). But I don't know how to write code that accesses
[Symbol(cookieOptions)]

httpsAgent.defaultPort works fine and gets me 443, but how do I handle the symbol?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的夜不怕黑 2025-02-07 18:09:36

循环的想法使我走上了正确的轨道。
我还必须删除打字。现在您可以访问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();
    }
  }
}

不过,我更喜欢某种直接访问符号的方法。

The idea to loop brought me on the right track.
I also had to remove the typecasting. Now you can access the cookie
e.g. this way:

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();
    }
  }
}

Still, I'd prefer some way of accessing the symbol directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文