@joycostudio/chopper 0.5.1 → 0.5.3

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.
@@ -1,50 +1,123 @@
1
+ /** Maximum image dimensions in pixels: `[width, height]`. Both values must be positive integers. */
1
2
  type TextureSize = readonly [width: number, height: number];
2
3
  interface SizedEncoding {
3
- /** Maximum dimensions. Aspect ratio is preserved and images are not enlarged. */
4
+ /**
5
+ * Maximum output dimensions in pixels. Aspect ratio is preserved, and an
6
+ * image smaller than both limits is not enlarged. Omit this property to keep
7
+ * the source dimensions.
8
+ */
4
9
  maxSize?: TextureSize;
5
10
  }
6
11
  interface WebpEncoding extends SizedEncoding {
12
+ /** Encode the image as WebP. */
7
13
  codec: 'webp';
14
+ /** Lossy quality from 1 (smallest, lowest fidelity) to 100. @defaultValue `75` */
8
15
  quality?: number;
16
+ /** Encoder effort from 0 (fastest) to 100 (slowest). @defaultValue `50` */
9
17
  effort?: number;
18
+ /** Use lossless WebP instead of lossy encoding. Lossy `quality` is then ignored. @defaultValue `false` */
10
19
  lossless?: boolean;
11
- /** WebP near-lossless preprocessing strength, from 0 (lossy) to 100. */
20
+ /**
21
+ * Near-lossless preprocessing quality for lossless encoding, from 0
22
+ * (strongest pixel changes and smallest output) to 100 (no preprocessing).
23
+ *
24
+ * @defaultValue `100`
25
+ */
12
26
  nearLossless?: number;
13
27
  }
14
28
  interface AvifEncoding extends SizedEncoding {
29
+ /** Encode the image as AVIF. */
15
30
  codec: 'avif';
31
+ /** Lossy quality from 1 (smallest, lowest fidelity) to 100. @defaultValue `50` */
16
32
  quality?: number;
33
+ /** Encoder effort from 0 (fastest) to 100 (slowest). @defaultValue `40` */
17
34
  effort?: number;
35
+ /** Use lossless AVIF instead of lossy encoding, overriding `quality` and chroma subsampling. @defaultValue `false` */
18
36
  lossless?: boolean;
37
+ /**
38
+ * Chroma resolution: `4:2:0` reduces color detail for smaller files, while
39
+ * `4:4:4` preserves full color resolution. Lossless encoding always uses
40
+ * full color resolution.
41
+ *
42
+ * @defaultValue `'4:2:0'`
43
+ */
19
44
  chromaSubsampling?: '4:2:0' | '4:4:4';
20
45
  }
21
46
  interface JpegEncoding extends SizedEncoding {
47
+ /** Encode the image as JPEG. Alpha is not preserved. */
22
48
  codec: 'jpeg';
49
+ /** Lossy quality from 1 (smallest, lowest fidelity) to 100. @defaultValue `75` */
23
50
  quality?: number;
51
+ /**
52
+ * Chroma resolution: `4:2:0` reduces color detail for smaller files, while
53
+ * `4:4:4` preserves full color resolution.
54
+ *
55
+ * @defaultValue `'4:2:0'`
56
+ */
24
57
  chromaSubsampling?: '4:2:0' | '4:4:4';
25
58
  }
26
59
  interface PngEncoding extends SizedEncoding {
60
+ /** Encode the image as lossless PNG. */
27
61
  codec: 'png';
28
62
  }
29
63
  interface UastcEncoding extends SizedEncoding {
64
+ /** Encode the image in a GPU-ready KTX2 container. */
30
65
  codec: 'ktx2';
66
+ /** Use high-quality UASTC block compression for LDR pixels. */
31
67
  mode: 'uastc';
68
+ /**
69
+ * How the texture is sampled: `srgb` for color data such as albedo and
70
+ * emissive maps, or `linear` for data and normal maps.
71
+ *
72
+ * @defaultValue `'srgb'`
73
+ */
32
74
  colorSpace?: 'srgb' | 'linear';
75
+ /** Tune the encoder for tangent-space normal-map data. @defaultValue `false` */
33
76
  normalMap?: boolean;
77
+ /** Encoder effort from 0 (fastest) to 4 (slowest, highest quality). @defaultValue `2` */
34
78
  level?: 0 | 1 | 2 | 3 | 4;
79
+ /** Generate a complete mip chain for minified rendering. @defaultValue `true` */
35
80
  mipmaps?: boolean;
81
+ /** Apply Zstandard supercompression to reduce download size. @defaultValue `true` */
36
82
  zstd?: boolean;
83
+ /**
84
+ * Enable rate-distortion optimization. This can substantially reduce files
85
+ * at the cost of image fidelity; omit it to disable RDO.
86
+ */
37
87
  rdo?: {
88
+ /**
89
+ * Compression-to-quality tradeoff from 0.001 to 10. Larger values usually
90
+ * produce smaller files with more distortion.
91
+ *
92
+ * @defaultValue `1`
93
+ */
38
94
  lambda?: number;
39
95
  };
40
96
  }
