braidfs 0.0.21 → 0.0.23

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 +32 -17
  2. package/package.json +7 -2
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  let http = require('http');
4
- let { diff_main } = require('./diff.js')
4
+ let { diff_main } = require(require('path').join(__dirname, "diff.js"))
5
5
  let braid_text = require("braid-text");
6
6
  let braid_fetch = require('braid-http').fetch
7
7
 
@@ -56,6 +56,9 @@ braid_text.db_folder = config.braid_text_db
56
56
  require('fs').mkdirSync(config.proxy_base, { recursive: true })
57
57
  require('fs').mkdirSync(config.proxy_base_last_versions, { recursive: true })
58
58
 
59
+ let host_to_protocol = {}
60
+ let path_to_func = {}
61
+
59
62
  console.log({ sync_urls: config.sync_urls, sync_index_urls: config.sync_index_urls })
60
63
  for (let url of config.sync_urls) proxy_url(url)
61
64
  config.sync_index_urls.forEach(async url => {
@@ -71,6 +74,27 @@ braid_text.list().then(x => {
71
74
  for (let xx of x) proxy_url(xx)
72
75
  })
73
76
 
77
+ require('chokidar').watch(config.proxy_base).
78
+ on('change', (path) => {
79
+ path = require('path').relative(config.proxy_base, path)
80
+ console.log(`path changed: ${path}`)
81
+
82
+ path = normalize_url(path)
83
+ // console.log(`normalized path: ${path}`)
84
+
85
+ path_to_func[path]()
86
+ }).
87
+ on('add', async (path) => {
88
+ path = require('path').relative(config.proxy_base, path)
89
+ console.log(`path added: ${path}`)
90
+
91
+ let url = null
92
+ if (path.startsWith('localhost/')) url = path.replace(/^localhost\//, '')
93
+ else url = host_to_protocol[path.split('/')[0]] + '//' + path
94
+
95
+ proxy_url(url)
96
+ })
97
+
74
98
  const server = http.createServer(async (req, res) => {
75
99
  console.log(`${req.method} ${req.url}`);
76
100
 
@@ -93,7 +117,7 @@ const server = http.createServer(async (req, res) => {
93
117
 
94
118
  if (req.url.endsWith("?editor")) {
95
119
  res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
96
- require("fs").createReadStream("./editor.html").pipe(res)
120
+ require("fs").createReadStream(require('path').join(__dirname, "editor.html")).pipe(res)
97
121
  return
98
122
  }
99
123
 
@@ -175,6 +199,11 @@ async function proxy_url(url) {
175
199
  let path = is_external_link ? url.replace(/^https?:\/\//, '') : `localhost/${url}`
176
200
  let fullpath = require("path").join(config.proxy_base, path)
177
201
 
202
+ if (is_external_link) {
203
+ let u = new URL(url)
204
+ host_to_protocol[u.host] = u.protocol
205
+ }
206
+
178
207
  // if we're accessing /blah/index, it will be normalized to /blah,
179
208
  // but we still want to create a directory out of blah in this case
180
209
  if (wasnt_normal && !(await is_dir(fullpath))) await ensure_path(fullpath)
@@ -300,21 +329,7 @@ async function proxy_url(url) {
300
329
  })
301
330
  })
302
331
 
303
- if (!proxy_url.path_to_func) proxy_url.path_to_func = {}
304
- proxy_url.path_to_func[path] = signal_file_needs_reading
305
-
306
- if (!proxy_url.chokidar) {
307
- proxy_url.chokidar = true
308
- require('chokidar').watch(config.proxy_base).on('change', (path) => {
309
- path = require('path').relative(config.proxy_base, path)
310
- console.log(`path changed: ${path}`)
311
-
312
- path = normalize_url(path)
313
- // console.log(`normalized path: ${path}`)
314
-
315
- proxy_url.path_to_func[path]()
316
- });
317
- }
332
+ path_to_func[path] = signal_file_needs_reading
318
333
 
319
334
  // try a HEAD without subscribe to get the version
320
335
  let parents = null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",
@@ -12,5 +12,10 @@
12
12
  },
13
13
  "bin": {
14
14
  "braidfs": "./index.js"
15
- }
15
+ },
16
+ "files": [
17
+ "index.js",
18
+ "editor.html",
19
+ "diff.js"
20
+ ]
16
21
  }