@cortexkit/common-auth 0.49.0 → 0.49.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.
- package/dist/fs/canonical-file-path.d.ts +17 -0
- package/dist/fs/canonical-file-path.js +53 -0
- package/dist/fs/index.d.ts +1 -0
- package/dist/fs/index.js +3 -0
- package/dist/settings/fs.d.ts +1 -1
- package/dist/settings/fs.js +1 -0
- package/dist/settings/update.js +3 -18
- package/dist/tui-prefs/watcher.d.ts +8 -1
- package/dist/tui-prefs/watcher.js +9 -2
- package/package.json +1 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type * as syncFs from 'node:fs';
|
|
2
|
+
import type * as fs from 'node:fs/promises';
|
|
3
|
+
type FilePathFs = Pick<typeof fs, 'lstat' | 'readlink' | 'realpath'>;
|
|
4
|
+
type FilePathSyncFs = Pick<typeof syncFs, 'lstatSync' | 'readlinkSync' | 'realpathSync'>;
|
|
5
|
+
/**
|
|
6
|
+
* Canonical path of a file that other processes may replace by write-and-rename.
|
|
7
|
+
*
|
|
8
|
+
* On Linux, Bun's realpath on such a file can answer "<file> (deleted)" (Bun
|
|
9
|
+
* 1.3) or ENOENT (Bun 1.4) during the rename. So this resolves the folder with
|
|
10
|
+
* realpath and appends the name, then follows a file symlink one readlink hop at
|
|
11
|
+
* a time, at most 40 hops. A missing file resolves to its would-be path; a
|
|
12
|
+
* dangling link or a loop throws. Pass `node:fs/promises` (or a test double).
|
|
13
|
+
*/
|
|
14
|
+
export declare function canonicalFilePath(file: string, fs: FilePathFs): Promise<string>;
|
|
15
|
+
/** Synchronous counterpart of canonicalFilePath; pass `node:fs`. */
|
|
16
|
+
export declare function canonicalFilePathSync(file: string, fs: FilePathSyncFs): string;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
2
|
+
const maxSymlinkHops = 40;
|
|
3
|
+
/**
|
|
4
|
+
* Canonical path of a file that other processes may replace by write-and-rename.
|
|
5
|
+
*
|
|
6
|
+
* On Linux, Bun's realpath on such a file can answer "<file> (deleted)" (Bun
|
|
7
|
+
* 1.3) or ENOENT (Bun 1.4) during the rename. So this resolves the folder with
|
|
8
|
+
* realpath and appends the name, then follows a file symlink one readlink hop at
|
|
9
|
+
* a time, at most 40 hops. A missing file resolves to its would-be path; a
|
|
10
|
+
* dangling link or a loop throws. Pass `node:fs/promises` (or a test double).
|
|
11
|
+
*/
|
|
12
|
+
export async function canonicalFilePath(file, fs) {
|
|
13
|
+
let target = file;
|
|
14
|
+
for (let hops = 0;; hops++) {
|
|
15
|
+
// Bun can report an unlinked inode's " (deleted)" name for realpath(file).
|
|
16
|
+
target = join(await fs.realpath(dirname(target)), basename(target));
|
|
17
|
+
let isSymlink;
|
|
18
|
+
try {
|
|
19
|
+
isSymlink = (await fs.lstat(target)).isSymbolicLink();
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (error.code === 'ENOENT' && hops === 0)
|
|
23
|
+
return target;
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
if (!isSymlink)
|
|
27
|
+
return target;
|
|
28
|
+
if (hops >= maxSymlinkHops)
|
|
29
|
+
throw new Error('Too many file symlink hops');
|
|
30
|
+
target = resolve(dirname(target), await fs.readlink(target));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Synchronous counterpart of canonicalFilePath; pass `node:fs`. */
|
|
34
|
+
export function canonicalFilePathSync(file, fs) {
|
|
35
|
+
let target = file;
|
|
36
|
+
for (let hops = 0;; hops++) {
|
|
37
|
+
target = join(fs.realpathSync(dirname(target)), basename(target));
|
|
38
|
+
let isSymlink;
|
|
39
|
+
try {
|
|
40
|
+
isSymlink = fs.lstatSync(target).isSymbolicLink();
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (error.code === 'ENOENT' && hops === 0)
|
|
44
|
+
return target;
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
if (!isSymlink)
|
|
48
|
+
return target;
|
|
49
|
+
if (hops >= maxSymlinkHops)
|
|
50
|
+
throw new Error('Too many file symlink hops');
|
|
51
|
+
target = resolve(dirname(target), fs.readlinkSync(target));
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/fs/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export type { AtomicWriteOptions } from './atomic-write.js';
|
|
2
2
|
export { writeJsonAtomic } from './atomic-write.js';
|
|
3
|
+
export { canonicalFilePath, canonicalFilePathSync, } from './canonical-file-path.js';
|
|
3
4
|
export { WRITER_LOCK_CONSTANTS } from './lock-constants.js';
|
|
4
5
|
export type { PrivateWriteOutcome } from './private-files.js';
|
|
5
6
|
export { PrivateFileError } from './private-files.js';
|
package/dist/fs/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export { writeJsonAtomic } from './atomic-write.js';
|
|
2
|
+
// Canonical path of a file other processes may atomically replace: resolves
|
|
3
|
+
// the folder, never the file itself (see canonical-file-path.ts).
|
|
4
|
+
export { canonicalFilePath, canonicalFilePathSync, } from './canonical-file-path.js';
|
|
2
5
|
export { WRITER_LOCK_CONSTANTS } from './lock-constants.js';
|
|
3
6
|
export { PrivateFileError } from './private-files.js';
|
|
4
7
|
// The try-once lock underneath withLock, for callers that hold a lock across
|
package/dist/settings/fs.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as fs from 'node:fs/promises';
|
|
2
|
-
type SettingsFs = Pick<typeof fs, 'readFile' | 'stat' | 'lstat' | 'realpath' | 'open' | 'rename' | 'link' | 'unlink' | 'mkdir' | 'readdir'>;
|
|
2
|
+
type SettingsFs = Pick<typeof fs, 'readFile' | 'stat' | 'lstat' | 'readlink' | 'realpath' | 'open' | 'rename' | 'link' | 'unlink' | 'mkdir' | 'readdir'>;
|
|
3
3
|
export declare const SettingsFs: SettingsFs;
|
|
4
4
|
/** Internal test hook; omitted methods always use the native implementation. */
|
|
5
5
|
export declare function setSettingsFsForTest(overrides?: Partial<SettingsFs>): void;
|
package/dist/settings/fs.js
CHANGED
package/dist/settings/update.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { dirname } from 'node:path';
|
|
2
|
+
import { canonicalFilePath } from '../fs/canonical-file-path.js';
|
|
2
3
|
import { ensurePrivateDirectory } from '../fs/private-files.js';
|
|
3
4
|
import { withLock } from '../fs/with-lock.js';
|
|
4
5
|
import { deepFreeze } from './descriptors.js';
|
|
@@ -19,25 +20,9 @@ function unavailable(file, cause) {
|
|
|
19
20
|
}
|
|
20
21
|
async function writeTarget(file) {
|
|
21
22
|
try {
|
|
22
|
-
return await
|
|
23
|
+
return await canonicalFilePath(file, SettingsFs);
|
|
23
24
|
}
|
|
24
25
|
catch (error) {
|
|
25
|
-
if (error.code !== 'ENOENT')
|
|
26
|
-
unavailable(file, error);
|
|
27
|
-
try {
|
|
28
|
-
await SettingsFs.lstat(file);
|
|
29
|
-
}
|
|
30
|
-
catch (lstatError) {
|
|
31
|
-
if (lstatError.code !== 'ENOENT')
|
|
32
|
-
unavailable(file, lstatError);
|
|
33
|
-
try {
|
|
34
|
-
return join(await SettingsFs.realpath(dirname(file)), basename(file));
|
|
35
|
-
}
|
|
36
|
-
catch (parentError) {
|
|
37
|
-
unavailable(file, parentError);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
// A directory entry whose realpath is missing is a dangling symlink.
|
|
41
26
|
unavailable(file, error);
|
|
42
27
|
}
|
|
43
28
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Stats, watch } from 'node:fs';
|
|
1
|
+
import { lstatSync, readlinkSync, realpathSync, type Stats, watch } from 'node:fs';
|
|
2
2
|
import { PrivateFileError } from '../fs/private-files.js';
|
|
3
3
|
type Metadata = Pick<Stats, 'mtimeMs' | 'size' | 'ino' | 'dev'> & Partial<Pick<Stats, 'ctimeMs'>>;
|
|
4
4
|
export interface TuiPreferencesWatchOptions {
|
|
@@ -13,6 +13,13 @@ export interface TuiPreferencesWatchOptions {
|
|
|
13
13
|
stat?: (file: string) => Promise<Metadata>;
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
|
+
declare const pathFs: {
|
|
17
|
+
lstatSync: typeof lstatSync;
|
|
18
|
+
readlinkSync: typeof readlinkSync;
|
|
19
|
+
realpathSync: typeof realpathSync;
|
|
20
|
+
};
|
|
21
|
+
/** Internal test hook; omitted path operations use the native implementation. */
|
|
22
|
+
export declare function setTuiPreferencesPathFsForTest(overrides?: Partial<typeof pathFs>): void;
|
|
16
23
|
/**
|
|
17
24
|
* Share one directory watcher and metadata poll per canonical path, including
|
|
18
25
|
* across bundled copies. Native events use a 150 ms debounce; missed events
|
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
import { readFileSync, realpathSync, statSync, watch, } from 'node:fs';
|
|
1
|
+
import { lstatSync, readFileSync, readlinkSync, realpathSync, statSync, watch, } from 'node:fs';
|
|
2
2
|
import { readFile, stat } from 'node:fs/promises';
|
|
3
3
|
import { basename, dirname, join, resolve } from 'node:path';
|
|
4
4
|
import { clearTimeout, setTimeout } from 'node:timers';
|
|
5
|
+
import { canonicalFilePathSync } from '../fs/canonical-file-path.js';
|
|
5
6
|
import { PrivateFileError, readPrivateText, windowsPrivateFiles, } from '../fs/private-files.js';
|
|
7
|
+
const pathFsDefaults = { lstatSync, readlinkSync, realpathSync };
|
|
8
|
+
const pathFs = { ...pathFsDefaults };
|
|
9
|
+
/** Internal test hook; omitted path operations use the native implementation. */
|
|
10
|
+
export function setTuiPreferencesPathFsForTest(overrides) {
|
|
11
|
+
Object.assign(pathFs, pathFsDefaults, overrides);
|
|
12
|
+
}
|
|
6
13
|
function canonicalPath(file) {
|
|
7
14
|
const path = resolve(file);
|
|
8
15
|
// Leave reparse points in the path for private-files' ancestor walk to reject;
|
|
@@ -10,7 +17,7 @@ function canonicalPath(file) {
|
|
|
10
17
|
if (windowsPrivateFiles)
|
|
11
18
|
return path;
|
|
12
19
|
try {
|
|
13
|
-
return
|
|
20
|
+
return canonicalFilePathSync(path, pathFs);
|
|
14
21
|
}
|
|
15
22
|
catch {
|
|
16
23
|
// Resolve existing ancestors even when the preferences file is not yet present.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cortexkit/common-auth",
|
|
3
|
-
"version": "0.49.
|
|
3
|
+
"version": "0.49.2",
|
|
4
4
|
"description": "Shared code for the CortexKit auth plugins: account pool, quota and routing, commands and auth menu, OpenCode 2 hooks, Claustrum custody, and plumbing (loopback RPC, file locks, logger, sidebar state, TUI preferences and build).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|