@crypto-asset-custody/utils
Utility structures. For generic use.
Async Intersection
Converge multiple async streams into one. Create async streams with manuel trigger capability. Make completely unrelated to events wait each other. Rx based solution to make two promises in independent services wait each other. For example, you need to send a request and this request will trigger socket messages in two other services and, you want to take action after both messages arrive. Streamify all and wait them at the intersection.
Jwt Authenticator
JWT authenticator implementation. Sign and verify jwts.
Back-off Strategy
Exponential back-off handler implementation. To handle socket terminations gracefully and not to overwhelm server with ddos like connection requests.
Config
App config loader. Requires config.<environment>.json files at target config folder. Shared configurations should go into <config_folder>/shared/_unpack.json. Also for environment variables you must add <config_folder>/.env.
Context
App context to store short-lived request specific info. Like request id, language preference, etc. No need to instantiate, just import and you're good to go.
Crypto
Hasher implementation. Supported algorithms:
- md5
- crc32
- sha256
- hmac
AES-256 cipher implementation. Encrypt and decrypt strings and objects.
Data Structures
Some data structure implementations.
- A really simple circular buffer implementation.
- Graph implementation. Comes with BFS, DFS, cyclic check and topological sort.
- Multi index container. See multi index container from c++ library boost. JS implementation of this data structure, but much simpler.
Helper
Some random helper functions.
send_request()get_numeric_chars()accumulate()sorter.<sort_type>(for numeric sorting, JS surprisingly sorts string based sorting by default)
Id Generator
Random id generator implementation. Based on nanoid. Defined character sets:
base58base64numericotp
Logger
Logger config implementation based on log4js. Create loggers with random categories and track log sources. File rotation by size. Prints following fields:
- project code
- instance id
- local ip
- log level
- request id
- category name
- actual log data
Math
Some math functions and random number generator.
Network
Socket client implementations.
Pg Socket
Postgres pg_notify socket client connection.
Zocket
Websocket client. Automatic reconnection logic.
Rx
Custom rxjs operator implementations.
Sequence
Memory and redis based sequence counter. Use redis sequence for counters that needs to be shared across services.
Tasks
Define tasks and execute them with some configurations.
Async Task
Define async task with single execution guarantee. Until the task instance is reset calling execute on the task will not trigger anything. Task is accepted as completed only if it's successful. If any error occurred during execution it will retry until the max retry limit is reached.
Periodic Tasks
Define periodic tasks. Errors properly handled.
PeriodicTask: Arbitrary task that will run at interval.QueueConsumePeriodicTask: As the name says, pass the queue and how you want the pushed items to be handled with a callback function.HeapConsumePeriodicTask: Like queue consumer task. But for heaps.
Queue and heap implementations are assumed to be instantiated by mnemonist package
Validator
Some type validation functions.