diff --git a/examples/browser-add-readable-stream/index.js b/examples/browser-add-readable-stream/index.js index 9f7d0c0f7c..7abfb83554 100644 --- a/examples/browser-add-readable-stream/index.js +++ b/examples/browser-add-readable-stream/index.js @@ -38,27 +38,29 @@ const createFiles = (directory) => { }] } -const streamFiles = (ipfs, directory, files) => new Promise((resolve, reject) => { +const streamFiles = async (ipfs, directory, files) => { // Create a stream to write files to - const stream = ipfs.addReadableStream() + const stream = new ReadableStream({ + start(controller) { + for (let i = 0; i < files.length; i++) { + // Add the files one by one + controller.enqueue(files[i]) + } + + // When we have no more files to add, close the stream + controller.close() + } + }) - stream.on('data', (data) => { + for await (const data of ipfs.add(stream)) { log(`Added ${data.path} hash: ${data.hash}`) // The last data event will contain the directory hash if (data.path === directory) { - resolve(data.hash) + return data.cid } - }) - - stream.on('error', reject) - - // Add the files one by one - files.forEach(file => stream.write(file)) - - // When we have no more files to add, close the stream - stream.end() -}) + } +} const log = (line) => { document.getElementById('output').appendChild(document.createTextNode(`${line}\r\n`)) diff --git a/examples/browser-browserify/public/index.html b/examples/browser-browserify/public/index.html index 041385a09d..cebc18fcd4 100644 --- a/examples/browser-browserify/public/index.html +++ b/examples/browser-browserify/public/index.html @@ -18,7 +18,7 @@

JS IPFS - Add data to IPFS from the browser

diff --git a/examples/browser-browserify/src/index.js b/examples/browser-browserify/src/index.js index af565ae325..4fdbf533bf 100644 --- a/examples/browser-browserify/src/index.js +++ b/examples/browser-browserify/src/index.js @@ -9,23 +9,22 @@ document.addEventListener('DOMContentLoaded', async () => { async function store () { const toStore = document.getElementById('source').value - const result = await node.add(toStore) - for (const file of result) { - if (file && file.hash) { - console.log('successfully stored', file.hash) + for await (const file of node.add(toStore)) { + if (file && file.cid) { + console.log('successfully stored', file.cid) - await display(file.hash) + await display(file.cid) } } } - async function display (hash) { - const data = await node.cat(hash) - - document.getElementById('hash').innerText = hash - document.getElementById('content').innerText = data - document.getElementById('output').setAttribute('style', 'display: block') + async function display (cid) { + for await (const data of node.cat(cid)) { + document.getElementById('cid').innerText = cid + document.getElementById('content').innerText = data + document.getElementById('output').setAttribute('style', 'display: block') + } } document.getElementById('store').onclick = store diff --git a/examples/browser-browserify/test.js b/examples/browser-browserify/test.js index 4b729108d4..f3e8df812c 100644 --- a/examples/browser-browserify/test.js +++ b/examples/browser-browserify/test.js @@ -13,7 +13,7 @@ module.exports = { .click('#store') .waitForElementVisible('#output') - browser.expect.element('#hash').text.to.contain('QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX') + browser.expect.element('#cid').text.to.contain('QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX') browser.expect.element('#content').text.to.contain('hello') browser.end() diff --git a/examples/browser-create-react-app/src/hooks/use-ipfs-factory.js b/examples/browser-create-react-app/src/hooks/use-ipfs-factory.js index fa0464d484..9147b4678d 100644 --- a/examples/browser-create-react-app/src/hooks/use-ipfs-factory.js +++ b/examples/browser-create-react-app/src/hooks/use-ipfs-factory.js @@ -26,6 +26,7 @@ export default function useIpfsFactory ({ commands }) { if (ipfs && ipfs.stop) { console.log('Stopping IPFS') ipfs.stop().catch(err => console.error(err)) + ipfs = null setIpfsReady(false) } } diff --git a/examples/browser-mfs/filetree.js b/examples/browser-mfs/filetree.js index 1ca04cffa0..995658e8a4 100644 --- a/examples/browser-mfs/filetree.js +++ b/examples/browser-mfs/filetree.js @@ -1,7 +1,6 @@ 'use strict' const { - log, createNode } = require('./utils') @@ -20,13 +19,7 @@ const loadFiles = async (ipfs, path) => { const output = {} path = path.replace(/\/\/+/g, '/') - const contents = await ipfs.files.ls(path, { - long: true - }) - .catch(error => log(error)) - - for (let i = 0; i < contents.length; i++) { - let entry = contents[i] + for await (const entry of ipfs.files.ls(path)) { output[entry.name] = entry if (entry.type === FILE_TYPES.DIRECTORY) { diff --git a/examples/browser-parceljs/public/index.js b/examples/browser-parceljs/public/index.js index a24c5b3d97..803553a3fa 100644 --- a/examples/browser-parceljs/public/index.js +++ b/examples/browser-parceljs/public/index.js @@ -22,14 +22,18 @@ document.addEventListener('DOMContentLoaded', async () => { log(`The IPFS node version is ${version.version}`) - const filesAdded = await node.add({ + for await (const entry of node.add({ path: 'hello-parcel.txt', content: 'Hello from parcel.js bundled ipfs example' - }) + })) { + log(`This page deployed ${entry.path} to IPFS and its CID is ${entry.cid}`) - log(`This page deployed ${filesAdded[0].path} to IPFS and its hash is ${filesAdded[0].hash}`) + const buffers = [] - const fileBuffer = await node.cat(filesAdded[0].hash) + for await (const buf of node.cat(entry.cid)) { + buffers.push(buf) + } - log(`The contents of the file was: ${fileBuffer.toString()}`) + log(`The contents of the file was: ${Buffer.concat(buffers).toString()}`) + } }) diff --git a/examples/browser-readablestream/index.html b/examples/browser-readablestream/index.html index 61ded58b13..ba1e63b87e 100644 --- a/examples/browser-readablestream/index.html +++ b/examples/browser-readablestream/index.html @@ -55,7 +55,7 @@
- +
diff --git a/examples/browser-readablestream/index.js b/examples/browser-readablestream/index.js index 373cb51402..083d2c685a 100644 --- a/examples/browser-readablestream/index.js +++ b/examples/browser-readablestream/index.js @@ -4,6 +4,7 @@ const Ipfs = require('../../') const VideoStream = require('videostream') +const toStream = require('it-to-stream') const { dragDrop, statusMessages, @@ -18,14 +19,14 @@ document.addEventListener('DOMContentLoaded', async () => { // Set up event listeners on the