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

docs: updated examples to new api #2729

Merged
merged 56 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
5c2a71a
docs: updated examples to new api
achingbrain Jan 23, 2020
4597d88
chore: fix linting
achingbrain Jan 24, 2020
17e336c
chore: update deps
achingbrain Jan 24, 2020
e732978
chore: update gh urls
achingbrain Jan 24, 2020
732e786
chore: http gh urls maybe
achingbrain Jan 24, 2020
82326a3
chore: revert previous
achingbrain Jan 24, 2020
45a6f26
chore: use correct gh url
achingbrain Jan 24, 2020
ef65183
chore: update dep version
achingbrain Jan 24, 2020
f3e9c76
fix: fix failing tests
achingbrain Jan 24, 2020
2346488
chore: remove gh url
achingbrain Jan 24, 2020
a9ac189
docs: update module maintainers
achingbrain Jan 27, 2020
ba3998d
fix: returns string peer ids from all api methods
achingbrain Jan 27, 2020
3ce9db4
docs: update module maintainers
achingbrain Jan 27, 2020
7f79b69
chore: fix linter
achingbrain Jan 27, 2020
6597e37
chore: run pubsub room tests against master
achingbrain Jan 27, 2020
4a42063
fix: fix circuit relaying test
achingbrain Jan 28, 2020
7064f6b
fix: really fix circuit relay test
achingbrain Jan 28, 2020
cdc767d
wip: add debug
achingbrain Jan 28, 2020
a404a36
wip: debug only test job and restore gh versions
achingbrain Jan 28, 2020
6b0de9f
fix: fix exchange files in browser test
achingbrain Jan 28, 2020
0c30d05
chore: remove gh urls
achingbrain Jan 28, 2020
bc7e62d
wip: really force kill daemon
achingbrain Jan 28, 2020
a1d52de
chore: correct branch name
achingbrain Jan 28, 2020
92bc015
chore: make unhandled rejections fail tests
achingbrain Jan 28, 2020
dcf052f
chore: if debug is enabled do not treat stderr output as error
achingbrain Jan 28, 2020
b831a03
chore: skip silent test when debug log is turned on
achingbrain Jan 28, 2020
0c352f2
Merge remote-tracking branch 'origin/master' into refactor/async-exam…
achingbrain Jan 28, 2020
db52fba
chore: update deps
achingbrain Jan 29, 2020
41a738e
fix: peer ids are strings, do not stringify them
achingbrain Jan 29, 2020
b6988ee
fix: use gh url for ipfsd-ctl
achingbrain Jan 29, 2020
b5d76a3
fix: use version of interface tests without js-ipfs
achingbrain Jan 29, 2020
684b873
chore: remove debug env var
achingbrain Jan 30, 2020
2b6f0e9
fix: start webrtc sigserve during tests so browsers can dial things
achingbrain Jan 30, 2020
7b868a3
fix: include go-ipfs-dep for interop testing
achingbrain Jan 31, 2020
602f761
fix: specify ipfs js module for interop tests
achingbrain Jan 31, 2020
acd2409
fix: simplify interop test runs
achingbrain Jan 31, 2020
192d4fa
fix: update sigserver url
achingbrain Jan 31, 2020
2bb99f5
fix: use ws and not wss
achingbrain Jan 31, 2020
4f7fbe7
Update test/utils/factory.js
achingbrain Jan 31, 2020
2671356
chore: remove gh urls
achingbrain Jan 31, 2020
bf9ac59
fix: fix up ipfsd-ctl config for browser tests
achingbrain Jan 31, 2020
dd0eb4e
fix: increase dialer timeout in tests
achingbrain Jan 31, 2020
b0e2ee5
chore: enable libp2p debug for chrome build
achingbrain Jan 31, 2020
cbb6f24
chore: fail fast on browser builds
achingbrain Jan 31, 2020
5aa630d
chore: just run the failing tests
achingbrain Jan 31, 2020
6f7aca2
chore: split webworker tests out from browsers
achingbrain Feb 1, 2020
67e2bd8
fix: use go nodes with webworker interface tests
achingbrain Feb 2, 2020
75727b8
fix: use js for pubsub tests as before
achingbrain Feb 2, 2020
57277ef
fix: go-ipfs pubsub is no longer experimental
achingbrain Feb 3, 2020
e629d4a
fix: configure go-ipfs bin for ipfsd server
achingbrain Feb 3, 2020
b3cb06d
refactor: use string template
achingbrain Feb 3, 2020
30036f5
fix: update interface tests and remove only
achingbrain Feb 3, 2020
2b53ccb
chore: update ipfs-interop dep
Feb 3, 2020
e154bf5
fix: hls example
Feb 3, 2020
8f57ee3
fix: use libp2p with ping patch
achingbrain Feb 3, 2020
67e073d
chore: remove gh version
achingbrain Feb 3, 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
4 changes: 4 additions & 0 deletions examples/browser-video-streaming/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
<body>
<video id="video" controls></video>
<script src="../../dist/index.js"></script>
<!--
TODO: uncomment once https://github.com/ipfs/js-ipfs/pull/2683 and
https://github.com/moshisushi/hlsjs-ipfs-loader/pull/16 are released
<script src="https://unpkg.com/hlsjs-ipfs-loader@0.1.4/dist/index.js"></script>
-->
achingbrain marked this conversation as resolved.
Show resolved Hide resolved
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script src="streaming.js"></script>
</body>
Expand Down
143 changes: 142 additions & 1 deletion examples/browser-video-streaming/streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

