cojson 0.3.7 → 0.4.0

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 (78) hide show
  1. package/dist/coValue.d.ts +7 -12
  2. package/dist/coValue.js.map +1 -1
  3. package/dist/coValueCore.d.ts +9 -4
  4. package/dist/coValueCore.js +69 -33
  5. package/dist/coValueCore.js.map +1 -1
  6. package/dist/coValues/account.d.ts +62 -0
  7. package/dist/{account.js → coValues/account.js} +19 -11
  8. package/dist/coValues/account.js.map +1 -0
  9. package/dist/coValues/coList.d.ts +19 -19
  10. package/dist/coValues/coList.js.map +1 -1
  11. package/dist/coValues/coMap.d.ts +31 -23
  12. package/dist/coValues/coMap.js +4 -6
  13. package/dist/coValues/coMap.js.map +1 -1
  14. package/dist/coValues/coStream.d.ts +19 -19
  15. package/dist/coValues/coStream.js.map +1 -1
  16. package/dist/{group.d.ts → coValues/group.d.ts} +27 -38
  17. package/dist/{group.js → coValues/group.js} +69 -73
  18. package/dist/coValues/group.js.map +1 -0
  19. package/dist/ids.d.ts +1 -1
  20. package/dist/index.d.ts +15 -11
  21. package/dist/index.js +10 -5
  22. package/dist/index.js.map +1 -1
  23. package/dist/localNode.d.ts +20 -7
  24. package/dist/localNode.js +74 -39
  25. package/dist/localNode.js.map +1 -1
  26. package/dist/media.d.ts +1 -1
  27. package/dist/permissions.d.ts +1 -1
  28. package/dist/permissions.js +43 -22
  29. package/dist/permissions.js.map +1 -1
  30. package/dist/queriedCoValues/queriedAccount.d.ts +13 -0
  31. package/dist/queriedCoValues/queriedAccount.js +24 -0
  32. package/dist/queriedCoValues/queriedAccount.js.map +1 -0
  33. package/dist/queriedCoValues/queriedCoList.d.ts +10 -10
  34. package/dist/queriedCoValues/queriedCoList.js +11 -15
  35. package/dist/queriedCoValues/queriedCoList.js.map +1 -1
  36. package/dist/queriedCoValues/queriedCoMap.d.ts +14 -21
  37. package/dist/queriedCoValues/queriedCoMap.js +27 -28
  38. package/dist/queriedCoValues/queriedCoMap.js.map +1 -1
  39. package/dist/queriedCoValues/queriedCoStream.d.ts +9 -9
  40. package/dist/queriedCoValues/queriedCoStream.js +44 -20
  41. package/dist/queriedCoValues/queriedCoStream.js.map +1 -1
  42. package/dist/queriedCoValues/queriedGroup.d.ts +29 -0
  43. package/dist/queriedCoValues/queriedGroup.js +54 -0
  44. package/dist/queriedCoValues/queriedGroup.js.map +1 -0
  45. package/dist/queries.d.ts +40 -9
  46. package/dist/queries.js +104 -39
  47. package/dist/queries.js.map +1 -1
  48. package/dist/tests/testUtils.d.ts +15 -7
  49. package/dist/tests/testUtils.js +16 -17
  50. package/dist/tests/testUtils.js.map +1 -1
  51. package/package.json +2 -2
  52. package/src/coValue.ts +12 -31
  53. package/src/coValueCore.ts +100 -40
  54. package/src/{account.ts → coValues/account.ts} +46 -27
  55. package/src/coValues/coList.ts +24 -28
  56. package/src/coValues/coMap.ts +42 -68
  57. package/src/coValues/coStream.ts +20 -26
  58. package/src/{group.ts → coValues/group.ts} +121 -141
  59. package/src/ids.ts +1 -1
  60. package/src/index.ts +25 -10
  61. package/src/localNode.ts +180 -77
  62. package/src/media.ts +1 -1
  63. package/src/permissions.ts +67 -36
  64. package/src/queriedCoValues/queriedAccount.ts +40 -0
  65. package/src/queriedCoValues/queriedCoList.ts +22 -30
  66. package/src/queriedCoValues/queriedCoMap.ts +60 -72
  67. package/src/queriedCoValues/queriedCoStream.ts +67 -37
  68. package/src/queriedCoValues/queriedGroup.ts +90 -0
  69. package/src/queries.ts +181 -60
  70. package/src/tests/account.test.ts +14 -9
  71. package/src/tests/coValueCore.test.ts +2 -2
  72. package/src/tests/permissions.test.ts +351 -242
  73. package/src/tests/queries.test.ts +98 -79
  74. package/src/tests/sync.test.ts +11 -11
  75. package/src/tests/testUtils.ts +16 -18
  76. package/dist/account.d.ts +0 -58
  77. package/dist/account.js.map +0 -1
  78. package/dist/group.js.map +0 -1
