braidfs 0.0.15 → 0.0.16

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/README.md CHANGED
@@ -1,67 +1,60 @@
1
1
  # braidfs
2
- braid technology synchronizing files and webpages
2
+ Proxy braid collaborative text pages as editable files on disk.
3
3
 
4
- ## Features
5
-
6
- - Proxies web resources as collaborative text using the Braid protocol
7
- - Caches proxied content locally
8
- - Monitors local files for changes and syncs them back to the origin
9
- - Supports pinning specific URLs
10
4
 
11
5
  ## Installation
12
6
 
13
- Clone the repository:
7
+ Install braidfs globally using npm:
14
8
 
15
- ```bash
16
- git clone https://github.com/braid-org/braidfs.git
17
- cd braidfs
18
9
  ```
19
-
20
- Install dependencies:
21
-
22
- ```bash
23
- npm install
10
+ npm install -g braidfs
24
11
  ```
25
12
 
26
13
  ## Usage
27
14
 
28
- Run the server:
15
+ To start braidfs, run the following command:
29
16
 
30
- ```bash
31
- node index.js [PORT] [OPTIONS]
17
+ ```
18
+ braidfs [port] [-pin <url>] [-pin index <url>]
32
19
  ```
33
20
 
34
- ### Options
35
-
36
- - `PORT`: The port number to run the server on (default: 10000)
37
- - `-pin URL`: Pin a specific URL to be proxied
38
- - `-pin index URL`: Pin an index URL that contains a list of URLs to be proxied
39
- - `COOKIE`: Set a cookie to be used in requests (optional)
21
+ - `[port]`: Optional. Specify the port number (default is 10000).
22
+ - `-pin <url>`: Pin a specific URL for synchronization.
23
+ - `-pin index <url>`: Pin an index URL that contains a list of URLs to synchronize.
40
24
 
41
25
  Example:
42
26
 
43
- ```bash
44
- node index.js 60000 -pin https://example.com/resource -pin index https://example.com/index.json mycookie=value
27
+ ```
28
+ braidfs 8080 -pin https://example.com/document.txt -pin index https://example.com/index.json
45
29
  ```
46
30
 
47
- This will run a server on port 60000, pin the specified URL, use the index URL to proxy multiple resources, and set a cookie for requests.
31
+ ## Configuration
48
32
 
49
- ## Accessing the Proxy
33
+ braidfs looks for a configuration file at `~/.braidfs/config.json`. You can set the following options:
50
34
 
51
- The proxy only allows connections from localhost for security reasons.
35
+ - `port`: The port number for the proxy server.
36
+ - `pin_urls`: An array of URLs to pin for synchronization.
37
+ - `pindex_urls`: An array of index URLs containing lists of URLs to synchronize.
38
+ - `proxy_base`: The base directory for storing proxied files (default is `~/http`).
52
39
 
53
- - `/pages`: Shows all the proxied URLs
54
- - `/URL`: Proxies the specified URL as Braid text and creates a file in `proxy_base/URL`
40
+ Example `config.json`:
55
41
 
56
- ## File Structure
42
+ ```json
43
+ {
44
+ "port": 9000,
45
+ "pin_urls": ["https://example.com/document1.txt", "https://example.com/document2.txt"],
46
+ "pindex_urls": ["https://example.com/index.json"],
47
+ "proxy_base": "/path/to/custom/proxy/directory"
48
+ }
49
+ ```
57
50
 
58
- - `braid-text-db`: Stores local cached information
59
- - `proxy_base`: Stores proxied files, which are updated when resources change and monitored for local changes
51
+ ## Accessing the Proxy
60
52
 
61
- ## Security Note
53
+ The proxy only allows connections from localhost for security reasons.
62
54
 
63
- This proxy is designed to be accessible only from localhost. Attempting to access it from any other IP address will result in a 403 Forbidden error.
55
+ - `/pages`: Shows all the proxied URLs
56
+ - `/URL`: Proxies the specified URL and creates a file in `proxy_base/URL`
64
57
 
65
- ## Known Issues
58
+ ## Security
66
59
 
