jazz-tools 0.10.14 → 0.11.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.
- package/.turbo/turbo-build.log +11 -7
- package/CHANGELOG.md +31 -0
- package/dist/auth/AuthSecretStorage.d.ts +25 -0
- package/dist/auth/AuthSecretStorage.d.ts.map +1 -0
- package/dist/auth/DemoAuth.d.ts +27 -0
- package/dist/auth/DemoAuth.d.ts.map +1 -0
- package/dist/auth/InMemoryKVStore.d.ts +9 -0
- package/dist/auth/InMemoryKVStore.d.ts.map +1 -0
- package/dist/auth/KvStoreContext.d.ts +17 -0
- package/dist/auth/KvStoreContext.d.ts.map +1 -0
- package/dist/auth/PassphraseAuth.d.ts +35 -0
- package/dist/auth/PassphraseAuth.d.ts.map +1 -0
- package/dist/{chunk-5YDDEUNX.js → chunk-RTRX7HIO.js} +193 -81
- package/dist/chunk-RTRX7HIO.js.map +1 -0
- package/dist/coValues/account.d.ts +120 -0
- package/dist/coValues/account.d.ts.map +1 -0
- package/dist/coValues/coFeed.d.ts +361 -0
- package/dist/coValues/coFeed.d.ts.map +1 -0
- package/dist/coValues/coList.d.ts +221 -0
- package/dist/coValues/coList.d.ts.map +1 -0
- package/dist/coValues/coMap.d.ts +500 -0
- package/dist/coValues/coMap.d.ts.map +1 -0
- package/dist/coValues/coPlainText.d.ts +69 -0
- package/dist/coValues/coPlainText.d.ts.map +1 -0
- package/dist/coValues/coRichText.d.ts +259 -0
- package/dist/coValues/coRichText.d.ts.map +1 -0
- package/dist/coValues/deepLoading.d.ts +81 -0
- package/dist/coValues/deepLoading.d.ts.map +1 -0
- package/dist/coValues/extensions/imageDef.d.ts +17 -0
- package/dist/coValues/extensions/imageDef.d.ts.map +1 -0
- package/dist/coValues/group.d.ts +67 -0
- package/dist/coValues/group.d.ts.map +1 -0
- package/dist/coValues/inbox.d.ts +52 -0
- package/dist/coValues/inbox.d.ts.map +1 -0
- package/dist/coValues/interfaces.d.ts +97 -0
- package/dist/coValues/interfaces.d.ts.map +1 -0
- package/dist/coValues/profile.d.ts +28 -0
- package/dist/coValues/profile.d.ts.map +1 -0
- package/dist/coValues/registeredSchemas.d.ts +12 -0
- package/dist/coValues/registeredSchemas.d.ts.map +1 -0
- package/dist/coValues/schemaUnion.d.ts +79 -0
- package/dist/coValues/schemaUnion.d.ts.map +1 -0
- package/dist/exports.d.ts +27 -0
- package/dist/exports.d.ts.map +1 -0
- package/dist/implementation/ContextManager.d.ts +65 -0
- package/dist/implementation/ContextManager.d.ts.map +1 -0
- package/dist/implementation/activeAccountContext.d.ts +12 -0
- package/dist/implementation/activeAccountContext.d.ts.map +1 -0
- package/dist/implementation/anonymousJazzAgent.d.ts +7 -0
- package/dist/implementation/anonymousJazzAgent.d.ts.map +1 -0
- package/dist/implementation/createContext.d.ts +91 -0
- package/dist/implementation/createContext.d.ts.map +1 -0
- package/dist/implementation/devtoolsFormatters.d.ts +2 -0
- package/dist/implementation/devtoolsFormatters.d.ts.map +1 -0
- package/dist/implementation/errors.d.ts +2 -0
- package/dist/implementation/errors.d.ts.map +1 -0
- package/dist/implementation/inspect.d.ts +3 -0
- package/dist/implementation/inspect.d.ts.map +1 -0
- package/dist/implementation/invites.d.ts +23 -0
- package/dist/implementation/invites.d.ts.map +1 -0
- package/dist/implementation/refs.d.ts +21 -0
- package/dist/implementation/refs.d.ts.map +1 -0
- package/dist/implementation/schema.d.ts +72 -0
- package/dist/implementation/schema.d.ts.map +1 -0
- package/dist/implementation/subscriptionScope.d.ts +33 -0
- package/dist/implementation/subscriptionScope.d.ts.map +1 -0
- package/dist/implementation/symbols.d.ts +8 -0
- package/dist/implementation/symbols.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -1
- package/dist/internal.d.ts +12 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/lib/cache.d.ts +6 -0
- package/dist/lib/cache.d.ts.map +1 -0
- package/dist/lib/cache.test.d.ts +2 -0
- package/dist/lib/cache.test.d.ts.map +1 -0
- package/dist/testing.d.ts +41 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +11 -16
- package/dist/testing.js.map +1 -1
- package/dist/tests/AuthSecretStorage.test.d.ts +2 -0
- package/dist/tests/AuthSecretStorage.test.d.ts.map +1 -0
- package/dist/tests/ContextManager.test.d.ts +2 -0
- package/dist/tests/ContextManager.test.d.ts.map +1 -0
- package/dist/tests/DemoAuth.test.d.ts +2 -0
- package/dist/tests/DemoAuth.test.d.ts.map +1 -0
- package/dist/tests/PassphraseAuth.test.d.ts +2 -0
- package/dist/tests/PassphraseAuth.test.d.ts.map +1 -0
- package/dist/tests/account.test.d.ts +2 -0
- package/dist/tests/account.test.d.ts.map +1 -0
- package/dist/tests/coFeed.test.d.ts +2 -0
- package/dist/tests/coFeed.test.d.ts.map +1 -0
- package/dist/tests/coList.test.d.ts +2 -0
- package/dist/tests/coList.test.d.ts.map +1 -0
- package/dist/tests/coMap.test.d.ts +2 -0
- package/dist/tests/coMap.test.d.ts.map +1 -0
- package/dist/tests/coPlainText.test.d.ts +2 -0
- package/dist/tests/coPlainText.test.d.ts.map +1 -0
- package/dist/tests/coRichText.test.d.ts +2 -0
- package/dist/tests/coRichText.test.d.ts.map +1 -0
- package/dist/tests/createContext.test.d.ts +2 -0
- package/dist/tests/createContext.test.d.ts.map +1 -0
- package/dist/tests/deepLoading.test.d.ts +2 -0
- package/dist/tests/deepLoading.test.d.ts.map +1 -0
- package/dist/tests/fixtures.d.ts +2 -0
- package/dist/tests/fixtures.d.ts.map +1 -0
- package/dist/tests/groupsAndAccounts.test.d.ts +2 -0
- package/dist/tests/groupsAndAccounts.test.d.ts.map +1 -0
- package/dist/tests/inbox.test.d.ts +2 -0
- package/dist/tests/inbox.test.d.ts.map +1 -0
- package/dist/tests/interfaces.test.d.ts +2 -0
- package/dist/tests/interfaces.test.d.ts.map +1 -0
- package/dist/tests/invites.test.d.ts +2 -0
- package/dist/tests/invites.test.d.ts.map +1 -0
- package/dist/tests/schema.test.d.ts +2 -0
- package/dist/tests/schema.test.d.ts.map +1 -0
- package/dist/tests/schemaUnion.test.d.ts +2 -0
- package/dist/tests/schemaUnion.test.d.ts.map +1 -0
- package/dist/tests/subscribe.test.d.ts +2 -0
- package/dist/tests/subscribe.test.d.ts.map +1 -0
- package/dist/tests/testing.test.d.ts +2 -0
- package/dist/tests/testing.test.d.ts.map +1 -0
- package/dist/tests/utils.d.ts +21 -0
- package/dist/tests/utils.d.ts.map +1 -0
- package/dist/types.d.ts +52 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +8 -7
- package/src/coValues/account.ts +69 -11
- package/src/coValues/coMap.ts +2 -2
- package/src/coValues/coRichText.ts +42 -17
- package/src/coValues/group.ts +76 -31
- package/src/coValues/inbox.ts +10 -0
- package/src/coValues/interfaces.ts +1 -1
- package/src/coValues/profile.ts +35 -2
- package/src/implementation/ContextManager.ts +63 -15
- package/src/implementation/schema.ts +1 -3
- package/src/testing.ts +10 -16
- package/src/tests/AuthSecretStorage.test.ts +1 -2
- package/src/tests/ContextManager.test.ts +27 -14
- package/src/tests/PassphraseAuth.test.ts +7 -3
- package/src/tests/coMap.test.ts +20 -21
- package/src/tests/deepLoading.test.ts +8 -17
- package/src/tests/groupsAndAccounts.test.ts +429 -89
- package/src/tests/inbox.test.ts +24 -0
- package/src/tests/schema.test.ts +45 -5
- package/src/tests/utils.ts +7 -3
- package/src/types.ts +6 -0
- package/tsconfig.json +4 -1
- package/dist/chunk-5YDDEUNX.js.map +0 -1
@@ -0,0 +1,41 @@
|
|
1
|
+
import { LocalNode, Peer } from "cojson";
|
2
|
+
import { PureJSCrypto } from "cojson/dist/crypto/PureJSCrypto";
|
3
|
+
import { Account, AccountClass, JazzContextManagerAuthProps } from "./exports.js";
|
4
|
+
import { JazzContextManager, JazzContextManagerBaseProps } from "./implementation/ContextManager.js";
|
5
|
+
import { type AnonymousJazzAgent, type CoValueClass } from "./internal.js";
|
6
|
+
export declare class TestJSCrypto extends PureJSCrypto {
|
7
|
+
static create(): Promise<PureJSCrypto>;
|
8
|
+
}
|
9
|
+
export declare function getPeerConnectedToTestSyncServer(): Peer;
|
10
|
+
export declare function createJazzTestAccount<Acc extends Account>(options?: {
|
11
|
+
isCurrentActiveAccount?: boolean;
|
12
|
+
AccountSchema?: CoValueClass<Acc>;
|
13
|
+
creationProps?: Record<string, unknown>;
|
14
|
+
}): Promise<Acc>;
|
15
|
+
export declare function setActiveAccount(account: Account): void;
|
16
|
+
export declare function createJazzTestGuest(): Promise<{
|
17
|
+
guest: AnonymousJazzAgent;
|
18
|
+
}>;
|
19
|
+
export type TestJazzContextManagerProps<Acc extends Account> = JazzContextManagerBaseProps<Acc> & {
|
20
|
+
defaultProfileName?: string;
|
21
|
+
AccountSchema?: AccountClass<Acc>;
|
22
|
+
isAuthenticated?: boolean;
|
23
|
+
};
|
24
|
+
export declare class TestJazzContextManager<Acc extends Account> extends JazzContextManager<Acc, TestJazzContextManagerProps<Acc>> {
|
25
|
+
static fromAccountOrGuest<Acc extends Account>(account?: Acc | {
|
26
|
+
guest: AnonymousJazzAgent;
|
27
|
+
}, props?: TestJazzContextManagerProps<Acc>): TestJazzContextManager<Acc>;
|
28
|
+
static fromAccount<Acc extends Account>(account: Acc, props?: TestJazzContextManagerProps<Acc>): TestJazzContextManager<Acc>;
|
29
|
+
static fromGuest<Acc extends Account>({ guest }: {
|
30
|
+
guest: AnonymousJazzAgent;
|
31
|
+
}, props?: TestJazzContextManagerProps<Acc>): TestJazzContextManager<Acc>;
|
32
|
+
getNewContext(props: TestJazzContextManagerProps<Acc>, authProps?: JazzContextManagerAuthProps): Promise<{
|
33
|
+
me: Acc;
|
34
|
+
node: LocalNode;
|
35
|
+
done: () => void;
|
36
|
+
logOut: () => Promise<void>;
|
37
|
+
}>;
|
38
|
+
}
|
39
|
+
export declare function linkAccounts(a: Account, b: Account, aRole?: "server" | "client", bRole?: "server" | "client"): void;
|
40
|
+
export declare function setupJazzTestSync(): Promise<Account>;
|
41
|
+
//# sourceMappingURL=testing.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,SAAS,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEtE,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAC/D,OAAO,EACL,OAAO,EACP,YAAY,EAEZ,2BAA2B,EAC5B,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,kBAAkB,EAClB,2BAA2B,EAC5B,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,YAAY,EAIlB,MAAM,eAAe,CAAC;AAcvB,qBAAa,YAAa,SAAQ,YAAY;WAC/B,MAAM;CAoBpB;AAED,wBAAgB,gCAAgC,SAgB/C;AAKD,wBAAsB,qBAAqB,CAAC,GAAG,SAAS,OAAO,EAAE,OAAO,CAAC,EAAE;IACzE,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC,aAAa,CAAC,EAAE,YAAY,CAAC,GAAG,CAAC,CAAC;IAClC,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzC,GAAG,OAAO,CAAC,GAAG,CAAC,CAuDf;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,QAEhD;AAED,wBAAsB,mBAAmB;;GASxC;AAED,MAAM,MAAM,2BAA2B,CAAC,GAAG,SAAS,OAAO,IACzD,2BAA2B,CAAC,GAAG,CAAC,GAAG;IACjC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,aAAa,CAAC,EAAE,YAAY,CAAC,GAAG,CAAC,CAAC;IAClC,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEJ,qBAAa,sBAAsB,CACjC,GAAG,SAAS,OAAO,CACnB,SAAQ,kBAAkB,CAAC,GAAG,EAAE,2BAA2B,CAAC,GAAG,CAAC,CAAC;IACjE,MAAM,CAAC,kBAAkB,CAAC,GAAG,SAAS,OAAO,EAC3C,OAAO,CAAC,EAAE,GAAG,GAAG;QAAE,KAAK,EAAE,kBAAkB,CAAA;KAAE,EAC7C,KAAK,CAAC,EAAE,2BAA2B,CAAC,GAAG,CAAC;IAS1C,MAAM,CAAC,WAAW,CAAC,GAAG,SAAS,OAAO,EACpC,OAAO,EAAE,GAAG,EACZ,KAAK,CAAC,EAAE,2BAA2B,CAAC,GAAG,CAAC;IAyC1C,MAAM,CAAC,SAAS,CAAC,GAAG,SAAS,OAAO,EAClC,EAAE,KAAK,EAAE,EAAE;QAAE,KAAK,EAAE,kBAAkB,CAAA;KAAE,EACxC,KAAK,GAAE,2BAA2B,CAAC,GAAG,CAAM;IAmBxC,aAAa,CACjB,KAAK,EAAE,2BAA2B,CAAC,GAAG,CAAC,EACvC,SAAS,CAAC,EAAE,2BAA2B;;;;;;CA8B1C;AAED,wBAAgB,YAAY,CAC1B,CAAC,EAAE,OAAO,EACV,CAAC,EAAE,OAAO,EACV,KAAK,GAAE,QAAQ,GAAG,QAAmB,EACrC,KAAK,GAAE,QAAQ,GAAG,QAAmB,QAStC;AAED,wBAAsB,iBAAiB,qBAetC"}
|
package/dist/testing.js
CHANGED
@@ -5,7 +5,7 @@ import {
|
|
5
5
|
createAnonymousJazzContext,
|
6
6
|
createJazzContext,
|
7
7
|
randomSessionProvider
|
8
|
-
} from "./chunk-
|
8
|
+
} from "./chunk-RTRX7HIO.js";
|
9
9
|
|
10
10
|
// src/testing.ts
|
11
11
|
import { LocalNode } from "cojson";
|
@@ -152,8 +152,7 @@ var TestJazzContextManager = class _TestJazzContextManager extends JazzContextMa
|
|
152
152
|
});
|
153
153
|
return context;
|
154
154
|
}
|
155
|
-
async
|
156
|
-
this.props = props;
|
155
|
+
async getNewContext(props, authProps) {
|
157
156
|
if (!syncServer.current) {
|
158
157
|
throw new Error(
|
159
158
|
"You need to setup a test sync server with setupJazzTestSync to use the Auth functions"
|
@@ -169,20 +168,16 @@ var TestJazzContextManager = class _TestJazzContextManager extends JazzContextMa
|
|
169
168
|
authSecretStorage: this.getAuthSecretStorage(),
|
170
169
|
AccountSchema: props.AccountSchema
|
171
170
|
});
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
done: () => {
|
178
|
-
context.done();
|
179
|
-
},
|
180
|
-
logOut: () => {
|
181
|
-
return context.logOut();
|
182
|
-
}
|
171
|
+
return {
|
172
|
+
me: context.account,
|
173
|
+
node: context.node,
|
174
|
+
done: () => {
|
175
|
+
context.done();
|
183
176
|
},
|
184
|
-
|
185
|
-
|
177
|
+
logOut: () => {
|
178
|
+
return context.logOut();
|
179
|
+
}
|
180
|
+
};
|
186
181
|
}
|
187
182
|
};
|
188
183
|
function linkAccounts(a, b, aRole = "server", bRole = "server") {
|
package/dist/testing.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/testing.ts"],"sourcesContent":["import { AgentSecret, CryptoProvider, LocalNode, Peer } from \"cojson\";\nimport { cojsonInternals } from \"cojson\";\nimport { PureJSCrypto } from \"cojson/dist/crypto/PureJSCrypto\";\nimport {\n Account,\n AccountClass,\n AuthCredentials,\n JazzContextManagerAuthProps,\n} from \"./exports.js\";\nimport {\n JazzContextManager,\n JazzContextManagerBaseProps,\n} from \"./implementation/ContextManager.js\";\nimport { activeAccountContext } from \"./implementation/activeAccountContext.js\";\nimport {\n type AnonymousJazzAgent,\n type CoValueClass,\n createAnonymousJazzContext,\n createJazzContext,\n randomSessionProvider,\n} from \"./internal.js\";\n\nconst syncServer: { current: LocalNode | null } = { current: null };\n\ntype TestAccountSchema<Acc extends Account> = CoValueClass<Acc> & {\n fromNode: (typeof Account)[\"fromNode\"];\n create: (options: {\n creationProps: { name: string };\n initialAgentSecret?: AgentSecret;\n peersToLoadFrom?: Peer[];\n crypto: CryptoProvider;\n }) => Promise<Acc>;\n};\n\nexport class TestJSCrypto extends PureJSCrypto {\n static async create() {\n if (\"navigator\" in globalThis && navigator.userAgent?.includes(\"jsdom\")) {\n // Mocking crypto seal & encrypt to make it work with JSDom. Getting \"Error: Uint8Array expected\" there\n const crypto = new PureJSCrypto();\n\n crypto.seal = (options) =>\n `sealed_U${cojsonInternals.stableStringify(options.message)}` as any;\n crypto.unseal = (sealed) =>\n JSON.parse(sealed.substring(\"sealed_U\".length));\n crypto.encrypt = (message) =>\n `encrypted_U${cojsonInternals.stableStringify(message)}` as any;\n crypto.decryptRaw = (encrypted) =>\n encrypted.substring(\"encrypted_U\".length) as any;\n\n return crypto;\n }\n\n // For non-jsdom environments, we use the real crypto\n return new PureJSCrypto();\n }\n}\n\nexport function getPeerConnectedToTestSyncServer() {\n if (!syncServer.current) {\n throw new Error(\"Sync server not initialized\");\n }\n\n const [aPeer, bPeer] = cojsonInternals.connectedPeers(\n Math.random().toString(),\n Math.random().toString(),\n {\n peer1role: \"server\",\n peer2role: \"server\",\n },\n );\n syncServer.current.syncManager.addPeer(aPeer);\n\n return bPeer;\n}\n\nconst SecretSeedMap = new Map<string, Uint8Array>();\nlet isMigrationActive = false;\n\nexport async function createJazzTestAccount<Acc extends Account>(options?: {\n isCurrentActiveAccount?: boolean;\n AccountSchema?: CoValueClass<Acc>;\n creationProps?: Record<string, unknown>;\n}): Promise<Acc> {\n const AccountSchema = (options?.AccountSchema ??\n Account) as unknown as TestAccountSchema<Acc>;\n const peers = [];\n if (syncServer.current) {\n peers.push(getPeerConnectedToTestSyncServer());\n }\n\n const crypto = await TestJSCrypto.create();\n const secretSeed = crypto.newRandomSecretSeed();\n\n const { node } = await LocalNode.withNewlyCreatedAccount({\n creationProps: {\n name: \"Test Account\",\n ...options?.creationProps,\n },\n initialAgentSecret: crypto.agentSecretFromSecretSeed(secretSeed),\n crypto,\n peersToLoadFrom: peers,\n migration: async (rawAccount, _node, creationProps) => {\n if (isMigrationActive) {\n throw new Error(\n \"It is not possible to create multiple accounts in parallel inside the test environment.\",\n );\n }\n\n isMigrationActive = true;\n\n const account = new AccountSchema({\n fromRaw: rawAccount,\n });\n\n // We need to set the account as current because the migration\n // will probably rely on the global me\n const prevActiveAccount = activeAccountContext.maybeGet();\n activeAccountContext.set(account);\n\n await account.applyMigration?.(creationProps);\n\n if (!options?.isCurrentActiveAccount) {\n activeAccountContext.set(prevActiveAccount);\n }\n\n isMigrationActive = false;\n },\n });\n\n const account = AccountSchema.fromNode(node);\n SecretSeedMap.set(account.id, secretSeed);\n\n if (options?.isCurrentActiveAccount) {\n activeAccountContext.set(account);\n }\n\n return account;\n}\n\nexport function setActiveAccount(account: Account) {\n activeAccountContext.set(account);\n}\n\nexport async function createJazzTestGuest() {\n const ctx = await createAnonymousJazzContext({\n crypto: await PureJSCrypto.create(),\n peersToLoadFrom: [],\n });\n\n return {\n guest: ctx.agent,\n };\n}\n\nexport type TestJazzContextManagerProps<Acc extends Account> =\n JazzContextManagerBaseProps<Acc> & {\n defaultProfileName?: string;\n AccountSchema?: AccountClass<Acc>;\n isAuthenticated?: boolean;\n };\n\nexport class TestJazzContextManager<\n Acc extends Account,\n> extends JazzContextManager<Acc, TestJazzContextManagerProps<Acc>> {\n static fromAccountOrGuest<Acc extends Account>(\n account?: Acc | { guest: AnonymousJazzAgent },\n props?: TestJazzContextManagerProps<Acc>,\n ) {\n if (account && \"guest\" in account) {\n return this.fromGuest<Acc>(account, props);\n }\n\n return this.fromAccount<Acc>(account ?? (Account.getMe() as Acc), props);\n }\n\n static fromAccount<Acc extends Account>(\n account: Acc,\n props?: TestJazzContextManagerProps<Acc>,\n ) {\n const context = new TestJazzContextManager<Acc>();\n\n const provider = props?.isAuthenticated ? \"testProvider\" : \"anonymous\";\n const storage = context.getAuthSecretStorage();\n const node = account._raw.core.node;\n\n const credentials = {\n accountID: account.id,\n accountSecret: node.account.agentSecret,\n secretSeed: SecretSeedMap.get(account.id),\n provider,\n } satisfies AuthCredentials;\n\n storage.set(credentials);\n\n context.updateContext(\n {\n AccountSchema: account.constructor as AccountClass<Acc>,\n ...props,\n },\n {\n me: account,\n node,\n done: () => {\n node.gracefulShutdown();\n },\n logOut: async () => {\n await storage.clear();\n node.gracefulShutdown();\n },\n },\n {\n credentials,\n },\n );\n\n return context;\n }\n\n static fromGuest<Acc extends Account>(\n { guest }: { guest: AnonymousJazzAgent },\n props: TestJazzContextManagerProps<Acc> = {},\n ) {\n const context = new TestJazzContextManager<Acc>();\n const node = guest.node;\n\n context.updateContext(props, {\n guest,\n node,\n done: () => {\n node.gracefulShutdown();\n },\n logOut: async () => {\n node.gracefulShutdown();\n },\n });\n\n return context;\n }\n\n async createContext(\n props: TestJazzContextManagerProps<Acc>,\n authProps?: JazzContextManagerAuthProps,\n ) {\n this.props = props;\n\n if (!syncServer.current) {\n throw new Error(\n \"You need to setup a test sync server with setupJazzTestSync to use the Auth functions\",\n );\n }\n\n const context = await createJazzContext<Acc>({\n credentials: authProps?.credentials,\n defaultProfileName: props.defaultProfileName,\n newAccountProps: authProps?.newAccountProps,\n peersToLoadFrom: [getPeerConnectedToTestSyncServer()],\n crypto: await TestJSCrypto.create(),\n sessionProvider: randomSessionProvider,\n authSecretStorage: this.getAuthSecretStorage(),\n AccountSchema: props.AccountSchema,\n });\n\n await this.updateContext(\n props,\n {\n me: context.account,\n node: context.node,\n done: () => {\n context.done();\n },\n logOut: () => {\n return context.logOut();\n },\n },\n authProps,\n );\n }\n}\n\nexport function linkAccounts(\n a: Account,\n b: Account,\n aRole: \"server\" | \"client\" = \"server\",\n bRole: \"server\" | \"client\" = \"server\",\n) {\n const [aPeer, bPeer] = cojsonInternals.connectedPeers(b.id, a.id, {\n peer1role: aRole,\n peer2role: bRole,\n });\n\n a._raw.core.node.syncManager.addPeer(aPeer);\n b._raw.core.node.syncManager.addPeer(bPeer);\n}\n\nexport async function setupJazzTestSync() {\n if (syncServer.current) {\n syncServer.current.gracefulShutdown();\n }\n\n const account = await Account.create({\n creationProps: {\n name: \"Test Account\",\n },\n crypto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B,oBAAoB,MAAM;AAAA,MAC1B,iBAAiB,WAAW;AAAA,MAC5B,iBAAiB,CAAC,iCAAiC,CAAC;AAAA,MACpD,QAAQ,MAAM,aAAa,OAAO;AAAA,MAClC,iBAAiB;AAAA,MACjB,mBAAmB,KAAK,qBAAqB;AAAA,MAC7C,eAAe,MAAM;AAAA,IACvB,CAAC;AAED,UAAM,KAAK;AAAA,MACT;AAAA,MACA;AAAA,QACE,IAAI,QAAQ;AAAA,QACZ,MAAM,QAAQ;AAAA,QACd,MAAM,MAAM;AACV,kBAAQ,KAAK;AAAA,QACf;AAAA,QACA,QAAQ,MAAM;AACZ,iBAAO,QAAQ,OAAO;AAAA,QACxB;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;AAEO,SAAS,aACd,GACA,GACA,QAA6B,UAC7B,QAA6B,UAC7B;AACA,QAAM,CAAC,OAAO,KAAK,IAAI,gBAAgB,eAAe,EAAE,IAAI,EAAE,IAAI;AAAA,IAChE,WAAW;AAAA,IACX,WAAW;AAAA,EACb,CAAC;AAED,IAAE,KAAK,KAAK,KAAK,YAAY,QAAQ,KAAK;AAC1C,IAAE,KAAK,KAAK,KAAK,YAAY,QAAQ,KAAK;AAC5C;AAEA,eAAsB,oBAAoB;AACxC,MAAI,WAAW,SAAS;AACtB,eAAW,QAAQ,iBAAiB;AAAA,EACtC;AAEA,QAAM,UAAU,MAAM,QAAQ,OAAO;AAAA,IACnC,eAAe;AAAA,MACb,MAAM;AAAA,IACR;AAAA,IACA,QAAQ,MAAM,aAAa,OAAO;AAAA,EACpC,CAAC;AAED,aAAW,UAAU,QAAQ,KAAK,KAAK;AAEvC,SAAO;AACT;","names":["account"]}
|
1
|
+
{"version":3,"sources":["../src/testing.ts"],"sourcesContent":["import { AgentSecret, CryptoProvider, LocalNode, Peer } from \"cojson\";\nimport { cojsonInternals } from \"cojson\";\nimport { PureJSCrypto } from \"cojson/dist/crypto/PureJSCrypto\";\nimport {\n Account,\n AccountClass,\n AuthCredentials,\n JazzContextManagerAuthProps,\n} from \"./exports.js\";\nimport {\n JazzContextManager,\n JazzContextManagerBaseProps,\n} from \"./implementation/ContextManager.js\";\nimport { activeAccountContext } from \"./implementation/activeAccountContext.js\";\nimport {\n type AnonymousJazzAgent,\n type CoValueClass,\n createAnonymousJazzContext,\n createJazzContext,\n randomSessionProvider,\n} from \"./internal.js\";\n\nconst syncServer: { current: LocalNode | null } = { current: null };\n\ntype TestAccountSchema<Acc extends Account> = CoValueClass<Acc> & {\n fromNode: (typeof Account)[\"fromNode\"];\n create: (options: {\n creationProps: { name: string };\n initialAgentSecret?: AgentSecret;\n peersToLoadFrom?: Peer[];\n crypto: CryptoProvider;\n }) => Promise<Acc>;\n};\n\nexport class TestJSCrypto extends PureJSCrypto {\n static async create() {\n if (\"navigator\" in globalThis && navigator.userAgent?.includes(\"jsdom\")) {\n // Mocking crypto seal & encrypt to make it work with JSDom. Getting \"Error: Uint8Array expected\" there\n const crypto = new PureJSCrypto();\n\n crypto.seal = (options) =>\n `sealed_U${cojsonInternals.stableStringify(options.message)}` as any;\n crypto.unseal = (sealed) =>\n JSON.parse(sealed.substring(\"sealed_U\".length));\n crypto.encrypt = (message) =>\n `encrypted_U${cojsonInternals.stableStringify(message)}` as any;\n crypto.decryptRaw = (encrypted) =>\n encrypted.substring(\"encrypted_U\".length) as any;\n\n return crypto;\n }\n\n // For non-jsdom environments, we use the real crypto\n return new PureJSCrypto();\n }\n}\n\nexport function getPeerConnectedToTestSyncServer() {\n if (!syncServer.current) {\n throw new Error(\"Sync server not initialized\");\n }\n\n const [aPeer, bPeer] = cojsonInternals.connectedPeers(\n Math.random().toString(),\n Math.random().toString(),\n {\n peer1role: \"server\",\n peer2role: \"server\",\n },\n );\n syncServer.current.syncManager.addPeer(aPeer);\n\n return bPeer;\n}\n\nconst SecretSeedMap = new Map<string, Uint8Array>();\nlet isMigrationActive = false;\n\nexport async function createJazzTestAccount<Acc extends Account>(options?: {\n isCurrentActiveAccount?: boolean;\n AccountSchema?: CoValueClass<Acc>;\n creationProps?: Record<string, unknown>;\n}): Promise<Acc> {\n const AccountSchema = (options?.AccountSchema ??\n Account) as unknown as TestAccountSchema<Acc>;\n const peers = [];\n if (syncServer.current) {\n peers.push(getPeerConnectedToTestSyncServer());\n }\n\n const crypto = await TestJSCrypto.create();\n const secretSeed = crypto.newRandomSecretSeed();\n\n const { node } = await LocalNode.withNewlyCreatedAccount({\n creationProps: {\n name: \"Test Account\",\n ...options?.creationProps,\n },\n initialAgentSecret: crypto.agentSecretFromSecretSeed(secretSeed),\n crypto,\n peersToLoadFrom: peers,\n migration: async (rawAccount, _node, creationProps) => {\n if (isMigrationActive) {\n throw new Error(\n \"It is not possible to create multiple accounts in parallel inside the test environment.\",\n );\n }\n\n isMigrationActive = true;\n\n const account = new AccountSchema({\n fromRaw: rawAccount,\n });\n\n // We need to set the account as current because the migration\n // will probably rely on the global me\n const prevActiveAccount = activeAccountContext.maybeGet();\n activeAccountContext.set(account);\n\n await account.applyMigration?.(creationProps);\n\n if (!options?.isCurrentActiveAccount) {\n activeAccountContext.set(prevActiveAccount);\n }\n\n isMigrationActive = false;\n },\n });\n\n const account = AccountSchema.fromNode(node);\n SecretSeedMap.set(account.id, secretSeed);\n\n if (options?.isCurrentActiveAccount) {\n activeAccountContext.set(account);\n }\n\n return account;\n}\n\nexport function setActiveAccount(account: Account) {\n activeAccountContext.set(account);\n}\n\nexport async function createJazzTestGuest() {\n const ctx = await createAnonymousJazzContext({\n crypto: await PureJSCrypto.create(),\n peersToLoadFrom: [],\n });\n\n return {\n guest: ctx.agent,\n };\n}\n\nexport type TestJazzContextManagerProps<Acc extends Account> =\n JazzContextManagerBaseProps<Acc> & {\n defaultProfileName?: string;\n AccountSchema?: AccountClass<Acc>;\n isAuthenticated?: boolean;\n };\n\nexport class TestJazzContextManager<\n Acc extends Account,\n> extends JazzContextManager<Acc, TestJazzContextManagerProps<Acc>> {\n static fromAccountOrGuest<Acc extends Account>(\n account?: Acc | { guest: AnonymousJazzAgent },\n props?: TestJazzContextManagerProps<Acc>,\n ) {\n if (account && \"guest\" in account) {\n return this.fromGuest<Acc>(account, props);\n }\n\n return this.fromAccount<Acc>(account ?? (Account.getMe() as Acc), props);\n }\n\n static fromAccount<Acc extends Account>(\n account: Acc,\n props?: TestJazzContextManagerProps<Acc>,\n ) {\n const context = new TestJazzContextManager<Acc>();\n\n const provider = props?.isAuthenticated ? \"testProvider\" : \"anonymous\";\n const storage = context.getAuthSecretStorage();\n const node = account._raw.core.node;\n\n const credentials = {\n accountID: account.id,\n accountSecret: node.account.agentSecret,\n secretSeed: SecretSeedMap.get(account.id),\n provider,\n } satisfies AuthCredentials;\n\n storage.set(credentials);\n\n context.updateContext(\n {\n AccountSchema: account.constructor as AccountClass<Acc>,\n ...props,\n },\n {\n me: account,\n node,\n done: () => {\n node.gracefulShutdown();\n },\n logOut: async () => {\n await storage.clear();\n node.gracefulShutdown();\n },\n },\n {\n credentials,\n },\n );\n\n return context;\n }\n\n static fromGuest<Acc extends Account>(\n { guest }: { guest: AnonymousJazzAgent },\n props: TestJazzContextManagerProps<Acc> = {},\n ) {\n const context = new TestJazzContextManager<Acc>();\n const node = guest.node;\n\n context.updateContext(props, {\n guest,\n node,\n done: () => {\n node.gracefulShutdown();\n },\n logOut: async () => {\n node.gracefulShutdown();\n },\n });\n\n return context;\n }\n\n async getNewContext(\n props: TestJazzContextManagerProps<Acc>,\n authProps?: JazzContextManagerAuthProps,\n ) {\n if (!syncServer.current) {\n throw new Error(\n \"You need to setup a test sync server with setupJazzTestSync to use the Auth functions\",\n );\n }\n\n const context = await createJazzContext<Acc>({\n credentials: authProps?.credentials,\n defaultProfileName: props.defaultProfileName,\n newAccountProps: authProps?.newAccountProps,\n peersToLoadFrom: [getPeerConnectedToTestSyncServer()],\n crypto: await TestJSCrypto.create(),\n sessionProvider: randomSessionProvider,\n authSecretStorage: this.getAuthSecretStorage(),\n AccountSchema: props.AccountSchema,\n });\n\n return {\n me: context.account,\n node: context.node,\n done: () => {\n context.done();\n },\n logOut: () => {\n return context.logOut();\n },\n };\n }\n}\n\nexport function linkAccounts(\n a: Account,\n b: Account,\n aRole: \"server\" | \"client\" = \"server\",\n bRole: \"server\" | \"client\" = \"server\",\n) {\n const [aPeer, bPeer] = cojsonInternals.connectedPeers(b.id, a.id, {\n peer1role: aRole,\n peer2role: bRole,\n });\n\n a._raw.core.node.syncManager.addPeer(aPeer);\n b._raw.core.node.syncManager.addPeer(bPeer);\n}\n\nexport async function setupJazzTestSync() {\n if (syncServer.current) {\n syncServer.current.gracefulShutdown();\n }\n\n const account = await Account.create({\n creationProps: {\n name: \"Test Account\",\n },\n crypto: await TestJSCrypto.create(),\n });\n\n syncServer.current = account._raw.core.node;\n\n return account;\n}\n"],"mappings":";;;;;;;;;;AAAA,SAAsC,iBAAuB;AAC7D,SAAS,uBAAuB;AAChC,SAAS,oBAAoB;AAoB7B,IAAM,aAA4C,EAAE,SAAS,KAAK;AAY3D,IAAM,eAAN,cAA2B,aAAa;AAAA,EAC7C,aAAa,SAAS;AACpB,QAAI,eAAe,cAAc,UAAU,WAAW,SAAS,OAAO,GAAG;AAEvE,YAAM,SAAS,IAAI,aAAa;AAEhC,aAAO,OAAO,CAAC,YACb,WAAW,gBAAgB,gBAAgB,QAAQ,OAAO,CAAC;AAC7D,aAAO,SAAS,CAAC,WACf,KAAK,MAAM,OAAO,UAAU,WAAW,MAAM,CAAC;AAChD,aAAO,UAAU,CAAC,YAChB,cAAc,gBAAgB,gBAAgB,OAAO,CAAC;AACxD,aAAO,aAAa,CAAC,cACnB,UAAU,UAAU,cAAc,MAAM;AAE1C,aAAO;AAAA,IACT;AAGA,WAAO,IAAI,aAAa;AAAA,EAC1B;AACF;AAEO,SAAS,mCAAmC;AACjD,MAAI,CAAC,WAAW,SAAS;AACvB,UAAM,IAAI,MAAM,6BAA6B;AAAA,EAC/C;AAEA,QAAM,CAAC,OAAO,KAAK,IAAI,gBAAgB;AAAA,IACrC,KAAK,OAAO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IACnC,eAAe;AAAA,MACb,MAAM;AAAA,IACR;AAAA,IACA,QAAQ,MAAM,aAAa,OAAO;AAAA,EACpC,CAAC;AAED,aAAW,UAAU,QAAQ,KAAK,KAAK;AAEvC,SAAO;AACT;","names":["account"]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"AuthSecretStorage.test.d.ts","sourceRoot":"","sources":["../../src/tests/AuthSecretStorage.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"ContextManager.test.d.ts","sourceRoot":"","sources":["../../src/tests/ContextManager.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"DemoAuth.test.d.ts","sourceRoot":"","sources":["../../src/tests/DemoAuth.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"PassphraseAuth.test.d.ts","sourceRoot":"","sources":["../../src/tests/PassphraseAuth.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"account.test.d.ts","sourceRoot":"","sources":["../../src/tests/account.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"coFeed.test.d.ts","sourceRoot":"","sources":["../../src/tests/coFeed.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"coList.test.d.ts","sourceRoot":"","sources":["../../src/tests/coList.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"coMap.test.d.ts","sourceRoot":"","sources":["../../src/tests/coMap.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"coPlainText.test.d.ts","sourceRoot":"","sources":["../../src/tests/coPlainText.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"coRichText.test.d.ts","sourceRoot":"","sources":["../../src/tests/coRichText.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"createContext.test.d.ts","sourceRoot":"","sources":["../../src/tests/createContext.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"deepLoading.test.d.ts","sourceRoot":"","sources":["../../src/tests/deepLoading.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"fixtures.d.ts","sourceRoot":"","sources":["../../src/tests/fixtures.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,UAigExB,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"groupsAndAccounts.test.d.ts","sourceRoot":"","sources":["../../src/tests/groupsAndAccounts.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"inbox.test.d.ts","sourceRoot":"","sources":["../../src/tests/inbox.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"interfaces.test.d.ts","sourceRoot":"","sources":["../../src/tests/interfaces.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"invites.test.d.ts","sourceRoot":"","sources":["../../src/tests/invites.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"schema.test.d.ts","sourceRoot":"","sources":["../../src/tests/schema.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"schemaUnion.test.d.ts","sourceRoot":"","sources":["../../src/tests/schemaUnion.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"subscribe.test.d.ts","sourceRoot":"","sources":["../../src/tests/subscribe.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"testing.test.d.ts","sourceRoot":"","sources":["../../src/tests/testing.test.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { CoID, LocalNode, RawCoValue } from "cojson";
|
2
|
+
import { Account } from "../index";
|
3
|
+
export declare function setupAccount(): Promise<{
|
4
|
+
me: Account & {
|
5
|
+
isLocalNodeOwner: true;
|
6
|
+
sessionID: import("cojson").SessionID;
|
7
|
+
_raw: import("cojson").RawControlledAccount;
|
8
|
+
};
|
9
|
+
meOnSecondPeer: Account;
|
10
|
+
}>;
|
11
|
+
export declare function setupTwoNodes(options?: {
|
12
|
+
ServerAccountSchema?: typeof Account;
|
13
|
+
}): Promise<{
|
14
|
+
clientNode: LocalNode;
|
15
|
+
serverNode: LocalNode;
|
16
|
+
clientAccount: Account;
|
17
|
+
serverAccount: Account;
|
18
|
+
}>;
|
19
|
+
export declare function waitFor(callback: () => boolean | void): Promise<void>;
|
20
|
+
export declare function loadCoValueOrFail<V extends RawCoValue>(node: LocalNode, id: CoID<V>): Promise<V>;
|
21
|
+
//# sourceMappingURL=utils.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/tests/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAGrD,OAAO,EACL,OAAO,EAGR,MAAM,UAAU,CAAC;AAIlB,wBAAsB,YAAY;;;;;;;GA+BjC;AAED,wBAAsB,aAAa,CAAC,OAAO,CAAC,EAAE;IAC5C,mBAAmB,CAAC,EAAE,OAAO,OAAO,CAAC;CACtC;;;;;GAgDA;AAED,wBAAgB,OAAO,CAAC,QAAQ,EAAE,MAAM,OAAO,GAAG,IAAI,iBA0BrD;AAED,wBAAsB,iBAAiB,CAAC,CAAC,SAAS,UAAU,EAC1D,IAAI,EAAE,SAAS,EACf,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC,GACV,OAAO,CAAC,CAAC,CAAC,CAMZ"}
|
package/dist/types.d.ts
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
import type { AgentSecret, LocalNode } from "cojson";
|
2
|
+
import type { Account } from "./exports.js";
|
3
|
+
import type { AnonymousJazzAgent, ID } from "./internal.js";
|
4
|
+
export type AuthCredentials = {
|
5
|
+
accountID: ID<Account>;
|
6
|
+
secretSeed?: Uint8Array;
|
7
|
+
accountSecret: AgentSecret;
|
8
|
+
provider?: "anonymous" | "clerk" | "demo" | "passkey" | "passphrase" | string;
|
9
|
+
};
|
10
|
+
export type AuthenticateAccountFunction = (credentials: AuthCredentials) => Promise<void>;
|
11
|
+
export type RegisterAccountFunction = (accountSecret: AgentSecret, creationProps: {
|
12
|
+
name: string;
|
13
|
+
}) => Promise<ID<Account>>;
|
14
|
+
/** @category Context Creation */
|
15
|
+
export type JazzAuthContext<Acc extends Account> = {
|
16
|
+
me: Acc;
|
17
|
+
node: LocalNode;
|
18
|
+
authenticate: AuthenticateAccountFunction;
|
19
|
+
register: RegisterAccountFunction;
|
20
|
+
logOut: () => Promise<void>;
|
21
|
+
done: () => void;
|
22
|
+
isAuthenticated?: boolean;
|
23
|
+
};
|
24
|
+
export type JazzGuestContext = {
|
25
|
+
guest: AnonymousJazzAgent;
|
26
|
+
node: LocalNode;
|
27
|
+
authenticate: AuthenticateAccountFunction;
|
28
|
+
register: RegisterAccountFunction;
|
29
|
+
logOut: () => void;
|
30
|
+
done: () => void;
|
31
|
+
isAuthenticated?: boolean;
|
32
|
+
};
|
33
|
+
export type JazzContextType<Acc extends Account> = JazzAuthContext<Acc> | JazzGuestContext;
|
34
|
+
export type NewAccountProps = {
|
35
|
+
secret?: AgentSecret;
|
36
|
+
creationProps?: {
|
37
|
+
name: string;
|
38
|
+
};
|
39
|
+
};
|
40
|
+
export type SyncConfig = {
|
41
|
+
peer: `wss://${string}` | `ws://${string}`;
|
42
|
+
when?: "always" | "signedUp";
|
43
|
+
} | {
|
44
|
+
peer?: `wss://${string}` | `ws://${string}`;
|
45
|
+
when: "never";
|
46
|
+
};
|
47
|
+
export interface Register {
|
48
|
+
}
|
49
|
+
export type RegisteredAccount = Register extends {
|
50
|
+
Account: infer Acc;
|
51
|
+
} ? Acc : Account;
|
52
|
+
//# sourceMappingURL=types.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,kBAAkB,EAAE,EAAE,EAAE,MAAM,eAAe,CAAC;AAE5D,MAAM,MAAM,eAAe,GAAG;IAC5B,SAAS,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC;IACvB,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,aAAa,EAAE,WAAW,CAAC;IAC3B,QAAQ,CAAC,EAAE,WAAW,GAAG,OAAO,GAAG,MAAM,GAAG,SAAS,GAAG,YAAY,GAAG,MAAM,CAAC;CAC/E,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG,CACxC,WAAW,EAAE,eAAe,KACzB,OAAO,CAAC,IAAI,CAAC,CAAC;AAEnB,MAAM,MAAM,uBAAuB,GAAG,CACpC,aAAa,EAAE,WAAW,EAC1B,aAAa,EAAE;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,KAC5B,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAE1B,iCAAiC;AACjC,MAAM,MAAM,eAAe,CAAC,GAAG,SAAS,OAAO,IAAI;IACjD,EAAE,EAAE,GAAG,CAAC;IACR,IAAI,EAAE,SAAS,CAAC;IAChB,YAAY,EAAE,2BAA2B,CAAC;IAC1C,QAAQ,EAAE,uBAAuB,CAAC;IAClC,MAAM,EAAE,MAAM,OAAO,CAA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{
|
7
7
|
".": {
|
8
|
-
"types": "./
|
8
|
+
"types": "./dist/index.d.ts",
|
9
9
|
"default": "./dist/index.js"
|
10
10
|
},
|
11
11
|
"./testing": {
|
12
|
-
"types": "./
|
12
|
+
"types": "./dist/testing.d.ts",
|
13
13
|
"default": "./dist/testing.js"
|
14
14
|
},
|
15
15
|
"./dist/*": "./dist/*",
|
@@ -17,10 +17,10 @@
|
|
17
17
|
},
|
18
18
|
"type": "module",
|
19
19
|
"license": "MIT",
|
20
|
-
"version": "0.
|
20
|
+
"version": "0.11.0",
|
21
21
|
"dependencies": {
|
22
22
|
"@scure/bip39": "^1.3.0",
|
23
|
-
"cojson": "0.
|
23
|
+
"cojson": "0.11.0"
|
24
24
|
},
|
25
25
|
"devDependencies": {
|
26
26
|
"tsup": "8.3.5",
|
@@ -38,9 +38,10 @@
|
|
38
38
|
"scripts": {
|
39
39
|
"format-and-lint": "biome check .",
|
40
40
|
"format-and-lint:fix": "biome check . --write",
|
41
|
-
"dev": "tsup --watch",
|
41
|
+
"dev": "tsup --watch --dts",
|
42
42
|
"test": "vitest --run --root ../../ --project jazz-tools",
|
43
43
|
"test:watch": "vitest --watch --root ../../ --project jazz-tools",
|
44
|
-
"
|
44
|
+
"types": "tsc --outDir dist",
|
45
|
+
"build": "tsup && pnpm types"
|
45
46
|
}
|
46
47
|
}
|
package/src/coValues/account.ts
CHANGED
@@ -2,6 +2,7 @@ import {
|
|
2
2
|
AgentSecret,
|
3
3
|
CoID,
|
4
4
|
CryptoProvider,
|
5
|
+
Everyone,
|
5
6
|
InviteSecret,
|
6
7
|
LocalNode,
|
7
8
|
Peer,
|
@@ -9,6 +10,7 @@ import {
|
|
9
10
|
RawCoMap,
|
10
11
|
RawCoValue,
|
11
12
|
RawControlledAccount,
|
13
|
+
Role,
|
12
14
|
SessionID,
|
13
15
|
cojsonInternals,
|
14
16
|
} from "cojson";
|
@@ -21,7 +23,6 @@ import {
|
|
21
23
|
DeeplyLoaded,
|
22
24
|
DepthsIn,
|
23
25
|
ID,
|
24
|
-
MembersSym,
|
25
26
|
Ref,
|
26
27
|
type RefEncoded,
|
27
28
|
RefIfCoValue,
|
@@ -36,6 +37,7 @@ import {
|
|
36
37
|
subscriptionsScopes,
|
37
38
|
} from "../internal.js";
|
38
39
|
import { coValuesCache } from "../lib/cache.js";
|
40
|
+
import { RegisteredAccount } from "../types.js";
|
39
41
|
import { type CoMap } from "./coMap.js";
|
40
42
|
import { type Group } from "./group.js";
|
41
43
|
import { createInboxRoot } from "./inbox.js";
|
@@ -168,6 +170,57 @@ export class Account extends CoValueBase implements CoValue {
|
|
168
170
|
}
|
169
171
|
}
|
170
172
|
|
173
|
+
getRoleOf(member: Everyone | ID<Account> | "me") {
|
174
|
+
if (member === "me") {
|
175
|
+
return this.isMe ? "admin" : undefined;
|
176
|
+
}
|
177
|
+
|
178
|
+
if (member === this.id) {
|
179
|
+
return "admin";
|
180
|
+
}
|
181
|
+
|
182
|
+
return undefined;
|
183
|
+
}
|
184
|
+
|
185
|
+
getParentGroups(): Array<Group> {
|
186
|
+
return [];
|
187
|
+
}
|
188
|
+
|
189
|
+
get members(): Array<{
|
190
|
+
id: ID<RegisteredAccount> | "everyone";
|
191
|
+
role: Role;
|
192
|
+
ref: Ref<RegisteredAccount> | undefined;
|
193
|
+
account: RegisteredAccount | null | undefined;
|
194
|
+
}> {
|
195
|
+
const ref = new Ref<RegisteredAccount>(this.id, this._loadedAs, {
|
196
|
+
ref: () => this.constructor as typeof Account,
|
197
|
+
optional: false,
|
198
|
+
});
|
199
|
+
|
200
|
+
return [{ id: this.id, role: "admin", ref, account: this }];
|
201
|
+
}
|
202
|
+
|
203
|
+
canRead(value: CoValue) {
|
204
|
+
const role = value._owner.getRoleOf(this.id);
|
205
|
+
|
206
|
+
return (
|
207
|
+
role === "admin" ||
|
208
|
+
role === "writer" ||
|
209
|
+
role === "reader" ||
|
210
|
+
role === "writeOnly"
|
211
|
+
);
|
212
|
+
}
|
213
|
+
|
214
|
+
canWrite(value: CoValue) {
|
215
|
+
const role = value._owner.getRoleOf(this.id);
|
216
|
+
|
217
|
+
return role === "admin" || role === "writer" || role === "writeOnly";
|
218
|
+
}
|
219
|
+
|
220
|
+
canAdmin(value: CoValue) {
|
221
|
+
return value._owner.getRoleOf(this.id) === "admin";
|
222
|
+
}
|
223
|
+
|
171
224
|
async acceptInvite<V extends CoValue>(
|
172
225
|
valueID: ID<V>,
|
173
226
|
inviteSecret: InviteSecret,
|
@@ -259,13 +312,20 @@ export class Account extends CoValueBase implements CoValue {
|
|
259
312
|
}
|
260
313
|
|
261
314
|
async applyMigration(creationProps?: AccountCreationProps) {
|
262
|
-
|
315
|
+
await this.migrate(creationProps);
|
316
|
+
|
317
|
+
// if the user has not defined a profile themselves, we create one
|
318
|
+
if (this.profile === undefined && creationProps) {
|
263
319
|
const profileGroup = RegisteredSchemas["Group"].create({ owner: this });
|
264
|
-
|
265
|
-
this.profile = Profile.create(
|
266
|
-
|
267
|
-
|
268
|
-
)
|
320
|
+
|
321
|
+
this.profile = Profile.create({ name: creationProps.name }, profileGroup);
|
322
|
+
this.profile._owner.addMember("everyone", "reader");
|
323
|
+
} else if (this.profile && creationProps) {
|
324
|
+
if (this.profile._owner._type !== "Group") {
|
325
|
+
throw new Error("Profile must be owned by a Group", {
|
326
|
+
cause: `The profile of the account "${this.id}" was created with an Account as owner, which is not allowed.`,
|
327
|
+
});
|
328
|
+
}
|
269
329
|
}
|
270
330
|
|
271
331
|
const node = this._raw.core.node;
|
@@ -278,8 +338,6 @@ export class Account extends CoValueBase implements CoValue {
|
|
278
338
|
profile.set("inbox", inboxRoot.id);
|
279
339
|
profile.set("inboxInvite", inboxRoot.inviteLink);
|
280
340
|
}
|
281
|
-
|
282
|
-
await this.migrate(creationProps);
|
283
341
|
}
|
284
342
|
|
285
343
|
// Placeholder method for subclasses to override
|
@@ -400,7 +458,7 @@ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
|
|
400
458
|
},
|
401
459
|
set(target, key, value, receiver) {
|
402
460
|
if (
|
403
|
-
(key === "profile" || key === "root"
|
461
|
+
(key === "profile" || key === "root") &&
|
404
462
|
typeof value === "object" &&
|
405
463
|
SchemaInit in value
|
406
464
|
) {
|
@@ -433,7 +491,7 @@ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
|
|
433
491
|
},
|
434
492
|
defineProperty(target, key, descriptor) {
|
435
493
|
if (
|
436
|
-
(key === "profile" || key === "root"
|
494
|
+
(key === "profile" || key === "root") &&
|
437
495
|
typeof descriptor.value === "object" &&
|
438
496
|
SchemaInit in descriptor.value
|
439
497
|
) {
|
package/src/coValues/coMap.ts
CHANGED
@@ -151,7 +151,7 @@ export class CoMap extends CoValueBase implements CoValue {
|
|
151
151
|
}
|
152
152
|
|
153
153
|
/** @internal */
|
154
|
-
|
154
|
+
public getEditFromRaw(
|
155
155
|
target: CoMap,
|
156
156
|
rawEdit: {
|
157
157
|
by: RawAccountID | AgentID;
|
@@ -309,7 +309,7 @@ export class CoMap extends CoValueBase implements CoValue {
|
|
309
309
|
const descriptor = (this._schema[tKey] ||
|
310
310
|
this._schema[ItemsSym]) as Schema;
|
311
311
|
|
312
|
-
if (descriptor == "json" || "
|
312
|
+
if (descriptor == "json" || "encoded" in descriptor) {
|
313
313
|
return [key, this._raw.get(key)];
|
314
314
|
} else if (isRefEncoded(descriptor)) {
|
315
315
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|