jazz-tools 0.7.35-new-auth.0 → 0.7.35

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. package/.turbo/turbo-build.log +2 -2
  2. package/.turbo/turbo-test.log +144 -0
  3. package/CHANGELOG.md +8 -2
  4. package/dist/coValues/account.js +35 -20
  5. package/dist/coValues/account.js.map +1 -1
  6. package/dist/coValues/coList.js +19 -14
  7. package/dist/coValues/coList.js.map +1 -1
  8. package/dist/coValues/coMap.js +30 -14
  9. package/dist/coValues/coMap.js.map +1 -1
  10. package/dist/coValues/coStream.js +37 -15
  11. package/dist/coValues/coStream.js.map +1 -1
  12. package/dist/coValues/extensions/imageDef.js +8 -3
  13. package/dist/coValues/extensions/imageDef.js.map +1 -1
  14. package/dist/coValues/group.js +23 -20
  15. package/dist/coValues/group.js.map +1 -1
  16. package/dist/implementation/devtoolsFormatters.js +1 -0
  17. package/dist/implementation/devtoolsFormatters.js.map +1 -1
  18. package/dist/implementation/refs.js +0 -3
  19. package/dist/implementation/refs.js.map +1 -1
  20. package/dist/implementation/schema.js.map +1 -1
  21. package/dist/implementation/subscriptionScope.js +5 -8
  22. package/dist/implementation/subscriptionScope.js.map +1 -1
  23. package/dist/index.js +0 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/internal.js +0 -1
  26. package/dist/internal.js.map +1 -1
  27. package/dist/tests/coList.test.js +15 -23
  28. package/dist/tests/coList.test.js.map +1 -1
  29. package/dist/tests/coMap.test.js +213 -53
  30. package/dist/tests/coMap.test.js.map +1 -1
  31. package/dist/tests/coStream.test.js +59 -26
  32. package/dist/tests/coStream.test.js.map +1 -1
  33. package/dist/tests/deepLoading.test.js +41 -28
  34. package/dist/tests/deepLoading.test.js.map +1 -1
  35. package/dist/tests/groupsAndAccounts.test.js +22 -8
  36. package/dist/tests/groupsAndAccounts.test.js.map +1 -1
  37. package/package.json +3 -3
  38. package/src/coValues/account.ts +28 -5
  39. package/src/coValues/coList.ts +4 -4
  40. package/src/coValues/coMap.ts +54 -8
  41. package/src/coValues/coStream.ts +30 -5
  42. package/src/coValues/group.ts +2 -2
  43. package/src/coValues/interfaces.ts +4 -4
  44. package/src/implementation/devtoolsFormatters.ts +1 -0
  45. package/src/implementation/schema.ts +1 -0
  46. package/src/index.ts +1 -8
  47. package/src/internal.ts +0 -2
  48. package/src/tests/coList.test.ts +14 -30
  49. package/src/tests/coMap.test.ts +150 -16
  50. package/src/tests/coStream.test.ts +74 -27
  51. package/src/tests/deepLoading.test.ts +18 -22
  52. package/tsconfig.json +1 -1
  53. package/dist/implementation/createContext.js +0 -94
  54. package/dist/implementation/createContext.js.map +0 -1
  55. package/src/implementation/createContext.ts +0 -162
