bare-tls
Transport Layer Security (TLS) streams for JavaScript
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-tlsUsage
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
| Parameter | Type | Default | Description |
|---|---|---|---|
err | Error | — | The error to convert. |
TLSSocket
new TLSSocket(socket: Duplex, opts?: TLSSocketOptions)
Source
Wrap the duplex stream socket with TLS.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
socket | Duplex | — | The duplex stream to wrap; it handles transport while the TLS socket handles encryption and decryption. |
opts? | TLSSocketOptions | — | Options; rejectUnauthorized, eagerOpen, and allowHalfOpen default to true, and readBufferSize to 65536. |
Throws
TypeError—hostis missing for a client socket whilerejectUnauthorizedistrue.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
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | TLSSocketOptions | — | Options applied to each incoming socket; the same as TLSSocket, plus any options supported by bare-net. |
onconnection? | (socket: TLSSocket) => void | — | Called on each 'connection' event. |
createConnection
createConnection(opts: TLSSocketOptions & { port: number; host?: string }, onconnect?: () => void): TLSSocketCreates 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
| Parameter | Type | Default | Description |
|---|---|---|---|
opts | TLSSocketOptions & { port: number; host?: string } | — | Options passed to both the underlying TCP socket and TLSSocket; port is required and host defaults to 'localhost'. |
onconnect? | () => void | — | Called when the connection is established. |
Constants and variables
constants
constants: {
state: {
CONNECTED: number
ATTACHED: number
}
}Internal state flags used by TLSSocket, such as state.CONNECTED.
Types
TLSSocketEvents
interface TLSSocketEvents extends DuplexEvents {
connect: []
}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
}Options for a TLSSocket.
TLSNetServerEvents
interface TLSNetServerEvents {
listening: []
connection: [socket: TLSSocket]
error: [err: Error]
close: []
}Events emitted by a TLS server.
TLSNetServer
interface TLSNetServer extends EventEmitter<TLSNetServerEvents> {
readonly listening: boolean
}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
}
}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
| Parameter | Type | Default | Description |
|---|---|---|---|
err | Error | — | The error to convert. |
See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.