jazz-tools 0.7.0-alpha.0 → 0.7.0-alpha.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. package/.eslintrc.cjs +8 -1
  2. package/.turbo/turbo-build.log +79 -9
  3. package/CHANGELOG.md +76 -0
  4. package/dist/coValues/account.js +171 -0
  5. package/dist/coValues/account.js.map +1 -0
  6. package/dist/coValues/coList.js +246 -0
  7. package/dist/coValues/coList.js.map +1 -0
  8. package/dist/coValues/coMap.js +235 -0
  9. package/dist/coValues/coMap.js.map +1 -0
  10. package/dist/coValues/coStream.js +263 -0
  11. package/dist/coValues/coStream.js.map +1 -0
  12. package/dist/coValues/extensions/imageDef.js +14 -11
  13. package/dist/coValues/extensions/imageDef.js.map +1 -1
  14. package/dist/coValues/group.js +71 -0
  15. package/dist/coValues/group.js.map +1 -0
  16. package/dist/coValues/interfaces.js +61 -0
  17. package/dist/coValues/interfaces.js.map +1 -0
  18. package/dist/implementation/errors.js.map +1 -0
  19. package/dist/implementation/inspect.js +2 -0
  20. package/dist/implementation/inspect.js.map +1 -0
  21. package/dist/{refs.js → implementation/refs.js} +16 -15
  22. package/dist/implementation/refs.js.map +1 -0
  23. package/dist/implementation/schema.js +41 -0
  24. package/dist/implementation/schema.js.map +1 -0
  25. package/dist/{subscriptionScope.js → implementation/subscriptionScope.js} +1 -1
  26. package/dist/implementation/subscriptionScope.js.map +1 -0
  27. package/dist/index.js +10 -29
  28. package/dist/index.js.map +1 -1
  29. package/dist/internal.js +13 -0
  30. package/dist/internal.js.map +1 -0
  31. package/dist/tests/coList.test.js +22 -22
  32. package/dist/tests/coList.test.js.map +1 -1
  33. package/dist/tests/coMap.test.js +140 -46
  34. package/dist/tests/coMap.test.js.map +1 -1
  35. package/dist/tests/coStream.test.js +55 -49
  36. package/dist/tests/coStream.test.js.map +1 -1
  37. package/package.json +11 -4
  38. package/src/coValues/account.ts +327 -0
  39. package/src/coValues/coList.ts +391 -0
  40. package/src/coValues/coMap.ts +413 -0
  41. package/src/coValues/coStream.ts +404 -0
  42. package/src/coValues/extensions/imageDef.ts +20 -16
  43. package/src/coValues/group.ts +139 -0
  44. package/src/coValues/interfaces.ts +218 -0
  45. package/src/implementation/inspect.ts +2 -0
  46. package/src/{refs.ts → implementation/refs.ts} +53 -39
  47. package/src/implementation/schema.ts +124 -0
  48. package/src/{subscriptionScope.ts → implementation/subscriptionScope.ts} +11 -13
  49. package/src/index.ts +14 -57
  50. package/src/internal.ts +15 -0
  51. package/src/tests/coList.test.ts +23 -25
  52. package/src/tests/coMap.test.ts +140 -53
  53. package/src/tests/coStream.test.ts +76 -68
  54. package/dist/coValueInterfaces.js +0 -8
  55. package/dist/coValueInterfaces.js.map +0 -1
  56. package/dist/coValues/account/account.js +0 -11
  57. package/dist/coValues/account/account.js.map +0 -1
  58. package/dist/coValues/account/accountOf.js +0 -150
  59. package/dist/coValues/account/accountOf.js.map +0 -1
  60. package/dist/coValues/account/migration.js +0 -4
  61. package/dist/coValues/account/migration.js.map +0 -1
  62. package/dist/coValues/coList/coList.js +0 -2
  63. package/dist/coValues/coList/coList.js.map +0 -1
  64. package/dist/coValues/coList/coListOf.js +0 -235
  65. package/dist/coValues/coList/coListOf.js.map +0 -1
  66. package/dist/coValues/coList/internalDocs.js +0 -2
  67. package/dist/coValues/coList/internalDocs.js.map +0 -1
  68. package/dist/coValues/coMap/coMap.js +0 -2
  69. package/dist/coValues/coMap/coMap.js.map +0 -1
  70. package/dist/coValues/coMap/coMapOf.js +0 -262
  71. package/dist/coValues/coMap/coMapOf.js.map +0 -1
  72. package/dist/coValues/coMap/internalDocs.js +0 -2
  73. package/dist/coValues/coMap/internalDocs.js.map +0 -1
  74. package/dist/coValues/coStream/coStream.js +0 -2
  75. package/dist/coValues/coStream/coStream.js.map +0 -1
  76. package/dist/coValues/coStream/coStreamOf.js +0 -244
  77. package/dist/coValues/coStream/coStreamOf.js.map +0 -1
  78. package/dist/coValues/construction.js +0 -34
  79. package/dist/coValues/construction.js.map +0 -1
  80. package/dist/coValues/group/group.js +0 -2
  81. package/dist/coValues/group/group.js.map +0 -1
  82. package/dist/coValues/group/groupOf.js +0 -109
  83. package/dist/coValues/group/groupOf.js.map +0 -1
  84. package/dist/coValues/resolution.js +0 -66
  85. package/dist/coValues/resolution.js.map +0 -1
  86. package/dist/errors.js.map +0 -1
  87. package/dist/refs.js.map +0 -1
  88. package/dist/schemaHelpers.js +0 -14
  89. package/dist/schemaHelpers.js.map +0 -1
  90. package/dist/subscriptionScope.js.map +0 -1
  91. package/dist/tests/types.test.js +0 -33
  92. package/dist/tests/types.test.js.map +0 -1
  93. package/src/coValueInterfaces.ts +0 -105
  94. package/src/coValues/account/account.ts +0 -106
  95. package/src/coValues/account/accountOf.ts +0 -284
  96. package/src/coValues/account/migration.ts +0 -12
  97. package/src/coValues/coList/coList.ts +0 -57
  98. package/src/coValues/coList/coListOf.ts +0 -377
  99. package/src/coValues/coList/internalDocs.ts +0 -1
  100. package/src/coValues/coMap/coMap.ts +0 -110
  101. package/src/coValues/coMap/coMapOf.ts +0 -451
  102. package/src/coValues/coMap/internalDocs.ts +0 -1
  103. package/src/coValues/coStream/coStream.ts +0 -63
  104. package/src/coValues/coStream/coStreamOf.ts +0 -404
  105. package/src/coValues/construction.ts +0 -110
  106. package/src/coValues/group/group.ts +0 -27
  107. package/src/coValues/group/groupOf.ts +0 -183
  108. package/src/coValues/resolution.ts +0 -111
  109. package/src/schemaHelpers.ts +0 -72
  110. package/src/tests/types.test.ts +0 -37
  111. /package/dist/{errors.js → implementation/errors.js} +0 -0
  112. /package/src/{errors.ts → implementation/errors.ts} +0 -0
