@vanzxysenpai/baileys 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/LICENSE +25 -0
- package/NOTICE.md +158 -0
- package/README.md +5294 -2
- package/VanzxyBanner.js +134 -0
- package/WAProto/index.d.ts +13317 -0
- package/WAProto/index.js +147553 -0
- package/lib/Builders/A2UI.d.ts +131 -0
- package/lib/Builders/A2UI.js +343 -0
- package/lib/Builders/AIRich.d.ts +1021 -0
- package/lib/Builders/AIRich.js +2889 -0
- package/lib/Builders/Album.d.ts +51 -0
- package/lib/Builders/Album.js +99 -0
- package/lib/Builders/Button.d.ts +137449 -0
- package/lib/Builders/Button.js +1054 -0
- package/lib/Builders/ButtonV2.d.ts +43 -0
- package/lib/Builders/ButtonV2.js +175 -0
- package/lib/Builders/ButtonV3.d.ts +35 -0
- package/lib/Builders/ButtonV3.js +189 -0
- package/lib/Builders/Carousel.d.ts +68 -0
- package/lib/Builders/Carousel.js +161 -0
- package/lib/Builders/Event.d.ts +61 -0
- package/lib/Builders/Event.js +118 -0
- package/lib/Builders/Expiring.d.ts +17 -0
- package/lib/Builders/Expiring.js +27 -0
- package/lib/Builders/Poll.d.ts +63 -0
- package/lib/Builders/Poll.js +139 -0
- package/lib/Builders/VanzxyBaileys.d.ts +42 -0
- package/lib/Builders/VanzxyBaileys.js +110 -0
- package/lib/Builders/errors.d.ts +25 -0
- package/lib/Builders/errors.js +29 -0
- package/lib/Builders/index.d.ts +14 -0
- package/lib/Builders/index.js +23 -0
- package/lib/Builders/shared.d.ts +395 -0
- package/lib/Builders/shared.js +738 -0
- package/lib/Defaults/bot-jid.d.ts +1 -0
- package/lib/Defaults/bot-jid.js +1 -0
- package/lib/Defaults/index.d.ts +161 -0
- package/lib/Defaults/index.js +165 -0
- package/lib/Defaults/version.d.ts +1 -0
- package/lib/Defaults/version.js +1 -0
- package/lib/Framework/Bot.d.ts +865 -0
- package/lib/Framework/Bot.js +230 -0
- package/lib/Framework/Context.d.ts +17 -0
- package/lib/Framework/Context.js +90 -0
- package/lib/Framework/MediaManager.d.ts +15 -0
- package/lib/Framework/MediaManager.js +154 -0
- package/lib/Framework/SessionManager.d.ts +10 -0
- package/lib/Framework/SessionManager.js +34 -0
- package/lib/Framework/StatsManager.d.ts +20 -0
- package/lib/Framework/StatsManager.js +117 -0
- package/lib/Framework/Store/SQLiteStore.d.ts +13 -0
- package/lib/Framework/Store/SQLiteStore.js +68 -0
- package/lib/Framework/index.d.ts +6 -0
- package/lib/Framework/index.js +11 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.d.ts +7 -0
- package/lib/Signal/Group/group-session-builder.js +29 -0
- package/lib/Signal/Group/group_cipher.d.ts +10 -0
- package/lib/Signal/Group/group_cipher.js +81 -0
- package/lib/Signal/Group/index.d.ts +11 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.d.ts +6 -0
- package/lib/Signal/Group/keyhelper.js +17 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
- package/lib/Signal/Group/sender-chain-key.js +25 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
- package/lib/Signal/Group/sender-key-message.d.ts +18 -0
- package/lib/Signal/Group/sender-key-message.js +65 -0
- package/lib/Signal/Group/sender-key-name.d.ts +11 -0
- package/lib/Signal/Group/sender-key-name.js +47 -0
- package/lib/Signal/Group/sender-key-record.d.ts +12 -0
- package/lib/Signal/Group/sender-key-record.js +40 -0
- package/lib/Signal/Group/sender-key-state.d.ts +16 -0
- package/lib/Signal/Group/sender-key-state.js +83 -0
- package/lib/Signal/Group/sender-message-key.d.ts +11 -0
- package/lib/Signal/Group/sender-message-key.js +25 -0
- package/lib/Signal/libsignal.d.ts +58 -0
- package/lib/Signal/libsignal.js +436 -0
- package/lib/Signal/lid-mapping.d.ts +20 -0
- package/lib/Signal/lid-mapping.js +276 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.d.ts +5 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.d.ts +674 -0
- package/lib/Socket/business.js +379 -0
- package/lib/Socket/chats.d.ts +137 -0
- package/lib/Socket/chats.js +1299 -0
- package/lib/Socket/communities.d.ts +805 -0
- package/lib/Socket/communities.js +454 -0
- package/lib/Socket/dugong.d.ts +118 -0
- package/lib/Socket/dugong.js +810 -0
- package/lib/Socket/groups.d.ts +287 -0
- package/lib/Socket/groups.js +386 -0
- package/lib/Socket/index.d.ts +826 -0
- package/lib/Socket/index.js +35 -0
- package/lib/Socket/messages-recv.d.ts +613 -0
- package/lib/Socket/messages-recv.js +1851 -0
- package/lib/Socket/messages-send.d.ts +608 -0
- package/lib/Socket/messages-send.js +1574 -0
- package/lib/Socket/mex-fallback.d.ts +51 -0
- package/lib/Socket/mex-fallback.js +97 -0
- package/lib/Socket/mex.d.ts +1 -0
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter-ext.d.ts +47 -0
- package/lib/Socket/newsletter-ext.js +262 -0
- package/lib/Socket/newsletter.d.ts +285 -0
- package/lib/Socket/newsletter.js +223 -0
- package/lib/Socket/socket.d.ts +60 -0
- package/lib/Socket/socket.js +1001 -0
- package/lib/Socket/username.d.ts +842 -0
- package/lib/Socket/username.js +156 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.d.ts +62 -0
- package/lib/Store/make-in-memory-store.js +425 -0
- package/lib/Store/make-ordered-dictionary.d.ts +12 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.d.ts +1 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.d.ts +1 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.d.ts +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.d.ts +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.d.ts +1 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.d.ts +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.d.ts +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.d.ts +2 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.d.ts +2 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.d.ts +11 -0
- package/lib/Types/Message.js +17 -0
- package/lib/Types/Mex.d.ts +2 -0
- package/lib/Types/Mex.js +38 -0
- package/lib/Types/Product.d.ts +1 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/RichType.d.ts +2 -0
- package/lib/Types/RichType.js +22 -0
- package/lib/Types/Signal.d.ts +1 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.d.ts +1 -0
- package/lib/Types/Socket.js +1 -0
- package/lib/Types/State.d.ts +5 -0
- package/lib/Types/State.js +55 -0
- package/lib/Types/USync.d.ts +1 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.d.ts +15 -0
- package/lib/Types/index.js +27 -0
- package/lib/Utils/PersistentStore.d.ts +83 -0
- package/lib/Utils/PersistentStore.js +592 -0
- package/lib/Utils/anchor-guard.d.ts +31 -0
- package/lib/Utils/anchor-guard.js +329 -0
- package/lib/Utils/android-stanza-id.d.ts +4 -0
- package/lib/Utils/android-stanza-id.js +21 -0
- package/lib/Utils/anti-delete.d.ts +62 -0
- package/lib/Utils/anti-delete.js +212 -0
- package/lib/Utils/auth-secure.d.ts +180 -0
- package/lib/Utils/auth-secure.js +791 -0
- package/lib/Utils/auth-utils.d.ts +59 -0
- package/lib/Utils/auth-utils.js +326 -0
- package/lib/Utils/auto-reconnect.d.ts +8 -0
- package/lib/Utils/auto-reconnect.js +204 -0
- package/lib/Utils/auto-reply.d.ts +35 -0
- package/lib/Utils/auto-reply.js +152 -0
- package/lib/Utils/bad-mac-handler.d.ts +59 -0
- package/lib/Utils/bad-mac-handler.js +119 -0
- package/lib/Utils/baileys-event-stream.d.ts +5 -0
- package/lib/Utils/baileys-event-stream.js +62 -0
- package/lib/Utils/browser-utils.d.ts +9 -0
- package/lib/Utils/browser-utils.js +29 -0
- package/lib/Utils/business.d.ts +49 -0
- package/lib/Utils/business.js +236 -0
- package/lib/Utils/button-helper-utils.d.ts +59 -0
- package/lib/Utils/button-helper-utils.js +266 -0
- package/lib/Utils/button-sender.d.ts +164 -0
- package/lib/Utils/button-sender.js +845 -0
- package/lib/Utils/chat-control.d.ts +98 -0
- package/lib/Utils/chat-control.js +231 -0
- package/lib/Utils/chat-history-helpers.d.ts +4 -0
- package/lib/Utils/chat-history-helpers.js +70 -0
- package/lib/Utils/chat-utils.d.ts +408 -0
- package/lib/Utils/chat-utils.js +879 -0
- package/lib/Utils/companion-reg-client-utils.d.ts +4 -0
- package/lib/Utils/companion-reg-client-utils.js +39 -0
- package/lib/Utils/connection-watchdog.d.ts +10 -0
- package/lib/Utils/connection-watchdog.js +74 -0
- package/lib/Utils/crypto.d.ts +41 -0
- package/lib/Utils/crypto.js +145 -0
- package/lib/Utils/decode-wa-message.d.ts +83 -0
- package/lib/Utils/decode-wa-message.js +315 -0
- package/lib/Utils/disconnect-reason.d.ts +13 -0
- package/lib/Utils/disconnect-reason.js +36 -0
- package/lib/Utils/event-buffer.d.ts +12 -0
- package/lib/Utils/event-buffer.js +640 -0
- package/lib/Utils/fetch-timeout.d.ts +4 -0
- package/lib/Utils/fetch-timeout.js +14 -0
- package/lib/Utils/ffmpeg-path.d.ts +5 -0
- package/lib/Utils/ffmpeg-path.js +130 -0
- package/lib/Utils/generics.d.ts +79 -0
- package/lib/Utils/generics.js +426 -0
- package/lib/Utils/group-metadata-cache.d.ts +3 -0
- package/lib/Utils/group-metadata-cache.js +25 -0
- package/lib/Utils/group-status-detection.d.ts +5 -0
- package/lib/Utils/group-status-detection.js +114 -0
- package/lib/Utils/history.d.ts +91 -0
- package/lib/Utils/history.js +144 -0
- package/lib/Utils/html-app.d.ts +14 -0
- package/lib/Utils/html-app.js +97 -0
- package/lib/Utils/html-multiplayer.d.ts +8 -0
- package/lib/Utils/html-multiplayer.js +90 -0
- package/lib/Utils/humanizer.d.ts +8 -0
- package/lib/Utils/humanizer.js +74 -0
- package/lib/Utils/identity-change-handler.d.ts +13 -0
- package/lib/Utils/identity-change-handler.js +49 -0
- package/lib/Utils/index.d.ts +88 -0
- package/lib/Utils/index.js +93 -0
- package/lib/Utils/jid-tools.d.ts +3 -0
- package/lib/Utils/jid-tools.js +78 -0
- package/lib/Utils/link-preview-metadata.d.ts +47 -0
- package/lib/Utils/link-preview-metadata.js +90 -0
- package/lib/Utils/link-preview.d.ts +12 -0
- package/lib/Utils/link-preview.js +84 -0
- package/lib/Utils/lock-manager.d.ts +5 -0
- package/lib/Utils/lock-manager.js +50 -0
- package/lib/Utils/logger.d.ts +2 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +26 -0
- package/lib/Utils/make-mutex.d.ts +6 -0
- package/lib/Utils/make-mutex.js +32 -0
- package/lib/Utils/media-messages.d.ts +35 -0
- package/lib/Utils/media-messages.js +70 -0
- package/lib/Utils/media-processor.d.ts +6 -0
- package/lib/Utils/media-processor.js +174 -0
- package/lib/Utils/media-set.d.ts +6 -0
- package/lib/Utils/media-set.js +169 -0
- package/lib/Utils/message-kind.d.ts +26 -0
- package/lib/Utils/message-kind.js +138 -0
- package/lib/Utils/message-retry-manager.d.ts +79 -0
- package/lib/Utils/message-retry-manager.js +264 -0
- package/lib/Utils/message-search.d.ts +40 -0
- package/lib/Utils/message-search.js +181 -0
- package/lib/Utils/messages-media.d.ts +136 -0
- package/lib/Utils/messages-media.js +867 -0
- package/lib/Utils/messages.d.ts +66 -0
- package/lib/Utils/messages.js +2271 -0
- package/lib/Utils/meta-ai-msmsg.d.ts +3 -0
- package/lib/Utils/meta-ai-msmsg.js +132 -0
- package/lib/Utils/meta-ai.d.ts +4 -0
- package/lib/Utils/meta-ai.js +75 -0
- package/lib/Utils/migrate-auth-state.d.ts +23 -0
- package/lib/Utils/migrate-auth-state.js +126 -0
- package/lib/Utils/mini-app.d.ts +20 -0
- package/lib/Utils/mini-app.js +215 -0
- package/lib/Utils/mock-socket.d.ts +67 -0
- package/lib/Utils/mock-socket.js +254 -0
- package/lib/Utils/modern-messages.d.ts +64 -0
- package/lib/Utils/modern-messages.js +497 -0
- package/lib/Utils/native-flow.d.ts +19 -0
- package/lib/Utils/native-flow.js +112 -0
- package/lib/Utils/newsletter-status.d.ts +157 -0
- package/lib/Utils/newsletter-status.js +652 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.js +200 -0
- package/lib/Utils/offline-node-processor.d.ts +9 -0
- package/lib/Utils/offline-node-processor.js +39 -0
- package/lib/Utils/pairing-utils.d.ts +5 -0
- package/lib/Utils/pairing-utils.js +29 -0
- package/lib/Utils/passkey.d.ts +4 -0
- package/lib/Utils/passkey.js +11 -0
- package/lib/Utils/past-participants.d.ts +2 -0
- package/lib/Utils/past-participants.js +40 -0
- package/lib/Utils/payment-detection.d.ts +40 -0
- package/lib/Utils/payment-detection.js +213 -0
- package/lib/Utils/payment-guard.d.ts +28 -0
- package/lib/Utils/payment-guard.js +143 -0
- package/lib/Utils/payment-messages.d.ts +42 -0
- package/lib/Utils/payment-messages.js +161 -0
- package/lib/Utils/payment-utils.d.ts +75 -0
- package/lib/Utils/payment-utils.js +145 -0
- package/lib/Utils/phone-utils.d.ts +3 -0
- package/lib/Utils/phone-utils.js +29 -0
- package/lib/Utils/pre-key-manager.d.ts +25 -0
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.d.ts +103 -0
- package/lib/Utils/process-message.js +868 -0
- package/lib/Utils/qr-terminal.d.ts +1 -0
- package/lib/Utils/qr-terminal.js +16 -0
- package/lib/Utils/reconnect-backoff.d.ts +25 -0
- package/lib/Utils/reconnect-backoff.js +47 -0
- package/lib/Utils/reporting-utils.d.ts +12 -0
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/rich-message-utils.d.ts +54 -0
- package/lib/Utils/rich-message-utils.js +435 -0
- package/lib/Utils/rich-messages.d.ts +298 -0
- package/lib/Utils/rich-messages.js +1145 -0
- package/lib/Utils/scheduled-message.d.ts +81 -0
- package/lib/Utils/scheduled-message.js +101 -0
- package/lib/Utils/scheduling.d.ts +59 -0
- package/lib/Utils/scheduling.js +139 -0
- package/lib/Utils/signal.d.ts +82 -0
- package/lib/Utils/signal.js +200 -0
- package/lib/Utils/stanza-ack.d.ts +16 -0
- package/lib/Utils/stanza-ack.js +37 -0
- package/lib/Utils/status-stickers.d.ts +79 -0
- package/lib/Utils/status-stickers.js +261 -0
- package/lib/Utils/status.d.ts +107 -0
- package/lib/Utils/status.js +131 -0
- package/lib/Utils/sticker-pack-maker.d.ts +20 -0
- package/lib/Utils/sticker-pack-maker.js +112 -0
- package/lib/Utils/stickerpack.d.ts +18 -0
- package/lib/Utils/stickerpack.js +316 -0
- package/lib/Utils/sync-action-utils.d.ts +2 -0
- package/lib/Utils/sync-action-utils.js +48 -0
- package/lib/Utils/tag.d.ts +8 -0
- package/lib/Utils/tag.js +45 -0
- package/lib/Utils/tc-token-utils.d.ts +29 -0
- package/lib/Utils/tc-token-utils.js +162 -0
- package/lib/Utils/templates.d.ts +86 -0
- package/lib/Utils/templates.js +148 -0
- package/lib/Utils/text-format.d.ts +13 -0
- package/lib/Utils/text-format.js +81 -0
- package/lib/Utils/text-tools.d.ts +3 -0
- package/lib/Utils/text-tools.js +38 -0
- package/lib/Utils/ttl-cache.d.ts +29 -0
- package/lib/Utils/ttl-cache.js +123 -0
- package/lib/Utils/use-cache-manager-auth-state.d.ts +22 -0
- package/lib/Utils/use-cache-manager-auth-state.js +86 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +20 -0
- package/lib/Utils/use-multi-file-auth-state.js +201 -0
- package/lib/Utils/use-single-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-single-file-auth-state.js +111 -0
- package/lib/Utils/use-sqlite-auth-state.d.ts +13 -0
- package/lib/Utils/use-sqlite-auth-state.js +171 -0
- package/lib/Utils/username.d.ts +22 -0
- package/lib/Utils/username.js +59 -0
- package/lib/Utils/validate-connection.d.ts +43 -0
- package/lib/Utils/validate-connection.js +197 -0
- package/lib/Utils/vcard.d.ts +40 -0
- package/lib/Utils/vcard.js +92 -0
- package/lib/Utils/voice-note.d.ts +13 -0
- package/lib/Utils/voice-note.js +47 -0
- package/lib/VoIP/audio-feeder.d.ts +15 -0
- package/lib/VoIP/audio-feeder.js +132 -0
- package/lib/VoIP/index.d.ts +22 -0
- package/lib/VoIP/index.js +277 -0
- package/lib/VoIP/relay-transport.d.ts +16 -0
- package/lib/VoIP/relay-transport.js +559 -0
- package/lib/VoIP/signaling.d.ts +16 -0
- package/lib/VoIP/signaling.js +624 -0
- package/lib/VoIP/types.d.ts +11 -0
- package/lib/VoIP/types.js +17 -0
- package/lib/VoIP/wasm-engine.d.ts +28 -0
- package/lib/VoIP/wasm-engine.js +1214 -0
- package/lib/VoIP/worker-bootstrap.d.ts +1 -0
- package/lib/VoIP/worker-bootstrap.js +1042 -0
- package/lib/WABinary/constants.d.ts +61 -0
- package/lib/WABinary/constants.js +1466 -0
- package/lib/WABinary/decode.d.ts +9 -0
- package/lib/WABinary/decode.js +261 -0
- package/lib/WABinary/encode.d.ts +2 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.d.ts +45 -0
- package/lib/WABinary/generic-utils.js +285 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.d.ts +32 -0
- package/lib/WABinary/jid-utils.js +102 -0
- package/lib/WABinary/types.d.ts +1 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.d.ts +7 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.d.ts +34926 -0
- package/lib/WAM/constants.js +22852 -0
- package/lib/WAM/encode.d.ts +1 -0
- package/lib/WAM/encode.js +149 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.d.ts +43 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +51 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +29 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +60 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.d.ts +20 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +23 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.d.ts +14 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +24 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +49 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
- package/lib/WAUSync/Protocols/index.d.ts +12 -0
- package/lib/WAUSync/Protocols/index.js +12 -0
- package/lib/WAUSync/USyncQuery.d.ts +25 -0
- package/lib/WAUSync/USyncQuery.js +140 -0
- package/lib/WAUSync/USyncUser.d.ts +24 -0
- package/lib/WAUSync/USyncUser.js +47 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/assets/wasm/loader.js +5 -0
- package/lib/assets/wasm/whatsapp.wasm +0 -0
- package/lib/assets/wasm/worker-modules.js +273 -0
- package/lib/exports-extra.d.ts +45 -0
- package/lib/exports-extra.js +47 -0
- package/lib/index.d.ts +16 -0
- package/lib/index.js +24 -0
- package/package.json +161 -4
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adds caching capability to a SignalKeyStore
|
|
3
|
+
* @param store the store to add caching to
|
|
4
|
+
* @param logger to log trace events
|
|
5
|
+
* @param _cache cache store to use
|
|
6
|
+
*/
|
|
7
|
+
export function makeCacheableSignalKeyStore(store: any, logger: any, _cache: any): {
|
|
8
|
+
get(type: any, ids: any): Promise<any>;
|
|
9
|
+
set(data: any): Promise<any>;
|
|
10
|
+
list(type: any): AsyncGenerator<any, void, any>;
|
|
11
|
+
listIds(type: any): AsyncGenerator<any, void, any>;
|
|
12
|
+
clear(): Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
export function addTransactionCapability(state: any, logger: any, opts1: { [key: string]: any;
|
|
15
|
+
maxCommitRetries: any;
|
|
16
|
+
delayBetweenTriesMs: any;
|
|
17
|
+
}): {
|
|
18
|
+
get: (type: any, ids: any) => Promise<any>;
|
|
19
|
+
set: (data: any) => Promise<void>;
|
|
20
|
+
isInTransaction: () => boolean;
|
|
21
|
+
transaction: (work: any, key: any) => Promise<any>;
|
|
22
|
+
};
|
|
23
|
+
export function assertMeId(creds: any): any;
|
|
24
|
+
export function initAuthCreds(): {
|
|
25
|
+
noiseKey: {
|
|
26
|
+
private: any;
|
|
27
|
+
public: any;
|
|
28
|
+
};
|
|
29
|
+
pairingEphemeralKeyPair: {
|
|
30
|
+
private: any;
|
|
31
|
+
public: any;
|
|
32
|
+
};
|
|
33
|
+
signedIdentityKey: {
|
|
34
|
+
private: any;
|
|
35
|
+
public: any;
|
|
36
|
+
};
|
|
37
|
+
signedPreKey: {
|
|
38
|
+
keyPair: {
|
|
39
|
+
private: any;
|
|
40
|
+
public: any;
|
|
41
|
+
};
|
|
42
|
+
signature: any;
|
|
43
|
+
keyId: any;
|
|
44
|
+
};
|
|
45
|
+
registrationId: number;
|
|
46
|
+
advSecretKey: any;
|
|
47
|
+
processedHistoryMessages: any[];
|
|
48
|
+
nextPreKeyId: number;
|
|
49
|
+
firstUnuploadedPreKeyId: number;
|
|
50
|
+
accountSyncCounter: number;
|
|
51
|
+
accountSettings: {
|
|
52
|
+
unarchiveChats: boolean;
|
|
53
|
+
};
|
|
54
|
+
registered: boolean;
|
|
55
|
+
pairingCode: any;
|
|
56
|
+
lastPropHash: any;
|
|
57
|
+
routingInfo: any;
|
|
58
|
+
additionalData: any;
|
|
59
|
+
};
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import NodeCache from '@cacheable/node-cache';
|
|
2
|
+
import { Boom } from '@hapi/boom';
|
|
3
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
4
|
+
import { Mutex } from 'async-mutex';
|
|
5
|
+
import { randomBytes } from 'crypto';
|
|
6
|
+
import PQueue from 'p-queue';
|
|
7
|
+
import { DEFAULT_CACHE_TTLS } from '../Defaults/index.js';
|
|
8
|
+
import { Curve, signedKeyPair } from './crypto.js';
|
|
9
|
+
import { delay, generateRegistrationId } from './generics.js';
|
|
10
|
+
import { PreKeyManager } from './pre-key-manager.js';
|
|
11
|
+
/**
|
|
12
|
+
* Adds caching capability to a SignalKeyStore
|
|
13
|
+
* @param store the store to add caching to
|
|
14
|
+
* @param logger to log trace events
|
|
15
|
+
* @param _cache cache store to use
|
|
16
|
+
*/
|
|
17
|
+
export function makeCacheableSignalKeyStore(store, logger, _cache) {
|
|
18
|
+
const cache = _cache ||
|
|
19
|
+
new NodeCache({
|
|
20
|
+
stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes
|
|
21
|
+
useClones: false,
|
|
22
|
+
deleteOnExpire: true
|
|
23
|
+
});
|
|
24
|
+
// Mutex for protecting cache operations
|
|
25
|
+
const cacheMutex = new Mutex();
|
|
26
|
+
function getUniqueId(type, id) {
|
|
27
|
+
return `${type}.${id}`;
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
async get(type, ids) {
|
|
31
|
+
return cacheMutex.runExclusive(async () => {
|
|
32
|
+
const data = {};
|
|
33
|
+
const idsToFetch = [];
|
|
34
|
+
for (const id of ids) {
|
|
35
|
+
const item = (await cache.get(getUniqueId(type, id)));
|
|
36
|
+
if (typeof item !== 'undefined') {
|
|
37
|
+
data[id] = item;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
idsToFetch.push(id);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (idsToFetch.length) {
|
|
44
|
+
logger?.trace({ items: idsToFetch.length }, 'loading from store');
|
|
45
|
+
const fetched = await store.get(type, idsToFetch);
|
|
46
|
+
for (const id of idsToFetch) {
|
|
47
|
+
const item = fetched[id];
|
|
48
|
+
if (item) {
|
|
49
|
+
data[id] = item;
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
51
|
+
await cache.set(getUniqueId(type, id), item);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return data;
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
async set(data) {
|
|
59
|
+
return cacheMutex.runExclusive(async () => {
|
|
60
|
+
let keys = 0;
|
|
61
|
+
for (const type in data) {
|
|
62
|
+
for (const id in data[type]) {
|
|
63
|
+
await cache.set(getUniqueId(type, id), data[type][id]);
|
|
64
|
+
keys += 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
logger?.trace({ keys }, 'updated cache');
|
|
68
|
+
await store.set(data);
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
async *list(type) {
|
|
72
|
+
if (typeof store.list !== 'function')
|
|
73
|
+
throw new Error('SignalKeyStore.list is not implemented by the wrapped store');
|
|
74
|
+
yield* store.list(type);
|
|
75
|
+
},
|
|
76
|
+
async *listIds(type) {
|
|
77
|
+
if (typeof store.listIds === 'function') {
|
|
78
|
+
yield* store.listIds(type);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (typeof store.list === 'function') {
|
|
82
|
+
for await (const [id] of store.list(type))
|
|
83
|
+
yield id;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
throw new Error('SignalKeyStore.listIds is not implemented by the wrapped store');
|
|
87
|
+
},
|
|
88
|
+
async clear() {
|
|
89
|
+
await cache.flushAll();
|
|
90
|
+
await store.clear?.();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Adds DB-like transaction capability to the SignalKeyStore
|
|
96
|
+
* Uses AsyncLocalStorage for automatic context management
|
|
97
|
+
* @param state the key store to apply this capability to
|
|
98
|
+
* @param logger logger to log events
|
|
99
|
+
* @returns SignalKeyStore with transaction capability
|
|
100
|
+
*/
|
|
101
|
+
export const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetweenTriesMs }) => {
|
|
102
|
+
const txStorage = new AsyncLocalStorage();
|
|
103
|
+
// Queues for concurrency control (keyed by signal data type - bounded set)
|
|
104
|
+
const keyQueues = new Map();
|
|
105
|
+
// Transaction mutexes with reference counting for cleanup
|
|
106
|
+
const txMutexes = new Map();
|
|
107
|
+
const txMutexRefCounts = new Map();
|
|
108
|
+
// Pre-key manager for specialized operations
|
|
109
|
+
const preKeyManager = new PreKeyManager(state, logger);
|
|
110
|
+
/**
|
|
111
|
+
* Get or create a queue for a specific key type
|
|
112
|
+
*/
|
|
113
|
+
function getQueue(key) {
|
|
114
|
+
if (!keyQueues.has(key)) {
|
|
115
|
+
keyQueues.set(key, new PQueue({ concurrency: 1 }));
|
|
116
|
+
}
|
|
117
|
+
return keyQueues.get(key);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Get or create a transaction mutex
|
|
121
|
+
*/
|
|
122
|
+
function getTxMutex(key) {
|
|
123
|
+
if (!txMutexes.has(key)) {
|
|
124
|
+
txMutexes.set(key, new Mutex());
|
|
125
|
+
txMutexRefCounts.set(key, 0);
|
|
126
|
+
}
|
|
127
|
+
return txMutexes.get(key);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Acquire a reference to a transaction mutex
|
|
131
|
+
*/
|
|
132
|
+
function acquireTxMutexRef(key) {
|
|
133
|
+
const count = txMutexRefCounts.get(key) ?? 0;
|
|
134
|
+
txMutexRefCounts.set(key, count + 1);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Release a reference to a transaction mutex and cleanup if no longer needed
|
|
138
|
+
*/
|
|
139
|
+
function releaseTxMutexRef(key) {
|
|
140
|
+
const count = (txMutexRefCounts.get(key) ?? 1) - 1;
|
|
141
|
+
txMutexRefCounts.set(key, count);
|
|
142
|
+
// Cleanup if no more references and mutex is not locked
|
|
143
|
+
if (count <= 0) {
|
|
144
|
+
const mutex = txMutexes.get(key);
|
|
145
|
+
if (mutex && !mutex.isLocked()) {
|
|
146
|
+
txMutexes.delete(key);
|
|
147
|
+
txMutexRefCounts.delete(key);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Check if currently in a transaction
|
|
153
|
+
*/
|
|
154
|
+
function isInTransaction() {
|
|
155
|
+
return !!txStorage.getStore();
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* acquire every per-type queue in `types` — held
|
|
159
|
+
* simultaneously for the duration of `fn()` — via nested PQueue.add()
|
|
160
|
+
* calls in a fixed sorted order (prevents deadlock between overlapping
|
|
161
|
+
* commits sharing a type; a job never completes, and so never releases
|
|
162
|
+
* its queue slot, until every nested queue below it has too).
|
|
163
|
+
*/
|
|
164
|
+
function withTypeQueues(types, fn) {
|
|
165
|
+
const sorted = [...types].sort();
|
|
166
|
+
const run = (idx) => (idx >= sorted.length ? fn() : getQueue(sorted[idx]).add(() => run(idx + 1)));
|
|
167
|
+
return run(0);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Commit transaction with retries
|
|
171
|
+
*/
|
|
172
|
+
async function commitWithRetry(mutations) {
|
|
173
|
+
const types = Object.keys(mutations);
|
|
174
|
+
if (types.length === 0) {
|
|
175
|
+
logger.trace('no mutations in transaction');
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
logger.trace('committing transaction');
|
|
179
|
+
for (let attempt = 0; attempt < maxCommitRetries; attempt++) {
|
|
180
|
+
try {
|
|
181
|
+
await withTypeQueues(types, () => state.set(mutations));
|
|
182
|
+
logger.trace({ mutationCount: Object.keys(mutations).length }, 'committed transaction');
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
const retriesLeft = maxCommitRetries - attempt - 1;
|
|
187
|
+
logger.warn(`failed to commit mutations, retries left=${retriesLeft}`);
|
|
188
|
+
if (retriesLeft === 0) {
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
await delay(delayBetweenTriesMs);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
get: async (type, ids) => {
|
|
197
|
+
const ctx = txStorage.getStore();
|
|
198
|
+
if (!ctx) {
|
|
199
|
+
// No transaction - direct read without exclusive lock for concurrency
|
|
200
|
+
return state.get(type, ids);
|
|
201
|
+
}
|
|
202
|
+
// In transaction - check cache first
|
|
203
|
+
const cached = ctx.cache[type] || {};
|
|
204
|
+
const missing = ids.filter(id => !(id in cached));
|
|
205
|
+
if (missing.length > 0) {
|
|
206
|
+
ctx.dbQueries++;
|
|
207
|
+
logger.trace({ type, count: missing.length }, 'fetching missing keys in transaction');
|
|
208
|
+
const fetched = await getTxMutex(type).runExclusive(() => state.get(type, missing));
|
|
209
|
+
// Update cache
|
|
210
|
+
ctx.cache[type] = ctx.cache[type] || {};
|
|
211
|
+
Object.assign(ctx.cache[type], fetched);
|
|
212
|
+
}
|
|
213
|
+
// Return requested ids from cache
|
|
214
|
+
const result = {};
|
|
215
|
+
for (const id of ids) {
|
|
216
|
+
const value = ctx.cache[type]?.[id];
|
|
217
|
+
if (value !== undefined && value !== null) {
|
|
218
|
+
result[id] = value;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
},
|
|
223
|
+
set: async (data) => {
|
|
224
|
+
const ctx = txStorage.getStore();
|
|
225
|
+
if (!ctx) {
|
|
226
|
+
// No transaction - direct write with queue protection
|
|
227
|
+
const types = Object.keys(data);
|
|
228
|
+
await Promise.all(types.map(type => getQueue(type).add(async () => {
|
|
229
|
+
if (type === 'pre-key') {
|
|
230
|
+
await preKeyManager.validateDeletions(data, type);
|
|
231
|
+
}
|
|
232
|
+
const typeData = { [type]: data[type] };
|
|
233
|
+
await state.set(typeData);
|
|
234
|
+
})));
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
// In transaction - update cache and mutations
|
|
238
|
+
logger.trace({ types: Object.keys(data) }, 'caching in transaction');
|
|
239
|
+
for (const key_ in data) {
|
|
240
|
+
const key = key_;
|
|
241
|
+
// Ensure structures exist
|
|
242
|
+
ctx.cache[key] = ctx.cache[key] || {};
|
|
243
|
+
ctx.mutations[key] = ctx.mutations[key] || {};
|
|
244
|
+
// Special handling for pre-keys
|
|
245
|
+
if (key === 'pre-key') {
|
|
246
|
+
await preKeyManager.processOperations(data, key, ctx.cache, ctx.mutations, true);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
// Normal key types
|
|
250
|
+
Object.assign(ctx.cache[key], data[key]);
|
|
251
|
+
Object.assign(ctx.mutations[key], data[key]);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
isInTransaction,
|
|
256
|
+
transaction: async (work, key) => {
|
|
257
|
+
const existing = txStorage.getStore();
|
|
258
|
+
// Nested transaction - reuse existing context
|
|
259
|
+
if (existing) {
|
|
260
|
+
logger.trace('reusing existing transaction context');
|
|
261
|
+
return work();
|
|
262
|
+
}
|
|
263
|
+
// New transaction - acquire mutex and create context
|
|
264
|
+
const mutex = getTxMutex(key);
|
|
265
|
+
acquireTxMutexRef(key);
|
|
266
|
+
try {
|
|
267
|
+
return await mutex.runExclusive(async () => {
|
|
268
|
+
const ctx = {
|
|
269
|
+
cache: {},
|
|
270
|
+
mutations: {},
|
|
271
|
+
dbQueries: 0
|
|
272
|
+
};
|
|
273
|
+
logger.trace('entering transaction');
|
|
274
|
+
try {
|
|
275
|
+
const result = await txStorage.run(ctx, work);
|
|
276
|
+
// Commit mutations
|
|
277
|
+
await commitWithRetry(ctx.mutations);
|
|
278
|
+
logger.trace({ dbQueries: ctx.dbQueries }, 'transaction completed');
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
logger.error({ error }, 'transaction failed, rolling back');
|
|
283
|
+
throw error;
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
finally {
|
|
288
|
+
releaseTxMutexRef(key);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* Returns the authenticated user's JID, or throws a Boom-401 if creds are not yet authenticated.
|
|
295
|
+
* Use this anywhere we'd otherwise reach for `creds.me!.id` to fail fast with a descriptive error.
|
|
296
|
+
*/
|
|
297
|
+
export const assertMeId = (creds) => {
|
|
298
|
+
const id = creds.me?.id;
|
|
299
|
+
if (!id) {
|
|
300
|
+
throw new Boom('Cannot proceed: socket is not authenticated yet (creds.me.id is missing)', { statusCode: 401 });
|
|
301
|
+
}
|
|
302
|
+
return id;
|
|
303
|
+
};
|
|
304
|
+
export const initAuthCreds = () => {
|
|
305
|
+
const identityKey = Curve.generateKeyPair();
|
|
306
|
+
return {
|
|
307
|
+
noiseKey: Curve.generateKeyPair(),
|
|
308
|
+
pairingEphemeralKeyPair: Curve.generateKeyPair(),
|
|
309
|
+
signedIdentityKey: identityKey,
|
|
310
|
+
signedPreKey: signedKeyPair(identityKey, 1),
|
|
311
|
+
registrationId: generateRegistrationId(),
|
|
312
|
+
advSecretKey: randomBytes(32).toString('base64'),
|
|
313
|
+
processedHistoryMessages: [],
|
|
314
|
+
nextPreKeyId: 1,
|
|
315
|
+
firstUnuploadedPreKeyId: 1,
|
|
316
|
+
accountSyncCounter: 0,
|
|
317
|
+
accountSettings: {
|
|
318
|
+
unarchiveChats: false
|
|
319
|
+
},
|
|
320
|
+
registered: false,
|
|
321
|
+
pairingCode: undefined,
|
|
322
|
+
lastPropHash: undefined,
|
|
323
|
+
routingInfo: undefined,
|
|
324
|
+
additionalData: undefined
|
|
325
|
+
};
|
|
326
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function autoReconnect(socketFactory: any, options?: {}): {
|
|
2
|
+
start: () => Promise<any>;
|
|
3
|
+
stop: () => Promise<void>;
|
|
4
|
+
/** The live socket (replaced on every reconnect); null before start(). */
|
|
5
|
+
readonly socket: any;
|
|
6
|
+
/** Consecutive failed attempts since the last successful open. */
|
|
7
|
+
readonly attempts: number;
|
|
8
|
+
};
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Vanz@Port (v2.0.3) --- ported from @japofc/baileys 2.4.1 (MIT, author JAPofc / J.AP), lib/Utils/auto-reconnect.js. See NOTICE.md.
|
|
2
|
+
/**
|
|
3
|
+
* JAP@Add --- drop-in auto-reconnect for direct makeWASocket users.
|
|
4
|
+
*
|
|
5
|
+
* The Framework Bot has always had exponential-backoff reconnect built in;
|
|
6
|
+
* plain-socket users had to hand-roll the same connection.update dance every
|
|
7
|
+
* project (the #1 beginner Baileys question). This wraps it once, correctly:
|
|
8
|
+
*
|
|
9
|
+
* const manager = autoReconnect(() => makeWASocketAuto({ auth: state }), {
|
|
10
|
+
* onSocket: (sock) => sock.ev.on('messages.upsert', handler),
|
|
11
|
+
* });
|
|
12
|
+
* await manager.start();
|
|
13
|
+
*
|
|
14
|
+
* - exponential backoff with jitter (1s base -> 30s cap)
|
|
15
|
+
* - never reconnects on DisconnectReason.loggedOut (session is dead; caller
|
|
16
|
+
* gets onLoggedOut to clean up creds)
|
|
17
|
+
* - restartRequired (post-pairing) reconnects immediately, not backed off
|
|
18
|
+
* - stop() cancels timers and closes the live socket
|
|
19
|
+
* - factory may be async (works with makeWASocketAuto) or sync
|
|
20
|
+
*
|
|
21
|
+
* @author J.AP
|
|
22
|
+
*/
|
|
23
|
+
import { DisconnectReason } from '../Types/index.js';
|
|
24
|
+
import { isFatalDisconnect, mapDisconnectReason } from './disconnect-reason.js';
|
|
25
|
+
import defaultLogger from './logger.js';
|
|
26
|
+
|
|
27
|
+
export const autoReconnect = (socketFactory, options = {}) => {
|
|
28
|
+
const {
|
|
29
|
+
onSocket,
|
|
30
|
+
onOpen,
|
|
31
|
+
onLoggedOut,
|
|
32
|
+
onFatal,
|
|
33
|
+
maxAttempts = Infinity,
|
|
34
|
+
baseDelayMs = 1000,
|
|
35
|
+
maxDelayMs = 30000,
|
|
36
|
+
jitter = 0.25,
|
|
37
|
+
logger = defaultLogger.child({ module: 'auto-reconnect' }),
|
|
38
|
+
} = options;
|
|
39
|
+
|
|
40
|
+
let sock = null;
|
|
41
|
+
let attempts = 0;
|
|
42
|
+
let timer = null;
|
|
43
|
+
let stopped = false;
|
|
44
|
+
let started = false;
|
|
45
|
+
|
|
46
|
+
const delayFor = (attempt) => {
|
|
47
|
+
const exp = Math.min(maxDelayMs, baseDelayMs * Math.pow(2, attempt - 1));
|
|
48
|
+
const wiggle = exp * jitter * (Math.random() * 2 - 1);
|
|
49
|
+
return Math.max(0, Math.round(exp + wiggle));
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const scheduleRestart = (delay) => {
|
|
53
|
+
if (stopped || timer) {
|
|
54
|
+
return; // never two pending reconnects at once
|
|
55
|
+
}
|
|
56
|
+
timer = setTimeout(() => {
|
|
57
|
+
timer = null;
|
|
58
|
+
connect().catch((err) => logger.error({ err }, 'reconnect attempt failed'));
|
|
59
|
+
}, delay);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const connect = async () => {
|
|
63
|
+
if (stopped) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
// JAP@Fix (lifecycle): best-effort cleanup of the previous socket so a
|
|
67
|
+
// half-open ws never lingers next to its replacement.
|
|
68
|
+
const previous = sock;
|
|
69
|
+
if (previous) {
|
|
70
|
+
try {
|
|
71
|
+
await previous.end?.();
|
|
72
|
+
}
|
|
73
|
+
catch { /* already dead */ }
|
|
74
|
+
}
|
|
75
|
+
let current;
|
|
76
|
+
try {
|
|
77
|
+
current = await socketFactory();
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
// JAP@Fix (lifecycle): a throwing factory (e.g. makeWASocketAuto's
|
|
81
|
+
// version fetch while offline) used to kill the chain permanently.
|
|
82
|
+
// Treat it like a failed connection: back off and try again.
|
|
83
|
+
attempts += 1;
|
|
84
|
+
if (attempts > maxAttempts) {
|
|
85
|
+
logger.error({ err, attempts: attempts - 1 }, 'socket factory failed and max attempts reached — giving up');
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const delay = delayFor(attempts);
|
|
89
|
+
logger.warn({ err, attempt: attempts, delay }, 'socket factory failed — retrying');
|
|
90
|
+
scheduleRestart(delay);
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
sock = current;
|
|
94
|
+
// JAP@Fix (race): a 'close' may be emitted more than once by the same
|
|
95
|
+
// socket, and a stale (already-replaced) socket can emit late events.
|
|
96
|
+
// Without these guards a double-close schedules two parallel reconnect
|
|
97
|
+
// chains, and every stale close spawns a ghost socket.
|
|
98
|
+
let closeHandled = false;
|
|
99
|
+
try {
|
|
100
|
+
onSocket?.(current);
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
logger.warn({ err }, 'onSocket handler threw');
|
|
104
|
+
}
|
|
105
|
+
current.ev.on('connection.update', (update) => {
|
|
106
|
+
if (sock !== current) {
|
|
107
|
+
return; // stale socket — a newer one has replaced it
|
|
108
|
+
}
|
|
109
|
+
const { connection, lastDisconnect } = update;
|
|
110
|
+
if (connection === 'open') {
|
|
111
|
+
attempts = 0;
|
|
112
|
+
try {
|
|
113
|
+
onOpen?.(current);
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
logger.warn({ err }, 'onOpen handler threw');
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (connection !== 'close' || stopped || closeHandled) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
closeHandled = true;
|
|
124
|
+
const statusCode = lastDisconnect?.error?.output?.statusCode;
|
|
125
|
+
if (statusCode === DisconnectReason.loggedOut) {
|
|
126
|
+
logger.info('session logged out — not reconnecting');
|
|
127
|
+
try {
|
|
128
|
+
onLoggedOut?.(lastDisconnect?.error);
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
logger.warn({ err }, 'onLoggedOut handler threw');
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (isFatalDisconnect(mapDisconnectReason(statusCode))) {
|
|
136
|
+
logger.warn({ statusCode }, 'fatal disconnect — not reconnecting');
|
|
137
|
+
try {
|
|
138
|
+
onFatal?.(lastDisconnect?.error, statusCode);
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
logger.warn({ err }, 'onFatal handler threw');
|
|
142
|
+
}
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
attempts += 1;
|
|
146
|
+
if (attempts > maxAttempts) {
|
|
147
|
+
logger.warn({ attempts: attempts - 1 }, 'max reconnect attempts reached — giving up');
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
// JAP@Fix (lifecycle): restartRequired always reconnected with
|
|
151
|
+
// delay 0. Correct after pairing (single event), but when the
|
|
152
|
+
// server answers restartRequired on EVERY connect it becomes a
|
|
153
|
+
// tight spin loop (repro: 165 sockets in 200ms). Only the FIRST
|
|
154
|
+
// consecutive restartRequired is immediate; repeats back off.
|
|
155
|
+
const immediate = statusCode === DisconnectReason.restartRequired && attempts === 1;
|
|
156
|
+
const delay = immediate ? 0 : delayFor(attempts);
|
|
157
|
+
logger.info({ attempt: attempts, delay, statusCode }, 'scheduling reconnect');
|
|
158
|
+
scheduleRestart(delay);
|
|
159
|
+
});
|
|
160
|
+
return current;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// JAP@Fix (lifecycle): start() twice used to run two parallel reconnect
|
|
164
|
+
// chains forever (repro: 2 sockets immediately, 3 after one close storm).
|
|
165
|
+
// The public start() is now idempotent; connect() is the internal step.
|
|
166
|
+
const start = async () => {
|
|
167
|
+
if (started && !stopped) {
|
|
168
|
+
logger.warn('start() called while already running — returning the live socket');
|
|
169
|
+
return sock;
|
|
170
|
+
}
|
|
171
|
+
// JAP@Fix (lifecycle): start() after stop() used to return the dead
|
|
172
|
+
// socket silently (started stayed true, stopped stayed true, connect()
|
|
173
|
+
// no-oped). A stopped manager can now be restarted cleanly.
|
|
174
|
+
stopped = false;
|
|
175
|
+
started = true;
|
|
176
|
+
attempts = 0;
|
|
177
|
+
return connect();
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const stop = async () => {
|
|
181
|
+
stopped = true;
|
|
182
|
+
if (timer) {
|
|
183
|
+
clearTimeout(timer);
|
|
184
|
+
timer = null;
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
await sock?.end?.();
|
|
188
|
+
}
|
|
189
|
+
catch { /* socket may already be dead */ }
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
start,
|
|
194
|
+
stop,
|
|
195
|
+
/** The live socket (replaced on every reconnect); null before start(). */
|
|
196
|
+
get socket() {
|
|
197
|
+
return sock;
|
|
198
|
+
},
|
|
199
|
+
/** Consecutive failed attempts since the last successful open. */
|
|
200
|
+
get attempts() {
|
|
201
|
+
return attempts;
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export class AutoReplyHandler {
|
|
2
|
+
constructor(sendMessage: any, sendPresence: any, options?: {});
|
|
3
|
+
rules: Map<any, any>;
|
|
4
|
+
cooldowns: Map<any, any>;
|
|
5
|
+
globalCooldown: Map<any, any>;
|
|
6
|
+
sendMessage: any;
|
|
7
|
+
sendPresence: any;
|
|
8
|
+
options: {
|
|
9
|
+
globalCooldown: any;
|
|
10
|
+
simulateTyping: any;
|
|
11
|
+
typingDuration: any;
|
|
12
|
+
multiMatch: any;
|
|
13
|
+
onReply: any;
|
|
14
|
+
onError: any;
|
|
15
|
+
};
|
|
16
|
+
generateId(): string;
|
|
17
|
+
/** Add a rule keyed by keywords, a regex pattern, or an exact-match string. */
|
|
18
|
+
addRule(rule: any): any;
|
|
19
|
+
removeRule(id: any): boolean;
|
|
20
|
+
getRules(): any[];
|
|
21
|
+
getRule(id: any): any;
|
|
22
|
+
setRuleActive(id: any, active: any): boolean;
|
|
23
|
+
clearRules(): void;
|
|
24
|
+
checkCooldown(ruleId: any, jid: any): boolean;
|
|
25
|
+
checkGlobalCooldown(jid: any): boolean;
|
|
26
|
+
setCooldown(ruleId: any, jid: any, cooldown: any): void;
|
|
27
|
+
matchRule(text: any, rule: any): any;
|
|
28
|
+
isJidAllowed(jid: any, rule: any): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Feed each incoming message through here (e.g. from `messages.upsert`).
|
|
31
|
+
* Returns true if a rule matched and a reply was sent.
|
|
32
|
+
*/
|
|
33
|
+
processMessage(message: any): Promise<boolean>;
|
|
34
|
+
}
|
|
35
|
+
export function createAutoReply(sendMessage: any, sendPresence: any, options: any): AutoReplyHandler;
|