rezo 1.0.0

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.
Files changed (135) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1507 -0
  3. package/assets/icon.svg +37 -0
  4. package/assets/logo-dark.svg +47 -0
  5. package/assets/logo.svg +58 -0
  6. package/dist/adapters/curl.cjs +1034 -0
  7. package/dist/adapters/curl.js +1031 -0
  8. package/dist/adapters/entries/curl.cjs +4 -0
  9. package/dist/adapters/entries/curl.d.ts +2136 -0
  10. package/dist/adapters/entries/curl.js +2 -0
  11. package/dist/adapters/entries/fetch.cjs +2 -0
  12. package/dist/adapters/entries/fetch.d.ts +2127 -0
  13. package/dist/adapters/entries/fetch.js +1 -0
  14. package/dist/adapters/entries/http.cjs +2 -0
  15. package/dist/adapters/entries/http.d.ts +2126 -0
  16. package/dist/adapters/entries/http.js +1 -0
  17. package/dist/adapters/entries/http2.cjs +4 -0
  18. package/dist/adapters/entries/http2.d.ts +2136 -0
  19. package/dist/adapters/entries/http2.js +2 -0
  20. package/dist/adapters/entries/react-native.cjs +2 -0
  21. package/dist/adapters/entries/react-native.d.ts +2126 -0
  22. package/dist/adapters/entries/react-native.js +1 -0
  23. package/dist/adapters/entries/xhr.cjs +2 -0
  24. package/dist/adapters/entries/xhr.d.ts +2127 -0
  25. package/dist/adapters/entries/xhr.js +1 -0
  26. package/dist/adapters/fetch.cjs +740 -0
  27. package/dist/adapters/fetch.js +739 -0
  28. package/dist/adapters/http.cjs +1153 -0
  29. package/dist/adapters/http.js +1151 -0
  30. package/dist/adapters/http2.cjs +957 -0
  31. package/dist/adapters/http2.js +956 -0
  32. package/dist/adapters/index.cjs +6 -0
  33. package/dist/adapters/index.js +7 -0
  34. package/dist/adapters/picker.cjs +342 -0
  35. package/dist/adapters/picker.js +331 -0
  36. package/dist/adapters/react-native.cjs +545 -0
  37. package/dist/adapters/react-native.js +544 -0
  38. package/dist/adapters/xhr.cjs +622 -0
  39. package/dist/adapters/xhr.js +621 -0
  40. package/dist/cache/dns-cache.cjs +118 -0
  41. package/dist/cache/dns-cache.js +113 -0
  42. package/dist/cache/file-cacher.cjs +264 -0
  43. package/dist/cache/file-cacher.js +261 -0
  44. package/dist/cache/index.cjs +13 -0
  45. package/dist/cache/index.js +5 -0
  46. package/dist/cache/lru-cache.cjs +96 -0
  47. package/dist/cache/lru-cache.js +93 -0
  48. package/dist/cache/response-cache.cjs +314 -0
  49. package/dist/cache/response-cache.js +310 -0
  50. package/dist/cache/url-store.cjs +288 -0
  51. package/dist/cache/url-store.js +285 -0
  52. package/dist/core/hooks.cjs +133 -0
  53. package/dist/core/hooks.js +120 -0
  54. package/dist/core/rezo.cjs +464 -0
  55. package/dist/core/rezo.js +458 -0
  56. package/dist/crawler.d.ts +6255 -0
  57. package/dist/dom/index.cjs +1 -0
  58. package/dist/dom/index.d.ts +23 -0
  59. package/dist/dom/index.js +1 -0
  60. package/dist/entries/crawler.cjs +5 -0
  61. package/dist/entries/crawler.js +2 -0
  62. package/dist/errors/rezo-error.cjs +722 -0
  63. package/dist/errors/rezo-error.js +716 -0
  64. package/dist/index.cjs +34 -0
  65. package/dist/index.d.ts +3335 -0
  66. package/dist/index.js +26 -0
  67. package/dist/platform/browser.cjs +9 -0
  68. package/dist/platform/browser.d.ts +3203 -0
  69. package/dist/platform/browser.js +7 -0
  70. package/dist/platform/bun.cjs +9 -0
  71. package/dist/platform/bun.d.ts +3203 -0
  72. package/dist/platform/bun.js +7 -0
  73. package/dist/platform/deno.cjs +9 -0
  74. package/dist/platform/deno.d.ts +3203 -0
  75. package/dist/platform/deno.js +7 -0
  76. package/dist/platform/node.cjs +9 -0
  77. package/dist/platform/node.d.ts +3203 -0
  78. package/dist/platform/node.js +7 -0
  79. package/dist/platform/react-native.cjs +9 -0
  80. package/dist/platform/react-native.d.ts +3203 -0
  81. package/dist/platform/react-native.js +7 -0
  82. package/dist/platform/worker.cjs +9 -0
  83. package/dist/platform/worker.d.ts +3203 -0
  84. package/dist/platform/worker.js +7 -0
  85. package/dist/plugin/addon/decodo/index.cjs +1 -0
  86. package/dist/plugin/addon/decodo/index.js +1 -0
  87. package/dist/plugin/addon/decodo/options.cjs +1 -0
  88. package/dist/plugin/addon/decodo/options.js +1 -0
  89. package/dist/plugin/addon/oxylabs/index.cjs +1 -0
  90. package/dist/plugin/addon/oxylabs/index.js +1 -0
  91. package/dist/plugin/addon/oxylabs/options.cjs +1 -0
  92. package/dist/plugin/addon/oxylabs/options.js +1 -0
  93. package/dist/plugin/crawler-options.cjs +1 -0
  94. package/dist/plugin/crawler-options.js +1 -0
  95. package/dist/plugin/crawler.cjs +519 -0
  96. package/dist/plugin/crawler.js +517 -0
  97. package/dist/plugin/index.cjs +36 -0
  98. package/dist/plugin/index.js +32 -0
  99. package/dist/proxy/index.cjs +142 -0
  100. package/dist/proxy/index.js +139 -0
  101. package/dist/responses/buildError.cjs +452 -0
  102. package/dist/responses/buildError.js +441 -0
  103. package/dist/responses/buildResponse.cjs +365 -0
  104. package/dist/responses/buildResponse.js +361 -0
  105. package/dist/responses/download.cjs +54 -0
  106. package/dist/responses/download.js +52 -0
  107. package/dist/responses/stream.cjs +60 -0
  108. package/dist/responses/stream.js +58 -0
  109. package/dist/responses/upload.cjs +54 -0
  110. package/dist/responses/upload.js +52 -0
  111. package/dist/types/cookies.cjs +394 -0
  112. package/dist/types/cookies.js +391 -0
  113. package/dist/types/download.cjs +10 -0
  114. package/dist/types/download.js +10 -0
  115. package/dist/types/rezo-request.cjs +131 -0
  116. package/dist/types/rezo-request.js +131 -0
  117. package/dist/utils/agent-merger.cjs +111 -0
  118. package/dist/utils/agent-merger.js +108 -0
  119. package/dist/utils/compression.cjs +84 -0
  120. package/dist/utils/compression.js +82 -0
  121. package/dist/utils/cookies.cjs +514 -0
  122. package/dist/utils/cookies.js +511 -0
  123. package/dist/utils/data-operations.cjs +75 -0
  124. package/dist/utils/data-operations.js +73 -0
  125. package/dist/utils/form-data.cjs +164 -0
  126. package/dist/utils/form-data.js +161 -0
  127. package/dist/utils/headers.cjs +162 -0
  128. package/dist/utils/headers.js +161 -0
  129. package/dist/utils/http-config.cjs +723 -0
  130. package/dist/utils/http-config.js +718 -0
  131. package/dist/utils/index.cjs +8 -0
  132. package/dist/utils/index.js +8 -0
  133. package/dist/utils/tools.cjs +18 -0
  134. package/dist/utils/tools.js +15 -0
  135. package/package.json +172 -0