41
97
  interface Etc1sEncoding extends SizedEncoding {
98
+ /** Encode the image in a GPU-ready KTX2 container. */
42
99
  codec: 'ktx2';
100
+ /** Use ETC1S compression for the smallest download size at lower fidelity than UASTC. */
43
101
  mode: 'etc1s';
102
+ /**
103
+ * How the texture is sampled: `srgb` for color data such as albedo and
104
+ * emissive maps, or `linear` for data and normal maps.
105
+ *
106
+ * @defaultValue `'srgb'`
107
+ */
44
108
  colorSpace?: 'srgb' | 'linear';
109
+ /** Tune the encoder for tangent-space normal-map data. @defaultValue `false` */
45
110
  normalMap?: boolean;
111
+ /** Quality from 1 (smallest, lowest fidelity) to 255. @defaultValue `128` */
46
112
  quality?: number;
113
+ /**
114
+ * Encoder effort from 0 (fastest) to 6 (slowest). Higher values search
115
+ * harder for a smaller file at the requested quality.
116
+ *
117
+ * @defaultValue `1`
118
+ */
47
119
  compression?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
120
+ /** Generate a complete mip chain for minified rendering. @defaultValue `true` */
48
121
  mipmaps?: boolean;
49
122
  }
50
123
  /**
@@ -57,73 +130,133 @@ interface Etc1sEncoding extends SizedEncoding {
57
130
  * be authored at their target resolution.
58
131
  */
59
132
  interface UastcHdrEncoding {
133
+ /** Encode the image in a GPU-ready KTX2 container. */
60
134
  codec: 'ktx2';
135
+ /**
136
+ * Use UASTC HDR for half-float EXR or Radiance HDR input. This mode does not
137
+ * support resizing or alpha and is only available to standalone image recipes.
138
+ */
61
139
  mode: 'uastc-hdr';
62
- /** Encoder effort. Higher is slower; rarely helps on low-frequency lightmaps. */
140
+ /**
141
+ * Encoder effort from 0 (fastest) to 4 (slowest). Higher levels rarely help
142
+ * low-frequency content such as diffuse lightmaps.
143
+ *
144
+ * @defaultValue `1`
145
+ */
63
146
  level?: 0 | 1 | 2 | 3 | 4;
147
+ /** Generate a complete mip chain for minified rendering. @defaultValue `true` */
64
148
  mipmaps?: boolean;
149
+ /** Apply Zstandard supercompression to reduce download size. @defaultValue `true` */
65
150
  zstd?: boolean;
66
151
  }
67
152
  type RasterEncoding = WebpEncoding | AvifEncoding | JpegEncoding | PngEncoding;
68
153
  type ImageEncoding = RasterEncoding | UastcEncoding | Etc1sEncoding | UastcHdrEncoding;
69
154
  type GlbProperty = 'accessor' | 'animation' | 'buffer' | 'camera' | 'material' | 'mesh' | 'node' | 'primitive' | 'primitiveTarget' | 'skin' | 'texture';
