Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
document.addEventListener('DOMContentLoaded', async () => {
// IPFS node setup
const node = await IPFS.create({ repo: String(Math.random() + Date.now()) })
// UI elements
const status = document.getElementById('status')
const output = document.getElementById('output')
output.textContent = ''
function log (txt) {
console.info(txt)
output.textContent += `${txt.trim()}\n`
}
status.innerText = 'Connected to IPFS :)'
const version = await node.version()
async function main () {
// Initialize our IPFS node with the custom repo options
const node = await IPFS.create({
repo: new Repo('/tmp/custom-repo/.ipfs', customRepositoryOptions),
// This just means we dont try to connect to the network which isn't necessary
// to demonstrate custom repos
config: {
Bootstrap: []
}
})
// Test the new repo by adding and fetching some data
console.log('Ready')
const { version } = await node.version()
console.log('Version:', version)
// Once we have the version, let's add a file to IPFS
const filesAdded = await node.add({
async function main () {
const node = await IPFS.create()
const version = await node.version()
console.log('Version:', version.version)
const filesAdded = await node.add({
path: 'hello.txt',
content: Buffer.from('Hello World 101')
})
console.log('Added file:', filesAdded[0].path, filesAdded[0].hash)
const fileBuffer = await node.cat(filesAdded[0].hash)
console.log('Added file contents:', fileBuffer.toString())
}
async ops () {
const node = await IPFS.create({ repo: String(Math.random() + Date.now()) })
console.log('IPFS node is ready')
const { id, agentVersion, protocolVersion } = await node.id()
this.setState({ id, agentVersion, protocolVersion })
const [{ hash }] = await node.add(stringToUse)
this.setState({ addedFileHash: hash })
const data = await node.cat(hash)
this.setState({ addedFileContents: data.toString() })
}
preload: { enabled: false },
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star',
],
},
},
};
const orbitOptions = {
directory: `augur-orbit-chat-orbitdb-${ethereumNetwork}`,
id: senderAccount,
};
ipfs = await IPFS.create(ipfsOptions);
orbit = await Orbit.create(ipfs, orbitOptions);
orbit.events.emit('connected', (orbit as any).user);
trackNumberOfPeers();
ctx.postMessage({ method: 'IPFS:started' });
break;
}
case 'IPFS:sendMessage': {
const [data, channel] = messageData.params;
if (!data || !channel) break;
if (currentChannelName !== channel || currentChannel == null) {
async function startIpfs () {
if (ipfs) {
console.log('IPFS already started')
} else if (window.ipfs && window.ipfs.enable) {
console.log('Found window.ipfs')
ipfs = await window.ipfs.enable({ commands })
} else {
try {
console.time('IPFS Started')
ipfs = await Ipfs.create()
console.timeEnd('IPFS Started')
} catch (error) {
console.error('IPFS init error:', error)
ipfs = null
setIpfsInitError(error)
}
}
setIpfsReady(Boolean(ipfs))
}
async function start () {
if (!node) {
const options = {
repo: 'ipfs-' + Math.random(),
config: {
Addresses: {
Swarm: ['/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star']
}
}
}
node = await IPFS.create(options)
try {
info = await node.id()
updateView('ready', node)
} catch (err) {
return onError(err)
}
onSuccess('Node is ready.')
setInterval(async () => {
try {
await refreshPeerList()
} catch (err) {
err.message = `Failed to refresh the peer list: ${err.message}`
onError(err)
static async create(db: DB) {
const ipfs = await IPFS.create();
return new WarpController(db, ipfs);
}