1 | 'use strict'
|
2 |
|
3 | const { EventEmitter } = require('events')
|
4 | const debug = require('debug')('nock.socket')
|
5 |
|
6 | module.exports = class Socket extends EventEmitter {
|
7 | constructor(options) {
|
8 | super()
|
9 |
|
10 | if (options.proto === 'https') {
|
11 |
|
12 | this.authorized = true
|
13 | }
|
14 |
|
15 | this.bufferSize = 0
|
16 | this.writable = true
|
17 | this.readable = true
|
18 | this.pending = false
|
19 | this.destroyed = false
|
20 | this.connecting = false
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | this.totalDelayMs = 0
|
26 |
|
27 | this.timeoutMs = null
|
28 |
|
29 | const ipv6 = options.family === 6
|
30 | this.remoteFamily = ipv6 ? 'IPv6' : 'IPv4'
|
31 | this.localAddress = this.remoteAddress = ipv6 ? '::1' : '127.0.0.1'
|
32 | this.localPort = this.remotePort = parseInt(options.port)
|
33 | }
|
34 |
|
35 | setNoDelay() {}
|
36 | setKeepAlive() {}
|
37 | resume() {}
|
38 | ref() {}
|
39 | unref() {}
|
40 |
|
41 | address() {
|
42 | return {
|
43 | port: this.remotePort,
|
44 | family: this.remoteFamily,
|
45 | address: this.remoteAddress,
|
46 | }
|
47 | }
|
48 |
|
49 | setTimeout(timeoutMs, fn) {
|
50 | this.timeoutMs = timeoutMs
|
51 | if (fn) {
|
52 | this.once('timeout', fn)
|
53 | }
|
54 | }
|
55 |
|
56 | applyDelay(delayMs) {
|
57 | this.totalDelayMs += delayMs
|
58 |
|
59 | if (this.timeoutMs && this.totalDelayMs > this.timeoutMs) {
|
60 | debug('socket timeout')
|
61 | this.emit('timeout')
|
62 | }
|
63 | }
|
64 |
|
65 | getPeerCertificate() {
|
66 | return Buffer.from(
|
67 | (Math.random() * 10000 + Date.now()).toString()
|
68 | ).toString('base64')
|
69 | }
|
70 |
|
71 | destroy(err) {
|
72 | this.destroyed = true
|
73 | this.readable = this.writable = false
|
74 | if (err) {
|
75 | this.emit('error', err)
|
76 | }
|
77 | return this
|
78 | }
|
79 | }
|