@softtynet/zca-js 2.1.4 → 2.1.8

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.
@@ -308,16 +308,22 @@ async function request(ctx, url, options, raw = false) {
308
308
  const response = await ctx.options.polyfill(url, _options);
309
309
  const setCookieRaw = response.headers.get("set-cookie");
310
310
  if (setCookieRaw && !raw) {
311
- // Use getSetCookie() (Node.js 18+) to properly parse multi-cookie headers.
312
- // The split(", ") method breaks cookies containing Expires dates with commas
313
- // (e.g., "Expires=Wed, 18 Mar 2026..."), causing critical cookies like
314
- // zpsid and zpw_sek to be lost during QR login flow.
315
- let cookieStrings;
316
- if (typeof response.headers.getSetCookie === "function") {
317
- cookieStrings = response.headers.getSetCookie();
311
+ let cookieStrings = [];
312
+ const headers = response.headers;
313
+ // Cách 1: Ưu tiên lấy Mảng Raw chuẩn gốc (Array) của thư viện node-fetch
314
+ if (typeof headers.raw === "function") {
315
+ const rawHeaders = headers.raw();
316
+ if (rawHeaders && rawHeaders["set-cookie"]) {
317
+ cookieStrings = rawHeaders["set-cookie"];
318
+ }
319
+ }
320
+ // Cách 2: Nếu dùng Fetch API Native của NodeJS 18+
321
+ else if (typeof headers.getSetCookie === "function") {
322
+ cookieStrings = headers.getSetCookie();
318
323
  }
324
+ // Cách 3: Fallback dùng Regex Lookahead an toàn (Không bao giờ tách nhầm dấu phẩy của Expires date)
319
325
  else {
320
- cookieStrings = setCookieRaw.split(", ");
326
+ cookieStrings = setCookieRaw.split(/,\s*(?=[a-zA-Z0-9_-]+\s*=)/);
321
327
  }
322
328
  for (const cookie of cookieStrings) {
323
329
  const parsed = toughCookie.Cookie.parse(cookie);
package/dist/utils.js CHANGED
@@ -304,16 +304,22 @@ export async function request(ctx, url, options, raw = false) {
304
304
  const response = await ctx.options.polyfill(url, _options);
305
305
  const setCookieRaw = response.headers.get("set-cookie");
306
306
  if (setCookieRaw && !raw) {
307
- // Use getSetCookie() (Node.js 18+) to properly parse multi-cookie headers.
308
- // The split(", ") method breaks cookies containing Expires dates with commas
309
- // (e.g., "Expires=Wed, 18 Mar 2026..."), causing critical cookies like
310
- // zpsid and zpw_sek to be lost during QR login flow.
311
- let cookieStrings;
312
- if (typeof response.headers.getSetCookie === "function") {
313
- cookieStrings = response.headers.getSetCookie();
307
+ let cookieStrings = [];
308
+ const headers = response.headers;
309
+ // Cách 1: Ưu tiên lấy Mảng Raw chuẩn gốc (Array) của thư viện node-fetch
310
+ if (typeof headers.raw === "function") {
311
+ const rawHeaders = headers.raw();
312
+ if (rawHeaders && rawHeaders["set-cookie"]) {
313
+ cookieStrings = rawHeaders["set-cookie"];
314
+ }
315
+ }
316
+ // Cách 2: Nếu dùng Fetch API Native của NodeJS 18+
317
+ else if (typeof headers.getSetCookie === "function") {
318
+ cookieStrings = headers.getSetCookie();
314
319
  }
320
+ // Cách 3: Fallback dùng Regex Lookahead an toàn (Không bao giờ tách nhầm dấu phẩy của Expires date)
315
321
  else {
316
- cookieStrings = setCookieRaw.split(", ");
322
+ cookieStrings = setCookieRaw.split(/,\s*(?=[a-zA-Z0-9_-]+\s*=)/);
317
323
  }
318
324
  for (const cookie of cookieStrings) {
319
325
  const parsed = toughCookie.Cookie.parse(cookie);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softtynet/zca-js",
3
- "version": "2.1.4",
3
+ "version": "2.1.8",
4
4
  "description": "Unofficial Zalo API for JavaScript",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",