@motionskit/cli 0.2.2 → 0.2.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.
package/dist/index.js CHANGED
@@ -263,6 +263,67 @@ function ttlSeconds(ttl) {
263
263
  const ms = typeof ttl === "string" ? parseDuration2(ttl) : null;
264
264
  return ms === null ? `ttl must be a duration such as "7d" or "12h", or seconds, got ${JSON.stringify(ttl)}` : Math.round(ms / 1e3);
265
265
  }
266
+ var PACE_KEYS = /* @__PURE__ */ new Set(["requests", "per", "concurrency"]);
267
+ function parsePaceMs(per) {
268
+ if (typeof per === "number") return Number.isInteger(per) && per > 0 ? per : `per must be a positive integer in ms or a duration such as "1s", got ${per}`;
269
+ if (typeof per === "string") {
270
+ const msMatch = /^(\d+)\s*ms$/.exec(per.trim());
271
+ if (msMatch) return Number(msMatch[1]);
272
+ const ms = parseDuration2(per);
273
+ if (ms !== null) return ms;
274
+ }
275
+ return `per must be a duration such as "1s" or "10s", or milliseconds, got ${JSON.stringify(per)}`;
276
+ }
277
+ function isInlinePaceSpec(v) {
278
+ const keys = Object.keys(v);
279
+ return keys.length > 0 && keys.every((k) => PACE_KEYS.has(k)) && keys.every((k) => typeof v[k] !== "object" || v[k] === null);
280
+ }
281
+ function normalizeBucketSpec(where2, raw, errors) {
282
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
283
+ errors.push(`${where2}: pace must be an object`);
284
+ return null;
285
+ }
286
+ const obj = raw;
287
+ const extra = Object.keys(obj).filter((k) => !PACE_KEYS.has(k));
288
+ if (extra.length) {
289
+ errors.push(`${where2}: unknown pace field${extra.length > 1 ? "s" : ""} ${extra.join(", ")}`);
290
+ return null;
291
+ }
292
+ const spec = {};
293
+ if (obj.requests !== void 0) spec.requests = obj.requests;
294
+ if (obj.per !== void 0) {
295
+ const perMs = parsePaceMs(obj.per);
296
+ if (typeof perMs === "string") {
297
+ errors.push(`${where2}: ${perMs}`);
298
+ return null;
299
+ }
300
+ spec.perMs = perMs;
301
+ }
302
+ if (obj.concurrency !== void 0) spec.concurrency = obj.concurrency;
303
+ return spec;
304
+ }
305
+ function normalizeSourcePace(raw, errors) {
306
+ if (raw === void 0) return {};
307
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
308
+ errors.push("pace must be a bucket spec or a map of named buckets");
309
+ return {};
310
+ }
311
+ const obj = raw;
312
+ if (!Object.keys(obj).length) {
313
+ errors.push("pace must declare at least one bucket");
314
+ return {};
315
+ }
316
+ if (isInlinePaceSpec(obj)) {
317
+ const spec = normalizeBucketSpec("bucket default", obj, errors);
318
+ return spec ? { default: spec } : {};
319
+ }
320
+ const out = {};
321
+ for (const [name, entry] of Object.entries(obj)) {
322
+ const spec = normalizeBucketSpec(`bucket ${name}`, entry, errors);
323
+ if (spec) out[name] = spec;
324
+ }
325
+ return out;
326
+ }
266
327
  async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
267
328
  const tenantDir = resolve(tenantRoot);
268
329
  const id2 = folderName(dir);
@@ -286,6 +347,8 @@ async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
286
347
  if (def?.kind !== "source") throw new SourceErrors(where2, ["index.ts must `export default defineSource({ \u2026 })`"]);
287
348
  const errors = [];
288
349
  if (def.id !== id2) errors.push(`the source's id "${def.id}" must match its folder, sources/${id2}`);
350
+ const buckets = normalizeSourcePace(def.pace, errors);
351
+ const hasDefaultBucket = Object.hasOwn(buckets, "default");
289
352
  const ops = {};
