A fake net socket, that allows you to connect to local servers that don't have any open ports!
I have completely rewritten the entire thing from scratch, since it was a mess before.
This module lets you "connect" to a local net/http/https/etc server running in the same node instance, that you have access to as a variable, that doesn't have any ports open, you can send data to and from, as if it was connected through a regular net socket.
There are a few exports for the module:
net <Object>
tls <Object>
util <Object>
(Although anything accessible in these objects is also accessible directly through the exports as well, e.g. fakesocket.net.createFakeSocket is also accessible through fakesocket.createFakeSocket directly)
They are all covered in more detail below:
This is what you want to use if you want extra info on your sockets.
- Returns:
<Boolean>Whether or not the passedsocketwas created using this module.
- Returns:
<net.Socket>|<stream.Duplex>|<null>The socket/duplex that this is writing to, returnsnullif this is not a fake socket.
This is the utility you want to use if connecting to a normal net.Server or http.Server, for the version used to connect to tls.Server and https.Server, you want the fakesocket.tls submodule
- Returns:
<net.Socket>
This sets up a socket pair, that is ready to connect to a server via the returned socket's mockConnect method.
server<net.Server>The server to connect to.callback<Function>A function that is automatically called when the socket is ready to be used.- Returns:
<net.Socket>The socket itself.
This is required for making the socket actually connect to the server, similar to net.Socket.connect().
This function also overrides the socket's default connect() function, so it can be accessed through there as well
This method only exists by default if the socket is created through createFakeSocket().
server<net.Server>Server to connect to.- Returns:
<Function>A function that can be used in http/https request options.
This is a generator function for a valid createConnection argument in a http request (See http.request()). It only needs to be called once per server, as a single function will work for multiple requests.
- Returns:
<Object>input<net.Socket>A side of a socket pair, corresponds tooutput.output<net.Socket>A side of a socket pair, corresponds toinput.
This generates a socket pair, where writing to one will output data at the other, closing one will also close its other side.
These sockets will still have their normal connect functions, and will not have the mockConnect functions, which are only added when the socket is created through createFakeSocket(), do not use this function unless you know what you're doing.
The big one.
This is the submodule that you would use to make sockets that can connect to tls/https servers.
Does actually do the whole encryption/decryption thing, so useful if you want to pipe an actual tls connection to an external socket, but not really very good or fast if you want to do a whole bunch of communications, since encryption/decryption takes time and resources.
- Returns:
Objectinput<stream.Duplex>A dulplex that pushes all data tooutputoutput<stream.Duplex>A dulplex that pushes all data toinput
Creates a duplex pair, where writing to one will output data at the other, closing one will also close its other side.
No extra methods have been added to either duplex.
options<Object>options to pass tonew TLSSocket()- Returns:
TLSSocketATLSSocketthat is connected to a duplex, and can connect to local tls servers
Creates a tls socket that is ready to connect to a local tls server through Socket.mockConnect
The default Socket.connect is also overridden and aliased to Socket.mockConnect
server<tls.Server>The server the socket should connect to.host<String>The server name used for authenticating certificates, not actually for connecting, optional becausehostfrom the options ofcreateFakeSocketor theoptionsargument can be used instead.port<String | Integer>The server port used for authenticating certificates, not actually for connecting, optional becauseportfrom the options ofcreateFakeSocketor theoptionsargument can be used instead.options<Object>Options used to replaceoptionspassed increateFakeSocket, will not remove any options from the options passed increateFakeTLSSocketif they are not set here.callback<Function>Called when the socket is finished connecting to the server
Connects the socket to a specified tls server
host and port MUST be provided at some time, in the options to createFakeTLSSocket, in the options at mockConnect, or as arguments in mockConnect, unless you want issues to occurr in certificate validification
Alias for fakesocket.tls.createFakeTLSSocket(), will not override fakesocket.net.createFakeSocket() when they are aliased to the main module
server<tls.Server>a server can be passed to the function, if it is not passed like this, it must be passed either inoptionsor in the request it is attached to.host<String>a hostname that can be passed to the function and is used for certificate validification, if it is not passed here, it must be passed either inoptionsor in the request it is attached to.port<String>a port that can be passed to the function and is used for certificate validification, if it is not passed here, it must be passed either inoptionsor in the request it is attached to, if it is not passed there, a default port will be used.options<Object>options to be passed toconnectTLS, if it is not provided, the connected request will be passed instead- Returns:
<Function>the connection generator to be used in https requests.
Creates a connection generator for TLS requests
server<tls.Server>the server to connect tohost<String>the host used for certificate validification, can be passed inoptionsinsteadport<String | Integer>the port used for certificate validification, can be passed inoptionsinsteadoptions<Object>passed totls.connect, can be used to substitute inhostorportcallback<Function>called when the connection is completed- Returns:
<TLSSocket>the result oftls.connect
Connects to a server locally, done all by itself.
Preferred over createFakeTLSSocket().