@joycostudio/chopper 0.1.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.
Files changed (41) hide show
  1. package/README.md +37 -0
  2. package/THIRD_PARTY_NOTICES.md +19 -0
  3. package/codecs/avif_enc.wasm +0 -0
  4. package/codecs/basis_encoder.wasm +0 -0
  5. package/codecs/draco_decoder_gltf.js +116 -0
  6. package/codecs/draco_decoder_gltf.wasm +0 -0
  7. package/codecs/draco_encoder.wasm +0 -0
  8. package/codecs/draco_encoder_gltf.js +98 -0
  9. package/codecs/manifest.json +86 -0
  10. package/codecs/mozjpeg_dec.wasm +0 -0
  11. package/codecs/mozjpeg_enc.wasm +0 -0
  12. package/codecs/squoosh_png_bg.wasm +0 -0
  13. package/codecs/squoosh_resize_bg.wasm +0 -0
  14. package/codecs/webp_enc.wasm +0 -0
  15. package/dist/browser/chunk-5WRI5ZAA.js +29 -0
  16. package/dist/browser/chunk-5WRI5ZAA.js.map +1 -0
  17. package/dist/browser/chunk-OTTLKWP4.js +105 -0
  18. package/dist/browser/chunk-OTTLKWP4.js.map +1 -0
  19. package/dist/browser/in-process-V4NPIVZF.js +26747 -0
  20. package/dist/browser/in-process-V4NPIVZF.js.map +1 -0
  21. package/dist/browser/index.d.ts +244 -0
  22. package/dist/browser/index.js +86 -0
  23. package/dist/browser/index.js.map +1 -0
  24. package/dist/browser/node-shim-3YE6NHES.js +16 -0
  25. package/dist/browser/node-shim-3YE6NHES.js.map +1 -0
  26. package/dist/browser/worker.d.ts +2 -0
  27. package/dist/browser/worker.js +27425 -0
  28. package/dist/browser/worker.js.map +1 -0
  29. package/dist/cli/index.d.ts +2 -0
  30. package/dist/cli/index.js +1421 -0
  31. package/dist/cli/index.js.map +1 -0
  32. package/dist/config/index.d.ts +123 -0
  33. package/dist/config/index.js +9 -0
  34. package/dist/config/index.js.map +1 -0
  35. package/dist/index.d.ts +367 -0
  36. package/dist/index.js +396 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/node/index.d.ts +346 -0
  39. package/dist/node/index.js +1266 -0
  40. package/dist/node/index.js.map +1 -0
  41. package/package.json +64 -0
