@libp2p/interface-compliance-tests 1.0.8 → 1.1.3

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.
Files changed (84) hide show
  1. package/dist/src/peer-discovery/index.d.ts.map +1 -1
  2. package/dist/src/peer-discovery/index.js +5 -5
  3. package/dist/src/peer-discovery/index.js.map +1 -1
  4. package/dist/src/pubsub/api.d.ts +2 -2
  5. package/dist/src/pubsub/api.d.ts.map +1 -1
  6. package/dist/src/pubsub/api.js +6 -6
  7. package/dist/src/pubsub/api.js.map +1 -1
  8. package/dist/src/pubsub/connection-handlers.d.ts +2 -2
  9. package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
  10. package/dist/src/pubsub/connection-handlers.js +18 -9
  11. package/dist/src/pubsub/connection-handlers.js.map +1 -1
  12. package/dist/src/pubsub/emit-self.d.ts +2 -2
  13. package/dist/src/pubsub/emit-self.d.ts.map +1 -1
  14. package/dist/src/pubsub/emit-self.js +6 -2
  15. package/dist/src/pubsub/emit-self.js.map +1 -1
  16. package/dist/src/pubsub/index.d.ts +9 -3
  17. package/dist/src/pubsub/index.d.ts.map +1 -1
  18. package/dist/src/pubsub/index.js.map +1 -1
  19. package/dist/src/pubsub/messages.d.ts +2 -2
  20. package/dist/src/pubsub/messages.d.ts.map +1 -1
  21. package/dist/src/pubsub/messages.js.map +1 -1
  22. package/dist/src/pubsub/multiple-nodes.d.ts +2 -2
  23. package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
  24. package/dist/src/pubsub/multiple-nodes.js +58 -35
  25. package/dist/src/pubsub/multiple-nodes.js.map +1 -1
  26. package/dist/src/pubsub/two-nodes.d.ts +2 -2
  27. package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
  28. package/dist/src/pubsub/two-nodes.js +44 -19
  29. package/dist/src/pubsub/two-nodes.js.map +1 -1
  30. package/dist/src/stream-muxer/base-test.d.ts.map +1 -1
  31. package/dist/src/stream-muxer/base-test.js.map +1 -1
  32. package/dist/src/transport/listen-test.d.ts.map +1 -1
  33. package/dist/src/transport/listen-test.js +10 -7
  34. package/dist/src/transport/listen-test.js.map +1 -1
  35. package/dist/src/transport/utils/index.d.ts.map +1 -1
  36. package/dist/src/transport/utils/index.js +4 -1
  37. package/dist/src/transport/utils/index.js.map +1 -1
  38. package/dist/src/utils/mock-connection-gater.d.ts +12 -0
  39. package/dist/src/utils/mock-connection-gater.d.ts.map +1 -0
  40. package/dist/src/utils/mock-connection-gater.js +14 -0
  41. package/dist/src/utils/mock-connection-gater.js.map +1 -0
  42. package/dist/src/utils/mock-connection-manager.d.ts +18 -0
  43. package/dist/src/utils/mock-connection-manager.d.ts.map +1 -0
  44. package/dist/src/utils/mock-connection-manager.js +22 -0
  45. package/dist/src/utils/mock-connection-manager.js.map +1 -0
  46. package/dist/src/utils/mock-connection.d.ts +5 -0
  47. package/dist/src/utils/mock-connection.d.ts.map +1 -0
  48. package/dist/src/utils/mock-connection.js +52 -0
  49. package/dist/src/utils/mock-connection.js.map +1 -0
  50. package/dist/src/utils/mock-multiaddr-connection.d.ts +4 -0
  51. package/dist/src/utils/mock-multiaddr-connection.d.ts.map +1 -0
  52. package/dist/src/utils/mock-multiaddr-connection.js +14 -0
  53. package/dist/src/utils/mock-multiaddr-connection.js.map +1 -0
  54. package/dist/src/utils/mock-muxer.d.ts +3 -0
  55. package/dist/src/utils/mock-muxer.d.ts.map +1 -0
  56. package/dist/src/utils/mock-muxer.js +37 -0
  57. package/dist/src/utils/mock-muxer.js.map +1 -0
  58. package/dist/src/utils/mock-registrar.d.ts +12 -0
  59. package/dist/src/utils/mock-registrar.d.ts.map +1 -0
  60. package/dist/src/utils/mock-registrar.js +27 -0
  61. package/dist/src/utils/mock-registrar.js.map +1 -0
  62. package/dist/src/utils/mock-upgrader.d.ts +7 -0
  63. package/dist/src/utils/mock-upgrader.d.ts.map +1 -0
  64. package/dist/src/utils/mock-upgrader.js +24 -0
  65. package/dist/src/utils/mock-upgrader.js.map +1 -0
  66. package/package.json +42 -8
  67. package/src/peer-discovery/index.ts +5 -7
  68. package/src/pubsub/api.ts +10 -11
  69. package/src/pubsub/connection-handlers.ts +22 -13
  70. package/src/pubsub/emit-self.ts +9 -5
  71. package/src/pubsub/index.ts +10 -3
  72. package/src/pubsub/messages.ts +3 -3
  73. package/src/pubsub/multiple-nodes.ts +68 -45
  74. package/src/pubsub/two-nodes.ts +48 -23
  75. package/src/stream-muxer/base-test.ts +9 -8
  76. package/src/transport/listen-test.ts +10 -7
  77. package/src/transport/utils/index.ts +12 -8
  78. package/src/utils/mock-connection-gater.ts +14 -0
  79. package/src/utils/mock-connection-manager.ts +30 -0
  80. package/src/utils/mock-connection.ts +66 -0
  81. package/src/utils/mock-multiaddr-connection.ts +18 -0
  82. package/src/utils/mock-muxer.ts +43 -0
  83. package/src/utils/mock-registrar.ts +35 -0
  84. package/src/utils/mock-upgrader.ts +33 -0
