-
-
Notifications
You must be signed in to change notification settings - Fork 733
/
socket.js
70 lines (56 loc) · 1.69 KB
/
socket.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
'use strict'
const { EventEmitter } = require('events')
const debug = require('debug')('nock.socket')
const util = require('util')
module.exports = Socket
function Socket(options) {
if (!(this instanceof Socket)) {
return new Socket(options)
}
EventEmitter.apply(this)
if (options.proto === 'https') {
// https://github.com/nock/nock/issues/158
this.authorized = true
}
this.writable = true
this.readable = true
this.destroyed = false
this.connecting = false
this.setNoDelay = noop
this.setKeepAlive = noop
this.resume = noop
// totalDelay that has already been applied to the current
// request/connection, timeout error will be generated if
// it is timed-out.
this.totalDelayMs = 0
// Maximum allowed delay. Null means unlimited.
this.timeoutMs = null
}
util.inherits(Socket, EventEmitter)
Socket.prototype.setTimeout = function setTimeout(timeoutMs, fn) {
this.timeoutMs = timeoutMs
this.timeoutFunction = fn
}
Socket.prototype.applyDelay = function applyDelay(delayMs) {
this.totalDelayMs += delayMs
if (this.timeoutMs && this.totalDelayMs > this.timeoutMs) {
debug('socket timeout')
// TODO-coverage: Rewrite this so it always emits. In `setTimeout()`, if a
// timeout function is passed, register it using `this.once('timeout')`.
if (this.timeoutFunction) {
this.timeoutFunction()
} else {
this.emit('timeout')
}
}
}
Socket.prototype.getPeerCertificate = function getPeerCertificate() {
return Buffer.from((Math.random() * 10000 + Date.now()).toString()).toString(
'base64'
)
}
Socket.prototype.destroy = function destroy() {
this.destroyed = true
this.readable = this.writable = false
}
function noop() {}