chat-adapter-wwebjs 0.2.0 → 0.2.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # chat-adapter-wwebjs
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Fixed
6
+
7
+ - No session could link since WhatsApp Web removed the module whatsapp-web.js waited on, around 2026-11-25: `connect()` failed after 30 seconds with `auth timeout`, with every release up to 1.34.8 and with the commit this adapter pointed to. It is now tested against whatsapp-web.js `38eb45b`, which reads the new one, and the README installs that commit by its full SHA, since npm resolves a short one over SSH and fails without a GitHub SSH key.
8
+ - `connect()` now says what to do when whatsapp-web.js times out this way: the error names the installed version and the commit to install.
9
+ - A second signal while the browser was closing, as Ctrl+C under `npx` or `npm exec` sends, ended the process before the browser closed, leaving Chromium running with the profile locked. Signals are now listened for until every session has closed.
10
+ - The README says how to let npm 11 and bun download Chromium, which they skip until allowed, and how to use a browser you already have.
11
+
3
12
  ## 0.2.0
4
13
 
5
14
  ### Changed
package/README.md CHANGED
@@ -22,12 +22,12 @@ Documentation: [chat-sdk.dev/adapters/community/wwebjs](https://chat-sdk.dev/ada
22
22
  ## Installation
23
23
 
24
24
  ```bash
25
- pnpm add chat-adapter-wwebjs chat @chat-adapter/shared github:pedroslopez/whatsapp-web.js#064a3d5
25
+ pnpm add chat-adapter-wwebjs chat @chat-adapter/shared github:pedroslopez/whatsapp-web.js#38eb45b2759450cc6fccb6a7a3f2cbc3bdb87db2
26
26
  ```
27
27
 
28
- ESM only, Node >= 20. `chat`, `@chat-adapter/shared` and `whatsapp-web.js` are peers, so you control their versions. WhatsApp Web renamed the IDs whatsapp-web.js reads, and 1.34.7 cannot send or find chats since, so until a release carries the fix, install the commit this adapter is tested against, as above.
28
+ ESM only, Node >= 20. `chat`, `@chat-adapter/shared` and `whatsapp-web.js` are peers, so you control their versions. WhatsApp Web removed a module whatsapp-web.js waited on, so since 2026-11-25 every release up to 1.34.8 times out before showing a QR. Until a release carries the fix, install the commit this adapter is tested against, as above. Give the full commit SHA: with a short one, npm resolves it over SSH and fails without a GitHub SSH key.
29
29
 
30
- whatsapp-web.js builds when installed from GitHub, and Puppeteer downloads a Chromium build on install. pnpm 10 and later run those scripts only when you allow them, in `pnpm-workspace.yaml`:
30
+ whatsapp-web.js builds when installed from GitHub, and Puppeteer downloads a Chromium build on install. Package managers that run install scripts only when you allow them skip both, and the first start fails with "Could not find Chrome". In pnpm 10 and later, allow them in `pnpm-workspace.yaml`:
31
31
 
32
32
  ```yaml
33
33
  onlyBuiltDependencies:
@@ -35,7 +35,7 @@ onlyBuiltDependencies:
35
35
  - puppeteer
36
36
  ```
37
37
 
38
- pnpm pins the commit in the lockfile rather than in `package.json`, so commit the lockfile.
38
+ pnpm pins the commit in the lockfile rather than in `package.json`, so commit the lockfile. With npm 11, run `npm approve-scripts puppeteer whatsapp-web.js`; with bun, list both under `trustedDependencies` in `package.json`. Or skip the download: `npx puppeteer browsers install chrome`, or point `PUPPETEER_EXECUTABLE_PATH` or `clientOptions.puppeteer.executablePath` at a Chromium you have.
39
39
 
40
40
  ## Usage
41
41
 
@@ -413,12 +413,13 @@ Examples: `wwebjs:15551234567@c.us` (DM by phone), `wwebjs:99990000111122@lid` (
413
413
 
414
414
  ### The QR never appears
415
415
 
416
+ - `connect()` fails with `auth timeout` or `Waiting failed`: the installed whatsapp-web.js no longer reads current WhatsApp Web. Install the commit in [Installation](#installation); the error names the version you have.
416
417
  - Check that Chromium can start: run locally with `clientOptions.puppeteer.headless: false`, or look for missing shared libraries on Linux.
417
418
  - WhatsApp replaces the QR every 20 seconds or so; render each one `onQR` receives.
418
419
 
419
420
  ### Sends fail or chats are not found
420
421
 
421
- - whatsapp-web.js 1.34.7 cannot read current WhatsApp Web. Install `github:pedroslopez/whatsapp-web.js#064a3d5`.
422
+ - whatsapp-web.js 1.34.8 and older cannot read current WhatsApp Web. Install `github:pedroslopez/whatsapp-web.js#38eb45b2759450cc6fccb6a7a3f2cbc3bdb87db2`.
422
423
 
423
424
  ### "The browser is already running"
424
425
 
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/adapter.ts
2
2
  import { randomUUID } from "crypto";
3
- import { extractCard, ResourceNotFoundError, ValidationError as ValidationError3 } from "@chat-adapter/shared";
3
+ import { extractCard, ResourceNotFoundError, ValidationError as ValidationError4 } from "@chat-adapter/shared";
4
4
  import {
5
5
  ConsoleLogger,
6
6
  defaultEmojiResolver,
@@ -167,12 +167,27 @@ import {
167
167
  downloadAttachment,
168
168
  extractFiles,
169
169
  extractPostableAttachments,
170
- ValidationError
170
+ ValidationError as ValidationError2
171
171
  } from "@chat-adapter/shared";
172
172
 
173
173
  // src/wwebjs.ts
174
+ import { createRequire } from "module";
175
+ import { ValidationError } from "@chat-adapter/shared";
174
176
  import wwebjs from "whatsapp-web.js";
175
177
  var { Client, Location, MessageMedia, Poll, ScheduledEvent } = wwebjs;
178
+ var TESTED_WWEBJS = "github:pedroslopez/whatsapp-web.js#38eb45b2759450cc6fccb6a7a3f2cbc3bdb87db2";
179
+ function outdatedError(error) {
180
+ if (!/auth timeout|Waiting failed/i.test(String(error))) return void 0;
181
+ let version = "unknown";
182
+ try {
183
+ version = createRequire(import.meta.url)("whatsapp-web.js/package.json").version;
184
+ } catch {
185
+ }
186
+ return new ValidationError(
187
+ "wwebjs",
188
+ `WhatsApp Web did not load (${String(error)}). The installed whatsapp-web.js ${version} likely cannot read current WhatsApp Web; install ${TESTED_WWEBJS}.`
189
+ );
190
+ }
176
191
 
177
192
  // src/media.ts
178
193
  var ATTACHMENT_TYPES = {
@@ -256,7 +271,7 @@ async function toMedia(source, maxBytes = DEFAULT_MAX_MEDIA_BYTES) {
256
271
  const data = source.data ?? await source.fetchData?.();
257
272
  if (!data) {
258
273
  if (!source.url) {
259
- throw new ValidationError("wwebjs", "Attachment has no data, fetchData, or url to send.");
274
+ throw new ValidationError2("wwebjs", "Attachment has no data, fetchData, or url to send.");
260
275
  }
261
276
  return fetchMedia(source.url, source, maxBytes);
262
277
  }
@@ -304,13 +319,13 @@ async function collect(stream, maxBytes, label) {
304
319
  return Buffer.concat(chunks);
305
320
  }
306
321
  function tooLarge(label, size, maxBytes) {
307
- return new ValidationError(
322
+ return new ValidationError2(
308
323
  "wwebjs",
309
324
  `Media for ${label} is ${size} bytes or more, over the ${maxBytes}-byte limit.`
310
325
  );
311
326
  }
312
327
  function unavailable(messageId) {
313
- return new ValidationError(
328
+ return new ValidationError2(
314
329
  "wwebjs",
315
330
  `Media for message ${messageId} is no longer available for download.`
316
331
  );
@@ -322,7 +337,7 @@ async function toBase64(data) {
322
337
  }
323
338
 
324
339
  // src/modals.ts
325
- import { ValidationError as ValidationError2 } from "@chat-adapter/shared";
340
+ import { ValidationError as ValidationError3 } from "@chat-adapter/shared";
326
341
  var DEFAULT_MODAL_TEXTS = {
327
342
  cancel: "cancel",
328
343
  cancelHint: "Send *cancel* to stop.",
@@ -353,7 +368,7 @@ function fromTrigger(triggerId) {
353
368
  }
354
369
  const [threadId, userId] = Array.isArray(ids) ? ids : [];
355
370
  if (typeof threadId === "string" && typeof userId === "string") return [threadId, userId];
356
- throw new ValidationError2("wwebjs", `Not a trigger this adapter issued: ${triggerId}`);
371
+ throw new ValidationError3("wwebjs", `Not a trigger this adapter issued: ${triggerId}`);
357
372
  }
358
373
  function createForm(modal, viewId, contextId) {
359
374
  const fields = modal.children.filter((child) => INPUTS.has(child.type));
@@ -497,14 +512,20 @@ function alive(pid) {
497
512
  var SIGNALS = ["SIGHUP", "SIGINT", "SIGTERM"];
498
513
  var CLOSE_TIMEOUT_MS = 8e3;
499
514
  var sessions = /* @__PURE__ */ new Map();
500
- async function onSignal(signal) {
501
- for (const name of SIGNALS) process.off(name, onSignal);
515
+ var shuttingDown = false;
516
+ function onSignal(signal) {
517
+ if (shuttingDown) return;
518
+ shuttingDown = true;
519
+ void shutdown(signal);
520
+ }
521
+ async function shutdown(signal) {
502
522
  const closing = [...sessions];
503
523
  await Promise.race([
504
524
  Promise.allSettled(closing.map(([close]) => close())),
505
525
  new Promise((resolve) => setTimeout(resolve, CLOSE_TIMEOUT_MS).unref())
506
526
  ]);
507
527
  for (const [, kill] of closing) kill();
528
+ for (const name of SIGNALS) process.off(name, onSignal);
508
529
  if (process.listenerCount(signal) === 0) process.kill(process.pid, signal);
509
530
  }
510
531
  function closeOnSignal(close, kill) {
@@ -512,7 +533,9 @@ function closeOnSignal(close, kill) {
512
533
  sessions.set(close, kill);
513
534
  return () => {
514
535
  sessions.delete(close);
515
- if (sessions.size === 0) for (const name of SIGNALS) process.off(name, onSignal);
536
+ if (sessions.size === 0 && !shuttingDown) {
537
+ for (const name of SIGNALS) process.off(name, onSignal);
538
+ }
516
539
  };
517
540
  }
518
541
 
@@ -621,7 +644,7 @@ var WWebJSAdapter = class {
621
644
  constructor(config = {}) {
622
645
  const name = config.adapterName ?? "wwebjs";
623
646
  if (name.includes(":")) {
624
- throw new ValidationError3("wwebjs", `Invalid adapterName "${name}". ":" is not allowed.`);
647
+ throw new ValidationError4("wwebjs", `Invalid adapterName "${name}". ":" is not allowed.`);
625
648
  }
626
649
  this._config = config;
627
650
  this.name = name;
@@ -703,7 +726,7 @@ var WWebJSAdapter = class {
703
726
  decodeThreadId(threadId) {
704
727
  const prefix = `${this.name}:`;
705
728
  if (!threadId.startsWith(prefix)) {
706
- throw new ValidationError3("wwebjs", `Invalid WhatsApp thread ID: ${threadId}`);
729
+ throw new ValidationError4("wwebjs", `Invalid WhatsApp thread ID: ${threadId}`);
707
730
  }
708
731
  return { chatId: threadId.slice(prefix.length) };
709
732
  }
@@ -774,7 +797,7 @@ var WWebJSAdapter = class {
774
797
  edited = await edit();
775
798
  }
776
799
  if (!edited) {
777
- throw new ValidationError3(
800
+ throw new ValidationError4(
778
801
  "wwebjs",
779
802
  `Message ${messageId} could not be edited. WhatsApp only allows editing your own messages within 15 minutes.`
780
803
  );
@@ -943,7 +966,7 @@ var WWebJSAdapter = class {
943
966
  /** Send a native WhatsApp poll. Between 2 and 12 options. */
944
967
  async sendPoll(args) {
945
968
  if (args.options.length < MIN_POLL_OPTIONS || args.options.length > MAX_POLL_OPTIONS) {
946
- throw new ValidationError3(
969
+ throw new ValidationError4(
947
970
  "wwebjs",
948
971
  `sendPoll: WhatsApp polls need between ${MIN_POLL_OPTIONS} and ${MAX_POLL_OPTIONS} options. Received ${args.options.length}.`
949
972
  );
@@ -1017,7 +1040,7 @@ var WWebJSAdapter = class {
1017
1040
  async fetchGroupParticipants(threadId) {
1018
1041
  const chat = await this._requireChat(threadId);
1019
1042
  if (!isGroupChat(chat)) {
1020
- throw new ValidationError3("wwebjs", "fetchGroupParticipants: thread is not a group.");
1043
+ throw new ValidationError4("wwebjs", "fetchGroupParticipants: thread is not a group.");
1021
1044
  }
1022
1045
  return chat.participants.map((participant) => ({
1023
1046
  isAdmin: participant.isAdmin || participant.isSuperAdmin,
@@ -1067,7 +1090,7 @@ var WWebJSAdapter = class {
1067
1090
  return;
1068
1091
  }
1069
1092
  if (!second) {
1070
- throw new ValidationError3(
1093
+ throw new ValidationError4(
1071
1094
  "wwebjs",
1072
1095
  "onPollVote: a handler is required when filtering by poll message ID."
1073
1096
  );
@@ -1114,7 +1137,7 @@ var WWebJSAdapter = class {
1114
1137
  let timer;
1115
1138
  const timedOut = new Promise((_resolve, reject) => {
1116
1139
  timer = setTimeout(
1117
- () => reject(new ValidationError3("wwebjs", `WhatsApp was not ready within ${timeoutMs}ms.`)),
1140
+ () => reject(new ValidationError4("wwebjs", `WhatsApp was not ready within ${timeoutMs}ms.`)),
1118
1141
  timeoutMs
1119
1142
  );
1120
1143
  timer.unref?.();
@@ -1126,7 +1149,7 @@ var WWebJSAdapter = class {
1126
1149
  await Promise.race([this._open(attempt), timedOut]);
1127
1150
  } catch (error) {
1128
1151
  if (this._owned && attempt === this._attempts) await this._close().catch(() => void 0);
1129
- throw error;
1152
+ throw outdatedError(error) ?? error;
1130
1153
  } finally {
1131
1154
  clearTimeout(timer);
1132
1155
  this.client.off("qr", waiting);
@@ -1147,7 +1170,7 @@ var WWebJSAdapter = class {
1147
1170
  let onFailure;
1148
1171
  const ready = new Promise((resolve, reject) => {
1149
1172
  onReady = resolve;
1150
- onFailure = (message) => reject(new ValidationError3("wwebjs", `WhatsApp authentication failed: ${message}`));
1173
+ onFailure = (message) => reject(new ValidationError4("wwebjs", `WhatsApp authentication failed: ${message}`));
1151
1174
  });
1152
1175
  ready.catch(() => void 0);
1153
1176
  this.client.on("ready", onReady);
@@ -1294,7 +1317,7 @@ var WWebJSAdapter = class {
1294
1317
  return { ...DEFAULT_MODAL_TEXTS, ...this._config.modalTexts };
1295
1318
  }
1296
1319
  async _start(threadId, userId, modal, form) {
1297
- if (!this._chat) throw new ValidationError3("wwebjs", "A modal needs Chat to be initialized.");
1320
+ if (!this._chat) throw new ValidationError4("wwebjs", "A modal needs Chat to be initialized.");
1298
1321
  const field = currentField(form);
1299
1322
  await this._chat.getState().set(this._formKey(threadId, userId), form, FORM_TTL_MS);
1300
1323
  const intro = introOf(modal, this._texts());
@@ -1878,9 +1901,9 @@ ${questionOf(first, retry, texts)}`);
1878
1901
  }
1879
1902
  _raw(sent, threadId) {
1880
1903
  if (!sent) {
1881
- throw new ValidationError3(
1904
+ throw new ValidationError4(
1882
1905
  "wwebjs",
1883
- "WhatsApp did not return the sent message. whatsapp-web.js 1.34.7 and older cannot read current WhatsApp Web; install github:pedroslopez/whatsapp-web.js#064a3d5."
1906
+ `WhatsApp did not return the sent message. whatsapp-web.js 1.34.8 and older cannot read current WhatsApp Web; install ${TESTED_WWEBJS}.`
1884
1907
  );
1885
1908
  }
1886
1909
  return { id: sent.id._serialized, raw: sent, threadId };
@@ -1962,7 +1985,7 @@ function participantAuthor(userId, isMe) {
1962
1985
  }
1963
1986
  function assertRange(label, value, bound) {
1964
1987
  if (!Number.isFinite(value) || value < -bound || value > bound) {
1965
- throw new ValidationError3(
1988
+ throw new ValidationError4(
1966
1989
  "wwebjs",
1967
1990
  `sendLocation: ${label} must be between -${bound} and ${bound}. Received ${value}.`
1968
1991
  );
@@ -1970,14 +1993,14 @@ function assertRange(label, value, bound) {
1970
1993
  }
1971
1994
 
1972
1995
  // src/guards.ts
1973
- import { ValidationError as ValidationError4 } from "@chat-adapter/shared";
1996
+ import { ValidationError as ValidationError5 } from "@chat-adapter/shared";
1974
1997
  function isWWebJSAdapter(value) {
1975
1998
  return value instanceof WWebJSAdapter;
1976
1999
  }
1977
2000
  function requireWWebJSAdapter(value) {
1978
2001
  const adapter = value && typeof value === "object" && "adapter" in value ? value.adapter : value;
1979
2002
  if (isWWebJSAdapter(adapter)) return adapter;
1980
- throw new ValidationError4("wwebjs", "This context does not belong to a wwebjs adapter.");
2003
+ throw new ValidationError5("wwebjs", "This context does not belong to a wwebjs adapter.");
1981
2004
  }
1982
2005
 
1983
2006
  // src/numbered-actions.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chat-adapter-wwebjs",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "WhatsApp adapter for Chat SDK, backed by whatsapp-web.js",
5
5
  "author": "nivandres",
6
6
  "license": "MIT",
@@ -63,7 +63,7 @@
63
63
  "tsup": "^9.0.0",
64
64
  "typescript": "^5.7.0",
65
65
  "vitest": "^5.1.0",
66
- "whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#064a3d5"
66
+ "whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#38eb45b2759450cc6fccb6a7a3f2cbc3bdb87db2"
67
67
  },
68
68
  "publishConfig": {
69
69
  "access": "public"