@mherod/get-cookie 2.0.0-rc.3 → 2.0.0-rc.4
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/dist/index.js +79 -24
- package/package.json +3 -2
- package/src/CookieSpec.ts +1 -1
- package/src/CookieStore.ts +10 -0
- package/src/StringToRegex.ts +6 -0
- package/src/browsers/ChromeCookieQueryStrategy.ts +14 -5
- package/src/browsers/CompositeCookieQueryStrategy.ts +13 -5
- package/src/browsers/CookieQueryStrategy.ts +1 -0
- package/src/browsers/CookieStoreQueryStrategy.ts +88 -0
- package/src/browsers/FirefoxCookieQueryStrategy.ts +2 -0
- package/src/browsers/SafariCookieQueryStrategy.ts +2 -0
- package/src/fetchWithCookies.ts +1 -1
- package/src/index.ts +1 -0
- package/src/MemoryCookieStore.ts +0 -5
- package/src/browsers/MemoryCookieJarQueryStrategy.ts +0 -53
package/dist/index.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
var $7oI3p$lodash = require("lodash");
|
|
3
3
|
var $7oI3p$lrucache = require("lru-cache");
|
|
4
|
+
var $7oI3p$colorette = require("colorette");
|
|
4
5
|
var $7oI3p$fs = require("fs");
|
|
5
6
|
var $7oI3p$crypto = require("crypto");
|
|
6
7
|
var $7oI3p$path = require("path");
|
|
7
8
|
var $7oI3p$child_process = require("child_process");
|
|
8
9
|
var $7oI3p$sqlite3 = require("sqlite3");
|
|
9
10
|
var $7oI3p$toughcookie = require("tough-cookie");
|
|
11
|
+
var $7oI3p$toughcookiefilestore = require("tough-cookie-file-store");
|
|
10
12
|
var $7oI3p$jsonwebtoken = require("jsonwebtoken");
|
|
11
13
|
var $7oI3p$crossfetch = require("cross-fetch");
|
|
12
14
|
var $7oI3p$destr = require("destr");
|
|
@@ -39,6 +41,7 @@ $parcel$export(module.exports, "getFirefoxCookie", () => $cd1ba0160120c006$expor
|
|
|
39
41
|
$parcel$export(module.exports, "getChromeCookie", () => $cd1ba0160120c006$export$e5637297e9eb6f2e);
|
|
40
42
|
$parcel$export(module.exports, "getGroupedRenderedCookies", () => $300d8b33187a203c$export$4d2e9997e2c23417);
|
|
41
43
|
$parcel$export(module.exports, "fetchWithCookies", () => $cfc07ae4aa2c7e44$export$b24a545a0b39f491);
|
|
44
|
+
$parcel$export(module.exports, "stringToRegex", () => $68c0ed99fadee6ca$export$4eadcf0bf35a2ddb);
|
|
42
45
|
|
|
43
46
|
const $fac425c10fbbada5$export$a7b6bc01c63cdfc3 = {};
|
|
44
47
|
(0, $7oI3p$lodash.merge)($fac425c10fbbada5$export$a7b6bc01c63cdfc3, process.env);
|
|
@@ -181,7 +184,16 @@ function $d66f40b581ba8bfa$export$963c8d651337e4a4(obj) {
|
|
|
181
184
|
}
|
|
182
185
|
|
|
183
186
|
|
|
187
|
+
function $68c0ed99fadee6ca$export$4eadcf0bf35a2ddb(s) {
|
|
188
|
+
const s1 = s.replaceAll(/\./gi, ".");
|
|
189
|
+
const s2 = s1.replace(/%/g, ".*");
|
|
190
|
+
const s3 = s2.replace(/\*/g, ".*");
|
|
191
|
+
return new RegExp(s3);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
|
|
184
195
|
class $269fe42bfd555305$export$2e2bcd8739ae039 {
|
|
196
|
+
browserName = "Chrome";
|
|
185
197
|
async queryCookies(name, domain) {
|
|
186
198
|
if (process.platform !== "darwin") throw new Error("This only works on macOS");
|
|
187
199
|
if ((0, $fac425c10fbbada5$export$a7b6bc01c63cdfc3).FIREFOX_ONLY) return [];
|
|
@@ -260,15 +272,18 @@ async function $269fe42bfd555305$var$getEncryptedChromeCookie({ name: name , dom
|
|
|
260
272
|
const wildcardRegexp = /^([*%])$/i;
|
|
261
273
|
const specifiedName = name.match(wildcardRegexp) == null;
|
|
262
274
|
const specifiedDomain = domain.match(wildcardRegexp) == null;
|
|
263
|
-
|
|
275
|
+
const wildcardDomain = domain.match(/[%*]/) != null;
|
|
276
|
+
const queryDomain = specifiedDomain && !wildcardDomain;
|
|
277
|
+
// if we have a wildcard domain, we need to use a regexp
|
|
278
|
+
if (specifiedName || queryDomain) {
|
|
264
279
|
sql += ` WHERE `;
|
|
265
280
|
if (specifiedName) {
|
|
266
281
|
sql += `name = '${name}'`;
|
|
267
|
-
if (
|
|
282
|
+
if (queryDomain) sql += ` AND `;
|
|
268
283
|
}
|
|
269
|
-
if (
|
|
284
|
+
if (queryDomain) sql += `host_key LIKE '${domain}';`;
|
|
270
285
|
}
|
|
271
|
-
|
|
286
|
+
const sqliteQuery1 = await (0, $a085b524d8ee9fce$export$f19f611d1fa7c6f3)({
|
|
272
287
|
file: file,
|
|
273
288
|
sql: sql,
|
|
274
289
|
rowTransform: (row)=>{
|
|
@@ -279,6 +294,9 @@ async function $269fe42bfd555305$var$getEncryptedChromeCookie({ name: name , dom
|
|
|
279
294
|
};
|
|
280
295
|
}
|
|
281
296
|
});
|
|
297
|
+
return sqliteQuery1.filter((row)=>{
|
|
298
|
+
return row.domain.match((0, $68c0ed99fadee6ca$export$4eadcf0bf35a2ddb)(domain)) != null;
|
|
299
|
+
});
|
|
282
300
|
}
|
|
283
301
|
async function $269fe42bfd555305$var$getChromePassword() {
|
|
284
302
|
return (0, $25b78d6746d4ad48$export$8d71c74f97c0202e)('security find-generic-password -w -s "Chrome Safe Storage"');
|
|
@@ -355,6 +373,7 @@ function $b744d19cc39f964e$export$da22813e5a2ec500({ name: name , domain: domain
|
|
|
355
373
|
|
|
356
374
|
|
|
357
375
|
class $7bb7da9a77b2fd99$export$2e2bcd8739ae039 {
|
|
376
|
+
browserName = "Firefox";
|
|
358
377
|
async queryCookies(name, domain) {
|
|
359
378
|
if (process.platform !== "darwin") throw new Error("This only works on macOS");
|
|
360
379
|
if ((0, $fac425c10fbbada5$export$a7b6bc01c63cdfc3).CHROME_ONLY) return [];
|
|
@@ -424,6 +443,7 @@ class $7bb7da9a77b2fd99$export$2e2bcd8739ae039 {
|
|
|
424
443
|
|
|
425
444
|
|
|
426
445
|
class $fad6f51bd5c7bae2$export$2e2bcd8739ae039 {
|
|
446
|
+
browserName = "Safari";
|
|
427
447
|
async queryCookies(name, domain) {
|
|
428
448
|
return [];
|
|
429
449
|
}
|
|
@@ -432,39 +452,57 @@ class $fad6f51bd5c7bae2$export$2e2bcd8739ae039 {
|
|
|
432
452
|
|
|
433
453
|
|
|
434
454
|
|
|
435
|
-
const $0fd79d7b9f7bb064$export$231f8b179925e6a5 = new (0, $7oI3p$toughcookie.MemoryCookieStore)();
|
|
436
|
-
const $0fd79d7b9f7bb064$export$928a906645f9d018 = new (0, $7oI3p$toughcookie.CookieJar)($0fd79d7b9f7bb064$export$231f8b179925e6a5);
|
|
437
455
|
|
|
438
456
|
|
|
439
|
-
|
|
457
|
+
var $53a3bcc99a05dfcf$require$FileCookieStore = $7oI3p$toughcookiefilestore.FileCookieStore;
|
|
458
|
+
const $53a3bcc99a05dfcf$export$6c62df16f2e9c7e8 = new $53a3bcc99a05dfcf$require$FileCookieStore(`${(0, $fac425c10fbbada5$export$a7b6bc01c63cdfc3)["HOME"]}/cookie-star.json`);
|
|
459
|
+
const $53a3bcc99a05dfcf$export$928a906645f9d018 = new (0, $7oI3p$toughcookie.CookieJar)($53a3bcc99a05dfcf$export$6c62df16f2e9c7e8);
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
class $e4efb16e005540de$export$2e2bcd8739ae039 {
|
|
464
|
+
browserName = "internal";
|
|
440
465
|
async queryCookies(name, domain) {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
466
|
+
const exportedCookies = [];
|
|
467
|
+
if (name.match(/[%*]/) || domain.match(/[%*]/)) {
|
|
468
|
+
const cookies = await this.#getAllCookies();
|
|
469
|
+
const allExportedCookies = cookies.map((cookie)=>{
|
|
444
470
|
return this.#extracted(cookie, {
|
|
445
471
|
name: name,
|
|
446
472
|
domain: domain
|
|
447
473
|
});
|
|
448
474
|
});
|
|
475
|
+
exportedCookies.push(...allExportedCookies);
|
|
449
476
|
}
|
|
477
|
+
if (name == "%" && domain == "%") return exportedCookies;
|
|
450
478
|
const path = "/";
|
|
451
|
-
if (
|
|
452
|
-
const
|
|
453
|
-
|
|
479
|
+
if (domain != "%") {
|
|
480
|
+
const domain1 = domain.match(/(\w+.+\w+)/gi)?.pop() ?? domain;
|
|
481
|
+
const url = new URL("https://" + domain1);
|
|
482
|
+
url.pathname = path;
|
|
483
|
+
const cookies1 = await (0, $53a3bcc99a05dfcf$export$928a906645f9d018).getCookies(url.href);
|
|
484
|
+
const domainCookies = cookies1.map((cookie)=>{
|
|
454
485
|
return this.#extracted(cookie, {
|
|
455
|
-
|
|
456
|
-
|
|
486
|
+
domain: domain,
|
|
487
|
+
name: name
|
|
457
488
|
});
|
|
458
489
|
});
|
|
490
|
+
exportedCookies.push(...domainCookies);
|
|
459
491
|
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
492
|
+
if (name == "%") return exportedCookies;
|
|
493
|
+
const cookie = await (0, $53a3bcc99a05dfcf$export$6c62df16f2e9c7e8).findCookie(domain, path, name);
|
|
494
|
+
if (cookie) {
|
|
495
|
+
const singleCookie = this.#extracted(cookie, {
|
|
463
496
|
name: name,
|
|
464
497
|
domain: domain
|
|
465
|
-
})
|
|
466
|
-
|
|
467
|
-
|
|
498
|
+
});
|
|
499
|
+
return [
|
|
500
|
+
singleCookie
|
|
501
|
+
];
|
|
502
|
+
}
|
|
503
|
+
return exportedCookies.filter((cookie)=>{
|
|
504
|
+
return cookie.name.match((0, $68c0ed99fadee6ca$export$4eadcf0bf35a2ddb)(name)) && cookie.domain.match((0, $68c0ed99fadee6ca$export$4eadcf0bf35a2ddb)(domain));
|
|
505
|
+
});
|
|
468
506
|
}
|
|
469
507
|
#extracted(cookie, cookieSpec) {
|
|
470
508
|
return {
|
|
@@ -476,18 +514,29 @@ class $0d3ccbe1f68c2e3d$export$2e2bcd8739ae039 {
|
|
|
476
514
|
}
|
|
477
515
|
};
|
|
478
516
|
}
|
|
517
|
+
#getAllCookies() {
|
|
518
|
+
return new Promise((resolve, reject)=>{
|
|
519
|
+
// @ts-ignore
|
|
520
|
+
return (0, $53a3bcc99a05dfcf$export$6c62df16f2e9c7e8).getAllCookies((err, cookies)=>{
|
|
521
|
+
if (err) reject(err);
|
|
522
|
+
else resolve(cookies ?? []);
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
}
|
|
479
526
|
}
|
|
480
527
|
|
|
481
528
|
|
|
529
|
+
|
|
482
530
|
const $f72befa528c23ca7$var$cache = new (0, ($parcel$interopDefault($7oI3p$lrucache)))({
|
|
483
531
|
ttl: 2000,
|
|
484
532
|
max: 10
|
|
485
533
|
});
|
|
486
534
|
class $f72befa528c23ca7$export$2e2bcd8739ae039 {
|
|
535
|
+
browserName = "all";
|
|
487
536
|
#strategies;
|
|
488
537
|
constructor(){
|
|
489
538
|
this.#strategies = [
|
|
490
|
-
(0, $
|
|
539
|
+
(0, $e4efb16e005540de$export$2e2bcd8739ae039),
|
|
491
540
|
(0, $269fe42bfd555305$export$2e2bcd8739ae039),
|
|
492
541
|
(0, $7bb7da9a77b2fd99$export$2e2bcd8739ae039),
|
|
493
542
|
(0, $fad6f51bd5c7bae2$export$2e2bcd8739ae039),
|
|
@@ -496,13 +545,18 @@ class $f72befa528c23ca7$export$2e2bcd8739ae039 {
|
|
|
496
545
|
});
|
|
497
546
|
}
|
|
498
547
|
async queryCookies(name, domain) {
|
|
548
|
+
// domain = domain.match(/(\w+.+\w+)/gi)?.pop() ?? domain;
|
|
499
549
|
const key = `${name}:${domain}`;
|
|
500
550
|
const cached = $f72befa528c23ca7$var$cache.get(key);
|
|
501
551
|
if (cached) return cached;
|
|
552
|
+
console.log("Querying cookies:", name, domain);
|
|
502
553
|
const results = await Promise.all(this.#strategies.map(async (strategy)=>{
|
|
503
554
|
// @ts-ignore
|
|
504
555
|
const cookies = strategy.queryCookies(name, domain);
|
|
505
|
-
return cookies.catch(()=>
|
|
556
|
+
return cookies.catch((e)=>{
|
|
557
|
+
console.log((0, $7oI3p$colorette.red)(`Error querying ${strategy.browserName} cookies`), e);
|
|
558
|
+
return [];
|
|
559
|
+
});
|
|
506
560
|
}));
|
|
507
561
|
const flat = results.flat();
|
|
508
562
|
$f72befa528c23ca7$var$cache.set(`${name}:${domain}`, flat);
|
|
@@ -618,7 +672,7 @@ async function $cfc07ae4aa2c7e44$export$b24a545a0b39f491(url, options = {}, fetc
|
|
|
618
672
|
value
|
|
619
673
|
]);
|
|
620
674
|
});
|
|
621
|
-
for (const [key, value] of headers)if (key === "set-cookie") await (0, $
|
|
675
|
+
for (const [key, value] of headers)if (key === "set-cookie") await (0, $53a3bcc99a05dfcf$export$928a906645f9d018).setCookie(value, url2);
|
|
622
676
|
const newUrl = res.headers.get("location");
|
|
623
677
|
if (res.redirected || newUrl && newUrl !== url2) return $cfc07ae4aa2c7e44$export$b24a545a0b39f491(newUrl, newOptions1);
|
|
624
678
|
const arrayBuffer1 = res.arrayBuffer();
|
|
@@ -664,6 +718,7 @@ var $3d7e27b54f670f46$exports = {};
|
|
|
664
718
|
var $6c197c88880e045c$exports = {};
|
|
665
719
|
|
|
666
720
|
|
|
721
|
+
|
|
667
722
|
async function $cd1ba0160120c006$export$4be65e66cfa2648a(params) {
|
|
668
723
|
const cookies = await (0, $7a068a32d4710f0b$export$e6d428437cf2cacb)(params, new (0, $f72befa528c23ca7$export$2e2bcd8739ae039)());
|
|
669
724
|
if (Array.isArray(cookies) && cookies.length > 0) return cookies.find((cookie)=>cookie != null);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mherod/get-cookie",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.4",
|
|
4
4
|
"description": "Node.js module for querying a local user's Chrome cookie",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"bin": "dist/cli.js",
|
|
@@ -67,7 +67,8 @@
|
|
|
67
67
|
"lru-cache": "^7.14.0",
|
|
68
68
|
"minimist": "^1.2.6",
|
|
69
69
|
"sqlite3": "^5.1.1",
|
|
70
|
-
"tough-cookie": "^4.1.2"
|
|
70
|
+
"tough-cookie": "^4.1.2",
|
|
71
|
+
"tough-cookie-file-store": "^2.0.3"
|
|
71
72
|
},
|
|
72
73
|
"devDependencies": {
|
|
73
74
|
"@parcel/packager-ts": "^2.7.0",
|
package/src/CookieSpec.ts
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CookieJar } from "tough-cookie";
|
|
2
|
+
import { env } from "./global";
|
|
3
|
+
const { FileCookieStore } = require("tough-cookie-file-store");
|
|
4
|
+
|
|
5
|
+
export const cookieStore = new FileCookieStore(
|
|
6
|
+
`${env["HOME"]}/cookie-star.json`
|
|
7
|
+
);
|
|
8
|
+
// export const cookieStore = new MemoryCookieStore();
|
|
9
|
+
|
|
10
|
+
export const cookieJar = new CookieJar(cookieStore);
|
|
@@ -11,8 +11,11 @@ import { isCookieRow } from "../IsCookieRow";
|
|
|
11
11
|
import { isExportedCookie } from "../IsExportedCookie";
|
|
12
12
|
import CookieRow from "../CookieRow";
|
|
13
13
|
import ExportedCookie from "../ExportedCookie";
|
|
14
|
+
import { stringToRegex } from "../StringToRegex";
|
|
14
15
|
|
|
15
16
|
export default class ChromeCookieQueryStrategy implements CookieQueryStrategy {
|
|
17
|
+
browserName = "Chrome";
|
|
18
|
+
|
|
16
19
|
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
17
20
|
if (process.platform !== "darwin") {
|
|
18
21
|
throw new Error("This only works on macOS");
|
|
@@ -56,7 +59,7 @@ async function getPromise(name: string, domain: string): Promise<CookieRow[]> {
|
|
|
56
59
|
const promises: Promise<CookieRow[]>[] = files.map((file) =>
|
|
57
60
|
getPromise1(name, domain, file)
|
|
58
61
|
);
|
|
59
|
-
const results1:
|
|
62
|
+
const results1: CookieRow[][] = await Promise.all(promises);
|
|
60
63
|
return results1.flat().filter(isCookieRow);
|
|
61
64
|
} catch (error) {
|
|
62
65
|
if (env.VERBOSE) {
|
|
@@ -146,19 +149,22 @@ async function getEncryptedChromeCookie({
|
|
|
146
149
|
const wildcardRegexp = /^([*%])$/i;
|
|
147
150
|
const specifiedName = name.match(wildcardRegexp) == null;
|
|
148
151
|
const specifiedDomain = domain.match(wildcardRegexp) == null;
|
|
149
|
-
|
|
152
|
+
const wildcardDomain = domain.match(/[%*]/) != null;
|
|
153
|
+
const queryDomain = specifiedDomain && !wildcardDomain;
|
|
154
|
+
// if we have a wildcard domain, we need to use a regexp
|
|
155
|
+
if (specifiedName || queryDomain) {
|
|
150
156
|
sql += ` WHERE `;
|
|
151
157
|
if (specifiedName) {
|
|
152
158
|
sql += `name = '${name}'`;
|
|
153
|
-
if (
|
|
159
|
+
if (queryDomain) {
|
|
154
160
|
sql += ` AND `;
|
|
155
161
|
}
|
|
156
162
|
}
|
|
157
|
-
if (
|
|
163
|
+
if (queryDomain) {
|
|
158
164
|
sql += `host_key LIKE '${domain}';`;
|
|
159
165
|
}
|
|
160
166
|
}
|
|
161
|
-
|
|
167
|
+
const sqliteQuery1: CookieRow[] = await doSqliteQuery1({
|
|
162
168
|
file: file,
|
|
163
169
|
sql: sql,
|
|
164
170
|
rowTransform: (row) => {
|
|
@@ -169,6 +175,9 @@ async function getEncryptedChromeCookie({
|
|
|
169
175
|
};
|
|
170
176
|
},
|
|
171
177
|
});
|
|
178
|
+
return sqliteQuery1.filter((row) => {
|
|
179
|
+
return row.domain.match(stringToRegex(domain)) != null;
|
|
180
|
+
});
|
|
172
181
|
}
|
|
173
182
|
|
|
174
183
|
async function getChromePassword(): Promise<string> {
|
|
@@ -4,7 +4,8 @@ import SafariCookieQueryStrategy from "./SafariCookieQueryStrategy";
|
|
|
4
4
|
import CookieQueryStrategy from "./CookieQueryStrategy";
|
|
5
5
|
import ExportedCookie from "../ExportedCookie";
|
|
6
6
|
import LRUCache from "lru-cache";
|
|
7
|
-
import
|
|
7
|
+
import CookieStoreQueryStrategy from "./CookieStoreQueryStrategy";
|
|
8
|
+
import { red } from "colorette";
|
|
8
9
|
|
|
9
10
|
const cache = new LRUCache<string, ExportedCookie[]>({
|
|
10
11
|
ttl: 1000 * 2,
|
|
@@ -14,11 +15,13 @@ const cache = new LRUCache<string, ExportedCookie[]>({
|
|
|
14
15
|
export default class CompositeCookieQueryStrategy
|
|
15
16
|
implements CookieQueryStrategy
|
|
16
17
|
{
|
|
18
|
+
browserName = "all";
|
|
19
|
+
|
|
17
20
|
#strategies;
|
|
18
21
|
|
|
19
22
|
constructor() {
|
|
20
23
|
this.#strategies = [
|
|
21
|
-
|
|
24
|
+
CookieStoreQueryStrategy,
|
|
22
25
|
ChromeCookieQueryStrategy,
|
|
23
26
|
FirefoxCookieQueryStrategy,
|
|
24
27
|
SafariCookieQueryStrategy,
|
|
@@ -28,19 +31,24 @@ export default class CompositeCookieQueryStrategy
|
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
34
|
+
// domain = domain.match(/(\w+.+\w+)/gi)?.pop() ?? domain;
|
|
31
35
|
const key = `${name}:${domain}`;
|
|
32
36
|
const cached = cache.get(key);
|
|
33
37
|
if (cached) {
|
|
34
38
|
return cached;
|
|
35
39
|
}
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
console.log("Querying cookies:", name, domain);
|
|
41
|
+
const results: ExportedCookie[][] = await Promise.all(
|
|
42
|
+
this.#strategies.map(async (strategy: CookieQueryStrategy) => {
|
|
38
43
|
// @ts-ignore
|
|
39
44
|
const cookies: Promise<ExportedCookie[]> = strategy.queryCookies(
|
|
40
45
|
name,
|
|
41
46
|
domain
|
|
42
47
|
);
|
|
43
|
-
return cookies.catch(() =>
|
|
48
|
+
return cookies.catch((e) => {
|
|
49
|
+
console.log(red(`Error querying ${strategy.browserName} cookies`), e);
|
|
50
|
+
return [];
|
|
51
|
+
});
|
|
44
52
|
})
|
|
45
53
|
);
|
|
46
54
|
const flat: ExportedCookie[] = results.flat();
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import CookieQueryStrategy from "./CookieQueryStrategy";
|
|
2
|
+
import CookieSpec from "../CookieSpec";
|
|
3
|
+
import ExportedCookie from "../ExportedCookie";
|
|
4
|
+
import { Cookie } from "tough-cookie";
|
|
5
|
+
import { cookieStore, cookieJar } from "../CookieStore";
|
|
6
|
+
import { stringToRegex } from "../StringToRegex";
|
|
7
|
+
|
|
8
|
+
export default class CookieStoreQueryStrategy implements CookieQueryStrategy {
|
|
9
|
+
browserName = "internal";
|
|
10
|
+
|
|
11
|
+
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
12
|
+
const exportedCookies: ExportedCookie[] = [];
|
|
13
|
+
|
|
14
|
+
if (name.match(/[%*]/) || domain.match(/[%*]/)) {
|
|
15
|
+
const cookies: Cookie[] = await this.#getAllCookies();
|
|
16
|
+
const allExportedCookies = cookies.map((cookie: Cookie) => {
|
|
17
|
+
return this.#extracted(cookie, { name, domain });
|
|
18
|
+
});
|
|
19
|
+
exportedCookies.push(...allExportedCookies);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (name == "%" && domain == "%") {
|
|
23
|
+
return exportedCookies;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const path = "/";
|
|
27
|
+
|
|
28
|
+
if (domain != "%") {
|
|
29
|
+
const domain1 = domain.match(/(\w+.+\w+)/gi)?.pop() ?? domain;
|
|
30
|
+
const url = new URL("https://" + domain1);
|
|
31
|
+
url.pathname = path;
|
|
32
|
+
const cookies: Cookie[] = await cookieJar.getCookies(url.href);
|
|
33
|
+
const domainCookies = cookies.map((cookie: Cookie) => {
|
|
34
|
+
return this.#extracted(cookie, {
|
|
35
|
+
domain,
|
|
36
|
+
name,
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
exportedCookies.push(...domainCookies);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (name == "%") {
|
|
43
|
+
return exportedCookies;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const cookie: Cookie | null = await cookieStore.findCookie(
|
|
47
|
+
domain,
|
|
48
|
+
path,
|
|
49
|
+
name
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
if (cookie) {
|
|
53
|
+
const singleCookie = this.#extracted(cookie, { name, domain });
|
|
54
|
+
return [singleCookie];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return exportedCookies.filter((cookie: ExportedCookie) => {
|
|
58
|
+
return (
|
|
59
|
+
cookie.name.match(stringToRegex(name)) &&
|
|
60
|
+
cookie.domain.match(stringToRegex(domain))
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#extracted(cookie: Cookie, cookieSpec: CookieSpec): ExportedCookie {
|
|
66
|
+
return {
|
|
67
|
+
domain: cookie.domain ?? cookieSpec.domain,
|
|
68
|
+
name: cookie.key ?? cookieSpec.name,
|
|
69
|
+
value: cookie.value,
|
|
70
|
+
meta: {
|
|
71
|
+
file: "memory",
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
#getAllCookies(): Promise<Cookie[]> {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
// @ts-ignore
|
|
79
|
+
return cookieStore.getAllCookies((err, cookies) => {
|
|
80
|
+
if (err) {
|
|
81
|
+
reject(err);
|
|
82
|
+
} else {
|
|
83
|
+
resolve(cookies ?? []);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -10,6 +10,8 @@ import CookieSpec from "../CookieSpec";
|
|
|
10
10
|
import { specialCases } from "../SpecialCases";
|
|
11
11
|
|
|
12
12
|
export default class FirefoxCookieQueryStrategy implements CookieQueryStrategy {
|
|
13
|
+
browserName = "Firefox";
|
|
14
|
+
|
|
13
15
|
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
14
16
|
if (process.platform !== "darwin") {
|
|
15
17
|
throw new Error("This only works on macOS");
|
|
@@ -2,6 +2,8 @@ import CookieQueryStrategy from "./CookieQueryStrategy";
|
|
|
2
2
|
import ExportedCookie from "../ExportedCookie";
|
|
3
3
|
|
|
4
4
|
export default class SafariCookieQueryStrategy implements CookieQueryStrategy {
|
|
5
|
+
browserName = "Safari";
|
|
6
|
+
|
|
5
7
|
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
6
8
|
return [];
|
|
7
9
|
}
|
package/src/fetchWithCookies.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { merge } from "lodash";
|
|
|
6
6
|
import destr from "destr";
|
|
7
7
|
import CookieSpec from "./CookieSpec";
|
|
8
8
|
import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
|
|
9
|
-
import { cookieJar } from "./
|
|
9
|
+
import { cookieJar } from "./CookieStore";
|
|
10
10
|
|
|
11
11
|
export async function fetchWithCookies(
|
|
12
12
|
url: RequestInfo | URL,
|
package/src/index.ts
CHANGED
package/src/MemoryCookieStore.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import ExportedCookie from "../ExportedCookie";
|
|
2
|
-
import CookieQueryStrategy from "./CookieQueryStrategy";
|
|
3
|
-
import { Cookie } from "tough-cookie";
|
|
4
|
-
import CookieSpec from "../CookieSpec";
|
|
5
|
-
import { memoryCookieStore } from "../MemoryCookieStore";
|
|
6
|
-
|
|
7
|
-
export default class MemoryCookieStoreQueryStrategy
|
|
8
|
-
implements CookieQueryStrategy
|
|
9
|
-
{
|
|
10
|
-
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
11
|
-
if (name == "%" && domain == "%") {
|
|
12
|
-
const cookies: Cookie[] = await memoryCookieStore.getAllCookies();
|
|
13
|
-
return cookies.map((cookie) => {
|
|
14
|
-
return this.#extracted(cookie, { name, domain });
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const path = "/";
|
|
19
|
-
|
|
20
|
-
if (name == "%") {
|
|
21
|
-
const cookies: Cookie[] = await memoryCookieStore.findCookies(
|
|
22
|
-
domain,
|
|
23
|
-
path
|
|
24
|
-
);
|
|
25
|
-
return cookies.map((cookie) => {
|
|
26
|
-
return this.#extracted(cookie, { name, domain });
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const cookie: Cookie | null = await memoryCookieStore.findCookie(
|
|
31
|
-
domain,
|
|
32
|
-
path,
|
|
33
|
-
name
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
if (cookie) {
|
|
37
|
-
return [this.#extracted(cookie, { name, domain })];
|
|
38
|
-
} else {
|
|
39
|
-
return [];
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
#extracted(cookie: Cookie, cookieSpec: CookieSpec): ExportedCookie {
|
|
44
|
-
return {
|
|
45
|
-
domain: cookie.domain ?? cookieSpec.domain,
|
|
46
|
-
name: cookie.key ?? cookieSpec.name,
|
|
47
|
-
value: cookie.value,
|
|
48
|
-
meta: {
|
|
49
|
-
file: "memory",
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
}
|