Open History to see the last commit
1import assert from 'node:assert/strict'
2import { createRequire } from 'node:module'
3import { once } from 'node:events'
4import { setTimeout as delay } from 'node:timers/promises'
5import { pathToFileURL } from 'node:url'
6import { resolve } from 'node:path'
7
8// Resolve only from the isolated install supplied by the clean-install runner.
9const require = createRequire(
10 pathToFileURL(resolve(process.argv[2], 'packages/server/package.json'))
11)
12const { createRealtime, rawSql } = await import(
13 pathToFileURL(require.resolve('@neon/realtime/server')).href
14)
15const { WebSocketServer } = require('ws')
16const server = new WebSocketServer({ port: 0, host: '127.0.0.1' })
17await once(server, 'listening')
18const connected = once(server, 'connection')
19const sdk = createRealtime({
20 url: `ws://127.0.0.1:${server.address().port}`,
21 secret: `nrt_live_1${Buffer.alloc(32, 7).toString('base64url')}`,
22 db: 'neondb',
23})
24try {
25 const subscription = await sdk.subscribe(rawSql('select id from items'), {
26 materialize: false,
27 rawValues: true,
28 })
29 const events = []
30 subscription.onSource((e) => events.push(e))
31 const [socket] = await connected
32 const send = (e) => socket.send(JSON.stringify(e))
33 send({ type: 'ready' })
34 const [raw] = await once(socket, 'message')
35 const command = JSON.parse(raw.toString())
36 const columns = [
37 { name: 'id', type_oid: 99999, typmod: -1, codec: 'pg_text' },
38 ]
39 const row = { row_key: 'a'.repeat(64), values: ['9007199254740993'] }
40 const mvcc = { xmin: '10', xmax: '10', xip: [] }
41 send({
42 type: 'subscribed',
43 request_id: command.request_id,
44 live_id: '1',
45 epoch: '1',
46 first_sequence: '1',
47 columns,
48 })
49 send({
50 type: 'baseline_sync_start',
51 live_id: '1',
52 epoch: '1',
53 baseline_sync_attempt: '1',
54 mvcc,
55 })
56 send({
57 type: 'baseline_sync_batch',
58 live_id: '1',
59 epoch: '1',
60 baseline_sync_attempt: '1',
61 index: 0,
62 rows: [row],
63 })
64 send({
65 type: 'baseline_sync_end',
66 live_id: '1',
67 epoch: '1',
68 baseline_sync_attempt: '1',
69 batch_count: 1,
70 })
71 send({ type: 'open', publication_id: 'p1' })
72 send({
73 type: 'keyed_results',
74 publication_id: 'p1',
75 index: 0,
76 targets: [{ live_id: '1', epoch: '1', sequence: '1' }],
77 changes: [{ op: 'upsert', ...row }],
78 txids: ['9007199254740995'],
79 })
80 send({
81 type: 'commit',
82 publication_id: 'p1',
83 body_count: 1,
84 frontier: { lsn: '1/ABCDEF01' },
85 })
86 send({ type: 'progress', mvcc: { xmin: '11', xmax: '11', xip: [] } })
87 for (let i = 0; i < 200 && events.length < 4; i++) await delay(10)
88 assert.deepEqual(
89 events.map((e) => e.type),
90 ['schema', 'baseline', 'publication', 'progress']
91 )
92 assert.deepEqual(events[1].rows, [row])
93 assert.deepEqual(events[1].mvcc, mvcc)
94 assert.deepEqual(events[2].frontier, { lsn: '1/ABCDEF01' })
95 assert.deepEqual(events[2].txids, ['9007199254740995'])
96 assert.equal(subscription.getState().status, 'live')
97 console.log(
98 'Clean frozen install: Realtime raw values, schema, MVCC, frontier and direct wrapper verified'
99 )
100} finally {
101 sdk.close()
102 for (const socket of server.clients) socket.terminate()
103 await new Promise((resolve) => server.close(() => resolve()))
104}