remove cookie package (#5427)

This commit is contained in:
Jack Kavanagh 2022-11-16 15:07:49 +00:00 committed by GitHub
parent 6a93d9d256
commit 8827e240ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 139 additions and 3701 deletions

View File

@ -1,3 +0,0 @@
src/tough-cookie.d.ts
node_modules
dist

View File

@ -1,5 +0,0 @@
# Insomnia Cookie Utilities
[![Npm Version](https://img.shields.io/npm/v/insomnia-cookies.svg)](https://www.npmjs.com/package/insomnia-cookies)
Various utility functions for interacting with cookies and cookie jars.

View File

@ -1,9 +0,0 @@
/** @type { import('@jest/types').Config.InitialOptions } */
module.exports = {
preset: '../../jest-preset.js',
globals: {
'ts-jest': {
isolatedModules: true,
},
},
};

File diff suppressed because it is too large Load Diff

View File

@ -1,38 +0,0 @@
{
"private": true,
"name": "insomnia-cookies",
"version": "3.6.1-beta.3",
"author": "Kong <office@konghq.com>",
"description": "Cookie utilities",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/Kong/insomnia.git",
"directory": "packages/insomnia-cookies"
},
"bugs": {
"url": "https://github.com/Kong/insomnia/issues"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist",
"package.json",
"READEM.md"
],
"scripts": {
"bootstrap": "npm run build",
"lint": "eslint . --ext .js,.ts,.tsx --cache",
"lint:fix": "npm run lint -- --fix",
"clean": "tsc --build tsconfig.build.json --clean",
"postclean": "rimraf dist",
"build": "tsc --build tsconfig.build.json",
"test": "jest"
},
"devDependencies": {
"@jest/globals": "^28.1.0"
},
"dependencies": {
"tough-cookie": "^2.3.3"
}
}

View File

@ -1 +0,0 @@
export { cookiesFromJar, jarFromCookies, cookieToString } from './cookies';

View File

@ -1,14 +0,0 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"outDir": "dist",
"rootDir": "src",
},
"include": [
"src",
],
"exclude": [
"**/*.test.ts",
],
}

View File

@ -1,14 +0,0 @@
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"composite": false,
"rootDir": ".",
},
"include": [
"jest.config.js",
"src",
],
"exclude": [
"dist",
],
}

View File

@ -66,7 +66,6 @@
"iconv-lite": "^0.6.3",
"insomnia-common": "^3.6.1-beta.3",
"insomnia-config": "^3.6.1-beta.3",
"insomnia-cookies": "^3.6.1-beta.3",
"insomnia-importers": "^3.6.1-beta.3",
"insomnia-plugin-base64": "^3.6.1-beta.3",
"insomnia-plugin-cookie-jar": "^3.6.1-beta.3",

View File

