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

Commit 2daef3d

Browse files
committed
docs: bring examples back
1 parent 660d3db commit 2daef3d

File tree

207 files changed

+8194
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+8194
-17
lines changed

.travis.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
include:
6363
- stage: check
6464
script:
65-
- npm run build -- $RUN_SINCE --scope={ipfs,ipfs-http-client} -- -- --bundlesize
65+
- npm run build -- $RUN_SINCE --scope={ipfs,ipfs-http-client}
6666
- npm run dep-check -- $RUN_SINCE -- -- -- -i electron-webrtc
6767
- npm run lint -- $RUN_SINCE --concurrency 1
6868

@@ -128,6 +128,14 @@ jobs:
128128
script:
129129
- npm run test:interop:electron-renderer -- $RUN_SINCE -- -- --bail --timeout 10000
130130

131+
- stage: test
132+
name: examples
133+
script:
134+
- npx json -I -f ./lerna.json -e "this.packages.push('examples/*')"
135+
- npm run reset
136+
- npm install
137+
- npm run test -- --scope=example* --concurrency=1
138+
131139
- stage: release-rc-npm
132140
# only run on changes to master
133141
if: branch = master AND type = push AND fork = false
@@ -208,12 +216,5 @@ jobs:
208216
script:
209217
- npm run test:external -- -- -- https://github.com/decentralized-identity/sidetree.git --deps=ipfs@next
210218

211-
- stage: test-external
212-
# only run on changes to master
213-
if: branch = master AND type = push AND fork = false
214-
name: external - examples
215-
script:
216-
- npm run test:external -- -- -- https://github.com/ipfs-shipyard/js-ipfs-examples.git --deps=ipfs@next --deps=ipfs-http-client@next
217-
218219
notifications:
219220
email: false

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ We've come a long way, but this project is still in Alpha, lots of development i
2626

2727
## Getting started
2828

29-
* Look into the [examples repo](https://github.com/ipfs-shipyard/js-ipfs-examples) to learn how to spawn an IPFS node in Node.js and in the Browser
29+
* Look into the [examples](/examples) to learn how to spawn an IPFS node in Node.js and in the Browser
3030
* Read the [Core API docs](https://github.com/ipfs/js-ipfs/tree/master/docs/core-api) to see what you can do with an IPFS node
3131
* Visit https://dweb-primer.ipfs.io to learn about IPFS and the concepts that underpin it
3232
* Head over to https://proto.school to take interactive tutorials that cover core IPFS APIs
@@ -47,7 +47,7 @@ We've come a long way, but this project is still in Alpha, lots of development i
4747
## Documentation
4848

4949
* [Core API](./docs/core-api)
50-
* [Examples](https://github.com/ipfs-shipyard/js-ipfs-examples)
50+
* [Examples](./examples)
5151
* [Development](./docs/DEVELOPMENT.md)
5252

5353
## Structure

docs/BROWSERS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ document.addEventListener('DOMContentLoaded', async () => {
7171
</script>
7272
```
7373

74-
More advanced examples and tutorials can be found in the [examples repo](https://github.com/ipfs-shipyard/js-ipfs-examples)
74+
More advanced examples and tutorials can be found in the [examples](../examples)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5+
6+
## [2.0.1](https://github.com/ipfs/js-ipfs/compare/example-browser-add-readable-stream@[email protected]) (2020-04-08)
7+
8+
**Note:** Version bump only for package example-browser-add-readable-stream
9+
10+
11+
12+
13+
14+
# 2.0.0 (2020-03-31)
15+
16+
17+
### Bug Fixes
18+
19+
* examples after files API refactor ([#1740](https://github.com/ipfs/js-ipfs/issues/1740)) ([34ec036](https://github.com/ipfs/js-ipfs/commit/34ec036b0df9563a014c1348f0a056c1f98aadad))
20+
21+
22+
### Code Refactoring
23+
24+
* export types and utilities statically ([#1908](https://github.com/ipfs/js-ipfs/issues/1908)) ([79d7fef](https://github.com/ipfs/js-ipfs/commit/79d7fef7d28c0e0405fb69af149ff09681ac4273))
25+
26+
27+
### BREAKING CHANGES
28+
29+
* `ipfs.util.isIPFS` and `ipfs.util.crypto` have moved to static exports and should be accessed via `const { isIPFS, crypto } = require('ipfs')`.
30+
31+
The modules available under `ipfs.types.*` have also become static exports.
32+
33+
License: MIT
34+
Signed-off-by: Alan Shaw <[email protected]>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Using duplex streams to add files to IPFS in the browser
2+
3+
If you have a number of files that you'd like to add to IPFS and end up with a hash representing the directory containing your files, you can invoke [`ipfs.add`](https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/SPEC/FILES.md#add) with an array of objects.
4+
5+
But what if you don't know how many there will be in advance? You can add multiple files to a directory in IPFS over time by using [`ipfs.addReadableStream`](https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/SPEC/FILES.md#addreadablestream).
6+
7+
## Before you start
8+
9+
First clone this repo, install dependencies in the project root and build the project.
10+
11+
```console
12+
$ git clone https://github.com/ipfs/js-ipfs.git
13+
$ cd js-ipfs
14+
$ npm install
15+
$ npm run build
16+
```
17+
18+
## Running the example
19+
20+
See [`index.js`](./index.js) for a working example and open [`index.html`](./index.html) in your browser to see it run.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Add readable stream</title>
5+
</head>
6+
<body>
7+
<pre id="output"></pre>
8+
<script src="../../node_modules/ipfs/dist/index.min.js"></script>
9+
<script src="index.js"></script>
10+
</body>
11+
</html>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict'
2+
3+
/* global Ipfs */
4+
/* eslint-env browser */
5+
6+
const main = async () => {
7+
const repoPath = `ipfs-${Math.random()}`
8+
const ipfs = await Ipfs.create({ repo: repoPath })
9+
10+
const directoryName = 'directory'
11+
12+
// Our list of files
13+
const inputFiles = createFiles(directoryName)
14+
15+
const directoryHash = await streamFiles(ipfs, directoryName, inputFiles)
16+
17+
const fileList = await ipfs.ls(directoryHash)
18+
19+
log(`\n--\n\nDirectory contents:\n\n${directoryName}/ ${directoryHash}`)
20+
21+
fileList.forEach((file, index) => {
22+
log(` ${index < fileList.length - 1 ? '\u251C' : '\u2514'}\u2500 ${file.name} ${file.path} ${file.hash}`)
23+
})
24+
}
25+
26+
const createFiles = (directory) => {
27+
return [{
28+
path: `${directory}/file1.txt`,
29+
30+
// content could be a stream, a url, a Buffer, a File etc
31+
content: 'one'
32+
}, {
33+
path: `${directory}/file2.txt`,
34+
content: 'two'
35+
}, {
36+
path: `${directory}/file3.txt`,
37+
content: 'three'
38+
}]
39+
}
40+
41+
const streamFiles = async (ipfs, directory, files) => {
42+
// Create a stream to write files to
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+
})
54+
55+
for await (const data of ipfs.add(stream)) {
56+
log(`Added ${data.path} hash: ${data.hash}`)
57+
58+
// The last data event will contain the directory hash
59+
if (data.path === directory) {
60+
return data.cid
61+
}
62+
}
63+
}
64+
65+
const log = (line) => {
66+
document.getElementById('output').appendChild(document.createTextNode(`${line}\r\n`))
67+
}
68+
69+
main()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "example-browser-add-readable-stream",
3+
"description": "How to add readable streams in the browser",
4+
"version": "2.0.1",
5+
"main": "index.js",
6+
"private": true,
7+
"scripts": {
8+
"serve": "http-server public -a 127.0.0.1 -p 8888",
9+
"start": "npm run serve",
10+
"test": "test-ipfs-example"
11+
},
12+
"keywords": [],
13+
"license": "MIT",
14+
"devDependencies": {
15+
"ipfs": "^0.46.0",
16+
"test-ipfs-example": "^2.0.3"
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
module.exports = {
4+
'Add readable stream example': function (browser) {
5+
browser
6+
.url(process.env.IPFS_EXAMPLE_TEST_URL)
7+
.waitForElementVisible('#output')
8+
9+
browser.expect.element('#output').text.to.contain('directory/ QmVgJePRxp1vhRxDcJWdmuFGfUB5S5RYTtG1NR3bQM4BBn')
10+
11+
browser.end()
12+
}
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5+
6+
## [1.1.1](https://github.com/ipfs/js-ipfs/compare/[email protected]@1.1.1) (2020-04-08)
7+
8+
**Note:** Version bump only for package example-browser-browserify
9+
10+
11+
12+
13+
14+
# 1.1.0 (2020-03-31)
15+
16+
17+
### Bug Fixes
18+
19+
* examples after files API refactor ([#1740](https://github.com/ipfs/js-ipfs/issues/1740)) ([34ec036](https://github.com/ipfs/js-ipfs/commit/34ec036b0df9563a014c1348f0a056c1f98aadad))
20+
21+
22+
### Features
23+
24+
* implementing the new streaming interfaces ([#1086](https://github.com/ipfs/js-ipfs/issues/1086)) ([2c4b8b3](https://github.com/ipfs/js-ipfs/commit/2c4b8b325b94d4506b87441f06c5d29bb6f37f72))

examples/browser-browserify/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Bundle js-ipfs with Browserify!
2+
3+
In this example, you will find a boilerplate you can use to guide yourself into bundling js-ipfs with [browserify](http://browserify.org/), so that you can use it in your own web app!
4+
5+
## Before you start
6+
7+
First clone this repo, install dependencies in the project root and build the project.
8+
9+
```console
10+
$ git clone https://github.com/ipfs/js-ipfs.git
11+
$ cd js-ipfs
12+
$ npm install
13+
$ npm run build
14+
```
15+
16+
## Running the example
17+
18+
In this directory run:
19+
20+
```bash
21+
> npm start
22+
```
23+
24+
Now open your browser at `http://localhost:8888`
25+
26+
You should see the following:
27+
28+
![](https://ipfs.io/ipfs/QmNtpcWCEd6LjdPNfBFDaVZdD4jpgT8ZTAwoFJXKhYMJdo/1.png)
29+
![](https://ipfs.io/ipfs/QmNtpcWCEd6LjdPNfBFDaVZdD4jpgT8ZTAwoFJXKhYMJdo/2.png)

examples/browser-browserify/img/1.png

98.2 KB
Loading

examples/browser-browserify/img/2.png

143 KB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "example-browser-browserify",
3+
"description": "Bundle js-ipfs with Browserify",
4+
"version": "1.1.1",
5+
"main": "index.js",
6+
"private": true,
7+
"scripts": {
8+
"bundle": "browserify src/index.js > public/bundle.js",
9+
"serve": "http-server public -a 127.0.0.1 -p 8888",
10+
"start": "npm run bundle && npm run serve",
11+
"clean": "rm -rf public/bundle.js",
12+
"test": "test-ipfs-example"
13+
},
14+
"keywords": [],
15+
"license": "MIT",
16+
"devDependencies": {
17+
"browserify": "^16.2.3",
18+
"concat-stream": "^2.0.0",
19+
"execa": "^4.0.0",
20+
"http-server": "^0.11.1",
21+
"ipfs": "^0.46.0",
22+
"test-ipfs-example": "^2.0.3"
23+
},
24+
"browser": {
25+
"ipfs": "ipfs/dist/index.min.js"
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<title>js-ipfs example</title>
6+
<script src="bundle.js"></script>
7+
<style>
8+
.content {
9+
border: 1px solid black;
10+
padding: 10px;
11+
margin: 5px 0;
12+
}
13+
</style>
14+
</head>
15+
<body>
16+
<h1>JS IPFS - Add data to IPFS from the browser</h1>
17+
<textarea id="source" placeholder="Enter some text here"></textarea>
18+
<button id="store">Add text to ipfs</button>
19+
<div id="output" style="display: none">
20+
<div>found in ipfs:</div>
21+
<div class="content" id="cid">[ipfs cid]</div>
22+
<div class="content" id="content">[ipfs content]</div>
23+
</div>
24+
</body>
25+
</html>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
const IPFS = require('ipfs')
4+
5+
document.addEventListener('DOMContentLoaded', async () => {
6+
const node = await IPFS.create({ repo: String(Math.random() + Date.now()) })
7+
8+
console.log('IPFS node is ready')
9+
10+
async function store () {
11+
const toStore = document.getElementById('source').value
12+
13+
for await (const file of node.add(toStore)) {
14+
if (file && file.cid) {
15+
console.log('successfully stored', file.cid)
16+
17+
await display(file.cid)
18+
}
19+
}
20+
}
21+
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+
}
28+
}
29+
30+
document.getElementById('store').onclick = store
31+
})

examples/browser-browserify/test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const pkg = require('./package.json')
4+
5+
module.exports = {
6+
[pkg.name]: function (browser) {
7+
browser
8+
.url(process.env.IPFS_EXAMPLE_TEST_URL)
9+
.waitForElementVisible('#source')
10+
.setValue('#source', 'hello')
11+
.waitForElementVisible('#store')
12+
.pause(1000)
13+
.click('#store')
14+
.waitForElementVisible('#output')
15+
16+
browser.expect.element('#cid').text.to.contain('QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX')
17+
browser.expect.element('#content').text.to.contain('hello')
18+
19+
browser.end()
20+
}
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# required because react-scripts scans *up* the tree from this project and finds
2+
# a conflicting version of eslint in the node_modules dir for js-ipfs.
3+
SKIP_PREFLIGHT_CHECK=true

0 commit comments

Comments
 (0)