jazz-tools 0.18.25 → 0.18.27
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 +41 -41
- package/CHANGELOG.md +21 -0
- package/dist/{chunk-DOCEAUVD.js → chunk-ZIAN4UY5.js} +338 -9
- package/dist/chunk-ZIAN4UY5.js.map +1 -0
- package/dist/index.js +97 -5
- package/dist/index.js.map +1 -1
- package/dist/media/{chunk-W3S526L3.js → chunk-K6GCHLQU.js} +1 -1
- package/dist/media/chunk-K6GCHLQU.js.map +1 -0
- package/dist/media/create-image/browser.d.ts +1 -1
- package/dist/media/create-image/react-native.d.ts +1 -1
- package/dist/media/create-image/react-native.d.ts.map +1 -1
- package/dist/media/create-image/server.d.ts +1 -1
- package/dist/media/create-image-factory.d.ts +5 -2
- package/dist/media/create-image-factory.d.ts.map +1 -1
- package/dist/media/index.browser.js +1 -1
- package/dist/media/index.d.ts +3 -4
- package/dist/media/index.d.ts.map +1 -1
- package/dist/media/index.js +1 -1
- package/dist/media/index.native.js +63 -28
- package/dist/media/index.native.js.map +1 -1
- package/dist/media/index.server.js +1 -1
- package/dist/react/index.js.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/tools/coValues/coVector.d.ts +127 -0
- package/dist/tools/coValues/coVector.d.ts.map +1 -0
- package/dist/tools/coValues/request.d.ts +70 -0
- package/dist/tools/coValues/request.d.ts.map +1 -1
- package/dist/tools/exports.d.ts +1 -1
- package/dist/tools/exports.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/coExport.d.ts +2 -1
- package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.d.ts +47 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +2 -2
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +2 -2
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodCo.d.ts +2 -1
- package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts +3 -2
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
- package/dist/tools/internal.d.ts +2 -0
- package/dist/tools/internal.d.ts.map +1 -1
- package/dist/tools/tests/authenticate-request.test.d.ts +2 -0
- package/dist/tools/tests/authenticate-request.test.d.ts.map +1 -0
- package/dist/tools/tests/coVector.test-d.d.ts +2 -0
- package/dist/tools/tests/coVector.test-d.d.ts.map +1 -0
- package/dist/tools/tests/coVector.test.d.ts +2 -0
- package/dist/tools/tests/coVector.test.d.ts.map +1 -0
- package/package.json +8 -4
- package/src/media/create-image/react-native.ts +75 -30
- package/src/media/create-image-factory.test.ts +18 -0
- package/src/media/create-image-factory.ts +6 -1
- package/src/media/index.ts +7 -4
- package/src/tools/coValues/coVector.ts +432 -0
- package/src/tools/coValues/request.ts +188 -4
- package/src/tools/exports.ts +3 -0
- package/src/tools/implementation/zodSchema/coExport.ts +2 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +13 -0
- package/src/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.ts +105 -0
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +9 -5
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +15 -9
- package/src/tools/implementation/zodSchema/zodCo.ts +15 -0
- package/src/tools/implementation/zodSchema/zodSchema.ts +15 -6
- package/src/tools/internal.ts +2 -0
- package/src/tools/tests/authenticate-request.test.ts +194 -0
- package/src/tools/tests/coVector.test-d.ts +40 -0
- package/src/tools/tests/coVector.test.ts +891 -0
- package/dist/chunk-DOCEAUVD.js.map +0 -1
- package/dist/media/chunk-W3S526L3.js.map +0 -1
|
@@ -12,12 +12,14 @@ import {
|
|
|
12
12
|
CoValueClass,
|
|
13
13
|
FileStream,
|
|
14
14
|
FileStreamSchema,
|
|
15
|
+
CoVectorSchema,
|
|
15
16
|
PlainTextSchema,
|
|
16
17
|
SchemaUnion,
|
|
17
18
|
enrichAccountSchema,
|
|
18
19
|
enrichCoMapSchema,
|
|
19
20
|
isCoValueClass,
|
|
20
21
|
Group,
|
|
22
|
+
CoVector,
|
|
21
23
|
} from "../../../internal.js";
|
|
22
24
|
import { coField } from "../../schema.js";
|
|
23
25
|
|
|
@@ -123,6 +125,17 @@ export function hydrateCoreCoValueSchema<S extends AnyCoreCoValueSchema>(
|
|
|
123
125
|
} else if (schema.builtin === "FileStream") {
|
|
124
126
|
const coValueClass = FileStream;
|
|
125
127
|
return new FileStreamSchema(coValueClass) as CoValueSchemaFromCoreSchema<S>;
|
|
128
|
+
} else if (schema.builtin === "CoVector") {
|
|
129
|
+
const dimensions = schema.dimensions;
|
|
130
|
+
|
|
131
|
+
const coValueClass = class CoVectorWithDimensions extends CoVector {
|
|
132
|
+
protected static requiredDimensionsCount = dimensions;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
return new CoVectorSchema(
|
|
136
|
+
dimensions,
|
|
137
|
+
coValueClass,
|
|
138
|
+
) as CoValueSchemaFromCoreSchema<S>;
|
|
126
139
|
} else if (schema.builtin === "CoPlainText") {
|
|
127
140
|
const coValueClass = CoPlainText;
|
|
128
141
|
return new PlainTextSchema(coValueClass) as CoValueSchemaFromCoreSchema<S>;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Account,
|
|
3
|
+
AnonymousJazzAgent,
|
|
4
|
+
CoVector,
|
|
5
|
+
Group,
|
|
6
|
+
InstanceOrPrimitiveOfSchema,
|
|
7
|
+
InstanceOrPrimitiveOfSchemaCoValuesNullable,
|
|
8
|
+
coOptionalDefiner,
|
|
9
|
+
} from "../../../internal.js";
|
|
10
|
+
import { CoOptionalSchema } from "./CoOptionalSchema.js";
|
|
11
|
+
import { CoreCoValueSchema } from "./CoValueSchema.js";
|
|
12
|
+
|
|
13
|
+
export interface CoreCoVectorSchema extends CoreCoValueSchema {
|
|
14
|
+
builtin: "CoVector";
|
|
15
|
+
dimensions: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function createCoreCoVectorSchema(
|
|
19
|
+
dimensions: number,
|
|
20
|
+
): CoreCoVectorSchema {
|
|
21
|
+
return {
|
|
22
|
+
collaborative: true as const,
|
|
23
|
+
builtin: "CoVector" as const,
|
|
24
|
+
dimensions,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class CoVectorSchema implements CoreCoVectorSchema {
|
|
29
|
+
readonly collaborative = true as const;
|
|
30
|
+
readonly builtin = "CoVector" as const;
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
public dimensions: number,
|
|
34
|
+
private coValueClass: typeof CoVector,
|
|
35
|
+
) {}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Create a `CoVector` from a given vector.
|
|
39
|
+
*/
|
|
40
|
+
create(
|
|
41
|
+
vector: number[] | Float32Array,
|
|
42
|
+
options?: { owner: Group } | Group,
|
|
43
|
+
): CoVectorInstance;
|
|
44
|
+
/**
|
|
45
|
+
* Create a `CoVector` from a given vector.
|
|
46
|
+
*
|
|
47
|
+
* @deprecated Creating CoValues with an Account as owner is deprecated. Use a Group instead.
|
|
48
|
+
*/
|
|
49
|
+
create(
|
|
50
|
+
vector: number[] | Float32Array,
|
|
51
|
+
options?: { owner: Account | Group } | Account | Group,
|
|
52
|
+
): CoVectorInstance;
|
|
53
|
+
create(
|
|
54
|
+
vector: number[] | Float32Array,
|
|
55
|
+
options?: { owner: Account | Group } | Account | Group,
|
|
56
|
+
): CoVectorInstance {
|
|
57
|
+
return this.coValueClass.create(vector, options);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Load a `CoVector` with a given ID.
|
|
62
|
+
*/
|
|
63
|
+
load(
|
|
64
|
+
id: string,
|
|
65
|
+
options?: { loadAs: Account | AnonymousJazzAgent },
|
|
66
|
+
): Promise<CoVectorInstanceNullable> {
|
|
67
|
+
return this.coValueClass.load(id, options);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Subscribe to a `CoVector`, when you have an ID but don't have a `CoVector` instance yet
|
|
72
|
+
*/
|
|
73
|
+
subscribe(
|
|
74
|
+
id: string,
|
|
75
|
+
options: { loadAs: Account | AnonymousJazzAgent },
|
|
76
|
+
listener: (
|
|
77
|
+
value: CoVectorInstanceNullable,
|
|
78
|
+
unsubscribe: () => void,
|
|
79
|
+
) => void,
|
|
80
|
+
): () => void;
|
|
81
|
+
subscribe(
|
|
82
|
+
id: string,
|
|
83
|
+
listener: (
|
|
84
|
+
value: CoVectorInstanceNullable,
|
|
85
|
+
unsubscribe: () => void,
|
|
86
|
+
) => void,
|
|
87
|
+
): () => void;
|
|
88
|
+
subscribe(...args: [any, ...any[]]) {
|
|
89
|
+
// @ts-expect-error
|
|
90
|
+
return this.coValueClass.subscribe(...args);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
getCoValueClass(): typeof CoVector {
|
|
94
|
+
return this.coValueClass;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
optional(): CoOptionalSchema<this> {
|
|
98
|
+
return coOptionalDefiner(this);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export type CoVectorInstance = InstanceOrPrimitiveOfSchema<CoVectorSchema>;
|
|
103
|
+
|
|
104
|
+
export type CoVectorInstanceNullable =
|
|
105
|
+
InstanceOrPrimitiveOfSchemaCoValuesNullable<CoVectorSchema>;
|
|
@@ -7,9 +7,11 @@ import {
|
|
|
7
7
|
CoMap,
|
|
8
8
|
CoPlainText,
|
|
9
9
|
CoRichText,
|
|
10
|
+
CoVector,
|
|
10
11
|
CoValueClass,
|
|
11
12
|
CoreAccountSchema,
|
|
12
13
|
CoreCoRecordSchema,
|
|
14
|
+
CoreCoVectorSchema,
|
|
13
15
|
FileStream,
|
|
14
16
|
Group,
|
|
15
17
|
} from "../../../internal.js";
|
|
@@ -63,11 +65,13 @@ export type InstanceOfSchema<S extends CoValueClass | AnyZodOrCoValueSchema> =
|
|
|
63
65
|
? CoRichText
|
|
64
66
|
: S extends CoreFileStreamSchema
|
|
65
67
|
? FileStream
|
|
66
|
-
: S extends
|
|
67
|
-
?
|
|
68
|
-
: S extends
|
|
69
|
-
? InstanceOrPrimitiveOfSchema<
|
|
70
|
-
:
|
|
68
|
+
: S extends CoreCoVectorSchema
|
|
69
|
+
? Readonly<CoVector>
|
|
70
|
+
: S extends CoreCoOptionalSchema<infer T>
|
|
71
|
+
? InstanceOrPrimitiveOfSchema<T> | undefined
|
|
72
|
+
: S extends CoDiscriminatedUnionSchema<infer Members>
|
|
73
|
+
? InstanceOrPrimitiveOfSchema<Members[number]>
|
|
74
|
+
: never
|
|
71
75
|
: S extends CoValueClass
|
|
72
76
|
? InstanceType<S>
|
|
73
77
|
: never;
|
package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts
CHANGED
|
@@ -13,6 +13,8 @@ import {
|
|
|
13
13
|
CoreCoListSchema,
|
|
14
14
|
CoreCoMapSchema,
|
|
15
15
|
CoreCoRecordSchema,
|
|
16
|
+
CoreCoVectorSchema,
|
|
17
|
+
CoVector,
|
|
16
18
|
FileStream,
|
|
17
19
|
Group,
|
|
18
20
|
} from "../../../internal.js";
|
|
@@ -70,15 +72,19 @@ export type InstanceOfSchemaCoValuesNullable<
|
|
|
70
72
|
? CoRichText | null
|
|
71
73
|
: S extends CoreFileStreamSchema
|
|
72
74
|
? FileStream | null
|
|
73
|
-
: S extends
|
|
74
|
-
?
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
75
|
+
: S extends CoreCoVectorSchema
|
|
76
|
+
? Readonly<CoVector> | null
|
|
77
|
+
: S extends CoreCoOptionalSchema<infer Inner>
|
|
78
|
+
?
|
|
79
|
+
| InstanceOrPrimitiveOfSchemaCoValuesNullable<Inner>
|
|
80
|
+
| undefined
|
|
81
|
+
: S extends CoreCoDiscriminatedUnionSchema<
|
|
82
|
+
infer Members
|
|
83
|
+
>
|
|
84
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
|
85
|
+
Members[number]
|
|
86
|
+
>
|
|
87
|
+
: never
|
|
82
88
|
: S extends CoValueClass
|
|
83
89
|
? InstanceType<S> | null
|
|
84
90
|
: never;
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
type CoRecordSchema,
|
|
10
10
|
type DefaultProfileShape,
|
|
11
11
|
type FileStreamSchema,
|
|
12
|
+
type CoVectorSchema,
|
|
12
13
|
ImageDefinition,
|
|
13
14
|
type PlainTextSchema,
|
|
14
15
|
type Simplify,
|
|
@@ -18,6 +19,7 @@ import {
|
|
|
18
19
|
createCoreCoMapSchema,
|
|
19
20
|
createCoreCoPlainTextSchema,
|
|
20
21
|
createCoreFileStreamSchema,
|
|
22
|
+
createCoreCoVectorSchema,
|
|
21
23
|
hydrateCoreCoValueSchema,
|
|
22
24
|
isAnyCoValueSchema,
|
|
23
25
|
isCoValueClass,
|
|
@@ -193,6 +195,19 @@ export const coFileStreamDefiner = (): FileStreamSchema => {
|
|
|
193
195
|
return hydrateCoreCoValueSchema(coreSchema);
|
|
194
196
|
};
|
|
195
197
|
|
|
198
|
+
export const coVectorDefiner = (dimensions: number): CoVectorSchema => {
|
|
199
|
+
const isPositiveInteger = Number.isInteger(dimensions) && dimensions > 0;
|
|
200
|
+
|
|
201
|
+
if (!isPositiveInteger) {
|
|
202
|
+
throw new Error(
|
|
203
|
+
"co.vector() expects the vector dimensions count to be a positive integer",
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const coreSchema = createCoreCoVectorSchema(dimensions);
|
|
208
|
+
return hydrateCoreCoValueSchema(coreSchema);
|
|
209
|
+
};
|
|
210
|
+
|
|
196
211
|
export const coPlainTextDefiner = (): PlainTextSchema => {
|
|
197
212
|
const coreSchema = createCoreCoPlainTextSchema();
|
|
198
213
|
return hydrateCoreCoValueSchema(coreSchema);
|
|
@@ -33,6 +33,10 @@ import {
|
|
|
33
33
|
CoreFileStreamSchema,
|
|
34
34
|
FileStreamSchema,
|
|
35
35
|
} from "./schemaTypes/FileStreamSchema.js";
|
|
36
|
+
import {
|
|
37
|
+
CoreCoVectorSchema,
|
|
38
|
+
CoVectorSchema,
|
|
39
|
+
} from "./schemaTypes/CoVectorSchema.js";
|
|
36
40
|
import {
|
|
37
41
|
CorePlainTextSchema,
|
|
38
42
|
PlainTextSchema,
|
|
@@ -75,11 +79,15 @@ export type CoValueSchemaFromCoreSchema<S extends CoreCoValueSchema> =
|
|
|
75
79
|
? RichTextSchema
|
|
76
80
|
: S extends CoreFileStreamSchema
|
|
77
81
|
? FileStreamSchema
|
|
78
|
-
: S extends
|
|
79
|
-
?
|
|
80
|
-
: S extends
|
|
81
|
-
?
|
|
82
|
-
:
|
|
82
|
+
: S extends CoreCoVectorSchema
|
|
83
|
+
? CoVectorSchema
|
|
84
|
+
: S extends CoreCoOptionalSchema<infer Inner>
|
|
85
|
+
? CoOptionalSchema<Inner>
|
|
86
|
+
: S extends CoreCoDiscriminatedUnionSchema<
|
|
87
|
+
infer Members
|
|
88
|
+
>
|
|
89
|
+
? CoDiscriminatedUnionSchema<Members>
|
|
90
|
+
: never;
|
|
83
91
|
|
|
84
92
|
export type CoValueClassFromAnySchema<S extends CoValueClassOrSchema> =
|
|
85
93
|
S extends CoValueClass<any>
|
|
@@ -104,7 +112,8 @@ export type AnyCoreCoValueSchema =
|
|
|
104
112
|
| CoreCoOptionalSchema
|
|
105
113
|
| CorePlainTextSchema
|
|
106
114
|
| CoreRichTextSchema
|
|
107
|
-
| CoreFileStreamSchema
|
|
115
|
+
| CoreFileStreamSchema
|
|
116
|
+
| CoreCoVectorSchema;
|
|
108
117
|
|
|
109
118
|
export type AnyZodSchema = z.core.$ZodType;
|
|
110
119
|
|
package/src/tools/internal.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from "./coValues/inbox.js";
|
|
|
16
16
|
export * from "./coValues/coPlainText.js";
|
|
17
17
|
export * from "./coValues/coRichText.js";
|
|
18
18
|
export * from "./coValues/schemaUnion.js";
|
|
19
|
+
export * from "./coValues/coVector.js";
|
|
19
20
|
|
|
20
21
|
export type * from "./subscribe/types.js";
|
|
21
22
|
|
|
@@ -43,6 +44,7 @@ export * from "./implementation/zodSchema/schemaTypes/CoListSchema.js";
|
|
|
43
44
|
export * from "./implementation/zodSchema/schemaTypes/CoFeedSchema.js";
|
|
44
45
|
export * from "./implementation/zodSchema/schemaTypes/AccountSchema.js";
|
|
45
46
|
export * from "./implementation/zodSchema/schemaTypes/FileStreamSchema.js";
|
|
47
|
+
export * from "./implementation/zodSchema/schemaTypes/CoVectorSchema.js";
|
|
46
48
|
export * from "./implementation/zodSchema/schemaTypes/PlainTextSchema.js";
|
|
47
49
|
export * from "./implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.js";
|
|
48
50
|
export * from "./implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { authenticateRequest, generateAuthToken } from "../coValues/request.js";
|
|
3
|
+
import { createJazzTestAccount } from "../testing.js";
|
|
4
|
+
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
vi.restoreAllMocks();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
describe("authenticateRequest", () => {
|
|
10
|
+
it("should correctly authenticate a request", async () => {
|
|
11
|
+
const me = await createJazzTestAccount({
|
|
12
|
+
isCurrentActiveAccount: true,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const token = generateAuthToken();
|
|
16
|
+
|
|
17
|
+
const { account, error } = await authenticateRequest(
|
|
18
|
+
new Request("https://api.example.com/api/user", {
|
|
19
|
+
headers: {
|
|
20
|
+
Authorization: `Jazz ${token}`,
|
|
21
|
+
},
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
expect(error).toBeUndefined();
|
|
26
|
+
expect(account?.$jazz.id).toBe(me.$jazz.id);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should not return an account if no token is provided", async () => {
|
|
30
|
+
await createJazzTestAccount({
|
|
31
|
+
isCurrentActiveAccount: true,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const { account, error } = await authenticateRequest(
|
|
35
|
+
new Request("https://api.example.com/api/user", {}),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
expect(error).toBeUndefined();
|
|
39
|
+
expect(account).toBeUndefined();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should return an error if the token is invalid", async () => {
|
|
43
|
+
const { account, error } = await authenticateRequest(
|
|
44
|
+
new Request("https://api.example.com/api/user", {
|
|
45
|
+
headers: {
|
|
46
|
+
Authorization: `Jazz invalid~invalid~invalid`,
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
expect(error).toMatchObject(
|
|
52
|
+
expect.objectContaining({
|
|
53
|
+
message: "Invalid token",
|
|
54
|
+
details: expect.anything(),
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
expect(account).toBeUndefined();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should return an error if the token is malformed", async () => {
|
|
61
|
+
const { account, error } = await authenticateRequest(
|
|
62
|
+
new Request("https://api.example.com/api/user", {
|
|
63
|
+
headers: {
|
|
64
|
+
Authorization: `Jazz malformed`,
|
|
65
|
+
},
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
expect(error).toMatchObject(
|
|
70
|
+
expect.objectContaining({
|
|
71
|
+
message: "Invalid token",
|
|
72
|
+
details: expect.anything(),
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
75
|
+
expect(account).toBeUndefined();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should be resilient to tampering", async () => {
|
|
79
|
+
await createJazzTestAccount({
|
|
80
|
+
isCurrentActiveAccount: true,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const token = generateAuthToken();
|
|
84
|
+
const tokenParts = token.split("~");
|
|
85
|
+
tokenParts[2] = "999999999999999";
|
|
86
|
+
const tamperedToken = tokenParts.join("~");
|
|
87
|
+
|
|
88
|
+
const { account, error } = await authenticateRequest(
|
|
89
|
+
new Request("https://api.example.com/api/user", {
|
|
90
|
+
headers: {
|
|
91
|
+
Authorization: `Jazz ${tamperedToken}`,
|
|
92
|
+
},
|
|
93
|
+
}),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
expect(error).toMatchObject(
|
|
97
|
+
expect.objectContaining({
|
|
98
|
+
message: "Invalid signature",
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
expect(account).toBeUndefined();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("should return an error if the token is expired", async () => {
|
|
105
|
+
await createJazzTestAccount({
|
|
106
|
+
isCurrentActiveAccount: true,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const token = generateAuthToken();
|
|
110
|
+
|
|
111
|
+
const { account, error } = await authenticateRequest(
|
|
112
|
+
new Request("https://api.example.com/api/user", {
|
|
113
|
+
headers: {
|
|
114
|
+
Authorization: `Jazz ${token}`,
|
|
115
|
+
},
|
|
116
|
+
}),
|
|
117
|
+
{
|
|
118
|
+
expiration: -1000,
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
expect(error).toMatchObject(
|
|
123
|
+
expect.objectContaining({
|
|
124
|
+
message: "Token expired",
|
|
125
|
+
}),
|
|
126
|
+
);
|
|
127
|
+
expect(account).toBeUndefined();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("should treat the request as unauthenticated if the token is not in the default format, even if present.", async () => {
|
|
131
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
132
|
+
await createJazzTestAccount({
|
|
133
|
+
isCurrentActiveAccount: true,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const token = generateAuthToken();
|
|
137
|
+
|
|
138
|
+
const { account, error } = await authenticateRequest(
|
|
139
|
+
new Request("https://api.example.com/api/user", {
|
|
140
|
+
headers: {
|
|
141
|
+
Authorization: `${token}`,
|
|
142
|
+
},
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
expect(console.warn).toHaveBeenCalled();
|
|
147
|
+
expect(account).toBeUndefined();
|
|
148
|
+
expect(error).toBeUndefined();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("should correctly validate a request when the token is in a non standard location", async () => {
|
|
152
|
+
const me = await createJazzTestAccount({
|
|
153
|
+
isCurrentActiveAccount: true,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const token = generateAuthToken();
|
|
157
|
+
|
|
158
|
+
const { account, error } = await authenticateRequest(
|
|
159
|
+
new Request("https://api.example.com/api/user", {
|
|
160
|
+
headers: {
|
|
161
|
+
["x-jazz-auth-token"]: `${token}`,
|
|
162
|
+
},
|
|
163
|
+
}),
|
|
164
|
+
{
|
|
165
|
+
getToken: (request) => request.headers.get("x-jazz-auth-token"),
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
expect(error).toBeUndefined();
|
|
170
|
+
expect(account?.$jazz.id).toBe(me.$jazz.id);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("should correctly validate a request when the token is generated from a non active account", async () => {
|
|
174
|
+
const notAnActiveAccount = await createJazzTestAccount({
|
|
175
|
+
isCurrentActiveAccount: false,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const token = generateAuthToken(notAnActiveAccount);
|
|
179
|
+
|
|
180
|
+
const { account, error } = await authenticateRequest(
|
|
181
|
+
new Request("https://api.example.com/api/user", {
|
|
182
|
+
headers: {
|
|
183
|
+
Authorization: `Jazz ${token}`,
|
|
184
|
+
},
|
|
185
|
+
}),
|
|
186
|
+
{
|
|
187
|
+
loadAs: notAnActiveAccount,
|
|
188
|
+
},
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
expect(error).toBeUndefined();
|
|
192
|
+
expect(account?.$jazz.id).toBe(notAnActiveAccount.$jazz.id);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expectTypeOf, test } from "vitest";
|
|
2
|
+
import { Group, co } from "../exports.js";
|
|
3
|
+
import { CoVectorSchema } from "../internal.js";
|
|
4
|
+
|
|
5
|
+
describe("CoVector types", () => {
|
|
6
|
+
test("co.vector() • defines a correct CoVectorSchema", () => {
|
|
7
|
+
type ExpectedType = CoVectorSchema;
|
|
8
|
+
|
|
9
|
+
function matches(value: ExpectedType) {
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
matches(co.vector(384));
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("co.vector().create() • creates a CoVector with Float32Array-like typing", () => {
|
|
17
|
+
const embedding = co.vector(3).create([1, 2, 3]);
|
|
18
|
+
|
|
19
|
+
type ExpectedType = Readonly<Float32Array>;
|
|
20
|
+
|
|
21
|
+
function matches(value: ExpectedType) {
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
matches(embedding);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("CoVector instance • has the owner property", () => {
|
|
29
|
+
const embedding = co.vector(3).create([1, 2, 3]);
|
|
30
|
+
|
|
31
|
+
expectTypeOf(embedding.$jazz.owner).toEqualTypeOf<Group>();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("setting a value via index access • is a type error", () => {
|
|
35
|
+
const embedding = co.vector(3).create([1, 2, 3]);
|
|
36
|
+
|
|
37
|
+
// @ts-expect-error: assignment to index should be disallowed
|
|
38
|
+
embedding[0] = 6;
|
|
39
|
+
});
|
|
40
|
+
});
|