@fluid-app/fluid-cli 0.1.353 → 0.1.355

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,9 +1,10 @@
1
- import { existsSync, mkdirSync, readFileSync, realpathSync, renameSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
1
+ import { closeSync, constants, existsSync, fstatSync, mkdirSync, openSync, readFileSync, readdirSync, realpathSync, renameSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
2
2
  import { createHash, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
3
3
  import { execFile } from "node:child_process";
4
4
  import { createServer } from "node:http";
5
5
  import { dirname, isAbsolute, join, sep } from "node:path";
6
6
  import { homedir } from "node:os";
7
+ import { AsyncLocalStorage } from "node:async_hooks";
7
8
  //#region src/catalog/annotations.ts
8
9
  const ANNOTATION = Symbol.for("@fluid-app/fluid-cli.command-annotation");
9
10
  /**
@@ -3336,7 +3337,8 @@ const resolvers = /* @__PURE__ */ new WeakMap();
3336
3337
  * Declares the local inputs a command's action reads.
3337
3338
  *
3338
3339
  * command - the leaf command whose action reads the inputs
3339
- * resolver - maps commander's parsed opts and args to the input specs
3340
+ * resolver - maps commander's parsed opts and args to the input specs; it
3341
+ * must be a pure function of opts and args and must not touch the filesystem
3340
3342
  *
3341
3343
  * Returns the command.
3342
3344
  */
@@ -3350,9 +3352,10 @@ function getInputResolver(command) {
3350
3352
  //#endregion
3351
3353
  //#region src/inputs/scope.ts
3352
3354
  let internalDepth = 0;
3355
+ const asyncScope = new AsyncLocalStorage();
3353
3356
  /** True while the inputs implementation itself is touching the filesystem. */
3354
3357
  function inInternalRead() {
3355
- return internalDepth > 0;
3358
+ return internalDepth > 0 || asyncScope.getStore() === true;
3356
3359
  }
3357
3360
  function withInternalReads(fn) {
3358
3361
  internalDepth++;
@@ -3362,6 +3365,13 @@ function withInternalReads(fn) {
3362
3365
  internalDepth--;
3363
3366
  }
3364
3367
  }
3368
+ /**
3369
+ * Async form of withInternalReads. The scope follows fn's own async
3370
+ * continuations only; unrelated concurrent work stays guarded.
3371
+ */
3372
+ async function withInternalReadsAsync(fn) {
3373
+ return asyncScope.run(true, fn);
3374
+ }
3365
3375
  //#endregion
3366
3376
  //#region src/inputs/paths.ts
3367
3377
  function lexicalPath(path, cwd = process.cwd()) {
@@ -3380,39 +3390,89 @@ function canonicalPath(path, cwd = process.cwd()) {
3380
3390
  };
3381
3391
  }
3382
3392
  //#endregion
3383
- //#region src/inputs/reader.ts
3384
- let activeSnapshot = null;
3385
- function setActiveSnapshot(snapshot) {
3386
- activeSnapshot = snapshot;
3393
+ //#region src/inputs/tree.ts
3394
+ function normalizeIgnore(ignore) {
3395
+ const out = /* @__PURE__ */ new Set();
3396
+ for (const pattern of ignore ?? []) {
3397
+ const segments = pattern.split("/");
3398
+ if (pattern === "" || /[*?[\]{}]/.test(pattern) || segments.some((s) => s === "" || s === "." || s === "..")) throw new Error(`Invalid tree ignore pattern: ${JSON.stringify(pattern)}`);
3399
+ out.add(pattern);
3400
+ }
3401
+ return [...out].sort(compare);
3387
3402
  }
3388
- var InputsUndeclaredError = class extends Error {
3389
- constructor(path) {
3390
- super(`Undeclared input read: ${path}`);
3391
- this.name = "InputsUndeclaredError";
3403
+ function ignoreKey(ignore) {
3404
+ return JSON.stringify(ignore);
3405
+ }
3406
+ function compare(a, b) {
3407
+ return a < b ? -1 : a > b ? 1 : 0;
3408
+ }
3409
+ /**
3410
+ * The single ignore matcher for tree inputs, used by resolve, expect and
3411
+ * default mode alike. A pattern without "/" matches an entry's basename at
3412
+ * any depth; a pattern with "/" matches a root-relative path. A match on a
3413
+ * directory also excludes every descendant.
3414
+ *
3415
+ * rel - "/"-separated path relative to the tree root
3416
+ * ignore - patterns from normalizeIgnore
3417
+ *
3418
+ * Returns true when rel or any ancestor directory of rel is ignored.
3419
+ */
3420
+ function isIgnored(rel, ignore) {
3421
+ const segments = rel.split("/");
3422
+ let prefix = "";
3423
+ for (const segment of segments) {
3424
+ prefix = prefix === "" ? segment : `${prefix}/${segment}`;
3425
+ if (ignore.includes(segment) || ignore.includes(prefix)) return true;
3392
3426
  }
3393
- };
3394
- function declaredKey(path, snapshot) {
3395
- const lexical = lexicalPath(path);
3396
- const key = snapshot.aliases.get(lexical) ?? lexical;
3397
- return snapshot.files.has(key) ? key : void 0;
3427
+ return false;
3398
3428
  }
3399
3429
  /**
3400
- * The reader behind ctx.inputs. Outside expect mode it reads from disk; once
3401
- * an expect-mode snapshot is active it serves only that snapshot and throws
3402
- * for anything undeclared.
3430
+ * Lists the regular files under an already canonical root as sorted
3431
+ * "/"-separated relative paths. Symlink entries are skipped at list time in
3432
+ * every mode. Throws if a directory being walked is not its own canonical
3433
+ * path.
3403
3434
  */
3404
- function createInputsReader() {
3405
- return { readFile(path, encoding) {
3406
- const snapshot = activeSnapshot;
3407
- if (!snapshot) {
3408
- const content = withInternalReads(() => readFileSync(lexicalPath(path)));
3409
- return encoding ? content.toString(encoding) : content;
3435
+ function listTreeFiles(root, ignore) {
3436
+ const out = [];
3437
+ const walk = (dir, relDir) => {
3438
+ assertCanonicalDir(dir);
3439
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
3440
+ const rel = relDir === "" ? entry.name : `${relDir}/${entry.name}`;
3441
+ if (isIgnored(rel, ignore)) continue;
3442
+ if (entry.isDirectory()) walk(join(dir, entry.name), rel);
3443
+ else if (entry.isFile()) out.push(rel);
3410
3444
  }
3411
- const key = declaredKey(path, snapshot);
3412
- const content = key === void 0 ? void 0 : snapshot.files.get(key);
3413
- if (!content) throw new InputsUndeclaredError(path);
3414
- return encoding ? content.toString(encoding) : Buffer.from(content);
3415
- } };
3445
+ };
3446
+ withInternalReads(() => walk(root, ""));
3447
+ return out.sort(compare);
3448
+ }
3449
+ function assertCanonicalDir(dir) {
3450
+ if (realpathSync.native(dir) !== dir) throw new Error(`Tree directory changed during read: ${dir}`);
3451
+ }
3452
+ /**
3453
+ * Reads one tree member from a canonical root. Rejects a non-canonical parent
3454
+ * directory and a symlinked or non-regular final component (O_NOFOLLOW).
3455
+ * Known residual: a parent directory swapped between the canonical check and
3456
+ * the open is not detected; it needs a local writer to the tree during the
3457
+ * command, and Node has no openat2.
3458
+ */
3459
+ function readTreeFileFromDisk(root, rel) {
3460
+ return withInternalReads(() => {
3461
+ const path = join(root, rel);
3462
+ assertCanonicalDir(join(root, dirname(rel)));
3463
+ const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
3464
+ try {
3465
+ if (!fstatSync(fd).isFile()) throw new Error(`Tree member is not a regular file: ${path}`);
3466
+ return readFileSync(fd);
3467
+ } finally {
3468
+ closeSync(fd);
3469
+ }
3470
+ });
3471
+ }
3472
+ //#endregion
3473
+ //#region src/inputs/snapshot.ts
3474
+ function treeKey(root, ignore) {
3475
+ return `${root}\0${ignoreKey(ignore)}`;
3416
3476
  }
3417
3477
  function sha256(data) {
3418
3478
  return createHash("sha256").update(data).digest("hex");
@@ -3421,15 +3481,20 @@ function resolveInputs(specs, cwd = process.cwd()) {
3421
3481
  return withInternalReads(() => {
3422
3482
  const snapshot = {
3423
3483
  files: /* @__PURE__ */ new Map(),
3424
- aliases: /* @__PURE__ */ new Map()
3484
+ aliases: /* @__PURE__ */ new Map(),
3485
+ trees: /* @__PURE__ */ new Map(),
3486
+ treeAliases: /* @__PURE__ */ new Map(),
3487
+ refs: /* @__PURE__ */ new Map()
3425
3488
  };
3426
3489
  const entries = [];
3427
- for (const spec of specs) if (spec.kind === "ref") entries.push({
3428
- kind: "ref",
3429
- name: spec.name,
3430
- value: spec.value
3431
- });
3432
- else if (spec.kind === "file" || spec.kind === "config") {
3490
+ for (const spec of specs) if (spec.kind === "ref") {
3491
+ snapshot.refs.set(spec.name, spec.value);
3492
+ entries.push({
3493
+ kind: "ref",
3494
+ name: spec.name,
3495
+ value: spec.value
3496
+ });
3497
+ } else if (spec.kind === "file" || spec.kind === "config") {
3433
3498
  const { lexical, canonical: path } = canonicalPath(spec.path, cwd);
3434
3499
  const content = readFileSync(path);
3435
3500
  snapshot.aliases.set(lexical, path);
@@ -3441,7 +3506,31 @@ function resolveInputs(specs, cwd = process.cwd()) {
3441
3506
  sha256: sha256(content),
3442
3507
  size: content.length
3443
3508
  });
3444
- } else throw new Error("tree inputs not supported yet");
3509
+ } else {
3510
+ if (spec.kind !== "tree" || typeof spec.path !== "string") throw new Error("Invalid tree input spec");
3511
+ const { lexical, canonical: root } = canonicalPath(spec.path, cwd);
3512
+ const ignore = normalizeIgnore(spec.ignore);
3513
+ const members = /* @__PURE__ */ new Map();
3514
+ const files = [];
3515
+ for (const rel of listTreeFiles(root, ignore)) {
3516
+ const content = readTreeFileFromDisk(root, rel);
3517
+ members.set(rel, content);
3518
+ files.push({
3519
+ path: rel,
3520
+ sha256: sha256(content),
3521
+ size: content.length
3522
+ });
3523
+ }
3524
+ snapshot.treeAliases.set(lexical, root);
3525
+ snapshot.treeAliases.set(root, root);
3526
+ snapshot.trees.set(treeKey(root, ignore), members);
3527
+ entries.push({
3528
+ kind: "tree",
3529
+ path: root,
3530
+ ignore,
3531
+ files
3532
+ });
3533
+ }
3445
3534
  return {
3446
3535
  entries,
3447
3536
  snapshot
@@ -3449,7 +3538,7 @@ function resolveInputs(specs, cwd = process.cwd()) {
3449
3538
  });
3450
3539
  }
3451
3540
  function sortKey(entry) {
3452
- const id = entry.kind === "ref" ? entry.name : entry.path;
3541
+ const id = entry.kind === "ref" ? entry.name : entry.kind === "tree" ? `${entry.path}\0${ignoreKey(entry.ignore)}` : entry.path;
3453
3542
  return `${entry.kind}\0${id}`;
3454
3543
  }
3455
3544
  function canonicalEntries(entries) {
@@ -3459,6 +3548,16 @@ function canonicalEntries(entries) {
3459
3548
  name: e.name,
3460
3549
  value: e.value
3461
3550
  };
3551
+ if (e.kind === "tree") return {
3552
+ kind: "tree",
3553
+ path: e.path,
3554
+ ignore: [...e.ignore],
3555
+ files: e.files.map((f) => ({
3556
+ path: f.path,
3557
+ sha256: f.sha256,
3558
+ size: f.size
3559
+ }))
3560
+ };
3462
3561
  return {
3463
3562
  kind: e.kind,
3464
3563
  path: e.path,
@@ -3479,6 +3578,100 @@ function manifestJson(command, entries) {
3479
3578
  });
3480
3579
  }
3481
3580
  //#endregion
3581
+ //#region src/inputs/reader.ts
3582
+ let activeSnapshot = null;
3583
+ function setActiveSnapshot(snapshot) {
3584
+ activeSnapshot = snapshot;
3585
+ }
3586
+ var InputsUndeclaredError = class extends Error {
3587
+ constructor(path) {
3588
+ super(`Undeclared input read: ${path}`);
3589
+ this.name = "InputsUndeclaredError";
3590
+ }
3591
+ };
3592
+ function declaredKey(path, snapshot) {
3593
+ const lexical = lexicalPath(path);
3594
+ const key = snapshot.aliases.get(lexical) ?? lexical;
3595
+ return snapshot.files.has(key) ? key : void 0;
3596
+ }
3597
+ function bytes(content, encoding) {
3598
+ return encoding ? content.toString(encoding) : Buffer.from(content);
3599
+ }
3600
+ function snapshotTree(rootPath, ignore, snapshot) {
3601
+ const root = snapshot.treeAliases.get(lexicalPath(rootPath)) ?? rootPath;
3602
+ const members = snapshot.trees.get(treeKey(root, ignore));
3603
+ if (!members) throw new InputsUndeclaredError(rootPath);
3604
+ return {
3605
+ root,
3606
+ members
3607
+ };
3608
+ }
3609
+ function openTree(rootPath, options) {
3610
+ const ignore = normalizeIgnore(options?.ignore);
3611
+ let disk;
3612
+ const fromDisk = () => {
3613
+ if (!disk) {
3614
+ const root = canonicalPath(rootPath).canonical;
3615
+ disk = {
3616
+ root,
3617
+ known: new Set(listTreeFiles(root, ignore))
3618
+ };
3619
+ }
3620
+ return disk;
3621
+ };
3622
+ if (activeSnapshot) snapshotTree(rootPath, ignore, activeSnapshot);
3623
+ else fromDisk();
3624
+ return {
3625
+ get root() {
3626
+ return activeSnapshot ? snapshotTree(rootPath, ignore, activeSnapshot).root : fromDisk().root;
3627
+ },
3628
+ list() {
3629
+ if (activeSnapshot) return [...snapshotTree(rootPath, ignore, activeSnapshot).members.keys()];
3630
+ return [...fromDisk().known];
3631
+ },
3632
+ readFile(rel, encoding) {
3633
+ const missing = () => new InputsUndeclaredError(`${rootPath}/${rel}`);
3634
+ if (activeSnapshot) {
3635
+ const content = snapshotTree(rootPath, ignore, activeSnapshot).members.get(rel);
3636
+ if (!content) throw missing();
3637
+ return bytes(content, encoding);
3638
+ }
3639
+ const { root, known } = fromDisk();
3640
+ if (!known.has(rel)) throw missing();
3641
+ return bytes(readTreeFileFromDisk(root, rel), encoding);
3642
+ }
3643
+ };
3644
+ }
3645
+ /**
3646
+ * The reader behind ctx.inputs. Outside expect mode it reads from disk; once
3647
+ * an expect-mode snapshot is active it serves only that snapshot and throws
3648
+ * for anything undeclared.
3649
+ */
3650
+ function createInputsReader() {
3651
+ return {
3652
+ isExpecting: () => activeSnapshot !== null,
3653
+ ref(name) {
3654
+ const snapshot = activeSnapshot;
3655
+ if (!snapshot) return void 0;
3656
+ const value = snapshot.refs.get(name);
3657
+ if (value === void 0) throw new InputsUndeclaredError(`ref ${name}`);
3658
+ return value;
3659
+ },
3660
+ tree: openTree,
3661
+ readFile(path, encoding) {
3662
+ const snapshot = activeSnapshot;
3663
+ if (!snapshot) {
3664
+ const content = withInternalReads(() => readFileSync(lexicalPath(path)));
3665
+ return encoding ? content.toString(encoding) : content;
3666
+ }
3667
+ const key = declaredKey(path, snapshot);
3668
+ const content = key === void 0 ? void 0 : snapshot.files.get(key);
3669
+ if (!content) throw new InputsUndeclaredError(path);
3670
+ return encoding ? content.toString(encoding) : Buffer.from(content);
3671
+ }
3672
+ };
3673
+ }
3674
+ //#endregion
3482
3675
  //#region src/inputs/hook.ts
3483
3676
  const INPUTS_MODE_ENV = "FLUID_INPUTS_MODE";
3484
3677
  const INPUTS_EXPECT_ENV = "FLUID_INPUTS_EXPECT";
@@ -3579,6 +3772,6 @@ function exitOnInputsError(error, io = processIo) {
3579
3772
  io.exit(64);
3580
3773
  }
3581
3774
  //#endregion
3582
- export { mapResult as $, signInWithBrowser as A, getFluidApiBase as B, getConfigDir as C, primeIdentityProfile as D, identityToken as E, isIdentityProfile as F, validateToken as G, sendMfa as H, FLUID_API_ERROR as I, isError as J, failure as K, SIGNUP_ERROR as L, resolveIdentityUrl as M, createFetchClient as N, profileAccessToken as O, createDefaultConfig as P, mapError as Q, confirmMfa as R, isStrictConfig as S, describeIdentityError as T, signup as U, redeemIdentityLink as V, switchCompany as W, isNodeError as X, isFailure as Y, isSuccess as Z, resolveCredentialSource as _, INPUTS_UNDECLARED_CODE as a, filterCatalog as at, updateConfig as b, inputsModeActive as c, visibleSubcommands as ct, inInternalRead as d, success as et, declareInputs as f, listProfileNames as g, getAuthTokenAsync as h, INPUTS_MODE_ENV as i, unwrapOr as it, signOutIdentity as j, sessionFacts as k, registerInputsHook as l, walkCommands as lt, getAuthToken as m, INPUTS_MISMATCH_CODE as n, tryCatchAsync as nt, INPUTS_UNDECLARED_EXIT as o, rootCommand as ot, getActiveProfile as p, getErrorMessage$1 as q, INPUTS_MISMATCH_EXIT as r, unwrap as rt, exitOnInputsError as s, visibleOptions as st, INPUTS_EXPECT_ENV as t, tryCatch as tt, createInputsReader as u, describeCommand as ut, findProjectConfig as v, getConfigFilePath as w, writeConfig as x, readConfig as y, fetchUserCompanies as z };
3775
+ export { isNodeError as $, primeIdentityProfile as A, SIGNUP_ERROR as B, updateConfig as C, getConfigFilePath as D, getConfigDir as E, resolveIdentityUrl as F, sendMfa as G, fetchUserCompanies as H, createFetchClient as I, validateToken as J, signup as K, createDefaultConfig as L, sessionFacts as M, signInWithBrowser as N, describeIdentityError as O, signOutIdentity as P, isFailure as Q, isIdentityProfile as R, readConfig as S, isStrictConfig as T, getFluidApiBase as U, confirmMfa as V, redeemIdentityLink as W, getErrorMessage$1 as X, failure as Y, isError as Z, getAuthToken as _, INPUTS_UNDECLARED_CODE as a, tryCatchAsync as at, resolveCredentialSource as b, inputsModeActive as c, filterCatalog as ct, setActiveSnapshot as d, visibleSubcommands as dt, isSuccess as et, inInternalRead as f, walkCommands as ft, getActiveProfile as g, declareInputs as h, INPUTS_MODE_ENV as i, tryCatch as it, profileAccessToken as j, identityToken as k, registerInputsHook as l, rootCommand as lt, withInternalReadsAsync as m, INPUTS_MISMATCH_CODE as n, mapResult as nt, INPUTS_UNDECLARED_EXIT as o, unwrap as ot, withInternalReads as p, describeCommand as pt, switchCompany as q, INPUTS_MISMATCH_EXIT as r, success as rt, exitOnInputsError as s, unwrapOr as st, INPUTS_EXPECT_ENV as t, mapError as tt, createInputsReader as u, visibleOptions as ut, getAuthTokenAsync as v, writeConfig as w, findProjectConfig as x, listProfileNames as y, FLUID_API_ERROR as z };
3583
3776
 
3584
- //# sourceMappingURL=hook-CZoMQwJE.mjs.map
3777
+ //# sourceMappingURL=hook-LNjPTMl3.mjs.map