mock-tls-server 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +20 -0
- package/README.md +103 -0
- package/lib/ca.js +135 -0
- package/lib/index.js +437 -0
- package/lib/kernel.js +75 -0
- package/lib/net.js +272 -0
- package/lib/socket.js +115 -0
- package/lib/utils.js +91 -0
- package/package.json +50 -0
- package/types/ca.d.ts +74 -0
- package/types/index.d.ts +812 -0
- package/types/kernel.d.ts +32 -0
- package/types/net.d.ts +158 -0
- package/types/socket.d.ts +37 -0
- package/types/utils.d.ts +34 -0
package/lib/net.js
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import {extendedError, log, normalizeArgs} from './utils.js'
|
|
2
|
+
import {EventEmitter} from 'events'
|
|
3
|
+
import {MockSocket} from './socket.js'
|
|
4
|
+
import {globalKernel} from './kernel.js'
|
|
5
|
+
import stream from 'stream'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} SocketConnectOpts
|
|
9
|
+
* @property {number} port Listening server port to connect to.
|
|
10
|
+
* @property {boolean} [allowHalfOpen=false] If set to false, then the
|
|
11
|
+
* socket will automatically end the writable side when the readable side
|
|
12
|
+
* ends.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {object} ServerOpts
|
|
17
|
+
* @property {boolean} [allowHalfOpen=false] If set to false, then the
|
|
18
|
+
* socket will automatically end the writable side when the readable side
|
|
19
|
+
* ends.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Initiate a connection to a given server port.
|
|
24
|
+
*
|
|
25
|
+
* @param {number|SocketConnectOpts} options The server port to connect to.
|
|
26
|
+
* Must already be listening.
|
|
27
|
+
* @param {SocketCallback} [connectListener] Event listener for the 'connect'
|
|
28
|
+
* event.
|
|
29
|
+
* @returns {stream.Duplex} The client side HalfSocket.
|
|
30
|
+
* @throws {TypeError} Invalid type for port.
|
|
31
|
+
*/
|
|
32
|
+
export function connect(options, connectListener) {
|
|
33
|
+
const opts = normalizeArgs(options, {connectListener})
|
|
34
|
+
const {port, connectListener: listener, ...rest} = opts
|
|
35
|
+
|
|
36
|
+
if (typeof port !== 'number') {
|
|
37
|
+
throw new TypeError('Invalid type for port')
|
|
38
|
+
}
|
|
39
|
+
const ms = new MockSocket(rest)
|
|
40
|
+
const srv = /** @type {Server} */ (globalKernel.lookup(port))
|
|
41
|
+
|
|
42
|
+
if (srv) {
|
|
43
|
+
srv._addClient(ms)
|
|
44
|
+
if (listener) {
|
|
45
|
+
ms.clientSocket.on('connect', listener)
|
|
46
|
+
}
|
|
47
|
+
process.nextTick(() => ms.clientSocket.emit('connect'))
|
|
48
|
+
} else {
|
|
49
|
+
const er = extendedError(`Error: connect ECONNREFUSED ${port}`, {
|
|
50
|
+
code: 'ECONNREFUSED',
|
|
51
|
+
port,
|
|
52
|
+
})
|
|
53
|
+
process.nextTick(() => ms.clientSocket.emit('error', er))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return ms.clientSocket
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get the Server associated with a port.
|
|
61
|
+
*
|
|
62
|
+
* @param {SocketConnectOpts|number} options Port to look up.
|
|
63
|
+
* @returns {Server} The server that was found, or null if none found.
|
|
64
|
+
*/
|
|
65
|
+
export function getServer(options) {
|
|
66
|
+
const opts = normalizeArgs(options)
|
|
67
|
+
return /** @type {Server} */ (globalKernel.lookup(opts.port))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @callback SocketCallback
|
|
72
|
+
* @param {stream.Duplex} socket Server side of socket that was just connected.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A mock server socket, more-or-less like net.Server.
|
|
77
|
+
*/
|
|
78
|
+
export class Server extends EventEmitter {
|
|
79
|
+
/**
|
|
80
|
+
* Creates an instance of Server.
|
|
81
|
+
*
|
|
82
|
+
* @param {ServerOpts|SocketCallback} [options] Options for the server.
|
|
83
|
+
* @param {SocketCallback} [connectionListener] Register this as the event
|
|
84
|
+
* listener for the 'listening' event, if provided.
|
|
85
|
+
*/
|
|
86
|
+
constructor(options, connectionListener) {
|
|
87
|
+
super()
|
|
88
|
+
const opts = normalizeArgs(options, {connectionListener})
|
|
89
|
+
|
|
90
|
+
this.clients = new Set()
|
|
91
|
+
this.port = null
|
|
92
|
+
this.listening = false
|
|
93
|
+
this.closing = false
|
|
94
|
+
|
|
95
|
+
if (opts.connectionListener) {
|
|
96
|
+
this.on('connection', opts.connectionListener)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Add debug logging to EventEmitter. Start with NODE_DEBUG=mock-tls-server
|
|
102
|
+
* to see all events.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} eventName The name of the event.
|
|
105
|
+
* @param {any[]} args The arguments to be passed to listeners.
|
|
106
|
+
* @returns {boolean} Returns `true` if the event had listeners, `false`
|
|
107
|
+
* otherwise.
|
|
108
|
+
*/
|
|
109
|
+
emit(eventName, ...args) {
|
|
110
|
+
log(this.constructor.name, eventName, ...args.map(
|
|
111
|
+
o => ((typeof o === 'object') ? o.constructor.name || o.toString() : o)
|
|
112
|
+
))
|
|
113
|
+
return super.emit(eventName, ...args)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
address() {
|
|
117
|
+
return (this.port === null) ? null : `mock:${this.port}`
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @typedef {object} ListenOptions
|
|
122
|
+
* @property {number} port The port to listen on.
|
|
123
|
+
* @property {Function} listeningListener If it exists, installed as
|
|
124
|
+
* a listener for the 'listening' event.
|
|
125
|
+
*/
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Listen for incoming connections. Whenever `connect` is called on this
|
|
129
|
+
* server's port, create a mock socket wrapped in a TLSSocket.
|
|
130
|
+
*
|
|
131
|
+
* @param {number|ListenOptions} [options] Port number or options object.
|
|
132
|
+
* @param {Function} [listeningListener] If it exists, installed as
|
|
133
|
+
* a listener for the 'listening' event.
|
|
134
|
+
* @returns {Server} This.
|
|
135
|
+
*/
|
|
136
|
+
listen(options, listeningListener) {
|
|
137
|
+
const opts = normalizeArgs(options, {listeningListener})
|
|
138
|
+
|
|
139
|
+
if (this.listening) {
|
|
140
|
+
const er = extendedError('Server already listening', {
|
|
141
|
+
code: 'ERR_SERVER_ALREADY_LISTEN',
|
|
142
|
+
})
|
|
143
|
+
process.nextTick(() => this.emit('error', er))
|
|
144
|
+
return this
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
this.port = globalKernel.open(opts.port, this)
|
|
149
|
+
} catch (er) {
|
|
150
|
+
process.nextTick(() => this.emit('error', er))
|
|
151
|
+
return this
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (opts.listeningListener) {
|
|
155
|
+
this.on('listening', opts.listeningListener)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
this.listening = true
|
|
159
|
+
process.nextTick(() => {
|
|
160
|
+
this.emit('listening')
|
|
161
|
+
})
|
|
162
|
+
return this
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Add a client to this server.
|
|
167
|
+
*
|
|
168
|
+
* @param {MockSocket} ms The MockSocket to add.
|
|
169
|
+
* @returns {Server} This.
|
|
170
|
+
* @throws {Error} Server not listening.
|
|
171
|
+
* @ignore
|
|
172
|
+
*/
|
|
173
|
+
_addClient(ms) {
|
|
174
|
+
if (!this.listening) {
|
|
175
|
+
throw new Error('Server not listening')
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
this.clients.add(ms)
|
|
179
|
+
for (const sock of [ms.serverSocket, ms.clientSocket]) {
|
|
180
|
+
for (const event of ['close', 'error']) {
|
|
181
|
+
sock.on(event, () => {
|
|
182
|
+
this.clients.delete(ms)
|
|
183
|
+
this._checkClosed()
|
|
184
|
+
})
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
this.emit('connection', ms.serverSocket)
|
|
189
|
+
return this
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* If all of the clients have closed, clean up and emit the 'close' event.
|
|
194
|
+
*/
|
|
195
|
+
_checkClosed() {
|
|
196
|
+
if (this.closing && (this.clients.size === 0)) {
|
|
197
|
+
this.closing = false
|
|
198
|
+
this.port = null
|
|
199
|
+
process.nextTick(() => this.emit('close'))
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* @callback PlainCallback
|
|
205
|
+
* @returns {void}
|
|
206
|
+
*/
|
|
207
|
+
/**
|
|
208
|
+
* Close down all of the clients, then the server.
|
|
209
|
+
*
|
|
210
|
+
* @param {PlainCallback} [cb] Registered as listener for the 'close' event.
|
|
211
|
+
* @returns {Server} This.
|
|
212
|
+
*/
|
|
213
|
+
close(cb) {
|
|
214
|
+
if (cb) {
|
|
215
|
+
this.on('close', cb)
|
|
216
|
+
}
|
|
217
|
+
this.listening = false
|
|
218
|
+
this.closing = true
|
|
219
|
+
for (const cli of this.clients) {
|
|
220
|
+
cli.serverSocket.end()
|
|
221
|
+
}
|
|
222
|
+
this._checkClosed()
|
|
223
|
+
return this
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @callback CountCallback
|
|
228
|
+
* @param {Error} [err] If not null, an error occurred.
|
|
229
|
+
* @param {number} [count] The number of connected clients.
|
|
230
|
+
* @returns {void}
|
|
231
|
+
*/
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Get the number of current clients.
|
|
235
|
+
*
|
|
236
|
+
* @param {CountCallback} cb Called with the current number of clients.
|
|
237
|
+
* @returns {Server} This.
|
|
238
|
+
* @throws {TypeError} Callback not a function.
|
|
239
|
+
*/
|
|
240
|
+
getConnections(cb) {
|
|
241
|
+
if (typeof cb !== 'function') {
|
|
242
|
+
throw new TypeError('Callback not a function')
|
|
243
|
+
}
|
|
244
|
+
process.nextTick(() => {
|
|
245
|
+
if (this.listening) {
|
|
246
|
+
cb(null, this.clients.size)
|
|
247
|
+
} else {
|
|
248
|
+
cb(new Error('Not listening'))
|
|
249
|
+
}
|
|
250
|
+
})
|
|
251
|
+
return this
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Creates an instance of Server.
|
|
257
|
+
*
|
|
258
|
+
* @param {ServerOpts|SocketCallback} [opts] Options for the server.
|
|
259
|
+
* @param {SocketCallback} [connectionListener] Register this as the event
|
|
260
|
+
* listener for the 'listening' event, if provided.
|
|
261
|
+
* @returns {Server} The created Server.
|
|
262
|
+
*/
|
|
263
|
+
export function createServer(opts, connectionListener) {
|
|
264
|
+
return new Server(opts, connectionListener)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export default {
|
|
268
|
+
connect,
|
|
269
|
+
createServer,
|
|
270
|
+
getServer,
|
|
271
|
+
Server,
|
|
272
|
+
}
|
package/lib/socket.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {log} from './utils.js'
|
|
2
|
+
import stream from 'stream'
|
|
3
|
+
import util from 'util'
|
|
4
|
+
|
|
5
|
+
// A mock socket is two duplex HalfSockets, each of which is connected to the
|
|
6
|
+
// other with a crossover cable.
|
|
7
|
+
|
|
8
|
+
// Summary of node stream events:
|
|
9
|
+
// end() => 'finish' (the send side)
|
|
10
|
+
// push(null) => 'end' (the receive side)
|
|
11
|
+
// 'finish' + 'end' => 'close'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Data was written to a HalfSocket.
|
|
15
|
+
*
|
|
16
|
+
* @event HalfSocket#written
|
|
17
|
+
* @param {Buffer|string} data The data that was written.
|
|
18
|
+
* @param {string} encoding The encoding for the data.
|
|
19
|
+
*/
|
|
20
|
+
const WRITTEN_EVENT = 'written'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* One end of a socket, either client or server. Serves as a pass-through to
|
|
24
|
+
* the other side.
|
|
25
|
+
*
|
|
26
|
+
* @fires {HalfSocket#written} When data is written to this side of the socket
|
|
27
|
+
* to be sent.
|
|
28
|
+
*/
|
|
29
|
+
export class HalfSocket extends stream.Duplex {
|
|
30
|
+
/**
|
|
31
|
+
* Creates an instance of HalfSocket.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} name The name of this socket end, for debugging.
|
|
34
|
+
* @param {stream.DuplexOptions} opts Options for the socket end.
|
|
35
|
+
*/
|
|
36
|
+
constructor(name, opts) {
|
|
37
|
+
opts = {
|
|
38
|
+
// These have slightly different values between Duplex and Socket
|
|
39
|
+
// on node 12 and 16.
|
|
40
|
+
allowHalfOpen: false,
|
|
41
|
+
autoDestroy: true,
|
|
42
|
+
emitClose: true,
|
|
43
|
+
...opts,
|
|
44
|
+
}
|
|
45
|
+
super(opts)
|
|
46
|
+
this.name = name
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Add debug logging to EventEmitter. Start with NODE_DEBUG=mock-tls-server
|
|
51
|
+
* to see all events.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} eventName The name of the event.
|
|
54
|
+
* @param {any[]} args The arguments to be passed to listeners.
|
|
55
|
+
* @returns {boolean} Returns `true` if the event had listeners, `false`
|
|
56
|
+
* otherwise.
|
|
57
|
+
*/
|
|
58
|
+
emit(eventName, ...args) {
|
|
59
|
+
log(this.name, eventName, ...args)
|
|
60
|
+
return super.emit(eventName, ...args)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_write(chunk, encoding, cb) {
|
|
64
|
+
// De-couple the halves
|
|
65
|
+
this.emit(WRITTEN_EVENT, chunk, encoding)
|
|
66
|
+
cb()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// eslint-disable-next-line class-methods-use-this, no-empty-function
|
|
70
|
+
_read(sz) {
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
[util.inspect.custom]() {
|
|
74
|
+
return `[HalfSocket "${this.name}"]`
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A double-ended mock socket, for use in testing. No actual network
|
|
80
|
+
* connections are created, so this can be used for testing no matter what
|
|
81
|
+
* permissions you have in your CI system.
|
|
82
|
+
*/
|
|
83
|
+
export class MockSocket extends stream.Duplex {
|
|
84
|
+
/**
|
|
85
|
+
* Creates an instance of MockSocket.
|
|
86
|
+
*
|
|
87
|
+
* @param {stream.DuplexOptions} opts Options for both server and client
|
|
88
|
+
* sides.
|
|
89
|
+
*/
|
|
90
|
+
constructor(opts) {
|
|
91
|
+
super(opts)
|
|
92
|
+
this.clientSocket = new HalfSocket('cli', opts)
|
|
93
|
+
this.serverSocket = new HalfSocket('srv', opts)
|
|
94
|
+
|
|
95
|
+
// Simulate the TCP flavor of the Internet.
|
|
96
|
+
// If we were going to add delays, throttling, or Nagel-like coalescing,
|
|
97
|
+
// this would be the place to do it.
|
|
98
|
+
this.clientSocket.on(WRITTEN_EVENT, (chunk, encoding) => {
|
|
99
|
+
this.serverSocket.push(chunk)
|
|
100
|
+
})
|
|
101
|
+
this.serverSocket.on(WRITTEN_EVENT, (chunk, encoding) => {
|
|
102
|
+
this.clientSocket.push(chunk)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
// This is from the song by Jimmy Buffer:
|
|
106
|
+
// FINs to the left.
|
|
107
|
+
this.clientSocket.on('finish', () => {
|
|
108
|
+
this.serverSocket.push(null)
|
|
109
|
+
})
|
|
110
|
+
// FINs to the right.
|
|
111
|
+
this.serverSocket.on('finish', () => {
|
|
112
|
+
this.clientSocket.push(null)
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
}
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import util from 'util'
|
|
2
|
+
|
|
3
|
+
// Object.<> triggers this:
|
|
4
|
+
/* eslint-disable jsdoc/check-types */
|
|
5
|
+
/**
|
|
6
|
+
* Normalize arguments for functions that take one of these three things
|
|
7
|
+
* for the first arg: port (number), options (object), listener (function).
|
|
8
|
+
*
|
|
9
|
+
* @param {object|number|Function} [options] Options.
|
|
10
|
+
* @param {Object.<string,Function>} [listener] Callback.
|
|
11
|
+
* @returns {object} The normalized options.
|
|
12
|
+
* @throws {TypeError} No callback allowed for this function.
|
|
13
|
+
*/
|
|
14
|
+
export function normalizeArgs(options = {}, listener = null) {
|
|
15
|
+
let opts = null
|
|
16
|
+
if (typeof options === 'function') {
|
|
17
|
+
if (listener) {
|
|
18
|
+
listener[Object.keys(listener)[0]] = options
|
|
19
|
+
} else {
|
|
20
|
+
throw new Error('Function provided for options when not expecting it')
|
|
21
|
+
}
|
|
22
|
+
options = {}
|
|
23
|
+
}
|
|
24
|
+
switch (typeof options) {
|
|
25
|
+
case 'object':
|
|
26
|
+
opts = options || {}
|
|
27
|
+
break
|
|
28
|
+
case 'number':
|
|
29
|
+
opts = {port: options}
|
|
30
|
+
break
|
|
31
|
+
default:
|
|
32
|
+
throw new TypeError('Invalid type for options')
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
...listener,
|
|
36
|
+
...opts,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/* eslint-enable jsdoc/check-types*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Create an extended Error, by assigning all of the properties in opts.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} message Error text.
|
|
45
|
+
* @param {object} opts Extra properties for error (e.g. {code: 'EADDRINUSE'}).
|
|
46
|
+
* @returns {Error} The extended error.
|
|
47
|
+
*/
|
|
48
|
+
export function extendedError(message, opts) {
|
|
49
|
+
const er = new Error(message)
|
|
50
|
+
Object.assign(er, opts)
|
|
51
|
+
return er
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Select some properties from an object into multiple other objects.
|
|
56
|
+
*
|
|
57
|
+
* @param {object} obj The source object.
|
|
58
|
+
* @param {string[][]} args Arrays of strings to select into the result
|
|
59
|
+
* objects.
|
|
60
|
+
* @returns {object[]} One object for each of args, plus an extra one for
|
|
61
|
+
* everything that was left over.
|
|
62
|
+
*/
|
|
63
|
+
export function select(obj, ...args) {
|
|
64
|
+
const res = args.map(() => ({}))
|
|
65
|
+
const leftovers = {}
|
|
66
|
+
res.push(leftovers)
|
|
67
|
+
|
|
68
|
+
if (!obj) {
|
|
69
|
+
return res
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const sets = args.map(a => new Set(a))
|
|
73
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
74
|
+
let found = false
|
|
75
|
+
sets.forEach((s, i) => {
|
|
76
|
+
if (!found && s.has(k)) {
|
|
77
|
+
found = true
|
|
78
|
+
res[i][k] = v
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
if (!found) {
|
|
82
|
+
leftovers[k] = v
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return res
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** @type {util.DebugLoggerFunction|util.DebugLogger} */
|
|
89
|
+
export let log = util.debuglog('mock-tls-server', optimized => {
|
|
90
|
+
log = optimized
|
|
91
|
+
})
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mock-tls-server",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "ava",
|
|
9
|
+
"coverage": "c8 --all --src lib -r lcov npm test",
|
|
10
|
+
"lint": "eslint . --ext js",
|
|
11
|
+
"docs": "jsdoc -c .jsdoc.conf",
|
|
12
|
+
"types": "tsc",
|
|
13
|
+
"check": "npm run lint && npm run types && npm run docs && npm run coverage && npm pack --dry-run"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"tls",
|
|
17
|
+
"ssl",
|
|
18
|
+
"socket",
|
|
19
|
+
"server",
|
|
20
|
+
"certificate",
|
|
21
|
+
"X.509",
|
|
22
|
+
"ca",
|
|
23
|
+
"certificate authority",
|
|
24
|
+
"localhost",
|
|
25
|
+
"test",
|
|
26
|
+
"mock"
|
|
27
|
+
],
|
|
28
|
+
"author": "Joe Hildebrand <joe-github@cursive.net>",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"jsrsasign": "^10.4.1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@cto.af/eslint-config": "^0.0.10",
|
|
35
|
+
"@types/node": "^16.10.3",
|
|
36
|
+
"ava": "^3.15.0",
|
|
37
|
+
"c8": "^7.10.0",
|
|
38
|
+
"eslint": "^7.32.0",
|
|
39
|
+
"eslint-plugin-ava": "^13.0.0",
|
|
40
|
+
"eslint-plugin-jsdoc": "^36.1.1",
|
|
41
|
+
"eslint-plugin-node": "^11.1.0",
|
|
42
|
+
"jsdoc": "^3.6.7",
|
|
43
|
+
"minami": "^1.2.3",
|
|
44
|
+
"p-event": "^4.2.0",
|
|
45
|
+
"typescript": "^4.4.3"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/types/ca.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {object} CertOpts
|
|
3
|
+
* @property {Date} [notafter='Now + 5m'] Certificate not valid after this
|
|
4
|
+
* date.
|
|
5
|
+
* @property {Date} [notbefore='Now - 1s'] Certificate not valid before this
|
|
6
|
+
* date.
|
|
7
|
+
* @property {string} [subject] Subject Distinguished Name (DN).
|
|
8
|
+
* @property {string[]} [names=['localhost']] DNS Subject Alternative Names
|
|
9
|
+
* for the cert. Ignored for the CA cert.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} ServerProperties
|
|
13
|
+
* @property {string} cert PEM-encoded server certificate.
|
|
14
|
+
* @property {string} key PEM-encoded server private key.
|
|
15
|
+
* @property {string} ca PEM-encoded CA certificate.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Very rudimentary Certificate Authority (CA).
|
|
19
|
+
* Warning: DO NOT use this on the Internet. It is only for testing purposes.
|
|
20
|
+
*/
|
|
21
|
+
export class CertificateAuthority {
|
|
22
|
+
/**
|
|
23
|
+
* Creates an instance of CertificateAuthority.
|
|
24
|
+
*
|
|
25
|
+
* @param {CertOpts} [opts={}] CA cert options.
|
|
26
|
+
*/
|
|
27
|
+
constructor(opts?: CertOpts);
|
|
28
|
+
subject: string;
|
|
29
|
+
/** @type {string} */
|
|
30
|
+
cert_pem: string;
|
|
31
|
+
/**
|
|
32
|
+
* Creates and signs a server certificate.
|
|
33
|
+
*
|
|
34
|
+
* @param {CertOpts} [opts={}] Server cert options.
|
|
35
|
+
* @returns {ServerProperties} Information about the created certificate.
|
|
36
|
+
* @throws {Error} No subject or names.
|
|
37
|
+
*/
|
|
38
|
+
issue(opts?: CertOpts): ServerProperties;
|
|
39
|
+
}
|
|
40
|
+
export type CertOpts = {
|
|
41
|
+
/**
|
|
42
|
+
* Certificate not valid after this
|
|
43
|
+
* date.
|
|
44
|
+
*/
|
|
45
|
+
notafter?: Date;
|
|
46
|
+
/**
|
|
47
|
+
* Certificate not valid before this
|
|
48
|
+
* date.
|
|
49
|
+
*/
|
|
50
|
+
notbefore?: Date;
|
|
51
|
+
/**
|
|
52
|
+
* Subject Distinguished Name (DN).
|
|
53
|
+
*/
|
|
54
|
+
subject?: string;
|
|
55
|
+
/**
|
|
56
|
+
* DNS Subject Alternative Names
|
|
57
|
+
* for the cert. Ignored for the CA cert.
|
|
58
|
+
*/
|
|
59
|
+
names?: string[];
|
|
60
|
+
};
|
|
61
|
+
export type ServerProperties = {
|
|
62
|
+
/**
|
|
63
|
+
* PEM-encoded server certificate.
|
|
64
|
+
*/
|
|
65
|
+
cert: string;
|
|
66
|
+
/**
|
|
67
|
+
* PEM-encoded server private key.
|
|
68
|
+
*/
|
|
69
|
+
key: string;
|
|
70
|
+
/**
|
|
71
|
+
* PEM-encoded CA certificate.
|
|
72
|
+
*/
|
|
73
|
+
ca: string;
|
|
74
|
+
};
|