jazz-tools 0.7.0-alpha.4 → 0.7.0-alpha.41

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. package/.eslintrc.cjs +3 -10
  2. package/.prettierrc.js +9 -0
  3. package/.turbo/turbo-build.log +3 -19
  4. package/.turbo/turbo-lint.log +4 -0
  5. package/.turbo/turbo-test.log +140 -0
  6. package/CHANGELOG.md +243 -0
  7. package/README.md +10 -2
  8. package/dist/coValues/account.js +104 -50
  9. package/dist/coValues/account.js.map +1 -1
  10. package/dist/coValues/coList.js +165 -112
  11. package/dist/coValues/coList.js.map +1 -1
  12. package/dist/coValues/coMap.js +243 -163
  13. package/dist/coValues/coMap.js.map +1 -1
  14. package/dist/coValues/coStream.js +256 -73
  15. package/dist/coValues/coStream.js.map +1 -1
  16. package/dist/coValues/deepLoading.js +57 -0
  17. package/dist/coValues/deepLoading.js.map +1 -0
  18. package/dist/coValues/extensions/imageDef.js +14 -8
  19. package/dist/coValues/extensions/imageDef.js.map +1 -1
  20. package/dist/coValues/group.js +49 -38
  21. package/dist/coValues/group.js.map +1 -1
  22. package/dist/coValues/interfaces.js +66 -26
  23. package/dist/coValues/interfaces.js.map +1 -1
  24. package/dist/implementation/devtoolsFormatters.js +114 -0
  25. package/dist/implementation/devtoolsFormatters.js.map +1 -0
  26. package/dist/implementation/refs.js +60 -19
  27. package/dist/implementation/refs.js.map +1 -1
  28. package/dist/implementation/schema.js +44 -1
  29. package/dist/implementation/schema.js.map +1 -1
  30. package/dist/implementation/subscriptionScope.js +19 -1
  31. package/dist/implementation/subscriptionScope.js.map +1 -1
  32. package/dist/implementation/symbols.js +5 -0
  33. package/dist/implementation/symbols.js.map +1 -0
  34. package/dist/index.js +4 -5
  35. package/dist/index.js.map +1 -1
  36. package/dist/internal.js +4 -1
  37. package/dist/internal.js.map +1 -1
  38. package/dist/tests/coList.test.js +51 -52
  39. package/dist/tests/coList.test.js.map +1 -1
  40. package/dist/tests/coMap.test.js +196 -75
  41. package/dist/tests/coMap.test.js.map +1 -1
  42. package/dist/tests/coStream.test.js +95 -85
  43. package/dist/tests/coStream.test.js.map +1 -1
  44. package/dist/tests/deepLoading.test.js +188 -0
  45. package/dist/tests/deepLoading.test.js.map +1 -0
  46. package/dist/tests/groupsAndAccounts.test.js +83 -0
  47. package/dist/tests/groupsAndAccounts.test.js.map +1 -0
  48. package/package.json +17 -9
  49. package/src/coValues/account.ts +183 -152
  50. package/src/coValues/coList.ts +220 -173
  51. package/src/coValues/coMap.ts +322 -312
  52. package/src/coValues/coStream.ts +397 -135
  53. package/src/coValues/deepLoading.ts +215 -0
  54. package/src/coValues/extensions/imageDef.ts +16 -17
  55. package/src/coValues/group.ts +95 -111
  56. package/src/coValues/interfaces.ts +217 -115
  57. package/src/implementation/devtoolsFormatters.ts +110 -0
  58. package/src/implementation/inspect.ts +1 -1
  59. package/src/implementation/refs.ts +91 -38
  60. package/src/implementation/schema.ts +87 -46
  61. package/src/implementation/subscriptionScope.ts +44 -12
  62. package/src/implementation/symbols.ts +11 -0
  63. package/src/index.ts +13 -9
  64. package/src/internal.ts +6 -2
  65. package/src/tests/coList.test.ts +67 -66
  66. package/src/tests/coMap.test.ts +226 -123
  67. package/src/tests/coStream.test.ts +141 -131
  68. package/src/tests/deepLoading.test.ts +301 -0
  69. package/src/tests/groupsAndAccounts.test.ts +91 -0