@@ -1,162 +0,0 @@
1
- import {
2
- AgentSecret,
3
- CoID,
4
- cojsonInternals,
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
- }
21
- | {
22
- type: "new";
23
- creationProps: { name: string };
24
- initialSecret?: AgentSecret;
25
- saveCredentials: (credentials: {
26
- accountID: ID<Account>;
27
- secret: AgentSecret;
28
- }) => Promise<void>;
29
- onSuccess: () => void;
30
- onError: (error: string | Error) => void;
31
- };
32
-
33
- export interface AuthMethod {
34
- start(crypto: CryptoProvider): Promise<AuthResult>;
35
- }
36
-
37
- export const fixedCredentialsAuth = (credentials: {
38
- accountID: ID<Account>;
39
- secret: AgentSecret;
40
- }): AuthMethod => {
41
- return {
42
- start: async () => ({
43
- type: "existing",
44
- credentials,
45
- onSuccess: () => {},
46
- onError: () => {},
47
- }),
48
- };
49
- };
50
-
51
- export async function randomSessionProvider(accountID: ID<Account>) {
52
- return {
53
- sessionID: cojsonInternals.newRandomSessionID(
54
- accountID as unknown as RawAccountID,
55
- ),
56
- sessionDone: () => {},
57
- };
58
- }
59
-
60
- export async function createJazzContext<Acc extends Account>({
61
- AccountSchema = Account as unknown as AccountClass<Acc>,
62
- auth,
63
- sessionProvider,
64
- peersToLoadFrom,
65
- crypto,
66
- }: {
67
- AccountSchema?: AccountClass<Acc>;
68
- auth: AuthMethod;
69
- sessionProvider: (
70
- accountID: ID<Account>,
71
- ) => Promise<{ sessionID: SessionID; sessionDone: () => void }>;
72
- peersToLoadFrom: Peer[];
73
- crypto: CryptoProvider;
74
- }): Promise<{ account: Acc; done: () => void }> {
75
- // eslint-disable-next-line no-constant-condition
76
- while (true) {
77
- const authResult = await auth.start(crypto);
78
-
79
- if (authResult.type === "existing") {
80
- try {
81
- const { sessionID, sessionDone } = await sessionProvider(
82
- authResult.credentials.accountID,
83
- );
84
-
85
- try {
86
- const node = await LocalNode.withLoadedAccount({
87
- accountID: authResult.credentials
88
- .accountID as unknown as CoID<RawAccount>,
89
- accountSecret: authResult.credentials.secret,
90
- sessionID: sessionID,
91
- peersToLoadFrom: peersToLoadFrom,
92
- crypto: crypto,
93
- migration: async (rawAccount, _node, creationProps) => {
94
- const account = new AccountSchema({
95
- fromRaw: rawAccount,
96
- }) as Acc;
97
-
98
- await account.migrate?.(creationProps);
99
- },
100
- });
101
-
102
- const account = AccountSchema.fromNode(node);
103
- authResult.onSuccess();
104
-
105
- return {
106
- account,
107
- done: () => {
108
- node.gracefulShutdown();
109
- sessionDone();
110
- },
111
- };
112
- } catch (e) {
113
- authResult.onError(
114
- new Error("Error loading account", { cause: e }),
115
- );
116
- sessionDone();
117
- }
118
- } catch (e) {
119
- authResult.onError(
120
- new Error("Error acquiring sessionID", { cause: e }),
121
- );
122
- }
123
- } else if (authResult.type === "new") {
124
- try {
125
- // TODO: figure out a way to not "waste" the first SessionID
126
- const { node } = await LocalNode.withNewlyCreatedAccount({
127
- creationProps: authResult.creationProps,
128
- peersToLoadFrom: peersToLoadFrom,
129
- crypto: crypto,
130
- initialAgentSecret: authResult.initialSecret,
131
- migration: async (rawAccount, _node, creationProps) => {
132
- const account = new AccountSchema({
133
- fromRaw: rawAccount,
134
- }) as Acc;
135
-
136
- await account.migrate?.(creationProps);
137
- },
138
- });
139
-
140
- const account = AccountSchema.fromNode(node);
141
-
142
- await authResult.saveCredentials({
143
- accountID: node.account.id as unknown as ID<Account>,
144
- secret: node.account.agentSecret,
145
- });
146
-
147
- authResult.onSuccess();
148
-
149
- return {
150
- account,
151
- done: () => {
152
- node.gracefulShutdown();
153
- },
154
- };
155
- } catch (e) {
156
- authResult.onError(
157
- new Error("Error creating account", { cause: e }),
158
- );
159
- }
160
- }
161
- }
162
- }