kopia lustrzana https://gitlab.com/rysiekpl/libresilient
dnslink-ipfs: alpha working code! (ref. #61)
rodzic
251739fe1a
commit
77bafbe35f
|
@ -17,7 +17,7 @@
|
|||
const pluginName = "dnslink-ipfs"
|
||||
LRPC.set(pluginName, (LR, init={})=>{
|
||||
|
||||
var ipfs;
|
||||
var ipfsPromise;
|
||||
|
||||
// sane defaults
|
||||
let defaultConfig = {
|
||||
|
@ -37,41 +37,25 @@
|
|||
if (typeof self.importScripts !== 'undefined') {
|
||||
self.importScripts.apply(self, args)
|
||||
} else {
|
||||
console.log('(COMMIT_UNKNOWN) assuming these scripts are already included:')
|
||||
LR.log(pluginName, 'assuming these scripts are already included:')
|
||||
args.forEach(function(src){
|
||||
console.log('+--', src)
|
||||
LR.log(pluginName, '+--', src)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function setup_ipfs() {
|
||||
if (ipfs === undefined) {
|
||||
ipfs = false // we don't want to start a few times over
|
||||
console.log('(COMMIT_UNKNOWN) Importing IPFS-related libraries...');
|
||||
doImport(
|
||||
"./lib/ipfs.js");
|
||||
console.log('(COMMIT_UNKNOWN) Setting up IPFS...')
|
||||
try {
|
||||
ipfs = await self.Ipfs.create({
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true,
|
||||
clientMode: true
|
||||
}
|
||||
},
|
||||
libp2p: {
|
||||
config: {
|
||||
dht: {
|
||||
enabled: true,
|
||||
clientMode: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
console.log('+-- IPFS loaded :: ipfs is : ' + typeof ipfs)
|
||||
} catch(e) {
|
||||
console.error('+-- Error loading IPFS: ' + e)
|
||||
}
|
||||
LR.log(pluginName, 'Importing IPFS-related libraries...');
|
||||
doImport(
|
||||
"./lib/ipfs.js");
|
||||
LR.log(pluginName, 'Setting up IPFS...')
|
||||
try {
|
||||
var ipfs = await self.Ipfs.create();
|
||||
LR.log(pluginName, '+-- IPFS loaded :: ipfs is : ' + typeof ipfs)
|
||||
return ipfs
|
||||
} catch(e) {
|
||||
LR.log(pluginName, '+-- Error loading IPFS: ' + e)
|
||||
throw new Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,6 +68,10 @@
|
|||
*/
|
||||
async function getContentFromDNSLinkAndIPFS(url, init={}) {
|
||||
|
||||
// make sure IPFS is set-up
|
||||
var ipfs = await ipfsPromise
|
||||
|
||||
// we don't want the scheme
|
||||
var dnslinkAddr = url.replace(/https?:\/\//, '')
|
||||
|
||||
/*
|
||||
|
@ -92,11 +80,11 @@
|
|||
* TODO: might not be necessary; if removed, update the content-type switch statement below!
|
||||
*/
|
||||
if (dnslinkAddr.charAt(dnslinkAddr.length - 1) === '/') {
|
||||
console.log("NOTICE: address ends in '/', assuming '/index.html' should be appended.");
|
||||
LR.log(pluginName, "NOTICE: address ends in '/', assuming '/index.html' should be appended.");
|
||||
dnslinkAddr += 'index.html';
|
||||
}
|
||||
|
||||
console.log("+-- starting DNSLink lookup of: '" + dnslinkAddr + "'");
|
||||
LR.log(pluginName, "+-- starting DNSLink lookup of: '" + dnslinkAddr + "'");
|
||||
|
||||
/*
|
||||
* naïvely assume content type based on file extension
|
||||
|
@ -124,45 +112,55 @@
|
|||
console.log(" +-- guessed contentType : " + contentType);
|
||||
|
||||
return ipfs.resolve('/ipns/' + dnslinkAddr).then(ipfsaddr => {
|
||||
console.log("+-- starting IPFS retrieval of: '" + ipfsaddr + "'");
|
||||
return ipfs.get(ipfsaddr).next();
|
||||
}).then(file => {
|
||||
// we only need one
|
||||
if (file.value.content) {
|
||||
async function getContent(source) {
|
||||
var content = new Uint8Array()
|
||||
var data = await source.next()
|
||||
while (! data.done) {
|
||||
var newContent = new Uint8Array(content.length + data.value.length);
|
||||
newContent.set(content)
|
||||
newContent.set(data.value, content.length)
|
||||
content = newContent
|
||||
data = await source.next()
|
||||
}
|
||||
return content
|
||||
}
|
||||
return getContent(file.value.content).then((content)=>{
|
||||
console.log('+-- got a DNSLink-resolved IPFS-stored file: ' + file.value.path + '; content is: ' + typeof content);
|
||||
// creating and populating the blob
|
||||
var blob = new Blob(
|
||||
[content],
|
||||
{'type': contentType}
|
||||
);
|
||||
LR.log(pluginName, "+-- starting IPFS retrieval of: '" + ipfsaddr + "'");
|
||||
return ipfs.get(ipfsaddr);
|
||||
|
||||
}).then(async (source) => {
|
||||
|
||||
LR.log(pluginName, '+-- started receiving file data')
|
||||
// source is an iterator
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
|
||||
var filedata = await source.next();
|
||||
|
||||
// did we get anything?
|
||||
if (filedata.value) {
|
||||
|
||||
// initialize
|
||||
var content = new Uint8Array()
|
||||
|
||||
do {
|
||||
LR.log(pluginName, ' +-- new data:', filedata.done, filedata.value.length)
|
||||
var newContent = new Uint8Array(content.length + filedata.value.length);
|
||||
newContent.set(content)
|
||||
newContent.set(filedata.value, content.length)
|
||||
content = newContent
|
||||
filedata = await source.next()
|
||||
} while (! filedata.done)
|
||||
|
||||
LR.log(pluginName, '+-- got a DNSLink-resolved IPFS-stored file; content is: ' + typeof content);
|
||||
|
||||
// creating and populating the blob
|
||||
var blob = new Blob(
|
||||
[content],
|
||||
{'type': contentType}
|
||||
);
|
||||
|
||||
return new Response(
|
||||
blob,
|
||||
{
|
||||
'status': 200,
|
||||
'statusText': 'OK',
|
||||
'headers': {
|
||||
'Content-Type': contentType,
|
||||
'ETag': file.value.path,
|
||||
'X-LibResilient-Method': pluginName,
|
||||
'X-LibResilient-ETag': file.value.path
|
||||
}
|
||||
return new Response(
|
||||
blob,
|
||||
{
|
||||
'status': 200,
|
||||
'statusText': 'OK',
|
||||
'headers': {
|
||||
'Content-Type': contentType,
|
||||
'ETag': 'WOLOLO',
|
||||
'X-LibResilient-Method': pluginName,
|
||||
'X-LibResilient-ETag': 'WOLOLO'
|
||||
}
|
||||
);
|
||||
})
|
||||
}
|
||||
);
|
||||
} else {
|
||||
LR.log(pluginName, '+-- IPFS retrieval failed: no content.')
|
||||
throw new Error('IPFS retrieval failed: no content.')
|
||||
};
|
||||
});
|
||||
}
|
||||
|
@ -183,7 +181,7 @@
|
|||
\* ========================================================================= */
|
||||
|
||||
// we probably need to handle this better
|
||||
setup_ipfs();
|
||||
ipfsPromise = setup_ipfs();
|
||||
|
||||
|
||||
// and add ourselves to it
|
||||
|
|
Ładowanie…
Reference in New Issue