/* global Hls Ipfs HlsjsIpfsLoader */
/* eslint-env browser */

document.addEventListener('DOMContentLoaded', async () => {
const testHash = 'QmdpAidwAsBGptFB3b6A9Pyi5coEbgjHrL3K2Qrsutmj9K'
const repoPath = 'ipfs-' + Math.random()
Expand All @@ -20,3 +19,145 @@ document.addEventListener('DOMContentLoaded', async () => {
hls.on(Hls.Events.MANIFEST_PARSED, () => video.play())
}
})

// TODO: remove everything below here once https://github.com/ipfs/js-ipfs/pull/2683
// and https://github.com/moshisushi/hlsjs-ipfs-loader/pull/16 are released
achingbrain marked this conversation as resolved.
Show resolved Hide resolved

class HlsjsIpfsLoader {
constructor(config) {
this.ipfs = config.ipfs
this.hash = config.ipfsHash
if (config.debug === false) {
this.debug = function() {}
} else if (config.debug === true) {
this.debug = console.log
} else {
this.debug = config.debug
}
if(config.m3u8provider) {
this.m3u8provider = config.m3u8provider;
} else {
this.m3u8provider = null;
}
if(config.tsListProvider) {
this.tsListProvider = config.tsListProvider;
} else {
this.tsListProvider = null;
}
}

destroy() {
}

abort() {
}

load(context, config, callbacks) {
this.context = context
this.config = config
this.callbacks = callbacks
this.stats = { trequest: performance.now(), retry: 0 }
this.retryDelay = config.retryDelay
this.loadInternal()
}
/**
* Call this by getting the HLSIPFSLoader instance from hls.js hls.coreComponents[0].loaders.manifest.setM3U8Provider()
* @param {function} provider
*/
setM3U8Provider(provider) {
this.m3u8provider = provider;
}
/**
*
* @param {function} provider
*/
setTsListProvider(provider) {
this.tsListProvider = provider;
}

loadInternal() {
const { stats, context, callbacks } = this

stats.tfirst = Math.max(performance.now(), stats.trequest)
stats.loaded = 0

const urlParts = context.url.split("/")
const filename = urlParts[urlParts.length - 1]

if(filename.split(".")[1] === "m3u8" && this.m3u8provider !== null) {
const res = this.m3u8provider();
let data;
if(Buffer.isBuffer(res)) {
data = buf2str(res)
} else {
data = res;
}
const response = { url: context.url, data: data }
callbacks.onSuccess(response, stats, context)
return;
}
if(filename.split(".")[1] === "m3u8" && this.tsListProvider !== null) {
var tslist = this.tsListProvider();
var hash = tslist[filename];
if(hash) {
this.cat(hash).then(res => {
let data;
if(Buffer.isBuffer(res)) {
data = buf2str(res)
} else {
data = res;
}
stats.loaded = stats.total = data.length
stats.tload = Math.max(stats.tfirst, performance.now())
const response = { url: context.url, data: data }
callbacks.onSuccess(response, stats, context)
});
}
return;
}
getFile(this.ipfs, this.hash, filename, this.debug).then(res => {
const data = (context.responseType === 'arraybuffer') ? res : buf2str(res)
stats.loaded = stats.total = data.length
stats.tload = Math.max(stats.tfirst, performance.now())
const response = { url: context.url, data: data }
callbacks.onSuccess(response, stats, context)
}, console.error)
}
}

async function getFile(ipfs, rootHash, filename, debug) {
debug(`Fetching hash for '${rootHash}/${filename}'`)
if(filename === null) {
return cat(rootHash, ipfs, debug)
}

for await (const link of ipfs.ls(rootHash)) {
if (link.name !== filename) {
continue
}

debug(`Requesting '${link.path}'`)

return cat(link.cid, ipfs, debug)
}

throw new Error(`File not found: ${rootHash}/${filename}`)
}

function buf2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf))
}

