Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/exchange-files-in-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
19 changes: 14 additions & 5 deletions examples/exchange-files-in-browser/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
'use strict'

const IPFS = require('ipfs')
const all = require('it-all')
const last = require('it-last')
const { Buffer } = IPFS

// Node
Expand Down Expand Up @@ -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: []
}
})

Expand Down Expand Up @@ -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'
}
Expand All @@ -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()
Expand Down
19 changes: 18 additions & 1 deletion examples/exchange-files-in-browser/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const http = require('http')
const fs = require('fs-extra')
const path = require('path')
const os = require('os')
Expand All @@ -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, '../'),
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}