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

init ipfs in default document loader #513

Open
wants to merge 4 commits 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
11 changes: 7 additions & 4 deletions lib/documentLoaders/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ module.exports = ({
async function loadDocument(url, redirects) {
const isHttp = url.startsWith('http:');
const isHttps = url.startsWith('https:');
if(!isHttp && !isHttps) {
const isIpfs = url.startsWith('ipfs:');
if(!isHttp && !isHttps && !isIpfs) {
throw new JsonLdError(
'URL could not be dereferenced; only "http" and "https" URLs are ' +
'URL could not be dereferenced; only "http", "https", and "ipfs" URLs are ' +
'supported.',
'jsonld.InvalidUrl', {code: 'loading document failed', url});
}
if(secure && !isHttps) {
if(secure && isHttp) {
throw new JsonLdError(
'URL could not be dereferenced; secure mode is enabled and ' +
'the URL\'s scheme is not "https".',
Expand All @@ -76,8 +77,10 @@ module.exports = ({

let alternate = null;

const requestUrl = !isIpfs ? url : 'https://ipfs.io/ipfs/' + url.split('ipfs://')[1];

const {res, body} = await _fetch({
url, headers, strictSSL, httpAgent, httpsAgent
url: requestUrl, headers, strictSSL, httpAgent, httpsAgent
});
doc = {contextUrl: null, documentUrl: url, document: body || null};

Expand Down
10 changes: 6 additions & 4 deletions lib/documentLoaders/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,24 @@ module.exports = ({
return queue.wrapLoader(loader);

async function loader(url) {
if(url.indexOf('http:') !== 0 && url.indexOf('https:') !== 0) {
if(url.indexOf('http:') !== 0 && url.indexOf('https:') !== 0 && url.indexOf('ipfs:') !== 0) {
throw new JsonLdError(
'URL could not be dereferenced; only "http" and "https" URLs are ' +
'URL could not be dereferenced; only "http", "https", and "ipfs" URLs are ' +
'supported.',
'jsonld.InvalidUrl', {code: 'loading document failed', url});
}
if(secure && url.indexOf('https') !== 0) {
if(secure && (url.indexOf('https') !== 0 || url.indexOf('ipfs') !== 0)) {
throw new JsonLdError(
'URL could not be dereferenced; secure mode is enabled and ' +
'the URL\'s scheme is not "https".',
'jsonld.InvalidUrl', {code: 'loading document failed', url});
}

const requestUrl = url.indexOf('ipfs:') === 0 ? 'https://ipfs.io/ipfs/' + url.split('ipfs://')[1] : url;

let req;
try {
req = await _get(xhr, url, headers);
req = await _get(xhr, requestUrl, headers);
} catch(e) {
throw new JsonLdError(
'URL could not be dereferenced, an error occurred.',
Expand Down