-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
211 lines (164 loc) · 3.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* global Bare */
const { Duplex } = require('bare-stream')
const binding = require('./binding')
const constants = require('./lib/constants')
const errors = require('./lib/errors')
const readBufferSize = 65536
const context = binding.context()
exports.Socket = class TLSSocket extends Duplex {
static _buffer = Buffer.alloc(readBufferSize)
constructor(socket, opts = {}) {
const {
isServer = false,
cert = null,
key = null,
host = null,
eagerOpen = true,
allowHalfOpen = true
} = opts
super({ eagerOpen })
this._state = 0
this._socket = socket
this._key = key
this._cert = cert
this._allowHalfOpen = allowHalfOpen
this._pendingOpen = null
this._pendingWrite = null
this._buffer = null
this._handle = binding.init(
context,
isServer,
cert,
key,
host,
this,
this._onread,
this._onwrite
)
}
get socket() {
return this._socket
}
get encrypted() {
return true
}
_onconnect() {
this._state |= constants.state.HANDSHAKE
this.emit('connect')
const cb = this._pendingOpen
this._pendingOpen = null
cb(null)
}
_ondata(data) {
if (this._buffer !== null) {
this._buffer = Buffer.concat([this._buffer, data])
} else {
this._buffer = data
}
while (this._buffer !== null) {
if (this._state & constants.state.HANDSHAKE) {
let read
try {
read = binding.read(this._handle, TLSSocket._buffer)
} catch (err) {
return this.destroy(errors.from(err))
}
if (read < 0) break
if (read === 0) {
this.push(null)
if (this._allowHalfOpen === false) this.end()
return
}
const copy = Buffer.allocUnsafe(read)
copy.set(TLSSocket._buffer.subarray(0, read))
this.push(copy)
} else {
try {
if (binding.handshake(this._handle)) this._onconnect()
else break
} catch (err) {
if (this._pendingOpen) this._pendingOpen(errors.from(err))
else this.destroy(errors.from(err))
return
}
}
}
}
_ondrain() {
const cb = this._pendingWrite
this._pendingWrite = null
if (cb) cb(null)
}
_onend() {
this.push(null)
}
_onclose() {
this.destroy()
}
_onread(data) {
let buffer = this._buffer
if (buffer === null) return 0
if (buffer.byteLength > data.byteLength) {
const rest = buffer.subarray(data.byteLength)
buffer = buffer.subarray(0, data.byteLength)
this._buffer = rest
} else {
this._buffer = null
}
data.set(buffer)
return buffer.byteLength
}
_onwrite(data) {
data = Buffer.from(data)
if (this._socket.write(data)) this._pendingWrite = null
return data.byteLength
}
_open(cb) {
this._pendingOpen = cb
this._socket
.on('data', this._ondata.bind(this))
.on('drain', this._ondrain.bind(this))
.on('end', this._onend.bind(this))
.on('close', this._onclose.bind(this))
try {
if (binding.handshake(this._handle)) this._onconnect()
} catch (err) {
this._pendingOpen = null
cb(errors.from(err))
}
}
_write(data, encoding, cb) {
this._pendingWrite = cb
try {
binding.write(this._handle, data)
if (this._pendingWrite !== null) return
cb(null)
} catch (err) {
this._pendingWrite = null
cb(errors.from(err))
}
}
_final(cb) {
try {
binding.shutdown(this._handle)
cb(null)
} catch (err) {
cb(err)
}
this._socket.end()
}
_predestroy() {
binding.destroy(this._handle)
this._handle = null
}
_destroy(err, cb) {
if (this._handle) {
binding.destroy(this._handle)
this._handle = null
}
cb(err)
}
}
exports.TLSSocket = exports.Socket // For Node.js compatibility
exports.constants = constants
exports.errors = errors