-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix(telegram): add socket timeout and extract tgApi into shared module #1399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
tommylin-signalpro
wants to merge
7
commits into
NVIDIA:main
from
tommylin-signalpro:refactor/telegram-api-module
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
241c738
refactor(telegram): extract tgApi into shared module with socket timeout
tommylin-signalpro 5bc35da
fix(telegram): handle mid-response errors and improve test teardown
tommylin-signalpro 00eecf1
refactor(installer): thin bootstrap and versioned payload (#1395)
kjw3 2bd9025
fix(telegram): set UTF-8 encoding on response stream
tommylin-signalpro 86a9307
feat(policy): allow selecting policy presets by number (#1195)
cr7258 90df000
docs(telegram): add JSDoc to test helper for docstring coverage
tommylin-signalpro 9f51c01
fix(telegram): resolve type-check errors in tgApi module and tests
tommylin-signalpro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /** | ||
| * Telegram Bot API client with socket timeout protection. | ||
| * | ||
| * Exported so both the bridge script and tests use the same implementation. | ||
| */ | ||
|
|
||
| const https = require("https"); | ||
|
|
||
| const DEFAULT_TIMEOUT_MS = 60000; | ||
|
|
||
| /** | ||
| * Call a Telegram Bot API method. | ||
| * | ||
| * @param {string} token - Bot token from BotFather | ||
| * @param {string} method - API method name (e.g. "getUpdates") | ||
| * @param {object} body - JSON-serialisable request body | ||
| * @param {object} [opts] | ||
| * @param {number} [opts.timeout] - socket idle timeout in ms (default 60 000) | ||
| * @param {string} [opts.hostname] - override hostname (useful for tests) | ||
| * @param {number} [opts.port] - override port (useful for tests) | ||
| * @param {boolean} [opts.rejectUnauthorized] - TLS cert check (default true) | ||
| * @returns {Promise<object>} parsed JSON response | ||
| */ | ||
| function tgApi(token, method, body, opts = {}) { | ||
| const { | ||
| timeout = DEFAULT_TIMEOUT_MS, | ||
| hostname = "api.telegram.org", | ||
| port, | ||
| rejectUnauthorized, | ||
| } = opts; | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| let settled = false; | ||
| const settle = (fn, value) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| fn(value); | ||
| }; | ||
|
|
||
| const data = JSON.stringify(body); | ||
| const reqOpts = { | ||
| hostname, | ||
| path: `/bot${token}/${method}`, | ||
| method: "POST", | ||
| timeout, | ||
| headers: { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(data) }, | ||
| }; | ||
| if (port != null) reqOpts.port = port; | ||
| if (rejectUnauthorized != null) reqOpts.rejectUnauthorized = rejectUnauthorized; | ||
|
|
||
| const req = https.request(reqOpts, (res) => { | ||
| let buf = ""; | ||
| res.setEncoding("utf8"); | ||
| res.on("data", (c) => (buf += c)); | ||
| res.on("aborted", () => settle(reject, new Error(`Telegram API ${method} response aborted`))); | ||
| res.on("error", (err) => settle(reject, err)); | ||
| res.on("end", () => { | ||
| try { | ||
| settle(resolve, JSON.parse(buf)); | ||
| } catch { | ||
| settle(resolve, { ok: false, error: buf }); | ||
| } | ||
| }); | ||
| }); | ||
| req.on("timeout", () => { | ||
| req.destroy(new Error(`Telegram API ${method} timed out`)); | ||
| }); | ||
| req.on("error", (err) => settle(reject, err)); | ||
| req.write(data); | ||
| req.end(); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { tgApi, DEFAULT_TIMEOUT_MS }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.