-
Notifications
You must be signed in to change notification settings - Fork 46
feat: custom address filter #116
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -35,7 +35,50 @@ | |||||
> npm i libp2p-websockets | ||||||
``` | ||||||
|
||||||
### Example | ||||||
### Constructor properties | ||||||
|
||||||
```js | ||||||
const WS = require('libp2p-websockets') | ||||||
|
||||||
const properties = { | ||||||
upgrader, | ||||||
filter | ||||||
} | ||||||
|
||||||
const ws = new WS(properties) | ||||||
``` | ||||||
|
||||||
| Name | Type | Description | Default | | ||||||
|------|------|-------------|---------| | ||||||
| upgrader | [`Upgrader`](https://github.com/libp2p/interface-transport#upgrader) | connection upgrader object with `upgradeOutbound` and `upgradeInbound` | **REQUIRED** | | ||||||
| filter | `(multiaddrs: Array<Multiaddr>) => Array<Multiaddr>` | override transport addresses filter | **Browser:** DNS+WSS multiaddrs / **Node.js:** DNS+{WS, WSS} multiaddrs | | ||||||
|
||||||
## Libp2p Usage Example | ||||||
|
||||||
```js | ||||||
const Libp2p = require('libp2p') | ||||||
const Websockets = require('libp2p-websockets') | ||||||
const filters = require('libp2p-websockets/src/filters') | ||||||
|
||||||
const transportKey = Websockets.prototype[Symbol.toStringTag] | ||||||
const node = await Libp2p.create({ | ||||||
modules: { | ||||||
transport: [WebRTCStar], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// ... other mandatory modules | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would just add NOISE and mplex in here. There aren't other options right now, so having a config people can just use would be helpful imo. |
||||||
}, | ||||||
config: { | ||||||
transport: { | ||||||
[transportKey]: { // Transport properties -- Libp2p upgrader is automatically added | ||||||
filter: filters.dnsWsOrWss | ||||||
} | ||||||
} | ||||||
} | ||||||
}) | ||||||
``` | ||||||
|
||||||
For more information see [libp2p/js-libp2p/doc/CONFIGURATION.md#customizing-transports](https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md#customizing-transports). | ||||||
|
||||||
## Base Example | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would just delete this whole section, nobody should use it like this. |
||||||
|
||||||
```js | ||||||
const WS = require('libp2p-websockets') | ||||||
|
@@ -69,6 +112,26 @@ console.log(`Value: ${values.toString()}`) | |||||
await listener.close() | ||||||
``` | ||||||
|
||||||
## Custom filters Example | ||||||
|
||||||
You can create your own address filters for this transports, or rely in the filters [provided](./src/filters.js). | ||||||
|
||||||
```js | ||||||
const WS = require('libp2p-websockets') | ||||||
const filters = require('libp2p-websockets/src/filters') | ||||||
|
||||||
const ws = new WS({ upgrader, filter: filters.all }) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think the libp2p transport configuration example will be ideal here, https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md#customizing-transports. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the libp2p usage example, but kept the other. They could still be relevant, but the Readme might get too noisy. What's your opinion? |
||||||
``` | ||||||
|
||||||
The available filters are: | ||||||
|
||||||
- `filters.all` | ||||||
- Returns all TCP and DNS based addresses, both with `ws` or `wss`. | ||||||
- `filters.dnsWss` | ||||||
- Returns all DNS based addresses with `wss`. | ||||||
- `filters.dnsWsOrWss` | ||||||
- Returns all DNS based addresses, both with `ws` or `wss`. | ||||||
|
||||||
## API | ||||||
|
||||||
### Transport | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict' | ||
|
||
const mafmt = require('mafmt') | ||
const { | ||
CODE_CIRCUIT, | ||
CODE_P2P, | ||
CODE_TCP, | ||
CODE_WS, | ||
CODE_WSS | ||
} = require('./constants') | ||
|
||
module.exports = { | ||
all: (multiaddrs) => multiaddrs.filter((ma) => { | ||
if (ma.protoCodes().includes(CODE_CIRCUIT)) { | ||
return false | ||
} | ||
|
||
const testMa = ma.decapsulateCode(CODE_P2P) | ||
|
||
return mafmt.WebSockets.matches(testMa) || | ||
mafmt.WebSocketsSecure.matches(testMa) | ||
}), | ||
dnsWss: (multiaddrs) => multiaddrs.filter((ma) => { | ||
if (ma.protoCodes().includes(CODE_CIRCUIT)) { | ||
return false | ||
} | ||
|
||
const testMa = ma.decapsulateCode(CODE_P2P) | ||
|
||
return mafmt.WebSocketsSecure.matches(testMa) && | ||
mafmt.DNS.matches(testMa.decapsulateCode(CODE_TCP).decapsulateCode(CODE_WSS)) | ||
}), | ||
dnsWsOrWss: (multiaddrs) => multiaddrs.filter((ma) => { | ||
if (ma.protoCodes().includes(CODE_CIRCUIT)) { | ||
return false | ||
} | ||
|
||
const testMa = ma.decapsulateCode(CODE_P2P) | ||
|
||
// WS | ||
if (mafmt.WebSockets.matches(testMa)) { | ||
return mafmt.DNS.matches(testMa.decapsulateCode(CODE_TCP).decapsulateCode(CODE_WS)) | ||
} | ||
|
||
// WSS | ||
return mafmt.WebSocketsSecure.matches(testMa) && | ||
mafmt.DNS.matches(testMa.decapsulateCode(CODE_TCP).decapsulateCode(CODE_WSS)) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this is a good time to just replace this example with how to use it in a libp2p configuration with the passed options. That's likely much more useful than the existing examples as most people won't use this standalone.