async function cat (cid, ipfs, debug) {
let value = new Uint8Array(0)

for await (const buf of ipfs.cat(cid)) {
const newBuf = new Uint8Array(value.length + buf.length)
newBuf.set(value)
newBuf.set(buf, value.length)
value = newBuf
}

debug(`Received data for file '${cid}' size: ${value.length}`)
return value
}
2 changes: 1 addition & 1 deletion examples/circuit-relaying/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"license": "MIT",
"dependencies": {
"ipfs": "file:../../",
"ipfs-pubsub-room": "^1.4.0"
"ipfs-pubsub-room": "^2.0.0"
},
"devDependencies": {
"aegir": "^20.0.0",
Expand Down
6 changes: 3 additions & 3 deletions examples/circuit-relaying/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const mkRoomName = (name) => {

module.exports = (ipfs, peersSet) => {
const createRoom = (name) => {
const room = Room(ipfs, mkRoomName(name))
const room = new Room(ipfs, mkRoomName(name))

room.on('peer joined', (peer) => {
console.log('peer ' + peer + ' joined')
Expand All @@ -31,9 +31,9 @@ module.exports = (ipfs, peersSet) => {

// send and receive messages
room.on('message', (message) => {
console.log('got message from ' + message.from + ': ' + message.data.toString())
console.log('got message from ' + message.from.toString() + ': ' + message.data.toString())
const node = document.createElement(`li`)
node.innerText = `${message.from.substr(-4)}: ${message.data.toString()}`
node.innerText = `${message.from.toString().substr(-4)}: ${message.data.toString()}`
$msgs.appendChild(node)
})

Expand Down
6 changes: 3 additions & 3 deletions examples/circuit-relaying/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function runTest () {

try {
const id = await ipfsd.api.id()
const address = id.addresses.filter(addr => addr.includes('/ws/ipfs/Qm')).pop()
const address = id.addresses.filter(addr => addr.includes('/ws/p2p/Qm')).pop()

if (!address) {
throw new Error(`Could not find web socket address in ${id.addresses}`)
Expand Down Expand Up @@ -145,8 +145,8 @@ module.exports[pkg.name] = function (browser) {
.pause(1000)
.click('#send')

browser.expect.element('#msgs').text.to.contain(`${remotePeerId.substr(-4)}: hello`)
browser.expect.element('#msgs').text.to.contain(`${localPeerId.substr(-4)}: hello`)
browser.expect.element('#msgs').text.to.contain(`${remotePeerId.toString().substr(-4)}: hello`)
browser.expect.element('#msgs').text.to.contain(`${localPeerId.toString().substr(-4)}: hello`)
})

browser.end()
Expand Down
20 changes: 12 additions & 8 deletions examples/custom-ipfs-repo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,23 @@ async function main () {
console.log('Version:', version)

// Once we have the version, let's add a file to IPFS
const filesAdded = await node.add({
for await (const file of node.add({
path: 'test-data.txt',
content: Buffer.from('We are using a customized repo!')
})
})) {
// Log out the added files metadata and cat the file from IPFS
console.log('\nAdded file:', file.path, file.cid)

// Log out the added files metadata and cat the file from IPFS
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
const bufs = []

const data = await node.cat(filesAdded[0].hash)
for await (const buf of node.cat(file.cid)) {
bufs.push(buf)
}

// Print out the files contents to console
console.log('\nFetched file content:')
process.stdout.write(data)
// Print out the files contents to console
console.log('\nFetched file content:')
process.stdout.write(Buffer.concat(bufs))
}

// After everything is done, shut the node down
console.log('\n\nStopping the node')
Expand Down
16 changes: 4 additions & 12 deletions examples/custom-libp2p/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

const Libp2p = require('libp2p')
const IPFS = require('ipfs')
const IPFS = require('../../')
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WebSocketStar = require('libp2p-websocket-star')
const Bootstrap = require('libp2p-bootstrap')
const SPDY = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const MPLEX = require('pull-mplex')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')

/**
Expand All @@ -32,11 +31,6 @@ const libp2pBundle = (opts) => {
const peerBook = opts.peerBook
const bootstrapList = opts.config.Bootstrap

// Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node
const wsstar = new WebSocketStar({
id: peerInfo.id
})

// Build and return our libp2p node
return new Libp2p({
peerInfo,
Expand All @@ -49,8 +43,7 @@ const libp2pBundle = (opts) => {
},
modules: {
transport: [
TCP,
wsstar
TCP
],
streamMuxer: [
MPLEX,
Expand All @@ -61,8 +54,7 @@ const libp2pBundle = (opts) => {
],
peerDiscovery: [
MulticastDNS,
Bootstrap,
wsstar.discovery
Bootstrap
],
dht: KadDHT
},
Expand Down
21 changes: 10 additions & 11 deletions examples/custom-libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
},
"license": "MIT",
"dependencies": {
"ipfs": "file:../../",
"libp2p": "^0.26.2",
"libp2p-bootstrap": "~0.9.7",
"libp2p-kad-dht": "~0.16.0",
"libp2p-mdns": "~0.12.2",
"libp2p-secio": "~0.11.1",
"libp2p-spdy": "~0.13.3",
"libp2p-tcp": "~0.13.0",
"libp2p-websocket-star": "~0.10.2",
"pull-mplex": "~0.1.0"
"libp2p": "^0.27.0-rc.0",
"libp2p-bootstrap": "^0.10.3",
"libp2p-kad-dht": "^0.18.3",
"libp2p-mdns": "^0.13.1",
"libp2p-mplex": "^0.9.3",
"libp2p-secio": "^0.12.2",
"libp2p-spdy": "^0.13.3",
"libp2p-tcp": "^0.14.3",
"libp2p-webrtc-star": "^0.17.2"
},
"devDependencies": {
"execa": "^3.2.0"
"execa": "^4.0.0"
}
}
7 changes: 2 additions & 5 deletions examples/custom-libp2p/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ const execa = require('execa')
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const SPDY = require('libp2p-spdy')
const MPLEX = require('pull-mplex')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const promisify = require('promisify-es6')
const PeerBook = require('peer-book')

async function test () {
let output = ''
Expand All @@ -31,12 +29,11 @@ async function test () {

console.info('Dialling', address)

const peerInfo = new PeerInfo(await promisify(PeerId.create)())
const peerInfo = new PeerInfo(await PeerId.create())
peerInfo.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/0'))

const libp2p = new Libp2p({
peerInfo,
peerBook: new PeerBook(),
modules: {
transport: [
TCP
Expand Down
Loading