@@ -2,25 +2,22 @@ import { JsonObject, JsonValue } from "../jsonValue.js";
2
2
  import { AgentID, TransactionID } from "../ids.js";
3
3
  import { CoID, CoValue, isCoValue } from "../coValue.js";
4
4
  import { CoValueCore, accountOrAgentIDfromSessionID } from "../coValueCore.js";
5
- import { AccountID } from "../account.js";
6
- import { Group } from "../group.js";
5
+ import { AccountID } from "./account.js";
7
6
  import { parseJSON } from "../jsonStringify.js";
7
+ import { Group } from "./group.js";
8
8
 
9
- type MapOp<K extends string, V extends JsonValue | CoValue | undefined> = {
9
+ type MapOp<K extends string, V extends JsonValue | undefined> = {
10
10
  txID: TransactionID;
11
11
  madeAt: number;
12
12
  changeIdx: number;
13
13
  } & MapOpPayload<K, V>;
14
14
  // TODO: add after TransactionID[] for conflicts/ordering
15
15
 
16
- export type MapOpPayload<
17
- K extends string,
18
- V extends JsonValue | CoValue | undefined
19
- > =
16
+ export type MapOpPayload<K extends string, V extends JsonValue | undefined> =
20
17
  | {
21
18
  op: "set";
22
19
  key: K;
23
- value: V extends CoValue ? CoID<V> : Exclude<V, CoValue>;
20
+ value: V;
24
21
  }
25
22
  | {
26
23
  op: "del";
@@ -28,8 +25,10 @@ export type MapOpPayload<
28
25
  };
29
26
 
30
27
  export class CoMapView<
31
- Shape extends { [key: string]: JsonValue | CoValue | undefined },
32
- Meta extends JsonObject | null = null
28
+ Shape extends { [key: string]: JsonValue | undefined } = {
29
+ [key: string]: JsonValue | undefined;
30
+ },
31
+ Meta extends JsonObject | null = JsonObject | null
33
32
  > implements CoValue
34
33
  {
35
34
  /** @category 6. Meta */
@@ -48,16 +47,17 @@ export class CoMapView<
48
47
  readonly _shape!: Shape;
49
48
 
50
49
  /** @internal */
51
- constructor(core: CoValueCore) {
50
+ constructor(
51
+ core: CoValueCore,
52
+ options?: { ignorePrivateTransactions: true }
53
+ ) {
52
54
  this.id = core.id as CoID<this>;
53
55
  this.core = core;
54
56
  this.ops = {};
55
57
 
56
- for (const {
57
- txID,
58
- changes,
59
- madeAt,
60
- } of core.getValidSortedTransactions()) {
58
+ for (const { txID, changes, madeAt } of core.getValidSortedTransactions(
59
+ options
60
+ )) {
61
61
  for (const [changeIdx, changeUntyped] of parseJSON(
62
62
  changes
63
63
  ).entries()) {
@@ -121,13 +121,11 @@ export class CoMapView<
121
121
  * Get all keys currently in the map.
122
122
  *
123
123
  * @category 1. Reading */
124
- keys(): (keyof Shape & string)[] {
125
- const keys = Object.keys(this.ops) as (keyof Shape & string)[];
124
+ keys<K extends (keyof Shape & string) = (keyof Shape & string)>(): K[] {
125
+ const keys = Object.keys(this.ops) as K[];
126
126
 
127
127
  if (this.atTimeFilter) {
128
- return keys.filter((key) => {
129
- this.timeFilteredOps(key)?.length;
130
- });
128
+ return keys.filter((key) => this.timeFilteredOps(key)?.length);
131
129
  } else {
132
130
  return keys;
133
131
  }
@@ -138,13 +136,7 @@ export class CoMapView<
138
136
  *
139
137
  * @category 1. Reading
140
138
  **/
141
- get<K extends keyof Shape & string>(
142
- key: K
143
- ):
144
- | (Shape[K] extends CoValue
145
- ? CoID<Shape[K]>
146
- : Exclude<Shape[K], CoValue>)
147
- | undefined {
139
+ get<K extends keyof Shape & string>(key: K): Shape[K] | undefined {
148
140
  const ops = this.timeFilteredOps(key);
149
141
  if (!ops) {
150
142
  return undefined;
@@ -164,14 +156,10 @@ export class CoMapView<
164
156
 
165
157
  /** @category 1. Reading */
166
158
  asObject(): {
167
- [K in keyof Shape & string]: Shape[K] extends CoValue
168
- ? CoID<Shape[K]>
169
- : Exclude<Shape[K], CoValue>;
159
+ [K in keyof Shape & string]: Shape[K];
170
160
  } {
171
161
  const object: Partial<{
172
- [K in keyof Shape & string]: Shape[K] extends CoValue
173
- ? CoID<Shape[K]>
174
- : Exclude<Shape[K], CoValue>;
162
+ [K in keyof Shape & string]: Shape[K];
175
163
  }> = {};
176
164
 
177
165
  for (const key of this.keys()) {
@@ -182,17 +170,13 @@ export class CoMapView<
182
170
  }
183
171
 
184
172
  return object as {
185
- [K in keyof Shape & string]: Shape[K] extends CoValue
186
- ? CoID<Shape[K]>
187
- : Exclude<Shape[K], CoValue>;
173
+ [K in keyof Shape & string]: Shape[K];
188
174
  };
189
175
  }
190
176
 
191
177
  /** @category 1. Reading */
192
178
  toJSON(): {
193
- [K in keyof Shape & string]: Shape[K] extends CoValue
194
- ? CoID<Shape[K]>
195
- : Exclude<Shape[K], CoValue>;
179
+ [K in keyof Shape & string]: Shape[K];
196
180
  } {
197
181
  return this.asObject();
198
182
  }
@@ -206,9 +190,7 @@ export class CoMapView<
206
190
  by: AccountID | AgentID;
207
191
  tx: TransactionID;
208
192
  at: Date;
209
- value?: Shape[K] extends CoValue
210
- ? CoID<Shape[K]>
211
- : Exclude<Shape[K], CoValue>;
193
+ value?: Shape[K];
212
194
  }
213
195
  | undefined {
214
196
  const ops = this.timeFilteredOps(key);
@@ -238,9 +220,7 @@ export class CoMapView<
238
220
  by: AccountID | AgentID;
239
221
  tx: TransactionID;
240
222
  at: Date;
241
- value?: Shape[K] extends CoValue
242
- ? CoID<Shape[K]>
243
- : Exclude<Shape[K], CoValue>;
223
+ value?: Shape[K];
244
224
  }
245
225
  | undefined {
246
226
  const ops = this.timeFilteredOps(key);
@@ -272,14 +252,14 @@ export class CoMapView<
272
252
 
273
253
  /** A collaborative map with precise shape `Shape` and optional static metadata `Meta` */
274
254
  export class CoMap<
275
- Shape extends { [key: string]: JsonValue | CoValue | undefined },
276
- Meta extends JsonObject | null = null
255
+ Shape extends { [key: string]: JsonValue | undefined } = {
256
+ [key: string]: JsonValue | undefined;
257
+ },
258
+ Meta extends JsonObject | null = JsonObject | null
277
259
  >
278
260
  extends CoMapView<Shape, Meta>
279
261
  implements CoValue
280
262
  {
281
-
282
-
283
263
  /** Returns a new version of this CoMap with a new value for the given key.
284
264
  *
285
265
  * If `privacy` is `"private"` **(default)**, both `key` and `value` are encrypted in the transaction, only readable by other members of the group this `CoMap` belongs to. Not even sync servers can see the content in plaintext.
@@ -290,14 +270,12 @@ export class CoMap<
290
270
  **/
291
271
  set<K extends keyof Shape & string>(
292
272
  key: K,
293
- value: Shape[K] extends CoValue ? Shape[K] | CoID<Shape[K]> : Shape[K],
273
+ value: Shape[K],
294
274
  privacy?: "private" | "trusting"
295
275
  ): this;
296
276
  set(
297
277
  kv: {
298
- [K in keyof Shape & string]?: Shape[K] extends CoValue
299
- ? Shape[K] | CoID<Shape[K]>
300
- : Shape[K];
278
+ [K in keyof Shape & string]?: Shape[K];
301
279
  },
302
280
  privacy?: "private" | "trusting"
303
281
  ): this;
@@ -305,19 +283,11 @@ export class CoMap<
305
283
  ...args:
306
284
  | [
307
285
  {
308
- [K in keyof Shape & string]?: Shape[K] extends CoValue
309
- ? Shape[K] | CoID<Shape[K]>
310
- : Shape[K];
286
+ [K in keyof Shape & string]?: Shape[K];
311
287
  },
312
288
  ("private" | "trusting")?
313
289
  ]
314
- | [
315
- K,
316
- Shape[K] extends CoValue
317
- ? Shape[K] | CoID<Shape[K]>
318
- : Shape[K],
319
- ("private" | "trusting")?
320
- ]
290
+ | [K, Shape[K], ("private" | "trusting")?]
321
291
  ): this {
322
292
  if (typeof args[0] === "string") {
323
293
  const [key, value, privacy = "private"] = args;
@@ -387,7 +357,9 @@ export class CoMap<
387
357
  mutate(mutator: (mutable: MutableCoMap<Shape, Meta>) => void): this {
388
358
  const mutable = new MutableCoMap<Shape, Meta>(this.core);
389
359
  mutator(mutable);
390
- return new CoMap(this.core) as this;
360
+ return new (this.constructor as new (core: CoValueCore) => this)(
361
+ this.core
362
+ );
391
363
  }
392
364
 
393
365
  /** @deprecated Use `mutate` instead. */
@@ -397,8 +369,10 @@ export class CoMap<
397
369
  }
398
370
 
399
371
  export class MutableCoMap<
400
- Shape extends { [key: string]: JsonValue | CoValue | undefined },
401
- Meta extends JsonObject | null = null
372
+ Shape extends { [key: string]: JsonValue | undefined } = {
373
+ [key: string]: JsonValue | undefined;
374
+ },
375
+ Meta extends JsonObject | null = JsonObject | null
402
376
  >
403
377
  extends CoMapView<Shape, Meta>
404
378
  implements CoValue
@@ -413,7 +387,7 @@ export class MutableCoMap<
413
387
  */
414
388
  set<K extends keyof Shape & string>(
415
389
  key: K,
416
- value: Shape[K] extends CoValue ? Shape[K] | CoID<Shape[K]> : Shape[K],
390
+ value: Shape[K],
417
391
  privacy: "private" | "trusting" = "private"
418
392
  ): void {
419
393
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -1,10 +1,10 @@
1
1
  import { JsonObject, JsonValue } from "../jsonValue.js";
2
2
  import { CoValue, CoID, isCoValue } from "../coValue.js";
3
3
  import { CoValueCore, accountOrAgentIDfromSessionID } from "../coValueCore.js";
4
- import { Group } from "../group.js";
4
+ import { Group } from "./group.js";
5
5
  import { AgentID, SessionID, TransactionID } from "../ids.js";
6
6
  import { base64URLtoBytes, bytesToBase64url } from "../base64url.js";
7
- import { AccountID } from "../account.js";
7
+ import { AccountID } from "./account.js";
8
8
  import { parseJSON } from "../jsonStringify.js";
9
9
 
10
10
  export type BinaryStreamInfo = {
@@ -33,15 +33,15 @@ export type BinaryStreamItem =
33
33
  | BinaryStreamChunk
34
34
  | BinaryStreamEnd;
35
35
 
36
- export type CoStreamItem<Item extends JsonValue | CoValue> = {
37
- value: Item extends CoValue ? CoID<Item> : Exclude<Item, CoValue>;
36
+ export type CoStreamItem<Item extends JsonValue> = {
37
+ value: Item;
38
38
  tx: TransactionID;
39
39
  madeAt: number;
40
40
  };
41
41
 
42
42
  export class CoStreamView<
43
- Item extends JsonValue | CoValue,
44
- Meta extends JsonObject | null = null
43
+ Item extends JsonValue = JsonValue,
44
+ Meta extends JsonObject | null = JsonObject | null
45
45
  > implements CoValue
46
46
  {
47
47
  id: CoID<this>;
@@ -82,9 +82,7 @@ export class CoStreamView<
82
82
  changes,
83
83
  } of this.core.getValidSortedTransactions()) {
84
84
  for (const changeUntyped of parseJSON(changes)) {
85
- const change = changeUntyped as Item extends CoValue
86
- ? CoID<Item>
87
- : Exclude<Item, CoValue>;
85
+ const change = changeUntyped as Item;
88
86
  let entries = this.items[txID.sessionID];
89
87
  if (!entries) {
90
88
  entries = [];
@@ -96,7 +94,7 @@ export class CoStreamView<
96
94
  }
97
95
 
98
96
  getSingleStream():
99
- | (Item extends CoValue ? CoID<Item> : Exclude<Item, CoValue>)[]
97
+ | (Item)[]
100
98
  | undefined {
101
99
  if (Object.keys(this.items).length === 0) {
102
100
  return undefined;
@@ -125,7 +123,7 @@ export class CoStreamView<
125
123
  by: AccountID | AgentID;
126
124
  tx: TransactionID;
127
125
  at: Date;
128
- value: Item extends CoValue ? CoID<Item> : Exclude<Item, CoValue>;
126
+ value: Item;
129
127
  }
130
128
  | undefined {
131
129
  const items = this.items[sessionID];
@@ -147,7 +145,7 @@ export class CoStreamView<
147
145
  by: AccountID | AgentID;
148
146
  tx: TransactionID;
149
147
  at: Date;
150
- value: Item extends CoValue ? CoID<Item> : Exclude<Item, CoValue>;
148
+ value: Item;
151
149
  }
152
150
  | undefined {
153
151
  const items = this.items[sessionID];
@@ -163,7 +161,7 @@ export class CoStreamView<
163
161
  by: accountOrAgentIDfromSessionID(sessionID),
164
162
  tx: item.tx,
165
163
  at: new Date(item.madeAt),
166
- value: item.value,
164
+ value: item.value as Item,
167
165
  };
168
166
  }
169
167
  }
@@ -173,7 +171,7 @@ export class CoStreamView<
173
171
  by: AccountID | AgentID;
174
172
  tx: TransactionID;
175
173
  at: Date;
176
- value: Item extends CoValue ? CoID<Item> : Exclude<Item, CoValue>;
174
+ value: Item;
177
175
  }
178
176
  | undefined {
179
177
  let latestItem:
@@ -181,9 +179,7 @@ export class CoStreamView<
181
179
  by: AccountID | AgentID;
182
180
  tx: TransactionID;
183
181
  at: Date;
184
- value: Item extends CoValue
185
- ? CoID<Item>
186
- : Exclude<Item, CoValue>;
182
+ value: Item;
187
183
  }
188
184
  | undefined;
189
185
 
@@ -226,9 +222,7 @@ export class CoStreamView<
226
222
  }
227
223
 
228
224
  toJSON(): {
229
- [key: SessionID]: (Item extends CoValue
230
- ? CoID<Item>
231
- : Exclude<Item, CoValue>)[];
225
+ [key: SessionID]: (Item )[];
232
226
  } {
233
227
  return Object.fromEntries(
234
228
  Object.entries(this.items).map(([sessionID, items]) => [
@@ -246,14 +240,14 @@ export class CoStreamView<
246
240
  }
247
241
 
248
242
  export class CoStream<
249
- Item extends JsonValue | CoValue,
250
- Meta extends JsonObject | null = null
243
+ Item extends JsonValue = JsonValue,
244
+ Meta extends JsonObject | null = JsonObject | null
251
245
  >
252
246
  extends CoStreamView<Item, Meta>
253
247
  implements CoValue
254
248
  {
255
249
  push(
256
- item: Item extends CoValue ? Item | CoID<Item> : Item,
250
+ item: Item,
257
251
  privacy: "private" | "trusting" = "private"
258
252
  ): this {
259
253
  this.core.makeTransaction([isCoValue(item) ? item.id : item], privacy);
@@ -273,14 +267,14 @@ export class CoStream<
273
267
  }
274
268
 
275
269
  export class MutableCoStream<
276
- Item extends JsonValue | CoValue,
277
- Meta extends JsonObject | null = null
270
+ Item extends JsonValue,
271
+ Meta extends JsonObject | null = JsonObject | null
278
272
  >
279
273
  extends CoStreamView<Item, Meta>
280
274
  implements CoValue
281
275
  {
282
276
  push(
283
- item: Item extends CoValue ? Item | CoID<Item> : Item,
277
+ item: Item,
284
278
  privacy: "private" | "trusting" = "private"
285
279
  ) {
286
280
  this.core.makeTransaction([isCoValue(item) ? item.id : item], privacy);