@@ -0,0 +1,511 @@
1
+ import { CookieJar as TouchCookieJar, Cookie as TouchCookie } from "tough-cookie";
2
+
3
+ export class Cookie extends TouchCookie {
4
+ constructor(options) {
5
+ super(options);
6
+ this.fixDateFields();
7
+ }
8
+ fixDateFields() {
9
+ if (this.creation && typeof this.creation === "string") {
10
+ this.creation = new Date(this.creation);
11
+ }
12
+ if (this.lastAccessed && typeof this.lastAccessed === "string") {
13
+ this.lastAccessed = new Date(this.lastAccessed);
14
+ }
15
+ if (this.expires && typeof this.expires === "string" && this.expires !== "Infinity") {
16
+ this.expires = new Date(this.expires);
17
+ }
18
+ }
19
+ getExpires() {
20
+ let expires = 0;
21
+ if (this.expires && typeof this.expires !== "string") {
22
+ expires = Math.round(this.expires.getTime() / 1000);
23
+ } else if (this.maxAge) {
24
+ if (this.maxAge === "Infinity") {
25
+ expires = 2147483647;
26
+ } else if (this.maxAge === "-Infinity") {
27
+ expires = 0;
28
+ } else if (typeof this.maxAge === "number") {
29
+ expires = Math.round(Date.now() / 1000 + this.maxAge);
30
+ }
31
+ }
32
+ return expires;
33
+ }
34
+ toNetscapeFormat() {
35
+ const domain = !this.hostOnly ? this.domain.startsWith(".") ? this.domain : "." + this.domain : this.domain;
36
+ const secure = this.secure ? "TRUE" : "FALSE";
37
+ const expires = this.getExpires();
38
+ return `${domain} TRUE ${this.path} ${secure} ${expires} ${this.key} ${this.value}`;
39
+ }
40
+ toSetCookieString() {
41
+ let str = this.cookieString();
42
+ if (this.expires instanceof Date) {
43
+ str += `; Expires=${this.expires.toUTCString()}`;
44
+ }
45
+ if (this.maxAge != null) {
46
+ str += `; Max-Age=${this.maxAge}`;
47
+ }
48
+ if (this.domain) {
49
+ str += `; Domain=${this.domain}`;
50
+ }
51
+ if (this.path) {
52
+ str += `; Path=${this.path}`;
53
+ }
54
+ if (this.secure) {
55
+ str += "; Secure";
56
+ }
57
+ if (this.httpOnly) {
58
+ str += "; HttpOnly";
59
+ }
60
+ if (this.sameSite) {
61
+ str += `; SameSite=${this.sameSite}`;
62
+ }
63
+ if (this.extensions) {
64
+ str += `; ${this.extensions.join("; ")}`;
65
+ }
66
+ return str;
67
+ }
68
+ getURL() {
69
+ if (!this.domain) {
70
+ return;
71
+ }
72
+ const protocol = this.secure ? "https://" : "http://";
73
+ const domain = this.domain.startsWith(".") ? this.domain.substring(1) : this.domain;
74
+ const path = this.path || "/";
75
+ return `${protocol}${domain}${path}`;
76
+ }
77
+ static isCookie(cookie) {
78
+ return cookie instanceof Cookie || cookie instanceof TouchCookie;
79
+ }
80
+ }
81
+
82
+ export class RezoCookieJar extends TouchCookieJar {
83
+ constructor(store, options) {
84
+ if (Array.isArray(store)) {
85
+ super();
86
+ } else {
87
+ super(store, typeof options !== "string" ? options : undefined);
88
+ }
89
+ if (Array.isArray(store) && store.length > 0 && Cookie.isCookie(store[0])) {
90
+ if (options && typeof options === "string") {
91
+ this.setCookiesSync(store, options);
92
+ } else {
93
+ this.setCookiesSync(store);
94
+ }
95
+ }
96
+ }
97
+ generateCookies(data) {
98
+ 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)));
99
+ const netscape = cookies.map((cookie) => cookie.toNetscapeFormat());
100
+ const cookieString = cookies.map((cookie) => cookie.cookieString());
101
+ const setCookiesString = cookies.map((cookie) => cookie.toSetCookieString());
102
+ let netscapeString = `# Netscape HTTP Cookie File
103
+ `;
104
+ netscapeString += `# This file was generated by Rezo HTTP client
105
+ `;
106
+ netscapeString += `# Based on uniqhtt cookie implementation
107
+ `;
108
+ netscapeString += netscape.join(`
109
+ `) || "";
110
+ return {
111
+ array: cookies,
112
+ serialized: this.toJSON()?.cookies || [],
113
+ netscape: netscapeString,
114
+ string: cookieString.join("; "),
115
+ setCookiesString
116
+ };
117
+ }
118
+ cookies() {
119
+ return this.generateCookies();
120
+ }
121
+ parseResponseCookies(cookies) {
122
+ return this.generateCookies(cookies);
123
+ }
124
+ static toNetscapeCookie(cookies) {
125
+ cookies = cookies.map((cookie) => {
126
+ return cookie instanceof Cookie ? cookie : new Cookie(Cookie.fromJSON(cookie));
127
+ });
128
+ let netscapeString = `# Netscape HTTP Cookie File
129
+ `;
130
+ netscapeString += `# This file was generated by Rezo HTTP client
131
+ `;
132
+ netscapeString += `# Based on uniqhtt cookie implementation
133
+ `;
134
+ netscapeString += cookies.map((cookie) => cookie.toNetscapeFormat()).join(`
135
+ `) || "";
136
+ return netscapeString;
137
+ }
138
+ static toCookieString(cookies) {
139
+ cookies = cookies.map((cookie) => {
140
+ return cookie instanceof Cookie ? cookie : new Cookie(Cookie.fromJSON(cookie));
141
+ });
142
+ return cookies.map((cookie) => cookie.toNetscapeFormat()).join("; ") || "";
143
+ }
144
+ toCookieString() {
145
+ return this.cookies().string;
146
+ }
147
+ toNetscapeCookie() {
148
+ return this.cookies().netscape;
149
+ }
150
+ toArray() {
151
+ return this.cookies().array;
152
+ }
153
+ toSetCookies() {
154
+ return this.cookies().setCookiesString;
155
+ }
156
+ toSerializedCookies() {
157
+ return this.cookies().serialized;
158
+ }
159
+ setCookiesSync(cookiesData, url) {
160
+ const cookies = [];
161
+ if (Array.isArray(cookiesData)) {
162
+ cookiesData.forEach((c) => {
163
+ const cookie = c instanceof Cookie ? c : typeof c === "string" ? new Cookie(Cookie.parse(c)) : new Cookie(Cookie.fromJSON(c));
164
+ let isFailed = 0;
165
+ while (isFailed < 2) {
166
+ try {
167
+ if (cookie) {
168
+ const _url = isFailed > 0 ? cookie.getURL() || url || this.getUrlFromCookie(cookie) : url || this.getUrlFromCookie(cookie);
169
+ if (_url) {
170
+ const __cookie = this.setCookieSync(cookie, _url);
171
+ if (__cookie) {
172
+ cookies.push(__cookie);
173
+ }
174
+ }
175
+ isFailed = 4;
176
+ break;
177
+ } else {
178
+ isFailed++;
179
+ }
180
+ } catch (error) {
181
+ isFailed++;
182
+ if (isFailed > 1) {
183
+ break;
184
+ }
185
+ }
186
+ }
187
+ });
188
+ } else if (typeof cookiesData === "string") {
189
+ if (cookiesData.includes(",") && (cookiesData.includes("=") && (cookiesData.includes(";") || cookiesData.includes("expires=") || cookiesData.includes("path=")))) {
190
+ const setCookieStrings = this.splitSetCookiesString(cookiesData);
191
+ setCookieStrings.forEach((cookieStr) => {
192
+ const cookie = new Cookie(Cookie.parse(cookieStr));
193
+ let isFailed = 0;
194
+ while (isFailed < 2) {
195
+ try {
196
+ if (cookie) {
197
+ const _url = isFailed > 0 ? cookie.getURL() || url || this.getUrlFromCookie(cookie) : url || this.getUrlFromCookie(cookie);
198
+ if (_url) {
199
+ const __cookie = this.setCookieSync(cookie, _url);
200
+ if (__cookie) {
201
+ cookies.push(__cookie);
202
+ }
203
+ }
204
+ isFailed = 4;
205
+ break;
206
+ } else {
207
+ isFailed++;
208
+ }
209
+ } catch (error) {
210
+ isFailed++;
211
+ if (isFailed > 1) {
212
+ break;
213
+ }
214
+ }
215
+ }
216
+ });
217
+ } else if (cookiesData.includes("=") && cookiesData.includes(";")) {
218
+ const cookiePairs = cookiesData.split(";");
219
+ cookiePairs.forEach((pair) => {
220
+ const trimmedPair = pair.trim();
221
+ if (trimmedPair) {
222
+ const cookie = new Cookie({ key: trimmedPair.split("=")[0].trim(), value: trimmedPair.split("=")[1]?.trim() || "" });
223
+ let isFailed = 0;
224
+ while (isFailed < 2) {
225
+ try {
226
+ if (cookie) {
227
+ const _url = isFailed > 0 ? cookie.getURL() || url || this.getUrlFromCookie(cookie) : url || this.getUrlFromCookie(cookie);
228
+ if (_url) {
229
+ const __cookie = this.setCookieSync(cookie, _url);
230
+ if (__cookie) {
231
+ cookies.push(__cookie);
232
+ }
233
+ }
234
+ isFailed = 4;
235
+ break;
236
+ } else {
237
+ isFailed++;
238
+ }
239
+ } catch (error) {
240
+ isFailed++;
241
+ if (isFailed > 1) {
242
+ break;
243
+ }
244
+ }
245
+ }
246
+ }
247
+ });
248
+ } else if (cookiesData.includes("\t") && /^\S+\t/.test(cookiesData)) {
249
+ const netscapeCookies = this.parseNetscapeCookies(cookiesData);
250
+ netscapeCookies.forEach((c) => {
251
+ const cookie = new Cookie(c);
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
+ } else if (cookiesData.includes("=")) {
277
+ const cookie = new Cookie(Cookie.parse(cookiesData));
278
+ let isFailed = 0;
279
+ while (isFailed < 2) {
280
+ try {
281
+ if (cookie) {
282
+ const _url = isFailed > 0 ? cookie.getURL() || url || this.getUrlFromCookie(cookie) : url || this.getUrlFromCookie(cookie);
283
+ if (_url) {
284
+ const __cookie = this.setCookieSync(cookie, _url);
285
+ if (__cookie) {
286
+ cookies.push(__cookie);
287
+ }
288
+ }
289
+ isFailed = 4;
290
+ break;
291
+ } else {
292
+ isFailed++;
293
+ }
294
+ } catch (error) {
295
+ isFailed++;
296
+ if (isFailed > 1) {
297
+ break;
298
+ }
299
+ }
300
+ }
301
+ }
302
+ }
303
+ return this.generateCookies(cookies);
304
+ }
305
+ splitSetCookiesString(cookiesString) {
306
+ const result = [];
307
+ let currentCookie = "";
308
+ let withinValue = false;
309
+ for (let i = 0;i < cookiesString.length; i++) {
310
+ const char = cookiesString[i];
311
+ if (char === "," && !withinValue) {
312
+ result.push(currentCookie.trim());
313
+ currentCookie = "";
314
+ continue;
315
+ }
316
+ if (char === "=") {
317
+ withinValue = true;
318
+ } else if (char === ";") {
319
+ withinValue = false;
320
+ }
321
+ currentCookie += char;
322
+ }
323
+ if (currentCookie.trim()) {
324
+ result.push(currentCookie.trim());
325
+ }
326
+ return result;
327
+ }
328
+ getUrlFromCookie(cookie) {
329
+ if (!cookie.domain) {
330
+ return;
331
+ }
332
+ const proto = cookie.secure ? "https://" : "http://";
333
+ const domain = cookie.domain.startsWith(".") ? cookie.domain.substring(1) : cookie.domain;
334
+ const pathname = cookie.path || "/";
335
+ return `${proto}${domain}${pathname}`;
336
+ }
337
+ 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
+ key: name,
356
+ value,
357
+ httpOnly: false,
358
+ sameSite: undefined
359
+ };
360
+ });
361
+ };
362
+ static netscapeCookiesToSetCookieArray(netscapeCookieText) {
363
+ const parseNetscapeCookies = (cookieText) => {
364
+ const lines = cookieText.split(`
365
+ `).filter((line) => line.trim() !== "" && !line.startsWith("#"));
366
+ return lines.map((line) => {
367
+ const parts = line.split("\t");
368
+ if (parts.length < 7) {
369
+ throw new Error(`Invalid Netscape cookie format: ${line}`);
370
+ }
371
+ const [domain, _, path, secureStr, expiresStr, name, value] = parts;
372
+ let expires = null;
373
+ if (expiresStr !== "0" && expiresStr !== "") {
374
+ expires = new Date(parseInt(expiresStr, 10) * 1000);
375
+ }
376
+ return {
377
+ domain,
378
+ path,
379
+ secure: secureStr.toUpperCase() === "TRUE",
380
+ expires,
381
+ name,
382
+ value,
383
+ httpOnly: false,
384
+ sameSite: undefined
385
+ };
386
+ });
387
+ };
388
+ const cookieToSetCookieString = (cookie) => {
389
+ let setCookie = `${cookie.name}=${cookie.value}`;
390
+ if (cookie.domain) {
391
+ setCookie += `; Domain=${cookie.domain}`;
392
+ }
393
+ if (cookie.path) {
394
+ setCookie += `; Path=${cookie.path}`;
395
+ }
396
+ if (cookie.expires) {
397
+ setCookie += `; Expires=${cookie.expires.toUTCString()}`;
398
+ }
399
+ if (cookie.secure) {
400
+ setCookie += "; Secure";
401
+ }
402
+ if (cookie.httpOnly) {
403
+ setCookie += "; HttpOnly";
404
+ }
405
+ if (cookie.sameSite) {
406
+ setCookie += `; SameSite=${cookie.sameSite}`;
407
+ }
408
+ return setCookie;
409
+ };
410
+ try {
411
+ const cookies = parseNetscapeCookies(netscapeCookieText);
412
+ return cookies.map(cookieToSetCookieString);
413
+ } catch (error) {
414
+ return [];
415
+ }
416
+ }
417
+ [Symbol.for("nodejs.util.inspect.custom")]() {
418
+ const cookies = this.cookies();
419
+ return {
420
+ cookies: cookies.array.map((c) => ({
421
+ name: c.key,
422
+ value: c.value,
423
+ domain: c.domain,
424
+ path: c.path,
425
+ expires: c.expires instanceof Date ? c.expires.toISOString() : c.expires,
426
+ secure: c.secure,
427
+ httpOnly: c.httpOnly,
428
+ sameSite: c.sameSite
429
+ })),
430
+ count: cookies.array.length
431
+ };
432
+ }
433
+ _cookieFile;
434
+ get cookieFile() {
435
+ return this._cookieFile;
436
+ }
437
+ loadFromFile(filePath, defaultUrl) {
438
+ const fs = require("node:fs");
439
+ if (!fs.existsSync(filePath)) {
440
+ this._cookieFile = filePath;
441
+ return;
442
+ }
443
+ const content = fs.readFileSync(filePath, "utf-8");
444
+ const isJson = filePath.toLowerCase().endsWith(".json");
445
+ const store = this.store;
446
+ const putCookieSync = (cookie) => {
447
+ let done = false;
448
+ let error = null;
449
+ store.putCookie(cookie, (err) => {
450
+ error = err;
451
+ done = true;
452
+ });
453
+ if (error)
454
+ throw error;
455
+ };
456
+ if (isJson) {
457
+ try {
458
+ const data = JSON.parse(content);
459
+ const cookies = Array.isArray(data) ? data : data.cookies || [];
460
+ for (const c of cookies) {
461
+ try {
462
+ const cookie = new Cookie(Cookie.fromJSON(c));
463
+ if (cookie && cookie.key && cookie.domain) {
464
+ putCookieSync(cookie);
465
+ }
466
+ } catch (e) {}
467
+ }
468
+ } catch (e) {}
469
+ } else {
470
+ const setCookieStrings = RezoCookieJar.netscapeCookiesToSetCookieArray(content);
471
+ for (const cookieStr of setCookieStrings) {
472
+ try {
473
+ const cookie = new Cookie(Cookie.parse(cookieStr));
474
+ if (cookie && cookie.key && cookie.domain) {
475
+ putCookieSync(cookie);
476
+ }
477
+ } catch (e) {}
478
+ }
479
+ }
480
+ this._cookieFile = filePath;
481
+ }
482
+ saveToFile(filePath) {
483
+ const targetPath = filePath || this._cookieFile;
484
+ if (!targetPath) {
485
+ throw new Error("No cookie file path specified. Provide a path or load from a file first.");
486
+ }
487
+ const fs = require("node:fs");
488
+ const isJson = targetPath.toLowerCase().endsWith(".json");
489
+ const cookies = this.cookies();
490
+ if (isJson) {
491
+ const data = {
492
+ version: 1,
493
+ cookies: cookies.serialized
494
+ };
495
+ fs.writeFileSync(targetPath, JSON.stringify(data, null, 2));
496
+ } else {
497
+ fs.writeFileSync(targetPath, cookies.netscape);
498
+ }
499
+ if (filePath) {
500
+ this._cookieFile = filePath;
501
+ }
502
+ }
503
+ static fromFile(filePath, defaultUrl) {
504
+ const jar = new RezoCookieJar;
505
+ jar.loadFromFile(filePath, defaultUrl);
506
+ return jar;
507
+ }
508
+ }
509
+ export const CookieJar = RezoCookieJar;
510
+
511
+ export { TouchCookieJar, TouchCookie };
@@ -0,0 +1,75 @@
1
+ class RezoURLSearchParams extends URLSearchParams {
2
+ constructor(init) {
3
+ super();
4
+ if (init) {
5
+ if (init instanceof RezoURLSearchParams) {
6
+ this.appendObject(init.toObject());
7
+ } else if (typeof init === "string" || init instanceof URLSearchParams || Array.isArray(init)) {
8
+ const params = new URLSearchParams(init);
9
+ params.forEach((value, key) => this.append(key, value));
10
+ } else {
11
+ this.appendObject(init);
12
+ }
13
+ }
14
+ }
15
+ appendObject(obj, prefix = "") {
16
+ Object.entries(obj).forEach(([key, value]) => {
17
+ this.appendValue(prefix ? `${prefix}[${key}]` : key, value);
18
+ });
19
+ }
20
+ setObject(obj, prefix = "") {
21
+ if (prefix) {
22
+ const keysToDelete = [];
23
+ this.forEach((_, key) => {
24
+ if (key.startsWith(prefix + "[")) {
25
+ keysToDelete.push(key);
26
+ }
27
+ });
28
+ keysToDelete.forEach((key) => this.delete(key));
29
+ } else {
30
+ const keysToDelete = [];
31
+ this.forEach((_, key) => keysToDelete.push(key));
32
+ keysToDelete.forEach((key) => this.delete(key));
33
+ }
34
+ this.appendObject(obj, prefix);
35
+ }
36
+ appendValue(key, value) {
37
+ if (value === null || value === undefined) {
38
+ return;
39
+ }
40
+ if (value instanceof Date) {
41
+ this.append(key, value.toISOString());
42
+ return;
43
+ }
44
+ if (Array.isArray(value)) {
45
+ this.appendArray(key, value);
46
+ return;
47
+ }
48
+ if (typeof value === "object") {
49
+ this.appendObject(value, key);
50
+ return;
51
+ }
52
+ this.append(key, String(value));
53
+ }
54
+ appendArray(key, arr) {
55
+ arr.forEach((item, index) => {
56
+ this.appendValue(`${key}[${index}]`, item);
57
+ });
58
+ }
59
+ toObject() {
60
+ const obj = {};
61
+ this.forEach((value, key) => {
62
+ obj[key] = value;
63
+ });
64
+ return obj;
65
+ }
66
+ static fromFlat(flat) {
67
+ const params = new RezoURLSearchParams;
68
+ Object.entries(flat).forEach(([key, value]) => {
69
+ params.append(key, value);
70
+ });
71
+ return params;
72
+ }
73
+ }
74
+
75
+ exports.RezoURLSearchParams = RezoURLSearchParams;
@@ -0,0 +1,73 @@
1
+ export class RezoURLSearchParams extends URLSearchParams {
2
+ constructor(init) {
3
+ super();
4
+ if (init) {
5
+ if (init instanceof RezoURLSearchParams) {
6
+ this.appendObject(init.toObject());
7
+ } else if (typeof init === "string" || init instanceof URLSearchParams || Array.isArray(init)) {
8
+ const params = new URLSearchParams(init);
9
+ params.forEach((value, key) => this.append(key, value));
10
+ } else {
11
+ this.appendObject(init);
12
+ }
13
+ }
14
+ }
15
+ appendObject(obj, prefix = "") {
16
+ Object.entries(obj).forEach(([key, value]) => {
17
+ this.appendValue(prefix ? `${prefix}[${key}]` : key, value);
18
+ });
19
+ }
20
+ setObject(obj, prefix = "") {
21
+ if (prefix) {
22
+ const keysToDelete = [];
23
+ this.forEach((_, key) => {
24
+ if (key.startsWith(prefix + "[")) {
25
+ keysToDelete.push(key);
26
+ }
27
+ });
28
+ keysToDelete.forEach((key) => this.delete(key));
29
+ } else {
30
+ const keysToDelete = [];
31
+ this.forEach((_, key) => keysToDelete.push(key));
32
+ keysToDelete.forEach((key) => this.delete(key));
33
+ }
34
+ this.appendObject(obj, prefix);
35
+ }
36
+ appendValue(key, value) {
37
+ if (value === null || value === undefined) {
38
+ return;
39
+ }
40
+ if (value instanceof Date) {
41
+ this.append(key, value.toISOString());
42
+ return;
43
+ }
44
+ if (Array.isArray(value)) {
45
+ this.appendArray(key, value);
46
+ return;
47
+ }
48
+ if (typeof value === "object") {
49
+ this.appendObject(value, key);
50
+ return;
51
+ }
52
+ this.append(key, String(value));
53
+ }
54
+ appendArray(key, arr) {
55
+ arr.forEach((item, index) => {
56
+ this.appendValue(`${key}[${index}]`, item);
57
+ });
58
+ }
59
+ toObject() {
60
+ const obj = {};
61
+ this.forEach((value, key) => {
62
+ obj[key] = value;
63
+ });
64
+ return obj;
65
+ }
66
+ static fromFlat(flat) {
67
+ const params = new RezoURLSearchParams;
68
+ Object.entries(flat).forEach(([key, value]) => {
69
+ params.append(key, value);
70
+ });
71
+ return params;
72
+ }
73
+ }