@enderrealmmc/cf-static-guard 0.1.0
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/README.md +125 -0
- package/bin/csg.js +7 -0
- package/dist/commands/auth.js +16 -0
- package/dist/commands/config.js +253 -0
- package/dist/commands/deploy.js +65 -0
- package/dist/commands/doctor.js +41 -0
- package/dist/commands/init.js +257 -0
- package/dist/commands/kv.js +39 -0
- package/dist/commands/open.js +54 -0
- package/dist/commands/profile.js +69 -0
- package/dist/commands/secrets.js +72 -0
- package/dist/index.js +40 -0
- package/dist/lib/cf.js +22 -0
- package/dist/lib/deploy.js +75 -0
- package/dist/lib/kv.js +125 -0
- package/dist/lib/log.js +22 -0
- package/dist/lib/paths.js +29 -0
- package/dist/lib/store.js +76 -0
- package/dist/lib/wrangler.js +48 -0
- package/dist-worker/worker.js +2333 -0
- package/package.json +40 -0
package/dist/lib/kv.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { runWrangler } from './wrangler.js';
|
|
5
|
+
const AUTH_KEY = 'auth:config';
|
|
6
|
+
const UI_KEY = 'ui:config';
|
|
7
|
+
function requireKv(profile) {
|
|
8
|
+
if (!profile.kvNamespaceId) {
|
|
9
|
+
throw new Error('KV namespace not set on profile. Run `csg init` or `csg kv create` first.');
|
|
10
|
+
}
|
|
11
|
+
return profile.kvNamespaceId;
|
|
12
|
+
}
|
|
13
|
+
export async function kvPut(profile, key, value) {
|
|
14
|
+
const id = requireKv(profile);
|
|
15
|
+
const dir = mkdtempSync(join(tmpdir(), 'csg-kv-'));
|
|
16
|
+
const file = join(dir, 'value.json');
|
|
17
|
+
writeFileSync(file, value, 'utf8');
|
|
18
|
+
try {
|
|
19
|
+
const res = await runWrangler([
|
|
20
|
+
'kv',
|
|
21
|
+
'key',
|
|
22
|
+
'put',
|
|
23
|
+
key,
|
|
24
|
+
'--namespace-id',
|
|
25
|
+
id,
|
|
26
|
+
'--path',
|
|
27
|
+
file,
|
|
28
|
+
]);
|
|
29
|
+
if (res.exitCode !== 0) {
|
|
30
|
+
throw new Error(res.stderr || res.stdout || 'kv put failed');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
try {
|
|
35
|
+
rmSync(dir, { recursive: true, force: true });
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// ignore
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export async function kvGet(profile, key) {
|
|
43
|
+
const id = requireKv(profile);
|
|
44
|
+
const res = await runWrangler([
|
|
45
|
+
'kv',
|
|
46
|
+
'key',
|
|
47
|
+
'get',
|
|
48
|
+
key,
|
|
49
|
+
'--namespace-id',
|
|
50
|
+
id,
|
|
51
|
+
]);
|
|
52
|
+
if (res.exitCode !== 0)
|
|
53
|
+
return null;
|
|
54
|
+
const out = res.stdout.trim();
|
|
55
|
+
return out || null;
|
|
56
|
+
}
|
|
57
|
+
export function profileToAuthConfig(profile) {
|
|
58
|
+
return {
|
|
59
|
+
mode: profile.mode,
|
|
60
|
+
providers: {
|
|
61
|
+
github: {
|
|
62
|
+
enabled: profile.providers.github?.enabled !== false,
|
|
63
|
+
mode: profile.providers.github?.mode,
|
|
64
|
+
rules: profile.providers.github?.rules || {},
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export function profileToUiConfig(profile) {
|
|
70
|
+
return {
|
|
71
|
+
siteTitle: profile.ui?.siteTitle ?? '受保护站点',
|
|
72
|
+
siteSubtitle: profile.ui?.siteSubtitle ?? '登录后继续访问',
|
|
73
|
+
loginHeading: profile.ui?.loginHeading ?? '访问受保护站点',
|
|
74
|
+
loginSubtitle: profile.ui?.loginSubtitle ?? '使用已授权账号登录后继续浏览',
|
|
75
|
+
providerButtonPrefix: profile.ui?.providerButtonPrefix ?? '使用',
|
|
76
|
+
footerBrand: profile.ui?.footerBrand ?? 'cf-static-guard',
|
|
77
|
+
copyright: profile.ui?.copyright ?? '',
|
|
78
|
+
icp: profile.ui?.icp ?? '',
|
|
79
|
+
icpLink: profile.ui?.icpLink ?? 'https://beian.miit.gov.cn/',
|
|
80
|
+
showModeBadge: profile.ui?.showModeBadge !== false,
|
|
81
|
+
showFooterBrand: profile.ui?.showFooterBrand !== false,
|
|
82
|
+
showGithubIcon: profile.ui?.showGithubIcon !== false,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export async function pushAuthConfig(profile) {
|
|
86
|
+
await kvPut(profile, AUTH_KEY, JSON.stringify(profileToAuthConfig(profile), null, 2));
|
|
87
|
+
}
|
|
88
|
+
export async function pushUiConfig(profile) {
|
|
89
|
+
await kvPut(profile, UI_KEY, JSON.stringify(profileToUiConfig(profile), null, 2));
|
|
90
|
+
}
|
|
91
|
+
export async function pullAuthConfig(profile) {
|
|
92
|
+
const raw = await kvGet(profile, AUTH_KEY);
|
|
93
|
+
if (!raw)
|
|
94
|
+
return null;
|
|
95
|
+
return JSON.parse(raw);
|
|
96
|
+
}
|
|
97
|
+
export async function pullUiConfig(profile) {
|
|
98
|
+
const raw = await kvGet(profile, UI_KEY);
|
|
99
|
+
if (!raw)
|
|
100
|
+
return null;
|
|
101
|
+
return JSON.parse(raw);
|
|
102
|
+
}
|
|
103
|
+
/** Merge pulled auth config into a local profile (for csg config pull --apply). */
|
|
104
|
+
export function applyAuthToProfile(profile, auth) {
|
|
105
|
+
const gh = auth.providers?.github;
|
|
106
|
+
return {
|
|
107
|
+
...profile,
|
|
108
|
+
mode: auth.mode || profile.mode,
|
|
109
|
+
providers: {
|
|
110
|
+
github: {
|
|
111
|
+
enabled: gh?.enabled !== false,
|
|
112
|
+
mode: gh?.mode,
|
|
113
|
+
rules: gh?.rules || {},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
updatedAt: new Date().toISOString(),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export function applyUiToProfile(profile, ui) {
|
|
120
|
+
return {
|
|
121
|
+
...profile,
|
|
122
|
+
ui: { ...profile.ui, ...ui },
|
|
123
|
+
updatedAt: new Date().toISOString(),
|
|
124
|
+
};
|
|
125
|
+
}
|
package/dist/lib/log.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import pc from 'picocolors';
|
|
2
|
+
export function info(msg) {
|
|
3
|
+
console.log(pc.cyan('i'), msg);
|
|
4
|
+
}
|
|
5
|
+
export function success(msg) {
|
|
6
|
+
console.log(pc.green('✓'), msg);
|
|
7
|
+
}
|
|
8
|
+
export function warn(msg) {
|
|
9
|
+
console.log(pc.yellow('!'), msg);
|
|
10
|
+
}
|
|
11
|
+
export function error(msg) {
|
|
12
|
+
console.error(pc.red('✗'), msg);
|
|
13
|
+
}
|
|
14
|
+
export function step(msg) {
|
|
15
|
+
console.log(pc.dim('→'), msg);
|
|
16
|
+
}
|
|
17
|
+
export function box(lines) {
|
|
18
|
+
console.log('');
|
|
19
|
+
for (const line of lines)
|
|
20
|
+
console.log(' ' + line);
|
|
21
|
+
console.log('');
|
|
22
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { dirname, join } from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
4
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
5
|
+
export function packageRoot() {
|
|
6
|
+
// dist/lib -> dist -> package root
|
|
7
|
+
return join(__dirname, '..', '..');
|
|
8
|
+
}
|
|
9
|
+
export function workerBundleDir() {
|
|
10
|
+
return join(packageRoot(), 'dist-worker');
|
|
11
|
+
}
|
|
12
|
+
export function workerBundlePath() {
|
|
13
|
+
return join(workerBundleDir(), 'worker.js');
|
|
14
|
+
}
|
|
15
|
+
export function hasWorkerBundle() {
|
|
16
|
+
return existsSync(workerBundlePath());
|
|
17
|
+
}
|
|
18
|
+
export function readWorkerBundle() {
|
|
19
|
+
return readFileSync(workerBundlePath(), 'utf8');
|
|
20
|
+
}
|
|
21
|
+
export function cliVersion() {
|
|
22
|
+
try {
|
|
23
|
+
const pkg = JSON.parse(readFileSync(join(packageRoot(), 'package.json'), 'utf8'));
|
|
24
|
+
return pkg.version || '0.0.0';
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return '0.0.0';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync, existsSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
export function configDir() {
|
|
5
|
+
if (process.platform === 'win32') {
|
|
6
|
+
const base = process.env.APPDATA || join(homedir(), 'AppData', 'Roaming');
|
|
7
|
+
return join(base, 'csg');
|
|
8
|
+
}
|
|
9
|
+
const base = process.env.XDG_CONFIG_HOME || join(homedir(), '.config');
|
|
10
|
+
return join(base, 'csg');
|
|
11
|
+
}
|
|
12
|
+
export function configPath() {
|
|
13
|
+
return join(configDir(), 'config.json');
|
|
14
|
+
}
|
|
15
|
+
export function emptyStore() {
|
|
16
|
+
return { defaultProfile: 'default', profiles: {} };
|
|
17
|
+
}
|
|
18
|
+
export function loadStore() {
|
|
19
|
+
const p = configPath();
|
|
20
|
+
if (!existsSync(p))
|
|
21
|
+
return emptyStore();
|
|
22
|
+
try {
|
|
23
|
+
const raw = JSON.parse(readFileSync(p, 'utf8'));
|
|
24
|
+
if (!raw || typeof raw !== 'object' || !raw.profiles)
|
|
25
|
+
return emptyStore();
|
|
26
|
+
return {
|
|
27
|
+
defaultProfile: raw.defaultProfile || 'default',
|
|
28
|
+
profiles: raw.profiles,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return emptyStore();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export function saveStore(store) {
|
|
36
|
+
const dir = configDir();
|
|
37
|
+
mkdirSync(dir, { recursive: true });
|
|
38
|
+
writeFileSync(configPath(), JSON.stringify(store, null, 2) + '\n', 'utf8');
|
|
39
|
+
}
|
|
40
|
+
export function getProfile(name) {
|
|
41
|
+
const store = loadStore();
|
|
42
|
+
const key = name || store.defaultProfile;
|
|
43
|
+
return store.profiles[key] || null;
|
|
44
|
+
}
|
|
45
|
+
export function upsertProfile(profile, asDefault = true) {
|
|
46
|
+
const store = loadStore();
|
|
47
|
+
store.profiles[profile.name] = profile;
|
|
48
|
+
if (asDefault)
|
|
49
|
+
store.defaultProfile = profile.name;
|
|
50
|
+
saveStore(store);
|
|
51
|
+
}
|
|
52
|
+
export function listProfileNames() {
|
|
53
|
+
return Object.keys(loadStore().profiles);
|
|
54
|
+
}
|
|
55
|
+
export function setDefaultProfile(name) {
|
|
56
|
+
const store = loadStore();
|
|
57
|
+
if (!store.profiles[name])
|
|
58
|
+
return false;
|
|
59
|
+
store.defaultProfile = name;
|
|
60
|
+
saveStore(store);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
export function defaultProfile(name, mode = 'normal') {
|
|
64
|
+
return {
|
|
65
|
+
name,
|
|
66
|
+
mode,
|
|
67
|
+
spaFallback: false,
|
|
68
|
+
publicPaths: ['/health'],
|
|
69
|
+
sessionTtlSeconds: 604800,
|
|
70
|
+
providers: {
|
|
71
|
+
github: { enabled: true, mode: undefined, rules: {} },
|
|
72
|
+
},
|
|
73
|
+
ui: {},
|
|
74
|
+
updatedAt: new Date().toISOString(),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { execa } from 'execa';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
export function wranglerBin() {
|
|
6
|
+
try {
|
|
7
|
+
// resolve wrangler's bin from this package's dependency
|
|
8
|
+
const pkgPath = require.resolve('wrangler/package.json');
|
|
9
|
+
const root = dirname(pkgPath);
|
|
10
|
+
const bin = process.platform === 'win32'
|
|
11
|
+
? `${root}\\bin\\wrangler.js`
|
|
12
|
+
: `${root}/bin/wrangler.js`;
|
|
13
|
+
return bin;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return 'wrangler';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export async function runWrangler(args, opts = {}) {
|
|
20
|
+
const bin = wranglerBin();
|
|
21
|
+
const isJs = bin.endsWith('.js');
|
|
22
|
+
const cmd = isJs ? process.execPath : bin;
|
|
23
|
+
const cmdArgs = isJs ? [bin, ...args] : args;
|
|
24
|
+
try {
|
|
25
|
+
const result = await execa(cmd, cmdArgs, {
|
|
26
|
+
reject: false,
|
|
27
|
+
stdio: ['inherit', 'pipe', 'pipe'],
|
|
28
|
+
...opts,
|
|
29
|
+
});
|
|
30
|
+
return {
|
|
31
|
+
stdout: String(result.stdout ?? ''),
|
|
32
|
+
stderr: String(result.stderr ?? ''),
|
|
33
|
+
exitCode: result.exitCode ?? 1,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
38
|
+
return { stdout: '', stderr: msg, exitCode: 1 };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export async function runWranglerInherit(args, opts = {}) {
|
|
42
|
+
const bin = wranglerBin();
|
|
43
|
+
const isJs = bin.endsWith('.js');
|
|
44
|
+
const cmd = isJs ? process.execPath : bin;
|
|
45
|
+
const cmdArgs = isJs ? [bin, ...args] : args;
|
|
46
|
+
const result = await execa(cmd, cmdArgs, { reject: false, stdio: 'inherit', ...opts });
|
|
47
|
+
return result.exitCode ?? 1;
|
|
48
|
+
}
|