@xmanrui/dsh-im 4.2.0 → 4.3.0

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 {
2
+ EnvHttpProxyAgent,
3
+ FormData as UndiciFormData,
4
+ fetch as undiciFetch,
5
+ } from 'undici';
6
+
7
+ const TELEGRAM_CONNECTIONS = 4;
8
+
9
+ export function createTelegramHttpTransport() {
10
+ const dispatcher = new EnvHttpProxyAgent({ connections: TELEGRAM_CONNECTIONS });
11
+ let destroyed = false;
12
+ return {
13
+ fetchImpl: (url, options = {}) => undiciFetch(url, { ...options, dispatcher }),
14
+ FormDataImpl: UndiciFormData,
15
+ async destroy() {
16
+ if (destroyed) return;
17
+ destroyed = true;
18
+ await dispatcher.destroy();
19
+ },
20
+ };
21
+ }
@@ -5,6 +5,7 @@ import { createTextDeliveryBlock } from '../shared/semantic/delivery.mjs';
5
5
  import { t } from '../shared/i18n.mjs';
6
6
  import { captureContextEnhancement } from '../shared/context-enhancement.mjs';
7
7
  import { COMMANDS_MENU_BUTTON, TelegramApi } from './telegram-api.mjs';
8
+ import { createTelegramHttpTransport } from './telegram-http.mjs';
8
9
  import { createTelegramBridgeStatus, TelegramHarnessBridge } from './telegram-bridge.mjs';
9
10
  import {
10
11
  splitTelegramRegularText,
@@ -671,9 +672,11 @@ export class TelegramRuntime {
671
672
  #logger;
672
673
  #replyTimeoutMs;
673
674
  #createApi;
675
+ #createHttpTransport;
674
676
  #accessMode;
675
677
  #allowedPrivateUserIds;
676
678
  #status = createTelegramRuntimeStatus();
679
+ #httpTransport = null;
677
680
  #api = null;
678
681
  #bridge = null;
679
682
  #abortController = null;
@@ -689,6 +692,7 @@ export class TelegramRuntime {
689
692
  logger = console,
690
693
  replyTimeoutMs = 600_000,
691
694
  createApi = (options) => new TelegramApi(options),
695
+ createHttpTransport = createTelegramHttpTransport,
692
696
  }) {
693
697
  if (!config || !token || !harness || !state) {
694
698
  throw new TypeError('TelegramRuntime requires config, token, Harness, and state');
@@ -701,6 +705,7 @@ export class TelegramRuntime {
701
705
  this.#logger = logger;
702
706
  this.#replyTimeoutMs = replyTimeoutMs;
703
707
  this.#createApi = createApi;
708
+ this.#createHttpTransport = createHttpTransport;
704
709
  const accessPolicy = normalizeTelegramAccessPolicy(config);
705
710
  this.#accessMode = accessPolicy.accessMode;
706
711
  this.#allowedPrivateUserIds = new Set(accessPolicy.allowedUsers);
@@ -762,9 +767,15 @@ export class TelegramRuntime {
762
767
 
763
768
  const controller = new AbortController();
764
769
  this.#abortController = controller;
765
- const api = this.#createApi({ token: this.#token });
766
- this.#api = api;
767
770
  try {
771
+ const transport = this.#createHttpTransport();
772
+ this.#httpTransport = transport;
773
+ const api = this.#createApi({
774
+ token: this.#token,
775
+ fetchImpl: transport.fetchImpl,
776
+ FormDataImpl: transport.FormDataImpl,
777
+ });
778
+ this.#api = api;
768
779
  const bot = await api.getMe({ signal: controller.signal });
769
780
  if (String(bot?.id ?? '') !== this.#config.platformId || bot?.is_bot !== true) {
770
781
  throw new Error('Telegram token identity does not match the saved bot');
@@ -878,9 +889,11 @@ export class TelegramRuntime {
878
889
  async stop() {
879
890
  const pollTask = this.#pollTask;
880
891
  const bridge = this.#bridge;
892
+ const httpTransport = this.#httpTransport;
881
893
  this.#abortController?.abort();
882
894
  this.#abortController = null;
883
895
  this.#pollTask = null;
896
+ this.#httpTransport = null;
884
897
  this.#api = null;
885
898
  this.#bridge = null;
886
899
  await Promise.race([
@@ -891,6 +904,11 @@ export class TelegramRuntime {
891
904
  bridge?.waitForIdle() ?? Promise.resolve(),
892
905
  new Promise((resolve) => setTimeout(resolve, 2_000)),
893
906
  ]);
907
+ try {
908
+ await httpTransport?.destroy();
909
+ } catch {
910
+ this.#logger.warn?.('[dsh-im:telegram] Telegram HTTP transport cleanup failed');
911
+ }
894
912
  this.#status.ready = false;
895
913
  this.#status.connectionState = 'idle';
896
914
  return this.status;