Skip to content

Commit

Permalink
fix: Allow native reading of data: urls for VerifiableURI
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Feb 23, 2024
1 parent f7cdfec commit 980d553
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/lib/getDataFromExternalSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,29 @@ export const getDataFromExternalSources = (
urlDataWithHash as URLDataWithHash,
ipfsGateway,
);

try {
if (/[=?/]$/.test(url)) {
// this URL is not verifiable and the URL ends with a / or ? or = meaning it's not a file
// and more likely to be some kind of directory or query BaseURI
return dataEntry;
}
const receivedData = await fetch(url).then(async (response) => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response
.arrayBuffer()
.then((buffer) => new Uint8Array(buffer));
});
let receivedData: Uint8Array;
const [, encoding, data] = url.match(/^data:.*?;(.*?),(.*)$/) || [];
if (data) {
receivedData = Uint8Array.from(
Buffer.from(data, encoding === 'base64' ? 'base64' : 'utf8'),
);
} else {
receivedData = await fetch(url).then(async (response) => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response
.arrayBuffer()
.then((buffer) => new Uint8Array(buffer));
});
}
if (receivedData.length >= 2) {
// JSON data cannot be less than 2 characters long.
try {
Expand Down

0 comments on commit 980d553

Please sign in to comment.