@michengai/dsh-im-connect 0.1.19 → 0.1.21

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 (67) hide show
  1. package/README.en.md +2 -2
  2. package/README.md +2 -2
  3. package/SECURITY.md +1 -1
  4. package/lib/channels/dingtalk-card.d.ts.map +1 -1
  5. package/lib/channels/dingtalk-card.js +3 -0
  6. package/lib/channels/dingtalk-card.js.map +1 -1
  7. package/lib/channels/dingtalk.d.ts.map +1 -1
  8. package/lib/channels/dingtalk.js +6 -1
  9. package/lib/channels/dingtalk.js.map +1 -1
  10. package/lib/channels/feishu.d.ts +9 -0
  11. package/lib/channels/feishu.d.ts.map +1 -1
  12. package/lib/channels/feishu.js +23 -6
  13. package/lib/channels/feishu.js.map +1 -1
  14. package/lib/channels/qq.d.ts.map +1 -1
  15. package/lib/channels/qq.js +17 -1
  16. package/lib/channels/qq.js.map +1 -1
  17. package/lib/channels/telegram.d.ts.map +1 -1
  18. package/lib/channels/telegram.js +17 -4
  19. package/lib/channels/telegram.js.map +1 -1
  20. package/lib/channels/wecom.d.ts +7 -1
  21. package/lib/channels/wecom.d.ts.map +1 -1
  22. package/lib/channels/wecom.js +44 -11
  23. package/lib/channels/wecom.js.map +1 -1
  24. package/lib/channels/weixin.d.ts +3 -0
  25. package/lib/channels/weixin.d.ts.map +1 -1
  26. package/lib/channels/weixin.js +68 -15
  27. package/lib/channels/weixin.js.map +1 -1
  28. package/lib/engine/abort.d.ts +4 -0
  29. package/lib/engine/abort.d.ts.map +1 -0
  30. package/lib/engine/abort.js +26 -0
  31. package/lib/engine/abort.js.map +1 -0
  32. package/lib/engine/atomic-file.d.ts +5 -0
  33. package/lib/engine/atomic-file.d.ts.map +1 -0
  34. package/lib/engine/atomic-file.js +46 -0
  35. package/lib/engine/atomic-file.js.map +1 -0
  36. package/lib/engine/json-state.d.ts.map +1 -1
  37. package/lib/engine/json-state.js +7 -5
  38. package/lib/engine/json-state.js.map +1 -1
  39. package/lib/engine/secure-file.d.ts.map +1 -1
  40. package/lib/engine/secure-file.js +2 -12
  41. package/lib/engine/secure-file.js.map +1 -1
  42. package/lib/engine/seen-store.d.ts.map +1 -1
  43. package/lib/engine/seen-store.js +4 -4
  44. package/lib/engine/seen-store.js.map +1 -1
  45. package/lib/engine/session-store.d.ts.map +1 -1
  46. package/lib/engine/session-store.js +7 -5
  47. package/lib/engine/session-store.js.map +1 -1
  48. package/lib/manager.d.ts +7 -0
  49. package/lib/manager.d.ts.map +1 -1
  50. package/lib/manager.js +50 -42
  51. package/lib/manager.js.map +1 -1
  52. package/package.json +1 -1
  53. package/src/channels/dingtalk-card.ts +195 -192
  54. package/src/channels/dingtalk.ts +144 -140
  55. package/src/channels/feishu.ts +92 -66
  56. package/src/channels/qq.ts +303 -288
  57. package/src/channels/telegram.ts +170 -158
  58. package/src/channels/wecom.ts +209 -177
  59. package/src/channels/weixin.ts +158 -112
  60. package/src/engine/abort.ts +25 -0
  61. package/src/engine/access.ts +34 -34
  62. package/src/engine/atomic-file.ts +36 -0
  63. package/src/engine/json-state.ts +16 -15
  64. package/src/engine/secure-file.ts +7 -13
  65. package/src/engine/seen-store.ts +39 -39
  66. package/src/engine/session-store.ts +40 -39
  67. package/src/manager.ts +346 -321
