cojson 0.6.6 → 0.7.0-alpha.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 (56) hide show
  1. package/.eslintrc.cjs +1 -0
  2. package/.turbo/turbo-build.log +35 -0
  3. package/CHANGELOG.md +6 -0
  4. package/dist/coValue.js.map +1 -1
  5. package/dist/coValueCore.js.map +1 -1
  6. package/dist/coValues/account.js +5 -5
  7. package/dist/coValues/account.js.map +1 -1
  8. package/dist/coValues/coList.js +39 -58
  9. package/dist/coValues/coList.js.map +1 -1
  10. package/dist/coValues/coMap.js +20 -61
  11. package/dist/coValues/coMap.js.map +1 -1
  12. package/dist/coValues/coStream.js +14 -64
  13. package/dist/coValues/coStream.js.map +1 -1
  14. package/dist/coValues/group.js +57 -59
  15. package/dist/coValues/group.js.map +1 -1
  16. package/dist/coreToCoValue.js +17 -12
  17. package/dist/coreToCoValue.js.map +1 -1
  18. package/dist/index.js +8 -8
  19. package/dist/index.js.map +1 -1
  20. package/dist/localNode.js +54 -38
  21. package/dist/localNode.js.map +1 -1
  22. package/dist/permissions.js +2 -2
  23. package/dist/permissions.js.map +1 -1
  24. package/dist/sync.js +3 -3
  25. package/dist/sync.js.map +1 -1
  26. package/dist/tests/testUtils.js +6 -11
  27. package/dist/tests/testUtils.js.map +1 -1
  28. package/dist/typeUtils/expectGroup.js +2 -2
  29. package/dist/typeUtils/expectGroup.js.map +1 -1
  30. package/dist/typeUtils/isCoValue.js +8 -8
  31. package/dist/typeUtils/isCoValue.js.map +1 -1
  32. package/package.json +52 -53
  33. package/src/coValue.ts +21 -21
  34. package/src/coValueCore.ts +8 -8
  35. package/src/coValues/account.ts +14 -26
  36. package/src/coValues/coList.ts +58 -97
  37. package/src/coValues/coMap.ts +27 -129
  38. package/src/coValues/coStream.ts +31 -137
  39. package/src/coValues/group.ts +52 -46
  40. package/src/coreToCoValue.ts +16 -12
  41. package/src/index.ts +27 -36
  42. package/src/jsonValue.ts +1 -1
  43. package/src/localNode.ts +88 -75
  44. package/src/media.ts +4 -4
  45. package/src/permissions.ts +2 -2
  46. package/src/sync.ts +3 -3
  47. package/src/tests/account.test.ts +12 -12
  48. package/src/tests/coValue.test.ts +149 -182
  49. package/src/tests/coValueCore.test.ts +8 -3
  50. package/src/tests/crypto.test.ts +7 -0
  51. package/src/tests/group.test.ts +13 -6
  52. package/src/tests/permissions.test.ts +648 -840
  53. package/src/tests/sync.test.ts +44 -68
  54. package/src/tests/testUtils.ts +6 -11
  55. package/src/typeUtils/expectGroup.ts +4 -4
  56. package/src/typeUtils/isCoValue.ts +11 -11
package/src/coValue.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  import { JsonObject, JsonValue } from "./jsonValue.js";
2
2
  import { RawCoID } from "./ids.js";
3
- import { CoMap } from "./coValues/coMap.js";
4
- import { BinaryCoStream, CoStream } from "./coValues/coStream.js";
5
- import { CoList } from "./coValues/coList.js";
3
+ import { RawCoMap } from "./coValues/coMap.js";
4
+ import { RawBinaryCoStream, RawCoStream } from "./coValues/coStream.js";
5
+ import { RawCoList } from "./coValues/coList.js";
6
6
  import { CoValueCore } from "./coValueCore.js";
