zapo-js 0.1.2 → 0.2.0
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 +12 -4
- package/dist/appstate/WaAppStateCrypto.js +1 -1
- package/dist/appstate/WaAppStateSyncClient.js +138 -93
- package/dist/appstate/{store/sqlite.js → encoding.js} +13 -8
- package/dist/appstate/index.js +8 -6
- package/dist/appstate/utils.js +0 -5
- package/dist/auth/WaAuthClient.js +36 -47
- package/dist/auth/flow/WaAuthCredentialsFlow.js +7 -7
- package/dist/auth/index.js +1 -6
- package/dist/auth/pairing/WaPairingCodeCrypto.js +6 -4
- package/dist/auth/pairing/WaPairingFlow.js +13 -3
- package/dist/client/WaClient.js +225 -101
- package/dist/client/WaClientFactory.js +294 -44
- package/dist/client/connection/WaConnectionManager.js +19 -10
- package/dist/client/coordinators/WaBusinessCoordinator.js +241 -0
- package/dist/client/coordinators/WaGroupCoordinator.js +11 -7
- package/dist/client/coordinators/WaIncomingNodeCoordinator.js +1 -0
- package/dist/client/coordinators/WaMessageDispatchCoordinator.js +292 -99
- package/dist/client/coordinators/WaPassiveTasksCoordinator.js +74 -31
- package/dist/client/coordinators/WaPrivacyCoordinator.js +134 -0
- package/dist/client/coordinators/WaProfileCoordinator.js +212 -0
- package/dist/client/coordinators/WaRetryCoordinator.js +179 -27
- package/dist/client/coordinators/WaStreamControlCoordinator.js +18 -11
- package/dist/client/coordinators/WaTrustedContactTokenCoordinator.js +166 -0
- package/dist/client/dirty.js +40 -20
- package/dist/client/events/devices.js +72 -0
- package/dist/client/events/group.js +3 -11
- package/dist/client/events/identity.js +22 -0
- package/dist/client/events/privacy-token.js +39 -0
- package/dist/client/history-sync.js +50 -9
- package/dist/client/incoming.js +37 -7
- package/dist/client/mailbox.js +24 -23
- package/dist/client/messages.js +107 -31
- package/dist/client/messaging/fanout.js +21 -11
- package/dist/client/messaging/participants.js +6 -4
- package/dist/client/persistence/WriteBehindPersistence.js +129 -0
- package/dist/client/tokens/cs-token.js +50 -0
- package/dist/client/tokens/tc-token.js +25 -0
- package/dist/crypto/core/index.js +2 -2
- package/dist/crypto/core/keys.js +4 -4
- package/dist/crypto/core/nonce.js +2 -0
- package/dist/crypto/core/primitives.js +0 -8
- package/dist/crypto/core/random.js +22 -0
- package/dist/crypto/curves/X25519.js +25 -6
- package/dist/crypto/index.js +3 -0
- package/dist/crypto/math/constants.js +13 -36
- package/dist/crypto/math/edwards.js +171 -44
- package/dist/crypto/math/fe.js +706 -0
- package/dist/crypto/math/mod.js +10 -3
- package/dist/esm/appstate/WaAppStateCrypto.js +1 -1
- package/dist/esm/appstate/WaAppStateSyncClient.js +138 -93
- package/dist/esm/appstate/{store/sqlite.js → encoding.js} +13 -8
- package/dist/esm/appstate/index.js +2 -2
- package/dist/esm/appstate/utils.js +2 -5
- package/dist/esm/auth/WaAuthClient.js +36 -47
- package/dist/esm/auth/flow/WaAuthCredentialsFlow.js +7 -7
- package/dist/esm/auth/index.js +0 -2
- package/dist/esm/auth/pairing/WaPairingCodeCrypto.js +6 -4
- package/dist/esm/auth/pairing/WaPairingFlow.js +14 -4
- package/dist/esm/client/WaClient.js +225 -101
- package/dist/esm/client/WaClientFactory.js +295 -45
- package/dist/esm/client/connection/WaConnectionManager.js +19 -10
- package/dist/esm/client/coordinators/WaBusinessCoordinator.js +238 -0
- package/dist/esm/client/coordinators/WaGroupCoordinator.js +11 -7
- package/dist/esm/client/coordinators/WaIncomingNodeCoordinator.js +1 -0
- package/dist/esm/client/coordinators/WaMessageDispatchCoordinator.js +295 -102
- package/dist/esm/client/coordinators/WaPassiveTasksCoordinator.js +74 -31
- package/dist/esm/client/coordinators/WaPrivacyCoordinator.js +131 -0
- package/dist/esm/client/coordinators/WaProfileCoordinator.js +209 -0
- package/dist/esm/client/coordinators/WaRetryCoordinator.js +181 -29
- package/dist/esm/client/coordinators/WaStreamControlCoordinator.js +19 -12
- package/dist/esm/client/coordinators/WaTrustedContactTokenCoordinator.js +162 -0
- package/dist/esm/client/dirty.js +40 -20
- package/dist/esm/client/events/devices.js +68 -0
- package/dist/esm/client/events/group.js +3 -11
- package/dist/esm/client/events/identity.js +19 -0
- package/dist/esm/client/events/privacy-token.js +36 -0
- package/dist/esm/client/history-sync.js +50 -9
- package/dist/esm/client/incoming.js +38 -8
- package/dist/esm/client/mailbox.js +24 -23
- package/dist/esm/client/messages.js +108 -32
- package/dist/esm/client/messaging/fanout.js +22 -12
- package/dist/esm/client/messaging/participants.js +6 -4
- package/dist/esm/client/persistence/WriteBehindPersistence.js +125 -0
- package/dist/esm/client/tokens/cs-token.js +46 -0
- package/dist/esm/client/tokens/tc-token.js +18 -0
- package/dist/esm/crypto/core/index.js +2 -2
- package/dist/esm/crypto/core/keys.js +1 -1
- package/dist/esm/crypto/core/nonce.js +2 -0
- package/dist/esm/crypto/core/primitives.js +0 -7
- package/dist/esm/crypto/core/random.js +22 -1
- package/dist/esm/crypto/curves/X25519.js +25 -6
- package/dist/esm/crypto/index.js +1 -0
- package/dist/esm/crypto/math/constants.js +12 -35
- package/dist/esm/crypto/math/edwards.js +174 -47
- package/dist/esm/crypto/math/fe.js +691 -0
- package/dist/esm/crypto/math/mod.js +10 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/infra/perf/BackgroundQueue.js +478 -0
- package/dist/esm/infra/perf/BoundedTaskQueue.js +3 -1
- package/dist/esm/infra/perf/PromiseDedup.js +20 -0
- package/dist/esm/infra/perf/SharedExclusiveGate.js +109 -0
- package/dist/esm/infra/perf/StoreLock.js +77 -0
- package/dist/esm/media/WaMediaCrypto.js +95 -13
- package/dist/esm/media/WaMediaTransferClient.js +39 -47
- package/dist/esm/media/constants.js +2 -1
- package/dist/esm/message/WaMessageClient.js +26 -19
- package/dist/esm/message/content.js +195 -9
- package/dist/esm/message/icdc.js +76 -0
- package/dist/esm/message/incoming.js +24 -12
- package/dist/esm/message/phash.js +3 -1
- package/dist/esm/message/reporting-token.js +14 -27
- package/dist/esm/protocol/appstate.js +9 -40
- package/dist/esm/protocol/browser.js +10 -18
- package/dist/esm/protocol/constants.js +5 -3
- package/dist/esm/protocol/defaults.js +6 -0
- package/dist/esm/protocol/index.js +1 -2
- package/dist/esm/protocol/jid.js +105 -36
- package/dist/esm/protocol/message.js +61 -1
- package/dist/esm/protocol/nodes.js +2 -0
- package/dist/esm/protocol/notification.js +3 -1
- package/dist/esm/protocol/privacy-token.js +17 -0
- package/dist/esm/protocol/privacy.js +55 -0
- package/dist/esm/protocol/stream.js +26 -1
- package/dist/esm/retry/codec.js +216 -0
- package/dist/esm/retry/constants.js +1 -1
- package/dist/esm/retry/index.js +2 -2
- package/dist/esm/retry/parse.js +50 -30
- package/dist/esm/retry/replay.js +11 -7
- package/dist/esm/retry/tracker.js +50 -12
- package/dist/esm/signal/api/SignalDeviceSyncApi.js +49 -32
- package/dist/esm/signal/api/SignalDigestSyncApi.js +13 -9
- package/dist/esm/signal/api/SignalIdentitySyncApi.js +26 -11
- package/dist/esm/signal/api/SignalMissingPreKeysSyncApi.js +18 -7
- package/dist/esm/signal/api/SignalRotateKeyApi.js +4 -2
- package/dist/esm/signal/api/SignalSessionSyncApi.js +16 -7
- package/dist/esm/signal/api/result-map.js +10 -0
- package/dist/esm/signal/constants.js +0 -4
- package/dist/esm/signal/crypto/WaAdvSignature.js +12 -6
- package/dist/esm/signal/{store/sqlite.js → encoding.js} +78 -24
- package/dist/esm/signal/group/SenderKeyCodec.js +3 -2
- package/dist/esm/signal/group/SenderKeyManager.js +125 -106
- package/dist/esm/signal/index.js +1 -0
- package/dist/esm/signal/registration/keygen.js +6 -2
- package/dist/esm/signal/registration/utils.js +1 -0
- package/dist/esm/signal/session/SignalProtocol.js +150 -74
- package/dist/esm/signal/session/resolver.js +137 -102
- package/dist/esm/store/contracts/privacy-token.store.js +1 -0
- package/dist/esm/store/createStore.js +101 -187
- package/dist/esm/store/index.js +1 -10
- package/dist/esm/store/locks/appstate.lock.js +26 -0
- package/dist/esm/store/locks/auth.lock.js +15 -0
- package/dist/esm/store/locks/contact.lock.js +20 -0
- package/dist/esm/store/locks/device-list.lock.js +20 -0
- package/dist/esm/store/locks/message.lock.js +21 -0
- package/dist/esm/store/locks/participants.lock.js +20 -0
- package/dist/esm/store/locks/privacy-token.lock.js +18 -0
- package/dist/esm/store/locks/retry.lock.js +29 -0
- package/dist/esm/store/locks/sender-key.lock.js +52 -0
- package/dist/esm/store/locks/signal.lock.js +63 -0
- package/dist/esm/store/locks/thread.lock.js +21 -0
- package/dist/esm/store/noop.store.js +1 -1
- package/dist/esm/store/providers/memory/appstate.store.js +22 -24
- package/dist/esm/store/providers/memory/device-list.store.js +10 -5
- package/dist/esm/store/providers/memory/privacy-token.store.js +43 -0
- package/dist/esm/store/providers/memory/retry.store.js +77 -2
- package/dist/esm/store/providers/memory/sender-key.store.js +6 -1
- package/dist/esm/store/providers/memory/signal.store.js +36 -19
- package/dist/esm/transport/WaComms.js +3 -1
- package/dist/esm/transport/WaWebSocket.js +0 -6
- package/dist/esm/transport/binary/constants.js +0 -30
- package/dist/esm/transport/binary/decoder.js +4 -4
- package/dist/esm/transport/binary/encoder.js +8 -15
- package/dist/esm/transport/binary/index.js +0 -1
- package/dist/esm/transport/node/WaNodeOrchestrator.js +25 -19
- package/dist/esm/transport/node/builders/business.js +129 -0
- package/dist/esm/transport/node/builders/global.js +370 -0
- package/dist/esm/transport/node/builders/index.js +5 -2
- package/dist/esm/transport/node/builders/message.js +63 -239
- package/dist/esm/transport/node/builders/pairing.js +0 -24
- package/dist/esm/transport/node/builders/privacy-token.js +41 -0
- package/dist/esm/transport/node/builders/privacy.js +48 -0
- package/dist/esm/transport/node/builders/profile.js +70 -0
- package/dist/esm/transport/node/builders/retry.js +10 -22
- package/dist/esm/transport/node/builders/usync.js +6 -2
- package/dist/esm/transport/node/helpers.js +19 -1
- package/dist/esm/transport/node/usync.js +3 -33
- package/dist/esm/transport/node/xml.js +35 -14
- package/dist/esm/transport/noise/WaClientPayload.js +10 -10
- package/dist/esm/transport/noise/WaNoiseCert.js +3 -3
- package/dist/esm/transport/noise/WaNoiseSession.js +64 -23
- package/dist/esm/transport/noise/WaNoiseSocket.js +8 -4
- package/dist/esm/transport/stream/parse.js +8 -4
- package/dist/esm/util/bytes.js +22 -18
- package/dist/esm/util/index.js +5 -0
- package/dist/esm/util/primitives.js +3 -2
- package/dist/index.js +7 -1
- package/dist/infra/perf/BackgroundQueue.js +482 -0
- package/dist/infra/perf/BoundedTaskQueue.js +3 -1
- package/dist/infra/perf/PromiseDedup.js +24 -0
- package/dist/infra/perf/SharedExclusiveGate.js +113 -0
- package/dist/infra/perf/StoreLock.js +81 -0
- package/dist/media/WaMediaCrypto.js +94 -12
- package/dist/media/WaMediaTransferClient.js +39 -47
- package/dist/media/constants.js +2 -1
- package/dist/message/WaMessageClient.js +26 -19
- package/dist/message/content.js +198 -9
- package/dist/message/icdc.js +81 -0
- package/dist/message/incoming.js +24 -12
- package/dist/message/phash.js +3 -1
- package/dist/message/reporting-token.js +14 -28
- package/dist/protocol/appstate.js +10 -41
- package/dist/protocol/browser.js +10 -18
- package/dist/protocol/constants.js +21 -2
- package/dist/protocol/defaults.js +6 -0
- package/dist/protocol/index.js +8 -5
- package/dist/protocol/jid.js +111 -36
- package/dist/protocol/message.js +62 -2
- package/dist/protocol/nodes.js +2 -0
- package/dist/protocol/notification.js +3 -1
- package/dist/protocol/privacy-token.js +20 -0
- package/dist/protocol/privacy.js +58 -0
- package/dist/protocol/stream.js +27 -2
- package/dist/retry/codec.js +220 -0
- package/dist/retry/constants.js +1 -1
- package/dist/retry/index.js +5 -5
- package/dist/retry/parse.js +51 -30
- package/dist/retry/replay.js +10 -6
- package/dist/retry/tracker.js +50 -12
- package/dist/signal/api/SignalDeviceSyncApi.js +48 -31
- package/dist/signal/api/SignalDigestSyncApi.js +13 -9
- package/dist/signal/api/SignalIdentitySyncApi.js +25 -10
- package/dist/signal/api/SignalMissingPreKeysSyncApi.js +17 -6
- package/dist/signal/api/SignalRotateKeyApi.js +4 -2
- package/dist/signal/api/SignalSessionSyncApi.js +16 -7
- package/dist/signal/api/result-map.js +13 -0
- package/dist/signal/constants.js +1 -5
- package/dist/signal/crypto/WaAdvSignature.js +11 -5
- package/dist/signal/{store/sqlite.js → encoding.js} +79 -25
- package/dist/signal/group/SenderKeyCodec.js +4 -3
- package/dist/signal/group/SenderKeyManager.js +125 -106
- package/dist/signal/index.js +13 -1
- package/dist/signal/registration/keygen.js +6 -2
- package/dist/signal/registration/utils.js +1 -0
- package/dist/signal/session/SignalProtocol.js +150 -74
- package/dist/signal/session/resolver.js +135 -100
- package/dist/store/contracts/privacy-token.store.js +2 -0
- package/dist/store/createStore.js +101 -187
- package/dist/store/index.js +15 -33
- package/dist/store/locks/appstate.lock.js +29 -0
- package/dist/store/locks/auth.lock.js +18 -0
- package/dist/store/locks/contact.lock.js +23 -0
- package/dist/store/locks/device-list.lock.js +23 -0
- package/dist/store/locks/message.lock.js +24 -0
- package/dist/store/locks/participants.lock.js +23 -0
- package/dist/store/locks/privacy-token.lock.js +21 -0
- package/dist/store/locks/retry.lock.js +32 -0
- package/dist/store/locks/sender-key.lock.js +55 -0
- package/dist/store/locks/signal.lock.js +66 -0
- package/dist/store/locks/thread.lock.js +24 -0
- package/dist/store/noop.store.js +1 -1
- package/dist/store/providers/memory/appstate.store.js +22 -24
- package/dist/store/providers/memory/device-list.store.js +10 -5
- package/dist/store/providers/memory/privacy-token.store.js +47 -0
- package/dist/store/providers/memory/retry.store.js +77 -2
- package/dist/store/providers/memory/sender-key.store.js +6 -1
- package/dist/store/providers/memory/signal.store.js +36 -19
- package/dist/transport/WaComms.js +3 -1
- package/dist/transport/WaWebSocket.js +0 -6
- package/dist/transport/binary/constants.js +1 -31
- package/dist/transport/binary/decoder.js +4 -4
- package/dist/transport/binary/encoder.js +8 -15
- package/dist/transport/binary/index.js +0 -4
- package/dist/transport/node/WaNodeOrchestrator.js +24 -18
- package/dist/transport/node/builders/business.js +137 -0
- package/dist/transport/node/builders/global.js +375 -0
- package/dist/transport/node/builders/index.js +18 -9
- package/dist/transport/node/builders/message.js +64 -245
- package/dist/transport/node/builders/pairing.js +0 -26
- package/dist/transport/node/builders/privacy-token.js +46 -0
- package/dist/transport/node/builders/privacy.js +55 -0
- package/dist/transport/node/builders/profile.js +78 -0
- package/dist/transport/node/builders/retry.js +9 -21
- package/dist/transport/node/builders/usync.js +6 -2
- package/dist/transport/node/helpers.js +20 -1
- package/dist/transport/node/usync.js +2 -32
- package/dist/transport/node/xml.js +35 -14
- package/dist/transport/noise/WaClientPayload.js +13 -13
- package/dist/transport/noise/WaNoiseCert.js +2 -2
- package/dist/transport/noise/WaNoiseSession.js +64 -23
- package/dist/transport/noise/WaNoiseSocket.js +8 -4
- package/dist/transport/stream/parse.js +7 -3
- package/dist/types/appstate/encoding.d.ts +7 -0
- package/dist/types/appstate/index.d.ts +3 -3
- package/dist/types/appstate/utils.d.ts +0 -2
- package/dist/types/auth/flow/WaAuthCredentialsFlow.d.ts +1 -1
- package/dist/types/auth/index.d.ts +0 -2
- package/dist/types/auth/types.d.ts +1 -0
- package/dist/types/client/WaClient.d.ts +27 -12
- package/dist/types/client/WaClientFactory.d.ts +12 -4
- package/dist/types/client/connection/WaConnectionManager.d.ts +2 -0
- package/dist/types/client/coordinators/WaBusinessCoordinator.d.ts +57 -0
- package/dist/types/client/coordinators/WaIncomingNodeCoordinator.d.ts +3 -1
- package/dist/types/client/coordinators/WaMessageDispatchCoordinator.d.ts +14 -0
- package/dist/types/client/coordinators/WaPassiveTasksCoordinator.d.ts +4 -0
- package/dist/types/client/coordinators/WaPrivacyCoordinator.d.ts +26 -0
- package/dist/types/client/coordinators/WaProfileCoordinator.d.ts +36 -0
- package/dist/types/client/coordinators/WaRetryCoordinator.d.ts +6 -0
- package/dist/types/client/coordinators/WaStreamControlCoordinator.d.ts +3 -2
- package/dist/types/client/coordinators/WaTrustedContactTokenCoordinator.d.ts +45 -0
- package/dist/types/client/events/devices.d.ts +20 -0
- package/dist/types/client/events/identity.d.ts +9 -0
- package/dist/types/client/events/privacy-token.d.ts +7 -0
- package/dist/types/client/history-sync.d.ts +9 -6
- package/dist/types/client/incoming.d.ts +3 -1
- package/dist/types/client/index.d.ts +1 -1
- package/dist/types/client/mailbox.d.ts +3 -5
- package/dist/types/client/messages.d.ts +1 -2
- package/dist/types/client/persistence/WriteBehindPersistence.d.ts +34 -0
- package/dist/types/client/tokens/cs-token.d.ts +10 -0
- package/dist/types/client/tokens/tc-token.d.ts +5 -0
- package/dist/types/client/types.d.ts +51 -3
- package/dist/types/crypto/core/index.d.ts +2 -2
- package/dist/types/crypto/core/nonce.d.ts +2 -0
- package/dist/types/crypto/core/primitives.d.ts +0 -1
- package/dist/types/crypto/core/random.d.ts +1 -0
- package/dist/types/crypto/index.d.ts +1 -0
- package/dist/types/crypto/math/constants.d.ts +4 -2
- package/dist/types/crypto/math/fe.d.ts +30 -0
- package/dist/types/crypto/math/mod.d.ts +0 -2
- package/dist/types/crypto/math/types.d.ts +11 -4
- package/dist/types/index.d.ts +5 -3
- package/dist/types/infra/perf/BackgroundQueue.d.ts +58 -0
- package/dist/types/infra/perf/PromiseDedup.d.ts +4 -0
- package/dist/types/infra/perf/SharedExclusiveGate.d.ts +17 -0
- package/dist/types/infra/perf/StoreLock.d.ts +10 -0
- package/dist/types/media/WaMediaCrypto.d.ts +3 -2
- package/dist/types/media/WaMediaTransferClient.d.ts +3 -12
- package/dist/types/media/constants.d.ts +1 -1
- package/dist/types/media/index.d.ts +1 -1
- package/dist/types/media/types.d.ts +10 -2
- package/dist/types/message/content.d.ts +8 -0
- package/dist/types/message/icdc.d.ts +13 -0
- package/dist/types/message/reporting-token.d.ts +0 -1
- package/dist/types/message/types.d.ts +45 -6
- package/dist/types/protocol/appstate.d.ts +0 -11
- package/dist/types/protocol/constants.d.ts +7 -3
- package/dist/types/protocol/defaults.d.ts +6 -0
- package/dist/types/protocol/index.d.ts +1 -2
- package/dist/types/protocol/jid.d.ts +19 -2
- package/dist/types/protocol/message.d.ts +60 -0
- package/dist/types/protocol/nodes.d.ts +2 -0
- package/dist/types/protocol/notification.d.ts +2 -0
- package/dist/types/protocol/privacy-token.d.ts +17 -0
- package/dist/types/protocol/privacy.d.ts +75 -0
- package/dist/types/protocol/stream.d.ts +30 -0
- package/dist/types/retry/codec.d.ts +3 -0
- package/dist/types/retry/index.d.ts +3 -3
- package/dist/types/retry/parse.d.ts +5 -2
- package/dist/types/retry/tracker.d.ts +1 -0
- package/dist/types/retry/types.d.ts +6 -1
- package/dist/types/signal/api/SignalDeviceSyncApi.d.ts +2 -1
- package/dist/types/signal/api/SignalDigestSyncApi.d.ts +6 -0
- package/dist/types/signal/api/SignalIdentitySyncApi.d.ts +2 -0
- package/dist/types/signal/api/SignalRotateKeyApi.d.ts +4 -5
- package/dist/types/signal/api/SignalSessionSyncApi.d.ts +8 -6
- package/dist/types/signal/api/result-map.d.ts +1 -0
- package/dist/types/signal/constants.d.ts +0 -3
- package/dist/types/signal/{store/sqlite.d.ts → encoding.d.ts} +3 -3
- package/dist/types/signal/group/SenderKeyManager.d.ts +10 -5
- package/dist/types/signal/index.d.ts +2 -0
- package/dist/types/signal/session/SignalProtocol.d.ts +10 -4
- package/dist/types/signal/session/resolver.d.ts +7 -2
- package/dist/types/store/contracts/appstate.store.d.ts +1 -1
- package/dist/types/store/contracts/privacy-token.store.d.ts +16 -0
- package/dist/types/store/contracts/retry.store.d.ts +7 -0
- package/dist/types/store/contracts/signal.store.d.ts +7 -0
- package/dist/types/store/createStore.d.ts +1 -1
- package/dist/types/store/index.d.ts +5 -13
- package/dist/types/store/locks/appstate.lock.d.ts +3 -0
- package/dist/types/store/locks/auth.lock.d.ts +3 -0
- package/dist/types/store/locks/contact.lock.d.ts +3 -0
- package/dist/types/store/locks/device-list.lock.d.ts +2 -0
- package/dist/types/store/locks/message.lock.d.ts +3 -0
- package/dist/types/store/locks/participants.lock.d.ts +2 -0
- package/dist/types/store/locks/privacy-token.lock.d.ts +2 -0
- package/dist/types/store/locks/retry.lock.d.ts +2 -0
- package/dist/types/store/locks/sender-key.lock.d.ts +3 -0
- package/dist/types/store/locks/signal.lock.d.ts +3 -0
- package/dist/types/store/locks/thread.lock.d.ts +3 -0
- package/dist/types/store/providers/memory/appstate.store.d.ts +1 -1
- package/dist/types/store/providers/memory/privacy-token.store.d.ts +13 -0
- package/dist/types/store/providers/memory/retry.store.d.ts +8 -0
- package/dist/types/store/providers/memory/signal.store.d.ts +2 -1
- package/dist/types/store/types.d.ts +49 -61
- package/dist/types/transport/WaWebSocket.d.ts +0 -1
- package/dist/types/transport/binary/constants.d.ts +0 -30
- package/dist/types/transport/binary/index.d.ts +0 -1
- package/dist/types/transport/node/WaNodeOrchestrator.d.ts +3 -4
- package/dist/types/transport/node/builders/business.d.ts +29 -0
- package/dist/types/transport/node/builders/global.d.ts +102 -0
- package/dist/types/transport/node/builders/index.d.ts +5 -2
- package/dist/types/transport/node/builders/message.d.ts +8 -7
- package/dist/types/transport/node/builders/pairing.d.ts +0 -2
- package/dist/types/transport/node/builders/privacy-token.d.ts +9 -0
- package/dist/types/transport/node/builders/privacy.d.ts +7 -0
- package/dist/types/transport/node/builders/profile.d.ts +8 -0
- package/dist/types/transport/node/builders/retry.d.ts +0 -1
- package/dist/types/transport/node/helpers.d.ts +5 -0
- package/dist/types/transport/noise/WaNoiseSession.d.ts +3 -2
- package/dist/types/transport/noise/WaNoiseSocket.d.ts +4 -2
- package/dist/types/util/bytes.d.ts +1 -1
- package/dist/types/util/index.d.ts +5 -0
- package/dist/types/util/primitives.d.ts +0 -1
- package/dist/util/bytes.js +22 -18
- package/dist/util/index.js +23 -0
- package/dist/util/primitives.js +2 -2
- package/package.json +29 -7
- package/proto/index.js +1 -1
- package/dist/crypto/core/constants.js +0 -4
- package/dist/esm/crypto/core/constants.js +0 -1
- package/dist/esm/retry/outbound.js +0 -82
- package/dist/esm/store/providers/sqlite/BaseSqliteStore.js +0 -37
- package/dist/esm/store/providers/sqlite/appstate.store.js +0 -250
- package/dist/esm/store/providers/sqlite/auth.store.js +0 -176
- package/dist/esm/store/providers/sqlite/connection.js +0 -245
- package/dist/esm/store/providers/sqlite/contact.store.js +0 -74
- package/dist/esm/store/providers/sqlite/device-list.store.js +0 -127
- package/dist/esm/store/providers/sqlite/message.store.js +0 -132
- package/dist/esm/store/providers/sqlite/migrations.js +0 -347
- package/dist/esm/store/providers/sqlite/participants.store.js +0 -77
- package/dist/esm/store/providers/sqlite/retry.store.js +0 -141
- package/dist/esm/store/providers/sqlite/sender-key.store.js +0 -198
- package/dist/esm/store/providers/sqlite/signal.store.js +0 -435
- package/dist/esm/store/providers/sqlite/table-names.js +0 -107
- package/dist/esm/store/providers/sqlite/thread.store.js +0 -85
- package/dist/retry/outbound.js +0 -87
- package/dist/store/providers/sqlite/BaseSqliteStore.js +0 -41
- package/dist/store/providers/sqlite/appstate.store.js +0 -254
- package/dist/store/providers/sqlite/auth.store.js +0 -180
- package/dist/store/providers/sqlite/connection.js +0 -281
- package/dist/store/providers/sqlite/contact.store.js +0 -78
- package/dist/store/providers/sqlite/device-list.store.js +0 -131
- package/dist/store/providers/sqlite/message.store.js +0 -136
- package/dist/store/providers/sqlite/migrations.js +0 -350
- package/dist/store/providers/sqlite/participants.store.js +0 -81
- package/dist/store/providers/sqlite/retry.store.js +0 -145
- package/dist/store/providers/sqlite/sender-key.store.js +0 -202
- package/dist/store/providers/sqlite/signal.store.js +0 -439
- package/dist/store/providers/sqlite/table-names.js +0 -113
- package/dist/store/providers/sqlite/thread.store.js +0 -89
- package/dist/types/appstate/store/sqlite.d.ts +0 -7
- package/dist/types/crypto/core/constants.d.ts +0 -1
- package/dist/types/retry/outbound.d.ts +0 -4
- package/dist/types/store/providers/sqlite/BaseSqliteStore.d.ts +0 -12
- package/dist/types/store/providers/sqlite/appstate.store.d.ts +0 -17
- package/dist/types/store/providers/sqlite/auth.store.d.ts +0 -10
- package/dist/types/store/providers/sqlite/connection.d.ts +0 -10
- package/dist/types/store/providers/sqlite/contact.store.d.ts +0 -12
- package/dist/types/store/providers/sqlite/device-list.store.d.ts +0 -15
- package/dist/types/store/providers/sqlite/message.store.d.ts +0 -13
- package/dist/types/store/providers/sqlite/migrations.d.ts +0 -3
- package/dist/types/store/providers/sqlite/participants.store.d.ts +0 -12
- package/dist/types/store/providers/sqlite/retry.store.d.ts +0 -15
- package/dist/types/store/providers/sqlite/sender-key.store.d.ts +0 -24
- package/dist/types/store/providers/sqlite/signal.store.d.ts +0 -53
- package/dist/types/store/providers/sqlite/table-names.d.ts +0 -5
- package/dist/types/store/providers/sqlite/thread.store.d.ts +0 -13
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
import { createSqliteTableNameSqlResolver, resolveSqliteTableNames, serializeSqliteTableNames } from '../../providers/sqlite/table-names.js';
|
|
2
|
-
import { toSafeNumber } from '../../../util/primitives.js';
|
|
3
|
-
import { isBunRuntime } from '../../../util/runtime.js';
|
|
4
|
-
const BETTER_SQLITE3_MODULE = 'better-sqlite3';
|
|
5
|
-
const BUN_SQLITE_MODULE = 'bun:sqlite';
|
|
6
|
-
const SQLITE_PRAGMA_TOKEN_PATTERN = /^[A-Za-z0-9_+-]+$/;
|
|
7
|
-
const DEFAULT_SQLITE_PRAGMAS = Object.freeze({
|
|
8
|
-
journal_mode: 'WAL',
|
|
9
|
-
synchronous: 'normal',
|
|
10
|
-
busy_timeout: 5000
|
|
11
|
-
});
|
|
12
|
-
const ALLOWED_SQLITE_PRAGMAS = {
|
|
13
|
-
auto_vacuum: 'token_or_int',
|
|
14
|
-
busy_timeout: 'int',
|
|
15
|
-
cache_size: 'int',
|
|
16
|
-
foreign_keys: 'token_or_int',
|
|
17
|
-
journal_mode: 'token',
|
|
18
|
-
journal_size_limit: 'int',
|
|
19
|
-
legacy_alter_table: 'token_or_int',
|
|
20
|
-
locking_mode: 'token',
|
|
21
|
-
mmap_size: 'int',
|
|
22
|
-
page_size: 'int',
|
|
23
|
-
recursive_triggers: 'token_or_int',
|
|
24
|
-
secure_delete: 'token_or_int',
|
|
25
|
-
synchronous: 'token_or_int',
|
|
26
|
-
temp_store: 'token_or_int',
|
|
27
|
-
wal_autocheckpoint: 'int'
|
|
28
|
-
};
|
|
29
|
-
function asConstructor(loaded) {
|
|
30
|
-
if (typeof loaded === 'function') {
|
|
31
|
-
return loaded;
|
|
32
|
-
}
|
|
33
|
-
if (loaded && typeof loaded === 'object') {
|
|
34
|
-
const candidate = loaded.default;
|
|
35
|
-
if (typeof candidate === 'function') {
|
|
36
|
-
return candidate;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
throw new Error('invalid sqlite driver export');
|
|
40
|
-
}
|
|
41
|
-
function statementFor(db, sql) {
|
|
42
|
-
const prepare = db.prepare ?? db.query;
|
|
43
|
-
if (!prepare) {
|
|
44
|
-
throw new Error('sqlite driver does not expose prepare/query method');
|
|
45
|
-
}
|
|
46
|
-
const statement = prepare.call(db, sql);
|
|
47
|
-
if (!statement ||
|
|
48
|
-
typeof statement.run !== 'function' ||
|
|
49
|
-
typeof statement.get !== 'function' ||
|
|
50
|
-
typeof statement.all !== 'function') {
|
|
51
|
-
throw new Error('invalid sqlite statement API');
|
|
52
|
-
}
|
|
53
|
-
return statement;
|
|
54
|
-
}
|
|
55
|
-
function wrapConnection(db, driver, resolveSql, onClose) {
|
|
56
|
-
const statementCache = new Map();
|
|
57
|
-
const cachedStatementFor = (sql) => {
|
|
58
|
-
const resolvedSql = resolveSql(sql);
|
|
59
|
-
const cached = statementCache.get(resolvedSql);
|
|
60
|
-
if (cached) {
|
|
61
|
-
return cached;
|
|
62
|
-
}
|
|
63
|
-
const statement = statementFor(db, resolvedSql);
|
|
64
|
-
statementCache.set(resolvedSql, statement);
|
|
65
|
-
return statement;
|
|
66
|
-
};
|
|
67
|
-
return {
|
|
68
|
-
driver,
|
|
69
|
-
exec(sql) {
|
|
70
|
-
db.exec(resolveSql(sql));
|
|
71
|
-
},
|
|
72
|
-
run(sql, params) {
|
|
73
|
-
const statement = cachedStatementFor(sql);
|
|
74
|
-
if (!params || params.length === 0) {
|
|
75
|
-
statement.run();
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
statement.run(...params);
|
|
79
|
-
},
|
|
80
|
-
get(sql, params) {
|
|
81
|
-
const statement = cachedStatementFor(sql);
|
|
82
|
-
const row = !params || params.length === 0 ? statement.get() : statement.get(...params);
|
|
83
|
-
return row ?? null;
|
|
84
|
-
},
|
|
85
|
-
all(sql, params) {
|
|
86
|
-
const statement = cachedStatementFor(sql);
|
|
87
|
-
const rows = !params || params.length === 0 ? statement.all() : statement.all(...params);
|
|
88
|
-
return Array.isArray(rows) ? rows : [];
|
|
89
|
-
},
|
|
90
|
-
close() {
|
|
91
|
-
statementCache.clear();
|
|
92
|
-
try {
|
|
93
|
-
db.close();
|
|
94
|
-
}
|
|
95
|
-
finally {
|
|
96
|
-
onClose?.();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
function pragmaEntries(options) {
|
|
102
|
-
return Object.entries(mergePragmas(options.pragmas));
|
|
103
|
-
}
|
|
104
|
-
function mergePragmas(pragmas) {
|
|
105
|
-
return {
|
|
106
|
-
...DEFAULT_SQLITE_PRAGMAS,
|
|
107
|
-
...(pragmas ?? {})
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
const ALLOWED_SQLITE_PRAGMA_LIST = Object.keys(ALLOWED_SQLITE_PRAGMAS).sort().join(', ');
|
|
111
|
-
function normalizePragmaKey(rawKey) {
|
|
112
|
-
const key = rawKey.trim().toLowerCase();
|
|
113
|
-
if (!Object.prototype.hasOwnProperty.call(ALLOWED_SQLITE_PRAGMAS, key)) {
|
|
114
|
-
throw new Error(`unsupported sqlite pragma "${rawKey}". Allowed pragmas: ${ALLOWED_SQLITE_PRAGMA_LIST}`);
|
|
115
|
-
}
|
|
116
|
-
return key;
|
|
117
|
-
}
|
|
118
|
-
function normalizePragmaToken(key, rawValue) {
|
|
119
|
-
const value = rawValue.trim();
|
|
120
|
-
if (value.length === 0 || !SQLITE_PRAGMA_TOKEN_PATTERN.test(value)) {
|
|
121
|
-
throw new Error(`invalid sqlite pragma "${key}" value "${rawValue}". Allowed token pattern: ${SQLITE_PRAGMA_TOKEN_PATTERN}`);
|
|
122
|
-
}
|
|
123
|
-
return value;
|
|
124
|
-
}
|
|
125
|
-
function normalizePragmaValue(key, rawValue) {
|
|
126
|
-
const kind = ALLOWED_SQLITE_PRAGMAS[key];
|
|
127
|
-
if (kind === 'int') {
|
|
128
|
-
if (typeof rawValue !== 'number') {
|
|
129
|
-
throw new Error(`sqlite pragma "${key}" must be a number`);
|
|
130
|
-
}
|
|
131
|
-
return String(toSafeNumber(rawValue, `sqlite pragma "${key}"`));
|
|
132
|
-
}
|
|
133
|
-
if (kind === 'token') {
|
|
134
|
-
if (typeof rawValue !== 'string') {
|
|
135
|
-
throw new Error(`sqlite pragma "${key}" must be a string token`);
|
|
136
|
-
}
|
|
137
|
-
return normalizePragmaToken(key, rawValue);
|
|
138
|
-
}
|
|
139
|
-
if (typeof rawValue === 'number') {
|
|
140
|
-
return String(toSafeNumber(rawValue, `sqlite pragma "${key}"`));
|
|
141
|
-
}
|
|
142
|
-
return normalizePragmaToken(key, rawValue);
|
|
143
|
-
}
|
|
144
|
-
function applyPragmas(db, options) {
|
|
145
|
-
for (const [rawKey, rawValue] of pragmaEntries(options)) {
|
|
146
|
-
const key = normalizePragmaKey(rawKey);
|
|
147
|
-
const value = normalizePragmaValue(key, rawValue);
|
|
148
|
-
const statement = `${key}=${value}`;
|
|
149
|
-
if (db.pragma) {
|
|
150
|
-
db.pragma(statement);
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
db.exec(`PRAGMA ${statement}`);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
function closeDatabaseSafely(db) {
|
|
157
|
-
try {
|
|
158
|
-
db.close();
|
|
159
|
-
}
|
|
160
|
-
catch {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
async function openBetterSqlite(options, resolveSql, onClose) {
|
|
165
|
-
let loaded;
|
|
166
|
-
try {
|
|
167
|
-
loaded = await import(BETTER_SQLITE3_MODULE);
|
|
168
|
-
}
|
|
169
|
-
catch {
|
|
170
|
-
throw new Error('optional dependency "better-sqlite3" is not installed. Install with: npm i better-sqlite3');
|
|
171
|
-
}
|
|
172
|
-
const Database = asConstructor(loaded);
|
|
173
|
-
const db = new Database(options.path);
|
|
174
|
-
try {
|
|
175
|
-
applyPragmas(db, options);
|
|
176
|
-
}
|
|
177
|
-
catch (error) {
|
|
178
|
-
closeDatabaseSafely(db);
|
|
179
|
-
throw error;
|
|
180
|
-
}
|
|
181
|
-
return wrapConnection(db, 'better-sqlite3', resolveSql, onClose);
|
|
182
|
-
}
|
|
183
|
-
async function openBunSqlite(options, resolveSql, onClose) {
|
|
184
|
-
let loaded;
|
|
185
|
-
try {
|
|
186
|
-
loaded = await import(BUN_SQLITE_MODULE);
|
|
187
|
-
}
|
|
188
|
-
catch {
|
|
189
|
-
throw new Error('bun runtime sqlite module "bun:sqlite" is unavailable. Run this in Bun or set storage.sqlite.driver to "better-sqlite3".');
|
|
190
|
-
}
|
|
191
|
-
if (!loaded || typeof loaded !== 'object') {
|
|
192
|
-
throw new Error('invalid bun sqlite module export');
|
|
193
|
-
}
|
|
194
|
-
const ctor = loaded.Database;
|
|
195
|
-
if (typeof ctor !== 'function') {
|
|
196
|
-
throw new Error('invalid bun sqlite module export');
|
|
197
|
-
}
|
|
198
|
-
const db = new ctor(options.path);
|
|
199
|
-
try {
|
|
200
|
-
applyPragmas(db, options);
|
|
201
|
-
}
|
|
202
|
-
catch (error) {
|
|
203
|
-
closeDatabaseSafely(db);
|
|
204
|
-
throw error;
|
|
205
|
-
}
|
|
206
|
-
return wrapConnection(db, 'bun', resolveSql, onClose);
|
|
207
|
-
}
|
|
208
|
-
function resolveDriver(requested) {
|
|
209
|
-
if (requested && requested !== 'auto') {
|
|
210
|
-
return requested;
|
|
211
|
-
}
|
|
212
|
-
return isBunRuntime() ? 'bun' : 'better-sqlite3';
|
|
213
|
-
}
|
|
214
|
-
export async function openSqliteConnection(options) {
|
|
215
|
-
const driver = resolveDriver(options.driver);
|
|
216
|
-
const resolvedTableNames = resolveSqliteTableNames(options.tableNames);
|
|
217
|
-
const resolveSql = createSqliteTableNameSqlResolver(resolvedTableNames);
|
|
218
|
-
const normalizedOptions = {
|
|
219
|
-
...options,
|
|
220
|
-
driver,
|
|
221
|
-
pragmas: mergePragmas(options.pragmas),
|
|
222
|
-
tableNames: resolvedTableNames
|
|
223
|
-
};
|
|
224
|
-
const cacheKey = `${driver}|${options.path}|${Object.entries(normalizedOptions.pragmas ?? {})
|
|
225
|
-
.sort(([left], [right]) => left.localeCompare(right))
|
|
226
|
-
.map(([key, value]) => `${key}=${String(value)}`)
|
|
227
|
-
.join(';')}|${serializeSqliteTableNames(resolvedTableNames)}`;
|
|
228
|
-
const cached = SQLITE_CONNECTION_CACHE.get(cacheKey);
|
|
229
|
-
if (cached) {
|
|
230
|
-
return cached;
|
|
231
|
-
}
|
|
232
|
-
const onClose = () => {
|
|
233
|
-
SQLITE_CONNECTION_CACHE.delete(cacheKey);
|
|
234
|
-
};
|
|
235
|
-
const created = driver === 'bun'
|
|
236
|
-
? openBunSqlite(normalizedOptions, resolveSql, onClose)
|
|
237
|
-
: openBetterSqlite(normalizedOptions, resolveSql, onClose);
|
|
238
|
-
const guarded = created.catch((error) => {
|
|
239
|
-
SQLITE_CONNECTION_CACHE.delete(cacheKey);
|
|
240
|
-
throw error;
|
|
241
|
-
});
|
|
242
|
-
SQLITE_CONNECTION_CACHE.set(cacheKey, guarded);
|
|
243
|
-
return guarded;
|
|
244
|
-
}
|
|
245
|
-
const SQLITE_CONNECTION_CACHE = new Map();
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { BaseSqliteStore } from '../../providers/sqlite/BaseSqliteStore.js';
|
|
2
|
-
import { asNumber, asOptionalString, asString } from '../../../util/coercion.js';
|
|
3
|
-
function decodeContactRow(row) {
|
|
4
|
-
return {
|
|
5
|
-
jid: asString(row.jid, 'mailbox_contacts.jid'),
|
|
6
|
-
displayName: asOptionalString(row.display_name, 'mailbox_contacts.display_name'),
|
|
7
|
-
pushName: asOptionalString(row.push_name, 'mailbox_contacts.push_name'),
|
|
8
|
-
lid: asOptionalString(row.lid, 'mailbox_contacts.lid'),
|
|
9
|
-
phoneNumber: asOptionalString(row.phone_number, 'mailbox_contacts.phone_number'),
|
|
10
|
-
lastUpdatedMs: asNumber(row.last_updated_ms, 'mailbox_contacts.last_updated_ms')
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
export class WaContactSqliteStore extends BaseSqliteStore {
|
|
14
|
-
constructor(options) {
|
|
15
|
-
super(options, ['mailbox']);
|
|
16
|
-
}
|
|
17
|
-
async upsert(record) {
|
|
18
|
-
const db = await this.getConnection();
|
|
19
|
-
this.upsertContactRow(db, record);
|
|
20
|
-
}
|
|
21
|
-
async upsertBatch(records) {
|
|
22
|
-
if (records.length === 0) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
await this.withTransaction((db) => {
|
|
26
|
-
for (const record of records) {
|
|
27
|
-
this.upsertContactRow(db, record);
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
async getByJid(jid) {
|
|
32
|
-
const db = await this.getConnection();
|
|
33
|
-
const row = db.get(`SELECT jid, display_name, push_name, lid, phone_number, last_updated_ms
|
|
34
|
-
FROM mailbox_contacts
|
|
35
|
-
WHERE session_id = ? AND jid = ?`, [this.options.sessionId, jid]);
|
|
36
|
-
return row ? decodeContactRow(row) : null;
|
|
37
|
-
}
|
|
38
|
-
async deleteByJid(jid) {
|
|
39
|
-
const db = await this.getConnection();
|
|
40
|
-
db.run(`DELETE FROM mailbox_contacts
|
|
41
|
-
WHERE session_id = ? AND jid = ?`, [this.options.sessionId, jid]);
|
|
42
|
-
const row = db.get('SELECT changes() AS total', []);
|
|
43
|
-
return row ? Number(row.total) : 0;
|
|
44
|
-
}
|
|
45
|
-
async clear() {
|
|
46
|
-
const db = await this.getConnection();
|
|
47
|
-
db.run('DELETE FROM mailbox_contacts WHERE session_id = ?', [this.options.sessionId]);
|
|
48
|
-
}
|
|
49
|
-
upsertContactRow(db, record) {
|
|
50
|
-
db.run(`INSERT INTO mailbox_contacts (
|
|
51
|
-
session_id,
|
|
52
|
-
jid,
|
|
53
|
-
display_name,
|
|
54
|
-
push_name,
|
|
55
|
-
lid,
|
|
56
|
-
phone_number,
|
|
57
|
-
last_updated_ms
|
|
58
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
59
|
-
ON CONFLICT(session_id, jid) DO UPDATE SET
|
|
60
|
-
display_name=COALESCE(excluded.display_name, mailbox_contacts.display_name),
|
|
61
|
-
push_name=COALESCE(excluded.push_name, mailbox_contacts.push_name),
|
|
62
|
-
lid=COALESCE(excluded.lid, mailbox_contacts.lid),
|
|
63
|
-
phone_number=COALESCE(excluded.phone_number, mailbox_contacts.phone_number),
|
|
64
|
-
last_updated_ms=excluded.last_updated_ms`, [
|
|
65
|
-
this.options.sessionId,
|
|
66
|
-
record.jid,
|
|
67
|
-
record.displayName ?? null,
|
|
68
|
-
record.pushName ?? null,
|
|
69
|
-
record.lid ?? null,
|
|
70
|
-
record.phoneNumber ?? null,
|
|
71
|
-
record.lastUpdatedMs
|
|
72
|
-
]);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { BaseSqliteStore } from '../../providers/sqlite/BaseSqliteStore.js';
|
|
2
|
-
import { asNumber, asString, resolvePositive } from '../../../util/coercion.js';
|
|
3
|
-
const DEFAULTS = Object.freeze({
|
|
4
|
-
ttlMs: 5 * 60 * 1000,
|
|
5
|
-
batchSize: 500
|
|
6
|
-
});
|
|
7
|
-
export class WaDeviceListSqliteStore extends BaseSqliteStore {
|
|
8
|
-
constructor(options, ttlMs = DEFAULTS.ttlMs, batchSize) {
|
|
9
|
-
super(options, ['deviceList']);
|
|
10
|
-
this.ttlMs = ttlMs;
|
|
11
|
-
this.batchSize = resolvePositive(batchSize, DEFAULTS.batchSize, 'deviceList.sqlite.batchSize');
|
|
12
|
-
}
|
|
13
|
-
async upsertUserDevicesBatch(snapshots) {
|
|
14
|
-
if (snapshots.length === 0) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
await this.withTransaction((db) => {
|
|
18
|
-
for (const snapshot of snapshots) {
|
|
19
|
-
this.upsertUserDevicesRow(db, snapshot);
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
async getUserDevicesBatch(userJids, nowMs = Date.now()) {
|
|
24
|
-
if (userJids.length === 0) {
|
|
25
|
-
return [];
|
|
26
|
-
}
|
|
27
|
-
const uniqueUserJids = [...new Set(userJids)];
|
|
28
|
-
return this.withTransaction((db) => {
|
|
29
|
-
const activeByUserJid = new Map();
|
|
30
|
-
const expiredUserJids = [];
|
|
31
|
-
for (let start = 0; start < uniqueUserJids.length; start += this.batchSize) {
|
|
32
|
-
const end = Math.min(start + this.batchSize, uniqueUserJids.length);
|
|
33
|
-
const batchLength = end - start;
|
|
34
|
-
const placeholders = '?, '.repeat(batchLength).slice(0, -2);
|
|
35
|
-
const params = [this.options.sessionId];
|
|
36
|
-
for (let index = start; index < end; index += 1) {
|
|
37
|
-
params.push(uniqueUserJids[index]);
|
|
38
|
-
}
|
|
39
|
-
const rows = db.all(`SELECT user_jid, device_jids_json, updated_at_ms, expires_at_ms
|
|
40
|
-
FROM device_list_cache
|
|
41
|
-
WHERE session_id = ? AND user_jid IN (${placeholders})`, params);
|
|
42
|
-
for (const row of rows) {
|
|
43
|
-
const userJid = asString(row.user_jid, 'device_list_cache.user_jid');
|
|
44
|
-
const expiresAtMs = asNumber(row.expires_at_ms, 'device_list_cache.expires_at_ms');
|
|
45
|
-
if (expiresAtMs <= nowMs) {
|
|
46
|
-
expiredUserJids.push(userJid);
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
activeByUserJid.set(userJid, {
|
|
50
|
-
userJid,
|
|
51
|
-
deviceJids: decodeDeviceJids(row.device_jids_json),
|
|
52
|
-
updatedAtMs: asNumber(row.updated_at_ms, 'device_list_cache.updated_at_ms')
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
if (expiredUserJids.length > 0) {
|
|
57
|
-
this.deleteUserDevicesByJids(db, expiredUserJids);
|
|
58
|
-
}
|
|
59
|
-
return userJids.map((userJid) => activeByUserJid.get(userJid) ?? null);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
async deleteUserDevices(userJid) {
|
|
63
|
-
const db = await this.getConnection();
|
|
64
|
-
db.run(`DELETE FROM device_list_cache
|
|
65
|
-
WHERE session_id = ? AND user_jid = ?`, [this.options.sessionId, userJid]);
|
|
66
|
-
const row = db.get('SELECT changes() AS total', []);
|
|
67
|
-
return row ? Number(row.total) : 0;
|
|
68
|
-
}
|
|
69
|
-
async cleanupExpired(nowMs) {
|
|
70
|
-
const db = await this.getConnection();
|
|
71
|
-
db.run(`DELETE FROM device_list_cache
|
|
72
|
-
WHERE session_id = ? AND expires_at_ms <= ?`, [this.options.sessionId, nowMs]);
|
|
73
|
-
const row = db.get('SELECT changes() AS total', []);
|
|
74
|
-
return row ? Number(row.total) : 0;
|
|
75
|
-
}
|
|
76
|
-
async clear() {
|
|
77
|
-
const db = await this.getConnection();
|
|
78
|
-
db.run('DELETE FROM device_list_cache WHERE session_id = ?', [this.options.sessionId]);
|
|
79
|
-
}
|
|
80
|
-
upsertUserDevicesRow(db, snapshot) {
|
|
81
|
-
db.run(`INSERT INTO device_list_cache (
|
|
82
|
-
session_id,
|
|
83
|
-
user_jid,
|
|
84
|
-
device_jids_json,
|
|
85
|
-
updated_at_ms,
|
|
86
|
-
expires_at_ms
|
|
87
|
-
) VALUES (?, ?, ?, ?, ?)
|
|
88
|
-
ON CONFLICT(session_id, user_jid) DO UPDATE SET
|
|
89
|
-
device_jids_json=excluded.device_jids_json,
|
|
90
|
-
updated_at_ms=excluded.updated_at_ms,
|
|
91
|
-
expires_at_ms=excluded.expires_at_ms`, [
|
|
92
|
-
this.options.sessionId,
|
|
93
|
-
snapshot.userJid,
|
|
94
|
-
JSON.stringify(snapshot.deviceJids),
|
|
95
|
-
snapshot.updatedAtMs,
|
|
96
|
-
snapshot.updatedAtMs + this.ttlMs
|
|
97
|
-
]);
|
|
98
|
-
}
|
|
99
|
-
deleteUserDevicesByJids(db, userJids) {
|
|
100
|
-
if (userJids.length === 0) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
for (let start = 0; start < userJids.length; start += this.batchSize) {
|
|
104
|
-
const end = Math.min(start + this.batchSize, userJids.length);
|
|
105
|
-
const batchLength = end - start;
|
|
106
|
-
const placeholders = '?, '.repeat(batchLength).slice(0, -2);
|
|
107
|
-
const params = [this.options.sessionId];
|
|
108
|
-
for (let index = start; index < end; index += 1) {
|
|
109
|
-
params.push(userJids[index]);
|
|
110
|
-
}
|
|
111
|
-
db.run(`DELETE FROM device_list_cache
|
|
112
|
-
WHERE session_id = ? AND user_jid IN (${placeholders})`, params);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
function decodeDeviceJids(raw) {
|
|
117
|
-
const json = asString(raw, 'device_list_cache.device_jids_json');
|
|
118
|
-
const parsed = JSON.parse(json);
|
|
119
|
-
if (!Array.isArray(parsed)) {
|
|
120
|
-
throw new Error('device_list_cache.device_jids_json must be an array');
|
|
121
|
-
}
|
|
122
|
-
const deviceJids = new Array(parsed.length);
|
|
123
|
-
for (let index = 0; index < parsed.length; index += 1) {
|
|
124
|
-
deviceJids[index] = asString(parsed[index], 'device_list_cache.device_jids_json entry');
|
|
125
|
-
}
|
|
126
|
-
return deviceJids;
|
|
127
|
-
}
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { BaseSqliteStore } from '../../providers/sqlite/BaseSqliteStore.js';
|
|
2
|
-
import { asOptionalBytes, asOptionalNumber, asOptionalString, asString } from '../../../util/coercion.js';
|
|
3
|
-
import { normalizeQueryLimit } from '../../../util/collections.js';
|
|
4
|
-
function decodeMessageRow(row) {
|
|
5
|
-
return {
|
|
6
|
-
id: asString(row.message_id, 'mailbox_messages.message_id'),
|
|
7
|
-
threadJid: asString(row.thread_jid, 'mailbox_messages.thread_jid'),
|
|
8
|
-
senderJid: asOptionalString(row.sender_jid, 'mailbox_messages.sender_jid'),
|
|
9
|
-
participantJid: asOptionalString(row.participant_jid, 'mailbox_messages.participant_jid'),
|
|
10
|
-
fromMe: Number(row.from_me) === 1,
|
|
11
|
-
timestampMs: asOptionalNumber(row.timestamp_ms, 'mailbox_messages.timestamp_ms'),
|
|
12
|
-
encType: asOptionalString(row.enc_type, 'mailbox_messages.enc_type'),
|
|
13
|
-
plaintext: asOptionalBytes(row.plaintext, 'mailbox_messages.plaintext'),
|
|
14
|
-
messageBytes: asOptionalBytes(row.message_bytes, 'mailbox_messages.message_bytes')
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
export class WaMessageSqliteStore extends BaseSqliteStore {
|
|
18
|
-
constructor(options) {
|
|
19
|
-
super(options, ['mailbox']);
|
|
20
|
-
}
|
|
21
|
-
async upsert(record) {
|
|
22
|
-
const db = await this.getConnection();
|
|
23
|
-
this.upsertMessageRow(db, record);
|
|
24
|
-
}
|
|
25
|
-
async upsertBatch(records) {
|
|
26
|
-
if (records.length === 0) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
await this.withTransaction((db) => {
|
|
30
|
-
for (const record of records) {
|
|
31
|
-
this.upsertMessageRow(db, record);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
async getById(id) {
|
|
36
|
-
const db = await this.getConnection();
|
|
37
|
-
const row = db.get(`SELECT
|
|
38
|
-
message_id,
|
|
39
|
-
thread_jid,
|
|
40
|
-
sender_jid,
|
|
41
|
-
participant_jid,
|
|
42
|
-
from_me,
|
|
43
|
-
timestamp_ms,
|
|
44
|
-
enc_type,
|
|
45
|
-
plaintext,
|
|
46
|
-
message_bytes
|
|
47
|
-
FROM mailbox_messages
|
|
48
|
-
WHERE session_id = ? AND message_id = ?`, [this.options.sessionId, id]);
|
|
49
|
-
return row ? decodeMessageRow(row) : null;
|
|
50
|
-
}
|
|
51
|
-
async listByThread(threadJid, limit, beforeTimestampMs) {
|
|
52
|
-
const db = await this.getConnection();
|
|
53
|
-
const normalizedLimit = normalizeQueryLimit(limit, 50);
|
|
54
|
-
const rows = beforeTimestampMs === undefined
|
|
55
|
-
? db.all(`SELECT
|
|
56
|
-
message_id,
|
|
57
|
-
thread_jid,
|
|
58
|
-
sender_jid,
|
|
59
|
-
participant_jid,
|
|
60
|
-
from_me,
|
|
61
|
-
timestamp_ms,
|
|
62
|
-
enc_type,
|
|
63
|
-
plaintext,
|
|
64
|
-
message_bytes
|
|
65
|
-
FROM mailbox_messages
|
|
66
|
-
WHERE session_id = ? AND thread_jid = ?
|
|
67
|
-
ORDER BY timestamp_ms DESC, message_id DESC
|
|
68
|
-
LIMIT ?`, [this.options.sessionId, threadJid, normalizedLimit])
|
|
69
|
-
: db.all(`SELECT
|
|
70
|
-
message_id,
|
|
71
|
-
thread_jid,
|
|
72
|
-
sender_jid,
|
|
73
|
-
participant_jid,
|
|
74
|
-
from_me,
|
|
75
|
-
timestamp_ms,
|
|
76
|
-
enc_type,
|
|
77
|
-
plaintext,
|
|
78
|
-
message_bytes
|
|
79
|
-
FROM mailbox_messages
|
|
80
|
-
WHERE session_id = ?
|
|
81
|
-
AND thread_jid = ?
|
|
82
|
-
AND timestamp_ms < ?
|
|
83
|
-
ORDER BY timestamp_ms DESC, message_id DESC
|
|
84
|
-
LIMIT ?`, [this.options.sessionId, threadJid, beforeTimestampMs, normalizedLimit]);
|
|
85
|
-
return rows.map(decodeMessageRow);
|
|
86
|
-
}
|
|
87
|
-
async deleteById(id) {
|
|
88
|
-
const db = await this.getConnection();
|
|
89
|
-
db.run(`DELETE FROM mailbox_messages
|
|
90
|
-
WHERE session_id = ? AND message_id = ?`, [this.options.sessionId, id]);
|
|
91
|
-
const row = db.get('SELECT changes() AS total', []);
|
|
92
|
-
return row ? Number(row.total) : 0;
|
|
93
|
-
}
|
|
94
|
-
async clear() {
|
|
95
|
-
const db = await this.getConnection();
|
|
96
|
-
db.run('DELETE FROM mailbox_messages WHERE session_id = ?', [this.options.sessionId]);
|
|
97
|
-
}
|
|
98
|
-
upsertMessageRow(db, record) {
|
|
99
|
-
db.run(`INSERT INTO mailbox_messages (
|
|
100
|
-
session_id,
|
|
101
|
-
message_id,
|
|
102
|
-
thread_jid,
|
|
103
|
-
sender_jid,
|
|
104
|
-
participant_jid,
|
|
105
|
-
from_me,
|
|
106
|
-
timestamp_ms,
|
|
107
|
-
enc_type,
|
|
108
|
-
plaintext,
|
|
109
|
-
message_bytes
|
|
110
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
111
|
-
ON CONFLICT(session_id, message_id) DO UPDATE SET
|
|
112
|
-
thread_jid=excluded.thread_jid,
|
|
113
|
-
sender_jid=excluded.sender_jid,
|
|
114
|
-
participant_jid=excluded.participant_jid,
|
|
115
|
-
from_me=excluded.from_me,
|
|
116
|
-
timestamp_ms=excluded.timestamp_ms,
|
|
117
|
-
enc_type=excluded.enc_type,
|
|
118
|
-
plaintext=excluded.plaintext,
|
|
119
|
-
message_bytes=excluded.message_bytes`, [
|
|
120
|
-
this.options.sessionId,
|
|
121
|
-
record.id,
|
|
122
|
-
record.threadJid,
|
|
123
|
-
record.senderJid ?? null,
|
|
124
|
-
record.participantJid ?? null,
|
|
125
|
-
record.fromMe ? 1 : 0,
|
|
126
|
-
record.timestampMs ?? null,
|
|
127
|
-
record.encType ?? null,
|
|
128
|
-
record.plaintext ?? null,
|
|
129
|
-
record.messageBytes ?? null
|
|
130
|
-
]);
|
|
131
|
-
}
|
|
132
|
-
}
|