@alemonjs/qq-bot 2.1.31 → 2.1.33

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,21 @@
1
+ import type { QQBotAPI } from './sdk/api';
2
+ import { FileType } from './sdk/typing';
3
+ export declare const CHUNK_THRESHOLD = 10002432;
4
+ export declare const fileDataToBuffer: (fileData: any) => Buffer | null;
5
+ export declare const computeFileDigests: (filePath: string) => {
6
+ buffer: NonSharedBuffer;
7
+ md5: string;
8
+ sha1: string;
9
+ md5_10m: string;
10
+ };
11
+ export declare const chunkedUpload: (client: QQBotAPI, scope: "user" | "group", openId: string, file: string | Buffer, options: {
12
+ file_type: FileType;
13
+ file_name?: string;
14
+ srv_send_msg?: boolean;
15
+ }) => Promise<{
16
+ file_uuid: string;
17
+ file_info: string;
18
+ ttl: number;
19
+ id?: string;
20
+ raw_url?: string;
21
+ }>;
package/lib/upload.js ADDED
@@ -0,0 +1,60 @@
1
+ import { createHash } from 'crypto';
2
+ import { readFileSync } from 'fs';
3
+
4
+ const CHUNK_THRESHOLD = 10002432;
5
+ const fileDataToBuffer = (fileData) => {
6
+ if (!fileData) {
7
+ return null;
8
+ }
9
+ if (Buffer.isBuffer(fileData)) {
10
+ return fileData;
11
+ }
12
+ if (typeof fileData === 'string') {
13
+ if (fileData.indexOf('base64://') === 0) {
14
+ return Buffer.from(fileData.replace('base64://', ''), 'base64');
15
+ }
16
+ return Buffer.from(fileData, 'base64');
17
+ }
18
+ return null;
19
+ };
20
+ const computeFileDigests = (filePath) => {
21
+ const buffer = readFileSync(filePath);
22
+ const md5 = createHash('md5').update(buffer).digest('hex');
23
+ const sha1 = createHash('sha1').update(buffer).digest('hex');
24
+ const md5_10m = createHash('md5').update(buffer.subarray(0, CHUNK_THRESHOLD)).digest('hex');
25
+ return { buffer, md5, sha1, md5_10m };
26
+ };
27
+ const chunkedUpload = async (client, scope, openId, file, options) => {
28
+ const buffer = typeof file === 'string' ? readFileSync(file) : file;
29
+ const md5 = createHash('md5').update(buffer).digest('hex');
30
+ const sha1 = createHash('sha1').update(buffer).digest('hex');
31
+ const md5_10m = createHash('md5').update(buffer.subarray(0, CHUNK_THRESHOLD)).digest('hex');
32
+ const file_name = options.file_name ?? (typeof file === 'string' ? (file.split(/[\\/]/).pop() ?? 'file') : 'file');
33
+ const prepareData = {
34
+ file_type: options.file_type,
35
+ file_name,
36
+ file_size: String(buffer.byteLength),
37
+ md5,
38
+ sha1,
39
+ md5_10m
40
+ };
41
+ const prepare = await (scope === 'user' ? client.usersUploadPrepare(openId, prepareData) : client.groupUploadPrepare(openId, prepareData));
42
+ const blockSize = Number(prepare.block_size);
43
+ for (const part of prepare.parts) {
44
+ const start = part.index * blockSize;
45
+ const chunk = Buffer.from(buffer.subarray(start, start + Number(part.block_size)));
46
+ const partMd5 = createHash('md5').update(chunk).digest('hex');
47
+ await client.uploadPartDirect(part.presigned_url, chunk);
48
+ const finishData = {
49
+ upload_id: prepare.upload_id,
50
+ part_index: part.index,
51
+ block_size: part.block_size,
52
+ md5: partMd5
53
+ };
54
+ await (scope === 'user' ? client.usersUploadPartFinish(openId, finishData) : client.groupUploadPartFinish(openId, finishData));
55
+ }
56
+ const merge = { file_type: options.file_type, upload_id: prepare.upload_id, file_name, srv_send_msg: options.srv_send_msg };
57
+ return scope === 'user' ? client.postRichMediaByUser(openId, merge) : client.postRichMediaByGroup(openId, merge);
58
+ };
59
+
60
+ export { CHUNK_THRESHOLD, chunkedUpload, computeFileDigests, fileDataToBuffer };
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@alemonjs/qq-bot",
3
- "version": "2.1.31",
4
- "description": "阿柠檬qq-bot平台连接",
3
+ "version": "2.1.33",
4
+ "description": "QQ官方平台",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
8
  "main": "lib/index.js",
9
9
  "scripts": {
10
- "build": "lvy build"
10
+ "build": "lvy build",
11
+ "test:ws": "yarn build && node tests/websocket.integration.mjs"
11
12
  },
12
13
  "dependencies": {
13
14
  "@noble/curves": "^1.7.0",
@@ -54,7 +55,7 @@
54
55
  }
55
56
  ],
56
57
  "config-source": {
57
- "readme": "https://github.com/lemonade-lab/alemonjs/blob/main/packages/qq-bot/README.md",
58
+ "readme": "https://raw.githubusercontent.com/lemonade-lab/alemonjs/main/packages/qq-bot/README.md",
58
59
  "official": "https://bot.q.qq.com/wiki/develop/api-v2/",
59
60
  "platform": "https://q.qq.com/"
60
61
  },
@@ -93,5 +94,5 @@
93
94
  "type": "git",
94
95
  "url": "https://github.com/lemonade-lab/alemonjs.git"
95
96
  },
96
- "gitHead": "0425a2fbe4d6ca852f19096526219a68221f6bf5"
97
+ "gitHead": "6c93b0455b8db78ffa1a83becc1c2de19ba544db"
97
98
  }