package/.eslintrc.cjs CHANGED
@@ -3,6 +3,7 @@ module.exports = {
3
3
  "eslint:recommended",
4
4
  "plugin:@typescript-eslint/recommended",
5
5
  "plugin:require-extensions/recommended",
6
+ "prettier"
6
7
  ],
7
8
  parser: "@typescript-eslint/parser",
8
9
  plugins: ["@typescript-eslint", "require-extensions"],
@@ -10,7 +11,7 @@ module.exports = {
10
11
  project: "./tsconfig.json",
11
12
  tsconfigRootDir: __dirname,
12
13
  },
13
- ignorePatterns: [".eslint.cjs", "**/tests/*"],
14
+ ignorePatterns: [".eslint.cjs"],
14
15
  root: true,
15
16
  rules: {
16
17
  "no-unused-vars": "off",
@@ -19,13 +20,5 @@ module.exports = {
19
20
  { argsIgnorePattern: "^_", varsIgnorePattern: "^_" },
20
21
  ],
21
22
  "@typescript-eslint/no-floating-promises": "error",
22
- "@typescript-eslint/no-explicit-any": "warn",
23
- "@typescript-eslint/consistent-type-imports": [
24
- "error",
25
- {
26
- prefer: "type-imports",
27
- disallowTypeAnnotations: false,
28
- },
29
- ],
30
23
  },
31
- };
24
+ }
package/.prettierrc.js ADDED
@@ -0,0 +1,9 @@
1
+ /** @type {import("prettier").Config} */
2
+ const config = {
3
+ trailingComma: "all",
4
+ tabWidth: 4,
5
+ semi: true,
6
+ singleQuote: false,
7
+ };
8
+
9
+ export default config;
@@ -1,24 +1,8 @@
1
1
 
2
- > jazz-js@0.6.1 build /Users/anselm/jazz/jazz/packages/jazz-js
2
+ > jazz-tools@0.7.0-alpha.37 build /Users/anselm/jazz/jazz/packages/jazz-tools
3
3
  > npm run lint && rm -rf ./dist && tsc --sourceMap --outDir dist
4
4
 
5
5
 
6
- > jazz-js@0.6.1 lint
7
- > eslint src/**/*.ts
6
+ > jazz-tools@0.7.0-alpha.37 lint
7
+ > eslint . --ext ts,tsx
8
8
 
