@mherod/get-cookie 2.1.3 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/commit-msg +2 -0
- package/.husky/pre-commit +4 -0
- package/.husky/pre-push +4 -0
- package/README.md +163 -18
- package/commitlint.config.js +12 -0
- package/dist/chunk-56Z35D5R.js +2 -0
- package/dist/chunk-56Z35D5R.js.map +1 -0
- package/dist/chunk-5FUMK7M3.js +4 -0
- package/dist/chunk-5FUMK7M3.js.map +1 -0
- package/dist/chunk-5ZT2G45S.js +2 -0
- package/dist/chunk-5ZT2G45S.js.map +1 -0
- package/dist/chunk-FR2MKDHT.js +2 -0
- package/dist/chunk-FR2MKDHT.js.map +1 -0
- package/dist/chunk-HMKSQBDC.js +2 -0
- package/dist/chunk-HMKSQBDC.js.map +1 -0
- package/dist/chunk-VMBA4NVU.js +2 -0
- package/dist/chunk-VMBA4NVU.js.map +1 -0
- package/dist/chunk-W3JALMAX.js +2 -0
- package/dist/chunk-W3JALMAX.js.map +1 -0
- package/dist/cli.cjs +19 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/getChromeCookie-4ZWACG6M.js +2 -0
- package/dist/getChromeCookie-4ZWACG6M.js.map +1 -0
- package/dist/getChromePassword-GTIXL733.js +2 -0
- package/dist/getChromePassword-GTIXL733.js.map +1 -0
- package/dist/getCookie-43FKMQEZ.js +2 -0
- package/dist/getCookie-43FKMQEZ.js.map +1 -0
- package/dist/getFirefoxCookie-OJYYZFZP.js +2 -0
- package/dist/getFirefoxCookie-OJYYZFZP.js.map +1 -0
- package/dist/getGroupedRenderedCookies-P5VZXZUC.js +2 -0
- package/dist/getGroupedRenderedCookies-P5VZXZUC.js.map +1 -0
- package/dist/getMergedRenderedCookies-C6TXK2I4.js +2 -0
- package/dist/getMergedRenderedCookies-C6TXK2I4.js.map +1 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +252 -0
- package/dist/index.d.ts +252 -0
- package/dist/index.js +2 -24712
- package/dist/index.js.map +1 -0
- package/eslint.config.js +243 -0
- package/jest.setup.js +11 -0
- package/package.json +87 -36
- package/tsconfig.build.json +9 -0
- package/tsup.cli.ts +27 -0
- package/tsup.config.ts +27 -0
- package/tsup.lib.ts +27 -0
- package/typedoc.json +22 -0
- package/.parcelrc +0 -7
- package/.prettierignore +0 -5
- package/.prettierrc +0 -12
- package/.terserrc +0 -26
- package/build-indexes.ts +0 -108
- package/bun.lockb +0 -0
- package/dist/cli.js +0 -18319
- package/dist/types.d.ts +0 -361
- package/jest.config.ts +0 -14
- package/package-lock.json +0 -5608
- package/src/CookieRow.ts +0 -43
- package/src/CookieSpec.ts +0 -42
- package/src/ExportedCookie.ts +0 -19
- package/src/FetchResponse.ts +0 -13
- package/src/SpecialCases.ts +0 -15
- package/src/StringToRegex.ts +0 -16
- package/src/argv.ts +0 -30
- package/src/browsers/CompositeCookieQueryStrategy.ts +0 -76
- package/src/browsers/CookieQueryStrategy.ts +0 -7
- package/src/browsers/CookieStoreQueryStrategy.ts +0 -115
- package/src/browsers/QuerySqliteThenTransform.ts +0 -48
- package/src/browsers/chrome/ChromeApplicationSupport.ts +0 -14
- package/src/browsers/chrome/ChromeCookieQueryStrategy.ts +0 -158
- package/src/browsers/chrome/decrypt.ts +0 -154
- package/src/browsers/chrome/getChromePassword.ts +0 -28
- package/src/browsers/firefox/FirefoxCookieQueryStrategy.ts +0 -109
- package/src/browsers/getEncryptedChromeCookie.ts +0 -122
- package/src/browsers/index.ts +0 -12
- package/src/browsers/mock/MockCookieQueryStrategy.ts +0 -22
- package/src/browsers/safari/SafariCookieQueryStrategy.ts +0 -42
- package/src/cli.ts +0 -94
- package/src/cliQueryCookies.ts +0 -65
- package/src/comboQueryCookieSpec.test.ts +0 -92
- package/src/comboQueryCookieSpec.ts +0 -26
- package/src/cookieQueryOptions.ts +0 -27
- package/src/cookieSpecsFromUrl.test.ts +0 -65
- package/src/cookieSpecsFromUrl.ts +0 -73
- package/src/decodeBinaryCookies.ts +0 -72
- package/src/execSimple.ts +0 -12
- package/src/fetchWithCookies.test.ts +0 -32
- package/src/fetchWithCookies.ts +0 -217
- package/src/findAllFiles.ts +0 -48
- package/src/getChromeCookie.ts +0 -19
- package/src/getCookie.ts +0 -22
- package/src/getFirefoxCookie.ts +0 -25
- package/src/getGroupedRenderedCookies.ts +0 -42
- package/src/getMergedRenderedCookies.test.ts +0 -43
- package/src/getMergedRenderedCookies.ts +0 -19
- package/src/global.ts +0 -8
- package/src/index.ts +0 -16
- package/src/isValidJwt.ts +0 -26
- package/src/listChromeProfiles.ts +0 -53
- package/src/logger.ts +0 -12
- package/src/processBeforeReturn.ts +0 -29
- package/src/queryCookies.ts +0 -41
- package/src/resultsRendered.ts +0 -24
- package/src/unpackHeaders.ts +0 -18
- package/src/util/flatMapAsync.test.ts +0 -55
- package/src/util/flatMapAsync.ts +0 -41
- package/src/util/index.ts +0 -1
- package/tsconfig.json +0 -26
package/src/getCookie.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import CookieSpec from "./CookieSpec";
|
|
2
|
-
import ExportedCookie from "./ExportedCookie";
|
|
3
|
-
import { queryCookies } from "./queryCookies";
|
|
4
|
-
import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
|
|
5
|
-
|
|
6
|
-
const queryStrategy: CompositeCookieQueryStrategy =
|
|
7
|
-
new CompositeCookieQueryStrategy();
|
|
8
|
-
|
|
9
|
-
export async function getCookie(
|
|
10
|
-
params: CookieSpec,
|
|
11
|
-
): Promise<ExportedCookie | undefined> {
|
|
12
|
-
const cookies: ExportedCookie[] = await queryCookies(params, {
|
|
13
|
-
strategy: queryStrategy,
|
|
14
|
-
});
|
|
15
|
-
if (Array.isArray(cookies) && cookies.length > 0) {
|
|
16
|
-
return cookies.find((cookie) => cookie != null);
|
|
17
|
-
} else {
|
|
18
|
-
throw new Error("Cookie not found");
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export default getCookie;
|
package/src/getFirefoxCookie.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import CookieSpec from "./CookieSpec";
|
|
2
|
-
import ExportedCookie from "./ExportedCookie";
|
|
3
|
-
import { queryCookies } from "./queryCookies";
|
|
4
|
-
import FirefoxCookieQueryStrategy from "./browsers/firefox/FirefoxCookieQueryStrategy";
|
|
5
|
-
|
|
6
|
-
export async function getFirefoxCookie(
|
|
7
|
-
params: CookieSpec,
|
|
8
|
-
): Promise<ExportedCookie | undefined> {
|
|
9
|
-
const cookies: ExportedCookie[] = await queryCookies(params, {
|
|
10
|
-
strategy: new FirefoxCookieQueryStrategy(),
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
if (!Array.isArray(cookies) || cookies.length === 0) {
|
|
14
|
-
throw new Error("Cookie not found");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const validCookie: ExportedCookie | undefined = cookies.find(
|
|
18
|
-
(cookie) => cookie != null,
|
|
19
|
-
);
|
|
20
|
-
if (!validCookie) {
|
|
21
|
-
throw new Error("Cookie not found");
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return validCookie;
|
|
25
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { groupBy } from "lodash";
|
|
2
|
-
import { resultsRendered } from "./resultsRendered";
|
|
3
|
-
import { MultiCookieSpec } from "./CookieSpec";
|
|
4
|
-
import ExportedCookie from "./ExportedCookie";
|
|
5
|
-
import { comboQueryCookieSpec } from "./comboQueryCookieSpec";
|
|
6
|
-
|
|
7
|
-
export async function getGroupedRenderedCookies(
|
|
8
|
-
cookieSpec: MultiCookieSpec,
|
|
9
|
-
): Promise<string[]> {
|
|
10
|
-
const cookies: ExportedCookie[] = await fetchCookies(cookieSpec);
|
|
11
|
-
const groupedByFile = groupCookiesByFile(cookies);
|
|
12
|
-
return renderGroupedCookies(groupedByFile);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
async function fetchCookies(
|
|
16
|
-
cookieSpec: MultiCookieSpec,
|
|
17
|
-
): Promise<ExportedCookie[]> {
|
|
18
|
-
const cookies: ExportedCookie[] = await comboQueryCookieSpec(cookieSpec);
|
|
19
|
-
if (cookies.length === 0) {
|
|
20
|
-
throw new Error("Cookie not found");
|
|
21
|
-
}
|
|
22
|
-
return cookies;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function groupCookiesByFile(
|
|
26
|
-
cookies: ExportedCookie[],
|
|
27
|
-
): Record<string, ExportedCookie[]> {
|
|
28
|
-
return groupBy(cookies, (r: ExportedCookie) => r.meta?.file);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function renderGroupedCookies(
|
|
32
|
-
groupedByFile: Record<string, ExportedCookie[]>,
|
|
33
|
-
): string[] {
|
|
34
|
-
const renderedResults: string[] = [];
|
|
35
|
-
for (const file in groupedByFile) {
|
|
36
|
-
if (groupedByFile.hasOwnProperty(file)) {
|
|
37
|
-
const results: ExportedCookie[] = groupedByFile[file];
|
|
38
|
-
renderedResults.push(resultsRendered(results));
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return renderedResults;
|
|
42
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { getMergedRenderedCookies } from "./getMergedRenderedCookies";
|
|
2
|
-
import { MultiCookieSpec } from "./CookieSpec";
|
|
3
|
-
import MockCookieQueryStrategy from "./browsers/mock/MockCookieQueryStrategy";
|
|
4
|
-
|
|
5
|
-
describe("getMergedRenderedCookies", () => {
|
|
6
|
-
it("should return empty string if no cookies are present", async () => {
|
|
7
|
-
const cookieSpec: MultiCookieSpec = [];
|
|
8
|
-
const result = await getMergedRenderedCookies(cookieSpec);
|
|
9
|
-
expect(result).toBe("");
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it("should return rendered cookies if cookies are present", async () => {
|
|
13
|
-
const cookieSpec: MultiCookieSpec = [
|
|
14
|
-
{
|
|
15
|
-
name: "sessionCookie",
|
|
16
|
-
domain: "www.example.com",
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
name: "persistentCookie",
|
|
20
|
-
domain: "www.example.com",
|
|
21
|
-
},
|
|
22
|
-
];
|
|
23
|
-
const result = await getMergedRenderedCookies(
|
|
24
|
-
cookieSpec,
|
|
25
|
-
new MockCookieQueryStrategy([
|
|
26
|
-
{
|
|
27
|
-
name: "sessionCookie",
|
|
28
|
-
value: "sessionCookieValue",
|
|
29
|
-
domain: "www.example.com",
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
name: "persistentCookie",
|
|
33
|
-
value: "persistentCookieValue",
|
|
34
|
-
domain: "www.example.com",
|
|
35
|
-
},
|
|
36
|
-
]),
|
|
37
|
-
//
|
|
38
|
-
);
|
|
39
|
-
expect(result).not.toBe("");
|
|
40
|
-
expect(result).toContain("sessionCookie=sessionCookieValue");
|
|
41
|
-
expect(result).toContain("persistentCookie=persistentCookieValue");
|
|
42
|
-
});
|
|
43
|
-
});
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { resultsRendered } from "./resultsRendered";
|
|
2
|
-
import { comboQueryCookieSpec } from "./comboQueryCookieSpec";
|
|
3
|
-
import { MultiCookieSpec } from "./CookieSpec";
|
|
4
|
-
import ExportedCookie from "./ExportedCookie";
|
|
5
|
-
import CookieQueryStrategy from "./browsers/CookieQueryStrategy";
|
|
6
|
-
import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
|
|
7
|
-
|
|
8
|
-
export async function getMergedRenderedCookies(
|
|
9
|
-
cookieSpec: MultiCookieSpec,
|
|
10
|
-
strategy: CookieQueryStrategy = new CompositeCookieQueryStrategy(),
|
|
11
|
-
): Promise<string> {
|
|
12
|
-
const cookies: ExportedCookie[] = await comboQueryCookieSpec(cookieSpec, {
|
|
13
|
-
strategy,
|
|
14
|
-
});
|
|
15
|
-
if (cookies.length > 0) {
|
|
16
|
-
return resultsRendered(cookies);
|
|
17
|
-
}
|
|
18
|
-
return "";
|
|
19
|
-
}
|
package/src/global.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { getCookie } from "./getCookie";
|
|
2
|
-
import { getChromeCookie } from "./getChromeCookie";
|
|
3
|
-
import { getFirefoxCookie } from "./getFirefoxCookie";
|
|
4
|
-
import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
|
|
5
|
-
import { getMergedRenderedCookies } from "./getMergedRenderedCookies";
|
|
6
|
-
import { fetchWithCookies } from "./fetchWithCookies";
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
getCookie,
|
|
10
|
-
getChromeCookie,
|
|
11
|
-
getFirefoxCookie,
|
|
12
|
-
getMergedRenderedCookies,
|
|
13
|
-
getGroupedRenderedCookies,
|
|
14
|
-
fetchWithCookies,
|
|
15
|
-
//
|
|
16
|
-
};
|
package/src/isValidJwt.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import jsonwebtoken, { JwtPayload } from "jsonwebtoken";
|
|
2
|
-
import { parsedArgs } from "./argv";
|
|
3
|
-
|
|
4
|
-
export default function isValidJwt(token: string): boolean {
|
|
5
|
-
try {
|
|
6
|
-
const result = jsonwebtoken.decode(token, { complete: true });
|
|
7
|
-
if (parsedArgs.verbose && result) {
|
|
8
|
-
console.debug(result);
|
|
9
|
-
}
|
|
10
|
-
const payload: JwtPayload | undefined = result?.payload as
|
|
11
|
-
| JwtPayload
|
|
12
|
-
| undefined;
|
|
13
|
-
if (payload) {
|
|
14
|
-
const exp: number | undefined = payload.exp;
|
|
15
|
-
if (exp) {
|
|
16
|
-
const now: number = new Date().getTime() / 1000;
|
|
17
|
-
if (now > exp) {
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return true;
|
|
23
|
-
} catch (err) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { sync } from "fast-glob";
|
|
2
|
-
import { chromeApplicationSupport } from "./browsers/chrome/ChromeApplicationSupport";
|
|
3
|
-
import { dirname } from "path";
|
|
4
|
-
import { readFile } from "fs/promises";
|
|
5
|
-
import destr from "destr";
|
|
6
|
-
|
|
7
|
-
export async function listChromeProfilePaths(): Promise<string[]> {
|
|
8
|
-
const files: string[] = sync(`./**/Cookies`, {
|
|
9
|
-
cwd: chromeApplicationSupport,
|
|
10
|
-
absolute: true,
|
|
11
|
-
onlyFiles: true,
|
|
12
|
-
deep: 2,
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
const directories: string[] = [];
|
|
16
|
-
for (const file of files) {
|
|
17
|
-
directories.push(dirname(file));
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return directories;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export async function listChromeProfiles(): Promise<ChromeProfile[]> {
|
|
24
|
-
const paths: string[] = await listChromeProfilePaths();
|
|
25
|
-
const profiles: ChromeProfile[] = [];
|
|
26
|
-
|
|
27
|
-
for (const path of paths) {
|
|
28
|
-
const content: string = await readFile(`${path}/Preferences`, "utf-8");
|
|
29
|
-
const profile: ChromeProfile = await destr(content);
|
|
30
|
-
profiles.push(profile);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return profiles;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export type ChromeProfileAccountInfo = {
|
|
37
|
-
account_id: string;
|
|
38
|
-
accountcapabilities: any;
|
|
39
|
-
email: string;
|
|
40
|
-
full_name: string;
|
|
41
|
-
gaia: string;
|
|
42
|
-
given_name: string;
|
|
43
|
-
hd: string;
|
|
44
|
-
is_supervised_child: number;
|
|
45
|
-
is_under_advanced_protection: boolean;
|
|
46
|
-
last_downloaded_image_url_with_size: string;
|
|
47
|
-
locale: string;
|
|
48
|
-
picture_url: string;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export type ChromeProfile = {
|
|
52
|
-
account_info: ChromeProfileAccountInfo[];
|
|
53
|
-
};
|
package/src/logger.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import ExportedCookie from "./ExportedCookie";
|
|
2
|
-
import { uniqBy } from "lodash";
|
|
3
|
-
import CookieQueryStrategy from "./browsers/CookieQueryStrategy";
|
|
4
|
-
import { CookieQueryOptions } from "./cookieQueryOptions";
|
|
5
|
-
|
|
6
|
-
export function processBeforeReturn<T extends CookieQueryStrategy>(
|
|
7
|
-
cookies: ExportedCookie[],
|
|
8
|
-
options?: CookieQueryOptions<T>,
|
|
9
|
-
): ExportedCookie[] {
|
|
10
|
-
let processedCookies: ExportedCookie[] = cookies;
|
|
11
|
-
|
|
12
|
-
if (options && options.removeExpired) {
|
|
13
|
-
const now: number = Date.now();
|
|
14
|
-
processedCookies = processedCookies.filter((c: ExportedCookie) => {
|
|
15
|
-
const expiry: Date | "Infinity" | undefined = c.expiry;
|
|
16
|
-
return (
|
|
17
|
-
expiry === undefined || expiry === "Infinity" || expiry.getTime() > now
|
|
18
|
-
);
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (options && options.limit) {
|
|
23
|
-
processedCookies = processedCookies.slice(0, options.limit);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return uniqBy(processedCookies, (cookie: ExportedCookie) =>
|
|
27
|
-
JSON.stringify(cookie),
|
|
28
|
-
);
|
|
29
|
-
}
|
package/src/queryCookies.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { uniqBy } from "lodash";
|
|
2
|
-
import { parsedArgs } from "./argv";
|
|
3
|
-
import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
|
|
4
|
-
import CookieQueryStrategy from "./browsers/CookieQueryStrategy";
|
|
5
|
-
import isValidJwt from "./isValidJwt";
|
|
6
|
-
import CookieSpec from "./CookieSpec";
|
|
7
|
-
import ExportedCookie from "./ExportedCookie";
|
|
8
|
-
import { CookieQueryOptions } from "./cookieQueryOptions";
|
|
9
|
-
import consola from "./logger";
|
|
10
|
-
|
|
11
|
-
export async function queryCookies(
|
|
12
|
-
{ name, domain }: CookieSpec,
|
|
13
|
-
options?: CookieQueryOptions<CookieQueryStrategy>,
|
|
14
|
-
): Promise<ExportedCookie[]> {
|
|
15
|
-
const strategy: CookieQueryStrategy =
|
|
16
|
-
options?.strategy || new CompositeCookieQueryStrategy();
|
|
17
|
-
|
|
18
|
-
consola.debug(`Using strategy: ${strategy.browserName}`);
|
|
19
|
-
|
|
20
|
-
const results: ExportedCookie[] = await strategy.queryCookies(name, domain);
|
|
21
|
-
const allCookies: ExportedCookie[] = uniqBy(results, JSON.stringify);
|
|
22
|
-
|
|
23
|
-
const filterCookies = (
|
|
24
|
-
cookies: ExportedCookie[],
|
|
25
|
-
filterFn: (value: string) => boolean,
|
|
26
|
-
): ExportedCookie[] => {
|
|
27
|
-
const filteredCookies: ExportedCookie[] = [];
|
|
28
|
-
for (const cookie of cookies) {
|
|
29
|
-
if (filterFn(cookie.value)) {
|
|
30
|
-
filteredCookies.push(cookie);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return filteredCookies;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const jwtCookies: ExportedCookie[] = parsedArgs["require-jwt"]
|
|
37
|
-
? filterCookies(allCookies, isValidJwt)
|
|
38
|
-
: allCookies;
|
|
39
|
-
|
|
40
|
-
return parsedArgs["single"] ? [jwtCookies[0]] : jwtCookies;
|
|
41
|
-
}
|
package/src/resultsRendered.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { orderBy, uniqBy } from "lodash";
|
|
2
|
-
import ExportedCookie from "./ExportedCookie";
|
|
3
|
-
|
|
4
|
-
function sortResults(results: ExportedCookie[]): ExportedCookie[] {
|
|
5
|
-
return orderBy(results, ["name", "expiry"], ["asc", "desc"]);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
function getUniqueResults(results: ExportedCookie[]): ExportedCookie[] {
|
|
9
|
-
return uniqBy(results, "name");
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function formatResults(results: ExportedCookie[]): string {
|
|
13
|
-
const resultStrings: string[] = [];
|
|
14
|
-
for (const result of results) {
|
|
15
|
-
resultStrings.push(`${result.name}=${result.value}`);
|
|
16
|
-
}
|
|
17
|
-
return resultStrings.join("; ");
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function resultsRendered(results: ExportedCookie[]): string {
|
|
21
|
-
const orderedResults: ExportedCookie[] = sortResults(results);
|
|
22
|
-
const uniqueResults: ExportedCookie[] = getUniqueResults(orderedResults);
|
|
23
|
-
return formatResults(uniqueResults);
|
|
24
|
-
}
|
package/src/unpackHeaders.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export function unpackHeaders(
|
|
2
|
-
headerArgs: string[] | string | null,
|
|
3
|
-
): Record<string, string> {
|
|
4
|
-
const headers: Record<string, string> = {};
|
|
5
|
-
if (headerArgs === null) {
|
|
6
|
-
return headers;
|
|
7
|
-
}
|
|
8
|
-
if (Array.isArray(headerArgs)) {
|
|
9
|
-
for (let i = 0; i < headerArgs.length; i++) {
|
|
10
|
-
const [key, value] = headerArgs[i].split("=");
|
|
11
|
-
headers[key] = value;
|
|
12
|
-
}
|
|
13
|
-
} else {
|
|
14
|
-
const [key, value] = headerArgs.split("=");
|
|
15
|
-
headers[key] = value;
|
|
16
|
-
}
|
|
17
|
-
return headers;
|
|
18
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { flatMapAsync } from "./flatMapAsync";
|
|
2
|
-
|
|
3
|
-
describe("flatMapAsync", () => {
|
|
4
|
-
it("should apply async callback to each element, wait for all promises to fulfill and flatten the resulting array", async () => {
|
|
5
|
-
const array = [1, 2, 3];
|
|
6
|
-
const callback = async (value: number) => [value, value * 2];
|
|
7
|
-
const result = await flatMapAsync(array, callback);
|
|
8
|
-
expect(result).toEqual([1, 2, 2, 4, 3, 6]);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it("should handle errors with the provided error callback", async () => {
|
|
12
|
-
const array = [1, 2, 3];
|
|
13
|
-
const callback = async (value: number) => {
|
|
14
|
-
if (value === 2) {
|
|
15
|
-
throw new Error("Test error");
|
|
16
|
-
}
|
|
17
|
-
return [value, value * 2];
|
|
18
|
-
};
|
|
19
|
-
const errorCallback = (error: any) => [0];
|
|
20
|
-
const result = await flatMapAsync(array, callback, errorCallback);
|
|
21
|
-
expect(result).toEqual([1, 2, 0, 3, 6]);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it("should return an empty array when input array is empty", async () => {
|
|
25
|
-
const array: number[] = [];
|
|
26
|
-
const callback = async (value: number) => [value, value * 2];
|
|
27
|
-
const result = await flatMapAsync(array, callback);
|
|
28
|
-
expect(result).toEqual([]);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("should return an array with single element when input array has one element", async () => {
|
|
32
|
-
const array = [1];
|
|
33
|
-
const callback = async (value: number) => [value, value * 2];
|
|
34
|
-
const result = await flatMapAsync(array, callback);
|
|
35
|
-
expect(result).toEqual([1, 2]);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("should handle null values in the array", async () => {
|
|
39
|
-
const array = [1, null, 3];
|
|
40
|
-
const callback = async (value: number) =>
|
|
41
|
-
value ? [value, value * 2] : [0];
|
|
42
|
-
// @ts-expect-error
|
|
43
|
-
const result = await flatMapAsync(array, callback);
|
|
44
|
-
expect(result).toEqual([1, 2, 0, 3, 6]);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("should handle undefined values in the array", async () => {
|
|
48
|
-
const array = [1, undefined, 3];
|
|
49
|
-
const callback = async (value: number) =>
|
|
50
|
-
value ? [value, value * 2] : [0];
|
|
51
|
-
// @ts-expect-error
|
|
52
|
-
const result = await flatMapAsync(array, callback);
|
|
53
|
-
expect(result).toEqual([1, 2, 0, 3, 6]);
|
|
54
|
-
});
|
|
55
|
-
});
|
package/src/util/flatMapAsync.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// This utility function applies an async callback to each element of the input array (just like Array.prototype.map),
|
|
2
|
-
// waits for all resulting promises to fulfill (similar to Promise.all), and then flattens the resulting array (like Array.prototype.flat).
|
|
3
|
-
|
|
4
|
-
export async function flatMapAsync<T, O>(
|
|
5
|
-
array: T[], // The input array to transform.
|
|
6
|
-
callback: (value: T, index: number, array: T[]) => Promise<O[]>, // The async function to apply to each input array element.
|
|
7
|
-
or?: O[] | ((error: any) => O[] | Promise<O[]>), // An optional callback to handle errors.
|
|
8
|
-
): Promise<O[]> {
|
|
9
|
-
if (or) {
|
|
10
|
-
const errorHandlingCallback = async (
|
|
11
|
-
value: T,
|
|
12
|
-
index: number,
|
|
13
|
-
array: T[],
|
|
14
|
-
): Promise<O[]> => {
|
|
15
|
-
try {
|
|
16
|
-
return await callback(value, index, array);
|
|
17
|
-
} catch (error) {
|
|
18
|
-
return typeof or === "function" ? await or(error) : or;
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
return flatMapAsync(array, errorHandlingCallback);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Step 1: Use a for loop to transform each element of the input array with the provided callback.
|
|
25
|
-
// This will result in an array of promises.
|
|
26
|
-
const promises: Promise<O[]>[] = [];
|
|
27
|
-
for (let i = 0; i < array.length; i++) {
|
|
28
|
-
promises.push(callback(array[i], i, array));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Step 2: Use Promise.all to wait for all promises in the array to fulfill.
|
|
32
|
-
// This will give us an array of fulfilled promise values.
|
|
33
|
-
const awaitedAll: O[][] = await Promise.all(promises);
|
|
34
|
-
|
|
35
|
-
// Step 3: Flatten the array of fulfilled promise values into a single array.
|
|
36
|
-
// Note: The '@ts-ignore' comment is used to suppress TypeScript compiler warnings, as .flat() method might not be recognized
|
|
37
|
-
// as a valid method for an array of Awaited<O> instances. However, this code assumes that the result of Promise.all
|
|
38
|
-
// is indeed an array that can be flattened.
|
|
39
|
-
// @ts-ignore
|
|
40
|
-
return awaitedAll.flat();
|
|
41
|
-
}
|
package/src/util/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./flatMapAsync";
|
package/tsconfig.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"include": [
|
|
3
|
-
"src/**.ts",
|
|
4
|
-
"src/**.tsx",
|
|
5
|
-
"src/**.js",
|
|
6
|
-
"src/**.jsx",
|
|
7
|
-
"src/**.test.ts",
|
|
8
|
-
"src/**.test.js",
|
|
9
|
-
],
|
|
10
|
-
"compilerOptions": {
|
|
11
|
-
"moduleResolution": "node",
|
|
12
|
-
"target": "es6",
|
|
13
|
-
"types": ["node", "jest", "bun-types"],
|
|
14
|
-
"lib": ["es2015", "es2017", "es2021", "dom"],
|
|
15
|
-
"strict": true,
|
|
16
|
-
"skipDefaultLibCheck": true,
|
|
17
|
-
"skipLibCheck": true,
|
|
18
|
-
"allowSyntheticDefaultImports": true,
|
|
19
|
-
"emitDeclarationOnly": true,
|
|
20
|
-
"declaration": true,
|
|
21
|
-
"esModuleInterop": true,
|
|
22
|
-
"module": "commonjs",
|
|
23
|
-
"outDir": "dist",
|
|
24
|
-
"outFile": "dist/types.d.ts",
|
|
25
|
-
},
|
|
26
|
-
}
|