@@ -1,43 +1,43 @@
1
- import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
2
- import { dirname } from 'node:path'
3
-
4
- /** 去重最近见过的消息 id,避免重启或重复投递打两次。 */
5
- export class SeenStore {
6
- private ids: string[] = []
7
- private known = new Set<string>()
8
-
9
- constructor(private readonly file: string, private readonly limit = 2000) {
10
- this.load()
11
- }
12
-
13
- has(id: string): boolean {
14
- return this.known.has(id)
15
- }
16
-
17
- add(id: string): void {
18
- if (!id || this.has(id)) return
19
- this.ids.push(id)
20
- this.known.add(id)
21
- if (this.ids.length > this.limit) {
22
- this.ids = this.ids.slice(-this.limit)
23
- this.known = new Set(this.ids)
24
- }
25
- this.flush()
26
- }
27
-
28
- private load(): void {
29
- try {
30
- const parsed = JSON.parse(readFileSync(this.file, 'utf8')) as { ids?: string[] }
31
- this.ids = Array.isArray(parsed.ids) ? parsed.ids.filter((item) => typeof item === 'string') : []
32
- this.known = new Set(this.ids)
1
+ import { readFileSync } from 'node:fs'
2
+ import { backupCorruptFileSync, writeFileAtomicSync } from './atomic-file.js'
3
+
4
+ /** 去重最近见过的消息 id,避免重启或重复投递打两次。 */
5
+ export class SeenStore {
6
+ private ids: string[] = []
7
+ private known = new Set<string>()
8
+
9
+ constructor(private readonly file: string, private readonly limit = 2000) {
10
+ this.load()
11
+ }
12
+
13
+ has(id: string): boolean {
14
+ return this.known.has(id)
15
+ }
16
+
17
+ add(id: string): void {
18
+ if (!id || this.has(id)) return
19
+ this.ids.push(id)
20
+ this.known.add(id)
21
+ if (this.ids.length > this.limit) {
22
+ this.ids = this.ids.slice(-this.limit)
23
+ this.known = new Set(this.ids)
24
+ }
25
+ this.flush()
26
+ }
27
+
28
+ private load(): void {
29
+ try {
30
+ const parsed = JSON.parse(readFileSync(this.file, 'utf8')) as { ids?: string[] }
31
+ this.ids = Array.isArray(parsed.ids) ? parsed.ids.filter((item) => typeof item === 'string') : []
32
+ this.known = new Set(this.ids)
33
33
  } catch {
34
+ backupCorruptFileSync(this.file)
34
35
  this.ids = []
35
- this.known.clear()
36
- }
37
- }
38
-
36
+ this.known.clear()
37
+ }
38
+ }
39
+
39
40
  private flush(): void {
40
- mkdirSync(dirname(this.file), { recursive: true })
41
- writeFileSync(this.file, `${JSON.stringify({ ids: this.ids }, null, 2)}\n`, 'utf8')
41
+ writeFileAtomicSync(this.file, `${JSON.stringify({ ids: this.ids }, null, 2)}\n`)
42
42
  }
43
- }
43
+ }
@@ -1,45 +1,46 @@
1
- import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
2
- import { dirname } from 'node:path'
1
+ import { readFileSync } from 'node:fs'
3
2
  import type { SessionRecord } from './session-id.js'
4
-
5
- export class SessionMapStore {
6
- private readonly file: string
7
- private records: Record<string, SessionRecord> = {}
8
-
9
- constructor(file: string) {
10
- this.file = file
11
- this.load()
12
- }
13
-
14
- list(): SessionRecord[] {
15
- return Object.values(this.records).sort((a, b) => b.updatedAt.localeCompare(a.updatedAt))
16
- }
17
-
18
- get(key: string): SessionRecord | undefined {
19
- return this.records[key]
20
- }
21
-
22
- upsert(key: string, record: SessionRecord): void {
23
- this.records[key] = record
24
- this.flush()
25
- }
26
-
27
- remove(key: string): void {
28
- delete this.records[key]
29
- this.flush()
30
- }
31
-
32
- private load(): void {
33
- try {
3
+ import { backupCorruptFileSync, writeFileAtomicSync } from './atomic-file.js'
4
+
5
+ export class SessionMapStore {
6
+ private readonly file: string
7
+ private records: Record<string, SessionRecord> = {}
8
+
9
+ constructor(file: string) {
10
+ this.file = file
11
+ this.load()
12
+ }
13
+
14
+ list(): SessionRecord[] {
15
+ return Object.values(this.records).sort((a, b) => b.updatedAt.localeCompare(a.updatedAt))
16
+ }
17
+
18
+ get(key: string): SessionRecord | undefined {
19
+ return this.records[key]
20
+ }
21
+
22
+ upsert(key: string, record: SessionRecord): void {
23
+ this.records[key] = record
24
+ this.flush()
25
+ }
26
+
27
+ remove(key: string): void {
28
+ delete this.records[key]
29
+ this.flush()
30
+ }
31
+
32
+ private load(): void {
33
+ try {
34
34
  const parsed = JSON.parse(readFileSync(this.file, 'utf8')) as Record<string, SessionRecord>
35
- this.records = parsed && typeof parsed === 'object' ? parsed : {}
35
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw new TypeError('会话映射顶层必须是对象')
36
+ this.records = parsed
36
37
  } catch {
38
+ backupCorruptFileSync(this.file)
37
39
  this.records = {}
38
- }
39
- }
40
-
40
+ }
41
+ }
42
+
41
43
  private flush(): void {
42
- mkdirSync(dirname(this.file), { recursive: true })
43
- writeFileSync(this.file, `${JSON.stringify(this.records, null, 2)}\n`, 'utf8')
44
+ writeFileAtomicSync(this.file, `${JSON.stringify(this.records, null, 2)}\n`)
44
45
  }
45
- }
46
+ }