@walkhi/code-relax 0.1.0-beta.76 → 0.1.0-beta.79
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/bin/codex-remote.mjs +23 -5
- package/dist/build-platform.json +1 -1
- package/dist/src/code-browser.mjs +0 -1
- package/dist/src/dots-chat-server.mjs +531 -531
- package/dist/src/dots-message-images.mjs +0 -1
- package/dist/src/dots-room-client.mjs +163 -157
- package/dist/src/dots-task-files.mjs +2 -2
- package/dist/src/file-upload-session.mjs +13 -6
- package/dist/src/lan-socket-server.mjs +5 -4
- package/dist/src/message-upload-files.mjs +115 -109
- package/dist/src/server.mjs +1981 -1984
- package/dist/src/shared-app-server.mjs +4 -3
- package/dist/src/workstation-downloads.mjs +120 -120
- package/package.json +4 -4
|
@@ -1,109 +1,115 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
await
|
|
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
|
-
throw new Error('
|
|
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
|
-
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { openAsBlob } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
import sharp from 'sharp';
|
|
6
|
+
import media from '../shared/code-browser.cjs';
|
|
7
|
+
import { validateInlineImage } from './message-images.mjs';
|
|
8
|
+
import { createFileUploadSession, FILE_CHUNK_BYTES, MAX_FILE_COUNT } from './file-upload-session.mjs';
|
|
9
|
+
import { fileRevision } from './file-transfer-range.mjs';
|
|
10
|
+
|
|
11
|
+
const mimeTypes = { pdf: 'application/pdf', txt: 'text/plain', md: 'text/markdown', csv: 'text/csv',
|
|
12
|
+
json: 'application/json', zip: 'application/zip', doc: 'application/msword',
|
|
13
|
+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
14
|
+
xls: 'application/vnd.ms-excel', xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
15
|
+
ppt: 'application/vnd.ms-powerpoint', pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' };
|
|
16
|
+
|
|
17
|
+
// Reuse the incoming-file contract without sending workstation bytes to the phone.
|
|
18
|
+
export async function stageWorkstationMessageFile(file, uploadsRoot) {
|
|
19
|
+
const handle = await fs.open(file.path, 'r');
|
|
20
|
+
const session = createFileUploadSession(uploadsRoot);
|
|
21
|
+
try {
|
|
22
|
+
const stat = await handle.stat();
|
|
23
|
+
if (!stat.isFile() || stat.size < 1) throw new Error('附件为空。');
|
|
24
|
+
const validate = async () => {
|
|
25
|
+
if (fileRevision(await handle.stat()) !== file.revision || fileRevision(await fs.stat(file.path)) !== file.revision) {
|
|
26
|
+
throw new Error('电脑文件已变化,请重新添加。');
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
await validate();
|
|
30
|
+
const bytes = Buffer.alloc(FILE_CHUNK_BYTES);
|
|
31
|
+
const hash = crypto.createHash('sha256');
|
|
32
|
+
const readPart = async offset => {
|
|
33
|
+
const length = Math.min(bytes.length, stat.size - offset);
|
|
34
|
+
let count = 0;
|
|
35
|
+
while (count < length) {
|
|
36
|
+
const { bytesRead } = await handle.read(bytes, count, length - count, offset + count);
|
|
37
|
+
if (!bytesRead) throw new Error('电脑文件读取不完整,请重新添加。');
|
|
38
|
+
count += bytesRead;
|
|
39
|
+
}
|
|
40
|
+
return bytes.subarray(0, length);
|
|
41
|
+
};
|
|
42
|
+
for (let offset = 0; offset < stat.size; offset += FILE_CHUNK_BYTES) hash.update(await readPart(offset));
|
|
43
|
+
await validate();
|
|
44
|
+
const sha256 = hash.digest('hex');
|
|
45
|
+
const started = await session.receive({ type: 'start', fileKey: '0', name: file.name, size: stat.size, sha256 });
|
|
46
|
+
for (let offset = 0; offset < stat.size; offset += FILE_CHUNK_BYTES) {
|
|
47
|
+
await session.receive({ type: 'chunk', id: started.id, offset, data: await readPart(offset) });
|
|
48
|
+
}
|
|
49
|
+
await validate();
|
|
50
|
+
const result = await session.receive({ type: 'finish', id: started.id });
|
|
51
|
+
await session.receive({ type: 'commit' });
|
|
52
|
+
return { ...result, mediaKind: media.mediaKind(file),
|
|
53
|
+
mimeType: media.mediaMime(file) || mimeTypes[path.extname(file.name).slice(1).toLowerCase()] || 'application/octet-stream' };
|
|
54
|
+
} finally { await handle.close(); await session.close(); }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function localFile(file, directory) {
|
|
58
|
+
if (typeof file?.path !== 'string' || typeof file.name !== 'string' || !file.name || /[\r\n\0]/.test(file.name)) {
|
|
59
|
+
throw new Error('附件信息无效,请重新选择。');
|
|
60
|
+
}
|
|
61
|
+
const root = await fs.realpath(directory), resolved = await fs.realpath(file.path);
|
|
62
|
+
if (path.relative(root, path.dirname(resolved)) !== '') throw new Error('附件必须来自本次工作站上传目录。');
|
|
63
|
+
const handle = await fs.open(resolved, 'r');
|
|
64
|
+
try {
|
|
65
|
+
const stat = await handle.stat();
|
|
66
|
+
if (!stat.isFile() || stat.size < 1) throw new Error('附件为空。');
|
|
67
|
+
const bytes = await openAsBlob(resolved);
|
|
68
|
+
if (bytes.size !== stat.size) throw new Error('附件在读取时发生变化,请重新上传。');
|
|
69
|
+
const mimeType = media.mediaMime(file) || mimeTypes[path.extname(file.name).slice(1).toLowerCase()] || 'application/octet-stream';
|
|
70
|
+
if (!/^[a-z0-9.+-]+\/[a-z0-9.+-]+$/i.test(mimeType)) throw new Error('附件类型无效。');
|
|
71
|
+
return { name: file.name, mimeType, bytes, path: resolved };
|
|
72
|
+
} finally { await handle.close(); }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function readMessageUploads(body, uploadsRoot) {
|
|
76
|
+
const images = body.images ?? [], originals = body.originalImages ?? [], files = body.files ?? [];
|
|
77
|
+
if (![images, originals, files].every(Array.isArray) || images.length + originals.length > 4 || files.length + originals.length > MAX_FILE_COUNT) {
|
|
78
|
+
throw new Error('每条消息最多 4 张图片、4 个文件。');
|
|
79
|
+
}
|
|
80
|
+
const uploads = [];
|
|
81
|
+
let imageBytes = 0;
|
|
82
|
+
for (const image of images) {
|
|
83
|
+
let file;
|
|
84
|
+
if (typeof image === 'string' && image.startsWith('data:')) {
|
|
85
|
+
validateInlineImage(image);
|
|
86
|
+
const mimeType = image.slice(5, image.indexOf(';'));
|
|
87
|
+
file = { name: `image.${mimeType.split('/')[1]}`, mimeType, bytes: Buffer.from(image.slice(image.indexOf(',') + 1), 'base64') };
|
|
88
|
+
} else {
|
|
89
|
+
if (typeof image !== 'string' || !/^[a-f0-9]{64}$/.test(image)) throw new Error('图片标识无效。');
|
|
90
|
+
let extension = 'webp';
|
|
91
|
+
try { await fs.access(path.join(uploadsRoot, `${image}.webp`)); }
|
|
92
|
+
catch (error) { if (error.code !== 'ENOENT') throw error; extension = 'png'; }
|
|
93
|
+
file = await localFile({ path: path.join(uploadsRoot, `${image}.${extension}`), name: `image.${extension}` }, uploadsRoot);
|
|
94
|
+
}
|
|
95
|
+
imageBytes += file.bytes.size ?? file.bytes.length;
|
|
96
|
+
if (imageBytes > 8 * 1024 * 1024) throw new Error('压缩图片合计不能超过 8 MiB。');
|
|
97
|
+
uploads.push(file);
|
|
98
|
+
}
|
|
99
|
+
for (const original of originals) {
|
|
100
|
+
if (typeof original !== 'string' || !/\.(png|jpe?g|webp|gif)$/i.test(original)) throw new Error('原图路径无效。');
|
|
101
|
+
}
|
|
102
|
+
for (const file of [...originals.map(value => ({ path: value, name: path.basename(value) })), ...files]) {
|
|
103
|
+
const uploaded = await localFile(file, path.join(uploadsRoot, 'incoming'));
|
|
104
|
+
uploads.push(uploaded);
|
|
105
|
+
}
|
|
106
|
+
for (const file of uploads) {
|
|
107
|
+
if (/^image\/(?:png|jpeg|webp|gif)$/.test(file.mimeType)) {
|
|
108
|
+
const metadata = await sharp(file.path || file.bytes).metadata();
|
|
109
|
+
file.width = metadata.width; file.height = metadata.height;
|
|
110
|
+
if (!file.width || !file.height) throw new Error('无法读取 图片尺寸。');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (uploads.filter(file => file.width).length > 4) throw new Error('每条消息最多 4 张图片。');
|
|
114
|
+
return uploads;
|
|
115
|
+
}
|