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

Commit 93ad92d

Browse files
achingbrainAlan Shaw
authored and
Alan Shaw
committed
docs: almost all browser examples passing
1 parent 37864a8 commit 93ad92d

File tree

13 files changed

+72
-64
lines changed

13 files changed

+72
-64
lines changed

browser-add-readable-stream/index.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,29 @@ const createFiles = (directory) => {
3838
}]
3939
}
4040

41-
const streamFiles = (ipfs, directory, files) => new Promise((resolve, reject) => {
41+
const streamFiles = async (ipfs, directory, files) => {
4242
// Create a stream to write files to
43-
const stream = ipfs.addReadableStream()
43+
const stream = new ReadableStream({
44+
start(controller) {
45+
for (let i = 0; i < files.length; i++) {
46+
// Add the files one by one
47+
controller.enqueue(files[i])
48+
}
49+
50+
// When we have no more files to add, close the stream
51+
controller.close()
52+
}
53+
})
4454

45-
stream.on('data', (data) => {
55+
for await (const data of ipfs.add(stream)) {
4656
log(`Added ${data.path} hash: ${data.hash}`)
4757

4858
// The last data event will contain the directory hash
4959
if (data.path === directory) {
50-
resolve(data.hash)
60+
return data.cid
5161
}
52-
})
53-
54-
stream.on('error', reject)
55-
56-
// Add the files one by one
57-
files.forEach(file => stream.write(file))
58-
59-
// When we have no more files to add, close the stream
60-
stream.end()
61-
})
62+
}
63+
}
6264

