@xtr-dev/rondevu-client 0.13.0 → 0.17.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 +100 -381
- package/dist/api.d.ts +67 -116
- package/dist/api.js +201 -244
- package/dist/crypto-adapter.d.ts +37 -0
- package/dist/crypto-adapter.js +4 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +4 -1
- package/dist/node-crypto-adapter.d.ts +35 -0
- package/dist/node-crypto-adapter.js +80 -0
- package/dist/rondevu-signaler.d.ts +10 -7
- package/dist/rondevu-signaler.js +96 -64
- package/dist/rondevu.d.ts +199 -37
- package/dist/rondevu.js +513 -103
- package/dist/rpc-batcher.d.ts +61 -0
- package/dist/rpc-batcher.js +111 -0
- package/dist/web-crypto-adapter.d.ts +16 -0
- package/dist/web-crypto-adapter.js +52 -0
- package/package.json +1 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RPC Batcher - Throttles and batches RPC requests to reduce HTTP overhead
|
|
3
|
+
*/
|
|
4
|
+
export interface BatcherOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Maximum number of requests to batch together
|
|
7
|
+
* Default: 10
|
|
8
|
+
*/
|
|
9
|
+
maxBatchSize?: number;
|
|
10
|
+
/**
|
|
11
|
+
* Maximum time to wait before sending a batch (ms)
|
|
12
|
+
* Default: 50ms
|
|
13
|
+
*/
|
|
14
|
+
maxWaitTime?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Minimum time between batches (ms)
|
|
17
|
+
* Default: 10ms
|
|
18
|
+
*/
|
|
19
|
+
throttleInterval?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Batches and throttles RPC requests to optimize network usage
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const batcher = new RpcBatcher(
|
|
27
|
+
* (requests) => api.rpcBatch(requests),
|
|
28
|
+
* { maxBatchSize: 10, maxWaitTime: 50 }
|
|
29
|
+
* )
|
|
30
|
+
*
|
|
31
|
+
* // These will be batched together if called within maxWaitTime
|
|
32
|
+
* const result1 = await batcher.add(request1)
|
|
33
|
+
* const result2 = await batcher.add(request2)
|
|
34
|
+
* const result3 = await batcher.add(request3)
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare class RpcBatcher {
|
|
38
|
+
private queue;
|
|
39
|
+
private batchTimeout;
|
|
40
|
+
private lastBatchTime;
|
|
41
|
+
private options;
|
|
42
|
+
private sendBatch;
|
|
43
|
+
constructor(sendBatch: (requests: any[]) => Promise<any[]>, options?: BatcherOptions);
|
|
44
|
+
/**
|
|
45
|
+
* Add an RPC request to the batch queue
|
|
46
|
+
* Returns a promise that resolves when the request completes
|
|
47
|
+
*/
|
|
48
|
+
add(request: any): Promise<any>;
|
|
49
|
+
/**
|
|
50
|
+
* Flush the queue immediately
|
|
51
|
+
*/
|
|
52
|
+
flush(): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Get current queue size
|
|
55
|
+
*/
|
|
56
|
+
getQueueSize(): number;
|
|
57
|
+
/**
|
|
58
|
+
* Clear the queue without sending
|
|
59
|
+
*/
|
|
60
|
+
clear(): void;
|
|
61
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RPC Batcher - Throttles and batches RPC requests to reduce HTTP overhead
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Batches and throttles RPC requests to optimize network usage
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const batcher = new RpcBatcher(
|
|
10
|
+
* (requests) => api.rpcBatch(requests),
|
|
11
|
+
* { maxBatchSize: 10, maxWaitTime: 50 }
|
|
12
|
+
* )
|
|
13
|
+
*
|
|
14
|
+
* // These will be batched together if called within maxWaitTime
|
|
15
|
+
* const result1 = await batcher.add(request1)
|
|
16
|
+
* const result2 = await batcher.add(request2)
|
|
17
|
+
* const result3 = await batcher.add(request3)
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export class RpcBatcher {
|
|
21
|
+
constructor(sendBatch, options) {
|
|
22
|
+
this.queue = [];
|
|
23
|
+
this.batchTimeout = null;
|
|
24
|
+
this.lastBatchTime = 0;
|
|
25
|
+
this.sendBatch = sendBatch;
|
|
26
|
+
this.options = {
|
|
27
|
+
maxBatchSize: options?.maxBatchSize ?? 10,
|
|
28
|
+
maxWaitTime: options?.maxWaitTime ?? 50,
|
|
29
|
+
throttleInterval: options?.throttleInterval ?? 10,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Add an RPC request to the batch queue
|
|
34
|
+
* Returns a promise that resolves when the request completes
|
|
35
|
+
*/
|
|
36
|
+
async add(request) {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
this.queue.push({ request, resolve, reject });
|
|
39
|
+
// Send immediately if batch is full
|
|
40
|
+
if (this.queue.length >= this.options.maxBatchSize) {
|
|
41
|
+
this.flush();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
// Schedule batch if not already scheduled
|
|
45
|
+
if (!this.batchTimeout) {
|
|
46
|
+
this.batchTimeout = setTimeout(() => {
|
|
47
|
+
this.flush();
|
|
48
|
+
}, this.options.maxWaitTime);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Flush the queue immediately
|
|
54
|
+
*/
|
|
55
|
+
async flush() {
|
|
56
|
+
// Clear timeout if set
|
|
57
|
+
if (this.batchTimeout) {
|
|
58
|
+
clearTimeout(this.batchTimeout);
|
|
59
|
+
this.batchTimeout = null;
|
|
60
|
+
}
|
|
61
|
+
// Nothing to flush
|
|
62
|
+
if (this.queue.length === 0) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// Throttle: wait if we sent a batch too recently
|
|
66
|
+
const now = Date.now();
|
|
67
|
+
const timeSinceLastBatch = now - this.lastBatchTime;
|
|
68
|
+
if (timeSinceLastBatch < this.options.throttleInterval) {
|
|
69
|
+
const waitTime = this.options.throttleInterval - timeSinceLastBatch;
|
|
70
|
+
await new Promise(resolve => setTimeout(resolve, waitTime));
|
|
71
|
+
}
|
|
72
|
+
// Extract requests from queue
|
|
73
|
+
const batch = this.queue.splice(0, this.options.maxBatchSize);
|
|
74
|
+
const requests = batch.map(item => item.request);
|
|
75
|
+
this.lastBatchTime = Date.now();
|
|
76
|
+
try {
|
|
77
|
+
// Send batch request
|
|
78
|
+
const results = await this.sendBatch(requests);
|
|
79
|
+
// Resolve individual promises
|
|
80
|
+
for (let i = 0; i < batch.length; i++) {
|
|
81
|
+
batch[i].resolve(results[i]);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
// Reject all promises in batch
|
|
86
|
+
for (const item of batch) {
|
|
87
|
+
item.reject(error);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get current queue size
|
|
93
|
+
*/
|
|
94
|
+
getQueueSize() {
|
|
95
|
+
return this.queue.length;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Clear the queue without sending
|
|
99
|
+
*/
|
|
100
|
+
clear() {
|
|
101
|
+
if (this.batchTimeout) {
|
|
102
|
+
clearTimeout(this.batchTimeout);
|
|
103
|
+
this.batchTimeout = null;
|
|
104
|
+
}
|
|
105
|
+
// Reject all pending requests
|
|
106
|
+
for (const item of this.queue) {
|
|
107
|
+
item.reject(new Error('Batch queue cleared'));
|
|
108
|
+
}
|
|
109
|
+
this.queue = [];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Crypto adapter for browser environments
|
|
3
|
+
*/
|
|
4
|
+
import { CryptoAdapter, Keypair } from './crypto-adapter.js';
|
|
5
|
+
/**
|
|
6
|
+
* Web Crypto implementation using browser APIs
|
|
7
|
+
* Uses btoa/atob for base64 encoding and crypto.getRandomValues for random bytes
|
|
8
|
+
*/
|
|
9
|
+
export declare class WebCryptoAdapter implements CryptoAdapter {
|
|
10
|
+
generateKeypair(): Promise<Keypair>;
|
|
11
|
+
signMessage(message: string, privateKeyBase64: string): Promise<string>;
|
|
12
|
+
verifySignature(message: string, signatureBase64: string, publicKeyBase64: string): Promise<boolean>;
|
|
13
|
+
bytesToBase64(bytes: Uint8Array): string;
|
|
14
|
+
base64ToBytes(base64: string): Uint8Array;
|
|
15
|
+
randomBytes(length: number): Uint8Array;
|
|
16
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web Crypto adapter for browser environments
|
|
3
|
+
*/
|
|
4
|
+
import * as ed25519 from '@noble/ed25519';
|
|
5
|
+
// Set SHA-512 hash function for ed25519 (required in @noble/ed25519 v3+)
|
|
6
|
+
ed25519.hashes.sha512Async = async (message) => {
|
|
7
|
+
return new Uint8Array(await crypto.subtle.digest('SHA-512', message));
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Web Crypto implementation using browser APIs
|
|
11
|
+
* Uses btoa/atob for base64 encoding and crypto.getRandomValues for random bytes
|
|
12
|
+
*/
|
|
13
|
+
export class WebCryptoAdapter {
|
|
14
|
+
async generateKeypair() {
|
|
15
|
+
const privateKey = ed25519.utils.randomSecretKey();
|
|
16
|
+
const publicKey = await ed25519.getPublicKeyAsync(privateKey);
|
|
17
|
+
return {
|
|
18
|
+
publicKey: this.bytesToBase64(publicKey),
|
|
19
|
+
privateKey: this.bytesToBase64(privateKey),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
async signMessage(message, privateKeyBase64) {
|
|
23
|
+
const privateKey = this.base64ToBytes(privateKeyBase64);
|
|
24
|
+
const encoder = new TextEncoder();
|
|
25
|
+
const messageBytes = encoder.encode(message);
|
|
26
|
+
const signature = await ed25519.signAsync(messageBytes, privateKey);
|
|
27
|
+
return this.bytesToBase64(signature);
|
|
28
|
+
}
|
|
29
|
+
async verifySignature(message, signatureBase64, publicKeyBase64) {
|
|
30
|
+
try {
|
|
31
|
+
const signature = this.base64ToBytes(signatureBase64);
|
|
32
|
+
const publicKey = this.base64ToBytes(publicKeyBase64);
|
|
33
|
+
const encoder = new TextEncoder();
|
|
34
|
+
const messageBytes = encoder.encode(message);
|
|
35
|
+
return await ed25519.verifyAsync(signature, messageBytes, publicKey);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
bytesToBase64(bytes) {
|
|
42
|
+
const binString = Array.from(bytes, byte => String.fromCodePoint(byte)).join('');
|
|
43
|
+
return btoa(binString);
|
|
44
|
+
}
|
|
45
|
+
base64ToBytes(base64) {
|
|
46
|
+
const binString = atob(base64);
|
|
47
|
+
return Uint8Array.from(binString, char => char.codePointAt(0));
|
|
48
|
+
}
|
|
49
|
+
randomBytes(length) {
|
|
50
|
+
return crypto.getRandomValues(new Uint8Array(length));
|
|
51
|
+
}
|
|
52
|
+
}
|
package/package.json
CHANGED