FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tanstack.com/src/utils/github-content-cache.server.ts at main · TanStack/tanstack.com · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TanStack
/
tanstack.com
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
396
Star
1.1k
Code
Issues
45
Pull requests
56
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
tanstack.com
/
src
/
utils
/
github-content-cache.server.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
1529 lines (1309 loc) · 37.3 KB
Breadcrumbs
tanstack.com
/
src
/
utils
/
github-content-cache.server.ts
Copy path
File metadata and controls
1529 lines (1309 loc) · 37.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
{
getBlobStorage
,
getBlobStorageCache
,
type
BlobStorage
,
type
BlobStorageCache
,
type
BlobStorageListedObject
,
}
from
'~/server/runtime/blob-storage.server'
import
{
scheduleHostRuntimeTask
}
from
'~/server/runtime/host.server'
import
{
isValidRepoPath
,
MAX_REPO_PATH_LENGTH
}
from
'./repo-path'
const
POSITIVE_STALE_MS
=
5
*
60
*
1000
const
NEGATIVE_STALE_MS
=
15
*
60
*
1000
const
DEFAULT_PRUNE_MAX_AGE_MS
=
30
*
24
*
60
*
60
*
1000
const
DEFAULT_NEGATIVE_PRUNE_MAX_AGE_MS
=
24
*
60
*
60
*
1000
const
GITHUB_CONTENT_BLOB_STORAGE
=
'githubContentCache'
const
STORED_CONTENT_TYPE
=
'application/json; charset=utf-8'
const
EPOCH_ISO
=
new
Date
(
0
)
.
toISOString
(
)
const
MAX_MEMORY_CACHE_ENTRIES
=
300
// Internal sentinel path used for recursive repository tree metadata. Allowed
// alongside normal repo paths.
const
SENTINEL_PATHS
=
new
Set
(
[
'__github_recursive_tree__'
]
)
const
REPO_PATTERN
=
/
^
[
a
-
z
A
-
Z
0
-
9
.
_
-
]
+
\/
[
a
-
z
A
-
Z
0
-
9
.
_
-
]
+
$
/
// Git refs allow a fairly wide character set, but we restrict to the subset
// we actually publish from (branch names + tags). No spaces, no shell
// metacharacters, no path traversal.
const
GIT_REF_PATTERN
=
/
^
[
a
-
z
A
-
Z
0
-
9
.
_
/
-
]
+
$
/
const
ARTIFACT_ID_PATTERN
=
/
^
[
a
-
z
A
-
Z
0
-
9
.
_
:
-
]
+
$
/
const
MAX_REPO_LEN
=
100
const
MAX_GIT_REF_LEN
=
100
const
MAX_ARTIFACT_TYPE_LEN
=
50
const
MAX_ARTIFACT_KEY_LEN
=
255
type
ContentKind
=
'dir'
|
'file'
type
CachedValue
<
T
>
=
T
|
null
|
undefined
type
GithubContentKey
=
{
contentKind
:
ContentKind
gitRef
:
string
path
:
string
repo
:
string
}
type
GithubContentRecord
=
GithubContentKey
&
{
isPresent
:
boolean
jsonContent
?:
unknown
staleAt
:
Date
textContent
?:
string
updatedAt
:
Date
}
type
DocsArtifactKey
=
{
artifactKey
:
string
artifactType
:
string
docsRoot
:
string
gitRef
:
string
repo
:
string
}
type
DocsArtifactRecord
=
DocsArtifactKey
&
{
payload
:
unknown
staleAt
:
Date
updatedAt
:
Date
}
type
StoredBlob
=
{
metadata
:
Record
<
string
,
string
>
text
:
string
value
:
unknown
}
type
PruneResult
=
{
cutoff
:
Date
docsArtifactDeleted
:
number
githubContentDeleted
:
number
githubContentNegativesDeleted
:
number
negativeCutoff
:
Date
}
export
type
DocsCacheRepoStats
=
{
artifactEntries
:
number
cachedRefCount
:
number
contentEntries
:
number
lastUpdatedAt
:
string
|
null
repo
:
string
staleArtifactEntries
:
number
staleContentEntries
:
number
staleEntries
:
number
totalEntries
:
number
}
type
RepoStatsAccumulator
=
{
artifactEntries
:
number
contentEntries
:
number
lastUpdatedAt
:
Date
|
null
refs
:
Set
<
string
>
repo
:
string
staleArtifactEntries
:
number
staleContentEntries
:
number
}
type
MemoryCacheEntry
=
{
cache
:
'artifact'
|
'content'
key
:
string
updatedAt
:
Date
}
type
CacheBackend
=
{
getDocsArtifact
:
(
key
:
DocsArtifactKey
,
)
=>
Promise
<
DocsArtifactRecord
|
undefined
>
getGitHubContent
:
(
key
:
GithubContentKey
,
)
=>
Promise
<
GithubContentRecord
|
undefined
>
listRepoStats
:
(
)
=>
Promise
<
Array
<
DocsCacheRepoStats
>
>
markDocsArtifactsStale
:
(
opts
:
{
gitRef
?:
string
repo
?:
string
}
)
=>
Promise
<
number
>
markGitHubContentStale
:
(
opts
:
{
gitRef
?:
string
repo
?:
string
}
)
=>
Promise
<
number
>
pruneStaleCacheRows
:
(
opts
:
{
maxAgeMs
?:
number
negativeMaxAgeMs
?:
number
}
)
=>
Promise
<
PruneResult
>
putDocsArtifact
:
(
record
:
DocsArtifactRecord
)
=>
Promise
<
void
>
putGitHubContent
:
(
record
:
GithubContentRecord
)
=>
Promise
<
void
>
}
export
class
InvalidCacheKeyError
extends
Error
{
constructor
(
field
:
string
,
value
:
string
)
{
super
(
`Refusing to cache: invalid
${
field
}
=
${
JSON
.
stringify
(
value
)
.
slice
(
0
,
80
)
}
`
,
)
this
.
name
=
'InvalidCacheKeyError'
}
}
const
pendingRefreshes
=
new
Map
<
string
,
Promise
<
void
>
>
(
)
const
memoryGitHubContent
=
new
Map
<
string
,
GithubContentRecord
>
(
)
const
memoryDocsArtifacts
=
new
Map
<
string
,
DocsArtifactRecord
>
(
)
let
blobBackend
:
CacheBackend
|
undefined
function
assertValidRepo
(
repo
:
string
)
{
if
(
repo
.
length
===
0
||
repo
.
length
>
MAX_REPO_LEN
||
!
REPO_PATTERN
.
test
(
repo
)
)
{
throw
new
InvalidCacheKeyError
(
'repo'
,
repo
)
}
}
function
assertValidGitRef
(
gitRef
:
string
)
{
if
(
gitRef
.
length
===
0
||
gitRef
.
length
>
MAX_GIT_REF_LEN
||
!
GIT_REF_PATTERN
.
test
(
gitRef
)
||
gitRef
.
includes
(
'..'
)
||
gitRef
.
startsWith
(
'/'
)
||
gitRef
.
endsWith
(
'/'
)
||
gitRef
.
includes
(
'//'
)
)
{
throw
new
InvalidCacheKeyError
(
'gitRef'
,
gitRef
)
}
}
function
assertValidContentPath
(
path
:
string
)
{
if
(
path
===
''
||
SENTINEL_PATHS
.
has
(
path
)
)
{
return
}
if
(
path
.
length
>
MAX_REPO_PATH_LENGTH
)
{
throw
new
InvalidCacheKeyError
(
'path'
,
path
)
}
if
(
!
isValidRepoPath
(
path
)
)
{
throw
new
InvalidCacheKeyError
(
'path'
,
path
)
}
}
function
assertValidArtifactId
(
field
:
string
,
value
:
string
,
maxLength
:
number
,
)
{
if
(
value
.
length
===
0
||
value
.
length
>
maxLength
||
!
ARTIFACT_ID_PATTERN
.
test
(
value
)
)
{
throw
new
InvalidCacheKeyError
(
field
,
value
)
}
}
function
assertValidGithubContentKey
(
opts
:
GithubContentKey
)
{
assertValidRepo
(
opts
.
repo
)
assertValidGitRef
(
opts
.
gitRef
)
assertValidContentPath
(
opts
.
path
)
}
function
assertValidDocsArtifactKey
(
opts
:
DocsArtifactKey
)
{
assertValidRepo
(
opts
.
repo
)
assertValidGitRef
(
opts
.
gitRef
)
assertValidContentPath
(
opts
.
docsRoot
)
assertValidArtifactId
(
'artifactType'
,
opts
.
artifactType
,
MAX_ARTIFACT_TYPE_LEN
,
)
assertValidArtifactId
(
'artifactKey'
,
opts
.
artifactKey
,
MAX_ARTIFACT_KEY_LEN
)
}
async
function
withPendingRefresh
<
T
>
(
key
:
string
,
fn
:
(
)
=>
Promise
<
T
>
)
{
const
pending
=
pendingRefreshes
.
get
(
key
)
if
(
pending
)
{
await
pending
return
fn
(
)
}
const
promise
=
fn
(
)
pendingRefreshes
.
set
(
key
,
promise
.
then
(
(
)
=>
undefined
,
(
)
=>
undefined
,
)
,
)
try
{
return
await
promise
}
finally
{
pendingRefreshes
.
delete
(
key
)
}
}
function
createFreshnessWindow
(
isPresent
:
boolean
)
{
const
now
=
Date
.
now
(
)
const
staleFor
=
isPresent
?
POSITIVE_STALE_MS
:
NEGATIVE_STALE_MS
return
{
staleAt
:
new
Date
(
now
+
staleFor
)
,
}
}
function
isFresh
(
staleAt
:
Date
)
{
return
staleAt
.
getTime
(
)
>
Date
.
now
(
)
}
// markGitHubContentStale / markDocsArtifactsStale set staleAt to the epoch
// (new Date(0)) as a sentinel for "forcibly invalidated": an admin clicked
// the purge button or a push webhook fired. The object stays around so refresh
// paths can still fall back to it if GitHub is unreachable, and docs artifacts
// can serve it while scheduling a background rebuild when the runtime supports
// waitUntil.
function
isForciblyStale
(
staleAt
:
Date
)
{
return
staleAt
.
getTime
(
)
<=
0
}
function
encodeKeySegment
(
value
:
string
)
{
return
encodeURIComponent
(
value
)
}
function
getGitHubContentBlobKey
(
opts
:
GithubContentKey
)
{
const
prefix
=
opts
.
contentKind
===
'file'
?
'github:file'
:
'github:dir'
return
`
${
prefix
}
/
${
opts
.
repo
}
/
${
encodeKeySegment
(
opts
.
gitRef
)
}
/
${
opts
.
path
}
`
}
function
getDocsArtifactBlobKey
(
opts
:
DocsArtifactKey
)
{
return
`docs-artifact/
${
opts
.
repo
}
/
${
encodeKeySegment
(
opts
.
gitRef
)
}
/
${
opts
.
docsRoot
}
/
${
encodeKeySegment
(
opts
.
artifactType
)
}
/
${
encodeKeySegment
(
opts
.
artifactKey
)
}
`
}
function
getGitHubContentPrefixes
(
opts
:
{
gitRef
?:
string
;
repo
?:
string
}
)
{
const
kinds
:
Array
<
ContentKind
>
=
[
'file'
,
'dir'
]
const
prefixes
=
kinds
.
map
(
(
contentKind
)
=>
contentKind
===
'file'
?
'github:file'
:
'github:dir'
,
)
const
repo
=
opts
.
repo
const
gitRef
=
opts
.
gitRef
if
(
repo
&&
gitRef
)
{
return
prefixes
.
map
(
(
prefix
)
=>
`
${
prefix
}
/
${
repo
}
/
${
encodeKeySegment
(
gitRef
)
}
/`
,
)
}
if
(
repo
)
{
return
prefixes
.
map
(
(
prefix
)
=>
`
${
prefix
}
/
${
repo
}
/`
)
}
return
prefixes
.
map
(
(
prefix
)
=>
`
${
prefix
}
/`
)
}
function
getDocsArtifactPrefixes
(
opts
:
{
gitRef
?:
string
;
repo
?:
string
}
)
{
if
(
opts
.
repo
&&
opts
.
gitRef
)
{
return
[
`docs-artifact/
${
opts
.
repo
}
/
${
encodeKeySegment
(
opts
.
gitRef
)
}
/`
]
}
if
(
opts
.
repo
)
{
return
[
`docs-artifact/
${
opts
.
repo
}
/`
]
}
return
[
'docs-artifact/'
]
}
function
createStoredText
(
value
:
unknown
)
{
if
(
value
===
undefined
)
{
throw
new
Error
(
'Cannot cache undefined GitHub content'
)
}
return
JSON
.
stringify
(
{
value
,
}
)
}
function
readStoredValue
(
text
:
string
)
{
let
parsed
:
unknown
try
{
parsed
=
JSON
.
parse
(
text
)
}
catch
{
return
undefined
}
if
(
typeof
parsed
!==
'object'
||
parsed
===
null
||
!
(
'value'
in
parsed
)
)
{
return
undefined
}
return
parsed
.
value
}
function
readMetadataString
(
metadata
:
Record
<
string
,
string
>
|
undefined
,
key
:
string
,
)
{
const
value
=
metadata
?.
[
key
]
return
typeof
value
===
'string'
?
value
:
undefined
}
function
readMetadataBoolean
(
metadata
:
Record
<
string
,
string
>
|
undefined
,
key
:
string
,
)
{
const
value
=
readMetadataString
(
metadata
,
key
)
if
(
value
===
'true'
)
{
return
true
}
if
(
value
===
'false'
)
{
return
false
}
return
undefined
}
function
readMetadataDate
(
metadata
:
Record
<
string
,
string
>
|
undefined
,
key
:
string
,
)
{
const
value
=
readMetadataString
(
metadata
,
key
)
if
(
!
value
)
{
return
undefined
}
const
date
=
new
Date
(
value
)
return
Number
.
isNaN
(
date
.
getTime
(
)
)
?
undefined
:
date
}
function
readMetadataContentKind
(
metadata
:
Record
<
string
,
string
>
|
undefined
,
)
:
ContentKind
|
undefined
{
const
value
=
readMetadataString
(
metadata
,
'contentKind'
)
if
(
value
===
'dir'
)
{
return
'dir'
}
if
(
value
===
'file'
)
{
return
'file'
}
return
undefined
}
function
getRequiredFileContent
(
value
:
unknown
)
{
if
(
typeof
value
===
'string'
)
{
return
value
}
throw
new
Error
(
'Cannot cache non-string GitHub file content'
)
}
function
getGithubContentMetadata
(
record
:
GithubContentRecord
)
{
return
{
contentKind
:
record
.
contentKind
,
gitRef
:
record
.
gitRef
,
isPresent
:
record
.
isPresent
?
'true'
:
'false'
,
path
:
record
.
path
,
repo
:
record
.
repo
,
staleAt
:
record
.
staleAt
.
toISOString
(
)
,
updatedAt
:
record
.
updatedAt
.
toISOString
(
)
,
}
}
function
getDocsArtifactMetadata
(
record
:
DocsArtifactRecord
)
{
return
{
artifactKey
:
record
.
artifactKey
,
artifactType
:
record
.
artifactType
,
docsRoot
:
record
.
docsRoot
,
gitRef
:
record
.
gitRef
,
repo
:
record
.
repo
,
staleAt
:
record
.
staleAt
.
toISOString
(
)
,
updatedAt
:
record
.
updatedAt
.
toISOString
(
)
,
}
}
function
inferGithubContentMetadataFromBlobKey
(
key
:
string
,
uploaded
?:
Date
)
{
const
segments
=
key
.
split
(
'/'
)
const
prefix
=
segments
[
0
]
const
contentKind
=
prefix
===
'github:file'
?
'file'
:
prefix
===
'github:dir'
?
'dir'
:
null
if
(
!
contentKind
||
segments
.
length
<
5
)
{
return
undefined
}
const
repo
=
`
${
segments
[
1
]
}
/
${
segments
[
2
]
}
`
const
gitRef
=
decodeURIComponent
(
segments
[
3
]
)
const
path
=
segments
.
slice
(
4
)
.
join
(
'/'
)
const
updatedAt
=
uploaded
??
new
Date
(
)
const
staleAt
=
new
Date
(
updatedAt
.
getTime
(
)
+
POSITIVE_STALE_MS
)
return
{
contentKind
,
gitRef
,
isPresent
:
'true'
,
path
,
repo
,
staleAt
:
staleAt
.
toISOString
(
)
,
updatedAt
:
updatedAt
.
toISOString
(
)
,
}
}
function
getStoredBlob
(
opts
:
{
key
?:
string
metadata
:
Record
<
string
,
string
>
|
undefined
text
:
string
uploaded
?:
Date
}
)
{
const
value
=
readStoredValue
(
opts
.
text
)
const
storedMetadata
=
opts
.
metadata
&&
Object
.
keys
(
opts
.
metadata
)
.
length
>
0
?
opts
.
metadata
:
undefined
const
metadata
=
storedMetadata
??
(
opts
.
key
?
inferGithubContentMetadataFromBlobKey
(
opts
.
key
,
opts
.
uploaded
)
:
undefined
)
if
(
value
===
undefined
||
!
metadata
)
{
return
undefined
}
return
{
metadata
,
text
:
opts
.
text
,
value
,
}
}
function
readGithubContentRecord
(
stored
:
StoredBlob
)
{
const
repo
=
readMetadataString
(
stored
.
metadata
,
'repo'
)
const
gitRef
=
readMetadataString
(
stored
.
metadata
,
'gitRef'
)
const
path
=
readMetadataString
(
stored
.
metadata
,
'path'
)
const
contentKind
=
readMetadataContentKind
(
stored
.
metadata
)
const
isPresent
=
readMetadataBoolean
(
stored
.
metadata
,
'isPresent'
)
const
staleAt
=
readMetadataDate
(
stored
.
metadata
,
'staleAt'
)
const
updatedAt
=
readMetadataDate
(
stored
.
metadata
,
'updatedAt'
)
if
(
!
repo
||
!
gitRef
||
path
===
undefined
||
!
contentKind
||
isPresent
===
undefined
||
!
staleAt
||
!
updatedAt
)
{
return
undefined
}
return
{
contentKind
,
gitRef
,
isPresent
,
jsonContent
:
contentKind
===
'dir'
&&
isPresent
?
stored
.
value
:
undefined
,
path
,
repo
,
staleAt
,
textContent
:
contentKind
===
'file'
&&
isPresent
&&
typeof
stored
.
value
===
'string'
?
stored
.
value
:
undefined
,
updatedAt
,
}
}
function
isNegativeGithubDirectoryBlob
(
stored
:
StoredBlob
)
{
return
(
readMetadataContentKind
(
stored
.
metadata
)
===
'dir'
&&
readMetadataBoolean
(
stored
.
metadata
,
'isPresent'
)
===
false
)
}
function
readDocsArtifactRecord
(
stored
:
StoredBlob
)
{
const
repo
=
readMetadataString
(
stored
.
metadata
,
'repo'
)
const
gitRef
=
readMetadataString
(
stored
.
metadata
,
'gitRef'
)
const
docsRoot
=
readMetadataString
(
stored
.
metadata
,
'docsRoot'
)
const
artifactType
=
readMetadataString
(
stored
.
metadata
,
'artifactType'
)
const
artifactKey
=
readMetadataString
(
stored
.
metadata
,
'artifactKey'
)
const
staleAt
=
readMetadataDate
(
stored
.
metadata
,
'staleAt'
)
const
updatedAt
=
readMetadataDate
(
stored
.
metadata
,
'updatedAt'
)
if
(
!
repo
||
!
gitRef
||
docsRoot
===
undefined
||
!
artifactType
||
!
artifactKey
||
!
staleAt
||
!
updatedAt
)
{
return
undefined
}
return
{
artifactKey
,
artifactType
,
docsRoot
,
gitRef
,
payload
:
stored
.
value
,
repo
,
staleAt
,
updatedAt
,
}
}
async
function
readBlob
(
storage
:
BlobStorage
,
cache
:
BlobStorageCache
|
undefined
,
key
:
string
,
)
{
const
cached
=
await
cache
?.
get
(
key
)
const
cachedBlob
=
cached
?
getStoredBlob
(
{
key
,
metadata
:
cached
.
metadata
,
text
:
cached
.
text
}
)
:
undefined
if
(
cachedBlob
&&
!
isNegativeGithubDirectoryBlob
(
cachedBlob
)
)
{
return
cachedBlob
}
const
object
=
await
storage
.
get
(
key
)
if
(
!
object
)
{
return
undefined
}
const
text
=
await
object
.
text
(
)
const
stored
=
getStoredBlob
(
{
key
,
metadata
:
object
.
metadata
,
text
,
uploaded
:
object
.
uploaded
,
}
)
if
(
!
stored
)
{
return
undefined
}
await
writeBlobCache
(
cache
,
key
,
{
metadata
:
stored
.
metadata
,
text
:
stored
.
text
,
}
)
return
stored
}
async
function
writeBlobCache
(
cache
:
BlobStorageCache
|
undefined
,
key
:
string
,
entry
:
{
metadata
:
Record
<
string
,
string
>
text
:
string
}
,
)
{
try
{
await
cache
?.
put
(
key
,
entry
)
}
catch
{
// The runtime front cache is opportunistic.
}
}
async
function
deleteBlobCache
(
cache
:
BlobStorageCache
|
undefined
,
key
:
string
,
)
{
try
{
await
cache
?.
delete
(
key
)
}
catch
{
// The durable blob store remains authoritative.
}
}
function
pruneMemoryCache
(
)
{
const
totalEntries
=
memoryGitHubContent
.
size
+
memoryDocsArtifacts
.
size
if
(
totalEntries
<=
MAX_MEMORY_CACHE_ENTRIES
)
{
return
}
const
entries
:
Array
<
MemoryCacheEntry
>
=
[
...
Array
.
from
(
memoryGitHubContent
.
entries
(
)
)
.
map
(
(
[
key
,
record
]
)
:
MemoryCacheEntry
=>
(
{
cache
:
'content'
,
key
,
updatedAt
:
record
.
updatedAt
,
}
)
,
)
,
...
Array
.
from
(
memoryDocsArtifacts
.
entries
(
)
)
.
map
(
(
[
key
,
record
]
)
:
MemoryCacheEntry
=>
(
{
cache
:
'artifact'
,
key
,
updatedAt
:
record
.
updatedAt
,
}
)
,
)
,
]
.
sort
(
(
a
,
b
)
=>
a
.
updatedAt
.
getTime
(
)
-
b
.
updatedAt
.
getTime
(
)
)
for
(
const
entry
of
entries
.
slice
(
0
,
totalEntries
-
MAX_MEMORY_CACHE_ENTRIES
,
)
)
{
if
(
entry
.
cache
===
'content'
)
{
memoryGitHubContent
.
delete
(
entry
.
key
)
}
else
{
memoryDocsArtifacts
.
delete
(
entry
.
key
)
}
}
}
function
readStoredTextValue
(
row
:
GithubContentRecord
|
undefined
)
{
if
(
!
row
)
{
return
undefined
}
if
(
!
row
.
isPresent
)
{
return
null
}
return
typeof
row
.
textContent
===
'string'
?
row
.
textContent
:
undefined
}
function
readStoredJsonValue
<
T
>
(
row
:
GithubContentRecord
|
undefined
,
isValue
:
(
value
:
unknown
)
=>
value
is
T
,
)
{
if
(
!
row
)
{
return
undefined
}
if
(
!
row
.
isPresent
)
{
return
null
}
return
isValue
(
row
.
jsonContent
)
?
row
.
jsonContent
:
undefined
}
function
canUseStoredGithubContentValue
<
T
>
(
contentKind
:
ContentKind
,
value
:
CachedValue
<
T
>
,
)
:
value
is
T
|
null
{
if
(
value
===
undefined
)
{
return
false
}
// Directory metadata is derived from the GitHub API. A transient API failure
// used to poison examples with fresh null entries, hiding the file explorer.
// Revalidate those negative JSON entries on the next read-through.
return
!
(
contentKind
===
'dir'
&&
value
===
null
)
}
function
metadataMatches
(
metadata
:
Record
<
string
,
string
>
|
undefined
,
opts
:
{
gitRef
?:
string
repo
?:
string
}
,
)
{
if
(
!
metadata
)
{
return
false
}
if
(
opts
.
repo
&&
readMetadataString
(
metadata
,
'repo'
)
!==
opts
.
repo
)
{
return
false
}
if
(
opts
.
gitRef
&&
readMetadataString
(
metadata
,
'gitRef'
)
!==
opts
.
gitRef
)
{
return
false
}
return
true
}
async
function
listBlobObjects
(
storage
:
BlobStorage
,
prefix
:
string
)
{
const
objects
:
Array
<
BlobStorageListedObject
>
=
[
]
let
cursor
:
string
|
undefined
do
{
const
result
=
await
storage
.
list
(
{
cursor
,
limit
:
1000
,
prefix
,
}
)
objects
.
push
(
...
result
.
objects
)
cursor
=
result
.
truncated
?
result
.
cursor
:
undefined
}
while
(
cursor
)
return
objects
}
async
function
getListedObjectMetadata
(
storage
:
BlobStorage
,
object
:
BlobStorageListedObject
,
)
{
if
(
object
.
metadata
&&
Object
.
keys
(
object
.
metadata
)
.
length
>
0
)
{
return
object
.
metadata
}
const
stored
=
await
readBlob
(
storage
,
undefined
,
object
.
key
)
return
stored
?.
metadata
}
async
function
deleteBlobKeys
(
storage
:
BlobStorage
,
cache
:
BlobStorageCache
|
undefined
,
keys
:
Array
<
string
>
,
)
{
const
chunkSize
=
1000
for
(
let
index
=
0
;
index
<
keys
.
length
;
index
+=
chunkSize
)
{
const
chunk
=
keys
.
slice
(
index
,
index
+
chunkSize
)
await
storage
.
delete
(
chunk
)
await
Promise
.
all
(
chunk
.
map
(
(
key
)
=>
deleteBlobCache
(
cache
,
key
)
)
)
}
}
function
getStatsAccumulator
(
statsByRepo
:
Map
<
string
,
RepoStatsAccumulator
>
,
repo
:
string
,
)
{
const
existing
=
statsByRepo
.
get
(
repo
)
if
(
existing
)
{
return
existing
}
const
created
=
{
artifactEntries
:
0
,
contentEntries
:
0
,
lastUpdatedAt
:
null
,
refs
:
new
Set
<
string
>
(
)
,
repo
,
staleArtifactEntries
:
0
,
staleContentEntries
:
0
,
}
statsByRepo
.
set
(
repo
,
created
)
return
created
}
function
updateLastUpdatedAt
(
accumulator
:
RepoStatsAccumulator
,
updatedAt
:
Date
,
)
{
if
(
!
accumulator
.
lastUpdatedAt
||
updatedAt
.
getTime
(
)
>
accumulator
.
lastUpdatedAt
.
getTime
(
)
)
{
accumulator
.
lastUpdatedAt
=
updatedAt
}
}
function
addGithubContentStats
(
statsByRepo
:
Map
<
string
,
RepoStatsAccumulator
>
,
record
:
Pick
<
GithubContentRecord
,
'gitRef'
|
'repo'
|
'staleAt'
|
'updatedAt'
>
,
)
{
const
accumulator
=
getStatsAccumulator
(
statsByRepo
,
record
.
repo
)
accumulator
.
contentEntries
+=
1
accumulator
.
refs
.
add
(
record
.
gitRef
)
if
(
!
isFresh
(
record
.
staleAt
)
)
{
accumulator
.
staleContentEntries
+=
1
}
updateLastUpdatedAt
(
accumulator
,
record
.
updatedAt
)
}
function
addDocsArtifactStats
(
statsByRepo
:
Map
<
string
,
RepoStatsAccumulator
>
,
record
:
Pick
<
DocsArtifactRecord
,
'gitRef'
|
'repo'
|
'staleAt'
|
'updatedAt'
>
,
)
{
const
accumulator
=
getStatsAccumulator
(
statsByRepo
,
record
.
repo
)
accumulator
.
artifactEntries
+=
1
accumulator
.
refs
.
add
(
record
.
gitRef
)
if
(
!
isFresh
(
record
.
staleAt
)
)
{
accumulator
.
staleArtifactEntries
+=
1
}
updateLastUpdatedAt
(
accumulator
,
record
.
updatedAt
)
}
function
finalizeRepoStats
(
statsByRepo
:
Map
<
string
,
RepoStatsAccumulator
>
,
)
:
Array
<
DocsCacheRepoStats
>
{
return
Array
.
from
(
statsByRepo
.
values
(
)
)
.
map
(
(
repo
)
=>
{
const
totalEntries
=
repo
.
contentEntries
+
repo
.
artifactEntries
const
staleEntries
=
repo
.
staleContentEntries
+
repo
.
staleArtifactEntries
return
{
artifactEntries
:
repo
.
artifactEntries
,
cachedRefCount
:
repo
.
refs
.
size
,
contentEntries
:
repo
.
contentEntries
,
lastUpdatedAt
:
repo
.
lastUpdatedAt
?.
toISOString
(
)
??
null
,
repo
:
repo
.
repo
,
staleArtifactEntries
:
repo
.
staleArtifactEntries
,
staleContentEntries
:
repo
.
staleContentEntries
,
staleEntries
,
totalEntries
,
}
}
)
.
sort
(
(
a
,
b
)
=>
b
.
totalEntries
-
a
.
totalEntries
||
a
.
repo
.
localeCompare
(
b
.
repo
)
,
)
}
function
readGithubContentStatsFromMetadata
(
metadata
:
Record
<
string
,
string
>
|
undefined
,
)
{
const
repo
=
readMetadataString
(
metadata
,
'repo'
)
const
gitRef
=
readMetadataString
(
metadata
,
'gitRef'
)
const
staleAt
=
readMetadataDate
(
metadata
,
'staleAt'
)
const
updatedAt
=
readMetadataDate
(
metadata
,
'updatedAt'
)
const
isPresent
=
readMetadataBoolean
(
metadata
,
'isPresent'
)
if
(
!
repo
||
!
gitRef
||
!
staleAt
||
!
updatedAt
||
isPresent
===
undefined
)
{
return
undefined
}
return
{
gitRef
,
isPresent
,
repo
,
staleAt
,
updatedAt
,
}
}
function
readDocsArtifactStatsFromMetadata
(
metadata
:
Record
<
string
,
string
>
|
undefined
,
)
{
const
repo
=
readMetadataString
(
metadata
,
'repo'
)
const
gitRef
=
readMetadataString
(
metadata
,
'gitRef'
)
const
staleAt
=
readMetadataDate
(
metadata
,
'staleAt'
)
const
updatedAt
=
readMetadataDate
(
metadata
,
'updatedAt'
)
if
(
!
repo
||
!
gitRef
||
!
staleAt
||
!
updatedAt
)
{
return
undefined
}
return
{
gitRef
,
repo
,
staleAt
,
updatedAt
,
}
}
function
createMemoryBackend
(
)
:
CacheBackend
{
return
{
async
getDocsArtifact
(
key
)
{
return
memoryDocsArtifacts
.
get
(
getDocsArtifactBlobKey
(
key
)
)
}
,
async
getGitHubContent
(
key
)
{
return
memoryGitHubContent
.
get
(
getGitHubContentBlobKey
(
key
)
)
}
,
async
listRepoStats
(
)
{
const
statsByRepo
=
new
Map
<
string
,
RepoStatsAccumulator
>
(
)
for
(
const
record
of
memoryGitHubContent
.
values
(
)
)
{
addGithubContentStats
(
statsByRepo
,
record
)
}
for
(
const
record
of
memoryDocsArtifacts
.
values
(
)
)
{
addDocsArtifactStats
(
statsByRepo
,
record
)
}
return
finalizeRepoStats
(
statsByRepo
)
}
,
async
markDocsArtifactsStale
(
opts
)
{
let
count
=
0
for
(
const
[
key
,
record
]
of
memoryDocsArtifacts
.
entries
(
)
)
{
if
(
!
metadataMatches
(
getDocsArtifactMetadata
(
record
)
,
opts
)
)
{
continue
}
memoryDocsArtifacts
.
set
(
key
,
{
...
record
,
staleAt
:
new
Date
(
0
)
,
}
)
count
+=
1
}
return
count
}
,
async
markGitHubContentStale
(
opts
)
{
let
count
=
0
for
(
const
[
key
,
record
]
of
memoryGitHubContent
.
entries
(
)
)
{
if
(
!
metadataMatches
(
getGithubContentMetadata
(
record
)
,
opts
)
)
{
continue
}
memoryGitHubContent
.
set
(
key
,
{
...
record
,
staleAt
:
new
Date
(
0
)
,
}
)
count
+=
1
}
return
count
}
,
async
pruneStaleCacheRows
(
opts
)
{
const
maxAgeMs
=
opts
.
maxAgeMs
??
DEFAULT_PRUNE_MAX_AGE_MS
const
negativeMaxAgeMs
=
opts
.
negativeMaxAgeMs
??
DEFAULT_NEGATIVE_PRUNE_MAX_AGE_MS
const
cutoff
=
new
Date
(
Date
.
now
(
)
-
maxAgeMs
)
const
negativeCutoff
=
new
Date
(
Date
.
now
(
)
-
negativeMaxAgeMs
)
let
githubContentDeleted
=
0
let
githubContentNegativesDeleted
=
0
let
docsArtifactDeleted
=
0
for
(
const
[
key
,
record
]
of
memoryGitHubContent
.
entries
(
)
)
{
const
shouldDeleteByAge
=
record
.
updatedAt
<
cutoff
const
shouldDeleteNegative
=
!
record
.
isPresent
&&
record
.
updatedAt
<
negativeCutoff
if
(
!
shouldDeleteByAge
&&
!
shouldDeleteNegative
)
{
continue
}
memoryGitHubContent
.
delete
(
key
)
githubContentDeleted
+=
1
if
(
shouldDeleteNegative
)
{
githubContentNegativesDeleted
+=
1
}
}
for
(
const
[
key
,
record
]
of
memoryDocsArtifacts
.
entries
(
)
)
{
if
(
record
.
updatedAt
>=
cutoff
)
{
continue
}
memoryDocsArtifacts
.
delete
(
key
)
docsArtifactDeleted
+=
1
}
return
{
cutoff
,
docsArtifactDeleted
,
githubContentDeleted
,
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL