From f846d3fef9be9109ca9c203da47d240bcbc09d90 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Fri, 10 Feb 2023 12:58:16 -0800 Subject: [PATCH 1/9] fix: issues with using in ESM webui --- package-lock.json | 12 ++++++------ src/bundles/explore.js | 16 +++++++++++----- src/components/object-info/ObjectInfo.js | 20 +++++++++++++++++--- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index ddce09ea..42f50f13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13566,9 +13566,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001327", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001327.tgz", - "integrity": "sha512-1/Cg4jlD9qjZzhbzkzEaAC2JHsP0WrOc8Rd/3a3LuajGzGWR/hD7TVyvq99VqmTy99eVh8Zkmdq213OgvgXx7w==", + "version": "1.0.30001451", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001451.tgz", + "integrity": "sha512-XY7UbUpGRatZzoRft//5xOa69/1iGJRBlrieH6QYrkKLIFn3m7OVEJ81dSrKoy2BnKsdbX5cLrOispZNYo9v2w==", "funding": [ { "type": "opencollective", @@ -53910,9 +53910,9 @@ } }, "caniuse-lite": { - "version": "1.0.30001327", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001327.tgz", - "integrity": "sha512-1/Cg4jlD9qjZzhbzkzEaAC2JHsP0WrOc8Rd/3a3LuajGzGWR/hD7TVyvq99VqmTy99eVh8Zkmdq213OgvgXx7w==" + "version": "1.0.30001451", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001451.tgz", + "integrity": "sha512-XY7UbUpGRatZzoRft//5xOa69/1iGJRBlrieH6QYrkKLIFn3m7OVEJ81dSrKoy2BnKsdbX5cLrOispZNYo9v2w==" }, "capture-exit": { "version": "2.0.0", diff --git a/src/bundles/explore.js b/src/bundles/explore.js index 512577a1..80dd625e 100644 --- a/src/bundles/explore.js +++ b/src/bundles/explore.js @@ -185,16 +185,22 @@ async function getIpld () { import(/* webpackChunkName: "ipld" */ '@ipld/dag-cbor'), import(/* webpackChunkName: "ipld" */ '@ipld/dag-pb'), import(/* webpackChunkName: "ipld" */ 'ipld-git'), - import(/* webpackChunkName: "ipld" */ 'ipld-raw'), - import(/* webpackChunkName: "ipld" */ 'ipld-ethereum') + import(/* webpackChunkName: "ipld" */ 'ipld-raw') ]) // CommonJs exports object is .default when imported ESM style - const [ipld, ...formats] = ipldDeps.map(mod => mod.default) + const [ipld, ...formats] = ipldDeps.map(mod => { + const actualModule = mod.default ?? mod + if (actualModule.name != null && actualModule.code != null && actualModule.codec == null) { + // fix throw new Error(`Resolver already exists for codec "${codecName}"`) from ipld when `codecName` is undefined + actualModule.codec = actualModule.code + } + return actualModule + }) // ipldEthereum is an Object, each key points to a ipld format impl - const ipldEthereum = formats.pop() - formats.push(...Object.values(ipldEthereum)) + const ipldEthereum = await import(/* webpackChunkName: "ipld" */ 'ipld-ethereum') + formats.push(...Object.values(ipldEthereum.default ?? ipldEthereum)) // ipldJson uses the new format, use the conversion tool const ipldJson = await import(/* webpackChunkName: "ipld" */ '@ipld/dag-json') diff --git a/src/components/object-info/ObjectInfo.js b/src/components/object-info/ObjectInfo.js index ab7e910c..74531cc0 100644 --- a/src/components/object-info/ObjectInfo.js +++ b/src/components/object-info/ObjectInfo.js @@ -3,6 +3,7 @@ import { withTranslation } from 'react-i18next' import { ObjectInspector, chromeLight } from '@tableflip/react-inspector' import filesize from 'filesize' import LinksTable from './LinksTable' +import multicodec from 'multicodec' const humansize = filesize.partial({ round: 0 }) const objectInspectorTheme = { @@ -28,18 +29,31 @@ const nodeStyles = { 'eth-state-trie': { shortName: 'ETH', name: 'Ethereum State Trie', color: '#383838' } } +/** + * Support getting the style object for a node type using the codec number by redirecting the number to the name + */ +const nodeStylesProxy = new Proxy(nodeStyles, { + get (target, prop) { + if (isNaN(prop)) { + return target[prop] + } + console.log(`getting codec name from code number for ${prop}: `, multicodec.getNameFromCode(prop)) + return target[multicodec.getNameFromCode(prop)] + } +}) + export function shortNameForNode (type) { - const style = nodeStyles[type] + const style = nodeStylesProxy[type] return (style && style.shortName) || 'DAG' } export function nameForNode (type) { - const style = nodeStyles[type] + const style = nodeStylesProxy[type] return (style && style.name) || 'DAG Node' } export function colorForNode (type) { - const style = nodeStyles[type] + const style = nodeStylesProxy[type] return (style && style.color) || '#ea5037' } From e9f140fa5820c7c87f485fb7bc9c62f83e3f076d Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Wed, 15 Feb 2023 14:08:29 -0800 Subject: [PATCH 2/9] fix: webui PR comments --- src/bundles/explore.js | 25 ++++++++++++++++++------ src/components/object-info/ObjectInfo.js | 12 ++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/bundles/explore.js b/src/bundles/explore.js index 80dd625e..96d858ba 100644 --- a/src/bundles/explore.js +++ b/src/bundles/explore.js @@ -28,7 +28,11 @@ const makeBundle = () => { IpldResolver = ipld ipldFormats = formats } + console.log('IpldResolver: ', IpldResolver) + console.log('ipldFormats: ', ipldFormats) + console.log('getIpfs: ', getIpfs) const ipld = makeIpld(IpldResolver, ipldFormats, getIpfs) + console.log('ipld: ', ipld) // TODO: handle ipns, which would give us a fqdn in the cid position. const cid = new Cid(cidOrFqdn) const { @@ -187,13 +191,21 @@ async function getIpld () { import(/* webpackChunkName: "ipld" */ 'ipld-git'), import(/* webpackChunkName: "ipld" */ 'ipld-raw') ]) - - // CommonJs exports object is .default when imported ESM style - const [ipld, ...formats] = ipldDeps.map(mod => { + const [ipld, ...formatImports] = ipldDeps.map(mod => { + // CommonJs exports object is .default when imported ESM style const actualModule = mod.default ?? mod - if (actualModule.name != null && actualModule.code != null && actualModule.codec == null) { - // fix throw new Error(`Resolver already exists for codec "${codecName}"`) from ipld when `codecName` is undefined - actualModule.codec = actualModule.code + return actualModule + }) + const formats = formatImports.map((actualModule) => { + // if (actualModule.name != null && actualModule.code != null && actualModule.codec == null) { + // // fix throw new Error(`Resolver already exists for codec "${codecName}"`) from ipld when `codecName` is undefined + // actualModule.codec = actualModule.code + // } + if (actualModule.util == null) { + // actualModule has no util. using blockcodec-to-ipld-format + const convertedModule = convert(actualModule) + console.log('convertedModule: ', convertedModule) + return convertedModule } return actualModule }) @@ -205,6 +217,7 @@ async function getIpld () { // ipldJson uses the new format, use the conversion tool const ipldJson = await import(/* webpackChunkName: "ipld" */ '@ipld/dag-json') formats.push(convert(ipldJson)) + console.log('formats: ', formats) return { ipld, formats } } diff --git a/src/components/object-info/ObjectInfo.js b/src/components/object-info/ObjectInfo.js index 74531cc0..d55e76ff 100644 --- a/src/components/object-info/ObjectInfo.js +++ b/src/components/object-info/ObjectInfo.js @@ -34,6 +34,7 @@ const nodeStyles = { */ const nodeStylesProxy = new Proxy(nodeStyles, { get (target, prop) { + console.log('nodeStylesProxy for prop: ', prop) if (isNaN(prop)) { return target[prop] } @@ -43,16 +44,19 @@ const nodeStylesProxy = new Proxy(nodeStyles, { }) export function shortNameForNode (type) { + console.log('shortNameForNode type: ', type) const style = nodeStylesProxy[type] return (style && style.shortName) || 'DAG' } export function nameForNode (type) { + console.log('nameForNode type: ', type) const style = nodeStylesProxy[type] return (style && style.name) || 'DAG Node' } export function colorForNode (type) { + console.log('colorForNode type: ', type) const style = nodeStylesProxy[type] return (style && style.color) || '#ea5037' } @@ -77,8 +81,12 @@ const DagNodeIcon = ({ type, ...props }) => ( ) const ObjectInfo = ({ t, tReady, className, type, cid, localPath, size, data, links, format, onLinkClick, gatewayUrl, publicGatewayUrl, ...props }) => { + console.log('type, cid, localPath, size, data, links, format,: ', type, cid, localPath, size, data, links, format) + // console.log('type: ', type) + const unixFsFileOrDirectory = format === 'unixfs' && data.type && ['directory', 'file'].some(x => x === data.type) + console.log('unixFsFileOrDirectory: ', unixFsFileOrDirectory) return ( -
+

