braidfs 0.0.133 → 0.0.135

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 +72 -50
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -141,6 +141,10 @@ You can run it with:
141
141
  async function main() {
142
142
  process.on("unhandledRejection", (x) => console.log(`unhandledRejection: ${x.stack}`))
143
143
  process.on("uncaughtException", (x) => console.log(`uncaughtException: ${x.stack}`))
144
+
145
+ await braid_text.db_folder_init()
146
+ await braid_blob.init()
147
+
144
148
  require('http').createServer(async (req, res) => {
145
149
  try {
146
150
  // console.log(`${req.method} ${req.url}`)
@@ -216,45 +220,44 @@ async function main() {
216
220
  console.log(`daemon started on port ${config.port}`)
217
221
  console.log('!! only accessible from localhost !!')
218
222
 
219
- sync_url('.braidfs/config').then(() => {
220
- braid_text.get('.braidfs/config', {
221
- subscribe: async update => {
222
- let prev = config
223
+ sync_url('.braidfs/config')
224
+ braid_text.get('.braidfs/config', {
225
+ subscribe: async update => {
226
+ let prev = config
223
227
 
224
- let x = await braid_text.get('.braidfs/config')
225
- try {
226
- config = JSON.parse(x)
227
-
228
- // did anything get deleted?
229
- var old_syncs = Object.entries(prev.sync).filter(x => x[1]).map(x => normalize_url(x[0]).replace(/^https?:\/\//, ''))
230
- var new_syncs = new Set(Object.entries(config.sync).filter(x => x[1]).map(x => normalize_url(x[0]).replace(/^https?:\/\//, '')))
231
- for (let url of old_syncs.filter(x => !new_syncs.has(x)))
232
- unsync_url(url)
233
-
234
- // sync all the new stuff
235
- for (let x of Object.entries(config.sync)) if (x[1]) sync_url(x[0])
236
-
237
- // if any auth stuff has changed,
238
- // have the appropriate connections reconnect
239
- let changed = new Set()
240
- // any old domains no longer exist?
241
- for (let domain of Object.keys(prev.cookies ?? {}))
242
- if (!config.cookies?.[domain]) changed.add(domain)
243
- // any new domains not like the old?
244
- for (let [domain, v] of Object.entries(config.cookies ?? {}))
245
- if (!prev.cookies?.[domain]
246
- || JSON.stringify(prev.cookies[domain]) !== JSON.stringify(v))
247
- changed.add(domain)
248
- // ok, have every domain which has changed reconnect
249
- for (let [path, x] of Object.entries(sync_url.cache))
250
- if (changed.has(path.split(/\//)[0].split(/:/)[0]))
251
- (await x).reconnect?.()
252
- } catch (e) {
253
- if (x !== '') console.log(`warning: config file is currently invalid.`)
254
- return
255
- }
228
+ let x = await braid_text.get('.braidfs/config')
229
+ try {
230
+ config = JSON.parse(x)
231
+
232
+ // did anything get deleted?
233
+ var old_syncs = Object.entries(prev.sync).filter(x => x[1]).map(x => normalize_url(x[0]).replace(/^https?:\/\//, ''))
234
+ var new_syncs = new Set(Object.entries(config.sync).filter(x => x[1]).map(x => normalize_url(x[0]).replace(/^https?:\/\//, '')))
235
+ for (let url of old_syncs.filter(x => !new_syncs.has(x)))
236
+ unsync_url(url)
237
+
238
+ // sync all the new stuff
239
+ for (let x of Object.entries(config.sync)) if (x[1]) sync_url(x[0])
240
+
241
+ // if any auth stuff has changed,
242
+ // have the appropriate connections reconnect
243
+ let changed = new Set()
244
+ // any old domains no longer exist?
245
+ for (let domain of Object.keys(prev.cookies ?? {}))
246
+ if (!config.cookies?.[domain]) changed.add(domain)
247
+ // any new domains not like the old?
248
+ for (let [domain, v] of Object.entries(config.cookies ?? {}))
249
+ if (!prev.cookies?.[domain]
250
+ || JSON.stringify(prev.cookies[domain]) !== JSON.stringify(v))
251
+ changed.add(domain)
252
+ // ok, have every domain which has changed reconnect
253
+ for (let [path, x] of Object.entries(sync_url.cache))
254
+ if (changed.has(path.split(/\//)[0].split(/:/)[0]))
255
+ (await x).reconnect?.()
256
+ } catch (e) {
257
+ if (x !== '') console.log(`warning: config file is currently invalid.`)
258
+ return
256
259
  }
257
- })
260
+ }
258
261
  })
259
262
  sync_url('.braidfs/errors')
260
263
 
@@ -413,15 +416,12 @@ function unsync_url(url) {
413
416
  delete unsync_url.cache[url]
414
417
  }
415
418
 
416
- async function sync_url(url) {
419
+ function sync_url(url) {
417
420
  // normalize url by removing any trailing /index/index/
418
421
  var normalized_url = normalize_url(url),
419
422
  wasnt_normal = normalized_url != url
420
423
  url = normalized_url
421
424
 
422
- await braid_text.db_folder_init()
423
- await braid_blob.init()
424
-
425
425
  var is_external_link = url.match(/^https?:\/\//),
426
426
  path = is_external_link ? url.replace(/^https?:\/\//, '') : url,
427
427
  fullpath = `${sync_base}/${path}`,
@@ -480,7 +480,6 @@ async function sync_url(url) {
480
480
  } else throw new Error(`unknown merge-type: ${self.merge_type}`)
481
481
  })()
482
482
  }
483
- return
484
483
 
485
484
  async function detect_merge_type() {
486
485
  // special case for .braidfs/config and .braidfs/error
@@ -1157,24 +1156,41 @@ function ReconnectRateLimiter(get_wait_time) {
1157
1156
  async function fetch_http2(url, options = {}) {
1158
1157
  if (!fetch_http2.sessions) {
1159
1158
  fetch_http2.sessions = new Map()
1160
- process.on("exit", () => fetch_http2.sessions.forEach(s => s.close()))
1159
+ process.on("exit", () => fetch_http2.sessions.forEach(s => s.session.close()))
1161
1160
  }
1162
1161
 
1163
1162
  var u = new URL(url)
1164
1163
  if (u.protocol !== "https:") return fetch(url, options)
1165
1164
 
1166
1165
  try {
1167
- var session = fetch_http2.sessions.get(u.origin)
1168
- if (!session || session.closed) {
1169
- session = require("http2").connect(u.origin, {
1166
+ var sessionInfo = fetch_http2.sessions.get(u.origin)
1167
+ if (!sessionInfo || sessionInfo.session.closed) {
1168
+ var session = require("http2").connect(u.origin, {
1170
1169
  rejectUnauthorized: options.rejectUnauthorized !== false,
1171
1170
  })
1172
- session.on("error", () => fetch_http2.sessions.delete(u.origin))
1173
- session.on("close", () => fetch_http2.sessions.delete(u.origin))
1174
- fetch_http2.sessions.set(u.origin, session)
1171
+ sessionInfo = { session, pendingRejects: new Set() }
1172
+
1173
+ session.on("error", (e) => {
1174
+ for (var f of sessionInfo.pendingRejects) f(e)
1175
+ fetch_http2.sessions.delete(u.origin)
1176
+ })
1177
+ session.on("close", () => {
1178
+ var e = new Error('Session closed')
1179
+ for (var f of sessionInfo.pendingRejects) f(e)
1180
+ fetch_http2.sessions.delete(u.origin)
1181
+ })
1182
+ fetch_http2.sessions.set(u.origin, sessionInfo)
1175
1183
  }
1176
1184
 
1185
+ var session = sessionInfo.session
1186
+
1177
1187
  return await new Promise((resolve, reject) => {
1188
+ sessionInfo.pendingRejects.add(reject)
1189
+
1190
+ var responseTimeout = setTimeout(() => {
1191
+ stream.destroy(new Error('Response timeout'))
1192
+ }, options.responseTimeout || 10000)
1193
+
1178
1194
  var stream = session.request({
1179
1195
  ":method": options.method || "GET",
1180
1196
  ":path": u.pathname + u.search,
@@ -1188,6 +1204,8 @@ async function fetch_http2(url, options = {}) {
1188
1204
  { once: true })
1189
1205
 
1190
1206
  stream.on("response", headers => {
1207
+ clearTimeout(responseTimeout)
1208
+ sessionInfo.pendingRejects.delete(reject)
1191
1209
  var status = +headers[":status"]
1192
1210
  resolve({
1193
1211
  ok: status >= 200 && status < 300,
@@ -1226,7 +1244,11 @@ async function fetch_http2(url, options = {}) {
1226
1244
  })
1227
1245
  })
1228
1246
 
1229
- stream.on("error", reject)
1247
+ stream.on("error", (err) => {
1248
+ clearTimeout(responseTimeout)
1249
+ sessionInfo.pendingRejects.delete(reject)
1250
+ reject(err)
1251
+ })
1230
1252
 
1231
1253
  var body = options.body
1232
1254
  if (!body) return stream.end()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.133",
3
+ "version": "0.0.135",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",