From 5298a1b7dbafa537cbb92cfc828533ba46cb52a5 Mon Sep 17 00:00:00 2001 From: F-Node-Karlsruhe Date: Tue, 21 Mar 2023 16:51:16 +0100 Subject: [PATCH 1/4] init ipfs in default document loader --- lib/documentLoaders/node.js | 11 +++++++---- lib/documentLoaders/xhr.js | 10 ++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/documentLoaders/node.js b/lib/documentLoaders/node.js index 61ad6b30..4313531e 100644 --- a/lib/documentLoaders/node.js +++ b/lib/documentLoaders/node.js @@ -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".', @@ -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 + requestUrl, headers, strictSSL, httpAgent, httpsAgent }); doc = {contextUrl: null, documentUrl: url, document: body || null}; diff --git a/lib/documentLoaders/xhr.js b/lib/documentLoaders/xhr.js index 80688817..33864324 100644 --- a/lib/documentLoaders/xhr.js +++ b/lib/documentLoaders/xhr.js @@ -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.', From 4a4222e576fe188dc6ed9668ae8b2c6549f11123 Mon Sep 17 00:00:00 2001 From: F-Node-Karlsruhe <41575897+F-Node-Karlsruhe@users.noreply.github.com> Date: Tue, 21 Mar 2023 19:22:39 +0100 Subject: [PATCH 2/4] Update lib/documentLoaders/node.js Co-authored-by: Dave Longley --- lib/documentLoaders/node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/documentLoaders/node.js b/lib/documentLoaders/node.js index 4313531e..5f98edd7 100644 --- a/lib/documentLoaders/node.js +++ b/lib/documentLoaders/node.js @@ -80,7 +80,7 @@ module.exports = ({ const requestUrl = !isIpfs ? url : 'https://ipfs.io/ipfs/' + url.split('ipfs://')[1]; const {res, body} = await _fetch({ - requestUrl, headers, strictSSL, httpAgent, httpsAgent + url: requestUrl, headers, strictSSL, httpAgent, httpsAgent }); doc = {contextUrl: null, documentUrl: url, document: body || null}; From 96c941fecd226fba9f69cd37c577a844e291562d Mon Sep 17 00:00:00 2001 From: F-Node-Karlsruhe <41575897+F-Node-Karlsruhe@users.noreply.github.com> Date: Tue, 21 Mar 2023 19:22:52 +0100 Subject: [PATCH 3/4] Update lib/documentLoaders/xhr.js Co-authored-by: Dave Longley --- lib/documentLoaders/xhr.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/documentLoaders/xhr.js b/lib/documentLoaders/xhr.js index 33864324..e7b52ec1 100644 --- a/lib/documentLoaders/xhr.js +++ b/lib/documentLoaders/xhr.js @@ -34,7 +34,7 @@ module.exports = ({ async function loader(url) { if(url.indexOf('http:') !== 0 && url.indexOf('https:') !== 0 && url.indexOf('ipfs:') !== 0) { throw new JsonLdError( - 'URL could not be dereferenced; only "http", "https" and "ipfs" URLs are ' + + 'URL could not be dereferenced; only "http", "https", and "ipfs" URLs are ' + 'supported.', 'jsonld.InvalidUrl', {code: 'loading document failed', url}); } From 1f5b5ceca1eacaa295ac54bb7f5b2d4cc6e753dc Mon Sep 17 00:00:00 2001 From: F-Node-Karlsruhe <41575897+F-Node-Karlsruhe@users.noreply.github.com> Date: Tue, 21 Mar 2023 19:23:05 +0100 Subject: [PATCH 4/4] Update lib/documentLoaders/node.js Co-authored-by: Dave Longley --- lib/documentLoaders/node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/documentLoaders/node.js b/lib/documentLoaders/node.js index 5f98edd7..7c6e7ce5 100644 --- a/lib/documentLoaders/node.js +++ b/lib/documentLoaders/node.js @@ -59,7 +59,7 @@ module.exports = ({ const isIpfs = url.startsWith('ipfs:'); if(!isHttp && !isHttps && !isIpfs) { throw new JsonLdError( - 'URL could not be dereferenced; only "http", "https" and "ipfs" URLs are ' + + 'URL could not be dereferenced; only "http", "https", and "ipfs" URLs are ' + 'supported.', 'jsonld.InvalidUrl', {code: 'loading document failed', url}); }