braid-blob 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +34 -16
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -13,7 +13,6 @@ var key_to_subs = {}
|
|
|
13
13
|
braid_blob.serve = async (req, res, options = {}) => {
|
|
14
14
|
if (!options.key) options.key = decodeURIComponent(req.url.split('?')[0])
|
|
15
15
|
|
|
16
|
-
|
|
17
16
|
braidify(req, res)
|
|
18
17
|
if (res.is_multiplexer) return
|
|
19
18
|
|
|
@@ -28,16 +27,18 @@ braid_blob.serve = async (req, res, options = {}) => {
|
|
|
28
27
|
|
|
29
28
|
try {
|
|
30
29
|
var our_v = Math.round((await fs.promises.stat(filename)).mtimeMs)
|
|
31
|
-
} catch (e) {
|
|
32
|
-
var our_v = 0
|
|
33
|
-
}
|
|
30
|
+
} catch (e) {}
|
|
34
31
|
|
|
35
32
|
if (req.method === 'GET') {
|
|
36
33
|
// Handle GET request for binary files
|
|
37
|
-
res.setHeader('Current-Version', `"${our_v}"`)
|
|
34
|
+
res.setHeader('Current-Version', our_v != null ? `"${our_v}"` : '')
|
|
38
35
|
|
|
39
36
|
if (!req.subscribe)
|
|
40
|
-
return res.end(
|
|
37
|
+
return res.end(our_v != null ?
|
|
38
|
+
await fs.promises.readFile(filename) : '')
|
|
39
|
+
|
|
40
|
+
if (!res.hasHeader("editable"))
|
|
41
|
+
res.setHeader("Editable", "true")
|
|
41
42
|
|
|
42
43
|
// Start a subscription for future updates.
|
|
43
44
|
if (!key_to_subs[options.key]) key_to_subs[options.key] = new Map()
|
|
@@ -50,34 +51,51 @@ braid_blob.serve = async (req, res, options = {}) => {
|
|
|
50
51
|
delete key_to_subs[options.key]
|
|
51
52
|
}})
|
|
52
53
|
|
|
53
|
-
|
|
54
|
+
|
|
55
|
+
// Send an immediate update when:
|
|
56
|
+
if (!req.parents || // 1) They have no version history
|
|
57
|
+
// (need full sync)
|
|
58
|
+
(our_v != null && ( // 2) We have a version AND...
|
|
59
|
+
!req.parents.length || // a) Their version is the empty set
|
|
60
|
+
our_v > 1*req.parents[0] // b) Our version is newer
|
|
61
|
+
)))
|
|
54
62
|
return res.sendUpdate({
|
|
55
|
-
version: ['' + our_v],
|
|
56
|
-
body:
|
|
63
|
+
version: our_v != null ? ['' + our_v] : [],
|
|
64
|
+
body: our_v != null ? await fs.promises.readFile(filename) : ''
|
|
57
65
|
})
|
|
58
|
-
else res.write('\n\n') // get
|
|
66
|
+
else res.write('\n\n') // get the node http code to send headers
|
|
59
67
|
} else if (req.method === 'PUT') {
|
|
60
68
|
// Handle PUT request to update binary files
|
|
61
69
|
|
|
62
70
|
// Ensure directory exists
|
|
63
71
|
await fs.promises.mkdir(path.dirname(filename), { recursive: true })
|
|
64
72
|
|
|
65
|
-
var their_v =
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
73
|
+
var their_v =
|
|
74
|
+
!req.version ?
|
|
75
|
+
// we'll give them a version in this case
|
|
76
|
+
Math.max(our_v != null ? our_v + 1 : 0, Date.now()) :
|
|
77
|
+
!req.version.length ?
|
|
78
|
+
null :
|
|
79
|
+
1*req.version[0]
|
|
80
|
+
|
|
81
|
+
if (their_v != null &&
|
|
82
|
+
(our_v == null || their_v > our_v)) {
|
|
83
|
+
|
|
69
84
|
// Write the file
|
|
70
85
|
await fs.promises.writeFile(filename, body)
|
|
71
86
|
await fs.promises.utimes(filename, new Date(), new Date(their_v))
|
|
72
87
|
|
|
73
|
-
// Notify all subscriptions of the update
|
|
88
|
+
// Notify all subscriptions of the update
|
|
89
|
+
// (except the peer which made the PUT request itself)
|
|
74
90
|
if (key_to_subs[options.key])
|
|
75
91
|
for (var [peer, sub] of key_to_subs[options.key].entries())
|
|
76
92
|
if (peer !== req.peer)
|
|
77
93
|
sub.sendUpdate({ body, version: ['' + their_v] })
|
|
78
94
|
|
|
79
95
|
res.setHeader("Version", `"${their_v}"`)
|
|
80
|
-
} else
|
|
96
|
+
} else {
|
|
97
|
+
res.setHeader("Version", our_v != null ? `"${our_v}"` : '')
|
|
98
|
+
}
|
|
81
99
|
res.end('')
|
|
82
100
|
}
|
|
83
101
|
})
|