@libp2p/memory 0.0.0-adc767899
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
package/src/listener.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
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 { ListenError, TypedEventEmitter } from '@libp2p/interface'
|
|
46
|
+
import { multiaddr } from '@multiformats/multiaddr'
|
|
47
|
+
import { nanoid } from 'nanoid'
|
|
48
|
+
import { MemoryConnection, connections } from './connections.js'
|
|
49
|
+
import type { MemoryTransportComponents, MemoryTransportInit } from './index.js'
|
|
50
|
+
import type { Listener, CreateListenerOptions, ListenerEvents, MultiaddrConnection, UpgraderOptions } from '@libp2p/interface'
|
|
51
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
52
|
+
|
|
53
|
+
export interface MemoryTransportListenerInit extends CreateListenerOptions, MemoryTransportInit {
|
|
54
|
+
upgraderOptions?: UpgraderOptions
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class MemoryTransportListener extends TypedEventEmitter<ListenerEvents> implements Listener {
|
|
58
|
+
private listenAddr?: Multiaddr
|
|
59
|
+
private connection?: MemoryConnection
|
|
60
|
+
private readonly components: MemoryTransportComponents
|
|
61
|
+
private readonly init: MemoryTransportListenerInit
|
|
62
|
+
|
|
63
|
+
constructor (components: MemoryTransportComponents, init: MemoryTransportListenerInit) {
|
|
64
|
+
super()
|
|
65
|
+
|
|
66
|
+
this.components = components
|
|
67
|
+
this.init = init
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async listen (ma: Multiaddr): Promise<void> {
|
|
71
|
+
const [[, value]] = ma.stringTuples()
|
|
72
|
+
|
|
73
|
+
const address = `/memory/${value ?? nanoid()}`
|
|
74
|
+
|
|
75
|
+
if (value != null && connections.has(address)) {
|
|
76
|
+
throw new ListenError(`Memory address ${address} already in use`)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
this.connection = new MemoryConnection(this.components, {
|
|
80
|
+
onConnection: this.onConnection.bind(this),
|
|
81
|
+
address
|
|
82
|
+
})
|
|
83
|
+
this.listenAddr = multiaddr(address)
|
|
84
|
+
|
|
85
|
+
connections.set(address, this.connection)
|
|
86
|
+
|
|
87
|
+
queueMicrotask(() => {
|
|
88
|
+
this.safeDispatchEvent('listening')
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
onConnection (maConn: MultiaddrConnection): void {
|
|
93
|
+
let signal: AbortSignal | undefined
|
|
94
|
+
|
|
95
|
+
if (this.init.inboundUpgradeTimeout != null) {
|
|
96
|
+
signal = AbortSignal.timeout(this.init.inboundUpgradeTimeout)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this.init.upgrader.upgradeInbound(maConn, {
|
|
100
|
+
...this.init.upgraderOptions,
|
|
101
|
+
signal
|
|
102
|
+
})
|
|
103
|
+
.then(connection => {
|
|
104
|
+
this.init.handler?.(connection)
|
|
105
|
+
this.safeDispatchEvent('connection', {
|
|
106
|
+
detail: connection
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
.catch(err => {
|
|
110
|
+
maConn.abort(err)
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
getAddrs (): Multiaddr[] {
|
|
115
|
+
if (this.listenAddr == null) {
|
|
116
|
+
return []
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return [
|
|
120
|
+
this.listenAddr
|
|
121
|
+
]
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async close (): Promise<void> {
|
|
125
|
+
this.connection?.close()
|
|
126
|
+
|
|
127
|
+
if (this.listenAddr != null) {
|
|
128
|
+
connections.delete(this.listenAddr.toString())
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
queueMicrotask(() => {
|
|
132
|
+
this.safeDispatchEvent('close')
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
}
|
package/src/memory.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
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, serviceCapabilities, transportSymbol } from '@libp2p/interface'
|
|
46
|
+
import { Memory } from '@multiformats/multiaddr-matcher'
|
|
47
|
+
import { connections } from './connections.js'
|
|
48
|
+
import { MemoryTransportListener } from './listener.js'
|
|
49
|
+
import type { MemoryTransportComponents, MemoryTransportInit } from './index.js'
|
|
50
|
+
import type { Connection, Transport, Listener, CreateListenerOptions, DialTransportOptions } from '@libp2p/interface'
|
|
51
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
52
|
+
|
|
53
|
+
export class MemoryTransport implements Transport {
|
|
54
|
+
private readonly components: MemoryTransportComponents
|
|
55
|
+
private readonly init: MemoryTransportInit
|
|
56
|
+
|
|
57
|
+
constructor (components: MemoryTransportComponents, init: MemoryTransportInit = {}) {
|
|
58
|
+
this.components = components
|
|
59
|
+
this.init = init
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
readonly [transportSymbol] = true
|
|
63
|
+
|
|
64
|
+
readonly [Symbol.toStringTag] = '@libp2p/memory'
|
|
65
|
+
|
|
66
|
+
readonly [serviceCapabilities]: string[] = [
|
|
67
|
+
'@libp2p/transport'
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
async dial (ma: Multiaddr, options: DialTransportOptions): Promise<Connection> {
|
|
71
|
+
options.signal?.throwIfAborted()
|
|
72
|
+
|
|
73
|
+
const memoryConnection = connections.get(`${ma.getPeerId() == null ? ma : ma.decapsulate('/p2p')}`)
|
|
74
|
+
|
|
75
|
+
if (memoryConnection == null) {
|
|
76
|
+
throw new ConnectionFailedError(`No memory listener found at ${ma}`)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const maConn = await memoryConnection.dial(this.components.peerId)
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
options.signal?.throwIfAborted()
|
|
83
|
+
|
|
84
|
+
return await options.upgrader.upgradeOutbound(maConn, {
|
|
85
|
+
...options,
|
|
86
|
+
...this.init.upgraderOptions
|
|
87
|
+
})
|
|
88
|
+
} catch (err: any) {
|
|
89
|
+
maConn.abort(err)
|
|
90
|
+
throw err
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates a TCP listener. The provided `handler` function will be called
|
|
96
|
+
* anytime a new incoming Connection has been successfully upgraded via
|
|
97
|
+
* `upgrader.upgradeInbound`.
|
|
98
|
+
*/
|
|
99
|
+
createListener (options: CreateListenerOptions): Listener {
|
|
100
|
+
return new MemoryTransportListener(this.components, {
|
|
101
|
+
...options,
|
|
102
|
+
...this.init
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
listenFilter (multiaddrs: Multiaddr[]): Multiaddr[] {
|
|
107
|
+
return multiaddrs.filter(ma => Memory.exactMatch(ma))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
dialFilter (multiaddrs: Multiaddr[]): Multiaddr[] {
|
|
111
|
+
return this.listenFilter(multiaddrs)
|
|
112
|
+
}
|
|
113
|
+
}
|