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

refactor: async await roundup #2683

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
fadff95
refactor: async iterables
Dec 17, 2019
af678ae
fix: many fixes
Jan 14, 2020
8fe9441
refactor: object core tests
Jan 14, 2020
ee92d29
refactor: pin core tests
Jan 14, 2020
584b27f
chore: appease linter
Jan 14, 2020
8e31365
refactor: pin-set core tests
Jan 14, 2020
8806741
refactor: core swarm tests
Jan 14, 2020
43deb73
refactor: ping core tests
Jan 14, 2020
7123eb9
refactor: preload core tests
Jan 14, 2020
ffc37a6
fix: pubsub core tests
Jan 14, 2020
c53aa25
chore: cleanups
Jan 14, 2020
008b9ef
fix: no skip at describe
Jan 14, 2020
85a3378
docs: update README
Jan 15, 2020
d6176bc
refactor: ipns core tests
Jan 15, 2020
1d232ce
chore: remove .only
Jan 15, 2020
adb0040
fix: resolvePath tests
Jan 15, 2020
f6ae1d2
chore: appease linter
Jan 15, 2020
014ffbd
fix: circuit relay test
Jan 15, 2020
a968522
fix: move skip to the right place
Jan 15, 2020
6a288f1
fix: error message
Jan 15, 2020
fecc9a6
feat: add error code to no link error
Jan 15, 2020
a3a30aa
fix: remove tests for functionality that was removed
Jan 15, 2020
15fc5ba
fix: preload test
Jan 15, 2020
9f82e6f
chore: debug CI issue
Jan 15, 2020
33e5ea7
chore: consistent rejected()
Jan 15, 2020
eb317ac
fix: more test fixes
Jan 15, 2020
d001348
fix: some fixes for browser tests
Jan 16, 2020
22a14fd
refactor: convert dht API to async await
Jan 16, 2020
e48a952
feat: always log the error for unexpected command failures
Jan 16, 2020
32c4e06
fix: apply profiles before any passed config
Jan 16, 2020
d34035e
fix: appease linter
Jan 16, 2020
c1f4364
fix: unhandledPromiseRejections from pubsub tests
Jan 16, 2020
8e051d0
fix: oops fix error logging
Jan 16, 2020
28bba4b
fix: ensure options exists
Jan 16, 2020
ce9b0b7
docs: add globSource and urlSource docs
Jan 16, 2020
fb40e7b
fix: swarm addrs for browsre nodes
Jan 16, 2020
fbbb43d
fix: remove unnecessary skips
Jan 16, 2020
d5f4873
chore: update ipfsd-ctl dep
Jan 16, 2020
5d5a3f7
fix: repoOwner default value
Jan 16, 2020
7cf5b48
docs: almost all browser examples passing
achingbrain Jan 16, 2020
4d2ce32
fix: increase timings for slow CI
Jan 17, 2020
ae1108d
fix: windows test fixes
Jan 17, 2020
d12a061
refactor: return CID instances from block.rm
Jan 17, 2020
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
124 changes: 92 additions & 32 deletions README.md

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions examples/browser-add-readable-stream/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`))
Expand Down
2 changes: 1 addition & 1 deletion examples/browser-browserify/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h1>JS IPFS - Add data to IPFS from the browser</h1>
<button id="store">Add text to ipfs</button>
<div id="output" style="display: none">
<div>found in ipfs:</div>
<div class="content" id="hash">[ipfs hash]</div>
<div class="content" id="cid">[ipfs cid]</div>
<div class="content" id="content">[ipfs content]</div>
</div>
</body>
Expand Down
21 changes: 10 additions & 11 deletions examples/browser-browserify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/browser-browserify/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
9 changes: 1 addition & 8 deletions examples/browser-mfs/filetree.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const {
log,
createNode
} = require('./utils')

Expand All @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions examples/browser-parceljs/public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`)
}
})
2 changes: 1 addition & 1 deletion examples/browser-readablestream/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<div id="container" ondrop="dropHandler(event)" ondragover="dragOverHandler(event)">
<div id="form-wrapper">
<form>
<input type="text" id="hash" placeholder="Hash" disabled />
<input type="text" id="cid" placeholder="CID" disabled />
<button id="gobutton" disabled>Go!</button>
</form>
<video id="video" controls></video>
Expand Down
11 changes: 6 additions & 5 deletions examples/browser-readablestream/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const Ipfs = require('../../')
const VideoStream = require('videostream')
const toStream = require('it-to-stream')
const {
dragDrop,
statusMessages,
Expand All @@ -18,14 +19,14 @@ document.addEventListener('DOMContentLoaded', async () => {

// Set up event listeners on the <video> element from index.html
const videoElement = createVideoElement()
const hashInput = document.getElementById('hash')
const cidInput = document.getElementById('cid')
const goButton = document.getElementById('gobutton')
let stream

goButton.onclick = function (event) {
event.preventDefault()

log(`IPFS: Playing ${hashInput.value.trim()}`)
log(`IPFS: Playing ${cidInput.value.trim()}`)

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

// This stream will contain the requested bytes
stream = ipfs.catReadableStream(hashInput.value.trim(), {
stream = toStream.readable(ipfs.cat(cidInput.value.trim(), {
offset: start,
length: end && end - start
})
}))

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

hashInput.disabled = false
cidInput.disabled = false
goButton.disabled = false
})
3 changes: 2 additions & 1 deletion examples/browser-readablestream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"webpack": "^4.28.4"
},
"dependencies": {
"videostream": "^3.2.0"
"videostream": "^3.2.0",
"it-to-stream": "^0.1.1"
}
}
24 changes: 12 additions & 12 deletions examples/browser-readablestream/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ const dragDrop = (ipfs) => {
const files = Array.from(event.dataTransfer.items)
.filter(item => item.kind === 'file')
.map(item => item.getAsFile())

for (const file of files) {
const progress = log(`IPFS: Adding ${file.name} 0%`)
const added = await ipfs.add({
path: file.name,
content: file
}, {
progress: (addedBytes) => {
progress.textContent = `IPFS: Adding ${file.name} ${parseInt((addedBytes / file.size) * 100)}%\r\n`
.map(file => {
return {
path: file.name,
content: file
}
})

const hash = added[0].hash
const progress = log(`IPFS: Adding...`)

log(`IPFS: Added ${hash}`)
for await (const added of ipfs.add(files, {
progress: (addedBytes) => {
progress.textContent = `IPFS: Adding ${addedBytes} bytes\r\n`
}
})) {
log(`IPFS: Added ${added.cid}`)

document.querySelector('#hash').value = hash
document.querySelector('#cid').value = added.cid.toString()
}

if (event.dataTransfer.items && event.dataTransfer.items.clear) {
Expand Down
2 changes: 1 addition & 1 deletion examples/browser-video-streaming/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<body>
<video id="video" controls></video>
<script src="https://unpkg.com/ipfs/dist/index.js"></script>
<script src="../../dist/index.js"></script>
<script src="https://unpkg.com/hlsjs-ipfs-loader@0.1.4/dist/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script src="streaming.js"></script>
Expand Down
15 changes: 11 additions & 4 deletions examples/browser-webpack/src/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ class App extends React.Component {

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

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

const data = await node.cat(hash)
this.setState({ addedFileContents: data.toString() })
let bufs = []

for await (const buf of node.cat(cid)) {
bufs.push(buf)
}

const data = Buffer.concat(bufs)
this.setState({ addedFileContents: data.toString('utf8') })
}
}

render () {
Expand Down
Loading