mock-tls-server 0.0.1 → 1.0.0
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/README.md +18 -18
- package/lib/ca.js +46 -41
- package/lib/index.js +70 -345
- package/lib/kernel.js +23 -22
- package/lib/net.js +94 -88
- package/lib/socket.js +36 -26
- package/lib/utils.js +54 -38
- package/package.json +23 -20
- package/types/ca.d.ts +9 -8
- package/types/index.d.ts +46 -763
- package/types/kernel.d.ts +6 -5
- package/types/net.d.ts +63 -46
- package/types/socket.d.ts +12 -3
- package/types/utils.d.ts +20 -13
package/types/kernel.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('node:events').EventEmitter} EventEmitter
|
|
3
|
+
*/
|
|
1
4
|
/**
|
|
2
5
|
* Simulate the bit of the OS kernel that uses port numbers to make endpoints.
|
|
3
6
|
*/
|
|
4
7
|
export class Kernel {
|
|
5
|
-
/** @type {
|
|
6
|
-
ports:
|
|
7
|
-
[x: number]: EventEmitter;
|
|
8
|
-
};
|
|
8
|
+
/** @type {Record<number, EventEmitter>} */
|
|
9
|
+
ports: Record<number, EventEmitter>;
|
|
9
10
|
/** @type {any} */
|
|
10
11
|
timer: any;
|
|
11
12
|
count: number;
|
|
@@ -29,4 +30,4 @@ export class Kernel {
|
|
|
29
30
|
lookup(port: number): EventEmitter;
|
|
30
31
|
}
|
|
31
32
|
export const globalKernel: Kernel;
|
|
32
|
-
|
|
33
|
+
export type EventEmitter = import("node:events").EventEmitter;
|
package/types/net.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('node:stream').Duplex} DuplexStream
|
|
3
|
+
*/
|
|
2
4
|
/**
|
|
3
5
|
* @typedef {object} SocketConnectOpts
|
|
4
6
|
* @property {number} port Listening server port to connect to.
|
|
5
7
|
* @property {boolean} [allowHalfOpen=false] If set to false, then the
|
|
6
8
|
* socket will automatically end the writable side when the readable side
|
|
7
9
|
* ends.
|
|
10
|
+
* @property {SocketCallback} [connectListener]
|
|
8
11
|
*/
|
|
9
12
|
/**
|
|
10
13
|
* @typedef {object} ServerOpts
|
|
@@ -12,17 +15,34 @@
|
|
|
12
15
|
* socket will automatically end the writable side when the readable side
|
|
13
16
|
* ends.
|
|
14
17
|
*/
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {object} ListenOptions
|
|
20
|
+
* @property {number} port The port to listen on.
|
|
21
|
+
* @property {() => void} listeningListener If it exists, installed as
|
|
22
|
+
* a listener for the 'listening' event.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* @callback PlainCallback
|
|
26
|
+
* @returns {void}
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* @callback CountCallback
|
|
30
|
+
* @param {Error} [err] If not null, an error occurred.
|
|
31
|
+
* @param {number} [count] The number of connected clients.
|
|
32
|
+
* @returns {void}
|
|
33
|
+
*/
|
|
15
34
|
/**
|
|
16
35
|
* Initiate a connection to a given server port.
|
|
17
36
|
*
|
|
18
|
-
* @param {number|
|
|
19
|
-
*
|
|
37
|
+
* @param {number |
|
|
38
|
+
* (SocketConnectOpts & import('node:stream').DuplexOptions)} options The
|
|
39
|
+
* server port to connect to. Must already be listening.
|
|
20
40
|
* @param {SocketCallback} [connectListener] Event listener for the 'connect'
|
|
21
41
|
* event.
|
|
22
|
-
* @returns {
|
|
42
|
+
* @returns {DuplexStream} The client side HalfSocket.
|
|
23
43
|
* @throws {TypeError} Invalid type for port.
|
|
24
44
|
*/
|
|
25
|
-
export function connect(options: number | SocketConnectOpts, connectListener?: SocketCallback):
|
|
45
|
+
export function connect(options: number | (SocketConnectOpts & import("node:stream").DuplexOptions), connectListener?: SocketCallback | undefined): DuplexStream;
|
|
26
46
|
/**
|
|
27
47
|
* Get the Server associated with a port.
|
|
28
48
|
*
|
|
@@ -38,15 +58,14 @@ export function getServer(options: SocketConnectOpts | number): Server;
|
|
|
38
58
|
* listener for the 'listening' event, if provided.
|
|
39
59
|
* @returns {Server} The created Server.
|
|
40
60
|
*/
|
|
41
|
-
export function createServer(opts?: ServerOpts |
|
|
61
|
+
export function createServer(opts?: SocketCallback | ServerOpts | undefined, connectionListener?: SocketCallback | undefined): Server;
|
|
42
62
|
/**
|
|
43
|
-
* @
|
|
44
|
-
* @param {stream.Duplex} socket Server side of socket that was just connected.
|
|
63
|
+
* @typedef {(socket: DuplexStream) => void} SocketCallback
|
|
45
64
|
*/
|
|
46
65
|
/**
|
|
47
66
|
* A mock server socket, more-or-less like net.Server.
|
|
48
67
|
*/
|
|
49
|
-
export class Server extends EventEmitter {
|
|
68
|
+
export class Server extends EventEmitter<[never]> {
|
|
50
69
|
/**
|
|
51
70
|
* Creates an instance of Server.
|
|
52
71
|
*
|
|
@@ -54,38 +73,32 @@ export class Server extends EventEmitter {
|
|
|
54
73
|
* @param {SocketCallback} [connectionListener] Register this as the event
|
|
55
74
|
* listener for the 'listening' event, if provided.
|
|
56
75
|
*/
|
|
57
|
-
constructor(options?: ServerOpts |
|
|
76
|
+
constructor(options?: SocketCallback | ServerOpts | undefined, connectionListener?: SocketCallback | undefined);
|
|
58
77
|
clients: Set<any>;
|
|
59
|
-
port: number;
|
|
78
|
+
port: number | null;
|
|
60
79
|
listening: boolean;
|
|
61
80
|
closing: boolean;
|
|
62
|
-
address(): string;
|
|
63
81
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
82
|
+
* Add debug logging to EventEmitter. Start with NODE_DEBUG=mock-tls-server
|
|
83
|
+
* to see all events.
|
|
84
|
+
*
|
|
85
|
+
* @param {string} eventName The name of the event.
|
|
86
|
+
* @param {any[]} args The arguments to be passed to listeners.
|
|
87
|
+
* @returns {boolean} Returns `true` if the event had listeners, `false`
|
|
88
|
+
* otherwise.
|
|
68
89
|
*/
|
|
90
|
+
emit(eventName: string, ...args: any[]): boolean;
|
|
91
|
+
address(): string | null;
|
|
69
92
|
/**
|
|
70
93
|
* Listen for incoming connections. Whenever `connect` is called on this
|
|
71
94
|
* server's port, create a mock socket wrapped in a TLSSocket.
|
|
72
95
|
*
|
|
73
96
|
* @param {number|ListenOptions} [options] Port number or options object.
|
|
74
|
-
* @param {
|
|
97
|
+
* @param {() => void} [listeningListener] If it exists, installed as
|
|
75
98
|
* a listener for the 'listening' event.
|
|
76
99
|
* @returns {Server} This.
|
|
77
100
|
*/
|
|
78
|
-
listen(options?: number |
|
|
79
|
-
/**
|
|
80
|
-
* The port to listen on.
|
|
81
|
-
*/
|
|
82
|
-
port: number;
|
|
83
|
-
/**
|
|
84
|
-
* If it exists, installed as
|
|
85
|
-
* a listener for the 'listening' event.
|
|
86
|
-
*/
|
|
87
|
-
listeningListener: Function;
|
|
88
|
-
}, listeningListener?: Function): Server;
|
|
101
|
+
listen(options?: number | ListenOptions | undefined, listeningListener?: (() => void) | undefined): Server;
|
|
89
102
|
/**
|
|
90
103
|
* Add a client to this server.
|
|
91
104
|
*
|
|
@@ -99,23 +112,13 @@ export class Server extends EventEmitter {
|
|
|
99
112
|
* If all of the clients have closed, clean up and emit the 'close' event.
|
|
100
113
|
*/
|
|
101
114
|
_checkClosed(): void;
|
|
102
|
-
/**
|
|
103
|
-
* @callback PlainCallback
|
|
104
|
-
* @returns {void}
|
|
105
|
-
*/
|
|
106
115
|
/**
|
|
107
116
|
* Close down all of the clients, then the server.
|
|
108
117
|
*
|
|
109
118
|
* @param {PlainCallback} [cb] Registered as listener for the 'close' event.
|
|
110
119
|
* @returns {Server} This.
|
|
111
120
|
*/
|
|
112
|
-
close(cb?:
|
|
113
|
-
/**
|
|
114
|
-
* @callback CountCallback
|
|
115
|
-
* @param {Error} [err] If not null, an error occurred.
|
|
116
|
-
* @param {number} [count] The number of connected clients.
|
|
117
|
-
* @returns {void}
|
|
118
|
-
*/
|
|
121
|
+
close(cb?: PlainCallback | undefined): Server;
|
|
119
122
|
/**
|
|
120
123
|
* Get the number of current clients.
|
|
121
124
|
*
|
|
@@ -123,7 +126,7 @@ export class Server extends EventEmitter {
|
|
|
123
126
|
* @returns {Server} This.
|
|
124
127
|
* @throws {TypeError} Callback not a function.
|
|
125
128
|
*/
|
|
126
|
-
getConnections(cb:
|
|
129
|
+
getConnections(cb: CountCallback): Server;
|
|
127
130
|
}
|
|
128
131
|
declare namespace _default {
|
|
129
132
|
export { connect };
|
|
@@ -132,6 +135,7 @@ declare namespace _default {
|
|
|
132
135
|
export { Server };
|
|
133
136
|
}
|
|
134
137
|
export default _default;
|
|
138
|
+
export type DuplexStream = import("node:stream").Duplex;
|
|
135
139
|
export type SocketConnectOpts = {
|
|
136
140
|
/**
|
|
137
141
|
* Listening server port to connect to.
|
|
@@ -142,7 +146,8 @@ export type SocketConnectOpts = {
|
|
|
142
146
|
* socket will automatically end the writable side when the readable side
|
|
143
147
|
* ends.
|
|
144
148
|
*/
|
|
145
|
-
allowHalfOpen?: boolean;
|
|
149
|
+
allowHalfOpen?: boolean | undefined;
|
|
150
|
+
connectListener?: SocketCallback | undefined;
|
|
146
151
|
};
|
|
147
152
|
export type ServerOpts = {
|
|
148
153
|
/**
|
|
@@ -150,9 +155,21 @@ export type ServerOpts = {
|
|
|
150
155
|
* socket will automatically end the writable side when the readable side
|
|
151
156
|
* ends.
|
|
152
157
|
*/
|
|
153
|
-
allowHalfOpen?: boolean;
|
|
158
|
+
allowHalfOpen?: boolean | undefined;
|
|
159
|
+
};
|
|
160
|
+
export type ListenOptions = {
|
|
161
|
+
/**
|
|
162
|
+
* The port to listen on.
|
|
163
|
+
*/
|
|
164
|
+
port: number;
|
|
165
|
+
/**
|
|
166
|
+
* If it exists, installed as
|
|
167
|
+
* a listener for the 'listening' event.
|
|
168
|
+
*/
|
|
169
|
+
listeningListener: () => void;
|
|
154
170
|
};
|
|
155
|
-
export type
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
import {
|
|
171
|
+
export type PlainCallback = () => void;
|
|
172
|
+
export type CountCallback = (err?: Error | undefined, count?: number | undefined) => void;
|
|
173
|
+
export type SocketCallback = (socket: DuplexStream) => void;
|
|
174
|
+
import { EventEmitter } from 'node:events';
|
|
175
|
+
import { MockSocket } from './socket.js';
|
package/types/socket.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
/**
|
|
3
2
|
* One end of a socket, either client or server. Serves as a pass-through to
|
|
4
3
|
* the other side.
|
|
@@ -15,6 +14,16 @@ export class HalfSocket extends stream.Duplex {
|
|
|
15
14
|
*/
|
|
16
15
|
constructor(name: string, opts: stream.DuplexOptions);
|
|
17
16
|
name: string;
|
|
17
|
+
/**
|
|
18
|
+
* Add debug logging to EventEmitter. Start with NODE_DEBUG=mock-tls-server
|
|
19
|
+
* to see all events.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} eventName The name of the event.
|
|
22
|
+
* @param {any[]} args The arguments to be passed to listeners.
|
|
23
|
+
* @returns {boolean} Returns `true` if the event had listeners, `false`
|
|
24
|
+
* otherwise.
|
|
25
|
+
*/
|
|
26
|
+
emit(eventName: string, ...args: any[]): boolean;
|
|
18
27
|
[util.inspect.custom](): string;
|
|
19
28
|
}
|
|
20
29
|
/**
|
|
@@ -33,5 +42,5 @@ export class MockSocket extends stream.Duplex {
|
|
|
33
42
|
clientSocket: HalfSocket;
|
|
34
43
|
serverSocket: HalfSocket;
|
|
35
44
|
}
|
|
36
|
-
import stream from
|
|
37
|
-
import util from
|
|
45
|
+
import stream from 'node:stream';
|
|
46
|
+
import util from 'node:util';
|
package/types/utils.d.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @template T
|
|
3
|
+
* @typedef {T | Readonly<T[]>} Many
|
|
4
|
+
*/
|
|
2
5
|
/**
|
|
3
6
|
* Normalize arguments for functions that take one of these three things
|
|
4
7
|
* for the first arg: port (number), options (object), listener (function).
|
|
5
8
|
*
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
+
* @template {Record<string,Function|undefined>} T
|
|
10
|
+
* @template U
|
|
11
|
+
* @param {number | U} [options] Options.
|
|
12
|
+
* @param {T} [listener] Callback.
|
|
13
|
+
* @returns {U & {port?: number} & T} The normalized options.
|
|
9
14
|
* @throws {TypeError} No callback allowed for this function.
|
|
10
15
|
*/
|
|
11
|
-
export function normalizeArgs(options?:
|
|
12
|
-
|
|
13
|
-
}
|
|
16
|
+
export function normalizeArgs<T extends Record<string, Function | undefined>, U>(options?: number | U | undefined, listener?: T | undefined): U & {
|
|
17
|
+
port?: number;
|
|
18
|
+
} & T;
|
|
14
19
|
/**
|
|
15
20
|
* Create an extended Error, by assigning all of the properties in opts.
|
|
16
21
|
*
|
|
@@ -22,13 +27,15 @@ export function extendedError(message: string, opts: object): Error;
|
|
|
22
27
|
/**
|
|
23
28
|
* Select some properties from an object into multiple other objects.
|
|
24
29
|
*
|
|
25
|
-
* @
|
|
26
|
-
* @param {
|
|
30
|
+
* @template {Record<string,any>} T
|
|
31
|
+
* @param {T} obj The source object.
|
|
32
|
+
* @param {(keyof T)[][]} args Arrays of strings to select into the result
|
|
27
33
|
* objects.
|
|
28
|
-
* @returns {
|
|
29
|
-
*
|
|
34
|
+
* @returns {Partial<Record<keyof T, any>>[]} One object for each of args,
|
|
35
|
+
* plus an extra one for everything that was left over.
|
|
30
36
|
*/
|
|
31
|
-
export function select(obj:
|
|
37
|
+
export function select<T extends Record<string, any>>(obj: T, ...args: (keyof T)[][]): Partial<Record<keyof T, any>>[];
|
|
32
38
|
/** @type {util.DebugLoggerFunction|util.DebugLogger} */
|
|
33
39
|
export let log: util.DebugLoggerFunction | util.DebugLogger;
|
|
34
|
-
|
|
40
|
+
export type Many<T> = T | Readonly<T[]>;
|
|
41
|
+
import util from 'node:util';
|