67
- - The server doesn't automatically resubscribe to proxied resources when restarted, though it will keep trying to re-establish broken connections while running.
60
+ braidfs is designed to run locally and only accepts connections from localhost (127.0.0.1 or ::1) for security reasons.
package/index.js CHANGED
@@ -7,34 +7,47 @@ let braid_fetch = require('braid-http').fetch
7
7
  process.on("unhandledRejection", (x) => console.log(`unhandledRejection: ${x.stack}`))
8
8
  process.on("uncaughtException", (x) => console.log(`uncaughtException: ${x.stack}`))
9
9
 
10
- let port = 10000
11
- let cookie = null
12
- let pin_urls = []
13
- let pindex_urls = []
14
- let proxy_base = `./proxy_base`
15
- let proxy_base_support = `./proxy_base_support`
10
+ let braidfs_config_dir = require('path').join(require('os').homedir(), '.braidfs')
11
+ let braidfs_config_path = require('path').join(braidfs_config_dir, 'config.json')
12
+ braid_text.db_folder = require('path').join(braidfs_config_dir, 'braid-text-db')
13
+
14
+ let config = {
15
+ port: 10000,
16
+ pin_urls: [],
17
+ pindex_urls: [],
18
+ proxy_base: require('path').join(require('os').homedir(), 'http'),
19
+ proxy_base_last_versions: require('path').join(braidfs_config_dir, 'proxy_base_last_versions')
20
+ }
21
+
22
+ // process config file
23
+ try {
24
+ console.log(`loading config file at: ${braidfs_config_path}`)
25
+ Object.assign(config, JSON.parse(require('fs').readFileSync(braidfs_config_path, 'utf8')))
26
+ } catch (e) { console.error(`Error loading config file:`, e.message) }
16
27
 
28
+ // process command line args (override config)
17
29
  let argv = process.argv.slice(2)
18
30
  while (argv.length) {
19
31
  let a = argv.shift()
20
32
  if (a.match(/^\d+$/)) {
21
- port = parseInt(a)
33
+ config.port = parseInt(a)
22
34
  } else if (a === '-pin') {
23
35
  let b = argv.shift()
24
36
  if (b === 'index') {
25
- pindex_urls.push(argv.shift())
37
+ config.pindex_urls.push(argv.shift())
26
38
  } else {
27
- pin_urls.push(b)
39
+ config.pin_urls.push(b)
28
40
  }
29
- } else {
30
- cookie = a
31
- console.log(`cookie = ${cookie}`)
32
41
  }
33
42
  }
34
- console.log({ pin_urls, pindex_urls })
35
43
 
