UNPKG

@metamask/network-controller

Version:

Provides an interface to the currently selected network via a MetaMask-compatible provider object

1 lines 12.3 kB
{"version":3,"file":"create-auto-managed-network-client.mjs","sourceRoot":"","sources":["../src/create-auto-managed-network-client.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,mBAAmB,EAAE,oCAAgC;AAS9D;;;;GAIG;AACH,MAAM,wBAAwB,GAAG,YAAY,CAAC;AA+B9C;;;;GAIG;AACH,gFAAgF;AAChF,gEAAgE;AAChE,MAAM,oBAAoB,GAAG,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,8BAA8B,CAE5C,EACA,0BAA0B,EAC1B,oBAAoB,EACpB,sBAAsB,GAAG,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC,EACnC,SAAS,EACT,oBAAoB,EAAE,yBAAyB,GAWhD;IACC,IAAI,oBAAoB,GAAG,yBAAyB,CAAC;IACrD,IAAI,aAAwC,CAAC;IAE7C,MAAM,0BAA0B,GAAG,GAAkB,EAAE;QACrD,aAAa,KAAb,aAAa,GAAK,mBAAmB,CAAC;YACpC,aAAa,EAAE,0BAA0B;YACzC,oBAAoB;YACpB,sBAAsB;YACtB,SAAS;YACT,oBAAoB;SACrB,CAAC,EAAC;QAEH,IAAI,aAAa,KAAK,SAAS,EAAE;YAC/B,MAAM,IAAI,KAAK,CACb,wFAAwF,CACzF,CAAC;SACH;QAED,OAAO,aAAa,CAAC;IACvB,CAAC,CAAC;IAEF,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,MAAM,EAAE,QAAQ,EAAE,GAAG,0BAA0B,EAAE,CAAC;YAElD,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-block-tracker';\n\nimport type { NetworkClient } from './create-network-client';\nimport { createNetworkClient } from './create-network-client';\nimport type { NetworkControllerMessenger } from './NetworkController';\nimport type { RpcServiceOptions } from './rpc-service/rpc-service';\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 enableRpcFailover: () => void;\n disableRpcFailover: () => 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.getRpcServiceOptions - Factory for constructing RPC service\n * options. See {@link NetworkControllerOptions.getRpcServiceOptions}.\n * @param args.getBlockTrackerOptions - Factory for constructing block tracker\n * options. See {@link NetworkControllerOptions.getBlockTrackerOptions}.\n * @param args.messenger - The network controller messenger.\n * @param args.isRpcFailoverEnabled - Whether or not requests sent to the\n * primary RPC endpoint for this network should be automatically diverted to\n * provided failover endpoints if the primary is unavailable.\n * @returns The auto-managed network client.\n */\nexport function createAutoManagedNetworkClient<\n Configuration extends NetworkClientConfiguration,\n>({\n networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions = () => ({}),\n messenger,\n isRpcFailoverEnabled: givenIsRpcFailoverEnabled,\n}: {\n networkClientConfiguration: Configuration;\n getRpcServiceOptions: (\n rpcEndpointUrl: string,\n ) => Omit<RpcServiceOptions, 'failoverService' | 'endpointUrl'>;\n getBlockTrackerOptions?: (\n rpcEndpointUrl: string,\n ) => Omit<PollingBlockTrackerOptions, 'provider'>;\n messenger: NetworkControllerMessenger;\n isRpcFailoverEnabled: boolean;\n}): AutoManagedNetworkClient<Configuration> {\n let isRpcFailoverEnabled = givenIsRpcFailoverEnabled;\n let networkClient: NetworkClient | undefined;\n\n const ensureNetworkClientCreated = (): NetworkClient => {\n networkClient ??= createNetworkClient({\n configuration: networkClientConfiguration,\n getRpcServiceOptions,\n getBlockTrackerOptions,\n messenger,\n isRpcFailoverEnabled,\n });\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\n return networkClient;\n };\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 const { provider } = ensureNetworkClientCreated();\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 const { provider } = ensureNetworkClientCreated();\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 const { blockTracker } = ensureNetworkClientCreated();\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 const { blockTracker } = ensureNetworkClientCreated();\n return propertyName in blockTracker;\n },\n },\n );\n\n const destroy = () => {\n networkClient?.destroy();\n };\n\n const enableRpcFailover = () => {\n isRpcFailoverEnabled = true;\n destroy();\n networkClient = undefined;\n };\n\n const disableRpcFailover = () => {\n isRpcFailoverEnabled = false;\n destroy();\n networkClient = undefined;\n };\n\n return {\n configuration: networkClientConfiguration,\n provider: providerProxy,\n blockTracker: blockTrackerProxy,\n destroy,\n enableRpcFailover,\n disableRpcFailover,\n };\n}\n"]}