-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-demo.js
123 lines (104 loc) · 4.35 KB
/
server-demo.js
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
var port = process.argv[2] || 8888
var braid_text = require("./index.js")
// TODO: set a custom database folder
// (the default is ./braid-text-db)
//
// braid_text.db_folder = './custom_db_folder'
var server = require("http").createServer(async (req, res) => {
console.log(`${req.method} ${req.url}`)
// Free the CORS
free_the_cors(req, res)
if (req.method === 'OPTIONS') return
if (req.url.endsWith("?editor")) {
res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
require("fs").createReadStream("./editor.html").pipe(res)
return
}
if (req.url.endsWith("?markdown-editor")) {
res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
require("fs").createReadStream("./markdown-editor.html").pipe(res)
return
}
if (req.url == '/simpleton-client.js') {
res.writeHead(200, { "Content-Type": "text/javascript", "Cache-Control": "no-cache" })
require("fs").createReadStream("./simpleton-client.js").pipe(res)
return
}
if (req.url.startsWith('/test.html')) {
let parts = req.url.split(/[\?&=]/g)
if (parts[1] === 'check') {
res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-cache" })
return res.end(JSON.stringify({
checking: parts[2],
result: (await braid_text.get(parts[2])) != null
}))
} else if (parts[1] === 'dt_create_bytes_big_name') {
try {
braid_text.dt_create_bytes('x'.repeat(1000000) + '-0', [], 0, 0, 'hi')
return res.end(JSON.stringify({ ok: true }))
} catch (e) {
return res.end(JSON.stringify({ ok: false, error: '' + e }))
}
} else if (parts[1] === 'dt_create_bytes_many_names') {
try {
braid_text.dt_create_bytes('hi-0', new Array(1000000).fill(0).map((x, i) => `x${i}-0`), 0, 0, 'hi')
return res.end(JSON.stringify({ ok: true }))
} catch (e) {
return res.end(JSON.stringify({ ok: false, error: '' + e }))
}
}
res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
require("fs").createReadStream("./test.html").pipe(res)
return
}
// TODO: uncomment out the code below to add /pages endpoint,
// which displays all the currently used keys
//
// if (req.url === '/pages') {
// var pages = await braid_text.list()
// res.writeHead(200, {
// "Content-Type": "application/json",
// "Access-Control-Expose-Headers": "*"
// })
// res.end(JSON.stringify(pages))
// return
// }
// TODO: uncomment out the code below to add basic access control,
// where a user logs in by openning the javascript console
// and entering: document.cookie = 'fake_password'
//
// var admin_pass = "fake_password"
//
// if (req.method == "PUT" || req.method == "POST" || req.method == "PATCH") {
// if (!req.headers.cookie?.split(/;/).map(x => x.trim()).some(x => x === admin_pass)) {
// console.log("Blocked PUT:", { cookie: req.headers.cookie })
// res.statusCode = 401
// return res.end()
// }
// }
// Create some initial text for new documents
if (!(await braid_text.get(req.url, {})).version.length) {
await braid_text.put(req.url, {body: 'This is a fresh blank document, ready for you to edit.' })
}
// Now serve the collaborative text!
braid_text.serve(req, res)
})
server.listen(port, () => {
console.log(`server started on port ${port}`)
})
// Free the CORS!
function free_the_cors (req, res) {
res.setHeader('Range-Request-Allow-Methods', 'PATCH, PUT')
res.setHeader('Range-Request-Allow-Units', 'json')
res.setHeader("Patches", "OK")
var free_the_cors = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS, HEAD, GET, PUT, UNSUBSCRIBE",
"Access-Control-Allow-Headers": "subscribe, client, version, parents, merge-type, content-type, content-range, patches, cache-control, peer"
}
Object.entries(free_the_cors).forEach(x => res.setHeader(x[0], x[1]))
if (req.method === 'OPTIONS') {
res.writeHead(200)
res.end()
}
}