@ryuhan/baileys 1.0.9 → 1.2.9
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/{README.MD → README.md} +63 -7
- package/WAProto/GenerateStatics.sh +3 -4
- package/WAProto/WAProto.proto +1215 -511
- package/WAProto/fix-imports.js +81 -0
- package/WAProto/index.d.ts +14017 -0
- package/WAProto/index.js +65471 -137445
- package/index.cjs +196 -0
- package/index.d.ts +30 -0
- package/lib/Defaults/index.d.ts +74 -0
- package/lib/Defaults/index.d.ts.map +1 -0
- package/lib/Defaults/index.js +79 -65
- package/lib/Defaults/index.js.map +1 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
- package/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
- package/lib/Signal/Group/ciphertext-message.js +2 -5
- package/lib/Signal/Group/ciphertext-message.js.map +1 -0
- package/lib/Signal/Group/group-session-builder.d.ts +15 -0
- package/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
- package/lib/Signal/Group/group-session-builder.js +7 -41
- package/lib/Signal/Group/group-session-builder.js.map +1 -0
- package/lib/Signal/Group/group_cipher.d.ts +17 -0
- package/lib/Signal/Group/group_cipher.d.ts.map +1 -0
- package/lib/Signal/Group/group_cipher.js +37 -51
- package/lib/Signal/Group/group_cipher.js.map +1 -0
- package/lib/Signal/Group/index.d.ts +12 -0
- package/lib/Signal/Group/index.d.ts.map +1 -0
- package/lib/Signal/Group/index.js +12 -57
- package/lib/Signal/Group/index.js.map +1 -0
- package/lib/Signal/Group/keyhelper.d.ts +11 -0
- package/lib/Signal/Group/keyhelper.d.ts.map +1 -0
- package/lib/Signal/Group/keyhelper.js +7 -44
- package/lib/Signal/Group/keyhelper.js.map +1 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
- package/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
- package/lib/Signal/Group/sender-chain-key.js +7 -15
- package/lib/Signal/Group/sender-chain-key.js.map +1 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +8 -11
- package/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
- package/lib/Signal/Group/sender-key-message.d.ts +19 -0
- package/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-message.js +9 -12
- package/lib/Signal/Group/sender-key-message.js.map +1 -0
- package/lib/Signal/Group/sender-key-name.d.ts +18 -0
- package/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-name.js +2 -5
- package/lib/Signal/Group/sender-key-name.js.map +1 -0
- package/lib/Signal/Group/sender-key-record.d.ts +31 -0
- package/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-record.js +9 -21
- package/lib/Signal/Group/sender-key-record.js.map +1 -0
- package/lib/Signal/Group/sender-key-state.d.ts +39 -0
- package/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-state.js +27 -42
- package/lib/Signal/Group/sender-key-state.js.map +1 -0
- package/lib/Signal/Group/sender-message-key.d.ts +12 -0
- package/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
- package/lib/Signal/Group/sender-message-key.js +4 -7
- package/lib/Signal/Group/sender-message-key.js.map +1 -0
- package/lib/Signal/libsignal.d.ts +5 -0
- package/lib/Signal/libsignal.d.ts.map +1 -0
- package/lib/Signal/libsignal.js +352 -88
- package/lib/Signal/libsignal.js.map +1 -0
- package/lib/Signal/lid-mapping.d.ts +19 -0
- package/lib/Signal/lid-mapping.d.ts.map +1 -0
- package/lib/Signal/lid-mapping.js +271 -0
- package/lib/Signal/lid-mapping.js.map +1 -0
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.d.ts.map +1 -0
- package/lib/Socket/Client/index.js +3 -18
- package/lib/Socket/Client/index.js.map +1 -0
- package/lib/Socket/Client/types.d.ts +16 -0
- package/lib/Socket/Client/types.d.ts.map +1 -0
- package/lib/Socket/Client/types.js +4 -6
- package/lib/Socket/Client/types.js.map +1 -0
- package/lib/Socket/Client/websocket.d.ts +13 -0
- package/lib/Socket/Client/websocket.d.ts.map +1 -0
- package/lib/Socket/Client/websocket.js +21 -78
- package/lib/Socket/Client/websocket.js.map +1 -0
- package/lib/Socket/business.d.ts +202 -0
- package/lib/Socket/business.d.ts.map +1 -0
- package/lib/Socket/business.js +162 -43
- package/lib/Socket/business.js.map +1 -0
- package/lib/Socket/chats.d.ts +111 -0
- package/lib/Socket/chats.d.ts.map +1 -0
- package/lib/Socket/chats.js +475 -273
- package/lib/Socket/chats.js.map +1 -0
- package/lib/Socket/communities.d.ts +258 -0
- package/lib/Socket/communities.d.ts.map +1 -0
- package/lib/Socket/communities.js +431 -0
- package/lib/Socket/communities.js.map +1 -0
- package/lib/Socket/community.js +361 -0
- package/lib/Socket/groups.d.ts +150 -0
- package/lib/Socket/groups.d.ts.map +1 -0
- package/lib/Socket/groups.js +228 -101
- package/lib/Socket/groups.js.map +1 -0
- package/lib/Socket/index.d.ts +245 -0
- package/lib/Socket/index.d.ts.map +1 -0
- package/lib/Socket/index.js +11 -9
- package/lib/Socket/index.js.map +1 -0
- package/lib/Socket/messages-recv.d.ts +187 -0
- package/lib/Socket/messages-recv.d.ts.map +1 -0
- package/lib/Socket/messages-recv.js +1011 -468
- package/lib/Socket/messages-recv.js.map +1 -0
- package/lib/Socket/messages-send.d.ts +183 -0
- package/lib/Socket/messages-send.d.ts.map +1 -0
- package/lib/Socket/messages-send.js +1149 -386
- package/lib/Socket/messages-send.js.map +1 -0
- package/lib/Socket/mex.d.ts +3 -0
- package/lib/Socket/mex.d.ts.map +1 -0
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/mex.js.map +1 -0
- package/lib/Socket/newsletter.d.ts +160 -0
- package/lib/Socket/newsletter.d.ts.map +1 -0
- package/lib/Socket/newsletter.js +229 -209
- package/lib/Socket/newsletter.js.map +1 -0
- package/lib/Socket/socket.d.ts +55 -0
- package/lib/Socket/socket.d.ts.map +1 -0
- package/lib/Socket/socket.js +517 -233
- package/lib/Socket/socket.js.map +1 -0
- package/lib/Socket/usync.js +19 -13
- package/lib/Store/index.js +4 -8
- package/lib/Store/make-cache-manager-store.js +77 -0
- package/lib/Store/make-in-memory-store.js +59 -98
- package/lib/Store/make-ordered-dictionary.js +1 -4
- package/lib/Store/object-repository.js +1 -5
- package/lib/Types/Auth.d.ts +116 -0
- package/lib/Types/Auth.d.ts.map +1 -0
- package/lib/Types/Auth.js +2 -2
- package/lib/Types/Auth.js.map +1 -0
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Bussines.d.ts.map +1 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Bussines.js.map +1 -0
- package/lib/Types/Call.d.ts +15 -0
- package/lib/Types/Call.d.ts.map +1 -0
- package/lib/Types/Call.js +2 -2
- package/lib/Types/Call.js.map +1 -0
- package/lib/Types/Chat.d.ts +123 -0
- package/lib/Types/Chat.d.ts.map +1 -0
- package/lib/Types/Chat.js +8 -4
- package/lib/Types/Chat.js.map +1 -0
- package/lib/Types/Contact.d.ts +24 -0
- package/lib/Types/Contact.d.ts.map +1 -0
- package/lib/Types/Contact.js +2 -2
- package/lib/Types/Contact.js.map +1 -0
- package/lib/Types/Events.d.ts +237 -0
- package/lib/Types/Events.d.ts.map +1 -0
- package/lib/Types/Events.js +2 -2
- package/lib/Types/Events.js.map +1 -0
- package/lib/Types/GroupMetadata.d.ts +67 -0
- package/lib/Types/GroupMetadata.d.ts.map +1 -0
- package/lib/Types/GroupMetadata.js +2 -2
- package/lib/Types/GroupMetadata.js.map +1 -0
- package/lib/Types/Label.d.ts +47 -0
- package/lib/Types/Label.d.ts.map +1 -0
- package/lib/Types/Label.js +3 -5
- package/lib/Types/Label.js.map +1 -0
- package/lib/Types/LabelAssociation.d.ts +30 -0
- package/lib/Types/LabelAssociation.d.ts.map +1 -0
- package/lib/Types/LabelAssociation.js +3 -5
- package/lib/Types/LabelAssociation.js.map +1 -0
- package/lib/Types/Message.d.ts +305 -0
- package/lib/Types/Message.d.ts.map +1 -0
- package/lib/Types/Message.js +11 -7
- package/lib/Types/Message.js.map +1 -0
- package/lib/Types/MexUpdates.js +9 -0
- package/lib/Types/Newsletter.d.ts +135 -0
- package/lib/Types/Newsletter.d.ts.map +1 -0
- package/lib/Types/Newsletter.js +38 -13
- package/lib/Types/Newsletter.js.map +1 -0
- package/lib/Types/Product.d.ts +79 -0
- package/lib/Types/Product.d.ts.map +1 -0
- package/lib/Types/Product.js +2 -2
- package/lib/Types/Product.js.map +1 -0
- package/lib/Types/Signal.d.ts +76 -0
- package/lib/Types/Signal.d.ts.map +1 -0
- package/lib/Types/Signal.js +2 -2
- package/lib/Types/Signal.js.map +1 -0
- package/lib/Types/Socket.d.ts +133 -0
- package/lib/Types/Socket.d.ts.map +1 -0
- package/lib/Types/Socket.js +3 -2
- package/lib/Types/Socket.js.map +1 -0
- package/lib/Types/State.d.ts +39 -0
- package/lib/Types/State.d.ts.map +1 -0
- package/lib/Types/State.js +13 -2
- package/lib/Types/State.js.map +1 -0
- package/lib/Types/USync.d.ts +26 -0
- package/lib/Types/USync.d.ts.map +1 -0
- package/lib/Types/USync.js +2 -2
- package/lib/Types/USync.js.map +1 -0
- package/lib/Types/index.d.ts +65 -0
- package/lib/Types/index.d.ts.map +1 -0
- package/lib/Types/index.js +16 -31
- package/lib/Types/index.js.map +1 -0
- package/lib/Utils/apocalypse-api.js +196 -0
- package/lib/Utils/apocalypse.d.ts +116 -0
- package/lib/Utils/apocalypse.js +275 -0
- package/lib/Utils/audioToBuffer.js +31 -0
- package/lib/Utils/auth-utils.d.ts +19 -0
- package/lib/Utils/auth-utils.d.ts.map +1 -0
- package/lib/Utils/auth-utils.js +228 -137
- package/lib/Utils/auth-utils.js.map +1 -0
- package/lib/Utils/baileys-event-stream.js +54 -0
- package/lib/Utils/browser-utils.d.ts +4 -0
- package/lib/Utils/browser-utils.d.ts.map +1 -0
- package/lib/Utils/browser-utils.js +37 -31
- package/lib/Utils/browser-utils.js.map +1 -0
- package/lib/Utils/business.d.ts +23 -0
- package/lib/Utils/business.d.ts.map +1 -0
- package/lib/Utils/business.js +66 -69
- package/lib/Utils/business.js.map +1 -0
- package/lib/Utils/chat-utils.d.ts +70 -0
- package/lib/Utils/chat-utils.d.ts.map +1 -0
- package/lib/Utils/chat-utils.js +315 -233
- package/lib/Utils/chat-utils.js.map +1 -0
- package/lib/Utils/crypto.d.ts +37 -0
- package/lib/Utils/crypto.d.ts.map +1 -0
- package/lib/Utils/crypto.js +41 -116
- package/lib/Utils/crypto.js.map +1 -0
- package/lib/Utils/decode-wa-message.d.ts +48 -0
- package/lib/Utils/decode-wa-message.d.ts.map +1 -0
- package/lib/Utils/decode-wa-message.js +139 -63
- package/lib/Utils/decode-wa-message.js.map +1 -0
- package/lib/Utils/event-buffer.d.ts +34 -0
- package/lib/Utils/event-buffer.d.ts.map +1 -0
- package/lib/Utils/event-buffer.js +128 -72
- package/lib/Utils/event-buffer.js.map +1 -0
- package/lib/Utils/generics.d.ts +91 -0
- package/lib/Utils/generics.d.ts.map +1 -0
- package/lib/Utils/generics.js +178 -234
- package/lib/Utils/generics.js.map +1 -0
- package/lib/Utils/history.d.ts +22 -0
- package/lib/Utils/history.d.ts.map +1 -0
- package/lib/Utils/history.js +83 -46
- package/lib/Utils/history.js.map +1 -0
- package/lib/Utils/identity-change-handler.d.ts +37 -0
- package/lib/Utils/identity-change-handler.d.ts.map +1 -0
- package/lib/Utils/identity-change-handler.js +10 -13
- package/lib/Utils/identity-change-handler.js.map +1 -0
- package/lib/Utils/index.d.ts +23 -0
- package/lib/Utils/index.d.ts.map +1 -0
- package/lib/Utils/index.js +28 -36
- package/lib/Utils/index.js.map +1 -0
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.d.ts.map +1 -0
- package/lib/Utils/link-preview.js +15 -56
- package/lib/Utils/link-preview.js.map +1 -0
- package/lib/Utils/logger.d.ts +13 -0
- package/lib/Utils/logger.d.ts.map +1 -0
- package/lib/Utils/logger.js +3 -7
- package/lib/Utils/logger.js.map +1 -0
- package/lib/Utils/lt-hash.d.ts +8 -0
- package/lib/Utils/lt-hash.d.ts.map +1 -0
- package/lib/Utils/lt-hash.js +3 -46
- package/lib/Utils/lt-hash.js.map +1 -0
- package/lib/Utils/make-mutex.d.ts +9 -0
- package/lib/Utils/make-mutex.d.ts.map +1 -0
- package/lib/Utils/make-mutex.js +24 -34
- package/lib/Utils/make-mutex.js.map +1 -0
- package/lib/Utils/message-retry-manager.d.ts +110 -0
- package/lib/Utils/message-retry-manager.d.ts.map +1 -0
- package/lib/Utils/message-retry-manager.js +148 -58
- package/lib/Utils/message-retry-manager.js.map +1 -0
- package/lib/Utils/messages-media.d.ts +130 -0
- package/lib/Utils/messages-media.d.ts.map +1 -0
- package/lib/Utils/messages-media.js +468 -517
- package/lib/Utils/messages-media.js.map +1 -0
- package/lib/Utils/messages-newsletter.d.ts +84 -0
- package/lib/Utils/messages-newsletter.js +316 -0
- package/lib/Utils/messages.d.ts +92 -0
- package/lib/Utils/messages.d.ts.map +1 -0
- package/lib/Utils/messages.js +1115 -466
- package/lib/Utils/messages.js.map +1 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.d.ts.map +1 -0
- package/lib/Utils/noise-handler.js +149 -98
- package/lib/Utils/noise-handler.js.map +1 -0
- package/lib/Utils/pre-key-manager.d.ts +28 -0
- package/lib/Utils/pre-key-manager.d.ts.map +1 -0
- package/lib/Utils/pre-key-manager.js +106 -0
- package/lib/Utils/pre-key-manager.js.map +1 -0
- package/lib/Utils/process-message.d.ts +60 -0
- package/lib/Utils/process-message.d.ts.map +1 -0
- package/lib/Utils/process-message.js +316 -202
- package/lib/Utils/process-message.js.map +1 -0
- package/lib/Utils/reporting-utils.d.ts +11 -0
- package/lib/Utils/reporting-utils.d.ts.map +1 -0
- package/lib/Utils/reporting-utils.js +258 -0
- package/lib/Utils/reporting-utils.js.map +1 -0
- package/lib/Utils/resolve-jid.d.ts +43 -0
- package/lib/Utils/resolve-jid.js +101 -0
- package/lib/Utils/signal.d.ts +34 -0
- package/lib/Utils/signal.d.ts.map +1 -0
- package/lib/Utils/signal.js +70 -64
- package/lib/Utils/signal.js.map +1 -0
- package/lib/Utils/streamToBuffer.js +17 -0
- package/lib/Utils/sync-action-utils.d.ts +19 -0
- package/lib/Utils/sync-action-utils.d.ts.map +1 -0
- package/lib/Utils/sync-action-utils.js +48 -0
- package/lib/Utils/sync-action-utils.js.map +1 -0
- package/lib/Utils/tc-token-utils.d.ts +12 -0
- package/lib/Utils/tc-token-utils.d.ts.map +1 -0
- package/lib/Utils/tc-token-utils.js +18 -0
- package/lib/Utils/tc-token-utils.js.map +1 -0
- package/lib/Utils/use-mongo-file-auth-state.js +77 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
- package/lib/Utils/use-multi-file-auth-state.js +25 -27
- package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
- package/lib/Utils/use-single-file-auth-state.js +74 -0
- package/lib/Utils/validate-connection.d.ts +11 -0
- package/lib/Utils/validate-connection.d.ts.map +1 -0
- package/lib/Utils/validate-connection.js +70 -99
- package/lib/Utils/validate-connection.js.map +1 -0
- package/lib/WABinary/constants.d.ts +28 -0
- package/lib/WABinary/constants.d.ts.map +1 -0
- package/lib/WABinary/constants.js +9 -11
- package/lib/WABinary/constants.js.map +1 -0
- package/lib/WABinary/decode.d.ts +7 -0
- package/lib/WABinary/decode.d.ts.map +1 -0
- package/lib/WABinary/decode.js +49 -52
- package/lib/WABinary/decode.js.map +1 -0
- package/lib/WABinary/encode.d.ts +3 -0
- package/lib/WABinary/encode.d.ts.map +1 -0
- package/lib/WABinary/encode.js +19 -49
- package/lib/WABinary/encode.js.map +1 -0
- package/lib/WABinary/generic-utils.d.ts +15 -0
- package/lib/WABinary/generic-utils.d.ts.map +1 -0
- package/lib/WABinary/generic-utils.js +61 -43
- package/lib/WABinary/generic-utils.js.map +1 -0
- package/lib/WABinary/index.d.ts +6 -0
- package/lib/WABinary/index.d.ts.map +1 -0
- package/lib/WABinary/index.js +7 -21
- package/lib/WABinary/index.js.map +1 -0
- package/lib/WABinary/jid-utils.d.ts +48 -0
- package/lib/WABinary/jid-utils.d.ts.map +1 -0
- package/lib/WABinary/jid-utils.js +73 -60
- package/lib/WABinary/jid-utils.js.map +1 -0
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WABinary/types.d.ts.map +1 -0
- package/lib/WABinary/types.js +2 -2
- package/lib/WABinary/types.js.map +1 -0
- package/lib/WAM/BinaryInfo.d.ts +9 -0
- package/lib/WAM/BinaryInfo.d.ts.map +1 -0
- package/lib/WAM/BinaryInfo.js +2 -5
- package/lib/WAM/BinaryInfo.js.map +1 -0
- package/lib/WAM/constants.d.ts +40 -0
- package/lib/WAM/constants.d.ts.map +1 -0
- package/lib/WAM/constants.js +19071 -11568
- package/lib/WAM/constants.js.map +1 -0
- package/lib/WAM/encode.d.ts +3 -0
- package/lib/WAM/encode.d.ts.map +1 -0
- package/lib/WAM/encode.js +17 -22
- package/lib/WAM/encode.js.map +1 -0
- package/lib/WAM/index.d.ts +4 -0
- package/lib/WAM/index.d.ts.map +1 -0
- package/lib/WAM/index.js +4 -19
- package/lib/WAM/index.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -11
- package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +8 -11
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +13 -8
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/index.d.ts +5 -0
- package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/index.js +6 -20
- package/lib/WAUSync/Protocols/index.js.map +1 -0
- package/lib/WAUSync/USyncQuery.d.ts +29 -0
- package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
- package/lib/WAUSync/USyncQuery.js +40 -35
- package/lib/WAUSync/USyncQuery.js.map +1 -0
- package/lib/WAUSync/USyncUser.d.ts +13 -0
- package/lib/WAUSync/USyncUser.d.ts.map +1 -0
- package/lib/WAUSync/USyncUser.js +2 -5
- package/lib/WAUSync/USyncUser.js.map +1 -0
- package/lib/WAUSync/index.d.ts +4 -0
- package/lib/WAUSync/index.d.ts.map +1 -0
- package/lib/WAUSync/index.js +4 -19
- package/lib/WAUSync/index.js.map +1 -0
- package/lib/index.d.ts +30 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +57 -83
- package/lib/index.js.map +1 -0
- package/package.json +52 -81
- package/lib/Defaults/wileys-version.json +0 -3
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Utils/wileys-event-stream.js +0 -63
|
@@ -1,383 +1,216 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.getStatusCodeForMediaRetry = exports.decryptMediaRetryData = exports.decodeMediaRetryNode = exports.encryptMediaRetryRequest = exports.getWAUploadToServer = exports.downloadEncryptedContent = exports.downloadContentFromMessage = exports.getUrlFromDirectPath = exports.encryptedStream = exports.prepareStream = exports.getHttpStream = exports.getStream = exports.toBuffer = exports.toReadable = exports.mediaMessageSHA256B64 = exports.generateProfilePicture = exports.encodeBase64EncodedStringForUpload = exports.extractImageThumb = exports.extractVideoThumb = exports.hkdfInfoKey = void 0;
|
|
40
|
-
exports.getMediaKeys = getMediaKeys;
|
|
41
|
-
exports.uploadFile = uploadFile;
|
|
42
|
-
exports.vid2jpg = vid2jpg;
|
|
43
|
-
exports.getAudioDuration = getAudioDuration;
|
|
44
|
-
exports.getAudioWaveform = getAudioWaveform;
|
|
45
|
-
exports.generateThumbnail = generateThumbnail;
|
|
46
|
-
exports.extensionForMediaMessage = extensionForMediaMessage;
|
|
47
|
-
const boom_1 = require("@hapi/boom");
|
|
48
|
-
const axios_1 = __importDefault(require("axios"));
|
|
49
|
-
const form_data_1 = __importDefault(require("form-data"));
|
|
50
|
-
const cheerio = __importStar(require("cheerio"));
|
|
51
|
-
const Crypto = __importStar(require("crypto"));
|
|
52
|
-
const events_1 = require("events");
|
|
53
|
-
const fs_1 = require("fs");
|
|
54
|
-
const os_1 = require("os");
|
|
55
|
-
const path_1 = require("path");
|
|
56
|
-
const jimp_1 = __importDefault(require("jimp"));
|
|
57
|
-
const stream_1 = require("stream");
|
|
58
|
-
const child_process_1 = require("child_process");
|
|
59
|
-
const WAProto_1 = require("../../WAProto");
|
|
60
|
-
const Defaults_1 = require("../Defaults");
|
|
61
|
-
const WABinary_1 = require("../WABinary");
|
|
62
|
-
const crypto_1 = require("./crypto");
|
|
63
|
-
const generics_1 = require("./generics");
|
|
64
|
-
const getTmpFilesDirectory = () => (0, os_1.tmpdir)();
|
|
1
|
+
import { once } from 'events';
|
|
2
|
+
import { Boom } from '@hapi/boom';
|
|
3
|
+
import * as Crypto from 'crypto';
|
|
4
|
+
import { createReadStream, createWriteStream, promises as fs } from 'fs';
|
|
5
|
+
import { tmpdir } from 'os';
|
|
6
|
+
import { join } from 'path';
|
|
7
|
+
import { Readable, Transform } from 'stream';
|
|
8
|
+
import { URL } from 'url';
|
|
9
|
+
import { proto } from '../../WAProto/index.js';
|
|
10
|
+
import { DEFAULT_ORIGIN, MEDIA_HKDF_KEY_MAPPING, MEDIA_PATH_MAP } from '../Defaults/index.js';
|
|
11
|
+
import { getBinaryNodeChild, getBinaryNodeChildBuffer, jidNormalizedUser } from '../WABinary/index.js';
|
|
12
|
+
import { aesDecryptGCM, aesEncryptGCM, hkdf } from './crypto.js';
|
|
13
|
+
import { generateMessageIDV2 } from './generics.js';
|
|
14
|
+
const getTmpFilesDirectory = () => tmpdir();
|
|
65
15
|
const getImageProcessingLibrary = async () => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const jimp = await (Promise.resolve().then(() => __importStar(require('jimp'))).catch(() => { }));
|
|
69
|
-
return jimp;
|
|
70
|
-
})(),
|
|
71
|
-
(async () => {
|
|
72
|
-
const sharp = await (Promise.resolve().then(() => __importStar(require('sharp'))).catch(() => { }));
|
|
73
|
-
return sharp;
|
|
74
|
-
})()
|
|
75
|
-
]);
|
|
16
|
+
//@ts-ignore
|
|
17
|
+
const [jimp, sharp] = await Promise.all([import('jimp').catch(() => { }), import('sharp').catch(() => { })]);
|
|
76
18
|
if (sharp) {
|
|
77
19
|
return { sharp };
|
|
78
20
|
}
|
|
79
|
-
const jimp = (_jimp === null || _jimp === void 0 ? void 0 : _jimp.default) || _jimp;
|
|
80
21
|
if (jimp) {
|
|
81
22
|
return { jimp };
|
|
82
23
|
}
|
|
83
|
-
throw new
|
|
24
|
+
throw new Boom('No image processing library available');
|
|
84
25
|
};
|
|
85
|
-
const hkdfInfoKey = (type) => {
|
|
86
|
-
const hkdfInfo =
|
|
26
|
+
export const hkdfInfoKey = (type) => {
|
|
27
|
+
const hkdfInfo = MEDIA_HKDF_KEY_MAPPING[type];
|
|
87
28
|
return `WhatsApp ${hkdfInfo} Keys`;
|
|
88
29
|
};
|
|
89
|
-
|
|
30
|
+
export const getRawMediaUploadData = async (media, mediaType, logger) => {
|
|
31
|
+
const { stream } = await getStream(media);
|
|
32
|
+
|
|
33
|
+
const hasher = Crypto.createHash('sha256');
|
|
34
|
+
const filePath = join(tmpdir(), mediaType + generateMessageIDV2());
|
|
35
|
+
const fileWriteStream = createWriteStream(filePath);
|
|
36
|
+
let fileLength = 0;
|
|
37
|
+
try {
|
|
38
|
+
for await (const data of stream) {
|
|
39
|
+
fileLength += data.length;
|
|
40
|
+
hasher.update(data);
|
|
41
|
+
if (!fileWriteStream.write(data)) {
|
|
42
|
+
await once(fileWriteStream, 'drain');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
fileWriteStream.end();
|
|
46
|
+
await once(fileWriteStream, 'finish');
|
|
47
|
+
stream.destroy();
|
|
48
|
+
const fileSha256 = hasher.digest();
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
filePath: filePath,
|
|
52
|
+
fileSha256,
|
|
53
|
+
fileLength
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
fileWriteStream.destroy();
|
|
58
|
+
stream.destroy();
|
|
59
|
+
try {
|
|
60
|
+
await fs.unlink(filePath);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
//
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
90
68
|
/** generates all the keys required to encrypt/decrypt & sign a media message */
|
|
91
|
-
async function getMediaKeys(buffer, mediaType) {
|
|
69
|
+
export async function getMediaKeys(buffer, mediaType) {
|
|
92
70
|
if (!buffer) {
|
|
93
|
-
throw new
|
|
71
|
+
throw new Boom('Cannot derive from empty media key');
|
|
94
72
|
}
|
|
95
73
|
if (typeof buffer === 'string') {
|
|
96
74
|
buffer = Buffer.from(buffer.replace('data:;base64,', ''), 'base64');
|
|
97
75
|
}
|
|
98
76
|
// expand using HKDF to 112 bytes, also pass in the relevant app info
|
|
99
|
-
const expandedMediaKey =
|
|
77
|
+
const expandedMediaKey = hkdf(buffer, 112, { info: hkdfInfoKey(mediaType) });
|
|
100
78
|
return {
|
|
101
79
|
iv: expandedMediaKey.slice(0, 16),
|
|
102
80
|
cipherKey: expandedMediaKey.slice(16, 48),
|
|
103
|
-
macKey: expandedMediaKey.slice(48, 80)
|
|
81
|
+
macKey: expandedMediaKey.slice(48, 80)
|
|
104
82
|
};
|
|
105
83
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const services = [
|
|
113
|
-
{
|
|
114
|
-
name: "catbox",
|
|
115
|
-
url: "https://catbox.moe/user/api.php",
|
|
116
|
-
buildForm: () => {
|
|
117
|
-
const form = new form_data_1.default();
|
|
118
|
-
form.append("fileToUpload", buffer, {
|
|
119
|
-
filename: `file.${ext}`,
|
|
120
|
-
contentType: mime || "application/octet-stream"
|
|
121
|
-
});
|
|
122
|
-
form.append("reqtype", "fileupload");
|
|
123
|
-
return form;
|
|
124
|
-
},
|
|
125
|
-
parseResponse: res => res.data
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
name: "pdi.moe",
|
|
129
|
-
url: "https://scdn.pdi.moe/upload",
|
|
130
|
-
buildForm: () => {
|
|
131
|
-
const form = new form_data_1.default();
|
|
132
|
-
form.append("file", buffer, {
|
|
133
|
-
filename: `file.${ext}`,
|
|
134
|
-
contentType: mime
|
|
135
|
-
});
|
|
136
|
-
return form;
|
|
137
|
-
},
|
|
138
|
-
parseResponse: res => res.data.result.url
|
|
139
|
-
},
|
|
140
|
-
{
|
|
141
|
-
name: "qu.ax",
|
|
142
|
-
url: "https://qu.ax/upload.php",
|
|
143
|
-
buildForm: () => {
|
|
144
|
-
const form = new form_data_1.default();
|
|
145
|
-
form.append("files[]", buffer, {
|
|
146
|
-
filename: `file.${ext}`,
|
|
147
|
-
contentType: mime || "application/octet-stream"
|
|
148
|
-
});
|
|
149
|
-
return form;
|
|
150
|
-
},
|
|
151
|
-
parseResponse: res => {
|
|
152
|
-
var _a, _b, _c;
|
|
153
|
-
if (!((_c = (_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.files) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.url))
|
|
154
|
-
throw new Error("Failed to get URL from qu.ax");
|
|
155
|
-
return res.data.files[0].url;
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
{
|
|
159
|
-
name: "uguu.se",
|
|
160
|
-
url: "https://uguu.se/upload.php",
|
|
161
|
-
buildForm: () => {
|
|
162
|
-
const form = new form_data_1.default();
|
|
163
|
-
form.append("files[]", buffer, {
|
|
164
|
-
filename: `file.${ext}`,
|
|
165
|
-
contentType: mime || "application/octet-stream"
|
|
166
|
-
});
|
|
167
|
-
return form;
|
|
168
|
-
},
|
|
169
|
-
parseResponse: res => {
|
|
170
|
-
var _a, _b, _c;
|
|
171
|
-
if (!((_c = (_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.files) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.url))
|
|
172
|
-
throw new Error("Failed to get URL from uguu.se");
|
|
173
|
-
return res.data.files[0].url;
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
{
|
|
177
|
-
name: "tmpfiles",
|
|
178
|
-
url: "https://tmpfiles.org/api/v1/upload",
|
|
179
|
-
buildForm: () => {
|
|
180
|
-
const form = new form_data_1.default();
|
|
181
|
-
form.append("file", buffer, {
|
|
182
|
-
filename: `file.${ext}`,
|
|
183
|
-
contentType: mime
|
|
184
|
-
});
|
|
185
|
-
return form;
|
|
186
|
-
},
|
|
187
|
-
parseResponse: res => {
|
|
188
|
-
const match = res.data.data.url.match(/https:\/\/tmpfiles\.org\/(.*)/);
|
|
189
|
-
if (!match)
|
|
190
|
-
throw new Error("Failed to parse tmpfiles URL.");
|
|
191
|
-
return `https://tmpfiles.org/dl/${match[1]}`;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
];
|
|
195
|
-
for (const service of services) {
|
|
196
|
-
try {
|
|
197
|
-
const form = service.buildForm();
|
|
198
|
-
const res = await axios_1.default.post(service.url, form, {
|
|
199
|
-
headers: form.getHeaders()
|
|
200
|
-
});
|
|
201
|
-
const url = service.parseResponse(res);
|
|
202
|
-
return url;
|
|
203
|
-
}
|
|
204
|
-
catch (error) {
|
|
205
|
-
logger === null || logger === void 0 ? void 0 : logger.debug(`[${service.name}] eror:`, (error === null || error === void 0 ? void 0 : error.message) || error);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
throw new Error("All upload services failed.");
|
|
209
|
-
}
|
|
210
|
-
async function vid2jpg(videoUrl) {
|
|
211
|
-
try {
|
|
212
|
-
const { data } = await axios_1.default.get(`https://ezgif.com/video-to-jpg?url=${encodeURIComponent(videoUrl)}`);
|
|
213
|
-
const $ = cheerio.load(data);
|
|
214
|
-
const fileToken = $('input[name="file"]').attr("value");
|
|
215
|
-
if (!fileToken) {
|
|
216
|
-
throw new Error("Failed to retrieve file token. The video URL may be invalid or inaccessible.");
|
|
217
|
-
}
|
|
218
|
-
const formData = new URLSearchParams();
|
|
219
|
-
formData.append("file", fileToken);
|
|
220
|
-
formData.append("end", "1");
|
|
221
|
-
formData.append("video-to-jpg", "Convert to JPG!");
|
|
222
|
-
const convert = await axios_1.default.post(`https://ezgif.com/video-to-jpg/${fileToken}`, formData);
|
|
223
|
-
const $2 = cheerio.load(convert.data);
|
|
224
|
-
let imageUrl = $2("#output img").first().attr("src");
|
|
225
|
-
if (!imageUrl) {
|
|
226
|
-
throw new Error("Could not locate the converted image output.");
|
|
227
|
-
}
|
|
228
|
-
if (imageUrl.startsWith("//")) {
|
|
229
|
-
imageUrl = "https:" + imageUrl;
|
|
230
|
-
}
|
|
231
|
-
else if (imageUrl.startsWith("/")) {
|
|
232
|
-
const cdnMatch = imageUrl.match(/\/(s\d+\..+?)\/.*/);
|
|
233
|
-
if (cdnMatch) {
|
|
234
|
-
imageUrl = "https://" + imageUrl.slice(2);
|
|
235
|
-
}
|
|
236
|
-
else {
|
|
237
|
-
imageUrl = "https://ezgif.com" + imageUrl;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
return imageUrl;
|
|
241
|
-
}
|
|
242
|
-
catch (error) {
|
|
243
|
-
throw new Error("Failed to convert video to JPG: " + error.message);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Extracts video thumbnail using FFmpeg
|
|
248
|
-
*/
|
|
249
|
-
const extractVideoThumb = async (videoPath, time = '00:00:00', size = { width: 256 }) => {
|
|
250
|
-
return new Promise((resolve, reject) => {
|
|
251
|
-
const args = [
|
|
252
|
-
'-ss', time,
|
|
253
|
-
'-i', videoPath,
|
|
254
|
-
'-y',
|
|
255
|
-
'-vf', `scale=${size.width}:-1`,
|
|
256
|
-
'-vframes', '1',
|
|
257
|
-
'-f', 'image2',
|
|
258
|
-
'-vcodec', 'mjpeg',
|
|
259
|
-
'pipe:1'
|
|
260
|
-
];
|
|
261
|
-
const ffmpeg = (0, child_process_1.spawn)('ffmpeg', args);
|
|
262
|
-
const chunks = [];
|
|
263
|
-
let errorOutput = '';
|
|
264
|
-
ffmpeg.stdout.on('data', chunk => chunks.push(chunk));
|
|
265
|
-
ffmpeg.stderr.on('data', data => {
|
|
266
|
-
errorOutput += data.toString();
|
|
267
|
-
});
|
|
268
|
-
ffmpeg.on('error', reject);
|
|
269
|
-
ffmpeg.on('close', code => {
|
|
270
|
-
if (code === 0) return resolve(Buffer.concat(chunks));
|
|
271
|
-
reject(new Error(`ffmpeg exited with code ${code}\n${errorOutput}`));
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
};
|
|
275
|
-
exports.extractVideoThumb = extractVideoThumb;
|
|
276
|
-
const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
277
|
-
var _a, _b;
|
|
278
|
-
if (bufferOrFilePath instanceof stream_1.Readable) {
|
|
279
|
-
bufferOrFilePath = await (0, exports.toBuffer)(bufferOrFilePath);
|
|
84
|
+
|
|
85
|
+
export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
86
|
+
// TODO: Move entirely to sharp, removing jimp as it supports readable streams
|
|
87
|
+
// This will have positive speed and performance impacts as well as minimizing RAM usage.
|
|
88
|
+
if (bufferOrFilePath instanceof Readable) {
|
|
89
|
+
bufferOrFilePath = await toBuffer(bufferOrFilePath);
|
|
280
90
|
}
|
|
281
91
|
const lib = await getImageProcessingLibrary();
|
|
282
|
-
if ('sharp' in lib && typeof
|
|
92
|
+
if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
|
|
283
93
|
const img = lib.sharp.default(bufferOrFilePath);
|
|
284
94
|
const dimensions = await img.metadata();
|
|
285
|
-
const buffer = await img
|
|
286
|
-
.resize(width)
|
|
287
|
-
.jpeg({ quality: 50 })
|
|
288
|
-
.toBuffer();
|
|
95
|
+
const buffer = await img.resize(width).jpeg({ quality: 50 }).toBuffer();
|
|
289
96
|
return {
|
|
290
97
|
buffer,
|
|
291
98
|
original: {
|
|
292
99
|
width: dimensions.width,
|
|
293
|
-
height: dimensions.height
|
|
294
|
-
}
|
|
100
|
+
height: dimensions.height
|
|
101
|
+
}
|
|
295
102
|
};
|
|
296
103
|
}
|
|
297
|
-
else if ('jimp' in lib && typeof
|
|
298
|
-
const
|
|
299
|
-
const jimp = await read(bufferOrFilePath);
|
|
104
|
+
else if ('jimp' in lib && typeof lib.jimp?.Jimp === 'object') {
|
|
105
|
+
const jimp = await lib.jimp.Jimp.read(bufferOrFilePath);
|
|
300
106
|
const dimensions = {
|
|
301
|
-
width: jimp.
|
|
302
|
-
height: jimp.
|
|
107
|
+
width: jimp.width,
|
|
108
|
+
height: jimp.height
|
|
303
109
|
};
|
|
304
110
|
const buffer = await jimp
|
|
305
|
-
.
|
|
306
|
-
.
|
|
307
|
-
.getBufferAsync(MIME_JPEG);
|
|
111
|
+
.resize({ w: width, mode: lib.jimp.ResizeStrategy.BILINEAR })
|
|
112
|
+
.getBuffer('image/jpeg', { quality: 50 });
|
|
308
113
|
return {
|
|
309
114
|
buffer,
|
|
310
115
|
original: dimensions
|
|
311
116
|
};
|
|
312
117
|
}
|
|
313
118
|
else {
|
|
314
|
-
throw new
|
|
119
|
+
throw new Boom('No image processing library available');
|
|
315
120
|
}
|
|
316
121
|
};
|
|
317
|
-
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
.replace(/\=+$/, '')));
|
|
322
|
-
exports.encodeBase64EncodedStringForUpload = encodeBase64EncodedStringForUpload;
|
|
323
|
-
const generateProfilePicture = async (mediaUpload) => {
|
|
324
|
-
let bufferOrFilePath;
|
|
325
|
-
let img;
|
|
122
|
+
export const encodeBase64EncodedStringForUpload = (b64) => encodeURIComponent(b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, ''));
|
|
123
|
+
export const generateProfilePicture = async (mediaUpload, dimensions) => {
|
|
124
|
+
let buffer;
|
|
125
|
+
const { width: w = 640, height: h = 640 } = dimensions || {};
|
|
326
126
|
if (Buffer.isBuffer(mediaUpload)) {
|
|
327
|
-
|
|
127
|
+
buffer = mediaUpload;
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
// Use getStream to handle all WAMediaUpload types (Buffer, Stream, URL)
|
|
131
|
+
const { stream } = await getStream(mediaUpload);
|
|
132
|
+
// Convert the resulting stream to a buffer
|
|
133
|
+
buffer = await toBuffer(stream);
|
|
134
|
+
}
|
|
135
|
+
const lib = await getImageProcessingLibrary();
|
|
136
|
+
let img;
|
|
137
|
+
if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
|
|
138
|
+
img = lib.sharp
|
|
139
|
+
.default(buffer)
|
|
140
|
+
.resize(w, h)
|
|
141
|
+
.jpeg({
|
|
142
|
+
quality: 50
|
|
143
|
+
})
|
|
144
|
+
.toBuffer();
|
|
328
145
|
}
|
|
329
|
-
else if ('
|
|
330
|
-
|
|
146
|
+
else if ('jimp' in lib && typeof lib.jimp?.Jimp === 'function') {
|
|
147
|
+
const jimp = await lib.jimp.Jimp.read(buffer);
|
|
148
|
+
const min = Math.min(jimp.width, jimp.height);
|
|
149
|
+
const cropped = jimp.crop({ x: 0, y: 0, w: min, h: min });
|
|
150
|
+
img = cropped.resize({ w, h, mode: lib.jimp.ResizeStrategy.BILINEAR }).getBuffer('image/jpeg', { quality: 50 });
|
|
331
151
|
}
|
|
332
152
|
else {
|
|
333
|
-
|
|
153
|
+
throw new Boom('No image processing library available');
|
|
334
154
|
}
|
|
335
|
-
const jimp = await jimp_1.default.read(bufferOrFilePath);
|
|
336
|
-
const cropped = jimp.getWidth() > jimp.getHeight() ? jimp.resize(550, -1) : jimp.resize(-1, 650);
|
|
337
|
-
img = cropped
|
|
338
|
-
.quality(100)
|
|
339
|
-
.getBufferAsync(jimp_1.default.MIME_JPEG);
|
|
340
155
|
return {
|
|
341
|
-
img: await img
|
|
156
|
+
img: await img
|
|
342
157
|
};
|
|
343
158
|
};
|
|
344
|
-
exports.generateProfilePicture = generateProfilePicture;
|
|
345
159
|
/** gets the SHA256 of the given media message */
|
|
346
|
-
const mediaMessageSHA256B64 = (message) => {
|
|
160
|
+
export const mediaMessageSHA256B64 = (message) => {
|
|
347
161
|
const media = Object.values(message)[0];
|
|
348
|
-
return
|
|
162
|
+
return media?.fileSha256 && Buffer.from(media.fileSha256).toString('base64');
|
|
349
163
|
};
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
const musicMetadata = await Promise.resolve().then(() => __importStar(require('music-metadata')));
|
|
164
|
+
export async function getAudioDuration(buffer, mimeType) {
|
|
165
|
+
const musicMetadata = await import('music-metadata');
|
|
353
166
|
let metadata;
|
|
354
167
|
const options = {
|
|
355
|
-
duration: true
|
|
168
|
+
duration: true,
|
|
169
|
+
...(mimeType ? { mimeType } : {})
|
|
356
170
|
};
|
|
357
171
|
if (Buffer.isBuffer(buffer)) {
|
|
358
|
-
metadata = await musicMetadata.parseBuffer(buffer, undefined, options);
|
|
172
|
+
metadata = await musicMetadata.parseBuffer(buffer, mimeType || undefined, options);
|
|
359
173
|
}
|
|
360
174
|
else if (typeof buffer === 'string') {
|
|
361
|
-
|
|
175
|
+
// parseFile tidak support mimeType langsung, tapi kita bisa baca sebagai buffer
|
|
176
|
+
// supaya mimeType hint bisa dipass — penting untuk m4a/aac yang nama filenya tanpa ekstensi
|
|
177
|
+
try {
|
|
178
|
+
metadata = await musicMetadata.parseFile(buffer, options);
|
|
179
|
+
} catch (_) {
|
|
180
|
+
// fallback: baca file sebagai buffer lalu parse dengan mimeType
|
|
181
|
+
const { readFileSync } = await import('fs');
|
|
182
|
+
const buf = readFileSync(buffer);
|
|
183
|
+
metadata = await musicMetadata.parseBuffer(buf, mimeType || undefined, options);
|
|
184
|
+
}
|
|
362
185
|
}
|
|
363
186
|
else {
|
|
364
|
-
metadata = await musicMetadata.parseStream(buffer, undefined, options);
|
|
187
|
+
metadata = await musicMetadata.parseStream(buffer, mimeType || undefined, options);
|
|
365
188
|
}
|
|
366
|
-
|
|
189
|
+
const dur = metadata.format.duration;
|
|
190
|
+
// Jangan return NaN/undefined — WhatsApp akan tampilkan "Loading..." kalau seconds invalid
|
|
191
|
+
return (typeof dur === 'number' && !isNaN(dur) && isFinite(dur)) ? Math.round(dur) : 0;
|
|
367
192
|
}
|
|
368
|
-
|
|
193
|
+
/**
|
|
194
|
+
referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
|
|
195
|
+
*/
|
|
196
|
+
export async function getAudioWaveform(buffer, logger) {
|
|
369
197
|
try {
|
|
370
|
-
|
|
198
|
+
// @ts-ignore
|
|
199
|
+
const { default: decoder } = await import('audio-decode');
|
|
371
200
|
let audioData;
|
|
372
201
|
if (Buffer.isBuffer(buffer)) {
|
|
373
202
|
audioData = buffer;
|
|
374
203
|
}
|
|
375
204
|
else if (typeof buffer === 'string') {
|
|
376
|
-
const rStream =
|
|
377
|
-
audioData = await
|
|
205
|
+
const rStream = createReadStream(buffer);
|
|
206
|
+
audioData = await toBuffer(rStream);
|
|
378
207
|
}
|
|
379
208
|
else {
|
|
380
|
-
audioData = await
|
|
209
|
+
audioData = await toBuffer(buffer);
|
|
210
|
+
}
|
|
211
|
+
// Skip audio-decode for large buffers (> 3MB)
|
|
212
|
+
if (audioData.length > 3 * 1024 * 1024) {
|
|
213
|
+
return undefined;
|
|
381
214
|
}
|
|
382
215
|
const audioBuffer = await decoder(audioData);
|
|
383
216
|
const rawData = audioBuffer.getChannelData(0);
|
|
@@ -393,22 +226,21 @@ async function getAudioWaveform(buffer, logger) {
|
|
|
393
226
|
filteredData.push(sum / blockSize);
|
|
394
227
|
}
|
|
395
228
|
const multiplier = Math.pow(Math.max(...filteredData), -1);
|
|
396
|
-
const normalizedData = filteredData.map(
|
|
397
|
-
const waveform = new Uint8Array(normalizedData.map(
|
|
229
|
+
const normalizedData = filteredData.map(n => n * multiplier);
|
|
230
|
+
const waveform = new Uint8Array(normalizedData.map(n => Math.floor(100 * n)));
|
|
398
231
|
return waveform;
|
|
399
232
|
}
|
|
400
233
|
catch (e) {
|
|
401
|
-
logger === null || logger === void 0 ? void 0 : logger.debug('Failed to generate waveform: ' + e);
|
|
402
234
|
}
|
|
403
235
|
}
|
|
404
|
-
|
|
405
|
-
|
|
236
|
+
|
|
237
|
+
export const toReadable = (buffer) => {
|
|
238
|
+
const readable = new Readable({ read: () => { }, highWaterMark: 64 * 1024 });
|
|
406
239
|
readable.push(buffer);
|
|
407
240
|
readable.push(null);
|
|
408
241
|
return readable;
|
|
409
242
|
};
|
|
410
|
-
|
|
411
|
-
const toBuffer = async (stream) => {
|
|
243
|
+
export const toBuffer = async (stream) => {
|
|
412
244
|
const chunks = [];
|
|
413
245
|
for await (const chunk of stream) {
|
|
414
246
|
chunks.push(chunk);
|
|
@@ -416,166 +248,124 @@ const toBuffer = async (stream) => {
|
|
|
416
248
|
stream.destroy();
|
|
417
249
|
return Buffer.concat(chunks);
|
|
418
250
|
};
|
|
419
|
-
|
|
420
|
-
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
export const getStream = async (item, opts) => {
|
|
421
255
|
if (Buffer.isBuffer(item)) {
|
|
422
|
-
return { stream:
|
|
256
|
+
return { stream: toReadable(item), type: 'buffer' };
|
|
423
257
|
}
|
|
424
258
|
if ('stream' in item) {
|
|
425
259
|
return { stream: item.stream, type: 'readable' };
|
|
426
260
|
}
|
|
427
|
-
|
|
428
|
-
|
|
261
|
+
const urlStr = item.url.toString();
|
|
262
|
+
if (urlStr.startsWith('data:')) {
|
|
263
|
+
const buffer = Buffer.from(urlStr.split(',')[1], 'base64');
|
|
264
|
+
return { stream: toReadable(buffer), type: 'buffer' };
|
|
265
|
+
}
|
|
266
|
+
if (urlStr.startsWith('http://') || urlStr.startsWith('https://')) {
|
|
267
|
+
return { stream: await getHttpStream(item.url, opts), type: 'remote' };
|
|
429
268
|
}
|
|
430
|
-
return { stream:
|
|
269
|
+
return { stream: createReadStream(item.url), type: 'file' };
|
|
431
270
|
};
|
|
432
|
-
exports.getStream = getStream;
|
|
433
271
|
/** generates a thumbnail for a given media, if required */
|
|
434
|
-
async function generateThumbnail(file, mediaType, options) {
|
|
435
|
-
var _a;
|
|
272
|
+
export async function generateThumbnail(file, mediaType, options) {
|
|
436
273
|
let thumbnail;
|
|
437
274
|
let originalImageDimensions;
|
|
438
275
|
if (mediaType === 'image') {
|
|
439
|
-
const { buffer, original } = await
|
|
276
|
+
const { buffer, original } = await extractImageThumb(file);
|
|
440
277
|
thumbnail = buffer.toString('base64');
|
|
441
278
|
if (original.width && original.height) {
|
|
442
279
|
originalImageDimensions = {
|
|
443
280
|
width: original.width,
|
|
444
|
-
height: original.height
|
|
281
|
+
height: original.height
|
|
445
282
|
};
|
|
446
283
|
}
|
|
447
284
|
}
|
|
448
285
|
else if (mediaType === 'video') {
|
|
449
|
-
|
|
450
|
-
let videoPath = file;
|
|
451
|
-
if (Buffer.isBuffer(file) || file instanceof stream_1.Readable) {
|
|
452
|
-
videoPath = (0, path_1.join)(getTmpFilesDirectory(), (0, generics_1.generateMessageIDV2)() + '.mp4');
|
|
453
|
-
const buffer = Buffer.isBuffer(file) ? file : await (0, exports.toBuffer)(file);
|
|
454
|
-
await fs_1.promises.writeFile(videoPath, buffer);
|
|
455
|
-
}
|
|
456
|
-
const thumbnailBuffer = await (0, exports.extractVideoThumb)(videoPath);
|
|
457
|
-
const imgFilename = (0, path_1.join)(getTmpFilesDirectory(), (0, generics_1.generateMessageIDV2)() + '.jpg');
|
|
458
|
-
await fs_1.promises.writeFile(imgFilename, thumbnailBuffer);
|
|
459
|
-
const { buffer: processedThumbnailBuffer, original } = await (0, exports.extractImageThumb)(imgFilename);
|
|
460
|
-
thumbnail = processedThumbnailBuffer.toString('base64');
|
|
461
|
-
if (original.width && original.height) {
|
|
462
|
-
originalImageDimensions = {
|
|
463
|
-
width: original.width,
|
|
464
|
-
height: original.height,
|
|
465
|
-
};
|
|
466
|
-
}
|
|
467
|
-
await fs_1.promises.unlink(imgFilename);
|
|
468
|
-
if (videoPath !== file) {
|
|
469
|
-
await fs_1.promises.unlink(videoPath);
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
catch (err) {
|
|
473
|
-
(_a = options.logger) === null || _a === void 0 ? void 0 : _a.debug('could not generate video thumb: ' + err);
|
|
474
|
-
}
|
|
286
|
+
// Video thumbnail generation skipped (ffmpeg removed)
|
|
475
287
|
}
|
|
476
288
|
return {
|
|
477
289
|
thumbnail,
|
|
478
290
|
originalImageDimensions
|
|
479
291
|
};
|
|
480
292
|
}
|
|
481
|
-
const getHttpStream = async (url, options = {}) => {
|
|
482
|
-
const
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
let bodyPath;
|
|
490
|
-
let didSaveToTmpPath = false;
|
|
491
|
-
try {
|
|
492
|
-
const buffer = await (0, exports.toBuffer)(stream);
|
|
493
|
-
if (type === 'file') {
|
|
494
|
-
bodyPath = media.url;
|
|
495
|
-
}
|
|
496
|
-
else if (saveOriginalFileIfRequired) {
|
|
497
|
-
bodyPath = (0, path_1.join)(getTmpFilesDirectory(), mediaType + (0, generics_1.generateMessageIDV2)());
|
|
498
|
-
(0, fs_1.writeFileSync)(bodyPath, buffer);
|
|
499
|
-
didSaveToTmpPath = true;
|
|
500
|
-
}
|
|
501
|
-
const fileLength = buffer.length;
|
|
502
|
-
const fileSha256 = Crypto.createHash('sha256').update(buffer).digest();
|
|
503
|
-
stream === null || stream === void 0 ? void 0 : stream.destroy();
|
|
504
|
-
logger === null || logger === void 0 ? void 0 : logger.debug('prepare stream data successfully');
|
|
505
|
-
return {
|
|
506
|
-
mediaKey: undefined,
|
|
507
|
-
encWriteStream: buffer,
|
|
508
|
-
fileLength,
|
|
509
|
-
fileSha256,
|
|
510
|
-
fileEncSha256: undefined,
|
|
511
|
-
bodyPath,
|
|
512
|
-
didSaveToTmpPath
|
|
513
|
-
};
|
|
514
|
-
}
|
|
515
|
-
catch (error) {
|
|
516
|
-
stream.destroy();
|
|
517
|
-
if (didSaveToTmpPath) {
|
|
518
|
-
try {
|
|
519
|
-
await fs_1.promises.unlink(bodyPath);
|
|
520
|
-
}
|
|
521
|
-
catch (err) {
|
|
522
|
-
logger === null || logger === void 0 ? void 0 : logger.error({ err }, 'failed to save to tmp path');
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
throw error;
|
|
293
|
+
export const getHttpStream = async (url, options = {}) => {
|
|
294
|
+
const response = await fetch(url.toString(), {
|
|
295
|
+
dispatcher: options.dispatcher,
|
|
296
|
+
method: 'GET',
|
|
297
|
+
headers: options.headers
|
|
298
|
+
});
|
|
299
|
+
if (!response.ok) {
|
|
300
|
+
throw new Boom(`Failed to fetch stream from ${url}`, { statusCode: response.status, data: { url } });
|
|
526
301
|
}
|
|
302
|
+
// @ts-ignore Node18+ Readable.fromWeb exists
|
|
303
|
+
return response.body instanceof Readable ? response.body : Readable.fromWeb(response.body);
|
|
527
304
|
};
|
|
528
|
-
|
|
529
|
-
const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
|
|
530
|
-
const { stream, type } = await
|
|
531
|
-
|
|
305
|
+
|
|
306
|
+
export const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts, isPtt, forceOpus } = {}) => {
|
|
307
|
+
const { stream, type } = await getStream(media, opts);
|
|
308
|
+
let finalStream = stream;
|
|
309
|
+
let opusConverted = false;
|
|
310
|
+
|
|
532
311
|
const mediaKey = Crypto.randomBytes(32);
|
|
533
312
|
const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType);
|
|
534
|
-
const encWriteStream = new
|
|
313
|
+
const encWriteStream = new Readable({ read: () => {}, highWaterMark: 64 * 1024 });
|
|
535
314
|
let bodyPath;
|
|
536
315
|
let writeStream;
|
|
537
316
|
let didSaveToTmpPath = false;
|
|
317
|
+
|
|
538
318
|
if (type === 'file') {
|
|
539
|
-
bodyPath = media.url;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
writeStream = (0, fs_1.createWriteStream)(bodyPath);
|
|
319
|
+
bodyPath = media.url?.toString?.() || media.url;
|
|
320
|
+
} else if (saveOriginalFileIfRequired) {
|
|
321
|
+
bodyPath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2());
|
|
322
|
+
writeStream = createWriteStream(bodyPath);
|
|
544
323
|
didSaveToTmpPath = true;
|
|
545
324
|
}
|
|
325
|
+
|
|
546
326
|
let fileLength = 0;
|
|
547
327
|
const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv);
|
|
548
328
|
let hmac = Crypto.createHmac('sha256', macKey).update(iv);
|
|
549
329
|
let sha256Plain = Crypto.createHash('sha256');
|
|
550
330
|
let sha256Enc = Crypto.createHash('sha256');
|
|
331
|
+
|
|
332
|
+
const onChunk = (buff) => {
|
|
333
|
+
sha256Enc = sha256Enc.update(buff);
|
|
334
|
+
hmac = hmac.update(buff);
|
|
335
|
+
encWriteStream.push(buff);
|
|
336
|
+
};
|
|
337
|
+
|
|
551
338
|
try {
|
|
552
|
-
for await (const data of
|
|
339
|
+
for await (const data of finalStream) {
|
|
553
340
|
fileLength += data.length;
|
|
554
|
-
if (type === 'remote'
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
throw new
|
|
341
|
+
if (type === 'remote' &&
|
|
342
|
+
opts?.maxContentLength &&
|
|
343
|
+
fileLength + data.length > opts.maxContentLength) {
|
|
344
|
+
throw new Boom(`content length exceeded when encrypting "${type}"`, {
|
|
558
345
|
data: { media, type }
|
|
559
346
|
});
|
|
560
347
|
}
|
|
561
348
|
sha256Plain = sha256Plain.update(data);
|
|
562
349
|
if (writeStream) {
|
|
563
350
|
if (!writeStream.write(data)) {
|
|
564
|
-
await
|
|
351
|
+
await once(writeStream, 'drain');
|
|
565
352
|
}
|
|
566
353
|
}
|
|
567
354
|
onChunk(aes.update(data));
|
|
568
355
|
}
|
|
356
|
+
|
|
569
357
|
onChunk(aes.final());
|
|
570
358
|
const mac = hmac.digest().slice(0, 10);
|
|
571
359
|
sha256Enc = sha256Enc.update(mac);
|
|
572
360
|
const fileSha256 = sha256Plain.digest();
|
|
573
361
|
const fileEncSha256 = sha256Enc.digest();
|
|
362
|
+
|
|
574
363
|
encWriteStream.push(mac);
|
|
575
364
|
encWriteStream.push(null);
|
|
576
|
-
writeStream
|
|
577
|
-
|
|
578
|
-
|
|
365
|
+
writeStream?.end();
|
|
366
|
+
if (writeStream) await once(writeStream, 'finish');
|
|
367
|
+
finalStream.destroy();
|
|
368
|
+
|
|
579
369
|
return {
|
|
580
370
|
mediaKey,
|
|
581
371
|
encWriteStream,
|
|
@@ -584,55 +374,47 @@ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfReq
|
|
|
584
374
|
fileEncSha256,
|
|
585
375
|
fileSha256,
|
|
586
376
|
fileLength,
|
|
587
|
-
didSaveToTmpPath
|
|
377
|
+
didSaveToTmpPath,
|
|
378
|
+
opusConverted
|
|
588
379
|
};
|
|
589
|
-
}
|
|
590
|
-
catch (error) {
|
|
380
|
+
} catch (error) {
|
|
591
381
|
encWriteStream.destroy();
|
|
592
|
-
writeStream
|
|
382
|
+
writeStream?.destroy();
|
|
593
383
|
aes.destroy();
|
|
594
384
|
hmac.destroy();
|
|
595
385
|
sha256Plain.destroy();
|
|
596
386
|
sha256Enc.destroy();
|
|
597
387
|
stream.destroy();
|
|
598
388
|
if (didSaveToTmpPath) {
|
|
599
|
-
try {
|
|
600
|
-
await fs_1.promises.unlink(bodyPath);
|
|
601
|
-
}
|
|
602
|
-
catch (err) {
|
|
603
|
-
logger === null || logger === void 0 ? void 0 : logger.error({ err }, 'failed to save to tmp path');
|
|
604
|
-
}
|
|
389
|
+
try { await fs.unlink(bodyPath); } catch (_) {}
|
|
605
390
|
}
|
|
606
391
|
throw error;
|
|
607
392
|
}
|
|
608
|
-
function onChunk(buff) {
|
|
609
|
-
sha256Enc = sha256Enc.update(buff);
|
|
610
|
-
hmac = hmac.update(buff);
|
|
611
|
-
encWriteStream.push(buff);
|
|
612
|
-
}
|
|
613
393
|
};
|
|
614
|
-
exports.encryptedStream = encryptedStream;
|
|
615
394
|
const DEF_HOST = 'mmg.whatsapp.net';
|
|
616
395
|
const AES_CHUNK_SIZE = 16;
|
|
617
396
|
const toSmallestChunkSize = (num) => {
|
|
618
397
|
return Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE;
|
|
619
398
|
};
|
|
620
|
-
const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`;
|
|
621
|
-
|
|
622
|
-
const
|
|
623
|
-
const
|
|
624
|
-
const downloadUrl = isValidMediaUrl ? url : (0, exports.getUrlFromDirectPath)(directPath);
|
|
399
|
+
export const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`;
|
|
400
|
+
export const downloadContentFromMessage = async ({ mediaKey, directPath, url }, type, opts = {}) => {
|
|
401
|
+
const isValidMediaUrl = url?.startsWith('https://mmg.whatsapp.net/');
|
|
402
|
+
const downloadUrl = isValidMediaUrl ? url : getUrlFromDirectPath(directPath);
|
|
625
403
|
if (!downloadUrl) {
|
|
626
|
-
throw new
|
|
404
|
+
throw new Boom('No valid media URL or directPath present in message', { statusCode: 400 });
|
|
627
405
|
}
|
|
628
406
|
const keys = await getMediaKeys(mediaKey, type);
|
|
629
|
-
return
|
|
407
|
+
return downloadEncryptedContent(downloadUrl, keys, opts);
|
|
630
408
|
};
|
|
631
|
-
|
|
632
|
-
|
|
409
|
+
/**
|
|
410
|
+
* Decrypts and downloads an AES256-CBC encrypted file given the keys.
|
|
411
|
+
* Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
|
|
412
|
+
* */
|
|
413
|
+
export const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
|
|
633
414
|
let bytesFetched = 0;
|
|
634
415
|
let startChunk = 0;
|
|
635
416
|
let firstBlockIsIV = false;
|
|
417
|
+
// if a start byte is specified -- then we need to fetch the previous chunk as that will form the IV
|
|
636
418
|
if (startByte) {
|
|
637
419
|
const chunk = toSmallestChunkSize(startByte || 0);
|
|
638
420
|
if (chunk) {
|
|
@@ -642,9 +424,14 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
642
424
|
}
|
|
643
425
|
}
|
|
644
426
|
const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
|
|
427
|
+
const headersInit = options?.headers ? options.headers : undefined;
|
|
645
428
|
const headers = {
|
|
646
|
-
...(
|
|
647
|
-
|
|
429
|
+
...(headersInit
|
|
430
|
+
? Array.isArray(headersInit)
|
|
431
|
+
? Object.fromEntries(headersInit)
|
|
432
|
+
: headersInit
|
|
433
|
+
: {}),
|
|
434
|
+
Origin: DEFAULT_ORIGIN
|
|
648
435
|
};
|
|
649
436
|
if (startChunk || endChunk) {
|
|
650
437
|
headers.Range = `bytes=${startChunk}-`;
|
|
@@ -652,11 +439,10 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
652
439
|
headers.Range += endChunk;
|
|
653
440
|
}
|
|
654
441
|
}
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
maxContentLength: Infinity,
|
|
442
|
+
// download the message
|
|
443
|
+
const fetched = await getHttpStream(downloadUrl, {
|
|
444
|
+
...(options || {}),
|
|
445
|
+
headers
|
|
660
446
|
});
|
|
661
447
|
let remainingBytes = Buffer.from([]);
|
|
662
448
|
let aes;
|
|
@@ -671,7 +457,7 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
671
457
|
push(bytes);
|
|
672
458
|
}
|
|
673
459
|
};
|
|
674
|
-
const output = new
|
|
460
|
+
const output = new Transform({
|
|
675
461
|
transform(chunk, _, callback) {
|
|
676
462
|
let data = Buffer.concat([remainingBytes, chunk]);
|
|
677
463
|
const decryptLength = toSmallestChunkSize(data.length);
|
|
@@ -684,6 +470,8 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
684
470
|
data = data.slice(AES_CHUNK_SIZE);
|
|
685
471
|
}
|
|
686
472
|
aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue);
|
|
473
|
+
// if an end byte that is not EOF is specified
|
|
474
|
+
// stop auto padding (PKCS7) -- otherwise throws an error for decryption
|
|
687
475
|
if (endByte) {
|
|
688
476
|
aes.setAutoPadding(false);
|
|
689
477
|
}
|
|
@@ -704,18 +492,15 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
704
492
|
catch (error) {
|
|
705
493
|
callback(error);
|
|
706
494
|
}
|
|
707
|
-
}
|
|
495
|
+
}
|
|
708
496
|
});
|
|
709
497
|
return fetched.pipe(output, { end: true });
|
|
710
498
|
};
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
const getExtension = (mimetype) => mimetype.split(';')[0].split('/')[1];
|
|
499
|
+
export function extensionForMediaMessage(message) {
|
|
500
|
+
const getExtension = (mimetype) => mimetype.split(';')[0]?.split('/')[1];
|
|
714
501
|
const type = Object.keys(message)[0];
|
|
715
502
|
let extension;
|
|
716
|
-
if (type === 'locationMessage' ||
|
|
717
|
-
type === 'liveLocationMessage' ||
|
|
718
|
-
type === 'productMessage') {
|
|
503
|
+
if (type === 'locationMessage' || type === 'liveLocationMessage' || type === 'productMessage') {
|
|
719
504
|
extension = '.jpeg';
|
|
720
505
|
}
|
|
721
506
|
else {
|
|
@@ -724,39 +509,163 @@ function extensionForMediaMessage(message) {
|
|
|
724
509
|
}
|
|
725
510
|
return extension;
|
|
726
511
|
}
|
|
727
|
-
const
|
|
728
|
-
return
|
|
729
|
-
|
|
512
|
+
const isNodeRuntime = () => {
|
|
513
|
+
return (typeof process !== 'undefined' &&
|
|
514
|
+
process.versions?.node !== null &&
|
|
515
|
+
typeof process.versions.bun === 'undefined' &&
|
|
516
|
+
typeof globalThis.Deno === 'undefined');
|
|
517
|
+
};
|
|
518
|
+
export const uploadWithNodeHttp = async ({ url, filePath, headers, timeoutMs, agent }, redirectCount = 0) => {
|
|
519
|
+
if (redirectCount > 5) {
|
|
520
|
+
throw new Error('Too many redirects');
|
|
521
|
+
}
|
|
522
|
+
const parsedUrl = new URL(url);
|
|
523
|
+
const httpModule = parsedUrl.protocol === 'https:' ? await import('https') : await import('http');
|
|
524
|
+
// Get file size for Content-Length header (required for Node.js streaming)
|
|
525
|
+
const fileStats = await fs.stat(filePath);
|
|
526
|
+
const fileSize = fileStats.size;
|
|
527
|
+
return new Promise((resolve, reject) => {
|
|
528
|
+
const req = httpModule.request({
|
|
529
|
+
hostname: parsedUrl.hostname,
|
|
530
|
+
port: parsedUrl.port || (parsedUrl.protocol === 'https:' ? 443 : 80),
|
|
531
|
+
path: parsedUrl.pathname + parsedUrl.search,
|
|
532
|
+
method: 'POST',
|
|
533
|
+
headers: {
|
|
534
|
+
...headers,
|
|
535
|
+
'Content-Length': fileSize
|
|
536
|
+
},
|
|
537
|
+
agent,
|
|
538
|
+
timeout: timeoutMs
|
|
539
|
+
}, res => {
|
|
540
|
+
// Handle redirects (3xx)
|
|
541
|
+
if (res.statusCode && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
542
|
+
res.resume(); // Consume response to free resources
|
|
543
|
+
const newUrl = new URL(res.headers.location, url).toString();
|
|
544
|
+
resolve(uploadWithNodeHttp({
|
|
545
|
+
url: newUrl,
|
|
546
|
+
filePath,
|
|
547
|
+
headers,
|
|
548
|
+
timeoutMs,
|
|
549
|
+
agent
|
|
550
|
+
}, redirectCount + 1));
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
let body = '';
|
|
554
|
+
res.on('data', chunk => (body += chunk));
|
|
555
|
+
res.on('end', () => {
|
|
556
|
+
try {
|
|
557
|
+
resolve(JSON.parse(body));
|
|
558
|
+
}
|
|
559
|
+
catch {
|
|
560
|
+
resolve(undefined);
|
|
561
|
+
}
|
|
562
|
+
});
|
|
563
|
+
});
|
|
564
|
+
req.on('error', reject);
|
|
565
|
+
req.on('timeout', () => {
|
|
566
|
+
req.destroy();
|
|
567
|
+
reject(new Error('Upload timeout'));
|
|
568
|
+
});
|
|
569
|
+
const stream = createReadStream(filePath);
|
|
570
|
+
stream.pipe(req);
|
|
571
|
+
stream.on('error', err => {
|
|
572
|
+
req.destroy();
|
|
573
|
+
reject(err);
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
};
|
|
577
|
+
const uploadWithFetch = async ({ url, filePath, headers, timeoutMs, agent }) => {
|
|
578
|
+
// Convert Node.js Readable to Web ReadableStream
|
|
579
|
+
const nodeStream = createReadStream(filePath);
|
|
580
|
+
const webStream = Readable.toWeb(nodeStream);
|
|
581
|
+
const response = await fetch(url, {
|
|
582
|
+
dispatcher: agent,
|
|
583
|
+
method: 'POST',
|
|
584
|
+
body: webStream,
|
|
585
|
+
headers,
|
|
586
|
+
duplex: 'half',
|
|
587
|
+
signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : undefined
|
|
588
|
+
});
|
|
589
|
+
try {
|
|
590
|
+
return (await response.json());
|
|
591
|
+
}
|
|
592
|
+
catch {
|
|
593
|
+
return undefined;
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
/**
|
|
597
|
+
* Uploads media to WhatsApp servers.
|
|
598
|
+
*
|
|
599
|
+
* ## Why we have two upload implementations:
|
|
600
|
+
*
|
|
601
|
+
* Node.js's native `fetch` (powered by undici) has a known bug where it buffers
|
|
602
|
+
* the entire request body in memory before sending, even when using streams.
|
|
603
|
+
* This causes memory issues with large files (e.g., 1GB file = 1GB+ memory usage).
|
|
604
|
+
* See: https://github.com/nodejs/undici/issues/4058
|
|
605
|
+
*
|
|
606
|
+
* Other runtimes (Bun, Deno, browsers) correctly stream the request body without
|
|
607
|
+
* buffering, so we can use the web-standard Fetch API there.
|
|
608
|
+
*
|
|
609
|
+
* ## Future considerations:
|
|
610
|
+
* Once the undici bug is fixed, we can simplify this to use only the Fetch API
|
|
611
|
+
* across all runtimes. Monitor the GitHub issue for updates.
|
|
612
|
+
*/
|
|
613
|
+
const uploadMedia = async (params, logger) => {
|
|
614
|
+
if (isNodeRuntime()) {
|
|
615
|
+
return uploadWithNodeHttp(params);
|
|
616
|
+
}
|
|
617
|
+
else {
|
|
618
|
+
return uploadWithFetch(params);
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
|
|
622
|
+
return async (streamOrPath, { mediaType, fileEncSha256B64, timeoutMs, newsletter }) => {
|
|
623
|
+
// send a query JSON to obtain the url & auth token to upload our media
|
|
730
624
|
let uploadInfo = await refreshMediaConn(false);
|
|
731
625
|
let urls;
|
|
732
626
|
const hosts = [...customUploadHosts, ...uploadInfo.hosts];
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
627
|
+
fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
|
|
628
|
+
// Prepare common headers
|
|
629
|
+
const customHeaders = (() => {
|
|
630
|
+
const hdrs = options?.headers;
|
|
631
|
+
if (!hdrs)
|
|
632
|
+
return {};
|
|
633
|
+
return Array.isArray(hdrs) ? Object.fromEntries(hdrs) : hdrs;
|
|
634
|
+
})();
|
|
635
|
+
const headers = {
|
|
636
|
+
...customHeaders,
|
|
637
|
+
'Content-Type': 'application/octet-stream',
|
|
638
|
+
Origin: DEFAULT_ORIGIN
|
|
639
|
+
};
|
|
640
|
+
// Collect buffer from Readable stream or read from file path
|
|
641
|
+
let reqBuffer;
|
|
642
|
+
if (Buffer.isBuffer(streamOrPath)) {
|
|
643
|
+
reqBuffer = streamOrPath;
|
|
644
|
+
} else if (typeof streamOrPath === 'string') {
|
|
645
|
+
reqBuffer = await fs.readFile(streamOrPath);
|
|
646
|
+
} else {
|
|
647
|
+
// Readable stream
|
|
648
|
+
const chunks = [];
|
|
649
|
+
for await (const chunk of streamOrPath) chunks.push(chunk);
|
|
650
|
+
reqBuffer = Buffer.concat(chunks);
|
|
738
651
|
}
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
let media = Defaults_1.MEDIA_PATH_MAP[mediaType];
|
|
652
|
+
// Newsletter uses different upload path
|
|
653
|
+
let mediaPath = MEDIA_PATH_MAP[mediaType];
|
|
742
654
|
if (newsletter) {
|
|
743
|
-
|
|
655
|
+
mediaPath = mediaPath?.replace('/mms/', '/newsletter/newsletter-');
|
|
744
656
|
}
|
|
745
657
|
for (const { hostname, maxContentLengthBytes } of hosts) {
|
|
746
|
-
logger.debug(`uploading to "${hostname}"`);
|
|
747
658
|
const auth = encodeURIComponent(uploadInfo.auth);
|
|
748
|
-
const url = `https://${hostname}${
|
|
659
|
+
const url = `https://${hostname}${mediaPath}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
660
|
+
|
|
749
661
|
let result;
|
|
750
662
|
try {
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
const body = await axios_1.default.post(url, reqBody, {
|
|
663
|
+
// Upload buffer directly like wiley (avoids file I/O issues)
|
|
664
|
+
const axios = (await import('axios')).default;
|
|
665
|
+
const body = await axios.post(url, reqBuffer, {
|
|
755
666
|
...options,
|
|
756
667
|
headers: {
|
|
757
|
-
...
|
|
758
|
-
'Content-Type': 'application/octet-stream',
|
|
759
|
-
'Origin': Defaults_1.DEFAULT_ORIGIN
|
|
668
|
+
...headers,
|
|
760
669
|
},
|
|
761
670
|
httpsAgent: fetchAgent,
|
|
762
671
|
timeout: timeoutMs,
|
|
@@ -765,11 +674,15 @@ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options },
|
|
|
765
674
|
maxContentLength: Infinity,
|
|
766
675
|
});
|
|
767
676
|
result = body.data;
|
|
768
|
-
|
|
677
|
+
|
|
678
|
+
if (result?.url || result?.direct_path) {
|
|
769
679
|
urls = {
|
|
770
680
|
mediaUrl: result.url,
|
|
771
681
|
directPath: result.direct_path,
|
|
772
|
-
handle: result.handle
|
|
682
|
+
handle: result.handle,
|
|
683
|
+
meta_hmac: result.meta_hmac,
|
|
684
|
+
fbid: result.fbid,
|
|
685
|
+
ts: result.ts
|
|
773
686
|
};
|
|
774
687
|
break;
|
|
775
688
|
}
|
|
@@ -779,37 +692,40 @@ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options },
|
|
|
779
692
|
}
|
|
780
693
|
}
|
|
781
694
|
catch (error) {
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
}
|
|
785
|
-
const isLast = hostname === ((_b = hosts[uploadInfo.hosts.length - 1]) === null || _b === void 0 ? void 0 : _b.hostname);
|
|
786
|
-
logger.warn({ trace: error.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`);
|
|
695
|
+
const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
|
|
696
|
+
|
|
697
|
+
logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`);
|
|
787
698
|
}
|
|
788
699
|
}
|
|
789
700
|
if (!urls) {
|
|
790
|
-
throw new
|
|
701
|
+
throw new Boom('Media upload failed on all hosts', { statusCode: 500 });
|
|
791
702
|
}
|
|
792
703
|
return urls;
|
|
793
704
|
};
|
|
794
705
|
};
|
|
795
|
-
exports.getWAUploadToServer = getWAUploadToServer;
|
|
796
706
|
const getMediaRetryKey = (mediaKey) => {
|
|
797
|
-
return
|
|
707
|
+
return hkdf(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' });
|
|
798
708
|
};
|
|
799
|
-
|
|
709
|
+
/**
|
|
710
|
+
* Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
|
|
711
|
+
*/
|
|
712
|
+
export const encryptMediaRetryRequest = (key, mediaKey, meId) => {
|
|
800
713
|
const recp = { stanzaId: key.id };
|
|
801
|
-
const recpBuffer =
|
|
714
|
+
const recpBuffer = proto.ServerErrorReceipt.encode(recp).finish();
|
|
802
715
|
const iv = Crypto.randomBytes(12);
|
|
803
|
-
const retryKey =
|
|
804
|
-
const ciphertext =
|
|
716
|
+
const retryKey = getMediaRetryKey(mediaKey);
|
|
717
|
+
const ciphertext = aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id));
|
|
805
718
|
const req = {
|
|
806
719
|
tag: 'receipt',
|
|
807
720
|
attrs: {
|
|
808
721
|
id: key.id,
|
|
809
|
-
to:
|
|
722
|
+
to: jidNormalizedUser(meId),
|
|
810
723
|
type: 'server-error'
|
|
811
724
|
},
|
|
812
725
|
content: [
|
|
726
|
+
// this encrypt node is actually pretty useless
|
|
727
|
+
// the media is returned even without this node
|
|
728
|
+
// keeping it here to maintain parity with WA Web
|
|
813
729
|
{
|
|
814
730
|
tag: 'encrypt',
|
|
815
731
|
attrs: {},
|
|
@@ -822,7 +738,8 @@ const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
|
|
|
822
738
|
tag: 'rmr',
|
|
823
739
|
attrs: {
|
|
824
740
|
jid: key.remoteJid,
|
|
825
|
-
|
|
741
|
+
from_me: (!!key.fromMe).toString(),
|
|
742
|
+
// @ts-ignore
|
|
826
743
|
participant: key.participant || undefined
|
|
827
744
|
}
|
|
828
745
|
}
|
|
@@ -830,9 +747,8 @@ const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
|
|
|
830
747
|
};
|
|
831
748
|
return req;
|
|
832
749
|
};
|
|
833
|
-
|
|
834
|
-
const
|
|
835
|
-
const rmrNode = (0, WABinary_1.getBinaryNodeChild)(node, 'rmr');
|
|
750
|
+
export const decodeMediaRetryNode = (node) => {
|
|
751
|
+
const rmrNode = getBinaryNodeChild(node, 'rmr');
|
|
836
752
|
const event = {
|
|
837
753
|
key: {
|
|
838
754
|
id: node.attrs.id,
|
|
@@ -841,39 +757,74 @@ const decodeMediaRetryNode = (node) => {
|
|
|
841
757
|
participant: rmrNode.attrs.participant
|
|
842
758
|
}
|
|
843
759
|
};
|
|
844
|
-
const errorNode =
|
|
760
|
+
const errorNode = getBinaryNodeChild(node, 'error');
|
|
845
761
|
if (errorNode) {
|
|
846
762
|
const errorCode = +errorNode.attrs.code;
|
|
847
|
-
event.error = new
|
|
763
|
+
event.error = new Boom(`Failed to re-upload media (${errorCode})`, {
|
|
764
|
+
data: errorNode.attrs,
|
|
765
|
+
statusCode: getStatusCodeForMediaRetry(errorCode)
|
|
766
|
+
});
|
|
848
767
|
}
|
|
849
768
|
else {
|
|
850
|
-
const encryptedInfoNode =
|
|
851
|
-
const ciphertext =
|
|
852
|
-
const iv =
|
|
769
|
+
const encryptedInfoNode = getBinaryNodeChild(node, 'encrypt');
|
|
770
|
+
const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p');
|
|
771
|
+
const iv = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_iv');
|
|
853
772
|
if (ciphertext && iv) {
|
|
854
773
|
event.media = { ciphertext, iv };
|
|
855
774
|
}
|
|
856
775
|
else {
|
|
857
|
-
event.error = new
|
|
776
|
+
event.error = new Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 });
|
|
858
777
|
}
|
|
859
778
|
}
|
|
860
779
|
return event;
|
|
861
780
|
};
|
|
862
|
-
|
|
863
|
-
const
|
|
864
|
-
const
|
|
865
|
-
|
|
866
|
-
return WAProto_1.proto.MediaRetryNotification.decode(plaintext);
|
|
781
|
+
export const decryptMediaRetryData = ({ ciphertext, iv }, mediaKey, msgId) => {
|
|
782
|
+
const retryKey = getMediaRetryKey(mediaKey);
|
|
783
|
+
const plaintext = aesDecryptGCM(ciphertext, retryKey, iv, Buffer.from(msgId));
|
|
784
|
+
return proto.MediaRetryNotification.decode(plaintext);
|
|
867
785
|
};
|
|
868
|
-
|
|
869
|
-
const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
|
|
870
|
-
exports.getStatusCodeForMediaRetry = getStatusCodeForMediaRetry;
|
|
786
|
+
export const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
|
|
871
787
|
const MEDIA_RETRY_STATUS_MAP = {
|
|
872
|
-
[
|
|
873
|
-
[
|
|
874
|
-
[
|
|
875
|
-
[
|
|
788
|
+
[proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
|
|
789
|
+
[proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
|
|
790
|
+
[proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
|
|
791
|
+
[proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
|
|
792
|
+
};
|
|
793
|
+
|
|
794
|
+
export const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts, isPtt, forceOpus } = {}) => {
|
|
795
|
+
const { stream, type } = await getStream(media, opts);
|
|
796
|
+
let buffer = await toBuffer(stream);
|
|
797
|
+
|
|
798
|
+
let opusConverted = false;
|
|
799
|
+
|
|
800
|
+
let bodyPath;
|
|
801
|
+
let didSaveToTmpPath = false;
|
|
802
|
+
try {
|
|
803
|
+
if (type === 'file') {
|
|
804
|
+
bodyPath = media.url?.toString?.() || media.url;
|
|
805
|
+
} else if (saveOriginalFileIfRequired) {
|
|
806
|
+
bodyPath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2());
|
|
807
|
+
const { writeFileSync } = await import('fs');
|
|
808
|
+
writeFileSync(bodyPath, buffer);
|
|
809
|
+
didSaveToTmpPath = true;
|
|
810
|
+
}
|
|
811
|
+
const fileLength = buffer.length;
|
|
812
|
+
const fileSha256 = Crypto.createHash('sha256').update(buffer).digest();
|
|
813
|
+
return {
|
|
814
|
+
mediaKey: undefined,
|
|
815
|
+
encWriteStream: buffer,
|
|
816
|
+
fileLength,
|
|
817
|
+
fileSha256,
|
|
818
|
+
fileEncSha256: undefined,
|
|
819
|
+
bodyPath,
|
|
820
|
+
didSaveToTmpPath,
|
|
821
|
+
opusConverted
|
|
822
|
+
};
|
|
823
|
+
} catch (error) {
|
|
824
|
+
if (didSaveToTmpPath && bodyPath) {
|
|
825
|
+
try { await fs.unlink(bodyPath); } catch (_) {}
|
|
826
|
+
}
|
|
827
|
+
throw error;
|
|
828
|
+
}
|
|
876
829
|
};
|
|
877
|
-
|
|
878
|
-
throw new Error('Function not implemented.');
|
|
879
|
-
}
|
|
830
|
+
//# sourceMappingURL=messages-media.js.map
|