diff --git a/README.md b/README.md index 387c452..934a5de 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Avatar resolver library for both nodejs and browser. ### Prerequisites - Have your web3 provider ready (web3.js, ethers.js) -- [Only for node env] Have jsdom installed. +- [Only for node env] Have jsdom / happydom installed. And good to go! @@ -29,7 +29,10 @@ import { StaticJsonRpcProvider } from '@ethersproject/providers'; import { AvatarResolver, utils as avtUtils } from '@ensdomains/ens-avatar'; // const { JSDOM } = require('jsdom'); on nodejs -// const jsdom = new JSDOM().window; on nodejs +// const jsDOM = new JSDOM().window; on nodejs +// or +// const { Window } = require('happy-dom'); on nodejs +// const happyDOM = new Window(); on nodejs const provider = new StaticJsonRpcProvider( ... @@ -37,13 +40,13 @@ const provider = new StaticJsonRpcProvider( ... async function getAvatar() { const resolver = new AvatarResolver(provider); - const avatarURI = await resolver.getAvatar('tanrikulu.eth', { /* jsdomWindow: jsdom (on nodejs) */ }); + const avatarURI = await resolver.getAvatar('tanrikulu.eth', { /* jsdomWindow: jsdom / happydom (on nodejs) */ }); // avatarURI = https://ipfs.io/ipfs/QmUShgfoZQSHK3TQyuTfUpsc8UfeNfD8KwPUvDBUdZ4nmR } async function getHeader() { const resolver = new AvatarResolver(provider); - const headerURI = await resolver.getHeader('tanrikulu.eth', { /* jsdomWindow: jsdom (on nodejs) */ }); + const headerURI = await resolver.getHeader('tanrikulu.eth', { /* jsdomWindow: jsdom / happydom (on nodejs) */ }); // headerURI = https://ipfs.io/ipfs/QmRFnn6c9rj6NuHenFVyKXb6tuKxynAvGiw7yszQJ2EsjN } @@ -53,7 +56,7 @@ async function getAvatarMetadata() { // avatarMetadata = { image: ... , uri: ... , name: ... , description: ... } const headerMetadata = await resolver.getMetadata('tanrikulu.eth', 'header'); // headerMetadata = { image: ... , uri: ... , name: ... , description: ... } - const avatarURI = avtUtils.getImageURI({ metadata: avatarMetadata /*, jsdomWindow: jsdom (on nodejs) */ }); + const avatarURI = avtUtils.getImageURI({ metadata: avatarMetadata /*, jsdomWindow: jsdom / happydom (on nodejs) */ }); // avatarURI = https://ipfs.io/ipfs/QmUShgfoZQSHK3TQyuTfUpsc8UfeNfD8KwPUvDBUdZ4nmR } ``` diff --git a/example/nodeWithHappyDOM.js b/example/nodeWithHappyDOM.js new file mode 100644 index 0000000..53991cd --- /dev/null +++ b/example/nodeWithHappyDOM.js @@ -0,0 +1,45 @@ +require('dotenv').config(); +const { StaticJsonRpcProvider } = require('@ethersproject/providers'); +const { AvatarResolver, utils: avtUtils } = require('../dist/index'); +const { Window } = require('happy-dom'); + +const happyDOM = new Window(); +const ensName = process.argv[2]; +if (!ensName) { + console.log( + 'Please provide an ENS name as an argument (> node demo.js nick.eth)' + ); + process.exit(1); +} +const IPFS = 'https://cf-ipfs.com'; +const provider = new StaticJsonRpcProvider( + `https://mainnet.infura.io/v3/${process.env.INFURA_KEY}` +); +const avt = new AvatarResolver(provider, { + ipfs: IPFS, + apiKey: { opensea: process.env.OPENSEA_KEY }, +}); +avt + .getMetadata(ensName) + .then(metadata => { + if (!metadata) { + console.log('Avatar not found!'); + return; + } + const avatar = avtUtils.getImageURI({ + metadata, + gateways: { + ipfs: IPFS, + }, + jsdomWindow: happyDOM, + }); + console.log('avatar: ', avatar); + }) + .catch(console.log); + +avt + .getHeader(ensName) + .then(header => { + console.log('header: ', header); + }) + .catch(console.log); diff --git a/example/node.js b/example/nodeWithJsDOM.js similarity index 94% rename from example/node.js rename to example/nodeWithJsDOM.js index e43067f..ee16645 100644 --- a/example/node.js +++ b/example/nodeWithJsDOM.js @@ -3,7 +3,7 @@ const { StaticJsonRpcProvider } = require('@ethersproject/providers'); const { AvatarResolver, utils: avtUtils } = require('../dist/index'); const { JSDOM } = require('jsdom'); -const jsdom = new JSDOM().window; +const jsDOM = new JSDOM().window; const ensName = process.argv[2]; if (!ensName) { console.log( @@ -31,7 +31,7 @@ avt gateways: { ipfs: IPFS, }, - jsdomWindow: jsdom, + jsdomWindow: jsDOM, }); console.log('avatar: ', avatar); }) diff --git a/package.json b/package.json index a5deac1..34f3167 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "buffer": "^6.0.3", "dompurify": "^3.0.6", "ethers": "6.12.0", + "happy-dom": "^15.7.4", "is-svg": "^4.3.2", "multiformats": "^9.6.2", "url-join": "^4.0.1" diff --git a/yarn.lock b/yarn.lock index 97b1c05..93c81a5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3013,7 +3013,7 @@ enquirer@^2.3.4: dependencies: ansi-colors "^4.1.1" -entities@^4.4.0: +entities@^4.4.0, entities@^4.5.0: version "4.5.0" resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48" integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw== @@ -3973,6 +3973,15 @@ growly@^1.3.0: resolved "https://registry.npmjs.org/growly/-/growly-1.3.0.tgz" integrity sha1-8QdIy+dq+WS3yWyTxrzCivEgwIE= +happy-dom@^15.7.4: + version "15.7.4" + resolved "https://registry.yarnpkg.com/happy-dom/-/happy-dom-15.7.4.tgz#05aade59c1d307336001b7004c76dfc6a829f220" + integrity sha512-r1vadDYGMtsHAAsqhDuk4IpPvr6N8MGKy5ntBo7tSdim+pWDxus2PNqOcOt8LuDZ4t3KJHE+gCuzupcx/GKnyQ== + dependencies: + entities "^4.5.0" + webidl-conversions "^7.0.0" + whatwg-mimetype "^3.0.0" + har-schema@^2.0.0: version "2.0.0" resolved "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz" @@ -7344,6 +7353,11 @@ webidl-conversions@^4.0.2: resolved "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-4.0.2.tgz" integrity sha512-YQ+BmxuTgd6UXZW3+ICGfyqRyHXVlD5GtQr5+qjiNW7bF0cqrzX500HVXPBOvgXb5YnzDd+h0zqyv61KUD7+Sg== +webidl-conversions@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz#256b4e1882be7debbf01d05f0aa2039778ea080a" + integrity sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g== + whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.5: version "1.0.5" resolved "https://registry.npmjs.org/whatwg-encoding/-/whatwg-encoding-1.0.5.tgz" @@ -7356,6 +7370,11 @@ whatwg-mimetype@^2.2.0, whatwg-mimetype@^2.3.0: resolved "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz" integrity sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g== +whatwg-mimetype@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz#5fa1a7623867ff1af6ca3dc72ad6b8a4208beba7" + integrity sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q== + whatwg-url@^7.0.0: version "7.1.0" resolved "https://registry.npmjs.org/whatwg-url/-/whatwg-url-7.1.0.tgz"