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

Faster gateway usage #148

Merged
Merged
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16.13.2
v16.16.0
12 changes: 12 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ exports.onCreateWebpackConfig = ({
fallback: { canvas: false },
}
})
if (stage === "build-html" || stage === "develop-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /ipfs-utils|ipfs-http-client/,
use: loaders.null(),
},
],
},
})
}
}

function isSuperset(set, subset) {
Expand Down
28,732 changes: 15,514 additions & 13,218 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"gatsby-transformer-sharp": "^4.6.0",
"html-react-parser": "^1.4.5",
"idb-keyval": "^6.1.0",
"ipfs-http-client": "^56.0.0",
"ipfs-http-client": "^57.0.3",
"js-base64": "^3.6.0",
"jszip": "^3.6.0",
"material-design-icons": "^3.0.1",
Expand Down
4 changes: 2 additions & 2 deletions src/ipfs/ipfsClient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { create as createIpfsClient } from 'ipfs-http-client';

const ipfsAPIUrl = 'https://dweb.link/api/v0'
const ipfsClient = createIpfsClient({ url: ipfsAPIUrl })
const ipfsClient = typeof window !== "undefined" ? createIpfsClient({ url: ipfsAPIUrl }) : null

export default ipfsClient
export default ipfsClient
1 change: 1 addition & 0 deletions src/siteMetadata/insightJournal.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"articlesCid": "bafybeialgmdqikskc56dhy2xrnio2te23xdzqdm77wtqxpxybr3ys5cpli",
"targetJournal": 3,
"title": "The Insight Journal",
"copyrightHolder": "NumFOCUS, Inc. for the Insight Software Consortium",
Expand Down
1 change: 1 addition & 0 deletions src/siteMetadata/midasJournal.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"articlesCid": "bafybeialgmdqikskc56dhy2xrnio2te23xdzqdm77wtqxpxybr3ys5cpli",
"targetJournal": 31,
"title": "The MIDAS Journal",
"copyrightHolder": "Kitware, Inc.",
Expand Down
1 change: 1 addition & 0 deletions src/siteMetadata/vtkJournal.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"articlesCid": "bafybeialgmdqikskc56dhy2xrnio2te23xdzqdm77wtqxpxybr3ys5cpli",
"targetJournal": 35,
"title": "The VTK Journal",
"copyrightHolder": "Kitware, Inc.",
Expand Down
25 changes: 11 additions & 14 deletions src/templates/publication.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from "axios";
import React, { Suspense } from "react";
import { graphql } from "gatsby";
import { ThemeProvider, makeStyles } from '@mui/styles';
Expand Down Expand Up @@ -176,24 +177,19 @@ async function sourceCodeTreeRows(sourceCodeCid, treePath) {
return treeRows
}

async function loadArticle(publication, revision, setArticleContent) {
async function loadArticle(articlesCid, publication, revision, setArticleContent) {
const articleCid = publication.revisions[revision].article
if (!articleCid) {
setArticleContent(<><Typography m={2}>Article not found.</Typography></>)
} else {
const response = await fetch(`https://gateway.pinata.cloud/ipfs/${articleCid}`, { mode: 'cors', headers: { 'Content-Type': 'application/octet-stream' } })
const reader = response.body.getReader();

const chunks = []
while (true) {
const { value, done } = await reader.read();
if (done) break;
chunks.push(value)
const chunkNum = `${chunks.length}`
setArticleContent(<><LinearProgressWithLabel variant="indeterminate" color="secondary" label={`loading chunk ${chunkNum}`} /></>)
}
setArticleContent(<><LinearProgressWithLabel variant="indeterminate" color="secondary" label={`loading article`} /></>)
const pinataArticleUrl = `https://itk.mypinata.cloud/ipfs/${articlesCid}/ij-articles/${publication.publication_id}/${revision+1}/article.pdf`
const getPinata = axios.get(pinataArticleUrl, { responseType: 'arraybuffer' })
const dwebArticleUrl = `https://${articlesCid}.ipfs.dweb.link/ij-articles/${publication.publication_id}/${revision+1}/article.pdf`
const getDweb = axios.get(dwebArticleUrl, { responseType: 'arraybuffer' })
const response = await Promise.race([getPinata, getDweb])
setArticleContent(<><LinearProgressWithLabel color="primary" variant="determinate" value={100} label={`loading complete`} /></>)
const pdf = uint8arrays.concat(chunks)
const pdf = new Uint8Array(response.data)
const pdfBlob = new Blob([pdf.buffer])
const pdfBase64 = Base64.fromUint8Array(pdf)
const titleForFile = publication.title.split(' ').join('_')
Expand Down Expand Up @@ -420,7 +416,7 @@ const Render = ({ data, pageContext }) => {
setFileContent(<></>)
break
case '2':
loadArticle(publication, revision, setArticleContent)
loadArticle(data.site.siteMetadata.articlesCid, publication, revision, setArticleContent)
setFileContent(<></>)
break
case '3':
Expand Down Expand Up @@ -528,6 +524,7 @@ export const query = graphql`
}
site {
siteMetadata {
articlesCid,
targetJournal,
title,
}
Expand Down