jazz-tools 0.7.35-unique.2 → 0.7.35
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-test.log +44 -57
- package/CHANGELOG.md +8 -10
- package/dist/coValues/account.js +35 -20
- package/dist/coValues/account.js.map +1 -1
- package/dist/coValues/coList.js +19 -14
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/coMap.js +33 -32
- package/dist/coValues/coMap.js.map +1 -1
- package/dist/coValues/coStream.js +37 -15
- package/dist/coValues/coStream.js.map +1 -1
- package/dist/coValues/extensions/imageDef.js +8 -3
- package/dist/coValues/extensions/imageDef.js.map +1 -1
- package/dist/coValues/group.js +23 -20
- package/dist/coValues/group.js.map +1 -1
- package/dist/implementation/devtoolsFormatters.js +1 -0
- package/dist/implementation/devtoolsFormatters.js.map +1 -1
- package/dist/implementation/refs.js +0 -3
- package/dist/implementation/refs.js.map +1 -1
- package/dist/implementation/schema.js.map +1 -1
- package/dist/implementation/subscriptionScope.js +5 -8
- package/dist/implementation/subscriptionScope.js.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.js +0 -1
- package/dist/internal.js.map +1 -1
- package/dist/tests/coList.test.js +15 -23
- package/dist/tests/coList.test.js.map +1 -1
- package/dist/tests/coMap.test.js +216 -75
- package/dist/tests/coMap.test.js.map +1 -1
- package/dist/tests/coStream.test.js +59 -26
- package/dist/tests/coStream.test.js.map +1 -1
- package/dist/tests/deepLoading.test.js +41 -28
- package/dist/tests/deepLoading.test.js.map +1 -1
- package/dist/tests/groupsAndAccounts.test.js +22 -8
- package/dist/tests/groupsAndAccounts.test.js.map +1 -1
- package/package.json +3 -3
- package/src/coValues/account.ts +28 -5
- package/src/coValues/coList.ts +4 -4
- package/src/coValues/coMap.ts +54 -46
- package/src/coValues/coStream.ts +30 -5
- package/src/coValues/group.ts +2 -2
- package/src/coValues/interfaces.ts +4 -4
- package/src/implementation/devtoolsFormatters.ts +1 -0
- package/src/implementation/schema.ts +1 -0
- package/src/index.ts +1 -9
- package/src/internal.ts +0 -2
- package/src/tests/coList.test.ts +14 -30
- package/src/tests/coMap.test.ts +158 -49
- package/src/tests/coStream.test.ts +74 -27
- package/src/tests/deepLoading.test.ts +18 -22
- package/tsconfig.json +1 -1
- package/dist/implementation/createContext.js +0 -94
- package/dist/implementation/createContext.js.map +0 -1
- package/src/implementation/createContext.ts +0 -162
@@ -63,11 +63,11 @@ type IDMarker<out T> = { __type(_: never): T };
|
|
63
63
|
|
64
64
|
/** @internal */
|
65
65
|
export class CoValueBase implements CoValue {
|
66
|
-
|
67
|
-
|
68
|
-
|
66
|
+
id!: ID<this>;
|
67
|
+
_type!: string;
|
68
|
+
_raw!: RawCoValue;
|
69
69
|
/** @category Internals */
|
70
|
-
|
70
|
+
_instanceID!: string;
|
71
71
|
|
72
72
|
get _owner(): Account | Group {
|
73
73
|
const owner =
|
@@ -88,6 +88,7 @@ function optionalRef<C extends CoValueClass>(
|
|
88
88
|
|
89
89
|
function ref<C extends CoValueClass>(
|
90
90
|
arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
|
91
|
+
options?: never,
|
91
92
|
): co<InstanceType<C> | null>;
|
92
93
|
function ref<C extends CoValueClass>(
|
93
94
|
arg: C | ((_raw: InstanceType<C>["_raw"]) => C),
|
package/src/index.ts
CHANGED
@@ -11,7 +11,6 @@ export type {
|
|
11
11
|
AgentID,
|
12
12
|
SyncMessage,
|
13
13
|
CryptoProvider,
|
14
|
-
CoValueUniqueness,
|
15
14
|
} from "cojson";
|
16
15
|
|
17
16
|
export type { ID, CoValue } from "./internal.js";
|
@@ -22,16 +21,9 @@ export { CoMap, type CoMapInit } from "./internal.js";
|
|
22
21
|
export { CoList } from "./internal.js";
|
23
22
|
export { CoStream, BinaryCoStream } from "./internal.js";
|
24
23
|
export { Group, Profile } from "./internal.js";
|
25
|
-
export { Account, isControlledAccount
|
24
|
+
export { Account, isControlledAccount } from "./internal.js";
|
26
25
|
export { ImageDefinition } from "./internal.js";
|
27
26
|
export { CoValueBase, type CoValueClass } from "./internal.js";
|
28
27
|
export type { DepthsIn, DeeplyLoaded } from "./internal.js";
|
29
28
|
|
30
29
|
export { loadCoValue, subscribeToCoValue } from "./internal.js";
|
31
|
-
|
32
|
-
export {
|
33
|
-
type AuthMethod,
|
34
|
-
type AuthResult,
|
35
|
-
createJazzContext,
|
36
|
-
fixedCredentialsAuth,
|
37
|
-
} from "./internal.js";
|
package/src/internal.ts
CHANGED
package/src/tests/coList.test.ts
CHANGED
@@ -1,16 +1,14 @@
|
|
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";
|
3
4
|
import {
|
4
5
|
Account,
|
5
6
|
CoList,
|
6
7
|
WasmCrypto,
|
7
8
|
co,
|
8
9
|
cojsonInternals,
|
9
|
-
createJazzContext,
|
10
10
|
isControlledAccount,
|
11
|
-
fixedCredentialsAuth,
|
12
11
|
} from "../index.js";
|
13
|
-
import { randomSessionProvider } from "../internal.js";
|
14
12
|
|
15
13
|
const Crypto = await WasmCrypto.create();
|
16
14
|
|
@@ -171,13 +169,12 @@ describe("CoList resolution", async () => {
|
|
171
169
|
throw "me is not a controlled account";
|
172
170
|
}
|
173
171
|
me._raw.core.node.syncManager.addPeer(secondPeer);
|
174
|
-
const
|
175
|
-
|
176
|
-
|
177
|
-
secret: me._raw.agentSecret,
|
178
|
-
}),
|
179
|
-
sessionProvider: randomSessionProvider,
|
172
|
+
const meOnSecondPeer = await Account.become({
|
173
|
+
accountID: me.id,
|
174
|
+
accountSecret: me._raw.agentSecret,
|
180
175
|
peersToLoadFrom: [initialAsPeer],
|
176
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
177
|
+
sessionID: newRandomSessionID(me.id as any),
|
181
178
|
crypto: Crypto,
|
182
179
|
});
|
183
180
|
|
@@ -195,11 +192,7 @@ describe("CoList resolution", async () => {
|
|
195
192
|
expect(loadedList?.[0]).toBeDefined();
|
196
193
|
expect(loadedList?.[0]?.[0]).toBe(null);
|
197
194
|
expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]![0]!.id);
|
198
|
-
|
199
|
-
// expect(loadedList?._refs[0]?.value).toEqual(loadedNestedList);
|
200
|
-
expect(loadedList?._refs[0]?.value?.toJSON()).toEqual(
|
201
|
-
loadedNestedList?.toJSON(),
|
202
|
-
);
|
195
|
+
expect(loadedList?._refs[0]?.value).toEqual(loadedNestedList);
|
203
196
|
|
204
197
|
const loadedTwiceNestedList = await TwiceNestedList.load(
|
205
198
|
list[0]![0]!.id,
|
@@ -211,11 +204,7 @@ describe("CoList resolution", async () => {
|
|
211
204
|
expect(loadedList?.[0]?.[0]?.[0]).toBe("a");
|
212
205
|
expect(loadedList?.[0]?.[0]?.joined()).toBe("a,b");
|
213
206
|
expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]?.[0]?.id);
|
214
|
-
|
215
|
-
// expect(loadedList?.[0]?._refs[0]?.value).toEqual(loadedTwiceNestedList);
|
216
|
-
expect(loadedList?.[0]?._refs[0]?.value?.toJSON()).toEqual(
|
217
|
-
loadedTwiceNestedList?.toJSON(),
|
218
|
-
);
|
207
|
+
expect(loadedList?.[0]?._refs[0]?.value).toEqual(loadedTwiceNestedList);
|
219
208
|
|
220
209
|
const otherNestedList = NestedList.create(
|
221
210
|
[TwiceNestedList.create(["e", "f"], { owner: meOnSecondPeer })],
|
@@ -223,11 +212,7 @@ describe("CoList resolution", async () => {
|
|
223
212
|
);
|
224
213
|
|
225
214
|
loadedList![0] = otherNestedList;
|
226
|
-
|
227
|
-
// expect(loadedList?.[0]).toEqual(otherNestedList);
|
228
|
-
expect(loadedList?._refs[0]?.value?.toJSON()).toEqual(
|
229
|
-
otherNestedList.toJSON(),
|
230
|
-
);
|
215
|
+
expect(loadedList?.[0]).toEqual(otherNestedList);
|
231
216
|
expect(loadedList?._refs[0]?.id).toEqual(otherNestedList.id);
|
232
217
|
});
|
233
218
|
|
@@ -246,13 +231,12 @@ describe("CoList resolution", async () => {
|
|
246
231
|
throw "me is not a controlled account";
|
247
232
|
}
|
248
233
|
me._raw.core.node.syncManager.addPeer(secondPeer);
|
249
|
-
const
|
250
|
-
|
251
|
-
|
252
|
-
secret: me._raw.agentSecret,
|
253
|
-
}),
|
254
|
-
sessionProvider: randomSessionProvider,
|
234
|
+
const meOnSecondPeer = await Account.become({
|
235
|
+
accountID: me.id,
|
236
|
+
accountSecret: me._raw.agentSecret,
|
255
237
|
peersToLoadFrom: [initialAsPeer],
|
238
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
239
|
+
sessionID: newRandomSessionID(me.id as any),
|
256
240
|
crypto: Crypto,
|
257
241
|
});
|
258
242
|
|
package/src/tests/coMap.test.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
import { expect, describe, test } from "vitest";
|
1
|
+
import { expect, describe, test, expectTypeOf } from "vitest";
|
2
2
|
import { connectedPeers } from "cojson/src/streamUtils.js";
|
3
|
+
import { newRandomSessionID } from "cojson/src/coValueCore.js";
|
3
4
|
import {
|
4
5
|
Account,
|
5
6
|
Encoders,
|
@@ -8,35 +9,32 @@ import {
|
|
8
9
|
WasmCrypto,
|
9
10
|
isControlledAccount,
|
10
11
|
cojsonInternals,
|
11
|
-
createJazzContext,
|
12
|
-
fixedCredentialsAuth,
|
13
12
|
} from "../index.js";
|
14
|
-
import { Group, randomSessionProvider } from "../internal.js";
|
15
13
|
|
16
14
|
const Crypto = await WasmCrypto.create();
|
17
15
|
|
18
|
-
class TestMap extends CoMap {
|
19
|
-
color = co.string;
|
20
|
-
_height = co.number;
|
21
|
-
birthday = co.encoded(Encoders.Date);
|
22
|
-
name? = co.string;
|
23
|
-
nullable = co.optional.encoded<string | undefined>({
|
24
|
-
encode: (value: string | undefined) => value || null,
|
25
|
-
decode: (value: unknown) => (value as string) || undefined,
|
26
|
-
});
|
27
|
-
optionalDate = co.optional.encoded(Encoders.Date);
|
28
|
-
|
29
|
-
get roughColor() {
|
30
|
-
return this.color + "ish";
|
31
|
-
}
|
32
|
-
}
|
33
|
-
|
34
16
|
describe("Simple CoMap operations", async () => {
|
35
17
|
const me = await Account.create({
|
36
18
|
creationProps: { name: "Hermes Puggington" },
|
37
19
|
crypto: Crypto,
|
38
20
|
});
|
39
21
|
|
22
|
+
class TestMap extends CoMap {
|
23
|
+
color = co.string;
|
24
|
+
_height = co.number;
|
25
|
+
birthday = co.encoded(Encoders.Date);
|
26
|
+
name? = co.string;
|
27
|
+
nullable = co.optional.encoded<string | undefined>({
|
28
|
+
encode: (value: string | undefined) => value || null,
|
29
|
+
decode: (value: unknown) => (value as string) || undefined,
|
30
|
+
});
|
31
|
+
optionalDate = co.optional.encoded(Encoders.Date);
|
32
|
+
|
33
|
+
get roughColor() {
|
34
|
+
return this.color + "ish";
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
40
38
|
console.log("TestMap schema", TestMap.prototype._schema);
|
41
39
|
|
42
40
|
const birthday = new Date();
|
@@ -299,13 +297,12 @@ describe("CoMap resolution", async () => {
|
|
299
297
|
throw "me is not a controlled account";
|
300
298
|
}
|
301
299
|
me._raw.core.node.syncManager.addPeer(secondPeer);
|
302
|
-
const
|
303
|
-
|
304
|
-
|
305
|
-
secret: me._raw.agentSecret,
|
306
|
-
}),
|
307
|
-
sessionProvider: randomSessionProvider,
|
300
|
+
const meOnSecondPeer = await Account.become({
|
301
|
+
accountID: me.id,
|
302
|
+
accountSecret: me._raw.agentSecret,
|
308
303
|
peersToLoadFrom: [initialAsPeer],
|
304
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
305
|
+
sessionID: newRandomSessionID(me.id as any),
|
309
306
|
crypto: Crypto,
|
310
307
|
});
|
311
308
|
|
@@ -374,13 +371,12 @@ describe("CoMap resolution", async () => {
|
|
374
371
|
throw "me is not a controlled account";
|
375
372
|
}
|
376
373
|
me._raw.core.node.syncManager.addPeer(secondAsPeer);
|
377
|
-
const
|
378
|
-
|
379
|
-
|
380
|
-
secret: me._raw.agentSecret,
|
381
|
-
}),
|
382
|
-
sessionProvider: randomSessionProvider,
|
374
|
+
const meOnSecondPeer = await Account.become({
|
375
|
+
accountID: me.id,
|
376
|
+
accountSecret: me._raw.agentSecret,
|
383
377
|
peersToLoadFrom: [initialAsPeer],
|
378
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
379
|
+
sessionID: newRandomSessionID(me.id as any),
|
384
380
|
crypto: Crypto,
|
385
381
|
});
|
386
382
|
|
@@ -592,6 +588,7 @@ describe("CoMap applyDiff", async () => {
|
|
592
588
|
birthday = co.encoded(Encoders.Date);
|
593
589
|
nested = co.ref(NestedMap);
|
594
590
|
optionalField = co.optional.string;
|
591
|
+
optionalNested = co.optional.ref(NestedMap);
|
595
592
|
}
|
596
593
|
|
597
594
|
class NestedMap extends CoMap {
|
@@ -737,29 +734,141 @@ describe("CoMap applyDiff", async () => {
|
|
737
734
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
738
735
|
expect((map as any).invalidField).toBeUndefined();
|
739
736
|
});
|
737
|
+
|
738
|
+
test("applyDiff with optional reference set to null", () => {
|
739
|
+
const map = TestMap.create(
|
740
|
+
{
|
741
|
+
name: "Jack",
|
742
|
+
age: 50,
|
743
|
+
isActive: true,
|
744
|
+
birthday: new Date("1970-01-01"),
|
745
|
+
nested: NestedMap.create({ value: "original" }, { owner: me }),
|
746
|
+
optionalNested: NestedMap.create(
|
747
|
+
{ value: "optional" },
|
748
|
+
{ owner: me },
|
749
|
+
),
|
750
|
+
},
|
751
|
+
{ owner: me },
|
752
|
+
);
|
753
|
+
|
754
|
+
const newValues = {
|
755
|
+
optionalNested: null,
|
756
|
+
};
|
757
|
+
|
758
|
+
map.applyDiff(newValues);
|
759
|
+
|
760
|
+
expect(map.optionalNested).toBeNull();
|
761
|
+
});
|
762
|
+
|
763
|
+
test("applyDiff with required reference set to null should throw", () => {
|
764
|
+
const map = TestMap.create(
|
765
|
+
{
|
766
|
+
name: "Kate",
|
767
|
+
age: 55,
|
768
|
+
isActive: true,
|
769
|
+
birthday: new Date("1965-01-01"),
|
770
|
+
nested: NestedMap.create({ value: "original" }, { owner: me }),
|
771
|
+
},
|
772
|
+
{ owner: me },
|
773
|
+
);
|
774
|
+
|
775
|
+
const newValues = {
|
776
|
+
nested: null,
|
777
|
+
};
|
778
|
+
|
779
|
+
// @ts-expect-error testing invalid usage
|
780
|
+
expect(() => map.applyDiff(newValues)).toThrowError(
|
781
|
+
"Cannot set required reference nested to null",
|
782
|
+
);
|
783
|
+
});
|
740
784
|
});
|
741
785
|
|
742
|
-
describe("
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
});
|
786
|
+
describe("CoMap Typescript validation", async () => {
|
787
|
+
const me = await Account.create({
|
788
|
+
creationProps: { name: "Hermes Puggington" },
|
789
|
+
crypto: Crypto,
|
790
|
+
});
|
748
791
|
|
749
|
-
|
750
|
-
|
751
|
-
|
792
|
+
test("Is not ok to pass null into a required ref", () => {
|
793
|
+
class TestMap extends CoMap {
|
794
|
+
required = co.ref(NestedMap);
|
795
|
+
optional = co.optional.ref(NestedMap);
|
796
|
+
}
|
752
797
|
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
798
|
+
class NestedMap extends CoMap {
|
799
|
+
value = co.string;
|
800
|
+
}
|
801
|
+
|
802
|
+
expectTypeOf<typeof TestMap.create<TestMap>>().toBeCallableWith(
|
803
|
+
{
|
804
|
+
optional: NestedMap.create({ value: "" }, { owner: me }),
|
805
|
+
// @ts-expect-error null can't be passed to a non-optional field
|
806
|
+
required: null,
|
807
|
+
},
|
808
|
+
{ owner: me },
|
809
|
+
);
|
810
|
+
});
|
811
|
+
|
812
|
+
test("Is not ok if a required ref is omitted", () => {
|
813
|
+
class TestMap extends CoMap {
|
814
|
+
required = co.ref(NestedMap);
|
815
|
+
optional = co.ref(NestedMap, { optional: true });
|
816
|
+
}
|
817
|
+
|
818
|
+
class NestedMap extends CoMap {
|
819
|
+
value = co.string;
|
820
|
+
}
|
821
|
+
|
822
|
+
expectTypeOf<typeof TestMap.create<TestMap>>().toBeCallableWith(
|
823
|
+
// @ts-expect-error non-optional fields can't be omitted
|
824
|
+
{},
|
825
|
+
{ owner: me },
|
826
|
+
);
|
827
|
+
});
|
758
828
|
|
759
|
-
|
829
|
+
test("Is ok to omit optional fields", () => {
|
830
|
+
class TestMap extends CoMap {
|
831
|
+
required = co.ref(NestedMap);
|
832
|
+
optional = co.ref(NestedMap, { optional: true });
|
833
|
+
}
|
834
|
+
|
835
|
+
class NestedMap extends CoMap {
|
836
|
+
value = co.string;
|
837
|
+
}
|
838
|
+
|
839
|
+
expectTypeOf<typeof TestMap.create<TestMap>>().toBeCallableWith(
|
840
|
+
{
|
841
|
+
required: NestedMap.create({ value: "" }, { owner: me }),
|
842
|
+
},
|
843
|
+
{ owner: me },
|
844
|
+
);
|
760
845
|
|
761
|
-
|
846
|
+
expectTypeOf<typeof TestMap.create<TestMap>>().toBeCallableWith(
|
847
|
+
{
|
848
|
+
required: NestedMap.create({ value: "" }, { owner: me }),
|
849
|
+
optional: null,
|
850
|
+
},
|
851
|
+
{ owner: me },
|
852
|
+
);
|
853
|
+
});
|
854
|
+
|
855
|
+
test("the required refs should be nullable", () => {
|
856
|
+
class TestMap extends CoMap {
|
857
|
+
required = co.ref(NestedMap);
|
858
|
+
optional = co.ref(NestedMap, { optional: true });
|
859
|
+
}
|
860
|
+
|
861
|
+
class NestedMap extends CoMap {
|
862
|
+
value = co.string;
|
863
|
+
}
|
864
|
+
|
865
|
+
const map = TestMap.create(
|
866
|
+
{
|
867
|
+
required: NestedMap.create({ value: "" }, { owner: me }),
|
868
|
+
},
|
869
|
+
{ owner: me },
|
870
|
+
);
|
762
871
|
|
763
|
-
|
872
|
+
expectTypeOf(map.required).toBeNullable();
|
764
873
|
});
|
765
874
|
});
|
@@ -1,5 +1,6 @@
|
|
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";
|
3
4
|
import {
|
4
5
|
BinaryCoStream,
|
5
6
|
ID,
|
@@ -9,10 +10,7 @@ import {
|
|
9
10
|
WasmCrypto,
|
10
11
|
isControlledAccount,
|
11
12
|
cojsonInternals,
|
12
|
-
createJazzContext,
|
13
|
-
fixedCredentialsAuth,
|
14
13
|
} from "../index.js";
|
15
|
-
import { randomSessionProvider } from "../internal.js";
|
16
14
|
|
17
15
|
const Crypto = await WasmCrypto.create();
|
18
16
|
|
@@ -97,13 +95,12 @@ describe("CoStream resolution", async () => {
|
|
97
95
|
throw "me is not a controlled account";
|
98
96
|
}
|
99
97
|
me._raw.core.node.syncManager.addPeer(secondPeer);
|
100
|
-
const
|
101
|
-
|
102
|
-
|
103
|
-
secret: me._raw.agentSecret,
|
104
|
-
}),
|
105
|
-
sessionProvider: randomSessionProvider,
|
98
|
+
const meOnSecondPeer = await Account.become({
|
99
|
+
accountID: me.id,
|
100
|
+
accountSecret: me._raw.agentSecret,
|
106
101
|
peersToLoadFrom: [initialAsPeer],
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
103
|
+
sessionID: newRandomSessionID(me.id as any),
|
107
104
|
crypto: Crypto,
|
108
105
|
});
|
109
106
|
|
@@ -194,13 +191,12 @@ describe("CoStream resolution", async () => {
|
|
194
191
|
if (!isControlledAccount(me)) {
|
195
192
|
throw "me is not a controlled account";
|
196
193
|
}
|
197
|
-
const
|
198
|
-
|
199
|
-
|
200
|
-
secret: me._raw.agentSecret,
|
201
|
-
}),
|
202
|
-
sessionProvider: randomSessionProvider,
|
194
|
+
const meOnSecondPeer = await Account.become({
|
195
|
+
accountID: me.id,
|
196
|
+
accountSecret: me._raw.agentSecret,
|
203
197
|
peersToLoadFrom: [initialAsPeer],
|
198
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
199
|
+
sessionID: newRandomSessionID(me.id as any),
|
204
200
|
crypto: Crypto,
|
205
201
|
});
|
206
202
|
|
@@ -338,13 +334,12 @@ describe("BinaryCoStream loading & Subscription", async () => {
|
|
338
334
|
throw "me is not a controlled account";
|
339
335
|
}
|
340
336
|
me._raw.core.node.syncManager.addPeer(secondAsPeer);
|
341
|
-
const
|
342
|
-
|
343
|
-
|
344
|
-
secret: me._raw.agentSecret,
|
345
|
-
}),
|
346
|
-
sessionProvider: randomSessionProvider,
|
337
|
+
const meOnSecondPeer = await Account.become({
|
338
|
+
accountID: me.id,
|
339
|
+
accountSecret: me._raw.agentSecret,
|
347
340
|
peersToLoadFrom: [initialAsPeer],
|
341
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
342
|
+
sessionID: newRandomSessionID(me.id as any),
|
348
343
|
crypto: Crypto,
|
349
344
|
});
|
350
345
|
|
@@ -374,13 +369,12 @@ describe("BinaryCoStream loading & Subscription", async () => {
|
|
374
369
|
if (!isControlledAccount(me)) {
|
375
370
|
throw "me is not a controlled account";
|
376
371
|
}
|
377
|
-
const
|
378
|
-
|
379
|
-
|
380
|
-
secret: me._raw.agentSecret,
|
381
|
-
}),
|
382
|
-
sessionProvider: randomSessionProvider,
|
372
|
+
const meOnSecondPeer = await Account.become({
|
373
|
+
accountID: me.id,
|
374
|
+
accountSecret: me._raw.agentSecret,
|
383
375
|
peersToLoadFrom: [initialAsPeer],
|
376
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
377
|
+
sessionID: newRandomSessionID(me.id as any),
|
384
378
|
crypto: Crypto,
|
385
379
|
});
|
386
380
|
|
@@ -443,3 +437,56 @@ describe("BinaryCoStream loading & Subscription", async () => {
|
|
443
437
|
});
|
444
438
|
});
|
445
439
|
});
|
440
|
+
|
441
|
+
describe("BinaryCoStream.loadAsBlob", async () => {
|
442
|
+
async function setup() {
|
443
|
+
const me = await Account.create({
|
444
|
+
creationProps: { name: "Hermes Puggington" },
|
445
|
+
crypto: Crypto,
|
446
|
+
});
|
447
|
+
|
448
|
+
const stream = BinaryCoStream.create({ owner: me });
|
449
|
+
|
450
|
+
stream.start({ mimeType: "text/plain" });
|
451
|
+
|
452
|
+
return { stream, me };
|
453
|
+
}
|
454
|
+
|
455
|
+
test("resolves only when the stream is ended", async () => {
|
456
|
+
const { stream, me } = await setup();
|
457
|
+
stream.push(new Uint8Array([1]));
|
458
|
+
|
459
|
+
const promise = BinaryCoStream.loadAsBlob(stream.id, me);
|
460
|
+
|
461
|
+
await stream.ensureLoaded([]);
|
462
|
+
|
463
|
+
stream.push(new Uint8Array([2]));
|
464
|
+
stream.end();
|
465
|
+
|
466
|
+
const blob = await promise;
|
467
|
+
|
468
|
+
// The promise resolves only when the stream is ended
|
469
|
+
// so we get a blob with all the chunks
|
470
|
+
expect(blob?.size).toBe(2);
|
471
|
+
});
|
472
|
+
|
473
|
+
test("resolves with an unfinshed blob if allowUnfinished: true", async () => {
|
474
|
+
const { stream, me } = await setup();
|
475
|
+
stream.push(new Uint8Array([1]));
|
476
|
+
|
477
|
+
const promise = BinaryCoStream.loadAsBlob(stream.id, me, {
|
478
|
+
allowUnfinished: true,
|
479
|
+
});
|
480
|
+
|
481
|
+
await stream.ensureLoaded([]);
|
482
|
+
|
483
|
+
stream.push(new Uint8Array([2]));
|
484
|
+
stream.end();
|
485
|
+
|
486
|
+
const blob = await promise;
|
487
|
+
|
488
|
+
// The promise resolves before the stream is ended
|
489
|
+
// so we get a blob only with the first chunk
|
490
|
+
expect(blob?.size).toBe(1);
|
491
|
+
});
|
492
|
+
});
|
@@ -12,10 +12,8 @@ import {
|
|
12
12
|
Profile,
|
13
13
|
isControlledAccount,
|
14
14
|
ID,
|
15
|
-
createJazzContext,
|
16
|
-
fixedCredentialsAuth,
|
17
15
|
} from "../index.js";
|
18
|
-
import {
|
16
|
+
import { newRandomSessionID } from "cojson/src/coValueCore.js";
|
19
17
|
|
20
18
|
class TestMap extends CoMap {
|
21
19
|
list = co.ref(TestList);
|
@@ -49,17 +47,17 @@ describe("Deep loading with depth arg", async () => {
|
|
49
47
|
throw "me is not a controlled account";
|
50
48
|
}
|
51
49
|
me._raw.core.node.syncManager.addPeer(secondPeer);
|
52
|
-
const
|
53
|
-
|
54
|
-
|
55
|
-
secret: me._raw.agentSecret,
|
56
|
-
}),
|
57
|
-
sessionProvider: randomSessionProvider,
|
50
|
+
const meOnSecondPeer = await Account.become({
|
51
|
+
accountID: me.id,
|
52
|
+
accountSecret: me._raw.agentSecret,
|
58
53
|
peersToLoadFrom: [initialAsPeer],
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
55
|
+
sessionID: newRandomSessionID(me.id as any),
|
59
56
|
crypto: Crypto,
|
60
57
|
});
|
61
58
|
|
62
59
|
test("loading a deeply nested object will wait until all required refs are loaded", async () => {
|
60
|
+
const ownership = { owner: me };
|
63
61
|
const map = TestMap.create(
|
64
62
|
{
|
65
63
|
list: TestList.create(
|
@@ -70,19 +68,19 @@ describe("Deep loading with depth arg", async () => {
|
|
70
68
|
[
|
71
69
|
InnermostMap.create(
|
72
70
|
{ value: "hello" },
|
73
|
-
|
71
|
+
ownership,
|
74
72
|
),
|
75
73
|
],
|
76
|
-
|
74
|
+
ownership,
|
77
75
|
),
|
78
76
|
},
|
79
|
-
|
77
|
+
ownership,
|
80
78
|
),
|
81
79
|
],
|
82
|
-
|
80
|
+
ownership,
|
83
81
|
),
|
84
82
|
},
|
85
|
-
|
83
|
+
ownership,
|
86
84
|
);
|
87
85
|
|
88
86
|
const map1 = await TestMap.load(map.id, meOnSecondPeer, {});
|
@@ -141,8 +139,7 @@ describe("Deep loading with depth arg", async () => {
|
|
141
139
|
throw new Error("map4 is undefined");
|
142
140
|
}
|
143
141
|
expect(map4.list[0]?.stream).not.toBe(null);
|
144
|
-
|
145
|
-
// expect(map4.list[0]?.stream?.[me.id]).toBe(undefined)
|
142
|
+
expect(map4.list[0]?.stream?.[me.id]).not.toBe(null);
|
146
143
|
expect(map4.list[0]?.stream?.byMe?.value).toBe(null);
|
147
144
|
|
148
145
|
const map5 = await TestMap.load(map.id, meOnSecondPeer, {
|
@@ -266,13 +263,12 @@ test("Deep loading a record-like coMap", async () => {
|
|
266
263
|
}
|
267
264
|
|
268
265
|
me._raw.core.node.syncManager.addPeer(secondPeer);
|
269
|
-
const
|
270
|
-
|
271
|
-
|
272
|
-
secret: me._raw.agentSecret,
|
273
|
-
}),
|
274
|
-
sessionProvider: randomSessionProvider,
|
266
|
+
const meOnSecondPeer = await Account.become({
|
267
|
+
accountID: me.id,
|
268
|
+
accountSecret: me._raw.agentSecret,
|
275
269
|
peersToLoadFrom: [initialAsPeer],
|
270
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
271
|
+
sessionID: newRandomSessionID(me.id as any),
|
276
272
|
crypto: Crypto,
|
277
273
|
});
|
278
274
|
|