-
-
Save OliverUv/84e661c2df2638944c3f7ab5ef933cac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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