@peerbit/document 6.0.5 → 6.0.7-a4f88b6

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 (47) hide show
  1. package/README.md +2 -2
  2. package/dist/benchmark/index.d.ts +2 -0
  3. package/dist/benchmark/index.d.ts.map +1 -0
  4. package/dist/benchmark/index.js +126 -0
  5. package/dist/benchmark/index.js.map +1 -0
  6. package/dist/benchmark/replication.d.ts +2 -0
  7. package/dist/benchmark/replication.d.ts.map +1 -0
  8. package/dist/benchmark/replication.js +174 -0
  9. package/dist/benchmark/replication.js.map +1 -0
  10. package/dist/src/constants.d.ts +2 -0
  11. package/dist/src/constants.d.ts.map +1 -0
  12. package/dist/src/constants.js +2 -0
  13. package/dist/src/constants.js.map +1 -0
  14. package/dist/src/index.d.ts +5 -0
  15. package/dist/src/index.d.ts.map +1 -0
  16. package/dist/src/index.js +5 -0
  17. package/dist/src/index.js.map +1 -0
  18. package/dist/src/program.d.ts +90 -0
  19. package/dist/src/program.d.ts.map +1 -0
  20. package/{lib/esm/document-store.js → dist/src/program.js} +141 -109
  21. package/dist/src/program.js.map +1 -0
  22. package/dist/src/search.d.ts +118 -0
  23. package/dist/src/search.d.ts.map +1 -0
  24. package/{lib/esm/document-index.js → dist/src/search.js} +247 -447
  25. package/dist/src/search.js.map +1 -0
  26. package/package.json +69 -43
  27. package/src/constants.ts +1 -0
  28. package/src/index.ts +4 -3
  29. package/src/{document-store.ts → program.ts} +216 -183
  30. package/src/search.ts +997 -0
  31. package/LICENSE +0 -202
  32. package/lib/esm/document-index.d.ts +0 -147
  33. package/lib/esm/document-index.js.map +0 -1
  34. package/lib/esm/document-store.d.ts +0 -72
  35. package/lib/esm/document-store.js.map +0 -1
  36. package/lib/esm/index.d.ts +0 -3
  37. package/lib/esm/index.js +0 -4
  38. package/lib/esm/index.js.map +0 -1
  39. package/lib/esm/query.d.ts +0 -191
  40. package/lib/esm/query.js +0 -615
  41. package/lib/esm/query.js.map +0 -1
  42. package/lib/esm/utils.d.ts +0 -3
  43. package/lib/esm/utils.js +0 -12
  44. package/lib/esm/utils.js.map +0 -1
  45. package/src/document-index.ts +0 -1268
  46. package/src/query.ts +0 -525
  47. package/src/utils.ts +0 -17
