-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (69 loc) · 2.79 KB
/
server.js
File metadata and controls
95 lines (69 loc) · 2.79 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
const express = require('express')
const bodyParser = require('body-parser')
const Hypermerge = require('./hypermerge')
const createDocumentLink = require('./share-link')
const parseDocumentLink = require('./share-link')
const _shareLink = require('./share-link');
const app = express()
const HYPERMERGE_PATH = '/tmp/hypermerge' // shrug?
const hm = new Hypermerge({ storage: HYPERMERGE_PATH, port: 0 })
hm.once('ready', () => {
hm.joinSwarm()
app.use( bodyParser.json() )
app.get('/', (req, res) => res.send(`
<script type="text/javascript">
navigator.registerProtocolHandler("web+pushpin", "//" + window.location.host + "/redirect?q=%s", "HyperMerge REST");
</script>
<h1>HyperMerge REST</h1>
<p>Note, you can accept this as a place to open web+pushpin:// links. Then this should work:</p>
<p><a href="web+pushpin://some-type/9ahVrn5UZuMusdacs8xsFE2HCtmWfC9R1z4xpAi2AjBY/6Dm">web+pushpin://some-type/9ahVrn5UZuMusdacs8xsFE2HCtmWfC9R1z4xpAi2AjBY/6Dm</a></p>
<p>Checkout the docs: <a href="https://github.com/ukd1/hypermerge-rest">https://github.com/ukd1/hypermerge-rest</a></p>
`))
app.get('/redirect', function (req, res) {
if (req.query.q.substr(0,11) == 'web+pushpin') {
parsed = _shareLink.parseDocumentLink(req.query.q.substr(4))
var link = _shareLink.createDocumentLink(parsed.type, parsed.docId)
res.redirect(link.substr(9))
} else {
res.status(400)
}
})
app.get('/:type/:hash/:crc', function (req, res) {
pp_url = 'pushpin://' + req.params.type + '/' + req.params.hash + '/' + req.params.crc
parsed = _shareLink.parseDocumentLink(pp_url)
const handle = hm.openHandle(parsed.docId)
res.set('X-PushPin-DocId', parsed.docId);
res.set('X-PushPin-Ready', hm.readyIndex[parsed.docId])
doc = handle.get()
if (doc) {
res.json(doc.contents)
} else {
res.status(202)
}
})
app.put('/:type/:hash/:crc', function (req, res) {
pp_url = 'pushpin://' + req.params.type + '/' + req.params.hash + '/' + req.params.crc
parsed = _shareLink.parseDocumentLink(pp_url)
const handle = hm.openHandle(parsed.docId)
handle.change((doc) => {
doc.contents = req.body
})
res.set('X-PushPin-DocId', parsed.docId);
res.json(req.body)
})
app.post('/:type', function (req, res) {
const doc = hm.create()
const docId = hm.getId(doc)
const handle = hm.openHandle(docId)
console.log(req.params.type, req.body)
handle.change((doc) => {
doc.contents = req.body
})
var link = _shareLink.createDocumentLink(req.params.type, docId)
res.set('X-PushPin-DocId', docId);
res.set('X-PushPin-Link', link);
res.json(req.body)
})
port = process.env.PORT || 3000
app.listen(port, () => console.log('HyperMerge REST listening on port ' + port + '!'))
})