arn-browser 0.1.17 → 0.1.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arn-browser",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
4
4
  "description": "A lightweight, browser autmation helper.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -3,18 +3,12 @@
3
3
  import crypto from "crypto";
4
4
  import { arn, query } from "arn-knexjs";
5
5
 
6
- // Module-level cache for credentials (fetched once per process)
7
- let _credentials = null;
8
-
9
6
  /**
10
7
  * Loads Multilogin credentials from the database.
11
- * Queries `api_multilogin_token` for a single row where `status = 1`.
12
- * Results are cached in-memory so the DB is only hit once per process.
8
+ * Always reads fresh from DB (no in-memory cache) to support multiple processes.
13
9
  * @returns {Promise<{id: string, email: string, password: string, workspace_id: string, data: object}>}
14
10
  */
15
11
  async function loadCredentials() {
16
- if (_credentials) return _credentials;
17
-
18
12
  const { data: [row] = [], error } = await arn.single(
19
13
  query("api_multilogin_token").select("*").where({ status: 1 }).orderByRaw("random()").limit(1)
20
14
  );
@@ -23,13 +17,10 @@ async function loadCredentials() {
23
17
  throw new Error("[TokenManager] No active Multilogin credentials found (status = 1).");
24
18
  }
25
19
 
26
- _credentials = row;
27
- return _credentials;
20
+ return row;
28
21
  }
29
22
 
30
- async function saveTokens(tokens) {
31
- const creds = await loadCredentials();
32
-
23
+ async function saveTokens(creds, tokens) {
33
24
  await arn.single(
34
25
  query("api_multilogin_token")
35
26
  .update({
@@ -39,18 +30,6 @@ async function saveTokens(tokens) {
39
30
  );
40
31
  }
41
32
 
42
- async function loadTokens() {
43
- try {
44
- const creds = await loadCredentials();
45
- if (!creds.data || Object.keys(creds.data).length === 0) {
46
- return null;
47
- }
48
- return creds.data;
49
- } catch (err) {
50
- return null;
51
- }
52
- }
53
-
54
33
  function isJwtExpired(token) {
55
34
  if (!token) return true;
56
35
  try {
@@ -62,9 +41,7 @@ function isJwtExpired(token) {
62
41
  }
63
42
  }
64
43
 
65
- async function loginAndSaveTokens() {
66
- const creds = await loadCredentials();
67
-
44
+ async function loginAndSaveTokens(creds) {
68
45
  const passwordHash = crypto.createHash("md5").update(creds.password).digest("hex");
69
46
  const data = {
70
47
  email: creds.email,
@@ -85,7 +62,7 @@ async function loginAndSaveTokens() {
85
62
  const json = await res.json();
86
63
  const { token, refresh_token } = json.data;
87
64
 
88
- await saveTokens({
65
+ await saveTokens(creds, {
89
66
  token,
90
67
  refresh_token,
91
68
  email: creds.email,
@@ -96,9 +73,7 @@ async function loginAndSaveTokens() {
96
73
  return { token, refresh_token };
97
74
  }
98
75
 
99
- async function refreshAndSaveTokens(refresh_token) {
100
- const creds = await loadCredentials();
101
-
76
+ async function refreshAndSaveTokens(creds, refresh_token) {
102
77
  const data = {
103
78
  email: creds.email,
104
79
  refresh_token: refresh_token,
@@ -119,7 +94,7 @@ async function refreshAndSaveTokens(refresh_token) {
119
94
  const json = await res.json();
120
95
  const { token, refresh_token: new_refresh } = json.data;
121
96
 
122
- await saveTokens({
97
+ await saveTokens(creds, {
123
98
  token,
124
99
  refresh_token: new_refresh,
125
100
  email: creds.email,
@@ -131,7 +106,8 @@ async function refreshAndSaveTokens(refresh_token) {
131
106
  }
132
107
 
133
108
  async function getMultiloginToken() {
134
- let tokens = await loadTokens();
109
+ const creds = await loadCredentials();
110
+ const tokens = creds.data && Object.keys(creds.data).length > 0 ? creds.data : null;
135
111
 
136
112
  if (tokens && tokens.token && !isJwtExpired(tokens.token)) {
137
113
  return tokens.token;
@@ -139,14 +115,14 @@ async function getMultiloginToken() {
139
115
 
140
116
  if (tokens && tokens.refresh_token) {
141
117
  try {
142
- const { token } = await refreshAndSaveTokens(tokens.refresh_token);
118
+ const { token } = await refreshAndSaveTokens(creds, tokens.refresh_token);
143
119
  return token;
144
120
  } catch (err) {
145
121
  console.warn("[REFRESH FAILED] Will login again:", err.message);
146
122
  }
147
123
  }
148
124
 
149
- const { token } = await loginAndSaveTokens();
125
+ const { token } = await loginAndSaveTokens(creds);
150
126
  return token;
151
127
  }
152
128