@maiyunnet/kebab 9.26.5 → 9.27.1
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/abox/response.d.ts +3 -0
- package/abox/response.js +59 -0
- package/abox/rpc.d.ts +14 -0
- package/abox/rpc.js +116 -0
- package/abox/text-path.d.ts +3 -0
- package/abox/text-path.js +94 -0
- package/doc/kebab-rag.md +731 -341
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/core/rpc.d.ts +61 -0
- package/lib/core/rpc.js +51 -0
- package/lib/core.d.ts +3 -4
- package/lib/core.js +36 -50
- package/lib/sql.d.ts +2 -1
- package/lib/sql.js +8 -7
- package/lib/text.d.ts +6 -3
- package/lib/text.js +26 -61
- package/lib/undici.js +11 -0
- package/package.json +1 -1
- package/sys/child.js +19 -5
- package/sys/cmd.js +5 -4
- package/sys/ctr.js +1 -1
- package/sys/master.js +256 -260
- package/sys/mod.d.ts +16 -3
- package/sys/mod.js +43 -19
- package/sys/monitor/watchdog.js +3 -3
- package/sys/route.js +21 -22
- package/www/example/abox/core-updatecode.d.ts +2 -0
- package/www/example/abox/core-updatecode.js +43 -0
- package/www/example/ctr/test.d.ts +4 -0
- package/www/example/ctr/test.js +29 -4
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import * as stream from 'stream';
|
|
2
|
+
/** Pipe all parts as one response and stop every source when the response fails. */
|
|
3
|
+
export declare function pipeResponse(parts: Array<stream.Readable | string | Buffer>, response: stream.Writable, compress?: stream.Transform, onInputDone?: () => void): void;
|
package/abox/response.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import * as stream from 'stream';
|
|
2
|
+
/** Pipe all parts as one response and stop every source when the response fails. */
|
|
3
|
+
export function pipeResponse(parts, response, compress, onInputDone) {
|
|
4
|
+
const sources = parts.filter((part) => part instanceof stream.Readable);
|
|
5
|
+
let failure;
|
|
6
|
+
let inputDone = false;
|
|
7
|
+
const combined = stream.Readable.from((async function* () {
|
|
8
|
+
for (const part of parts) {
|
|
9
|
+
if (failure) {
|
|
10
|
+
throw failure;
|
|
11
|
+
}
|
|
12
|
+
if (part instanceof stream.Readable) {
|
|
13
|
+
for await (const chunk of part) {
|
|
14
|
+
yield chunk;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
yield part;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
})());
|
|
22
|
+
const stopSources = () => {
|
|
23
|
+
for (const source of sources) {
|
|
24
|
+
if (!source.destroyed) {
|
|
25
|
+
source.destroy();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const onError = (error) => {
|
|
30
|
+
failure ??= error;
|
|
31
|
+
if (!inputDone) {
|
|
32
|
+
combined.destroy(error);
|
|
33
|
+
}
|
|
34
|
+
stopSources();
|
|
35
|
+
};
|
|
36
|
+
for (const source of sources) {
|
|
37
|
+
source.on('error', onError);
|
|
38
|
+
source.once('close', () => source.off('error', onError));
|
|
39
|
+
}
|
|
40
|
+
response.once('close', stopSources);
|
|
41
|
+
const finishInput = () => {
|
|
42
|
+
if (!inputDone) {
|
|
43
|
+
inputDone = true;
|
|
44
|
+
onInputDone?.();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
combined.once('end', finishInput);
|
|
48
|
+
const done = () => {
|
|
49
|
+
stopSources();
|
|
50
|
+
response.off('close', stopSources);
|
|
51
|
+
finishInput();
|
|
52
|
+
};
|
|
53
|
+
if (compress) {
|
|
54
|
+
stream.pipeline(combined, compress, response, done);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
stream.pipeline(combined, response, done);
|
|
58
|
+
}
|
|
59
|
+
}
|
package/abox/rpc.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type * as http from 'node:http';
|
|
2
|
+
export declare function validSecret(secret: unknown): secret is string;
|
|
3
|
+
export declare function hash(data: string | Buffer): string;
|
|
4
|
+
export declare function hashFile(file: string): Promise<string | false>;
|
|
5
|
+
export declare function sign(message: Record<string, unknown>, secret: string, method?: string, bodyHash?: string): string;
|
|
6
|
+
export declare function authenticate(req: http.IncomingMessage, secret: unknown): (Record<string, any> & {
|
|
7
|
+
action: string;
|
|
8
|
+
bodyHash: string;
|
|
9
|
+
}) | false;
|
|
10
|
+
export declare function reject(req: http.IncomingMessage): boolean;
|
|
11
|
+
export declare function readBody(req: http.IncomingMessage, expected: string, directory: string, upload: boolean, limit: number): Promise<{
|
|
12
|
+
body?: Buffer;
|
|
13
|
+
file?: string;
|
|
14
|
+
} | false>;
|
package/abox/rpc.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import * as crypto from 'node:crypto';
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
const emptyHash = crypto.createHash('sha256').digest('hex');
|
|
5
|
+
const nonces = new Map();
|
|
6
|
+
const rates = new Map();
|
|
7
|
+
const windowMs = 5_000;
|
|
8
|
+
export function validSecret(secret) {
|
|
9
|
+
return typeof secret === 'string' && secret.trim().length >= 32 && !/^(.{1,8})\1+$/s.test(secret.trim()) && !/^(mustchange|changeme|placeholder|your[_ -]?secret)/i.test(secret.trim());
|
|
10
|
+
}
|
|
11
|
+
export function hash(data) {
|
|
12
|
+
return crypto.createHash('sha256').update(data).digest('hex');
|
|
13
|
+
}
|
|
14
|
+
export async function hashFile(file) {
|
|
15
|
+
try {
|
|
16
|
+
const digest = crypto.createHash('sha256');
|
|
17
|
+
for await (const chunk of fs.createReadStream(file))
|
|
18
|
+
digest.update(chunk);
|
|
19
|
+
return digest.digest('hex');
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function sign(message, secret, method = 'GET', bodyHash = emptyHash) {
|
|
26
|
+
if (!validSecret(secret))
|
|
27
|
+
return 'invalid';
|
|
28
|
+
const iv = crypto.randomBytes(12);
|
|
29
|
+
const cipher = crypto.createCipheriv('aes-256-gcm', crypto.createHash('sha256').update(secret).digest(), iv);
|
|
30
|
+
const data = Buffer.from(JSON.stringify({ ...message, time: Date.now(), nonce: crypto.randomBytes(16).toString('hex'), method, bodyHash }));
|
|
31
|
+
const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
|
|
32
|
+
return Buffer.concat([iv, cipher.getAuthTag(), encrypted]).toString('base64url');
|
|
33
|
+
}
|
|
34
|
+
export function authenticate(req, secret) {
|
|
35
|
+
const now = Date.now();
|
|
36
|
+
const ip = req.socket.remoteAddress ?? 'unknown';
|
|
37
|
+
for (const [key, entry] of rates)
|
|
38
|
+
if (entry.expires <= now)
|
|
39
|
+
rates.delete(key);
|
|
40
|
+
const rate = rates.get(ip);
|
|
41
|
+
if (rate && rate.count >= 120 || !rate && rates.size >= 4096 || !validSecret(secret))
|
|
42
|
+
return false;
|
|
43
|
+
const token = req.url?.slice(1) ?? '';
|
|
44
|
+
if (!/^[A-Za-z0-9_-]+$/.test(token) || token.length > 32768)
|
|
45
|
+
return false;
|
|
46
|
+
try {
|
|
47
|
+
const raw = Buffer.from(token, 'base64url');
|
|
48
|
+
if (raw.length < 29)
|
|
49
|
+
return false;
|
|
50
|
+
const decipher = crypto.createDecipheriv('aes-256-gcm', crypto.createHash('sha256').update(secret).digest(), raw.subarray(0, 12));
|
|
51
|
+
decipher.setAuthTag(raw.subarray(12, 28));
|
|
52
|
+
const msg = JSON.parse(Buffer.concat([decipher.update(raw.subarray(28)), decipher.final()]).toString());
|
|
53
|
+
if (!msg || typeof msg !== 'object' || typeof msg.action !== 'string' || !Number.isSafeInteger(msg.time) || Math.abs(now - msg.time) > windowMs || !/^[a-f0-9]{32}$/.test(msg.nonce) || !/^[a-f0-9]{64}$/.test(msg.bodyHash) || msg.method !== req.method)
|
|
54
|
+
return false;
|
|
55
|
+
for (const [key, expires] of nonces)
|
|
56
|
+
if (expires < now)
|
|
57
|
+
nonces.delete(key);
|
|
58
|
+
if (nonces.has(msg.nonce) || nonces.size >= 10000)
|
|
59
|
+
return false;
|
|
60
|
+
nonces.set(msg.nonce, msg.time + windowMs);
|
|
61
|
+
return msg;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export function reject(req) {
|
|
68
|
+
const now = Date.now();
|
|
69
|
+
const ip = req.socket.remoteAddress ?? 'unknown';
|
|
70
|
+
for (const [key, entry] of rates)
|
|
71
|
+
if (entry.expires <= now)
|
|
72
|
+
rates.delete(key);
|
|
73
|
+
const entry = rates.get(ip);
|
|
74
|
+
if (entry) {
|
|
75
|
+
entry.count = Math.min(entry.count + 1, 120);
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
if (rates.size >= 4096)
|
|
79
|
+
return false;
|
|
80
|
+
rates.set(ip, { count: 1, expires: now + 60_000 });
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
export async function readBody(req, expected, directory, upload, limit) {
|
|
84
|
+
let file;
|
|
85
|
+
let handle;
|
|
86
|
+
const chunks = [];
|
|
87
|
+
let size = 0;
|
|
88
|
+
const digest = crypto.createHash('sha256');
|
|
89
|
+
try {
|
|
90
|
+
if (upload) {
|
|
91
|
+
file = path.join(directory, `rpc-${crypto.randomBytes(16).toString('hex')}`);
|
|
92
|
+
handle = await fs.promises.open(file, 'wx', 0o600);
|
|
93
|
+
}
|
|
94
|
+
for await (const chunk of req) {
|
|
95
|
+
const data = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
96
|
+
size += data.length;
|
|
97
|
+
if (size > limit)
|
|
98
|
+
throw new Error('limit');
|
|
99
|
+
digest.update(data);
|
|
100
|
+
if (handle)
|
|
101
|
+
await handle.writeFile(data);
|
|
102
|
+
else
|
|
103
|
+
chunks.push(data);
|
|
104
|
+
}
|
|
105
|
+
if (digest.digest('hex') !== expected)
|
|
106
|
+
throw new Error('digest');
|
|
107
|
+
await handle?.close();
|
|
108
|
+
return upload ? { file } : { body: Buffer.concat(chunks, size) };
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
await handle?.close().catch(() => { });
|
|
112
|
+
if (file)
|
|
113
|
+
await fs.promises.unlink(file).catch(() => { });
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
export function urlAtom(url) {
|
|
2
|
+
const suffixAt = url.search(/[?#]/);
|
|
3
|
+
const suffix = suffixAt < 0 ? '' : url.slice(suffixAt);
|
|
4
|
+
let path = suffixAt < 0 ? url : url.slice(0, suffixAt);
|
|
5
|
+
const authority = /^(?:[a-z][a-z\d+.-]*:)?\/\/[^/]*/i.exec(path);
|
|
6
|
+
const drive = /^[a-z]:/i.exec(path);
|
|
7
|
+
const prefix = authority?.[0] ?? drive?.[0] ?? '';
|
|
8
|
+
path = path.slice(prefix.length);
|
|
9
|
+
const rooted = path.startsWith('/');
|
|
10
|
+
const segments = [];
|
|
11
|
+
const parts = path.split('/');
|
|
12
|
+
for (let i = 0; i < parts.length; i++) {
|
|
13
|
+
const part = parts[i];
|
|
14
|
+
if (part === '.') {
|
|
15
|
+
if (i === parts.length - 1)
|
|
16
|
+
segments.push('');
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (part === '..') {
|
|
20
|
+
while (segments.length > (rooted ? 1 : 0) && segments[segments.length - 1] === '')
|
|
21
|
+
segments.pop();
|
|
22
|
+
if (segments.length && segments[segments.length - 1] !== '..' && segments[segments.length - 1] !== '') {
|
|
23
|
+
segments.pop();
|
|
24
|
+
if (i === parts.length - 1)
|
|
25
|
+
segments.push('');
|
|
26
|
+
}
|
|
27
|
+
else if (!rooted)
|
|
28
|
+
segments.push('..');
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
segments.push(part);
|
|
32
|
+
}
|
|
33
|
+
let result = segments.join('/');
|
|
34
|
+
if (rooted && !result.startsWith('/'))
|
|
35
|
+
result = '/' + result;
|
|
36
|
+
return prefix + result + suffix;
|
|
37
|
+
}
|
|
38
|
+
export function urlWithinHttpBase(base, target) {
|
|
39
|
+
try {
|
|
40
|
+
const baseUrl = new URL(base);
|
|
41
|
+
const targetUrl = new URL(target);
|
|
42
|
+
return baseUrl.origin === targetUrl.origin &&
|
|
43
|
+
(targetUrl.pathname === baseUrl.pathname.slice(0, -1) || targetUrl.pathname.startsWith(baseUrl.pathname));
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export function queryParse(query) {
|
|
50
|
+
const ret = {};
|
|
51
|
+
const arrayKeys = {};
|
|
52
|
+
const arr = query.split('&');
|
|
53
|
+
for (const i of arr) {
|
|
54
|
+
if (!i.length) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const pos = i.indexOf('=');
|
|
58
|
+
let key;
|
|
59
|
+
try {
|
|
60
|
+
key = decodeURIComponent((pos === -1 ? i : i.slice(0, pos)).replace(/\+/g, ' '));
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
key = (pos === -1 ? i : i.slice(0, pos)).replace(/\+/g, ' ');
|
|
64
|
+
}
|
|
65
|
+
let value = '';
|
|
66
|
+
try {
|
|
67
|
+
value = pos === -1 ? '' : decodeURIComponent(i.slice(pos + 1).replace(/\+/g, ' '));
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
value = pos === -1 ? '' : i.slice(pos + 1).replace(/\+/g, ' ');
|
|
71
|
+
}
|
|
72
|
+
if (Object.hasOwn(arrayKeys, key)) {
|
|
73
|
+
ret[key].push(value);
|
|
74
|
+
}
|
|
75
|
+
else if (!Object.hasOwn(ret, key)) {
|
|
76
|
+
Object.defineProperty(ret, key, {
|
|
77
|
+
'configurable': true,
|
|
78
|
+
'enumerable': true,
|
|
79
|
+
'value': value,
|
|
80
|
+
'writable': true,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
ret[key] = [ret[key], value];
|
|
85
|
+
Object.defineProperty(arrayKeys, key, {
|
|
86
|
+
'configurable': true,
|
|
87
|
+
'enumerable': true,
|
|
88
|
+
'value': true,
|
|
89
|
+
'writable': true,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return ret;
|
|
94
|
+
}
|