blind-peer-muxer 1.3.0 → 1.5.0
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/index.js +16 -2
- package/package.json +2 -2
- package/spec/hyperschema/index.js +25 -0
- package/spec/hyperschema/schema.json +13 -0
package/index.js
CHANGED
|
@@ -3,15 +3,29 @@ const { BlindPeerRequest: NotificationRequest } = require('blind-push/encodings'
|
|
|
3
3
|
const Protomux = require('protomux')
|
|
4
4
|
|
|
5
5
|
const Cores = getEncoding('@blind-peer/cores')
|
|
6
|
+
const Handshake = getEncoding('@blind-peer/handshake')
|
|
7
|
+
// The wrapper makes it work seamlessly if the other side does not have a handshake
|
|
8
|
+
// We default the value to a real decode, which will create a default object based on hyperschema config.
|
|
9
|
+
// This way there is no need to handle special null states by blind-peer-muxer users.
|
|
10
|
+
const HandshakeWrapped = {
|
|
11
|
+
preencode: Handshake.preencode,
|
|
12
|
+
encode: Handshake.encode,
|
|
13
|
+
decode(state) {
|
|
14
|
+
return state.start >= state.end
|
|
15
|
+
? Handshake.decode({ buffer: Buffer.alloc(1), start: 0, end: 1 })
|
|
16
|
+
: Handshake.decode(state)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
6
19
|
|
|
7
20
|
module.exports = class BlindPeerChannel {
|
|
8
21
|
constructor(
|
|
9
22
|
stream,
|
|
10
|
-
{ oncores = noop, onnotification = noop, onopen = noop, onclose = noop } = {}
|
|
23
|
+
{ handshake = {}, oncores = noop, onnotification = noop, onopen = noop, onclose = noop } = {}
|
|
11
24
|
) {
|
|
12
25
|
this.muxer = Protomux.from(stream)
|
|
13
26
|
this.channel = this.muxer.createChannel({
|
|
14
27
|
protocol: 'blind-peer',
|
|
28
|
+
handshake: HandshakeWrapped,
|
|
15
29
|
messages: [
|
|
16
30
|
{ encoding: Cores, onmessage: oncores },
|
|
17
31
|
{ encoding: NotificationRequest, onmessage: onnotification }
|
|
@@ -21,7 +35,7 @@ module.exports = class BlindPeerChannel {
|
|
|
21
35
|
})
|
|
22
36
|
this.wireCores = this.channel.messages[0]
|
|
23
37
|
this.wireNotification = this.channel.messages[1]
|
|
24
|
-
this.channel.open()
|
|
38
|
+
this.channel.open(handshake)
|
|
25
39
|
}
|
|
26
40
|
|
|
27
41
|
get stream() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blind-peer-muxer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Protomux channel muxer for blind peers",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"prettier-config-holepunch": "^2.0.0"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"blind-push": "^0.
|
|
35
|
+
"blind-push": "^1.0.1",
|
|
36
36
|
"compact-encoding": "^3.0.0",
|
|
37
37
|
"hyperschema": "^1.19.0",
|
|
38
38
|
"protomux": "^3.10.1"
|
|
@@ -65,6 +65,29 @@ const encoding1 = {
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
// @blind-peer/handshake
|
|
69
|
+
const encoding2 = {
|
|
70
|
+
preencode(state, m) {
|
|
71
|
+
state.end++ // max flag is 1 so always one byte
|
|
72
|
+
|
|
73
|
+
if (m.blindPeeringVersion) c.string.preencode(state, m.blindPeeringVersion)
|
|
74
|
+
},
|
|
75
|
+
encode(state, m) {
|
|
76
|
+
const flags = m.blindPeeringVersion ? 1 : 0
|
|
77
|
+
|
|
78
|
+
c.uint.encode(state, flags)
|
|
79
|
+
|
|
80
|
+
if (m.blindPeeringVersion) c.string.encode(state, m.blindPeeringVersion)
|
|
81
|
+
},
|
|
82
|
+
decode(state) {
|
|
83
|
+
const flags = c.uint.decode(state)
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
blindPeeringVersion: (flags & 1) !== 0 ? c.string.decode(state) : null
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
68
91
|
function setVersion(v) {
|
|
69
92
|
version = v
|
|
70
93
|
}
|
|
@@ -92,6 +115,8 @@ function getEncoding(name) {
|
|
|
92
115
|
return encoding0
|
|
93
116
|
case '@blind-peer/cores':
|
|
94
117
|
return encoding1
|
|
118
|
+
case '@blind-peer/handshake':
|
|
119
|
+
return encoding2
|
|
95
120
|
default:
|
|
96
121
|
throw new Error('Encoder not found ' + name)
|
|
97
122
|
}
|
|
@@ -50,6 +50,19 @@
|
|
|
50
50
|
"version": 1
|
|
51
51
|
}
|
|
52
52
|
]
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"name": "handshake",
|
|
56
|
+
"namespace": "blind-peer",
|
|
57
|
+
"compact": false,
|
|
58
|
+
"flagsPosition": 0,
|
|
59
|
+
"fields": [
|
|
60
|
+
{
|
|
61
|
+
"name": "blindPeeringVersion",
|
|
62
|
+
"type": "string",
|
|
63
|
+
"version": 1
|
|
64
|
+
}
|
|
65
|
+
]
|
|
53
66
|
}
|
|
54
67
|
]
|
|
55
68
|
}
|