LogoPear Docs

bare-https

HTTPS library for JavaScript

stable

bare-https — HTTPS library for JavaScript.

Mirrors the Node.js https module.

npm i bare-https

Usage

const https = require('bare-https')

const options = {
  cert: fs.readFileSync('test/fixtures/cert.crt'),
  key: fs.readFileSync('test/fixtures/cert.key')
}

const server = https.createServer(options, (req, res) => {
  res.statusCode = 200
  res.setHeader('Content-Length', 10)
  res.write('hello world!')
  res.end()
})

server.listen(0, () => {
  const { port } = server.address()
  console.log('server is bound on', port)

  const client = https.request({ port }, (res) => {
    res.on('data', (data) => console.log(data.toString()))
  })
  client.end()
})

API

HTTPSAgent

createConnection(opts?: HTTPSSocketOptions): HTTPSSocket

Source

Creates a new HTTPSSocket connection wrapping a plain TCP connection in TLS.

Parameters

ParameterTypeDefaultDescription
opts?HTTPSSocketOptionsOptions for the underlying TCP connection and its TLS wrapper.

HTTPSAgent.global: HTTPSAgent

Source

The agent's own default instance (created with keepAlive: 1000 and timeout: 5000), used as bare-https's globalAgent.

HTTPSServer

HTTPSServer

new HTTPSServer(opts?: HTTPSServerOptions, onrequest?: (req: HTTPIncomingMessage, res: HTTPServerResponse) => void)
Source

An HTTPS server, reusing bare-http1's request parsing and response handling over HTTPSSocket connections instead of plain TCPSocket connections.

Parameters

ParameterTypeDefaultDescription
opts?HTTPSServerOptionsServer options: TLS socket options (e.g. cert, key) plus bare-http1 server connection options.
onrequest?(req: HTTPIncomingMessage, res: HTTPServerResponse) => voidAdded as a 'request' listener.

HTTPSClientRequest

new HTTPSClientRequest(opts?: HTTPSClientRequestOptions, onresponse?: () => void)

Source

An outgoing HTTPS request, extending bare-http1's HTTPClientRequest but defaulting to an HTTPSAgent instead of an HTTPAgent.

Overloads:

new HTTPSClientRequest(opts?: HTTPSClientRequestOptions, onresponse?: () => void)
new HTTPSClientRequest(onresponse: () => void)

Parameters

ParameterTypeDefaultDescription
opts?HTTPSClientRequestOptionsbare-http1 client request options; agent defaults to HTTPSAgent.global, or pass agent: false to use a fresh HTTPSAgent.
onresponse?() => voidAdded as a one-time 'response' listener.

Functions

createServer

createServer(opts?: HTTPSServerOptions, onrequest?: (req: HTTPIncomingMessage, res: HTTPServerResponse) => void): HTTPSServer
Source

Creates an HTTPSServer. If onrequest is given, it's added as a 'request' listener.

Parameters

ParameterTypeDefaultDescription
opts?HTTPSServerOptionsServer options: TLS socket options (e.g. cert, key) plus bare-http1 server connection options.
onrequest?(req: HTTPIncomingMessage, res: HTTPServerResponse) => voidAdded as a 'request' listener.

request

request(url: URL | string, opts?: HTTPSClientRequestOptions, onresponse?: (res: HTTPIncomingMessage) => void): HTTPSClientRequest
Source

Creates an HTTPSClientRequest to url (a URL or a URL string), using TLS. If onresponse is given, it's added as a one-time 'response' listener. Does not send the request until it's ended.

Parameters

ParameterTypeDefaultDescription
urlURL | stringThe URL to request, as a URL object or a URL string.
opts?HTTPSClientRequestOptionsbare-http1 client request options; agent defaults to globalAgent, or pass agent: false to use a fresh HTTPSAgent.
onresponse?(res: HTTPIncomingMessage) => voidAdded as a one-time 'response' listener.

Constants and variables

globalAgent: HTTPSAgent

Source

The default HTTPSAgent used by request() when no agent option is given.

Types

HTTPSSocketEvents

interface HTTPSSocketEvents extends TLSSocketEvents, TCPSocketEvents {}
Source

The events an HTTPSSocket emits: those of both TLSSocket and TCPSocket.

HTTPSSocketOptions

interface HTTPSSocketOptions
  extends TLSSocketOptions, TCPSocketOptions, TCPSocketConnectOptions {}
Source

Options for HTTPSSocket: those of TLSSocket combined with TCPSocket's connection options.

Classes

HTTPSSocket

Source
class HTTPSSocket {
}

See also

On this page