@vinzsocket/baileys 2.8.9 → 2.8.20

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.
Files changed (83) hide show
  1. package/lib/Defaults/index.js +75 -73
  2. package/lib/Signal/Group/ciphertext-message.js +9 -9
  3. package/lib/Signal/Group/group-session-builder.js +21 -21
  4. package/lib/Signal/Group/group_cipher.js +79 -81
  5. package/lib/Signal/Group/index.js +2 -2
  6. package/lib/Signal/Group/keyhelper.js +9 -9
  7. package/lib/Signal/Group/queue-job.js +43 -46
  8. package/lib/Signal/Group/sender-chain-key.js +24 -25
  9. package/lib/Signal/Group/sender-key-distribution-message.js +50 -58
  10. package/lib/Signal/Group/sender-key-message.js +55 -60
  11. package/lib/Signal/Group/sender-key-name.js +38 -39
  12. package/lib/Signal/Group/sender-key-record.js +39 -41
  13. package/lib/Signal/Group/sender-key-state.js +85 -89
  14. package/lib/Signal/Group/sender-message-key.js +22 -22
  15. package/lib/Signal/libsignal.js +387 -292
  16. package/lib/Signal/lid-mapping.js +98 -93
  17. package/lib/Socket/Client/types.js +6 -6
  18. package/lib/Socket/Client/websocket.js +44 -44
  19. package/lib/Socket/business.js +350 -365
  20. package/lib/Socket/chats.js +1019 -914
  21. package/lib/Socket/communities.js +454 -376
  22. package/lib/Socket/groups.js +378 -310
  23. package/lib/Socket/index.js +11 -11
  24. package/lib/Socket/messages-recv.js +1355 -1182
  25. package/lib/Socket/messages-send.js +1224 -1081
  26. package/lib/Socket/mex.js +40 -34
  27. package/lib/Socket/newsletter.js +212 -169
  28. package/lib/Socket/socket.js +896 -757
  29. package/lib/Store/index.js +1 -1
  30. package/lib/Store/make-cache-manager-store.js +75 -79
  31. package/lib/Store/make-in-memory-store.js +450 -389
  32. package/lib/Store/make-ordered-dictionary.js +73 -76
  33. package/lib/Store/object-repository.js +24 -22
  34. package/lib/Types/Chat.js +1 -7
  35. package/lib/Types/Label.js +20 -20
  36. package/lib/Types/LabelAssociation.js +2 -2
  37. package/lib/Types/Message.js +2 -2
  38. package/lib/Types/Newsletter.js +24 -24
  39. package/lib/Types/Socket.js +1 -1
  40. package/lib/Types/State.js +8 -8
  41. package/lib/Types/index.js +10 -10
  42. package/lib/Utils/auth-utils.js +408 -401
  43. package/lib/Utils/baileys-event-stream.js +44 -35
  44. package/lib/Utils/business.js +200 -197
  45. package/lib/Utils/chat-utils.js +775 -708
  46. package/lib/Utils/crypto.js +97 -85
  47. package/lib/Utils/decode-wa-message.js +240 -227
  48. package/lib/Utils/event-buffer.js +547 -518
  49. package/lib/Utils/extended-messages.js +250 -203
  50. package/lib/Utils/generics.js +300 -289
  51. package/lib/Utils/history.js +76 -69
  52. package/lib/Utils/lid-resolver.js +47 -42
  53. package/lib/Utils/link-preview.js +75 -65
  54. package/lib/Utils/logger.js +3 -1
  55. package/lib/Utils/lt-hash.js +38 -34
  56. package/lib/Utils/make-mutex.js +32 -34
  57. package/lib/Utils/mention.js +79 -86
  58. package/lib/Utils/message-retry-manager.js +148 -136
  59. package/lib/Utils/messages-media.js +652 -562
  60. package/lib/Utils/messages.js +753 -707
  61. package/lib/Utils/noise-handler.js +156 -135
  62. package/lib/Utils/process-message.js +404 -341
  63. package/lib/Utils/signal.js +180 -118
  64. package/lib/Utils/use-multi-file-auth-state.js +87 -91
  65. package/lib/Utils/validate-connection.js +167 -142
  66. package/lib/WABinary/constants.js +28 -1293
  67. package/lib/WABinary/decode.js +220 -218
  68. package/lib/WABinary/encode.js +199 -201
  69. package/lib/WABinary/generic-utils.js +80 -77
  70. package/lib/WABinary/jid-utils.js +41 -35
  71. package/lib/WAM/BinaryInfo.js +7 -7
  72. package/lib/WAM/constants.js +1 -15241
  73. package/lib/WAM/encode.js +124 -131
  74. package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
  75. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +51 -47
  76. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -22
  77. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -32
  78. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -19
  79. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -43
  80. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +17 -17
  81. package/lib/WAUSync/USyncQuery.js +82 -84
  82. package/lib/WAUSync/USyncUser.js +20 -20
  83. package/package.json +4 -1
