Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add happy-dom example for node env #53

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand All @@ -29,21 +29,24 @@ 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(
...
);
...
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
}

Expand All @@ -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
}
```
Expand Down
45 changes: 45 additions & 0 deletions example/nodeWithHappyDOM.js
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions example/node.js → example/nodeWithJsDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -31,7 +31,7 @@ avt
gateways: {
ipfs: IPFS,
},
jsdomWindow: jsdom,
jsdomWindow: jsDOM,
});
console.log('avatar: ', avatar);
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 20 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
Loading