@kaynooo/cli-pkg 0.17.4 → 0.18.1

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 (3) hide show
  1. package/README.md +1 -0
  2. package/dist/cli.js +37 -16
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -29,6 +29,7 @@ pkg outdated
29
29
 
30
30
  - `licenses` is the allowlist of SPDX ids, compared without regard to case. An expression is allowed when one side of an `OR` is and every term of an `AND` is; a `WITH` exception is ignored. With an allowlist set, `pkg check` refuses a package whose licence is outside it or missing (a legacy `SEE LICENSE IN` or `UNLICENSED` counts as outside), so the `bun add` the guard checks through `pkg check` is refused with it. Without the key the licence is only printed.
31
31
  - `minAge` (a whole number of days, 14 when no config sets it) is how old a version must be when `--min-age` is not given, in `check`, `audit` and `outdated`.
32
+ - `trusted` (names or `@scope/*` globs, `["@kaynooo/*", "@kayjs/*"]` when no config sets it) lists the packages whose age is not checked: the user's own releases install as soon as they are out, the report saying the age was not checked. Their licence, advisories and near misses still are; `[]` checks every package's age.
32
33
  - `concurrency` (a positive whole number, 8 when no config sets it) is how many registry lookups `audit` and `outdated` run at once.
33
34
  - `blockSeverity` (`low`, `moderate`, `high` or `critical`, `high` when no config sets it) makes `pkg check` ask the registry for the advisories of the exact `name@version` and refuse it when one is at that severity or above. The request is the one `bun audit` makes: a `POST` of `{"name": ["version"]}` to `<registry>/-/npm/v1/security/advisories/bulk`. A registry without that endpoint (a private one, say) leaves the version unrefused with a warning, while an advisory request that fails (unreachable, a 5xx, not JSON) exits 2, since the advisories could not be read.
34
35
 
package/dist/cli.js CHANGED
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env -S bun --no-env-file --config=/dev/null
2
- // @bun
1
+ #!/bin/sh
2
+ // @bun 2>/dev/null; exec bun --no-env-file --config=/dev/null "$0" "$@"
3
3
  var __defProp = Object.defineProperty;
4
4
  var __returnValue = (v) => v;
