Skip to content

Commit

Permalink
refactor: Convert Socket to a class (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow committed Jul 19, 2019
1 parent 08b1b6b commit 3bdadef
Showing 1 changed file with 46 additions and 48 deletions.
94 changes: 46 additions & 48 deletions lib/socket.js
Expand Up @@ -2,62 +2,60 @@

const { EventEmitter } = require('events')
const debug = require('debug')('nock.socket')
const util = require('util')

module.exports = Socket

function Socket(options) {
EventEmitter.apply(this)
function noop() {}

if (options.proto === 'https') {
// https://github.com/nock/nock/issues/158
this.authorized = true
module.exports = class Socket extends EventEmitter {
constructor(options) {
super()

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
this.unref = 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
}

this.writable = true
this.readable = true
this.destroyed = false
this.connecting = false

this.setNoDelay = noop
this.setKeepAlive = noop
this.resume = noop
this.unref = 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
if (fn) {
this.once('timeout', fn)
setTimeout(timeoutMs, fn) {
this.timeoutMs = timeoutMs
if (fn) {
this.once('timeout', fn)
}
}
}

Socket.prototype.applyDelay = function applyDelay(delayMs) {
this.totalDelayMs += delayMs
applyDelay(delayMs) {
this.totalDelayMs += delayMs

if (this.timeoutMs && this.totalDelayMs > this.timeoutMs) {
debug('socket timeout')
this.emit('timeout')
if (this.timeoutMs && this.totalDelayMs > this.timeoutMs) {
debug('socket timeout')
this.emit('timeout')
}
}
}

Socket.prototype.getPeerCertificate = function getPeerCertificate() {
return Buffer.from((Math.random() * 10000 + Date.now()).toString()).toString(
'base64'
)
}
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
destroy() {
this.destroyed = true
this.readable = this.writable = false
}
}

function noop() {}

0 comments on commit 3bdadef

Please sign in to comment.