70
155
  type GlbMethodRecipe = {
156
+ /** Merge equivalent accessors, materials, meshes, skins, or textures. */
71
157
  method: 'dedup';
158
+ /** Keep otherwise-identical properties separate when their names differ. @defaultValue `false` */
72
159
  keepUniqueNames?: boolean;
160
+ /**
161
+ * Property kinds to merge.
162
+ *
163
+ * @defaultValue `['accessor', 'material', 'mesh', 'skin', 'texture']`
164
+ */
73
165
  propertyTypes?: readonly Extract<GlbProperty, 'accessor' | 'material' | 'mesh' | 'skin' | 'texture'>[];
74
166
  } | {
167
+ /** Remove unused GLB properties and optionally simplify redundant data. */
75
168
  method: 'prune';
169
+ /** Keep unused vertex attributes, such as UVs without an assigned texture. @defaultValue `false` */
76
170
  keepAttributes?: boolean;
171
+ /** Prevent a property with custom `extras` data from being removed. @defaultValue `false` */
77
172
  keepExtras?: boolean;
173
+ /** Keep empty leaf nodes that would otherwise be removed. @defaultValue `false` */
78
174
  keepLeaves?: boolean;
175
+ /** Keep single-color textures instead of replacing them with material factors. @defaultValue `false` */
79
176
  keepSolidTextures?: boolean;
177
+ /** Property kinds eligible for pruning. By default, every supported kind is eligible. */
80
178
  propertyTypes?: readonly GlbProperty[];
81
179
  } | {
180
+ /** Remove redundant animation keyframes while keeping motion within a tolerance. */
82
181
  method: 'resample';
182
+ /** Maximum permitted animation-value error. Must be greater than zero. @defaultValue `0.0001` */
83
183
  tolerance?: number;
184
+ /**
185
+ * Deduplicate and prune temporary or unused resources after resampling.
186
+ * Disable this when those resources must be preserved for a later step.
187
+ *
188
+ * @defaultValue `true`
189
+ */
84
190
  cleanup?: boolean;
85
191
  };
86
192
  type TextureSelector = {
193
+ /** Select textures by the material slots that reference them. */
87
194
  by: 'slot';
195
+ /** glTF material texture-slot names, such as `baseColorTexture` or `normalTexture`. */
88
196
  slots: readonly string[];
89
197
  } | {
198
+ /** Select textures by their authored GLB names. */
90
199
  by: 'name';
200
+ /** Exact, case-sensitive texture names. Names should be unique within the GLB. */
91
201
  names: readonly string[];
92
202
  } | {
203
+ /** Select textures by their zero-based order in the GLB. */
93
204
  by: 'index';
205
+ /** Non-negative texture indices. Indices can change when a source GLB is re-exported. */
94
206
  indices: readonly number[];
95
207
  };
96
208
  interface EmbeddedTextureRecipe {
209
+ /**
210
+ * Unique, non-empty name for this texture group in progress events and
211
+ * optimization reports. It does not rename textures in the GLB.
212
+ */
97
213
  label: string;
214
+ /** Textures to encode. A texture cannot be claimed by more than one selector. */
98
215
  select: TextureSelector;
216
+ /** Output encoding for the selected textures. UASTC HDR is not supported inside GLBs. */
99
217
  encode: ImageEncoding;
100
218
  }
101
219
  type GeometryEncoding = {
220
+ /** Compress geometry with `KHR_draco_mesh_compression` for broad decoder support. */
102
221
  codec: 'draco';
103
222
  } | {
223
+ /** Compress geometry with `EXT_meshopt_compression` for fast runtime decoding. */
104
224
  codec: 'meshopt';
105
225
  };
106
226
  interface GlbAssetRecipe {
227
+ /** Optimize a binary glTF (`.glb`) asset. */
107
228
  kind: 'glb';
229
+ /** Structural transforms to run in array order before texture and geometry encoding. @defaultValue `[]` */
108
230
  methods?: readonly GlbMethodRecipe[];
231
+ /** Embedded texture groups to encode. Unselected textures keep their source encoding. @defaultValue `[]` */
109
232
  textures?: readonly EmbeddedTextureRecipe[];
233
+ /** Geometry codec to apply, or `false` to leave geometry uncompressed. @defaultValue `false` */
110
234
  geometry?: GeometryEncoding | false;
111
235
  }
112
236
  interface ImageAssetRecipe {
237
+ /** Optimize a standalone raster or HDR image. */
113
238
  kind: 'image';
239
+ /** Output codec and its quality, size, and performance tradeoffs. */
114
240
  encode: ImageEncoding;
115
241
  }
116
242
  type AssetRecipe = GlbAssetRecipe | ImageAssetRecipe;
117
243
  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'>) | Required<UastcHdrEncoding>;
118
244
  type NormalizedAssetRecipe = {
245
+ /** Identifies the normalized recipe as a standalone image recipe. */
119
246
  kind: 'image';
247
+ /** Image encoding with every codec default resolved. */
120
248
  encode: NormalizedImageEncoding;
121
249
  } | {
250
+ /** Identifies the normalized recipe as a binary glTF recipe. */
122
251
  kind: 'glb';
252
+ /** Structural transforms to run, with an omitted value resolved to an empty array. */
123
253
  methods: readonly GlbMethodRecipe[];
254
+ /** Embedded texture groups with every codec default resolved. */
124
255
  textures: readonly (Omit<EmbeddedTextureRecipe, 'encode'> & {
256
+ /** Output encoding with every codec default resolved. */
125
257
  encode: NormalizedImageEncoding;
126
258
  })[];
259
+ /** Resolved geometry codec, or `false` when geometry compression is disabled. */
127
260
  geometry: GeometryEncoding | false;
128
261
  };
