braid-blob 0.0.1 → 0.0.2

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 +14 -19
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -5,33 +5,18 @@ var {http_server: braidify, free_cors} = require('braid-http'),
5
5
  port = 8888
6
6
 
7
7
  var braid_blob = {
8
- storage_base: './braid-blob-files',
8
+ db_folder: './braid-blob-db',
9
9
  cache: {}
10
10
  }
11
11
 
12
- // Helper function to normalize URL and create host-specific path
13
- function get_storage_path(req) {
14
- // Get host from request headers, default to localhost if not present
15
- const host = req.headers.host || `localhost:${port}`;
16
- // Remove protocol and normalize, similar to index.js
17
- let normalized_host = host.replace(/^https?:\/\//, '');
18
- // Remove any double slashes that might occur
19
- normalized_host = normalized_host.replace(/\/+/g, '/');
20
- // Ensure path doesn't start with a slash (since we'll join with storage_base)
21
- if (normalized_host.startsWith('/')) normalized_host = normalized_host.substring(1);
22
- // Combine host and URL for storage path
23
- const combined_path = `${normalized_host}${req.url}`;
24
- // Remove any double slashes that might result from concatenation
25
- return combined_path.replace(/\/+/g, '/');
26
- }
27
-
28
12
  var subscriptions = {};
29
13
 
30
14
  // Create a hash key for subscriptions based on peer and URL
31
15
  var hash = (req) => JSON.stringify([req.headers.peer, req.url]);
32
16
 
33
17
  braid_blob.serve = async (req, res, options = {}) => {
34
- if (!options.filename) options.filename = path.join(braid_blob.storage_base, get_storage_path(req))
18
+ if (!options.key) options.key = decodeURIComponent(req.url.split('?')[0])
19
+
35
20
 
36
21
  braidify(req, res)
37
22
 
@@ -41,7 +26,7 @@ braid_blob.serve = async (req, res, options = {}) => {
41
26
  // Handle OPTIONS request
42
27
  if (req.method === 'OPTIONS') return res.end();
43
28
 
44
- const filename = options.filename
29
+ const filename = `${braid_blob.db_folder}/${encode_filename(options.key)}`
45
30
 
46
31
  if (req.method === 'GET') {
47
32
  // Handle GET request for binary files
@@ -150,4 +135,14 @@ braid_blob.serve = async (req, res, options = {}) => {
150
135
  }
151
136
  }
152
137
 
138
+ function encode_filename(filename) {
139
+ // Swap all "!" and "/" characters
140
+ let swapped = filename.replace(/[!/]/g, (match) => (match === "!" ? "/" : "!"))
141
+
142
+ // Encode the filename using encodeURIComponent()
143
+ let encoded = encodeURIComponent(swapped)
144
+
145
+ return encoded
146
+ }
147
+
153
148
  module.exports = braid_blob
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-blob",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Library for collaborative blobs over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-blob",