Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

1.7 KB
Raw Blame
import { connect, constants, type ClientHttp2Session } from 'node:http2'
import { readApnsStreamResponse, type ApnsResponse } from './apns-stream-response.js'

export type ApnsRequest = {
  host: string
  path: string
  headers: Record<string, string>
  body: string
}

export type { ApnsResponse }
export type ApnsTransport = (request: ApnsRequest) => Promise<ApnsResponse>

// Cache APNs HTTP/2 sessions per host until socket loss; long-lived connections avoid reconnect overhead.
export function createApnsHttp2Transport(): ApnsTransport & { close(): void } {
  const sessions = new Map<string, ClientHttp2Session>()

  const sessionFor = (host: string): ClientHttp2Session => {
    const existing = sessions.get(host)
    if (existing && !existing.closed && !existing.destroyed) return existing
    const session = connect(`https://${host}`)
    const forget = (): void => {
      if (sessions.get(host) === session) sessions.delete(host)
    }
    session.on('error', forget)
    session.on('close', forget)
    sessions.set(host, session)
    return session
  }

  const transport = async (request: ApnsRequest): Promise<ApnsResponse> => {
    const stream = sessionFor(request.host).request({
      ...request.headers,
      [constants.HTTP2_HEADER_METHOD]: 'POST',
      [constants.HTTP2_HEADER_PATH]: request.path,
      [constants.HTTP2_HEADER_AUTHORITY]: request.host,
      'content-type': 'application/json',
      'content-length': String(Buffer.byteLength(request.body))
    })
    return await readApnsStreamResponse(stream, request.body)
  }

  return Object.assign(transport, {
    close(): void {
      for (const session of sessions.values()) session.close()
      sessions.clear()
    }
  })
}