From b5af18bd94af0533ca73b0ac49c4fb4e55ce5d4d Mon Sep 17 00:00:00 2001 From: "Masahiro Miyashiro (3846masa)" <3846masahiro+git@gmail.com> Date: Thu, 2 May 2024 19:18:20 +0900 Subject: [PATCH] fix: removed async_UNSTABLE option because it is not working correctly (#874) * closed https://github.com/3846masa/http-cookie-agent/issues/831 --- README.md | 20 -- http/index.d.ts | 1 - package.json | 6 - pnpm-lock.yaml | 33 --- src/cookie_options.ts | 1 - src/http/__tests__/http_async.spec.ts | 276 ------------------ src/undici/__tests__/undici_async.spec.ts | 211 ------------- .../__tests__/validate_cookie_options.spec.ts | 26 +- src/utils/create_cookie_header_value.ts | 13 +- src/utils/save_cookies_from_header.ts | 13 +- src/utils/validate_cookie_options.ts | 20 +- undici/index.d.ts | 1 - 12 files changed, 8 insertions(+), 613 deletions(-) delete mode 100644 src/http/__tests__/http_async.spec.ts delete mode 100644 src/undici/__tests__/undici_async.spec.ts diff --git a/README.md b/README.md index cbc962ec..d818404c 100644 --- a/README.md +++ b/README.md @@ -267,26 +267,6 @@ setGlobalDispatcher(agent); await request('https://example.com'); ``` -### Using with an asynchronous Cookie store - -`http-cookie-agent` use synchronous CookieJar functions by default. - -Therefore, you cannot use an asynchronous Cookie store (e.g. `redis-cookie-store`) by default. - -If you want to use an asynchronous Cookie store, set `cookies.async_UNSTABLE` to true. - -```js -// node:http, node:https -const jar = new CookieJar(); -const agent = new HttpsCookieAgent({ cookies: { async_UNSTABLE: true, jar } }); -``` - -```js -// undici -const jar = new CookieJar(); -const agent = new CookieAgent({ cookies: { async_UNSTABLE: true, jar } } }); -``` - ### Using with another Agent library If you want to use another Agent library, wrap the agent in `createCookieAgent`. diff --git a/http/index.d.ts b/http/index.d.ts index ba57d908..7208f40d 100644 --- a/http/index.d.ts +++ b/http/index.d.ts @@ -4,7 +4,6 @@ import type https from 'node:https'; import type { CookieJar } from 'tough-cookie'; export interface CookieOptions { - async_UNSTABLE?: true; jar: CookieJar; } diff --git a/package.json b/package.json index 6aa3f6ae..ade4e253 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,6 @@ "@reactioncommerce/babel-remove-es-create-require": "1.0.0", "@semantic-release/changelog": "6.0.3", "@semantic-release/git": "10.0.1", - "@types/deasync": "0.1.5", "@types/needle": "3.3.0", "@types/node": "18.19.31", "@types/request": "2.48.12", @@ -80,7 +79,6 @@ "agentkeepalive": "4.5.0", "axios": "1.6.8", "babel-jest": "29.7.0", - "deasync": "0.1.29", "disposablestack": "1.1.4", "got": "12.6.1", "http-proxy-agent": "7.0.2", @@ -100,14 +98,10 @@ "urllib": "3.23.0" }, "peerDependencies": { - "deasync": "^0.1.26", "tough-cookie": "^4.0.0", "undici": "^5.11.0 || ^6.0.0" }, "peerDependenciesMeta": { - "deasync": { - "optional": true - }, "undici": { "optional": true } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 73209d25..df648103 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -51,9 +51,6 @@ devDependencies: '@semantic-release/git': specifier: 10.0.1 version: 10.0.1(patch_hash=kuxvwzfamrmcpadvl3v3nsbcu4)(semantic-release@22.0.12) - '@types/deasync': - specifier: 0.1.5 - version: 0.1.5 '@types/needle': specifier: 3.3.0 version: 3.3.0 @@ -81,9 +78,6 @@ devDependencies: babel-jest: specifier: 29.7.0 version: 29.7.0(@babel/core@7.24.4) - deasync: - specifier: 0.1.29 - version: 0.1.29 disposablestack: specifier: 1.1.4 version: 1.1.4 @@ -2385,10 +2379,6 @@ packages: resolution: {integrity: sha512-he+DHOWReW0nghN24E1WUqM0efK4kI9oTqDm6XmK8ZPe2djZ90BSNdGnIyCLzCPw7/pogPlGbzI2wHGGmi4O/Q==} dev: true - /@types/deasync@0.1.5: - resolution: {integrity: sha512-mLov/tw+fOX4ZsrT9xuHOJv8xToOpNsp6W4gp8VDHy2qniJ58izyOzHlisnz5r8HdZ+WItDHtANWZy/W0JEJwg==} - dev: true - /@types/graceful-fs@4.1.9: resolution: {integrity: sha512-olP3sd1qOEe5dXTSaFvQG+02VdRXcdytWLAZsAq1PecU8uqQAhkrnbli7DagjtXKW/Bl7YJbUsa8MPcuc8LHEQ==} dependencies: @@ -3114,12 +3104,6 @@ packages: dev: true optional: true - /bindings@1.5.0: - resolution: {integrity: sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==} - dependencies: - file-uri-to-path: 1.0.0 - dev: true - /bottleneck@2.19.5: resolution: {integrity: sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==} dev: true @@ -3550,15 +3534,6 @@ packages: is-data-view: 1.0.1 dev: true - /deasync@0.1.29: - resolution: {integrity: sha512-EBtfUhVX23CE9GR6m+F8WPeImEE4hR/FW9RkK0PMl9V1t283s0elqsTD8EZjaKX28SY1BW2rYfCgNsAYdpamUw==} - engines: {node: '>=0.11.0'} - requiresBuild: true - dependencies: - bindings: 1.5.0 - node-addon-api: 1.7.2 - dev: true - /debug@3.2.7: resolution: {integrity: sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==} peerDependencies: @@ -4309,10 +4284,6 @@ packages: flat-cache: 3.2.0 dev: true - /file-uri-to-path@1.0.0: - resolution: {integrity: sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==} - dev: true - /fill-range@7.0.1: resolution: {integrity: sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==} engines: {node: '>=8'} @@ -6083,10 +6054,6 @@ packages: resolution: {integrity: sha512-EZSPZB70jiVsivaBLYDCyntd5eH8NTSMOn3rB+HxwdmKThGELLdYv8qVIMWvZEFy9w8ZZpW9h9OB32l1rGtj7g==} dev: true - /node-addon-api@1.7.2: - resolution: {integrity: sha512-ibPK3iA+vaY1eEjESkQkM0BbCqFOaZMiXRTtdB0u7b4djtY6JnsjvPdUHVMg6xQt3B8fpTTWHI9A+ADjM9frzg==} - dev: true - /node-domexception@1.0.0: resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==} engines: {node: '>=10.5.0'} diff --git a/src/cookie_options.ts b/src/cookie_options.ts index 5490fdaa..37477b84 100644 --- a/src/cookie_options.ts +++ b/src/cookie_options.ts @@ -1,7 +1,6 @@ import type { CookieJar } from 'tough-cookie'; interface CookieOptions { - async_UNSTABLE?: true; jar: CookieJar; } diff --git a/src/http/__tests__/http_async.spec.ts b/src/http/__tests__/http_async.spec.ts deleted file mode 100644 index b8836ba5..00000000 --- a/src/http/__tests__/http_async.spec.ts +++ /dev/null @@ -1,276 +0,0 @@ -import http from 'node:http'; -import { text } from 'node:stream/consumers'; - -import { expect, jest, test } from '@jest/globals'; -import { CookieJar } from 'tough-cookie'; - -import { createTestServer } from '../../__tests__/helpers'; -import { HttpCookieAgent } from '../index'; - -export function request(url: string, options: http.RequestOptions, payload?: unknown) { - const promise = new Promise((resolve, reject) => { - const req = http.request(url, options); - req.on('response', (res) => { - res.on('error', (err) => reject(err)); - res.on('end', () => resolve(data)); - const data = text(res); - }); - req.on('error', (err) => reject(err)); - req.end(payload); - }); - - return promise; -} - -test('should set cookies to CookieJar from Set-Cookie header', async () => { - using server = await createTestServer([ - (_req, res) => { - res.setHeader('Set-Cookie', 'key=value'); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await request(`http://localhost:${server.port}`, { - agent, - method: 'GET', - }); - - const actual = await jar.getCookies(`http://localhost:${server.port}`); - expect(actual).toMatchObject([{ key: 'key', value: 'value' }]); -}); - -test('should set cookies to CookieJar from multiple Set-Cookie headers', async () => { - using server = await createTestServer([ - (_req, res) => { - res.setHeader('Set-Cookie', ['key1=value1', 'key2=value2']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await request(`http://localhost:${server.port}`, { - agent, - method: 'GET', - }); - - const actual = await jar.getCookies(`http://localhost:${server.port}`); - expect(actual).toMatchObject([ - { key: 'key1', value: 'value1' }, - { key: 'key2', value: 'value2' }, - ]); -}); - -test('should send cookies from CookieJar', async () => { - using server = await createTestServer([ - (req, res) => { - res.write(req.headers['cookie']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await jar.setCookie('key=value', `http://localhost:${server.port}`); - - const actual = await request(`http://localhost:${server.port}`, { - agent, - method: 'GET', - }); - expect(actual).toBe('key=value'); -}); - -test('should send cookies from CookieJar when value is url-encoded', async () => { - using server = await createTestServer([ - (req, res) => { - res.write(req.headers['cookie']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await jar.setCookie('key=hello%20world', `http://localhost:${server.port}`); - - const actual = await request(`http://localhost:${server.port}`, { - agent, - method: 'GET', - }); - expect(actual).toBe('key=hello%20world'); -}); - -test('should send cookies from both a request options and CookieJar', async () => { - using server = await createTestServer([ - (req, res) => { - res.write(req.headers['cookie']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await jar.setCookie('key1=value1', `http://localhost:${server.port}`); - - const actual = await request(`http://localhost:${server.port}`, { - agent, - headers: { Cookie: 'key2=value2' }, - method: 'GET', - }); - expect(actual).toBe('key1=value1; key2=value2'); -}); - -test('should send cookies from a request options when the key is duplicated in both a request options and CookieJar', async () => { - using server = await createTestServer([ - (req, res) => { - res.write(req.headers['cookie']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await jar.setCookie('key=notexpected', `http://localhost:${server.port}`); - - const actual = await request(`http://localhost:${server.port}`, { - agent, - headers: { Cookie: 'key=expected' }, - method: 'GET', - }); - expect(actual).toBe('key=expected'); -}); - -test('should emit error when CookieJar#getCookies throws error.', async () => { - using server = await createTestServer([ - (_req, res) => { - res.setHeader('Set-Cookie', 'key=value'); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - jest.spyOn(jar, 'getCookies').mockImplementation((...args: unknown[]): void | Promise => { - const cb = args.at(-1); - if (typeof cb === 'function') { - cb(new Error('Error')); - } else { - return Promise.reject(new Error('Error')); - } - }); - - const actual = request(`http://localhost:${server.port}`, { - agent, - method: 'GET', - }); - await expect(actual).rejects.toThrowError(); -}); - -test('should emit error when CookieJar#setCookie throws error.', async () => { - using server = await createTestServer([ - (_req, res) => { - res.setHeader('Set-Cookie', 'key=value'); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - jest.spyOn(jar, 'setCookie').mockImplementation((...args: unknown[]): void | Promise => { - const cb = args.at(-1); - if (typeof cb === 'function') { - cb(new Error('Error')); - } else { - return Promise.reject(new Error('Error')); - } - }); - - const actual = request(`http://localhost:${server.port}`, { - agent, - method: 'GET', - }); - await expect(actual).rejects.toThrowError(); -}); - -test('should send cookies even when target is same host but different port', async () => { - using firstServer = await createTestServer([ - (_req, res) => { - res.setHeader('Set-Cookie', 'key=expected'); - res.end(); - }, - ]); - using secondServer = await createTestServer([ - (req, res) => { - res.write(req.headers['cookie']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - { - await request(`http://localhost:${firstServer.port}`, { - agent, - method: 'GET', - }); - } - { - const actual = await request(`http://localhost:${secondServer.port}`, { - agent, - method: 'GET', - }); - expect(actual).toBe('key=expected'); - } -}); - -test('should send post data when keepalive is enabled', async () => { - using server = await createTestServer([ - async (req, res) => { - res.write( - JSON.stringify({ - cookie: req.headers['cookie'], - payload: await text(req), - }), - ); - res.end(); - }, - async (req, res) => { - res.write( - JSON.stringify({ - cookie: req.headers['cookie'], - payload: await text(req), - }), - ); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new HttpCookieAgent({ cookies: { async_UNSTABLE: true, jar }, keepAlive: true }); - - await jar.setCookie('key=expected', `http://localhost:${server.port}`); - - const actual = await Promise.all([ - request( - `http://localhost:${server.port}`, - { - agent, - method: 'POST', - }, - `payload-01`, - ).then((res) => JSON.parse(res)), - request( - `http://localhost:${server.port}`, - { - agent, - method: 'POST', - }, - `payload-02`, - ).then((res) => JSON.parse(res)), - ]); - - expect(actual).toEqual([ - { cookie: 'key=expected', payload: 'payload-01' }, - { cookie: 'key=expected', payload: 'payload-02' }, - ]); -}); diff --git a/src/undici/__tests__/undici_async.spec.ts b/src/undici/__tests__/undici_async.spec.ts deleted file mode 100644 index b0537134..00000000 --- a/src/undici/__tests__/undici_async.spec.ts +++ /dev/null @@ -1,211 +0,0 @@ -import { text } from 'node:stream/consumers'; - -import { expect, jest, test } from '@jest/globals'; -import { CookieJar } from 'tough-cookie'; -import { request } from 'undici'; - -import { createTestServer } from '../../__tests__/helpers'; -import { CookieAgent } from '../cookie_agent'; - -test('should set cookies to CookieJar from Set-Cookie header', async () => { - using server = await createTestServer([ - (_req, res) => { - res.setHeader('Set-Cookie', 'key=value'); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new CookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await request(`http://localhost:${server.port}`, { dispatcher: agent }); - - const actual = await jar.getCookies(`http://localhost:${server.port}`); - expect(actual).toMatchObject([{ key: 'key', value: 'value' }]); -}); - -test('should set cookies to CookieJar from multiple Set-Cookie headers', async () => { - using server = await createTestServer([ - (_req, res) => { - res.setHeader('Set-Cookie', ['key1=value1', 'key2=value2']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new CookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await request(`http://localhost:${server.port}`, { dispatcher: agent }); - - const actual = await jar.getCookies(`http://localhost:${server.port}`); - expect(actual).toMatchObject([ - { key: 'key1', value: 'value1' }, - { key: 'key2', value: 'value2' }, - ]); -}); - -test('should send cookies from CookieJar', async () => { - using server = await createTestServer([ - (req, res) => { - res.write(req.headers['cookie']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new CookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await jar.setCookie('key=value', `http://localhost:${server.port}`); - - const actual = await request(`http://localhost:${server.port}`, { dispatcher: agent }).then((res) => res.body.text()); - expect(actual).toBe('key=value'); -}); - -test('should send cookies from both a request options and CookieJar', async () => { - using server = await createTestServer([ - (req, res) => { - res.write(req.headers['cookie']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new CookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await jar.setCookie('key1=value1', `http://localhost:${server.port}`); - - const actual = await request(`http://localhost:${server.port}`, { - dispatcher: agent, - headers: { Cookie: 'key2=value2' }, - }).then((res) => res.body.text()); - expect(actual).toBe('key1=value1; key2=value2'); -}); - -test('should send cookies from a request options when the key is duplicated in both a request options and CookieJar', async () => { - using server = await createTestServer([ - (req, res) => { - res.write(req.headers['cookie']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new CookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - await jar.setCookie('key=notexpected', `http://localhost:${server.port}`); - - const actual = await request(`http://localhost:${server.port}`, { - dispatcher: agent, - headers: { Cookie: 'key=expected' }, - }).then((res) => res.body.text()); - expect(actual).toBe('key=expected'); -}); - -test('should send cookies from the first response when redirecting', async () => { - using server = await createTestServer([ - (_req, res) => { - res.statusCode = 301; - res.setHeader('Location', '/redirect'); - res.setHeader('Set-Cookie', 'key=value'); - res.end(); - }, - (req, res) => { - res.write(req.headers['cookie']); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new CookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - const actual = await request(`http://localhost:${server.port}`, { - dispatcher: agent, - maxRedirections: 1, - }).then((res) => res.body.text()); - expect(actual).toBe('key=value'); -}); - -test('should emit error when CookieJar#getCookies throws error.', async () => { - using server = await createTestServer([ - (_req, res) => { - res.setHeader('Set-Cookie', 'key=value'); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new CookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - jest.spyOn(jar, 'getCookies').mockImplementation((...args: unknown[]): void | Promise => { - const cb = args.at(-1); - if (typeof cb === 'function') { - cb(new Error('Error')); - } else { - return Promise.reject(new Error('Error')); - } - }); - - const actual = request(`http://localhost:${server.port}`, { dispatcher: agent }); - await expect(actual).rejects.toThrowError(); -}); - -test('should emit error when CookieJar#setCookie throws error.', async () => { - using server = await createTestServer([ - (_req, res) => { - res.setHeader('Set-Cookie', 'key=value'); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new CookieAgent({ cookies: { async_UNSTABLE: true, jar } }); - - jest.spyOn(jar, 'setCookie').mockImplementation((...args: unknown[]): void | Promise => { - const cb = args.at(-1); - if (typeof cb === 'function') { - cb(new Error('Error')); - } else { - return Promise.reject(new Error('Error')); - } - }); - - const actual = request(`http://localhost:${server.port}`, { dispatcher: agent }); - await expect(actual).rejects.toThrowError(); -}); - -test('should send post data when keepalive is enabled', async () => { - using server = await createTestServer([ - async (req, res) => { - res.write( - JSON.stringify({ - cookie: req.headers['cookie'], - payload: await text(req), - }), - ); - res.end(); - }, - async (req, res) => { - res.write( - JSON.stringify({ - cookie: req.headers['cookie'], - payload: await text(req), - }), - ); - res.end(); - }, - ]); - const jar = new CookieJar(); - const agent = new CookieAgent({ cookies: { jar } }); - - await jar.setCookie('key=expected', `http://localhost:${server.port}`); - - const actual = await Promise.all([ - request(`http://localhost:${server.port}`, { - body: `payload-01`, - dispatcher: agent, - method: 'POST', - }).then((res) => res.body.json()), - request(`http://localhost:${server.port}`, { - body: `payload-02`, - dispatcher: agent, - method: 'POST', - }).then((res) => res.body.json()), - ]); - - expect(actual).toEqual([ - { cookie: 'key=expected', payload: 'payload-01' }, - { cookie: 'key=expected', payload: 'payload-02' }, - ]); -}); diff --git a/src/utils/__tests__/validate_cookie_options.spec.ts b/src/utils/__tests__/validate_cookie_options.spec.ts index 965cd6ec..c84d9dc9 100644 --- a/src/utils/__tests__/validate_cookie_options.spec.ts +++ b/src/utils/__tests__/validate_cookie_options.spec.ts @@ -11,28 +11,14 @@ test('should throw Error when jar is not set', () => { }); }); -test('should throw Error when async_UNSTABLE is true but deasync is not installed', () => { - const jar = new CookieJar(); - - const resolver = () => { - throw new Error(); - }; - - expect(() => { - validateCookieOptions({ async_UNSTABLE: true, jar }, resolver); - }).toThrowError({ - message: 'you should install deasync library when cookies.async_UNSTABLE is true.', - }); -}); - -test('should throw Error when cookie store is asynchronous but async_UNSTABLE is not set', () => { +test('should throw Error when cookie store is asynchronous', () => { const jar = new CookieJar(); jar.store.synchronous = false; expect(() => { validateCookieOptions({ jar }); }).toThrowError({ - message: 'you should set cookies.async_UNSTABLE to true for using the asynchronous cookie store.', + message: 'an asynchronous cookie store is not supported.', }); }); @@ -43,11 +29,3 @@ test('should not throw Error when only pass synchronous cookie store', () => { validateCookieOptions({ jar }); }).not.toThrowError(); }); - -test('should not throw Error when cookie store is asynchronous and async_UNSTABLE is true', () => { - const jar = new CookieJar(); - - expect(() => { - validateCookieOptions({ async_UNSTABLE: true, jar }); - }).not.toThrowError(); -}); diff --git a/src/utils/create_cookie_header_value.ts b/src/utils/create_cookie_header_value.ts index 6b32ee9f..894d567c 100644 --- a/src/utils/create_cookie_header_value.ts +++ b/src/utils/create_cookie_header_value.ts @@ -1,11 +1,7 @@ -import { createRequire } from 'node:module'; - import { Cookie } from 'tough-cookie'; import type { CookieOptions } from '../cookie_options'; -const require = createRequire(import.meta.url); - type Params = { cookieOptions: CookieOptions; passedValues: (string | number | undefined | null)[]; @@ -13,14 +9,9 @@ type Params = { }; export function createCookieHeaderValue({ cookieOptions, passedValues, requestUrl }: Params): string { - const { async_UNSTABLE = false, jar } = cookieOptions; - - const getCookiesSync = async_UNSTABLE - ? // eslint-disable-next-line @typescript-eslint/no-var-requires - (require('deasync') as typeof import('deasync'))(jar.getCookies.bind(jar)) - : jar.getCookiesSync.bind(jar); + const { jar } = cookieOptions; - const cookies = getCookiesSync(requestUrl); + const cookies = jar.getCookiesSync(requestUrl); const cookiesMap = new Map(cookies.map((cookie) => [cookie.key, cookie])); for (const passedValue of passedValues) { diff --git a/src/utils/save_cookies_from_header.ts b/src/utils/save_cookies_from_header.ts index d79e8916..6f2b3475 100644 --- a/src/utils/save_cookies_from_header.ts +++ b/src/utils/save_cookies_from_header.ts @@ -1,9 +1,5 @@ -import { createRequire } from 'node:module'; - import type { CookieOptions } from '../cookie_options'; -const require = createRequire(import.meta.url); - type Params = { cookieOptions: CookieOptions; cookies: string | string[] | undefined; @@ -11,17 +7,12 @@ type Params = { }; export function saveCookiesFromHeader({ cookieOptions, cookies, requestUrl }: Params): void { - const { async_UNSTABLE = false, jar } = cookieOptions; - - const setCookieSync = async_UNSTABLE - ? // eslint-disable-next-line @typescript-eslint/no-var-requires - (require('deasync') as typeof import('deasync'))(jar.setCookie.bind(jar)) - : jar.setCookieSync.bind(jar); + const { jar } = cookieOptions; for (const cookie of [cookies].flat()) { if (cookie == null) { continue; } - setCookieSync(cookie, requestUrl, { ignoreError: true }); + jar.setCookieSync(cookie, requestUrl, { ignoreError: true }); } } diff --git a/src/utils/validate_cookie_options.ts b/src/utils/validate_cookie_options.ts index 49afba92..e029740c 100644 --- a/src/utils/validate_cookie_options.ts +++ b/src/utils/validate_cookie_options.ts @@ -1,30 +1,14 @@ -import { createRequire } from 'node:module'; - import type { CookieOptions } from '../cookie_options'; -const require = createRequire(import.meta.url); - -type ModuleResolver = (id: string) => string; - function validateCookieOptions( // eslint-disable-next-line @typescript-eslint/no-explicit-any opts: any, - resolver: ModuleResolver = require, ): asserts opts is CookieOptions { if (!('jar' in opts)) { throw new TypeError('invalid cookies.jar'); } - - if (opts.async_UNSTABLE) { - try { - resolver('deasync'); - } catch (_err) { - throw new Error('you should install deasync library when cookies.async_UNSTABLE is true.'); - } - } else { - if (!opts.jar.store.synchronous) { - throw new TypeError('you should set cookies.async_UNSTABLE to true for using the asynchronous cookie store.'); - } + if (!opts.jar.store.synchronous) { + throw new TypeError('an asynchronous cookie store is not supported.'); } } diff --git a/undici/index.d.ts b/undici/index.d.ts index 7574e534..718b2f84 100644 --- a/undici/index.d.ts +++ b/undici/index.d.ts @@ -2,7 +2,6 @@ import type { CookieJar } from 'tough-cookie'; import { Agent, Client } from 'undici'; export interface CookieOptions { - async_UNSTABLE?: true; jar: CookieJar; }