-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
19 changed files
with
827 additions
and
0 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
17 changes: 17 additions & 0 deletions
17
examples/js-libp2p-example-transports/.github/pull_request_template.md
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,17 @@ | ||
# ⚠️ IMPORTANT ⚠️ | ||
|
||
# Please do not create a Pull Request for this repository | ||
|
||
The contents of this repository are automatically synced from the parent [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples) so any changes made to the standalone repository will be lost after the next sync. | ||
|
||
Please open a PR against [js-libp2p Examples](https://github.com/libp2p/js-libp2p-examples) instead. | ||
|
||
## Contributing | ||
|
||
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. | ||
|
||
1. Fork the [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples) | ||
2. Create your Feature Branch (`git checkout -b feature/amazing-example`) | ||
3. Commit your Changes (`git commit -a -m 'feat: add some amazing example'`) | ||
4. Push to the Branch (`git push origin feature/amazing-example`) | ||
5. Open a Pull Request |
19 changes: 19 additions & 0 deletions
19
examples/js-libp2p-example-transports/.github/workflows/sync.yml
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,19 @@ | ||
name: pull | ||
|
||
on: | ||
workflow_dispatch | ||
|
||
jobs: | ||
sync: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Pull from another repository | ||
uses: ipfs-examples/actions-pull-directory-from-repo@main | ||
with: | ||
source-repo: libp2p/js-libp2p-examples | ||
source-folder-path: examples/${{ github.event.repository.name }} | ||
source-branch: main | ||
target-branch: main | ||
git-username: github-actions | ||
git-email: github-actions@github.com |
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,31 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { tcp } from '@libp2p/tcp' | ||
import { createLibp2p } from 'libp2p' | ||
|
||
const createNode = async () => { | ||
const node = await createLibp2p({ | ||
addresses: { | ||
// To signal the addresses we want to be available, we use | ||
// the multiaddr format, a self describable address | ||
listen: [ | ||
'/ip4/0.0.0.0/tcp/0' | ||
] | ||
}, | ||
transports: [ | ||
tcp() | ||
], | ||
connectionEncrypters: [ | ||
noise() | ||
] | ||
}) | ||
|
||
return node | ||
} | ||
|
||
const node = await createNode() | ||
|
||
console.log('node has started') | ||
console.log('listening on:') | ||
node.getMultiaddrs().forEach((ma) => console.log(ma.toString())) |
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,61 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { tcp } from '@libp2p/tcp' | ||
import { pipe } from 'it-pipe' | ||
import toBuffer from 'it-to-buffer' | ||
import { createLibp2p } from 'libp2p' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
|
||
const createNode = async () => { | ||
const node = await createLibp2p({ | ||
addresses: { | ||
// To signal the addresses we want to be available, we use | ||
// the multiaddr format, a self describable address | ||
listen: ['/ip4/0.0.0.0/tcp/0'] | ||
}, | ||
transports: [tcp()], | ||
connectionEncrypters: [noise()], | ||
streamMuxers: [yamux()] | ||
}) | ||
|
||
return node | ||
} | ||
|
||
function printAddrs (node, number) { | ||
console.log('node %s is listening on:', number) | ||
node.getMultiaddrs().forEach((ma) => console.log(ma.toString())) | ||
} | ||
|
||
const [node1, node2] = await Promise.all([ | ||
createNode(), | ||
createNode() | ||
]) | ||
|
||
printAddrs(node1, '1') | ||
printAddrs(node2, '2') | ||
|
||
node2.handle('/print', async ({ stream }) => { | ||
const result = await pipe( | ||
stream, | ||
async function * (source) { | ||
for await (const list of source) { | ||
yield list.subarray() | ||
} | ||
}, | ||
toBuffer | ||
) | ||
console.log(uint8ArrayToString(result)) | ||
}) | ||
|
||
await node1.peerStore.patch(node2.peerId, { | ||
multiaddrs: node2.getMultiaddrs() | ||
}) | ||
const stream = await node1.dialProtocol(node2.peerId, '/print') | ||
|
||
await pipe( | ||
['Hello', ' ', 'p2p', ' ', 'world', '!'].map(str => uint8ArrayFromString(str)), | ||
stream | ||
) |
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,88 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { tcp } from '@libp2p/tcp' | ||
import { webSockets } from '@libp2p/websockets' | ||
import { pipe } from 'it-pipe' | ||
import { createLibp2p } from 'libp2p' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
|
||
const createNode = async (transports, addresses = []) => { | ||
if (!Array.isArray(addresses)) { | ||
addresses = [addresses] | ||
} | ||
|
||
const node = await createLibp2p({ | ||
addresses: { | ||
listen: addresses | ||
}, | ||
transports, | ||
connectionEncrypters: [noise()], | ||
streamMuxers: [yamux()] | ||
}) | ||
|
||
return node | ||
} | ||
|
||
function printAddrs (node, number) { | ||
console.log('node %s is listening on:', number) | ||
node.getMultiaddrs().forEach((ma) => console.log(ma.toString())) | ||
} | ||
|
||
function print ({ stream }) { | ||
pipe( | ||
stream, | ||
async function (source) { | ||
for await (const msg of source) { | ||
console.log(uint8ArrayToString(msg.subarray())) | ||
} | ||
} | ||
) | ||
} | ||
|
||
const [node1, node2, node3] = await Promise.all([ | ||
createNode([tcp()], '/ip4/0.0.0.0/tcp/0'), | ||
createNode([tcp(), webSockets()], ['/ip4/0.0.0.0/tcp/0', '/ip4/127.0.0.1/tcp/10000/ws']), | ||
createNode([webSockets()], '/ip4/127.0.0.1/tcp/20000/ws') | ||
]) | ||
|
||
printAddrs(node1, '1') | ||
printAddrs(node2, '2') | ||
printAddrs(node3, '3') | ||
|
||
node1.handle('/print', print) | ||
node2.handle('/print', print) | ||
node3.handle('/print', print) | ||
|
||
await node1.peerStore.patch(node2.peerId, { | ||
multiaddrs: node2.getMultiaddrs() | ||
}) | ||
await node2.peerStore.patch(node3.peerId, { | ||
multiaddrs: node3.getMultiaddrs() | ||
}) | ||
await node3.peerStore.patch(node1.peerId, { | ||
multiaddrs: node1.getMultiaddrs() | ||
}) | ||
|
||
// node 1 (TCP) dials to node 2 (TCP+WebSockets) | ||
const stream = await node1.dialProtocol(node2.peerId, '/print') | ||
await pipe( | ||
[uint8ArrayFromString('node 1 dialed to node 2 successfully')], | ||
stream | ||
) | ||
|
||
// node 2 (TCP+WebSockets) dials to node 3 (WebSockets) | ||
const stream2 = await node2.dialProtocol(node3.peerId, '/print') | ||
await pipe( | ||
[uint8ArrayFromString('node 2 dialed to node 3 successfully')], | ||
stream2 | ||
) | ||
|
||
// node 3 (listening WebSockets) can dial node 1 (TCP) | ||
try { | ||
await node3.dialProtocol(node1.peerId, '/print') | ||
} catch (err) { | ||
console.log('node 3 failed to dial to node 1 with:', err.message) | ||
} |
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,78 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import fs from 'fs' | ||
import https from 'https' | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { tcp } from '@libp2p/tcp' | ||
import { webSockets } from '@libp2p/websockets' | ||
import { pipe } from 'it-pipe' | ||
import { createLibp2p } from 'libp2p' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
|
||
const httpServer = https.createServer({ | ||
cert: fs.readFileSync('./test_certs/cert.pem'), | ||
key: fs.readFileSync('./test_certs/key.pem') | ||
}) | ||
|
||
const createNode = async (addresses = []) => { | ||
if (!Array.isArray(addresses)) { | ||
addresses = [addresses] | ||
} | ||
|
||
const node = await createLibp2p({ | ||
addresses: { | ||
listen: addresses | ||
}, | ||
transports: [ | ||
tcp(), | ||
webSockets({ | ||
server: httpServer, | ||
websocket: { | ||
rejectUnauthorized: false | ||
} | ||
}) | ||
], | ||
connectionEncrypters: [noise()], | ||
streamMuxers: [yamux()] | ||
}) | ||
|
||
return node | ||
} | ||
|
||
function printAddrs (node, number) { | ||
console.log('node %s is listening on:', number) | ||
node.getMultiaddrs().forEach((ma) => console.log(ma.toString())) | ||
} | ||
|
||
function print ({ stream }) { | ||
pipe( | ||
stream, | ||
async function (source) { | ||
for await (const msg of source) { | ||
console.log(uint8ArrayToString(msg.subarray())) | ||
} | ||
} | ||
) | ||
} | ||
|
||
const [node1, node2] = await Promise.all([ | ||
createNode('/ip4/127.0.0.1/tcp/10000/wss'), | ||
createNode([]) | ||
]) | ||
|
||
printAddrs(node1, '1') | ||
printAddrs(node2, '2') | ||
|
||
node1.handle('/print', print) | ||
node2.handle('/print', print) | ||
|
||
const targetAddr = node1.getMultiaddrs()[0] | ||
|
||
// node 2 (Secure WebSockets) dials to node 1 (Secure Websockets) | ||
const stream = await node2.dialProtocol(targetAddr, '/print') | ||
await pipe( | ||
[uint8ArrayFromString('node 2 dialed to node 1 successfully')], | ||
stream | ||
) |
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,4 @@ | ||
This project is dual licensed under MIT and Apache-2.0. | ||
|
||
MIT: https://www.opensource.org/licenses/mit | ||
Apache-2.0: https://www.apache.org/licenses/license-2.0 |
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,5 @@ | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
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,19 @@ | ||
The MIT License (MIT) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Oops, something went wrong.