jazz-tools 0.7.35 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +2 -2
- package/.turbo/turbo-lint.log +1 -1
- package/CHANGELOG.md +23 -1
- package/dist/coValues/account.js +27 -36
- package/dist/coValues/account.js.map +1 -1
- package/dist/coValues/coList.js +19 -21
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/coMap.js +29 -23
- package/dist/coValues/coMap.js.map +1 -1
- package/dist/coValues/coStream.js +26 -21
- package/dist/coValues/coStream.js.map +1 -1
- package/dist/coValues/extensions/imageDef.js +3 -8
- package/dist/coValues/extensions/imageDef.js.map +1 -1
- package/dist/coValues/group.js +20 -23
- package/dist/coValues/group.js.map +1 -1
- package/dist/coValues/interfaces.js +5 -2
- package/dist/coValues/interfaces.js.map +1 -1
- package/dist/implementation/createContext.js +144 -0
- package/dist/implementation/createContext.js.map +1 -0
- package/dist/implementation/refs.js +11 -2
- package/dist/implementation/refs.js.map +1 -1
- package/dist/implementation/subscriptionScope.js +12 -8
- package/dist/implementation/subscriptionScope.js.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/internal.js +1 -0
- package/dist/internal.js.map +1 -1
- package/dist/tests/coList.test.js +23 -15
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +89 -139
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coStream.test.js +26 -22
- package/dist/tests/coStream.test.js.map +1 -1
- package/dist/tests/deepLoading.test.js +21 -34
- package/dist/tests/deepLoading.test.js.map +1 -1
- package/dist/tests/groupsAndAccounts.test.js +8 -22
- package/dist/tests/groupsAndAccounts.test.js.map +1 -1
- package/package.json +3 -3
- package/src/coValues/account.ts +14 -31
- package/src/coValues/coList.ts +9 -5
- package/src/coValues/coMap.ts +40 -9
- package/src/coValues/coStream.ts +16 -7
- package/src/coValues/group.ts +2 -2
- package/src/coValues/interfaces.ts +12 -8
- package/src/implementation/createContext.ts +268 -0
- package/src/implementation/refs.ts +13 -6
- package/src/implementation/subscriptionScope.ts +28 -25
- package/src/index.ts +12 -1
- package/src/internal.ts +2 -0
- package/src/tests/coList.test.ts +30 -14
- package/src/tests/coMap.test.ts +56 -27
- package/src/tests/coStream.test.ts +27 -21
- package/src/tests/deepLoading.test.ts +15 -11
- package/tsconfig.json +1 -1
@@ -8,6 +8,7 @@ import {
|
|
8
8
|
Ref,
|
9
9
|
inspect,
|
10
10
|
subscriptionsScopes,
|
11
|
+
AnonymousJazzAgent,
|
11
12
|
} from "../internal.js";
|
12
13
|
import { fulfillsDepth } from "./deepLoading.js";
|
13
14
|
|
@@ -35,7 +36,7 @@ export interface CoValue {
|
|
35
36
|
/** @category Internals */
|
36
37
|
_raw: RawCoValue;
|
37
38
|
/** @internal */
|
38
|
-
readonly _loadedAs: Account;
|
39
|
+
readonly _loadedAs: Account | AnonymousJazzAgent;
|
39
40
|
/** @category Stringifying & Inspection */
|
40
41
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
41
42
|
toJSON(key?: string, seenAbove?: ID<CoValue>[]): any[] | object | string;
|
@@ -63,11 +64,11 @@ type IDMarker<out T> = { __type(_: never): T };
|
|
63
64
|
|
64
65
|
/** @internal */
|
65
66
|
export class CoValueBase implements CoValue {
|
66
|
-
id
|
67
|
-
_type
|
68
|
-
_raw
|
67
|
+
declare id: ID<this>;
|
68
|
+
declare _type: string;
|
69
|
+
declare _raw: RawCoValue;
|
69
70
|
/** @category Internals */
|
70
|
-
_instanceID
|
71
|
+
declare _instanceID: string;
|
71
72
|
|
72
73
|
get _owner(): Account | Group {
|
73
74
|
const owner =
|
@@ -86,7 +87,10 @@ export class CoValueBase implements CoValue {
|
|
86
87
|
|
87
88
|
/** @private */
|
88
89
|
get _loadedAs() {
|
89
|
-
|
90
|
+
if (this._raw.core.node.account instanceof RawAccount) {
|
91
|
+
return Account.fromRaw(this._raw.core.node.account);
|
92
|
+
}
|
93
|
+
return new AnonymousJazzAgent(this._raw.core.node);
|
90
94
|
}
|
91
95
|
|
92
96
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
@@ -134,7 +138,7 @@ export class CoValueBase implements CoValue {
|
|
134
138
|
export function loadCoValue<V extends CoValue, Depth>(
|
135
139
|
cls: CoValueClass<V>,
|
136
140
|
id: ID<V>,
|
137
|
-
as: Account,
|
141
|
+
as: Account | AnonymousJazzAgent,
|
138
142
|
depth: Depth & DepthsIn<V>,
|
139
143
|
): Promise<DeeplyLoaded<V, Depth> | undefined> {
|
140
144
|
return new Promise((resolve) => {
|
@@ -170,7 +174,7 @@ export function ensureCoValueLoaded<V extends CoValue, Depth>(
|
|
170
174
|
export function subscribeToCoValue<V extends CoValue, Depth>(
|
171
175
|
cls: CoValueClass<V>,
|
172
176
|
id: ID<V>,
|
173
|
-
as: Account,
|
177
|
+
as: Account | AnonymousJazzAgent,
|
174
178
|
depth: Depth & DepthsIn<V>,
|
175
179
|
listener: (value: DeeplyLoaded<V, Depth>) => void,
|
176
180
|
onUnavailable?: () => void,
|
@@ -0,0 +1,268 @@
|
|
1
|
+
import {
|
2
|
+
AgentSecret,
|
3
|
+
CoID,
|
4
|
+
ControlledAgent,
|
5
|
+
CryptoProvider,
|
6
|
+
LocalNode,
|
7
|
+
Peer,
|
8
|
+
RawAccount,
|
9
|
+
RawAccountID,
|
10
|
+
SessionID,
|
11
|
+
} from "cojson";
|
12
|
+
import { Account, AccountClass, ID } from "../internal.js";
|
13
|
+
|
14
|
+
export type AuthResult =
|
15
|
+
| {
|
16
|
+
type: "existing";
|
17
|
+
credentials: { accountID: ID<Account>; secret: AgentSecret };
|
18
|
+
onSuccess: () => void;
|
19
|
+
onError: (error: string | Error) => void;
|
20
|
+
logOut: () => void;
|
21
|
+
}
|
22
|
+
| {
|
23
|
+
type: "new";
|
24
|
+
creationProps: { name: string };
|
25
|
+
initialSecret?: AgentSecret;
|
26
|
+
saveCredentials: (credentials: {
|
27
|
+
accountID: ID<Account>;
|
28
|
+
secret: AgentSecret;
|
29
|
+
}) => Promise<void>;
|
30
|
+
onSuccess: () => void;
|
31
|
+
onError: (error: string | Error) => void;
|
32
|
+
logOut: () => void;
|
33
|
+
};
|
34
|
+
|
35
|
+
export interface AuthMethod {
|
36
|
+
start(crypto: CryptoProvider): Promise<AuthResult>;
|
37
|
+
}
|
38
|
+
|
39
|
+
export const fixedCredentialsAuth = (credentials: {
|
40
|
+
accountID: ID<Account>;
|
41
|
+
secret: AgentSecret;
|
42
|
+
}): AuthMethod => {
|
43
|
+
return {
|
44
|
+
start: async () => ({
|
45
|
+
type: "existing",
|
46
|
+
credentials,
|
47
|
+
onSuccess: () => {},
|
48
|
+
onError: () => {},
|
49
|
+
logOut: () => {},
|
50
|
+
}),
|
51
|
+
};
|
52
|
+
};
|
53
|
+
|
54
|
+
export const ephemeralCredentialsAuth = (): AuthMethod => {
|
55
|
+
return {
|
56
|
+
start: async () => ({
|
57
|
+
type: "new",
|
58
|
+
creationProps: { name: "Ephemeral" },
|
59
|
+
saveCredentials: async () => {},
|
60
|
+
onSuccess: () => {},
|
61
|
+
onError: () => {},
|
62
|
+
logOut: () => {},
|
63
|
+
}),
|
64
|
+
};
|
65
|
+
};
|
66
|
+
|
67
|
+
export async function randomSessionProvider(
|
68
|
+
accountID: ID<Account>,
|
69
|
+
crypto: CryptoProvider,
|
70
|
+
) {
|
71
|
+
return {
|
72
|
+
sessionID: crypto.newRandomSessionID(
|
73
|
+
accountID as unknown as RawAccountID,
|
74
|
+
),
|
75
|
+
sessionDone: () => {},
|
76
|
+
};
|
77
|
+
}
|
78
|
+
|
79
|
+
type ContextParamsWithAuth<Acc extends Account> = {
|
80
|
+
AccountSchema?: AccountClass<Acc>;
|
81
|
+
auth: AuthMethod;
|
82
|
+
sessionProvider: (
|
83
|
+
accountID: ID<Account>,
|
84
|
+
crypto: CryptoProvider,
|
85
|
+
) => Promise<{ sessionID: SessionID; sessionDone: () => void }>;
|
86
|
+
} & BaseContextParams;
|
87
|
+
|
88
|
+
type BaseContextParams = {
|
89
|
+
peersToLoadFrom: Peer[];
|
90
|
+
crypto: CryptoProvider;
|
91
|
+
};
|
92
|
+
|
93
|
+
export type JazzContextWithAccount<Acc extends Account> = {
|
94
|
+
account: Acc;
|
95
|
+
done: () => void;
|
96
|
+
logOut: () => void;
|
97
|
+
};
|
98
|
+
|
99
|
+
export type JazzContextWithAgent = {
|
100
|
+
agent: AnonymousJazzAgent;
|
101
|
+
done: () => void;
|
102
|
+
logOut: () => void;
|
103
|
+
};
|
104
|
+
|
105
|
+
export type JazzContext<Acc extends Account> = JazzContextWithAccount<Acc> | JazzContextWithAgent;
|
106
|
+
|
107
|
+
export async function createJazzContext<Acc extends Account>({
|
108
|
+
AccountSchema,
|
109
|
+
auth,
|
110
|
+
sessionProvider,
|
111
|
+
peersToLoadFrom,
|
112
|
+
crypto,
|
113
|
+
}: ContextParamsWithAuth<Acc>): Promise<JazzContextWithAccount<Acc>>;
|
114
|
+
export async function createJazzContext({
|
115
|
+
peersToLoadFrom,
|
116
|
+
crypto,
|
117
|
+
}: BaseContextParams): Promise<JazzContextWithAgent>;
|
118
|
+
export async function createJazzContext<Acc extends Account>(
|
119
|
+
options: ContextParamsWithAuth<Acc> | BaseContextParams,
|
120
|
+
): Promise<
|
121
|
+
JazzContext<Acc>
|
122
|
+
>
|
123
|
+
export async function createJazzContext<Acc extends Account>(
|
124
|
+
options: ContextParamsWithAuth<Acc> | BaseContextParams,
|
125
|
+
): Promise<
|
126
|
+
JazzContext<Acc>
|
127
|
+
> {
|
128
|
+
// eslint-disable-next-line no-constant-condition
|
129
|
+
while (true) {
|
130
|
+
if (!("auth" in options)) {
|
131
|
+
return createAnonymousJazzContext({
|
132
|
+
peersToLoadFrom: options.peersToLoadFrom,
|
133
|
+
crypto: options.crypto,
|
134
|
+
});
|
135
|
+
}
|
136
|
+
|
137
|
+
const { auth, sessionProvider, peersToLoadFrom, crypto } = options;
|
138
|
+
const AccountSchema =
|
139
|
+
options.AccountSchema ?? (Account as unknown as AccountClass<Acc>);
|
140
|
+
|
141
|
+
const authResult = await auth.start(crypto);
|
142
|
+
|
143
|
+
if (authResult.type === "existing") {
|
144
|
+
try {
|
145
|
+
const { sessionID, sessionDone } = await sessionProvider(
|
146
|
+
authResult.credentials.accountID,
|
147
|
+
crypto,
|
148
|
+
);
|
149
|
+
|
150
|
+
try {
|
151
|
+
const node = await LocalNode.withLoadedAccount({
|
152
|
+
accountID: authResult.credentials
|
153
|
+
.accountID as unknown as CoID<RawAccount>,
|
154
|
+
accountSecret: authResult.credentials.secret,
|
155
|
+
sessionID: sessionID,
|
156
|
+
peersToLoadFrom: peersToLoadFrom,
|
157
|
+
crypto: crypto,
|
158
|
+
migration: async (rawAccount, _node, creationProps) => {
|
159
|
+
const account = new AccountSchema({
|
160
|
+
fromRaw: rawAccount,
|
161
|
+
}) as Acc;
|
162
|
+
|
163
|
+
await account.migrate?.(creationProps);
|
164
|
+
},
|
165
|
+
});
|
166
|
+
|
167
|
+
const account = AccountSchema.fromNode(node);
|
168
|
+
authResult.onSuccess();
|
169
|
+
|
170
|
+
return {
|
171
|
+
account,
|
172
|
+
done: () => {
|
173
|
+
node.gracefulShutdown();
|
174
|
+
sessionDone();
|
175
|
+
},
|
176
|
+
logOut: () => {
|
177
|
+
node.gracefulShutdown();
|
178
|
+
sessionDone();
|
179
|
+
authResult.logOut();
|
180
|
+
},
|
181
|
+
};
|
182
|
+
} catch (e) {
|
183
|
+
authResult.onError(
|
184
|
+
new Error("Error loading account", { cause: e }),
|
185
|
+
);
|
186
|
+
sessionDone();
|
187
|
+
}
|
188
|
+
} catch (e) {
|
189
|
+
authResult.onError(
|
190
|
+
new Error("Error acquiring sessionID", { cause: e }),
|
191
|
+
);
|
192
|
+
}
|
193
|
+
} else if (authResult.type === "new") {
|
194
|
+
try {
|
195
|
+
// TODO: figure out a way to not "waste" the first SessionID
|
196
|
+
const { node } = await LocalNode.withNewlyCreatedAccount({
|
197
|
+
creationProps: authResult.creationProps,
|
198
|
+
peersToLoadFrom: peersToLoadFrom,
|
199
|
+
crypto: crypto,
|
200
|
+
initialAgentSecret: authResult.initialSecret,
|
201
|
+
migration: async (rawAccount, _node, creationProps) => {
|
202
|
+
const account = new AccountSchema({
|
203
|
+
fromRaw: rawAccount,
|
204
|
+
}) as Acc;
|
205
|
+
|
206
|
+
await account.migrate?.(creationProps);
|
207
|
+
},
|
208
|
+
});
|
209
|
+
|
210
|
+
const account = AccountSchema.fromNode(node);
|
211
|
+
|
212
|
+
await authResult.saveCredentials({
|
213
|
+
accountID: node.account.id as unknown as ID<Account>,
|
214
|
+
secret: node.account.agentSecret,
|
215
|
+
});
|
216
|
+
|
217
|
+
authResult.onSuccess();
|
218
|
+
|
219
|
+
return {
|
220
|
+
account,
|
221
|
+
done: () => {
|
222
|
+
node.gracefulShutdown();
|
223
|
+
},
|
224
|
+
logOut: () => {
|
225
|
+
node.gracefulShutdown();
|
226
|
+
authResult.logOut();
|
227
|
+
},
|
228
|
+
};
|
229
|
+
} catch (e) {
|
230
|
+
authResult.onError(
|
231
|
+
new Error("Error creating account", { cause: e }),
|
232
|
+
);
|
233
|
+
}
|
234
|
+
}
|
235
|
+
}
|
236
|
+
}
|
237
|
+
|
238
|
+
export class AnonymousJazzAgent {
|
239
|
+
_type = "Anonymous" as const;
|
240
|
+
constructor(public node: LocalNode) {}
|
241
|
+
}
|
242
|
+
|
243
|
+
export async function createAnonymousJazzContext({
|
244
|
+
peersToLoadFrom,
|
245
|
+
crypto,
|
246
|
+
}: {
|
247
|
+
peersToLoadFrom: Peer[];
|
248
|
+
crypto: CryptoProvider;
|
249
|
+
}): Promise<JazzContextWithAgent> {
|
250
|
+
const agentSecret = crypto.newRandomAgentSecret();
|
251
|
+
const rawAgent = new ControlledAgent(agentSecret, crypto);
|
252
|
+
|
253
|
+
const node = new LocalNode(
|
254
|
+
rawAgent,
|
255
|
+
crypto.newRandomSessionID(rawAgent.id),
|
256
|
+
crypto,
|
257
|
+
);
|
258
|
+
|
259
|
+
for (const peer of peersToLoadFrom) {
|
260
|
+
node.syncManager.addPeer(peer);
|
261
|
+
}
|
262
|
+
|
263
|
+
return {
|
264
|
+
agent: new AnonymousJazzAgent(node),
|
265
|
+
done: () => {},
|
266
|
+
logOut: () => {},
|
267
|
+
};
|
268
|
+
}
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import type { CoID, RawCoValue } from "cojson";
|
2
2
|
import type {
|
3
3
|
Account,
|
4
|
+
AnonymousJazzAgent,
|
4
5
|
CoValue,
|
5
6
|
ID,
|
6
7
|
RefEncoded,
|
@@ -19,7 +20,7 @@ const TRACE_ACCESSES = false;
|
|
19
20
|
export class Ref<out V extends CoValue> {
|
20
21
|
constructor(
|
21
22
|
readonly id: ID<V>,
|
22
|
-
readonly controlledAccount: Account,
|
23
|
+
readonly controlledAccount: Account | AnonymousJazzAgent,
|
23
24
|
readonly schema: RefEncoded<V>,
|
24
25
|
) {
|
25
26
|
if (!isRefEncoded(schema)) {
|
@@ -28,9 +29,11 @@ export class Ref<out V extends CoValue> {
|
|
28
29
|
}
|
29
30
|
|
30
31
|
get value() {
|
31
|
-
const
|
32
|
-
this.
|
33
|
-
|
32
|
+
const node =
|
33
|
+
"node" in this.controlledAccount
|
34
|
+
? this.controlledAccount.node
|
35
|
+
: this.controlledAccount._raw.core.node;
|
36
|
+
const raw = node.getLoaded(this.id as unknown as CoID<RawCoValue>);
|
34
37
|
if (raw) {
|
35
38
|
let value = refCache.get(raw);
|
36
39
|
if (value) {
|
@@ -48,7 +51,11 @@ export class Ref<out V extends CoValue> {
|
|
48
51
|
private async loadHelper(options?: {
|
49
52
|
onProgress: (p: number) => void;
|
50
53
|
}): Promise<V | "unavailable"> {
|
51
|
-
const
|
54
|
+
const node =
|
55
|
+
"node" in this.controlledAccount
|
56
|
+
? this.controlledAccount.node
|
57
|
+
: this.controlledAccount._raw.core.node;
|
58
|
+
const raw = await node.load(
|
52
59
|
this.id as unknown as CoID<RawCoValue>,
|
53
60
|
options?.onProgress,
|
54
61
|
);
|
@@ -117,7 +124,7 @@ export class Ref<out V extends CoValue> {
|
|
117
124
|
export function makeRefs<Keys extends string | number>(
|
118
125
|
getIdForKey: (key: Keys) => ID<CoValue> | undefined,
|
119
126
|
getKeysWithIds: () => Keys[],
|
120
|
-
controlledAccount: Account,
|
127
|
+
controlledAccount: Account | AnonymousJazzAgent,
|
121
128
|
refSchemaForKey: (key: Keys) => RefEncoded<CoValue>,
|
122
129
|
): { [K in Keys]: Ref<CoValue> } & {
|
123
130
|
[Symbol.iterator]: () => IterableIterator<Ref<CoValue>>;
|
@@ -5,6 +5,7 @@ import type {
|
|
5
5
|
ID,
|
6
6
|
CoValueClass,
|
7
7
|
CoValueFromRaw,
|
8
|
+
AnonymousJazzAgent,
|
8
9
|
} from "../internal.js";
|
9
10
|
|
10
11
|
export const subscriptionsScopes = new WeakMap<
|
@@ -17,7 +18,7 @@ const TRACE_INVALIDATIONS = false;
|
|
17
18
|
|
18
19
|
export class SubscriptionScope<Root extends CoValue> {
|
19
20
|
scopeID: string = `scope-${Math.random().toString(36).slice(2)}`;
|
20
|
-
subscriber: Account;
|
21
|
+
subscriber: Account | AnonymousJazzAgent;
|
21
22
|
entries = new Map<
|
22
23
|
ID<CoValue>,
|
23
24
|
| { state: "loading"; immediatelyUnsub?: boolean }
|
@@ -89,32 +90,34 @@ export class SubscriptionScope<Root extends CoValue> {
|
|
89
90
|
immediatelyUnsub: false,
|
90
91
|
} as const;
|
91
92
|
this.entries.set(accessedOrSetId, loadingEntry);
|
92
|
-
|
93
|
-
.
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
93
|
+
const node =
|
94
|
+
this.subscriber._type === "Account"
|
95
|
+
? this.subscriber._raw.core.node
|
96
|
+
: this.subscriber.node;
|
97
|
+
void node.loadCoValueCore(accessedOrSetId).then((core) => {
|
98
|
+
if (
|
99
|
+
loadingEntry.state === "loading" &&
|
100
|
+
loadingEntry.immediatelyUnsub
|
101
|
+
) {
|
102
|
+
return;
|
103
|
+
}
|
104
|
+
if (core !== "unavailable") {
|
105
|
+
const entry = {
|
106
|
+
state: "loaded" as const,
|
107
|
+
rawUnsub: () => {}, // placeholder
|
108
|
+
};
|
109
|
+
this.entries.set(accessedOrSetId, entry);
|
107
110
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
111
|
+
const rawUnsub = core.subscribe((rawUpdate) => {
|
112
|
+
// console.log("ref update", this.scopeID, accessedOrSetId, JSON.stringify(rawUpdate))
|
113
|
+
if (!rawUpdate) return;
|
114
|
+
this.invalidate(accessedOrSetId);
|
115
|
+
this.scheduleUpdate();
|
116
|
+
});
|
114
117
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
+
entry.rawUnsub = rawUnsub;
|
119
|
+
}
|
120
|
+
});
|
118
121
|
}
|
119
122
|
}
|
120
123
|
|
package/src/index.ts
CHANGED
@@ -11,6 +11,7 @@ export type {
|
|
11
11
|
AgentID,
|
12
12
|
SyncMessage,
|
13
13
|
CryptoProvider,
|
14
|
+
CoValueUniqueness,
|
14
15
|
} from "cojson";
|
15
16
|
|
16
17
|
export type { ID, CoValue } from "./internal.js";
|
@@ -21,9 +22,19 @@ export { CoMap, type CoMapInit } from "./internal.js";
|
|
21
22
|
export { CoList } from "./internal.js";
|
22
23
|
export { CoStream, BinaryCoStream } from "./internal.js";
|
23
24
|
export { Group, Profile } from "./internal.js";
|
24
|
-
export { Account, isControlledAccount } from "./internal.js";
|
25
|
+
export { Account, isControlledAccount, type AccountClass } from "./internal.js";
|
25
26
|
export { ImageDefinition } from "./internal.js";
|
26
27
|
export { CoValueBase, type CoValueClass } from "./internal.js";
|
27
28
|
export type { DepthsIn, DeeplyLoaded } from "./internal.js";
|
28
29
|
|
29
30
|
export { loadCoValue, subscribeToCoValue } from "./internal.js";
|
31
|
+
|
32
|
+
export {
|
33
|
+
type AuthMethod,
|
34
|
+
type AuthResult,
|
35
|
+
createJazzContext,
|
36
|
+
fixedCredentialsAuth,
|
37
|
+
ephemeralCredentialsAuth,
|
38
|
+
AnonymousJazzAgent,
|
39
|
+
createAnonymousJazzContext,
|
40
|
+
} from "./internal.js";
|
package/src/internal.ts
CHANGED
package/src/tests/coList.test.ts
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
import { expect, describe, test } from "vitest";
|
2
2
|
import { connectedPeers } from "cojson/src/streamUtils.js";
|
3
|
-
import { newRandomSessionID } from "cojson/src/coValueCore.js";
|
4
3
|
import {
|
5
4
|
Account,
|
6
5
|
CoList,
|
7
6
|
WasmCrypto,
|
8
7
|
co,
|
9
8
|
cojsonInternals,
|
9
|
+
createJazzContext,
|
10
10
|
isControlledAccount,
|
11
|
+
fixedCredentialsAuth,
|
11
12
|
} from "../index.js";
|
13
|
+
import { randomSessionProvider } from "../internal.js";
|
12
14
|
|
13
15
|
const Crypto = await WasmCrypto.create();
|
14
16
|
|
@@ -169,12 +171,13 @@ describe("CoList resolution", async () => {
|
|
169
171
|
throw "me is not a controlled account";
|
170
172
|
}
|
171
173
|
me._raw.core.node.syncManager.addPeer(secondPeer);
|
172
|
-
const meOnSecondPeer = await
|
173
|
-
|
174
|
-
|
174
|
+
const { account: meOnSecondPeer } = await createJazzContext({
|
175
|
+
auth: fixedCredentialsAuth({
|
176
|
+
accountID: me.id,
|
177
|
+
secret: me._raw.agentSecret,
|
178
|
+
}),
|
179
|
+
sessionProvider: randomSessionProvider,
|
175
180
|
peersToLoadFrom: [initialAsPeer],
|
176
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
177
|
-
sessionID: newRandomSessionID(me.id as any),
|
178
181
|
crypto: Crypto,
|
179
182
|
});
|
180
183
|
|
@@ -192,7 +195,11 @@ describe("CoList resolution", async () => {
|
|
192
195
|
expect(loadedList?.[0]).toBeDefined();
|
193
196
|
expect(loadedList?.[0]?.[0]).toBe(null);
|
194
197
|
expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]![0]!.id);
|
195
|
-
|
198
|
+
// TODO: this should be ref equal
|
199
|
+
// expect(loadedList?._refs[0]?.value).toEqual(loadedNestedList);
|
200
|
+
expect(loadedList?._refs[0]?.value?.toJSON()).toEqual(
|
201
|
+
loadedNestedList?.toJSON(),
|
202
|
+
);
|
196
203
|
|
197
204
|
const loadedTwiceNestedList = await TwiceNestedList.load(
|
198
205
|
list[0]![0]!.id,
|
@@ -204,7 +211,11 @@ describe("CoList resolution", async () => {
|
|
204
211
|
expect(loadedList?.[0]?.[0]?.[0]).toBe("a");
|
205
212
|
expect(loadedList?.[0]?.[0]?.joined()).toBe("a,b");
|
206
213
|
expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]?.[0]?.id);
|
207
|
-
|
214
|
+
// TODO: this should be ref equal
|
215
|
+
// expect(loadedList?.[0]?._refs[0]?.value).toEqual(loadedTwiceNestedList);
|
216
|
+
expect(loadedList?.[0]?._refs[0]?.value?.toJSON()).toEqual(
|
217
|
+
loadedTwiceNestedList?.toJSON(),
|
218
|
+
);
|
208
219
|
|
209
220
|
const otherNestedList = NestedList.create(
|
210
221
|
[TwiceNestedList.create(["e", "f"], { owner: meOnSecondPeer })],
|
@@ -212,7 +223,11 @@ describe("CoList resolution", async () => {
|
|
212
223
|
);
|
213
224
|
|
214
225
|
loadedList![0] = otherNestedList;
|
215
|
-
|
226
|
+
// TODO: this should be ref equal
|
227
|
+
// expect(loadedList?.[0]).toEqual(otherNestedList);
|
228
|
+
expect(loadedList?._refs[0]?.value?.toJSON()).toEqual(
|
229
|
+
otherNestedList.toJSON(),
|
230
|
+
);
|
216
231
|
expect(loadedList?._refs[0]?.id).toEqual(otherNestedList.id);
|
217
232
|
});
|
218
233
|
|
@@ -231,12 +246,13 @@ describe("CoList resolution", async () => {
|
|
231
246
|
throw "me is not a controlled account";
|
232
247
|
}
|
233
248
|
me._raw.core.node.syncManager.addPeer(secondPeer);
|
234
|
-
const meOnSecondPeer = await
|
235
|
-
|
236
|
-
|
249
|
+
const { account: meOnSecondPeer } = await createJazzContext({
|
250
|
+
auth: fixedCredentialsAuth({
|
251
|
+
accountID: me.id,
|
252
|
+
secret: me._raw.agentSecret,
|
253
|
+
}),
|
254
|
+
sessionProvider: randomSessionProvider,
|
237
255
|
peersToLoadFrom: [initialAsPeer],
|
238
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
239
|
-
sessionID: newRandomSessionID(me.id as any),
|
240
256
|
crypto: Crypto,
|
241
257
|
});
|
242
258
|
|