@metamask-previews/network-controller 22.2.1-preview-74b4a89 → 22.2.1-preview-ea165590
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/CHANGELOG.md +31 -1
- package/dist/NetworkController.cjs +41 -21
- package/dist/NetworkController.cjs.map +1 -1
- package/dist/NetworkController.d.cts +10 -1
- package/dist/NetworkController.d.cts.map +1 -1
- package/dist/NetworkController.d.mts +10 -1
- package/dist/NetworkController.d.mts.map +1 -1
- package/dist/NetworkController.mjs +41 -21
- package/dist/NetworkController.mjs.map +1 -1
- package/dist/create-auto-managed-network-client.cjs +27 -6
- package/dist/create-auto-managed-network-client.cjs.map +1 -1
- package/dist/create-auto-managed-network-client.d.cts +11 -2
- package/dist/create-auto-managed-network-client.d.cts.map +1 -1
- package/dist/create-auto-managed-network-client.d.mts +11 -2
- package/dist/create-auto-managed-network-client.d.mts.map +1 -1
- package/dist/create-auto-managed-network-client.mjs +27 -6
- package/dist/create-auto-managed-network-client.mjs.map +1 -1
- package/dist/create-network-client.cjs +30 -17
- package/dist/create-network-client.cjs.map +1 -1
- package/dist/create-network-client.d.cts +11 -2
- package/dist/create-network-client.d.cts.map +1 -1
- package/dist/create-network-client.d.mts +11 -2
- package/dist/create-network-client.d.mts.map +1 -1
- package/dist/create-network-client.mjs +30 -17
- package/dist/create-network-client.mjs.map +1 -1
- package/package.json +3 -3
|
@@ -26,11 +26,16 @@ const UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };
|
|
|
26
26
|
* part of the network client is serving as the receiver. The network client is
|
|
27
27
|
* then cached for subsequent usages.
|
|
28
28
|
*
|
|
29
|
-
* @param
|
|
29
|
+
* @param args - The arguments.
|
|
30
|
+
* @param args.networkClientConfiguration - The configuration object that will be
|
|
30
31
|
* used to instantiate the network client when it is needed.
|
|
32
|
+
* @param args.fetch - A function that can be used to make an HTTP request,
|
|
33
|
+
* compatible with the Fetch API.
|
|
34
|
+
* @param args.btoa - A function that can be used to convert a binary string
|
|
35
|
+
* into base-64.
|
|
31
36
|
* @returns The auto-managed network client.
|
|
32
37
|
*/
|
|
33
|
-
function createAutoManagedNetworkClient(networkClientConfiguration) {
|
|
38
|
+
function createAutoManagedNetworkClient({ networkClientConfiguration, fetch: givenFetch, btoa: givenBtoa, }) {
|
|
34
39
|
let networkClient;
|
|
35
40
|
const providerProxy = new Proxy(UNINITIALIZED_TARGET, {
|
|
36
41
|
// TODO: Replace `any` with type
|
|
@@ -39,7 +44,11 @@ function createAutoManagedNetworkClient(networkClientConfiguration) {
|
|
|
39
44
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
40
45
|
return networkClient?.provider;
|
|
41
46
|
}
|
|
42
|
-
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)(
|
|
47
|
+
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)({
|
|
48
|
+
configuration: networkClientConfiguration,
|
|
49
|
+
fetch: givenFetch,
|
|
50
|
+
btoa: givenBtoa,
|
|
51
|
+
}));
|
|
43
52
|
if (networkClient === undefined) {
|
|
44
53
|
throw new Error("It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?");
|
|
45
54
|
}
|
|
@@ -71,7 +80,11 @@ function createAutoManagedNetworkClient(networkClientConfiguration) {
|
|
|
71
80
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
72
81
|
return true;
|
|
73
82
|
}
|
|
74
|
-
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)(
|
|
83
|
+
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)({
|
|
84
|
+
configuration: networkClientConfiguration,
|
|
85
|
+
fetch: givenFetch,
|
|
86
|
+
btoa: givenBtoa,
|
|
87
|
+
}));
|
|
75
88
|
const { provider } = networkClient;
|
|
76
89
|
return propertyName in provider;
|
|
77
90
|
},
|
|
@@ -83,7 +96,11 @@ function createAutoManagedNetworkClient(networkClientConfiguration) {
|
|
|
83
96
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
84
97
|
return networkClient?.blockTracker;
|
|
85
98
|
}
|
|
86
|
-
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)(
|
|
99
|
+
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)({
|
|
100
|
+
configuration: networkClientConfiguration,
|
|
101
|
+
fetch: givenFetch,
|
|
102
|
+
btoa: givenBtoa,
|
|
103
|
+
}));
|
|
87
104
|
if (networkClient === undefined) {
|
|
88
105
|
throw new Error("It looks like createNetworkClient returned undefined. Perhaps it's mocked?");
|
|
89
106
|
}
|
|
@@ -115,7 +132,11 @@ function createAutoManagedNetworkClient(networkClientConfiguration) {
|
|
|
115
132
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
116
133
|
return true;
|
|
117
134
|
}
|
|
118
|
-
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)(
|
|
135
|
+
networkClient ?? (networkClient = (0, create_network_client_1.createNetworkClient)({
|
|
136
|
+
configuration: networkClientConfiguration,
|
|
137
|
+
fetch: givenFetch,
|
|
138
|
+
btoa: givenBtoa,
|
|
139
|
+
}));
|
|
119
140
|
const { blockTracker } = networkClient;
|
|
120
141
|
return propertyName in blockTracker;
|
|
121
142
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-auto-managed-network-client.cjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":";;;AACA,uEAA8D;AAO9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA6B9C;;;;GAIG;AACH,gFAAgF;AAChF,gEAAgE;AAChE,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;AAEzD;;;;;;;;;;;;;GAaG;AACH,SAAgB,8BAA8B,CAG5C,0BAAyC;IAEzC,IAAI,aAAwC,CAAC;IAE7C,MAAM,aAAa,GAAG,IAAI,KAAK,CAAC,oBAAoB,EAAE;QACpD,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AAG,aAAa,CAAC;YACvC,OAAO,YAAY,IAAI,YAAY,CAAC;QACtC,CAAC;KACF,CACF,CAAC;IAEF,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,aAAa,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC,CAAC;IAEF,OAAO;QACL,aAAa,EAAE,0BAA0B;QACzC,QAAQ,EAAE,aAAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;KACR,CAAC;AACJ,CAAC;AA1HD,wEA0HC","sourcesContent":["import type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type {\n BlockTracker,\n NetworkClientConfiguration,\n Provider,\n} from './types';\n\n/**\n * The name of the method on both the provider and block tracker proxy which can\n * be used to get the underlying provider or block tracker from the network\n * client, when it is initialized.\n */\nconst REFLECTIVE_PROPERTY_NAME = '__target__';\n\n/**\n * Represents a proxy object which wraps a target object. As a proxy, it allows\n * for accessing and setting all of the properties that the target object\n * supports, but also supports an extra propertyName (`__target__`) to access\n * the target itself.\n *\n * @template Type - The type of the target object. It is assumed that this type\n * will be constant even when the target is swapped.\n */\nexport type ProxyWithAccessibleTarget<TargetType> = TargetType & {\n [REFLECTIVE_PROPERTY_NAME]: TargetType;\n};\n\n/**\n * An object that provides the same interface as a network client but where the\n * network client is not initialized until either the provider or block tracker\n * is first accessed.\n */\nexport type AutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n> = {\n configuration: Configuration;\n provider: ProxyWithAccessibleTarget<Provider>;\n blockTracker: ProxyWithAccessibleTarget<BlockTracker>;\n destroy: () => void;\n};\n\n/**\n * By default, the provider and block provider proxies will point to nothing.\n * This is impossible when using the Proxy API, as the target object has to be\n * something, so this object represents that \"something\".\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nconst UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };\n\n/**\n * This function creates two proxies, one that wraps a provider and another that\n * wraps a block tracker. These proxies are unique in that both will be \"empty\"\n * at first; that is, neither will point to a functional provider or block\n * tracker. Instead, as soon as a method or event is accessed on either object\n * that requires a network request to function, a network client is created on\n * the fly and the method or event in question is then forwarded to whichever\n * part of the network client is serving as the receiver. The network client is\n * then cached for subsequent usages.\n *\n * @param networkClientConfiguration - The configuration object that will be\n * used to instantiate the network client when it is needed.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>(\n networkClientConfiguration: Configuration,\n): AutoManagedNetworkClient<Configuration> {\n let networkClient: NetworkClient | undefined;\n\n const providerProxy = new Proxy(UNINITIALIZED_TARGET, {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.provider;\n }\n\n networkClient ??= createNetworkClient(networkClientConfiguration);\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?\",\n );\n }\n const { provider } = networkClient;\n\n if (propertyName in provider) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = provider[propertyName as keyof typeof provider];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be compatible\n // with the signature of the method being called, as technically\n // it can be anything.\n return value.apply(this === receiver ? provider : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient(networkClientConfiguration);\n const { provider } = networkClient;\n return propertyName in provider;\n },\n });\n\n const blockTrackerProxy: ProxyWithAccessibleTarget<BlockTracker> = new Proxy(\n UNINITIALIZED_TARGET,\n {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.blockTracker;\n }\n\n networkClient ??= createNetworkClient(networkClientConfiguration);\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like createNetworkClient returned undefined. Perhaps it's mocked?\",\n );\n }\n const { blockTracker } = networkClient;\n\n if (propertyName in blockTracker) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = blockTracker[propertyName as keyof typeof blockTracker];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be\n // compatible with the signature of the method being called, as\n // technically it can be anything.\n return value.apply(this === receiver ? blockTracker : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient(networkClientConfiguration);\n const { blockTracker } = networkClient;\n return propertyName in blockTracker;\n },\n },\n );\n\n const destroy = () => {\n networkClient?.destroy();\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"create-auto-managed-network-client.cjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":";;;AACA,uEAA8D;AAO9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA6B9C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,8DAA8D;oBAC9D,OAAO,UAAyB,GAAG,IAAW;wBAC5C,wDAAwD;wBACxD,+DAA+D;wBAC/D,kCAAkC;wBAClC,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;oBACpE,CAAC,CAAC;iBACH;gBACD,OAAO,KAAK,CAAC;aACd;YAED,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,gCAAgC;QAChC,8DAA8D;QAC9D,GAAG,CAAC,OAAY,EAAE,YAAyB;YACzC,IAAI,YAAY,KAAK,wBAAwB,EAAE;gBAC7C,OAAO,IAAI,CAAC;aACb;YACD,aAAa,KAAb,aAAa,GAAK,IAAA,2CAAmB,EAAC;gBACpC,aAAa,EAAE,0BAA0B;gBACzC,KAAK,EAAE,UAAU;gBACjB,IAAI,EAAE,SAAS;aAChB,CAAC,EAAC;YACH,MAAM,EAAE,YAAY,EAAE,GAAG,aAAa,CAAC;YACvC,OAAO,YAAY,IAAI,YAAY,CAAC;QACtC,CAAC;KACF,CACF,CAAC;IAEF,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,aAAa,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC,CAAC;IAEF,OAAO;QACL,aAAa,EAAE,0BAA0B;QACzC,QAAQ,EAAE,aAAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;KACR,CAAC;AACJ,CAAC;AAhJD,wEAgJC","sourcesContent":["import type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type {\n BlockTracker,\n NetworkClientConfiguration,\n Provider,\n} from './types';\n\n/**\n * The name of the method on both the provider and block tracker proxy which can\n * be used to get the underlying provider or block tracker from the network\n * client, when it is initialized.\n */\nconst REFLECTIVE_PROPERTY_NAME = '__target__';\n\n/**\n * Represents a proxy object which wraps a target object. As a proxy, it allows\n * for accessing and setting all of the properties that the target object\n * supports, but also supports an extra propertyName (`__target__`) to access\n * the target itself.\n *\n * @template Type - The type of the target object. It is assumed that this type\n * will be constant even when the target is swapped.\n */\nexport type ProxyWithAccessibleTarget<TargetType> = TargetType & {\n [REFLECTIVE_PROPERTY_NAME]: TargetType;\n};\n\n/**\n * An object that provides the same interface as a network client but where the\n * network client is not initialized until either the provider or block tracker\n * is first accessed.\n */\nexport type AutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n> = {\n configuration: Configuration;\n provider: ProxyWithAccessibleTarget<Provider>;\n blockTracker: ProxyWithAccessibleTarget<BlockTracker>;\n destroy: () => void;\n};\n\n/**\n * By default, the provider and block provider proxies will point to nothing.\n * This is impossible when using the Proxy API, as the target object has to be\n * something, so this object represents that \"something\".\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nconst UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };\n\n/**\n * This function creates two proxies, one that wraps a provider and another that\n * wraps a block tracker. These proxies are unique in that both will be \"empty\"\n * at first; that is, neither will point to a functional provider or block\n * tracker. Instead, as soon as a method or event is accessed on either object\n * that requires a network request to function, a network client is created on\n * the fly and the method or event in question is then forwarded to whichever\n * part of the network client is serving as the receiver. The network client is\n * then cached for subsequent usages.\n *\n * @param args - The arguments.\n * @param args.networkClientConfiguration - The configuration object that will be\n * used to instantiate the network client when it is needed.\n * @param args.fetch - A function that can be used to make an HTTP request,\n * compatible with the Fetch API.\n * @param args.btoa - A function that can be used to convert a binary string\n * into base-64.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>({\n networkClientConfiguration,\n fetch: givenFetch,\n btoa: givenBtoa,\n}: {\n networkClientConfiguration: Configuration;\n fetch: typeof fetch;\n btoa: typeof btoa;\n}): AutoManagedNetworkClient<Configuration> {\n let networkClient: NetworkClient | undefined;\n\n const providerProxy = new Proxy(UNINITIALIZED_TARGET, {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.provider;\n }\n\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n fetch: givenFetch,\n btoa: givenBtoa,\n });\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?\",\n );\n }\n const { provider } = networkClient;\n\n if (propertyName in provider) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = provider[propertyName as keyof typeof provider];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be compatible\n // with the signature of the method being called, as technically\n // it can be anything.\n return value.apply(this === receiver ? provider : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n fetch: givenFetch,\n btoa: givenBtoa,\n });\n const { provider } = networkClient;\n return propertyName in provider;\n },\n });\n\n const blockTrackerProxy: ProxyWithAccessibleTarget<BlockTracker> = new Proxy(\n UNINITIALIZED_TARGET,\n {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.blockTracker;\n }\n\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n fetch: givenFetch,\n btoa: givenBtoa,\n });\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like createNetworkClient returned undefined. Perhaps it's mocked?\",\n );\n }\n const { blockTracker } = networkClient;\n\n if (propertyName in blockTracker) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = blockTracker[propertyName as keyof typeof blockTracker];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be\n // compatible with the signature of the method being called, as\n // technically it can be anything.\n return value.apply(this === receiver ? blockTracker : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n fetch: givenFetch,\n btoa: givenBtoa,\n });\n const { blockTracker } = networkClient;\n return propertyName in blockTracker;\n },\n },\n );\n\n const destroy = () => {\n networkClient?.destroy();\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n };\n}\n"]}
|
|
@@ -38,10 +38,19 @@ export type AutoManagedNetworkClient<Configuration extends NetworkClientConfigur
|
|
|
38
38
|
* part of the network client is serving as the receiver. The network client is
|
|
39
39
|
* then cached for subsequent usages.
|
|
40
40
|
*
|
|
41
|
-
* @param
|
|
41
|
+
* @param args - The arguments.
|
|
42
|
+
* @param args.networkClientConfiguration - The configuration object that will be
|
|
42
43
|
* used to instantiate the network client when it is needed.
|
|
44
|
+
* @param args.fetch - A function that can be used to make an HTTP request,
|
|
45
|
+
* compatible with the Fetch API.
|
|
46
|
+
* @param args.btoa - A function that can be used to convert a binary string
|
|
47
|
+
* into base-64.
|
|
43
48
|
* @returns The auto-managed network client.
|
|
44
49
|
*/
|
|
45
|
-
export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>(networkClientConfiguration:
|
|
50
|
+
export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>({ networkClientConfiguration, fetch: givenFetch, btoa: givenBtoa, }: {
|
|
51
|
+
networkClientConfiguration: Configuration;
|
|
52
|
+
fetch: typeof fetch;
|
|
53
|
+
btoa: typeof btoa;
|
|
54
|
+
}): AutoManagedNetworkClient<Configuration>;
|
|
46
55
|
export {};
|
|
47
56
|
//# sourceMappingURL=create-auto-managed-network-client.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-auto-managed-network-client.d.cts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,EACZ,0BAA0B,EAC1B,QAAQ,EACT,oBAAgB;AAEjB;;;;GAIG;AACH,QAAA,MAAM,wBAAwB,eAAe,CAAC;AAE9C;;;;;;;;GAQG;AACH,MAAM,MAAM,yBAAyB,CAAC,UAAU,IAAI,UAAU,GAAG;IAC/D,CAAC,wBAAwB,CAAC,EAAE,UAAU,CAAC;CACxC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,CAClC,aAAa,SAAS,0BAA0B,IAC9C;IACF,aAAa,EAAE,aAAa,CAAC;IAC7B,QAAQ,EAAE,yBAAyB,CAAC,QAAQ,CAAC,CAAC;IAC9C,YAAY,EAAE,yBAAyB,CAAC,YAAY,CAAC,CAAC;IACtD,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAWF
|
|
1
|
+
{"version":3,"file":"create-auto-managed-network-client.d.cts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,EACZ,0BAA0B,EAC1B,QAAQ,EACT,oBAAgB;AAEjB;;;;GAIG;AACH,QAAA,MAAM,wBAAwB,eAAe,CAAC;AAE9C;;;;;;;;GAQG;AACH,MAAM,MAAM,yBAAyB,CAAC,UAAU,IAAI,UAAU,GAAG;IAC/D,CAAC,wBAAwB,CAAC,EAAE,UAAU,CAAC;CACxC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,CAClC,aAAa,SAAS,0BAA0B,IAC9C;IACF,aAAa,EAAE,aAAa,CAAC;IAC7B,QAAQ,EAAE,yBAAyB,CAAC,QAAQ,CAAC,CAAC;IAC9C,YAAY,EAAE,yBAAyB,CAAC,YAAY,CAAC,CAAC;IACtD,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAWF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,8BAA8B,CAC5C,aAAa,SAAS,0BAA0B,EAChD,EACA,0BAA0B,EAC1B,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,GAChB,EAAE;IACD,0BAA0B,EAAE,aAAa,CAAC;IAC1C,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,IAAI,EAAE,OAAO,IAAI,CAAC;CACnB,GAAG,wBAAwB,CAAC,aAAa,CAAC,CAsI1C"}
|
|
@@ -38,10 +38,19 @@ export type AutoManagedNetworkClient<Configuration extends NetworkClientConfigur
|
|
|
38
38
|
* part of the network client is serving as the receiver. The network client is
|
|
39
39
|
* then cached for subsequent usages.
|
|
40
40
|
*
|
|
41
|
-
* @param
|
|
41
|
+
* @param args - The arguments.
|
|
42
|
+
* @param args.networkClientConfiguration - The configuration object that will be
|
|
42
43
|
* used to instantiate the network client when it is needed.
|
|
44
|
+
* @param args.fetch - A function that can be used to make an HTTP request,
|
|
45
|
+
* compatible with the Fetch API.
|
|
46
|
+
* @param args.btoa - A function that can be used to convert a binary string
|
|
47
|
+
* into base-64.
|
|
43
48
|
* @returns The auto-managed network client.
|
|
44
49
|
*/
|
|
45
|
-
export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>(networkClientConfiguration:
|
|
50
|
+
export declare function createAutoManagedNetworkClient<Configuration extends NetworkClientConfiguration>({ networkClientConfiguration, fetch: givenFetch, btoa: givenBtoa, }: {
|
|
51
|
+
networkClientConfiguration: Configuration;
|
|
52
|
+
fetch: typeof fetch;
|
|
53
|
+
btoa: typeof btoa;
|
|
54
|
+
}): AutoManagedNetworkClient<Configuration>;
|
|
46
55
|
export {};
|
|
47
56
|
//# sourceMappingURL=create-auto-managed-network-client.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-auto-managed-network-client.d.mts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,EACZ,0BAA0B,EAC1B,QAAQ,EACT,oBAAgB;AAEjB;;;;GAIG;AACH,QAAA,MAAM,wBAAwB,eAAe,CAAC;AAE9C;;;;;;;;GAQG;AACH,MAAM,MAAM,yBAAyB,CAAC,UAAU,IAAI,UAAU,GAAG;IAC/D,CAAC,wBAAwB,CAAC,EAAE,UAAU,CAAC;CACxC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,CAClC,aAAa,SAAS,0BAA0B,IAC9C;IACF,aAAa,EAAE,aAAa,CAAC;IAC7B,QAAQ,EAAE,yBAAyB,CAAC,QAAQ,CAAC,CAAC;IAC9C,YAAY,EAAE,yBAAyB,CAAC,YAAY,CAAC,CAAC;IACtD,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAWF
|
|
1
|
+
{"version":3,"file":"create-auto-managed-network-client.d.mts","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,YAAY,EACZ,0BAA0B,EAC1B,QAAQ,EACT,oBAAgB;AAEjB;;;;GAIG;AACH,QAAA,MAAM,wBAAwB,eAAe,CAAC;AAE9C;;;;;;;;GAQG;AACH,MAAM,MAAM,yBAAyB,CAAC,UAAU,IAAI,UAAU,GAAG;IAC/D,CAAC,wBAAwB,CAAC,EAAE,UAAU,CAAC;CACxC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,CAClC,aAAa,SAAS,0BAA0B,IAC9C;IACF,aAAa,EAAE,aAAa,CAAC;IAC7B,QAAQ,EAAE,yBAAyB,CAAC,QAAQ,CAAC,CAAC;IAC9C,YAAY,EAAE,yBAAyB,CAAC,YAAY,CAAC,CAAC;IACtD,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAWF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,8BAA8B,CAC5C,aAAa,SAAS,0BAA0B,EAChD,EACA,0BAA0B,EAC1B,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,GAChB,EAAE;IACD,0BAA0B,EAAE,aAAa,CAAC;IAC1C,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,IAAI,EAAE,OAAO,IAAI,CAAC;CACnB,GAAG,wBAAwB,CAAC,aAAa,CAAC,CAsI1C"}
|
|
@@ -23,11 +23,16 @@ const UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };
|
|
|
23
23
|
* part of the network client is serving as the receiver. The network client is
|
|
24
24
|
* then cached for subsequent usages.
|
|
25
25
|
*
|
|
26
|
-
* @param
|
|
26
|
+
* @param args - The arguments.
|
|
27
|
+
* @param args.networkClientConfiguration - The configuration object that will be
|
|
27
28
|
* used to instantiate the network client when it is needed.
|
|
29
|
+
* @param args.fetch - A function that can be used to make an HTTP request,
|
|
30
|
+
* compatible with the Fetch API.
|
|
31
|
+
* @param args.btoa - A function that can be used to convert a binary string
|
|
32
|
+
* into base-64.
|
|
28
33
|
* @returns The auto-managed network client.
|
|
29
34
|
*/
|
|
30
|
-
export function createAutoManagedNetworkClient(networkClientConfiguration) {
|
|
35
|
+
export function createAutoManagedNetworkClient({ networkClientConfiguration, fetch: givenFetch, btoa: givenBtoa, }) {
|
|
31
36
|
let networkClient;
|
|
32
37
|
const providerProxy = new Proxy(UNINITIALIZED_TARGET, {
|
|
33
38
|
// TODO: Replace `any` with type
|
|
@@ -36,7 +41,11 @@ export function createAutoManagedNetworkClient(networkClientConfiguration) {
|
|
|
36
41
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
37
42
|
return networkClient?.provider;
|
|
38
43
|
}
|
|
39
|
-
networkClient ?? (networkClient = createNetworkClient(
|
|
44
|
+
networkClient ?? (networkClient = createNetworkClient({
|
|
45
|
+
configuration: networkClientConfiguration,
|
|
46
|
+
fetch: givenFetch,
|
|
47
|
+
btoa: givenBtoa,
|
|
48
|
+
}));
|
|
40
49
|
if (networkClient === undefined) {
|
|
41
50
|
throw new Error("It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?");
|
|
42
51
|
}
|
|
@@ -68,7 +77,11 @@ export function createAutoManagedNetworkClient(networkClientConfiguration) {
|
|
|
68
77
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
69
78
|
return true;
|
|
70
79
|
}
|
|
71
|
-
networkClient ?? (networkClient = createNetworkClient(
|
|
80
|
+
networkClient ?? (networkClient = createNetworkClient({
|
|
81
|
+
configuration: networkClientConfiguration,
|
|
82
|
+
fetch: givenFetch,
|
|
83
|
+
btoa: givenBtoa,
|
|
84
|
+
}));
|
|
72
85
|
const { provider } = networkClient;
|
|
73
86
|
return propertyName in provider;
|
|
74
87
|
},
|
|
@@ -80,7 +93,11 @@ export function createAutoManagedNetworkClient(networkClientConfiguration) {
|
|
|
80
93
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
81
94
|
return networkClient?.blockTracker;
|
|
82
95
|
}
|
|
83
|
-
networkClient ?? (networkClient = createNetworkClient(
|
|
96
|
+
networkClient ?? (networkClient = createNetworkClient({
|
|
97
|
+
configuration: networkClientConfiguration,
|
|
98
|
+
fetch: givenFetch,
|
|
99
|
+
btoa: givenBtoa,
|
|
100
|
+
}));
|
|
84
101
|
if (networkClient === undefined) {
|
|
85
102
|
throw new Error("It looks like createNetworkClient returned undefined. Perhaps it's mocked?");
|
|
86
103
|
}
|
|
@@ -112,7 +129,11 @@ export function createAutoManagedNetworkClient(networkClientConfiguration) {
|
|
|
112
129
|
if (propertyName === REFLECTIVE_PROPERTY_NAME) {
|
|
113
130
|
return true;
|
|
114
131
|
}
|
|
115
|
-
networkClient ?? (networkClient = createNetworkClient(
|
|
132
|
+
networkClient ?? (networkClient = createNetworkClient({
|
|
133
|
+
configuration: networkClientConfiguration,
|
|
134
|
+
fetch: givenFetch,
|
|
135
|
+
btoa: givenBtoa,
|
|
136
|
+
}));
|
|
116
137
|
const { blockTracker } = networkClient;
|
|
117
138
|
return propertyName in blockTracker;
|
|
118
139
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-auto-managed-network-client.mjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,oCAAgC;AAO9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA6B9C;;;;GAIG;AACH,gFAAgF;AAChF,gEAAgE;AAChE,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;AAEzD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,8BAA8B,CAG5C,0BAAyC;IAEzC,IAAI,aAAwC,CAAC;IAE7C,MAAM,aAAa,GAAG,IAAI,KAAK,CAAC,oBAAoB,EAAE;QACpD,gCAAgC;QAChC,8DAA8D;QAC9D,GAAG,CAAC,OAAY,EAAE,YAAyB,EAAE,QAAiB;YAC5D,IAAI,YAAY,KAAK,wBAAwB,EAAE;gBAC7C,OAAO,aAAa,EAAE,QAAQ,CAAC;aAChC;YAED,aAAa,KAAb,aAAa,GAAK,mBAAmB,CAAC,0BAA0B,CAAC,EAAC;YAClE,IAAI,aAAa,KAAK,SAAS,EAAE;gBAC/B,MAAM,IAAI,KAAK,CACb,wFAAwF,CACzF,CAAC;aACH;YACD,MAAM,EAAE,QAAQ,EAAE,GAAG,aAAa,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AA4E,CAC7E,CAAC;aACH;YACD,MAAM,EAAE,YAAY,EAAE,GAAG,aAAa,CAAC;YAEvC,IAAI,YAAY,IAAI,YAAY,EAAE;gBAChC,+DAA+D;gBAC/D,cAAc;gBACd,MAAM,KAAK,GAAG,YAAY,CAAC,YAAyC,CAAC,CAAC;gBACtE,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE;oBAC/B,kEAAkE;oBAClE,2DAA2D;oBAC3D,gDAAgD;oBAChD,gCAAgC;oBAChC,8DAA8D;oBAC9D,OAAO,UAAyB,GAAG,IAAW;wBAC5C,wDAAwD;wBACxD,+DAA+D;wBAC/D,kCAAkC;wBAClC,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;oBACpE,CAAC,CAAC;iBACH;gBACD,OAAO,KAAK,CAAC;aACd;YAED,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,gCAAgC;QAChC,8DAA8D;QAC9D,GAAG,CAAC,OAAY,EAAE,YAAyB;YACzC,IAAI,YAAY,KAAK,wBAAwB,EAAE;gBAC7C,OAAO,IAAI,CAAC;aACb;YACD,aAAa,KAAb,aAAa,GAAK,mBAAmB,CAAC,0BAA0B,CAAC,EAAC;YAClE,MAAM,EAAE,YAAY,EAAE,GAAG,aAAa,CAAC;YACvC,OAAO,YAAY,IAAI,YAAY,CAAC;QACtC,CAAC;KACF,CACF,CAAC;IAEF,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,aAAa,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC,CAAC;IAEF,OAAO;QACL,aAAa,EAAE,0BAA0B;QACzC,QAAQ,EAAE,aAAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;KACR,CAAC;AACJ,CAAC","sourcesContent":["import type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type {\n BlockTracker,\n NetworkClientConfiguration,\n Provider,\n} from './types';\n\n/**\n * The name of the method on both the provider and block tracker proxy which can\n * be used to get the underlying provider or block tracker from the network\n * client, when it is initialized.\n */\nconst REFLECTIVE_PROPERTY_NAME = '__target__';\n\n/**\n * Represents a proxy object which wraps a target object. As a proxy, it allows\n * for accessing and setting all of the properties that the target object\n * supports, but also supports an extra propertyName (`__target__`) to access\n * the target itself.\n *\n * @template Type - The type of the target object. It is assumed that this type\n * will be constant even when the target is swapped.\n */\nexport type ProxyWithAccessibleTarget<TargetType> = TargetType & {\n [REFLECTIVE_PROPERTY_NAME]: TargetType;\n};\n\n/**\n * An object that provides the same interface as a network client but where the\n * network client is not initialized until either the provider or block tracker\n * is first accessed.\n */\nexport type AutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n> = {\n configuration: Configuration;\n provider: ProxyWithAccessibleTarget<Provider>;\n blockTracker: ProxyWithAccessibleTarget<BlockTracker>;\n destroy: () => void;\n};\n\n/**\n * By default, the provider and block provider proxies will point to nothing.\n * This is impossible when using the Proxy API, as the target object has to be\n * something, so this object represents that \"something\".\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nconst UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };\n\n/**\n * This function creates two proxies, one that wraps a provider and another that\n * wraps a block tracker. These proxies are unique in that both will be \"empty\"\n * at first; that is, neither will point to a functional provider or block\n * tracker. Instead, as soon as a method or event is accessed on either object\n * that requires a network request to function, a network client is created on\n * the fly and the method or event in question is then forwarded to whichever\n * part of the network client is serving as the receiver. The network client is\n * then cached for subsequent usages.\n *\n * @param networkClientConfiguration - The configuration object that will be\n * used to instantiate the network client when it is needed.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>(\n networkClientConfiguration: Configuration,\n): AutoManagedNetworkClient<Configuration> {\n let networkClient: NetworkClient | undefined;\n\n const providerProxy = new Proxy(UNINITIALIZED_TARGET, {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.provider;\n }\n\n networkClient ??= createNetworkClient(networkClientConfiguration);\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?\",\n );\n }\n const { provider } = networkClient;\n\n if (propertyName in provider) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = provider[propertyName as keyof typeof provider];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be compatible\n // with the signature of the method being called, as technically\n // it can be anything.\n return value.apply(this === receiver ? provider : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient(networkClientConfiguration);\n const { provider } = networkClient;\n return propertyName in provider;\n },\n });\n\n const blockTrackerProxy: ProxyWithAccessibleTarget<BlockTracker> = new Proxy(\n UNINITIALIZED_TARGET,\n {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.blockTracker;\n }\n\n networkClient ??= createNetworkClient(networkClientConfiguration);\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like createNetworkClient returned undefined. Perhaps it's mocked?\",\n );\n }\n const { blockTracker } = networkClient;\n\n if (propertyName in blockTracker) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = blockTracker[propertyName as keyof typeof blockTracker];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be\n // compatible with the signature of the method being called, as\n // technically it can be anything.\n return value.apply(this === receiver ? blockTracker : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient(networkClientConfiguration);\n const { blockTracker } = networkClient;\n return propertyName in blockTracker;\n },\n },\n );\n\n const destroy = () => {\n networkClient?.destroy();\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"create-auto-managed-network-client.mjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,oCAAgC;AAO9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA6B9C;;;;GAIG;AACH,gFAAgF;AAChF,gEAAgE;AAChE,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;AAEzD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,8BAA8B,CAE5C,EACA,0BAA0B,EAC1B,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,GAKhB;IACC,IAAI,aAAwC,CAAC;IAE7C,MAAM,aAAa,GAAG,IAAI,KAAK,CAAC,oBAAoB,EAAE;QACpD,gCAAgC;QAChC,8DAA8D;QAC9D,GAAG,CAAC,OAA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,YAAY,KAAK,wBAAwB,EAAE;gBAC7C,OAAO,IAAI,CAAC;aACb;YACD,aAAa,KAAb,aAAa,GAAK,mBAAmB,CAAC;gBACpC,aAAa,EAAE,0BAA0B;gBACzC,KAAK,EAAE,UAAU;gBACjB,IAAI,EAAE,SAAS;aAChB,CAAC,EAAC;YACH,MAAM,EAAE,YAAY,EAAE,GAAG,aAAa,CAAC;YACvC,OAAO,YAAY,IAAI,YAAY,CAAC;QACtC,CAAC;KACF,CACF,CAAC;IAEF,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,aAAa,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC,CAAC;IAEF,OAAO;QACL,aAAa,EAAE,0BAA0B;QACzC,QAAQ,EAAE,aAAa;QACvB,YAAY,EAAE,iBAAiB;QAC/B,OAAO;KACR,CAAC;AACJ,CAAC","sourcesContent":["import type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type {\n BlockTracker,\n NetworkClientConfiguration,\n Provider,\n} from './types';\n\n/**\n * The name of the method on both the provider and block tracker proxy which can\n * be used to get the underlying provider or block tracker from the network\n * client, when it is initialized.\n */\nconst REFLECTIVE_PROPERTY_NAME = '__target__';\n\n/**\n * Represents a proxy object which wraps a target object. As a proxy, it allows\n * for accessing and setting all of the properties that the target object\n * supports, but also supports an extra propertyName (`__target__`) to access\n * the target itself.\n *\n * @template Type - The type of the target object. It is assumed that this type\n * will be constant even when the target is swapped.\n */\nexport type ProxyWithAccessibleTarget<TargetType> = TargetType & {\n [REFLECTIVE_PROPERTY_NAME]: TargetType;\n};\n\n/**\n * An object that provides the same interface as a network client but where the\n * network client is not initialized until either the provider or block tracker\n * is first accessed.\n */\nexport type AutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n> = {\n configuration: Configuration;\n provider: ProxyWithAccessibleTarget<Provider>;\n blockTracker: ProxyWithAccessibleTarget<BlockTracker>;\n destroy: () => void;\n};\n\n/**\n * By default, the provider and block provider proxies will point to nothing.\n * This is impossible when using the Proxy API, as the target object has to be\n * something, so this object represents that \"something\".\n */\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nconst UNINITIALIZED_TARGET = { __UNINITIALIZED__: true };\n\n/**\n * This function creates two proxies, one that wraps a provider and another that\n * wraps a block tracker. These proxies are unique in that both will be \"empty\"\n * at first; that is, neither will point to a functional provider or block\n * tracker. Instead, as soon as a method or event is accessed on either object\n * that requires a network request to function, a network client is created on\n * the fly and the method or event in question is then forwarded to whichever\n * part of the network client is serving as the receiver. The network client is\n * then cached for subsequent usages.\n *\n * @param args - The arguments.\n * @param args.networkClientConfiguration - The configuration object that will be\n * used to instantiate the network client when it is needed.\n * @param args.fetch - A function that can be used to make an HTTP request,\n * compatible with the Fetch API.\n * @param args.btoa - A function that can be used to convert a binary string\n * into base-64.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>({\n networkClientConfiguration,\n fetch: givenFetch,\n btoa: givenBtoa,\n}: {\n networkClientConfiguration: Configuration;\n fetch: typeof fetch;\n btoa: typeof btoa;\n}): AutoManagedNetworkClient<Configuration> {\n let networkClient: NetworkClient | undefined;\n\n const providerProxy = new Proxy(UNINITIALIZED_TARGET, {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.provider;\n }\n\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n fetch: givenFetch,\n btoa: givenBtoa,\n });\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like `createNetworkClient` didn't return anything. Perhaps it's being mocked?\",\n );\n }\n const { provider } = networkClient;\n\n if (propertyName in provider) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = provider[propertyName as keyof typeof provider];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be compatible\n // with the signature of the method being called, as technically\n // it can be anything.\n return value.apply(this === receiver ? provider : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n fetch: givenFetch,\n btoa: givenBtoa,\n });\n const { provider } = networkClient;\n return propertyName in provider;\n },\n });\n\n const blockTrackerProxy: ProxyWithAccessibleTarget<BlockTracker> = new Proxy(\n UNINITIALIZED_TARGET,\n {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n get(_target: any, propertyName: PropertyKey, receiver: unknown) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return networkClient?.blockTracker;\n }\n\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n fetch: givenFetch,\n btoa: givenBtoa,\n });\n if (networkClient === undefined) {\n throw new Error(\n \"It looks like createNetworkClient returned undefined. Perhaps it's mocked?\",\n );\n }\n const { blockTracker } = networkClient;\n\n if (propertyName in blockTracker) {\n // Typecast: We know that `[propertyName]` is a propertyName on\n // `provider`.\n const value = blockTracker[propertyName as keyof typeof blockTracker];\n if (typeof value === 'function') {\n // Ensure that the method on the provider is called with `this` as\n // the target, *not* the proxy (which happens by default) —\n // this allows private properties to be accessed\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (this: unknown, ...args: any[]) {\n // @ts-expect-error We don't care that `this` may not be\n // compatible with the signature of the method being called, as\n // technically it can be anything.\n return value.apply(this === receiver ? blockTracker : this, args);\n };\n }\n return value;\n }\n\n return undefined;\n },\n\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n has(_target: any, propertyName: PropertyKey) {\n if (propertyName === REFLECTIVE_PROPERTY_NAME) {\n return true;\n }\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n fetch: givenFetch,\n btoa: givenBtoa,\n });\n const { blockTracker } = networkClient;\n return propertyName in blockTracker;\n },\n },\n );\n\n const destroy = () => {\n networkClient?.destroy();\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n };\n}\n"]}
|
|
@@ -7,45 +7,58 @@ const eth_json_rpc_infura_1 = require("@metamask/eth-json-rpc-infura");
|
|
|
7
7
|
const eth_json_rpc_middleware_1 = require("@metamask/eth-json-rpc-middleware");
|
|
8
8
|
const eth_json_rpc_provider_1 = require("@metamask/eth-json-rpc-provider");
|
|
9
9
|
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
10
|
+
const rpc_service_1 = require("./rpc-service/rpc-service.cjs");
|
|
10
11
|
const types_1 = require("./types.cjs");
|
|
11
12
|
const SECOND = 1000;
|
|
12
13
|
/**
|
|
13
14
|
* Create a JSON RPC network client for a specific network.
|
|
14
15
|
*
|
|
15
|
-
* @param
|
|
16
|
+
* @param args - The arguments.
|
|
17
|
+
* @param args.configuration - The network configuration.
|
|
18
|
+
* @param args.fetch - A function that can be used to make an HTTP request,
|
|
19
|
+
* compatible with the Fetch API.
|
|
20
|
+
* @param args.btoa - A function that can be used to convert a binary string
|
|
21
|
+
* into base-64.
|
|
16
22
|
* @returns The network client.
|
|
17
23
|
*/
|
|
18
|
-
function createNetworkClient(
|
|
19
|
-
const
|
|
20
|
-
?
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
source: 'metamask',
|
|
24
|
+
function createNetworkClient({ configuration, fetch: givenFetch, btoa: givenBtoa, }) {
|
|
25
|
+
const rpcService = configuration.type === types_1.NetworkClientType.Infura
|
|
26
|
+
? new rpc_service_1.RpcService({
|
|
27
|
+
fetch: givenFetch,
|
|
28
|
+
btoa: givenBtoa,
|
|
29
|
+
endpointUrl: `https://${configuration.network}.infura.io/v3/${configuration.infuraProjectId}`,
|
|
25
30
|
})
|
|
26
|
-
:
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
: new rpc_service_1.RpcService({
|
|
32
|
+
fetch: givenFetch,
|
|
33
|
+
btoa: givenBtoa,
|
|
34
|
+
endpointUrl: configuration.rpcUrl,
|
|
30
35
|
});
|
|
36
|
+
const rpcApiMiddleware = configuration.type === types_1.NetworkClientType.Infura
|
|
37
|
+
? (0, eth_json_rpc_infura_1.createInfuraMiddleware)({
|
|
38
|
+
rpcService,
|
|
39
|
+
options: {
|
|
40
|
+
source: 'metamask',
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
: (0, eth_json_rpc_middleware_1.createFetchMiddleware)({ rpcService });
|
|
31
44
|
const rpcProvider = (0, eth_json_rpc_provider_1.providerFromMiddleware)(rpcApiMiddleware);
|
|
32
|
-
const blockTrackerOpts = process.env.IN_TEST &&
|
|
45
|
+
const blockTrackerOpts = process.env.IN_TEST && configuration.type === types_1.NetworkClientType.Custom
|
|
33
46
|
? { pollingInterval: SECOND }
|
|
34
47
|
: {};
|
|
35
48
|
const blockTracker = new eth_block_tracker_1.PollingBlockTracker({
|
|
36
49
|
...blockTrackerOpts,
|
|
37
50
|
provider: rpcProvider,
|
|
38
51
|
});
|
|
39
|
-
const networkMiddleware =
|
|
52
|
+
const networkMiddleware = configuration.type === types_1.NetworkClientType.Infura
|
|
40
53
|
? createInfuraNetworkMiddleware({
|
|
41
54
|
blockTracker,
|
|
42
|
-
network:
|
|
55
|
+
network: configuration.network,
|
|
43
56
|
rpcProvider,
|
|
44
57
|
rpcApiMiddleware,
|
|
45
58
|
})
|
|
46
59
|
: createCustomNetworkMiddleware({
|
|
47
60
|
blockTracker,
|
|
48
|
-
chainId:
|
|
61
|
+
chainId: configuration.chainId,
|
|
49
62
|
rpcApiMiddleware,
|
|
50
63
|
});
|
|
51
64
|
const engine = new json_rpc_engine_1.JsonRpcEngine();
|
|
@@ -56,7 +69,7 @@ function createNetworkClient(networkConfig) {
|
|
|
56
69
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
57
70
|
blockTracker.destroy();
|
|
58
71
|
};
|
|
59
|
-
return { configuration
|
|
72
|
+
return { configuration, provider, blockTracker, destroy };
|
|
60
73
|
}
|
|
61
74
|
exports.createNetworkClient = createNetworkClient;
|
|
62
75
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-network-client.cjs","sourceRoot":"","sources":["../src/create-network-client.ts"],"names":[],"mappings":";;;AACA,iEAAqD;AACrD,mEAAkE;AAClE,uEAAuE;AACvE,+EAQ2C;AAE3C,2EAGyC;AACzC,+DAKmC;AASnC,uCAA4C;AAE5C,MAAM,MAAM,GAAG,IAAI,CAAC;AAapB;;;;;GAKG;AACH,SAAgB,mBAAmB,CACjC,aAAyC;IAEzC,MAAM,gBAAgB,GACpB,aAAa,CAAC,IAAI,KAAK,yBAAiB,CAAC,MAAM;QAC7C,CAAC,CAAC,IAAA,4CAAsB,EAAC;YACrB,OAAO,EAAE,aAAa,CAAC,OAAO;YAC9B,SAAS,EAAE,aAAa,CAAC,eAAe;YACxC,WAAW,EAAE,CAAC;YACd,MAAM,EAAE,UAAU;SACnB,CAAC;QACJ,CAAC,CAAC,IAAA,+CAAqB,EAAC;YACpB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,MAAM,EAAE,aAAa,CAAC,MAAM;SAC7B,CAAC,CAAC;IAET,MAAM,WAAW,GAAG,IAAA,8CAAsB,EAAC,gBAAgB,CAAC,CAAC;IAE7D,MAAM,gBAAgB,GACpB,OAAO,CAAC,GAAG,CAAC,OAAO,IAAI,aAAa,CAAC,IAAI,KAAK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S,oCAAoC;IAC3C,OAAO,IAAA,uCAAqB,EAAC,KAAK,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE;QAClD,IAAI,GAAG,CAAC,MAAM,KAAK,iBAAiB,EAAE;YACpC,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;SACjE;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { InfuraNetworkType } from '@metamask/controller-utils';\nimport { ChainId } from '@metamask/controller-utils';\nimport { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport { createInfuraMiddleware } from '@metamask/eth-json-rpc-infura';\nimport {\n createBlockCacheMiddleware,\n createBlockRefMiddleware,\n createBlockRefRewriteMiddleware,\n createBlockTrackerInspectorMiddleware,\n createInflightCacheMiddleware,\n createFetchMiddleware,\n createRetryOnEmptyMiddleware,\n} from '@metamask/eth-json-rpc-middleware';\nimport type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';\nimport {\n providerFromEngine,\n providerFromMiddleware,\n} from '@metamask/eth-json-rpc-provider';\nimport {\n createAsyncMiddleware,\n createScaffoldMiddleware,\n JsonRpcEngine,\n mergeMiddleware,\n} from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Hex, Json, JsonRpcParams } from '@metamask/utils';\n\nimport type {\n BlockTracker,\n NetworkClientConfiguration,\n Provider,\n} from './types';\nimport { NetworkClientType } from './types';\n\nconst SECOND = 1000;\n\n/**\n * The pair of provider / block tracker that can be used to interface with the\n * network and respond to new activity.\n */\nexport type NetworkClient = {\n configuration: NetworkClientConfiguration;\n provider: Provider;\n blockTracker: BlockTracker;\n destroy: () => void;\n};\n\n/**\n * Create a JSON RPC network client for a specific network.\n *\n * @param networkConfig - The network configuration.\n * @returns The network client.\n */\nexport function createNetworkClient(\n networkConfig: NetworkClientConfiguration,\n): NetworkClient {\n const rpcApiMiddleware =\n networkConfig.type === NetworkClientType.Infura\n ? createInfuraMiddleware({\n network: networkConfig.network,\n projectId: networkConfig.infuraProjectId,\n maxAttempts: 5,\n source: 'metamask',\n })\n : createFetchMiddleware({\n btoa: global.btoa,\n fetch: global.fetch,\n rpcUrl: networkConfig.rpcUrl,\n });\n\n const rpcProvider = providerFromMiddleware(rpcApiMiddleware);\n\n const blockTrackerOpts =\n process.env.IN_TEST && networkConfig.type === 'custom'\n ? { pollingInterval: SECOND }\n : {};\n const blockTracker = new PollingBlockTracker({\n ...blockTrackerOpts,\n provider: rpcProvider,\n });\n\n const networkMiddleware =\n networkConfig.type === NetworkClientType.Infura\n ? createInfuraNetworkMiddleware({\n blockTracker,\n network: networkConfig.network,\n rpcProvider,\n rpcApiMiddleware,\n })\n : createCustomNetworkMiddleware({\n blockTracker,\n chainId: networkConfig.chainId,\n rpcApiMiddleware,\n });\n\n const engine = new JsonRpcEngine();\n\n engine.push(networkMiddleware);\n\n const provider = providerFromEngine(engine);\n\n const destroy = () => {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n blockTracker.destroy();\n };\n\n return { configuration: networkConfig, provider, blockTracker, destroy };\n}\n\n/**\n * Create middleware for infura.\n *\n * @param args - The arguments.\n * @param args.blockTracker - The block tracker to use.\n * @param args.network - The Infura network to use.\n * @param args.rpcProvider - The RPC provider to use.\n * @param args.rpcApiMiddleware - Additional middleware.\n * @returns The collection of middleware that makes up the Infura client.\n */\nfunction createInfuraNetworkMiddleware({\n blockTracker,\n network,\n rpcProvider,\n rpcApiMiddleware,\n}: {\n blockTracker: PollingBlockTracker;\n network: InfuraNetworkType;\n rpcProvider: SafeEventEmitterProvider;\n rpcApiMiddleware: JsonRpcMiddleware<JsonRpcParams, Json>;\n}) {\n return mergeMiddleware([\n createNetworkAndChainIdMiddleware({ network }),\n createBlockCacheMiddleware({ blockTracker }),\n createInflightCacheMiddleware(),\n createBlockRefMiddleware({ blockTracker, provider: rpcProvider }),\n createRetryOnEmptyMiddleware({ blockTracker, provider: rpcProvider }),\n createBlockTrackerInspectorMiddleware({ blockTracker }),\n rpcApiMiddleware,\n ]);\n}\n\n/**\n * Creates static method middleware.\n *\n * @param args - The Arguments.\n * @param args.network - The Infura network to use.\n * @returns The middleware that implements the eth_chainId method.\n */\nfunction createNetworkAndChainIdMiddleware({\n network,\n}: {\n network: InfuraNetworkType;\n}) {\n return createScaffoldMiddleware({\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n eth_chainId: ChainId[network],\n });\n}\n\nconst createChainIdMiddleware = (\n chainId: Hex,\n): JsonRpcMiddleware<JsonRpcParams, Json> => {\n return (req, res, next, end) => {\n if (req.method === 'eth_chainId') {\n res.result = chainId;\n return end();\n }\n return next();\n };\n};\n\n/**\n * Creates custom middleware.\n *\n * @param args - The arguments.\n * @param args.blockTracker - The block tracker to use.\n * @param args.chainId - The chain id to use.\n * @param args.rpcApiMiddleware - Additional middleware.\n * @returns The collection of middleware that makes up the Infura client.\n */\nfunction createCustomNetworkMiddleware({\n blockTracker,\n chainId,\n rpcApiMiddleware,\n}: {\n blockTracker: PollingBlockTracker;\n chainId: Hex;\n rpcApiMiddleware: JsonRpcMiddleware<JsonRpcParams, Json>;\n}): JsonRpcMiddleware<JsonRpcParams, Json> {\n const testMiddlewares = process.env.IN_TEST\n ? [createEstimateGasDelayTestMiddleware()]\n : [];\n\n return mergeMiddleware([\n ...testMiddlewares,\n createChainIdMiddleware(chainId),\n createBlockRefRewriteMiddleware({ blockTracker }),\n createBlockCacheMiddleware({ blockTracker }),\n createInflightCacheMiddleware(),\n createBlockTrackerInspectorMiddleware({ blockTracker }),\n rpcApiMiddleware,\n ]);\n}\n\n/**\n * For use in tests only.\n * Adds a delay to `eth_estimateGas` calls.\n *\n * @returns The middleware for delaying gas estimation calls by 2 seconds when in test.\n */\nfunction createEstimateGasDelayTestMiddleware() {\n return createAsyncMiddleware(async (req, _, next) => {\n if (req.method === 'eth_estimateGas') {\n await new Promise((resolve) => setTimeout(resolve, SECOND * 2));\n }\n return next();\n });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"create-network-client.cjs","sourceRoot":"","sources":["../src/create-network-client.ts"],"names":[],"mappings":";;;AACA,iEAAqD;AACrD,mEAAkE;AAClE,uEAAuE;AACvE,+EAQ2C;AAE3C,2EAGyC;AACzC,+DAKmC;AAInC,+DAAuD;AAMvD,uCAA4C;AAE5C,MAAM,MAAM,GAAG,IAAI,CAAC;AAapB;;;;;;;;;;GAUG;AACH,SAAgB,mBAAmB,CAAC,EAClC,aAAa,EACb,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,GAKhB;IACC,MAAM,UAAU,GACd,aAAa,CAAC,IAAI,KAAK,yBAAiB,CAAC,MAAM;QAC7C,CAAC,CAAC,IAAI,wBAAU,CAAC;YACb,KAAK,EAAE,UAAU;YACjB,IAAI,EAAE,SAAS;YACf,WAAW,EAAE,WAAW,aAAa,CAAC,OAAO,iBAAiB,aAAa,CAAC,eAAe,EAAE;SAC9F,CAAC;QACJ,CAAC,CAAC,IAAI,wBAAU,CAAC;YACb,KAAK,EAAE,UAAU;YACjB,IAAI,EAAE,SAAS;YACf,WAAW,EAAE,aAAa,CAAC,MAAM;SAClC,CAAC,CAAC;IAET,MAAM,gBAAgB,GACpB,aAAa,CAAC,IAAI,KAAK,yBAAiB,CAAC,MAAM;QAC7C,CAAC,CAAC,IAAA,4CAAsB,EAAC;YACrB,UAAU;YACV,OAAO,EAAE;gBACP,MAAM,EAAE,UAAU;aACnB;SACF,CAAC;QACJ,CAAC,CAAC,IAAA,+CAAqB,EAAC,EAAE,UAAU,EAAE,CAAC,CAAC;IAE5C,MAAM,WAAW,GAAG,IAAA,8CAAsB,EAAC,gBAAgB,CAAC,CAAC;IAE7D,MAAM,gBAAgB,GACpB,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AAgB;KACjB,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,SAAS,oCAAoC;IAC3C,OAAO,IAAA,uCAAqB,EAAC,KAAK,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE;QAClD,IAAI,GAAG,CAAC,MAAM,KAAK,iBAAiB,EAAE;YACpC,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;SACjE;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { InfuraNetworkType } from '@metamask/controller-utils';\nimport { ChainId } from '@metamask/controller-utils';\nimport { PollingBlockTracker } from '@metamask/eth-block-tracker';\nimport { createInfuraMiddleware } from '@metamask/eth-json-rpc-infura';\nimport {\n createBlockCacheMiddleware,\n createBlockRefMiddleware,\n createBlockRefRewriteMiddleware,\n createBlockTrackerInspectorMiddleware,\n createInflightCacheMiddleware,\n createFetchMiddleware,\n createRetryOnEmptyMiddleware,\n} from '@metamask/eth-json-rpc-middleware';\nimport type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';\nimport {\n providerFromEngine,\n providerFromMiddleware,\n} from '@metamask/eth-json-rpc-provider';\nimport {\n createAsyncMiddleware,\n createScaffoldMiddleware,\n JsonRpcEngine,\n mergeMiddleware,\n} from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Hex, Json, JsonRpcParams } from '@metamask/utils';\n\nimport { RpcService } from './rpc-service/rpc-service';\nimport type {\n BlockTracker,\n NetworkClientConfiguration,\n Provider,\n} from './types';\nimport { NetworkClientType } from './types';\n\nconst SECOND = 1000;\n\n/**\n * The pair of provider / block tracker that can be used to interface with the\n * network and respond to new activity.\n */\nexport type NetworkClient = {\n configuration: NetworkClientConfiguration;\n provider: Provider;\n blockTracker: BlockTracker;\n destroy: () => void;\n};\n\n/**\n * Create a JSON RPC network client for a specific network.\n *\n * @param args - The arguments.\n * @param args.configuration - The network configuration.\n * @param args.fetch - A function that can be used to make an HTTP request,\n * compatible with the Fetch API.\n * @param args.btoa - A function that can be used to convert a binary string\n * into base-64.\n * @returns The network client.\n */\nexport function createNetworkClient({\n configuration,\n fetch: givenFetch,\n btoa: givenBtoa,\n}: {\n configuration: NetworkClientConfiguration;\n fetch: typeof fetch;\n btoa: typeof btoa;\n}): NetworkClient {\n const rpcService =\n configuration.type === NetworkClientType.Infura\n ? new RpcService({\n fetch: givenFetch,\n btoa: givenBtoa,\n endpointUrl: `https://${configuration.network}.infura.io/v3/${configuration.infuraProjectId}`,\n })\n : new RpcService({\n fetch: givenFetch,\n btoa: givenBtoa,\n endpointUrl: configuration.rpcUrl,\n });\n\n const rpcApiMiddleware =\n configuration.type === NetworkClientType.Infura\n ? createInfuraMiddleware({\n rpcService,\n options: {\n source: 'metamask',\n },\n })\n : createFetchMiddleware({ rpcService });\n\n const rpcProvider = providerFromMiddleware(rpcApiMiddleware);\n\n const blockTrackerOpts =\n process.env.IN_TEST && configuration.type === NetworkClientType.Custom\n ? { pollingInterval: SECOND }\n : {};\n const blockTracker = new PollingBlockTracker({\n ...blockTrackerOpts,\n provider: rpcProvider,\n });\n\n const networkMiddleware =\n configuration.type === NetworkClientType.Infura\n ? createInfuraNetworkMiddleware({\n blockTracker,\n network: configuration.network,\n rpcProvider,\n rpcApiMiddleware,\n })\n : createCustomNetworkMiddleware({\n blockTracker,\n chainId: configuration.chainId,\n rpcApiMiddleware,\n });\n\n const engine = new JsonRpcEngine();\n\n engine.push(networkMiddleware);\n\n const provider = providerFromEngine(engine);\n\n const destroy = () => {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n blockTracker.destroy();\n };\n\n return { configuration, provider, blockTracker, destroy };\n}\n\n/**\n * Create middleware for infura.\n *\n * @param args - The arguments.\n * @param args.blockTracker - The block tracker to use.\n * @param args.network - The Infura network to use.\n * @param args.rpcProvider - The RPC provider to use.\n * @param args.rpcApiMiddleware - Additional middleware.\n * @returns The collection of middleware that makes up the Infura client.\n */\nfunction createInfuraNetworkMiddleware({\n blockTracker,\n network,\n rpcProvider,\n rpcApiMiddleware,\n}: {\n blockTracker: PollingBlockTracker;\n network: InfuraNetworkType;\n rpcProvider: SafeEventEmitterProvider;\n rpcApiMiddleware: JsonRpcMiddleware<JsonRpcParams, Json>;\n}) {\n return mergeMiddleware([\n createNetworkAndChainIdMiddleware({ network }),\n createBlockCacheMiddleware({ blockTracker }),\n createInflightCacheMiddleware(),\n createBlockRefMiddleware({ blockTracker, provider: rpcProvider }),\n createRetryOnEmptyMiddleware({ blockTracker, provider: rpcProvider }),\n createBlockTrackerInspectorMiddleware({ blockTracker }),\n rpcApiMiddleware,\n ]);\n}\n\n/**\n * Creates static method middleware.\n *\n * @param args - The Arguments.\n * @param args.network - The Infura network to use.\n * @returns The middleware that implements the eth_chainId method.\n */\nfunction createNetworkAndChainIdMiddleware({\n network,\n}: {\n network: InfuraNetworkType;\n}) {\n return createScaffoldMiddleware({\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n eth_chainId: ChainId[network],\n });\n}\n\nconst createChainIdMiddleware = (\n chainId: Hex,\n): JsonRpcMiddleware<JsonRpcParams, Json> => {\n return (req, res, next, end) => {\n if (req.method === 'eth_chainId') {\n res.result = chainId;\n return end();\n }\n return next();\n };\n};\n\n/**\n * Creates custom middleware.\n *\n * @param args - The arguments.\n * @param args.blockTracker - The block tracker to use.\n * @param args.chainId - The chain id to use.\n * @param args.rpcApiMiddleware - Additional middleware.\n * @returns The collection of middleware that makes up the Infura client.\n */\nfunction createCustomNetworkMiddleware({\n blockTracker,\n chainId,\n rpcApiMiddleware,\n}: {\n blockTracker: PollingBlockTracker;\n chainId: Hex;\n rpcApiMiddleware: JsonRpcMiddleware<JsonRpcParams, Json>;\n}): JsonRpcMiddleware<JsonRpcParams, Json> {\n const testMiddlewares = process.env.IN_TEST\n ? [createEstimateGasDelayTestMiddleware()]\n : [];\n\n return mergeMiddleware([\n ...testMiddlewares,\n createChainIdMiddleware(chainId),\n createBlockRefRewriteMiddleware({ blockTracker }),\n createBlockCacheMiddleware({ blockTracker }),\n createInflightCacheMiddleware(),\n createBlockTrackerInspectorMiddleware({ blockTracker }),\n rpcApiMiddleware,\n ]);\n}\n\n/**\n * For use in tests only.\n * Adds a delay to `eth_estimateGas` calls.\n *\n * @returns The middleware for delaying gas estimation calls by 2 seconds when in test.\n */\nfunction createEstimateGasDelayTestMiddleware() {\n return createAsyncMiddleware(async (req, _, next) => {\n if (req.method === 'eth_estimateGas') {\n await new Promise((resolve) => setTimeout(resolve, SECOND * 2));\n }\n return next();\n });\n}\n"]}
|
|
@@ -12,8 +12,17 @@ export type NetworkClient = {
|
|
|
12
12
|
/**
|
|
13
13
|
* Create a JSON RPC network client for a specific network.
|
|
14
14
|
*
|
|
15
|
-
* @param
|
|
15
|
+
* @param args - The arguments.
|
|
16
|
+
* @param args.configuration - The network configuration.
|
|
17
|
+
* @param args.fetch - A function that can be used to make an HTTP request,
|
|
18
|
+
* compatible with the Fetch API.
|
|
19
|
+
* @param args.btoa - A function that can be used to convert a binary string
|
|
20
|
+
* into base-64.
|
|
16
21
|
* @returns The network client.
|
|
17
22
|
*/
|
|
18
|
-
export declare function createNetworkClient(
|
|
23
|
+
export declare function createNetworkClient({ configuration, fetch: givenFetch, btoa: givenBtoa, }: {
|
|
24
|
+
configuration: NetworkClientConfiguration;
|
|
25
|
+
fetch: typeof fetch;
|
|
26
|
+
btoa: typeof btoa;
|
|
27
|
+
}): NetworkClient;
|
|
19
28
|
//# sourceMappingURL=create-network-client.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-network-client.d.cts","sourceRoot":"","sources":["../src/create-network-client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-network-client.d.cts","sourceRoot":"","sources":["../src/create-network-client.ts"],"names":[],"mappings":"AA4BA,OAAO,KAAK,EACV,YAAY,EACZ,0BAA0B,EAC1B,QAAQ,EACT,oBAAgB;AAKjB;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,aAAa,EAAE,0BAA0B,CAAC;IAC1C,QAAQ,EAAE,QAAQ,CAAC;IACnB,YAAY,EAAE,YAAY,CAAC;IAC3B,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAEF;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,aAAa,EACb,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,GAChB,EAAE;IACD,aAAa,EAAE,0BAA0B,CAAC;IAC1C,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,IAAI,EAAE,OAAO,IAAI,CAAC;CACnB,GAAG,aAAa,CA8DhB"}
|
|
@@ -12,8 +12,17 @@ export type NetworkClient = {
|
|
|
12
12
|
/**
|
|
13
13
|
* Create a JSON RPC network client for a specific network.
|
|
14
14
|
*
|
|
15
|
-
* @param
|
|
15
|
+
* @param args - The arguments.
|
|
16
|
+
* @param args.configuration - The network configuration.
|
|
17
|
+
* @param args.fetch - A function that can be used to make an HTTP request,
|
|
18
|
+
* compatible with the Fetch API.
|
|
19
|
+
* @param args.btoa - A function that can be used to convert a binary string
|
|
20
|
+
* into base-64.
|
|
16
21
|
* @returns The network client.
|
|
17
22
|
*/
|
|
18
|
-
export declare function createNetworkClient(
|
|
23
|
+
export declare function createNetworkClient({ configuration, fetch: givenFetch, btoa: givenBtoa, }: {
|
|
24
|
+
configuration: NetworkClientConfiguration;
|
|
25
|
+
fetch: typeof fetch;
|
|
26
|
+
btoa: typeof btoa;
|
|
27
|
+
}): NetworkClient;
|
|
19
28
|
//# sourceMappingURL=create-network-client.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-network-client.d.mts","sourceRoot":"","sources":["../src/create-network-client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-network-client.d.mts","sourceRoot":"","sources":["../src/create-network-client.ts"],"names":[],"mappings":"AA4BA,OAAO,KAAK,EACV,YAAY,EACZ,0BAA0B,EAC1B,QAAQ,EACT,oBAAgB;AAKjB;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,aAAa,EAAE,0BAA0B,CAAC;IAC1C,QAAQ,EAAE,QAAQ,CAAC;IACnB,YAAY,EAAE,YAAY,CAAC;IAC3B,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAEF;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,aAAa,EACb,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,GAChB,EAAE;IACD,aAAa,EAAE,0BAA0B,CAAC;IAC1C,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,IAAI,EAAE,OAAO,IAAI,CAAC;CACnB,GAAG,aAAa,CA8DhB"}
|
|
@@ -4,45 +4,58 @@ import { createInfuraMiddleware } from "@metamask/eth-json-rpc-infura";
|
|
|
4
4
|
import { createBlockCacheMiddleware, createBlockRefMiddleware, createBlockRefRewriteMiddleware, createBlockTrackerInspectorMiddleware, createInflightCacheMiddleware, createFetchMiddleware, createRetryOnEmptyMiddleware } from "@metamask/eth-json-rpc-middleware";
|
|
5
5
|
import { providerFromEngine, providerFromMiddleware } from "@metamask/eth-json-rpc-provider";
|
|
6
6
|
import { createAsyncMiddleware, createScaffoldMiddleware, JsonRpcEngine, mergeMiddleware } from "@metamask/json-rpc-engine";
|
|
7
|
+
import { RpcService } from "./rpc-service/rpc-service.mjs";
|
|
7
8
|
import { NetworkClientType } from "./types.mjs";
|
|
8
9
|
const SECOND = 1000;
|
|
9
10
|
/**
|
|
10
11
|
* Create a JSON RPC network client for a specific network.
|
|
11
12
|
*
|
|
12
|
-
* @param
|
|
13
|
+
* @param args - The arguments.
|
|
14
|
+
* @param args.configuration - The network configuration.
|
|
15
|
+
* @param args.fetch - A function that can be used to make an HTTP request,
|
|
16
|
+
* compatible with the Fetch API.
|
|
17
|
+
* @param args.btoa - A function that can be used to convert a binary string
|
|
18
|
+
* into base-64.
|
|
13
19
|
* @returns The network client.
|
|
14
20
|
*/
|
|
15
|
-
export function createNetworkClient(
|
|
16
|
-
const
|
|
17
|
-
?
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
source: 'metamask',
|
|
21
|
+
export function createNetworkClient({ configuration, fetch: givenFetch, btoa: givenBtoa, }) {
|
|
22
|
+
const rpcService = configuration.type === NetworkClientType.Infura
|
|
23
|
+
? new RpcService({
|
|
24
|
+
fetch: givenFetch,
|
|
25
|
+
btoa: givenBtoa,
|
|
26
|
+
endpointUrl: `https://${configuration.network}.infura.io/v3/${configuration.infuraProjectId}`,
|
|
22
27
|
})
|
|
23
|
-
:
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
: new RpcService({
|
|
29
|
+
fetch: givenFetch,
|
|
30
|
+
btoa: givenBtoa,
|
|
31
|
+
endpointUrl: configuration.rpcUrl,
|
|
27
32
|
});
|
|
33
|
+
const rpcApiMiddleware = configuration.type === NetworkClientType.Infura
|
|
34
|
+
? createInfuraMiddleware({
|
|
35
|
+
rpcService,
|
|
36
|
+
options: {
|
|
37
|
+
source: 'metamask',
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
: createFetchMiddleware({ rpcService });
|
|
28
41
|
const rpcProvider = providerFromMiddleware(rpcApiMiddleware);
|
|
29
|
-
const blockTrackerOpts = process.env.IN_TEST &&
|
|
42
|
+
const blockTrackerOpts = process.env.IN_TEST && configuration.type === NetworkClientType.Custom
|
|
30
43
|
? { pollingInterval: SECOND }
|
|
31
44
|
: {};
|
|
32
45
|
const blockTracker = new PollingBlockTracker({
|
|
33
46
|
...blockTrackerOpts,
|
|
34
47
|
provider: rpcProvider,
|
|
35
48
|
});
|
|
36
|
-
const networkMiddleware =
|
|
49
|
+
const networkMiddleware = configuration.type === NetworkClientType.Infura
|
|
37
50
|
? createInfuraNetworkMiddleware({
|
|
38
51
|
blockTracker,
|
|
39
|
-
network:
|
|
52
|
+
network: configuration.network,
|
|
40
53
|
rpcProvider,
|
|
41
54
|
rpcApiMiddleware,
|
|
42
55
|
})
|
|
43
56
|
: createCustomNetworkMiddleware({
|
|
44
57
|
blockTracker,
|
|
45
|
-
chainId:
|
|
58
|
+
chainId: configuration.chainId,
|
|
46
59
|
rpcApiMiddleware,
|
|
47
60
|
});
|
|
48
61
|
const engine = new JsonRpcEngine();
|
|
@@ -53,7 +66,7 @@ export function createNetworkClient(networkConfig) {
|
|
|
53
66
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
54
67
|
blockTracker.destroy();
|
|
55
68
|
};
|
|
56
|
-
return { configuration
|
|
69
|
+
return { configuration, provider, blockTracker, destroy };
|
|
57
70
|
}
|
|
58
71
|
/**
|
|
59
72
|
* Create middleware for infura.
|