@libp2p/memory 0.0.0-02f285fc8

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.
@@ -0,0 +1,124 @@
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
+ * @example Simulating slow connections
45
+ *
46
+ * A `latency` argument can be passed to the factory. Each byte array that
47
+ * passes through the transport will be delayed by this many ms.
48
+ *
49
+ * ```TypeScript
50
+ * import { createLibp2p } from 'libp2p'
51
+ * import { memory } from '@libp2p/memory'
52
+ *
53
+ * const dialer = await createLibp2p({
54
+ * transports: [
55
+ * memory({
56
+ * latency: 100
57
+ * })
58
+ * ]
59
+ * })
60
+ * ```
61
+ */
62
+ import { ListenError, TypedEventEmitter } from '@libp2p/interface';
63
+ import { multiaddr } from '@multiformats/multiaddr';
64
+ import { nanoid } from 'nanoid';
65
+ import { MemoryConnection, connections } from './connections.js';
66
+ export class MemoryTransportListener extends TypedEventEmitter {
67
+ listenAddr;
68
+ connection;
69
+ components;
70
+ init;
71
+ constructor(components, init) {
72
+ super();
73
+ this.components = components;
74
+ this.init = init;
75
+ }
76
+ async listen(ma) {
77
+ const [[, value]] = ma.stringTuples();
78
+ const address = `/memory/${value ?? nanoid()}`;
79
+ if (value != null && connections.has(address)) {
80
+ throw new ListenError(`Memory address ${address} already in use`);
81
+ }
82
+ this.connection = new MemoryConnection(this.components, {
83
+ ...this.init,
84
+ onConnection: this.onConnection.bind(this),
85
+ address
86
+ });
87
+ this.listenAddr = multiaddr(address);
88
+ connections.set(address, this.connection);
89
+ queueMicrotask(() => {
90
+ this.safeDispatchEvent('listening');
91
+ });
92
+ }
93
+ onConnection(maConn) {
94
+ let signal;
95
+ if (this.init.inboundUpgradeTimeout != null) {
96
+ signal = AbortSignal.timeout(this.init.inboundUpgradeTimeout);
97
+ }
98
+ this.init.upgrader.upgradeInbound(maConn, {
99
+ ...this.init.upgraderOptions,
100
+ signal
101
+ })
102
+ .catch(err => {
103
+ maConn.abort(err);
104
+ });
105
+ }
106
+ getAddrs() {
107
+ if (this.listenAddr == null) {
108
+ return [];
109
+ }
110
+ return [
111
+ this.listenAddr
112
+ ];
113
+ }
114
+ async close() {
115
+ this.connection?.close();
116
+ if (this.listenAddr != null) {
117
+ connections.delete(this.listenAddr.toString());
118
+ }
119
+ queueMicrotask(() => {
120
+ this.safeDispatchEvent('close');
121
+ });
122
+ }
123
+ }
124
+ //# sourceMappingURL=listener.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"listener.js","sourceRoot":"","sources":["../../src/listener.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;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;AA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AI,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,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,22 @@
1
+ import { serviceCapabilities, transportSymbol } from '@libp2p/interface';
2
+ import type { MemoryTransportComponents, MemoryTransportInit } from './index.js';
3
+ import type { Connection, Transport, Listener, CreateListenerOptions, DialTransportOptions } from '@libp2p/interface';
4
+ import type { Multiaddr } from '@multiformats/multiaddr';
5
+ export declare class MemoryTransport implements Transport {
6
+ private readonly components;
7
+ private readonly init;
8
+ constructor(components: MemoryTransportComponents, init?: MemoryTransportInit);
9
+ readonly [transportSymbol] = true;
10
+ readonly [Symbol.toStringTag] = "@libp2p/memory";
11
+ readonly [serviceCapabilities]: string[];
12
+ dial(ma: Multiaddr, options: DialTransportOptions): Promise<Connection>;
13
+ /**
14
+ * Creates a TCP listener. The provided `handler` function will be called
15
+ * anytime a new incoming Connection has been successfully upgraded via
16
+ * `upgrader.upgradeInbound`.
17
+ */
18
+ createListener(options: CreateListenerOptions): Listener;
19
+ listenFilter(multiaddrs: Multiaddr[]): Multiaddr[];
20
+ dialFilter(multiaddrs: Multiaddr[]): Multiaddr[];
21
+ }
22
+ //# sourceMappingURL=memory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/memory.ts"],"names":[],"mappings":"AAAA,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,54 @@
1
+ import { ConnectionFailedError, serviceCapabilities, transportSymbol } from '@libp2p/interface';
2
+ import { Memory } from '@multiformats/multiaddr-matcher';
3
+ import { connections } from './connections.js';
4
+ import { MemoryTransportListener } from './listener.js';
5
+ export class MemoryTransport {
6
+ components;
7
+ init;
8
+ constructor(components, init = {}) {
9
+ this.components = components;
10
+ this.init = init;
11
+ }
12
+ [transportSymbol] = true;
13
+ [Symbol.toStringTag] = '@libp2p/memory';
14
+ [serviceCapabilities] = [
15
+ '@libp2p/transport'
16
+ ];
17
+ async dial(ma, options) {
18
+ options.signal?.throwIfAborted();
19
+ const memoryConnection = connections.get(`${ma.getPeerId() == null ? ma : ma.decapsulate('/p2p')}`);
20
+ if (memoryConnection == null) {
21
+ throw new ConnectionFailedError(`No memory listener found at ${ma}`);
22
+ }
23
+ const maConn = await memoryConnection.dial(this.components.peerId);
24
+ try {
25
+ options.signal?.throwIfAborted();
26
+ return await options.upgrader.upgradeOutbound(maConn, {
27
+ ...options,
28
+ ...this.init.upgraderOptions
29
+ });
30
+ }
31
+ catch (err) {
32
+ maConn.abort(err);
33
+ throw err;
34
+ }
35
+ }
36
+ /**
37
+ * Creates a TCP listener. The provided `handler` function will be called
38
+ * anytime a new incoming Connection has been successfully upgraded via
39
+ * `upgrader.upgradeInbound`.
40
+ */
41
+ createListener(options) {
42
+ return new MemoryTransportListener(this.components, {
43
+ ...options,
44
+ ...this.init
45
+ });
46
+ }
47
+ listenFilter(multiaddrs) {
48
+ return multiaddrs.filter(ma => Memory.exactMatch(ma));
49
+ }
50
+ dialFilter(multiaddrs) {
51
+ return this.listenFilter(multiaddrs);
52
+ }
53
+ }
54
+ //# sourceMappingURL=memory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.js","sourceRoot":"","sources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,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-02f285fc8",
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-02f285fc8",
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/logger": "5.1.3-02f285fc8",
66
+ "@libp2p/peer-id": "5.0.7-02f285fc8",
67
+ "aegir": "^44.0.1",
68
+ "sinon": "^19.0.2",
69
+ "sinon-ts": "^2.0.0"
70
+ },
71
+ "browser": {
72
+ "./dist/src/tcp.js": "./dist/src/tcp.browser.js"
73
+ },
74
+ "sideEffects": false
75
+ }
@@ -0,0 +1,178 @@
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, MemoryTransportInit } 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 extends MemoryTransportInit {
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 readonly 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 = init.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
+ if (self.latency > 0) {
87
+ await delay(self.latency)
88
+ }
89
+
90
+ return buf
91
+ })
92
+ })(),
93
+ sink: async (source) => {
94
+ for await (const buf of source) {
95
+ dialerPushable.push(buf)
96
+ }
97
+ },
98
+ close: async () => {
99
+ dialerPushable.end()
100
+ this.connections.delete(dialer)
101
+ dialer.timeline.close = Date.now()
102
+
103
+ listenerPushable.end()
104
+ this.connections.delete(listener)
105
+ listener.timeline.close = Date.now()
106
+ },
107
+ abort: (err) => {
108
+ dialerPushable.end(err)
109
+ this.connections.delete(dialer)
110
+ dialer.timeline.close = Date.now()
111
+
112
+ listenerPushable.end(err)
113
+ this.connections.delete(listener)
114
+ listener.timeline.close = Date.now()
115
+ },
116
+ timeline: {
117
+ open: Date.now()
118
+ },
119
+ remoteAddr: multiaddr(`${this.init.address}/p2p/${this.components.peerId}`),
120
+ log: this.components.logger.forComponent(`libp2p:memory:outgoing:${1}`)
121
+ }
122
+
123
+ const listener: MultiaddrConnection = {
124
+ source: (async function * () {
125
+ yield * map(dialerPushable, async buf => {
126
+ if (self.latency > 0) {
127
+ await delay(self.latency)
128
+ }
129
+
130
+ return buf
131
+ })
132
+ })(),
133
+ sink: async (source) => {
134
+ for await (const buf of source) {
135
+ listenerPushable.push(buf)
136
+ }
137
+ },
138
+ close: async () => {
139
+ listenerPushable.end()
140
+ this.connections.delete(listener)
141
+ listener.timeline.close = Date.now()
142
+
143
+ dialerPushable.end()
144
+ this.connections.delete(dialer)
145
+ dialer.timeline.close = Date.now()
146
+ },
147
+ abort: (err) => {
148
+ listenerPushable.end(err)
149
+ this.connections.delete(listener)
150
+ listener.timeline.close = Date.now()
151
+
152
+ dialerPushable.end(err)
153
+ this.connections.delete(dialer)
154
+ dialer.timeline.close = Date.now()
155
+ },
156
+ timeline: {
157
+ open: Date.now()
158
+ },
159
+ remoteAddr: multiaddr(`${this.init.address}-outgoing/p2p/${dialingPeerId}`),
160
+ log: this.components.logger.forComponent(`libp2p:memory:outgoing:${1}`)
161
+ }
162
+
163
+ this.connections.add(dialer)
164
+ this.connections.add(listener)
165
+
166
+ await delay(this.latency)
167
+
168
+ this.init.onConnection(listener)
169
+
170
+ return dialer
171
+ }
172
+
173
+ close (): void {
174
+ [...this.connections].forEach(maConn => {
175
+ maConn.abort(new ConnectionFailedError('Memory Connection closed'))
176
+ })
177
+ }
178
+ }
package/src/index.ts ADDED
@@ -0,0 +1,69 @@
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
+ * Add this much latency in ms to every buffer sent over the transport
59
+ *
60
+ * @default 0
61
+ */
62
+ latency?: number
63
+ }
64
+
65
+ export function memory (init?: MemoryTransportInit): (components: MemoryTransportComponents) => Transport {
66
+ return (components) => {
67
+ return new MemoryTransport(components, init)
68
+ }
69
+ }
@@ -0,0 +1,148 @@
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
+ * @example Simulating slow connections
45
+ *
46
+ * A `latency` argument can be passed to the factory. Each byte array that
47
+ * passes through the transport will be delayed by this many ms.
48
+ *
49
+ * ```TypeScript
50
+ * import { createLibp2p } from 'libp2p'
51
+ * import { memory } from '@libp2p/memory'
52
+ *
53
+ * const dialer = await createLibp2p({
54
+ * transports: [
55
+ * memory({
56
+ * latency: 100
57
+ * })
58
+ * ]
59
+ * })
60
+ * ```
61
+ */
62
+
63
+ import { ListenError, TypedEventEmitter } from '@libp2p/interface'
64
+ import { multiaddr } from '@multiformats/multiaddr'
65
+ import { nanoid } from 'nanoid'
66
+ import { MemoryConnection, connections } from './connections.js'
67
+ import type { MemoryTransportComponents, MemoryTransportInit } from './index.js'
68
+ import type { Listener, CreateListenerOptions, ListenerEvents, MultiaddrConnection, UpgraderOptions } from '@libp2p/interface'
69
+ import type { Multiaddr } from '@multiformats/multiaddr'
70
+
71
+ export interface MemoryTransportListenerInit extends CreateListenerOptions, MemoryTransportInit {
72
+ upgraderOptions?: UpgraderOptions
73
+ }
74
+
75
+ export class MemoryTransportListener extends TypedEventEmitter<ListenerEvents> implements Listener {
76
+ private listenAddr?: Multiaddr
77
+ private connection?: MemoryConnection
78
+ private readonly components: MemoryTransportComponents
79
+ private readonly init: MemoryTransportListenerInit
80
+
81
+ constructor (components: MemoryTransportComponents, init: MemoryTransportListenerInit) {
82
+ super()
83
+
84
+ this.components = components
85
+ this.init = init
86
+ }
87
+
88
+ async listen (ma: Multiaddr): Promise<void> {
89
+ const [[, value]] = ma.stringTuples()
90
+
91
+ const address = `/memory/${value ?? nanoid()}`
92
+
93
+ if (value != null && connections.has(address)) {
94
+ throw new ListenError(`Memory address ${address} already in use`)
95
+ }
96
+
97
+ this.connection = new MemoryConnection(this.components, {
98
+ ...this.init,
99
+ onConnection: this.onConnection.bind(this),
100
+ address
101
+ })
102
+ this.listenAddr = multiaddr(address)
103
+
104
+ connections.set(address, this.connection)
105
+
106
+ queueMicrotask(() => {
107
+ this.safeDispatchEvent('listening')
108
+ })
109
+ }
110
+
111
+ onConnection (maConn: MultiaddrConnection): void {
112
+ let signal: AbortSignal | undefined
113
+
114
+ if (this.init.inboundUpgradeTimeout != null) {
115
+ signal = AbortSignal.timeout(this.init.inboundUpgradeTimeout)
116
+ }
117
+
118
+ this.init.upgrader.upgradeInbound(maConn, {
119
+ ...this.init.upgraderOptions,
120
+ signal
121
+ })
122
+ .catch(err => {
123
+ maConn.abort(err)
124
+ })
125
+ }
126
+
127
+ getAddrs (): Multiaddr[] {
128
+ if (this.listenAddr == null) {
129
+ return []
130
+ }
131
+
132
+ return [
133
+ this.listenAddr
134
+ ]
135
+ }
136
+
137
+ async close (): Promise<void> {
138
+ this.connection?.close()
139
+
140
+ if (this.listenAddr != null) {
141
+ connections.delete(this.listenAddr.toString())
142
+ }
143
+
144
+ queueMicrotask(() => {
145
+ this.safeDispatchEvent('close')
146
+ })
147
+ }
148
+ }