harn-stdlib 0.10.157

Embedded Harn standard library source catalog
Documentation
/** std/net - local networking helpers for scripts. */
type UnixSocketJsonOptions = {timeout_ms?: int, max_response_bytes?: int}

type UnixSocketJsonResult = {
  ok: bool,
  status: string,
  path: string,
  duration_ms: int,
  response?: any,
  raw_response?: string,
  bytes_read?: int,
  error?: string,
}

/**
 * Send one JSON request line to a Unix-domain socket and read one JSON response line.
 *
 * Expected readiness failures return `{ok: false, status, error?}` instead of throwing.
 *
 * @effects: [network]
 * @errors: []
 * @example: unix_socket_json_request(harness.net, path, {id: "ping", method: "ping"})
 */
pub fn unix_socket_json_request(
  net: HarnessNet,
  path: string,
  request: any,
  options: UnixSocketJsonOptions = {},
) -> UnixSocketJsonResult {
  return net.unix_socket_json_request(path, request, options ?? {})
}

/**
 * Parse an HTTP `Retry-After` header value into a wait in milliseconds.
 *
 * Accepts both RFC 9110 forms — delay seconds (integer or fractional) and an
 * HTTP-date — returning `0` for values already in the past and `nil` when the
 * value is empty or unparseable. One owner for every retry surface: hand-rolled
 * copies that assumed the numeric form crashed on legal date headers.
 *
 * @effects: []
 * @errors: []
 * @example: http_retry_after_ms(harness.clock, "120")
 */
pub fn http_retry_after_ms(clock: HarnessClock, value: any) -> int? {
  return __http_retry_after_ms_at(to_string(value ?? ""), clock.timestamp())
}