6365
const log = (line) => {
6466
document.getElementById('output').appendChild(document.createTextNode(`${line}\r\n`))

browser-browserify/public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ <h1>JS IPFS - Add data to IPFS from the browser</h1>
1818
<button id="store">Add text to ipfs</button>
1919
<div id="output" style="display: none">
2020
<div>found in ipfs:</div>
21-
<div class="content" id="hash">[ipfs hash]</div>
21+
<div class="content" id="cid">[ipfs cid]</div>
2222
<div class="content" id="content">[ipfs content]</div>
2323
</div>
2424
</body>

browser-browserify/src/index.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ document.addEventListener('DOMContentLoaded', async () => {
99

1010
async function store () {
1111
const toStore = document.getElementById('source').value
12-
const result = await node.add(toStore)
1312

14-
for (const file of result) {
15-
if (file && file.hash) {
16-
console.log('successfully stored', file.hash)
13+
for await (const file of node.add(toStore)) {
14+
if (file && file.cid) {
15+
console.log('successfully stored', file.cid)
1716

18-
await display(file.hash)
17+
await display(file.cid)
1918
}
2019
}
2120
}
2221

23-
async function display (hash) {
24-
const data = await node.cat(hash)
25-
26-
document.getElementById('hash').innerText = hash
27-
document.getElementById('content').innerText = data
28-
document.getElementById('output').setAttribute('style', 'display: block')
22+
async function display (cid) {
23+
for await (const data of node.cat(cid)) {
24+
document.getElementById('cid').innerText = cid
25+
document.getElementById('content').innerText = data
26+
document.getElementById('output').setAttribute('style', 'display: block')
27+
}
2928
}
3029

3130
document.getElementById('store').onclick = store

browser-browserify/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
.click('#store')
1414
.waitForElementVisible('#output')
1515

16-
browser.expect.element('#hash').text.to.contain('QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX')
16+
browser.expect.element('#cid').text.to.contain('QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX')
1717
browser.expect.element('#content').text.to.contain('hello')
1818

1919
browser.end()

browser-create-react-app/src/hooks/use-ipfs-factory.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default function useIpfsFactory ({ commands }) {
2626
if (ipfs && ipfs.stop) {
2727
console.log('Stopping IPFS')
2828
ipfs.stop().catch(err => console.error(err))
29+
ipfs = null
2930
setIpfsReady(false)
3031
}
3132
}

browser-mfs/filetree.js

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

33
const {
4-
log,
54
createNode
65
} = require('./utils')
76

@@ -20,13 +19,7 @@ const loadFiles = async (ipfs, path) => {
2019
const output = {}
2120
path = path.replace(/\/\/+/g, '/')
2221

23-
const contents = await ipfs.files.ls(path, {
24-
long: true
25-
})
26-
.catch(error => log(error))
27-
28-
for (let i = 0; i < contents.length; i++) {
29-
let entry = contents[i]
22+
for await (const entry of ipfs.files.ls(path)) {
3023
output[entry.name] = entry
3124

3225
if (entry.type === FILE_TYPES.DIRECTORY) {

browser-parceljs/public/index.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ document.addEventListener('DOMContentLoaded', async () => {
2222

2323
log(`The IPFS node version is ${version.version}`)
2424

25-
const filesAdded = await node.add({
25+
for await (const entry of node.add({
2626
path: 'hello-parcel.txt',
2727
content: 'Hello from parcel.js bundled ipfs example'
28-
})
28+
})) {
29+
log(`This page deployed ${entry.path} to IPFS and its CID is ${entry.cid}`)
2930

30-
log(`This page deployed ${filesAdded[0].path} to IPFS and its hash is ${filesAdded[0].hash}`)
31+
const buffers = []
3132

32-
const fileBuffer = await node.cat(filesAdded[0].hash)
33+
for await (const buf of node.cat(entry.cid)) {
34+
buffers.push(buf)
35+
}
3336

34-
log(`The contents of the file was: ${fileBuffer.toString()}`)
37+
log(`The contents of the file was: ${Buffer.concat(buffers).toString()}`)
38+
}
3539
})

browser-readablestream/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<div id="container" ondrop="dropHandler(event)" ondragover="dragOverHandler(event)">
5656
<div id="form-wrapper">
5757
<form>
58-
<input type="text" id="hash" placeholder="Hash" disabled />
58+
<input type="text" id="cid" placeholder="CID" disabled />
5959
<button id="gobutton" disabled>Go!</button>
6060
</form>
6161
<video id="video" controls></video>

browser-readablestream/index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const Ipfs = require('../../')
66
const VideoStream = require('videostream')
7+
const toStream = require('it-to-stream')
78
const {
89
dragDrop,
910
statusMessages,
@@ -18,14 +19,14 @@ document.addEventListener('DOMContentLoaded', async () => {
1819

1920
// Set up event listeners on the <video> element from index.html
2021
const videoElement = createVideoElement()
21-
const hashInput = document.getElementById('hash')
22+
const cidInput = document.getElementById('cid')
2223
const goButton = document.getElementById('gobutton')
2324
let stream
2425

2526
goButton.onclick = function (event) {
2627
event.preventDefault()
2728

28-
log(`IPFS: Playing ${hashInput.value.trim()}`)
29+
log(`IPFS: Playing ${cidInput.value.trim()}`)
2930

3031
// Set up the video stream an attach it to our <video> element
3132
const videoStream = new VideoStream({
@@ -46,10 +47,10 @@ document.addEventListener('DOMContentLoaded', async () => {
4647
}
4748

4849
// This stream will contain the requested bytes
49-
stream = ipfs.catReadableStream(hashInput.value.trim(), {
50+
stream = toStream.readable(ipfs.cat(cidInput.value.trim(), {
5051
offset: start,
5152
length: end && end - start
52-
})
53+
}))
5354

5455
// Log error messages
5556
stream.on('error', (error) => log(error))
@@ -73,6 +74,6 @@ document.addEventListener('DOMContentLoaded', async () => {
7374
log('IPFS: Drop an .mp4 file into this window to add a file')
7475
log('IPFS: Then press the "Go!" button to start playing a video')
7576

76-
hashInput.disabled = false
77+
cidInput.disabled = false
7778
goButton.disabled = false
7879
})

browser-readablestream/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"webpack": "^4.28.4"
1818
},
1919
"dependencies": {
20-
"videostream": "^3.2.0"
20+
"videostream": "^3.2.0",
21+
"it-to-stream": "^0.1.1"
2122
}
2223
}

browser-readablestream/utils.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ const dragDrop = (ipfs) => {
3636
const files = Array.from(event.dataTransfer.items)
3737
.filter(item => item.kind === 'file')
3838
.map(item => item.getAsFile())
39-
40-
for (const file of files) {
41-
const progress = log(`IPFS: Adding ${file.name} 0%`)
42-
const added = await ipfs.add({
43-
path: file.name,
44-
content: file
45-
}, {
46-
progress: (addedBytes) => {
47-
progress.textContent = `IPFS: Adding ${file.name} ${parseInt((addedBytes / file.size) * 100)}%\r\n`
39+
.map(file => {
40+
return {
41+
path: file.name,
42+
content: file
4843
}
4944
})
5045

51-
const hash = added[0].hash
46+
const progress = log(`IPFS: Adding...`)
5247

53-
log(`IPFS: Added ${hash}`)
48+
for await (const added of ipfs.add(files, {
49+
progress: (addedBytes) => {
50+
progress.textContent = `IPFS: Adding ${addedBytes} bytes\r\n`
51+
}
52+
})) {
53+
log(`IPFS: Added ${added.cid}`)
5454

55-
document.querySelector('#hash').value = hash
55+
document.querySelector('#cid').value = added.cid.toString()
5656
}
5757

5858
if (event.dataTransfer.items && event.dataTransfer.items.clear) {

browser-video-streaming/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<html>
22
<body>
33
<video id="video" controls></video>
4-
<script src="https://unpkg.com/ipfs/dist/index.js"></script>
4+
<script src="../../dist/index.js"></script>
55
<script src="https://unpkg.com/hlsjs-ipfs-loader@0.1.4/dist/index.js"></script>
66
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
77
<script src="streaming.js"></script>

browser-webpack/src/components/app.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ class App extends React.Component {
3030

3131
this.setState({ id, agentVersion, protocolVersion })
3232

33-
const [{ hash }] = await node.add(stringToUse)
34-
this.setState({ addedFileHash: hash })
33+
for await (const { cid } of node.add(stringToUse)) {
34+
this.setState({ addedFileHash: cid.toString() })
3535

36-
const data = await node.cat(hash)
37-
this.setState({ addedFileContents: data.toString() })
36+
let bufs = []
37+
38+
for await (const buf of node.cat(cid)) {
39+
bufs.push(buf)
40+
}
41+
42+
const data = Buffer.concat(bufs)
43+
this.setState({ addedFileContents: data.toString('utf8') })
44+
}
3845
}
3946

4047
render () {

0 commit comments

Comments
 (0)