@@ -7,78 +7,85 @@ import { normalizeMessageContent } from './messages.js';
7
7
  import { downloadContentFromMessage } from './messages-media.js';
8
8
  const inflatePromise = promisify(inflate);
9
9
  export const downloadHistory = async (msg, options) => {
10
- const stream = await downloadContentFromMessage(msg, 'md-msg-hist', { options });
11
- const bufferArray = [];
12
- for await (const chunk of stream) {
13
- bufferArray.push(chunk);
14
- }
15
- let buffer = Buffer.concat(bufferArray);
16
- // decompress buffer
17
- buffer = await inflatePromise(buffer);
18
- const syncData = proto.HistorySync.decode(buffer);
19
- return syncData;
10
+ const stream = await downloadContentFromMessage(msg, 'md-msg-hist', {
11
+ options
12
+ });
13
+ const bufferArray = [];
14
+ for await (const chunk of stream) {
15
+ bufferArray.push(chunk);
16
+ }
17
+ let buffer = Buffer.concat(bufferArray);
18
+ // decompress buffer
19
+ buffer = await inflatePromise(buffer);
20
+ const syncData = proto.HistorySync.decode(buffer);
21
+ return syncData;
20
22
  };
21
- export const processHistoryMessage = (item) => {
22
- const messages = [];
23
- const contacts = [];
24
- const chats = [];
25
- switch (item.syncType) {
26
- case proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP:
27
- case proto.HistorySync.HistorySyncType.RECENT:
28
- case proto.HistorySync.HistorySyncType.FULL:
29
- case proto.HistorySync.HistorySyncType.ON_DEMAND:
30
- for (const chat of item.conversations) {
31
- contacts.push({
32
- id: chat.id,
33
- name: chat.name || undefined,
34
- lid: chat.lidJid || undefined,
35
- phoneNumber: chat.pnJid || undefined
36
- });
37
- const msgs = chat.messages || [];
38
- delete chat.messages;
39
- for (const item of msgs) {
40
- const message = item.message;
41
- messages.push(message);
42
- if (!chat.messages?.length) {
43
- // keep only the most recent message in the chat array
44
- chat.messages = [{ message }];
45
- }
46
- if (!message.key.fromMe && !chat.lastMessageRecvTimestamp) {
47
- chat.lastMessageRecvTimestamp = toNumber(message.messageTimestamp);
48
- }
49
- if ((message.messageStubType === WAMessageStubType.BIZ_PRIVACY_MODE_TO_BSP ||
50
- message.messageStubType === WAMessageStubType.BIZ_PRIVACY_MODE_TO_FB) &&
51
- message.messageStubParameters?.[0]) {
52
- contacts.push({
53
- id: message.key.participant || message.key.remoteJid,
54
- verifiedName: message.messageStubParameters?.[0]
55
- });
56
- }
57
- }
58
- chats.push({ ...chat });
59
- }
60
- break;
61
- case proto.HistorySync.HistorySyncType.PUSH_NAME:
62
- for (const c of item.pushnames) {
63
- contacts.push({ id: c.id, notify: c.pushname });
64
- }
65
- break;
66
- }
67
- return {
68
- chats,
69
- contacts,
70
- messages,
71
- syncType: item.syncType,
72
- progress: item.progress
73
- };
23
+ export const processHistoryMessage = item => {
24
+ const messages = [];
25
+ const contacts = [];
26
+ const chats = [];
27
+ switch (item.syncType) {
28
+ case proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP:
29
+ case proto.HistorySync.HistorySyncType.RECENT:
30
+ case proto.HistorySync.HistorySyncType.FULL:
31
+ case proto.HistorySync.HistorySyncType.ON_DEMAND:
32
+ for (const chat of item.conversations) {
33
+ contacts.push({
34
+ id: chat.id,
35
+ name: chat.name || undefined,
36
+ lid: chat.lidJid || undefined,
37
+ phoneNumber: chat.pnJid || undefined
38
+ });
39
+ const msgs = chat.messages || [];
40
+ delete chat.messages;
41
+ for (const item of msgs) {
42
+ const message = item.message;
43
+ messages.push(message);
44
+ if (!chat.messages?.length) {
45
+ // keep only the most recent message in the chat array
46
+ chat.messages = [{
47
+ message
48
+ }];
49
+ }
50
+ if (!message.key.fromMe && !chat.lastMessageRecvTimestamp) {
51
+ chat.lastMessageRecvTimestamp = toNumber(message.messageTimestamp);
52
+ }
53
+ if ((message.messageStubType === WAMessageStubType.BIZ_PRIVACY_MODE_TO_BSP || message.messageStubType === WAMessageStubType.BIZ_PRIVACY_MODE_TO_FB) && message.messageStubParameters?.[0]) {
54
+ contacts.push({
55
+ id: message.key.participant || message.key.remoteJid,
56
+ verifiedName: message.messageStubParameters?.[0]
57
+ });
58
+ }
59
+ }
60
+ chats.push({
61
+ ...chat
62
+ });
63
+ }
64
+ break;
65
+ case proto.HistorySync.HistorySyncType.PUSH_NAME:
66
+ for (const c of item.pushnames) {
67
+ contacts.push({
68
+ id: c.id,
69
+ notify: c.pushname
70
+ });
71
+ }
72
+ break;
73
+ }
74
+ return {
75
+ chats,
76
+ contacts,
77
+ messages,
78
+ syncType: item.syncType,
79
+ progress: item.progress
80
+ };
74
81
  };
75
82
  export const downloadAndProcessHistorySyncNotification = async (msg, options) => {
76
- const historyMsg = await downloadHistory(msg, options);
77
- return processHistoryMessage(historyMsg);
83
+ const historyMsg = await downloadHistory(msg, options);
84
+ return processHistoryMessage(historyMsg);
78
85
  };
79
- export const getHistoryMsg = (message) => {
80
- const normalizedContent = !!message ? normalizeMessageContent(message) : undefined;
81
- const anyHistoryMsg = normalizedContent?.protocolMessage?.historySyncNotification;
82
- return anyHistoryMsg;
86
+ export const getHistoryMsg = message => {
87
+ const normalizedContent = !!message ? normalizeMessageContent(message) : undefined;
88
+ const anyHistoryMsg = normalizedContent?.protocolMessage?.historySyncNotification;
89
+ return anyHistoryMsg;
83
90
  };
84
91
  //# sourceMappingURL=history.js.map
@@ -16,52 +16,57 @@
16
16
  //
17
17
  // After that, buildTagAllText() (mention.js) will use it automatically.
18
18
  export function createLidResolver() {
19
- const nameByJid = new Map(); // exact jid string -> display name
20
- const MAX_ENTRIES = 5000;
21
- function remember(jid, name) {
22
- if (!jid || !name)
23
- return;
24
- if (nameByJid.size > MAX_ENTRIES)
25
- nameByJid.clear();
26
- nameByJid.set(jid, name);
19
+ const nameByJid = new Map(); // exact jid string -> display name
20
+ const MAX_ENTRIES = 5000;
21
+ function remember(jid, name) {
22
+ if (!jid || !name) return;
23
+ if (nameByJid.size > MAX_ENTRIES) nameByJid.clear();
24
+ nameByJid.set(jid, name);
25
+ }
26
+ function getDisplayName(jid) {
27
+ return jid ? nameByJid.get(jid) : undefined;
28
+ }
29
+ /** Feed it whatever groupMetadata() gave you (call this is optional — groups.js already does it automatically) */
30
+ function feedGroupMetadata(metadata) {
31
+ for (const p of metadata && metadata.participants || []) {
32
+ if (p.username) remember(p.id, p.username);
27
33
  }
28
- function getDisplayName(jid) {
29
- return jid ? nameByJid.get(jid) : undefined;
30
- }
31
- /** Feed it whatever groupMetadata() gave you (call this is optional — groups.js already does it automatically) */
32
- function feedGroupMetadata(metadata) {
33
- for (const p of (metadata && metadata.participants) || []) {
34
- if (p.username)
35
- remember(p.id, p.username);
36
- }
37
- }
38
- function attach(sock) {
39
- if (!sock || !sock.ev || typeof sock.ev.on !== 'function')
40
- return { getDisplayName, feedGroupMetadata, remember };
41
- sock.ev.on('contacts.update', (updates) => {
42
- for (const c of updates || []) {
43
- if (c.notify)
44
- remember(c.id, c.notify);
45
- if (c.name)
46
- remember(c.id, c.name);
47
- }
48
- });
49
- sock.ev.on('contacts.upsert', (contacts) => {
50
- for (const c of contacts || []) {
51
- if (c.name)
52
- remember(c.id, c.name);
53
- if (c.notify)
54
- remember(c.id, c.notify);
55
- }
56
- });
57
- return { getDisplayName, feedGroupMetadata, remember };
58
- }
59
- return { attach, getDisplayName, feedGroupMetadata, remember };
34
+ }
35
+ function attach(sock) {
36
+ if (!sock || !sock.ev || typeof sock.ev.on !== 'function') return {
37
+ getDisplayName,
38
+ feedGroupMetadata,
39
+ remember
40
+ };
41
+ sock.ev.on('contacts.update', updates => {
42
+ for (const c of updates || []) {
43
+ if (c.notify) remember(c.id, c.notify);
44
+ if (c.name) remember(c.id, c.name);
45
+ }
46
+ });
47
+ sock.ev.on('contacts.upsert', contacts => {
48
+ for (const c of contacts || []) {
49
+ if (c.name) remember(c.id, c.name);
50
+ if (c.notify) remember(c.id, c.notify);
51
+ }
52
+ });
53
+ return {
54
+ getDisplayName,
55
+ feedGroupMetadata,
56
+ remember
57
+ };
58
+ }
59
+ return {
60
+ attach,
61
+ getDisplayName,
62
+ feedGroupMetadata,
63
+ remember
64
+ };
60
65
  }
61
66
  // Module-level singleton so other utils (e.g. mention.js) can share the same
62
67
  // cache without every caller having to pass a resolver instance around.
63
68
  const defaultResolver = createLidResolver();
64
- export const attachLidResolver = (sock) => defaultResolver.attach(sock);
69
+ export const attachLidResolver = sock => defaultResolver.attach(sock);
65
70
  export const getDisplayName = defaultResolver.getDisplayName;
66
71
  export const feedGroupMetadata = defaultResolver.feedGroupMetadata;
67
- export const remember = defaultResolver.remember;
72
+ export const remember = defaultResolver.remember;
@@ -2,10 +2,13 @@ import { prepareWAMessageMedia } from './messages.js';
2
2
  import { extractImageThumb, getHttpStream } from './messages-media.js';
3
3
  const THUMBNAIL_WIDTH_PX = 192;
4
4
  /** Fetches an image and generates a thumbnail for it */
5
- const getCompressedJpegThumbnail = async (url, { thumbnailWidth, fetchOpts }) => {
6
- const stream = await getHttpStream(url, fetchOpts);
7
- const result = await extractImageThumb(stream, thumbnailWidth);
8
- return result;
5
+ const getCompressedJpegThumbnail = async (url, {
6
+ thumbnailWidth,
7
+ fetchOpts
8
+ }) => {
9
+ const stream = await getHttpStream(url, fetchOpts);
10
+ const result = await extractImageThumb(stream, thumbnailWidth);
11
+ return result;
9
12
  };
10
13
  /**
11
14
  * Given a piece of text, checks for any URL present, generates link preview for the same and returns it
@@ -14,72 +17,79 @@ const getCompressedJpegThumbnail = async (url, { thumbnailWidth, fetchOpts }) =>
14
17
  * @returns the URL info required to generate link preview
15
18
  */
16
19
  export const getUrlInfo = async (text, opts = {
17
- thumbnailWidth: THUMBNAIL_WIDTH_PX,
18
- fetchOpts: { timeout: 3000 }
20
+ thumbnailWidth: THUMBNAIL_WIDTH_PX,
21
+ fetchOpts: {
22
+ timeout: 3000
23
+ }
19
24
  }) => {
20
- try {
21
- // retries
22
- const retries = 0;
23
- const maxRetry = 5;
24
- const { getLinkPreview } = await import('link-preview-js');
25
- let previewLink = text;
26
- if (!text.startsWith('https://') && !text.startsWith('http://')) {
27
- previewLink = 'https://' + previewLink;
25
+ try {
26
+ // retries
27
+ const retries = 0;
28
+ const maxRetry = 5;
29
+ const {
30
+ getLinkPreview
31
+ } = await import('link-preview-js');
32
+ let previewLink = text;
33
+ if (!text.startsWith('https://') && !text.startsWith('http://')) {
34
+ previewLink = 'https://' + previewLink;
35
+ }
36
+ const info = await getLinkPreview(previewLink, {
37
+ ...opts.fetchOpts,
38
+ followRedirects: 'follow',
39
+ handleRedirects: (baseURL, forwardedURL) => {
40
+ const urlObj = new URL(baseURL);
41
+ const forwardedURLObj = new URL(forwardedURL);
42
+ if (retries >= maxRetry) {
43
+ return false;
28
44
  }
29
- const info = await getLinkPreview(previewLink, {
30
- ...opts.fetchOpts,
31
- followRedirects: 'follow',
32
- handleRedirects: (baseURL, forwardedURL) => {
33
- const urlObj = new URL(baseURL);
34
- const forwardedURLObj = new URL(forwardedURL);
35
- if (retries >= maxRetry) {
36
- return false;
37
- }
38
- if (forwardedURLObj.hostname === urlObj.hostname ||
39
- forwardedURLObj.hostname === 'www.' + urlObj.hostname ||
40
- 'www.' + forwardedURLObj.hostname === urlObj.hostname) {
41
- retries + 1;
42
- return true;
43
- }
44
- else {
45
- return false;
46
- }
47
- },
48
- headers: opts.fetchOpts
45
+ if (forwardedURLObj.hostname === urlObj.hostname || forwardedURLObj.hostname === 'www.' + urlObj.hostname || 'www.' + forwardedURLObj.hostname === urlObj.hostname) {
46
+ retries + 1;
47
+ return true;
48
+ } else {
49
+ return false;
50
+ }
51
+ },
52
+ headers: opts.fetchOpts
53
+ });
54
+ if (info && 'title' in info && info.title) {
55
+ const [image] = info.images;
56
+ const urlInfo = {
57
+ 'canonical-url': info.url,
58
+ 'matched-text': text,
59
+ title: info.title,
60
+ description: info.description,
61
+ originalThumbnailUrl: image
62
+ };
63
+ if (opts.uploadImage) {
64
+ const {
65
+ imageMessage
66
+ } = await prepareWAMessageMedia({
67
+ image: {
68
+ url: image
69
+ }
70
+ }, {
71
+ upload: opts.uploadImage,
72
+ mediaTypeOverride: 'thumbnail-link',
73
+ options: opts.fetchOpts
49
74
  });
50
- if (info && 'title' in info && info.title) {
51
- const [image] = info.images;
52
- const urlInfo = {
53
- 'canonical-url': info.url,
54
- 'matched-text': text,
55
- title: info.title,
56
- description: info.description,
57
- originalThumbnailUrl: image
58
- };
59
- if (opts.uploadImage) {
60
- const { imageMessage } = await prepareWAMessageMedia({ image: { url: image } }, {
61
- upload: opts.uploadImage,
62
- mediaTypeOverride: 'thumbnail-link',
63
- options: opts.fetchOpts
64
- });
65
- urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail ? Buffer.from(imageMessage.jpegThumbnail) : undefined;
66
- urlInfo.highQualityThumbnail = imageMessage || undefined;
67
- }
68
- else {
69
- try {
70
- urlInfo.jpegThumbnail = image ? (await getCompressedJpegThumbnail(image, opts)).buffer : undefined;
71
- }
72
- catch (error) {
73
- opts.logger?.debug({ err: error.stack, url: previewLink }, 'error in generating thumbnail');
74
- }
75
- }
76
- return urlInfo;
75
+ urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail ? Buffer.from(imageMessage.jpegThumbnail) : undefined;
76
+ urlInfo.highQualityThumbnail = imageMessage || undefined;
77
+ } else {
78
+ try {
79
+ urlInfo.jpegThumbnail = image ? (await getCompressedJpegThumbnail(image, opts)).buffer : undefined;
80
+ } catch (error) {
81
+ opts.logger?.debug({
82
+ err: error.stack,
83
+ url: previewLink
84
+ }, 'error in generating thumbnail');
77
85
  }
86
+ }
87
+ return urlInfo;
78
88
  }
79
- catch (error) {
80
- if (!error.message.includes('receive a valid')) {
81
- throw error;
82
- }
89
+ } catch (error) {
90
+ if (!error.message.includes('receive a valid')) {
91
+ throw error;
83
92
  }
93
+ }
84
94
  };
85
95
  //# sourceMappingURL=link-preview.js.map
@@ -1,3 +1,5 @@
1
1
  import P from 'pino';
2
- export default P({ timestamp: () => `,"time":"${new Date().toJSON()}"` });
2
+ export default P({
3
+ timestamp: () => `,"time":"${new Date().toJSON()}"`
4
+ });
3
5
  //# sourceMappingURL=logger.js.map
@@ -6,43 +6,47 @@ import { hkdf } from './crypto.js';
6
6
  */
7
7
  const o = 128;
8
8
  class LTHash {
9
- constructor(e) {
10
- this.salt = e;
9
+ constructor(e) {
10
+ this.salt = e;
11
+ }
12
+ async add(e, t) {
13
+ for (const item of t) {
14
+ e = await this._addSingle(e, item);
11
15
  }
12
- async add(e, t) {
13
- for (const item of t) {
14
- e = await this._addSingle(e, item);
15
- }
16
- return e;
16
+ return e;
17
+ }
18
+ async subtract(e, t) {
19
+ for (const item of t) {
20
+ e = await this._subtractSingle(e, item);
17
21
  }
18
- async subtract(e, t) {
19
- for (const item of t) {
20
- e = await this._subtractSingle(e, item);
21
- }
22
- return e;
23
- }
24
- async subtractThenAdd(e, addList, subtractList) {
25
- const subtracted = await this.subtract(e, subtractList);
26
- return this.add(subtracted, addList);
27
- }
28
- async _addSingle(e, t) {
29
- const derived = new Uint8Array(await hkdf(Buffer.from(t), o, { info: this.salt })).buffer;
30
- return this.performPointwiseWithOverflow(e, derived, (a, b) => a + b);
31
- }
32
- async _subtractSingle(e, t) {
33
- const derived = new Uint8Array(await hkdf(Buffer.from(t), o, { info: this.salt })).buffer;
34
- return this.performPointwiseWithOverflow(e, derived, (a, b) => a - b);
35
- }
36
- performPointwiseWithOverflow(e, t, op) {
37
- const n = new DataView(e);
38
- const i = new DataView(t);
39
- const out = new ArrayBuffer(n.byteLength);
40
- const s = new DataView(out);
41
- for (let offset = 0; offset < n.byteLength; offset += 2) {
42
- s.setUint16(offset, op(n.getUint16(offset, true), i.getUint16(offset, true)), true);
43
- }
44
- return out;
22
+ return e;
23
+ }
24
+ async subtractThenAdd(e, addList, subtractList) {
25
+ const subtracted = await this.subtract(e, subtractList);
26
+ return this.add(subtracted, addList);
27
+ }
28
+ async _addSingle(e, t) {
29
+ const derived = new Uint8Array(await hkdf(Buffer.from(t), o, {
30
+ info: this.salt
31
+ })).buffer;
32
+ return this.performPointwiseWithOverflow(e, derived, (a, b) => a + b);
33
+ }
34
+ async _subtractSingle(e, t) {
35
+ const derived = new Uint8Array(await hkdf(Buffer.from(t), o, {
36
+ info: this.salt
37
+ })).buffer;
38
+ return this.performPointwiseWithOverflow(e, derived, (a, b) => a - b);
39
+ }
40
+ performPointwiseWithOverflow(e, t, op) {
41
+ const n = new DataView(e);
42
+ const i = new DataView(t);
43
+ const out = new ArrayBuffer(n.byteLength);
44
+ const s = new DataView(out);
45
+ for (let offset = 0; offset < n.byteLength; offset += 2) {
46
+ s.setUint16(offset, op(n.getUint16(offset, true), i.getUint16(offset, true)), true);
45
47
  }
48
+ return out;
49
+ }
46
50
  }
47
51
  export const LT_HASH_ANTI_TAMPERING = new LTHash('WhatsApp Patch Integrity');
48
52
  //# sourceMappingURL=lt-hash.js.map
@@ -1,40 +1,38 @@
1
1
  export const makeMutex = () => {
2
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
- let task = Promise.resolve();
4
- let taskTimeout;
5
- return {
6
- mutex(code) {
7
- task = (async () => {
8
- // wait for the previous task to complete
9
- // if there is an error, we swallow so as to not block the queue
10
- try {
11
- await task;
12
- }
13
- catch { }
14
- try {
15
- // execute the current task
16
- const result = await code();
17
- return result;
18
- }
19
- finally {
20
- clearTimeout(taskTimeout);
21
- }
22
- })();
23
- // we replace the existing task, appending the new piece of execution to it
24
- // so the next task will have to wait for this one to finish
25
- return task;
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ let task = Promise.resolve();
4
+ let taskTimeout;
5
+ return {
6
+ mutex(code) {
7
+ task = (async () => {
8
+ // wait for the previous task to complete
9
+ // if there is an error, we swallow so as to not block the queue
10
+ try {
11
+ await task;
12
+ } catch {}
13
+ try {
14
+ // execute the current task
15
+ const result = await code();
16
+ return result;
17
+ } finally {
18
+ clearTimeout(taskTimeout);
26
19
  }
27
- };
20
+ })();
21
+ // we replace the existing task, appending the new piece of execution to it
22
+ // so the next task will have to wait for this one to finish
23
+ return task;
24
+ }
25
+ };
28
26
  };
29
27
  export const makeKeyedMutex = () => {
30
- const map = {};
31
- return {
32
- mutex(key, task) {
33
- if (!map[key]) {
34
- map[key] = makeMutex();
35
- }
36
- return map[key].mutex(task);
37
- }
38
- };
28
+ const map = {};
29
+ return {
30
+ mutex(key, task) {
31
+ if (!map[key]) {
32
+ map[key] = makeMutex();
33
+ }
34
+ return map[key].mutex(task);
35
+ }
36
+ };
39
37
  };
40
38
  //# sourceMappingURL=make-mutex.js.map