zapo-js 0.1.1 → 0.1.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/README.md +8 -0
- package/dist/appstate/WaAppStateCrypto.js +18 -25
- package/dist/appstate/WaAppStateSyncClient.js +181 -114
- package/dist/appstate/WaAppStateSyncResponseParser.js +16 -5
- package/dist/appstate/constants.js +4 -3
- package/dist/appstate/utils.js +10 -30
- package/dist/auth/WaAuthClient.js +48 -55
- package/dist/auth/flow/WaAuthCredentialsFlow.js +21 -14
- package/dist/auth/index.js +1 -3
- package/dist/auth/pairing/WaPairingFlow.js +21 -23
- package/dist/auth/pairing/WaQrFlow.js +37 -24
- package/dist/client/WaClient.js +103 -276
- package/dist/client/WaClientFactory.js +227 -110
- package/dist/client/connection/WaConnectionManager.js +292 -0
- package/dist/client/connection/WaKeyShareCoordinator.js +63 -0
- package/dist/client/connection/WaReceiptQueue.js +51 -0
- package/dist/client/coordinators/WaAppStateMutationCoordinator.js +471 -0
- package/dist/client/coordinators/WaGroupCoordinator.js +27 -17
- package/dist/client/coordinators/WaIncomingNodeCoordinator.js +20 -27
- package/dist/client/coordinators/WaMessageDispatchCoordinator.js +231 -686
- package/dist/client/coordinators/WaRetryCoordinator.js +70 -37
- package/dist/client/dirty.js +35 -29
- package/dist/client/events/chat.js +4 -3
- package/dist/client/events/group.js +59 -36
- package/dist/client/history-sync.js +53 -63
- package/dist/client/incoming.js +23 -20
- package/dist/client/mailbox.js +8 -8
- package/dist/client/messages.js +4 -4
- package/dist/client/messaging/fanout.js +189 -0
- package/dist/client/messaging/key-protocol.js +130 -0
- package/dist/client/messaging/participants.js +191 -0
- package/dist/crypto/core/hkdf.js +3 -8
- package/dist/crypto/core/index.js +1 -4
- package/dist/crypto/core/keys.js +2 -3
- package/dist/crypto/core/primitives.js +12 -15
- package/dist/crypto/core/random.js +7 -26
- package/dist/crypto/curves/Ed25519.js +7 -8
- package/dist/crypto/curves/X25519.js +13 -16
- package/dist/crypto/index.js +0 -5
- package/dist/esm/appstate/WaAppStateCrypto.js +6 -13
- package/dist/esm/appstate/WaAppStateSyncClient.js +174 -107
- package/dist/esm/appstate/WaAppStateSyncResponseParser.js +17 -6
- package/dist/esm/appstate/constants.js +3 -2
- package/dist/esm/appstate/utils.js +8 -27
- package/dist/esm/auth/WaAuthClient.js +48 -55
- package/dist/esm/auth/flow/WaAuthCredentialsFlow.js +21 -14
- package/dist/esm/auth/index.js +0 -1
- package/dist/esm/auth/pairing/WaPairingFlow.js +14 -16
- package/dist/esm/auth/pairing/WaQrFlow.js +37 -24
- package/dist/esm/client/WaClient.js +103 -276
- package/dist/esm/client/WaClientFactory.js +227 -110
- package/dist/esm/client/connection/WaConnectionManager.js +288 -0
- package/dist/esm/client/connection/WaKeyShareCoordinator.js +59 -0
- package/dist/esm/client/connection/WaReceiptQueue.js +47 -0
- package/dist/esm/client/coordinators/WaAppStateMutationCoordinator.js +467 -0
- package/dist/esm/client/coordinators/WaGroupCoordinator.js +20 -10
- package/dist/esm/client/coordinators/WaIncomingNodeCoordinator.js +20 -27
- package/dist/esm/client/coordinators/WaMessageDispatchCoordinator.js +232 -687
- package/dist/esm/client/coordinators/WaRetryCoordinator.js +71 -38
- package/dist/esm/client/dirty.js +30 -24
- package/dist/esm/client/events/chat.js +4 -3
- package/dist/esm/client/events/group.js +50 -28
- package/dist/esm/client/history-sync.js +50 -60
- package/dist/esm/client/incoming.js +23 -20
- package/dist/esm/client/mailbox.js +8 -8
- package/dist/esm/client/messages.js +1 -1
- package/dist/esm/client/messaging/fanout.js +186 -0
- package/dist/esm/client/messaging/key-protocol.js +127 -0
- package/dist/esm/client/messaging/participants.js +188 -0
- package/dist/esm/crypto/core/hkdf.js +3 -8
- package/dist/esm/crypto/core/index.js +0 -1
- package/dist/esm/crypto/core/keys.js +2 -3
- package/dist/esm/crypto/core/primitives.js +12 -15
- package/dist/esm/crypto/core/random.js +6 -25
- package/dist/esm/crypto/curves/Ed25519.js +4 -5
- package/dist/esm/crypto/curves/X25519.js +10 -13
- package/dist/esm/crypto/index.js +0 -2
- package/dist/esm/infra/log/ConsoleLogger.js +18 -17
- package/dist/esm/infra/log/PinoLogger.js +15 -9
- package/dist/esm/infra/log/types.js +11 -1
- package/dist/esm/infra/perf/BoundedTaskQueue.js +13 -17
- package/dist/esm/media/WaMediaCrypto.js +2 -4
- package/dist/esm/media/WaMediaTransferClient.js +226 -58
- package/dist/esm/media/conn.js +10 -6
- package/dist/esm/media/constants.js +4 -1
- package/dist/esm/message/WaMessageClient.js +4 -13
- package/dist/esm/message/ack.js +6 -6
- package/dist/esm/message/addon-crypto.js +59 -0
- package/dist/esm/message/incoming.js +106 -111
- package/dist/esm/message/index.js +2 -0
- package/dist/esm/message/reporting-token.js +438 -0
- package/dist/esm/message/use-case-secret.js +49 -0
- package/dist/esm/protocol/appstate.js +58 -0
- package/dist/esm/protocol/constants.js +2 -1
- package/dist/esm/protocol/index.js +2 -10
- package/dist/esm/protocol/jid.js +63 -51
- package/dist/esm/protocol/media.js +3 -3
- package/dist/esm/protocol/nodes.js +2 -0
- package/dist/esm/protocol/usync.js +11 -0
- package/dist/esm/retry/index.js +1 -0
- package/dist/esm/retry/outbound.js +4 -5
- package/dist/esm/retry/parse.js +58 -76
- package/dist/esm/retry/replay.js +48 -49
- package/dist/esm/retry/tracker.js +56 -0
- package/dist/esm/signal/api/SignalDeviceSyncApi.js +249 -82
- package/dist/esm/signal/api/SignalDigestSyncApi.js +6 -1
- package/dist/esm/signal/api/SignalIdentitySyncApi.js +49 -34
- package/dist/esm/signal/api/SignalMissingPreKeysSyncApi.js +70 -62
- package/dist/esm/signal/api/SignalSessionSyncApi.js +23 -30
- package/dist/esm/signal/crypto/WaAdvSignature.js +3 -5
- package/dist/esm/signal/group/SenderKeyChain.js +28 -23
- package/dist/esm/signal/group/SenderKeyCodec.js +2 -4
- package/dist/esm/signal/group/SenderKeyManager.js +26 -16
- package/dist/esm/signal/index.js +1 -0
- package/dist/esm/signal/session/SignalProtocol.js +49 -14
- package/dist/esm/signal/session/SignalRatchet.js +24 -15
- package/dist/esm/signal/session/SignalSession.js +14 -9
- package/dist/esm/signal/session/resolver.js +186 -0
- package/dist/esm/signal/store/sqlite.js +16 -37
- package/dist/esm/store/createStore.js +16 -18
- package/dist/esm/store/noop.store.js +3 -6
- package/dist/esm/store/providers/memory/appstate.store.js +30 -6
- package/dist/esm/store/providers/memory/contact.store.js +5 -0
- package/dist/esm/store/providers/memory/device-list.store.js +3 -30
- package/dist/esm/store/providers/memory/message.store.js +11 -5
- package/dist/esm/store/providers/memory/participants.store.js +1 -8
- package/dist/esm/store/providers/memory/sender-key.store.js +5 -7
- package/dist/esm/store/providers/memory/signal.store.js +13 -1
- package/dist/esm/store/providers/memory/thread.store.js +5 -0
- package/dist/esm/store/providers/sqlite/appstate.store.js +82 -1
- package/dist/esm/store/providers/sqlite/connection.js +18 -13
- package/dist/esm/store/providers/sqlite/contact.store.js +31 -18
- package/dist/esm/store/providers/sqlite/device-list.store.js +7 -35
- package/dist/esm/store/providers/sqlite/message.store.js +45 -32
- package/dist/esm/store/providers/sqlite/migrations.js +1 -1
- package/dist/esm/store/providers/sqlite/participants.store.js +1 -9
- package/dist/esm/store/providers/sqlite/retry.store.js +8 -11
- package/dist/esm/store/providers/sqlite/sender-key.store.js +25 -30
- package/dist/esm/store/providers/sqlite/signal.store.js +104 -22
- package/dist/esm/store/providers/sqlite/table-names.js +107 -0
- package/dist/esm/store/providers/sqlite/thread.store.js +35 -22
- package/dist/esm/transport/WaComms.js +25 -23
- package/dist/esm/transport/WaWebSocket.js +115 -12
- package/dist/esm/transport/binary/decoder.js +4 -4
- package/dist/esm/transport/binary/encoder.js +12 -4
- package/dist/esm/transport/index.js +1 -0
- package/dist/esm/transport/keepalive/WaKeepAlive.js +2 -8
- package/dist/esm/transport/node/WaNodeOrchestrator.js +2 -4
- package/dist/esm/transport/node/WaNodeTransport.js +0 -3
- package/dist/esm/transport/node/builders/{accountSync.js → account-sync.js} +16 -36
- package/dist/esm/transport/node/builders/index.js +2 -1
- package/dist/esm/transport/node/builders/message.js +9 -0
- package/dist/esm/transport/node/builders/pairing.js +4 -5
- package/dist/esm/transport/node/builders/usync.js +41 -0
- package/dist/esm/transport/node/helpers.js +107 -5
- package/dist/esm/transport/node/usync.js +35 -0
- package/dist/esm/transport/noise/WaFrameCodec.js +48 -33
- package/dist/esm/transport/noise/WaNoiseCert.js +3 -6
- package/dist/esm/transport/noise/WaNoiseSession.js +17 -10
- package/dist/esm/transport/proxy.js +27 -0
- package/dist/esm/transport/stream/parse.js +13 -48
- package/dist/esm/util/bytes.js +50 -32
- package/dist/esm/util/coercion.js +6 -14
- package/dist/esm/util/primitives.js +39 -14
- package/dist/infra/log/ConsoleLogger.js +18 -17
- package/dist/infra/log/PinoLogger.js +15 -9
- package/dist/infra/log/types.js +12 -0
- package/dist/infra/perf/BoundedTaskQueue.js +13 -17
- package/dist/media/WaMediaCrypto.js +1 -3
- package/dist/media/WaMediaTransferClient.js +259 -58
- package/dist/media/conn.js +10 -6
- package/dist/media/constants.js +4 -1
- package/dist/message/WaMessageClient.js +5 -14
- package/dist/message/ack.js +6 -6
- package/dist/message/addon-crypto.js +65 -0
- package/dist/message/incoming.js +104 -109
- package/dist/message/index.js +2 -0
- package/dist/message/reporting-token.js +443 -0
- package/dist/message/use-case-secret.js +55 -0
- package/dist/protocol/appstate.js +59 -1
- package/dist/protocol/constants.js +7 -1
- package/dist/protocol/index.js +20 -42
- package/dist/protocol/jid.js +64 -51
- package/dist/protocol/media.js +3 -3
- package/dist/protocol/nodes.js +2 -0
- package/dist/protocol/usync.js +14 -0
- package/dist/retry/index.js +3 -1
- package/dist/retry/outbound.js +6 -7
- package/dist/retry/parse.js +57 -75
- package/dist/retry/replay.js +46 -47
- package/dist/retry/tracker.js +59 -0
- package/dist/signal/api/SignalDeviceSyncApi.js +247 -80
- package/dist/signal/api/SignalDigestSyncApi.js +6 -1
- package/dist/signal/api/SignalIdentitySyncApi.js +49 -34
- package/dist/signal/api/SignalMissingPreKeysSyncApi.js +67 -59
- package/dist/signal/api/SignalSessionSyncApi.js +23 -30
- package/dist/signal/crypto/WaAdvSignature.js +2 -4
- package/dist/signal/group/SenderKeyChain.js +27 -22
- package/dist/signal/group/SenderKeyCodec.js +1 -3
- package/dist/signal/group/SenderKeyManager.js +26 -16
- package/dist/signal/index.js +3 -1
- package/dist/signal/session/SignalProtocol.js +49 -14
- package/dist/signal/session/SignalRatchet.js +24 -15
- package/dist/signal/session/SignalSession.js +14 -9
- package/dist/signal/session/resolver.js +189 -0
- package/dist/signal/store/sqlite.js +16 -37
- package/dist/store/createStore.js +16 -18
- package/dist/store/noop.store.js +3 -6
- package/dist/store/providers/memory/appstate.store.js +28 -4
- package/dist/store/providers/memory/contact.store.js +5 -0
- package/dist/store/providers/memory/device-list.store.js +3 -30
- package/dist/store/providers/memory/message.store.js +11 -5
- package/dist/store/providers/memory/participants.store.js +1 -8
- package/dist/store/providers/memory/sender-key.store.js +8 -10
- package/dist/store/providers/memory/signal.store.js +21 -9
- package/dist/store/providers/memory/thread.store.js +5 -0
- package/dist/store/providers/sqlite/appstate.store.js +81 -0
- package/dist/store/providers/sqlite/connection.js +18 -13
- package/dist/store/providers/sqlite/contact.store.js +31 -18
- package/dist/store/providers/sqlite/device-list.store.js +7 -35
- package/dist/store/providers/sqlite/message.store.js +45 -32
- package/dist/store/providers/sqlite/migrations.js +1 -1
- package/dist/store/providers/sqlite/participants.store.js +1 -9
- package/dist/store/providers/sqlite/retry.store.js +8 -11
- package/dist/store/providers/sqlite/sender-key.store.js +24 -29
- package/dist/store/providers/sqlite/signal.store.js +105 -23
- package/dist/store/providers/sqlite/table-names.js +113 -0
- package/dist/store/providers/sqlite/thread.store.js +35 -22
- package/dist/transport/WaComms.js +27 -25
- package/dist/transport/WaWebSocket.js +148 -12
- package/dist/transport/binary/decoder.js +4 -4
- package/dist/transport/binary/encoder.js +12 -4
- package/dist/transport/index.js +7 -1
- package/dist/transport/keepalive/WaKeepAlive.js +1 -7
- package/dist/transport/node/WaNodeOrchestrator.js +2 -4
- package/dist/transport/node/WaNodeTransport.js +0 -3
- package/dist/transport/node/builders/{accountSync.js → account-sync.js} +15 -35
- package/dist/transport/node/builders/index.js +12 -9
- package/dist/transport/node/builders/message.js +9 -0
- package/dist/transport/node/builders/pairing.js +4 -5
- package/dist/transport/node/builders/usync.js +45 -0
- package/dist/transport/node/helpers.js +112 -4
- package/dist/transport/node/usync.js +38 -0
- package/dist/transport/noise/WaFrameCodec.js +47 -32
- package/dist/transport/noise/WaNoiseCert.js +5 -8
- package/dist/transport/noise/WaNoiseSession.js +17 -10
- package/dist/transport/proxy.js +34 -0
- package/dist/transport/stream/parse.js +17 -53
- package/dist/types/appstate/WaAppStateCrypto.d.ts +0 -1
- package/dist/types/appstate/WaAppStateSyncClient.d.ts +5 -2
- package/dist/types/appstate/constants.d.ts +1 -0
- package/dist/types/appstate/store/sqlite.d.ts +4 -18
- package/dist/types/appstate/utils.d.ts +0 -1
- package/dist/types/auth/WaAuthClient.d.ts +10 -12
- package/dist/types/auth/index.d.ts +0 -2
- package/dist/types/auth/pairing/WaQrFlow.d.ts +1 -1
- package/dist/types/auth/types.d.ts +6 -9
- package/dist/types/client/WaClient.d.ts +27 -25
- package/dist/types/client/WaClientFactory.d.ts +22 -23
- package/dist/types/client/connection/WaConnectionManager.d.ts +64 -0
- package/dist/types/client/connection/WaKeyShareCoordinator.d.ts +14 -0
- package/dist/types/client/connection/WaReceiptQueue.d.ts +13 -0
- package/dist/types/client/coordinators/WaAppStateMutationCoordinator.d.ts +46 -0
- package/dist/types/client/coordinators/WaIncomingNodeCoordinator.d.ts +0 -1
- package/dist/types/client/coordinators/WaMessageDispatchCoordinator.d.ts +18 -41
- package/dist/types/client/coordinators/WaRetryCoordinator.d.ts +2 -0
- package/dist/types/client/dirty.d.ts +1 -0
- package/dist/types/client/events/group.d.ts +2 -1
- package/dist/types/client/index.d.ts +1 -1
- package/dist/types/client/messaging/fanout.d.ts +14 -0
- package/dist/types/client/messaging/key-protocol.d.ts +18 -0
- package/dist/types/client/messaging/participants.d.ts +13 -0
- package/dist/types/client/types.d.ts +24 -1
- package/dist/types/crypto/core/hkdf.d.ts +0 -6
- package/dist/types/crypto/core/index.d.ts +0 -1
- package/dist/types/crypto/core/random.d.ts +1 -7
- package/dist/types/crypto/index.d.ts +0 -2
- package/dist/types/index.d.ts +1 -1
- package/dist/types/infra/log/ConsoleLogger.d.ts +2 -1
- package/dist/types/infra/log/PinoLogger.d.ts +1 -1
- package/dist/types/infra/log/types.d.ts +1 -0
- package/dist/types/infra/perf/BoundedTaskQueue.d.ts +1 -1
- package/dist/types/media/WaMediaTransferClient.d.ts +13 -3
- package/dist/types/media/types.d.ts +5 -0
- package/dist/types/message/addon-crypto.d.ts +25 -0
- package/dist/types/message/index.d.ts +2 -0
- package/dist/types/message/reporting-token.d.ts +19 -0
- package/dist/types/message/use-case-secret.d.ts +20 -0
- package/dist/types/protocol/appstate.d.ts +58 -0
- package/dist/types/protocol/constants.d.ts +2 -1
- package/dist/types/protocol/index.d.ts +2 -10
- package/dist/types/protocol/jid.d.ts +3 -3
- package/dist/types/protocol/nodes.d.ts +2 -0
- package/dist/types/protocol/usync.d.ts +11 -0
- package/dist/types/retry/index.d.ts +1 -0
- package/dist/types/retry/replay.d.ts +0 -4
- package/dist/types/retry/tracker.d.ts +19 -0
- package/dist/types/retry/types.d.ts +4 -3
- package/dist/types/signal/api/SignalDeviceSyncApi.d.ts +13 -1
- package/dist/types/signal/group/SenderKeyCodec.d.ts +4 -6
- package/dist/types/signal/index.d.ts +1 -0
- package/dist/types/signal/session/SignalProtocol.d.ts +9 -0
- package/dist/types/signal/session/resolver.d.ts +17 -0
- package/dist/types/store/contracts/appstate.store.d.ts +3 -0
- package/dist/types/store/contracts/contact.store.d.ts +1 -0
- package/dist/types/store/contracts/device-list.store.d.ts +0 -3
- package/dist/types/store/contracts/message.store.d.ts +1 -0
- package/dist/types/store/contracts/participants.store.d.ts +0 -1
- package/dist/types/store/contracts/sender-key.store.d.ts +0 -1
- package/dist/types/store/contracts/signal.store.d.ts +6 -0
- package/dist/types/store/contracts/thread.store.d.ts +1 -0
- package/dist/types/store/index.d.ts +1 -1
- package/dist/types/store/providers/memory/appstate.store.d.ts +2 -0
- package/dist/types/store/providers/memory/contact.store.d.ts +1 -0
- package/dist/types/store/providers/memory/device-list.store.d.ts +0 -3
- package/dist/types/store/providers/memory/message.store.d.ts +1 -0
- package/dist/types/store/providers/memory/participants.store.d.ts +0 -1
- package/dist/types/store/providers/memory/sender-key.store.d.ts +0 -1
- package/dist/types/store/providers/memory/signal.store.d.ts +6 -0
- package/dist/types/store/providers/memory/thread.store.d.ts +1 -0
- package/dist/types/store/providers/sqlite/appstate.store.d.ts +2 -0
- package/dist/types/store/providers/sqlite/contact.store.d.ts +2 -0
- package/dist/types/store/providers/sqlite/device-list.store.d.ts +0 -3
- package/dist/types/store/providers/sqlite/message.store.d.ts +2 -0
- package/dist/types/store/providers/sqlite/participants.store.d.ts +0 -1
- package/dist/types/store/providers/sqlite/retry.store.d.ts +0 -1
- package/dist/types/store/providers/sqlite/sender-key.store.d.ts +0 -1
- package/dist/types/store/providers/sqlite/signal.store.d.ts +7 -0
- package/dist/types/store/providers/sqlite/table-names.d.ts +5 -0
- package/dist/types/store/providers/sqlite/thread.store.d.ts +2 -0
- package/dist/types/store/types.d.ts +3 -0
- package/dist/types/transport/WaWebSocket.d.ts +3 -0
- package/dist/types/transport/index.d.ts +2 -1
- package/dist/types/transport/keepalive/WaKeepAlive.d.ts +0 -1
- package/dist/types/transport/node/WaNodeTransport.d.ts +0 -9
- package/dist/types/transport/node/builders/group.d.ts +4 -6
- package/dist/types/transport/node/builders/index.d.ts +2 -1
- package/dist/types/transport/node/builders/message.d.ts +14 -25
- package/dist/types/transport/node/builders/retry.d.ts +2 -4
- package/dist/types/transport/node/builders/usync.d.ts +21 -0
- package/dist/types/transport/node/helpers.d.ts +8 -0
- package/dist/types/transport/node/usync.d.ts +2 -0
- package/dist/types/transport/noise/WaFrameCodec.d.ts +3 -0
- package/dist/types/transport/noise/WaNoiseSession.d.ts +1 -0
- package/dist/types/transport/proxy.d.ts +6 -0
- package/dist/types/transport/stream/parse.d.ts +0 -1
- package/dist/types/transport/types.d.ts +18 -1
- package/dist/types/util/bytes.d.ts +5 -0
- package/dist/types/util/primitives.d.ts +3 -0
- package/dist/util/bytes.js +55 -33
- package/dist/util/coercion.js +6 -14
- package/dist/util/primitives.js +42 -14
- package/package.json +26 -5
- package/dist/crypto/core/encoding.js +0 -29
- package/dist/esm/crypto/core/encoding.js +0 -25
- package/dist/esm/util/base64.js +0 -18
- package/dist/esm/util/signal-address.js +0 -5
- package/dist/types/crypto/core/encoding.d.ts +0 -11
- package/dist/types/util/base64.d.ts +0 -4
- package/dist/types/util/signal-address.d.ts +0 -2
- package/dist/util/base64.js +0 -24
- package/dist/util/signal-address.js +0 -8
- /package/dist/types/transport/node/builders/{accountSync.d.ts → account-sync.d.ts} +0 -0
|
@@ -6,11 +6,10 @@ import { persistIncomingMailboxEntities } from './mailbox.js';
|
|
|
6
6
|
import { buildWaClientDependencies, resolveWaClientBase } from './WaClientFactory.js';
|
|
7
7
|
import { ConsoleLogger } from '../infra/log/ConsoleLogger.js';
|
|
8
8
|
import { proto } from '../proto.js';
|
|
9
|
-
import { WA_APP_STATE_COLLECTION_STATES, WA_DEFAULTS
|
|
10
|
-
import { normalizeDeviceJid, toUserJid } from '../protocol/jid.js';
|
|
9
|
+
import { WA_APP_STATE_COLLECTION_STATES, WA_DEFAULTS } from '../protocol/constants.js';
|
|
10
|
+
import { normalizeDeviceJid, parsePhoneJid, toUserJid } from '../protocol/jid.js';
|
|
11
11
|
import { queryWithContext as queryNodeWithContext } from '../transport/node/query.js';
|
|
12
|
-
import {
|
|
13
|
-
import { decodeProtoBytes } from '../util/base64.js';
|
|
12
|
+
import { decodeProtoBytes } from '../util/bytes.js';
|
|
14
13
|
import { bytesToHex } from '../util/bytes.js';
|
|
15
14
|
import { toError } from '../util/primitives.js';
|
|
16
15
|
const SYNC_RELATED_PROTOCOL_TYPES = new Set([
|
|
@@ -24,15 +23,7 @@ const WA_APP_STATE_KEY_SHARE_MAX_RETRIES = 2;
|
|
|
24
23
|
export class WaClient extends EventEmitter {
|
|
25
24
|
constructor(options, logger = new ConsoleLogger('info')) {
|
|
26
25
|
super();
|
|
27
|
-
this.clockSkewMs = null;
|
|
28
|
-
this.mediaConnCache = null;
|
|
29
|
-
this.comms = null;
|
|
30
|
-
this.pairingReconnectPromise = null;
|
|
31
26
|
this.connectPromise = null;
|
|
32
|
-
this.danglingReceipts = [];
|
|
33
|
-
this.appStateKeyShareWaiters = new Set();
|
|
34
|
-
this.appStateKeyShareVersion = 0;
|
|
35
|
-
this.appStateBootstrapKeyShareWaitDone = false;
|
|
36
27
|
const base = resolveWaClientBase(options, logger);
|
|
37
28
|
this.options = base.options;
|
|
38
29
|
this.logger = base.logger;
|
|
@@ -45,31 +36,20 @@ export class WaClient extends EventEmitter {
|
|
|
45
36
|
this.signalStore = base.sessionStore.signal;
|
|
46
37
|
this.senderKeyStore = base.sessionStore.senderKey;
|
|
47
38
|
this.threadStore = base.sessionStore.threads;
|
|
48
|
-
const host = {
|
|
49
|
-
sendNode: (node) => this.sendNode(node),
|
|
50
|
-
query: (node, timeoutMs) => this.query(node, timeoutMs),
|
|
51
|
-
queryWithContext: this.queryWithContext.bind(this),
|
|
52
|
-
syncAppState: () => this.syncAppState().then(() => { }),
|
|
53
|
-
emitEvent: this.emit.bind(this),
|
|
54
|
-
handleIncomingMessageEvent: this.handleIncomingMessageEvent.bind(this),
|
|
55
|
-
handleError: this.handleError.bind(this),
|
|
56
|
-
scheduleReconnectAfterPairing: this.scheduleReconnectAfterPairing.bind(this),
|
|
57
|
-
updateClockSkewFromSuccess: this.updateClockSkewFromSuccess.bind(this),
|
|
58
|
-
getComms: () => this.comms,
|
|
59
|
-
getMediaConnCache: () => this.mediaConnCache,
|
|
60
|
-
setMediaConnCache: (mediaConn) => {
|
|
61
|
-
this.mediaConnCache = mediaConn;
|
|
62
|
-
},
|
|
63
|
-
disconnect: this.disconnect.bind(this),
|
|
64
|
-
clearStoredState: this.clearStoredState.bind(this),
|
|
65
|
-
connect: this.connect.bind(this),
|
|
66
|
-
shouldQueueDanglingReceipt: (node, error) => this.shouldQueueDanglingReceipt(node, error),
|
|
67
|
-
enqueueDanglingReceipt: this.enqueueDanglingReceipt.bind(this),
|
|
68
|
-
takeDanglingReceipts: () => this.danglingReceipts.splice(0)
|
|
69
|
-
};
|
|
70
39
|
const dependencies = buildWaClientDependencies({
|
|
71
40
|
base,
|
|
72
|
-
|
|
41
|
+
runtime: {
|
|
42
|
+
sendNode: (node) => this.sendNode(node),
|
|
43
|
+
query: (node, timeoutMs) => this.query(node, timeoutMs),
|
|
44
|
+
queryWithContext: this.queryWithContext.bind(this),
|
|
45
|
+
syncAppState: () => this.syncAppState().then(() => { }),
|
|
46
|
+
syncAppStateWithOptions: (syncOptions) => this.syncAppState(syncOptions),
|
|
47
|
+
emitEvent: this.emit.bind(this),
|
|
48
|
+
handleIncomingMessageEvent: this.handleIncomingMessageEvent.bind(this),
|
|
49
|
+
handleError: this.handleError.bind(this),
|
|
50
|
+
handleIncomingFrame: this.handleIncomingFrame.bind(this),
|
|
51
|
+
clearStoredState: this.clearStoredState.bind(this)
|
|
52
|
+
}
|
|
73
53
|
});
|
|
74
54
|
Object.assign(this, dependencies);
|
|
75
55
|
this.bindNodeTransportEvents();
|
|
@@ -87,7 +67,7 @@ export class WaClient extends EventEmitter {
|
|
|
87
67
|
return super.emit(event, ...args);
|
|
88
68
|
}
|
|
89
69
|
getState() {
|
|
90
|
-
const connected = this.
|
|
70
|
+
const connected = this.connectionManager.isConnected();
|
|
91
71
|
this.logger.trace('wa client state requested', { connected });
|
|
92
72
|
return this.authClient.getState(connected);
|
|
93
73
|
}
|
|
@@ -95,7 +75,7 @@ export class WaClient extends EventEmitter {
|
|
|
95
75
|
return this.authClient.getCurrentCredentials();
|
|
96
76
|
}
|
|
97
77
|
getClockSkewMs() {
|
|
98
|
-
return this.
|
|
78
|
+
return this.connectionManager.getClockSkewMs();
|
|
99
79
|
}
|
|
100
80
|
async sendNode(node) {
|
|
101
81
|
try {
|
|
@@ -103,13 +83,13 @@ export class WaClient extends EventEmitter {
|
|
|
103
83
|
}
|
|
104
84
|
catch (error) {
|
|
105
85
|
const normalized = toError(error);
|
|
106
|
-
if (this.
|
|
107
|
-
this.
|
|
86
|
+
if (this.receiptQueue.shouldQueue(node, normalized)) {
|
|
87
|
+
this.receiptQueue.enqueue(node);
|
|
108
88
|
this.logger.warn('queued dangling receipt after send failure', {
|
|
109
89
|
id: node.attrs.id,
|
|
110
90
|
to: node.attrs.to,
|
|
111
91
|
message: normalized.message,
|
|
112
|
-
queueSize: this.
|
|
92
|
+
queueSize: this.receiptQueue.size()
|
|
113
93
|
});
|
|
114
94
|
return;
|
|
115
95
|
}
|
|
@@ -117,7 +97,7 @@ export class WaClient extends EventEmitter {
|
|
|
117
97
|
}
|
|
118
98
|
}
|
|
119
99
|
async query(node, timeoutMs = this.options.iqTimeoutMs ?? WA_DEFAULTS.IQ_TIMEOUT_MS) {
|
|
120
|
-
if (!this.
|
|
100
|
+
if (!this.connectionManager.isConnected()) {
|
|
121
101
|
throw new Error('client is not connected');
|
|
122
102
|
}
|
|
123
103
|
this.logger.debug('wa client query', { tag: node.tag, id: node.attrs.id, timeoutMs });
|
|
@@ -139,12 +119,17 @@ export class WaClient extends EventEmitter {
|
|
|
139
119
|
this.handleError(error);
|
|
140
120
|
});
|
|
141
121
|
}
|
|
122
|
+
getMailboxPersistenceDeps() {
|
|
123
|
+
return {
|
|
124
|
+
logger: this.logger,
|
|
125
|
+
contactStore: this.contactStore,
|
|
126
|
+
messageStore: this.messageStore
|
|
127
|
+
};
|
|
128
|
+
}
|
|
142
129
|
async handleIncomingMessageEvent(event) {
|
|
143
130
|
this.emit('message', event);
|
|
144
131
|
void persistIncomingMailboxEntities({
|
|
145
|
-
|
|
146
|
-
contactStore: this.contactStore,
|
|
147
|
-
messageStore: this.messageStore,
|
|
132
|
+
...this.getMailboxPersistenceDeps(),
|
|
148
133
|
event
|
|
149
134
|
});
|
|
150
135
|
const protocolMessage = event.message?.protocolMessage;
|
|
@@ -209,9 +194,8 @@ export class WaClient extends EventEmitter {
|
|
|
209
194
|
imported
|
|
210
195
|
});
|
|
211
196
|
if (imported > 0) {
|
|
212
|
-
const hadWaiters = this.
|
|
213
|
-
this.
|
|
214
|
-
this.notifyAppStateKeyShareWaiters(true);
|
|
197
|
+
const hadWaiters = this.keyShareCoordinator.hasWaiters();
|
|
198
|
+
this.keyShareCoordinator.notifyReceived();
|
|
215
199
|
if (hadWaiters) {
|
|
216
200
|
this.logger.debug('app-state key share imported and waiters released', {
|
|
217
201
|
id: event.stanzaId,
|
|
@@ -337,10 +321,8 @@ export class WaClient extends EventEmitter {
|
|
|
337
321
|
async handleHistorySyncNotification(notification) {
|
|
338
322
|
try {
|
|
339
323
|
await processHistorySyncNotification({
|
|
340
|
-
|
|
324
|
+
...this.getMailboxPersistenceDeps(),
|
|
341
325
|
mediaTransfer: this.mediaTransfer,
|
|
342
|
-
contactStore: this.contactStore,
|
|
343
|
-
messageStore: this.messageStore,
|
|
344
326
|
threadStore: this.threadStore,
|
|
345
327
|
emitEvent: this.emit.bind(this)
|
|
346
328
|
}, notification);
|
|
@@ -369,144 +351,98 @@ export class WaClient extends EventEmitter {
|
|
|
369
351
|
this.logger.trace('wa client connect already in-flight');
|
|
370
352
|
return this.connectPromise;
|
|
371
353
|
}
|
|
372
|
-
this.connectPromise = this.
|
|
354
|
+
this.connectPromise = this.connectionManager
|
|
355
|
+
.connect((frame) => this.handleIncomingFrame(frame))
|
|
356
|
+
.then(() => {
|
|
357
|
+
this.emit('connection_open', {});
|
|
358
|
+
})
|
|
359
|
+
.finally(() => {
|
|
373
360
|
this.connectPromise = null;
|
|
374
361
|
});
|
|
375
362
|
return this.connectPromise;
|
|
376
363
|
}
|
|
377
|
-
async connectInternal() {
|
|
378
|
-
if (this.comms) {
|
|
379
|
-
this.logger.trace('wa client connect skipped: comms already created');
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
this.logger.info('wa client connect start');
|
|
383
|
-
let credentials = await this.authClient.loadOrCreateCredentials();
|
|
384
|
-
try {
|
|
385
|
-
await this.startCommsWithCredentials(credentials);
|
|
386
|
-
}
|
|
387
|
-
catch (error) {
|
|
388
|
-
if (credentials.routingInfo) {
|
|
389
|
-
this.logger.warn('connect failed with routing info, retrying without routing info', {
|
|
390
|
-
message: toError(error).message
|
|
391
|
-
});
|
|
392
|
-
await this.disconnect();
|
|
393
|
-
credentials = await this.authClient.clearRoutingInfo();
|
|
394
|
-
await this.startCommsWithCredentials(credentials);
|
|
395
|
-
}
|
|
396
|
-
else {
|
|
397
|
-
await this.disconnect();
|
|
398
|
-
throw error;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
this.logger.info('wa client connected');
|
|
402
|
-
this.emit('connection_open', {});
|
|
403
|
-
}
|
|
404
|
-
scheduleReconnectAfterPairing() {
|
|
405
|
-
this.logger.debug('wa client scheduling reconnect after pairing');
|
|
406
|
-
setTimeout(() => {
|
|
407
|
-
void this.reconnectAsRegisteredAfterPairing().catch((error) => {
|
|
408
|
-
this.handleError(toError(error));
|
|
409
|
-
});
|
|
410
|
-
}, 0);
|
|
411
|
-
}
|
|
412
|
-
async reconnectAsRegisteredAfterPairing() {
|
|
413
|
-
if (this.pairingReconnectPromise) {
|
|
414
|
-
this.logger.trace('pairing reconnect already in-flight');
|
|
415
|
-
return this.pairingReconnectPromise;
|
|
416
|
-
}
|
|
417
|
-
this.pairingReconnectPromise = this.reconnectAsRegisteredAfterPairingInternal().finally(() => {
|
|
418
|
-
this.pairingReconnectPromise = null;
|
|
419
|
-
});
|
|
420
|
-
return this.pairingReconnectPromise;
|
|
421
|
-
}
|
|
422
|
-
async reconnectAsRegisteredAfterPairingInternal() {
|
|
423
|
-
const credentials = this.authClient.getCurrentCredentials();
|
|
424
|
-
if (!credentials?.meJid) {
|
|
425
|
-
this.logger.trace('pairing reconnect skipped: still unregistered');
|
|
426
|
-
return;
|
|
427
|
-
}
|
|
428
|
-
const currentComms = this.comms;
|
|
429
|
-
if (!currentComms) {
|
|
430
|
-
this.logger.trace('pairing reconnect skipped: no active comms');
|
|
431
|
-
return;
|
|
432
|
-
}
|
|
433
|
-
this.logger.info('pairing completed, restarting comms as registered');
|
|
434
|
-
this.keepAlive.stop();
|
|
435
|
-
this.nodeOrchestrator.clearPending(new Error('restarting comms after pairing'));
|
|
436
|
-
this.clearCommsBinding();
|
|
437
|
-
try {
|
|
438
|
-
await currentComms.stopComms();
|
|
439
|
-
}
|
|
440
|
-
catch (error) {
|
|
441
|
-
this.logger.warn('failed to stop pre-registration comms', {
|
|
442
|
-
message: toError(error).message
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
try {
|
|
446
|
-
await this.startCommsWithCredentials(credentials);
|
|
447
|
-
}
|
|
448
|
-
catch (error) {
|
|
449
|
-
this.logger.warn('pairing reconnect failed while starting registered comms', {
|
|
450
|
-
message: toError(error).message
|
|
451
|
-
});
|
|
452
|
-
throw error;
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
364
|
async disconnect() {
|
|
456
|
-
this.
|
|
457
|
-
this.
|
|
458
|
-
this.
|
|
459
|
-
this.appStateBootstrapKeyShareWaitDone = false;
|
|
460
|
-
await this.authClient.clearTransientState();
|
|
461
|
-
this.nodeOrchestrator.clearPending(new Error('client disconnected'));
|
|
462
|
-
this.clockSkewMs = null;
|
|
463
|
-
this.mediaConnCache = null;
|
|
464
|
-
this.passiveTasks.resetInFlightState();
|
|
465
|
-
const comms = this.comms;
|
|
466
|
-
this.clearCommsBinding();
|
|
467
|
-
if (comms) {
|
|
468
|
-
await comms.stopComms();
|
|
469
|
-
this.logger.info('wa client disconnected');
|
|
470
|
-
this.emit('connection_close', {});
|
|
471
|
-
}
|
|
365
|
+
this.keyShareCoordinator.notifyDisconnected();
|
|
366
|
+
await this.connectionManager.disconnect();
|
|
367
|
+
this.emit('connection_close', {});
|
|
472
368
|
}
|
|
473
369
|
async requestPairingCode(phoneNumber, shouldShowPushNotification = false) {
|
|
474
|
-
if (!this.
|
|
370
|
+
if (!this.connectionManager.isConnected() || !this.authClient.getCurrentCredentials()) {
|
|
475
371
|
throw new Error('client is not connected');
|
|
476
372
|
}
|
|
477
373
|
this.logger.debug('wa client request pairing code');
|
|
478
374
|
return this.authClient.requestPairingCode(phoneNumber, shouldShowPushNotification);
|
|
479
375
|
}
|
|
480
376
|
async fetchPairingCountryCodeIso() {
|
|
481
|
-
if (!this.
|
|
377
|
+
if (!this.connectionManager.isConnected() || !this.authClient.getCurrentCredentials()) {
|
|
482
378
|
throw new Error('client is not connected');
|
|
483
379
|
}
|
|
484
380
|
this.logger.trace('wa client fetch pairing country code iso');
|
|
485
381
|
return this.authClient.fetchPairingCountryCodeIso();
|
|
486
382
|
}
|
|
487
|
-
async
|
|
383
|
+
async getLidsByPhoneNumbers(phoneNumbers) {
|
|
384
|
+
if (!this.connectionManager.isConnected() || !this.authClient.getCurrentCredentials()) {
|
|
385
|
+
throw new Error('client is not connected');
|
|
386
|
+
}
|
|
387
|
+
const normalizedPhoneJids = phoneNumbers.map(parsePhoneJid);
|
|
388
|
+
this.logger.trace('wa client query lids by phone numbers', {
|
|
389
|
+
phones: normalizedPhoneJids.length
|
|
390
|
+
});
|
|
391
|
+
return this.signalDeviceSync.queryLidsByPhoneJids(normalizedPhoneJids);
|
|
392
|
+
}
|
|
393
|
+
sendMessage(to, content, options = {}) {
|
|
488
394
|
return this.messageDispatch.sendMessage(to, content, options);
|
|
489
395
|
}
|
|
490
|
-
|
|
491
|
-
|
|
396
|
+
syncSignalSession(jid, reasonIdentity = false) {
|
|
397
|
+
return this.messageDispatch.syncSignalSession(jid, reasonIdentity);
|
|
398
|
+
}
|
|
399
|
+
sendReceipt(input) {
|
|
400
|
+
return this.messageDispatch.sendReceipt(input);
|
|
401
|
+
}
|
|
402
|
+
setChatMute(chatJid, muted, muteEndTimestampMs) {
|
|
403
|
+
return this.appStateMutations.setChatMute(chatJid, muted, muteEndTimestampMs);
|
|
404
|
+
}
|
|
405
|
+
setChatRead(chatJid, read) {
|
|
406
|
+
return this.appStateMutations.setChatRead(chatJid, read);
|
|
407
|
+
}
|
|
408
|
+
setChatPin(chatJid, pinned) {
|
|
409
|
+
return this.appStateMutations.setChatPin(chatJid, pinned);
|
|
410
|
+
}
|
|
411
|
+
setChatArchive(chatJid, archived) {
|
|
412
|
+
return this.appStateMutations.setChatArchive(chatJid, archived);
|
|
492
413
|
}
|
|
493
|
-
|
|
494
|
-
|
|
414
|
+
clearChat(chatJid, options = {}) {
|
|
415
|
+
return this.appStateMutations.clearChat(chatJid, options);
|
|
416
|
+
}
|
|
417
|
+
deleteChat(chatJid, options = {}) {
|
|
418
|
+
return this.appStateMutations.deleteChat(chatJid, options);
|
|
419
|
+
}
|
|
420
|
+
setChatLock(chatJid, locked) {
|
|
421
|
+
return this.appStateMutations.setChatLock(chatJid, locked);
|
|
422
|
+
}
|
|
423
|
+
setMessageStar(message, starred) {
|
|
424
|
+
return this.appStateMutations.setMessageStar(message, starred);
|
|
425
|
+
}
|
|
426
|
+
deleteMessageForMe(message, options = {}) {
|
|
427
|
+
return this.appStateMutations.deleteMessageForMe(message, options);
|
|
428
|
+
}
|
|
429
|
+
flushAppStateMutations() {
|
|
430
|
+
return this.appStateMutations.flushMutations();
|
|
495
431
|
}
|
|
496
432
|
async exportAppState() {
|
|
497
433
|
return this.appStateSync.exportState();
|
|
498
434
|
}
|
|
499
435
|
async syncAppState(options = {}) {
|
|
500
|
-
if (!this.
|
|
436
|
+
if (!this.connectionManager.isConnected()) {
|
|
501
437
|
throw new Error('client is not connected');
|
|
502
438
|
}
|
|
503
439
|
const shouldWaitForKeyShare = (await this.appStateStore.getActiveSyncKey()) === null;
|
|
504
|
-
if (shouldWaitForKeyShare && !this.
|
|
505
|
-
this.
|
|
440
|
+
if (shouldWaitForKeyShare && !this.keyShareCoordinator.isBootstrapDone()) {
|
|
441
|
+
this.keyShareCoordinator.markBootstrapDone();
|
|
506
442
|
this.logger.info('app-state bootstrap pre-sync waiting for key share', {
|
|
507
443
|
timeoutMs: WA_APP_STATE_KEY_SHARE_WAIT_TIMEOUT_MS
|
|
508
444
|
});
|
|
509
|
-
const received = await this.
|
|
445
|
+
const received = await this.keyShareCoordinator.waitForShare(WA_APP_STATE_KEY_SHARE_WAIT_TIMEOUT_MS);
|
|
510
446
|
if (received) {
|
|
511
447
|
this.logger.info('app-state bootstrap pre-sync received key share, continuing sync');
|
|
512
448
|
}
|
|
@@ -523,17 +459,16 @@ export class WaClient extends EventEmitter {
|
|
|
523
459
|
return syncResult;
|
|
524
460
|
}
|
|
525
461
|
let retryCount = 0;
|
|
526
|
-
let observedKeyShareVersion = this.
|
|
527
|
-
while (blockedCollections.length > 0 &&
|
|
528
|
-
|
|
529
|
-
const hasFreshShare = this.appStateKeyShareVersion !== observedKeyShareVersion;
|
|
462
|
+
let observedKeyShareVersion = this.keyShareCoordinator.getVersion();
|
|
463
|
+
while (blockedCollections.length > 0 && retryCount < WA_APP_STATE_KEY_SHARE_MAX_RETRIES) {
|
|
464
|
+
const hasFreshShare = this.keyShareCoordinator.getVersion() !== observedKeyShareVersion;
|
|
530
465
|
if (!hasFreshShare) {
|
|
531
466
|
this.logger.info('app-state bootstrap waiting for key share', {
|
|
532
467
|
blockedCollections: blockedCollections.join(','),
|
|
533
468
|
timeoutMs: WA_APP_STATE_KEY_SHARE_WAIT_TIMEOUT_MS,
|
|
534
469
|
retryCount: retryCount + 1
|
|
535
470
|
});
|
|
536
|
-
const received = await this.
|
|
471
|
+
const received = await this.keyShareCoordinator.waitForShare(WA_APP_STATE_KEY_SHARE_WAIT_TIMEOUT_MS);
|
|
537
472
|
if (!received) {
|
|
538
473
|
this.logger.warn('app-state bootstrap key share wait timed out', {
|
|
539
474
|
blockedCollections: blockedCollections.join(','),
|
|
@@ -542,7 +477,7 @@ export class WaClient extends EventEmitter {
|
|
|
542
477
|
break;
|
|
543
478
|
}
|
|
544
479
|
}
|
|
545
|
-
observedKeyShareVersion = this.
|
|
480
|
+
observedKeyShareVersion = this.keyShareCoordinator.getVersion();
|
|
546
481
|
retryCount += 1;
|
|
547
482
|
this.logger.info('app-state bootstrap retrying sync after key share', {
|
|
548
483
|
retryCount,
|
|
@@ -573,38 +508,6 @@ export class WaClient extends EventEmitter {
|
|
|
573
508
|
.filter((entry) => entry.state === WA_APP_STATE_COLLECTION_STATES.BLOCKED)
|
|
574
509
|
.map((entry) => entry.collection);
|
|
575
510
|
}
|
|
576
|
-
async waitForAppStateKeyShare(timeoutMs) {
|
|
577
|
-
return new Promise((resolve) => {
|
|
578
|
-
let settled = false;
|
|
579
|
-
let timeoutHandle = null;
|
|
580
|
-
const waiter = (received) => {
|
|
581
|
-
if (settled) {
|
|
582
|
-
return;
|
|
583
|
-
}
|
|
584
|
-
settled = true;
|
|
585
|
-
if (timeoutHandle) {
|
|
586
|
-
clearTimeout(timeoutHandle);
|
|
587
|
-
timeoutHandle = null;
|
|
588
|
-
}
|
|
589
|
-
this.appStateKeyShareWaiters.delete(waiter);
|
|
590
|
-
resolve(received);
|
|
591
|
-
};
|
|
592
|
-
this.appStateKeyShareWaiters.add(waiter);
|
|
593
|
-
timeoutHandle = setTimeout(() => {
|
|
594
|
-
waiter(false);
|
|
595
|
-
}, timeoutMs);
|
|
596
|
-
});
|
|
597
|
-
}
|
|
598
|
-
notifyAppStateKeyShareWaiters(received) {
|
|
599
|
-
if (this.appStateKeyShareWaiters.size === 0) {
|
|
600
|
-
return;
|
|
601
|
-
}
|
|
602
|
-
const waiters = [...this.appStateKeyShareWaiters.values()];
|
|
603
|
-
this.appStateKeyShareWaiters.clear();
|
|
604
|
-
for (const waiter of waiters) {
|
|
605
|
-
waiter(received);
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
511
|
emitChatEventsFromAppStateSyncResult(syncResult) {
|
|
609
512
|
const shouldEmitSnapshotMutations = this.options.chatEvents?.emitSnapshotMutations === true;
|
|
610
513
|
for (const collectionResult of syncResult.collections) {
|
|
@@ -644,75 +547,13 @@ export class WaClient extends EventEmitter {
|
|
|
644
547
|
}
|
|
645
548
|
}
|
|
646
549
|
}
|
|
647
|
-
async startCommsWithCredentials(credentials) {
|
|
648
|
-
this.logger.debug('starting comms with credentials', {
|
|
649
|
-
registered: credentials.meJid !== null && credentials.meJid !== undefined
|
|
650
|
-
});
|
|
651
|
-
const commsConfig = this.authClient.buildCommsConfig(this.options);
|
|
652
|
-
const comms = new WaComms(commsConfig, this.logger);
|
|
653
|
-
this.mediaConnCache = null;
|
|
654
|
-
this.nodeTransport.bindComms(comms);
|
|
655
|
-
try {
|
|
656
|
-
comms.startComms(async (frame) => this.handleIncomingFrame(frame));
|
|
657
|
-
await comms.waitForConnection(commsConfig.connectTimeoutMs);
|
|
658
|
-
this.comms = comms;
|
|
659
|
-
this.logger.info('comms connected');
|
|
660
|
-
comms.startHandlingRequests();
|
|
661
|
-
if (credentials.meJid) {
|
|
662
|
-
this.keepAlive.start();
|
|
663
|
-
}
|
|
664
|
-
else {
|
|
665
|
-
this.keepAlive.stop();
|
|
666
|
-
}
|
|
667
|
-
const serverStaticKey = comms.getServerStaticKey();
|
|
668
|
-
if (!serverStaticKey) {
|
|
669
|
-
this.logger.trace('no server static key available to persist');
|
|
670
|
-
}
|
|
671
|
-
else {
|
|
672
|
-
await this.authClient.persistServerStaticKey(serverStaticKey);
|
|
673
|
-
this.logger.debug('persisted server static key after comms connect');
|
|
674
|
-
}
|
|
675
|
-
this.passiveTasks.startPassiveTasksAfterConnect();
|
|
676
|
-
}
|
|
677
|
-
catch (error) {
|
|
678
|
-
this.clearCommsBinding();
|
|
679
|
-
try {
|
|
680
|
-
await comms.stopComms();
|
|
681
|
-
}
|
|
682
|
-
catch (stopError) {
|
|
683
|
-
this.logger.warn('failed to cleanup comms after connection start failure', {
|
|
684
|
-
message: toError(stopError).message
|
|
685
|
-
});
|
|
686
|
-
}
|
|
687
|
-
throw error;
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
shouldQueueDanglingReceipt(node, error) {
|
|
691
|
-
if (node.tag !== WA_MESSAGE_TAGS.RECEIPT) {
|
|
692
|
-
return false;
|
|
693
|
-
}
|
|
694
|
-
const normalized = error.message.trim().toLowerCase();
|
|
695
|
-
return (normalized === 'comms is not connected' ||
|
|
696
|
-
normalized === 'websocket is not connected' ||
|
|
697
|
-
normalized === 'noise session socket closed' ||
|
|
698
|
-
normalized.startsWith('socket closed ('));
|
|
699
|
-
}
|
|
700
|
-
enqueueDanglingReceipt(node) {
|
|
701
|
-
if (this.danglingReceipts.length >= WA_DEFAULTS.MAX_DANGLING_RECEIPTS) {
|
|
702
|
-
this.danglingReceipts.shift();
|
|
703
|
-
}
|
|
704
|
-
this.danglingReceipts.push(node.content === undefined
|
|
705
|
-
? {
|
|
706
|
-
tag: node.tag,
|
|
707
|
-
attrs: { ...node.attrs }
|
|
708
|
-
}
|
|
709
|
-
: {
|
|
710
|
-
tag: node.tag,
|
|
711
|
-
attrs: { ...node.attrs },
|
|
712
|
-
content: node.content
|
|
713
|
-
});
|
|
714
|
-
}
|
|
715
550
|
async clearStoredState() {
|
|
551
|
+
const danglingReceipts = this.receiptQueue.take();
|
|
552
|
+
if (danglingReceipts.length > 0) {
|
|
553
|
+
this.logger.debug('cleared dangling receipts while clearing stored state', {
|
|
554
|
+
count: danglingReceipts.length
|
|
555
|
+
});
|
|
556
|
+
}
|
|
716
557
|
await this.authClient.clearStoredCredentials();
|
|
717
558
|
await this.appStateStore.clear();
|
|
718
559
|
await this.contactStore.clear();
|
|
@@ -728,18 +569,4 @@ export class WaClient extends EventEmitter {
|
|
|
728
569
|
this.logger.error('wa client error', { message: error.message });
|
|
729
570
|
this.emit('client_error', { error });
|
|
730
571
|
}
|
|
731
|
-
clearCommsBinding() {
|
|
732
|
-
this.notifyAppStateKeyShareWaiters(false);
|
|
733
|
-
this.comms = null;
|
|
734
|
-
this.nodeTransport.bindComms(null);
|
|
735
|
-
}
|
|
736
|
-
updateClockSkewFromSuccess(serverUnixSeconds) {
|
|
737
|
-
const serverMs = serverUnixSeconds * 1000;
|
|
738
|
-
const nowMs = Date.now();
|
|
739
|
-
this.clockSkewMs = serverMs - nowMs;
|
|
740
|
-
this.logger.debug('updated clock skew from success', {
|
|
741
|
-
serverUnixSeconds,
|
|
742
|
-
clockSkewMs: this.clockSkewMs
|
|
743
|
-
});
|
|
744
|
-
}
|
|
745
572
|
}
|