@joycostudio/chopper 0.5.0 → 0.5.2

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/dist/cli/index.js CHANGED
@@ -225,7 +225,7 @@ function latin1(bytes) {
225
225
 
226
226
  // package.json
227
227
  var package_default = {
228
- version: "0.5.0"};
228
+ version: "0.5.2"};
229
229
 
230
230
  // src/codecs/manifest.ts
231
231
  var CODEC_MANIFEST_HASH = "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322";
@@ -319,6 +319,12 @@ var failure = (code, message, cause) => ({
319
319
  error: cause === void 0 ? { code, message } : { code, message, cause }
320
320
  });
321
321
 
322
+ // src/core/object-validation.ts
323
+ function unknownFieldIssue(value, allowedFields, subject) {
324
+ const field = Object.keys(value).find((key) => !allowedFields.includes(key));
325
+ return field ? `Unknown ${subject} field: ${field}` : void 0;
326
+ }
327
+
322
328
  // src/core/validation.ts
323
329
  function validateRecipe(recipe) {
324
330
  const shapeIssue = recipeShapeIssue(recipe);
@@ -358,15 +364,26 @@ function recipeShapeIssue(recipe) {
358
364
  return `Unsupported recipe kind: ${String(candidate.kind)}`;
359
365
  }
360
366
  if (candidate.kind === "image") {
367
+ const unknownField2 = unknownFieldIssue(candidate, ["kind", "encode"], "image recipe");
368
+ if (unknownField2) return unknownField2;
361
369
  return encodingShapeIssue(candidate.encode);
362
370
  }
371
+ const unknownField = unknownFieldIssue(candidate, ["kind", "methods", "textures", "geometry"], "GLB recipe");
372
+ if (unknownField) return unknownField;
363
373
  if (candidate.methods !== void 0 && !Array.isArray(candidate.methods)) {
364
374
  return "GLB methods must be an array";
365
375
  }
366
376
  for (const method of candidate.methods ?? []) {
367
- if (!method || typeof method !== "object" || !["dedup", "prune", "resample"].includes(String(method.method))) {
377
+ if (!method || typeof method !== "object") {
378
+ return "GLB methods contain an unsupported transform";
379
+ }
380
+ const entry = method;
381
+ if (!["dedup", "prune", "resample"].includes(String(entry.method))) {
368
382
  return "GLB methods contain an unsupported transform";
369
383
  }
384
+ const allowedFields = entry.method === "dedup" ? ["method", "keepUniqueNames", "propertyTypes"] : entry.method === "prune" ? ["method", "keepAttributes", "keepExtras", "keepLeaves", "keepSolidTextures", "propertyTypes"] : ["method", "tolerance", "cleanup"];
385
+ const unknownField2 = unknownFieldIssue(entry, allowedFields, "GLB method");
386
+ if (unknownField2) return unknownField2;
370
387
  }
371
388
  if (candidate.textures !== void 0 && !Array.isArray(candidate.textures)) {
372
389
  return "GLB textures must be an array";
@@ -374,19 +391,32 @@ function recipeShapeIssue(recipe) {
374
391
  for (const texture of candidate.textures ?? []) {
375
392
  if (!texture || typeof texture !== "object") return "Texture recipes must be objects";
376
393
  const entry = texture;
394
+ const unknownField2 = unknownFieldIssue(entry, ["label", "select", "encode"], "texture recipe");
395
+ if (unknownField2) return unknownField2;
377
396
  if (typeof entry.label !== "string") return "Texture recipe labels must be strings";
378
397
  if (!entry.select || typeof entry.select !== "object") return "Texture recipes require a selector";
379
398
  const selector = entry.select;
380
- const values = selector[selector.by === "slot" ? "slots" : selector.by === "name" ? "names" : "indices"];
381
- if (!["slot", "name", "index"].includes(String(selector.by)) || !Array.isArray(values)) {
399
+ if (!["slot", "name", "index"].includes(String(selector.by))) {
382
400
  return "Texture recipe selectors are malformed";
383
401
  }
402
+ const valueField = selector.by === "slot" ? "slots" : selector.by === "name" ? "names" : "indices";
403
+ const selectorUnknownField = unknownFieldIssue(selector, ["by", valueField], "texture selector");
404
+ if (selectorUnknownField) return selectorUnknownField;
405
+ if (!Array.isArray(selector[valueField])) return "Texture recipe selectors are malformed";
384
406
  const encodingIssue2 = encodingShapeIssue(entry.encode);
385
407
  if (encodingIssue2) return encodingIssue2;
386
408
  }
387
409
  if (candidate.geometry !== void 0 && candidate.geometry !== false && (!candidate.geometry || typeof candidate.geometry !== "object" || !["draco", "meshopt"].includes(String(candidate.geometry.codec)))) {
388
410
  return "GLB geometry must be false, Draco, or Meshopt";
389
411
  }
412
+ if (candidate.geometry && typeof candidate.geometry === "object") {
413
+ const geometryUnknownField = unknownFieldIssue(
414
+ candidate.geometry,
415
+ ["codec"],
416
+ "GLB geometry"
417
+ );
418
+ if (geometryUnknownField) return geometryUnknownField;
419
+ }
390
420
  return void 0;
391
421
  }
392
422
  function encodingShapeIssue(encoding) {
@@ -401,6 +431,16 @@ function encodingShapeIssue(encoding) {
401
431
  if (candidate.codec === "ktx2" && candidate.mode === "uastc-hdr" && candidate.maxSize !== void 0) {
402
432
  return "UASTC HDR does not support maxSize; resizing requires a decoded 8-bit image. Author the source at its target resolution.";
403
433
  }
434
+ 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"];
435
+ const unknownField = unknownFieldIssue(candidate, allowedFields, "image encoding");
436
+ if (unknownField) return unknownField;
437
+ if (candidate.codec === "ktx2" && candidate.mode === "uastc" && candidate.rdo !== void 0) {
438
+ if (!candidate.rdo || typeof candidate.rdo !== "object" || Array.isArray(candidate.rdo)) {
439
+ return "UASTC RDO must be an object";
440
+ }
441
+ const rdoUnknownField = unknownFieldIssue(candidate.rdo, ["lambda"], "UASTC RDO");
442
+ if (rdoUnknownField) return rdoUnknownField;
443
+ }
404
444
  return void 0;
405
445
  }
406
446
  function recipeIssue(recipe) {
@@ -1320,6 +1360,31 @@ function createNodeChopper(options = {}) {
1320
1360
  if (!options.codecDirectory) chopper = created;
1321
1361
  return created;
1322
1362
  }
1363
+ function validateConfig(config) {
1364
+ if (!config || typeof config !== "object" || Array.isArray(config)) {
1365
+ return failure("invalid_recipe", "Config must be an object");
1366
+ }
1367
+ const candidate = config;
1368
+ const unknownField = unknownFieldIssue(candidate, ["output", "rules", "concurrency"], "config");
1369
+ if (unknownField) return failure("invalid_recipe", unknownField);
1370
+ if (!Array.isArray(candidate.rules)) {
1371
+ return failure("invalid_recipe", "Config rules must be an array");
1372
+ }
1373
+ if (candidate.concurrency !== void 0 && (typeof candidate.concurrency !== "number" || !Number.isInteger(candidate.concurrency) || candidate.concurrency < 1)) {
1374
+ return failure("invalid_recipe", "Config concurrency must be a positive integer");
1375
+ }
1376
+ for (const [index, rule] of candidate.rules.entries()) {
1377
+ if (!rule || typeof rule !== "object" || Array.isArray(rule)) {
1378
+ return failure("invalid_recipe", `Config rule ${index} must be an object`);
1379
+ }
1380
+ const entry = rule;
1381
+ const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe", "path"], `config rule ${index}`);
1382
+ if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
1383
+ const recipe = validateRecipe(entry.recipe);
1384
+ if (!recipe.ok) return recipe;
1385
+ }
1386
+ return success(config);
1387
+ }
1323
1388
  async function loadConfig(path = "chopper.config.ts") {
1324
1389
  const absolutePath = resolve(path);
1325
1390
  try {
@@ -1328,13 +1393,11 @@ async function loadConfig(path = "chopper.config.ts") {
1328
1393
  const config = await jiti.import(absolutePath, {
1329
1394
  default: true
1330
1395
  });
1331
- if (!config || !Array.isArray(config.rules)) {
1396
+ const validated = validateConfig(config);
1397
+ if (!validated.ok && validated.error.message === "Config must be an object") {
1332
1398
  return failure("invalid_recipe", `${path} must export a Chopper config`);
1333
1399
  }
1334
- if (config.concurrency !== void 0 && (!Number.isInteger(config.concurrency) || config.concurrency < 1)) {
1335
- return failure("invalid_recipe", "Config concurrency must be a positive integer");
1336
- }
1337
- return success(config);
1400
+ return validated;
1338
1401
  } catch (cause) {
1339
1402
  return failure("io_failed", `Could not load ${path}`, cause);
1340
1403
  }
@@ -1357,6 +1420,9 @@ async function runConfig(options = {}) {
1357
1420
  return success({ mode, output: relative(cwd, resolve(cwd, loaded.data.output)), files, totals: summarize(files) });
1358
1421
  }
1359
1422
  async function planAssets(config, cwd) {
1423
+ const validated = validateConfig(config);
1424
+ if (!validated.ok) return validated;
1425
+ config = validated.data;
1360
1426
  const planned = [];
1361
1427
  if (typeof config.output !== "string" || !config.output) {
1362
1428
  return failure("invalid_recipe", "Config output directory is required");
@@ -1421,7 +1487,8 @@ async function executeAsset(asset, mode, cwd, chopper2) {
1421
1487
  output: relative(cwd, asset.output),
1422
1488
  beforeBytes: source.byteLength,
1423
1489
  afterBytes: optimized.data.bytes.byteLength,
1424
- engineId: optimized.data.report.engineId
1490
+ engineId: optimized.data.report.engineId,
1491
+ optimization: optimized.data.report
1425
1492
  };
1426
1493
  if (mode === "report") return { ...base, status: "reported" };
1427
1494
  if (mode === "check") {