npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tlsclientserver

v0.1.5

Published

Node js TLS Server and Client

Downloads

4

Readme

tlsClientServer

Node js TLS Server and Client

Certificate generation (NOT FOR PRODUCTION USE)

  • Create folder
mkdir examplesCerts
cd examplesCerts
  • Generate ca cert
openssl genrsa -out ca.key 4096
openssl req -new -x509 -days 9999 -key ca.key -out ca.crt
cat ca.key ca.crt  > ca.pem
  • Create config file openssl-server.cnf to generate server cert. In the [alt_names] section, enter the appropriate DNS names and/or IP addresses
# NOT FOR PRODUCTION USE. OpenSSL configuration file for testing.
# openssl-server.cnf

[ req ]
default_bits = 4096
default_keyfile = myTestServerCertificateKey.pem    ## The default private key file name.
default_md = sha256
distinguished_name = req_dn
req_extensions = v3_req

[ v3_req ]
subjectKeyIdentifier  = hash
basicConstraints = CA:FALSE
keyUsage = critical, digitalSignature, keyEncipherment
nsComment = "OpenSSL Generated Certificate for TESTING only.  NOT FOR PRODUCTION USE."
extendedKeyUsage  = serverAuth, clientAuth
subjectAltName = @alt_names

[ alt_names ]

DNS.1 = localhost
#DNS.2 =

IP.1 = 127.0.0.1

#IP.2 =

[ req_dn ]
countryName = Country Name (2 letter code)

countryName_default = RU

countryName_min = 2
countryName_max = 2

stateOrProvinceName = State or Province Name (full name)

stateOrProvinceName_default = Moscow

stateOrProvinceName_max = 64

localityName = Locality Name (eg, city)

localityName_default = Moscow

localityName_max = 64

organizationName = Organization Name (eg, company)

organizationName_default = Organization

organizationName_max = 64

organizationalUnitName = Organizational Unit Name (eg, section)

organizationalUnitName_default = Server

organizationalUnitName_max = 64

commonName = Common Name (eg, YOUR name)
commonName_max = 64
commonName_default = Alexey
  • Generate server cert
openssl genrsa -out server.key 4096
openssl req -new -key server.key -out server.csr -config openssl-server.cnf
openssl x509 -sha256 -req -days 9999 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -extfile openssl-server.cnf -extensions v3_req
cat server.crt server.key > server.pem
  • Generate client cert
openssl genrsa -out client.key 4096
openssl req -new -key client.key -out client.csr
openssl x509 -sha256 -req -days 9999 -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -extensions v3_req
cat client.crt client.key > client.pem

Code example

const {TlsServer, TlsClient} = require('tlsclientserver');
const fs = require('fs');
const port = process.env.PORT || 8000;

/**
 * @type {{rejectUnauthorized: boolean, requestCert: boolean, cert: *, key: *, ca: *}}
 */
const serverOptions = {
  key: fs.readFileSync('./examplesCerts/server.key'),
  cert: fs.readFileSync('./examplesCerts/server.crt'),
  ca: fs.readFileSync('./examplesCerts/ca.pem'),
  requestCert: true,
  rejectUnauthorized: true
};
let tlsServer = new TlsServer(serverOptions);
tlsServer.listen(port, () => {console.log('bound')});


let clientOptions = {
  key: fs.readFileSync(`./examplesCerts/client.key`),
  cert: fs.readFileSync(`./examplesCerts/client.crt`),
  ca: fs.readFileSync(`./examplesCerts/ca.pem`),
  port: port,
  host: '127.0.0.1'
}
let tlsClient = new TlsClient(clientOptions);
tlsClient.socket.on('secureConnect', () => {console.log('successfully connected')});

;(async () => {
  /**
    * Define route on server
    */
  tlsServer.addRoute('testRoute', (props) => props.a + props.b);
  /**
   * Call method from client and get result
   */
  let result = await tlsClient.sendData({method: 'testRoute', data: {a: 12, b: 2}});
  /**
   * result = {data: 14}
   * if an error will occur result = {error: String}
   */
  console.log('result', result);
})();