@libp2p/memory 0.0.0-91687998d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -0
- package/README.md +86 -0
- package/dist/index.min.js +3 -0
- package/dist/src/connections.d.ts +65 -0
- package/dist/src/connections.d.ts.map +1 -0
- package/dist/src/connections.js +148 -0
- package/dist/src/connections.js.map +1 -0
- package/dist/src/index.d.ts +54 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +50 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/listener.d.ts +62 -0
- package/dist/src/listener.d.ts.map +1 -0
- package/dist/src/listener.js +111 -0
- package/dist/src/listener.js.map +1 -0
- package/dist/src/memory.d.ts +65 -0
- package/dist/src/memory.d.ts.map +1 -0
- package/dist/src/memory.js +97 -0
- package/dist/src/memory.js.map +1 -0
- package/package.json +75 -0
- package/src/connections.ts +176 -0
- package/src/index.ts +62 -0
- package/src/listener.ts +135 -0
- package/src/memory.ts +113 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* A [libp2p transport](https://docs.libp2p.io/concepts/transports/overview/)
|
|
5
|
+
* that operates in-memory only.
|
|
6
|
+
*
|
|
7
|
+
* This is intended for testing and can only be used to connect two libp2p nodes
|
|
8
|
+
* that are running in the same process.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```TypeScript
|
|
13
|
+
* import { createLibp2p } from 'libp2p'
|
|
14
|
+
* import { memory } from '@libp2p/memory'
|
|
15
|
+
* import { multiaddr } from '@multiformats/multiaddr'
|
|
16
|
+
*
|
|
17
|
+
* const listener = await createLibp2p({
|
|
18
|
+
* addresses: {
|
|
19
|
+
* listen: [
|
|
20
|
+
* '/memory/node-a'
|
|
21
|
+
* ]
|
|
22
|
+
* },
|
|
23
|
+
* transports: [
|
|
24
|
+
* memory()
|
|
25
|
+
* ]
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* const dialer = await createLibp2p({
|
|
29
|
+
* transports: [
|
|
30
|
+
* memory()
|
|
31
|
+
* ]
|
|
32
|
+
* })
|
|
33
|
+
*
|
|
34
|
+
* const ma = multiaddr('/memory/node-a')
|
|
35
|
+
*
|
|
36
|
+
* // dial the listener, timing out after 10s
|
|
37
|
+
* const connection = await dialer.dial(ma, {
|
|
38
|
+
* signal: AbortSignal.timeout(10_000)
|
|
39
|
+
* })
|
|
40
|
+
*
|
|
41
|
+
* // use connection...
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
import { ListenError, TypedEventEmitter } from '@libp2p/interface';
|
|
45
|
+
import { multiaddr } from '@multiformats/multiaddr';
|
|
46
|
+
import { nanoid } from 'nanoid';
|
|
47
|
+
import { MemoryConnection, connections } from './connections.js';
|
|
48
|
+
export class MemoryTransportListener extends TypedEventEmitter {
|
|
49
|
+
listenAddr;
|
|
50
|
+
connection;
|
|
51
|
+
components;
|
|
52
|
+
init;
|
|
53
|
+
constructor(components, init) {
|
|
54
|
+
super();
|
|
55
|
+
this.components = components;
|
|
56
|
+
this.init = init;
|
|
57
|
+
}
|
|
58
|
+
async listen(ma) {
|
|
59
|
+
const [[, value]] = ma.stringTuples();
|
|
60
|
+
const address = `/memory/${value ?? nanoid()}`;
|
|
61
|
+
if (value != null && connections.has(address)) {
|
|
62
|
+
throw new ListenError(`Memory address ${address} already in use`);
|
|
63
|
+
}
|
|
64
|
+
this.connection = new MemoryConnection(this.components, {
|
|
65
|
+
onConnection: this.onConnection.bind(this),
|
|
66
|
+
address
|
|
67
|
+
});
|
|
68
|
+
this.listenAddr = multiaddr(address);
|
|
69
|
+
connections.set(address, this.connection);
|
|
70
|
+
queueMicrotask(() => {
|
|
71
|
+
this.safeDispatchEvent('listening');
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
onConnection(maConn) {
|
|
75
|
+
let signal;
|
|
76
|
+
if (this.init.inboundUpgradeTimeout != null) {
|
|
77
|
+
signal = AbortSignal.timeout(this.init.inboundUpgradeTimeout);
|
|
78
|
+
}
|
|
79
|
+
this.init.upgrader.upgradeInbound(maConn, {
|
|
80
|
+
...this.init.upgraderOptions,
|
|
81
|
+
signal
|
|
82
|
+
})
|
|
83
|
+
.then(connection => {
|
|
84
|
+
this.init.handler?.(connection);
|
|
85
|
+
this.safeDispatchEvent('connection', {
|
|
86
|
+
detail: connection
|
|
87
|
+
});
|
|
88
|
+
})
|
|
89
|
+
.catch(err => {
|
|
90
|
+
maConn.abort(err);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
getAddrs() {
|
|
94
|
+
if (this.listenAddr == null) {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
return [
|
|
98
|
+
this.listenAddr
|
|
99
|
+
];
|
|
100
|
+
}
|
|
101
|
+
async close() {
|
|
102
|
+
this.connection?.close();
|
|
103
|
+
if (this.listenAddr != null) {
|
|
104
|
+
connections.delete(this.listenAddr.toString());
|
|
105
|
+
}
|
|
106
|
+
queueMicrotask(() => {
|
|
107
|
+
this.safeDispatchEvent('close');
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=listener.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"listener.js","sourceRoot":"","sources":["../../src/listener.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAClE,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAShE,MAAM,OAAO,uBAAwB,SAAQ,iBAAiC;IACpE,UAAU,CAAY;IACtB,UAAU,CAAmB;IACpB,UAAU,CAA2B;IACrC,IAAI,CAA6B;IAElD,YAAa,UAAqC,EAAE,IAAiC;QACnF,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;IAED,KAAK,CAAC,MAAM,CAAE,EAAa;QACzB,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC,YAAY,EAAE,CAAA;QAErC,MAAM,OAAO,GAAG,WAAW,KAAK,IAAI,MAAM,EAAE,EAAE,CAAA;QAE9C,IAAI,KAAK,IAAI,IAAI,IAAI,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YAC9C,MAAM,IAAI,WAAW,CAAC,kBAAkB,OAAO,iBAAiB,CAAC,CAAA;QACnE,CAAC;QAED,IAAI,CAAC,UAAU,GAAG,IAAI,gBAAgB,CAAC,IAAI,CAAC,UAAU,EAAE;YACtD,YAAY,EAAE,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC;YAC1C,OAAO;SACR,CAAC,CAAA;QACF,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC,OAAO,CAAC,CAAA;QAEpC,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,UAAU,CAAC,CAAA;QAEzC,cAAc,CAAC,GAAG,EAAE;YAClB,IAAI,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAA;QACrC,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,YAAY,CAAE,MAA2B;QACvC,IAAI,MAA+B,CAAA;QAEnC,IAAI,IAAI,CAAC,IAAI,CAAC,qBAAqB,IAAI,IAAI,EAAE,CAAC;YAC5C,MAAM,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAA;QAC/D,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,MAAM,EAAE;YACxC,GAAG,IAAI,CAAC,IAAI,CAAC,eAAe;YAC5B,MAAM;SACP,CAAC;aACC,IAAI,CAAC,UAAU,CAAC,EAAE;YACjB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,UAAU,CAAC,CAAA;YAC/B,IAAI,CAAC,iBAAiB,CAAC,YAAY,EAAE;gBACnC,MAAM,EAAE,UAAU;aACnB,CAAC,CAAA;QACJ,CAAC,CAAC;aACD,KAAK,CAAC,GAAG,CAAC,EAAE;YACX,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACnB,CAAC,CAAC,CAAA;IACN,CAAC;IAED,QAAQ;QACN,IAAI,IAAI,CAAC,UAAU,IAAI,IAAI,EAAE,CAAC;YAC5B,OAAO,EAAE,CAAA;QACX,CAAC;QAED,OAAO;YACL,IAAI,CAAC,UAAU;SAChB,CAAA;IACH,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,UAAU,EAAE,KAAK,EAAE,CAAA;QAExB,IAAI,IAAI,CAAC,UAAU,IAAI,IAAI,EAAE,CAAC;YAC5B,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAA;QAChD,CAAC;QAED,cAAc,CAAC,GAAG,EAAE;YAClB,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAA;QACjC,CAAC,CAAC,CAAA;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* A [libp2p transport](https://docs.libp2p.io/concepts/transports/overview/)
|
|
5
|
+
* that operates in-memory only.
|
|
6
|
+
*
|
|
7
|
+
* This is intended for testing and can only be used to connect two libp2p nodes
|
|
8
|
+
* that are running in the same process.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```TypeScript
|
|
13
|
+
* import { createLibp2p } from 'libp2p'
|
|
14
|
+
* import { memory } from '@libp2p/memory'
|
|
15
|
+
* import { multiaddr } from '@multiformats/multiaddr'
|
|
16
|
+
*
|
|
17
|
+
* const listener = await createLibp2p({
|
|
18
|
+
* addresses: {
|
|
19
|
+
* listen: [
|
|
20
|
+
* '/memory/node-a'
|
|
21
|
+
* ]
|
|
22
|
+
* },
|
|
23
|
+
* transports: [
|
|
24
|
+
* memory()
|
|
25
|
+
* ]
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* const dialer = await createLibp2p({
|
|
29
|
+
* transports: [
|
|
30
|
+
* memory()
|
|
31
|
+
* ]
|
|
32
|
+
* })
|
|
33
|
+
*
|
|
34
|
+
* const ma = multiaddr('/memory/node-a')
|
|
35
|
+
*
|
|
36
|
+
* // dial the listener, timing out after 10s
|
|
37
|
+
* const connection = await dialer.dial(ma, {
|
|
38
|
+
* signal: AbortSignal.timeout(10_000)
|
|
39
|
+
* })
|
|
40
|
+
*
|
|
41
|
+
* // use connection...
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
import { serviceCapabilities, transportSymbol } from '@libp2p/interface';
|
|
45
|
+
import type { MemoryTransportComponents, MemoryTransportInit } from './index.js';
|
|
46
|
+
import type { Connection, Transport, Listener, CreateListenerOptions, DialTransportOptions } from '@libp2p/interface';
|
|
47
|
+
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
48
|
+
export declare class MemoryTransport implements Transport {
|
|
49
|
+
private readonly components;
|
|
50
|
+
private readonly init;
|
|
51
|
+
constructor(components: MemoryTransportComponents, init?: MemoryTransportInit);
|
|
52
|
+
readonly [transportSymbol] = true;
|
|
53
|
+
readonly [Symbol.toStringTag] = "@libp2p/memory";
|
|
54
|
+
readonly [serviceCapabilities]: string[];
|
|
55
|
+
dial(ma: Multiaddr, options: DialTransportOptions): Promise<Connection>;
|
|
56
|
+
/**
|
|
57
|
+
* Creates a TCP listener. The provided `handler` function will be called
|
|
58
|
+
* anytime a new incoming Connection has been successfully upgraded via
|
|
59
|
+
* `upgrader.upgradeInbound`.
|
|
60
|
+
*/
|
|
61
|
+
createListener(options: CreateListenerOptions): Listener;
|
|
62
|
+
listenFilter(multiaddrs: Multiaddr[]): Multiaddr[];
|
|
63
|
+
dialFilter(multiaddrs: Multiaddr[]): Multiaddr[];
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/memory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,OAAO,EAAyB,mBAAmB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAI/F,OAAO,KAAK,EAAE,yBAAyB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAChF,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AACrH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,qBAAa,eAAgB,YAAW,SAAS;IAC/C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA2B;IACtD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAqB;gBAE7B,UAAU,EAAE,yBAAyB,EAAE,IAAI,GAAE,mBAAwB;IAKlF,QAAQ,CAAC,CAAC,eAAe,CAAC,QAAO;IAEjC,QAAQ,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,oBAAmB;IAEhD,QAAQ,CAAC,CAAC,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAEvC;IAEK,IAAI,CAAE,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC;IAwB9E;;;;OAIG;IACH,cAAc,CAAE,OAAO,EAAE,qBAAqB,GAAG,QAAQ;IAOzD,YAAY,CAAE,UAAU,EAAE,SAAS,EAAE,GAAG,SAAS,EAAE;IAInD,UAAU,CAAE,UAAU,EAAE,SAAS,EAAE,GAAG,SAAS,EAAE;CAGlD"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* A [libp2p transport](https://docs.libp2p.io/concepts/transports/overview/)
|
|
5
|
+
* that operates in-memory only.
|
|
6
|
+
*
|
|
7
|
+
* This is intended for testing and can only be used to connect two libp2p nodes
|
|
8
|
+
* that are running in the same process.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```TypeScript
|
|
13
|
+
* import { createLibp2p } from 'libp2p'
|
|
14
|
+
* import { memory } from '@libp2p/memory'
|
|
15
|
+
* import { multiaddr } from '@multiformats/multiaddr'
|
|
16
|
+
*
|
|
17
|
+
* const listener = await createLibp2p({
|
|
18
|
+
* addresses: {
|
|
19
|
+
* listen: [
|
|
20
|
+
* '/memory/node-a'
|
|
21
|
+
* ]
|
|
22
|
+
* },
|
|
23
|
+
* transports: [
|
|
24
|
+
* memory()
|
|
25
|
+
* ]
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* const dialer = await createLibp2p({
|
|
29
|
+
* transports: [
|
|
30
|
+
* memory()
|
|
31
|
+
* ]
|
|
32
|
+
* })
|
|
33
|
+
*
|
|
34
|
+
* const ma = multiaddr('/memory/node-a')
|
|
35
|
+
*
|
|
36
|
+
* // dial the listener, timing out after 10s
|
|
37
|
+
* const connection = await dialer.dial(ma, {
|
|
38
|
+
* signal: AbortSignal.timeout(10_000)
|
|
39
|
+
* })
|
|
40
|
+
*
|
|
41
|
+
* // use connection...
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
import { ConnectionFailedError, serviceCapabilities, transportSymbol } from '@libp2p/interface';
|
|
45
|
+
import { Memory } from '@multiformats/multiaddr-matcher';
|
|
46
|
+
import { connections } from './connections.js';
|
|
47
|
+
import { MemoryTransportListener } from './listener.js';
|
|
48
|
+
export class MemoryTransport {
|
|
49
|
+
components;
|
|
50
|
+
init;
|
|
51
|
+
constructor(components, init = {}) {
|
|
52
|
+
this.components = components;
|
|
53
|
+
this.init = init;
|
|
54
|
+
}
|
|
55
|
+
[transportSymbol] = true;
|
|
56
|
+
[Symbol.toStringTag] = '@libp2p/memory';
|
|
57
|
+
[serviceCapabilities] = [
|
|
58
|
+
'@libp2p/transport'
|
|
59
|
+
];
|
|
60
|
+
async dial(ma, options) {
|
|
61
|
+
options.signal?.throwIfAborted();
|
|
62
|
+
const memoryConnection = connections.get(`${ma.getPeerId() == null ? ma : ma.decapsulate('/p2p')}`);
|
|
63
|
+
if (memoryConnection == null) {
|
|
64
|
+
throw new ConnectionFailedError(`No memory listener found at ${ma}`);
|
|
65
|
+
}
|
|
66
|
+
const maConn = await memoryConnection.dial(this.components.peerId);
|
|
67
|
+
try {
|
|
68
|
+
options.signal?.throwIfAborted();
|
|
69
|
+
return await options.upgrader.upgradeOutbound(maConn, {
|
|
70
|
+
...options,
|
|
71
|
+
...this.init.upgraderOptions
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
maConn.abort(err);
|
|
76
|
+
throw err;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Creates a TCP listener. The provided `handler` function will be called
|
|
81
|
+
* anytime a new incoming Connection has been successfully upgraded via
|
|
82
|
+
* `upgrader.upgradeInbound`.
|
|
83
|
+
*/
|
|
84
|
+
createListener(options) {
|
|
85
|
+
return new MemoryTransportListener(this.components, {
|
|
86
|
+
...options,
|
|
87
|
+
...this.init
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
listenFilter(multiaddrs) {
|
|
91
|
+
return multiaddrs.filter(ma => Memory.exactMatch(ma));
|
|
92
|
+
}
|
|
93
|
+
dialFilter(multiaddrs) {
|
|
94
|
+
return this.listenFilter(multiaddrs);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../../src/memory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAC/F,OAAO,EAAE,MAAM,EAAE,MAAM,iCAAiC,CAAA;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAKvD,MAAM,OAAO,eAAe;IACT,UAAU,CAA2B;IACrC,IAAI,CAAqB;IAE1C,YAAa,UAAqC,EAAE,OAA4B,EAAE;QAChF,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;IAEQ,CAAC,eAAe,CAAC,GAAG,IAAI,CAAA;IAExB,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,gBAAgB,CAAA;IAEvC,CAAC,mBAAmB,CAAC,GAAa;QACzC,mBAAmB;KACpB,CAAA;IAED,KAAK,CAAC,IAAI,CAAE,EAAa,EAAE,OAA6B;QACtD,OAAO,CAAC,MAAM,EAAE,cAAc,EAAE,CAAA;QAEhC,MAAM,gBAAgB,GAAG,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,SAAS,EAAE,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;QAEnG,IAAI,gBAAgB,IAAI,IAAI,EAAE,CAAC;YAC7B,MAAM,IAAI,qBAAqB,CAAC,+BAA+B,EAAE,EAAE,CAAC,CAAA;QACtE,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;QAElE,IAAI,CAAC;YACH,OAAO,CAAC,MAAM,EAAE,cAAc,EAAE,CAAA;YAEhC,OAAO,MAAM,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAC,MAAM,EAAE;gBACpD,GAAG,OAAO;gBACV,GAAG,IAAI,CAAC,IAAI,CAAC,eAAe;aAC7B,CAAC,CAAA;QACJ,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAClB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YACjB,MAAM,GAAG,CAAA;QACX,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,cAAc,CAAE,OAA8B;QAC5C,OAAO,IAAI,uBAAuB,CAAC,IAAI,CAAC,UAAU,EAAE;YAClD,GAAG,OAAO;YACV,GAAG,IAAI,CAAC,IAAI;SACb,CAAC,CAAA;IACJ,CAAC;IAED,YAAY,CAAE,UAAuB;QACnC,OAAO,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAA;IACvD,CAAC;IAED,UAAU,CAAE,UAAuB;QACjC,OAAO,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA;IACtC,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@libp2p/memory",
|
|
3
|
+
"version": "0.0.0-91687998d",
|
|
4
|
+
"description": "A memory transport for libp2p",
|
|
5
|
+
"license": "Apache-2.0 OR MIT",
|
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/transport-tcp#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/libp2p/js-libp2p.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/libp2p/js-libp2p/issues"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"provenance": true
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"types": "./dist/src/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"src",
|
|
22
|
+
"dist",
|
|
23
|
+
"!dist/test",
|
|
24
|
+
"!**/*.tsbuildinfo"
|
|
25
|
+
],
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/src/index.d.ts",
|
|
29
|
+
"import": "./dist/src/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"eslintConfig": {
|
|
33
|
+
"extends": "ipfs",
|
|
34
|
+
"parserOptions": {
|
|
35
|
+
"project": true,
|
|
36
|
+
"sourceType": "module"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"clean": "aegir clean",
|
|
41
|
+
"lint": "aegir lint",
|
|
42
|
+
"dep-check": "aegir dep-check",
|
|
43
|
+
"doc-check": "aegir doc-check",
|
|
44
|
+
"build": "aegir build",
|
|
45
|
+
"test": "aegir test -t node -t electron-main",
|
|
46
|
+
"test:chrome": "aegir test -t browser -f ./dist/test/browser.js --cov",
|
|
47
|
+
"test:chrome-webworker": "aegir test -t webworker -f ./dist/test/browser.js",
|
|
48
|
+
"test:firefox": "aegir test -t browser -f ./dist/test/browser.js -- --browser firefox",
|
|
49
|
+
"test:firefox-webworker": "aegir test -t webworker -f ./dist/test/browser.js -- --browser firefox",
|
|
50
|
+
"test:node": "aegir test -t node --cov",
|
|
51
|
+
"test:electron-main": "aegir test -t electron-main"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@libp2p/interface": "2.2.0-91687998d",
|
|
55
|
+
"@multiformats/multiaddr": "^12.2.3",
|
|
56
|
+
"@multiformats/multiaddr-matcher": "^1.5.0",
|
|
57
|
+
"@types/sinon": "^17.0.3",
|
|
58
|
+
"delay": "^6.0.0",
|
|
59
|
+
"it-map": "^3.1.1",
|
|
60
|
+
"it-pushable": "^3.2.3",
|
|
61
|
+
"nanoid": "^5.0.8",
|
|
62
|
+
"uint8arraylist": "^2.4.8"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@libp2p/crypto": "5.0.6-91687998d",
|
|
66
|
+
"@libp2p/interface-compliance-tests": "6.1.8-91687998d",
|
|
67
|
+
"@libp2p/logger": "5.1.3-91687998d",
|
|
68
|
+
"@libp2p/peer-id": "5.0.7-91687998d",
|
|
69
|
+
"aegir": "^44.0.1"
|
|
70
|
+
},
|
|
71
|
+
"browser": {
|
|
72
|
+
"./dist/src/tcp.js": "./dist/src/tcp.browser.js"
|
|
73
|
+
},
|
|
74
|
+
"sideEffects": false
|
|
75
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* A [libp2p transport](https://docs.libp2p.io/concepts/transports/overview/)
|
|
5
|
+
* that operates in-memory only.
|
|
6
|
+
*
|
|
7
|
+
* This is intended for testing and can only be used to connect two libp2p nodes
|
|
8
|
+
* that are running in the same process.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```TypeScript
|
|
13
|
+
* import { createLibp2p } from 'libp2p'
|
|
14
|
+
* import { memory } from '@libp2p/memory'
|
|
15
|
+
* import { multiaddr } from '@multiformats/multiaddr'
|
|
16
|
+
*
|
|
17
|
+
* const listener = await createLibp2p({
|
|
18
|
+
* addresses: {
|
|
19
|
+
* listen: [
|
|
20
|
+
* '/memory/node-a'
|
|
21
|
+
* ]
|
|
22
|
+
* },
|
|
23
|
+
* transports: [
|
|
24
|
+
* memory()
|
|
25
|
+
* ]
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* const dialer = await createLibp2p({
|
|
29
|
+
* transports: [
|
|
30
|
+
* memory()
|
|
31
|
+
* ]
|
|
32
|
+
* })
|
|
33
|
+
*
|
|
34
|
+
* const ma = multiaddr('/memory/node-a')
|
|
35
|
+
*
|
|
36
|
+
* // dial the listener, timing out after 10s
|
|
37
|
+
* const connection = await dialer.dial(ma, {
|
|
38
|
+
* signal: AbortSignal.timeout(10_000)
|
|
39
|
+
* })
|
|
40
|
+
*
|
|
41
|
+
* // use connection...
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
import { ConnectionFailedError } from '@libp2p/interface'
|
|
46
|
+
import { multiaddr } from '@multiformats/multiaddr'
|
|
47
|
+
import delay from 'delay'
|
|
48
|
+
import map from 'it-map'
|
|
49
|
+
import { pushable } from 'it-pushable'
|
|
50
|
+
import type { MemoryTransportComponents } from './index.js'
|
|
51
|
+
import type { MultiaddrConnection, PeerId } from '@libp2p/interface'
|
|
52
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
53
|
+
|
|
54
|
+
export const connections = new Map<string, MemoryConnection>()
|
|
55
|
+
|
|
56
|
+
interface MemoryConnectionHandler {
|
|
57
|
+
(maConn: MultiaddrConnection): void
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface MemoryConnectionInit {
|
|
61
|
+
onConnection: MemoryConnectionHandler
|
|
62
|
+
address: string
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class MemoryConnection {
|
|
66
|
+
private readonly components: MemoryTransportComponents
|
|
67
|
+
private readonly init: MemoryConnectionInit
|
|
68
|
+
private readonly connections: Set<MultiaddrConnection>
|
|
69
|
+
private latency: number
|
|
70
|
+
|
|
71
|
+
constructor (components: MemoryTransportComponents, init: MemoryConnectionInit) {
|
|
72
|
+
this.components = components
|
|
73
|
+
this.init = init
|
|
74
|
+
this.connections = new Set()
|
|
75
|
+
this.latency = 0
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async dial (dialingPeerId: PeerId): Promise<MultiaddrConnection> {
|
|
79
|
+
const dialerPushable = pushable<Uint8Array | Uint8ArrayList>()
|
|
80
|
+
const listenerPushable = pushable<Uint8Array | Uint8ArrayList>()
|
|
81
|
+
const self = this
|
|
82
|
+
|
|
83
|
+
const dialer: MultiaddrConnection = {
|
|
84
|
+
source: (async function * () {
|
|
85
|
+
yield * map(listenerPushable, async buf => {
|
|
86
|
+
await delay(self.latency)
|
|
87
|
+
return buf
|
|
88
|
+
})
|
|
89
|
+
})(),
|
|
90
|
+
sink: async (source) => {
|
|
91
|
+
for await (const buf of source) {
|
|
92
|
+
dialerPushable.push(buf)
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
close: async () => {
|
|
96
|
+
dialerPushable.end()
|
|
97
|
+
this.connections.delete(dialer)
|
|
98
|
+
dialer.timeline.close = Date.now()
|
|
99
|
+
|
|
100
|
+
listenerPushable.end()
|
|
101
|
+
this.connections.delete(listener)
|
|
102
|
+
listener.timeline.close = Date.now()
|
|
103
|
+
},
|
|
104
|
+
abort: (err) => {
|
|
105
|
+
dialerPushable.end(err)
|
|
106
|
+
this.connections.delete(dialer)
|
|
107
|
+
dialer.timeline.close = Date.now()
|
|
108
|
+
|
|
109
|
+
listenerPushable.end(err)
|
|
110
|
+
this.connections.delete(listener)
|
|
111
|
+
listener.timeline.close = Date.now()
|
|
112
|
+
},
|
|
113
|
+
timeline: {
|
|
114
|
+
open: Date.now()
|
|
115
|
+
},
|
|
116
|
+
remoteAddr: multiaddr(`${this.init.address}/p2p/${this.components.peerId}`),
|
|
117
|
+
log: this.components.logger.forComponent(`libp2p:memory:outgoing:${1}`)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const listener: MultiaddrConnection = {
|
|
121
|
+
source: (async function * () {
|
|
122
|
+
yield * map(dialerPushable, async buf => {
|
|
123
|
+
await delay(self.latency)
|
|
124
|
+
return buf
|
|
125
|
+
})
|
|
126
|
+
})(),
|
|
127
|
+
sink: async (source) => {
|
|
128
|
+
for await (const buf of source) {
|
|
129
|
+
listenerPushable.push(buf)
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
close: async () => {
|
|
133
|
+
listenerPushable.end()
|
|
134
|
+
this.connections.delete(listener)
|
|
135
|
+
listener.timeline.close = Date.now()
|
|
136
|
+
|
|
137
|
+
dialerPushable.end()
|
|
138
|
+
this.connections.delete(dialer)
|
|
139
|
+
dialer.timeline.close = Date.now()
|
|
140
|
+
},
|
|
141
|
+
abort: (err) => {
|
|
142
|
+
listenerPushable.end(err)
|
|
143
|
+
this.connections.delete(listener)
|
|
144
|
+
listener.timeline.close = Date.now()
|
|
145
|
+
|
|
146
|
+
dialerPushable.end(err)
|
|
147
|
+
this.connections.delete(dialer)
|
|
148
|
+
dialer.timeline.close = Date.now()
|
|
149
|
+
},
|
|
150
|
+
timeline: {
|
|
151
|
+
open: Date.now()
|
|
152
|
+
},
|
|
153
|
+
remoteAddr: multiaddr(`${this.init.address}-outgoing/p2p/${dialingPeerId}`),
|
|
154
|
+
log: this.components.logger.forComponent(`libp2p:memory:outgoing:${1}`)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
this.connections.add(dialer)
|
|
158
|
+
this.connections.add(listener)
|
|
159
|
+
|
|
160
|
+
await delay(this.latency)
|
|
161
|
+
|
|
162
|
+
this.init.onConnection(listener)
|
|
163
|
+
|
|
164
|
+
return dialer
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
close (): void {
|
|
168
|
+
[...this.connections].forEach(maConn => {
|
|
169
|
+
maConn.abort(new ConnectionFailedError('Memory Connection closed'))
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
setLatency (ms: number): void {
|
|
174
|
+
this.latency = ms
|
|
175
|
+
}
|
|
176
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* A [libp2p transport](https://docs.libp2p.io/concepts/transports/overview/)
|
|
5
|
+
* that operates in-memory only.
|
|
6
|
+
*
|
|
7
|
+
* This is intended for testing and can only be used to connect two libp2p nodes
|
|
8
|
+
* that are running in the same process.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```TypeScript
|
|
13
|
+
* import { createLibp2p } from 'libp2p'
|
|
14
|
+
* import { memory } from '@libp2p/memory'
|
|
15
|
+
* import { multiaddr } from '@multiformats/multiaddr'
|
|
16
|
+
*
|
|
17
|
+
* const listener = await createLibp2p({
|
|
18
|
+
* addresses: {
|
|
19
|
+
* listen: [
|
|
20
|
+
* '/memory/address-a'
|
|
21
|
+
* ]
|
|
22
|
+
* },
|
|
23
|
+
* transports: [
|
|
24
|
+
* memory()
|
|
25
|
+
* ]
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* const dialer = await createLibp2p({
|
|
29
|
+
* transports: [
|
|
30
|
+
* memory()
|
|
31
|
+
* ]
|
|
32
|
+
* })
|
|
33
|
+
*
|
|
34
|
+
* const ma = multiaddr('/memory/address-a')
|
|
35
|
+
*
|
|
36
|
+
* // dial the listener, timing out after 10s
|
|
37
|
+
* const connection = await dialer.dial(ma, {
|
|
38
|
+
* signal: AbortSignal.timeout(10_000)
|
|
39
|
+
* })
|
|
40
|
+
*
|
|
41
|
+
* // use connection...
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
import { MemoryTransport } from './memory.js'
|
|
46
|
+
import type { Transport, ComponentLogger, UpgraderOptions, PeerId } from '@libp2p/interface'
|
|
47
|
+
|
|
48
|
+
export interface MemoryTransportComponents {
|
|
49
|
+
peerId: PeerId
|
|
50
|
+
logger: ComponentLogger
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface MemoryTransportInit {
|
|
54
|
+
upgraderOptions?: UpgraderOptions
|
|
55
|
+
inboundUpgradeTimeout?: number
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function memory (init?: MemoryTransportInit): (components: MemoryTransportComponents) => Transport {
|
|
59
|
+
return (components) => {
|
|
60
|
+
return new MemoryTransport(components, init)
|
|
61
|
+
}
|
|
62
|
+
}
|