290
353
  for (const [name, op] of Object.entries(def.ops ?? {})) {
291
354
  if (!op?.input || typeof op.input !== "object" || !("~standard" in op.input))
@@ -293,7 +356,25 @@ async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
293
356
  if (typeof op?.call !== "function") errors.push(`op ${name}: call must be a function`);
294
357
  const ttl = ttlSeconds(op?.ttl);
295
358
  if (typeof ttl === "string") errors.push(`op ${name}: ${ttl}`);
296
- ops[name] = { ttlSeconds: typeof ttl === "number" ? ttl : 0 };
359
+ let opBuckets;
360
+ if (op?.pace === void 0) {
361
+ if (hasDefaultBucket) opBuckets = ["default"];
362
+ } else if (op.pace === false) {
363
+ opBuckets = void 0;
364
+ } else if (Array.isArray(op.pace)) {
365
+ if (!op.pace.length || op.pace.some((b) => typeof b !== "string")) errors.push(`op ${name}: pace list must be a non-empty array of bucket names`);
366
+ else opBuckets = [...op.pace];
367
+ } else if (typeof op.pace === "object" && op.pace !== null) {
368
+ const bucketKey = Object.hasOwn(buckets, name) ? `op_${name}` : name;
369
+ const spec = normalizeBucketSpec(`op ${name}`, op.pace, errors);
370
+ if (spec) {
371
+ buckets[bucketKey] = spec;
372
+ opBuckets = [bucketKey];
373
+ }
374
+ } else {
375
+ errors.push(`op ${name}: pace must be false, a list of bucket names, or { requests, per, concurrency }`);
376
+ }
377
+ ops[name] = { ttlSeconds: typeof ttl === "number" ? ttl : 0, ...opBuckets ? { buckets: opBuckets } : {} };
297
378
  }
298
379
  const out = await build(
299
380
  options(
@@ -313,6 +394,7 @@ export default serveSource(def);
313
394
  title: yaml.data?.title ?? def.title ?? id2,
314
395
  hosts: [...def.hosts ?? []],
315
396
  secrets: [...def.secrets ?? []],
397
+ ...Object.keys(buckets).length ? { buckets } : {},
316
398
  ops,
317
399
  bundle: sha256(bundle2)
318
400
  };
package/dist/main.js CHANGED
@@ -452,6 +452,67 @@ function ttlSeconds(ttl) {
452
452
  const ms = typeof ttl === "string" ? parseDuration2(ttl) : null;
453
453
  return ms === null ? `ttl must be a duration such as "7d" or "12h", or seconds, got ${JSON.stringify(ttl)}` : Math.round(ms / 1e3);
454
454
  }
455
+ var PACE_KEYS = /* @__PURE__ */ new Set(["requests", "per", "concurrency"]);
456
+ function parsePaceMs(per) {
457
+ if (typeof per === "number") return Number.isInteger(per) && per > 0 ? per : `per must be a positive integer in ms or a duration such as "1s", got ${per}`;
458
+ if (typeof per === "string") {
459
+ const msMatch = /^(\d+)\s*ms$/.exec(per.trim());
460
+ if (msMatch) return Number(msMatch[1]);
461
+ const ms = parseDuration2(per);
462
+ if (ms !== null) return ms;
463
+ }
464
+ return `per must be a duration such as "1s" or "10s", or milliseconds, got ${JSON.stringify(per)}`;
465
+ }
466
+ function isInlinePaceSpec(v) {
467
+ const keys = Object.keys(v);
468
+ return keys.length > 0 && keys.every((k) => PACE_KEYS.has(k)) && keys.every((k) => typeof v[k] !== "object" || v[k] === null);
469
+ }
470
+ function normalizeBucketSpec(where2, raw, errors) {
471
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
472
+ errors.push(`${where2}: pace must be an object`);
473
+ return null;
474
+ }
475
+ const obj = raw;
476
+ const extra = Object.keys(obj).filter((k) => !PACE_KEYS.has(k));
477
+ if (extra.length) {
478
+ errors.push(`${where2}: unknown pace field${extra.length > 1 ? "s" : ""} ${extra.join(", ")}`);
479
+ return null;
480
+ }
481
+ const spec = {};
482
+ if (obj.requests !== void 0) spec.requests = obj.requests;
483
+ if (obj.per !== void 0) {
484
+ const perMs = parsePaceMs(obj.per);
485
+ if (typeof perMs === "string") {
486
+ errors.push(`${where2}: ${perMs}`);
487
+ return null;
488
+ }
489
+ spec.perMs = perMs;
490
+ }
491
+ if (obj.concurrency !== void 0) spec.concurrency = obj.concurrency;
492
+ return spec;
493
+ }
494
+ function normalizeSourcePace(raw, errors) {
495
+ if (raw === void 0) return {};
496
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
497
+ errors.push("pace must be a bucket spec or a map of named buckets");
498
+ return {};
499
+ }
500
+ const obj = raw;
501
+ if (!Object.keys(obj).length) {
502
+ errors.push("pace must declare at least one bucket");
503
+ return {};
504
+ }
505
+ if (isInlinePaceSpec(obj)) {
506
+ const spec = normalizeBucketSpec("bucket default", obj, errors);
507
+ return spec ? { default: spec } : {};
508
+ }
509
+ const out = {};
510
+ for (const [name, entry] of Object.entries(obj)) {
511
+ const spec = normalizeBucketSpec(`bucket ${name}`, entry, errors);
512
+ if (spec) out[name] = spec;
513
+ }
514
+ return out;
515
+ }
455
516
  async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
