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.
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Simulate the bit of the OS kernel that uses port numbers to make endpoints.
3
+ */
4
+ export class Kernel {
5
+ /** @type {Object.<number, EventEmitter>} */
6
+ ports: {
7
+ [x: number]: EventEmitter;
8
+ };
9
+ /** @type {any} */
10
+ timer: any;
11
+ count: number;
12
+ /**
13
+ * Store the Server associated with a given port number in the kernel.
14
+ *
15
+ * @param {number} port The port number. If null or 0, find the next unused
16
+ * port number starting with 1024.
17
+ * @param {EventEmitter} server The server to store. Must support the
18
+ * 'close' event.
19
+ * @returns {number} The selected port.
20
+ * @throws {Error} Address already in use for the given port.
21
+ */
22
+ open(port: number, server: EventEmitter): number;
23
+ /**
24
+ * Find the server instance for the given port.
25
+ *
26
+ * @param {number} port The port number.
27
+ * @returns {EventEmitter} The server, or null if not found.
28
+ */
29
+ lookup(port: number): EventEmitter;
30
+ }
31
+ export const globalKernel: Kernel;
32
+ import { EventEmitter } from "events";
package/types/net.d.ts ADDED
@@ -0,0 +1,158 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * @typedef {object} SocketConnectOpts
4
+ * @property {number} port Listening server port to connect to.
5
+ * @property {boolean} [allowHalfOpen=false] If set to false, then the
6
+ * socket will automatically end the writable side when the readable side
7
+ * ends.
8
+ */
9
+ /**
10
+ * @typedef {object} ServerOpts
11
+ * @property {boolean} [allowHalfOpen=false] If set to false, then the
12
+ * socket will automatically end the writable side when the readable side
13
+ * ends.
14
+ */
15
+ /**
16
+ * Initiate a connection to a given server port.
17
+ *
18
+ * @param {number|SocketConnectOpts} options The server port to connect to.
19
+ * Must already be listening.
20
+ * @param {SocketCallback} [connectListener] Event listener for the 'connect'
21
+ * event.
22
+ * @returns {stream.Duplex} The client side HalfSocket.
23
+ * @throws {TypeError} Invalid type for port.
24
+ */
25
+ export function connect(options: number | SocketConnectOpts, connectListener?: SocketCallback): stream.Duplex;
26
+ /**
27
+ * Get the Server associated with a port.
28
+ *
29
+ * @param {SocketConnectOpts|number} options Port to look up.
30
+ * @returns {Server} The server that was found, or null if none found.
31
+ */
32
+ export function getServer(options: SocketConnectOpts | number): Server;
33
+ /**
34
+ * Creates an instance of Server.
35
+ *
36
+ * @param {ServerOpts|SocketCallback} [opts] Options for the server.
37
+ * @param {SocketCallback} [connectionListener] Register this as the event
38
+ * listener for the 'listening' event, if provided.
39
+ * @returns {Server} The created Server.
40
+ */
41
+ export function createServer(opts?: ServerOpts | SocketCallback, connectionListener?: SocketCallback): Server;
42
+ /**
43
+ * @callback SocketCallback
44
+ * @param {stream.Duplex} socket Server side of socket that was just connected.
45
+ */
46
+ /**
47
+ * A mock server socket, more-or-less like net.Server.
48
+ */
49
+ export class Server extends EventEmitter {
50
+ /**
51
+ * Creates an instance of Server.
52
+ *
53
+ * @param {ServerOpts|SocketCallback} [options] Options for the server.
54
+ * @param {SocketCallback} [connectionListener] Register this as the event
55
+ * listener for the 'listening' event, if provided.
56
+ */
57
+ constructor(options?: ServerOpts | SocketCallback, connectionListener?: SocketCallback);
58
+ clients: Set<any>;
59
+ port: number;
60
+ listening: boolean;
61
+ closing: boolean;
62
+ address(): string;
63
+ /**
64
+ * @typedef {object} ListenOptions
65
+ * @property {number} port The port to listen on.
66
+ * @property {Function} listeningListener If it exists, installed as
67
+ * a listener for the 'listening' event.
68
+ */
69
+ /**
70
+ * Listen for incoming connections. Whenever `connect` is called on this
71
+ * server's port, create a mock socket wrapped in a TLSSocket.
72
+ *
73
+ * @param {number|ListenOptions} [options] Port number or options object.
74
+ * @param {Function} [listeningListener] If it exists, installed as
75
+ * a listener for the 'listening' event.
76
+ * @returns {Server} This.
77
+ */
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;
89
+ /**
90
+ * Add a client to this server.
91
+ *
92
+ * @param {MockSocket} ms The MockSocket to add.
93
+ * @returns {Server} This.
94
+ * @throws {Error} Server not listening.
95
+ * @ignore
96
+ */
97
+ _addClient(ms: MockSocket): Server;
98
+ /**
99
+ * If all of the clients have closed, clean up and emit the 'close' event.
100
+ */
101
+ _checkClosed(): void;
102
+ /**
103
+ * @callback PlainCallback
104
+ * @returns {void}
105
+ */
106
+ /**
107
+ * Close down all of the clients, then the server.
108
+ *
109
+ * @param {PlainCallback} [cb] Registered as listener for the 'close' event.
110
+ * @returns {Server} This.
111
+ */
112
+ close(cb?: () => void): Server;
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
+ */
119
+ /**
120
+ * Get the number of current clients.
121
+ *
122
+ * @param {CountCallback} cb Called with the current number of clients.
123
+ * @returns {Server} This.
124
+ * @throws {TypeError} Callback not a function.
125
+ */
126
+ getConnections(cb: (err?: Error, count?: number) => void): Server;
127
+ }
128
+ declare namespace _default {
129
+ export { connect };
130
+ export { createServer };
131
+ export { getServer };
132
+ export { Server };
133
+ }
134
+ export default _default;
135
+ export type SocketConnectOpts = {
136
+ /**
137
+ * Listening server port to connect to.
138
+ */
139
+ port: number;
140
+ /**
141
+ * If set to false, then the
142
+ * socket will automatically end the writable side when the readable side
143
+ * ends.
144
+ */
145
+ allowHalfOpen?: boolean;
146
+ };
147
+ export type ServerOpts = {
148
+ /**
149
+ * If set to false, then the
150
+ * socket will automatically end the writable side when the readable side
151
+ * ends.
152
+ */
153
+ allowHalfOpen?: boolean;
154
+ };
155
+ export type SocketCallback = (socket: stream.Duplex) => any;
156
+ import stream from "stream";
157
+ import { EventEmitter } from "events";
158
+ import { MockSocket } from "./socket.js";
@@ -0,0 +1,37 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * One end of a socket, either client or server. Serves as a pass-through to
4
+ * the other side.
5
+ *
6
+ * @fires {HalfSocket#written} When data is written to this side of the socket
7
+ * to be sent.
8
+ */
9
+ export class HalfSocket extends stream.Duplex {
10
+ /**
11
+ * Creates an instance of HalfSocket.
12
+ *
13
+ * @param {string} name The name of this socket end, for debugging.
14
+ * @param {stream.DuplexOptions} opts Options for the socket end.
15
+ */
16
+ constructor(name: string, opts: stream.DuplexOptions);
17
+ name: string;
18
+ [util.inspect.custom](): string;
19
+ }
20
+ /**
21
+ * A double-ended mock socket, for use in testing. No actual network
22
+ * connections are created, so this can be used for testing no matter what
23
+ * permissions you have in your CI system.
24
+ */
25
+ export class MockSocket extends stream.Duplex {
26
+ /**
27
+ * Creates an instance of MockSocket.
28
+ *
29
+ * @param {stream.DuplexOptions} opts Options for both server and client
30
+ * sides.
31
+ */
32
+ constructor(opts: stream.DuplexOptions);
33
+ clientSocket: HalfSocket;
34
+ serverSocket: HalfSocket;
35
+ }
36
+ import stream from "stream";
37
+ import util from "util";
@@ -0,0 +1,34 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Normalize arguments for functions that take one of these three things
4
+ * for the first arg: port (number), options (object), listener (function).
5
+ *
6
+ * @param {object|number|Function} [options] Options.
7
+ * @param {Object.<string,Function>} [listener] Callback.
8
+ * @returns {object} The normalized options.
9
+ * @throws {TypeError} No callback allowed for this function.
10
+ */
11
+ export function normalizeArgs(options?: object | number | Function, listener?: {
12
+ [x: string]: Function;
13
+ }): object;
14
+ /**
15
+ * Create an extended Error, by assigning all of the properties in opts.
16
+ *
17
+ * @param {string} message Error text.
18
+ * @param {object} opts Extra properties for error (e.g. {code: 'EADDRINUSE'}).
19
+ * @returns {Error} The extended error.
20
+ */
21
+ export function extendedError(message: string, opts: object): Error;
22
+ /**
23
+ * Select some properties from an object into multiple other objects.
24
+ *
25
+ * @param {object} obj The source object.
26
+ * @param {string[][]} args Arrays of strings to select into the result
27
+ * objects.
28
+ * @returns {object[]} One object for each of args, plus an extra one for
29
+ * everything that was left over.
30
+ */
31
+ export function select(obj: object, ...args: string[][]): object[];
32
+ /** @type {util.DebugLoggerFunction|util.DebugLogger} */
33
+ export let log: util.DebugLoggerFunction | util.DebugLogger;
34
+ import util from "util";