LogoPear Docs
ReferencesBareModules

bare-tls

Transport Layer Security (TLS) streams for JavaScript

stable

bare-tls — Transport Layer Security (TLS) streams for JavaScript. It is a native addon and requires Bare >=1.7.0.

Mirrors the Node.js tls module.

npm i bare-tls

Usage

const tls = require('bare-tls')
const fs = require('bare-fs')

const server = tls.createServer(
  {
    cert: fs.readFileSync('cert.pem'),
    key: fs.readFileSync('key.pem')
  },
  (socket) => {
    socket.on('data', (data) => socket.end('pong')).on('close', () => server.close())
  }
)

server.listen(8443)

const client = tls.connect({ port: 8443, host: 'localhost' })

client.on('data', (data) => console.log(data)).end('ping')

API

errors

errors.from(err: Error): TLSError

Source

Create a TLSError from err, copying its message and code.

Parameters

ParameterTypeDefaultDescription
errErrorThe error to convert.

TLSSocket

new TLSSocket(socket: Duplex, opts?: TLSSocketOptions)

Source

Wrap the duplex stream socket with TLS.

Parameters

ParameterTypeDefaultDescription
socketDuplexThe duplex stream to wrap; it handles transport while the TLS socket handles encryption and decryption.
opts?TLSSocketOptionsOptions; rejectUnauthorized, eagerOpen, and allowHalfOpen default to true, and readBufferSize to 65536.

Throws

  • TypeErrorhost is missing for a client socket while rejectUnauthorized is true.
  • RangeError — an ALPN protocol name is empty or longer than 255 bytes.

alpnProtocol: string | null

Source

The negotiated ALPN protocol as a string, or null if no protocol was negotiated.

encrypted: true

Source

Always true.

socket: Duplex

Source

The underlying duplex stream.

Functions

createServer(opts?: TLSSocketOptions, onconnection?: (socket: TLSSocket) => void): TLSNetServer

Source

Creates a TLS server that listens for TCP connections and wraps them with TLS. Incoming connections are emitted as 'connection' events with a tls.Socket instance. Options are the same as tls.Socket, plus any options supported by <https://github.com/holepunchto/bare-net>.

Parameters

ParameterTypeDefaultDescription
opts?TLSSocketOptionsOptions applied to each incoming socket; the same as TLSSocket, plus any options supported by bare-net.
onconnection?(socket: TLSSocket) => voidCalled on each 'connection' event.

createConnection

createConnection(opts: TLSSocketOptions & { port: number; host?: string }, onconnect?: () => void): TLSSocket
Source

Creates a TCP connection and wraps it with TLS. opts are passed to both the underlying TCP socket and TLSSocket. At minimum, port must be specified.

Parameters

ParameterTypeDefaultDescription
optsTLSSocketOptions & { port: number; host?: string }Options passed to both the underlying TCP socket and TLSSocket; port is required and host defaults to 'localhost'.
onconnect?() => voidCalled when the connection is established.

Constants and variables

constants

constants: {
  state: {
    CONNECTED: number
    ATTACHED: number
  }
}
Source

Internal state flags used by TLSSocket, such as state.CONNECTED.

Types

TLSSocketEvents

interface TLSSocketEvents extends DuplexEvents {
  connect: []
}
Source

Events emitted by a TLSSocket.

TLSSocketOptions

interface TLSSocketOptions {
  isServer?: boolean
  cert?: ArrayBufferView
  key?: ArrayBufferView
  host?: string
  rejectUnauthorized?: boolean
  ca?: ArrayBufferView
  alpnProtocols?: string[]
  eagerOpen?: boolean
  allowHalfOpen?: boolean
  readBufferSize?: number
}
Source

Options for a TLSSocket.

TLSNetServerEvents

interface TLSNetServerEvents {
  listening: []
  connection: [socket: TLSSocket]
  error: [err: Error]
  close: []
}
Source

Events emitted by a TLS server.

TLSNetServer

interface TLSNetServer extends EventEmitter<TLSNetServerEvents> {
  readonly listening: boolean
}
Source

A TLS server over TCP; incoming connections are wrapped with TLS and emitted as 'connection' events.

bare-tls/constants

Constants and variables

constants.constants

constants: {
  state: {
    CONNECTED: number
    ATTACHED: number
  }
}
Source

Internal state flags used by TLSSocket, such as state.CONNECTED.

bare-tls/errors

TLSError

TLSError.from(err: Error): TLSError

Source

Create a TLSError from err, copying its message and code.

Parameters

ParameterTypeDefaultDescription
errErrorThe error to convert.

See also

On this page