react-native-ble-mesh 1.0.1 → 1.0.3
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 +42 -11
- package/package.json +11 -35
- package/src/index.d.ts +29 -1
- package/src/mesh/peer/PeerDiscovery.js +1 -1
- package/src/mesh/peer/PeerManager.js +1 -1
- package/src/mesh/router/MessageRouter.js +1 -1
- package/src/mesh/router/PathFinder.js +1 -1
- package/src/protocol/header.js +3 -9
- package/src/service/HandshakeManager.js +1 -1
- package/src/service/MeshService.js +1 -1
- package/src/service/text/TextManager.js +1 -1
- package/src/service/text/broadcast/BroadcastManager.js +1 -1
- package/src/service/text/channel/ChannelManager.js +1 -1
- package/src/transport/Transport.js +1 -1
- package/src/transport/adapters/index.js +14 -2
- package/src/transport/index.js +7 -13
- package/src/utils/bytes.js +18 -14
- package/src/utils/encoding.js +0 -102
- package/src/utils/index.js +6 -30
- package/CHANGELOG.md +0 -151
- package/docs/ARCHITECTURE.md +0 -384
- package/docs/CONTRIBUTING.md +0 -244
- package/docs/NODE_QUICKSTART.md +0 -259
- package/docs/api/constants_audio.js.html +0 -184
- package/docs/api/constants_ble.js.html +0 -165
- package/docs/api/constants_crypto.js.html +0 -107
- package/docs/api/constants_errors.js.html +0 -256
- package/docs/api/constants_events.js.html +0 -148
- package/docs/api/constants_index.js.html +0 -76
- package/docs/api/constants_protocol.js.html +0 -205
- package/docs/api/crypto_aead.js.html +0 -243
- package/docs/api/crypto_chacha20.js.html +0 -235
- package/docs/api/crypto_hkdf.js.html +0 -241
- package/docs/api/crypto_hmac.js.html +0 -197
- package/docs/api/crypto_index.js.html +0 -126
- package/docs/api/crypto_keys_KeyManager.js.html +0 -325
- package/docs/api/crypto_keys_KeyPair.js.html +0 -270
- package/docs/api/crypto_keys_SecureStorage.js.html +0 -273
- package/docs/api/crypto_keys_index.js.html +0 -86
- package/docs/api/crypto_noise_handshake.js.html +0 -464
- package/docs/api/crypto_noise_index.js.html +0 -81
- package/docs/api/crypto_noise_session.js.html +0 -307
- package/docs/api/crypto_noise_state.js.html +0 -322
- package/docs/api/crypto_poly1305.js.html +0 -167
- package/docs/api/crypto_sha256.js.html +0 -294
- package/docs/api/crypto_x25519.js.html +0 -208
- package/docs/api/errors_AudioError.js.html +0 -218
- package/docs/api/errors_ConnectionError.js.html +0 -163
- package/docs/api/errors_CryptoError.js.html +0 -157
- package/docs/api/errors_HandshakeError.js.html +0 -176
- package/docs/api/errors_MeshError.js.html +0 -154
- package/docs/api/errors_MessageError.js.html +0 -183
- package/docs/api/errors_ValidationError.js.html +0 -204
- package/docs/api/errors_index.js.html +0 -78
- package/docs/api/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Bold-webfont.svg +0 -1830
- package/docs/api/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-BoldItalic-webfont.svg +0 -1830
- package/docs/api/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Italic-webfont.svg +0 -1830
- package/docs/api/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-Light-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Light-webfont.svg +0 -1831
- package/docs/api/fonts/OpenSans-Light-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-LightItalic-webfont.svg +0 -1835
- package/docs/api/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Regular-webfont.svg +0 -1831
- package/docs/api/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/docs/api/hooks_AppStateManager.js.html +0 -233
- package/docs/api/hooks_index.js.html +0 -87
- package/docs/api/hooks_useMesh.js.html +0 -213
- package/docs/api/hooks_useMessages.js.html +0 -263
- package/docs/api/hooks_usePeers.js.html +0 -165
- package/docs/api/index.html +0 -868
- package/docs/api/index.js.html +0 -236
- package/docs/api/mesh_dedup_BloomFilter.js.html +0 -261
- package/docs/api/mesh_dedup_DedupManager.js.html +0 -266
- package/docs/api/mesh_dedup_MessageCache.js.html +0 -273
- package/docs/api/mesh_dedup_index.js.html +0 -70
- package/docs/api/mesh_fragment_Assembler.js.html +0 -335
- package/docs/api/mesh_fragment_Fragmenter.js.html +0 -230
- package/docs/api/mesh_fragment_index.js.html +0 -75
- package/docs/api/mesh_index.js.html +0 -72
- package/docs/api/mesh_peer_Peer.js.html +0 -296
- package/docs/api/mesh_peer_PeerDiscovery.js.html +0 -334
- package/docs/api/mesh_peer_PeerManager.js.html +0 -320
- package/docs/api/mesh_peer_index.js.html +0 -70
- package/docs/api/mesh_router_MessageRouter.js.html +0 -411
- package/docs/api/mesh_router_PathFinder.js.html +0 -386
- package/docs/api/mesh_router_RouteTable.js.html +0 -346
- package/docs/api/mesh_router_index.js.html +0 -70
- package/docs/api/module-audio_buffer.html +0 -168
- package/docs/api/module-audio_buffer_FrameBuffer-FrameBuffer.html +0 -2971
- package/docs/api/module-audio_buffer_FrameBuffer.html +0 -178
- package/docs/api/module-audio_buffer_JitterBuffer-JitterBuffer.html +0 -2761
- package/docs/api/module-audio_buffer_JitterBuffer.html +0 -178
- package/docs/api/module-audio_codec.html +0 -168
- package/docs/api/module-audio_codec_LC3Codec-LC3Codec.html +0 -2876
- package/docs/api/module-audio_codec_LC3Codec.html +0 -178
- package/docs/api/module-audio_codec_LC3Decoder-LC3Decoder.html +0 -1788
- package/docs/api/module-audio_codec_LC3Decoder.html +0 -178
- package/docs/api/module-audio_codec_LC3Encoder-LC3Encoder.html +0 -1512
- package/docs/api/module-audio_codec_LC3Encoder.html +0 -178
- package/docs/api/module-audio_session.html +0 -168
- package/docs/api/module-audio_session_AudioSession-AudioSession.html +0 -3922
- package/docs/api/module-audio_session_AudioSession.html +0 -178
- package/docs/api/module-audio_session_VoiceMessage-VoiceMessage.html +0 -3690
- package/docs/api/module-audio_session_VoiceMessage-VoiceMessageRecorder.html +0 -1780
- package/docs/api/module-audio_session_VoiceMessage.html +0 -332
- package/docs/api/module-audio_transport.html +0 -168
- package/docs/api/module-audio_transport_AudioFragmenter-AudioAssembler.html +0 -1545
- package/docs/api/module-audio_transport_AudioFragmenter-AudioFragmenter.html +0 -658
- package/docs/api/module-audio_transport_AudioFragmenter.html +0 -181
- package/docs/api/module-audio_transport_AudioFramer.html +0 -1414
- package/docs/api/module-constants.html +0 -168
- package/docs/api/module-constants_audio.html +0 -1782
- package/docs/api/module-constants_ble.html +0 -940
- package/docs/api/module-constants_crypto.html +0 -823
- package/docs/api/module-constants_errors.html +0 -316
- package/docs/api/module-constants_events.html +0 -244
- package/docs/api/module-constants_protocol.html +0 -1534
- package/docs/api/module-crypto.html +0 -169
- package/docs/api/module-crypto_aead.html +0 -1625
- package/docs/api/module-crypto_chacha20.html +0 -1440
- package/docs/api/module-crypto_hkdf.html +0 -1421
- package/docs/api/module-crypto_hmac.html +0 -828
- package/docs/api/module-crypto_keys.html +0 -169
- package/docs/api/module-crypto_keys_KeyManager-KeyManager.html +0 -2364
- package/docs/api/module-crypto_keys_KeyManager.html +0 -252
- package/docs/api/module-crypto_keys_KeyPair.html +0 -245
- package/docs/api/module-crypto_keys_SecureStorage-MemorySecureStorage.html +0 -923
- package/docs/api/module-crypto_keys_SecureStorage-SecureStorage.html +0 -942
- package/docs/api/module-crypto_keys_SecureStorage.html +0 -516
- package/docs/api/module-crypto_noise.html +0 -169
- package/docs/api/module-crypto_noise_handshake-NoiseHandshake.html +0 -2240
- package/docs/api/module-crypto_noise_handshake.html +0 -782
- package/docs/api/module-crypto_noise_session-NoiseSession.html +0 -1804
- package/docs/api/module-crypto_noise_session.html +0 -325
- package/docs/api/module-crypto_noise_state-SymmetricState.html +0 -1387
- package/docs/api/module-crypto_noise_state.html +0 -324
- package/docs/api/module-crypto_poly1305.html +0 -884
- package/docs/api/module-crypto_sha256-HashContext.html +0 -447
- package/docs/api/module-crypto_sha256.html +0 -942
- package/docs/api/module-crypto_x25519.html +0 -1503
- package/docs/api/module-errors.html +0 -168
- package/docs/api/module-errors_AudioError-AudioError.html +0 -4711
- package/docs/api/module-errors_AudioError.html +0 -178
- package/docs/api/module-errors_ConnectionError-ConnectionError.html +0 -3649
- package/docs/api/module-errors_ConnectionError.html +0 -178
- package/docs/api/module-errors_CryptoError-CryptoError.html +0 -3453
- package/docs/api/module-errors_CryptoError.html +0 -178
- package/docs/api/module-errors_HandshakeError-HandshakeError.html +0 -4261
- package/docs/api/module-errors_HandshakeError.html +0 -178
- package/docs/api/module-errors_MeshError-MeshError.html +0 -2155
- package/docs/api/module-errors_MeshError.html +0 -178
- package/docs/api/module-errors_MessageError-MessageError.html +0 -4545
- package/docs/api/module-errors_MessageError.html +0 -178
- package/docs/api/module-errors_ValidationError-ValidationError.html +0 -3432
- package/docs/api/module-errors_ValidationError.html +0 -178
- package/docs/api/module-hooks.html +0 -182
- package/docs/api/module-hooks_AppStateManager-AppStateManager.html +0 -1620
- package/docs/api/module-hooks_AppStateManager.html +0 -178
- package/docs/api/module-hooks_useMesh.html +0 -457
- package/docs/api/module-hooks_useMessages.html +0 -466
- package/docs/api/module-hooks_usePeers.html +0 -348
- package/docs/api/module-mesh.html +0 -168
- package/docs/api/module-mesh_dedup.html +0 -168
- package/docs/api/module-mesh_dedup_BloomFilter-BloomFilter.html +0 -2158
- package/docs/api/module-mesh_dedup_BloomFilter.html +0 -178
- package/docs/api/module-mesh_dedup_DedupManager-DedupManager.html +0 -2880
- package/docs/api/module-mesh_dedup_DedupManager.html +0 -178
- package/docs/api/module-mesh_dedup_MessageCache-CacheNode.html +0 -246
- package/docs/api/module-mesh_dedup_MessageCache-MessageCache.html +0 -2314
- package/docs/api/module-mesh_dedup_MessageCache.html +0 -181
- package/docs/api/module-mesh_fragment.html +0 -168
- package/docs/api/module-mesh_fragment_Assembler-Assembler.html +0 -2869
- package/docs/api/module-mesh_fragment_Assembler-PendingFragmentSet.html +0 -895
- package/docs/api/module-mesh_fragment_Assembler.html +0 -181
- package/docs/api/module-mesh_fragment_Fragmenter.html +0 -1084
- package/docs/api/module-mesh_peer.html +0 -168
- package/docs/api/module-mesh_peer_Peer-Peer.html +0 -4986
- package/docs/api/module-mesh_peer_Peer.html +0 -178
- package/docs/api/module-mesh_peer_PeerDiscovery-PeerDiscovery.html +0 -3423
- package/docs/api/module-mesh_peer_PeerDiscovery.html +0 -438
- package/docs/api/module-mesh_peer_PeerManager-PeerManager.html +0 -5258
- package/docs/api/module-mesh_peer_PeerManager.html +0 -178
- package/docs/api/module-mesh_router.html +0 -168
- package/docs/api/module-mesh_router_MessageRouter-MessageRouter.html +0 -3285
- package/docs/api/module-mesh_router_MessageRouter.html +0 -178
- package/docs/api/module-mesh_router_PathFinder-PathFinder.html +0 -3323
- package/docs/api/module-mesh_router_PathFinder.html +0 -421
- package/docs/api/module-mesh_router_RouteTable-RouteTable.html +0 -4115
- package/docs/api/module-mesh_router_RouteTable.html +0 -421
- package/docs/api/module-protocol.html +0 -169
- package/docs/api/module-protocol_crc32.html +0 -815
- package/docs/api/module-protocol_deserializer.html +0 -1393
- package/docs/api/module-protocol_header-MessageHeader.html +0 -2879
- package/docs/api/module-protocol_header.html +0 -892
- package/docs/api/module-protocol_message-Message.html +0 -4682
- package/docs/api/module-protocol_message.html +0 -178
- package/docs/api/module-protocol_serializer.html +0 -911
- package/docs/api/module-protocol_validator.html +0 -1396
- package/docs/api/module-rn-ble-mesh.html +0 -866
- package/docs/api/module-service.html +0 -168
- package/docs/api/module-service_HandshakeManager-HandshakeManager.html +0 -185
- package/docs/api/module-service_HandshakeManager.html +0 -175
- package/docs/api/module-service_MeshService-MeshService.html +0 -185
- package/docs/api/module-service_MeshService.html +0 -175
- package/docs/api/module-service_SessionManager-SessionManager.html +0 -174
- package/docs/api/module-service_SessionManager.html +0 -175
- package/docs/api/module-service_audio.html +0 -168
- package/docs/api/module-service_audio_AudioManager-AudioManager.html +0 -4653
- package/docs/api/module-service_audio_AudioManager.html +0 -254
- package/docs/api/module-service_text.html +0 -168
- package/docs/api/module-service_text_TextManager-TextManager.html +0 -6104
- package/docs/api/module-service_text_TextManager.html +0 -254
- package/docs/api/module-service_text_broadcast.html +0 -168
- package/docs/api/module-service_text_broadcast_BroadcastManager-BroadcastManager.html +0 -2434
- package/docs/api/module-service_text_broadcast_BroadcastManager.html +0 -254
- package/docs/api/module-service_text_channel.html +0 -168
- package/docs/api/module-service_text_channel_Channel-Channel.html +0 -4337
- package/docs/api/module-service_text_channel_Channel.html +0 -178
- package/docs/api/module-service_text_channel_ChannelManager-ChannelManager.html +0 -1927
- package/docs/api/module-service_text_channel_ChannelManager.html +0 -175
- package/docs/api/module-service_text_message.html +0 -168
- package/docs/api/module-service_text_message_TextMessage-TextMessage.html +0 -4162
- package/docs/api/module-service_text_message_TextMessage.html +0 -178
- package/docs/api/module-service_text_message_TextSerializer.html +0 -1725
- package/docs/api/module-storage.html +0 -168
- package/docs/api/module-storage_AsyncStorageAdapter-AsyncStorageAdapter.html +0 -4159
- package/docs/api/module-storage_AsyncStorageAdapter.html +0 -178
- package/docs/api/module-storage_MemoryStorage-MemoryStorage.html +0 -3154
- package/docs/api/module-storage_MemoryStorage.html +0 -178
- package/docs/api/module-storage_MessageStore-MessageStore.html +0 -5299
- package/docs/api/module-storage_MessageStore.html +0 -178
- package/docs/api/module-storage_Storage-Storage.html +0 -4169
- package/docs/api/module-storage_Storage.html +0 -178
- package/docs/api/module-transport.html +0 -168
- package/docs/api/module-transport_BLEAdapter-BLEAdapter.html +0 -4724
- package/docs/api/module-transport_BLEAdapter.html +0 -178
- package/docs/api/module-transport_BLETransport-BLETransport.html +0 -3263
- package/docs/api/module-transport_BLETransport.html +0 -178
- package/docs/api/module-transport_MockTransport-MockTransport.html +0 -3947
- package/docs/api/module-transport_MockTransport.html +0 -178
- package/docs/api/module-transport_NodeBLEAdapter-NodeBLEAdapter.html +0 -3216
- package/docs/api/module-transport_NodeBLEAdapter.html +0 -178
- package/docs/api/module-transport_RNBLEAdapter-RNBLEAdapter.html +0 -3216
- package/docs/api/module-transport_RNBLEAdapter.html +0 -178
- package/docs/api/module-transport_Transport-Transport.html +0 -4071
- package/docs/api/module-transport_Transport.html +0 -254
- package/docs/api/module-transport_adapters.html +0 -168
- package/docs/api/module-transport_adapters_BLEAdapter-BLEAdapter.html +0 -4724
- package/docs/api/module-transport_adapters_BLEAdapter.html +0 -178
- package/docs/api/module-transport_adapters_NodeBLEAdapter-NodeBLEAdapter.html +0 -3216
- package/docs/api/module-transport_adapters_NodeBLEAdapter.html +0 -178
- package/docs/api/module-transport_adapters_RNBLEAdapter-RNBLEAdapter.html +0 -3216
- package/docs/api/module-transport_adapters_RNBLEAdapter.html +0 -178
- package/docs/api/module-utils.html +0 -168
- package/docs/api/module-utils_BoundedMap-BoundedMap.html +0 -3301
- package/docs/api/module-utils_BoundedMap.html +0 -178
- package/docs/api/module-utils_EventEmitter-EventEmitter.html +0 -3358
- package/docs/api/module-utils_EventEmitter.html +0 -178
- package/docs/api/module-utils_LRUCache-LRUCache.html +0 -4134
- package/docs/api/module-utils_LRUCache.html +0 -178
- package/docs/api/module-utils_RateLimiter-RateLimiter.html +0 -2176
- package/docs/api/module-utils_RateLimiter.html +0 -500
- package/docs/api/module-utils_TimeoutManager-TimeoutManager.html +0 -1781
- package/docs/api/module-utils_TimeoutManager.html +0 -175
- package/docs/api/module-utils_bytes.html +0 -1789
- package/docs/api/module-utils_debug.html +0 -837
- package/docs/api/module-utils_encoding.html +0 -1184
- package/docs/api/module-utils_retry.html +0 -1457
- package/docs/api/module-utils_time.html +0 -1665
- package/docs/api/module-utils_uuid.html +0 -1269
- package/docs/api/module-utils_validation.html +0 -2176
- package/docs/api/protocol_crc32.js.html +0 -147
- package/docs/api/protocol_deserializer.js.html +0 -295
- package/docs/api/protocol_header.js.html +0 -276
- package/docs/api/protocol_index.js.html +0 -120
- package/docs/api/protocol_message.js.html +0 -287
- package/docs/api/protocol_serializer.js.html +0 -240
- package/docs/api/protocol_validator.js.html +0 -330
- package/docs/api/scripts/linenumber.js +0 -25
- package/docs/api/scripts/prettify/Apache-License-2.0.txt +0 -202
- package/docs/api/scripts/prettify/lang-css.js +0 -2
- package/docs/api/scripts/prettify/prettify.js +0 -28
- package/docs/api/service_HandshakeManager.js.html +0 -232
- package/docs/api/service_MeshService.js.html +0 -371
- package/docs/api/service_SessionManager.js.html +0 -153
- package/docs/api/service_audio_AudioManager.js.html +0 -541
- package/docs/api/service_audio_buffer_FrameBuffer.js.html +0 -223
- package/docs/api/service_audio_buffer_JitterBuffer.js.html +0 -244
- package/docs/api/service_audio_buffer_index.js.html +0 -68
- package/docs/api/service_audio_codec_LC3Codec.js.html +0 -345
- package/docs/api/service_audio_codec_LC3Decoder.js.html +0 -185
- package/docs/api/service_audio_codec_LC3Encoder.js.html +0 -194
- package/docs/api/service_audio_codec_index.js.html +0 -70
- package/docs/api/service_audio_index.js.html +0 -96
- package/docs/api/service_audio_session_AudioSession.js.html +0 -348
- package/docs/api/service_audio_session_VoiceMessage.js.html +0 -432
- package/docs/api/service_audio_session_index.js.html +0 -70
- package/docs/api/service_audio_transport_AudioFragmenter.js.html +0 -314
- package/docs/api/service_audio_transport_AudioFramer.js.html +0 -236
- package/docs/api/service_audio_transport_index.js.html +0 -69
- package/docs/api/service_index.js.html +0 -93
- package/docs/api/service_text_TextManager.js.html +0 -578
- package/docs/api/service_text_broadcast_BroadcastManager.js.html +0 -276
- package/docs/api/service_text_broadcast_index.js.html +0 -66
- package/docs/api/service_text_channel_Channel.js.html +0 -280
- package/docs/api/service_text_channel_ChannelManager.js.html +0 -225
- package/docs/api/service_text_channel_index.js.html +0 -68
- package/docs/api/service_text_index.js.html +0 -85
- package/docs/api/service_text_message_TextMessage.js.html +0 -350
- package/docs/api/service_text_message_TextSerializer.js.html +0 -218
- package/docs/api/service_text_message_index.js.html +0 -68
- package/docs/api/storage_AsyncStorageAdapter.js.html +0 -357
- package/docs/api/storage_MemoryStorage.js.html +0 -279
- package/docs/api/storage_MessageStore.js.html +0 -369
- package/docs/api/storage_Storage.js.html +0 -214
- package/docs/api/storage_index.js.html +0 -72
- package/docs/api/styles/jsdoc-default.css +0 -358
- package/docs/api/styles/prettify-jsdoc.css +0 -111
- package/docs/api/styles/prettify-tomorrow.css +0 -132
- package/docs/api/transport_BLEAdapter.js.html +0 -231
- package/docs/api/transport_BLETransport.js.html +0 -411
- package/docs/api/transport_MockTransport.js.html +0 -339
- package/docs/api/transport_NodeBLEAdapter.js.html +0 -479
- package/docs/api/transport_RNBLEAdapter.js.html +0 -382
- package/docs/api/transport_Transport.js.html +0 -242
- package/docs/api/transport_adapters_BLEAdapter.js.html +0 -231
- package/docs/api/transport_adapters_NodeBLEAdapter.js.html +0 -479
- package/docs/api/transport_adapters_RNBLEAdapter.js.html +0 -382
- package/docs/api/transport_adapters_index.js.html +0 -70
- package/docs/api/transport_index.js.html +0 -87
- package/docs/api/utils_BoundedMap.js.html +0 -205
- package/docs/api/utils_EventEmitter.js.html +0 -259
- package/docs/api/utils_LRUCache.js.html +0 -256
- package/docs/api/utils_RateLimiter.js.html +0 -256
- package/docs/api/utils_TimeoutManager.js.html +0 -218
- package/docs/api/utils_base64%0A%0AThis%20implementation%20avoids%20string%20concatenation%20in%20loops%20which%20is%20O(n%C2%B2).%0AUses%20array%20building%20which%20is%20O(n)%20-%20critical%20for%20React%20Native%20performance.module_.html +0 -717
- package/docs/api/utils_base64.js.html +0 -205
- package/docs/api/utils_bytes.js.html +0 -241
- package/docs/api/utils_debug.js.html +0 -205
- package/docs/api/utils_encoding.js.html +0 -302
- package/docs/api/utils_index.js.html +0 -160
- package/docs/api/utils_retry.js.html +0 -200
- package/docs/api/utils_time.js.html +0 -220
- package/docs/api/utils_uuid.js.html +0 -199
- package/docs/api/utils_validation.js.html +0 -259
- package/examples/node-chat/chat.js +0 -220
- package/examples/node-quickstart/index.js +0 -94
- package/examples/testing/test-helper.js +0 -182
- package/src/transport/BLEAdapter.js +0 -177
- package/src/transport/NodeBLEAdapter.js +0 -425
- package/src/transport/RNBLEAdapter.js +0 -328
- package/src/utils/BoundedMap.js +0 -151
- package/src/utils/TimeoutManager.js +0 -164
- package/src/utils/debug.js +0 -151
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
> Inspired by [Bitchat](https://github.com/permissionlesstech/bitchat) - this is the React Native version.
|
|
4
4
|
|
|
5
|
-
A **production-ready BLE Mesh Network library** for
|
|
5
|
+
A **production-ready BLE Mesh Network library** for React Native with Noise Protocol security. This library enables peer-to-peer communication over Bluetooth Low Energy with end-to-end encryption, multi-hop routing, and offline-first capabilities.
|
|
6
6
|
|
|
7
7
|
[](https://www.npmjs.com/package/react-native-ble-mesh)
|
|
8
8
|
[](https://opensource.org/licenses/MIT)
|
|
@@ -32,24 +32,55 @@ A **production-ready BLE Mesh Network library** for Node.js and React Native wit
|
|
|
32
32
|
## Installation
|
|
33
33
|
|
|
34
34
|
```bash
|
|
35
|
-
npm install react-native-ble-mesh
|
|
35
|
+
npm install react-native-ble-mesh react-native-get-random-values react-native-ble-plx
|
|
36
|
+
cd ios && pod install
|
|
36
37
|
```
|
|
37
38
|
|
|
38
|
-
###
|
|
39
|
+
### Required Setup
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
**IMPORTANT:** You must import `react-native-get-random-values` at the very top of your entry file (index.js or App.js) **before any other imports**:
|
|
41
42
|
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
```javascript
|
|
44
|
+
// index.js or App.js - This MUST be the first import!
|
|
45
|
+
import 'react-native-get-random-values';
|
|
46
|
+
|
|
47
|
+
// Now you can import other modules
|
|
48
|
+
import { AppRegistry } from 'react-native';
|
|
49
|
+
import App from './App';
|
|
50
|
+
import { name as appName } from './app.json';
|
|
51
|
+
|
|
52
|
+
AppRegistry.registerComponent(appName, () => App);
|
|
45
53
|
```
|
|
46
54
|
|
|
47
|
-
|
|
55
|
+
This polyfill provides the Web Crypto API (`crypto.getRandomValues`) which is required for cryptographic operations in the mesh network.
|
|
48
56
|
|
|
49
|
-
|
|
57
|
+
### iOS Setup
|
|
50
58
|
|
|
51
|
-
|
|
52
|
-
|
|
59
|
+
Add Bluetooth permissions to your `Info.plist`:
|
|
60
|
+
|
|
61
|
+
```xml
|
|
62
|
+
<key>NSBluetoothAlwaysUsageDescription</key>
|
|
63
|
+
<string>This app uses Bluetooth to communicate with nearby devices</string>
|
|
64
|
+
<key>NSBluetoothPeripheralUsageDescription</key>
|
|
65
|
+
<string>This app uses Bluetooth to communicate with nearby devices</string>
|
|
66
|
+
<key>UIBackgroundModes</key>
|
|
67
|
+
<array>
|
|
68
|
+
<string>bluetooth-central</string>
|
|
69
|
+
<string>bluetooth-peripheral</string>
|
|
70
|
+
</array>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Android Setup
|
|
74
|
+
|
|
75
|
+
Add Bluetooth permissions to your `AndroidManifest.xml`:
|
|
76
|
+
|
|
77
|
+
```xml
|
|
78
|
+
<uses-permission android:name="android.permission.BLUETOOTH" />
|
|
79
|
+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
|
|
80
|
+
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
|
|
81
|
+
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
|
|
82
|
+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
|
|
83
|
+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
|
53
84
|
```
|
|
54
85
|
|
|
55
86
|
## Quick Start
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-ble-mesh",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Production-ready BLE Mesh Network with Noise Protocol security",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "Production-ready React Native BLE Mesh Network with Noise Protocol security",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
7
7
|
"exports": {
|
|
@@ -14,10 +14,6 @@
|
|
|
14
14
|
"require": "./src/transport/adapters/index.js",
|
|
15
15
|
"default": "./src/transport/adapters/index.js"
|
|
16
16
|
},
|
|
17
|
-
"./errors": {
|
|
18
|
-
"require": "./src/errors/index.js",
|
|
19
|
-
"default": "./src/errors/index.js"
|
|
20
|
-
},
|
|
21
17
|
"./crypto": {
|
|
22
18
|
"require": "./src/crypto/index.js",
|
|
23
19
|
"default": "./src/crypto/index.js"
|
|
@@ -29,35 +25,20 @@
|
|
|
29
25
|
"./hooks": {
|
|
30
26
|
"require": "./src/hooks/index.js",
|
|
31
27
|
"default": "./src/hooks/index.js"
|
|
32
|
-
}
|
|
33
|
-
"./package.json": "./package.json"
|
|
28
|
+
}
|
|
34
29
|
},
|
|
35
30
|
"sideEffects": false,
|
|
36
|
-
"
|
|
37
|
-
"node": ">=18.0.0"
|
|
38
|
-
},
|
|
31
|
+
"react-native": "src/index.js",
|
|
39
32
|
"scripts": {
|
|
40
33
|
"test": "jest",
|
|
41
34
|
"test:coverage": "jest --coverage",
|
|
42
|
-
"test:watch": "jest --watch",
|
|
43
|
-
"test:ci": "jest --ci --coverage --reporters=default --reporters=jest-junit",
|
|
44
35
|
"lint": "eslint src/",
|
|
45
36
|
"lint:fix": "eslint src/ --fix",
|
|
46
37
|
"typecheck": "tsc --noEmit",
|
|
47
|
-
"
|
|
48
|
-
"validate": "npm run lint && npm test",
|
|
49
|
-
"prepublishOnly": "npm run validate",
|
|
50
|
-
"preversion": "npm run validate",
|
|
51
|
-
"version": "npm run docs && git add -A docs",
|
|
52
|
-
"postversion": "git push && git push --tags",
|
|
53
|
-
"release": "npm version patch && npm publish",
|
|
54
|
-
"release:minor": "npm version minor && npm publish",
|
|
55
|
-
"release:major": "npm version major && npm publish",
|
|
56
|
-
"size": "node -e \"const s=require('fs').statSync('src/index.js');console.log('Index size:',s.size,'bytes')\"",
|
|
57
|
-
"example:node": "node examples/node-quickstart/index.js",
|
|
58
|
-
"example:chat": "node examples/node-chat/chat.js"
|
|
38
|
+
"validate": "npm run lint && npm test"
|
|
59
39
|
},
|
|
60
40
|
"keywords": [
|
|
41
|
+
"react-native",
|
|
61
42
|
"bluetooth",
|
|
62
43
|
"ble",
|
|
63
44
|
"mesh",
|
|
@@ -65,8 +46,6 @@
|
|
|
65
46
|
"peer-to-peer",
|
|
66
47
|
"noise-protocol",
|
|
67
48
|
"encryption",
|
|
68
|
-
"react-native",
|
|
69
|
-
"node",
|
|
70
49
|
"offline-first",
|
|
71
50
|
"x25519",
|
|
72
51
|
"chacha20-poly1305"
|
|
@@ -76,29 +55,26 @@
|
|
|
76
55
|
"devDependencies": {
|
|
77
56
|
"eslint": "^8.57.0",
|
|
78
57
|
"jest": "^29.7.0",
|
|
79
|
-
"jest-junit": "^16.0.0",
|
|
80
|
-
"jsdoc": "^4.0.4",
|
|
81
58
|
"typescript": "^5.9.3"
|
|
82
59
|
},
|
|
83
60
|
"peerDependencies": {
|
|
84
|
-
"
|
|
61
|
+
"react-native": ">=0.60.0",
|
|
62
|
+
"react-native-get-random-values": ">=1.8.0",
|
|
85
63
|
"react-native-ble-plx": ">=2.0.0"
|
|
86
64
|
},
|
|
87
65
|
"peerDependenciesMeta": {
|
|
88
|
-
"react-native
|
|
66
|
+
"react-native": {
|
|
89
67
|
"optional": true
|
|
90
68
|
},
|
|
91
|
-
"
|
|
69
|
+
"react-native-ble-plx": {
|
|
92
70
|
"optional": true
|
|
93
71
|
}
|
|
94
72
|
},
|
|
95
73
|
"files": [
|
|
96
74
|
"src",
|
|
97
75
|
"docs",
|
|
98
|
-
"examples",
|
|
99
76
|
"README.md",
|
|
100
|
-
"LICENSE"
|
|
101
|
-
"CHANGELOG.md"
|
|
77
|
+
"LICENSE"
|
|
102
78
|
],
|
|
103
79
|
"repository": {
|
|
104
80
|
"type": "git",
|
package/src/index.d.ts
CHANGED
|
@@ -1,9 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* TypeScript definitions for react-native-ble-mesh
|
|
3
3
|
* BLE Mesh Network Library with Noise Protocol Security
|
|
4
|
+
*
|
|
5
|
+
* This library is designed for React Native applications.
|
|
6
|
+
* Required setup:
|
|
7
|
+
* npm install react-native-get-random-values
|
|
8
|
+
* // In your entry file (index.js or App.js), add at the TOP:
|
|
9
|
+
* import 'react-native-get-random-values';
|
|
4
10
|
*/
|
|
5
11
|
|
|
6
|
-
|
|
12
|
+
// ============================================================================
|
|
13
|
+
// EventEmitter (React Native compatible)
|
|
14
|
+
// ============================================================================
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React Native compatible EventEmitter interface.
|
|
18
|
+
* This replaces the Node.js 'events' module which is not available in React Native.
|
|
19
|
+
*/
|
|
20
|
+
export interface EventEmitter {
|
|
21
|
+
on(event: string, listener: (...args: unknown[]) => void): this;
|
|
22
|
+
once(event: string, listener: (...args: unknown[]) => void): this;
|
|
23
|
+
off(event: string, listener: (...args: unknown[]) => void): this;
|
|
24
|
+
emit(event: string, ...args: unknown[]): boolean;
|
|
25
|
+
removeAllListeners(event?: string): this;
|
|
26
|
+
listenerCount(event: string): number;
|
|
27
|
+
eventNames(): string[];
|
|
28
|
+
setMaxListeners(n: number): this;
|
|
29
|
+
getMaxListeners(): number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class EventEmitter implements EventEmitter {
|
|
33
|
+
constructor(options?: { maxListeners?: number });
|
|
34
|
+
}
|
|
7
35
|
|
|
8
36
|
// ============================================================================
|
|
9
37
|
// Constants
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @module mesh/peer/PeerDiscovery
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const EventEmitter = require('
|
|
8
|
+
const EventEmitter = require('../../utils/EventEmitter');
|
|
9
9
|
const { MESSAGE_TYPE, EVENTS } = require('../../constants');
|
|
10
10
|
const { ValidationError } = require('../../errors');
|
|
11
11
|
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @module mesh/peer/PeerManager
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const EventEmitter = require('
|
|
8
|
+
const EventEmitter = require('../../utils/EventEmitter');
|
|
9
9
|
const Peer = require('./Peer');
|
|
10
10
|
const { CONNECTION_STATE, MESH_CONFIG, EVENTS } = require('../../constants');
|
|
11
11
|
const { ValidationError, ConnectionError } = require('../../errors');
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @module mesh/router/MessageRouter
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const EventEmitter = require('
|
|
8
|
+
const EventEmitter = require('../../utils/EventEmitter');
|
|
9
9
|
const { MESH_CONFIG, MESSAGE_TYPE, MESSAGE_FLAGS, EVENTS } = require('../../constants');
|
|
10
10
|
const { ValidationError } = require('../../errors');
|
|
11
11
|
const { DedupManager } = require('../dedup');
|
package/src/protocol/header.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
const { crc32 } = require('./crc32');
|
|
9
9
|
const { PROTOCOL_VERSION, MESSAGE_FLAGS, MESH_CONFIG } = require('../constants');
|
|
10
10
|
const { MessageError } = require('../errors');
|
|
11
|
+
const { randomBytes } = require('../utils/bytes');
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Header size in bytes.
|
|
@@ -161,18 +162,11 @@ class MessageHeader {
|
|
|
161
162
|
}
|
|
162
163
|
|
|
163
164
|
/**
|
|
164
|
-
* Generates a random 16-byte UUID.
|
|
165
|
+
* Generates a random 16-byte UUID (v4).
|
|
165
166
|
* @returns {Uint8Array} 16-byte UUID
|
|
166
167
|
*/
|
|
167
168
|
function generateUuid() {
|
|
168
|
-
const uuid =
|
|
169
|
-
if (typeof globalThis.crypto !== 'undefined' && globalThis.crypto.getRandomValues) {
|
|
170
|
-
globalThis.crypto.getRandomValues(uuid);
|
|
171
|
-
} else {
|
|
172
|
-
const nodeCrypto = require('crypto');
|
|
173
|
-
const randomBuffer = nodeCrypto.randomBytes(16);
|
|
174
|
-
uuid.set(randomBuffer);
|
|
175
|
-
}
|
|
169
|
+
const uuid = randomBytes(16);
|
|
176
170
|
// Set version 4 (random) and variant bits
|
|
177
171
|
uuid[6] = (uuid[6] & 0x0f) | 0x40;
|
|
178
172
|
uuid[8] = (uuid[8] & 0x3f) | 0x80;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @module service/HandshakeManager
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const EventEmitter = require('
|
|
8
|
+
const EventEmitter = require('../utils/EventEmitter');
|
|
9
9
|
const { HandshakeError } = require('../errors');
|
|
10
10
|
const { MESSAGE_TYPE, MESH_CONFIG, EVENTS } = require('../constants');
|
|
11
11
|
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @module service/MeshService
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const EventEmitter = require('
|
|
8
|
+
const EventEmitter = require('../utils/EventEmitter');
|
|
9
9
|
const { MeshError, ValidationError, AudioError } = require('../errors');
|
|
10
10
|
const { SERVICE_STATE, MESSAGE_TYPE, EVENTS, ERROR_CODE } = require('../constants');
|
|
11
11
|
const SessionManager = require('./SessionManager');
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @module service/text/TextManager
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const EventEmitter = require('
|
|
8
|
+
const EventEmitter = require('../../utils/EventEmitter');
|
|
9
9
|
const { MeshError } = require('../../errors');
|
|
10
10
|
const { MESSAGE_TYPE, EVENTS, ERROR_CODE } = require('../../constants');
|
|
11
11
|
const TextMessage = require('./message/TextMessage');
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @module service/text/channel/ChannelManager
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
const EventEmitter = require('
|
|
8
|
+
const EventEmitter = require('../../../utils/EventEmitter');
|
|
9
9
|
const { MeshError } = require('../../../errors');
|
|
10
10
|
const { EVENTS, ERROR_CODE } = require('../../../constants');
|
|
11
11
|
const Channel = require('./Channel');
|
|
@@ -3,14 +3,26 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* @fileoverview BLE adapter exports
|
|
5
5
|
* @module transport/adapters
|
|
6
|
+
*
|
|
7
|
+
* Note: NodeBLEAdapter uses Node.js-specific APIs (Buffer) and is NOT
|
|
8
|
+
* auto-loaded to maintain React Native compatibility. Use getNodeBLEAdapter()
|
|
9
|
+
* for lazy loading in Node.js environments.
|
|
6
10
|
*/
|
|
7
11
|
|
|
8
12
|
const BLEAdapter = require('./BLEAdapter');
|
|
9
13
|
const RNBLEAdapter = require('./RNBLEAdapter');
|
|
10
|
-
const NodeBLEAdapter = require('./NodeBLEAdapter');
|
|
11
14
|
|
|
12
15
|
module.exports = {
|
|
16
|
+
// React Native compatible adapters
|
|
13
17
|
BLEAdapter,
|
|
14
18
|
RNBLEAdapter,
|
|
15
|
-
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get the NodeBLEAdapter for Node.js environments.
|
|
22
|
+
* This is lazy-loaded to prevent issues in React Native.
|
|
23
|
+
* @returns {typeof import('./NodeBLEAdapter')}
|
|
24
|
+
*/
|
|
25
|
+
getNodeBLEAdapter() {
|
|
26
|
+
return require('./NodeBLEAdapter');
|
|
27
|
+
}
|
|
16
28
|
};
|
package/src/transport/index.js
CHANGED
|
@@ -9,25 +9,19 @@ const Transport = require('./Transport');
|
|
|
9
9
|
const MockTransport = require('./MockTransport');
|
|
10
10
|
const BLETransport = require('./BLETransport');
|
|
11
11
|
|
|
12
|
-
//
|
|
13
|
-
const BLEAdapter = require('./BLEAdapter');
|
|
14
|
-
const RNBLEAdapter = require('./RNBLEAdapter');
|
|
15
|
-
const NodeBLEAdapter = require('./NodeBLEAdapter');
|
|
16
|
-
|
|
17
|
-
// Legacy: keep adapters object for backward compatibility
|
|
18
|
-
const adapters = require('./adapters');
|
|
12
|
+
// React Native compatible adapters (from adapters subdirectory)
|
|
13
|
+
const BLEAdapter = require('./adapters/BLEAdapter');
|
|
14
|
+
const RNBLEAdapter = require('./adapters/RNBLEAdapter');
|
|
19
15
|
|
|
20
16
|
module.exports = {
|
|
21
|
-
// Core transports
|
|
22
17
|
Transport,
|
|
23
18
|
MockTransport,
|
|
24
19
|
BLETransport,
|
|
25
|
-
|
|
26
|
-
// Adapters (flat exports for easy imports)
|
|
27
20
|
BLEAdapter,
|
|
28
21
|
RNBLEAdapter,
|
|
29
|
-
NodeBLEAdapter,
|
|
30
22
|
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
/** Lazy-load NodeBLEAdapter for Node.js environments */
|
|
24
|
+
getNodeBLEAdapter() {
|
|
25
|
+
return require('./adapters/NodeBLEAdapter');
|
|
26
|
+
}
|
|
33
27
|
};
|
package/src/utils/bytes.js
CHANGED
|
@@ -51,6 +51,14 @@ function constantTimeEqual(a, b) {
|
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Generates cryptographically secure random bytes
|
|
54
|
+
*
|
|
55
|
+
* For React Native, you must install and import 'react-native-get-random-values'
|
|
56
|
+
* at the top of your entry file (index.js or App.js) BEFORE any other imports:
|
|
57
|
+
*
|
|
58
|
+
* ```javascript
|
|
59
|
+
* import 'react-native-get-random-values';
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
54
62
|
* @param {number} length - Number of bytes to generate
|
|
55
63
|
* @returns {Uint8Array} Random bytes
|
|
56
64
|
* @throws {Error} If crypto is not available
|
|
@@ -66,29 +74,25 @@ function randomBytes(length) {
|
|
|
66
74
|
|
|
67
75
|
const bytes = new Uint8Array(length);
|
|
68
76
|
|
|
69
|
-
// Try Web Crypto API (browser
|
|
77
|
+
// Try Web Crypto API (browser, React Native with polyfill, modern Node.js)
|
|
70
78
|
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
71
79
|
crypto.getRandomValues(bytes);
|
|
72
80
|
return bytes;
|
|
73
81
|
}
|
|
74
82
|
|
|
75
|
-
// Try globalThis.crypto (Node.js 19+, browsers)
|
|
76
|
-
if (typeof globalThis
|
|
83
|
+
// Try globalThis.crypto (Node.js 19+, browsers, React Native with polyfill)
|
|
84
|
+
if (typeof globalThis !== 'undefined' &&
|
|
85
|
+
typeof globalThis.crypto !== 'undefined' &&
|
|
86
|
+
globalThis.crypto.getRandomValues) {
|
|
77
87
|
globalThis.crypto.getRandomValues(bytes);
|
|
78
88
|
return bytes;
|
|
79
89
|
}
|
|
80
90
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return bytes;
|
|
87
|
-
} catch (e) {
|
|
88
|
-
// Ignore and throw error below
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
throw new Error('No secure random number generator available');
|
|
91
|
+
throw new Error(
|
|
92
|
+
'No secure random number generator available. ' +
|
|
93
|
+
'For React Native, install and import "react-native-get-random-values" ' +
|
|
94
|
+
'at the top of your entry file before any other imports.'
|
|
95
|
+
);
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
/**
|
package/src/utils/encoding.js
CHANGED
|
@@ -5,10 +5,6 @@
|
|
|
5
5
|
* @module utils/encoding
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* Lookup tables for hex encoding
|
|
10
|
-
* @private
|
|
11
|
-
*/
|
|
12
8
|
const HEX_CHARS = '0123456789abcdef';
|
|
13
9
|
const HEX_LOOKUP = new Uint8Array(256);
|
|
14
10
|
|
|
@@ -36,14 +32,12 @@ function bytesToHex(bytes) {
|
|
|
36
32
|
* Converts a hexadecimal string to a byte array
|
|
37
33
|
* @param {string} hex - Hexadecimal string
|
|
38
34
|
* @returns {Uint8Array} Byte array
|
|
39
|
-
* @throws {Error} If hex string has invalid length or characters
|
|
40
35
|
*/
|
|
41
36
|
function hexToBytes(hex) {
|
|
42
37
|
if (typeof hex !== 'string') {
|
|
43
38
|
throw new Error('Input must be a string');
|
|
44
39
|
}
|
|
45
40
|
|
|
46
|
-
// Remove optional 0x prefix
|
|
47
41
|
if (hex.startsWith('0x') || hex.startsWith('0X')) {
|
|
48
42
|
hex = hex.slice(2);
|
|
49
43
|
}
|
|
@@ -53,107 +47,15 @@ function hexToBytes(hex) {
|
|
|
53
47
|
}
|
|
54
48
|
|
|
55
49
|
const bytes = new Uint8Array(hex.length / 2);
|
|
56
|
-
|
|
57
50
|
for (let i = 0; i < bytes.length; i++) {
|
|
58
51
|
const hi = HEX_LOOKUP[hex.charCodeAt(i * 2)];
|
|
59
52
|
const lo = HEX_LOOKUP[hex.charCodeAt(i * 2 + 1)];
|
|
60
|
-
|
|
61
|
-
if (hi === undefined || lo === undefined) {
|
|
62
|
-
throw new Error(`Invalid hex character at position ${i * 2}`);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
53
|
bytes[i] = (hi << 4) | lo;
|
|
66
54
|
}
|
|
67
55
|
|
|
68
56
|
return bytes;
|
|
69
57
|
}
|
|
70
58
|
|
|
71
|
-
/**
|
|
72
|
-
* Base64 character set
|
|
73
|
-
* @private
|
|
74
|
-
*/
|
|
75
|
-
const BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
76
|
-
const BASE64_LOOKUP = new Uint8Array(256);
|
|
77
|
-
|
|
78
|
-
// Initialize base64 lookup table
|
|
79
|
-
for (let i = 0; i < BASE64_CHARS.length; i++) {
|
|
80
|
-
BASE64_LOOKUP[BASE64_CHARS.charCodeAt(i)] = i;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Converts a byte array to a Base64 string
|
|
85
|
-
* @param {Uint8Array} bytes - Bytes to convert
|
|
86
|
-
* @returns {string} Base64 string
|
|
87
|
-
*/
|
|
88
|
-
function bytesToBase64(bytes) {
|
|
89
|
-
let result = '';
|
|
90
|
-
const len = bytes.length;
|
|
91
|
-
const remainder = len % 3;
|
|
92
|
-
|
|
93
|
-
// Process 3 bytes at a time
|
|
94
|
-
for (let i = 0; i < len - remainder; i += 3) {
|
|
95
|
-
const n = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
|
|
96
|
-
result += BASE64_CHARS[(n >> 18) & 0x3f];
|
|
97
|
-
result += BASE64_CHARS[(n >> 12) & 0x3f];
|
|
98
|
-
result += BASE64_CHARS[(n >> 6) & 0x3f];
|
|
99
|
-
result += BASE64_CHARS[n & 0x3f];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Handle remainder
|
|
103
|
-
if (remainder === 1) {
|
|
104
|
-
const n = bytes[len - 1];
|
|
105
|
-
result += BASE64_CHARS[(n >> 2) & 0x3f];
|
|
106
|
-
result += BASE64_CHARS[(n << 4) & 0x3f];
|
|
107
|
-
result += '==';
|
|
108
|
-
} else if (remainder === 2) {
|
|
109
|
-
const n = (bytes[len - 2] << 8) | bytes[len - 1];
|
|
110
|
-
result += BASE64_CHARS[(n >> 10) & 0x3f];
|
|
111
|
-
result += BASE64_CHARS[(n >> 4) & 0x3f];
|
|
112
|
-
result += BASE64_CHARS[(n << 2) & 0x3f];
|
|
113
|
-
result += '=';
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return result;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Converts a Base64 string to a byte array
|
|
121
|
-
* @param {string} base64 - Base64 string
|
|
122
|
-
* @returns {Uint8Array} Byte array
|
|
123
|
-
* @throws {Error} If base64 string is invalid
|
|
124
|
-
*/
|
|
125
|
-
function base64ToBytes(base64) {
|
|
126
|
-
if (typeof base64 !== 'string') {
|
|
127
|
-
throw new Error('Input must be a string');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Remove padding and calculate length
|
|
131
|
-
let paddingLength = 0;
|
|
132
|
-
if (base64.endsWith('==')) {
|
|
133
|
-
paddingLength = 2;
|
|
134
|
-
} else if (base64.endsWith('=')) {
|
|
135
|
-
paddingLength = 1;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const len = base64.length;
|
|
139
|
-
const outputLength = (len * 3) / 4 - paddingLength;
|
|
140
|
-
const bytes = new Uint8Array(outputLength);
|
|
141
|
-
|
|
142
|
-
let j = 0;
|
|
143
|
-
for (let i = 0; i < len; i += 4) {
|
|
144
|
-
const a = BASE64_LOOKUP[base64.charCodeAt(i)];
|
|
145
|
-
const b = BASE64_LOOKUP[base64.charCodeAt(i + 1)];
|
|
146
|
-
const c = BASE64_LOOKUP[base64.charCodeAt(i + 2)];
|
|
147
|
-
const d = BASE64_LOOKUP[base64.charCodeAt(i + 3)];
|
|
148
|
-
|
|
149
|
-
bytes[j++] = (a << 2) | (b >> 4);
|
|
150
|
-
if (j < outputLength) { bytes[j++] = ((b & 0x0f) << 4) | (c >> 2); }
|
|
151
|
-
if (j < outputLength) { bytes[j++] = ((c & 0x03) << 6) | d; }
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return bytes;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
59
|
/**
|
|
158
60
|
* Converts a UTF-8 string to a byte array
|
|
159
61
|
* @param {string} str - String to convert
|
|
@@ -179,7 +81,6 @@ function stringToBytes(str) {
|
|
|
179
81
|
bytes.push(0x80 | ((code >> 6) & 0x3f));
|
|
180
82
|
bytes.push(0x80 | (code & 0x3f));
|
|
181
83
|
} else {
|
|
182
|
-
// Surrogate pair
|
|
183
84
|
i++;
|
|
184
85
|
code = 0x10000 + (((code & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
|
|
185
86
|
bytes.push(0xf0 | (code >> 18));
|
|
@@ -229,7 +130,6 @@ function bytesToString(bytes) {
|
|
|
229
130
|
((byte2 & 0x3f) << 12) |
|
|
230
131
|
((byte3 & 0x3f) << 6) |
|
|
231
132
|
(byte4 & 0x3f);
|
|
232
|
-
// Convert to surrogate pair
|
|
233
133
|
const adjusted = codePoint - 0x10000;
|
|
234
134
|
result += String.fromCharCode(0xd800 + (adjusted >> 10), 0xdc00 + (adjusted & 0x3ff));
|
|
235
135
|
}
|
|
@@ -241,8 +141,6 @@ function bytesToString(bytes) {
|
|
|
241
141
|
module.exports = {
|
|
242
142
|
bytesToHex,
|
|
243
143
|
hexToBytes,
|
|
244
|
-
bytesToBase64,
|
|
245
|
-
base64ToBytes,
|
|
246
144
|
stringToBytes,
|
|
247
145
|
bytesToString
|
|
248
146
|
};
|