Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

1.5 KB
Raw Blame
import { once } from 'node:events'
import { createServer } from 'node:http'
import type { AddressInfo } from 'node:net'
import WebSocket from 'ws'
import { acceptWebSocket } from './websocket'

describe('App database WebSocket framing', () => {
  it('reassembles fragmented binary messages, replies to ping, and refuses text payloads', async () => {
    const server = createServer()
    server.on('upgrade', (req, socket, head) => {
      const conn = acceptWebSocket(req, socket, head)!
      conn.on('data', (bytes) => conn.send(bytes))
    })
    server.listen(0, '127.0.0.1')
    await once(server, 'listening')
    const client = new WebSocket('ws://127.0.0.1:' + (server.address() as AddressInfo).port)
    try {
      await once(client, 'open')
      for (const n of [30, 128, 66000]) {
        const payload = Buffer.alloc(n, 0x61)
        const received = once(client, 'message')
        client.send(payload.subarray(0, 10), { fin: false })
        client.send(payload.subarray(10), { fin: true })
        expect(Buffer.compare((await received)[0], payload)).toBe(0)
      }
      const pong = once(client, 'pong')
      client.ping('hello')
      expect((await pong)[0].toString()).toBe('hello')
      const end = once(client, 'close')
      client.send('a text message')
      const [code, reason] = await end
      expect([code, reason.toString()]).toEqual([1003, 'binary messages only'])
    } finally {
      client.terminate()
      await new Promise<void>((r) => server.close(() => r()))
    }
  })
})