@@ -0,0 +1,18 @@
1
+ import { Multiaddr } from '@multiformats/multiaddr'
2
+ import type { MultiaddrConnection } from '@libp2p/interfaces/transport'
3
+ import type { Duplex } from 'it-stream-types'
4
+
5
+ export function mockMultiaddrConnection (source: Duplex<Uint8Array>): MultiaddrConnection {
6
+ const maConn: MultiaddrConnection = {
7
+ ...source,
8
+ async close () {
9
+
10
+ },
11
+ timeline: {
12
+ open: Date.now()
13
+ },
14
+ remoteAddr: new Multiaddr('/ip4/127.0.0.1/tcp/4001')
15
+ }
16
+
17
+ return maConn
18
+ }
@@ -0,0 +1,43 @@
1
+ import { pair } from 'it-pair'
2
+ import { pushable } from 'it-pushable'
3
+ import drain from 'it-drain'
4
+ import type { Stream } from '@libp2p/interfaces/connection'
5
+ import type { Muxer } from '@libp2p/interfaces/stream-muxer'
6
+
7
+ export function mockMuxer (): Muxer {
8
+ let streamId = 0
9
+ let streams: Stream[] = []
10
+ const p = pushable<Uint8Array>()
11
+
12
+ const muxer: Muxer = {
13
+ source: p,
14
+ sink: async (source) => {
15
+ await drain(source)
16
+ },
17
+ get streams () {
18
+ return streams
19
+ },
20
+ newStream: (name?: string) => {
21
+ const echo = pair<Uint8Array>()
22
+
23
+ const id = `${streamId++}`
24
+ const stream: Stream = {
25
+ id,
26
+ sink: echo.sink,
27
+ source: echo.source,
28
+ close: () => {
29
+ streams = streams.filter(s => s !== stream)
30
+ },
31
+ abort: () => {},
32
+ reset: () => {},
33
+ timeline: {
34
+ open: 0
35
+ }
36
+ }
37
+
38
+ return stream
39
+ }
40
+ }
41
+
42
+ return muxer
43
+ }
@@ -0,0 +1,35 @@
1
+ import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
2
+ import type { MulticodecTopology } from '../../../libp2p-topology/src/multicodec-topology'
3
+
4
+ export class MockRegistrar implements Registrar {
5
+ public readonly topologies: Map<string, MulticodecTopology> = new Map()
6
+ public readonly streamHandlers: Map<string, StreamHandler> = new Map()
7
+
8
+ async handle (multicodecs: string | string[], handler: StreamHandler) {
9
+ if (!Array.isArray(multicodecs)) {
10
+ multicodecs = [multicodecs]
11
+ }
12
+
13
+ this.streamHandlers.set(multicodecs[0], handler)
14
+ }
15
+
16
+ async unhandle (multicodec: string) {
17
+ this.streamHandlers.delete(multicodec)
18
+ }
19
+
20
+ register (topology: MulticodecTopology) {
21
+ const { multicodecs } = topology
22
+
23
+ this.topologies.set(multicodecs[0], topology)
24
+
25
+ return multicodecs[0]
26
+ }
27
+
28
+ unregister (id: string) {
29
+ this.topologies.delete(id)
30
+ }
31
+ }
32
+
33
+ export function mockRegistrar () {
34
+ return new MockRegistrar()
35
+ }
@@ -0,0 +1,33 @@
1
+ import { expect } from 'aegir/utils/chai.js'
2
+ import { mockMuxer } from './mock-muxer.js'
3
+ import { mockConnection } from './mock-connection.js'
4
+ import type { Upgrader, MultiaddrConnection } from '@libp2p/interfaces/transport'
5
+ import type { Muxer } from '@libp2p/interfaces/stream-muxer'
6
+
7
+ export interface MockUpgraderOptions {
8
+ muxer?: Muxer
9
+ }
10
+
11
+ export function mockUpgrader (options: MockUpgraderOptions = {}) {
12
+ const ensureProps = (multiaddrConnection: MultiaddrConnection) => {
13
+ ['sink', 'source', 'remoteAddr', 'timeline', 'close'].forEach(prop => {
14
+ expect(multiaddrConnection).to.have.property(prop)
15
+ })
16
+ return multiaddrConnection
17
+ }
18
+
19
+ const muxer = options.muxer ?? mockMuxer()
20
+
21
+ const upgrader: Upgrader = {
22
+ async upgradeOutbound (multiaddrConnection) {
23
+ ensureProps(multiaddrConnection)
24
+ return await mockConnection(multiaddrConnection, 'outbound', muxer)
25
+ },
26
+ async upgradeInbound (multiaddrConnection) {
27
+ ensureProps(multiaddrConnection)
28
+ return await mockConnection(multiaddrConnection, 'inbound', muxer)
29
+ }
30
+ }
31
+
32
+ return upgrader
33
+ }