456
517
  const tenantDir = resolve(tenantRoot);
457
518
  const id2 = folderName(dir);
@@ -475,6 +536,8 @@ async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
475
536
  if (def?.kind !== "source") throw new SourceErrors(where2, ["index.ts must `export default defineSource({ \u2026 })`"]);
476
537
  const errors = [];
477
538
  if (def.id !== id2) errors.push(`the source's id "${def.id}" must match its folder, sources/${id2}`);
539
+ const buckets = normalizeSourcePace(def.pace, errors);
540
+ const hasDefaultBucket = Object.hasOwn(buckets, "default");
478
541
  const ops = {};
479
542
  for (const [name, op] of Object.entries(def.ops ?? {})) {
480
543
  if (!op?.input || typeof op.input !== "object" || !("~standard" in op.input))
@@ -482,7 +545,25 @@ async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
482
545
  if (typeof op?.call !== "function") errors.push(`op ${name}: call must be a function`);
483
546
  const ttl = ttlSeconds(op?.ttl);
484
547
  if (typeof ttl === "string") errors.push(`op ${name}: ${ttl}`);
485
- ops[name] = { ttlSeconds: typeof ttl === "number" ? ttl : 0 };
548
+ let opBuckets;
549
+ if (op?.pace === void 0) {
550
+ if (hasDefaultBucket) opBuckets = ["default"];
551
+ } else if (op.pace === false) {
552
+ opBuckets = void 0;
553
+ } else if (Array.isArray(op.pace)) {
554
+ if (!op.pace.length || op.pace.some((b) => typeof b !== "string")) errors.push(`op ${name}: pace list must be a non-empty array of bucket names`);
555
+ else opBuckets = [...op.pace];
556
+ } else if (typeof op.pace === "object" && op.pace !== null) {
557
+ const bucketKey = Object.hasOwn(buckets, name) ? `op_${name}` : name;
558
+ const spec = normalizeBucketSpec(`op ${name}`, op.pace, errors);
559
+ if (spec) {
560
+ buckets[bucketKey] = spec;
561
+ opBuckets = [bucketKey];
562
+ }
563
+ } else {
564
+ errors.push(`op ${name}: pace must be false, a list of bucket names, or { requests, per, concurrency }`);
565
+ }
566
+ ops[name] = { ttlSeconds: typeof ttl === "number" ? ttl : 0, ...opBuckets ? { buckets: opBuckets } : {} };
486
567
  }
487
568
  const out = await build(
488
569
  options(
@@ -502,6 +583,7 @@ export default serveSource(def);
502
583
  title: yaml.data?.title ?? def.title ?? id2,
503
584
  hosts: [...def.hosts ?? []],
504
585
  secrets: [...def.secrets ?? []],
586
+ ...Object.keys(buckets).length ? { buckets } : {},
505
587
  ops,
506
588
  bundle: sha256(bundle2)
507
589
  };
@@ -543,10 +625,14 @@ function writeSourceTypes(tenantDir, sources) {
543
625
  }
544
626
  function reportSources(sources) {
545
627
  for (const { manifest: m, bundle: bundle2 } of sources) {
628
+ const bucketSummary = m.buckets ? ` pace=[${Object.keys(m.buckets).join(", ")}]` : "";
546
629
  console.log(
547
- ` source ${m.source} @ ${m.sha.slice(0, 10)} ${(bundle2.length / 1024).toFixed(0)} KB, hosts=[${m.hosts.join(", ")}] secrets=[${m.secrets.join(", ")}]`
630
+ ` source ${m.source} @ ${m.sha.slice(0, 10)} ${(bundle2.length / 1024).toFixed(0)} KB, hosts=[${m.hosts.join(", ")}] secrets=[${m.secrets.join(", ")}]${bucketSummary}`
548
631
  );
549
- for (const [op, { ttlSeconds: ttlSeconds2 }] of Object.entries(m.ops)) console.log(` op ${m.source}.${op}${ttlSeconds2 ? ` ttl=${ttlSeconds2}s` : ""}`);
632
+ for (const [op, { ttlSeconds: ttlSeconds2, buckets }] of Object.entries(m.ops)) {
633
+ const pace = buckets?.length ? ` pace=[${buckets.join(", ")}]` : "";
634
+ console.log(` op ${m.source}.${op}${ttlSeconds2 ? ` ttl=${ttlSeconds2}s` : ""}${pace}`);
635
+ }
550
636
  }
551
637
  }
552
638
  async function publishSources(post, tenant, sources, opts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@motionskit/cli",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -29,7 +29,7 @@
29
29
  "esbuild": "^0.28.2",
30
30
  "yaml": "^2.9.1",
31
31
  "zod": "^4.6.7",
32
- "@motionskit/kernel": "0.2.2"
32
+ "@motionskit/kernel": "0.2.3"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/node": "^22.19.0",