Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit cd46c78

Browse files
authored
fix: fix exchange in files example (#2913)
The test for this example did not go as far as downloading the file and asserting the contents are correct - that part had not been refactored to be async/await happy so wasn't working. I've expanded the test to peform the download too.
1 parent a640ef2 commit cd46c78

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

examples/exchange-files-in-browser/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
},
1919
"dependencies": {
2020
"ipfs": "^0.41.0",
21+
"it-all": "^1.0.1",
22+
"it-last": "^1.0.1",
2123
"test-ipfs-example": "^1.0.0"
2224
},
2325
"browser": {

examples/exchange-files-in-browser/public/app.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
'use strict'
33

44
const IPFS = require('ipfs')
5+
const all = require('it-all')
6+
const last = require('it-last')
57
const { Buffer } = IPFS
68

79
// Node
@@ -53,7 +55,9 @@ async function start () {
5355
// '/dns4/star-signal.cloud.ipfs.team/wss/p2p-webrtc-star'
5456
'/ip4/127.0.0.1/tcp/13579/wss/p2p-webrtc-star'
5557
]
56-
}
58+
},
59+
// If you want to connect to the public bootstrap nodes, remove the next line
60+
Bootstrap: []
5761
}
5862
})
5963

@@ -219,7 +223,9 @@ async function getFile () {
219223

220224
for await (const file of node.get(hash)) {
221225
if (file.content) {
222-
await appendFile(file.name, hash, file.size, file.content)
226+
const content = Buffer.concat(await all(file.content))
227+
228+
await appendFile(file.name, hash, file.size, content)
223229
onSuccess(`The ${file.name} file was added.`)
224230
$emptyRow.style.display = 'none'
225231
}
@@ -242,14 +248,17 @@ async function onDrop (event) {
242248
for (const file of files) {
243249
fileSize = file.size // Note: fileSize is used by updateProgress
244250

245-
const filesAdded = await node.add({
251+
const fileAdded = await last(node.add({
246252
path: file.name,
247253
content: file
248-
}, { wrapWithDirectory: true, progress: updateProgress })
254+
}, {
255+
wrapWithDirectory: true,
256+
progress: updateProgress
257+
}))
249258

250259
// As we are wrapping the content we use that hash to keep
251260
// the original file name when adding it to the table
252-
$cidInput.value = filesAdded[1].hash
261+
$cidInput.value = fileAdded.cid.toString()
253262

254263
resetProgress()
255264
await getFile()

examples/exchange-files-in-browser/test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const http = require('http')
34
const fs = require('fs-extra')
45
const path = require('path')
56
const os = require('os')
@@ -20,6 +21,8 @@ const {
2021
const pkg = require('./package.json')
2122
const webRTCStarSigServer = require('libp2p-webrtc-star/src/sig-server')
2223

24+
const FILE_CONTENT = 'A file with some content'
25+
2326
async function testUI (env) {
2427
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') ], {
2528
cwd: path.resolve(__dirname, '../'),
@@ -58,7 +61,7 @@ async function runTest () {
5861

5962
let cid
6063

61-
for await (const imported of relay.api.add('A file with some content')) {
64+
for await (const imported of relay.api.add(FILE_CONTENT)) {
6265
cid = imported.cid
6366
}
6467

@@ -186,5 +189,19 @@ module.exports[pkg.name] = function (browser) {
186189
// but should both see the added file
187190
browser.expect.element('#file-history').text.to.contain(process.env.IPFS_CID)
188191

192+
if (!process.env.IPFS_ADD_FILE) {
193+
// download the file from the other browser
194+
browser
195+
.getAttribute(`a[download=${process.env.IPFS_CID}]`, 'href', ({ value: url }) => {
196+
browser.executeAsync(function (url, done) {
197+
fetch(url)
198+
.then(res => res.text())
199+
.then(done, done)
200+
}, [ url ], ({ value: contents }) => {
201+
browser.expect(contents.toString('utf8')).to.equal(FILE_CONTENT)
202+
})
203+
})
204+
}
205+
189206
browser.end()
190207
}

0 commit comments

Comments
 (0)