9
-
10
- /Users/anselm/jazz/jazz/packages/jazz-js/src/tests/binaryCoStream.test.ts
11
- 0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override
12
-
13
- /Users/anselm/jazz/jazz/packages/jazz-js/src/tests/coList.test.ts
14
- 0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override
15
-
16
- /Users/anselm/jazz/jazz/packages/jazz-js/src/tests/coMap.test.ts
17
- 0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override
18
-
19
- /Users/anselm/jazz/jazz/packages/jazz-js/src/tests/coStream.test.ts
20
- 0:0 warning File ignored because of a matching ignore pattern. Use "--no-ignore" to override
21
-
22
- ✖ 4 problems (0 errors, 4 warnings)
23
-
24
-  ELIFECYCLE  Command failed.
@@ -0,0 +1,4 @@
1
+
2
+ > jazz-tools@0.7.0-alpha.37 lint /Users/anselm/jazz/jazz/packages/jazz-tools
3
+ > eslint . --ext ts,tsx
4
+
@@ -0,0 +1,140 @@
1
+
2
+ > jazz-tools@0.7.0-alpha.37 test /Users/anselm/jazz/jazz/packages/jazz-tools
3
+ > vitest --run
4
+
5
+
6
+ RUN v0.34.6 /Users/anselm/jazz/jazz/packages/jazz-tools
7
+
8
+ ✓ src/tests/groupsAndAccounts.test.ts (1 test) 41ms
9
+ stdout | unknown test
10
+ TestMap schema undefined
11
+
12
+ ✓ src/tests/deepLoading.test.ts (3 tests) 86ms
13
+ ✓ src/tests/coList.test.ts (10 tests) 160ms
14
+ stdout | src/tests/coList.test.ts > Simple CoList operations > Mutation > splice
15
+ [ 'bread', 'onion' ] 0
16
+ [ 'bread', 'salt', 'onion' ] 1
17
+
18
+ stdout | src/tests/coList.test.ts > CoList resolution > Subscription & auto-resolution
19
+ subscribedList?.[0]?.[0]?.[0] undefined
20
+ subscribedList?.[0]?.[0]?.[0] undefined
21
+ subscribedList?.[0]?.[0]?.[0] a
22
+ subscribedList?.[0]?.[0]?.[0] x
23
+ subscribedList?.[0]?.[0]?.[0] y
24
+ subscribedList?.[0]?.[0]?.[0] w
25
+
26
+ stdout | src/tests/coStream.test.ts > CoStream resolution > Subscription & auto-resolution
27
+ subscribedStream[me.id] {
28
+ value: [Getter],
29
+ ref: [Getter],
30
+ by: [Getter],
31
+ madeAt: 2024-05-24T20:46:44.463Z,
32
+ tx: {
33
+ sessionID: 'co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_z75hUWuWjTuX',
34
+ txIndex: 0
35
+ }
36
+ }
37
+ subscribedStream[me.id]?.value?.[me.id]?.value undefined
38
+ subscribedStream[me.id]?.value?.[me.id]?.value?.[me.id]?.value undefined
39
+ subscribedStream[me.id] {
40
+ value: [Getter],
41
+ ref: [Getter],
42
+ by: [Getter],
43
+ madeAt: 2024-05-24T20:46:44.463Z,
44
+ tx: {
45
+ sessionID: 'co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_z75hUWuWjTuX',
46
+ txIndex: 0
47
+ }
48
+ }
49
+ subscribedStream[me.id]?.value?.[me.id]?.value null
50
+ subscribedStream[me.id]?.value?.[me.id]?.value?.[me.id]?.value undefined
51
+ subscribedStream[me.id] {
52
+ value: [Getter],
53
+ ref: [Getter],
54
+ by: [Getter],
55
+ madeAt: 2024-05-24T20:46:44.463Z,
56
+ tx: {
57
+ sessionID: 'co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_z75hUWuWjTuX',
58
+ txIndex: 0
59
+ }
60
+ }
61
+ subscribedStream[me.id]?.value?.[me.id]?.value {
62
+ id: 'co_zGQiwntfLWcU3pybwp6nq8VhcEr',
63
+ _type: 'CoStream',
64
+ co_zNCLRSU2cPfqfz9aEAA1vL2JsF5: 'milk',
65
+ in: { co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_z75hUWuWjTuX: 'milk' }
66
+ }
67
+ subscribedStream[me.id]?.value?.[me.id]?.value?.[me.id]?.value milk
68
+ subscribedStream[me.id] {
69
+ value: [Getter],
70
+ ref: [Getter],
71
+ by: [Getter],
72
+ madeAt: 2024-05-24T20:46:44.463Z,
73
+ tx: {
74
+ sessionID: 'co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_z75hUWuWjTuX',
75
+ txIndex: 0
76
+ }
77
+ }
78
+ subscribedStream[me.id]?.value?.[me.id]?.value {
79
+ id: 'co_zGQiwntfLWcU3pybwp6nq8VhcEr',
80
+ _type: 'CoStream',
81
+ co_zNCLRSU2cPfqfz9aEAA1vL2JsF5: 'bread',
82
+ in: {
83
+ co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_z75hUWuWjTuX: 'milk',
84
+ co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_zSWSUsvvX8KW: 'bread'
85
+ }
86
+ }
87
+ subscribedStream[me.id]?.value?.[me.id]?.value?.[me.id]?.value bread
88
+ subscribedStream[me.id] {
89
+ value: [Getter],
90
+ ref: [Getter],
91
+ by: [Getter],
92
+ madeAt: 2024-05-24T20:46:44.481Z,
93
+ tx: {
94
+ sessionID: 'co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_zSWSUsvvX8KW',
95
+ txIndex: 0
96
+ }
97
+ }
98
+ subscribedStream[me.id]?.value?.[me.id]?.value {
99
+ id: 'co_zhX5ZcvxQW4w2LJ99rBQ6NmBziS',
100
+ _type: 'CoStream',
101
+ co_zNCLRSU2cPfqfz9aEAA1vL2JsF5: 'butter',
102
+ in: { co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_zSWSUsvvX8KW: 'butter' }
103
+ }
104
+ subscribedStream[me.id]?.value?.[me.id]?.value?.[me.id]?.value butter
105
+ subscribedStream[me.id] {
106
+ value: [Getter],
107
+ ref: [Getter],
108
+ by: [Getter],
109
+ madeAt: 2024-05-24T20:46:44.481Z,
110
+ tx: {
111
+ sessionID: 'co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_zSWSUsvvX8KW',
112
+ txIndex: 0
113
+ }
114
+ }
115
+ subscribedStream[me.id]?.value?.[me.id]?.value {
116
+ id: 'co_zhX5ZcvxQW4w2LJ99rBQ6NmBziS',
117
+ _type: 'CoStream',
118
+ co_zNCLRSU2cPfqfz9aEAA1vL2JsF5: 'jam',
119
+ in: { co_zNCLRSU2cPfqfz9aEAA1vL2JsF5_session_zSWSUsvvX8KW: 'jam' }
120
+ }
121
+ subscribedStream[me.id]?.value?.[me.id]?.value?.[me.id]?.value jam
122
+
123
+ ✓ src/tests/coStream.test.ts (10 tests) 181ms
124
+ ✓ src/tests/coMap.test.ts (11 tests) 203ms
125
+ stdout | src/tests/coMap.test.ts > CoMap resolution > Subscription & auto-resolution
126
+ subscribedMap.nested?.twiceNested?.taste undefined
127
+ subscribedMap.nested?.twiceNested?.taste undefined
128
+ subscribedMap.nested?.twiceNested?.taste sour
129
+ subscribedMap.nested?.twiceNested?.taste sour
130
+ subscribedMap.nested?.twiceNested?.taste sweet
131
+ subscribedMap.nested?.twiceNested?.taste sweet
132
+ subscribedMap.nested?.twiceNested?.taste salty
133
+ subscribedMap.nested?.twiceNested?.taste umami
134
+
135
+
136
+ Test Files 5 passed (5)
137
+ Tests 35 passed (35)
138
+ Start at 21:46:43
139
+ Duration 805ms (transform 212ms, setup 0ms, collect 2.19s, tests 671ms, environment 0ms, prepare 225ms)
140
+
package/CHANGELOG.md CHANGED
@@ -1,5 +1,248 @@
1
1
  # jazz-autosub
