braid-blob 0.0.9 → 0.0.11

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 CHANGED
@@ -5,6 +5,7 @@ var {http_server: braidify, free_cors} = require('braid-http'),
5
5
 
6
6
  var braid_blob = {
7
7
  db_folder: './braid-blob-db',
8
+ meta_folder: './braid-blob-meta',
8
9
  cache: {}
9
10
  }
10
11
 
@@ -24,15 +25,28 @@ braid_blob.serve = async (req, res, options = {}) => {
24
25
 
25
26
  await within_fiber(options.key, async () => {
26
27
  const filename = `${braid_blob.db_folder}/${encode_filename(options.key)}`
28
+ const metaname = `${braid_blob.meta_folder}/${encode_filename(options.key)}`
27
29
 
30
+ // Read the meta file
31
+ var meta = {}
28
32
  try {
29
- var our_v = Math.round((await fs.promises.stat(filename)).mtimeMs)
33
+ meta = JSON.parse(await fs.promises.readFile(metaname, 'utf8'))
30
34
  } catch (e) {}
35
+ var our_v = meta.version
31
36
 
32
37
  if (req.method === 'GET') {
33
38
  // Handle GET request for binary files
34
- res.setHeader('Current-Version', our_v != null ? `"${our_v}"` : '')
35
39
 
40
+ // Set Version header;
41
+ // but if this is a subscription,
42
+ // then we set Current-Version instead
43
+ res.setHeader((req.subscribe ? 'Current-' : '') + 'Version',
44
+ our_v != null ? `"${our_v}"` : '')
45
+
46
+ // Set Content-Type
47
+ if (meta.content_type)
48
+ res.setHeader('Content-Type', meta.content_type)
49
+
36
50
  if (!req.subscribe)
37
51
  return res.end(our_v != null ?
38
52
  await fs.promises.readFile(filename) : '')
@@ -69,6 +83,7 @@ braid_blob.serve = async (req, res, options = {}) => {
69
83
 
70
84
  // Ensure directory exists
71
85
  await fs.promises.mkdir(path.dirname(filename), { recursive: true })
86
+ await fs.promises.mkdir(path.dirname(metaname), { recursive: true })
72
87
 
73
88
  var their_v =
74
89
  !req.version ?
@@ -83,7 +98,12 @@ braid_blob.serve = async (req, res, options = {}) => {
83
98
 
84
99
  // Write the file
85
100
  await fs.promises.writeFile(filename, body)
86
- await fs.promises.utimes(filename, new Date(), new Date(their_v))
101
+
102
+ // Write the meta file
103
+ meta.version = their_v
104
+ if (req.headers['content-type'])
105
+ meta.content_type = req.headers['content-type']
106
+ await fs.promises.writeFile(metaname, JSON.stringify(meta))
87
107
 
88
108
  // Notify all subscriptions of the update
89
109
  // (except the peer which made the PUT request itself)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-blob",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "description": "Library for collaborative blobs over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-blob",
package/server-demo.js CHANGED
@@ -15,7 +15,7 @@ var server = require("http").createServer(async (req, res) => {
15
15
 
16
16
  server.listen(port, () => {
17
17
  console.log(`server started on port ${port}`)
18
- console.log(`files stored in: ${braid_blob.storage_base}`)
18
+ console.log(`files stored in: ${braid_blob.db_folder}`)
19
19
  })
20
20
 
21
21
  // curl -X PUT --data-binary @image.png http://localhost:8888/image.png