package/src/query.ts DELETED
@@ -1,525 +0,0 @@
1
- import {
2
- AbstractType,
3
- deserialize,
4
- field,
5
- fixedArray,
6
- serialize,
7
- variant,
8
- vec
9
- } from "@dao-xyz/borsh";
10
-
11
- import { asString } from "./utils.js";
12
- import { randomBytes, sha256Base64Sync } from "@peerbit/crypto";
13
-
14
- export enum Compare {
15
- Equal = 0,
16
- Greater = 1,
17
- GreaterOrEqual = 2,
18
- Less = 3,
19
- LessOrEqual = 4
20
- }
21
- export const compare = (
22
- test: bigint | number,
23
- compare: Compare,
24
- value: bigint | number
25
- ) => {
26
- switch (compare) {
27
- case Compare.Equal:
28
- return test == value; // == because with want bigint == number at some cases
29
- case Compare.Greater:
30
- return test > value;
31
- case Compare.GreaterOrEqual:
32
- return test >= value;
33
- case Compare.Less:
34
- return test < value;
35
- case Compare.LessOrEqual:
36
- return test <= value;
37
- default:
38
- console.warn("Unexpected compare");
39
- return false;
40
- }
41
- };
42
-
43
- /// ----- QUERY -----
44
-
45
- export abstract class Query {
46
- clone() {
47
- return deserialize(serialize(this), this.constructor) as this;
48
- }
49
- }
50
-
51
- export enum SortDirection {
52
- ASC = 0,
53
- DESC = 1
54
- }
55
-
56
- @variant(0)
57
- export class Sort {
58
- @field({ type: vec("string") })
59
- key: string[];
60
-
61
- @field({ type: "u8" })
62
- direction: SortDirection;
63
-
64
- constructor(properties: {
65
- key: string[] | string;
66
- direction?: SortDirection;
67
- }) {
68
- this.key = Array.isArray(properties.key)
69
- ? properties.key
70
- : [properties.key];
71
- this.direction = properties.direction || SortDirection.ASC;
72
- }
73
- }
74
-
75
- export abstract class AbstractSearchRequest {}
76
-
77
- /**
78
- * Search with query and collect with sort conditionss
79
- */
80
-
81
- const toArray = <T>(arr: T | T[] | undefined) =>
82
- (arr ? (Array.isArray(arr) ? arr : [arr]) : undefined) || [];
83
-
84
- @variant(0)
85
- export class SearchRequest extends AbstractSearchRequest {
86
- @field({ type: fixedArray("u8", 32) })
87
- id: Uint8Array; // Session id
88
-
89
- @field({ type: vec(Query) })
90
- query: Query[];
91
-
92
- @field({ type: vec(Sort) })
93
- sort: Sort[];
94
-
95
- @field({ type: "u32" })
96
- fetch: number;
97
-
98
- constructor(props?: { query?: Query[] | Query; sort?: Sort[] | Sort }) {
99
- super();
100
- this.id = randomBytes(32);
101
- this.query = toArray(props?.query);
102
- this.sort = toArray(props?.sort);
103
- this.fetch = 1;
104
- }
105
-
106
- private _idString: string;
107
- get idString(): string {
108
- return this._idString || (this._idString = sha256Base64Sync(this.id));
109
- }
110
- }
111
-
112
- /**
113
- * Collect documents from peers using 'collect' session ids. This is used for distributed sorting internally
114
- */
115
- @variant(2)
116
- export class CollectNextRequest extends AbstractSearchRequest {
117
- @field({ type: fixedArray("u8", 32) })
118
- id: Uint8Array; // collect with id
119
-
120
- @field({ type: "u32" })
121
- amount: number; // number of documents to ask for
122
-
123
- constructor(properties: { id: Uint8Array; amount: number }) {
124
- super();
125
- this.id = properties.id;
126
- this.amount = properties.amount;
127
- }
128
-
129
- private _idString: string;
130
- get idString(): string {
131
- return this._idString || (this._idString = sha256Base64Sync(this.id));
132
- }
133
- }
134
-
135
- @variant(3)
136
- export class CloseIteratorRequest extends AbstractSearchRequest {
137
- @field({ type: fixedArray("u8", 32) })
138
- id: Uint8Array; // collect with id
139
-
140
- constructor(properties: { id: Uint8Array }) {
141
- super();
142
- this.id = properties.id;
143
- }
144
-
145
- private _idString: string;
146
- get idString(): string {
147
- return this._idString || (this._idString = sha256Base64Sync(this.id));
148
- }
149
- }
150
-
151
- @variant(1)
152
- export abstract class LogicalQuery extends Query {}
153
-
154
- @variant(0)
155
- export class And extends LogicalQuery {
156
- @field({ type: vec(Query) })
157
- and: Query[];
158
-
159
- constructor(and: Query[]) {
160
- super();
161
- this.and = and;
162
- }
163
- }
164
-
165
- @variant(1)
166
- export class Or extends LogicalQuery {
167
- @field({ type: vec(Query) })
168
- or: Query[];
169
-
170
- constructor(or: Query[]) {
171
- super();
172
- this.or = or;
173
- }
174
- }
175
-
176
- @variant(2)
177
- export abstract class StateQuery extends Query {}
178
-
179
- @variant(1)
180
- export class StateFieldQuery extends StateQuery {
181
- @field({ type: vec("string") })
182
- key: string[];
183
-
184
- constructor(props: { key: string[] | string }) {
185
- super();
186
- this.key = Array.isArray(props.key) ? props.key : [props.key];
187
- }
188
- }
189
-
190
- export abstract class PrimitiveValue {}
191
-
192
- @variant(0)
193
- export class StringValue extends PrimitiveValue {
194
- @field({ type: "string" })
195
- string: string;
196
-
197
- constructor(string: string) {
198
- super();
199
- this.string = string;
200
- }
201
- }
202
-
203
- @variant(1)
204
- abstract class NumberValue extends PrimitiveValue {
205
- abstract get value(): number | bigint;
206
- }
207
-
208
- @variant(0)
209
- abstract class IntegerValue extends NumberValue {}
210
-
211
- @variant(0)
212
- export class UnsignedIntegerValue extends IntegerValue {
213
- @field({ type: "u32" })
214
- number: number;
215
-
216
- constructor(number: number) {
217
- super();
218
- if (
219
- Number.isInteger(number) === false ||
220
- number > 4294967295 ||
221
- number < 0
222
- ) {
223
- throw new Error("Number is not u32");
224
- }
225
- this.number = number;
226
- }
227
-
228
- get value() {
229
- return this.number;
230
- }
231
- }
232
-
233
- @variant(1)
234
- export class BigUnsignedIntegerValue extends IntegerValue {
235
- @field({ type: "u64" })
236
- number: bigint;
237
-
238
- constructor(number: bigint) {
239
- super();
240
- if (number > 18446744073709551615n || number < 0) {
241
- throw new Error("Number is not u32");
242
- }
243
- this.number = number;
244
- }
245
- get value() {
246
- return this.number;
247
- }
248
- }
249
-
250
- @variant(1)
251
- export class ByteMatchQuery extends StateFieldQuery {
252
- @field({ type: Uint8Array })
253
- value: Uint8Array;
254
-
255
- @field({ type: "u8" })
256
- private _reserved: number; // Replcate MemoryCompare query with this?
257
-
258
- constructor(props: { key: string[] | string; value: Uint8Array }) {
259
- super(props);
260
- this.value = props.value;
261
- this._reserved = 0;
262
- }
263
-
264
- _valueString: string;
265
- /**
266
- * value `asString`
267
- */
268
- get valueString() {
269
- return this._valueString ?? (this._valueString = asString(this.value));
270
- }
271
- }
272
-
273
- export enum StringMatchMethod {
274
- "exact" = 0,
275
- "prefix" = 1,
276
- "contains" = 2
277
- }
278
-
279
- @variant(2)
280
- export class StringMatch extends StateFieldQuery {
281
- @field({ type: "string" })
282
- value: string;
283
-
284
- @field({ type: "u8" })
285
- method: StringMatchMethod;
286
-
287
- @field({ type: "bool" })
288
- caseInsensitive: boolean;
289
-
290
- constructor(props: {
291
- key: string[] | string;
292
- value: string;
293
- method?: StringMatchMethod;
294
- caseInsensitive?: boolean;
295
- }) {
296
- super(props);
297
- this.value = props.value;
298
- this.method = props.method ?? StringMatchMethod.exact;
299
- this.caseInsensitive = props.caseInsensitive ?? false;
300
- }
301
- }
302
-
303
- @variant(3)
304
- export class IntegerCompare extends StateFieldQuery {
305
- @field({ type: "u8" })
306
- compare: Compare;
307
-
308
- @field({ type: IntegerValue })
309
- value: IntegerValue;
310
-
311
- constructor(props: {
312
- key: string[] | string;
313
- value: bigint | number | IntegerValue;
314
- compare: Compare;
315
- }) {
316
- super(props);
317
- if (props.value instanceof IntegerValue) {
318
- this.value = props.value;
319
- } else {
320
- if (typeof props.value === "bigint") {
321
- this.value = new BigUnsignedIntegerValue(props.value);
322
- } else {
323
- this.value = new UnsignedIntegerValue(props.value);
324
- }
325
- }
326
-
327
- this.compare = props.compare;
328
- }
329
- }
330
-
331
- @variant(4)
332
- export class MissingField extends StateFieldQuery {
333
- constructor(props: { key: string[] | string }) {
334
- super(props);
335
- }
336
- }
337
-
338
- @variant(5)
339
- export class BoolQuery extends StateFieldQuery {
340
- @field({ type: "bool" })
341
- value: boolean;
342
-
343
- constructor(props: { key: string[] | string; value: boolean }) {
344
- super(props);
345
- this.value = props.value;
346
- }
347
- }
348
-
349
- // TODO MemoryCompareQuery can be replaces with ByteMatchQuery? Or Nesteed Queries + ByteMatchQuery?
350
- /* @variant(0)
351
- export class MemoryCompare {
352
- @field({ type: Uint8Array })
353
- bytes: Uint8Array;
354
-
355
- @field({ type: "u64" })
356
- offset: bigint;
357
-
358
- constructor(opts?: { bytes: Uint8Array; offset: bigint }) {
359
- if (opts) {
360
- this.bytes = opts.bytes;
361
- this.offset = opts.offset;
362
- }
363
- }
364
- }
365
-
366
- @variant(4)
367
- export class MemoryCompareQuery extends Query {
368
- @field({ type: vec(MemoryCompare) })
369
- compares: MemoryCompare[];
370
-
371
- constructor(opts?: { compares: MemoryCompare[] }) {
372
- super();
373
- if (opts) {
374
- this.compares = opts.compares;
375
- }
376
- }
377
- } */
378
-
379
- /// ----- RESULTS -----
380
-
381
- export abstract class Result {}
382
-
383
- @variant(0)
384
- export class Context {
385
- @field({ type: "u64" })
386
- created: bigint;
387
-
388
- @field({ type: "u64" })
389
- modified: bigint;
390
-
391
- @field({ type: "string" })
392
- head: string;
393
-
394
- @field({ type: "string" })
395
- gid: string;
396
-
397
- constructor(properties: {
398
- created: bigint;
399
- modified: bigint;
400
- head: string;
401
- gid: string;
402
- }) {
403
- this.created = properties.created;
404
- this.modified = properties.modified;
405
- this.head = properties.head;
406
- this.gid = properties.gid;
407
- }
408
- }
409
-
410
- @variant(0)
411
- export class ResultWithSource<T> extends Result {
412
- @field({ type: Uint8Array })
413
- _source: Uint8Array;
414
-
415
- @field({ type: Context })
416
- context: Context;
417
-
418
- _type: AbstractType<T>;
419
- constructor(opts: { source: Uint8Array; context: Context; value?: T }) {
420
- super();
421
- this._source = opts.source;
422
- this.context = opts.context;
423
- this._value = opts.value;
424
- }
425
-
426
- init(type: AbstractType<T>) {
427
- this._type = type;
428
- }
429
-
430
- _value?: T;
431
- get value(): T {
432
- if (this._value) {
433
- return this._value;
434
- }
435
- if (!this._source) {
436
- throw new Error("Missing source binary");
437
- }
438
- this._value = deserialize(this._source, this._type);
439
- return this._value;
440
- }
441
- }
442
-
443
- export abstract class AbstractSearchResult<T> {}
444
-
445
- @variant(0)
446
- export class Results<T> extends AbstractSearchResult<T> {
447
- @field({ type: vec(ResultWithSource) })
448
- results: ResultWithSource<T>[];
449
-
450
- @field({ type: "u64" })
451
- kept: bigint; // how many results that were not sent, but can be collected later
452
-
453
- constructor(properties: { results: ResultWithSource<T>[]; kept: bigint }) {
454
- super();
455
- this.kept = properties.kept;
456
- this.results = properties.results;
457
- }
458
- }
459
-
460
- @variant(1)
461
- export class NoAccess extends AbstractSearchResult<any> {}
462
-
463
- /* @variant(5)
464
- export class LogQuery extends Query { } */
465
-
466
- /**
467
- * Find logs that can be decrypted by certain keys
468
- */
469
- /*
470
- @variant(0)
471
- export class EntryEncryptedByQuery
472
- extends LogQuery
473
- implements
474
- EntryEncryptionTemplate<
475
- X25519PublicKey[],
476
- X25519PublicKey[],
477
- X25519PublicKey[],
478
- X25519PublicKey[]
479
- >
480
- {
481
- @field({ type: vec(X25519PublicKey) })
482
- meta: X25519PublicKey[];
483
-
484
- @field({ type: vec(X25519PublicKey) })
485
- payload: X25519PublicKey[];
486
-
487
- @field({ type: vec(X25519PublicKey) })
488
- next: X25519PublicKey[];
489
-
490
- @field({ type: vec(X25519PublicKey) })
491
- signatures: X25519PublicKey[];
492
-
493
- constructor(properties?: {
494
- meta: X25519PublicKey[];
495
- next: X25519PublicKey[];
496
- payload: X25519PublicKey[];
497
- signatures: X25519PublicKey[];
498
- }) {
499
- super();
500
- if (properties) {
501
- this.metadata = properties.metadata;
502
- this.payload = properties.payload;
503
- this.next = properties.next;
504
- this.signatures = properties.signatures;
505
- }
506
- }
507
- }
508
-
509
- @variant(1)
510
- export class SignedByQuery extends LogQuery {
511
- @field({ type: vec(PublicSignKey) })
512
- _publicKeys: PublicSignKey[];
513
-
514
- constructor(properties: { publicKeys: PublicSignKey[] }) {
515
- super();
516
- if (properties) {
517
- this._publicKeys = properties.publicKeys;
518
- }
519
- }
520
-
521
- get publicKeys() {
522
- return this._publicKeys;
523
- }
524
- }
525
- */
package/src/utils.ts DELETED
@@ -1,17 +0,0 @@
1
- import { toBase64 } from "@peerbit/crypto";
2
-
3
- export type Keyable = string | Uint8Array;
4
- export const asString = (obj: Keyable) =>
5
- typeof obj === "string" ? obj : toBase64(obj);
6
- export const checkKeyable = (obj: Keyable) => {
7
- if (obj == null) {
8
- throw new Error(
9
- `The provided key value is null or undefined, expecting string or Uint8array`
10
- );
11
- }
12
- if (typeof obj === "string" || obj instanceof Uint8Array) {
13
- return;
14
- }
15
-
16
- throw new Error("Key is not string or Uint8array, key value: " + typeof obj);
17
- };