-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add protocol and stream muxing example (#176)
Restores old protocol and stream muxing example
- Loading branch information
1 parent
bab8b69
commit 4ecc1db
Showing
15 changed files
with
685 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
...s/js-libp2p-example-protocol-and-stream-muxing/.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-protocol-and-stream-muxing/.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 |
72 changes: 72 additions & 0 deletions
72
examples/js-libp2p-example-protocol-and-stream-muxing/1.js
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,72 @@ | ||
/* 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 { 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: { | ||
listen: ['/ip4/0.0.0.0/tcp/0'] | ||
}, | ||
transports: [tcp()], | ||
streamMuxers: [yamux()], | ||
connectionEncrypters: [noise()] | ||
}) | ||
|
||
return node | ||
} | ||
|
||
const [node1, node2] = await Promise.all([ | ||
createNode(), | ||
createNode() | ||
]) | ||
|
||
// exact matching | ||
node2.handle('/your-protocol', ({ stream }) => { | ||
pipe( | ||
stream, | ||
async function (source) { | ||
for await (const msg of source) { | ||
console.log(uint8ArrayToString(msg.subarray())) | ||
} | ||
} | ||
) | ||
}) | ||
|
||
// multiple protocols | ||
/* | ||
node2.handle(['/another-protocol/1.0.0', '/another-protocol/2.0.0'], ({ protocol, stream }) => { | ||
if (protocol === '/another-protocol/2.0.0') { | ||
// handle backwards compatibility | ||
} | ||
pipe( | ||
stream, | ||
async function (source) { | ||
for await (const msg of source) { | ||
console.log(uint8ArrayToString(msg)) | ||
} | ||
} | ||
) | ||
}) | ||
*/ | ||
|
||
const stream = await node1.dialProtocol(node2.getMultiaddrs(), ['/your-protocol']) | ||
await pipe( | ||
[uint8ArrayFromString('my own protocol, wow!')], | ||
stream | ||
) | ||
|
||
/* | ||
const stream = node1.dialProtocol(node2.peerId, ['/another-protocol/1.0.0']) | ||
await pipe( | ||
['my own protocol, wow!'], | ||
stream | ||
) | ||
*/ |
64 changes: 64 additions & 0 deletions
64
examples/js-libp2p-example-protocol-and-stream-muxing/2.js
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,64 @@ | ||
/* 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 { 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: { | ||
listen: ['/ip4/0.0.0.0/tcp/0'] | ||
}, | ||
transports: [tcp()], | ||
streamMuxers: [yamux()], | ||
connectionEncrypters: [noise()] | ||
}) | ||
|
||
return node | ||
} | ||
|
||
const [node1, node2] = await Promise.all([ | ||
createNode(), | ||
createNode() | ||
]) | ||
|
||
// Add node's 2 data to the PeerStore | ||
await node1.peerStore.patch(node2.peerId, { | ||
multiaddrs: node2.getMultiaddrs() | ||
}) | ||
|
||
node2.handle(['/a', '/b'], ({ protocol, stream }) => { | ||
pipe( | ||
stream, | ||
async function (source) { | ||
for await (const msg of source) { | ||
console.log(`from: ${protocol}, msg: ${uint8ArrayToString(msg.subarray())}`) | ||
} | ||
} | ||
).finally(() => { | ||
// clean up resources | ||
stream.close() | ||
}) | ||
}) | ||
|
||
const stream1 = await node1.dialProtocol(node2.peerId, ['/a']) | ||
await pipe( | ||
[uint8ArrayFromString('protocol (a)')], | ||
stream1 | ||
) | ||
|
||
const stream2 = await node1.dialProtocol(node2.peerId, ['/b']) | ||
await pipe( | ||
[uint8ArrayFromString('protocol (b)')], | ||
stream2 | ||
) | ||
|
||
const stream3 = await node1.dialProtocol(node2.peerId, ['/b']) | ||
await pipe( | ||
[uint8ArrayFromString('another stream on protocol (b)')], | ||
stream3 | ||
) |
66 changes: 66 additions & 0 deletions
66
examples/js-libp2p-example-protocol-and-stream-muxing/3.js
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,66 @@ | ||
/* 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 { 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: { | ||
listen: ['/ip4/0.0.0.0/tcp/0'] | ||
}, | ||
transports: [tcp()], | ||
streamMuxers: [yamux()], | ||
connectionEncrypters: [noise()] | ||
}) | ||
|
||
return node | ||
} | ||
|
||
const [node1, node2] = await Promise.all([ | ||
createNode(), | ||
createNode() | ||
]) | ||
|
||
// Add node's 2 data to the PeerStore | ||
await node1.peerStore.patch(node2.peerId, { | ||
multiaddrs: node2.getMultiaddrs() | ||
}) | ||
|
||
node1.handle('/node-1', ({ stream }) => { | ||
pipe( | ||
stream, | ||
async function (source) { | ||
for await (const msg of source) { | ||
console.log(uint8ArrayToString(msg.subarray())) | ||
} | ||
} | ||
) | ||
}) | ||
|
||
node2.handle('/node-2', ({ stream }) => { | ||
pipe( | ||
stream, | ||
async function (source) { | ||
for await (const msg of source) { | ||
console.log(uint8ArrayToString(msg.subarray())) | ||
} | ||
} | ||
) | ||
}) | ||
|
||
const stream1 = await node1.dialProtocol(node2.peerId, ['/node-2']) | ||
await pipe( | ||
[uint8ArrayFromString('from 1 to 2')], | ||
stream1 | ||
) | ||
|
||
const stream2 = await node2.dialProtocol(node1.peerId, ['/node-1']) | ||
await pipe( | ||
[uint8ArrayFromString('from 2 to 1')], | ||
stream2 | ||
) |
4 changes: 4 additions & 0 deletions
4
examples/js-libp2p-example-protocol-and-stream-muxing/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,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 |
5 changes: 5 additions & 0 deletions
5
examples/js-libp2p-example-protocol-and-stream-muxing/LICENSE-APACHE
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. |
19 changes: 19 additions & 0 deletions
19
examples/js-libp2p-example-protocol-and-stream-muxing/LICENSE-MIT
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.