Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move isomorphic-fetch to dev dependency #979

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions __tests__/rpcChannel.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { RPC07 } from '../src';
import { RPC06, RPC07 } from '../src';
import { createBlockForDevnet, getTestProvider } from './config/fixtures';
import { initializeMatcher } from './config/schema';

describe('RPC 0.7.0', () => {
const rpcProvider = getTestProvider(false);
const channel = rpcProvider.channel as RPC07.RpcChannel;
describe('RpcChannel', () => {
const { nodeUrl } = getTestProvider(false).channel;
const channel07 = new RPC07.RpcChannel({ nodeUrl });
initializeMatcher(expect);

beforeAll(async () => {
await createBlockForDevnet();
});

test('getBlockWithReceipts', async () => {
const response = await channel.getBlockWithReceipts('latest');
expect(response).toMatchSchemaRef('BlockWithTxReceipts');
test('baseFetch override', async () => {
const baseFetch = jest.fn();
const fetchChannel06 = new RPC06.RpcChannel({ nodeUrl, baseFetch });
const fetchChannel07 = new RPC07.RpcChannel({ nodeUrl, baseFetch });
(fetchChannel06.fetch as any)();
expect(baseFetch).toHaveBeenCalledTimes(1);
baseFetch.mockClear();
(fetchChannel07.fetch as any)();
expect(baseFetch).toHaveBeenCalledTimes(1);
});

describe('RPC 0.7.0', () => {
test('getBlockWithReceipts', async () => {
const response = await channel07.getBlockWithReceipts('latest');
expect(response).toMatchSchemaRef('BlockWithTxReceipts');
});
});
});
8 changes: 8 additions & 0 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ describeIfRpc('RPCProvider', () => {
await createBlockForDevnet();
});

test('baseFetch override', async () => {
const { nodeUrl } = rpcProvider.channel;
const baseFetch = jest.fn();
const fetchProvider = new RpcProvider({ nodeUrl, baseFetch });
(fetchProvider.fetch as any)();
expect(baseFetch.mock.calls.length).toBe(1);
});

test('getChainId', async () => {
const fetchSpy = jest.spyOn(rpcProvider.channel as any, 'fetchEndpoint');
(rpcProvider as any).chainId = undefined as unknown as StarknetChainId;
Expand Down
14 changes: 10 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"fetch-intercept": "^2.4.0",
"husky": "^9.0.11",
"import-sort-style-module": "^6.0.0",
"isomorphic-fetch": "^3.0.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"jest-json-schema": "^6.1.0",
Expand All @@ -95,7 +96,6 @@
"@scure/starknet": "~1.0.0",
"abi-wan-kanabi": "^2.2.2",
"fetch-cookie": "^3.0.0",
"isomorphic-fetch": "^3.0.0",
"lossless-json": "^4.0.1",
"pako": "^2.0.4",
"starknet-types": "^0.0.5",
Expand Down
34 changes: 23 additions & 11 deletions src/channel/rpc_0_6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { JRPC, RPCSPEC06 as RPC } from '../types/api';
import { CallData } from '../utils/calldata';
import { isSierra } from '../utils/contract';
import { validateAndParseEthAddress } from '../utils/eth';
import fetch from '../utils/fetchPonyfill';
import fetch from '../utils/fetch';
import { getSelector, getSelectorFromName } from '../utils/hash';
import { stringify } from '../utils/json';
import { getHexStringArray, toHex, toStorageKey } from '../utils/num';
Expand All @@ -40,34 +40,46 @@ export class RpcChannel {

public headers: object;

readonly retries: number;

public requestId: number;

readonly blockIdentifier: BlockIdentifier;

readonly retries: number;

readonly waitMode: boolean; // behave like web2 rpc and return when tx is processed

private chainId?: StarknetChainId;

private specVersion?: string;

readonly waitMode: Boolean; // behave like web2 rpc and return when tx is processed
private baseFetch: NonNullable<RpcProviderOptions['baseFetch']>;

constructor(optionsOrProvider?: RpcProviderOptions) {
const { nodeUrl, retries, headers, blockIdentifier, chainId, specVersion, waitMode } =
optionsOrProvider || {};
const {
baseFetch,
blockIdentifier,
chainId,
headers,
nodeUrl,
retries,
specVersion,
waitMode,
} = optionsOrProvider || {};
if (Object.values(NetworkName).includes(nodeUrl as NetworkName)) {
this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName, optionsOrProvider?.default);
} else if (nodeUrl) {
this.nodeUrl = nodeUrl;
} else {
this.nodeUrl = getDefaultNodeUrl(undefined, optionsOrProvider?.default);
}
this.retries = retries || defaultOptions.retries;
this.headers = { ...defaultOptions.headers, ...headers };
this.blockIdentifier = blockIdentifier || defaultOptions.blockIdentifier;
this.baseFetch = baseFetch ?? fetch;
this.blockIdentifier = blockIdentifier ?? defaultOptions.blockIdentifier;
this.chainId = chainId;
this.headers = { ...defaultOptions.headers, ...headers };
this.retries = retries ?? defaultOptions.retries;
this.specVersion = specVersion;
this.waitMode = waitMode || false;
this.waitMode = waitMode ?? false;

this.requestId = 0;
}

Expand All @@ -82,7 +94,7 @@ export class RpcChannel {
method,
...(params && { params }),
};
return fetch(this.nodeUrl, {
return this.baseFetch(this.nodeUrl, {
method: 'POST',
body: stringify(rpcRequestBody),
headers: this.headers as Record<string, string>,
Expand Down
34 changes: 23 additions & 11 deletions src/channel/rpc_0_7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { JRPC, RPCSPEC07 as RPC } from '../types/api';
import { CallData } from '../utils/calldata';
import { isSierra } from '../utils/contract';
import { validateAndParseEthAddress } from '../utils/eth';
import fetch from '../utils/fetchPonyfill';
import fetch from '../utils/fetch';
import { getSelector, getSelectorFromName } from '../utils/hash';
import { stringify } from '../utils/json';
import { getHexStringArray, toHex, toStorageKey } from '../utils/num';
Expand All @@ -40,34 +40,46 @@ export class RpcChannel {

public headers: object;

readonly retries: number;

public requestId: number;

readonly blockIdentifier: BlockIdentifier;

readonly retries: number;

readonly waitMode: boolean; // behave like web2 rpc and return when tx is processed

private chainId?: StarknetChainId;

private specVersion?: string;

readonly waitMode: Boolean; // behave like web2 rpc and return when tx is processed
private baseFetch: NonNullable<RpcProviderOptions['baseFetch']>;

constructor(optionsOrProvider?: RpcProviderOptions) {
const { nodeUrl, retries, headers, blockIdentifier, chainId, specVersion, waitMode } =
optionsOrProvider || {};
const {
baseFetch,
blockIdentifier,
chainId,
headers,
nodeUrl,
retries,
specVersion,
waitMode,
} = optionsOrProvider || {};
if (Object.values(NetworkName).includes(nodeUrl as NetworkName)) {
this.nodeUrl = getDefaultNodeUrl(nodeUrl as NetworkName, optionsOrProvider?.default);
} else if (nodeUrl) {
this.nodeUrl = nodeUrl;
} else {
this.nodeUrl = getDefaultNodeUrl(undefined, optionsOrProvider?.default);
}
this.retries = retries || defaultOptions.retries;
this.headers = { ...defaultOptions.headers, ...headers };
this.blockIdentifier = blockIdentifier || defaultOptions.blockIdentifier;
this.baseFetch = baseFetch ?? fetch;
this.blockIdentifier = blockIdentifier ?? defaultOptions.blockIdentifier;
this.chainId = chainId;
this.headers = { ...defaultOptions.headers, ...headers };
this.retries = retries ?? defaultOptions.retries;
this.specVersion = specVersion;
this.waitMode = waitMode || false;
this.waitMode = waitMode ?? false;

this.requestId = 0;
}

Expand All @@ -82,7 +94,7 @@ export class RpcChannel {
method,
...(params && { params }),
};
return fetch(this.nodeUrl, {
return this.baseFetch(this.nodeUrl, {
method: 'POST',
body: stringify(rpcRequestBody),
headers: this.headers as Record<string, string>,
Expand Down
1 change: 1 addition & 0 deletions src/types/provider/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type RpcProviderOptions = {
specVersion?: string;
default?: boolean;
waitMode?: boolean;
baseFetch?: WindowOrWorkerGlobalScope['fetch'];
feeMarginPercentage?: {
l1BoundMaxAmount: number;
l1BoundMaxPricePerUnit: number;
Expand Down
15 changes: 15 additions & 0 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// the ts-ignore suppresses an esm to cjs import error that is resolved with entry point resolution
// @ts-ignore
import makeFetchCookie from 'fetch-cookie';
import { LibraryError } from '../provider/errors';

// use built-in fetch in browser if available
export default (typeof window !== 'undefined' && window.fetch) ||
// use built-in fetch in node, react-native and service worker if available
(typeof global !== 'undefined' && makeFetchCookie(global.fetch)) ||
// throw with instructions when no fetch is detected
((() => {
throw new LibraryError(
"'fetch()' not detected, use the 'baseFetch' constructor parameter to set it"
);
}) as WindowOrWorkerGlobalScope['fetch']);
8 changes: 0 additions & 8 deletions src/utils/fetchPonyfill.ts

This file was deleted.