2
2
 
3
+ ## 0.7.0-alpha.41
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
8
+ - cojson-transport-nodejs-ws@0.7.0-alpha.41
9
+
10
+ ## 0.7.0-alpha.39
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies
15
+ - cojson@0.7.0-alpha.39
16
+ - cojson-transport-nodejs-ws@0.7.0-alpha.39
17
+
18
+ ## 0.7.0-alpha.38
19
+
20
+ ### Patch Changes
21
+
22
+ - Fix infinite recursion in subscriptionScope
23
+ - Fix type of init param for CoMap.create
24
+ - Implement deep loading, simplify API
25
+ - Updated dependencies
26
+ - cojson@0.7.0-alpha.38
27
+ - cojson-transport-nodejs-ws@0.7.0-alpha.38
28
+
29
+ ## 0.7.0-alpha.37
30
+
31
+ ### Patch Changes
32
+
33
+ - Updated dependencies
34
+ - cojson@0.7.0-alpha.37
35
+ - cojson-transport-nodejs-ws@0.7.0-alpha.37
36
+
37
+ ## 0.7.0-alpha.36
38
+
39
+ ### Patch Changes
40
+
41
+ - 1a35307: Implement first devtools formatters
42
+ - 6b0418f: Fix image resolution loading
43
+ - 1a35307: Add ability to declare minimum required data in subscribe & Improve property access tracing
44
+ - Updated dependencies [1a35307]
45
+ - Updated dependencies [1a35307]
46
+ - cojson@0.7.0-alpha.36
47
+ - cojson-transport-nodejs-ws@0.7.0-alpha.36
48
+
49
+ ## 0.7.0-alpha.35
50
+
51
+ ### Patch Changes
52
+
53
+ - Cache CoValue proxies
54
+ - Updated dependencies
55
+ - cojson@0.7.0-alpha.35
56
+ - cojson-transport-nodejs-ws@0.7.0-alpha.35
57
+
58
+ ## 0.7.0-alpha.34
59
+
60
+ ### Patch Changes
61
+
62
+ - Extract jazz cli into jazz-run package
63
+
64
+ ## 0.7.0-alpha.32
65
+
66
+ ### Patch Changes
67
+
68
+ - Introduce jazz-tools CLI
69
+ - Clean up exports
70
+
71
+ ## 0.7.0-alpha.31
72
+
73
+ ### Patch Changes
74
+
75
+ - Get rid of self generics, new create syntax
76
+
77
+ ## 0.7.0-alpha.30
78
+
79
+ ### Patch Changes
80
+
81
+ - CoValue casting & auto-subbing \_owner
82
+
83
+ ## 0.7.0-alpha.29
84
+
85
+ ### Patch Changes
86
+
87
+ - Reintroduce changes from main
88
+ - Updated dependencies
89
+ - cojson@0.7.0-alpha.29
90
+
91
+ ## 0.7.0-alpha.28
92
+
93
+ ### Patch Changes
94
+
95
+ - Implement profile visibility based on groups & new migration signature
96
+ - Updated dependencies
97
+ - cojson@0.7.0-alpha.28
98
+
99
+ ## 0.7.0-alpha.27
100
+
101
+ ### Patch Changes
102
+
103
+ - Fix CoList.splice / RawCoList.append
104
+ - Fix Costream[...].all
105
+ - Updated dependencies
106
+ - cojson@0.7.0-alpha.27
107
+
108
+ ## 0.7.0-alpha.26
109
+
110
+ ### Patch Changes
111
+
112
+ - Fix CoMap.Record.toJSON()
113
+
114
+ ## 0.7.0-alpha.25
115
+
116
+ ### Patch Changes
117
+
118
+ - Make Account -> Profile a lazy ref schema
119
+
120
+ ## 0.7.0-alpha.24
121
+
122
+ ### Patch Changes
123
+
124
+ - Relax types of CoMap.\_schema
125
+ - Fix schema of Account & Group
126
+ - Make sure delete on CoMaps deletes keys
127
+ - Updated dependencies
128
+ - cojson@0.7.0-alpha.24
129
+
130
+ ## 0.7.0-alpha.23
131
+
132
+ ### Patch Changes
133
+
134
+ - CoMap fixes and improvements
135
+
136
+ ## 0.7.0-alpha.22
137
+
138
+ ### Patch Changes
139
+
140
+ - Fix CoMap \_refs for co.items
141
+
142
+ ## 0.7.0-alpha.21
143
+
144
+ ### Patch Changes
145
+
146
+ - Add maxWidth option for loading images
147
+
148
+ ## 0.7.0-alpha.20
149
+
150
+ ### Patch Changes
151
+
152
+ - Make fast-check a direct dependency to help dev time resolution
153
+
154
+ ## 0.7.0-alpha.19
155
+
156
+ ### Patch Changes
157
+
158
+ - More precise imports from @effect/schema
159
+
160
+ ## 0.7.0-alpha.17
161
+
162
+ ### Patch Changes
163
+
164
+ - Use effect 3.0
165
+
166
+ ## 0.7.0-alpha.16
167
+
168
+ ### Patch Changes
169
+
170
+ - Make CoMaps even more subclassable
171
+
172
+ ## 0.7.0-alpha.15
173
+
174
+ ### Patch Changes
175
+
176
+ - More superclass-compatible CoMaps
177
+
178
+ ## 0.7.0-alpha.14
179
+
180
+ ### Patch Changes
181
+
182
+ - Fix CoStream types
183
+
184
+ ## 0.7.0-alpha.13
185
+
186
+ ### Patch Changes
187
+
188
+ - Add .all to CoStreamEntry
189
+
190
+ ## 0.7.0-alpha.12
191
+
192
+ ### Patch Changes
193
+
194
+ - Fix variance of ID.\_\_type
195
+
196
+ ## 0.7.0-alpha.11
197
+
198
+ ### Patch Changes
199
+
200
+ - Support stricter TS lint rules
201
+ - Updated dependencies
202
+ - cojson@0.7.0-alpha.11
203
+
204
+ ## 0.7.0-alpha.10
205
+
206
+ ### Patch Changes
207
+
208
+ - Clean up API more & re-add jazz-nodejs
209
+ - Updated dependencies
210
+ - cojson@0.7.0-alpha.10
211
+
212
+ ## 0.7.0-alpha.9
213
+
214
+ ### Patch Changes
215
+
216
+ - Even friendlier for subclassing CoMap
217
+
218
+ ## 0.7.0-alpha.8
219
+
220
+ ### Patch Changes
221
+
222
+ - More subclass-friendly types in CoMap
223
+
224
+ ## 0.7.0-alpha.7
225
+
226
+ ### Patch Changes
227
+
228
+ - Consistent proxy based API
229
+ - Updated dependencies
230
+ - cojson@0.7.0-alpha.7
231
+
232
+ ## 0.7.0-alpha.6
233
+
234
+ ### Patch Changes
235
+
236
+ - CoMap fix
237
+
238
+ ## 0.7.0-alpha.5
239
+
240
+ ### Patch Changes
241
+
242
+ - Refactoring
243
+ - Updated dependencies
244
+ - cojson@0.7.0-alpha.5
245
+
3
246
  ## 0.7.0-alpha.4
