braid-blob 0.0.37 → 0.0.39

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 +80 -32
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -92,8 +92,7 @@ function create_braid_blob() {
92
92
  }
93
93
 
94
94
  braid_blob.put = async (key, body, options = {}) => {
95
- // What's the content type?
96
- var content_type = options.content_type || options.accept || get_header(options.headers, 'content-type') || get_header(options.headers, 'accept')
95
+ options = normalize_options(options)
97
96
 
98
97
  // Handle URL case - make a remote PUT request
99
98
  if (key instanceof URL) {
@@ -107,9 +106,9 @@ function create_braid_blob() {
107
106
  params.retry = () => true
108
107
  for (var x of ['headers', 'version', 'peer'])
109
108
  if (options[x] != null) params[x] = options[x]
110
- if (content_type)
109
+ if (options.content_type)
111
110
  params.headers = { ...params.headers,
112
- 'Content-Type': content_type }
111
+ 'Content-Type': options.content_type }
113
112
 
114
113
  return await braid_fetch(key.href, params)
115
114
  }
@@ -140,8 +139,8 @@ function create_braid_blob() {
140
139
 
141
140
  // Update only the fields we want to change in metadata
142
141
  var meta_updates = { event: their_e }
143
- if (content_type)
144
- meta_updates.content_type = content_type
142
+ if (options.content_type)
143
+ meta_updates.content_type = options.content_type
145
144
 
146
145
  await update_meta(key, meta_updates)
147
146
  if (options.signal?.aborted) return
@@ -150,7 +149,7 @@ function create_braid_blob() {
150
149
  // (except the peer which made the PUT request itself)
151
150
  if (braid_blob.key_to_subs[key])
152
151
  for (var [peer, sub] of braid_blob.key_to_subs[key].entries())
153
- if (peer !== options.peer)
152
+ if (!options.peer || options.peer !== peer)
154
153
  sub.sendUpdate({
155
154
  version: [meta.event],
156
155
  'Merge-Type': 'aww',
@@ -162,8 +161,7 @@ function create_braid_blob() {
162
161
  }
163
162
 
164
163
  braid_blob.get = async (key, options = {}) => {
165
- // What's the content type?
166
- var content_type = options.content_type || options.accept || get_header(options.headers, 'content-type') || get_header(options.headers, 'accept')
164
+ options = normalize_options(options)
167
165
 
168
166
  // Handle URL case - make a remote GET request
169
167
  if (key instanceof URL) {
@@ -179,9 +177,9 @@ function create_braid_blob() {
179
177
  if (options.head) params.method = 'HEAD'
180
178
  for (var x of ['headers', 'parents', 'version', 'peer'])
181
179
  if (options[x] != null) params[x] = options[x]
182
- if (content_type)
180
+ if (options.content_type)
183
181
  params.headers = { ...params.headers,
184
- 'Accept': content_type }
182
+ 'Accept': options.content_type }
185
183
 
186
184
  var res = await braid_fetch(key.href, params)
187
185
 
@@ -210,7 +208,7 @@ function create_braid_blob() {
210
208
 
211
209
  var result = {
212
210
  version: [meta.event],
213
- content_type: meta.content_type || content_type
211
+ content_type: meta.content_type || options.content_type
214
212
  }
215
213
  if (options.header_cb) await options.header_cb(result)
216
214
  if (options.signal?.aborted) return
@@ -237,7 +235,7 @@ function create_braid_blob() {
237
235
  options.my_subscribe({
238
236
  body: update.body,
239
237
  version: update.version,
240
- content_type: meta.content_type || content_type
238
+ content_type: meta.content_type || options.content_type
241
239
  })
242
240
  }
243
241
  })
@@ -271,6 +269,8 @@ function create_braid_blob() {
271
269
  }
272
270
 
273
271
  braid_blob.delete = async (key, options = {}) => {
272
+ options = normalize_options(options)
273
+
274
274
  // Handle URL case - make a remote DELETE request
275
275
  if (key instanceof URL) {
276
276
 
@@ -388,8 +388,8 @@ function create_braid_blob() {
388
388
  }
389
389
 
390
390
  braid_blob.sync = (a, b, options = {}) => {
391
- // What's the content type?
392
- var content_type = options.content_type || options.accept || get_header(options.headers, 'content-type') || get_header(options.headers, 'accept')
391
+ options = normalize_options(options)
392
+ if (!options.peer) options.peer = Math.random().toString(36).slice(2)
393
393
 
394
394
  if ((a instanceof URL) === (b instanceof URL)) {
395
395
  // Both are URLs or both are local keys
@@ -400,13 +400,15 @@ function create_braid_blob() {
400
400
  var a_ops = {
401
401
  signal: options.signal,
402
402
  headers: options.headers,
403
- content_type,
403
+ content_type: options.content_type,
404
+ peer: options.peer,
404
405
  subscribe: update => {
405
406
  braid_blob.put(b, update.body, {
406
407
  signal: options.signal,
407
408
  version: update.version,
408
409
  headers: options.headers,
409
- content_type: update.content_type
410
+ content_type: update.content_type,
411
+ peer: options.peer,
410
412
  }).then(a_first_put)
411
413
  }
412
414
  }
@@ -417,13 +419,15 @@ function create_braid_blob() {
417
419
  var b_ops = {
418
420
  signal: options.signal,
419
421
  headers: options.headers,
420
- content_type,
422
+ content_type: options.content_type,
423
+ peer: options.peer,
421
424
  subscribe: update => {
422
425
  braid_blob.put(a, update.body, {
423
426
  signal: options.signal,
424
427
  version: update.version,
425
428
  headers: options.headers,
426
- content_type: update.content_type
429
+ content_type: update.content_type,
430
+ peer: options.peer,
427
431
  }).then(b_first_put)
428
432
  }
429
433
  }
@@ -460,12 +464,7 @@ function create_braid_blob() {
460
464
 
461
465
  try {
462
466
  // Check if remote has our current version (simple fork-point check)
463
- var local_result = await braid_blob.get(a, {
464
- signal: ac.signal,
465
- head: true,
466
- headers: options.headers,
467
- content_type
468
- })
467
+ var local_result = await braid_blob.get(a, { head: true })
469
468
  var local_version = local_result ? local_result.version : null
470
469
  var server_has_our_version = false
471
470
 
@@ -476,7 +475,8 @@ function create_braid_blob() {
476
475
  dont_retry: true,
477
476
  version: local_version,
478
477
  headers: options.headers,
479
- content_type
478
+ content_type: options.content_type,
479
+ peer: options.peer,
480
480
  })
481
481
  server_has_our_version = !!r
482
482
  }
@@ -485,7 +485,8 @@ function create_braid_blob() {
485
485
  var a_ops = {
486
486
  signal: ac.signal,
487
487
  headers: options.headers,
488
- content_type,
488
+ content_type: options.content_type,
489
+ peer: options.peer,
489
490
  subscribe: async update => {
490
491
  try {
491
492
  var x = await braid_blob.put(b, update.body, {
@@ -493,7 +494,8 @@ function create_braid_blob() {
493
494
  dont_retry: true,
494
495
  version: update.version,
495
496
  headers: options.headers,
496
- content_type: update.content_type
497
+ content_type: update.content_type,
498
+ peer: options.peer,
497
499
  })
498
500
  if (x.ok) local_first_put()
499
501
  else if (x.status === 401 || x.status === 403) {
@@ -515,12 +517,14 @@ function create_braid_blob() {
515
517
  signal: ac.signal,
516
518
  dont_retry: true,
517
519
  headers: options.headers,
518
- content_type,
520
+ content_type: options.content_type,
521
+ peer: options.peer,
519
522
  subscribe: async update => {
520
523
  await braid_blob.put(a, update.body, {
521
524
  version: update.version,
522
525
  headers: options.headers,
523
- content_type: update.content_type
526
+ content_type: update.content_type,
527
+ peer: options.peer,
524
528
  })
525
529
  remote_first_put()
526
530
  },
@@ -536,8 +540,12 @@ function create_braid_blob() {
536
540
 
537
541
  // Set up both subscriptions, handling cases where one doesn't exist yet
538
542
  braid_blob.get(a, a_ops).then(x =>
539
- x || remote_first_put_promise.then(() =>
540
- braid_blob.get(a, a_ops)))
543
+ x || remote_first_put_promise.then(async () => {
544
+ // update parents, since we know remote has the version we just got from them..
545
+ var local_result = await braid_blob.get(a, { head: true })
546
+ a_ops.parents = local_result.version
547
+ braid_blob.get(a, a_ops)
548
+ }))
541
549
 
542
550
  var remote_res = await braid_blob.get(b, b_ops)
543
551
 
@@ -680,6 +688,46 @@ function create_braid_blob() {
680
688
  return headers[headerKey]
681
689
  }
682
690
 
691
+ function normalize_options(options = {}) {
692
+ if (!normalize_options.special) {
693
+ normalize_options.special = {
694
+ version: 'version',
695
+ parents: 'parents',
696
+ 'content-type': 'content_type',
697
+ accept: 'content_type',
698
+ peer: 'peer',
699
+ }
700
+ }
701
+
702
+ var normalized = {}
703
+ Object.assign(normalized, options)
704
+
705
+ // Normalize top-level accept to content_type
706
+ if (options.accept) {
707
+ normalized.content_type = options.accept
708
+ delete normalized.accept
709
+ }
710
+
711
+ if (options.headers) {
712
+ normalized.headers = {}
713
+ for (var [k, v] of (options.headers instanceof Headers ?
714
+ options.headers.entries() :
715
+ Object.entries(options.headers))) {
716
+ var s = normalize_options.special[k]
717
+ if (s) normalized[s] = v
718
+ else normalized.headers[k] = v
719
+ }
720
+ }
721
+
722
+ // Normalize version/parents strings to arrays
723
+ if (typeof normalized.version === 'string')
724
+ normalized.version = JSON.parse('[' + normalized.version + ']')
725
+ if (typeof normalized.parents === 'string')
726
+ normalized.parents = JSON.parse('[' + normalized.parents + ']')
727
+
728
+ return normalized
729
+ }
730
+
683
731
  braid_blob.create_braid_blob = create_braid_blob
684
732
 
685
733
  return braid_blob
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-blob",
3
- "version": "0.0.37",
3
+ "version": "0.0.39",
4
4
  "description": "Library for collaborative blobs over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-blob",