@fhynella/baileys 1.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.
Files changed (100) hide show
  1. package/LICENSE +21 -0
  2. package/WAProto/GenerateStatics.sh +4 -0
  3. package/WAProto/WAProto.proto +4775 -0
  4. package/WAProto/index.js +169661 -0
  5. package/WAProto/p.html +1 -0
  6. package/engine-requirements.js +10 -0
  7. package/lib/Defaults/index.js +118 -0
  8. package/lib/Defaults/wileys-version.json +3 -0
  9. package/lib/Signal/Group/ciphertext-message.js +15 -0
  10. package/lib/Signal/Group/group-session-builder.js +64 -0
  11. package/lib/Signal/Group/group_cipher.js +96 -0
  12. package/lib/Signal/Group/index.js +57 -0
  13. package/lib/Signal/Group/keyhelper.js +55 -0
  14. package/lib/Signal/Group/queue-job.js +57 -0
  15. package/lib/Signal/Group/sender-chain-key.js +34 -0
  16. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  17. package/lib/Signal/Group/sender-key-message.js +69 -0
  18. package/lib/Signal/Group/sender-key-name.js +51 -0
  19. package/lib/Signal/Group/sender-key-record.js +53 -0
  20. package/lib/Signal/Group/sender-key-state.js +99 -0
  21. package/lib/Signal/Group/sender-message-key.js +29 -0
  22. package/lib/Signal/libsignal.js +174 -0
  23. package/lib/Socket/Client/index.js +18 -0
  24. package/lib/Socket/Client/types.js +13 -0
  25. package/lib/Socket/Client/websocket.js +111 -0
  26. package/lib/Socket/business.js +260 -0
  27. package/lib/Socket/chats.js +915 -0
  28. package/lib/Socket/groups.js +332 -0
  29. package/lib/Socket/index.js +10 -0
  30. package/lib/Socket/messages-recv.js +1054 -0
  31. package/lib/Socket/messages-send.js +949 -0
  32. package/lib/Socket/newsletter.js +250 -0
  33. package/lib/Socket/socket.js +654 -0
  34. package/lib/Socket/usync.js +70 -0
  35. package/lib/Store/index.js +8 -0
  36. package/lib/Store/make-in-memory-store.js +439 -0
  37. package/lib/Store/make-ordered-dictionary.js +81 -0
  38. package/lib/Store/object-repository.js +27 -0
  39. package/lib/Types/Auth.js +2 -0
  40. package/lib/Types/Call.js +2 -0
  41. package/lib/Types/Chat.js +4 -0
  42. package/lib/Types/Contact.js +2 -0
  43. package/lib/Types/Events.js +2 -0
  44. package/lib/Types/GroupMetadata.js +2 -0
  45. package/lib/Types/Label.js +27 -0
  46. package/lib/Types/LabelAssociation.js +9 -0
  47. package/lib/Types/Message.js +7 -0
  48. package/lib/Types/Newsletter.js +18 -0
  49. package/lib/Types/Product.js +2 -0
  50. package/lib/Types/Signal.js +2 -0
  51. package/lib/Types/Socket.js +2 -0
  52. package/lib/Types/State.js +2 -0
  53. package/lib/Types/USync.js +2 -0
  54. package/lib/Types/index.js +42 -0
  55. package/lib/Utils/auth-utils.js +199 -0
  56. package/lib/Utils/browser-utils.js +35 -0
  57. package/lib/Utils/business.js +234 -0
  58. package/lib/Utils/chat-utils.js +730 -0
  59. package/lib/Utils/crypto.js +193 -0
  60. package/lib/Utils/decode-wa-message.js +207 -0
  61. package/lib/Utils/event-buffer.js +518 -0
  62. package/lib/Utils/generics.js +467 -0
  63. package/lib/Utils/history.js +94 -0
  64. package/lib/Utils/index.js +35 -0
  65. package/lib/Utils/link-preview.js +126 -0
  66. package/lib/Utils/logger.js +7 -0
  67. package/lib/Utils/lt-hash.js +51 -0
  68. package/lib/Utils/make-mutex.js +43 -0
  69. package/lib/Utils/message-retry-manager.js +128 -0
  70. package/lib/Utils/messages-media.js +957 -0
  71. package/lib/Utils/messages.js +1049 -0
  72. package/lib/Utils/noise-handler.js +150 -0
  73. package/lib/Utils/process-message.js +404 -0
  74. package/lib/Utils/signal.js +153 -0
  75. package/lib/Utils/use-multi-file-auth-state.js +125 -0
  76. package/lib/Utils/validate-connection.js +229 -0
  77. package/lib/Utils/wileys-event-stream.js +63 -0
  78. package/lib/WABinary/constants.js +1303 -0
  79. package/lib/WABinary/decode.js +265 -0
  80. package/lib/WABinary/encode.js +250 -0
  81. package/lib/WABinary/generic-utils.js +110 -0
  82. package/lib/WABinary/index.js +21 -0
  83. package/lib/WABinary/jid-utils.js +85 -0
  84. package/lib/WABinary/types.js +2 -0
  85. package/lib/WAM/BinaryInfo.js +13 -0
  86. package/lib/WAM/constants.js +15350 -0
  87. package/lib/WAM/encode.js +155 -0
  88. package/lib/WAM/index.js +19 -0
  89. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
  90. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  91. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  92. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  93. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  94. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  95. package/lib/WAUSync/Protocols/index.js +20 -0
  96. package/lib/WAUSync/USyncQuery.js +89 -0
  97. package/lib/WAUSync/USyncUser.js +26 -0
  98. package/lib/WAUSync/index.js +19 -0
  99. package/lib/index.js +49 -0
  100. package/package.json +115 -0
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LabelAssociationType = void 0;
4
+ /** Association type */
5
+ var LabelAssociationType;
6
+ (function (LabelAssociationType) {
7
+ LabelAssociationType["Chat"] = "label_jid";
8
+ LabelAssociationType["Message"] = "label_message";
9
+ })(LabelAssociationType || (exports.LabelAssociationType = LabelAssociationType = {}));
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WAMessageStatus = exports.WAMessageStubType = exports.WAProto = void 0;
4
+ const WAProto_1 = require("../../WAProto");
5
+ Object.defineProperty(exports, "WAProto", { enumerable: true, get: function () { return WAProto_1.proto; } });
6
+ exports.WAMessageStubType = WAProto_1.proto.WebMessageInfo.StubType;
7
+ exports.WAMessageStatus = WAProto_1.proto.WebMessageInfo.Status;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.XWAPaths = exports.MexOperations = void 0;
4
+ var MexOperations;
5
+ (function (MexOperations) {
6
+ MexOperations["PROMOTE"] = "NotificationNewsletterAdminPromote";
7
+ MexOperations["DEMOTE"] = "NotificationNewsletterAdminDemote";
8
+ MexOperations["UPDATE"] = "NotificationNewsletterUpdate";
9
+ })(MexOperations || (exports.MexOperations = MexOperations = {}));
10
+ var XWAPaths;
11
+ (function (XWAPaths) {
12
+ XWAPaths["PROMOTE"] = "xwa2_notify_newsletter_admin_promote";
13
+ XWAPaths["DEMOTE"] = "xwa2_notify_newsletter_admin_demote";
14
+ XWAPaths["ADMIN_COUNT"] = "xwa2_newsletter_admin";
15
+ XWAPaths["CREATE"] = "xwa2_newsletter_create";
16
+ XWAPaths["NEWSLETTER"] = "xwa2_newsletter";
17
+ XWAPaths["METADATA_UPDATE"] = "xwa2_notify_newsletter_on_metadata_update";
18
+ })(XWAPaths || (exports.XWAPaths = XWAPaths = {}));
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.DisconnectReason = void 0;
18
+ __exportStar(require("./Auth"), exports);
19
+ __exportStar(require("./GroupMetadata"), exports);
20
+ __exportStar(require("./Newsletter"), exports);
21
+ __exportStar(require("./Chat"), exports);
22
+ __exportStar(require("./Contact"), exports);
23
+ __exportStar(require("./State"), exports);
24
+ __exportStar(require("./Message"), exports);
25
+ __exportStar(require("./Socket"), exports);
26
+ __exportStar(require("./Events"), exports);
27
+ __exportStar(require("./Product"), exports);
28
+ __exportStar(require("./Call"), exports);
29
+ __exportStar(require("./Signal"), exports);
30
+ var DisconnectReason;
31
+ (function (DisconnectReason) {
32
+ DisconnectReason[DisconnectReason["connectionClosed"] = 428] = "connectionClosed";
33
+ DisconnectReason[DisconnectReason["connectionLost"] = 408] = "connectionLost";
34
+ DisconnectReason[DisconnectReason["connectionReplaced"] = 440] = "connectionReplaced";
35
+ DisconnectReason[DisconnectReason["timedOut"] = 408] = "timedOut";
36
+ DisconnectReason[DisconnectReason["loggedOut"] = 401] = "loggedOut";
37
+ DisconnectReason[DisconnectReason["badSession"] = 500] = "badSession";
38
+ DisconnectReason[DisconnectReason["restartRequired"] = 515] = "restartRequired";
39
+ DisconnectReason[DisconnectReason["multideviceMismatch"] = 411] = "multideviceMismatch";
40
+ DisconnectReason[DisconnectReason["forbidden"] = 403] = "forbidden";
41
+ DisconnectReason[DisconnectReason["unavailableService"] = 503] = "unavailableService";
42
+ })(DisconnectReason || (exports.DisconnectReason = DisconnectReason = {}));
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.initAuthCreds = exports.addTransactionCapability = void 0;
7
+ exports.makeCacheableSignalKeyStore = makeCacheableSignalKeyStore;
8
+ const crypto_1 = require("crypto");
9
+ const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
10
+ const Defaults_1 = require("../Defaults");
11
+ const crypto_2 = require("./crypto");
12
+ const generics_1 = require("./generics");
13
+ /**
14
+ * Adds caching capability to a SignalKeyStore
15
+ * @param store the store to add caching to
16
+ * @param logger to log trace events
17
+ * @param _cache cache store to use
18
+ */
19
+ function makeCacheableSignalKeyStore(store, logger, _cache) {
20
+ const cache = _cache || new node_cache_1.default({
21
+ stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes
22
+ useClones: false,
23
+ deleteOnExpire: true,
24
+ });
25
+ function getUniqueId(type, id) {
26
+ return `${type}.${id}`;
27
+ }
28
+ return {
29
+ async get(type, ids) {
30
+ const data = {};
31
+ const idsToFetch = [];
32
+ for (const id of ids) {
33
+ const item = cache.get(getUniqueId(type, id));
34
+ if (typeof item !== 'undefined') {
35
+ data[id] = item;
36
+ }
37
+ else {
38
+ idsToFetch.push(id);
39
+ }
40
+ }
41
+ if (idsToFetch.length) {
42
+ logger === null || logger === void 0 ? void 0 : logger.trace({ items: idsToFetch.length }, 'loading from store');
43
+ const fetched = await store.get(type, idsToFetch);
44
+ for (const id of idsToFetch) {
45
+ const item = fetched[id];
46
+ if (item) {
47
+ data[id] = item;
48
+ cache.set(getUniqueId(type, id), item);
49
+ }
50
+ }
51
+ }
52
+ return data;
53
+ },
54
+ async set(data) {
55
+ let keys = 0;
56
+ for (const type in data) {
57
+ for (const id in data[type]) {
58
+ cache.set(getUniqueId(type, id), data[type][id]);
59
+ keys += 1;
60
+ }
61
+ }
62
+ logger === null || logger === void 0 ? void 0 : logger.trace({ keys }, 'updated cache');
63
+ await store.set(data);
64
+ },
65
+ async clear() {
66
+ var _a;
67
+ cache.flushAll();
68
+ await ((_a = store.clear) === null || _a === void 0 ? void 0 : _a.call(store));
69
+ }
70
+ };
71
+ }
72
+ /**
73
+ * Adds DB like transaction capability (https://en.wikipedia.org/wiki/Database_transaction) to the SignalKeyStore,
74
+ * this allows batch read & write operations & improves the performance of the lib
75
+ * @param state the key store to apply this capability to
76
+ * @param logger logger to log events
77
+ * @returns SignalKeyStore with transaction capability
78
+ */
79
+ const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetweenTriesMs }) => {
80
+ // number of queries made to the DB during the transaction
81
+ // only there for logging purposes
82
+ let dbQueriesInTransaction = 0;
83
+ let transactionCache = {};
84
+ let mutations = {};
85
+ let transactionsInProgress = 0;
86
+ return {
87
+ get: async (type, ids) => {
88
+ if (isInTransaction()) {
89
+ const dict = transactionCache[type];
90
+ const idsRequiringFetch = dict
91
+ ? ids.filter(item => typeof dict[item] === 'undefined')
92
+ : ids;
93
+ // only fetch if there are any items to fetch
94
+ if (idsRequiringFetch.length) {
95
+ dbQueriesInTransaction += 1;
96
+ const result = await state.get(type, idsRequiringFetch);
97
+ transactionCache[type] || (transactionCache[type] = {});
98
+ Object.assign(transactionCache[type], result);
99
+ }
100
+ return ids.reduce((dict, id) => {
101
+ var _a;
102
+ const value = (_a = transactionCache[type]) === null || _a === void 0 ? void 0 : _a[id];
103
+ if (value) {
104
+ dict[id] = value;
105
+ }
106
+ return dict;
107
+ }, {});
108
+ }
109
+ else {
110
+ return state.get(type, ids);
111
+ }
112
+ },
113
+ set: data => {
114
+ if (isInTransaction()) {
115
+ logger.trace({ types: Object.keys(data) }, 'caching in transaction');
116
+ for (const key in data) {
117
+ transactionCache[key] = transactionCache[key] || {};
118
+ Object.assign(transactionCache[key], data[key]);
119
+ mutations[key] = mutations[key] || {};
120
+ Object.assign(mutations[key], data[key]);
121
+ }
122
+ }
123
+ else {
124
+ return state.set(data);
125
+ }
126
+ },
127
+ isInTransaction,
128
+ async transaction(work) {
129
+ let result;
130
+ transactionsInProgress += 1;
131
+ if (transactionsInProgress === 1) {
132
+ logger.trace('entering transaction');
133
+ }
134
+ try {
135
+ result = await work();
136
+ // commit if this is the outermost transaction
137
+ if (transactionsInProgress === 1) {
138
+ if (Object.keys(mutations).length) {
139
+ logger.trace('committing transaction');
140
+ // retry mechanism to ensure we've some recovery
141
+ // in case a transaction fails in the first attempt
142
+ let tries = maxCommitRetries;
143
+ while (tries) {
144
+ tries -= 1;
145
+ try {
146
+ await state.set(mutations);
147
+ logger.trace({ dbQueriesInTransaction }, 'committed transaction');
148
+ break;
149
+ }
150
+ catch (error) {
151
+ logger.warn(`failed to commit ${Object.keys(mutations).length} mutations, tries left=${tries}`);
152
+ await (0, generics_1.delay)(delayBetweenTriesMs);
153
+ }
154
+ }
155
+ }
156
+ else {
157
+ logger.trace('no mutations in transaction');
158
+ }
159
+ }
160
+ }
161
+ finally {
162
+ transactionsInProgress -= 1;
163
+ if (transactionsInProgress === 0) {
164
+ transactionCache = {};
165
+ mutations = {};
166
+ dbQueriesInTransaction = 0;
167
+ }
168
+ }
169
+ return result;
170
+ }
171
+ };
172
+ function isInTransaction() {
173
+ return transactionsInProgress > 0;
174
+ }
175
+ };
176
+ exports.addTransactionCapability = addTransactionCapability;
177
+ const initAuthCreds = () => {
178
+ const identityKey = crypto_2.Curve.generateKeyPair();
179
+ return {
180
+ noiseKey: crypto_2.Curve.generateKeyPair(),
181
+ pairingEphemeralKeyPair: crypto_2.Curve.generateKeyPair(),
182
+ signedIdentityKey: identityKey,
183
+ signedPreKey: (0, crypto_2.signedKeyPair)(identityKey, 1),
184
+ registrationId: (0, generics_1.generateRegistrationId)(),
185
+ advSecretKey: (0, crypto_1.randomBytes)(32).toString('base64'),
186
+ processedHistoryMessages: [],
187
+ nextPreKeyId: 1,
188
+ firstUnuploadedPreKeyId: 1,
189
+ accountSyncCounter: 0,
190
+ accountSettings: {
191
+ unarchiveChats: false
192
+ },
193
+ registered: false,
194
+ pairingCode: undefined,
195
+ lastPropHash: undefined,
196
+ routingInfo: undefined,
197
+ };
198
+ };
199
+ exports.initAuthCreds = initAuthCreds;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPlatformId = exports.Browsers = void 0;
4
+
5
+ const os = require("os");
6
+ const { proto } = require("../../WAProto/index.js");
7
+
8
+ const PLATFORM_MAP = {
9
+ 'aix': 'AIX',
10
+ 'darwin': 'Mac OS',
11
+ 'win32': 'Windows',
12
+ 'android': 'Android',
13
+ 'freebsd': 'FreeBSD',
14
+ 'openbsd': 'OpenBSD',
15
+ 'sunos': 'Solaris',
16
+ 'linux': undefined, // Default ke Ubuntu untuk Linux
17
+ 'haiku': undefined,
18
+ 'cygwin': undefined,
19
+ 'netbsd': undefined
20
+ };
21
+
22
+ // Fixed: Browsers sekarang fungsi yang return array [platform, browser, version]
23
+ // Ini kompatibel dengan pemanggilan Browsers('Chrome') di Defaults/index.js
24
+ exports.Browsers = (browser) => {
25
+ const osName = PLATFORM_MAP[os.platform()] || 'Ubuntu'; // Default Ubuntu kalau undefined
26
+ const osRelease = os.release(); // Ambil versi OS real-time
27
+ return [osName, browser, osRelease];
28
+ };
29
+
30
+ const getPlatformId = (browser) => {
31
+ const platformType = proto.DeviceProps.PlatformType[browser.toUpperCase()];
32
+ return platformType ? platformType.toString() : '1'; // Default Chrome
33
+ };
34
+
35
+ exports.getPlatformId = getPlatformId;
@@ -0,0 +1,234 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uploadingNecessaryImages = exports.parseProductNode = exports.toProductNode = exports.parseOrderDetailsNode = exports.parseCollectionsNode = exports.parseCatalogNode = void 0;
4
+ exports.uploadingNecessaryImagesOfProduct = uploadingNecessaryImagesOfProduct;
5
+ const boom_1 = require("@hapi/boom");
6
+ const crypto_1 = require("crypto");
7
+ const WABinary_1 = require("../WABinary");
8
+ const messages_media_1 = require("./messages-media");
9
+ const parseCatalogNode = (node) => {
10
+ const catalogNode = (0, WABinary_1.getBinaryNodeChild)(node, 'product_catalog');
11
+ const products = (0, WABinary_1.getBinaryNodeChildren)(catalogNode, 'product').map(exports.parseProductNode);
12
+ const paging = (0, WABinary_1.getBinaryNodeChild)(catalogNode, 'paging');
13
+ return {
14
+ products,
15
+ nextPageCursor: paging
16
+ ? (0, WABinary_1.getBinaryNodeChildString)(paging, 'after')
17
+ : undefined
18
+ };
19
+ };
20
+ exports.parseCatalogNode = parseCatalogNode;
21
+ const parseCollectionsNode = (node) => {
22
+ const collectionsNode = (0, WABinary_1.getBinaryNodeChild)(node, 'collections');
23
+ const collections = (0, WABinary_1.getBinaryNodeChildren)(collectionsNode, 'collection').map(collectionNode => {
24
+ const id = (0, WABinary_1.getBinaryNodeChildString)(collectionNode, 'id');
25
+ const name = (0, WABinary_1.getBinaryNodeChildString)(collectionNode, 'name');
26
+ const products = (0, WABinary_1.getBinaryNodeChildren)(collectionNode, 'product').map(exports.parseProductNode);
27
+ return {
28
+ id,
29
+ name,
30
+ products,
31
+ status: parseStatusInfo(collectionNode)
32
+ };
33
+ });
34
+ return {
35
+ collections
36
+ };
37
+ };
38
+ exports.parseCollectionsNode = parseCollectionsNode;
39
+ const parseOrderDetailsNode = (node) => {
40
+ const orderNode = (0, WABinary_1.getBinaryNodeChild)(node, 'order');
41
+ const products = (0, WABinary_1.getBinaryNodeChildren)(orderNode, 'product').map(productNode => {
42
+ const imageNode = (0, WABinary_1.getBinaryNodeChild)(productNode, 'image');
43
+ return {
44
+ id: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'id'),
45
+ name: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'name'),
46
+ imageUrl: (0, WABinary_1.getBinaryNodeChildString)(imageNode, 'url'),
47
+ price: +(0, WABinary_1.getBinaryNodeChildString)(productNode, 'price'),
48
+ currency: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'currency'),
49
+ quantity: +(0, WABinary_1.getBinaryNodeChildString)(productNode, 'quantity')
50
+ };
51
+ });
52
+ const priceNode = (0, WABinary_1.getBinaryNodeChild)(orderNode, 'price');
53
+ const orderDetails = {
54
+ price: {
55
+ total: +(0, WABinary_1.getBinaryNodeChildString)(priceNode, 'total'),
56
+ currency: (0, WABinary_1.getBinaryNodeChildString)(priceNode, 'currency'),
57
+ },
58
+ products
59
+ };
60
+ return orderDetails;
61
+ };
62
+ exports.parseOrderDetailsNode = parseOrderDetailsNode;
63
+ const toProductNode = (productId, product) => {
64
+ const attrs = {};
65
+ const content = [];
66
+ if (typeof productId !== 'undefined') {
67
+ content.push({
68
+ tag: 'id',
69
+ attrs: {},
70
+ content: Buffer.from(productId)
71
+ });
72
+ }
73
+ if (typeof product.name !== 'undefined') {
74
+ content.push({
75
+ tag: 'name',
76
+ attrs: {},
77
+ content: Buffer.from(product.name)
78
+ });
79
+ }
80
+ if (typeof product.description !== 'undefined') {
81
+ content.push({
82
+ tag: 'description',
83
+ attrs: {},
84
+ content: Buffer.from(product.description)
85
+ });
86
+ }
87
+ if (typeof product.retailerId !== 'undefined') {
88
+ content.push({
89
+ tag: 'retailer_id',
90
+ attrs: {},
91
+ content: Buffer.from(product.retailerId)
92
+ });
93
+ }
94
+ if (product.images.length) {
95
+ content.push({
96
+ tag: 'media',
97
+ attrs: {},
98
+ content: product.images.map(img => {
99
+ if (!('url' in img)) {
100
+ throw new boom_1.Boom('Expected img for product to already be uploaded', { statusCode: 400 });
101
+ }
102
+ return {
103
+ tag: 'image',
104
+ attrs: {},
105
+ content: [
106
+ {
107
+ tag: 'url',
108
+ attrs: {},
109
+ content: Buffer.from(img.url.toString())
110
+ }
111
+ ]
112
+ };
113
+ })
114
+ });
115
+ }
116
+ if (typeof product.price !== 'undefined') {
117
+ content.push({
118
+ tag: 'price',
119
+ attrs: {},
120
+ content: Buffer.from(product.price.toString())
121
+ });
122
+ }
123
+ if (typeof product.currency !== 'undefined') {
124
+ content.push({
125
+ tag: 'currency',
126
+ attrs: {},
127
+ content: Buffer.from(product.currency)
128
+ });
129
+ }
130
+ if ('originCountryCode' in product) {
131
+ if (typeof product.originCountryCode === 'undefined') {
132
+ attrs['compliance_category'] = 'COUNTRY_ORIGIN_EXEMPT';
133
+ }
134
+ else {
135
+ content.push({
136
+ tag: 'compliance_info',
137
+ attrs: {},
138
+ content: [
139
+ {
140
+ tag: 'country_code_origin',
141
+ attrs: {},
142
+ content: Buffer.from(product.originCountryCode)
143
+ }
144
+ ]
145
+ });
146
+ }
147
+ }
148
+ if (typeof product.isHidden !== 'undefined') {
149
+ attrs['is_hidden'] = product.isHidden.toString();
150
+ }
151
+ const node = {
152
+ tag: 'product',
153
+ attrs,
154
+ content
155
+ };
156
+ return node;
157
+ };
158
+ exports.toProductNode = toProductNode;
159
+ const parseProductNode = (productNode) => {
160
+ const isHidden = productNode.attrs.is_hidden === 'true';
161
+ const id = (0, WABinary_1.getBinaryNodeChildString)(productNode, 'id');
162
+ const mediaNode = (0, WABinary_1.getBinaryNodeChild)(productNode, 'media');
163
+ const statusInfoNode = (0, WABinary_1.getBinaryNodeChild)(productNode, 'status_info');
164
+ const product = {
165
+ id,
166
+ imageUrls: parseImageUrls(mediaNode),
167
+ reviewStatus: {
168
+ whatsapp: (0, WABinary_1.getBinaryNodeChildString)(statusInfoNode, 'status'),
169
+ },
170
+ availability: 'in stock',
171
+ name: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'name'),
172
+ retailerId: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'retailer_id'),
173
+ url: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'url'),
174
+ description: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'description'),
175
+ price: +(0, WABinary_1.getBinaryNodeChildString)(productNode, 'price'),
176
+ currency: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'currency'),
177
+ isHidden,
178
+ };
179
+ return product;
180
+ };
181
+ exports.parseProductNode = parseProductNode;
182
+ /**
183
+ * Uploads images not already uploaded to WA's servers
184
+ */
185
+ async function uploadingNecessaryImagesOfProduct(product, waUploadToServer, timeoutMs = 30000) {
186
+ product = {
187
+ ...product,
188
+ images: product.images ? await (0, exports.uploadingNecessaryImages)(product.images, waUploadToServer, timeoutMs) : product.images
189
+ };
190
+ return product;
191
+ }
192
+ /**
193
+ * Uploads images not already uploaded to WA's servers
194
+ */
195
+ const uploadingNecessaryImages = async (images, waUploadToServer, timeoutMs = 30000) => {
196
+ const results = await Promise.all(images.map(async (img) => {
197
+ if ('url' in img) {
198
+ const url = img.url.toString();
199
+ if (url.includes('.whatsapp.net')) {
200
+ return { url };
201
+ }
202
+ }
203
+ const { stream } = await (0, messages_media_1.getStream)(img);
204
+ const hasher = (0, crypto_1.createHash)('sha256');
205
+ const contentBlocks = [];
206
+ for await (const block of stream) {
207
+ hasher.update(block);
208
+ contentBlocks.push(block);
209
+ }
210
+ const sha = hasher.digest('base64');
211
+ const { directPath } = await waUploadToServer((0, messages_media_1.toReadable)(Buffer.concat(contentBlocks)), {
212
+ mediaType: 'product-catalog-image',
213
+ fileEncSha256B64: sha,
214
+ timeoutMs
215
+ });
216
+ return { url: (0, messages_media_1.getUrlFromDirectPath)(directPath) };
217
+ }));
218
+ return results;
219
+ };
220
+ exports.uploadingNecessaryImages = uploadingNecessaryImages;
221
+ const parseImageUrls = (mediaNode) => {
222
+ const imgNode = (0, WABinary_1.getBinaryNodeChild)(mediaNode, 'image');
223
+ return {
224
+ requested: (0, WABinary_1.getBinaryNodeChildString)(imgNode, 'request_image_url'),
225
+ original: (0, WABinary_1.getBinaryNodeChildString)(imgNode, 'original_image_url')
226
+ };
227
+ };
228
+ const parseStatusInfo = (mediaNode) => {
229
+ const node = (0, WABinary_1.getBinaryNodeChild)(mediaNode, 'status_info');
230
+ return {
231
+ status: (0, WABinary_1.getBinaryNodeChildString)(node, 'status'),
232
+ canAppeal: (0, WABinary_1.getBinaryNodeChildString)(node, 'can_appeal') === 'true',
233
+ };
234
+ };