jazz-tools 0.18.2 → 0.18.4
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 +43 -41
- package/CHANGELOG.md +20 -0
- package/dist/{chunk-IERUTUXB.js → chunk-LHQQZH7I.js} +121 -36
- package/dist/chunk-LHQQZH7I.js.map +1 -0
- package/dist/index.js +1 -1
- package/dist/media/{chunk-KR2V6X2N.js → chunk-W3S526L3.js} +96 -93
- package/dist/media/chunk-W3S526L3.js.map +1 -0
- package/dist/media/create-image/browser.d.ts +43 -0
- package/dist/media/create-image/browser.d.ts.map +1 -0
- package/dist/media/create-image/react-native.d.ts +37 -0
- package/dist/media/create-image/react-native.d.ts.map +1 -0
- package/dist/media/create-image/server.d.ts +34 -0
- package/dist/media/create-image/server.d.ts.map +1 -0
- package/dist/media/create-image/server.test.d.ts +2 -0
- package/dist/media/create-image/server.test.d.ts.map +1 -0
- package/dist/media/{create-image.d.ts → create-image-factory.d.ts} +8 -7
- package/dist/media/create-image-factory.d.ts.map +1 -0
- package/dist/media/create-image-factory.test.d.ts +2 -0
- package/dist/media/create-image-factory.test.d.ts.map +1 -0
- package/dist/media/exports.d.ts +3 -0
- package/dist/media/exports.d.ts.map +1 -0
- package/dist/media/index.browser.d.ts +2 -14
- package/dist/media/index.browser.d.ts.map +1 -1
- package/dist/media/index.browser.js +11 -20
- package/dist/media/index.browser.js.map +1 -1
- package/dist/media/index.d.ts +12 -4
- package/dist/media/index.d.ts.map +1 -1
- package/dist/media/index.js +1 -1
- package/dist/media/index.native.d.ts +2 -16
- package/dist/media/index.native.d.ts.map +1 -1
- package/dist/media/index.native.js +23 -42
- package/dist/media/index.native.js.map +1 -1
- package/dist/media/index.server.d.ts +3 -0
- package/dist/media/index.server.d.ts.map +1 -0
- package/dist/media/index.server.js +103 -0
- package/dist/media/index.server.js.map +1 -0
- package/dist/react/index.js +7 -7
- package/dist/react/index.js.map +1 -1
- package/dist/react-core/index.js +120 -35
- package/dist/react-core/index.js.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/tools/coValues/account.d.ts.map +1 -1
- package/dist/tools/coValues/coFeed.d.ts +12 -0
- package/dist/tools/coValues/coFeed.d.ts.map +1 -1
- package/dist/tools/coValues/coMap.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +19 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
- package/dist/tools/subscribe/CoValueCoreSubscription.d.ts +61 -11
- package/dist/tools/subscribe/CoValueCoreSubscription.d.ts.map +1 -1
- package/dist/tools/subscribe/CoValueCoreSubscription.test.d.ts +2 -0
- package/dist/tools/subscribe/CoValueCoreSubscription.test.d.ts.map +1 -0
- package/dist/tools/testing.d.ts.map +1 -1
- package/package.json +27 -11
- package/src/media/create-image/browser.ts +161 -0
- package/src/media/create-image/react-native.ts +158 -0
- package/src/media/create-image/server.test.ts +74 -0
- package/src/media/create-image/server.ts +181 -0
- package/src/media/{create-image.test.ts → create-image-factory.test.ts} +1 -1
- package/src/media/{create-image.ts → create-image-factory.ts} +22 -12
- package/src/media/exports.ts +2 -0
- package/src/media/index.browser.ts +2 -150
- package/src/media/index.native.ts +2 -166
- package/src/media/index.server.ts +2 -0
- package/src/media/index.ts +16 -8
- package/src/tools/coValues/account.ts +3 -1
- package/src/tools/coValues/coFeed.ts +5 -0
- package/src/tools/coValues/coMap.ts +3 -1
- package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +19 -0
- package/src/tools/subscribe/CoValueCoreSubscription.test.ts +1000 -0
- package/src/tools/subscribe/CoValueCoreSubscription.ts +179 -43
- package/src/tools/tests/account.test.ts +12 -0
- package/src/tools/tests/coFeed.test.ts +25 -0
- package/src/tools/tests/coList.test.ts +20 -0
- package/src/tools/tests/coMap.record.test.ts +1 -0
- package/src/tools/tests/coMap.test.ts +12 -2
- package/tsup.config.ts +1 -0
- package/dist/chunk-IERUTUXB.js.map +0 -1
- package/dist/media/chunk-KR2V6X2N.js.map +0 -1
- package/dist/media/create-image.d.ts.map +0 -1
- package/dist/media/create-image.test.d.ts +0 -2
- package/dist/media/create-image.test.d.ts.map +0 -1
@@ -1,166 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
import { FileStream } from "jazz-tools";
|
4
|
-
import { Image } from "react-native";
|
5
|
-
import {
|
6
|
-
CreateImageOptions,
|
7
|
-
SourceType,
|
8
|
-
createImageFactory,
|
9
|
-
} from "./create-image.js";
|
10
|
-
|
11
|
-
export { highestResAvailable, loadImage, loadImageBySize } from "./utils.js";
|
12
|
-
export { createImageFactory };
|
13
|
-
|
14
|
-
let ImageResizer: typeof ImageResizerType | undefined;
|
15
|
-
|
16
|
-
export async function createImage(
|
17
|
-
imageBlobOrFile: Blob | File | string,
|
18
|
-
options?: CreateImageOptions,
|
19
|
-
) {
|
20
|
-
if (!ImageResizer) {
|
21
|
-
try {
|
22
|
-
ImageResizer = (await import("@bam.tech/react-native-image-resizer"))
|
23
|
-
.default;
|
24
|
-
} catch (e) {
|
25
|
-
throw new Error(
|
26
|
-
"ImageResizer is not installed, please run `npm install @bam.tech/react-native-image-resizer`",
|
27
|
-
);
|
28
|
-
}
|
29
|
-
}
|
30
|
-
|
31
|
-
return createImageFactory({
|
32
|
-
getImageSize,
|
33
|
-
getPlaceholderBase64,
|
34
|
-
createFileStreamFromSource,
|
35
|
-
resize,
|
36
|
-
})(imageBlobOrFile, options || {});
|
37
|
-
}
|
38
|
-
|
39
|
-
async function getImageSize(
|
40
|
-
filePath: SourceType,
|
41
|
-
): Promise<{ width: number; height: number }> {
|
42
|
-
if (typeof filePath !== "string") {
|
43
|
-
throw new Error(
|
44
|
-
"createImage(Blob | File) is not supported on this platform",
|
45
|
-
);
|
46
|
-
}
|
47
|
-
|
48
|
-
const { width, height } = await Image.getSize(filePath);
|
49
|
-
|
50
|
-
return { width, height };
|
51
|
-
}
|
52
|
-
|
53
|
-
async function getPlaceholderBase64(filePath: SourceType): Promise<string> {
|
54
|
-
if (typeof filePath !== "string") {
|
55
|
-
throw new Error(
|
56
|
-
"createImage(Blob | File) is not supported on this platform",
|
57
|
-
);
|
58
|
-
}
|
59
|
-
|
60
|
-
if (!ImageResizer) {
|
61
|
-
throw new Error(
|
62
|
-
"ImageResizer is not installed, please run `npm install @bam.tech/react-native-image-resizer`",
|
63
|
-
);
|
64
|
-
}
|
65
|
-
|
66
|
-
const { uri } = await ImageResizer.createResizedImage(
|
67
|
-
filePath,
|
68
|
-
8,
|
69
|
-
8,
|
70
|
-
"PNG",
|
71
|
-
100,
|
72
|
-
);
|
73
|
-
|
74
|
-
return imageUrlToBase64(uri);
|
75
|
-
}
|
76
|
-
|
77
|
-
async function resize(
|
78
|
-
filePath: SourceType,
|
79
|
-
width: number,
|
80
|
-
height: number,
|
81
|
-
): Promise<string> {
|
82
|
-
if (typeof filePath !== "string") {
|
83
|
-
throw new Error(
|
84
|
-
"createImage(Blob | File) is not supported on this platform",
|
85
|
-
);
|
86
|
-
}
|
87
|
-
|
88
|
-
if (!ImageResizer) {
|
89
|
-
throw new Error(
|
90
|
-
"ImageResizer is not installed, please run `npm install @bam.tech/react-native-image-resizer`",
|
91
|
-
);
|
92
|
-
}
|
93
|
-
|
94
|
-
const mimeType = await getMimeType(filePath);
|
95
|
-
|
96
|
-
const { uri } = await ImageResizer.createResizedImage(
|
97
|
-
filePath,
|
98
|
-
width,
|
99
|
-
height,
|
100
|
-
contentTypeToFormat(mimeType),
|
101
|
-
80,
|
102
|
-
);
|
103
|
-
|
104
|
-
return uri;
|
105
|
-
}
|
106
|
-
|
107
|
-
function getMimeType(filePath: string): Promise<string> {
|
108
|
-
return fetch(filePath)
|
109
|
-
.then((res) => res.blob())
|
110
|
-
.then((blob) => blob.type);
|
111
|
-
}
|
112
|
-
|
113
|
-
function contentTypeToFormat(contentType: string) {
|
114
|
-
if (contentType.includes("image/png")) return "PNG";
|
115
|
-
if (contentType.includes("image/jpeg")) return "JPEG";
|
116
|
-
if (contentType.includes("image/webp")) return "WEBP";
|
117
|
-
return "PNG";
|
118
|
-
}
|
119
|
-
|
120
|
-
export async function createFileStreamFromSource(
|
121
|
-
filePath: SourceType,
|
122
|
-
owner?: Account | Group,
|
123
|
-
): Promise<FileStream> {
|
124
|
-
if (typeof filePath !== "string") {
|
125
|
-
throw new Error(
|
126
|
-
"createImage(Blob | File) is not supported on this platform",
|
127
|
-
);
|
128
|
-
}
|
129
|
-
|
130
|
-
const blob = await fetch(filePath).then((res) => res.blob());
|
131
|
-
const arrayBuffer = await toArrayBuffer(blob);
|
132
|
-
|
133
|
-
return FileStream.createFromArrayBuffer(arrayBuffer, blob.type, undefined, {
|
134
|
-
owner,
|
135
|
-
});
|
136
|
-
}
|
137
|
-
|
138
|
-
// TODO: look for more efficient way to do this as React Native hasn't blob.arrayBuffer()
|
139
|
-
function toArrayBuffer(blob: Blob): Promise<ArrayBuffer> {
|
140
|
-
return new Promise((resolve, reject) => {
|
141
|
-
const reader = new FileReader();
|
142
|
-
reader.onloadend = () => {
|
143
|
-
resolve(reader.result as ArrayBuffer);
|
144
|
-
};
|
145
|
-
reader.onerror = (error) => {
|
146
|
-
reject(error);
|
147
|
-
};
|
148
|
-
reader.readAsArrayBuffer(blob);
|
149
|
-
});
|
150
|
-
}
|
151
|
-
|
152
|
-
async function imageUrlToBase64(url: string): Promise<string> {
|
153
|
-
const response = await fetch(url);
|
154
|
-
const blob = await response.blob();
|
155
|
-
return new Promise((onSuccess, onError) => {
|
156
|
-
try {
|
157
|
-
const reader = new FileReader();
|
158
|
-
reader.onload = function () {
|
159
|
-
onSuccess(reader.result as string);
|
160
|
-
};
|
161
|
-
reader.readAsDataURL(blob);
|
162
|
-
} catch (e) {
|
163
|
-
onError(e);
|
164
|
-
}
|
165
|
-
});
|
166
|
-
}
|
1
|
+
export * from "./exports";
|
2
|
+
export { createImage } from "./create-image/react-native";
|
package/src/media/index.ts
CHANGED
@@ -1,12 +1,7 @@
|
|
1
1
|
import type { ImageDefinition } from "jazz-tools";
|
2
|
-
import {
|
3
|
-
CreateImageOptions,
|
4
|
-
SourceType,
|
5
|
-
createImageFactory,
|
6
|
-
} from "./create-image.js";
|
2
|
+
import { CreateImageOptions } from "./create-image-factory";
|
7
3
|
|
8
|
-
export
|
9
|
-
export { createImageFactory };
|
4
|
+
export * from "./exports";
|
10
5
|
|
11
6
|
/**
|
12
7
|
* Creates an ImageDefinition from an image file or blob with built-in UX features.
|
@@ -38,7 +33,20 @@ export { createImageFactory };
|
|
38
33
|
* }
|
39
34
|
* }
|
40
35
|
* ```
|
36
|
+
* ```
|
37
|
+
*/
|
38
|
+
export declare function createImage(
|
39
|
+
imageBlobOrFile: Blob | File,
|
40
|
+
options?: CreateImageOptions,
|
41
|
+
): Promise<ImageDefinition>;
|
42
|
+
|
43
|
+
/**
|
44
|
+
* Creates an ImageDefinition from an image file path with built-in UX features.
|
41
45
|
*
|
46
|
+
* This function creates a specialized CoValue for managing images in Jazz applications.
|
47
|
+
* It supports blurry placeholders, built-in resizing, and progressive loading patterns.
|
48
|
+
*
|
49
|
+
* @returns Promise that resolves to an ImageDefinition
|
42
50
|
* @example
|
43
51
|
* ```ts
|
44
52
|
* // React Native example
|
@@ -56,6 +64,6 @@ export { createImageFactory };
|
|
56
64
|
* ```
|
57
65
|
*/
|
58
66
|
export declare function createImage(
|
59
|
-
|
67
|
+
filePath: string,
|
60
68
|
options?: CreateImageOptions,
|
61
69
|
): Promise<ImageDefinition>;
|
@@ -274,7 +274,9 @@ export class Account extends CoValueBase implements CoValue {
|
|
274
274
|
|
275
275
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
276
276
|
toJSON(): object | any[] {
|
277
|
-
return {
|
277
|
+
return {
|
278
|
+
$jazz: { id: this.$jazz.id },
|
279
|
+
};
|
278
280
|
}
|
279
281
|
|
280
282
|
[inspect]() {
|
@@ -228,6 +228,7 @@ export class CoFeed<out Item = any> extends CoValueBase implements CoValue {
|
|
228
228
|
* @category
|
229
229
|
*/
|
230
230
|
toJSON(): {
|
231
|
+
$jazz: { id: string };
|
231
232
|
[key: string]: unknown;
|
232
233
|
in: { [key: string]: unknown };
|
233
234
|
} {
|
@@ -240,6 +241,7 @@ export class CoFeed<out Item = any> extends CoValueBase implements CoValue {
|
|
240
241
|
: (v: unknown) => v && (v as CoValue).$jazz.id;
|
241
242
|
|
242
243
|
return {
|
244
|
+
$jazz: { id: this.$jazz.id },
|
243
245
|
...Object.fromEntries(
|
244
246
|
Object.entries(this).map(([account, entry]) => [
|
245
247
|
account,
|
@@ -257,6 +259,7 @@ export class CoFeed<out Item = any> extends CoValueBase implements CoValue {
|
|
257
259
|
|
258
260
|
/** @internal */
|
259
261
|
[inspect](): {
|
262
|
+
$jazz: { id: string };
|
260
263
|
[key: string]: unknown;
|
261
264
|
in: { [key: string]: unknown };
|
262
265
|
} {
|
@@ -928,6 +931,7 @@ export class FileStream extends CoValueBase implements CoValue {
|
|
928
931
|
* @category Content
|
929
932
|
*/
|
930
933
|
toJSON(): {
|
934
|
+
$jazz: { id: string };
|
931
935
|
mimeType?: string;
|
932
936
|
totalSizeBytes?: number;
|
933
937
|
fileName?: string;
|
@@ -935,6 +939,7 @@ export class FileStream extends CoValueBase implements CoValue {
|
|
935
939
|
finished?: boolean;
|
936
940
|
} {
|
937
941
|
return {
|
942
|
+
$jazz: { id: this.$jazz.id },
|
938
943
|
...this.getChunks(),
|
939
944
|
};
|
940
945
|
}
|
@@ -185,7 +185,9 @@ export class CoMap extends CoValueBase implements CoValue {
|
|
185
185
|
*/
|
186
186
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
187
187
|
toJSON(_key?: string, processedValues?: ID<CoValue>[]): any {
|
188
|
-
const result = {
|
188
|
+
const result = {
|
189
|
+
$jazz: { id: this.$jazz.id },
|
190
|
+
} as Record<string, any>;
|
189
191
|
|
190
192
|
for (const key of this.$jazz.raw.keys()) {
|
191
193
|
const tKey = key as CoKeys<this>;
|
@@ -131,6 +131,25 @@ export interface CoMapSchema<
|
|
131
131
|
R
|
132
132
|
> | null>;
|
133
133
|
|
134
|
+
/**
|
135
|
+
* @deprecated Use `co.map().catchall` will be removed in an upcoming version.
|
136
|
+
*
|
137
|
+
* Use a `co.record` nested inside a `co.map` if you need to store key-value properties.
|
138
|
+
*
|
139
|
+
* @example
|
140
|
+
* ```ts
|
141
|
+
* // Instead of:
|
142
|
+
* const Image = co.map({
|
143
|
+
* original: co.fileStream(),
|
144
|
+
* }).catchall(co.fileStream());
|
145
|
+
*
|
146
|
+
* // Use:
|
147
|
+
* const Image = co.map({
|
148
|
+
* original: co.fileStream(),
|
149
|
+
* resolutions: co.record(z.string(), co.fileStream()),
|
150
|
+
* });
|
151
|
+
* ```
|
152
|
+
*/
|
134
153
|
catchall<T extends AnyZodOrCoValueSchema>(schema: T): CoMapSchema<Shape, T>;
|
135
154
|
|
136
155
|
withMigration(
|