Repository navigation
Expand file tree
/
Copy pathnodeFileStorage.ts
More file actions
160 lines (141 loc) · 4.43 KB
/
Copy pathnodeFileStorage.ts
File metadata and controls
160 lines (141 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import type { KeylessStorage } from './service';
const CLERK_HIDDEN = '.clerk';
const CLERK_LOCK = 'clerk.lock';
const TEMP_DIR_NAME = '.tmp';
const CONFIG_FILE = 'keyless.json';
const README_FILE = 'README.md';
export interface NodeFileStorageOptions {
/**
* Function that returns the current working directory.
* Defaults to process.cwd().
*/
cwd?: () => string;
/**
* The framework name for the README message.
*
* @example '@clerk/nextjs'
*/
frameworkPackageName?: string;
}
export interface FileSystemAdapter {
existsSync: (path: string) => boolean;
readFileSync: (path: string, options: { encoding: BufferEncoding }) => string;
writeFileSync: (path: string, data: string, options: { encoding: BufferEncoding; mode?: number }) => void;
appendFileSync: (path: string, data: string) => void;
mkdirSync: (path: string, options: { recursive: boolean }) => void;
rmSync: (path: string, options: { force?: boolean; recursive?: boolean }) => void;
}
export interface PathAdapter {
join: (...paths: string[]) => string;
}
/**
* Creates a file-based storage adapter for keyless mode.
* This is used by Node.js-based frameworks (Next.js, TanStack Start, etc.)
* to persist keyless configuration to the file system.
*
* @param fs - Node.js fs module or compatible adapter
* @param path - Node.js path module or compatible adapter
* @param options - Configuration options
* @returns A KeylessStorage implementation
*/
export function createNodeFileStorage(
fs: FileSystemAdapter,
path: PathAdapter,
options: NodeFileStorageOptions = {},
): KeylessStorage {
const { cwd = () => process.cwd(), frameworkPackageName = '@clerk/shared' } = options;
let inMemoryLock = false;
const getClerkDir = () => path.join(cwd(), CLERK_HIDDEN);
const getTempDir = () => path.join(getClerkDir(), TEMP_DIR_NAME);
const getConfigPath = () => path.join(getTempDir(), CONFIG_FILE);
const getReadmePath = () => path.join(getTempDir(), README_FILE);
const getLockPath = () => path.join(cwd(), CLERK_LOCK);
const isLocked = (): boolean => inMemoryLock || fs.existsSync(getLockPath());
const lock = (): boolean => {
if (isLocked()) {
return false;
}
inMemoryLock = true;
try {
fs.writeFileSync(getLockPath(), 'This file can be deleted if your app is stuck.', {
encoding: 'utf8',
mode: 0o644,
});
return true;
} catch {
inMemoryLock = false;
return false;
}
};
const unlock = (): void => {
inMemoryLock = false;
try {
if (fs.existsSync(getLockPath())) {
fs.rmSync(getLockPath(), { force: true });
}
} catch {
// Ignore
}
};
const ensureDirectoryExists = () => {
const tempDir = getTempDir();
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
}
};
const updateGitignore = () => {
const gitignorePath = path.join(cwd(), '.gitignore');
const entry = `/${CLERK_HIDDEN}/`;
if (!fs.existsSync(gitignorePath)) {
fs.writeFileSync(gitignorePath, '', { encoding: 'utf8', mode: 0o644 });
}
const content = fs.readFileSync(gitignorePath, { encoding: 'utf-8' });
if (!content.includes(entry)) {
fs.appendFileSync(gitignorePath, `\n# clerk configuration (can include secrets)\n${entry}\n`);
}
};
const writeReadme = () => {
const readme = `## DO NOT COMMIT
This directory is auto-generated from \`${frameworkPackageName}\` because you are running in Keyless mode.
Avoid committing the \`.clerk/\` directory as it includes the secret key of the unclaimed instance.
`;
fs.writeFileSync(getReadmePath(), readme, { encoding: 'utf8', mode: 0o600 });
};
return {
read(): string {
try {
if (!fs.existsSync(getConfigPath())) {
return '';
}
return fs.readFileSync(getConfigPath(), { encoding: 'utf-8' });
} catch {
return '';
}
},
write(data: string): void {
if (!lock()) {
return;
}
try {
ensureDirectoryExists();
updateGitignore();
writeReadme();
fs.writeFileSync(getConfigPath(), data, { encoding: 'utf8', mode: 0o600 });
} finally {
unlock();
}
},
remove(): void {
if (!lock()) {
return;
}
try {
if (fs.existsSync(getClerkDir())) {
fs.rmSync(getClerkDir(), { recursive: true, force: true });
}
} finally {
unlock();
}
},
};
}