@joycostudio/chopper 0.5.1 → 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.
@@ -41,6 +41,12 @@ var failure = (code, message, cause) => ({
41
41
  error: cause === void 0 ? { code, message } : { code, message, cause }
42
42
  });
43
43
 
44
+ // src/core/object-validation.ts
45
+ function unknownFieldIssue(value, allowedFields, subject) {
46
+ const field = Object.keys(value).find((key) => !allowedFields.includes(key));
47
+ return field ? `Unknown ${subject} field: ${field}` : void 0;
48
+ }
49
+
44
50
  // src/core/validation.ts
45
51
  function validateRecipe(recipe) {
46
52
  const shapeIssue = recipeShapeIssue(recipe);
@@ -80,15 +86,26 @@ function recipeShapeIssue(recipe) {
80
86
  return `Unsupported recipe kind: ${String(candidate.kind)}`;
81
87
  }
82
88
  if (candidate.kind === "image") {
89
+ const unknownField2 = unknownFieldIssue(candidate, ["kind", "encode"], "image recipe");
90
+ if (unknownField2) return unknownField2;
83
91
  return encodingShapeIssue(candidate.encode);
84
92
  }
93
+ const unknownField = unknownFieldIssue(candidate, ["kind", "methods", "textures", "geometry"], "GLB recipe");
94
+ if (unknownField) return unknownField;
85
95
  if (candidate.methods !== void 0 && !Array.isArray(candidate.methods)) {
86
96
  return "GLB methods must be an array";
87
97
  }
88
98
  for (const method of candidate.methods ?? []) {
89
- if (!method || typeof method !== "object" || !["dedup", "prune", "resample"].includes(String(method.method))) {
99
+ if (!method || typeof method !== "object") {
100
+ return "GLB methods contain an unsupported transform";
101
+ }
102
+ const entry = method;
103
+ if (!["dedup", "prune", "resample"].includes(String(entry.method))) {
90
104
  return "GLB methods contain an unsupported transform";
91
105
  }
106
+ const allowedFields = entry.method === "dedup" ? ["method", "keepUniqueNames", "propertyTypes"] : entry.method === "prune" ? ["method", "keepAttributes", "keepExtras", "keepLeaves", "keepSolidTextures", "propertyTypes"] : ["method", "tolerance", "cleanup"];
107
+ const unknownField2 = unknownFieldIssue(entry, allowedFields, "GLB method");
108
+ if (unknownField2) return unknownField2;
92
109
  }
93
110
  if (candidate.textures !== void 0 && !Array.isArray(candidate.textures)) {
94
111
  return "GLB textures must be an array";
@@ -96,19 +113,32 @@ function recipeShapeIssue(recipe) {
96
113
  for (const texture of candidate.textures ?? []) {
97
114
  if (!texture || typeof texture !== "object") return "Texture recipes must be objects";
98
115
  const entry = texture;
116
+ const unknownField2 = unknownFieldIssue(entry, ["label", "select", "encode"], "texture recipe");
117
+ if (unknownField2) return unknownField2;
99
118
  if (typeof entry.label !== "string") return "Texture recipe labels must be strings";
100
119
  if (!entry.select || typeof entry.select !== "object") return "Texture recipes require a selector";
101
120
  const selector = entry.select;
102
- const values = selector[selector.by === "slot" ? "slots" : selector.by === "name" ? "names" : "indices"];
103
- if (!["slot", "name", "index"].includes(String(selector.by)) || !Array.isArray(values)) {
121
+ if (!["slot", "name", "index"].includes(String(selector.by))) {
104
122
  return "Texture recipe selectors are malformed";
105
123
  }
124
+ const valueField = selector.by === "slot" ? "slots" : selector.by === "name" ? "names" : "indices";
125
+ const selectorUnknownField = unknownFieldIssue(selector, ["by", valueField], "texture selector");
126
+ if (selectorUnknownField) return selectorUnknownField;
127
+ if (!Array.isArray(selector[valueField])) return "Texture recipe selectors are malformed";
106
128
  const encodingIssue2 = encodingShapeIssue(entry.encode);
107
129
  if (encodingIssue2) return encodingIssue2;
108
130
  }
109
131
  if (candidate.geometry !== void 0 && candidate.geometry !== false && (!candidate.geometry || typeof candidate.geometry !== "object" || !["draco", "meshopt"].includes(String(candidate.geometry.codec)))) {
110
132
  return "GLB geometry must be false, Draco, or Meshopt";
111
133
  }
134
+ if (candidate.geometry && typeof candidate.geometry === "object") {
135
+ const geometryUnknownField = unknownFieldIssue(
136
+ candidate.geometry,
137
+ ["codec"],
138
+ "GLB geometry"
139
+ );
140
+ if (geometryUnknownField) return geometryUnknownField;
141
+ }
112
142
  return void 0;
113
143
  }
114
144
  function encodingShapeIssue(encoding) {
@@ -123,6 +153,16 @@ function encodingShapeIssue(encoding) {
123
153
  if (candidate.codec === "ktx2" && candidate.mode === "uastc-hdr" && candidate.maxSize !== void 0) {
124
154
  return "UASTC HDR does not support maxSize; resizing requires a decoded 8-bit image. Author the source at its target resolution.";
125
155
  }
156
+ 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"];
157
+ const unknownField = unknownFieldIssue(candidate, allowedFields, "image encoding");
158
+ if (unknownField) return unknownField;
159
+ if (candidate.codec === "ktx2" && candidate.mode === "uastc" && candidate.rdo !== void 0) {
160
+ if (!candidate.rdo || typeof candidate.rdo !== "object" || Array.isArray(candidate.rdo)) {
161
+ return "UASTC RDO must be an object";
162
+ }
163
+ const rdoUnknownField = unknownFieldIssue(candidate.rdo, ["lambda"], "UASTC RDO");
164
+ if (rdoUnknownField) return rdoUnknownField;
165
+ }
126
166
  return void 0;
127
167
  }
128
168
  function recipeIssue(recipe) {
@@ -390,7 +430,7 @@ function latin1(bytes) {
390
430
 
391
431
  // package.json
392
432
  var package_default = {
393
- version: "0.5.1"};
433
+ version: "0.5.2"};
394
434
 
395
435
  // src/codecs/manifest.ts
396
436
  var CODEC_MANIFEST_HASH = "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322";
@@ -1283,6 +1323,31 @@ async function optimizeFile({
1283
1323
  return failure("io_failed", `Could not optimize ${input}`, cause);
1284
1324
  }
1285
1325
  }
1326
+ function validateConfig(config) {
1327
+ if (!config || typeof config !== "object" || Array.isArray(config)) {
1328
+ return failure("invalid_recipe", "Config must be an object");
1329
+ }
1330
+ const candidate = config;
1331
+ const unknownField = unknownFieldIssue(candidate, ["output", "rules", "concurrency"], "config");
1332
+ if (unknownField) return failure("invalid_recipe", unknownField);
1333
+ if (!Array.isArray(candidate.rules)) {
1334
+ return failure("invalid_recipe", "Config rules must be an array");
1335
+ }
1336
+ if (candidate.concurrency !== void 0 && (typeof candidate.concurrency !== "number" || !Number.isInteger(candidate.concurrency) || candidate.concurrency < 1)) {
1337
+ return failure("invalid_recipe", "Config concurrency must be a positive integer");
1338
+ }
1339
+ for (const [index, rule] of candidate.rules.entries()) {
1340
+ if (!rule || typeof rule !== "object" || Array.isArray(rule)) {
1341
+ return failure("invalid_recipe", `Config rule ${index} must be an object`);
1342
+ }
1343
+ const entry = rule;
1344
+ const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe", "path"], `config rule ${index}`);
1345
+ if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
1346
+ const recipe = validateRecipe(entry.recipe);
1347
+ if (!recipe.ok) return recipe;
1348
+ }
1349
+ return success(config);
1350
+ }
1286
1351
  async function loadConfig(path = "chopper.config.ts") {
1287
1352
  const absolutePath = resolve(path);
1288
1353
  try {
@@ -1291,13 +1356,11 @@ async function loadConfig(path = "chopper.config.ts") {
1291
1356
  const config = await jiti.import(absolutePath, {
1292
1357
  default: true
1293
1358
  });
1294
- if (!config || !Array.isArray(config.rules)) {
1359
+ const validated = validateConfig(config);
1360
+ if (!validated.ok && validated.error.message === "Config must be an object") {
1295
1361
  return failure("invalid_recipe", `${path} must export a Chopper config`);
1296
1362
  }
1297
- if (config.concurrency !== void 0 && (!Number.isInteger(config.concurrency) || config.concurrency < 1)) {
1298
- return failure("invalid_recipe", "Config concurrency must be a positive integer");
1299
- }
1300
- return success(config);
1363
+ return validated;
1301
1364
  } catch (cause) {
1302
1365
  return failure("io_failed", `Could not load ${path}`, cause);
1303
1366
  }
@@ -1318,6 +1381,9 @@ async function runConfig(options = {}) {
1318
1381
  return success({ mode, output: relative(cwd, resolve(cwd, loaded.data.output)), files, totals: summarize(files) });
1319
1382
  }
1320
1383
  async function planAssets(config, cwd) {
1384
+ const validated = validateConfig(config);
1385
+ if (!validated.ok) return validated;
1386
+ config = validated.data;
1321
1387
  const planned = [];
1322
1388
  if (typeof config.output !== "string" || !config.output) {
1323
1389
  return failure("invalid_recipe", "Config output directory is required");
@@ -1382,7 +1448,8 @@ async function executeAsset(asset, mode, cwd, chopper2) {
1382
1448
  output: relative(cwd, asset.output),
1383
1449
  beforeBytes: source.byteLength,
1384
1450
  afterBytes: optimized.data.bytes.byteLength,
1385
- engineId: optimized.data.report.engineId
1451
+ engineId: optimized.data.report.engineId,
1452
+ optimization: optimized.data.report
1386
1453
  };
1387
1454
  if (mode === "report") return { ...base, status: "reported" };
1388
1455
  if (mode === "check") {