@sns-parse/core 0.3.0-alpha.1 → 0.4.0-alpha.10

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.
Files changed (47) hide show
  1. package/lib/config.d.ts +7 -0
  2. package/lib/config.js +28 -0
  3. package/lib/engine/config-io.d.ts +44 -0
  4. package/lib/engine/config-io.js +155 -0
  5. package/lib/engine/config.d.ts +9 -0
  6. package/lib/engine/config.js +265 -0
  7. package/lib/engine/fetcher.d.ts +21 -0
  8. package/lib/engine/fetcher.js +228 -0
  9. package/lib/engine/gateway.d.ts +21 -0
  10. package/lib/engine/gateway.js +19 -0
  11. package/lib/engine/parser.d.ts +2 -0
  12. package/lib/engine/parser.js +162 -0
  13. package/lib/engine/platform-config.d.ts +24 -0
  14. package/lib/engine/platform-config.js +76 -0
  15. package/lib/engine/runtime.d.ts +49 -0
  16. package/lib/engine/runtime.js +90 -0
  17. package/lib/engine/translate.d.ts +15 -0
  18. package/lib/engine/translate.js +107 -0
  19. package/lib/engine/twitter-user.d.ts +67 -0
  20. package/lib/engine/twitter-user.js +185 -0
  21. package/lib/engine/twitter.d.ts +102 -0
  22. package/lib/engine/twitter.js +791 -0
  23. package/lib/index.d.ts +21 -0
  24. package/lib/index.js +21 -0
  25. package/lib/registry.d.ts +20 -0
  26. package/lib/registry.js +77 -0
  27. package/lib/utils/cache.d.ts +9 -0
  28. package/lib/utils/cache.js +34 -0
  29. package/lib/utils/common.d.ts +7 -0
  30. package/lib/utils/common.js +58 -0
  31. package/lib/utils/concurrency.d.ts +8 -0
  32. package/lib/utils/concurrency.js +29 -0
  33. package/lib/utils/field-mapping.d.ts +2 -0
  34. package/lib/utils/field-mapping.js +29 -0
  35. package/lib/utils/format.d.ts +4 -0
  36. package/lib/utils/format.js +84 -0
  37. package/lib/utils/gif.d.ts +10 -0
  38. package/lib/utils/gif.js +104 -0
  39. package/lib/utils/image-size.d.ts +10 -0
  40. package/lib/utils/image-size.js +69 -0
  41. package/lib/utils/merge.d.ts +64 -0
  42. package/lib/utils/merge.js +402 -0
  43. package/lib/utils/tls-client.d.ts +30 -0
  44. package/lib/utils/tls-client.js +120 -0
  45. package/lib/utils/url.d.ts +10 -0
  46. package/lib/utils/url.js +89 -0
  47. package/package.json +16 -4
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cleanUrl = cleanUrl;
4
+ exports.linkTypeParser = linkTypeParser;
5
+ exports.extractAllUrlsFromMessage = extractAllUrlsFromMessage;
6
+ function cleanUrl(url) {
7
+ url = url.replace(/&/g, '&')
8
+ .replace(/"/g, '"')
9
+ .replace(/&lt;/g, '<')
10
+ .replace(/&gt;/g, '>')
11
+ .replace(/\\\//g, '/');
12
+ url = url.replace(/^[\s"'<((“”‘’]+/, '');
13
+ url = url.replace(/[\s"'<>\{\}\[\]`,;,。!?:;“”‘’…—~.())]+$/, '');
14
+ // 剔除链接后残留的 XML/HTML 标签片段(如卡片消息里的 <op>…(上游 v1.6.7)
15
+ const tagStart = url.indexOf('<');
16
+ if (tagStart > 0)
17
+ url = url.slice(0, tagStart);
18
+ if (!/^https?:\/\//i.test(url)) {
19
+ if (/^\/\//.test(url))
20
+ url = 'https:' + url;
21
+ else
22
+ return url;
23
+ }
24
+ return url;
25
+ }
26
+ function linkTypeParser(content, rules) {
27
+ content = content.replace(/\\\//g, '/');
28
+ const matches = [];
29
+ const seen = new Set();
30
+ for (const rule of rules) {
31
+ let match;
32
+ rule.pattern.lastIndex = 0;
33
+ while ((match = rule.pattern.exec(content)) !== null) {
34
+ let url = match[0];
35
+ url = cleanUrl(url);
36
+ if (!url)
37
+ continue;
38
+ if (seen.has(url))
39
+ continue;
40
+ seen.add(url);
41
+ // 记录出现位置,多链接按正文出现顺序排列(上游 v1.6.7)
42
+ matches.push({ type: rule.type, url, id: match[1] || url, pos: match.index });
43
+ }
44
+ }
45
+ return matches.sort((a, b) => a.pos - b.pos).map(({ pos, ...m }) => m);
46
+ }
47
+ function extractAllUrlsFromMessage(session, rules) {
48
+ const content = session.content?.trim() || '';
49
+ const matchedLinks = linkTypeParser(content, rules);
50
+ const cardsContent = [];
51
+ if (session.elements) {
52
+ for (const elem of session.elements) {
53
+ if (elem.type === 'xml' && elem.data)
54
+ cardsContent.push(elem.data);
55
+ else if (elem.type === 'json' && elem.data) {
56
+ try {
57
+ const json = JSON.parse(elem.data);
58
+ const extract = (obj) => {
59
+ if (!obj || typeof obj !== 'object')
60
+ return;
61
+ for (const val of Object.values(obj)) {
62
+ if (typeof val === 'string')
63
+ cardsContent.push(val);
64
+ else if (typeof val === 'object')
65
+ extract(val);
66
+ }
67
+ };
68
+ extract(json);
69
+ }
70
+ catch { }
71
+ }
72
+ }
73
+ }
74
+ for (const cardContent of cardsContent) {
75
+ matchedLinks.push(...linkTypeParser(cardContent, rules));
76
+ }
77
+ const cleanResult = [];
78
+ const seenUrls = new Set();
79
+ for (const link of matchedLinks) {
80
+ const cleaned = cleanUrl(link.url);
81
+ if (!cleaned || !/^https?:\/\//i.test(cleaned))
82
+ continue;
83
+ if (!seenUrls.has(cleaned)) {
84
+ seenUrls.add(cleaned);
85
+ cleanResult.push({ ...link, url: cleaned });
86
+ }
87
+ }
88
+ return cleanResult;
89
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sns-parse/core",
3
- "version": "0.3.0-alpha.1+upstream.1.6.7",
4
- "description": "sns-parse 核心契约与宿主抽象:类型、可注入日志、Host、发送层契约、平台定义",
3
+ "version": "0.4.0-alpha.10+upstream.1.6.7",
4
+ "description": "sns-parse 核心:契约、宿主抽象、配置 DSL 与解析引擎(fetcher/parser/翻译/X 原生解析/合并/GIF/TLS 指纹客户端)",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "files": [
@@ -11,7 +11,8 @@
11
11
  "scripts": {
12
12
  "build": "tsc",
13
13
  "clean": "rimraf lib",
14
- "prepublishOnly": "npm run build"
14
+ "test": "tsx test/engine.test.ts",
15
+ "prepublishOnly": "tsc"
15
16
  },
16
17
  "keywords": [
17
18
  "sns-parse",
@@ -21,10 +22,20 @@
21
22
  "koishi"
22
23
  ],
23
24
  "devDependencies": {
25
+ "@char46/tlsget-rs": "^1.0.0",
24
26
  "@types/node": "^20.11.0",
25
27
  "rimraf": "^5.0.5",
28
+ "tsx": "^4.23.15",
26
29
  "typescript": "^5.3.3"
27
30
  },
31
+ "dependencies": {
32
+ "axios": "^1.16.1",
33
+ "https-proxy-agent": "^5.0.1"
34
+ },
35
+ "optionalDependencies": {
36
+ "@char46/tlsget-rs": "^1.0.0",
37
+ "ffmpeg-static": "^5.2.0"
38
+ },
28
39
  "publishConfig": {
29
40
  "access": "public",
30
41
  "registry": "https://registry.npmjs.org"
@@ -35,5 +46,6 @@
35
46
  },
36
47
  "engines": {
37
48
  "node": ">=16.0.0"
38
- }
49
+ },
50
+ "packageManager": "pnpm@12.3.4"
39
51
  }