Skip to content

Instantly share code, notes, and snippets.

@OliverUv
Created September 25, 2018 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OliverUv/84e661c2df2638944c3f7ab5ef933cac to your computer and use it in GitHub Desktop.
Save OliverUv/84e661c2df2638944c3f7ab5ef933cac to your computer and use it in GitHub Desktop.
'use strict'
const IPFS = require('ipfs')
const node = new IPFS({
// repo: String(Math.random() + Date.now()),
repo: 'REPOMAN',
// init: false,
// start: false,
preload: {
enabled: false,
addresses: [],
},
config: {
Addresses: {
Swarm: [],
},
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: false,
Interval: 10,
},
webRTCStar: {
Enabled: false,
},
},
},
});
const storedFiles = {};
const storedDirs = {};
async function onReady() {
try {
const r = await node.files.add([{
path: '/test/hello.txt',
content: Buffer.from('world'),
}], {
chunker: 'rabin',
});
if (!r) { return console.log('ipfs add error', r) }
r.forEach((file) => {
if (file && file.hash && file.path.includes('/')) {
storedFiles[file.path] = file.hash;
console.log('STORED FILE');
console.log(file);
} else {
storedDirs[file.path] = file.hash;
console.log('STORED DIR');
console.log(file);
}
});
} catch (e) {
console.log('ipfs add error', e);
return;
}
console.log('\n///////\n');
for (const path in storedFiles) {
const hash = storedFiles[path];
try {
const r = await node.files.cat(hash);
console.log(`In path '${path}' stored:`);
console.log(`hash: ${hash}`);
console.log(`data: ${r}`);
} catch (e) {
console.error('ipfs cat error', e);
}
}
console.log('\n///////\n');
for (const path in storedDirs) {
const hash = storedDirs[path];
console.log(`As path '${path}' stored dir:`);
console.log(`hash: ${hash}`);
console.log(`ls:`);
const r = await node.files.ls(`/ipfs/${hash}`);
console.log(r);
}
}
node.on('ready', async () => await onReady())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment