-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
364 lines (323 loc) · 12.9 KB
/
server.js
File metadata and controls
364 lines (323 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Test server for braid-fuzz.
// Wraps braid-text with a control API so tests can:
// - create/destroy documents
// - make server-side edits
// - read server-side state
// - configure behavior (delays, drops, heartbeats)
//
// Plain HTTP/1.1 (no TLS) — the socket proxy sits between this
// and the client for fault injection.
var http = require("http")
var braid_text = require("braid-text")
var { http_server: braidify } = require("braid-http")
class TestServer {
constructor(opts = {}) {
this.port = opts.port || 0 // 0 = auto-assign
this.host = opts.host || "127.0.0.1"
this.server = null
this.docs = new Set()
// Configurable behavior
this.ack_delay_ms = 0
this.drop_puts = false
this.drop_acks = false
this.heartbeat_seconds = 0
// Per-request hooks for tests to observe traffic
this._on_put = null
this._on_get = null
this._on_subscribe = null
// Track active subscriptions so we can force-close them
this._subscriptions = []
braid_text.db_folder = null
}
start() {
return new Promise((resolve, reject) => {
this.server = http.createServer(async (req, res) => {
try {
await this._handle(req, res)
} catch (e) {
console.error(`server error: ${req.method} ${req.url}: ${e.message}`)
if (!res.headersSent) { res.writeHead(500); res.end() }
}
})
this.server.on("error", reject)
this.server.listen(this.port, this.host, () => {
this.port = this.server.address().port
resolve(this.port)
})
})
}
async _handle(req, res) {
// CORS
res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader("Access-Control-Allow-Methods", "*")
res.setHeader("Access-Control-Allow-Headers", "*")
res.setHeader("Access-Control-Expose-Headers", "*")
if (req.method === "OPTIONS") { res.writeHead(200); res.end(); return }
var url = req.url
// ── Control API (/control/*) ─────────────────────────────
if (url.startsWith("/control/")) {
await this._handle_control(req, res, url)
return
}
// ── Document endpoints (everything else goes to braid-text) ──
// Track the doc
this.docs.add(url)
// Notify test hooks
if (req.method === "PUT" && this._on_put) this._on_put(req, res, url)
if (req.method === "GET" && this._on_get) this._on_get(req, res, url)
// Drop PUTs entirely if configured
if (req.method === "PUT" && this.drop_puts) {
req.on("data", () => {})
req.on("end", () => res.destroy())
return
}
// For PUTs, optionally delay the ACK or drop it
if (req.method === "PUT") {
if (this.drop_acks) {
// Process the PUT but destroy the response instead of acknowledging
res.writeHead = function () {} // swallow
res.end = function () { res.destroy() }
} else if (this.ack_delay_ms > 0) {
var delay = this.ack_delay_ms
var orig_writeHead = res.writeHead.bind(res)
var orig_end = res.end.bind(res)
var buffered_args = null
res.writeHead = function (...args) { buffered_args = args }
res.end = function (...args) {
setTimeout(() => {
if (buffered_args) orig_writeHead(...buffered_args)
orig_end(...args)
}, delay)
}
}
}
// For subscriptions, track them and optionally add heartbeat header
braidify(req, res)
if (req.method === "GET" && req.subscribe) {
if (this._on_subscribe) this._on_subscribe(req, res, url)
var sub = { res, url }
this._subscriptions.push(sub)
var orig_start = res.startSubscription.bind(res)
res.startSubscription = (opts) => {
var orig_onClose = opts && opts.onClose
orig_start({
...opts,
onClose: () => {
this._subscriptions = this._subscriptions.filter(s => s !== sub)
if (orig_onClose) orig_onClose()
}
})
}
// Add heartbeat support
if (this.heartbeat_seconds > 0) {
res.setHeader("Heartbeats", String(this.heartbeat_seconds))
var hb_interval = setInterval(() => {
try { res.write("\n") } catch (e) { clearInterval(hb_interval) }
}, this.heartbeat_seconds * 1000)
res.on("close", () => clearInterval(hb_interval))
}
}
await braid_text.serve(req, res)
}
async _handle_control(req, res, url) {
var body = await read_body(req)
// GET /control/state?doc=/path — get server's copy of a doc
if (url.startsWith("/control/state")) {
var doc = new URL(url, "http://localhost").searchParams.get("doc")
if (!doc) { res.writeHead(400); res.end("missing ?doc= param"); return }
var state = await braid_text.get(doc)
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ doc, state: state || "" }))
return
}
// POST /control/edit — make a server-side edit
if (url === "/control/edit" && req.method === "POST") {
var { doc, patches } = JSON.parse(body)
await braid_text.put(doc, { patches })
var state = await braid_text.get(doc)
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ ok: true, state }))
return
}
// POST /control/configure — change server behavior
if (url === "/control/configure" && req.method === "POST") {
var cfg = JSON.parse(body)
if (cfg.ack_delay_ms != null) this.ack_delay_ms = cfg.ack_delay_ms
if (cfg.drop_puts != null) this.drop_puts = cfg.drop_puts
if (cfg.drop_acks != null) this.drop_acks = cfg.drop_acks
if (cfg.heartbeat_seconds != null) this.heartbeat_seconds = cfg.heartbeat_seconds
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ ok: true }))
return
}
// POST /control/kill-subscriptions — force-close all active subscriptions for a doc
if (url === "/control/kill-subscriptions" && req.method === "POST") {
var { doc } = JSON.parse(body)
var killed = 0
for (var sub of this._subscriptions.slice()) {
if (!doc || sub.url === doc) {
try { sub.res.destroy() } catch (e) {}
killed++
}
}
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ ok: true, killed }))
return
}
// POST /control/reset — reset all server state
if (url === "/control/reset" && req.method === "POST") {
for (var sub of this._subscriptions.slice()) {
try { sub.res.destroy() } catch (e) {}
}
this._subscriptions = []
this.ack_delay_ms = 0
this.drop_puts = false
this.drop_acks = false
this.docs.clear()
this._on_put = null
this._on_get = null
this._on_subscribe = null
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ ok: true }))
return
}
// GET /control/digest?doc=/path — get SHA-256 digest of doc state
if (url.startsWith("/control/digest")) {
var doc = new URL(url, "http://localhost").searchParams.get("doc")
if (!doc) { res.writeHead(400); res.end("missing ?doc= param"); return }
var state = await braid_text.get(doc)
var digest = get_digest(state || "")
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ doc, digest }))
return
}
res.writeHead(404)
res.end("unknown control endpoint")
}
// Get server state for a doc directly (for tests running in-process)
async get_doc_state(doc) {
return (await braid_text.get(doc)) || ""
}
// Get SHA-256 digest of doc state (for tests running in-process)
async get_doc_digest(doc) {
var state = await this.get_doc_state(doc)
return get_digest(state)
}
// Make a server-side edit directly (for tests running in-process)
async edit_doc(doc, patches) {
await braid_text.put(doc, { patches })
return await this.get_doc_state(doc)
}
// Insert text at position
async insert_at(doc, pos, text) {
return this.edit_doc(doc, [{ unit: "text", range: `[${pos}:${pos}]`, content: text }])
}
// Delete text at range
async delete_range(doc, start, end) {
return this.edit_doc(doc, [{ unit: "text", range: `[${start}:${end}]`, content: "" }])
}
// Replace text at range
async replace_range(doc, start, end, text) {
return this.edit_doc(doc, [{ unit: "text", range: `[${start}:${end}]`, content: text }])
}
// Write a raw update directly to active subscriptions for a doc.
// Useful for sending updates that braid-text doesn't support natively
// (e.g. Patches: 0).
send_raw_update(doc, update_str) {
for (var sub of this._subscriptions) {
if (sub.url === doc) {
try { sub.res.write(update_str) } catch (e) {}
}
}
}
// Get cursor snapshot for a doc via regular HTTP GET
async get_cursors(doc) {
return new Promise((resolve, reject) => {
var req = http.request({
hostname: this.host,
port: this.port,
path: doc,
method: "GET",
headers: { "Accept": "application/text-cursors+json" }
}, res => {
var body = ""
res.on("data", d => body += d)
res.on("end", () => {
try { resolve(JSON.parse(body)) }
catch (e) { resolve({}) }
})
})
req.on("error", reject)
req.end()
})
}
// Subscribe a simulated peer to cursors (required before set_cursor)
async subscribe_cursors(doc, peer) {
return new Promise((resolve, reject) => {
var req = http.request({
hostname: this.host,
port: this.port,
path: doc,
method: "GET",
headers: {
"Accept": "application/text-cursors+json",
"Subscribe": "true",
"Peer": peer
}
}, res => {
// Keep the subscription open (don't consume/close)
resolve(res)
})
req.on("error", reject)
req.end()
})
}
// Set cursor for a peer via regular HTTP PUT
async set_cursor(doc, peer, selections) {
return new Promise((resolve, reject) => {
var body = JSON.stringify(selections)
var req = http.request({
hostname: this.host,
port: this.port,
path: doc,
method: "PUT",
headers: {
"Content-Type": "application/text-cursors+json",
"Content-Range": `json ["${peer}"]`,
"Peer": peer,
"Content-Length": Buffer.byteLength(body)
}
}, res => {
res.resume()
resolve(res.statusCode)
})
req.on("error", reject)
req.end(body)
})
}
subscription_count(doc) {
if (doc) return this._subscriptions.filter(s => s.url === doc).length
return this._subscriptions.length
}
async stop() {
for (var sub of this._subscriptions.slice()) {
try { sub.res.destroy() } catch (e) {}
}
if (this.server) {
await new Promise(resolve => this.server.close(resolve))
this.server = null
}
}
}
function get_digest(text) {
var buffer = Buffer.from(text, "utf8")
return `sha-256=:${require("crypto").createHash("sha256").update(buffer).digest("base64")}:`
}
function read_body(req) {
return new Promise((resolve) => {
var body = ""
req.on("data", d => body += d)
req.on("end", () => resolve(body))
})
}
module.exports = { TestServer }