-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
1190 lines (1024 loc) · 49.8 KB
/
Copy pathindex.js
File metadata and controls
1190 lines (1024 loc) · 49.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var {http_server: braidify, fetch: braid_fetch, free_cors} = require('braid-http')
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed')
}
function create_braid_blob() {
var braid_blob = {
db_folder: null, // defaults to './braid-blobs'
meta_folder: null, // defaults to './braid-blobs'
temp_folder: null, // defaults to './braid-blobs'
cache: {},
subscriptions_to: {},
peer: null, // will be auto-generated if not set by the user
db: null, // object with read/write/delete methods
meta_db: null, // sqlite database for meta storage
reconnect_delay_ms: 1000,
}
// Syncs a local key to a remote URL, forever, across reconnections.
braid_blob.sync = (local_key, remote_url, params = {}) => {
assert(typeof local_key === 'string', '.sync: local_key must be string')
assert(remote_url instanceof URL, '.sync: remote_url must be URL')
params = normalize_params(params)
// Set our peer ID. Will prevent echoes.
if (!params.peer) params.peer = Math.random().toString(36).slice(2)
reconnector(params.signal, (_e, count) => {
var delay = braid_blob.reconnect_delay_ms ?? Math.min(count, 3) * 1000
console.log(`disconnected from ${remote_url.href}, retrying in ${delay}ms`)
return delay
}, async (signal, handle_error) => {
if (signal.aborted) return
if (params.on_pre_connect) await params.on_pre_connect()
// Remote calls get dont_retry, because sync retries by
// reconnecting, which redoes the version check below
var local_params = {...params, signal}
var remote_params = {...local_params, dont_retry: true}
try {
// Learn the current version for local and remote. We will
// subscribe from these (as parents) below.
var local_version = (await braid_blob.get(local_key, {
...local_params, head: true}))?.version
if (signal.aborted) return
var remote_version = (await braid_blob.get(remote_url, {
...remote_params, head: true}))?.version
if (signal.aborted) return
// Sync Local -> Remote:
// - Subscribe to all local changes
// - For each, send a PUT to the remote.
await braid_blob.get(local_key, {
...local_params,
parents: remote_version,
subscribe: async update => {
try {
if (update.delete) {
var res = await braid_blob.delete(remote_url, {
...remote_params,
repr_type: update.repr_type})
if (signal.aborted) return
if (!res.ok) handle_error(new Error('failed to delete'))
} else {
var res = await braid_blob.put(remote_url, update.body, {
...remote_params,
version: update.version,
repr_type: update.repr_type})
if (signal.aborted) return
if (res.status === 401 || res.status === 403)
await params.on_unauthorized?.()
else if (!res.ok)
handle_error(new Error('failed to PUT: ' + res.status))
}
} catch (e) { handle_error(e) }
}
})
// Sync Remote -> Local:
// - Subscribe to the remote blob
// - Write each remote update to the local blob
var remote_res = await braid_blob.get(remote_url, {
...remote_params,
parents: local_version,
subscribe: async update => {
try {
if (update.delete)
await braid_blob.delete(local_key, {
...local_params,
repr_type: update.repr_type})
else
await braid_blob.put(local_key, update.body, {
...local_params,
version: update.version,
repr_type: update.repr_type})
} catch (e) { handle_error(e) }
},
on_error: e => {
params.on_disconnect?.()
handle_error(e)
}
})
params.on_res?.(remote_res)
} catch (e) { handle_error(e) }
})
}
braid_blob.serve = async (req, res, params = {}) => {
await braid_blob.init()
if (!params.key) {
var url = new URL(req.url, 'http://localhost')
params.key = url.pathname
}
free_cors(res)
braidify(req, res)
if (res.is_multiplexer) return
// Handle OPTIONS request
if (req.method === 'OPTIONS') return res.end()
// consume PUT body
var body = req.method === 'PUT' && await slurp(req)
if (req.method === 'GET' || req.method === 'HEAD') {
if (!res.hasHeader("editable")) res.setHeader("Editable", "true")
if (!req.subscribe) res.setHeader("Accept-Subscribe", "true")
res.setHeader("Merge-Type", "aww")
// Advertise range request support
if (!req.subscribe) res.setHeader("Accept-Ranges", "bytes")
// Set "no-cache". This makes etags the only way a browser can
// reuse a cache, which makes caching more accurate, which goes
// with our braidly ethos so I'm just making it default behavior
// here.
if (!req.subscribe && !res.hasHeader("cache-control"))
res.setHeader("Cache-Control", "no-cache")
// We don't do ranges over subscriptions yet
var range = req.subscribe ? null : parse_range(req.headers.range)
try {
var result = await braid_blob.get(params.key, {
peer: req.peer,
head: req.method === "HEAD",
version: req.version,
parents: req.parents,
if_none_match: etags_to_versions(req.headers['if-none-match']),
range,
if_range: etags_to_versions(req.headers['if-range']),
as_stream: req.method === 'GET' && !req.subscribe,
header_cb: (result) => {
res.setHeader((req.subscribe ? "Current-" : "") +
"Version", version_to_header(result.version))
res.setHeader("Version-Type", "wallclockish")
if (!req.subscribe && result.version?.length)
res.setHeader('ETag', version_to_etag(result.version[0]))
if (result.repr_type) {
res.setHeader('Repr-Type', result.repr_type)
if (!req.subscribe)
res.setHeader('Content-Type', result.repr_type)
}
},
before_send_cb: () => res.startSubscription(),
subscribe: req.subscribe ? (update) => {
if (update.delete) {
update.status = 404
delete update.delete
} else if (update.not_modified)
// The client already has this version, so send
// a 304 instead of the body
update = {
status: 304,
version: update.version,
ETag: version_to_etag(update.version[0]),
'Cache-Control': 'no-cache',
}
// Drop the legacy alias; sendUpdate takes repr_type
delete update.content_type
update['Merge-Type'] = 'aww'
update['Version-Type'] = 'wallclockish'
res.sendUpdate(update)
} : null
})
} catch (e) {
// Bad Request
if (e.message && e.message.startsWith('bad request')) {
res.statusCode = 400
return res.end(e.message.replace(/^bad request: /, ''))
} else throw e
}
// Not Found
if (!result) {
res.statusCode = 404
return res.end('File Not Found')
}
// Version Not Found
if (result.status === 432) {
res.statusCode = result.status
res.statusMessage = result.status_text
res.setHeader("Version-Type", "wallclockish")
if (result.version)
res.setHeader('Version', version_to_header(result.version))
if (result.parents)
res.setHeader('Parents', version_to_header(result.parents))
return res.end('')
}
// Range Not Satisfiable
if (result.status === 416) {
res.statusCode = result.status
res.statusMessage = result.status_text
res.setHeader('Content-Range',
`${range.unit} */${result.repr_length}`)
return res.end('')
}
// Not Acceptable
if (result.repr_type && req.headers.accept &&
!isAcceptable(result.repr_type, req.headers.accept)) {
res.statusCode = 406
// we opened a stream we won't send
;(result.patches?.[0].content ?? result.body)?.destroy?.()
return res.end(`Content-Type of ${result.repr_type} not in Accept: ${req.headers.accept}`)
}
// Not Modified
if (!req.subscribe && result.not_modified) {
res.statusCode = 304
return res.end()
}
// Partial Content
if (result.patches) {
res.statusCode = 206
res.statusMessage = 'Partial Content'
res.setHeader('Content-Range', `${result.patches[0].unit} ` +
`${result.patches[0].range}/${result.repr_length}`)
}
if (req.method == "HEAD") {
// A HEAD reports what a GET would send, where res.end('')
// would say zero
if (result.repr_length != null)
res.setHeader('Content-Length', result.repr_length)
return res.end('')
}
else if (!req.subscribe) {
var content = result.patches ? result.patches[0].content : result.body
if (typeof content?.pipe !== 'function') return res.end(content)
// Node falls back to chunking without an explicit length
var length = result.repr_length
if (result.patches) {
var [start, end] = result.patches[0].range.split('-').map(Number)
length = end - start + 1
}
res.setHeader('Content-Length', length)
return require('stream').pipeline(content, res, (e) => {
// No headers left to report an error in; just hang up
if (e) res.destroy(e)
})
}
else {
// If no immediate update was sent,
// get the node http code to send headers
if (!result.sent) res.write('\n\n')
}
} else if (req.method === 'PUT') {
// Handle PUT request to update binary files
var event = await braid_blob.put(params.key, body, {
version: req.version,
repr_type: req.headers['repr-type'] || req.headers['content-type'],
peer: req.peer
})
res.setHeader("Current-Version", version_to_header(event != null ? [event] : []))
res.setHeader("Version-Type", "wallclockish")
res.end('')
} else if (req.method === 'DELETE') {
await braid_blob.delete(params.key, {
repr_type: req.headers['repr-type'] || req.headers['content-type'],
peer: req.peer
})
res.end('')
}
}
braid_blob.get = async (key, params = {}) => {
params = normalize_params(params)
// A subscription starts from Parents, not Version
if (params.subscribe && params.version)
throw new Error('bad request: Version cannot be used with Subscribe')
// If the key is a URL, then fetch it from remote server!
if (key instanceof URL) {
// Do a braid-HTTP GET + Subscribe
var res = await braid_fetch(key.href, {
signal: params.signal,
subscribe: params.subscribe,
heartbeats: 120,
// If we have this... ...the request carries this:
...(params.head && {method: 'HEAD'}),
...(params.version != null && {version: params.version}),
...(params.parents != null && {parents: params.parents}),
...(params.peer != null && {peer: params.peer}),
// Retry unless the status is a final answer
...(!params.dont_retry && {retry: res =>
![304, 309, 404, 406, 432].includes(res.status)}),
headers: {
...params.headers,
// if we have this... ...the headers carry this:
...(params.repr_type && {'Accept': params.repr_type}),
...((params.version ||
params.parents) && {'Version-Type': 'wallclockish'}),
...(params.if_none_match && {'If-None-Match':
params.if_none_match.map(version_to_etag).join(', ')})
}
})
// ...and its response translates back into our result
if (!params.subscribe && res.status === 304) {
var result = { not_modified: true }
if (res.version) result.version = res.version
return result
}
if (!res.ok)
if (params.subscribe) throw new Error('failed to subscribe')
else return null
var result = {}
if (res.version) result.version = res.version
// On a solo response the body is the representation, so its type
// may come as Repr-Type or (from older servers) Content-Type
if (!params.subscribe)
set_repr_type(result, res.headers.get('repr-type')
|| res.headers.get('content-type'))
if (params.head) return result
if (params.subscribe) {
var repr_type = res.headers.get('repr-type') || undefined
res.subscribe(async update => {
if (update.status === 404 || update.status === 410)
update.delete = true
else if (update.status && update.status !== 200)
return // e.g. 304: no new state to apply
if (update.repr_type) repr_type = update.repr_type
set_repr_type(update, repr_type)
await params.subscribe(update)
}, e => params.on_error?.(e))
return res
} else {
result.body = await res.arrayBuffer()
return result
}
}
// Otherwise the blob is local.
else {
// Initialize storage on first use...
await braid_blob.init()
if (params.signal?.aborted) return
// ...and do the rest within this key's fiber, which serializes
// all operations on the key
return await within_fiber(key, async () => {
// Get the current version and content-type from metadata.
var meta = await get_meta(key)
if (params.signal?.aborted) return
// If the blob is missing, we want to return null, which becomes a 404.
if (!meta.event
// But if this is a subscription, we will just wait for the
// blob to appear. A 404 will get sent over the subscription
// as the first update.
&& !params.subscribe)
return null
var result = { version: meta.event ? [meta.event] : [] }
set_repr_type(result, meta.content_type)
if (!params.subscribe) {
// The spec requires a 432 to echo back the version it
// could not satisfy. We keep no history, so the current
// version is the only one we have. Use compare_events,
// since wallclockish versions can differ in trailing zeros.
if (params.version &&
compare_events(params.version[0], meta.event) !== 0)
return {status: 432, status_text: 'Version Not Found',
version: params.version}
// Only a newer Parents is one we don't have: an older one
// is a client catching up
if (compare_events(params.parents?.[0], meta.event) > 0)
return {status: 432, status_text: 'Version Not Found',
parents: params.parents}
}
// Set our response headers for hte .serve()
if (params.header_cb) await params.header_cb(result)
if (params.signal?.aborted) return
// Handle etags.
// If the client tells us (via if_none_match) that it already has this version...
if (params.if_none_match?.includes(result.version[0]))
// Return not_modified and skip the body. serve() will render a 304.
result.not_modified = true
var db = params.db || braid_blob.db
// A stale If-Range means the blob changed under the client,
// which wants the whole thing rather than a slice of it
var range = params.range
if (range && params.if_range &&
!params.if_range.includes(result.version[0]))
range = null
if (params.head) {
if (db.open) {
var handle = await db.open(key)
if (handle) {
result.repr_length = handle.size
await handle.close()
}
}
return result
}
if (params.subscribe) {
// Return subscription
var subscribe_chain = Promise.resolve()
params.my_subscribe = (x) => subscribe_chain =
subscribe_chain.then(() =>
!params.signal?.aborted && params.subscribe(x))
// Remember this subscription....
if (!braid_blob.subscriptions_to[key])
braid_blob.subscriptions_to[key] = new Map()
var peer = params.peer || Math.random().toString(36).slice(2)
braid_blob.subscriptions_to[key].set(peer, {
sendUpdate: (update) => {
if (update.delete ||
// Skip updates it already has
compare_events(update.version[0], params.parents?.[0]) > 0)
params.my_subscribe(update)
}
})
// ...until it aborts
params.signal?.addEventListener('abort', () => {
braid_blob.subscriptions_to[key].delete(peer)
if (!braid_blob.subscriptions_to[key].size)
delete braid_blob.subscriptions_to[key]
})
if (params.before_send_cb) await params.before_send_cb()
if (params.signal?.aborted) return
// Send an immediate update if needed
if (compare_events(result.version?.[0], params.parents?.[0]) > 0) {
result.sent = true
if (!result.not_modified)
result.body = await db.read(key)
params.my_subscribe(result)
}
} else if (!result.not_modified) {
// If not subscribe, send the body now.
var streaming = params.as_stream && db.open
// Ranges resolve against the length, and streams declare
// it as Content-Length
var handle = null, whole = null, repr_length = null
if (range || streaming) {
if (db.open) {
handle = await db.open(key)
repr_length = handle?.size ?? 0
} else {
whole = await db.read(key)
repr_length = whole?.length ?? 0
}
result.repr_length = repr_length
}
var offsets = range ? resolve_range(range, repr_length) : null
if (range && offsets === null) {
await handle?.close()
result.status = 416
result.status_text = 'Range Not Satisfiable'
return result
}
if (offsets === undefined) offsets = null
var content
if (streaming && handle) content = handle.stream(offsets)
else {
await handle?.close()
content = whole
? (offsets ? whole.subarray(offsets.start,
offsets.end + 1) : whole)
: await db.read(key, offsets)
}
// A range answers with patches, fragments scoped by range
if (offsets) result.patches = [{unit: range.unit,
range: `${offsets.start}-${offsets.end}`, content}]
else result.body = content
}
return result
})
}
}
braid_blob.put = async (key, body, params = {}) => {
params = normalize_params(params)
// If the key is a URL, this is a braid-HTTP PUT to a remote server
if (key instanceof URL)
return await braid_fetch(key.href, {
method: 'PUT',
signal: params.signal,
body,
// if we have this... ...the request carries this:
...(!params.dont_retry && {retry: () => true}),
...(params.version != null && {version: params.version}),
...(params.peer != null && {peer: params.peer}),
...(params.repr_type && {repr_type: params.repr_type}),
headers: {
...params.headers,
...(params.version && {'Version-Type': 'wallclockish'})
}
})
// Otherwise we are putting locally
else {
// Initialize the blob
await braid_blob.init()
if (params.signal?.aborted) return
// And write to it within a serialized fiber for this key
return await within_fiber(key, async () => {
var meta = await get_meta(key)
if (params.signal?.aborted) return
// Use the writer's event id, or mint a fresh one, which is
// newest by construction
var their_e = params.version ? params.version[0] :
create_event(meta.event)
// The write only wins if it's newer than what we have
if (compare_events(their_e, meta.event) > 0) {
meta.event = their_e
if (!params.skip_write)
await (params.db || braid_blob.db).write(key, body)
if (params.signal?.aborted) return
if (params.repr_type)
meta.content_type = params.repr_type
save_meta(key, meta)
if (params.signal?.aborted) return
// Notify all subscriptions of the update
// (except the peer which made the PUT request itself)
var update = { version: [meta.event], body }
set_repr_type(update, meta.content_type)
if (braid_blob.subscriptions_to[key])
for (var [peer, sub] of braid_blob.subscriptions_to[key].entries())
if (!params.peer || params.peer !== peer)
await sub.sendUpdate(update)
}
return meta.event // the winning event, either way
})
}
}
braid_blob.delete = async (key, params = {}) => {
params = normalize_params(params)
// If the key is a URL, this is a braid-HTTP DELETE to a remote server
if (key instanceof URL)
return await braid_fetch(key.href, {
method: 'DELETE',
signal: params.signal,
// if we have this... ...the request carries this:
...(params.peer != null && {peer: params.peer}),
// Retry unless the status is a final answer
...(!params.dont_retry && {retry: res =>
![309, 404, 406, 432].includes(res.status)}),
headers: {
...params.headers,
...(params.repr_type && {'Accept': params.repr_type})
}
})
// Else it's a local delete
else {
await braid_blob.init()
if (params.signal?.aborted) return
return await within_fiber(key, async () => {
var meta = await get_meta(key)
if (params.signal?.aborted) return
// Unlike put, a delete always wins -- it has no version to
// compare with ours
await (params.db || braid_blob.db).delete(key)
await delete_meta(key)
// Notify all subscriptions of the delete
// (except the peer which made the DELETE request itself)
var update = { delete: true }
set_repr_type(update, meta.content_type)
if (braid_blob.subscriptions_to[key])
for (var [peer, sub] of braid_blob.subscriptions_to[key].entries())
if (!params.peer || params.peer !== peer)
sub.sendUpdate(update)
})
}
}
// list() accepts an optional callback to process keys atomically,
// avoiding races where the set of keys changes between await and processing
braid_blob.list = async (cb) => {
await braid_blob.init()
var keys = braid_blob.meta_db.prepare(`SELECT key FROM meta`).all().map(row => row.key)
if (cb) cb(keys)
return keys
}
// exists() checks whether a key is present, with an optional callback
// to process the result atomically (same pattern as list())
braid_blob.exists = async (key, cb) => {
await braid_blob.init()
var exists = !!braid_blob.meta_db.prepare(`SELECT 1 FROM meta WHERE key = ?`).get(key)
if (cb) cb(exists)
return exists
}
braid_blob.init = async () => {
// We only want to initialize once
var init_p = real_init()
braid_blob.init = () => init_p
await braid_blob.init()
async function real_init() {
var fs = require('fs')
// Resolve the three folders:
// - db_folder holds the blobs (default ./braid-blobs)
// - meta_folder holds the sqlite (default: inside db_folder)
// - temp_folder holds the in-progress atomic writes (default: inside meta_folder or db_folder)
var db_was_not_set = !braid_blob.db_folder
if (db_was_not_set)
braid_blob.db_folder = './braid-blobs'
// The db_folder can also be a custom db object
var get_db_folder = () =>
// Then anything needing an actual folder falls back to the default
((typeof braid_blob.db_folder === 'string') &&
braid_blob.db_folder) || './braid-blobs'
// deal with temp folder
if (!braid_blob.temp_folder) {
// Deal with versions before 0.0.53
await fs.promises.rm(
`${braid_blob.meta_folder || './braid-blob-meta'}/temp`,
{ recursive: true, force: true })
braid_blob.temp_folder = braid_blob.meta_folder ||
get_db_folder()
}
await fs.promises.mkdir(braid_blob.temp_folder,
{ recursive: true })
// Delete temp files left by a crash
for (var f of await fs.promises.readdir(braid_blob.temp_folder))
if (f.match(/^temp_\w+$/))
await fs.promises.unlink(`${braid_blob.temp_folder}/${f}`)
// deal with meta folder
var meta_was_not_set = !braid_blob.meta_folder
if (meta_was_not_set)
braid_blob.meta_folder = get_db_folder()
await fs.promises.mkdir(braid_blob.meta_folder,
{ recursive: true })
// set up sqlite for meta storage
var Database = require('better-sqlite3')
braid_blob.meta_db = new Database(
`${braid_blob.meta_folder}/meta.sqlite`)
braid_blob.meta_db.pragma('journal_mode = WAL')
braid_blob.meta_db.exec(`
CREATE TABLE IF NOT EXISTS meta (
key TEXT PRIMARY KEY,
value JSON
)
`)
// Migrate from before 0.0.53, which stored meta as one JSON
// file per key (defaults: meta in ./braid-blob-meta, blobs in
// ./braid-blob-db)
async function migrate_meta_files(dir) {
for (var f of await fs.promises.readdir(dir)) {
if (!f.match(/\.[0-9a-f]+$/i)) continue
var key = decode_filename(f)
var value = JSON.parse(
await fs.promises.readFile(`${dir}/${f}`, 'utf8'))
save_meta(key, value)
await fs.promises.unlink(`${dir}/${f}`)
}
}
if (meta_was_not_set) {
try {
await fs.promises.access('./braid-blob-meta')
await migrate_meta_files('./braid-blob-meta')
await fs.promises.rm('./braid-blob-meta', { recursive: true })
} catch (e) {}
} else if (braid_blob.meta_folder !== braid_blob.db_folder)
await migrate_meta_files(braid_blob.meta_folder)
// Deal with versions before 0.0.53: migrate db files from ./braid-blob-db
if (db_was_not_set) {
try {
await fs.promises.access('./braid-blob-db')
for (var f of await fs.promises.readdir('./braid-blob-db')) {
if (!f.match(/\.[0-9a-f]+$/i)) continue
await fs.promises.copyFile(
`./braid-blob-db/${f}`,
`${braid_blob.db_folder}/${f}`)
await fs.promises.unlink(`./braid-blob-db/${f}`)
}
await fs.promises.rm('./braid-blob-db', { recursive: true })
} catch (e) {}
}
// Set up db - either use provided object or create file-based storage
if (typeof braid_blob.db_folder === 'string') {
await require('fs').promises.mkdir(braid_blob.db_folder, { recursive: true })
braid_blob.db = {
// The whole blob, or just the bytes between the given
// start and end offsets, inclusive
read: async (key, offsets) => {
var file_path = `${braid_blob.db_folder}/${encode_filename(key)}`
try {
if (!offsets)
return await require('fs').promises.readFile(file_path)
var file = await require('fs').promises.open(file_path)
try {
var len = offsets.end - offsets.start + 1
var buf = Buffer.allocUnsafe(len)
var {bytesRead} = await file.read(buf, 0, len, offsets.start)
return bytesRead === len ? buf : buf.subarray(0, bytesRead)
} finally { await file.close() }
} catch (e) {
if (e.code === 'ENOENT') return null
throw e
}
},
// Opens the blob for its size and bytes. Writes replace
// the file by rename, so a handle keeps seeing one version.
open: async (key) => {
var file_path = `${braid_blob.db_folder}/${encode_filename(key)}`
try {
var file = await require('fs').promises.open(file_path)
} catch (e) {
if (e.code === 'ENOENT') return null
throw e
}
return {
size: (await file.stat()).size,
stream: (offsets) => file.createReadStream(
offsets && {start: offsets.start, end: offsets.end}),
close: () => file.close(),
}
},
write: async (key, data) => {
var file_path = `${braid_blob.db_folder}/${encode_filename(key)}`
await atomic_write(file_path, data, braid_blob.temp_folder)
},
delete: async (key) => {
var file_path = `${braid_blob.db_folder}/${encode_filename(key)}`
try {
await require('fs').promises.unlink(file_path)
} catch (e) {
if (e.code !== 'ENOENT') throw e
}
}
}
} else {
// db_folder is already an object with read/write/delete
braid_blob.db = braid_blob.db_folder
}
// establish a peer id if not already set
if (!braid_blob.peer)
braid_blob.peer = Math.random().toString(36).slice(2)
}
}
function get_meta(key) {
var row = braid_blob.meta_db.prepare(
`SELECT value FROM meta WHERE key = ?`).get(key)
return row ? JSON.parse(row.value) : {}
}
function save_meta(key, meta) {
braid_blob.meta_db.prepare(
`INSERT OR REPLACE INTO meta (key, value) VALUES (?, json(?))`)
.run(key, JSON.stringify(meta))
}
function delete_meta(key) {
braid_blob.meta_db.prepare(`DELETE FROM meta WHERE key = ?`).run(key)
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
function compare_events(a, b) {
if (!a) a = ''
if (!b) b = ''
// Check if values match wallclockish format
var re = compare_events.re || (compare_events.re = /^-?[0-9]*\.[0-9]*$/)
var a_match = re.test(a)
var b_match = re.test(b)
// If only one matches, it wins
if (a_match && !b_match) return 1
if (b_match && !a_match) return -1
// If neither matches, compare lexicographically
if (!a_match && !b_match) {
if (a < b) return -1
if (a > b) return 1
return 0
}
// Both match - compare as decimals using BigInt
// Add decimal point if missing
if (a.indexOf('.') === -1) a += '.'
if (b.indexOf('.') === -1) b += '.'
// Pad the shorter fractional part with zeros
var diff = (a.length - a.indexOf('.')) - (b.length - b.indexOf('.'))
if (diff < 0) a += '0'.repeat(-diff)
else if (diff > 0) b += '0'.repeat(diff)
// Remove decimal and parse as BigInt
var a_big = BigInt(a.replace('.', ''))
var b_big = BigInt(b.replace('.', ''))
if (a_big < b_big) return -1
if (a_big > b_big) return 1
return 0
}
function create_event(current_event, decimal_places=3, entropy_digits=4) {
var now = '' + Date.now() / 1000
if (compare_events(now, current_event) > 0)
return now
// Add smallest increment to current_event using BigInt
var e = current_event || '0'
if (e.indexOf('.') === -1) e += '.'
// Truncate or pad to exactly decimal_places decimal places
var dot = e.indexOf('.')
var frac = e.slice(dot + 1)
if (frac.length > decimal_places) e = e.slice(0, dot + 1 + decimal_places)
else if (frac.length < decimal_places) e += '0'.repeat(decimal_places - frac.length)
var big = BigInt(e.replace('.', '')) + 1n
var str = String(big)
// Reinsert decimal point
var result = str.slice(0, -decimal_places) + '.' + str.slice(-decimal_places)
return result + random_digits(entropy_digits)
}
function random_digits(n) {
if (!n) return ''
var s = ''
for (var i = 0; i < n; i++) s += Math.floor(Math.random() * 10)
return s
}
function ascii_ify(s) {
return s.replace(/[^\x20-\x7E]/g, c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0'))
}
function version_to_header(version) {
// Convert version array to header format: JSON without outer brackets
if (!version || !version.length) return ''
return ascii_ify(version.map(v => JSON.stringify(v)).join(', '))
}
function within_fiber(id, func) {
if (!within_fiber.chains) within_fiber.chains = {}
var prev = within_fiber.chains[id] || Promise.resolve()
var curr = prev.then(async () => {
try {
return await func()
} finally {
if (within_fiber.chains[id] === curr)
delete within_fiber.chains[id]
}
})
return within_fiber.chains[id] = curr
}
async function slurp(req) {
return await new Promise(done => {
var chunks = []
req.on('data', chunk => chunks.push(chunk))
req.on('end', () => done(Buffer.concat(chunks)))
})
}
// A braid-blob etag is a version id, encoded in HTTP's etag syntax:
function version_to_etag(version_id) {
return '"' + version_id + '"'
}
// Decodes an If-None-Match header into an array of version ids,
// stripping each etag's quotes and W/ prefix (per RFC 9110,
// If-None-Match compares weakly, so W/"x" matches "x")
function etags_to_versions(header) {
if (!header) return null
var versions = [], m, re = /(?:W\/)?"([^"]*)"/g
while ((m = re.exec(header))) versions.push(m[1])
return versions
}
// Decodes a Range header: "bytes=500-600" -> {unit, range}. The form
// of the range string is up to the unit.
function parse_range(header) {
var m = header?.match(/^\s*([^=\s]+)\s*=\s*(\S.*?)\s*$/)
return m && {unit: m[1].toLowerCase(), range: m[2]}
}
// Resolves a {unit, range} into inclusive start/end offsets. undefined
// for a unit or form we don't support, null for one we can't satisfy.
function resolve_range(range, size) {
if (range.unit !== 'bytes') return undefined
// "500-600", "500-" (to the end), or "-500" (the last 500 bytes)
var m = range.range.match(/^(\d*)-(\d*)$/)
if (!m || (!m[1] && !m[2])) return undefined
var first = m[1] ? +m[1] : null, last = m[2] ? +m[2] : null
if (first == null) return last ? {start: Math.max(0, size - last),
end: size - 1} : null
if (last != null && last < first) return undefined // invalid: ignore it
if (first >= size) return null
return {start: first, end: last == null ? size - 1
: Math.min(last, size - 1)}
}
function isAcceptable(contentType, acceptHeader) {
// If no Accept header or Accept is */*, accept everything
if (!acceptHeader || acceptHeader === '*/*') return true;
// Parse the Accept header into individual media types
const acceptTypes = acceptHeader.split(',').map(type => type.trim());
for (const acceptType of acceptTypes) {
// Remove quality values (e.g., "text/html;q=0.9" -> "text/html")
const cleanAcceptType = acceptType.split(';')[0].trim();
// Exact match
if (cleanAcceptType === contentType) return true;