@@ -87,7 +95,7 @@ const ObjectInfo = ({ t, tReady, className, type, cid, localPath, size, data, li {format === 'unixfs' ? ( UnixFS ) : null} - {format === 'unixfs' && data.type && ['directory', 'file'].some(x => x === data.type) ? ( + {unixFsFileOrDirectory ? ( {gatewayUrl && gatewayUrl !== publicGatewayUrl && ( From fc014e32122efbcc72f97d1bdbf4eb579590038d Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Wed, 15 Feb 2023 14:15:08 -0800 Subject: [PATCH 3/9] fix: ObjectInfo uses correct node type --- src/components/object-info/ObjectInfo.js | 35 ++++++------------------ 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/src/components/object-info/ObjectInfo.js b/src/components/object-info/ObjectInfo.js index d55e76ff..afeb69e5 100644 --- a/src/components/object-info/ObjectInfo.js +++ b/src/components/object-info/ObjectInfo.js @@ -3,7 +3,7 @@ import { withTranslation } from 'react-i18next' import { ObjectInspector, chromeLight } from '@tableflip/react-inspector' import filesize from 'filesize' import LinksTable from './LinksTable' -import multicodec from 'multicodec' +import { decodeCid } from '../cid-info/decode-cid' const humansize = filesize.partial({ round: 0 }) const objectInspectorTheme = { @@ -29,35 +29,18 @@ const nodeStyles = { 'eth-state-trie': { shortName: 'ETH', name: 'Ethereum State Trie', color: '#383838' } } -/** - * Support getting the style object for a node type using the codec number by redirecting the number to the name - */ -const nodeStylesProxy = new Proxy(nodeStyles, { - get (target, prop) { - console.log('nodeStylesProxy for prop: ', prop) - if (isNaN(prop)) { - return target[prop] - } - console.log(`getting codec name from code number for ${prop}: `, multicodec.getNameFromCode(prop)) - return target[multicodec.getNameFromCode(prop)] - } -}) - export function shortNameForNode (type) { - console.log('shortNameForNode type: ', type) - const style = nodeStylesProxy[type] + const style = nodeStyles[type] return (style && style.shortName) || 'DAG' } export function nameForNode (type) { - console.log('nameForNode type: ', type) - const style = nodeStylesProxy[type] + const style = nodeStyles[type] return (style && style.name) || 'DAG Node' } export function colorForNode (type) { - console.log('colorForNode type: ', type) - const style = nodeStylesProxy[type] + const style = nodeStyles[type] return (style && style.color) || '#ea5037' } @@ -81,12 +64,10 @@ const DagNodeIcon = ({ type, ...props }) => ( ) const ObjectInfo = ({ t, tReady, className, type, cid, localPath, size, data, links, format, onLinkClick, gatewayUrl, publicGatewayUrl, ...props }) => { - console.log('type, cid, localPath, size, data, links, format,: ', type, cid, localPath, size, data, links, format) - // console.log('type: ', type) - const unixFsFileOrDirectory = format === 'unixfs' && data.type && ['directory', 'file'].some(x => x === data.type) - console.log('unixFsFileOrDirectory: ', unixFsFileOrDirectory) + const cidInfo = decodeCid(cid) + type = cidInfo.cid.codec ?? type return ( -
+

@@ -95,7 +76,7 @@ const ObjectInfo = ({ t, tReady, className, type, cid, localPath, size, data, li {format === 'unixfs' ? ( UnixFS ) : null} - {unixFsFileOrDirectory ? ( + {format === 'unixfs' && data.type && ['directory', 'file'].some(x => x === data.type) ? ( {gatewayUrl && gatewayUrl !== publicGatewayUrl && ( From 2f31bc8cff1ef4046af701bc00a72a8c5e6ec487 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Wed, 15 Feb 2023 14:18:29 -0800 Subject: [PATCH 4/9] chore: remove debugging code --- src/bundles/explore.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/bundles/explore.js b/src/bundles/explore.js index 96d858ba..9ed8dc5f 100644 --- a/src/bundles/explore.js +++ b/src/bundles/explore.js @@ -28,11 +28,7 @@ const makeBundle = () => { IpldResolver = ipld ipldFormats = formats } - console.log('IpldResolver: ', IpldResolver) - console.log('ipldFormats: ', ipldFormats) - console.log('getIpfs: ', getIpfs) const ipld = makeIpld(IpldResolver, ipldFormats, getIpfs) - console.log('ipld: ', ipld) // TODO: handle ipns, which would give us a fqdn in the cid position. const cid = new Cid(cidOrFqdn) const { @@ -197,14 +193,9 @@ async function getIpld () { return actualModule }) const formats = formatImports.map((actualModule) => { - // if (actualModule.name != null && actualModule.code != null && actualModule.codec == null) { - // // fix throw new Error(`Resolver already exists for codec "${codecName}"`) from ipld when `codecName` is undefined - // actualModule.codec = actualModule.code - // } if (actualModule.util == null) { // actualModule has no util. using blockcodec-to-ipld-format const convertedModule = convert(actualModule) - console.log('convertedModule: ', convertedModule) return convertedModule } return actualModule @@ -217,7 +208,6 @@ async function getIpld () { // ipldJson uses the new format, use the conversion tool const ipldJson = await import(/* webpackChunkName: "ipld" */ '@ipld/dag-json') formats.push(convert(ipldJson)) - console.log('formats: ', formats) return { ipld, formats } } From 8853611e69d50d0002882552cc94e6f5913d019b Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Thu, 16 Feb 2023 13:11:58 -0800 Subject: [PATCH 5/9] chore: remove debugging artifact --- src/bundles/explore.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/bundles/explore.js b/src/bundles/explore.js index 9ed8dc5f..77610b12 100644 --- a/src/bundles/explore.js +++ b/src/bundles/explore.js @@ -195,8 +195,7 @@ async function getIpld () { const formats = formatImports.map((actualModule) => { if (actualModule.util == null) { // actualModule has no util. using blockcodec-to-ipld-format - const convertedModule = convert(actualModule) - return convertedModule + return convert(actualModule) } return actualModule }) From 836b32292654d88184a961f0d812767510965a02 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Mon, 20 Feb 2023 12:45:37 -0800 Subject: [PATCH 6/9] debugging --- src/bundles/explore.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/bundles/explore.js b/src/bundles/explore.js index 77610b12..4bf34030 100644 --- a/src/bundles/explore.js +++ b/src/bundles/explore.js @@ -195,6 +195,7 @@ async function getIpld () { const formats = formatImports.map((actualModule) => { if (actualModule.util == null) { // actualModule has no util. using blockcodec-to-ipld-format + console.log('actualModule: ', actualModule) return convert(actualModule) } return actualModule @@ -207,6 +208,7 @@ async function getIpld () { // ipldJson uses the new format, use the conversion tool const ipldJson = await import(/* webpackChunkName: "ipld" */ '@ipld/dag-json') formats.push(convert(ipldJson)) + console.log('formats: ', formats) return { ipld, formats } } From 4cec96f3dbb926d87ef2b03e03356a3a3a619b80 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Wed, 22 Feb 2023 12:15:38 -0800 Subject: [PATCH 7/9] fix: further issues with explore page in webui see https://github.com/ipfs/ipfs-webui/pull/2098/commits/2cde6255a96adf69c22134437721e5acc4238bb2 --- src/bundles/explore.js | 40 ++++++++++++++++++++++++++++++++---- src/lib/resolve-ipld-path.js | 9 ++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/bundles/explore.js b/src/bundles/explore.js index 4bf34030..2bc06c88 100644 --- a/src/bundles/explore.js +++ b/src/bundles/explore.js @@ -137,7 +137,15 @@ function ensureLeadingSlash (str) { function makeIpld (IpldResolver, ipldFormats, getIpfs) { return new IpldResolver({ blockService: painfullyCompatibleBlockService(getIpfs()), - formats: ipldFormats + formats: ipldFormats, + async loadFormat (codec) { + const format = ipldFormats.find(f => f.codec === codec) + if (format == null) { + throw new Error('No format found for codec: ' + codec) + } + return format + } + }) } @@ -168,6 +176,9 @@ function painfullyCompatibleBlockService (ipfs) { // recover from new return type in modern JS APIs // https://github.com/ipfs/js-ipfs/pull/3990 if (typeof block.cid === 'undefined') { + if (typeof cid === 'string') { + return { cid: CID.parse(cid), data: block } + } return { cid, data: block } } return block @@ -195,8 +206,30 @@ async function getIpld () { const formats = formatImports.map((actualModule) => { if (actualModule.util == null) { // actualModule has no util. using blockcodec-to-ipld-format - console.log('actualModule: ', actualModule) - return convert(actualModule) + const options = {} + if (actualModule.code === 112) { + /** + * based off of + * * https://github.com/ipld/js-ipld-dag-cbor/blob/b1112f00b605661f6766cd420f48f730ac77a6e0/src/resolver.js#L15-L38 + * * https://github.com/ipld/js-blockcodec-to-ipld-format/blob/master/src/index.js#L38-L55 + */ + options.resolve = (buf, path = '') => { + let value = actualModule.decode(buf) + const entries = path.split('/').filter(x => x) + + if (entries.length > 0) { + const entry = entries.shift() + value = value.Links.find((link) => link.Name === entry) + if (typeof value === 'undefined') { + throw new Error(`Could not find link with name '${entry}'`) + } + } + + return { value, remainderPath: entries.join('/') } + } + } + + return convert(actualModule, options) } return actualModule }) @@ -208,7 +241,6 @@ async function getIpld () { // ipldJson uses the new format, use the conversion tool const ipldJson = await import(/* webpackChunkName: "ipld" */ '@ipld/dag-json') formats.push(convert(ipldJson)) - console.log('formats: ', formats) return { ipld, formats } } diff --git a/src/lib/resolve-ipld-path.js b/src/lib/resolve-ipld-path.js index a6922492..b7f2aa35 100644 --- a/src/lib/resolve-ipld-path.js +++ b/src/lib/resolve-ipld-path.js @@ -1,3 +1,5 @@ +import { CID } from 'multiformats/cid' + import normaliseDagNode from './normalise-dag-node' /** @@ -92,6 +94,13 @@ export async function ipldGetNodeAndRemainder (ipld, sourceCid, path) { // TODO: handle indexing into dag-pb links without using Links prefix as per go-ipfs dag.get does. // Current js-ipld-dag-pb resolver will throw with a path not available error if Links prefix is missing. + + // ensure we're using CID instances + const cidInstance = CID.asCID(sourceCid) + if (typeof sourceCid === 'string' && cidInstance == null) { + sourceCid = CID.parse(sourceCid) + } + return { value: await ipld.get(sourceCid), remainderPath: (await ipld.resolve(sourceCid, path || '/').first()).remainderPath From 92e99752b12143ed3d7c3c8a5f92d7fff7585509 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Wed, 22 Feb 2023 17:44:17 -0800 Subject: [PATCH 8/9] chore: address pr comments --- src/bundles/explore.js | 7 ++----- src/components/object-info/ObjectInfo.js | 8 ++++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/bundles/explore.js b/src/bundles/explore.js index 2bc06c88..967aa4c3 100644 --- a/src/bundles/explore.js +++ b/src/bundles/explore.js @@ -198,11 +198,8 @@ async function getIpld () { import(/* webpackChunkName: "ipld" */ 'ipld-git'), import(/* webpackChunkName: "ipld" */ 'ipld-raw') ]) - const [ipld, ...formatImports] = ipldDeps.map(mod => { - // CommonJs exports object is .default when imported ESM style - const actualModule = mod.default ?? mod - return actualModule - }) + // CommonJs exports object is .default when imported ESM style + const [ipld, ...formatImports] = ipldDeps.map(mod => mod.default ?? mod) const formats = formatImports.map((actualModule) => { if (actualModule.util == null) { // actualModule has no util. using blockcodec-to-ipld-format diff --git a/src/components/object-info/ObjectInfo.js b/src/components/object-info/ObjectInfo.js index afeb69e5..ff230b02 100644 --- a/src/components/object-info/ObjectInfo.js +++ b/src/components/object-info/ObjectInfo.js @@ -64,8 +64,12 @@ const DagNodeIcon = ({ type, ...props }) => ( ) const ObjectInfo = ({ t, tReady, className, type, cid, localPath, size, data, links, format, onLinkClick, gatewayUrl, publicGatewayUrl, ...props }) => { - const cidInfo = decodeCid(cid) - type = cidInfo.cid.codec ?? type + try { + const cidInfo = decodeCid(cid) + type = cidInfo.cid.codec + } catch { + // ignore the error and stick with existing type value. + } return (

From a3670707f408223df18c6d3ef13dfc201f1e2b10 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Mon, 13 Mar 2023 11:13:07 -0700 Subject: [PATCH 9/9] Update src/bundles/explore.js Co-authored-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> --- src/bundles/explore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bundles/explore.js b/src/bundles/explore.js index 967aa4c3..90e2cb83 100644 --- a/src/bundles/explore.js +++ b/src/bundles/explore.js @@ -216,7 +216,7 @@ async function getIpld () { if (entries.length > 0) { const entry = entries.shift() - value = value.Links.find((link) => link.Name === entry) + value = value.Links.find(({ Name }) => Name === entry) if (typeof value === 'undefined') { throw new Error(`Could not find link with name '${entry}'`) }