This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

token-server
TypeScript icon, indicating that this package has built-in type declarations

1.0.10 • Public • Published

token-server

TLS encrypted communication

CircleCI

Test Coverage Maintainability

This module simplifies SSL encrypted communication. It allows you to send messages (called tokens) from a client to a server and back.

Example

This example shows how a client and a server can be connected using self-signed certificates.

For some help on how to generate self-signed certificates see this comment.

const { TokenServer, TokenClient } = require('token-server')
 
/* ### Server ### */
 
const server = new TokenServer({
    host: 'localhost',
    port: 8090,
 
    key: fs.readFileSync('certs/server/server.key'),
    cert: fs.readFileSync('certs/server/server.crt'),
    ca: fs.readFileSync('certs/ca/ca.crt'),
    requestCert: true, // ask for a client cert
})
 
// Listen for tokens from the client.
server.on('token', (token, connection) => {
    console.log('The client says:', token.toString())
 
    const token = Buffer.from('I am happy to see you!')
    connection.send(token)
})
 
/* ### Client ### */
 
const client = new TokenClient({
    host: 'localhost',
    port: 8090,
 
    key: fs.readFileSync('certs/client/client.key'),
    cert: fs.readFileSync('certs/client/client.crt'),
    ca: fs.readFileSync('certs/ca/ca.crt'),
})
 
client.on('token', (token, connection) => {
    console.log('The server responds', token.toString())
})
 
const token = Buffer.from('Hello!')
client.send(token)

API

Table of Contents

TokenClient

This class represents a client that can connect to a server.

TokenClient constructor

const client = new Client(options)
  • options
    • port <number> The port that the client should connect to.
    • host <string> The hostname of the server.
    • key <string|Buffer> The private key of the client.
    • cert <string|Buffer> The SSL certificate of the client.
    • ca <string|Buffer> The authority certificate (used for self signed certificates)
    • All other options accepted by tls.connect()

TokenClient: "close"

This event is emitted when the client connection gets closed.

client.on('close', (hadError) => {
    // ...your code...
})
  • hadError <boolean> Is true if the client connection was closed by an error (e.g. if the server did not respond).

TokenClient: "connect"

This event is emitted when the client is successfully connected to the server.

client.on('connect', () => {
    // ...your code...
})

TokenClient: "error"

This event is emitted when a connection error occured. It is always followed by a "close" event.

client.on('error', (error) => {
    // ...your code...
})
  • error <Error> The error that was emitted by the underlying TLS socket.

TokenClient: "token"

This event is emitted when the server sends a token in reponse to a token that was sent by this client.

client.on('token', (token, connection) => {
    // ...your code...
})
  • token <Buffer> The token that was received.
  • connection <Connection> see here The connection that sent the token.

TokenClient#close()

This method is used to disconnect the client from the server.

client.close()

This method returns true if the connection was ended. If the client was already disconnected, the method returns false.

TokenClient#connect()

This method is used to reconnect the client to the server if the connection was closed.

client.connect(delay)
  • delay <number> (optional) Time in milliseconds to wait before a new connection to the server is created.

Returns true if a new connection will be created. If the client is already connected, then false will be returned.

TokenClient#send()

This method allows you to send a token to the server. Please note that the server has no send() method. It can only respond if it receives a token of the client.

client.send(token)
  • token <Buffer> The token that should be sent to the server.

TokenServer

This class represents a server that accepts SSL encrypted connections.

TokenServer constructor

const server = new TokenServer(options)
  • options
    • port <number> The port that the server should listen to.
    • host <string> The hostname to listen to.
    • key <string|Buffer> The private key of the server.
    • cert <string|Buffer> The SSL certificate of the server.
    • ca <string|Buffer> The authority certificate (used for self signed certificates)
    • All other options accepted by tls.createServer() and server.listen.

TokenServer: "close"

This event is emitted when the server gets closed.

server.on('close', (hadError) => {
    // ...your code...
})
  • hadError <boolean> Is true if the server was closed by an error (e.g. if the port was already in use).

TokenServer: "connect"

This event is emitted when the server is successfully listening to the specified port.

server.on('connect', () => {
    // ...your code...
})

TokenServer: "error"

This event is emitted when a server error occured. It is always followed by a "close" event.

server.on('error', (error) => {
    // ...your code...
})
  • error <Error> The error that was emitted by the underlying TLS server.

TokenServer: "token"

This event is emitted when the server receives a token from a client. The server send a token back to the client by calling connection.send(token).

server.on('token', (token, connection) => {
    // ...your code...
})
  • token <Buffer> The token that was received.
  • connection <Connection> see here The connection that sent the token.

TokenServer#close()

This method is used to disconnect the server. The server will wait for all connections to close before it stops listening to its port.

server.close()

This method returns true if the server was closed. If the server was already closed, the method returns false.

TokenServer#connect()

This method is used to reconnect the server to its port. You can use this method if the port was used before und you want to retry to listen to the port.

const result = client.connect(delay)
  • delay <number> (optional) Time in milliseconds to wait before the server tries to listen to the port.

Returns true if a new connection will be created. If the server is already connected, then false will be returned.

Connection

This class is a wrapper for a stream or socket. It parses all the data that goes through the stream and emits a "token" event when a token gets sent. The class can be used on both sides of a duplex stream to send and receive tokens.

Connection constructor

const connection = new Connection(socket)

Connection: "token"

This event is emitted when a token arrives at the underlying socket.

connection.on('token', (token) => {
    // ...your code...
})
  • token <Buffer> The token that was received.

Connection#isDead

Is a boolean that is true if the underlying socket is writable and false if it is not.

const status = connection.isDead

Connection#close()

This method is used to disconnect from the server.

const result = connection.close()

This method returns true if the connection was ended. If the client was already disconnected, the method returns false.

Connection#send()

This method allows you to send a token through the connection.

const success = client.send(token)

The method returns true if the message was written to the underlying socket. It returns false if the connection is dead.

Package Sidebar

Install

npm i token-server

Weekly Downloads

5

Version

1.0.10

License

MIT

Unpacked Size

121 kB

Total Files

20

Last publish

Collaborators

  • robojones