Skip to content

Commit

Permalink
revert to regex (#251)
Browse files Browse the repository at this point in the history
Squashed commits:
[2206bca] catch cid parse error
[d1d33b5] remove unused warning component
[41aac57] skip redirect if base href is default
[4bcf829] automatically redirect instead of warning
[8f5afd2] redirect with subdomain resolution
[6fefc66] Update app/ts/components/SubpathHostWarning.tsx

Co-authored-by: Micah Zoltu <[email protected]>
[056a8bd] add warning if hosted on subpath
  • Loading branch information
jubalm authored Mar 21, 2024
1 parent 2a3ea54 commit 1e87a9b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
"ethers": "./vendor/ethers/ethers.js",
"@preact/signals-core": "./vendor/@preact/signals-core/signals-core.module.js",
"@preact/signals": "./vendor/@preact/signals/signals.module.js",
"funtypes": "./vendor/funtypes/index.mjs"
"funtypes": "./vendor/funtypes/index.mjs",
"multiformats": "./vendor/multiformats/index.js"
}
}
</script>
Expand Down
3 changes: 3 additions & 0 deletions app/ts/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { TransferPage } from './TransferPage/index.js'
import { EthereumProvider } from '../context/Ethereum.js'
import { WalletProvider } from '../context/Wallet.js'
import { NotificationProvider } from '../context/Notification.js'
import { IPFSSubpathRedirect } from './IPFSSubpathRedirect.js'
import { TemplatesProvider } from '../context/TransferTemplates.js'

export function App() {

return (
<SplashScreen>
<IPFSSubpathRedirect />
<NotificationProvider>
<EthereumProvider>
<WalletProvider>
Expand Down
34 changes: 34 additions & 0 deletions app/ts/components/IPFSSubpathRedirect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CID } from 'multiformats'
import { useEffect } from 'preact/hooks'

export const IPFSSubpathRedirect = () => {
function extractHashFromBasePath() {
const htmlBase = document.querySelector('base')
if (!htmlBase || !htmlBase.href.includes('/ipfs/')) return
const cidRegex = /\/ipfs\/(Qm[1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,})/
const [_, maybeCidMatch] = htmlBase.href.match(cidRegex) || []
return maybeCidMatch
}

function generateIPFSRedirectUrl() {
const cidFromPath = extractHashFromBasePath()
console.log(cidFromPath)

if (!cidFromPath) {
console.log('No IPFS subpath detected')
return
}

const v1CidString = CID.parse(cidFromPath).toV1().toString()
if (!v1CidString) return
return `${location.protocol}//${v1CidString}.ipfs.${location.host}`
}

useEffect(() => {
const redirectUrl = generateIPFSRedirectUrl()
if (!redirectUrl) return
window.location.href = redirectUrl
}, [])

return <></>
}
1 change: 1 addition & 0 deletions build-scripts/vendor.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const dependencyPaths: Dependency[] = [
{ packageName: '@preact/signals-core', subfolderToVendor: 'dist', mainEntrypointFile: 'signals-core.module.js', alternateEntrypoints: {} },
{ packageName: '@preact/signals', subfolderToVendor: 'dist', mainEntrypointFile: 'signals.module.js', alternateEntrypoints: {} },
{ packageName: 'funtypes', subfolderToVendor: 'lib', mainEntrypointFile: 'index.mjs', alternateEntrypoints: {} },
{ packageName: 'multiformats', subfolderToVendor: 'dist/src', mainEntrypointFile: 'index.js', alternateEntrypoints: {} },
]

async function vendorDependencies() {
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@preact/signals": "1.1.1",
"ethers": "6.7.1",
"funtypes": "5.0.3",
"multiformats": "13.0.1",
"preact": "10.8.1"
},
"scripts": {
Expand Down

0 comments on commit 1e87a9b

Please sign in to comment.