OverviewFilesCommitsIssuesDocsReleasesPeers

favela-gateway / lib / spacedust-socket.js

23 lines · javascript ref:
// spacedust-socket.js — the Bare-only half of the spacedust client.
//
// This file exists so `spacedust.js` can stay runtime-agnostic and unit-testable under plain Node.
// `bare-ws` transitively pulls native addons (bare-dns), so any module that imports it is Bare-only.
// Keeping that import here means the protocol logic — URL building, event parsing, filter framing,
// queue behaviour — is testable with `node`, and only the socket itself needs `bare`.
//
// bare-ws details that matter and were verified against its source:
//   - `new Socket(url)` accepts a URL string and derives host/path/port/secure from it, so `wss://`
//     with a query string works (lib/socket.js:18-21, :77 routes through bare-https).
//   - It auto-replies PONG with the ECHOED payload on PING (lib/socket.js:163-165), which is exactly
//     what spacedust's 30s keepalive verifies. Nothing to implement.
//   - It is a non-objectMode Duplex that pushes one Buffer per message (:195), so messages arrive as
//     'data' events — see the framing note in spacedust.js.

import bareWs from 'bare-ws'
import { USER_AGENT } from './spacedust.js'

// The production socket factory. Pass as `createSocket` to SpacedustClient.
export function createBareSocket (url) {
  return new bareWs.Socket(url, { headers: { 'User-Agent': USER_AGENT } })
}