@@ -0,0 +1,346 @@
1
+ declare const RECIPE_SCHEMA_VERSION: 1;
2
+ type TextureSize = readonly [width: number, height: number];
3
+ interface SizedEncoding {
4
+ /** Maximum dimensions. Aspect ratio is preserved and images are not enlarged. */
5
+ maxSize?: TextureSize;
6
+ }
7
+ interface WebpEncoding extends SizedEncoding {
8
+ codec: 'webp';
9
+ quality?: number;
10
+ effort?: number;
11
+ lossless?: boolean;
12
+ /** WebP near-lossless preprocessing strength, from 0 (lossy) to 100. */
13
+ nearLossless?: number;
14
+ }
15
+ interface AvifEncoding extends SizedEncoding {
16
+ codec: 'avif';
17
+ quality?: number;
18
+ effort?: number;
19
+ lossless?: boolean;
20
+ chromaSubsampling?: '4:2:0' | '4:4:4';
21
+ }
22
+ interface JpegEncoding extends SizedEncoding {
23
+ codec: 'jpeg';
24
+ quality?: number;
25
+ chromaSubsampling?: '4:2:0' | '4:4:4';
26
+ }
27
+ interface PngEncoding extends SizedEncoding {
28
+ codec: 'png';
29
+ }
30
+ interface UastcEncoding extends SizedEncoding {
31
+ codec: 'ktx2';
32
+ mode: 'uastc';
33
+ colorSpace?: 'srgb' | 'linear';
34
+ normalMap?: boolean;
35
+ level?: 0 | 1 | 2 | 3 | 4;
36
+ mipmaps?: boolean;
37
+ zstd?: boolean;
38
+ rdo?: {
39
+ lambda?: number;
40
+ };
41
+ }
42
+ interface Etc1sEncoding extends SizedEncoding {
43
+ codec: 'ktx2';
44
+ mode: 'etc1s';
45
+ colorSpace?: 'srgb' | 'linear';
46
+ normalMap?: boolean;
47
+ quality?: number;
48
+ compression?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
49
+ mipmaps?: boolean;
50
+ }
51
+ type RasterEncoding = WebpEncoding | AvifEncoding | JpegEncoding | PngEncoding;
52
+ type ImageEncoding = RasterEncoding | UastcEncoding | Etc1sEncoding;
53
+ type GlbProperty = 'accessor' | 'animation' | 'buffer' | 'camera' | 'material' | 'mesh' | 'node' | 'primitive' | 'primitiveTarget' | 'skin' | 'texture';
54
+ type GlbMethodRecipe = {
55
+ method: 'dedup';
56
+ keepUniqueNames?: boolean;
57
+ propertyTypes?: readonly Extract<GlbProperty, 'accessor' | 'material' | 'mesh' | 'skin' | 'texture'>[];
58
+ } | {
59
+ method: 'prune';
60
+ keepAttributes?: boolean;
61
+ keepExtras?: boolean;
62
+ keepLeaves?: boolean;
63
+ keepSolidTextures?: boolean;
64
+ propertyTypes?: readonly GlbProperty[];
65
+ } | {
66
+ method: 'resample';
67
+ tolerance?: number;
68
+ cleanup?: boolean;
69
+ };
70
+ type TextureSelector = {
71
+ by: 'slot';
72
+ slots: readonly string[];
73
+ } | {
74
+ by: 'name';
75
+ names: readonly string[];
76
+ } | {
77
+ by: 'index';
78
+ indices: readonly number[];
79
+ };
80
+ interface EmbeddedTextureRecipe {
81
+ label: string;
82
+ select: TextureSelector;
83
+ encode: ImageEncoding;
84
+ }
85
+ type GeometryEncoding = {
86
+ codec: 'draco';
87
+ } | {
88
+ codec: 'meshopt';
89
+ };
90
+ interface GlbAssetRecipe {
91
+ schemaVersion: typeof RECIPE_SCHEMA_VERSION;
92
+ kind: 'glb';
93
+ methods?: readonly GlbMethodRecipe[];
94
+ textures?: readonly EmbeddedTextureRecipe[];
95
+ geometry?: GeometryEncoding | false;
96
+ }
97
+ interface ImageAssetRecipe {
98
+ schemaVersion: typeof RECIPE_SCHEMA_VERSION;
99
+ kind: 'image';
100
+ encode: ImageEncoding;
101
+ }
102
+ type AssetRecipe = GlbAssetRecipe | ImageAssetRecipe;
103
+ type NormalizedImageEncoding = (Required<Omit<WebpEncoding, 'maxSize'>> & Pick<WebpEncoding, 'maxSize'>) | (Required<Omit<AvifEncoding, 'maxSize'>> & Pick<AvifEncoding, 'maxSize'>) | (Required<Omit<JpegEncoding, 'maxSize'>> & Pick<JpegEncoding, 'maxSize'>) | PngEncoding | (Required<Omit<UastcEncoding, 'maxSize' | 'rdo'>> & Pick<UastcEncoding, 'maxSize' | 'rdo'>) | (Required<Omit<Etc1sEncoding, 'maxSize'>> & Pick<Etc1sEncoding, 'maxSize'>);
104
+ type NormalizedAssetRecipe = {
105
+ schemaVersion: typeof RECIPE_SCHEMA_VERSION;
106
+ kind: 'image';
107
+ encode: NormalizedImageEncoding;
108
+ } | {
109
+ schemaVersion: typeof RECIPE_SCHEMA_VERSION;
110
+ kind: 'glb';
111
+ methods: readonly GlbMethodRecipe[];
112
+ textures: readonly (Omit<EmbeddedTextureRecipe, 'encode'> & {
113
+ encode: NormalizedImageEncoding;
114
+ })[];
115
+ geometry: GeometryEncoding | false;
116
+ };
117
+
118
+ type ChopperErrorCode = 'unsupported_asset' | 'invalid_recipe' | 'codec_load_failed' | 'decode_failed' | 'encode_failed' | 'validation_failed' | 'aborted' | 'io_failed';
119
+ interface ChopperFailure {
120
+ ok: false;
121
+ error: {
122
+ code: ChopperErrorCode;
123
+ message: string;
124
+ cause?: unknown;
125
+ };
126
+ }
127
+ interface ChopperSuccess<T> {
128
+ ok: true;
129
+ data: T;
130
+ }
131
+ type ChopperResult<T> = ChopperSuccess<T> | ChopperFailure;
132
+
133
+ type SourceImageMimeType = 'image/jpeg' | 'image/png';
134
+ interface SourceImage {
135
+ bytes: Uint8Array;
136
+ mimeType: SourceImageMimeType;
137
+ }
138
+ interface EncodedImage {
139
+ bytes: Uint8Array;
140
+ mimeType: string;
141
+ width: number;
142
+ height: number;
143
+ }
144
+ interface ImageDimensions {
145
+ width: number;
146
+ height: number;
147
+ }
148
+ interface ImageCompressionEngine {
149
+ readonly id: string;
150
+ readonly manifestHash: string;
151
+ inspect(source: SourceImage): Promise<ChopperResult<ImageDimensions>>;
152
+ encode(source: SourceImage, encoding: ImageEncoding, signal?: AbortSignal): Promise<ChopperResult<EncodedImage>>;
153
+ }
154
+
155
+ interface NodeImageEngineOptions {
156
+ codecDirectory?: string | URL;
157
+ }
158
+ declare function createNodeImageEngine(options?: NodeImageEngineOptions): ImageCompressionEngine;
159
+
160
+ interface AssetInput {
161
+ bytes: Uint8Array;
162
+ fileName: string;
163
+ mimeType?: string;
164
+ }
165
+ interface ImageInspection {
166
+ kind: 'image';
167
+ mimeType: 'image/png' | 'image/jpeg';
168
+ width: number;
169
+ height: number;
170
+ byteLength: number;
171
+ }
172
+ interface GlbTextureInspection {
173
+ index: number;
174
+ name: string;
175
+ uri: string;
176
+ mimeType: string | null;
177
+ size: readonly [number, number] | null;
178
+ slots: readonly string[];
179
+ bytes: Uint8Array;
180
+ authoringSelector: TextureSelector;
181
+ }
182
+ interface GlbInspection {
183
+ kind: 'glb';
184
+ byteLength: number;
185
+ textures: GlbTextureInspection[];
186
+ geometry: {
187
+ compression: 'draco' | 'meshopt' | false;
188
+ meshCount: number;
189
+ primitiveCount: number;
190
+ };
191
+ }
192
+ type AssetInspection = ImageInspection | GlbInspection;
193
+ type OptimizationProgress = {
194
+ phase: 'inspect';
195
+ } | {
196
+ phase: 'decode';
197
+ } | {
198
+ phase: 'transform';
199
+ method: string;
200
+ index: number;
201
+ total: number;
202
+ } | {
203
+ phase: 'texture';
204
+ label: string;
205
+ index: number;
206
+ total: number;
207
+ } | {
208
+ phase: 'geometry';
209
+ codec: 'draco' | 'meshopt';
210
+ } | {
211
+ phase: 'validate';
212
+ } | {
213
+ phase: 'complete';
214
+ };
215
+ interface OptimizationOptions {
216
+ signal?: AbortSignal;
217
+ onProgress?: (event: OptimizationProgress) => void;
218
+ }
219
+ interface TextureOptimizationReport {
220
+ label: string;
221
+ sourceIndex: number;
222
+ outputIndex: number;
223
+ beforeBytes: number;
224
+ afterBytes: number;
225
+ beforeMimeType: string;
226
+ afterMimeType: string;
227
+ }
228
+ interface OptimizationReport {
229
+ engineId: string;
230
+ manifestHash: string;
231
+ recipe: NormalizedAssetRecipe;
232
+ beforeBytes: number;
233
+ afterBytes: number;
234
+ durationMs: number;
235
+ textures: TextureOptimizationReport[];
236
+ warnings: string[];
237
+ validation: {
238
+ valid: boolean;
239
+ messages: string[];
240
+ };
241
+ }
242
+ interface OptimizationOutput {
243
+ bytes: Uint8Array;
244
+ fileName: string;
245
+ mimeType: string;
246
+ inspection: AssetInspection;
247
+ report: OptimizationReport;
248
+ }
249
+ interface Chopper {
250
+ readonly engine: {
251
+ id: string;
252
+ version: string;
253
+ manifestHash: string;
254
+ };
255
+ inspect(input: AssetInput): Promise<ChopperResult<AssetInspection>>;
256
+ optimize(input: AssetInput, recipe: AssetRecipe, options?: OptimizationOptions): Promise<ChopperResult<OptimizationOutput>>;
257
+ }
258
+
259
+ interface OptimizedGlb {
260
+ bytes: Uint8Array;
261
+ inspection: GlbInspection;
262
+ textures: TextureOptimizationReport[];
263
+ }
264
+ interface GlbCompressionEngine {
265
+ readonly id: string;
266
+ inspect(source: Uint8Array): Promise<ChopperResult<GlbInspection>>;
267
+ optimize(source: Uint8Array, recipe: GlbAssetRecipe, options?: OptimizationOptions): Promise<ChopperResult<OptimizedGlb>>;
268
+ }
269
+
270
+ declare function createNodeGlbEngine(imageEngine?: ImageCompressionEngine): Promise<GlbCompressionEngine>;
271
+
272
+ declare function createNodeChopper(options?: NodeImageEngineOptions): Chopper;
273
+
274
+ interface OptimizeFileOptions extends OptimizationOptions {
275
+ input: string;
276
+ output: string;
277
+ recipe: AssetRecipe;
278
+ }
279
+ declare function optimizeFile({ input, output, recipe, ...options }: OptimizeFileOptions): Promise<ChopperResult<OptimizationOutput>>;
280
+
281
+ type ChopperOutput = {
282
+ mode: 'replace';
283
+ } | {
284
+ mode: 'template';
285
+ path: string;
286
+ };
287
+ interface ChopperRule {
288
+ include: readonly string[];
289
+ exclude?: readonly string[];
290
+ recipe: AssetRecipe;
291
+ output: ChopperOutput;
292
+ }
293
+ interface ChopperConfig {
294
+ rules: readonly ChopperRule[];
295
+ concurrency?: number;
296
+ }
297
+
298
+ declare function loadConfig(path?: string): Promise<ChopperResult<ChopperConfig>>;
299
+
300
+ type BatchMode = 'write' | 'check' | 'report';
301
+ interface BatchFileReport {
302
+ input: string;
303
+ output: string;
304
+ status: 'written' | 'current' | 'stale' | 'reported' | 'failed';
305
+ beforeBytes?: number;
306
+ afterBytes?: number;
307
+ engineId?: string;
308
+ error?: {
309
+ code: string;
310
+ message: string;
311
+ };
312
+ }
313
+ interface BatchReport {
314
+ mode: BatchMode;
315
+ files: BatchFileReport[];
316
+ totals: {
317
+ matched: number;
318
+ written: number;
319
+ current: number;
320
+ stale: number;
321
+ reported: number;
322
+ failed: number;
323
+ };
324
+ }
325
+ interface RunConfigOptions {
326
+ config?: ChopperConfig;
327
+ configPath?: string;
328
+ cwd?: string;
329
+ mode?: BatchMode;
330
+ changedPaths?: readonly string[];
331
+ chopper?: Chopper;
332
+ }
333
+ interface PlannedAsset {
334
+ input: string;
335
+ output: string;
336
+ rule: ChopperRule;
337
+ }
338
+ declare function runConfig(options?: RunConfigOptions): Promise<ChopperResult<BatchReport>>;
339
+ declare function planAssets(config: ChopperConfig, cwd: string, changedPaths?: readonly string[]): Promise<ChopperResult<PlannedAsset[]>>;
340
+
341
+ declare function syncCodecs(output: string): Promise<ChopperResult<{
342
+ directory: string;
343
+ files: string[];
344
+ }>>;
345
+
346
+ export { type BatchFileReport, type BatchMode, type BatchReport, type NodeImageEngineOptions, type OptimizeFileOptions, type RunConfigOptions, createNodeChopper, createNodeGlbEngine, createNodeImageEngine, loadConfig, optimizeFile, planAssets, runConfig, syncCodecs };