package/package.json CHANGED
@@ -5,16 +5,23 @@
5
5
  "types": "src/index.ts",
6
6
  "type": "module",
7
7
  "license": "MIT",
8
- "version": "0.7.0-alpha.0",
8
+ "version": "0.7.0-alpha.11",
9
9
  "dependencies": {
10
- "@effect/schema": "^0.63.0",
11
- "effect": "^2.4.0",
12
- "cojson": "0.7.0-alpha.0"
10
+ "@effect/schema": "^0.64.20",
11
+ "effect": "^2.4.18",
12
+ "cojson": "0.7.0-alpha.11"
13
13
  },
14
14
  "devDependencies": {
15
15
  "typescript": "^5.3.3",
16
16
  "vitest": "^0.34.6"
17
17
  },
18
+ "madge": {
19
+ "detectiveOptions": {
20
+ "ts": {
21
+ "skipTypeImports": true
22
+ }
23
+ }
24
+ },
18
25
  "scripts": {
19
26
  "test": "vitest",
20
27
  "lint": "eslint src/**/*.ts",
@@ -0,0 +1,327 @@
1
+ import { LocalNode } from "cojson";
2
+ import type {
3
+ AgentSecret,
4
+ CoID,
5
+ InviteSecret,
6
+ Peer,
7
+ RawAccount,
8
+ RawCoMap,
9
+ RawCoValue,
10
+ RawControlledAccount,
11
+ SessionID,
12
+ } from "cojson";
13
+ import { Context } from "effect";
14
+ import type {
15
+ CoMap,
16
+ CoValue,
17
+ CoValueClass,
18
+ Schema,
19
+ Group,
20
+ ID,
21
+ RefEncoded,
22
+ SubclassedConstructor,
23
+ UnavailableError,
24
+ } from "../internal.js";
25
+ import {
26
+ CoValueBase,
27
+ Profile,
28
+ Ref,
29
+ SchemaInit,
30
+ inspect,
31
+ subscriptionsScopes,
32
+ } from "../internal.js";
33
+ import type { Stream } from "effect/Stream";
34
+
35
+ export class Account<
36
+ Def extends { profile: Profile | null; root: CoMap | null } = {
37
+ profile: Profile | null;
38
+ root: CoMap | null;
39
+ },
40
+ >
41
+ extends CoValueBase
42
+ implements CoValue<"Account", RawAccount | RawControlledAccount>
43
+ {
44
+ declare id: ID<this>;
45
+ declare _type: "Account";
46
+ declare _raw: RawAccount | RawControlledAccount;
47
+
48
+ static _schema: any;
49
+ get _schema(): {
50
+ profile: Schema;
51
+ root: Schema;
52
+ } {
53
+ return (this.constructor as typeof Account)._schema;
54
+ }
55
+ static {
56
+ this._schema = {
57
+ profile: { ref: () => Profile },
58
+ root: { json: true },
59
+ } as any;
60
+ }
61
+
62
+ get _owner(): Account {
63
+ return this as Account;
64
+ }
65
+ get _loadedAs(): Account & Me {
66
+ return this.isMe
67
+ ? (this as Account & Me)
68
+ : Account.fromNode(this._raw.core.node);
69
+ }
70
+
71
+ profile!: NonNullable<Def["profile"]> | null;
72
+ root!: NonNullable<Def["root"]> | null;
73
+
74
+ get _refs(): {
75
+ profile: NonNullable<Def["profile"]> extends Profile
76
+ ? Ref<NonNullable<Def["profile"]>> | null
77
+ : null;
78
+ root: NonNullable<Def["root"]> extends CoMap
79
+ ? Ref<NonNullable<Def["root"]>> | null
80
+ : null;
81
+ } {
82
+ const profileID = this._raw.get("profile") as unknown as
83
+ | ID<NonNullable<Def["profile"]>>
84
+ | undefined;
85
+ const rootID = this._raw.get("root") as unknown as
86
+ | ID<NonNullable<Def["root"]>>
87
+ | undefined;
88
+ return {
89
+ profile:
90
+ profileID &&
91
+ (new Ref(
92
+ profileID,
93
+ this._loadedAs,
94
+ this._schema.profile as RefEncoded<
95
+ NonNullable<Def["profile"]> & CoValue
96
+ >
97
+ ) as any),
98
+ root:
99
+ rootID &&
100
+ (new Ref(
101
+ rootID,
102
+ this._loadedAs,
103
+ this._schema.root as RefEncoded<
104
+ NonNullable<Def["root"]> & CoValue
105
+ >
106
+ ) as any),
107
+ };
108
+ }
109
+
110
+ isMe: boolean;
111
+ sessionID: SessionID | undefined;
112
+
113
+ constructor(init: undefined, options: { owner: Group | Account });
114
+ constructor(
115
+ init: undefined,
116
+ options: { fromRaw: RawAccount | RawControlledAccount }
117
+ );
118
+ constructor(
119
+ _init: undefined,
120
+ options:
121
+ | { fromRaw: RawAccount | RawControlledAccount }
122
+ | { owner: Group | Account }
123
+ ) {
124
+ super();
125
+ if (!("fromRaw" in options)) {
126
+ throw new Error(
127
+ "Can only construct account from raw or with .create()"
128
+ );
129
+ }
130
+ this.isMe = options.fromRaw.id == options.fromRaw.core.node.account.id;
131
+
132
+ Object.defineProperties(this, {
133
+ id: {
134
+ value: options.fromRaw.id,
135
+ enumerable: false,
136
+ },
137
+ _raw: { value: options.fromRaw, enumerable: false },
138
+ });
139
+
140
+ if (this.isMe) {
141
+ (this as Account & Me).sessionID =
142
+ options.fromRaw.core.node.currentSessionID;
143
+ }
144
+
145
+ return new Proxy(
146
+ this,
147
+ AccountAndGroupProxyHandler as ProxyHandler<this>
148
+ );
149
+ }
150
+
151
+ myRole(): "admin" | undefined {
152
+ if (this.isMe) {
153
+ return "admin";
154
+ }
155
+ }
156
+
157
+ acceptInvite:
158
+ | (<V extends CoValue>(
159
+ valueID: ID<V>,
160
+ inviteSecret: InviteSecret,
161
+ coValueClass: CoValueClass<V>
162
+ ) => Promise<V | undefined>)
163
+ | undefined = (async <V extends CoValue>(
164
+ valueID: ID<V>,
165
+ inviteSecret: InviteSecret,
166
+ coValueClass: CoValueClass<V>
167
+ ) => {
168
+ if (!this.isMe) {
169
+ throw new Error("Only a controlled account can accept invites");
170
+ }
171
+
172
+ await (this._raw as RawControlledAccount).acceptInvite(
173
+ valueID as unknown as CoID<RawCoValue>,
174
+ inviteSecret
175
+ );
176
+
177
+ return coValueClass.load(valueID, {
178
+ as: this as Account & Me,
179
+ });
180
+ }) as any;
181
+
182
+ static async create<A extends Account>(
183
+ this: SubclassedConstructor<A> & typeof Account,
184
+ options: {
185
+ name: string;
186
+ initialAgentSecret?: AgentSecret;
187
+ peersToLoadFrom?: Peer[];
188
+ }
189
+ ): Promise<A & Me> {
190
+ const { node } = await LocalNode.withNewlyCreatedAccount({
191
+ ...options,
192
+ migration: async (rawAccount) => {
193
+ const account = new this(undefined, {
194
+ fromRaw: rawAccount,
195
+ }) as A & Me;
196
+
197
+ await account.migrate?.();
198
+ },
199
+ });
200
+
201
+ return this.fromNode(node) as A & Me;
202
+ }
203
+
204
+ static async become<A extends Account>(
205
+ this: SubclassedConstructor<A> & typeof Account,
206
+ options: {
207
+ accountID: ID<Account>;
208
+ accountSecret: AgentSecret;
209
+ sessionID: SessionID;
210
+ peersToLoadFrom: Peer[];
211
+ }
212
+ ): Promise<A & Me> {
213
+ const node = await LocalNode.withLoadedAccount({
214
+ accountID: options.accountID as unknown as CoID<RawAccount>,
215
+ accountSecret: options.accountSecret,
216
+ sessionID: options.sessionID,
217
+ peersToLoadFrom: options.peersToLoadFrom,
218
+ migration: async (rawAccount) => {
219
+ const account = new this(undefined, {
220
+ fromRaw: rawAccount,
221
+ }) as A & Me;
222
+
223
+ await account.migrate?.();
224
+ },
225
+ });
226
+
227
+ return this.fromNode(node) as A & Me;
228
+ }
229
+
230
+ static fromNode<A extends Account>(
231
+ this: SubclassedConstructor<A>,
232
+ node: LocalNode
233
+ ): A & Me {
234
+ return new this(undefined, {
235
+ fromRaw: node.account as RawControlledAccount,
236
+ }) as A & Me;
237
+ }
238
+
239
+ toJSON(): object | any[] {
240
+ return {
241
+ id: this.id,
242
+ _type: this._type,
243
+ };
244
+ }
245
+
246
+ [inspect]() {
247
+ return this.toJSON();
248
+ }
249
+
250
+ migrate: (() => void | Promise<void>) | undefined;
251
+ }
252
+
253
+ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
254
+ get(target, key, receiver) {
255
+ if (key === "profile") {
256
+ const ref = target._refs.profile;
257
+ return ref ? ref.accessFrom(receiver) : (undefined as any);
258
+ } else if (key === "root") {
259
+ const ref = target._refs.root;
260
+ return ref ? ref.accessFrom(receiver) : (undefined as any);
261
+ } else {
262
+ return Reflect.get(target, key, receiver);
263
+ }
264
+ },
265
+ set(target, key, value, receiver) {
266
+ if (
267
+ (key === "profile" || key === "root") &&
268
+ typeof value === "object" &&
269
+ SchemaInit in value
270
+ ) {
271
+ (target.constructor as typeof CoMap)._schema ||= {};
272
+ (target.constructor as typeof CoMap)._schema[key] =
273
+ value[SchemaInit];
274
+ return true;
275
+ } else if (key === "profile") {
276
+ if (value) {
277
+ target._raw.set(
278
+ "profile",
279
+ value.id as unknown as CoID<RawCoMap>
280
+ );
281
+ }
282
+ subscriptionsScopes.get(receiver)?.onRefAccessedOrSet(value.id);
283
+ return true;
284
+ } else if (key === "root") {
285
+ if (value) {
286
+ target._raw.set("root", value.id as unknown as CoID<RawCoMap>);
287
+ }
288
+ subscriptionsScopes.get(receiver)?.onRefAccessedOrSet(value.id);
289
+ return true;
290
+ } else {
291
+ return Reflect.set(target, key, value, receiver);
292
+ }
293
+ },
294
+ defineProperty(target, key, descriptor) {
295
+ if (
296
+ (key === "profile" || key === "root") &&
297
+ typeof descriptor.value === "object" &&
298
+ SchemaInit in descriptor.value
299
+ ) {
300
+ (target.constructor as typeof CoMap)._schema ||= {};
301
+ (target.constructor as typeof CoMap)._schema[key] =
302
+ descriptor.value[SchemaInit];
303
+ return true;
304
+ } else {
305
+ return Reflect.defineProperty(target, key, descriptor);
306
+ }
307
+ },
308
+ };
309
+
310
+ export interface Me {
311
+ id: ID<any>;
312
+ isMe: true;
313
+ _raw: RawControlledAccount;
314
+ sessionID: SessionID;
315
+ subscribe(listener: (update: this & Me) => void): () => void;
316
+ subscribeEf(): Stream<this & Me, UnavailableError, never>;
317
+ acceptInvite: (...args: any[]) => any;
318
+ }
319
+
320
+ export class AccountCtx extends Context.Tag("Account")<
321
+ AccountCtx,
322
+ Account & Me
323
+ >() {}
324
+
325
+ export function isControlledAccount(account: Account): account is Account & Me {
326
+ return account.isMe;
327
+ }