7
- import { Group } from "./coValues/group.js";
8
- import { Account, Profile } from "./index.js";
7
+ import { RawGroup } from "./coValues/group.js";
8
+ import { RawAccount, Profile } from "./index.js";
9
9
 
10
- export type CoID<T extends CoValue> = RawCoID & {
10
+ export type CoID<T extends RawCoValue> = RawCoID & {
11
11
  readonly __type: T;
12
12
  };
13
13
 
14
- export interface CoValue {
14
+ export interface RawCoValue {
15
15
  /** The `CoValue`'s (precisely typed) `CoID` */
16
16
  id: CoID<this>;
17
17
  core: CoValueCore;
@@ -20,7 +20,7 @@ export interface CoValue {
20
20
  /** The `CoValue`'s (precisely typed) static metadata */
21
21
  headerMeta: JsonObject | null;
22
22
  /** The `Group` this `CoValue` belongs to (determining permissions) */
23
- group: Group;
23
+ group: RawGroup;
24
24
  /** Returns an immutable JSON presentation of this `CoValue` */
25
25
  toJSON(): JsonValue;
26
26
  atTime(time: number): this;
@@ -34,35 +34,35 @@ export interface CoValue {
34
34
  subscribe(listener: (coValue: this) => void): () => void;
35
35
  }
36
36
 
37
- export type AnyCoValue =
38
- | CoMap
39
- | Group
40
- | Account
37
+ export type AnyRawCoValue =
38
+ | RawCoMap
39
+ | RawGroup
40
+ | RawAccount
41
41
  | Profile
42
- | CoList
43
- | CoStream
44
- | BinaryCoStream;
42
+ | RawCoList
43
+ | RawCoStream
44
+ | RawBinaryCoStream;
45
45
 
46
- export function expectMap(content: CoValue): CoMap {
46
+ export function expectMap(content: RawCoValue): RawCoMap {
47
47
  if (content.type !== "comap") {
48
48
  throw new Error("Expected map");
49
49
  }
50
50
 
51
- return content as CoMap;
51
+ return content as RawCoMap;
52
52
  }
53
53
 
54
- export function expectList(content: CoValue): CoList {
54
+ export function expectList(content: RawCoValue): RawCoList {
55
55
  if (content.type !== "colist") {
56
56
  throw new Error("Expected list");
57
57
  }
58
58
 
59
- return content as CoList;
59
+ return content as RawCoList;
60
60
  }
61
61
 
62
- export function expectStream(content: CoValue): CoStream {
62
+ export function expectStream(content: RawCoValue): RawCoStream {
63
63
  if (content.type !== "costream") {
64
64
  throw new Error("Expected stream");
65
65
  }
66
66
 
67
- return content as CoStream;
67
+ return content as RawCoStream;
68
68
  }
@@ -1,4 +1,4 @@
1
- import { AnyCoValue, CoValue } from "./coValue.js";
1
+ import { AnyRawCoValue, RawCoValue } from "./coValue.js";
2
2
  import {
3
3
  Encrypted,
4
4
  Hash,
@@ -23,7 +23,7 @@ import {
23
23
  determineValidTransactions,
24
24
  isKeyForKeyField,
25
25
  } from "./permissions.js";
26
- import { Group } from "./coValues/group.js";
26
+ import { RawGroup } from "./coValues/group.js";
27
27
  import { LocalNode } from "./localNode.js";
28
28
  import { CoValueKnownState, NewContentMessage } from "./sync.js";
29
29
  import { AgentID, RawCoID, SessionID, TransactionID } from "./ids.js";
@@ -37,7 +37,7 @@ import { accountOrAgentIDfromSessionID } from "./typeUtils/accountOrAgentIDfromS
37
37
  export const MAX_RECOMMENDED_TX_SIZE = 100 * 1024;
38
38
 
39
39
  export type CoValueHeader = {
40
- type: AnyCoValue["type"];
40
+ type: AnyRawCoValue["type"];
41
41
  ruleset: RulesetDef;
42
42
  meta: JsonObject | null;
43
43
  createdAt: `2${string}` | null;
@@ -95,8 +95,8 @@ export class CoValueCore {
95
95
  node: LocalNode;
96
96
  header: CoValueHeader;
97
97
  _sessionLogs: Map<SessionID, SessionLog>;
98
- _cachedContent?: CoValue;
99
- listeners: Set<(content?: CoValue) => void> = new Set();
98
+ _cachedContent?: RawCoValue;
99
+ listeners: Set<(content?: RawCoValue) => void> = new Set();
100
100
  _decryptionCache: {
101
101
  [key: Encrypted<JsonValue[], JsonValue>]: JsonValue[] | undefined;
102
102
  } = {};
@@ -409,7 +409,7 @@ export class CoValueCore {
409
409
  }
410
410
  }
411
411
 
412
- subscribe(listener: (content?: CoValue) => void): () => void {
412
+ subscribe(listener: (content?: RawCoValue) => void): () => void {
413
413
  this.listeners.add(listener);
414
414
  listener(this.getCurrentContent());
415
415
 
@@ -533,7 +533,7 @@ export class CoValueCore {
533
533
  return success;
534
534
  }
535
535
 
536
- getCurrentContent(options?: { ignorePrivateTransactions: true }): CoValue {
536
+ getCurrentContent(options?: { ignorePrivateTransactions: true }): RawCoValue {
537
537
  if (!options?.ignorePrivateTransactions && this._cachedContent) {
538
538
  return this._cachedContent;
539
539
  }
@@ -741,7 +741,7 @@ export class CoValueCore {
741
741
  }
742
742
  }
743
743
 
744
- getGroup(): Group {
744
+ getGroup(): RawGroup {
745
745
  if (this.header.ruleset.type !== "ownedByGroup") {
746
746
  throw new Error("Only values owned by groups have groups");
747
747
  }
@@ -1,5 +1,5 @@
1
1
  import { CoValueCore, CoValueHeader } from "../coValueCore.js";
2
- import { CoID, CoValue } from "../coValue.js";
2
+ import { CoID, RawCoValue } from "../coValue.js";
3
3
  import {
4
4
  AgentSecret,
5
5
  SealerID,
@@ -13,8 +13,8 @@ import {
13
13
  getAgentSignerSecret,
14
14
  } from "../crypto.js";
15
15
  import { AgentID } from "../ids.js";
16
- import { CoMap } from "./coMap.js";
17
- import { Group, InviteSecret } from "./group.js";
16
+ import { RawCoMap } from "./coMap.js";
17
+ import { RawGroup, InviteSecret } from "./group.js";
18
18
  import { LocalNode } from "../index.js";
19
19
 
20
20
  export function accountHeaderForInitialAgentSecret(
@@ -32,11 +32,9 @@ export function accountHeaderForInitialAgentSecret(
32
32
  };
33
33
  }
34
34
 
35
- export class Account<
36
- P extends Profile = Profile,
37
- R extends CoMap = CoMap,
35
+ export class RawAccount<
38
36
  Meta extends AccountMeta = AccountMeta
39
- > extends Group<P, R, Meta> {
37
+ > extends RawGroup<Meta> {
40
38
  currentAgentID(): AgentID {
41
39
  const agents = this.keys().filter((k): k is AgentID =>
42
40
  k.startsWith("sealer_")
@@ -64,12 +62,8 @@ export interface ControlledAccountOrAgent {
64
62
  }
65
63
 
66
64
  /** @hidden */
67
- export class ControlledAccount<
68
- P extends Profile = Profile,
69
- R extends CoMap = CoMap,
70
- Meta extends AccountMeta = AccountMeta
71
- >
72
- extends Account<P, R, Meta>
65
+ export class RawControlledAccount<Meta extends AccountMeta = AccountMeta>
66
+ extends RawAccount<Meta>
73
67
  implements ControlledAccountOrAgent
74
68
  {
75
69
  agentSecret: AgentSecret;
@@ -88,7 +82,7 @@ export class ControlledAccount<
88
82
  return this.core.node.createGroup();
89
83
  }
90
84
 
91
- async acceptInvite<T extends CoValue>(
85
+ async acceptInvite<T extends RawCoValue>(
92
86
  groupOrOwnedValueID: CoID<T>,
93
87
  inviteSecret: InviteSecret
94
88
  ): Promise<void> {
@@ -117,9 +111,7 @@ export class ControlledAccount<
117
111
  }
118
112
 
119
113
  /** @hidden */
120
- export class ControlledAgent
121
- implements ControlledAccountOrAgent
122
- {
114
+ export class ControlledAgent implements ControlledAccountOrAgent {
123
115
  agentSecret: AgentSecret;
124
116
 
125
117
  constructor(agentSecret: AgentSecret) {
@@ -152,7 +144,7 @@ export class ControlledAgent
152
144
  }
153
145
 
154
146
  export type AccountMeta = { type: "account" };
155
- export type AccountID = CoID<Account>;
147
+ export type AccountID = CoID<RawAccount>;
156
148
 
157
149
  export type ProfileShape = {
158
150
  name: string;
@@ -162,14 +154,10 @@ export type ProfileMeta = { type: "profile" };
162
154
  export class Profile<
163
155
  Shape extends ProfileShape = ProfileShape,
164
156
  Meta extends ProfileMeta = ProfileMeta
165
- > extends CoMap<Shape, Meta> {}
157
+ > extends RawCoMap<Shape, Meta> {}
166
158
 
167
- export type AccountMigration<
168
- P extends Profile = Profile,
169
- R extends CoMap = CoMap,
170
- Meta extends AccountMeta = AccountMeta
171
- > = (
172
- account: ControlledAccount<P, R, Meta>,
173
- profile: P,
159
+ export type RawAccountMigration<Meta extends AccountMeta = AccountMeta> = (
160
+ account: RawControlledAccount<Meta>,
161
+ profile: RawCoMap,
174
162
  localNode: LocalNode
175
163
  ) => void | Promise<void>;
@@ -1,11 +1,11 @@
1
1
  import { JsonObject, JsonValue } from "../jsonValue.js";
2
- import { CoID, CoValue } from "../coValue.js";
2
+ import { CoID, RawCoValue } from "../coValue.js";
3
3
  import { isCoValue } from "../typeUtils/isCoValue.js";
4
4
  import { CoValueCore } from "../coValueCore.js";
5
5
  import { accountOrAgentIDfromSessionID } from "../typeUtils/accountOrAgentIDfromSessionID.js";
6
6
  import { AgentID, SessionID, TransactionID } from "../ids.js";
7
7
  import { AccountID } from "./account.js";
8
- import { Group } from "./group.js";
8
+ import { RawGroup } from "./group.js";
9
9
 
10
10
  type OpID = TransactionID & { changeIdx: number };
11
11
 
@@ -41,10 +41,10 @@ type DeletionEntry = {
41
41
  deletionID: OpID;
42
42
  } & DeletionOpPayload;
43
43
 
44
- export class CoListView<
44
+ export class RawCoListView<
45
45
  Item extends JsonValue = JsonValue,
46
- Meta extends JsonObject | null = null
47
- > implements CoValue
46
+ Meta extends JsonObject | null = null,
47
+ > implements RawCoValue
48
48
  {
49
49
  /** @category 6. Meta */
50
50
  id: CoID<this>;
@@ -209,7 +209,7 @@ export class CoListView<
209
209
  }
210
210
 
211
211
  /** @category 6. Meta */
212
- get group(): Group {
212
+ get group(): RawGroup {
213
213
  return this.core.getGroup();
214
214
  }
215
215
 
@@ -306,7 +306,9 @@ export class CoListView<
306
306
  opID,
307
307
  });
308
308
  }
309
- for (const successor of entry.successors) {
309
+ // traverse successors in reverse for correct insertion behavior
310
+ for (let i = entry.successors.length - 1; i >= 0; i--) {
311
+ const successor = entry.successors[i]!;
310
312
  this.fillArrayFromOpID(successor, arr);
311
313
  }
312
314
  }
@@ -393,14 +395,14 @@ export class CoListView<
393
395
  }
394
396
  }
395
397
 
396
- export class CoList<
398
+ export class RawCoList<
397
399
  Item extends JsonValue = JsonValue,
398
- Meta extends JsonObject | null = JsonObject | null
400
+ Meta extends JsonObject | null = JsonObject | null,
399
401
  >
400
- extends CoListView<Item, Meta>
401
- implements CoValue
402
+ extends RawCoListView<Item, Meta>
403
+ implements RawCoValue
402
404
  {
403
- /** Returns a new version of this CoList with `item` appended after the item currently at index `after`.
405
+ /** Appends `item` after the item currently at index `after`.
404
406
  *
405
407
  * If `privacy` is `"private"` **(default)**, `item` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
406
408
  *
@@ -412,7 +414,7 @@ export class CoList<
412
414
  item: Item,
413
415
  after?: number,
414
416
  privacy: "private" | "trusting" = "private"
415
- ): this {
417
+ ) {
416
418
  const entries = this.entries();
417
419
  after =
418
420
  after === undefined
@@ -444,11 +446,17 @@ export class CoList<
444
446
  privacy
445
447
  );
446
448
 
447
- return new CoList(this.core) as this;
449
+ const listAfter = new RawCoList(this.core) as this;
450
+
451
+ this.afterStart = listAfter.afterStart;
452
+ this.beforeEnd = listAfter.beforeEnd;
453
+ this.insertions = listAfter.insertions;
454
+ this.deletionsByInsertion = listAfter.deletionsByInsertion;
455
+ this._cachedEntries = undefined;
448
456
  }
449
457
 
450
458
  /**
451
- * Returns a new version of this CoList with `item` prepended before the item currently at index `before`.
459
+ * Prepends `item` before the item currently at index `before`.
452
460
  *
453
461
  * If `privacy` is `"private"` **(default)**, `item` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
454
462
  *
@@ -460,7 +468,7 @@ export class CoList<
460
468
  item: Item,
461
469
  before?: number,
462
470
  privacy: "private" | "trusting" = "private"
463
- ): this {
471
+ ) {
464
472
  const entries = this.entries();
465
473
  before = before === undefined ? 0 : before;
466
474
  let opIDAfter;
@@ -491,10 +499,16 @@ export class CoList<
491
499
  privacy
492
500
  );
493
501
 
494
- return new CoList(this.core) as this;
502
+ const listAfter = new RawCoList(this.core) as this;
503
+
504
+ this.afterStart = listAfter.afterStart;
505
+ this.beforeEnd = listAfter.beforeEnd;
506
+ this.insertions = listAfter.insertions;
507
+ this.deletionsByInsertion = listAfter.deletionsByInsertion;
508
+ this._cachedEntries = undefined;
495
509
  }
496
510
 
497
- /** Returns a new version of this CoList with the item at index `at` deleted from the list.
511
+ /** Deletes the item at index `at`.
498
512
  *
499
513
  * If `privacy` is `"private"` **(default)**, the fact of this deletion is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
500
514
  *
@@ -502,7 +516,7 @@ export class CoList<
502
516
  *
503
517
  * @category 2. Editing
504
518
  **/
505
- delete(at: number, privacy: "private" | "trusting" = "private"): this {
519
+ delete(at: number, privacy: "private" | "trusting" = "private") {
506
520
  const entries = this.entries();
507
521
  const entry = entries[at];
508
522
  if (!entry) {
@@ -518,48 +532,8 @@ export class CoList<
518
532
  privacy
519
533
  );
520
534
 
521
- return new CoList(this.core) as this;
522
- }
523
-
524
- /** @category 2. Editing */
525
- mutate(mutator: (mutable: MutableCoList<Item, Meta>) => void): this {
526
- const mutable = new MutableCoList<Item, Meta>(this.core);
527
- mutator(mutable);
528
- return new CoList(this.core) as this;
529
- }
530
-
531
- /** @deprecated Use `mutate` instead. */
532
- edit(mutator: (mutable: MutableCoList<Item, Meta>) => void): this {
533
- return this.mutate(mutator);
534
- }
535
- }
535
+ const listAfter = new RawCoList(this.core) as this;
536
536
 
537
- export class MutableCoList<
538
- Item extends JsonValue = JsonValue,
539
- Meta extends JsonObject | null = JsonObject | null
540
- >
541
- extends CoListView<Item, Meta>
542
- implements CoValue
543
- {
544
- /** Appends `item` after the item currently at index `after`.
545
- *
546
- * If `privacy` is `"private"` **(default)**, `item` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
547
- *
548
- * If `privacy` is `"trusting"`, `item` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
549
- *
550
- * @category 2. Mutating
551
- **/
552
- append(
553
- item: Item,
554
- after?: number,
555
- privacy: "private" | "trusting" = "private"
556
- ): void {
557
- const listAfter = CoList.prototype.append.call(
558
- this,
559
- item,
560
- after,
561
- privacy
562
- ) as CoList<Item, Meta>;
563
537
  this.afterStart = listAfter.afterStart;
564
538
  this.beforeEnd = listAfter.beforeEnd;
565
539
  this.insertions = listAfter.insertions;
@@ -567,46 +541,33 @@ export class MutableCoList<
567
541
  this._cachedEntries = undefined;
568
542
  }
569
543
 
570
- /** Prepends `item` before the item currently at index `before`.
571
- *
572
- * If `privacy` is `"private"` **(default)**, `item` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
573
- *
574
- * If `privacy` is `"trusting"`, `item` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
575
- *
576
- * * @category 2. Mutating
577
- **/
578
- prepend(
579
- item: Item,
580
- before?: number,
544
+ replace(
545
+ at: number,
546
+ newItem: Item,
581
547
  privacy: "private" | "trusting" = "private"
582
- ): void {
583
- const listAfter = CoList.prototype.prepend.call(
584
- this,
585
- item,
586
- before,
587
- privacy
588
- ) as CoList<Item, Meta>;
589
- this.afterStart = listAfter.afterStart;
590
- this.beforeEnd = listAfter.beforeEnd;
591
- this.insertions = listAfter.insertions;
592
- this.deletionsByInsertion = listAfter.deletionsByInsertion;
593
- this._cachedEntries = undefined;
594
- }
548
+ ) {
549
+ const entries = this.entries();
550
+ const entry = entries[at];
551
+ if (!entry) {
552
+ throw new Error("Invalid index " + at);
553
+ }
595
554
 
596
- /** Deletes the item at index `at` from the list.
597
- *
598
- * If `privacy` is `"private"` **(default)**, the fact of this deletion is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
599
- *
600
- * If `privacy` is `"trusting"`, the fact of this deletion is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
601
- *
602
- * * @category 2. Mutating
603
- **/
604
- delete(at: number, privacy: "private" | "trusting" = "private"): void {
605
- const listAfter = CoList.prototype.delete.call(
606
- this,
607
- at,
555
+ this.core.makeTransaction(
556
+ [
557
+ {
558
+ op: "app",
559
+ value: isCoValue(newItem) ? newItem.id : newItem,
560
+ after: entry.opID,
561
+ },
562
+ {
563
+ op: "del",
564
+ insertion: entry.opID,
565
+ },
566
+ ],
608
567
  privacy
609
- ) as CoList<Item, Meta>;
568
+ );
569
+ const listAfter = new RawCoList(this.core) as this;
570
+
610
571
  this.afterStart = listAfter.afterStart;
611
572
  this.beforeEnd = listAfter.beforeEnd;
612
573
  this.insertions = listAfter.insertions;