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