-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (136 loc) · 3.93 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
import tls from 'node:tls'
import net from 'node:net'
import { STATUS_CODES } from 'node:http'
const originalTlsConnect = tls.connect
tls.connect = function mockTlsConnect(...args) {
const [options, callback] = net._normalizeArgs(args)
return new MockTlsSocket(options, callback)
}
const OriginalSocket = net.Socket
const originalSocketConnect = net.Socket.prototype.connect
net.Socket.prototype.connect = function mockSocketConnect(...args) {
const [options, callback] = net._normalizeArgs(args)
return new MockSocket(options, callback)
}
class MockSocket extends net.Socket {
constructor(options, callback) {
super()
this.options = options
this.callback = callback
this.error = null
this.requestBuffer = []
this.connect()
}
connect() {
console.trace('MockSocket.connect()')
this.connecting = true
}
write(chunk, encoding, callback) {
// console.log('write:', chunk, encoding)
this.requestBuffer.push([chunk, encoding, callback])
// TODO: This will be signalled by the HTTP parser.
if (chunk === '') {
this.emit('request')
}
return false
}
push(chunk, encoding) {
console.log('push:', chunk)
if (chunk !== null) {
const buffer = Buffer.isBuffer(chunk)
? chunk
: Buffer.from(chunk, encoding)
this.emit('data', buffer)
} else {
this.emit('end')
}
return true
}
async passthrough() {
console.log('MockSocket.passthrough()')
// Establish the actual Socket connection.
// Applying it to this class will automatically
// forward all the writes/pushes/events.
const socket = originalSocketConnect.apply(this, [
this.options,
this.callback,
])
console.log('writing chunks', this.requestBuffer)
for (const [chunk, encoding, callback] of this.requestBuffer) {
socket.write(chunk, encoding, callback)
}
console.log('request written!')
}
/**
* Push this `Response` instance as an incoming response
* to this socket.
*/
async respondWith(response) {
this.emit('resume')
const httpHeaders = []
httpHeaders.push(
Buffer.from(
`HTTP/1.1 ${response.status} ${response.statusText || STATUS_CODES[response.status]}\r\n`,
),
)
for (const [name, value] of response.headers) {
httpHeaders.push(Buffer.from(`${name}: ${value}\r\n`))
}
if (response.body) {
httpHeaders.push(Buffer.from('\r\n'))
const reader = response.body.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
if (httpHeaders.length > 0) {
httpHeaders.push(Buffer.from(value))
this.push(Buffer.concat(httpHeaders))
httpHeaders.length = 0
continue
}
this.push(value)
}
}
this.push('\r\n')
this.push(null)
}
}
class MockTlsSocket extends tls.TLSSocket {
constructor(options, callback) {
const socket = new MockSocket(options, callback)
super(socket)
this.options = options
this.callback = callback
this.socket = socket
this.socket.on('connect', () => {
this.emit('secureConnect')
})
// TODO: Remove this, really.
this.socket.on('request', () => {
this.emit('request')
})
}
write(chunk, encoding, callback) {
this.socket.write(chunk, encoding, callback)
}
push(chunk) {
this.socket.push(chunk)
}
passthrough() {
console.log('TLS passthrough')
// this.socket.passthrough()
const tlsSocket = new tls.TLSSocket(new OriginalSocket(), this.options)
if (this.callback) {
tlsSocket.once('secureConnect', this.callback)
}
// originalTlsConnect.apply(this, [this.options, this.callback])
this.socket.on('data', (chunk) => this.emit('data', chunk))
this.socket.on('error', (error) => {
console.trace(error)
this.emit('error', error)
})
this.socket.on('end', () => this.emit('end'))
}
}