@ezychat/wa 0.1.69 → 0.1.70
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/baileys/adapter.js
CHANGED
|
@@ -5,6 +5,7 @@ import { WaUnavailableError, } from '../types.js';
|
|
|
5
5
|
import { createAuthStore } from './auth-store.js';
|
|
6
6
|
import { backoffMs, classifyDisconnect } from './disconnect.js';
|
|
7
7
|
import { Lru } from './lru.js';
|
|
8
|
+
import { createWaVersionSource } from './wa-version.js';
|
|
8
9
|
import { jidType, mapStoredReactions, mapWAMessage, mapWAReaction, toMs } from './mapping.js';
|
|
9
10
|
import { quotedForChat, quotedFromRef } from './quoted.js';
|
|
10
11
|
import { mapWAProtocol } from './protocol.js';
|
|
@@ -16,6 +17,10 @@ const RAW_CACHE_MAX = 2000;
|
|
|
16
17
|
/** How long an edit or delete this adapter sent waits for WhatsApp's echo of it. */
|
|
17
18
|
const OWN_CHANGE_TTL_MS = 60_000;
|
|
18
19
|
const DAY_MS = 86_400_000;
|
|
20
|
+
/** One per process: every inbox and reconnect shares the looked-up WhatsApp Web version. */
|
|
21
|
+
const waVersion = createWaVersionSource({
|
|
22
|
+
fetchLatest: () => fetchLatestBaileysVersion(),
|
|
23
|
+
});
|
|
19
24
|
/** Boom status codes Baileys uses when the socket is closed / lost / replaced / unavailable. */
|
|
20
25
|
const CONNECTION_STATUS_CODES = new Set([408, 428, 440, 503]);
|
|
21
26
|
/** True when a send error means "the connection went away" (retry later) rather than a real rejection. */
|
|
@@ -239,14 +244,12 @@ export class BaileysAdapter extends EventEmitter {
|
|
|
239
244
|
async open() {
|
|
240
245
|
this.setStatus({ state: 'connecting', qr: null });
|
|
241
246
|
const { state, saveCreds } = await this.auth.load();
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
this.logger.warn({ err }, 'could not fetch latest WA version; using bundled default');
|
|
249
|
-
}
|
|
247
|
+
// Never wait for the GitHub lookup: connect with the version we have, refresh it for next time.
|
|
248
|
+
const version = waVersion.current();
|
|
249
|
+
void waVersion.refresh().then(({ error }) => {
|
|
250
|
+
if (error)
|
|
251
|
+
this.logger.warn({ err: error }, 'could not fetch latest WA version');
|
|
252
|
+
});
|
|
250
253
|
if (this.stopped)
|
|
251
254
|
return; // disconnect() was called while loading
|
|
252
255
|
const blog = this.logger.child({ module: 'baileys' });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WhatsApp Web version to announce. WhatsApp refuses clients whose version is too old, so Baileys
|
|
3
|
+
* suggests looking up the newest one from its GitHub copy on every connect. That lookup has no
|
|
4
|
+
* timeout, and awaiting it before the socket held every connect (and every new QR) at "connecting"
|
|
5
|
+
* while GitHub was slow or blocked. A connect never waits for it now: it uses the last version
|
|
6
|
+
* looked up (or Baileys' bundled one) at once, and the lookup refreshes in the background, bounded
|
|
7
|
+
* by `timeoutMs` and cached, so a connect WhatsApp refused for an old version retries with the new one.
|
|
8
|
+
*/
|
|
9
|
+
export type WaVersion = [number, number, number];
|
|
10
|
+
export interface WaVersionSourceOptions {
|
|
11
|
+
fetchLatest: () => Promise<{
|
|
12
|
+
version: WaVersion;
|
|
13
|
+
}>;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
/** How long a fetched version is reused before the next lookup. */
|
|
16
|
+
ttlMs?: number;
|
|
17
|
+
/** How long to skip the lookup after a failure or timeout. */
|
|
18
|
+
failureTtlMs?: number;
|
|
19
|
+
now?: () => number;
|
|
20
|
+
}
|
|
21
|
+
export declare const WA_VERSION_TIMEOUT_MS = 10000;
|
|
22
|
+
export interface WaVersionRefresh {
|
|
23
|
+
version: WaVersion | undefined;
|
|
24
|
+
/** Set when this refresh tried the lookup and it failed or timed out. */
|
|
25
|
+
error?: unknown;
|
|
26
|
+
}
|
|
27
|
+
export interface WaVersionSource {
|
|
28
|
+
/** The version to connect with now: the last one looked up, else undefined (Baileys' bundled one). */
|
|
29
|
+
current(): WaVersion | undefined;
|
|
30
|
+
/** Looks the version up unless a recent result is cached; never rejects, one lookup at a time. */
|
|
31
|
+
refresh(): Promise<WaVersionRefresh>;
|
|
32
|
+
}
|
|
33
|
+
export declare function createWaVersionSource(opts: WaVersionSourceOptions): WaVersionSource;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export const WA_VERSION_TIMEOUT_MS = 10_000;
|
|
2
|
+
const WA_VERSION_TTL_MS = 6 * 60 * 60_000;
|
|
3
|
+
const WA_VERSION_FAILURE_TTL_MS = 10 * 60_000;
|
|
4
|
+
export function createWaVersionSource(opts) {
|
|
5
|
+
const timeoutMs = opts.timeoutMs ?? WA_VERSION_TIMEOUT_MS;
|
|
6
|
+
const ttlMs = opts.ttlMs ?? WA_VERSION_TTL_MS;
|
|
7
|
+
const failureTtlMs = opts.failureTtlMs ?? WA_VERSION_FAILURE_TTL_MS;
|
|
8
|
+
const now = opts.now ?? Date.now;
|
|
9
|
+
let version;
|
|
10
|
+
let nextLookupAt = 0;
|
|
11
|
+
let inflight = null;
|
|
12
|
+
const lookup = async () => {
|
|
13
|
+
let timer;
|
|
14
|
+
try {
|
|
15
|
+
const timeout = new Promise((_, reject) => {
|
|
16
|
+
timer = setTimeout(() => reject(new Error(`WA version lookup timed out after ${timeoutMs} ms`)), timeoutMs);
|
|
17
|
+
timer.unref?.();
|
|
18
|
+
});
|
|
19
|
+
({ version } = await Promise.race([opts.fetchLatest(), timeout]));
|
|
20
|
+
nextLookupAt = now() + ttlMs;
|
|
21
|
+
return { version };
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
nextLookupAt = now() + failureTtlMs;
|
|
25
|
+
return { version, error };
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
current: () => version,
|
|
33
|
+
refresh() {
|
|
34
|
+
if (inflight)
|
|
35
|
+
return inflight;
|
|
36
|
+
if (now() < nextLookupAt)
|
|
37
|
+
return Promise.resolve({ version });
|
|
38
|
+
inflight = lookup().finally(() => {
|
|
39
|
+
inflight = null;
|
|
40
|
+
});
|
|
41
|
+
return inflight;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ezychat/wa",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.70",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "EzyChat core: WhatsApp adapter interface, Baileys adapter and fake adapter.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"typecheck": "tsc --noEmit"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@ezychat/shared": "0.1.
|
|
40
|
+
"@ezychat/shared": "0.1.70",
|
|
41
41
|
"baileys": "^7.0.0-rc14",
|
|
42
42
|
"pino": "^10.4.0",
|
|
43
43
|
"qrcode": "^1.5.4"
|