braid-blob 0.0.37 → 0.0.38

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 +75 -25
  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
  }
@@ -464,7 +468,8 @@ function create_braid_blob() {
464
468
  signal: ac.signal,
465
469
  head: true,
466
470
  headers: options.headers,
467
- content_type
471
+ content_type: options.content_type,
472
+ peer: options.peer,
468
473
  })
469
474
  var local_version = local_result ? local_result.version : null
470
475
  var server_has_our_version = false
@@ -476,7 +481,8 @@ function create_braid_blob() {
476
481
  dont_retry: true,
477
482
  version: local_version,
478
483
  headers: options.headers,
479
- content_type
484
+ content_type: options.content_type,
485
+ peer: options.peer,
480
486
  })
481
487
  server_has_our_version = !!r
482
488
  }
@@ -485,7 +491,8 @@ function create_braid_blob() {
485
491
  var a_ops = {
486
492
  signal: ac.signal,
487
493
  headers: options.headers,
488
- content_type,
494
+ content_type: options.content_type,
495
+ peer: options.peer,
489
496
  subscribe: async update => {
490
497
  try {
491
498
  var x = await braid_blob.put(b, update.body, {
@@ -493,7 +500,8 @@ function create_braid_blob() {
493
500
  dont_retry: true,
494
501
  version: update.version,
495
502
  headers: options.headers,
496
- content_type: update.content_type
503
+ content_type: update.content_type,
504
+ peer: options.peer,
497
505
  })
498
506
  if (x.ok) local_first_put()
499
507
  else if (x.status === 401 || x.status === 403) {
@@ -515,12 +523,14 @@ function create_braid_blob() {
515
523
  signal: ac.signal,
516
524
  dont_retry: true,
517
525
  headers: options.headers,
518
- content_type,
526
+ content_type: options.content_type,
527
+ peer: options.peer,
519
528
  subscribe: async update => {
520
529
  await braid_blob.put(a, update.body, {
521
530
  version: update.version,
522
531
  headers: options.headers,
523
- content_type: update.content_type
532
+ content_type: update.content_type,
533
+ peer: options.peer,
524
534
  })
525
535
  remote_first_put()
526
536
  },
@@ -680,6 +690,46 @@ function create_braid_blob() {
680
690
  return headers[headerKey]
681
691
  }
682
692
 
693
+ function normalize_options(options = {}) {
694
+ if (!normalize_options.special) {
695
+ normalize_options.special = {
696
+ version: 'version',
697
+ parents: 'parents',
698
+ 'content-type': 'content_type',
699
+ accept: 'content_type',
700
+ peer: 'peer',
701
+ }
702
+ }
703
+
704
+ var normalized = {}
705
+ Object.assign(normalized, options)
706
+
707
+ // Normalize top-level accept to content_type
708
+ if (options.accept) {
709
+ normalized.content_type = options.accept
710
+ delete normalized.accept
711
+ }
712
+
713
+ if (options.headers) {
714
+ normalized.headers = {}
715
+ for (var [k, v] of (options.headers instanceof Headers ?
716
+ options.headers.entries() :
717
+ Object.entries(options.headers))) {
718
+ var s = normalize_options.special[k]
719
+ if (s) normalized[s] = v
720
+ else normalized.headers[k] = v
721
+ }
722
+ }
723
+
724
+ // Normalize version/parents strings to arrays
725
+ if (typeof normalized.version === 'string')
726
+ normalized.version = JSON.parse('[' + normalized.version + ']')
727
+ if (typeof normalized.parents === 'string')
728
+ normalized.parents = JSON.parse('[' + normalized.parents + ']')
729
+
730
+ return normalized
731
+ }
732
+
683
733
  braid_blob.create_braid_blob = create_braid_blob
684
734
 
685
735
  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.38",
4
4
  "description": "Library for collaborative blobs over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-blob",