Skip to content

Commit

Permalink
feat: get rid of traverse, use corepack
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Jun 27, 2024
1 parent c42dc04 commit 83ea489
Show file tree
Hide file tree
Showing 5 changed files with 6,674 additions and 5,345 deletions.
8 changes: 4 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"search.exclude": {
"**/.yarn": true,
"**/.pnp.*": true
"prettier.enable": true,
"eslint.enable": true,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"prettier.prettierPath": "node_modules/prettier/index.js",
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"built": false,
"unplugged": true
}
}
},
"packageManager": "[email protected]"
}
4 changes: 1 addition & 3 deletions packages/ensjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@
"dns-packet": "^5.3.1",
"graphql": "^16.3.0",
"graphql-request": "6.1.0",
"pako": "^2.1.0",
"traverse": "^0.6.8"
"pako": "^2.1.0"
},
"devDependencies": {
"@ensdomains/buffer": "^0.0.13",
Expand All @@ -121,7 +120,6 @@
"@types/fs-extra": "^11.0.1",
"@types/node": "^20.3.3",
"@types/pako": "^2.0.0",
"@types/traverse": "^0.6.36",
"@vitest/coverage-v8": "^1.3.1",
"cbor": "^8.1.0",
"dotenv": "^16.0.0",
Expand Down
58 changes: 36 additions & 22 deletions packages/ensjs/src/functions/subgraph/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Kind, SelectionNode, SelectionSetNode } from 'graphql'
import type { RequestMiddleware, ResponseMiddleware } from 'graphql-request'
import { GraphQLClient } from 'graphql-request'
import { parse, print, visit } from 'graphql/language/index.js'
import traverse from 'traverse'
import type { ClientWithEns } from '../../contracts/consts.js'
import { namehash } from '../../utils/normalise.js'

Expand Down Expand Up @@ -57,30 +56,45 @@ export const requestMiddleware: RequestMiddleware = (request) => {
}

export const responseMiddleware: ResponseMiddleware = (response) => {
traverse(response).forEach(function (responseItem: unknown) {
if (
responseItem instanceof Object &&
'name' in responseItem &&
responseItem.name &&
typeof responseItem.name === 'string'
) {
// Name already in hashed form
if (responseItem.name && responseItem.name.includes('[')) {
return
}
const traverse = (obj: Record<string, any>) => {
if (obj && typeof obj === 'object') {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = obj[key]

let hashedName = '[Invalid ENS Name]'
try {
hashedName = namehash(responseItem.name)
} catch (e) {
this.update({ ...responseItem, name: hashedName, invalidName: true })
return
}
if ('id' in responseItem && responseItem.id !== hashedName) {
this.update({ ...responseItem, name: hashedName, invalidName: true })
if (value && typeof value === 'object') {
traverse(value)
}

if (
value instanceof Object &&
'name' in value &&
value.name &&
typeof value.name === 'string'
) {
// Name already in hashed form
if (value.name.includes('[')) {
// eslint-disable-next-line no-continue
continue
}

let hashedName = '[Invalid ENS Name]'
try {
hashedName = namehash(value.name)
} catch (e) {
obj[key] = { ...value, name: hashedName, invalidName: true }
}

if ('id' in value && value.id !== hashedName) {
obj[key] = { ...value, name: hashedName, invalidName: true }
}
}
}
}
}
})
}

traverse(response)
}

export const createSubgraphClient = ({ client }: { client: ClientWithEns }) =>
Expand Down
Loading

0 comments on commit 83ea489

Please sign in to comment.