@fluid-app/fluid-cli 0.1.738 → 0.1.740

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,4 +1,4 @@
1
- import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
1
+ import { closeSync, existsSync, linkSync, mkdirSync, openSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
2
2
  import { dirname, join } from "node:path";
3
3
  import { randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
4
4
  import { homedir } from "node:os";
@@ -2169,6 +2169,135 @@ function createKeyringSecureStorage(options) {
2169
2169
  };
2170
2170
  }
2171
2171
  //#endregion
2172
+ //#region src/config/lock.ts
2173
+ /** The config lock could not be acquired in time. */
2174
+ var ConfigLockTimeoutError = class extends Error {
2175
+ constructor(lockPath) {
2176
+ super(`The config lock ${lockPath} is held by another fluid process. If no fluid command is running, remove that file and try again.`);
2177
+ this.lockPath = lockPath;
2178
+ this.name = "ConfigLockTimeoutError";
2179
+ }
2180
+ };
2181
+ const configLockTimings = {
2182
+ timeoutMs: 5e3,
2183
+ staleMs: 3e4,
2184
+ breakStaleMs: 5e3
2185
+ };
2186
+ const lockIo = { write: writeSync };
2187
+ const RETRY_MS = 25;
2188
+ function sleep(ms) {
2189
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
2190
+ }
2191
+ function ageMs(path) {
2192
+ try {
2193
+ return Date.now() - statSync(path).mtimeMs;
2194
+ } catch {
2195
+ return -1;
2196
+ }
2197
+ }
2198
+ function isStale(path, staleMs) {
2199
+ return ageMs(path) > staleMs;
2200
+ }
2201
+ function holderAlive(content) {
2202
+ const pid = Number(/:(\d+)$/.exec(content)?.[1]);
2203
+ if (!Number.isInteger(pid) || pid <= 0) return false;
2204
+ try {
2205
+ process.kill(pid, 0);
2206
+ return true;
2207
+ } catch (error) {
2208
+ return error.code === "EPERM";
2209
+ }
2210
+ }
2211
+ function hasHolderPid(content) {
2212
+ return /:\d+$/.test(content);
2213
+ }
2214
+ function isLockStale(path) {
2215
+ if (ageMs(path) <= configLockTimings.staleMs) return false;
2216
+ try {
2217
+ const content = readFileSync(path, "utf8");
2218
+ return !hasHolderPid(content) || !holderAlive(content);
2219
+ } catch {
2220
+ return false;
2221
+ }
2222
+ }
2223
+ function tryCreate(path, content) {
2224
+ const tmp = `${path}.${process.pid}.${randomUUID()}.tmp`;
2225
+ try {
2226
+ const fd = openSync(tmp, "wx", 384);
2227
+ try {
2228
+ lockIo.write(fd, content);
2229
+ } finally {
2230
+ closeSync(fd);
2231
+ }
2232
+ try {
2233
+ linkSync(tmp, path);
2234
+ return true;
2235
+ } catch {
2236
+ return false;
2237
+ }
2238
+ } finally {
2239
+ try {
2240
+ unlinkSync(tmp);
2241
+ } catch {}
2242
+ }
2243
+ }
2244
+ function newContent() {
2245
+ return `${randomUUID()}:${process.pid}`;
2246
+ }
2247
+ function release(path, content) {
2248
+ try {
2249
+ if (readFileSync(path, "utf8") === content) unlinkSync(path);
2250
+ } catch {}
2251
+ }
2252
+ function breakStale(lockPath) {
2253
+ const breakPath = `${lockPath}.break`;
2254
+ const token = newContent();
2255
+ if (!tryCreate(breakPath, token)) {
2256
+ if (isStale(breakPath, configLockTimings.breakStaleMs)) try {
2257
+ unlinkSync(breakPath);
2258
+ } catch {}
2259
+ return;
2260
+ }
2261
+ try {
2262
+ if (isLockStale(lockPath)) try {
2263
+ unlinkSync(lockPath);
2264
+ } catch {}
2265
+ } finally {
2266
+ release(breakPath, token);
2267
+ }
2268
+ }
2269
+ /**
2270
+ * Runs `fn` while holding an exclusive lock file next to the config, so
2271
+ * concurrent no-replace profile saves cannot both succeed (other
2272
+ * `updateConfig` callers do not take this lock). A lock
2273
+ * older than the stale timeout is treated as abandoned, and removed under a
2274
+ * second short-lived lock so only one contender breaks it, only when its
2275
+ * holder process is gone or it records no holder.
2276
+ *
2277
+ * @param fn [() => T] Synchronous work to run under the lock.
2278
+ * @return [T] The result of `fn`.
2279
+ * @raise [ConfigLockTimeoutError] When the lock stays held past the timeout.
2280
+ */
2281
+ function withConfigLock(fn) {
2282
+ const lockPath = `${getConfigFilePath()}.lock`;
2283
+ mkdirSync(dirname(lockPath), {
2284
+ recursive: true,
2285
+ mode: 448
2286
+ });
2287
+ const token = newContent();
2288
+ const deadline = Date.now() + configLockTimings.timeoutMs;
2289
+ while (!tryCreate(lockPath, token)) {
2290
+ if (isLockStale(lockPath)) breakStale(lockPath);
2291
+ if (Date.now() >= deadline) throw new ConfigLockTimeoutError(lockPath);
2292
+ sleep(RETRY_MS);
2293
+ }
2294
+ try {
2295
+ return fn();
2296
+ } finally {
2297
+ release(lockPath, token);
2298
+ }
2299
+ }
2300
+ //#endregion
2172
2301
  //#region src/auth/identity/environment.ts
2173
2302
  /**
2174
2303
  * Which identity service the CLI signs in with, following the API base the
@@ -2399,18 +2528,20 @@ async function saveClientProfile(name, input, options = {}) {
2399
2528
  let before;
2400
2529
  let after;
2401
2530
  try {
2402
- before = readConfig();
2403
- after = updateConfig((config) => {
2404
- if (options.replace === false && config.profiles[name]) throw new ProfileExistsError(name);
2405
- return {
2406
- ...config,
2407
- activeProfile: config.activeProfile ?? name,
2408
- profiles: {
2409
- ...config.profiles,
2410
- [name]: profile
2411
- }
2412
- };
2413
- });
2531
+ const write = () => {
2532
+ return [readConfig(), updateConfig((config) => {
2533
+ if (options.replace === false && config.profiles[name]) throw new ProfileExistsError(name);
2534
+ return {
2535
+ ...config,
2536
+ activeProfile: config.activeProfile ?? name,
2537
+ profiles: {
2538
+ ...config.profiles,
2539
+ [name]: profile
2540
+ }
2541
+ };
2542
+ })];
2543
+ };
2544
+ [before, after] = options.replace === false ? withConfigLock(write) : write();
2414
2545
  } catch (error) {
2415
2546
  try {
2416
2547
  await storage.delete(storageKey);
@@ -2934,4 +3065,4 @@ async function endUnusedSessions(before, after) {
2934
3065
  //#endregion
2935
3066
  export { isClientProfile as A, readStoredConfig as C, getConfigDir as D, isStrictConfig as E, getConfigFilePath as O, readConfig as S, writeConfig as T, primeClientProfile as _, peekIdentityToken as a, ApiError as b, sessionFacts as c, OrphanedClientSecretError as d, ProfileExistsError as f, peekClientToken as g, invalidateClientToken as h, isRoleNotGrantedError as i, isIdentityProfile as j, createDefaultConfig as k, signInWithBrowser as l, deleteClientSecret as m, describeIdentityError as n, primeIdentityProfile as o, clientAccessToken as p, identityToken as r, profileAccessToken as s, endUnusedSessions as t, signOutIdentity as u, saveClientProfile as v, updateConfig as w, createFetchClient as x, resolveIdentityUrl as y };
2936
3067
 
2937
- //# sourceMappingURL=profiles-DmgZsxMS.mjs.map
3068
+ //# sourceMappingURL=profiles-BRMp_Rr8.mjs.map