@ -1,7 +1,7 @@
import { describe, expect, it, jest } from '@jest/globals';
import { Cookie, CookieJar, CookieSerialized } from 'tough-cookie';
import { cookiesFromJar, jarFromCookies } from './cookies';
import { cookiesFromJar, jarFromCookies } from '../cookies';
describe('jarFromCookies()', () => {
it('returns valid cookies', done => {

View File

@ -1,5 +1,7 @@
import { Cookie, CookieJar, CookieJSON } from 'tough-cookie';
import type { Cookie as CookieModel } from '../models/cookie-jar';
/**
* Get a list of cookie objects from a request.jar()
*/
@ -20,7 +22,7 @@ export const cookiesFromJar = (cookieJar: CookieJar): Promise<CookieJSON[]> => {
/**
* Get a request.jar() from a list of cookie objects
*/
export const jarFromCookies = (cookies: Cookie[]) => {
export const jarFromCookies = (cookies: Cookie[] | CookieModel[]) => {
let jar: CookieJar;
try {

View File

@ -1,10 +1,8 @@
import clone from 'clone';
import fs from 'fs';
import { jarFromCookies } from 'insomnia-cookies';
import { Cookie as toughCookie } from 'tough-cookie';
import { Cookie as ToughCookie } from 'tough-cookie';
import * as models from '../models';
import type { Cookie } from '../models/cookie-jar';
import type { Request } from '../models/request';
import { newBodyRaw } from '../models/request';
import type { Response } from '../models/response';
@ -15,6 +13,7 @@ import * as pluginContexts from '../plugins/context/index';
import { RenderError } from '../templating/index';
import { smartEncodeUrl } from '../utils/url/querystring';
import { getAppVersion } from './constants';
import { jarFromCookies } from './cookies';
import { database } from './database';
import { filterHeaders, getSetCookieHeaders, hasAuthHeader } from './misc';
import type { RenderedRequest } from './render';
@ -420,10 +419,10 @@ function getResponseCookies(response: Response) {
const headers = response.headers.filter(Boolean) as HarCookie[];
const responseCookies = getSetCookieHeaders(headers)
.reduce((accumulator, harCookie) => {
let cookie: null | undefined | toughCookie = null;
let cookie: null | undefined | ToughCookie = null;
try {
cookie = toughCookie.parse(harCookie.value || '');
cookie = ToughCookie.parse(harCookie.value || '');
} catch (error) {}
if (cookie === null || cookie === undefined) {
@ -432,13 +431,13 @@ function getResponseCookies(response: Response) {
return [
...accumulator,
mapCookie(cookie as unknown as Cookie),
mapCookie(cookie),
];
}, [] as HarCookie[]);
return responseCookies;
}
function mapCookie(cookie: Cookie) {
function mapCookie(cookie: ToughCookie) {
const harCookie: HarCookie = {
name: cookie.key,
value: cookie.value,

View File

@ -1,7 +1,6 @@
import electron, { ipcMain } from 'electron';
import fs from 'fs';
import { IncomingMessage } from 'http';
import { jarFromCookies } from 'insomnia-cookies';
import mkdirp from 'mkdirp';
import path from 'path';
import { KeyObject, PxfObject } from 'tls';
@ -15,6 +14,7 @@ import {
} from 'ws';
import { AUTH_API_KEY, AUTH_BASIC, AUTH_BEARER } from '../../common/constants';
import { jarFromCookies } from '../../common/cookies';
import { generateId, getSetCookieHeaders } from '../../common/misc';
import { webSocketRequest } from '../../models';
import * as models from '../../models';

View File

@ -1,6 +1,5 @@
import clone from 'clone';
import fs from 'fs';
import { cookiesFromJar, jarFromCookies } from 'insomnia-cookies';
import mkdirp from 'mkdirp';
import { join as pathJoin } from 'path';
import { v4 as uuidv4 } from 'uuid';
@ -8,6 +7,7 @@ import { v4 as uuidv4 } from 'uuid';
import {
STATUS_CODE_PLUGIN_ERROR,
} from '../common/constants';
import { cookiesFromJar, jarFromCookies } from '../common/cookies';
import { database as db } from '../common/database';
import { getDataDirectory } from '../common/electron-helpers';
import {
@ -25,6 +25,7 @@ import {
import type { ResponsePatch, ResponseTimelineEntry } from '../main/network/libcurl-promise';
import * as models from '../models';
import { ClientCertificate } from '../models/client-certificate';
import { Cookie, CookieJar } from '../models/cookie-jar';
import type { Environment } from '../models/environment';
import type { Request } from '../models/request';
import type { Settings } from '../models/settings';
@ -74,7 +75,7 @@ export async function _actuallySend(
// NOTE: conditionally use ipc bridge, renderer cannot import native modules directly
const nodejsCancelCurlRequest = process.type === 'renderer'
? window.main.cancelCurlRequest
: (await import('../main/network/libcurl-promise')).cancelCurlRequest;
: (await import('../main/network/libcurl-promise')).cancelCurlRequest;
nodejsCancelCurlRequest(renderedRequest._id);
return resolve({
@ -147,7 +148,8 @@ export async function _actuallySend(
rejectedCookies.forEach(errorMessage => timeline.push({ value: `Rejected cookie: ${errorMessage}`, name: 'Text', timestamp: Date.now() }));
const hasCookiesToPersist = totalSetCookies > rejectedCookies.length;
if (hasCookiesToPersist) {
await models.cookieJar.update(cookieJar, { cookies });
const patch: Partial<CookieJar> = { cookies };
await models.cookieJar.update(cookieJar, patch);
timeline.push({ value: `Saved ${totalSetCookies} cookies`, name: 'Text', timestamp: Date.now() });
}
}
@ -220,7 +222,7 @@ export const addSetCookiesToToughCookieJar = async ({ setCookieStrings, currentU
}
}
}
const cookies = await cookiesFromJar(jar);
const cookies = (await cookiesFromJar(jar)) as Cookie[];
return { cookies, rejectedCookies };
};

View File

@ -241,4 +241,4 @@ declare module 'tough-cookie' {
}
export class MemoryCookieStore extends Store { }
}
}

View File

@ -1,8 +1,8 @@
import { cookieToString } from 'insomnia-cookies';
import React, { FC, useCallback } from 'react';
import { Cookie as ToughCookie } from 'tough-cookie';
import { v4 as uuidv4 } from 'uuid';
import { cookieToString } from '../../common/cookies';
import { Cookie } from '../../models/cookie-jar';
import { Dropdown } from './base/dropdown/dropdown';
import { DropdownButton } from './base/dropdown/dropdown-button';
@ -29,8 +29,8 @@ const CookieRow: FC<{
index: number;
deleteCookie: (cookie: Cookie) => void;
}> = ({ cookie, index, deleteCookie }) => {
const cookieString = cookieToString(ToughCookie.fromJSON(cookie));
const c = ToughCookie.fromJSON(cookie);
const cookieString = c ? cookieToString(c) : '';
return <tr className="selectable" key={index}>
<td>
<RenderedText>{cookie.domain || ''}</RenderedText>

View File

@ -7,11 +7,11 @@ import { readFileSync } from 'fs';
import { DefinitionNode, DocumentNode, GraphQLNonNull, GraphQLSchema, Kind, NonNullTypeNode, OperationDefinitionNode, parse, typeFromAST } from 'graphql';
import { buildClientSchema, getIntrospectionQuery } from 'graphql/utilities';
import { Maybe } from 'graphql-language-service';
import { jarFromCookies } from 'insomnia-cookies';
import prettier from 'prettier';
import React, { FC, useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { jarFromCookies } from '../../../../common/cookies';
import { markdownToHTML } from '../../../../common/markdown-to-html';
import { jsonParseOr } from '../../../../common/misc';
import { getRenderContext, render, RENDER_PURPOSE_SEND } from '../../../../common/render';

View File

@ -1,10 +1,10 @@
import clone from 'clone';
import { isValid } from 'date-fns';
import { cookieToString } from 'insomnia-cookies';
import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import { useSelector } from 'react-redux';
import * as toughCookie from 'tough-cookie';
import { Cookie as ToughCookie } from 'tough-cookie';
import { cookieToString } from '../../../common/cookies';
import * as models from '../../../models';
import type { Cookie } from '../../../models/cookie-jar';
import { selectActiveCookieJar } from '../../redux/selectors';
@ -71,7 +71,8 @@ export const CookieModifyModal = forwardRef<CookieModifyModalHandle, ModalProps>
rawDefaultValue = '';
} else {
try {
rawDefaultValue = cookieToString(toughCookie.Cookie.fromJSON(JSON.stringify(cookie)));
const c = ToughCookie.fromJSON(JSON.stringify(cookie));
rawDefaultValue = c ? cookieToString(c) : '';
} catch (err) {
console.warn('Failed to parse cookie string', err);
rawDefaultValue = '';
@ -165,7 +166,7 @@ export const CookieModifyModal = forwardRef<CookieModifyModalHandle, ModalProps>
onChange={event => {
try {
// NOTE: Perform toJSON so we have a plain JS object instead of Cookie instance
const parsed = toughCookie.Cookie.parse(event.target.value)?.toJSON();
const parsed = ToughCookie.parse(event.target.value)?.toJSON();
if (parsed) {
// Make sure cookie has an id
parsed.id = cookie.id;

View File

@ -1,4 +1,35 @@
const { jarFromCookies, cookiesFromJar } = require('insomnia-cookies');
const { CookieJar } = require('tough-cookie');
/**
* Get a request.jar() from a list of cookie objects
*/
const jarFromCookies = (cookies) => {
let jar;
try {
// For some reason, fromJSON modifies `cookies`.
// Create a copy first just to be sure.
const copy = JSON.stringify({ cookies });
jar = CookieJar.fromJSON(copy);
} catch (error) {
console.log('[cookies] Failed to initialize cookie jar', error);
jar = new CookieJar();
}
jar.rejectPublicSuffixes = false;
jar.looseMode = true;
return jar;
};
const cookiesFromJar = (cookieJar) => {
return new Promise(resolve => {
cookieJar.store.getAllCookies((err, cookies) => {
if (err) {
console.warn('Failed to get cookies form jar', err);
resolve([]);
} else {
// NOTE: Perform toJSON so we have a plain JS object instead of Cookie instance
resolve(cookies.map(cookie => cookie.toJSON()));
}
});
});
};
const tag = require('..').templateTags[0];
describe('plugin', () => {

View File

@ -1,4 +1,22 @@
const { jarFromCookies } = require('insomnia-cookies');
const { CookieJar } = require('tough-cookie');
/**
* Get a request.jar() from a list of cookie objects
*/
const jarFromCookies = (cookies) => {
let jar;
try {
// For some reason, fromJSON modifies `cookies`.
// Create a copy first just to be sure.
const copy = JSON.stringify({ cookies });
jar = CookieJar.fromJSON(copy);
} catch (error) {
console.log('[cookies] Failed to initialize cookie jar', error);
jar = new CookieJar();
}
jar.rejectPublicSuffixes = false;
jar.looseMode = true;
return jar;
};
module.exports.templateTags = [
{

View File

@ -1,35 +1,62 @@
{
"name": "insomnia-plugin-cookie-jar",
"version": "3.6.1-beta.3",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"insomnia-cookies": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/insomnia-cookies/-/insomnia-cookies-2.1.4.tgz",
"integrity": "sha512-2Fr+FMGguK1xsv2n2jdt1+kLRe94q1A5v7nwVeO7DgdtO54TCaLlbkVo1LkFXGKDRp12Z04KChGHV7Bzisw6fA==",
"requires": {
"tough-cookie": "^2.3.3"
}
},
"psl": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz",
"integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ=="
},
"punycode": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
"integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A=="
},
"tough-cookie": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz",
"integrity": "sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==",
"requires": {
"psl": "^1.1.28",
"punycode": "^2.1.1"
}
}
}
"name": "insomnia-plugin-cookie-jar",
"version": "3.6.1-beta.3",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "insomnia-plugin-cookie-jar",
"version": "3.6.1-beta.3",
"license": "MIT",
"dependencies": {
"tough-cookie": "^2.3.3"
}
},
"node_modules/psl": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz",
"integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ=="
},
"node_modules/punycode": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
"integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==",
"engines": {
"node": ">=6"
}
},
"node_modules/tough-cookie": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz",
"integrity": "sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==",
"dependencies": {
"psl": "^1.1.28",
"punycode": "^2.1.1"
},
"engines": {
"node": ">=0.8"
}
}
},
"dependencies": {
"psl": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz",
"integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ=="
},
"punycode": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
"integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A=="
},
"tough-cookie": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz",
"integrity": "sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==",
"requires": {
"psl": "^1.1.28",
"punycode": "^2.1.1"
}
}
}
}

View File

@ -28,6 +28,6 @@
"test": "jest --silent"
},
"dependencies": {
"insomnia-cookies": "^3.6.1-beta.3"
"tough-cookie": "^2.3.3"
}
}