5
5
  function __exportSetter(name, newValue) {
@@ -708,11 +708,20 @@ function wholeNumber(value, name, fallback, min = 1) {
708
708
  badUsage(`${name.startsWith("<") ? name : `--${name}`} takes a whole number from ${min}, not ${value}`);
709
709
  return number;
710
710
  }
711
+ function durationMs(text, unit) {
712
+ if (BARE_DURATION.test(text))
713
+ return Number(text) * UNIT_MS[unit];
714
+ if (!DURATION.test(text))
715
+ return 0;
716
+ let ms = 0;
717
+ for (const [, amount, part] of text.matchAll(DURATION_PART))
718
+ ms += Number(amount) * UNIT_MS[part];
719
+ return ms;
720
+ }
711
721
  function duration(value, name, fallback, unit = "s") {
712
722
  if (value === undefined)
713
723
  return fallback;
714
- const match = DURATION.exec(value.trim());
715
- const ms = match === null ? 0 : Number(match[1]) * UNIT_MS[match[2] ?? unit];
724
+ const ms = durationMs(value.trim(), unit);
716
725
  if (ms < 1)
717
726
  badUsage(`--${name} takes a duration such as 30s, 5m, 1h or 7d, not ${value}`);
718
727
  return ms;
@@ -732,12 +741,14 @@ async function readInput(path, { noStdin = "nothing is piped on stdin" } = {}) {
732
741
  fail(`no file ${path}`);
733
742
  return file.text();
734
743
  }
735
- var WHOLE, DURATION, UNIT_MS;
744
+ var WHOLE, BARE_DURATION, DURATION, DURATION_PART, UNIT_MS;
736
745
  var init_input = __esm(() => {
737
746
  init_args();
738
747
  WHOLE = /^\d+$/;
739
- DURATION = /^(\d+(?:\.\d+)?)(ms|[smhd])?$/;
740
- UNIT_MS = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 };
748
+ BARE_DURATION = /^\d+(?:\.\d+)?$/;
749
+ DURATION = /^(?:\d+(?:\.\d+)?(?:ms|min|[smhd]))+$/;
750
+ DURATION_PART = /(\d+(?:\.\d+)?)(ms|min|[smhd])/g;
751
+ UNIT_MS = { ms: 1, s: 1000, m: 60000, min: 60000, h: 3600000, d: 86400000 };
741
752
  });
742
753
 
743
754
  // ../kit/src/args.ts
@@ -2796,6 +2807,9 @@ function daysAgo(age) {
2796
2807
  function installScriptsOf(version) {
2797
2808
  return Object.fromEntries(INSTALL_SCRIPTS.flatMap((name) => version?.scripts?.[name] === undefined ? [] : [[name, version.scripts[name]]]));
2798
2809
  }
2810
+ function trustedBy(name, trusted) {
2811
+ return trusted.find((pattern) => pattern === name || new Bun.Glob(pattern).match(name));
2812
+ }
2799
2813
  function safeRelease(packument, now, minAgeDays) {
2800
2814
  const old = Object.keys(packument.versions ?? {}).filter((version) => isStable(version) && (ageDays(packument.time?.[version], now) ?? 0) >= minAgeDays && packument.time?.[version] !== undefined);
2801
2815
  const version = old.sort(Bun.semver.order).at(-1);
@@ -2837,7 +2851,8 @@ function analyze(input) {
2837
2851
  report.license = licenseOf(entry?.license) ?? licenseOf(packument.license);
2838
2852
  report.repository = repositoryOf(packument);
2839
2853
  report.latest = latest === undefined ? undefined : { version: latest, published: packument.time?.[latest] };
2840
- report.safe = safeRelease(packument, now, minAgeDays);
2854
+ const trust = trustedBy(spec.name, input.trusted ?? DEFAULT_TRUSTED);
2855
+ report.safe = safeRelease(packument, now, trust === undefined ? minAgeDays : 0);
2841
2856
  report.version = version;
2842
2857
  report.published = version === undefined ? undefined : packument.time?.[version];
2843
2858
  report.installScripts = installScriptsOf(entry);
@@ -2851,11 +2866,13 @@ function analyze(input) {
2851
2866
  const kept = new Set(report.advisories.map((each) => each.id));
2852
2867
  report.fixes = report.safe?.version === version ? [] : (input.safeAdvisories ?? []).filter((each) => !kept.has(each.id));
2853
2868
  if (age === undefined) {
2854
- const isBlocking = minAgeDays > 0 && registry === PUBLIC_HOST;
2869
+ const isBlocking = minAgeDays > 0 && registry === PUBLIC_HOST && trust === undefined;
2855
2870
  report.blocked ||= isBlocking;
2856
2871
  report.warnings.push(`${registry} gives no publish date for ${version}, its age cannot be checked${isBlocking ? " and it is refused" : ""}`);
2857
2872
  } else if (age < minAgeDays && report.fixes.length > 0) {
2858
2873
  report.warnings.push(`${version} was published ${daysAgo(age)} ago, under ${minAgeDays} days, but it fixes ${report.fixes.map((each) => `${each.ghsa ?? each.id} (${each.severity})`).join(", ")} that ${report.safe?.version} has, so its age does not hold it back`);
2874
+ } else if (age < minAgeDays && trust !== undefined) {
2875
+ report.warnings.push(`${version} was published ${daysAgo(age)} ago, under ${minAgeDays} days, its age not checked as ${trust} is trusted`);
2859
2876
  } else if (age < minAgeDays && registry !== PUBLIC_HOST) {
2860
2877
  report.warnings.push(`${version} was published ${daysAgo(age)} ago, under ${minAgeDays} days, not held back on ${registry}`);
2861
2878
  } else if (age < minAgeDays) {
@@ -2887,7 +2904,7 @@ function analyze(input) {
2887
2904
  }
2888
2905
  return report;
2889
2906
  }
2890
- var DEFAULT_MIN_AGE_DAYS = 14, DAY = 86400000, PUBLIC_HOST, INSTALL_SCRIPTS, GIT_PREFIX, VERSION_SEPARATOR;
2907
+ var DEFAULT_MIN_AGE_DAYS = 14, DEFAULT_TRUSTED, DAY = 86400000, PUBLIC_HOST, INSTALL_SCRIPTS, GIT_PREFIX, VERSION_SEPARATOR;
2891
2908
  var init_analyze = __esm(() => {
2892
2909
  init_text();
2893
2910
  init_advisories();
@@ -2895,6 +2912,7 @@ var init_analyze = __esm(() => {
2895
2912
  init_nearmiss();
2896
2913
  init_registry();
2897
2914
  init_semver();
2915
+ DEFAULT_TRUSTED = ["@kaynooo/*", "@kayjs/*"];
2898
2916
  PUBLIC_HOST = new URL(PUBLIC_REGISTRY).host;
2899
2917
  INSTALL_SCRIPTS = ["preinstall", "install", "postinstall"];
2900
2918
  GIT_PREFIX = /^git\+/;
@@ -3024,6 +3042,8 @@ function parseSettings(text) {
3024
3042
  settings.minAge = raw.minAge;
3025
3043
  if (typeof raw?.concurrency === "number" && Number.isInteger(raw.concurrency) && raw.concurrency > 0)
3026
3044
  settings.concurrency = raw.concurrency;
3045
+ if (Array.isArray(raw?.trusted) && raw.trusted.every((each) => typeof each === "string"))
3046
+ settings.trusted = raw.trusted;
3027
3047
  return settings;
3028
3048
  }
3029
3049
  async function loadSettings(cwd = process20.cwd(), home) {
@@ -3041,6 +3061,7 @@ async function loadSettings(cwd = process20.cwd(), home) {
3041
3061
  settings.blockSeverity ??= found.blockSeverity;
3042
3062
  settings.minAge ??= found.minAge;
3043
3063
  settings.concurrency ??= found.concurrency;
3064
+ settings.trusted ??= found.trusted;
3044
3065
  }
3045
3066
  return settings;
3046
3067
  }
@@ -3278,7 +3299,7 @@ async function checkPackage(text, context) {
3278
3299
  const registry = registryFor(spec.name, context.configs);
3279
3300
  const packument = await fetchPackument(spec.name, registry, context.fetcher);
3280
3301
  const downloads = packument !== undefined && isPublic(registry) ? await fetchDownloads(spec.name, context.fetcher) : undefined;
3281
- const input = { spec, packument, downloads, registry: new URL(registry.url).host, now: context.now, minAgeDays: context.minAgeDays, licenses: context.settings?.licenses };
3302
+ const input = { spec, packument, downloads, registry: new URL(registry.url).host, now: context.now, minAgeDays: context.minAgeDays, licenses: context.settings?.licenses, trusted: context.settings?.trusted };
3282
3303
  const report = analyze(input);
3283
3304
  const blockSeverity = context.settings?.blockSeverity ?? DEFAULT_BLOCK_SEVERITY;
3284
3305
  if (report.version === undefined)
@@ -3345,7 +3366,7 @@ ${usage2}` : `${values.from} has no registry dependency to check`);
3345
3366
  }
3346
3367
  var usage2 = `pkg [check] <name>[@version]... | --from <package.json> | -
3347
3368
 
3348
- Checks npm packages before they are added: the name exists, the newest version at least two weeks old, downloads, licence, install scripts, a near miss of a popular name, and a changed publisher or lost provenance attestation.
3369
+ Checks npm packages before they are added: the name exists, the newest version at least two weeks old (any age for the \`trusted\` setting's names, \`@kaynooo/*\` and \`@kayjs/*\` by default), downloads, licence, install scripts, a near miss of a popular name, and a changed publisher or lost provenance attestation.
3349
3370
  Names come from the arguments, from a package.json with --from, or from stdin.
3350
3371
  A \`.pkgrc.json\` with \`licenses\` refuses a licence outside the list, and \`blockSeverity\` a version with an advisory at that level or above.`, EXITS, options, WORDS;
3351
3372
  var init_check = __esm(() => {
@@ -3449,7 +3470,7 @@ async function auditPackages(packages, context) {
3449
3470
  };
3450
3471
  const looked = await pool(packages, context.settings.concurrency ?? BATCH, async ({ name, version }) => {
3451
3472
  const registry = registryFor(name, context.configs);
3452
- const input = { spec: { name, request: version }, packument: await lookup(name, registry), registry: new URL(registry.url).host, now: context.now, minAgeDays: context.minAgeDays, licenses: context.settings.licenses };
3473
+ const input = { spec: { name, request: version }, packument: await lookup(name, registry), registry: new URL(registry.url).host, now: context.now, minAgeDays: context.minAgeDays, licenses: context.settings.licenses, trusted: context.settings.trusted };
3453
3474
  return { name, version, registry, input, safe: analyze(input).safe?.version };
3454
3475
  }, (done, total) => context.onProgress?.({ step: "looking up", done, total, unit: "packages" }));
3455
3476
  context.onProgress?.({ step: "reading the advisories" });
@@ -3580,7 +3601,7 @@ async function outdated(manifest, context) {
3580
3601
  warn(`${name} is not on ${new URL(registry.url).host}`);
3581
3602
  return;
3582
3603
  }
3583
- const { safe, latest } = analyze({ spec: { name }, packument, registry: new URL(registry.url).host, now: context.now, minAgeDays: context.minAgeDays });
3604
+ const { safe, latest } = analyze({ spec: { name }, packument, registry: new URL(registry.url).host, now: context.now, minAgeDays: context.minAgeDays, trusted: context.trusted });
3584
3605
  const current = currentOf(name, spec, context.locked);
3585
3606
  if (safe === undefined || current === undefined || Bun.semver.order(safe.version, current) <= 0)
3586
3607
  return;
@@ -3598,7 +3619,7 @@ async function outdatedCommand(args) {
3598
3619
  const lockPath = join10(folder, "bun.lock");
3599
3620
  const locked = existsSync5(lockPath) ? parseBunLock(await Bun.file(lockPath).text()) : [];
3600
3621
  const settings = await loadSettings(folder);
3601
- const rows = await outdated(await manifest.text(), { minAgeDays: wholeNumber(values["min-age"], "min-age", settings.minAge ?? DEFAULT_MIN_AGE_DAYS, 0), concurrency: settings.concurrency, locked });
3622
+ const rows = await outdated(await manifest.text(), { minAgeDays: wholeNumber(values["min-age"], "min-age", settings.minAge ?? DEFAULT_MIN_AGE_DAYS, 0), concurrency: settings.concurrency, locked, trusted: settings.trusted });
3602
3623
  const { shown, footer } = capped(rows, limitOf(values, OUTDATED_LIMIT));
3603
3624
  if (values.json === true)
3604
3625
  printJson(shown);
@@ -3611,7 +3632,7 @@ async function outdatedCommand(args) {
3611
3632
  }
3612
3633
  var usage4 = `pkg outdated [dir]
3613
3634
 
3614
- Lists the dependencies of the package.json of the folder that have a newer version safe to take: the highest stable one at least --min-age days old, the \`safe\` of \`pkg check\`.
3635
+ Lists the dependencies of the package.json of the folder that have a newer version safe to take: the highest stable one at least --min-age days old (any age for a \`trusted\` name), the \`safe\` of \`pkg check\`.
3615
3636
  It reads only the registry's metadata, no advisories, and changes nothing: \`bun add --exact name@version\` adopts a row.`, EXITS3, OUTDATED_LIMIT = 50, BATCH2 = 8, FIRST_VERSION, DOT, options3;
3616
3637
  var init_outdated = __esm(() => {
3617
3638
  init_src();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-pkg",
3
3
  "type": "module",
4
- "version": "0.17.4",
4
+ "version": "0.18.1",
5
5
  "description": "pkg: check an npm package before it is added, for a guessed name, a version under two weeks old and install scripts.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  "registry": "https://git.kaynooo.fr/api/packages/kaynooo/npm/"
25
25
  },
26
26
  "scripts": {
27
- "build": "$npm_execpath build src/cli.ts --target bun --outdir dist",
27
+ "build": "$npm_execpath build src/cli.ts --target bun --outdir dist && $npm_execpath ../../scripts/shell-shebang.ts",
28
28
  "pkg": "$npm_execpath src/cli.ts",
29
29
  "popular": "$npm_execpath scripts/popular.ts",
30
30
  "record": "$npm_execpath scripts/record.ts"
@@ -33,6 +33,6 @@
33
33
  "@kaynooo/utils": "3.1.0"
34
34
  },
35
35
  "devDependencies": {
36
- "@kaynooo/cli-kit": "0.46.1"
36
+ "@kaynooo/cli-kit": "0.46.4"
37
37
  }
38
38
  }