-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathprovider-socket.ts
More file actions
540 lines (491 loc) · 17.2 KB
/
Copy pathprovider-socket.ts
File metadata and controls
540 lines (491 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import { UnmanagedSubscriber } from './abstract-provider.js';
import { assert, assertArgument, makeError } from '../utils/index.js';
import { JsonRpcApiProvider } from './provider-jsonrpc.js';
import type { Subscriber, Subscription } from './abstract-provider.js';
import type { AccessesFilter, EventFilter } from './provider.js';
import type { JsonRpcApiProviderOptions, JsonRpcError, JsonRpcPayload, JsonRpcResult } from './provider-jsonrpc.js';
import type { Networkish } from './network.js';
import type { WebSocketLike } from './provider-websocket.js';
import { Shard, Zone, toShard } from '../constants/index.js';
/**
* @property {string} method - The method name.
* @property {Object} params - The parameters.
* @property {any} params.result - The result.
* @property {string} params.subscription - The subscription ID.
*/
type JsonRpcSubscription = {
method: string;
params: {
result: any;
subscription: string;
};
};
/**
* A **SocketSubscriber** uses a socket transport to handle events and should use
* {@link SocketSubscriber._emit | **_emit**} to manage the events.
*
* - A sub-class MUST call the `_start()` method once connected
* - A sub-class MUST override the `_write(string)` method
* - A sub-class MUST call `_processMessage(string)` for each message
*
* @category Providers
*/
export class SocketSubscriber implements Subscriber {
#provider: SocketProvider;
#filter: string;
/**
* The filter.
*
* @type {any[]}
*/
get filter(): Array<any> {
return JSON.parse(this.#filter);
}
#filterId: null | Promise<string | number>;
#paused: null | boolean;
#emitPromise: null | Promise<void>;
protected zone: Zone;
protected shard: Shard;
/**
* Creates a new **SocketSubscriber** attached to `provider` listening to `filter`.
*
* @param {SocketProvider} provider - The socket provider.
* @param {any[]} filter - The filter.
*/
constructor(provider: SocketProvider, filter: Array<any>, zone: Zone) {
this.#provider = provider;
this.#filter = JSON.stringify(filter);
this.#filterId = null;
this.#paused = null;
this.#emitPromise = null;
this.zone = zone;
this.shard = toShard(zone);
}
/**
* Start the subscriber.
*/
start(): void {
this.#filterId = this.#provider.send('quai_subscribe', this.filter, this.shard).then((filterId) => {
this.#provider._register(filterId, this);
return filterId;
});
}
/**
* Stop the subscriber.
*/
stop(): void {
(<Promise<number>>this.#filterId).then((filterId) => {
this.#provider.send('quai_unsubscribe', [filterId], this.shard);
});
this.#filterId = null;
}
/**
* Pause the subscriber.
*
* @param {boolean} [dropWhilePaused] - Whether to drop logs while paused.
*/
pause(dropWhilePaused?: boolean): void {
assert(
dropWhilePaused,
'preserve logs while paused not supported by SocketSubscriber yet',
'UNSUPPORTED_OPERATION',
{ operation: 'pause(false)' },
);
this.#paused = !!dropWhilePaused;
}
/**
* Resume the subscriber.
*/
resume(): void {
this.#paused = null;
}
/**
* Handle incoming messages.
*
* @ignore
* @param {any} message - The message to handle.
*/
_handleMessage(message: any): void {
if (this.#filterId == null) {
return;
}
if (this.#paused === null) {
let emitPromise: null | Promise<void> = this.#emitPromise;
if (emitPromise == null) {
emitPromise = this._emit(this.#provider, message);
} else {
emitPromise = emitPromise.then(async () => {
await this._emit(this.#provider, message);
});
}
this.#emitPromise = emitPromise.then(() => {
if (this.#emitPromise === emitPromise) {
this.#emitPromise = null;
}
});
}
}
/**
* Sub-classes **must** override this to emit the events on the provider.
*
* @abstract
* @param {SocketProvider} provider - The socket provider.
* @param {any} message - The message to emit.
* @returns {Promise<void>}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async _emit(provider: SocketProvider, message: any): Promise<void> {
throw new Error('sub-classes must implement this; _emit');
}
}
/**
* A **SocketBlockSubscriber** listens for `newHeads` events and emits `"block"` events.
*
* @category Providers
*/
export class SocketBlockSubscriber extends SocketSubscriber {
/**
* Creates a new **SocketBlockSubscriber**.
*
* @ignore
* @param {SocketProvider} provider - The socket provider.
*/
constructor(provider: SocketProvider, zone: Zone) {
super(provider, ['newHeads'], zone);
}
/**
* Emit the block event.
*
* @ignore
* @param {SocketProvider} provider - The socket provider.
* @param {any} message - The message to emit.
* @returns {Promise<void>}
*/
async _emit(provider: SocketProvider, message: any): Promise<void> {
provider.emit('block', this.zone, parseInt(message.woHeader.number));
}
}
/**
* A **SocketAccessesSubscriber** listens for `acceses` events and emits `accesses` events.
*
* @category Providers
*/
export class SocketAccessesSubscriber extends SocketSubscriber {
#accessesFilter: string;
get accessesFilter(): AccessesFilter {
return JSON.parse(this.#accessesFilter);
}
/**
* Creates a new **SocketBlockSubscriber**.
*
* @ignore
* @param {SocketProvider} provider - The socket provider.
* @param filter
* @param zone
*/
constructor(provider: SocketProvider, filter: AccessesFilter, zone: Zone) {
super(provider, ['accesses', filter.address], zone);
this.#accessesFilter = JSON.stringify(filter);
}
/**
* Emit the block event.
*
* @ignore
* @param {SocketProvider} provider - The socket provider.
* @param {any} message - The message to emit.
* @returns {Promise<void>}
*/
async _emit(provider: SocketProvider, message: any): Promise<void> {
if (this.accessesFilter.type === 'balance') {
message = await provider.getBalance(this.accessesFilter.address);
}
provider.emit(this.accessesFilter, this.zone, message);
}
}
/**
* A **SocketPendingSubscriber** listens for pending transactions and emits `"pending"` events.
*
* @category Providers
*/
export class SocketPendingSubscriber extends SocketSubscriber {
/**
* Creates a new **SocketPendingSubscriber**.
*
* @ignore
* @param {SocketProvider} provider - The socket provider.
*/
constructor(provider: SocketProvider, zone: Zone) {
super(provider, ['newPendingTransactions'], zone);
}
/**
* Emit the pending event.
*
* @ignore
* @param {SocketProvider} provider - The socket provider.
* @param {any} message - The message to emit.
* @returns {Promise<void>}
*/
async _emit(provider: SocketProvider, message: any): Promise<void> {
provider.emit('pending', message);
}
}
/**
* A **SocketEventSubscriber** listens for event logs.
*
* @category Providers
*/
export class SocketEventSubscriber extends SocketSubscriber {
#logFilter: string;
/**
* The filter.
*
* @type {EventFilter}
*/
get logFilter(): EventFilter {
return JSON.parse(this.#logFilter);
}
/**
* Creates a new **SocketEventSubscriber**.
*
* @ignore
* @param {SocketProvider} provider - The socket provider.
* @param {EventFilter} filter - The event filter.
*/
constructor(provider: SocketProvider, filter: EventFilter, zone: Zone) {
super(provider, ['logs', filter], zone);
this.#logFilter = JSON.stringify(filter);
}
/**
* Emit the event log.
*
* @ignore
* @param {SocketProvider} provider - The socket provider.
* @param {any} message - The message to emit.
* @returns {Promise<void>}
*/
async _emit(provider: SocketProvider, message: any): Promise<void> {
provider.emit(this.logFilter, this.zone, provider._wrapLog(message, provider._network));
}
}
/**
* A **SocketProvider** is backed by a long-lived connection over a socket, which can subscribe and receive real-time
* messages over its communication channel.
*
* @category Providers
*/
export class SocketProvider extends JsonRpcApiProvider<WebSocketLike> {
#callbacks: Map<number, { payload: JsonRpcPayload; resolve: (r: any) => void; reject: (e: Error) => void }>;
// Maps each filterId to its subscriber
#subs: Map<number | string, SocketSubscriber>;
// If any events come in before a subscriber has finished
// registering, queue them
#pending: Map<number | string, Array<any>>;
/**
* Creates a new **SocketProvider** connected to `network`.
*
* If unspecified, the network will be discovered.
*
* @param {Networkish} [network] - The network to connect to.
* @param {JsonRpcApiProviderOptions} [_options] - The options for the provider.
*/
constructor(network?: Networkish, _options?: JsonRpcApiProviderOptions) {
// Copy the options
const options = Object.assign({}, _options != null ? _options : {});
// Support for batches is generally not supported for
// connection-base providers; if this changes in the future
// the _send should be updated to reflect this
assertArgument(
options.batchMaxCount == null || options.batchMaxCount === 1,
'sockets-based providers do not support batches',
'options.batchMaxCount',
_options,
);
options.batchMaxCount = 1;
// Socket-based Providers (generally) cannot change their network,
// since they have a long-lived connection; but let people override
// this if they have just cause.
if (options.staticNetwork == null) {
options.staticNetwork = true;
}
super(network, options);
this.#callbacks = new Map();
this.#subs = new Map();
this.#pending = new Map();
}
/**
* Get the subscriber for a given subscription.
*
* @ignore
* @param {Subscription} sub - The subscription.
* @returns {Subscriber} The subscriber.
*/
_getSubscriber(sub: Subscription): Subscriber {
switch (sub.type) {
case 'close':
return new UnmanagedSubscriber('close');
case 'block':
return new SocketBlockSubscriber(this, sub.zone);
case 'accesses':
return new SocketAccessesSubscriber(this, sub.filter, sub.zone);
case 'pending':
return new SocketPendingSubscriber(this, sub.zone);
case 'event':
return new SocketEventSubscriber(this, sub.filter, sub.zone);
case 'orphan':
// Handled auto-matically within AbstractProvider
// when the log.removed = true
if (sub.filter.orphan === 'drop-log') {
return new UnmanagedSubscriber('drop-log');
}
}
return super._getSubscriber(sub);
}
/**
* Register a new subscriber. This is used internally by Subscribers and generally is unnecessary unless extending
* capabilities.
*
* @ignore
* @param {number | string} filterId - The filter ID.
* @param {SocketSubscriber} subscriber - The subscriber.
*/
_register(filterId: number | string, subscriber: SocketSubscriber): void {
this.#subs.set(filterId, subscriber);
const pending = this.#pending.get(filterId);
if (pending) {
for (const message of pending) {
subscriber._handleMessage(message);
}
this.#pending.delete(filterId);
}
}
/**
* Send a JSON-RPC payload.
*
* @ignore
* @param {JsonRpcPayload | JsonRpcPayload[]} payload - The payload to send.
* @param {Shard} [shard] - The shard.
* @param {boolean} [now] - Whether to send immediately.
* @returns {Promise<(JsonRpcResult | JsonRpcError)[]>} The result or error.
*/
async _send(
payload: JsonRpcPayload | Array<JsonRpcPayload>,
shard?: Shard,
now?: boolean,
): Promise<Array<JsonRpcResult | JsonRpcError>> {
if (this._initFailed) {
console.log('Provider failed to initialize on creation. Run initialize or create a new provider.');
return [
{
id: Array.isArray(payload) ? payload[0].id : payload.id,
error: {
code: -32000,
message: 'Provider failed to initialize on creation. Run initialize or create a new provider.',
},
},
];
}
// WebSocket provider doesn't accept batches
assertArgument(!Array.isArray(payload), 'WebSocket does not support batch send', 'payload', payload);
// @TODO: stringify payloads here and store to prevent mutations
// Prepare a promise to respond to
const promise = new Promise((resolve, reject) => {
this.#callbacks.set(payload.id, { payload, resolve, reject });
});
// Wait until the socket is connected before writing to it
try {
if (!now) {
await this._waitUntilReady();
}
} catch (error) {
this.#callbacks.delete(payload.id);
return [
{
id: Array.isArray(payload) ? payload[0].id : payload.id,
error: {
code: -32000,
message: 'Provider failed to initialize on creation. Run initialize or create a new provider.',
},
},
];
}
// Write the request to the socket
await this._write(JSON.stringify(payload), shard);
return <Array<JsonRpcResult | JsonRpcError>>[await promise];
}
/**
* Sub-classes **must** call this with messages received over their transport to be processed and dispatched.
*
* @ignore
* @param {string} message - The message to process.
*/
async _processMessage(message: string): Promise<void> {
const result = <JsonRpcResult | JsonRpcError | JsonRpcSubscription>JSON.parse(message);
if (result && typeof result === 'object' && 'id' in result) {
const callback = this.#callbacks.get(result.id);
if (callback == null) {
this.emit(
'error',
undefined,
makeError('received result for unknown id', 'UNKNOWN_ERROR', {
reasonCode: 'UNKNOWN_ID',
result,
}),
);
return;
}
this.#callbacks.delete(result.id);
callback.resolve(result);
} else if (result && result.method === 'quai_subscription') {
const filterId = result.params.subscription;
const subscriber = this.#subs.get(filterId);
if (subscriber) {
subscriber._handleMessage(result.params.result);
} else {
let pending = this.#pending.get(filterId);
if (pending == null) {
pending = [];
this.#pending.set(filterId, pending);
}
pending.push(result.params.result);
}
} else {
this.emit(
'error',
undefined,
makeError('received unexpected message', 'UNKNOWN_ERROR', {
reasonCode: 'UNEXPECTED_MESSAGE',
result,
}),
);
return;
}
}
/**
* Sub-classes **must** override this to send `message` over their transport.
*
* @ignore
* @param {string} message - The message to send.
* @param {Shard} [shard] - The shard.
* @returns {Promise<void>}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async _write(message: string, shard?: Shard): Promise<void> {
throw new Error('sub-classes must override this');
}
validateUrl(url: string): void {
const urlPattern = /^(wss?):\/\/[a-zA-Z0-9.-]+(:\d+)?$/;
if (!urlPattern.test(url)) {
let errorMessage = 'Invalid URL: ';
if (!/^wss?:\/\//.test(url)) {
errorMessage += 'URL must start with ws:// or wss://. ';
}
if (url.endsWith('/')) {
errorMessage += 'URL should not end with a /. ';
}
if (/\/[^/]+/.test(url)) {
errorMessage += 'URL should not contain a path, query string, or fragment. ';
}
throw new Error(errorMessage.trim());
}
}
}