Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 9e1d763

Browse files
Merge branch 'feature/5887/the-options-passed-to-the-ipcprovider-is-not-passed-to-the-constructor-of-the-underlying-socket-object' into feature/5889/keep-emitting-the-error-as-an-object-for-maximum-number-of-reconnect-attempts-reached
2 parents 6e84af7 + 9800deb commit 9e1d763

File tree

9 files changed

+91
-10
lines changed

9 files changed

+91
-10
lines changed

packages/web3-providers-ipc/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7171
### Added
7272

7373
- Added named export for `IpcProvider` (#5771)
74+
- Pass `_socketOptions` from `IpcProvider` constructor to the underlying `Socket` (#5891)
75+
- The getter of `SocketConnection` in `IpcProvider` (inherited from `SocketProvider`) returns `net.Socket` (#5891)

packages/web3-providers-ipc/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
import { Socket } from 'net';
18+
import { Socket, SocketConstructorOpts } from 'net';
1919
import { ConnectionNotOpenError, InvalidClientError } from 'web3-errors';
2020
import { SocketProvider } from 'web3-utils';
2121
import {
@@ -33,7 +33,8 @@ export default class IpcProvider<API extends Web3APISpec = EthExecutionAPI> exte
3333
Error,
3434
API
3535
> {
36-
// Message handlers. Due to bounding of `this` and removing the listeners we have to keep it's reference.
36+
protected readonly _socketOptions?: SocketConstructorOpts;
37+
3738
protected _socketConnection?: Socket;
3839

3940
public getStatus(): Web3ProviderStatus {
@@ -42,12 +43,13 @@ export default class IpcProvider<API extends Web3APISpec = EthExecutionAPI> exte
4243
}
4344
return this._connectionStatus;
4445
}
46+
4547
protected _openSocketConnection() {
4648
if (!existsSync(this._socketPath)) {
4749
throw new InvalidClientError(this._socketPath);
4850
}
4951
if (!this._socketConnection || this.getStatus() === 'disconnected') {
50-
this._socketConnection = new Socket();
52+
this._socketConnection = new Socket(this._socketOptions);
5153
}
5254

5355
this._socketConnection.connect({ path: this._socketPath });

packages/web3-providers-ipc/test/unit/ipc_provider.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18+
import { Socket } from 'net';
1819
import * as fs from 'fs';
1920
import { ConnectionError, InvalidClientError } from 'web3-errors';
2021
import IpcProvider from '../../src/index';
@@ -34,6 +35,7 @@ describe('IpcProvider', () => {
3435
it('should construct the instance of the provider', () => {
3536
const provider = new IpcProvider(socketPath);
3637
expect(provider).toBeInstanceOf(IpcProvider);
38+
expect(provider.SocketConnection).toBeInstanceOf(Socket);
3739
});
3840

3941
it('should try to connect', () => {

packages/web3-providers-ws/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6565
### Added
6666

6767
- Added named export for `WebSocketProvider` (#5771)
68+
- The getter of `SocketConnection` in `WebSocketProvider` (inherited from `SocketProvider`) returns isomorphic `WebSocket` (#5891)

packages/web3-providers-ws/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export { ClientOptions } from 'isomorphic-ws';
3434
export default class WebSocketProvider<
3535
API extends Web3APISpec = EthExecutionAPI,
3636
> extends SocketProvider<WebSocket.MessageEvent, WebSocket.CloseEvent, WebSocket.ErrorEvent, API> {
37-
protected readonly _providerOptions?: ClientOptions | ClientRequestArgs;
37+
protected readonly _socketOptions?: ClientOptions | ClientRequestArgs;
3838

3939
protected _socketConnection?: WebSocket;
4040

@@ -64,9 +64,9 @@ export default class WebSocketProvider<
6464
this._socketConnection = new WebSocket(
6565
this._socketPath,
6666
undefined,
67-
this._providerOptions && Object.keys(this._providerOptions).length === 0
67+
this._socketOptions && Object.keys(this._socketOptions).length === 0
6868
? undefined
69-
: this._providerOptions,
69+
: this._socketOptions,
7070
);
7171
}
7272

packages/web3-providers-ws/test/unit/web_socket_provider.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('WebSocketProvider', () => {
5757
expect(wsProvider.connect).toBeDefined();
5858
expect(wsProvider.disconnect).toBeDefined();
5959
expect(wsProvider.reset).toBeDefined();
60+
expect(wsProvider.SocketConnection).toBeInstanceOf(WebSocket);
6061
});
6162

6263
it('should allow for providerOptions to be passed upon instantiation', () => {

packages/web3-utils/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8787

8888
- `compareBlockNumbers` function now only supports comparison of both blocktags params ( except `earliest` vs number) or both block number params (#5842)
8989
- `SocketProvider` abstract class now resolves JSON RPC response errors instead of rejecting them (#5844)
90+
- Exposes the getter of `SocketConnection` in `SocketProvider` (#5891)

packages/web3-utils/src/socket_provider.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ export abstract class SocketProvider<
7272
/* eslint-disable @typescript-eslint/no-explicit-any */
7373
protected readonly _sentRequestsQueue: Map<JsonRpcId, SocketRequestItem<any, any, any>>;
7474
protected _reconnectAttempts!: number;
75-
protected readonly _providerOptions?: object;
75+
protected readonly _socketOptions?: object;
7676
protected readonly _reconnectOptions: ReconnectOptions;
7777
protected _socketConnection?: unknown;
78+
public get SocketConnection() {
79+
return this._socketConnection;
80+
}
7881
protected _connectionStatus: Web3ProviderStatus;
7982
protected readonly _onMessageHandler: (event: MessageEvent) => void;
8083
protected readonly _onOpenHandler: () => void;
@@ -84,20 +87,23 @@ export abstract class SocketProvider<
8487
/**
8588
* This is an abstract class for implementing a socket provider (e.g. WebSocket, IPC). It extends the EIP-1193 provider {@link EIP1193Provider}.
8689
* @param socketPath - The path to the socket (e.g. /ipc/path or ws://localhost:8546)
87-
* @param options - The options for the socket connection
90+
* @param socketOptions - The options for the socket connection
8891
* @param reconnectOptions - The options for the socket reconnection {@link ReconnectOptions}
8992
*/
90-
public constructor(socketPath: string, options?: object, reconnectOptions?: object) {
93+
public constructor(socketPath: string, socketOptions?: object, reconnectOptions?: object) {
9194
super();
9295
this._connectionStatus = 'connecting';
96+
97+
// Message handlers. Due to bounding of `this` and removing the listeners we have to keep it's reference.
9398
this._onMessageHandler = this._onMessage.bind(this);
9499
this._onOpenHandler = this._onConnect.bind(this);
95100
this._onCloseHandler = this._onCloseEvent.bind(this);
96101
this._onErrorHandler = this._onError.bind(this);
102+
97103
if (!this._validateProviderPath(socketPath)) throw new InvalidClientError(socketPath);
98104

99105
this._socketPath = socketPath;
100-
this._providerOptions = options;
106+
this._socketOptions = socketOptions;
101107

102108
const DEFAULT_PROVIDER_RECONNECTION_OPTIONS = {
103109
autoReconnect: true,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
This file is part of web3.js.
3+
4+
web3.js is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Lesser General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
web3.js is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public License
15+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
import { Web3APIPayload, EthExecutionAPI, JsonRpcResponse, Web3ProviderStatus } from 'web3-types';
19+
import { SocketProvider } from '../../src/socket_provider';
20+
21+
const dummySocketConnection = { dummy: 'dummy' };
22+
23+
class TestProvider extends SocketProvider<any, any, any> {
24+
protected _socketConnection?: typeof dummySocketConnection;
25+
26+
protected _openSocketConnection() {
27+
this._socketConnection = dummySocketConnection;
28+
}
29+
30+
// Dummy implementation of the abstract base methods
31+
// eslint-disable-next-line
32+
protected _addSocketListeners(): void {}
33+
// eslint-disable-next-line
34+
protected _removeSocketListeners(): void {}
35+
// eslint-disable-next-line
36+
protected _onCloseEvent(_event: unknown): void {}
37+
// eslint-disable-next-line
38+
protected _sendToSocket(_payload: Web3APIPayload<EthExecutionAPI, any>): void {}
39+
// eslint-disable-next-line
40+
protected _parseResponses(_event: any): JsonRpcResponse[] {
41+
return [] as JsonRpcResponse[];
42+
}
43+
// eslint-disable-next-line
44+
protected _closeSocketConnection(
45+
_code?: number | undefined,
46+
_data?: string | undefined,
47+
// eslint-disable-next-line
48+
): void {}
49+
// eslint-disable-next-line
50+
getStatus(): Web3ProviderStatus {
51+
return 'connected';
52+
}
53+
}
54+
55+
describe('SocketProvider', () => {
56+
const socketPath = `some_path`;
57+
const socketOption = { dummyOption: true } as const;
58+
59+
describe('constructor', () => {
60+
it('should construct the instance of the provider', () => {
61+
const provider = new TestProvider(socketPath, socketOption);
62+
expect(provider).toBeInstanceOf(SocketProvider);
63+
expect(provider.SocketConnection).toEqual(dummySocketConnection);
64+
});
65+
});
66+
});

0 commit comments

Comments
 (0)