@@ -47,6 +47,41 @@ const UUID_KEY = '_local_uuid';
47
47
48
48
const MD5_PREFIX = 'md5-' ;
49
49
50
+ const nullBuffer = Buffer . alloc ( 1 ) ;
51
+ const ffBuffer = Buffer . from ( [ 0xff ] ) ;
52
+ const startOfKeyspace = Symbol ( 'start of keyspace' ) ;
53
+ const endOfKeyspace = Symbol ( 'end of keyspace' ) ;
54
+
55
+ /**
56
+ * A custom encoder is needed due to https://github.com/josephg/node-foundationdb/issues/78
57
+ * @type {import('foundationdb/dist/lib/transformer.js').Transformer<
58
+ * import('./types.js').Id | typeof startOfKeyspace | typeof endOfKeyspace,
59
+ * import('./types.js').Id
60
+ * >}
61
+ */
62
+ const idEncoder = {
63
+ pack ( id ) {
64
+ if ( id === startOfKeyspace ) {
65
+ return nullBuffer ;
66
+ }
67
+
68
+ if ( id === endOfKeyspace ) {
69
+ return ffBuffer ;
70
+ }
71
+
72
+ const idBuffer = Buffer . from ( id , 'binary' ) ;
73
+
74
+ return Buffer . concat ( [ nullBuffer , idBuffer ] ) ;
75
+ } ,
76
+ unpack ( idBuffer ) {
77
+ if ( idBuffer [ 0 ] !== 0 && idBuffer . length > 1 ) {
78
+ throw new Error ( 'Invalid ID' ) ;
79
+ }
80
+
81
+ return /** @type {import('./types.js').Id } */ ( idBuffer . subarray ( 1 ) . toString ( 'binary' ) ) ;
82
+ }
83
+ } ;
84
+
50
85
/**
51
86
* @type {import('foundationdb/dist/lib/transformer.js').Transformer<number, number> }
52
87
*/
@@ -125,11 +160,11 @@ export default function FoundationdbAdapter(api, { db, name, revs_limit: revLimi
125
160
126
161
const stores = {
127
162
/* eslint-disable max-len */
128
- docStore : /** @type {fdb.Subspace<import('./types.js').Id, import('./types.js').Id, import('./types.js').Metadata, import('./types.js').Metadata> } */ (
129
- /** @type { unknown } */ ( subspace . at ( fdb . tuple . pack ( DOC_STORE ) , undefined , fdb . encoders . json ) )
163
+ docStore : /** @type {fdb.Subspace<import('./types.js').Id | typeof startOfKeyspace | typeof endOfKeyspace , import('./types.js').Id, import('./types.js').Metadata, import('./types.js').Metadata> } */ (
164
+ subspace . at ( fdb . tuple . pack ( DOC_STORE ) , idEncoder , fdb . encoders . json )
130
165
) ,
131
166
bySeqStore : /** @type {fdb.Subspace<number, number, import('./types.js').Doc, import('./types.js').Doc> } */ (
132
- /** @type { unknown } */ ( subspace . at ( fdb . tuple . pack ( BY_SEQ_STORE ) , seqEncoder , fdb . encoders . json ) )
167
+ subspace . at ( fdb . tuple . pack ( BY_SEQ_STORE ) , seqEncoder , fdb . encoders . json )
133
168
) ,
134
169
attachmentStore : /** @type {fdb.Subspace<import('./types.js').Digest, import('./types.js').Digest, import('./types.js').AttachmentRef, import('./types.js').AttachmentRef> } */ (
135
170
/** @type {unknown } */ ( subspace . at ( fdb . tuple . pack ( ATTACHMENT_STORE ) , undefined , fdb . encoders . json ) )
@@ -640,15 +675,15 @@ export default function FoundationdbAdapter(api, { db, name, revs_limit: revLimi
640
675
// eslint-disable-next-line no-nested-ternary
641
676
const start = gte !== undefined
642
677
? ( ! inclusiveEnd && reverse ? ks . firstGreaterThan ( gte ) : gte )
643
- : '' ;
678
+ : startOfKeyspace ;
644
679
// eslint-disable-next-line no-nested-ternary
645
680
const end = lte !== undefined
646
681
? ( ! inclusiveEnd && ! reverse ? lte : ks . firstGreaterThan ( lte ) )
647
- : '\xff' ; // TODO: make sure that ID-s starting with 0xff are also included
682
+ : endOfKeyspace ;
648
683
649
684
const it = tn . at ( stores . docStore ) . getRange (
650
- /** @type { any } */ ( start ) ,
651
- /** @type { any } */ ( end ) ,
685
+ start ,
686
+ end ,
652
687
reverse ? { reverse : true } : undefined
653
688
) ;
654
689
0 commit comments