This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
530767d
commit eff4073
Showing
67 changed files
with
2,243 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# JS IPFS API - Example Browser - Name | ||
|
||
## Setup | ||
|
||
```sh | ||
npm install -g ipfs | ||
jsipfs init | ||
# Configure CORS to allow ipfs-http-client to access this IPFS node | ||
jsipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://127.0.0.1:8888"]' | ||
# Start the IPFS node | ||
jsipfs daemon | ||
``` | ||
|
||
Then in this folder run | ||
|
||
```bash | ||
> npm install | ||
> npm start | ||
``` | ||
|
||
and open your browser at `http://127.0.0.1:8888`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>JS IPFS Client example</title> | ||
<style> | ||
.hidden { | ||
opacity: 0; | ||
} | ||
|
||
form { | ||
padding-bottom: 1em; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<h1>ipfs-client</h1> | ||
<form id="connect-to-api"> | ||
<h3>Enter IPFS API details</h3> | ||
<label for="grpc-input"> | ||
GRPC: | ||
<input id="grpc-input" name="grpc-input" type="text" value="/ip4/127.0.0.1/tcp/5003" required> | ||
</label> | ||
<label for="http-input"> | ||
HTTP: | ||
<input id="http-input" name="text" type="text" value="/ip4/127.0.0.1/tcp/5001" required> | ||
</label> | ||
<button id="connect-submit" type="submit">Connect</button> | ||
</form> | ||
<div id="output"> | ||
</div> | ||
|
||
<script src="index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* eslint-disable no-console */ | ||
'use strict' | ||
|
||
const ipfsClient = require('ipfs-client') | ||
let ipfs | ||
|
||
const COLORS = { | ||
active: 'blue', | ||
success: 'green', | ||
error: 'red' | ||
} | ||
|
||
const showStatus = (text, bg) => { | ||
console.info(text) | ||
|
||
const log = document.getElementById('output') | ||
|
||
if (!log) { | ||
return | ||
} | ||
|
||
const line = document.createElement('p') | ||
line.innerText = text | ||
line.style.color = bg | ||
|
||
log.appendChild(line) | ||
} | ||
|
||
async function * streamFiles () { | ||
for (let i = 0; i < 100; i++) { | ||
await new Promise((resolve) => { | ||
setTimeout(() => resolve(), 100) | ||
}) | ||
|
||
showStatus(`Sending /file-${i}.txt`, COLORS.active) | ||
|
||
yield { | ||
path: `/file-${i}.txt`, | ||
content: `file ${i}` | ||
} | ||
} | ||
} | ||
|
||
async function main (grpcApi, httpApi) { | ||
showStatus(`Connecting to ${grpcApi} using ${httpApi} as fallback`, COLORS.active) | ||
|
||
ipfs = ipfsClient({ | ||
grpc: grpcApi, | ||
http: httpApi | ||
}) | ||
|
||
const id = await ipfs.id() | ||
showStatus(`Daemon active\nID: ${id.id}`, COLORS.success) | ||
|
||
for await (const file of ipfs.addAll(streamFiles(), { | ||
wrapWithDirectory: true, | ||
// this is just to show the interleaving of uploads and progress events | ||
// otherwise we'd have to upload 50 files before we see any response from | ||
// the server. do not specify this so low in production as you'll have | ||
// greatly degraded import performance | ||
fileImportConcurrency: 1, | ||
progress: (bytes, file) => { | ||
showStatus(`File progress ${file} ${bytes}`, COLORS.active) | ||
} | ||
})) { | ||
showStatus(`Added file: ${file.path} ${file.cid}`, COLORS.success) | ||
} | ||
|
||
showStatus('Finished!', COLORS.success) | ||
} | ||
|
||
// Event listeners | ||
document.getElementById('connect-submit').onclick = (e) => { | ||
e.preventDefault() | ||
|
||
main(document.getElementById('grpc-input').value, document.getElementById('http-input').value) | ||
.catch(err => { | ||
showStatus(err.message, COLORS.error) | ||
console.error(err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "example-ipfs-client-add-files", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"private": true, | ||
"scripts": { | ||
"clean": "rm -rf ./dist", | ||
"build": "parcel build index.html --public-url '.'", | ||
"start": "parcel index.html -p 8888", | ||
"test": "test-ipfs-example" | ||
}, | ||
"dependencies": { | ||
"ipfs-client": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"execa": "^4.0.3", | ||
"ipfs": "^0.52.0", | ||
"ipfsd-ctl": "ipfs/js-ipfsd-ctl#feat/expose-grpc-addr", | ||
"parcel-bundler": "^1.12.4", | ||
"test-ipfs-example": "^2.0.3" | ||
}, | ||
"browserslist": [ | ||
"last 2 versions and not dead and > 2%" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
const execa = require('execa') | ||
const { createFactory } = require('ipfsd-ctl') | ||
const df = createFactory({ | ||
ipfsClientModule: require('ipfs-client'), | ||
ipfsBin: require.resolve('ipfs/src/cli.js') | ||
}) | ||
const { | ||
startServer | ||
} = require('test-ipfs-example/utils') | ||
const pkg = require('./package.json') | ||
|
||
async function testUI (url, http, grpc, id) { | ||
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, '../'), | ||
env: { | ||
...process.env, | ||
CI: true, | ||
IPFS_EXAMPLE_TEST_URL: url, | ||
IPFS_GRPC_API_MULTIADDR: grpc, | ||
IPFS_HTTP_API_MULTIADDR: http | ||
}, | ||
all: true | ||
}) | ||
proc.all.on('data', (data) => { | ||
process.stdout.write(data) | ||
}) | ||
|
||
await proc | ||
} | ||
|
||
async function runTest () { | ||
const app = await startServer(__dirname) | ||
const daemon = await df.spawn({ | ||
type: 'js', | ||
test: true, | ||
ipfsOptions: { | ||
config: { | ||
Addresses: { | ||
API: '/ip4/127.0.0.1/tcp/0', | ||
RPC: '/ip4/127.0.0.1/tcp/0' | ||
}, | ||
API: { | ||
HTTPHeaders: { | ||
'Access-Control-Allow-Origin': [ | ||
app.url | ||
] | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
try { | ||
await testUI(app.url, daemon.apiAddr, daemon.grpcAddr, daemon.api.peerId.id) | ||
} finally { | ||
await daemon.stop() | ||
await app.stop() | ||
} | ||
} | ||
|
||
module.exports = runTest | ||
|
||
module.exports[pkg.name] = function (browser) { | ||
browser | ||
.url(process.env.IPFS_EXAMPLE_TEST_URL) | ||
.waitForElementVisible('#grpc-input') | ||
.clearValue('#grpc-input') | ||
.setValue('#grpc-input', process.env.IPFS_GRPC_API_MULTIADDR) | ||
.pause(1000) | ||
.waitForElementVisible('#http-input') | ||
.clearValue('#http-input') | ||
.setValue('#http-input', process.env.IPFS_HTTP_API_MULTIADDR) | ||
.pause(1000) | ||
.click('#connect-submit') | ||
|
||
browser.expect.element('#output').text.to.contain('Added file: file-0.txt QmUDLiEJwL3vUhhXNXDF2RrCnVkSB2LemWYffpCCPcQCeU') | ||
|
||
browser.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.