braid-blob 0.0.34 → 0.0.36

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/index.js +35 -14
  2. package/package.json +2 -3
  3. package/test/tests.js +64 -2
package/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  var {http_server: braidify, fetch: braid_fetch} = require('braid-http'),
2
- {url_file_db} = require('url-file-db'),
3
2
  fs = require('fs'),
4
3
  path = require('path')
5
4
 
@@ -11,7 +10,7 @@ function create_braid_blob() {
11
10
  meta_cache: {},
12
11
  key_to_subs: {},
13
12
  peer: null, // will be auto-generated if not set by the user
14
- db: null // url-file-db instance
13
+ db: null // object with read/write/delete methods
15
14
  }
16
15
 
17
16
  braid_blob.init = async () => {
@@ -24,15 +23,36 @@ function create_braid_blob() {
24
23
  // Ensure our meta folder exists
25
24
  await fs.promises.mkdir(braid_blob.meta_folder, { recursive: true })
26
25
 
27
- // Create a fake meta folder for url-file-db (we manage our own meta)
28
- var fake_meta_folder = braid_blob.meta_folder + '-fake'
29
- await fs.promises.mkdir(fake_meta_folder, { recursive: true })
30
-
31
- // Create url-file-db instance (with fake meta folder - we manage our own)
32
- braid_blob.db = await url_file_db.create(
33
- braid_blob.db_folder,
34
- fake_meta_folder
35
- )
26
+ // Set up db - either use provided object or create file-based storage
27
+ if (typeof braid_blob.db_folder === 'string') {
28
+ await fs.promises.mkdir(braid_blob.db_folder, { recursive: true })
29
+ braid_blob.db = {
30
+ read: async (key) => {
31
+ var file_path = path.join(braid_blob.db_folder, encode_filename(key))
32
+ try {
33
+ return await fs.promises.readFile(file_path)
34
+ } catch (e) {
35
+ if (e.code === 'ENOENT') return null
36
+ throw e
37
+ }
38
+ },
39
+ write: async (key, data) => {
40
+ var file_path = path.join(braid_blob.db_folder, encode_filename(key))
41
+ await fs.promises.writeFile(file_path, data)
42
+ },
43
+ delete: async (key) => {
44
+ var file_path = path.join(braid_blob.db_folder, encode_filename(key))
45
+ try {
46
+ await fs.promises.unlink(file_path)
47
+ } catch (e) {
48
+ if (e.code !== 'ENOENT') throw e
49
+ }
50
+ }
51
+ }
52
+ } else {
53
+ // db_folder is already an object with read/write/delete
54
+ braid_blob.db = braid_blob.db_folder
55
+ }
36
56
 
37
57
  // establish a peer id if not already set
38
58
  if (!braid_blob.peer)
@@ -181,7 +201,7 @@ function create_braid_blob() {
181
201
  throw new Error('unkown version: ' + options.version)
182
202
  if (options.parents && options.parents.length && compare_events(options.parents[0], meta.event) > 0)
183
203
  throw new Error('unkown version: ' + options.parents)
184
- if (options.head) return
204
+ if (options.head) return result
185
205
 
186
206
  if (options.subscribe) {
187
207
  var subscribe_chain = Promise.resolve()
@@ -264,7 +284,7 @@ function create_braid_blob() {
264
284
 
265
285
  if (!options.key) {
266
286
  var url = new URL(req.url, 'http://localhost')
267
- options.key = url_file_db.get_canonical_path(url.pathname)
287
+ options.key = url.pathname
268
288
  }
269
289
 
270
290
  braidify(req, res)
@@ -412,7 +432,8 @@ function create_braid_blob() {
412
432
  try {
413
433
  // Check if remote has our current version (simple fork-point check)
414
434
  var local_result = await braid_blob.get(a, {
415
- signal: ac.signal
435
+ signal: ac.signal,
436
+ head: true
416
437
  })
417
438
  var local_version = local_result ? local_result.version : null
418
439
  var server_has_our_version = false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-blob",
3
- "version": "0.0.34",
3
+ "version": "0.0.36",
4
4
  "description": "Library for collaborative blobs over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-blob",
@@ -10,7 +10,6 @@
10
10
  "test:browser": "node test/test.js --browser"
11
11
  },
12
12
  "dependencies": {
13
- "braid-http": "~1.3.83",
14
- "url-file-db": "^0.0.25"
13
+ "braid-http": "~1.3.83"
15
14
  }
16
15
  }
package/test/tests.js CHANGED
@@ -1199,6 +1199,68 @@ runTest(
1199
1199
  'no disconnects'
1200
1200
  )
1201
1201
 
1202
+ runTest(
1203
+ "test sync connect does not read file body for version check",
1204
+ async () => {
1205
+ var local_key = '/test-sync-no-read-' + Math.random().toString(36).slice(2)
1206
+ var remote_key = 'test-sync-no-read-remote-' + Math.random().toString(36).slice(2)
1207
+
1208
+ // Put something on remote with SAME version as local, so no data needs to flow
1209
+ var put_result = await braid_fetch(`/${remote_key}`, {
1210
+ method: 'PUT',
1211
+ version: ['same-version-123'],
1212
+ body: 'same content'
1213
+ })
1214
+ if (!put_result.ok) return 'PUT status: ' + put_result.status
1215
+
1216
+ var r1 = await braid_fetch(`/eval`, {
1217
+ method: 'POST',
1218
+ body: `void (async () => {
1219
+ try {
1220
+ var braid_blob = require(\`\${__dirname}/../index.js\`)
1221
+
1222
+ // Put locally with SAME version - so when sync connects, no updates need to flow
1223
+ await braid_blob.put('${local_key}', Buffer.from('same content'), { version: ['same-version-123'] })
1224
+
1225
+ // Wrap db.read to count calls for our specific key
1226
+ var read_count = 0
1227
+ var original_read = braid_blob.db.read
1228
+ braid_blob.db.read = async function(key) {
1229
+ if (key === '${local_key}') read_count++
1230
+ return original_read.call(this, key)
1231
+ }
1232
+
1233
+ var remote_url = new URL('http://localhost:' + req.socket.localPort + '/${remote_key}')
1234
+
1235
+ // Create an AbortController to stop the sync
1236
+ var ac = new AbortController()
1237
+
1238
+ // Start sync - since both have same version, no updates should flow
1239
+ braid_blob.sync('${local_key}', remote_url, { signal: ac.signal })
1240
+
1241
+ // Wait for sync to establish connection
1242
+ await new Promise(done => setTimeout(done, 300))
1243
+
1244
+ // Stop sync
1245
+ ac.abort()
1246
+
1247
+ // Restore original read
1248
+ braid_blob.db.read = original_read
1249
+
1250
+ // db.read should not have been called since:
1251
+ // 1. Initial version check uses head:true (no body read)
1252
+ // 2. Both have same version so no updates flow
1253
+ res.end(read_count === 0 ? 'no reads' : 'reads: ' + read_count)
1254
+ } catch (e) {
1255
+ res.end('error: ' + e.message + ' ' + e.stack)
1256
+ }
1257
+ })()`
1258
+ })
1259
+ return await r1.text()
1260
+ },
1261
+ 'no reads'
1262
+ )
1263
+
1202
1264
  runTest(
1203
1265
  "test sync closed during error",
1204
1266
  async () => {
@@ -1419,7 +1481,7 @@ runTest(
1419
1481
  var result1 = await bb1.get(test_key)
1420
1482
 
1421
1483
  // Check what metadata was saved
1422
- var meta1 = bb1.db.get_meta(test_key)
1484
+ var meta1 = bb1.meta_cache[test_key]
1423
1485
  var debug_info = 'meta1: ' + JSON.stringify(meta1) + '; '
1424
1486
 
1425
1487
  // Wait a bit to ensure file system has settled
@@ -1435,7 +1497,7 @@ runTest(
1435
1497
  var result2 = await bb2.get(test_key)
1436
1498
 
1437
1499
  // Check what metadata bb2 sees
1438
- var meta2 = bb2.db.get_meta(test_key)
1500
+ var meta2 = bb2.meta_cache[test_key]
1439
1501
  debug_info += 'meta2: ' + JSON.stringify(meta2) + '; '
1440
1502
 
1441
1503
  // The version should be the same - no new event ID generated