diff --git a/examples/exchange-files-in-browser/package.json b/examples/exchange-files-in-browser/package.json index f8d95a78f5..ea74a683f9 100644 --- a/examples/exchange-files-in-browser/package.json +++ b/examples/exchange-files-in-browser/package.json @@ -18,6 +18,8 @@ }, "dependencies": { "ipfs": "^0.41.0", + "it-all": "^1.0.1", + "it-last": "^1.0.1", "test-ipfs-example": "^1.0.0" }, "browser": { diff --git a/examples/exchange-files-in-browser/public/app.js b/examples/exchange-files-in-browser/public/app.js index 8e021e8aa3..590650f564 100644 --- a/examples/exchange-files-in-browser/public/app.js +++ b/examples/exchange-files-in-browser/public/app.js @@ -2,6 +2,8 @@ 'use strict' const IPFS = require('ipfs') +const all = require('it-all') +const last = require('it-last') const { Buffer } = IPFS // Node @@ -53,7 +55,9 @@ async function start () { // '/dns4/star-signal.cloud.ipfs.team/wss/p2p-webrtc-star' '/ip4/127.0.0.1/tcp/13579/wss/p2p-webrtc-star' ] - } + }, + // If you want to connect to the public bootstrap nodes, remove the next line + Bootstrap: [] } }) @@ -219,7 +223,9 @@ async function getFile () { for await (const file of node.get(hash)) { if (file.content) { - await appendFile(file.name, hash, file.size, file.content) + const content = Buffer.concat(await all(file.content)) + + await appendFile(file.name, hash, file.size, content) onSuccess(`The ${file.name} file was added.`) $emptyRow.style.display = 'none' } @@ -242,14 +248,17 @@ async function onDrop (event) { for (const file of files) { fileSize = file.size // Note: fileSize is used by updateProgress - const filesAdded = await node.add({ + const fileAdded = await last(node.add({ path: file.name, content: file - }, { wrapWithDirectory: true, progress: updateProgress }) + }, { + wrapWithDirectory: true, + progress: updateProgress + })) // As we are wrapping the content we use that hash to keep // the original file name when adding it to the table - $cidInput.value = filesAdded[1].hash + $cidInput.value = fileAdded.cid.toString() resetProgress() await getFile() diff --git a/examples/exchange-files-in-browser/test.js b/examples/exchange-files-in-browser/test.js index ba05fe1b3e..30d7582e10 100644 --- a/examples/exchange-files-in-browser/test.js +++ b/examples/exchange-files-in-browser/test.js @@ -1,5 +1,6 @@ 'use strict' +const http = require('http') const fs = require('fs-extra') const path = require('path') const os = require('os') @@ -20,6 +21,8 @@ const { const pkg = require('./package.json') const webRTCStarSigServer = require('libp2p-webrtc-star/src/sig-server') +const FILE_CONTENT = 'A file with some content' + async function testUI (env) { const proc = execa(require.resolve('test-ipfs-example/node_modules/.bin/nightwatch'), [ '--config', require.resolve('test-ipfs-example/nightwatch.conf.js'), path.join(__dirname, 'test.js') ], { cwd: path.resolve(__dirname, '../'), @@ -58,7 +61,7 @@ async function runTest () { let cid - for await (const imported of relay.api.add('A file with some content')) { + for await (const imported of relay.api.add(FILE_CONTENT)) { cid = imported.cid } @@ -186,5 +189,19 @@ module.exports[pkg.name] = function (browser) { // but should both see the added file browser.expect.element('#file-history').text.to.contain(process.env.IPFS_CID) + if (!process.env.IPFS_ADD_FILE) { + // download the file from the other browser + browser + .getAttribute(`a[download=${process.env.IPFS_CID}]`, 'href', ({ value: url }) => { + browser.executeAsync(function (url, done) { + fetch(url) + .then(res => res.text()) + .then(done, done) + }, [ url ], ({ value: contents }) => { + browser.expect(contents.toString('utf8')).to.equal(FILE_CONTENT) + }) + }) + } + browser.end() }