@libp2p/memory 0.0.0 → 1.0.0-656db81cf

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/memory",
3
- "version": "0.0.0",
3
+ "version": "1.0.0-656db81cf",
4
4
  "description": "A memory transport for libp2p",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/transport-tcp#readme",
@@ -12,7 +12,8 @@
12
12
  "url": "https://github.com/libp2p/js-libp2p/issues"
13
13
  },
14
14
  "publishConfig": {
15
- "access": "public"
15
+ "access": "public",
16
+ "provenance": true
16
17
  },
17
18
  "type": "module",
18
19
  "types": "./dist/src/index.d.ts",
@@ -50,7 +51,7 @@
50
51
  "test:electron-main": "aegir test -t electron-main"
51
52
  },
52
53
  "dependencies": {
53
- "@libp2p/interface": "^2.2.0",
54
+ "@libp2p/interface": "2.2.0-656db81cf",
54
55
  "@multiformats/multiaddr": "^12.2.3",
55
56
  "@multiformats/multiaddr-matcher": "^1.5.0",
56
57
  "@types/sinon": "^17.0.3",
@@ -61,11 +62,11 @@
61
62
  "uint8arraylist": "^2.4.8"
62
63
  },
63
64
  "devDependencies": {
64
- "@libp2p/crypto": "^5.0.6",
65
- "@libp2p/interface-compliance-tests": "^6.1.8",
66
- "@libp2p/logger": "^5.1.3",
67
- "@libp2p/peer-id": "^5.0.7",
68
- "aegir": "^44.0.1"
65
+ "@libp2p/logger": "5.1.3-656db81cf",
66
+ "@libp2p/peer-id": "5.0.7-656db81cf",
67
+ "aegir": "^44.0.1",
68
+ "sinon": "^19.0.2",
69
+ "sinon-ts": "^2.0.0"
69
70
  },
70
71
  "browser": {
71
72
  "./dist/src/tcp.js": "./dist/src/tcp.browser.js"
@@ -47,7 +47,7 @@ import { multiaddr } from '@multiformats/multiaddr'
47
47
  import delay from 'delay'
48
48
  import map from 'it-map'
49
49
  import { pushable } from 'it-pushable'
50
- import type { MemoryTransportComponents } from './index.js'
50
+ import type { MemoryTransportComponents, MemoryTransportInit } from './index.js'
51
51
  import type { MultiaddrConnection, PeerId } from '@libp2p/interface'
52
52
  import type { Uint8ArrayList } from 'uint8arraylist'
53
53
 
@@ -57,7 +57,7 @@ interface MemoryConnectionHandler {
57
57
  (maConn: MultiaddrConnection): void
58
58
  }
59
59
 
60
- interface MemoryConnectionInit {
60
+ interface MemoryConnectionInit extends MemoryTransportInit {
61
61
  onConnection: MemoryConnectionHandler
62
62
  address: string
63
63
  }
@@ -66,13 +66,13 @@ export class MemoryConnection {
66
66
  private readonly components: MemoryTransportComponents
67
67
  private readonly init: MemoryConnectionInit
68
68
  private readonly connections: Set<MultiaddrConnection>
69
- private latency: number
69
+ private readonly latency: number
70
70
 
71
71
  constructor (components: MemoryTransportComponents, init: MemoryConnectionInit) {
72
72
  this.components = components
73
73
  this.init = init
74
74
  this.connections = new Set()
75
- this.latency = 0
75
+ this.latency = init.latency ?? 0
76
76
  }
77
77
 
78
78
  async dial (dialingPeerId: PeerId): Promise<MultiaddrConnection> {
@@ -83,7 +83,10 @@ export class MemoryConnection {
83
83
  const dialer: MultiaddrConnection = {
84
84
  source: (async function * () {
85
85
  yield * map(listenerPushable, async buf => {
86
- await delay(self.latency)
86
+ if (self.latency > 0) {
87
+ await delay(self.latency)
88
+ }
89
+
87
90
  return buf
88
91
  })
89
92
  })(),
@@ -120,7 +123,10 @@ export class MemoryConnection {
120
123
  const listener: MultiaddrConnection = {
121
124
  source: (async function * () {
122
125
  yield * map(dialerPushable, async buf => {
123
- await delay(self.latency)
126
+ if (self.latency > 0) {
127
+ await delay(self.latency)
128
+ }
129
+
124
130
  return buf
125
131
  })
126
132
  })(),
@@ -169,8 +175,4 @@ export class MemoryConnection {
169
175
  maConn.abort(new ConnectionFailedError('Memory Connection closed'))
170
176
  })
171
177
  }
172
-
173
- setLatency (ms: number): void {
174
- this.latency = ms
175
- }
176
178
  }
package/src/index.ts CHANGED
@@ -53,6 +53,13 @@ export interface MemoryTransportComponents {
53
53
  export interface MemoryTransportInit {
54
54
  upgraderOptions?: UpgraderOptions
55
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
56
63
  }
57
64
 
58
65
  export function memory (init?: MemoryTransportInit): (components: MemoryTransportComponents) => Transport {
package/src/listener.ts CHANGED
@@ -40,6 +40,24 @@
40
40
  *
41
41
  * // use connection...
42
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
+ * ```
43
61
  */
44
62
 
45
63
  import { ListenError, TypedEventEmitter } from '@libp2p/interface'
@@ -77,6 +95,7 @@ export class MemoryTransportListener extends TypedEventEmitter<ListenerEvents> i
77
95
  }
78
96
 
79
97
  this.connection = new MemoryConnection(this.components, {
98
+ ...this.init,
80
99
  onConnection: this.onConnection.bind(this),
81
100
  address
82
101
  })
@@ -100,12 +119,6 @@ export class MemoryTransportListener extends TypedEventEmitter<ListenerEvents> i
100
119
  ...this.init.upgraderOptions,
101
120
  signal
102
121
  })
103
- .then(connection => {
104
- this.init.handler?.(connection)
105
- this.safeDispatchEvent('connection', {
106
- detail: connection
107
- })
108
- })
109
122
  .catch(err => {
110
123
  maConn.abort(err)
111
124
  })
package/src/memory.ts CHANGED
@@ -1,47 +1,3 @@
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
1
  import { ConnectionFailedError, serviceCapabilities, transportSymbol } from '@libp2p/interface'
46
2
  import { Memory } from '@multiformats/multiaddr-matcher'
47
3
  import { connections } from './connections.js'