mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-25 18:45:35 +00:00
Separate the libp2p create options from Waku's
This commit is contained in:
parent
e2e14167b3
commit
f17a008278
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- **Breaking**: Options passed to `Waku.create` used to be passed to `Libp2p.create`;
|
||||||
|
Now, only the `libp2p` property is passed to `Libp2p.create`, allowing for a cleaner interface.
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
- Enable access to `WakuMessage.timestamp`.
|
- Enable access to `WakuMessage.timestamp`.
|
||||||
- Examples (web chat): Use `WakuMessage.timestamp` as unique key for list items.
|
- Examples (web chat): Use `WakuMessage.timestamp` as unique key for list items.
|
||||||
|
@ -27,8 +27,10 @@ export default async function startChat(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const waku = await Waku.create({
|
const waku = await Waku.create({
|
||||||
listenAddresses: [opts.listenAddr],
|
libp2p: {
|
||||||
modules: { transport: [TCP] },
|
addresses: { listen: [opts.listenAddr] },
|
||||||
|
modules: { transport: [TCP] },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
console.log('PeerId: ', waku.libp2p.peerId.toB58String());
|
console.log('PeerId: ', waku.libp2p.peerId.toB58String());
|
||||||
console.log('Listening on ');
|
console.log('Listening on ');
|
||||||
|
@ -181,10 +181,12 @@ export default function App() {
|
|||||||
async function initWaku(setter: (waku: Waku) => void) {
|
async function initWaku(setter: (waku: Waku) => void) {
|
||||||
try {
|
try {
|
||||||
const waku = await Waku.create({
|
const waku = await Waku.create({
|
||||||
config: {
|
libp2p: {
|
||||||
pubsub: {
|
config: {
|
||||||
enabled: true,
|
pubsub: {
|
||||||
emitSelf: true,
|
enabled: true,
|
||||||
|
emitSelf: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -17,7 +17,7 @@ describe('Waku Dial', function () {
|
|||||||
const [waku1, waku2] = await Promise.all([
|
const [waku1, waku2] = await Promise.all([
|
||||||
Waku.create({
|
Waku.create({
|
||||||
staticNoiseKey: NOISE_KEY_1,
|
staticNoiseKey: NOISE_KEY_1,
|
||||||
listenAddresses: ['/ip4/0.0.0.0/tcp/0/wss'],
|
libp2p: { addresses: { listen: ['/ip4/0.0.0.0/tcp/0/wss'] } },
|
||||||
}),
|
}),
|
||||||
Waku.create({ staticNoiseKey: NOISE_KEY_2 }),
|
Waku.create({ staticNoiseKey: NOISE_KEY_2 }),
|
||||||
]);
|
]);
|
||||||
@ -39,8 +39,10 @@ describe('Waku Dial', function () {
|
|||||||
this.timeout(10_000);
|
this.timeout(10_000);
|
||||||
const waku = await Waku.create({
|
const waku = await Waku.create({
|
||||||
staticNoiseKey: NOISE_KEY_1,
|
staticNoiseKey: NOISE_KEY_1,
|
||||||
listenAddresses: ['/ip4/0.0.0.0/tcp/0'],
|
libp2p: {
|
||||||
modules: { transport: [TCP] },
|
addresses: { listen: ['/ip4/0.0.0.0/tcp/0'] },
|
||||||
|
modules: { transport: [TCP] },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const multiAddrWithId = waku.getLocalMultiaddrWithID();
|
const multiAddrWithId = waku.getLocalMultiaddrWithID();
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import Libp2p, { Libp2pConfig, Libp2pModules, Libp2pOptions } from 'libp2p';
|
import Libp2p, { Libp2pModules, Libp2pOptions } from 'libp2p';
|
||||||
import Mplex from 'libp2p-mplex';
|
import Mplex from 'libp2p-mplex';
|
||||||
import { bytes } from 'libp2p-noise/dist/src/@types/basic';
|
import { bytes } from 'libp2p-noise/dist/src/@types/basic';
|
||||||
import { Noise } from 'libp2p-noise/dist/src/noise';
|
import { Noise } from 'libp2p-noise/dist/src/noise';
|
||||||
@ -11,16 +11,26 @@ import { WakuLightPush } from './waku_light_push';
|
|||||||
import { RelayCodec, WakuRelay } from './waku_relay';
|
import { RelayCodec, WakuRelay } from './waku_relay';
|
||||||
import { StoreCodec, WakuStore } from './waku_store';
|
import { StoreCodec, WakuStore } from './waku_store';
|
||||||
|
|
||||||
const transportKey = Websockets.prototype[Symbol.toStringTag];
|
const websocketsTransportKey = Websockets.prototype[Symbol.toStringTag];
|
||||||
|
|
||||||
export type CreateOptions =
|
export interface CreateOptions {
|
||||||
| {
|
/**
|
||||||
listenAddresses: string[] | undefined;
|
* You can pass options to the `Libp2p` instance used by {@link Waku} using the {@link CreateOptions.libp2p} property.
|
||||||
staticNoiseKey: bytes | undefined;
|
* This property is the same type than the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
||||||
modules: Partial<Libp2pModules>;
|
* apart that we made the `modules` property optional and partial,
|
||||||
config: Partial<Libp2pConfig>;
|
* allowing its omission and letting Waku set good defaults.
|
||||||
}
|
* Notes that some values are overridden by {@link Waku} to ensure it implements the Waku protocol.
|
||||||
| (Libp2pOptions & import('libp2p').CreateOptions);
|
*/
|
||||||
|
libp2p?: Omit<Libp2pOptions & import('libp2p').CreateOptions, 'modules'> & {
|
||||||
|
modules?: Partial<Libp2pModules>;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Byte array used as key for the noise protocol used for connection encryption
|
||||||
|
* by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create)
|
||||||
|
* This is only used for test purposes to not run out of entropy during CI runs.
|
||||||
|
*/
|
||||||
|
staticNoiseKey?: bytes;
|
||||||
|
}
|
||||||
|
|
||||||
export class Waku {
|
export class Waku {
|
||||||
public libp2p: Libp2p;
|
public libp2p: Libp2p;
|
||||||
@ -44,52 +54,45 @@ export class Waku {
|
|||||||
*
|
*
|
||||||
* @param options Takes the same options than `Libp2p`.
|
* @param options Takes the same options than `Libp2p`.
|
||||||
*/
|
*/
|
||||||
static async create(options: Partial<CreateOptions>): Promise<Waku> {
|
static async create(options?: CreateOptions): Promise<Waku> {
|
||||||
const opts = Object.assign(
|
// Get an object in case options or libp2p are undefined
|
||||||
{
|
const libp2pOpts = Object.assign({}, options?.libp2p);
|
||||||
listenAddresses: [],
|
|
||||||
staticNoiseKey: undefined,
|
|
||||||
},
|
|
||||||
options
|
|
||||||
);
|
|
||||||
|
|
||||||
opts.config = Object.assign(
|
// Default for Websocket filter is `all`:
|
||||||
|
// Returns all TCP and DNS based addresses, both with ws or wss.
|
||||||
|
libp2pOpts.config = Object.assign(
|
||||||
{
|
{
|
||||||
transport: {
|
transport: {
|
||||||
[transportKey]: {
|
[websocketsTransportKey]: {
|
||||||
filter: filters.all,
|
filter: filters.all,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
options.config
|
options?.libp2p?.config
|
||||||
);
|
);
|
||||||
|
|
||||||
opts.modules = Object.assign({}, options.modules);
|
libp2pOpts.modules = Object.assign({}, options?.libp2p?.modules);
|
||||||
|
|
||||||
let transport = [Websockets];
|
// Default transport for libp2p is Websockets
|
||||||
if (opts.modules?.transport) {
|
libp2pOpts.modules = Object.assign(
|
||||||
transport = transport.concat(opts.modules?.transport);
|
{
|
||||||
}
|
transport: [Websockets],
|
||||||
|
},
|
||||||
|
options?.libp2p?.modules
|
||||||
|
);
|
||||||
|
|
||||||
// FIXME: By controlling the creation of libp2p we have to think about what
|
// streamMuxer, connection encryption and pubsub are overridden
|
||||||
// needs to be exposed and what does not. Ideally, we should be able to let
|
// as those are the only ones currently supported by Waku nodes.
|
||||||
// the user create the WakuStore, WakuRelay instances and pass them when
|
libp2pOpts.modules = Object.assign(libp2pOpts.modules, {
|
||||||
// creating the libp2p instance.
|
streamMuxer: [Mplex],
|
||||||
const libp2p = await Libp2p.create({
|
connEncryption: [new Noise(options?.staticNoiseKey)],
|
||||||
addresses: {
|
pubsub: WakuRelay,
|
||||||
listen: opts.listenAddresses,
|
|
||||||
},
|
|
||||||
modules: {
|
|
||||||
transport,
|
|
||||||
streamMuxer: [Mplex],
|
|
||||||
connEncryption: [new Noise(opts.staticNoiseKey)],
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
||||||
// @ts-ignore: Type needs update
|
|
||||||
pubsub: WakuRelay,
|
|
||||||
},
|
|
||||||
config: opts.config,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore: modules property is correctly set thanks to voodoo
|
||||||
|
const libp2p = await Libp2p.create(libp2pOpts);
|
||||||
|
|
||||||
const wakuStore = new WakuStore(libp2p);
|
const wakuStore = new WakuStore(libp2p);
|
||||||
const wakuLightPush = new WakuLightPush(libp2p);
|
const wakuLightPush = new WakuLightPush(libp2p);
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ describe('Waku Light Push', () => {
|
|||||||
|
|
||||||
waku = await Waku.create({
|
waku = await Waku.create({
|
||||||
staticNoiseKey: NOISE_KEY_1,
|
staticNoiseKey: NOISE_KEY_1,
|
||||||
modules: { transport: [TCP] },
|
libp2p: { modules: { transport: [TCP] } },
|
||||||
});
|
});
|
||||||
await waku.dial(await nimWaku.getMultiaddrWithId());
|
await waku.dial(await nimWaku.getMultiaddrWithId());
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ describe('Waku Relay', () => {
|
|||||||
Waku.create({ staticNoiseKey: NOISE_KEY_1 }),
|
Waku.create({ staticNoiseKey: NOISE_KEY_1 }),
|
||||||
Waku.create({
|
Waku.create({
|
||||||
staticNoiseKey: NOISE_KEY_2,
|
staticNoiseKey: NOISE_KEY_2,
|
||||||
listenAddresses: ['/ip4/0.0.0.0/tcp/0/wss'],
|
libp2p: { addresses: { listen: ['/ip4/0.0.0.0/tcp/0/wss'] } },
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -153,8 +153,10 @@ describe('Waku Relay', () => {
|
|||||||
log('Create waku node');
|
log('Create waku node');
|
||||||
waku = await Waku.create({
|
waku = await Waku.create({
|
||||||
staticNoiseKey: NOISE_KEY_1,
|
staticNoiseKey: NOISE_KEY_1,
|
||||||
listenAddresses: ['/ip4/0.0.0.0/tcp/0'],
|
libp2p: {
|
||||||
modules: { transport: [TCP] },
|
addresses: { listen: ['/ip4/0.0.0.0/tcp/0'] },
|
||||||
|
modules: { transport: [TCP] },
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const multiAddrWithId = waku.getLocalMultiaddrWithID();
|
const multiAddrWithId = waku.getLocalMultiaddrWithID();
|
||||||
@ -231,7 +233,7 @@ describe('Waku Relay', () => {
|
|||||||
this.timeout(30_000);
|
this.timeout(30_000);
|
||||||
waku = await Waku.create({
|
waku = await Waku.create({
|
||||||
staticNoiseKey: NOISE_KEY_1,
|
staticNoiseKey: NOISE_KEY_1,
|
||||||
modules: { transport: [TCP] },
|
libp2p: { modules: { transport: [TCP] } },
|
||||||
});
|
});
|
||||||
|
|
||||||
nimWaku = new NimWaku(this.test?.ctx?.currentTest?.title + '');
|
nimWaku = new NimWaku(this.test?.ctx?.currentTest?.title + '');
|
||||||
@ -328,11 +330,11 @@ describe('Waku Relay', () => {
|
|||||||
[waku1, waku2] = await Promise.all([
|
[waku1, waku2] = await Promise.all([
|
||||||
Waku.create({
|
Waku.create({
|
||||||
staticNoiseKey: NOISE_KEY_1,
|
staticNoiseKey: NOISE_KEY_1,
|
||||||
modules: { transport: [TCP] },
|
libp2p: { modules: { transport: [TCP] } },
|
||||||
}),
|
}),
|
||||||
Waku.create({
|
Waku.create({
|
||||||
staticNoiseKey: NOISE_KEY_2,
|
staticNoiseKey: NOISE_KEY_2,
|
||||||
modules: { transport: [TCP] },
|
libp2p: { modules: { transport: [TCP] } },
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ describe('Waku Store', () => {
|
|||||||
|
|
||||||
waku = await Waku.create({
|
waku = await Waku.create({
|
||||||
staticNoiseKey: NOISE_KEY_1,
|
staticNoiseKey: NOISE_KEY_1,
|
||||||
modules: { transport: [TCP] },
|
libp2p: { modules: { transport: [TCP] } },
|
||||||
});
|
});
|
||||||
await waku.dial(await nimWaku.getMultiaddrWithId());
|
await waku.dial(await nimWaku.getMultiaddrWithId());
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ describe('Waku Store', () => {
|
|||||||
|
|
||||||
waku = await Waku.create({
|
waku = await Waku.create({
|
||||||
staticNoiseKey: NOISE_KEY_1,
|
staticNoiseKey: NOISE_KEY_1,
|
||||||
modules: { transport: [TCP] },
|
libp2p: { modules: { transport: [TCP] } },
|
||||||
});
|
});
|
||||||
await waku.dial(await nimWaku.getMultiaddrWithId());
|
await waku.dial(await nimWaku.getMultiaddrWithId());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user