129
262
 
@@ -1,4 +1,4 @@
1
- import { failure, CODEC_MANIFEST_HASH, CODEC_MANIFEST, ENGINE_ID } from './chunk-OLPLOAWX.js';
1
+ import { failure, CODEC_MANIFEST_HASH, CODEC_MANIFEST, ENGINE_ID } from './chunk-AQRTPT4W.js';
2
2
  import './chunk-5WRI5ZAA.js';
3
3
 
4
4
  // src/browser/chopper.ts
@@ -6,7 +6,7 @@ async function createBrowserChopper(options = {}) {
6
6
  if ((options.execution ?? "worker") === "worker") {
7
7
  return createWorkerChopper(options);
8
8
  }
9
- const { createInProcessChopper } = await import('./in-process-EWL5QP56.js');
9
+ const { createInProcessChopper } = await import('./in-process-E2XKKSPY.js');
10
10
  return createInProcessChopper(options);
11
11
  }
12
12
  function createWorkerChopper(options) {
@@ -49,7 +49,7 @@ var init_package = __esm({
49
49
  "package.json"() {
50
50
  package_default = {
51
51
  name: "@joycostudio/chopper",
52
- version: "0.5.1",
52
+ version: "0.5.3",
53
53
  description: "Isomorphic asset optimization pipeline",
54
54
  type: "module",
55
55
  license: "ISC",
@@ -362,6 +362,16 @@ var init_recipes = __esm({
362
362
  }
363
363
  });
364
364
 
365
+ // src/core/object-validation.ts
366
+ function unknownFieldIssue(value, allowedFields, subject) {
367
+ const field = Object.keys(value).find((key) => !allowedFields.includes(key));
368
+ return field ? `Unknown ${subject} field: ${field}` : void 0;
369
+ }
370
+ var init_object_validation = __esm({
371
+ "src/core/object-validation.ts"() {
372
+ }
373
+ });
374
+
365
375
  // src/core/validation.ts
366
376
  function validateRecipe(recipe) {
367
377
  const shapeIssue = recipeShapeIssue(recipe);
@@ -401,15 +411,26 @@ function recipeShapeIssue(recipe) {
401
411
  return `Unsupported recipe kind: ${String(candidate.kind)}`;
402
412
  }
403
413
  if (candidate.kind === "image") {
414
+ const unknownField2 = unknownFieldIssue(candidate, ["kind", "encode"], "image recipe");
415
+ if (unknownField2) return unknownField2;
404
416
  return encodingShapeIssue(candidate.encode);
405
417
  }
418
+ const unknownField = unknownFieldIssue(candidate, ["kind", "methods", "textures", "geometry"], "GLB recipe");
419
+ if (unknownField) return unknownField;
406
420
  if (candidate.methods !== void 0 && !Array.isArray(candidate.methods)) {
407
421
  return "GLB methods must be an array";
408
422
  }
409
423
  for (const method of candidate.methods ?? []) {
410
- if (!method || typeof method !== "object" || !["dedup", "prune", "resample"].includes(String(method.method))) {
424
+ if (!method || typeof method !== "object") {
425
+ return "GLB methods contain an unsupported transform";
426
+ }
427
+ const entry = method;
428
+ if (!["dedup", "prune", "resample"].includes(String(entry.method))) {
411
429
  return "GLB methods contain an unsupported transform";
412
430
  }
431
+ const allowedFields = entry.method === "dedup" ? ["method", "keepUniqueNames", "propertyTypes"] : entry.method === "prune" ? ["method", "keepAttributes", "keepExtras", "keepLeaves", "keepSolidTextures", "propertyTypes"] : ["method", "tolerance", "cleanup"];
432
+ const unknownField2 = unknownFieldIssue(entry, allowedFields, "GLB method");
433
+ if (unknownField2) return unknownField2;
413
434
  }
414
435
  if (candidate.textures !== void 0 && !Array.isArray(candidate.textures)) {
415
436
  return "GLB textures must be an array";
@@ -417,19 +438,32 @@ function recipeShapeIssue(recipe) {
417
438
  for (const texture of candidate.textures ?? []) {
418
439
  if (!texture || typeof texture !== "object") return "Texture recipes must be objects";
419
440
  const entry = texture;
441
+ const unknownField2 = unknownFieldIssue(entry, ["label", "select", "encode"], "texture recipe");
442
+ if (unknownField2) return unknownField2;
420
443
  if (typeof entry.label !== "string") return "Texture recipe labels must be strings";
421
444
  if (!entry.select || typeof entry.select !== "object") return "Texture recipes require a selector";
422
445
  const selector = entry.select;
423
- const values = selector[selector.by === "slot" ? "slots" : selector.by === "name" ? "names" : "indices"];
424
- if (!["slot", "name", "index"].includes(String(selector.by)) || !Array.isArray(values)) {
446
+ if (!["slot", "name", "index"].includes(String(selector.by))) {
425
447
  return "Texture recipe selectors are malformed";
426
448
  }
449
+ const valueField = selector.by === "slot" ? "slots" : selector.by === "name" ? "names" : "indices";
450
+ const selectorUnknownField = unknownFieldIssue(selector, ["by", valueField], "texture selector");
451
+ if (selectorUnknownField) return selectorUnknownField;
452
+ if (!Array.isArray(selector[valueField])) return "Texture recipe selectors are malformed";
427
453
  const encodingIssue2 = encodingShapeIssue(entry.encode);
428
454
  if (encodingIssue2) return encodingIssue2;
429
455
  }
430
456
  if (candidate.geometry !== void 0 && candidate.geometry !== false && (!candidate.geometry || typeof candidate.geometry !== "object" || !["draco", "meshopt"].includes(String(candidate.geometry.codec)))) {
431
457
  return "GLB geometry must be false, Draco, or Meshopt";
432
458
  }
459
+ if (candidate.geometry && typeof candidate.geometry === "object") {
460
+ const geometryUnknownField = unknownFieldIssue(
461
+ candidate.geometry,
462
+ ["codec"],
463
+ "GLB geometry"
464
+ );
465
+ if (geometryUnknownField) return geometryUnknownField;
466
+ }
433
467
  return void 0;
434
468
  }
435
469
  function encodingShapeIssue(encoding) {
@@ -444,6 +478,16 @@ function encodingShapeIssue(encoding) {
444
478
  if (candidate.codec === "ktx2" && candidate.mode === "uastc-hdr" && candidate.maxSize !== void 0) {
445
479
  return "UASTC HDR does not support maxSize; resizing requires a decoded 8-bit image. Author the source at its target resolution.";
446
480
  }
481
+ const allowedFields = candidate.codec === "webp" ? ["codec", "maxSize", "quality", "effort", "lossless", "nearLossless"] : candidate.codec === "avif" ? ["codec", "maxSize", "quality", "effort", "lossless", "chromaSubsampling"] : candidate.codec === "jpeg" ? ["codec", "maxSize", "quality", "chromaSubsampling"] : candidate.codec === "png" ? ["codec", "maxSize"] : candidate.mode === "uastc" ? ["codec", "mode", "maxSize", "colorSpace", "normalMap", "level", "mipmaps", "zstd", "rdo"] : candidate.mode === "etc1s" ? ["codec", "mode", "maxSize", "colorSpace", "normalMap", "quality", "compression", "mipmaps"] : ["codec", "mode", "level", "mipmaps", "zstd"];
482
+ const unknownField = unknownFieldIssue(candidate, allowedFields, "image encoding");
483
+ if (unknownField) return unknownField;
484
+ if (candidate.codec === "ktx2" && candidate.mode === "uastc" && candidate.rdo !== void 0) {
485
+ if (!candidate.rdo || typeof candidate.rdo !== "object" || Array.isArray(candidate.rdo)) {
486
+ return "UASTC RDO must be an object";
487
+ }
488
+ const rdoUnknownField = unknownFieldIssue(candidate.rdo, ["lambda"], "UASTC RDO");
489
+ if (rdoUnknownField) return rdoUnknownField;
490
+ }
447
491
  return void 0;
448
492
  }
449
493
  function recipeIssue(recipe) {
@@ -587,6 +631,7 @@ function normalizeEncoding(encoding) {
587
631
  var init_validation = __esm({
588
632
  "src/core/validation.ts"() {
589
633
  init_result();
634
+ init_object_validation();
590
635
  }
591
636
  });
592
637