qwenproxy-cli 1.2.10 → 1.3.1

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.
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Parse the expiration (exp) Unix timestamp in seconds from a JWT string or a cookie string.
3
+ * Returns null if the token is opaque, malformed, or has no numeric exp claim.
4
+ */
5
+ export function parseJwtExpiry(tokenOrCookie?: string): number | null {
6
+ if (!tokenOrCookie || typeof tokenOrCookie !== "string") return null;
7
+
8
+ const tokenMatch = tokenOrCookie.match(/(?:^|;\s*)token=([^;]+)/);
9
+ const candidate = tokenMatch ? tokenMatch[1] : tokenOrCookie.trim();
10
+
11
+ try {
12
+ const token = decodeURIComponent(candidate.trim());
13
+ const segments = token.split(".");
14
+ if (segments.length !== 3 || !segments[1]) return null;
15
+
16
+ const payloadJson = Buffer.from(segments[1], "base64url").toString("utf-8");
17
+ const payload = JSON.parse(payloadJson);
18
+ const exp = payload.exp;
19
+ if (typeof exp !== "number" || !Number.isFinite(exp)) return null;
20
+ return exp;
21
+ } catch {
22
+ return null;
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Check if a token in a cookie or raw string expires within the given threshold (in minutes).
28
+ * Returns false if the token has no parseable JWT exp claim.
29
+ */
30
+ export function isTokenExpiringSoon(
31
+ cookie: string,
32
+ minutesBeforeExpiry = 5,
33
+ ): boolean {
34
+ const exp = parseJwtExpiry(cookie);
35
+ if (exp === null) return false;
36
+
37
+ const nowSec = Math.floor(Date.now() / 1000);
38
+ const thresholdSec = minutesBeforeExpiry * 60;
39
+ return exp - nowSec < thresholdSec;
40
+ }