@maiyunnet/kebab 9.23.0 → 9.25.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.
@@ -0,0 +1,2 @@
1
+ export declare function isFiles(files: unknown): files is Record<string, string>;
2
+ export declare function replace(directory: string, files: Record<string, string>, report: (message: string, error: unknown) => void): Promise<string>;
@@ -0,0 +1,97 @@
1
+ import * as fs from 'fs/promises';
2
+ import * as path from 'path';
3
+ import * as lText from '#kebab/lib/text.js';
4
+ let pending = Promise.resolve('Done');
5
+ export function isFiles(files) {
6
+ if (!files || typeof files !== 'object' || Array.isArray(files)) {
7
+ return false;
8
+ }
9
+ const entries = Object.entries(files);
10
+ if (!entries.length) {
11
+ return false;
12
+ }
13
+ const names = new Set();
14
+ for (const [name, content] of entries) {
15
+ if (!name.endsWith('.json') || /[/\\\0:]/.test(name) || names.has(name.toLowerCase()) || typeof content !== 'string') {
16
+ return false;
17
+ }
18
+ names.add(name.toLowerCase());
19
+ const config = lText.parseJson(content);
20
+ const list = Array.isArray(config) ? config : [config];
21
+ for (const item of list) {
22
+ if (!item || typeof item !== 'object' || Array.isArray(item)) {
23
+ return false;
24
+ }
25
+ const vhost = item;
26
+ if (!Array.isArray(vhost['domains']) || vhost['domains'].some(domain => typeof domain !== 'string') ||
27
+ typeof vhost['root'] !== 'string' ||
28
+ vhost['name'] !== undefined && typeof vhost['name'] !== 'string' ||
29
+ vhost['remark'] !== undefined && typeof vhost['remark'] !== 'string') {
30
+ return false;
31
+ }
32
+ }
33
+ }
34
+ return true;
35
+ }
36
+ export function replace(directory, files, report) {
37
+ pending = pending.catch(() => '').then(() => replaceOnce(path.resolve(directory), files, report));
38
+ return pending;
39
+ }
40
+ async function replaceOnce(directory, files, report) {
41
+ let temporary = '';
42
+ let moved = false;
43
+ let committed = false;
44
+ let preserveBackup = false;
45
+ try {
46
+ let current;
47
+ try {
48
+ current = await fs.lstat(directory);
49
+ }
50
+ catch (error) {
51
+ if (error.code !== 'ENOENT') {
52
+ throw error;
53
+ }
54
+ }
55
+ if (current && !current.isDirectory()) {
56
+ return 'Invalid vhost directory';
57
+ }
58
+ temporary = await fs.mkdtemp(path.join(path.dirname(directory), '.vhost-'));
59
+ const staged = path.join(temporary, 'new');
60
+ const backup = path.join(temporary, 'old');
61
+ await fs.mkdir(staged, { mode: current ? current.mode & 0o777 : 0o755 });
62
+ for (const [name, content] of Object.entries(files)) {
63
+ await fs.writeFile(path.join(staged, name), content, { encoding: 'utf8', flag: 'wx' });
64
+ }
65
+ if (current) {
66
+ await fs.rename(directory, backup);
67
+ moved = true;
68
+ }
69
+ await fs.rename(staged, directory);
70
+ committed = true;
71
+ return 'Done';
72
+ }
73
+ catch (error) {
74
+ report('Directory replacement failed.', error);
75
+ if (moved && !committed) {
76
+ try {
77
+ await fs.rename(path.join(temporary, 'old'), directory);
78
+ }
79
+ catch (restoreError) {
80
+ preserveBackup = true;
81
+ report('Directory restore failed.', restoreError);
82
+ return `Failed to restore vhost directory; backup: ${path.join(temporary, 'old')}`;
83
+ }
84
+ }
85
+ return 'Failed to replace vhost directory';
86
+ }
87
+ finally {
88
+ if (temporary && !preserveBackup) {
89
+ try {
90
+ await fs.rm(temporary, { recursive: true, force: true });
91
+ }
92
+ catch (error) {
93
+ report(`Temporary directory cleanup failed: ${temporary}`, error);
94
+ }
95
+ }
96
+ }
97
+ }
@@ -0,0 +1,2 @@
1
+ import type * as http from 'http';
2
+ export declare function getPublicProtocol(req: Pick<http.IncomingMessage, 'headers'>, fallback: string): string;
@@ -0,0 +1,79 @@
1
+ export function getPublicProtocol(req, fallback) {
2
+ if (fallback !== 'http:' && fallback !== 'https:') {
3
+ return fallback;
4
+ }
5
+ const forwarded = req.headers['forwarded'];
6
+ if (typeof forwarded === 'string' && forwarded.length <= 4096) {
7
+ const forwardedParts = splitQuoted(forwarded, ',');
8
+ if (forwardedParts !== null) {
9
+ const params = splitQuoted(forwardedParts[0], ';');
10
+ if (params !== null) {
11
+ let protocol = null;
12
+ let valid = true;
13
+ for (const param of params) {
14
+ const index = param.indexOf('=');
15
+ const name = param.slice(0, index).trim();
16
+ if (index < 1 || !/^[^\s;,="]+$/u.test(name)) {
17
+ valid = false;
18
+ break;
19
+ }
20
+ if (name.toLowerCase() !== 'proto') {
21
+ continue;
22
+ }
23
+ const value = param.slice(index + 1).trim();
24
+ const unquoted = value.startsWith('"') && value.endsWith('"')
25
+ ? value.slice(1, -1).replace(/\\(.)/gu, '$1')
26
+ : value;
27
+ if (protocol !== null || !/^(?:http|https)$/iu.test(unquoted)) {
28
+ valid = false;
29
+ break;
30
+ }
31
+ protocol = unquoted.toLowerCase() + ':';
32
+ }
33
+ if (valid && protocol !== null) {
34
+ return protocol;
35
+ }
36
+ }
37
+ }
38
+ }
39
+ const xForwardedProto = req.headers['x-forwarded-proto'];
40
+ if (typeof xForwardedProto === 'string' && xForwardedProto.length <= 4096) {
41
+ const first = xForwardedProto.split(',', 1)[0].trim();
42
+ if (/^(?:http|https)$/iu.test(first)) {
43
+ return first.toLowerCase() + ':';
44
+ }
45
+ }
46
+ return fallback;
47
+ }
48
+ function splitQuoted(value, separator) {
49
+ const result = [];
50
+ let start = 0;
51
+ let quoted = false;
52
+ let escaped = false;
53
+ for (let i = 0; i < value.length; ++i) {
54
+ const char = value[i];
55
+ if (escaped) {
56
+ escaped = false;
57
+ continue;
58
+ }
59
+ if (quoted && char === '\\') {
60
+ escaped = true;
61
+ continue;
62
+ }
63
+ if (char === '"') {
64
+ quoted = !quoted;
65
+ }
66
+ else if (!quoted && char === separator) {
67
+ if (separator === ',') {
68
+ return [value.slice(0, i)];
69
+ }
70
+ result.push(value.slice(start, i));
71
+ start = i + 1;
72
+ }
73
+ }
74
+ if (quoted || escaped) {
75
+ return null;
76
+ }
77
+ result.push(value.slice(start));
78
+ return result;
79
+ }