Skip to content

Commit 1f2340a

Browse files
committed
feat: global fetch and websocket in config, globalThis for auto-detect fetch
1 parent ae83b1b commit 1f2340a

File tree

6 files changed

+14
-5
lines changed

6 files changed

+14
-5
lines changed

src/channel/rpc_0_7.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { Block, getDefaultNodeUrl, isV3Tx, wait } from '../utils/provider';
3636
import { decompressProgram, signatureToHexArray } from '../utils/stark';
3737
import { getVersionsByType } from '../utils/transaction';
3838
import { logger } from '../global/logger';
39+
import { config } from '../global/config';
3940

4041
const defaultOptions = {
4142
headers: { 'Content-Type': 'application/json' },
@@ -88,7 +89,7 @@ export class RpcChannel {
8889
} else {
8990
this.nodeUrl = getDefaultNodeUrl(undefined, optionsOrProvider?.default, '0.7');
9091
}
91-
this.baseFetch = baseFetch ?? fetch;
92+
this.baseFetch = baseFetch || config.get('fetch') || fetch;
9293
this.blockIdentifier = blockIdentifier ?? defaultOptions.blockIdentifier;
9394
this.chainId = chainId;
9495
this.headers = { ...defaultOptions.headers, ...headers };

src/channel/rpc_0_8.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { decompressProgram, signatureToHexArray } from '../utils/stark';
4242
import { getVersionsByType } from '../utils/transaction';
4343
import { logger } from '../global/logger';
4444
import { isRPC08_ResourceBounds } from '../provider/types/spec.type';
45+
import { config } from '../global/config';
4546
// TODO: check if we can filet type before entering to this method, as so to specify here only RPC 0.8 types
4647

4748
const defaultOptions = {
@@ -95,7 +96,7 @@ export class RpcChannel {
9596
} else {
9697
this.nodeUrl = getDefaultNodeUrl(undefined, optionsOrProvider?.default, '0.8');
9798
}
98-
this.baseFetch = baseFetch ?? fetch;
99+
this.baseFetch = baseFetch || config.get('fetch') || fetch;
99100
this.blockIdentifier = blockIdentifier ?? defaultOptions.blockIdentifier;
100101
this.chainId = chainId;
101102
this.headers = { ...defaultOptions.headers, ...headers };

src/channel/ws_0_8.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import WebSocket from '../utils/connect/ws';
1616
import { stringify } from '../utils/json';
1717
import { bigNumberishArrayToHexadecimalStringArray, toHex } from '../utils/num';
1818
import { Block } from '../utils/provider';
19+
import { config } from '../global/config';
1920

2021
export const WSSubscriptions = {
2122
NEW_HEADS: 'newHeads',
@@ -184,7 +185,7 @@ export class WebSocketChannel {
184185
// provided existing websocket
185186
const nodeUrl = options.nodeUrl || 'http://localhost:3000 '; // TODO: implement getDefaultNodeUrl default node when defined by providers?
186187
this.nodeUrl = options.websocket ? options.websocket.url : nodeUrl;
187-
this.websocket = options.websocket ? options.websocket : new WebSocket(nodeUrl);
188+
this.websocket = options.websocket || config.get('websocket') || new WebSocket(nodeUrl);
188189

189190
this.websocket.addEventListener('open', this.onOpen.bind(this));
190191
this.websocket.addEventListener('close', this.onCloseProxy.bind(this));

src/global/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ export const DEFAULT_GLOBAL_CONFIG: {
107107
rpcVersion: _SupportedRpcVersion;
108108
transactionVersion: SupportedTransactionVersion;
109109
feeMarginPercentage: FeeMarginPercentage;
110+
fetch: any;
111+
websocket: any;
110112
} = {
111113
legacyMode: false,
112114
rpcVersion: '0.8',
@@ -129,6 +131,8 @@ export const DEFAULT_GLOBAL_CONFIG: {
129131
},
130132
maxFee: 50,
131133
},
134+
fetch: undefined,
135+
websocket: undefined,
132136
};
133137

134138
export const RPC_DEFAULT_NODES = {

src/utils/connect/fetch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { LibraryError } from '../errors';
22

33
export default (typeof fetch !== 'undefined' && fetch) ||
4-
(typeof window !== 'undefined' && window.fetch) ||
4+
(typeof globalThis !== 'undefined' && globalThis.fetch) ||
5+
(typeof window !== 'undefined' && window.fetch.bind(window)) ||
56
(typeof global !== 'undefined' && global.fetch) ||
67
((() => {
78
throw new LibraryError(

src/utils/connect/ws.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { LibraryError } from '../errors';
22

33
export default (typeof WebSocket !== 'undefined' && WebSocket) ||
4-
(typeof window !== 'undefined' && window.WebSocket) ||
4+
(typeof globalThis !== 'undefined' && globalThis.WebSocket) ||
5+
(typeof window !== 'undefined' && window.WebSocket.bind(window)) ||
56
(typeof global !== 'undefined' && global.WebSocket) ||
67
(class {
78
constructor() {

0 commit comments

Comments
 (0)