36
- for (let url of pin_urls) proxy_url(url)
37
- pindex_urls.forEach(async url => {
44
+ // create directories
45
+ require('fs').mkdirSync(config.proxy_base, { recursive: true })
46
+ require('fs').mkdirSync(config.proxy_base_last_versions, { recursive: true })
47
+
48
+ console.log({ pin_urls: config.pin_urls, pindex_urls: config.pindex_urls })
49
+ for (let url of config.pin_urls) proxy_url(url)
50
+ config.pindex_urls.forEach(async url => {
38
51
  let prefix = new URL(url).origin
39
52
  while (true) {
40
53
  let urls = await (await fetch(url)).json()
@@ -88,8 +101,8 @@ const server = http.createServer(async (req, res) => {
88
101
  braid_text.serve(req, res, { key: normalize_url(url) })
89
102
  });
90
103
 
91
- server.listen(port, () => {
92
- console.log(`Proxy server started on port ${port}`);
104
+ server.listen(config.port, () => {
105
+ console.log(`Proxy server started on port ${config.port}`);
93
106
  console.log('This proxy is only accessible from localhost');
94
107
  });
95
108
 
@@ -141,7 +154,7 @@ async function proxy_url(url) {
141
154
  console.log(`proxy_url: ${url}`)
142
155
 
143
156
  let path = url.replace(/^https?:\/\//, '')
144
- let fullpath = require("path").join(proxy_base, path)
157
+ let fullpath = require("path").join(config.proxy_base, path)
145
158
 
146
159
  // if we're accessing /blah/index, it will be normalized to /blah,
147
160
  // but we still want to create a directory out of blah in this case
@@ -149,8 +162,6 @@ async function proxy_url(url) {
149
162
 
150
163
  await ensure_path(require("path").dirname(fullpath))
151
164
 
152
- await require("fs").promises.mkdir(proxy_base_support, { recursive: true })
153
-
154
165
  async function get_fullpath() {
155
166
  let p = fullpath
156
167
  while (await is_dir(p)) p = require("path").join(p, 'index')
@@ -180,7 +191,7 @@ async function proxy_url(url) {
180
191
  headers: {
181
192
  "Merge-Type": "dt",
182
193
  "Content-Type": 'text/plain',
183
- ...(cookie ? { "Cookie": cookie } : {}),
194
+ ...config?.domains?.[(new URL(url)).hostname]?.auth_headers,
184
195
  },
185
196
  method: "PUT",
186
197
  retry: true,
@@ -195,7 +206,7 @@ async function proxy_url(url) {
195
206
 
196
207
  if (file_last_version === null) {
197
208
  try {
198
- file_last_version = JSON.parse(await require('fs').promises.readFile(require('path').join(proxy_base_support, braid_text.encode_filename(url)), { encoding: 'utf8' }))
209
+ file_last_version = JSON.parse(await require('fs').promises.readFile(require('path').join(config.proxy_base_last_versions, braid_text.encode_filename(url)), { encoding: 'utf8' }))
199
210
  file_last_text = (await braid_text.get(url, { version: file_last_version })).body
200
211
  file_needs_writing = !v_eq(file_last_version, (await braid_text.get(url, {})).version)
201
212
  } catch (e) {
@@ -227,7 +238,7 @@ async function proxy_url(url) {
227
238
 
228
239
  await braid_text.put(url, { version, parents, patches, peer })
229
240
 
230
- await require('fs').promises.writeFile(require('path').join(proxy_base_support, braid_text.encode_filename(url)), JSON.stringify(file_last_version))
241
+ await require('fs').promises.writeFile(require('path').join(config.proxy_base_last_versions, braid_text.encode_filename(url)), JSON.stringify(file_last_version))
231
242
  }
232
243
  }
233
244
  if (file_needs_writing) {
@@ -240,7 +251,7 @@ async function proxy_url(url) {
240
251
  file_last_version = version
241
252
  file_last_text = body
242
253
  await require('fs').promises.writeFile(await get_fullpath(), file_last_text)
243
- await require('fs').promises.writeFile(require('path').join(proxy_base_support, braid_text.encode_filename(url)), JSON.stringify(file_last_version))
254
+ await require('fs').promises.writeFile(require('path').join(config.proxy_base_last_versions, braid_text.encode_filename(url)), JSON.stringify(file_last_version))
244
255
  }
245
256
  }
246
257
  }
@@ -275,8 +286,8 @@ async function proxy_url(url) {
275
286
 
276
287
  if (!proxy_url.chokidar) {
277
288
  proxy_url.chokidar = true
278
- require('chokidar').watch(proxy_base).on('change', (path) => {
279
- path = require('path').relative(proxy_base, path)
289
+ require('chokidar').watch(config.proxy_base).on('change', (path) => {
290
+ path = require('path').relative(config.proxy_base, path)
280
291
  console.log(`path changed: ${path}`)
281
292
 
282
293
  path = normalize_url(path)
@@ -310,7 +321,6 @@ async function proxy_url(url) {
310
321
  if (version.length == 0) return;
311
322
 
312
323
  // console.log(`local got: ${JSON.stringify({ version, parents, body, patches }, null, 4)}`)
313
- // console.log(`cookie = ${cookie}`)
314
324
 
315
325
  signal_file_needs_writing()
316
326
 
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",
7
7
  "homepage": "https://braid.org",
8
8
  "dependencies": {
9
9
  "braid-http": "^0.3.20",
10
- "braid-text": "^0.0.25",
10
+ "braid-text": "^0.0.26",
11
11
  "chokidar": "^3.6.0"
12
12
  }
13
13
  }
@@ -0,0 +1 @@
1
+ abcd