braid-blob 0.0.7 → 0.0.8

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.
Files changed (2) hide show
  1. package/index.js +31 -16
  2. 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,15 @@ 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(!our_v ? '' : await fs.promises.readFile(filename))
37
+ return res.end(our_v != null ?
38
+ await fs.promises.readFile(filename) : '')
41
39
 
42
40
  // Start a subscription for future updates.
43
41
  if (!key_to_subs[options.key]) key_to_subs[options.key] = new Map()
@@ -50,34 +48,51 @@ braid_blob.serve = async (req, res, options = {}) => {
50
48
  delete key_to_subs[options.key]
51
49
  }})
52
50
 
53
- if (!req.parents || 1*req.parents[0] < our_v)
51
+
52
+ // Send an immediate update when:
53
+ if (!req.parents || // 1) They have no version history
54
+ // (need full sync)
55
+ (our_v != null && ( // 2) We have a version AND...
56
+ !req.parents.length || // a) Their version is the empty set
57
+ our_v > 1*req.parents[0] // b) Our version is newer
58
+ )))
54
59
  return res.sendUpdate({
55
- version: ['' + our_v],
56
- body: !our_v ? '' : await fs.promises.readFile(filename)
60
+ version: our_v != null ? ['' + our_v] : [],
61
+ body: our_v != null ? await fs.promises.readFile(filename) : ''
57
62
  })
58
- else res.write('\n\n') // get it to send headers
63
+ else res.write('\n\n') // get the node http code to send headers
59
64
  } else if (req.method === 'PUT') {
60
65
  // Handle PUT request to update binary files
61
66
 
62
67
  // Ensure directory exists
63
68
  await fs.promises.mkdir(path.dirname(filename), { recursive: true })
64
69
 
65
- var their_v = req.version && 1*req.version[0]
66
- if (typeof their_v != 'number') their_v = 0
67
-
68
- if (their_v > our_v) {
70
+ var their_v =
71
+ !req.version ?
72
+ // we'll give them a version in this case
73
+ Math.max(our_v != null ? our_v + 1 : 0, Date.now()) :
74
+ !req.version.length ?
75
+ null :
76
+ 1*req.version[0]
77
+
78
+ if (their_v != null &&
79
+ (our_v == null || their_v > our_v)) {
80
+
69
81
  // Write the file
70
82
  await fs.promises.writeFile(filename, body)
71
83
  await fs.promises.utimes(filename, new Date(), new Date(their_v))
72
84
 
73
- // Notify all subscriptions of the update (except the peer which made the PUT request itself)
85
+ // Notify all subscriptions of the update
86
+ // (except the peer which made the PUT request itself)
74
87
  if (key_to_subs[options.key])
75
88
  for (var [peer, sub] of key_to_subs[options.key].entries())
76
89
  if (peer !== req.peer)
77
90
  sub.sendUpdate({ body, version: ['' + their_v] })
78
91
 
79
92
  res.setHeader("Version", `"${their_v}"`)
80
- } else res.setHeader("Version", `"${our_v}"`)
93
+ } else {
94
+ res.setHeader("Version", our_v != null ? `"${our_v}"` : '')
95
+ }
81
96
  res.end('')
82
97
  }
83
98
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-blob",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Library for collaborative blobs over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-blob",