rezo 1.0.29 → 1.0.31
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/adapters/curl.cjs +8 -10
- package/dist/adapters/curl.js +8 -10
- package/dist/adapters/entries/curl.d.ts +169 -313
- package/dist/adapters/entries/fetch.d.ts +169 -313
- package/dist/adapters/entries/http.d.ts +169 -313
- package/dist/adapters/entries/http2.d.ts +169 -313
- package/dist/adapters/entries/react-native.d.ts +169 -313
- package/dist/adapters/entries/xhr.d.ts +169 -313
- package/dist/adapters/fetch.cjs +10 -7
- package/dist/adapters/fetch.js +10 -7
- package/dist/adapters/http.cjs +9 -12
- package/dist/adapters/http.js +9 -12
- package/dist/adapters/http2.cjs +6 -11
- package/dist/adapters/http2.js +6 -11
- package/dist/adapters/index.cjs +6 -6
- package/dist/adapters/react-native.cjs +4 -4
- package/dist/adapters/react-native.js +4 -4
- package/dist/adapters/xhr.cjs +4 -4
- package/dist/adapters/xhr.js +4 -4
- package/dist/cache/index.cjs +13 -13
- package/dist/cache/universal-response-cache.cjs +156 -0
- package/dist/cache/universal-response-cache.js +155 -0
- package/dist/core/rezo.cjs +2 -8
- package/dist/core/rezo.js +2 -8
- package/dist/crawler.d.ts +163 -313
- package/dist/entries/crawler.cjs +5 -5
- package/dist/index.cjs +24 -24
- package/dist/index.d.ts +169 -313
- package/dist/platform/browser.d.ts +169 -313
- package/dist/platform/bun.d.ts +169 -313
- package/dist/platform/deno.d.ts +169 -313
- package/dist/platform/node.d.ts +169 -313
- package/dist/platform/react-native.d.ts +169 -313
- package/dist/platform/worker.d.ts +169 -313
- package/dist/plugin/crawler.cjs +1 -1
- package/dist/plugin/crawler.js +1 -1
- package/dist/plugin/index.cjs +36 -36
- package/dist/proxy/index.cjs +5 -80
- package/dist/proxy/index.js +2 -77
- package/dist/proxy/parse.cjs +79 -0
- package/dist/proxy/parse.js +77 -0
- package/dist/queue/index.cjs +8 -8
- package/dist/responses/buildResponse.cjs +15 -15
- package/dist/responses/buildResponse.js +15 -15
- package/dist/responses/universal/download.cjs +23 -0
- package/dist/responses/universal/download.js +22 -0
- package/dist/responses/universal/event-emitter.cjs +104 -0
- package/dist/responses/universal/event-emitter.js +102 -0
- package/dist/responses/universal/index.cjs +11 -0
- package/dist/responses/universal/index.js +4 -0
- package/dist/responses/universal/stream.cjs +32 -0
- package/dist/responses/universal/stream.js +31 -0
- package/dist/responses/universal/upload.cjs +23 -0
- package/dist/responses/universal/upload.js +22 -0
- package/dist/utils/cookies.browser.cjs +63 -0
- package/dist/utils/cookies.browser.js +61 -0
- package/dist/utils/form-data.cjs +212 -189
- package/dist/utils/form-data.js +212 -189
- package/dist/utils/http-config.cjs +41 -21
- package/dist/utils/http-config.js +41 -21
- package/package.json +11 -4
- package/dist/types/cookies.cjs +0 -394
- package/dist/types/cookies.js +0 -391
|
@@ -2,9 +2,12 @@ import { RezoCookieJar } from './cookies.js';
|
|
|
2
2
|
import RezoFormData from './form-data.js';
|
|
3
3
|
import { RezoHeaders } from './headers.js';
|
|
4
4
|
import { RezoURLSearchParams } from './data-operations.js';
|
|
5
|
-
import
|
|
6
|
-
import { parseProxyString } from '../proxy/index.js';
|
|
5
|
+
import { parseProxyString } from '../proxy/parse.js';
|
|
7
6
|
import { createDefaultHooks, mergeHooks, serializeHooks } from '../core/hooks.js';
|
|
7
|
+
const hasBuffer = typeof Buffer !== "undefined";
|
|
8
|
+
function isBuffer(value) {
|
|
9
|
+
return hasBuffer && Buffer.isBuffer(value);
|
|
10
|
+
}
|
|
8
11
|
export const ERROR_INFO = {
|
|
9
12
|
ECONNREFUSED: {
|
|
10
13
|
code: -111,
|
|
@@ -314,7 +317,6 @@ export function prepareHTTPOptions(options, jar, addedOptions, config) {
|
|
|
314
317
|
if (options.formData || options.multipart) {
|
|
315
318
|
if (options.multipart instanceof RezoFormData || options.formData instanceof RezoFormData) {
|
|
316
319
|
const body = options.multipart instanceof RezoFormData ? options.multipart : options.formData;
|
|
317
|
-
contentType = body.getContentType();
|
|
318
320
|
fetchOptions.body = body;
|
|
319
321
|
} else {
|
|
320
322
|
const body = new RezoFormData;
|
|
@@ -323,27 +325,31 @@ export function prepareHTTPOptions(options, jar, addedOptions, config) {
|
|
|
323
325
|
Object.entries(_body).forEach(([key, value]) => {
|
|
324
326
|
if (value === null || value === undefined) {
|
|
325
327
|
body.append(key, "");
|
|
326
|
-
} else if (typeof value === "string"
|
|
328
|
+
} else if (typeof value === "string") {
|
|
329
|
+
body.append(key, value);
|
|
330
|
+
} else if (isBuffer(value)) {
|
|
327
331
|
body.append(key, value);
|
|
328
|
-
} else if (
|
|
332
|
+
} else if (value instanceof Blob) {
|
|
329
333
|
body.append(key, value);
|
|
330
334
|
} else if (typeof value === "object" && value.value !== undefined) {
|
|
331
335
|
const val = value.value;
|
|
332
|
-
const
|
|
333
|
-
if (typeof val === "string"
|
|
334
|
-
body.append(key, val
|
|
336
|
+
const filename = value.options?.filename || value.filename;
|
|
337
|
+
if (typeof val === "string") {
|
|
338
|
+
body.append(key, val);
|
|
339
|
+
} else if (isBuffer(val)) {
|
|
340
|
+
body.append(key, val, filename);
|
|
341
|
+
} else if (val instanceof Blob) {
|
|
342
|
+
body.append(key, val, filename);
|
|
335
343
|
} else {
|
|
336
|
-
body.append(key, String(val)
|
|
344
|
+
body.append(key, String(val));
|
|
337
345
|
}
|
|
338
346
|
} else {
|
|
339
347
|
body.append(key, String(value));
|
|
340
348
|
}
|
|
341
349
|
});
|
|
342
350
|
}
|
|
343
|
-
contentType = body.getContentType();
|
|
344
351
|
fetchOptions.body = body;
|
|
345
352
|
}
|
|
346
|
-
fetchOptions.headers.set("Content-Type", contentType);
|
|
347
353
|
} else if (options.form) {
|
|
348
354
|
contentType = "application/x-www-form-urlencoded";
|
|
349
355
|
if (typeof options.form === "object") {
|
|
@@ -375,17 +381,23 @@ export function prepareHTTPOptions(options, jar, addedOptions, config) {
|
|
|
375
381
|
Object.entries(fetchOptions.body).forEach(([key, value]) => {
|
|
376
382
|
if (value === null || value === undefined) {
|
|
377
383
|
formData.append(key, "");
|
|
378
|
-
} else if (typeof value === "string"
|
|
384
|
+
} else if (typeof value === "string") {
|
|
379
385
|
formData.append(key, value);
|
|
380
|
-
} else if (
|
|
386
|
+
} else if (isBuffer(value)) {
|
|
387
|
+
formData.append(key, value);
|
|
388
|
+
} else if (value instanceof Blob) {
|
|
381
389
|
formData.append(key, value);
|
|
382
390
|
} else if (typeof value === "object" && value.value !== undefined) {
|
|
383
391
|
const val = value.value;
|
|
384
|
-
const
|
|
385
|
-
if (typeof val === "string"
|
|
386
|
-
formData.append(key, val
|
|
392
|
+
const filename = value.options?.filename || value.filename;
|
|
393
|
+
if (typeof val === "string") {
|
|
394
|
+
formData.append(key, val);
|
|
395
|
+
} else if (isBuffer(val)) {
|
|
396
|
+
formData.append(key, val, filename);
|
|
397
|
+
} else if (val instanceof Blob) {
|
|
398
|
+
formData.append(key, val, filename);
|
|
387
399
|
} else {
|
|
388
|
-
formData.append(key, String(val)
|
|
400
|
+
formData.append(key, String(val));
|
|
389
401
|
}
|
|
390
402
|
} else {
|
|
391
403
|
formData.append(key, String(value));
|
|
@@ -642,13 +654,21 @@ As a workaround, process.env.NODE_TLS_REJECT_UNAUTHORIZED is being set to '0'.
|
|
|
642
654
|
} else if (!fs) {
|
|
643
655
|
throw new Error(`You can only use this feature in nodejs module, not in Edge module.`);
|
|
644
656
|
}
|
|
645
|
-
const
|
|
646
|
-
|
|
647
|
-
const
|
|
657
|
+
const basename = (p) => p.split(/[/\\]/).pop() || "";
|
|
658
|
+
const dirname = (p) => {
|
|
659
|
+
const parts = p.split(/[/\\]/);
|
|
660
|
+
parts.pop();
|
|
661
|
+
return parts.join("/") || ".";
|
|
662
|
+
};
|
|
663
|
+
const join = (...parts) => parts.join("/").replace(/\/+/g, "/");
|
|
664
|
+
const name = basename(saveTo);
|
|
665
|
+
const cwd = typeof process !== "undefined" && process.cwd ? process.cwd() : ".";
|
|
666
|
+
if (checkISPermission && checkISPermission(saveTo.length ? dirname(saveTo) : cwd, fs)) {
|
|
667
|
+
const dir = name.length < saveTo.length ? dirname(saveTo) : join(cwd, "download");
|
|
648
668
|
if (!fs.existsSync(dir)) {
|
|
649
669
|
fs.mkdirSync(dir, { recursive: true });
|
|
650
670
|
}
|
|
651
|
-
fileName =
|
|
671
|
+
fileName = join(dir, name);
|
|
652
672
|
config.fileName = fileName;
|
|
653
673
|
} else {
|
|
654
674
|
throw new Error(`Permission denied to save to ${saveTo}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rezo",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.31",
|
|
4
4
|
"description": "Lightning-fast, enterprise-grade HTTP client for modern JavaScript. Full HTTP/2 support, intelligent cookie management, multiple adapters (HTTP, Fetch, cURL, XHR), streaming, proxy support (HTTP/HTTPS/SOCKS), and cross-environment compatibility.",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -61,7 +61,6 @@
|
|
|
61
61
|
"node": ">=22.0.0"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"form-data": "^4.0.4",
|
|
65
64
|
"tough-cookie": "^5.1.2",
|
|
66
65
|
"http-proxy-agent": "^7.0.2",
|
|
67
66
|
"https-proxy-agent": "^7.0.6",
|
|
@@ -211,12 +210,20 @@
|
|
|
211
210
|
"./dist/platform/node.js": "./dist/platform/browser.js",
|
|
212
211
|
"./dist/platform/node.cjs": "./dist/platform/browser.cjs",
|
|
213
212
|
"./dist/adapters/http.js": "./dist/adapters/fetch.js",
|
|
214
|
-
"./dist/adapters/http.cjs": "./dist/adapters/fetch.cjs"
|
|
213
|
+
"./dist/adapters/http.cjs": "./dist/adapters/fetch.cjs",
|
|
214
|
+
"./dist/utils/cookies.js": "./dist/utils/cookies.browser.js",
|
|
215
|
+
"./dist/utils/cookies.cjs": "./dist/utils/cookies.browser.cjs",
|
|
216
|
+
"./dist/cache/response-cache.js": "./dist/cache/universal-response-cache.js",
|
|
217
|
+
"./dist/cache/response-cache.cjs": "./dist/cache/universal-response-cache.cjs"
|
|
215
218
|
},
|
|
216
219
|
"react-native": {
|
|
217
220
|
"./dist/platform/node.js": "./dist/platform/react-native.js",
|
|
218
221
|
"./dist/platform/node.cjs": "./dist/platform/react-native.cjs",
|
|
219
|
-
"./dist/adapters/http.js": "./dist/adapters/react-native.js"
|
|
222
|
+
"./dist/adapters/http.js": "./dist/adapters/react-native.js",
|
|
223
|
+
"./dist/utils/cookies.js": "./dist/utils/cookies.browser.js",
|
|
224
|
+
"./dist/utils/cookies.cjs": "./dist/utils/cookies.browser.cjs",
|
|
225
|
+
"./dist/cache/response-cache.js": "./dist/cache/universal-response-cache.js",
|
|
226
|
+
"./dist/cache/response-cache.cjs": "./dist/cache/universal-response-cache.cjs"
|
|
220
227
|
},
|
|
221
228
|
"files": [
|
|
222
229
|
"dist/**/*",
|
package/dist/types/cookies.cjs
DELETED
|
@@ -1,394 +0,0 @@
|
|
|
1
|
-
const { CookieJar: TouchCookieJar, Cookie: TouchCookie } = require("tough-cookie");
|
|
2
|
-
|
|
3
|
-
class Cookie extends TouchCookie {
|
|
4
|
-
constructor(options) {
|
|
5
|
-
super(options);
|
|
6
|
-
}
|
|
7
|
-
getExpires() {
|
|
8
|
-
let expires = 0;
|
|
9
|
-
if (this.expires && typeof this.expires !== "string") {
|
|
10
|
-
expires = Math.round(this.expires.getTime() / 1000);
|
|
11
|
-
} else if (this.maxAge) {
|
|
12
|
-
if (this.maxAge === "Infinity") {
|
|
13
|
-
expires = 2147483647;
|
|
14
|
-
} else if (this.maxAge === "-Infinity") {
|
|
15
|
-
expires = 0;
|
|
16
|
-
} else if (typeof this.maxAge === "number") {
|
|
17
|
-
expires = Math.round(Date.now() / 1000 + this.maxAge);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return expires;
|
|
21
|
-
}
|
|
22
|
-
toNetscapeFormat() {
|
|
23
|
-
const domain = !this.hostOnly ? this.domain.startsWith(".") ? this.domain : "." + this.domain : this.domain;
|
|
24
|
-
const secure = this.secure ? "TRUE" : "FALSE";
|
|
25
|
-
const expires = this.getExpires();
|
|
26
|
-
return `${domain} TRUE ${this.path} ${secure} ${expires} ${this.key} ${this.value}`;
|
|
27
|
-
}
|
|
28
|
-
toSetCookieString() {
|
|
29
|
-
let str = this.cookieString();
|
|
30
|
-
if (this.expires instanceof Date) {
|
|
31
|
-
str += `; Expires=${this.expires.toUTCString()}`;
|
|
32
|
-
}
|
|
33
|
-
if (this.maxAge != null) {
|
|
34
|
-
str += `; Max-Age=${this.maxAge}`;
|
|
35
|
-
}
|
|
36
|
-
if (this.domain) {
|
|
37
|
-
str += `; Domain=${this.domain}`;
|
|
38
|
-
}
|
|
39
|
-
if (this.path) {
|
|
40
|
-
str += `; Path=${this.path}`;
|
|
41
|
-
}
|
|
42
|
-
if (this.secure) {
|
|
43
|
-
str += "; Secure";
|
|
44
|
-
}
|
|
45
|
-
if (this.httpOnly) {
|
|
46
|
-
str += "; HttpOnly";
|
|
47
|
-
}
|
|
48
|
-
if (this.sameSite) {
|
|
49
|
-
str += `; SameSite=${this.sameSite}`;
|
|
50
|
-
}
|
|
51
|
-
if (this.extensions) {
|
|
52
|
-
str += `; ${this.extensions.join("; ")}`;
|
|
53
|
-
}
|
|
54
|
-
return str;
|
|
55
|
-
}
|
|
56
|
-
getURL() {
|
|
57
|
-
if (!this.domain) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
const protocol = this.secure ? "https://" : "http://";
|
|
61
|
-
const domain = this.domain.startsWith(".") ? this.domain.substring(1) : this.domain;
|
|
62
|
-
const path = this.path || "/";
|
|
63
|
-
return `${protocol}${domain}${path}`;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
class CookieJar extends TouchCookieJar {
|
|
68
|
-
constructor(store, options) {
|
|
69
|
-
super(store, options);
|
|
70
|
-
}
|
|
71
|
-
generateCookies(data) {
|
|
72
|
-
const cookies = !data ? (this.toJSON()?.cookies || []).map((cookie) => new Cookie(cookie)) : data[0] instanceof Cookie ? data : data.map((cookie) => new Cookie(cookie instanceof TouchCookie ? cookie : Cookie.fromJSON(cookie)));
|
|
73
|
-
const netscape = cookies.map((cookie) => cookie.toNetscapeFormat());
|
|
74
|
-
const cookieString = cookies.map((cookie) => cookie.cookieString());
|
|
75
|
-
const setCookiesString = cookies.map((cookie) => cookie.toSetCookieString());
|
|
76
|
-
let netscapeString = `# Netscape HTTP Cookie File
|
|
77
|
-
`;
|
|
78
|
-
netscapeString += `# This file was generated by Rezo HTTP client
|
|
79
|
-
`;
|
|
80
|
-
netscapeString += `# https://github.com/yuniq-solutions/rezo
|
|
81
|
-
`;
|
|
82
|
-
netscapeString += netscape.join(`
|
|
83
|
-
`) || "";
|
|
84
|
-
return {
|
|
85
|
-
array: cookies,
|
|
86
|
-
serialized: this.toJSON()?.cookies || [],
|
|
87
|
-
netscape: netscapeString,
|
|
88
|
-
string: cookieString.join("; "),
|
|
89
|
-
setCookiesString
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
cookies() {
|
|
93
|
-
return this.generateCookies();
|
|
94
|
-
}
|
|
95
|
-
parseResponseCookies(cookies) {
|
|
96
|
-
return this.generateCookies(cookies);
|
|
97
|
-
}
|
|
98
|
-
static toNetscapeCookie(cookies) {
|
|
99
|
-
cookies = cookies.map((cookie) => {
|
|
100
|
-
return cookie instanceof Cookie ? cookie : new Cookie(Cookie.fromJSON(cookie));
|
|
101
|
-
});
|
|
102
|
-
let netscapeString = `# Netscape HTTP Cookie File
|
|
103
|
-
`;
|
|
104
|
-
netscapeString += `# This file was generated by Rezo HTTP client
|
|
105
|
-
`;
|
|
106
|
-
netscapeString += `# https://github.com/yuniq-solutions/rezo
|
|
107
|
-
`;
|
|
108
|
-
netscapeString += cookies.map((cookie) => cookie.toNetscapeFormat()).join(`
|
|
109
|
-
`) || "";
|
|
110
|
-
return netscapeString;
|
|
111
|
-
}
|
|
112
|
-
static toCookieString(cookies) {
|
|
113
|
-
cookies = cookies.map((cookie) => {
|
|
114
|
-
return cookie instanceof Cookie ? cookie : new Cookie(Cookie.fromJSON(cookie));
|
|
115
|
-
});
|
|
116
|
-
return cookies.map((cookie) => cookie.cookieString()).join("; ") || "";
|
|
117
|
-
}
|
|
118
|
-
toCookieString() {
|
|
119
|
-
return this.cookies().string;
|
|
120
|
-
}
|
|
121
|
-
toNetscapeCookie() {
|
|
122
|
-
return this.cookies().netscape;
|
|
123
|
-
}
|
|
124
|
-
toArray() {
|
|
125
|
-
return this.cookies().array;
|
|
126
|
-
}
|
|
127
|
-
toSetCookies() {
|
|
128
|
-
return this.cookies().setCookiesString;
|
|
129
|
-
}
|
|
130
|
-
toSerializedCookies() {
|
|
131
|
-
return this.cookies().serialized;
|
|
132
|
-
}
|
|
133
|
-
setCookiesSync(cookiesData, url) {
|
|
134
|
-
const cookies = [];
|
|
135
|
-
if (Array.isArray(cookiesData)) {
|
|
136
|
-
cookiesData.forEach((c) => {
|
|
137
|
-
const cookie = c instanceof Cookie ? c : typeof c === "string" ? new Cookie(Cookie.parse(c)) : new Cookie(Cookie.fromJSON(c));
|
|
138
|
-
let isFailed = 0;
|
|
139
|
-
while (isFailed < 2) {
|
|
140
|
-
try {
|
|
141
|
-
if (cookie) {
|
|
142
|
-
const _url = isFailed > 0 ? cookie.getURL() || url || this.getUrlFromCookie(cookie) : url || this.getUrlFromCookie(cookie);
|
|
143
|
-
if (_url) {
|
|
144
|
-
const __cookie = this.setCookieSync(cookie, _url);
|
|
145
|
-
if (__cookie) {
|
|
146
|
-
cookies.push(__cookie);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
isFailed = 4;
|
|
150
|
-
break;
|
|
151
|
-
} else {
|
|
152
|
-
isFailed++;
|
|
153
|
-
}
|
|
154
|
-
} catch (error) {
|
|
155
|
-
isFailed++;
|
|
156
|
-
if (isFailed > 1) {
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
} else if (typeof cookiesData === "string") {
|
|
163
|
-
if (cookiesData.includes(",") && (cookiesData.includes("=") && (cookiesData.includes(";") || cookiesData.includes("expires=") || cookiesData.includes("path=")))) {
|
|
164
|
-
const setCookieStrings = this.splitSetCookiesString(cookiesData);
|
|
165
|
-
setCookieStrings.forEach((cookieStr) => {
|
|
166
|
-
const cookie = new Cookie(Cookie.parse(cookieStr));
|
|
167
|
-
let isFailed = 0;
|
|
168
|
-
while (isFailed < 2) {
|
|
169
|
-
try {
|
|
170
|
-
if (cookie) {
|
|
171
|
-
const _url = isFailed > 0 ? cookie.getURL() || url || this.getUrlFromCookie(cookie) : url || this.getUrlFromCookie(cookie);
|
|
172
|
-
if (_url) {
|
|
173
|
-
const __cookie = this.setCookieSync(cookie, _url);
|
|
174
|
-
if (__cookie) {
|
|
175
|
-
cookies.push(__cookie);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
isFailed = 4;
|
|
179
|
-
break;
|
|
180
|
-
} else {
|
|
181
|
-
isFailed++;
|
|
182
|
-
}
|
|
183
|
-
} catch (error) {
|
|
184
|
-
isFailed++;
|
|
185
|
-
if (isFailed > 1) {
|
|
186
|
-
break;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
} else if (cookiesData.includes("=") && cookiesData.includes(";")) {
|
|
192
|
-
const cookiePairs = cookiesData.split(";");
|
|
193
|
-
cookiePairs.forEach((pair) => {
|
|
194
|
-
const trimmedPair = pair.trim();
|
|
195
|
-
if (trimmedPair) {
|
|
196
|
-
const cookie = new Cookie({ key: trimmedPair.split("=")[0].trim(), value: trimmedPair.split("=")[1]?.trim() || "" });
|
|
197
|
-
let isFailed = 0;
|
|
198
|
-
while (isFailed < 2) {
|
|
199
|
-
try {
|
|
200
|
-
if (cookie) {
|
|
201
|
-
const _url = isFailed > 0 ? cookie.getURL() || url || this.getUrlFromCookie(cookie) : url || this.getUrlFromCookie(cookie);
|
|
202
|
-
if (_url) {
|
|
203
|
-
const __cookie = this.setCookieSync(cookie, _url);
|
|
204
|
-
if (__cookie) {
|
|
205
|
-
cookies.push(__cookie);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
isFailed = 4;
|
|
209
|
-
break;
|
|
210
|
-
} else {
|
|
211
|
-
isFailed++;
|
|
212
|
-
}
|
|
213
|
-
} catch (error) {
|
|
214
|
-
isFailed++;
|
|
215
|
-
if (isFailed > 1) {
|
|
216
|
-
break;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
} else if (cookiesData.includes("\t") && /^\S+\t/.test(cookiesData)) {
|
|
223
|
-
const netscapeCookies = this.parseNetscapeCookies(cookiesData);
|
|
224
|
-
netscapeCookies.forEach((c) => {
|
|
225
|
-
const cookie = new Cookie(c);
|
|
226
|
-
let isFailed = 0;
|
|
227
|
-
while (isFailed < 2) {
|
|
228
|
-
try {
|
|
229
|
-
if (cookie) {
|
|
230
|
-
const _url = isFailed > 0 ? cookie.getURL() || url || this.getUrlFromCookie(cookie) : url || this.getUrlFromCookie(cookie);
|
|
231
|
-
if (_url) {
|
|
232
|
-
const __cookie = this.setCookieSync(cookie, _url);
|
|
233
|
-
if (__cookie) {
|
|
234
|
-
cookies.push(__cookie);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
isFailed = 4;
|
|
238
|
-
break;
|
|
239
|
-
} else {
|
|
240
|
-
isFailed++;
|
|
241
|
-
}
|
|
242
|
-
} catch (error) {
|
|
243
|
-
isFailed++;
|
|
244
|
-
if (isFailed > 1) {
|
|
245
|
-
break;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
} else if (cookiesData.includes("=")) {
|
|
251
|
-
const cookie = new Cookie(Cookie.parse(cookiesData));
|
|
252
|
-
let isFailed = 0;
|
|
253
|
-
while (isFailed < 2) {
|
|
254
|
-
try {
|
|
255
|
-
if (cookie) {
|
|
256
|
-
const _url = isFailed > 0 ? cookie.getURL() || url || this.getUrlFromCookie(cookie) : url || this.getUrlFromCookie(cookie);
|
|
257
|
-
if (_url) {
|
|
258
|
-
const __cookie = this.setCookieSync(cookie, _url);
|
|
259
|
-
if (__cookie) {
|
|
260
|
-
cookies.push(__cookie);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
isFailed = 4;
|
|
264
|
-
break;
|
|
265
|
-
} else {
|
|
266
|
-
isFailed++;
|
|
267
|
-
}
|
|
268
|
-
} catch (error) {
|
|
269
|
-
isFailed++;
|
|
270
|
-
if (isFailed > 1) {
|
|
271
|
-
break;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return this.generateCookies(cookies);
|
|
278
|
-
}
|
|
279
|
-
splitSetCookiesString(cookiesString) {
|
|
280
|
-
const result = [];
|
|
281
|
-
let currentCookie = "";
|
|
282
|
-
let withinValue = false;
|
|
283
|
-
for (let i = 0;i < cookiesString.length; i++) {
|
|
284
|
-
const char = cookiesString[i];
|
|
285
|
-
if (char === "," && !withinValue) {
|
|
286
|
-
result.push(currentCookie.trim());
|
|
287
|
-
currentCookie = "";
|
|
288
|
-
continue;
|
|
289
|
-
}
|
|
290
|
-
if (char === "=") {
|
|
291
|
-
withinValue = true;
|
|
292
|
-
} else if (char === ";") {
|
|
293
|
-
withinValue = false;
|
|
294
|
-
}
|
|
295
|
-
currentCookie += char;
|
|
296
|
-
}
|
|
297
|
-
if (currentCookie.trim()) {
|
|
298
|
-
result.push(currentCookie.trim());
|
|
299
|
-
}
|
|
300
|
-
return result;
|
|
301
|
-
}
|
|
302
|
-
getUrlFromCookie(cookie) {
|
|
303
|
-
if (!cookie.domain) {
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
const proto = cookie.secure ? "https://" : "http://";
|
|
307
|
-
const domain = cookie.domain.startsWith(".") ? cookie.domain.substring(1) : cookie.domain;
|
|
308
|
-
const pathname = cookie.path || "/";
|
|
309
|
-
return `${proto}${domain}${pathname}`;
|
|
310
|
-
}
|
|
311
|
-
parseNetscapeCookies = (cookieText) => {
|
|
312
|
-
const lines = cookieText.split(`
|
|
313
|
-
`).filter((line) => line.trim() !== "" && !line.startsWith("#"));
|
|
314
|
-
return lines.map((line) => {
|
|
315
|
-
const parts = line.split("\t");
|
|
316
|
-
if (parts.length < 7) {
|
|
317
|
-
throw new Error(`Invalid Netscape cookie format: ${line}`);
|
|
318
|
-
}
|
|
319
|
-
const [domain, _, path, secureStr, expiresStr, name, value] = parts;
|
|
320
|
-
let expires = null;
|
|
321
|
-
if (expiresStr !== "0" && expiresStr !== "") {
|
|
322
|
-
expires = new Date(parseInt(expiresStr, 10) * 1000);
|
|
323
|
-
}
|
|
324
|
-
return {
|
|
325
|
-
domain,
|
|
326
|
-
path,
|
|
327
|
-
secure: secureStr.toUpperCase() === "TRUE",
|
|
328
|
-
expires,
|
|
329
|
-
key: name,
|
|
330
|
-
value,
|
|
331
|
-
httpOnly: false,
|
|
332
|
-
sameSite: undefined
|
|
333
|
-
};
|
|
334
|
-
});
|
|
335
|
-
};
|
|
336
|
-
static netscapeCookiesToSetCookieArray(netscapeCookieText) {
|
|
337
|
-
const parseNetscapeCookies = (cookieText) => {
|
|
338
|
-
const lines = cookieText.split(`
|
|
339
|
-
`).filter((line) => line.trim() !== "" && !line.startsWith("#"));
|
|
340
|
-
return lines.map((line) => {
|
|
341
|
-
const parts = line.split("\t");
|
|
342
|
-
if (parts.length < 7) {
|
|
343
|
-
throw new Error(`Invalid Netscape cookie format: ${line}`);
|
|
344
|
-
}
|
|
345
|
-
const [domain, _, path, secureStr, expiresStr, name, value] = parts;
|
|
346
|
-
let expires = null;
|
|
347
|
-
if (expiresStr !== "0" && expiresStr !== "") {
|
|
348
|
-
expires = new Date(parseInt(expiresStr, 10) * 1000);
|
|
349
|
-
}
|
|
350
|
-
return {
|
|
351
|
-
domain,
|
|
352
|
-
path,
|
|
353
|
-
secure: secureStr.toUpperCase() === "TRUE",
|
|
354
|
-
expires,
|
|
355
|
-
name,
|
|
356
|
-
value,
|
|
357
|
-
httpOnly: false,
|
|
358
|
-
sameSite: undefined
|
|
359
|
-
};
|
|
360
|
-
});
|
|
361
|
-
};
|
|
362
|
-
const cookieToSetCookieString = (cookie) => {
|
|
363
|
-
let setCookie = `${cookie.name}=${cookie.value}`;
|
|
364
|
-
if (cookie.domain) {
|
|
365
|
-
setCookie += `; Domain=${cookie.domain}`;
|
|
366
|
-
}
|
|
367
|
-
if (cookie.path) {
|
|
368
|
-
setCookie += `; Path=${cookie.path}`;
|
|
369
|
-
}
|
|
370
|
-
if (cookie.expires) {
|
|
371
|
-
setCookie += `; Expires=${cookie.expires.toUTCString()}`;
|
|
372
|
-
}
|
|
373
|
-
if (cookie.secure) {
|
|
374
|
-
setCookie += "; Secure";
|
|
375
|
-
}
|
|
376
|
-
if (cookie.httpOnly) {
|
|
377
|
-
setCookie += "; HttpOnly";
|
|
378
|
-
}
|
|
379
|
-
if (cookie.sameSite) {
|
|
380
|
-
setCookie += `; SameSite=${cookie.sameSite}`;
|
|
381
|
-
}
|
|
382
|
-
return setCookie;
|
|
383
|
-
};
|
|
384
|
-
try {
|
|
385
|
-
const cookies = parseNetscapeCookies(netscapeCookieText);
|
|
386
|
-
return cookies.map(cookieToSetCookieString);
|
|
387
|
-
} catch (error) {
|
|
388
|
-
return [];
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
exports.Cookie = Cookie;
|
|
394
|
-
exports.CookieJar = CookieJar;
|