4
247
 
5
248
  ### Patch Changes
package/README.md CHANGED
@@ -1,3 +1,11 @@
1
- # CoJSON
1
+ # `jazz-tools`
2
2
 
3
- [See the top-level README](../../README.md#cojson)
3
+ The base implementation for Jazz (see [jazz.tools](https://jazz.tools)), a framework for distributed state.
4
+
5
+ Provides a high-level API around the CoJSON protocol.
6
+
7
+ ## Environments and bindings
8
+
9
+ - `jazz-browser` (Vanilla JavaScript)
10
+ - `jazz-react` (React apps)
11
+ - `jazz-nodejs` (NodeJS or Bun server workers)
@@ -1,10 +1,11 @@
1
1
  var _a;
2
2
  import { LocalNode } from "cojson";
3
3
  import { Context } from "effect";
4
- import { CoValueBase, Profile, ValueRef, inspect } from "../internal.js";
4
+ import { Group, CoValueBase, MembersSym, Profile, Ref, SchemaInit, inspect, subscriptionsScopes, } from "../internal.js";
5
+ /** @category Identity & Permissions */
5
6
  export class Account extends CoValueBase {
6
- get _encoding() {
7
- return this.constructor._encoding;
7
+ get _schema() {
8
+ return this.constructor._schema;
8
9
  }
9
10
  get _owner() {
10
11
  return this;
@@ -18,22 +19,14 @@ export class Account extends CoValueBase {
18
19
  const profileID = this._raw.get("profile");
19
20
  const rootID = this._raw.get("root");
20
21
  return {
21
- profile: profileID && (new ValueRef(profileID, this._loadedAs, this._encoding.profile.ref())),
22
+ profile: profileID &&
23
+ new Ref(profileID, this._loadedAs, this._schema.profile),
22
24
  root: rootID &&
23
- new ValueRef(rootID, this._loadedAs, this._encoding.root.ref()),
25
+ new Ref(rootID, this._loadedAs, this._schema.root),
24
26
  };
25
27
  }
26
- constructor(init, options) {
28
+ constructor(options) {
27
29
  super();
28
- this.acceptInvite = (async (valueID, inviteSecret, coValueClass) => {
29
- if (!this.isMe) {
30
- throw new Error("Only a controlled account can accept invites");
31
- }
32
- await this._raw.acceptInvite(valueID, inviteSecret);
33
- return coValueClass.load(valueID, {
34
- as: this,
35
- });
36
- });
37
30
  if (!("fromRaw" in options)) {
38
31
  throw new Error("Can only construct account from raw or with .create()");
39
32
  }
@@ -44,47 +37,35 @@ export class Account extends CoValueBase {
44
37
  enumerable: false,
45
38
  },
46
39
  _raw: { value: options.fromRaw, enumerable: false },
47
- profile: {
48
- get: () => {
49
- const ref = this._refs.profile;
50
- return ref ? ref.accessFrom(this) : undefined;
51
- },
52
- set: (value) => {
53
- if (value) {
54
- this._raw.set("profile", value.id);
55
- }
56
- },
57
- },
58
- root: {
59
- get: () => {
60
- const ref = this._refs.root;
61
- return ref ? ref.accessFrom(this) : undefined;
62
- },
63
- set: (value) => {
64
- if (value) {
65
- this._raw.set("root", value.id);
66
- }
67
- },
68
- },
40
+ _type: { value: "Account", enumerable: false },
69
41
  });
70
42
  if (this.isMe) {
71
43
  this.sessionID =
72
44
  options.fromRaw.core.node.currentSessionID;
73
45
  }
46
+ return new Proxy(this, AccountAndGroupProxyHandler);
74
47
  }
75
48
  myRole() {
76
49
  if (this.isMe) {
77
50
  return "admin";
78
51
  }
79
52
  }
53
+ async acceptInvite(valueID, inviteSecret, coValueClass) {
54
+ if (!this.isMe) {
55
+ throw new Error("Only a controlled account can accept invites");
56
+ }
57
+ await this._raw.acceptInvite(valueID, inviteSecret);
58
+ return coValueClass.load(valueID, this, []);
59
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
+ }
80
61
  static async create(options) {
81
62
  const { node } = await LocalNode.withNewlyCreatedAccount({
82
63
  ...options,
83
- migration: async (rawAccount) => {
84
- const account = new this(undefined, {
64
+ migration: async (rawAccount, _node, creationProps) => {
65
+ const account = new this({
85
66
  fromRaw: rawAccount,
86
67
  });
87
- await account.migrate?.();
68
+ await account.migrate?.(creationProps);
88
69
  },
89
70
  });
90
71
  return this.fromNode(node);
@@ -95,20 +76,22 @@ export class Account extends CoValueBase {
95
76
  accountSecret: options.accountSecret,
96
77
  sessionID: options.sessionID,
97
78
  peersToLoadFrom: options.peersToLoadFrom,
98
- migration: async (rawAccount) => {
99
- const account = new this(undefined, {
79
+ crypto: options.crypto,
80
+ migration: async (rawAccount, _node, creationProps) => {
81
+ const account = new this({
100
82
  fromRaw: rawAccount,
101
83
  });
102
- await account.migrate?.();
84
+ await account.migrate?.(creationProps);
103
85
  },
104
86
  });
105
87
  return this.fromNode(node);
106
88
  }
107
89
  static fromNode(node) {
108
- return new this(undefined, {
90
+ return new this({
109
91
  fromRaw: node.account,
110
92
  });
111
93
  }
94
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
112
95
  toJSON() {
113
96
  return {
114
97
  id: this.id,
@@ -118,20 +101,91 @@ export class Account extends CoValueBase {
118
101
  [inspect]() {
119
102
  return this.toJSON();
120
103
  }
121
- static encoding(fields) {
122
- this._encoding || (this._encoding = {});
123
- Object.assign(this._encoding, fields);
104
+ migrate(creationProps) {
105
+ if (creationProps) {
106
+ const profileGroup = Group.create({ owner: this });
107
+ profileGroup.addMember("everyone", "reader");
108
+ this.profile = Profile.create({ name: creationProps.name }, { owner: profileGroup });
109
+ }
124
110
  }
125
111
  }
126
112
  _a = Account;
127
113
  (() => {
128
- _a._encoding = {
129
- profile: { ref: () => Profile },
130
- root: { json: true },
114
+ _a._schema = {
115
+ profile: () => Profile,
116
+ root: "json",
117
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
131
118
  };
132
119
  })();
120
+ export const AccountAndGroupProxyHandler = {
121
+ get(target, key, receiver) {
122
+ if (key === "profile") {
123
+ const ref = target._refs.profile;
124
+ return ref
125
+ ? ref.accessFrom(receiver, "profile")
126
+ : // eslint-disable-next-line @typescript-eslint/no-explicit-any
127
+ undefined;
128
+ }
129
+ else if (key === "root") {
130
+ const ref = target._refs.root;
131
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
132
+ return ref ? ref.accessFrom(receiver, "root") : undefined;
133
+ }
134
+ else {
135
+ return Reflect.get(target, key, receiver);
136
+ }
137
+ },
138
+ set(target, key, value, receiver) {
139
+ var _b;
140
+ if ((key === "profile" || key === "root" || key === MembersSym) &&
141
+ typeof value === "object" &&
142
+ SchemaInit in value) {
143
+ (_b = target.constructor)._schema || (_b._schema = {});
144
+ target.constructor._schema[key] =
145
+ value[SchemaInit];
146
+ return true;
147
+ }
148
+ else if (key === "profile") {
149
+ if (value) {
150
+ target._raw.set("profile", value.id, "trusting");
151
+ }
152
+ subscriptionsScopes
153
+ .get(receiver)
154
+ ?.onRefAccessedOrSet(target.id, value.id);
155
+ return true;
156
+ }
157
+ else if (key === "root") {
158
+ if (value) {
159
+ target._raw.set("root", value.id);
160
+ }
161
+ subscriptionsScopes
162
+ .get(receiver)
163
+ ?.onRefAccessedOrSet(target.id, value.id);
164
+ return true;
165
+ }
166
+ else {
167
+ return Reflect.set(target, key, value, receiver);
168
+ }
169
+ },
170
+ defineProperty(target, key, descriptor) {
171
+ var _b;
172
+ if ((key === "profile" || key === "root" || key === MembersSym) &&
173
+ typeof descriptor.value === "object" &&
174
+ SchemaInit in descriptor.value) {
175
+ (_b = target.constructor)._schema || (_b._schema = {});
176
+ target.constructor._schema[key] =
177
+ descriptor.value[SchemaInit];
178
+ return true;
179
+ }
180
+ else {
181
+ return Reflect.defineProperty(target, key, descriptor);
182
+ }
183
+ },
184
+ };
185
+ /** @category Identity & Permissions */
133
186
  export class AccountCtx extends Context.Tag("Account")() {
134
187
  }
188
+ /** @category Identity & Permissions */
135
189
  export function isControlledAccount(account) {
136
190
  return account.isMe;
137
191
  }