braid-blob 0.0.10 → 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 +17 -2
- package/package.json +1 -1
- package/server-demo.js +1 -1
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,10 +25,14 @@ 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
|
-
|
|
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
|
|
@@ -37,6 +42,10 @@ braid_blob.serve = async (req, res, options = {}) => {
|
|
|
37
42
|
// then we set Current-Version instead
|
|
38
43
|
res.setHeader((req.subscribe ? 'Current-' : '') + 'Version',
|
|
39
44
|
our_v != null ? `"${our_v}"` : '')
|
|
45
|
+
|
|
46
|
+
// Set Content-Type
|
|
47
|
+
if (meta.content_type)
|
|
48
|
+
res.setHeader('Content-Type', meta.content_type)
|
|
40
49
|
|
|
41
50
|
if (!req.subscribe)
|
|
42
51
|
return res.end(our_v != null ?
|
|
@@ -74,6 +83,7 @@ braid_blob.serve = async (req, res, options = {}) => {
|
|
|
74
83
|
|
|
75
84
|
// Ensure directory exists
|
|
76
85
|
await fs.promises.mkdir(path.dirname(filename), { recursive: true })
|
|
86
|
+
await fs.promises.mkdir(path.dirname(metaname), { recursive: true })
|
|
77
87
|
|
|
78
88
|
var their_v =
|
|
79
89
|
!req.version ?
|
|
@@ -88,7 +98,12 @@ braid_blob.serve = async (req, res, options = {}) => {
|
|
|
88
98
|
|
|
89
99
|
// Write the file
|
|
90
100
|
await fs.promises.writeFile(filename, body)
|
|
91
|
-
|
|
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))
|
|
92
107
|
|
|
93
108
|
// Notify all subscriptions of the update
|
|
94
109
|
// (except the peer which made the PUT request itself)
|
package/package.json
CHANGED
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.
|
|
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
|