@manifoldxyz/frontend-provider-utils
Advanced tools
Comparing version 1.0.0-beta.3 to 1.0.0
@@ -1,22 +0,3 @@ | ||
import { EthereumProvider } from '@manifoldxyz/frontend-provider-types'; | ||
/** | ||
* Returns a Promise that resolves to the value of window.ManifoldEthereumProvider if it is | ||
* set within the given timeout, or null. | ||
* The Promise will not reject, but an error will be thrown if invalid options | ||
* are provided. | ||
* | ||
* @param options - Options bag. | ||
* @param options.silent - Whether to silence console errors. Does not affect | ||
* thrown errors. Default: false | ||
* @param options.timeout - Milliseconds to wait for 'ethereum#initialized' to | ||
* be dispatched. Default: 3000 | ||
* @param options.initialized - Whether to check if the provider has already | ||
* been initialized. Default: true | ||
* @returns A Promise that resolves with the Provider if it is detected within | ||
* given timeout, otherwise null. | ||
*/ | ||
export declare function detectManifoldEthereumProvider({ silent, timeout, initialized, }?: { | ||
silent?: boolean | undefined; | ||
timeout?: number | undefined; | ||
initialized?: boolean | undefined; | ||
}): Promise<typeof EthereumProvider | null>; | ||
import { Chains } from './chain'; | ||
import { detectManifoldEthereumProvider } from './detect'; | ||
export { Chains, detectManifoldEthereumProvider }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.detectManifoldEthereumProvider = void 0; | ||
var frontend_provider_types_1 = require("@manifoldxyz/frontend-provider-types"); | ||
/** | ||
* Returns a Promise that resolves to the value of window.ManifoldEthereumProvider if it is | ||
* set within the given timeout, or null. | ||
* The Promise will not reject, but an error will be thrown if invalid options | ||
* are provided. | ||
* | ||
* @param options - Options bag. | ||
* @param options.silent - Whether to silence console errors. Does not affect | ||
* thrown errors. Default: false | ||
* @param options.timeout - Milliseconds to wait for 'ethereum#initialized' to | ||
* be dispatched. Default: 3000 | ||
* @param options.initialized - Whether to check if the provider has already | ||
* been initialized. Default: true | ||
* @returns A Promise that resolves with the Provider if it is detected within | ||
* given timeout, otherwise null. | ||
*/ | ||
function detectManifoldEthereumProvider(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.silent, silent = _c === void 0 ? false : _c, _d = _b.timeout, timeout = _d === void 0 ? 3000 : _d, _e = _b.initialized, initialized = _e === void 0 ? true : _e; | ||
_validateInputs(); | ||
var handled = false; | ||
/** | ||
* Helper function to check if the provider has been initialized. | ||
*/ | ||
function providerInitialized() { | ||
var provider = window.ManifoldEthereumProvider; | ||
// Note: ._isInitialized is a property for older versions of the provider | ||
return ((!initialized && !!provider) || !!(provider === null || provider === void 0 ? void 0 : provider.initialized) || !!(provider === null || provider === void 0 ? void 0 : provider._isInitialized)); | ||
} | ||
return new Promise(function (resolve) { | ||
if (providerInitialized()) { | ||
handleManifoldEthereum(); | ||
} | ||
else { | ||
// Listen for provider via event | ||
window.addEventListener(frontend_provider_types_1.MANIFOLD_ETHEREUM_INITIALIZED, handleManifoldEthereum, { | ||
once: true, | ||
}); | ||
if (!initialized) { | ||
window.addEventListener(frontend_provider_types_1.MANIFOLD_ETHEREUM_EXISTS, handleManifoldEthereum, { | ||
once: true, | ||
}); | ||
} | ||
// Poll for provider (legacy clients that do not emit event) | ||
var intervalId_1 = setInterval(function () { | ||
if (providerInitialized()) { | ||
clearInterval(intervalId_1); | ||
handleManifoldEthereum(); | ||
} | ||
}, 100); | ||
// Timeout | ||
setTimeout(function () { | ||
clearInterval(intervalId_1); | ||
handleManifoldEthereum(); | ||
}, timeout); | ||
} | ||
function handleManifoldEthereum() { | ||
// Already andled | ||
if (handled) { | ||
return; | ||
} | ||
handled = true; | ||
window.removeEventListener(frontend_provider_types_1.MANIFOLD_ETHEREUM_INITIALIZED, handleManifoldEthereum); | ||
if (!initialized) { | ||
window.removeEventListener(frontend_provider_types_1.MANIFOLD_ETHEREUM_EXISTS, handleManifoldEthereum); | ||
} | ||
if (providerInitialized()) { | ||
resolve(window.ManifoldEthereumProvider); | ||
} | ||
else { | ||
!silent && | ||
console.error('@manifoldxyz/frontend-provider-utils:', 'Unable to detect window.ManifoldEthereumProvider.'); | ||
resolve(null); | ||
} | ||
} | ||
}); | ||
function _validateInputs() { | ||
if (typeof silent !== 'boolean') { | ||
throw new Error("@manifoldxyz/frontend-provider-utils: Expected option 'silent' to be a boolean."); | ||
} | ||
if (typeof timeout !== 'number') { | ||
throw new Error("@manifoldxyz/frontend-provider-utils: Expected option 'timeout' to be a number."); | ||
} | ||
} | ||
} | ||
exports.detectManifoldEthereumProvider = detectManifoldEthereumProvider; | ||
exports.detectManifoldEthereumProvider = exports.Chains = void 0; | ||
var chain_1 = require("./chain"); | ||
Object.defineProperty(exports, "Chains", { enumerable: true, get: function () { return chain_1.Chains; } }); | ||
var detect_1 = require("./detect"); | ||
Object.defineProperty(exports, "detectManifoldEthereumProvider", { enumerable: true, get: function () { return detect_1.detectManifoldEthereumProvider; } }); |
{ | ||
"name": "@manifoldxyz/frontend-provider-utils", | ||
"version": "1.0.0-beta.3", | ||
"version": "1.0.0", | ||
"description": "A tiny utility for detecting the Manifold Ethereum provider, an injected `window.ManifoldEthereumProvider` object", | ||
@@ -37,3 +37,3 @@ "typings": "./dist/index.d.ts", | ||
"peerDependencies": { | ||
"@manifoldxyz/frontend-provider-types": "^1.0.0-beta.2" | ||
"@manifoldxyz/frontend-provider-types": "^1.0.0" | ||
}, | ||
@@ -40,0 +40,0 @@ "jest": { |
114
src/index.ts
@@ -1,112 +0,4 @@ | ||
import { | ||
EthereumProvider, | ||
MANIFOLD_ETHEREUM_EXISTS, | ||
MANIFOLD_ETHEREUM_INITIALIZED, | ||
} from '@manifoldxyz/frontend-provider-types'; | ||
import { Chains } from './chain'; | ||
import { detectManifoldEthereumProvider } from './detect'; | ||
interface Window { | ||
ManifoldEthereumProvider?: typeof EthereumProvider; | ||
} | ||
/** | ||
* Returns a Promise that resolves to the value of window.ManifoldEthereumProvider if it is | ||
* set within the given timeout, or null. | ||
* The Promise will not reject, but an error will be thrown if invalid options | ||
* are provided. | ||
* | ||
* @param options - Options bag. | ||
* @param options.silent - Whether to silence console errors. Does not affect | ||
* thrown errors. Default: false | ||
* @param options.timeout - Milliseconds to wait for 'ethereum#initialized' to | ||
* be dispatched. Default: 3000 | ||
* @param options.initialized - Whether to check if the provider has already | ||
* been initialized. Default: true | ||
* @returns A Promise that resolves with the Provider if it is detected within | ||
* given timeout, otherwise null. | ||
*/ | ||
export function detectManifoldEthereumProvider({ | ||
silent = false, | ||
timeout = 3000, | ||
initialized = true, | ||
} = {}): Promise<typeof EthereumProvider | null> { | ||
_validateInputs(); | ||
let handled = false; | ||
/** | ||
* Helper function to check if the provider has been initialized. | ||
*/ | ||
function providerInitialized(): boolean { | ||
const provider = (window as Window).ManifoldEthereumProvider; | ||
// Note: ._isInitialized is a property for older versions of the provider | ||
return ( | ||
(!initialized && !!provider) || !!provider?.initialized || !!(provider as any)?._isInitialized | ||
); | ||
} | ||
return new Promise((resolve) => { | ||
if (providerInitialized()) { | ||
handleManifoldEthereum(); | ||
} else { | ||
// Listen for provider via event | ||
window.addEventListener(MANIFOLD_ETHEREUM_INITIALIZED, handleManifoldEthereum, { | ||
once: true, | ||
}); | ||
if (!initialized) { | ||
window.addEventListener(MANIFOLD_ETHEREUM_EXISTS, handleManifoldEthereum, { | ||
once: true, | ||
}); | ||
} | ||
// Poll for provider (legacy clients that do not emit event) | ||
const intervalId = setInterval(() => { | ||
if (providerInitialized()) { | ||
clearInterval(intervalId); | ||
handleManifoldEthereum(); | ||
} | ||
}, 100); | ||
// Timeout | ||
setTimeout(() => { | ||
clearInterval(intervalId); | ||
handleManifoldEthereum(); | ||
}, timeout); | ||
} | ||
function handleManifoldEthereum() { | ||
// Already andled | ||
if (handled) { | ||
return; | ||
} | ||
handled = true; | ||
window.removeEventListener(MANIFOLD_ETHEREUM_INITIALIZED, handleManifoldEthereum); | ||
if (!initialized) { | ||
window.removeEventListener(MANIFOLD_ETHEREUM_EXISTS, handleManifoldEthereum); | ||
} | ||
if (providerInitialized()) { | ||
resolve((window as Window).ManifoldEthereumProvider!); | ||
} else { | ||
!silent && | ||
console.error( | ||
'@manifoldxyz/frontend-provider-utils:', | ||
'Unable to detect window.ManifoldEthereumProvider.' | ||
); | ||
resolve(null); | ||
} | ||
} | ||
}); | ||
function _validateInputs() { | ||
if (typeof silent !== 'boolean') { | ||
throw new Error( | ||
`@manifoldxyz/frontend-provider-utils: Expected option 'silent' to be a boolean.` | ||
); | ||
} | ||
if (typeof timeout !== 'number') { | ||
throw new Error( | ||
`@manifoldxyz/frontend-provider-utils: Expected option 'timeout' to be a number.` | ||
); | ||
} | ||
} | ||
} | ||
export { Chains, detectManifoldEthereumProvider }; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
27254
20
542
0
1
1