braidfs 0.0.53 → 0.0.55

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 (3) hide show
  1. package/README.md +5 -15
  2. package/index.js +47 -9
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -14,10 +14,10 @@ npm install -g braidfs
14
14
 
15
15
  ## Usage
16
16
 
17
- To start the braidfs server:
17
+ To start the braidfs daemon:
18
18
 
19
19
  ```
20
- braidfs serve
20
+ braidfs run
21
21
  ```
22
22
 
23
23
  To sync a URL:
@@ -37,29 +37,18 @@ To unsync a URL:
37
37
  braidfs unsync <url>
38
38
  ```
39
39
 
40
- ## Accessing the Server
41
-
42
- The braidfs server runs on `http://localhost:10000` by default and provides the following endpoints:
43
-
44
- - `/<url>`: Synchronizes the specified URL: creates a local file, and provides a Braid-HTTP interface
45
- - `/.braidfs/config`: Displays the current configuration
46
- - `/.braidfs/errors`: Shows the error log
47
-
48
- Accessing a URL through the proxy (e.g., `http://localhost:10000/https://example.com/file.txt`) will automatically add it to the set of synced URLs, similar to using `braidfs sync <url>`.
49
-
50
40
  ## Configuration
51
41
 
52
42
  braidfs looks for a configuration file at `~/http/.braidfs/config`. You can set the following options:
53
43
 
54
- - `port`: The port number for the proxy server (default: 10000)
55
44
  - `sync`: An object where the keys are URLs to sync, and the values are simply "true"
56
45
  - `domains`: An object for setting domain-specific configurations, including authentication headers
46
+ - `port`: The port number for the internal daemon (default: 45678)
57
47
 
58
48
  Example `config.json`:
59
49
 
60
50
  ```json
61
51
  {
62
- "port": 10000,
63
52
  "sync": {
64
53
  "https://example.com/document1.txt": true,
65
54
  "https://example.com/document2.txt": true
@@ -70,7 +59,8 @@ Example `config.json`:
70
59
  "Cookie": "secret_pass"
71
60
  }
72
61
  }
73
- }
62
+ },
63
+ "port": 45678
74
64
  }
75
65
  ```
76
66
 
package/index.js CHANGED
@@ -28,9 +28,9 @@ if (require('fs').existsSync(proxy_base)) {
28
28
  }
29
29
  } else {
30
30
  config = {
31
- port: 10000,
32
31
  sync: {},
33
32
  domains: { 'example.com': { auth_headers: { Cookie: "secret_pass" } } },
33
+ port: 45678,
34
34
  scan_interval_ms: 1000 * 20,
35
35
  }
36
36
  require('fs').mkdirSync(braidfs_config_dir, { recursive: true })
@@ -43,10 +43,10 @@ require('fs').mkdirSync(temp_folder, { recursive: true })
43
43
 
44
44
  // process command line args
45
45
  let to_run_in_background = process.platform === 'darwin' ? `
46
- To run server in background:
47
- launchctl submit -l org.braid.braidfs -- braidfs serve` : ''
46
+ To run daemon in background:
47
+ launchctl submit -l org.braid.braidfs -- braidfs run` : ''
48
48
  let argv = process.argv.slice(2)
49
- if (argv.length === 1 && argv[0] === 'serve') {
49
+ if (argv.length === 1 && argv[0].match(/^(run|serve)$/)) {
50
50
  return main()
51
51
  } else if (argv.length && argv.length % 2 == 0 && argv.every((x, i) => i % 2 != 0 || x.match(/^(sync|unsync)$/))) {
52
52
  let operations = []
@@ -71,13 +71,13 @@ if (argv.length === 1 && argv[0] === 'serve') {
71
71
  }).then(() => console.log(`${operation}ed: ${url}`))
72
72
  )).then(() => console.log('All operations completed successfully.'))
73
73
  .catch(() => {
74
- return console.log(`The braidfs server does not appear to be running.
74
+ return console.log(`The braidfs daemon does not appear to be running.
75
75
  You can run it with:
76
- braidfs serve${to_run_in_background}`)
76
+ braidfs run${to_run_in_background}`)
77
77
  })
78
78
  } else {
79
79
  return console.log(`Usage:
80
- braidfs serve
80
+ braidfs run
81
81
  braidfs sync <URL>
82
82
  braidfs unsync <URL>${to_run_in_background}`)
83
83
  }
@@ -106,7 +106,7 @@ async function main() {
106
106
 
107
107
  braid_text.serve(req, res, { key: normalize_url(url) })
108
108
  }).listen(config.port, () => {
109
- console.log(`server started on port ${config.port}`)
109
+ console.log(`daemon started on port ${config.port}`)
110
110
  if (!config.allow_remote_access) console.log('!! only accessible from localhost !!')
111
111
 
112
112
  proxy_url('.braidfs/config').then(() => {
@@ -159,7 +159,7 @@ async function main() {
159
159
  watch_files()
160
160
  setTimeout(scan_files, 1200)
161
161
  }).on('error', e => {
162
- if (e.code === 'EADDRINUSE') return console.log(`server already running on port ${config.port}`)
162
+ if (e.code === 'EADDRINUSE') return console.log(`port ${config.port} is in use`)
163
163
  throw e
164
164
  })
165
165
  }
@@ -582,6 +582,44 @@ async function proxy_url(url) {
582
582
  }).catch(e => (e?.name !== "AbortError") && crash(e))
583
583
  }
584
584
 
585
+ // send them stuff we have but they don't
586
+ if (is_external_link) send_new_stuff()
587
+ async function send_new_stuff() {
588
+ if (!start_something()) return
589
+ try {
590
+ let a = new AbortController()
591
+ aborts.add(a)
592
+ var r = await braid_fetch(url, {
593
+ signal: a.signal,
594
+ method: "HEAD",
595
+ headers: {
596
+ Accept: 'text/plain',
597
+ ...config.domains?.[(new URL(url)).hostname]?.auth_headers,
598
+ },
599
+ retry: true
600
+ })
601
+ aborts.delete(a)
602
+
603
+ if (r.headers.get('editable') === 'false') {
604
+ console.log('do not send updates for read-only file: ' + url)
605
+ return
606
+ }
607
+
608
+ var chain = Promise.resolve()
609
+ braid_text.get(url, braid_text_get_options = {
610
+ parents: r.headers.get('version') && JSON.parse(`[${r.headers.get('version')}]`),
611
+ merge_type: 'dt',
612
+ peer,
613
+ subscribe: async (u) => {
614
+ if (u.version.length) chain = chain.then(() => send_out({...u, peer}))
615
+ },
616
+ })
617
+ } catch (e) {
618
+ if (e?.name !== "AbortError") crash(e)
619
+ }
620
+ finish_something()
621
+ }
622
+
585
623
  // for config and errors file, listen for web changes
586
624
  if (!is_external_link) braid_text.get(url, braid_text_get_options = {
587
625
  merge_type: 'dt',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.53",
3
+ "version": "0.0.55",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",