-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver-demo.js
More file actions
77 lines (66 loc) · 2.51 KB
/
server-demo.js
File metadata and controls
77 lines (66 loc) · 2.51 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
var port = process.argv[2] || 8888
var braid_text = require("./server.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
braid_text.free_cors(res)
if (req.method === 'OPTIONS') return res.end()
var q = req.url.split('?').slice(-1)[0]
if (q === 'editor' || q === 'markdown-editor' || q === 'yjs-editor') {
res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
require("fs").createReadStream(`./client/${q}.html`).pipe(res)
return
}
var libs = new Set([
'simpleton.js',
'text-client.js',
'yjs-sync.js',
'cursor-sync.js',
'textarea-highlights.js',
'myers-diff.js',
'syncarea.js'
])
if (libs.has(req.url.substr(1))) {
res.writeHead(200, { "Content-Type": "text/javascript", "Cache-Control": "no-cache" })
require("fs").createReadStream("./client" + req.url).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, {full_response: true})).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}`)
})