Comparing version 2.0.999 to 3.0.0
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const crypto = require("crypto"); | ||
const https = require("https"); | ||
const path = require("path"); | ||
const node_fetch_1 = require("node-fetch"); | ||
const check_1 = require("../helpers/check"); | ||
const compact_1 = require("../helpers/compact"); | ||
const error_1 = require("./error"); | ||
const url_1 = require("url"); | ||
const debug = require('debug')('telegraf:client'); | ||
const WEBHOOK_REPLY_METHOD_ALLOWLIST = new Set([ | ||
@@ -63,3 +61,3 @@ 'answerCallbackQuery', | ||
headers: { 'content-type': 'application/json', connection: 'keep-alive' }, | ||
body: JSON.stringify(payload, replacer), | ||
body: JSON.stringify(payload, null, 4), | ||
}); | ||
@@ -80,3 +78,3 @@ } | ||
} | ||
const boundary = crypto.randomBytes(32).toString('hex'); | ||
const boundary = 1 | ||
const formData = new multipart_stream_1.default(boundary); | ||
@@ -109,3 +107,3 @@ const tasks = Object.keys(payload).map((key) => attachFormValue(formData, key, payload[key], agent)); | ||
if (id === 'thumb') { | ||
const attachmentId = crypto.randomBytes(16).toString('hex'); | ||
const attachmentId = 1 | ||
await attachFormMedia(form, value, attachmentId, agent); | ||
@@ -122,3 +120,3 @@ return form.addPart({ | ||
} | ||
const attachmentId = crypto.randomBytes(16).toString('hex'); | ||
const attachmentId = 1 | ||
await attachFormMedia(form, item.media, attachmentId, agent); | ||
@@ -138,3 +136,3 @@ return { ...item, media: `attach://${attachmentId}` }; | ||
typeof value.type !== 'undefined') { | ||
const attachmentId = crypto.randomBytes(16).toString('hex'); | ||
const attachmentId = 1 | ||
await attachFormMedia(form, value.media, attachmentId, agent); | ||
@@ -155,3 +153,3 @@ return form.addPart({ | ||
if (media.url !== undefined) { | ||
const res = await node_fetch_1.default(media.url, { agent }); | ||
const res = await fetch(media.url, { agent }); | ||
return form.addPart({ | ||
@@ -226,3 +224,3 @@ headers: { | ||
WEBHOOK_REPLY_METHOD_ALLOWLIST.has(method)) { | ||
debug('Call via webhook', method, payload); | ||
// @ts-expect-error | ||
@@ -237,3 +235,3 @@ return await answerToWebhook(response, { method, ...payload }, options); | ||
} | ||
debug('HTTP call', method, payload); | ||
const config = includesMedia(payload) | ||
@@ -250,3 +248,3 @@ ? await buildFormDataConfig( | ||
if (!data.ok) { | ||
debug('API call failed', data); | ||
throw new error_1.default(data, { method, payload }); | ||
@@ -253,0 +251,0 @@ } |
@@ -25,3 +25,3 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
var session_1 = require("./session"); | ||
Object.defineProperty(exports, "Session", { enumerable: true, get: function () { return session_1.Session; } }); | ||
Object.defineProperty(exports, "session", { enumerable: true, get: function () { return session_1.session; } }); | ||
@@ -28,0 +28,0 @@ |
@@ -0,45 +1,41 @@ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Router = void 0; | ||
const { Application, Router } = require('./cfworker/web') | ||
const Mid = require('./cfworker-middware-telegraf') | ||
// class Router { | ||
// constructor(routeFn, handlers = new Map()) { | ||
// this.routeFn = routeFn; | ||
// this.handlers = handlers; | ||
// this.otherwiseHandler = composer_1.default.passThru(); | ||
// if (typeof routeFn !== 'function') { | ||
// throw new Error('Missing routing function'); | ||
// } | ||
// } | ||
// on(route, ...fns) { | ||
// if (fns.length === 0) { | ||
// throw new TypeError('At least one handler must be provided'); | ||
// } | ||
// this.handlers.set(route, composer_1.default.compose(fns)); | ||
// return this; | ||
// } | ||
// otherwise(...fns) { | ||
// if (fns.length === 0) { | ||
// throw new TypeError('At least one otherwise handler must be provided'); | ||
// } | ||
// this.otherwiseHandler = composer_1.default.compose(fns); | ||
// return this; | ||
// } | ||
// middleware() { | ||
// return composer_1.default.lazy((ctx) => { | ||
// var _a; | ||
// const result = this.routeFn(ctx); | ||
// if (result == null) { | ||
// return this.otherwiseHandler; | ||
// } | ||
// Object.assign(ctx, result.context); | ||
// Object.assign(ctx.state, result.state); | ||
// return (_a = this.handlers.get(result.route)) !== null && _a !== void 0 ? _a : this.otherwiseHandler; | ||
// }); | ||
// } | ||
// } | ||
exports.Router = Router | ||
exports.App = Application | ||
exports.Mid = Mid | ||
const composer_1 = require("./composer"); | ||
class Router { | ||
constructor(routeFn, handlers = new Map()) { | ||
this.routeFn = routeFn; | ||
this.handlers = handlers; | ||
this.otherwiseHandler = composer_1.default.passThru(); | ||
if (typeof routeFn !== 'function') { | ||
throw new Error('Missing routing function'); | ||
} | ||
} | ||
on(route, ...fns) { | ||
if (fns.length === 0) { | ||
throw new TypeError('At least one handler must be provided'); | ||
} | ||
this.handlers.set(route, composer_1.default.compose(fns)); | ||
return this; | ||
} | ||
otherwise(...fns) { | ||
if (fns.length === 0) { | ||
throw new TypeError('At least one otherwise handler must be provided'); | ||
} | ||
this.otherwiseHandler = composer_1.default.compose(fns); | ||
return this; | ||
} | ||
middleware() { | ||
return composer_1.default.lazy((ctx) => { | ||
var _a; | ||
const result = this.routeFn(ctx); | ||
if (result == null) { | ||
return this.otherwiseHandler; | ||
} | ||
Object.assign(ctx, result.context); | ||
Object.assign(ctx.state, result.state); | ||
return (_a = this.handlers.get(result.route)) !== null && _a !== void 0 ? _a : this.otherwiseHandler; | ||
}); | ||
} | ||
} | ||
exports.Router = Router; |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const composer_1 = require("../composer"); | ||
const debug_1 = require("debug"); | ||
const debug = debug_1.default('telegraf:scenes:context'); | ||
const noop = () => Promise.resolve(); | ||
@@ -58,3 +57,3 @@ const now = () => Math.floor(Date.now() / 1000); | ||
} | ||
debug('Entering scene', sceneId, initialState, silent); | ||
this.session.current = sceneId; | ||
@@ -83,3 +82,3 @@ this.state = initialState; | ||
return; | ||
debug('Leaving scene'); | ||
try { | ||
@@ -86,0 +85,0 @@ this.leaving = true; |
@@ -0,11 +1,61 @@ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Session = void 0; | ||
var Session = function() { | ||
return console.exit([...arguments]) | ||
} | ||
exports.Session = Session | ||
exports.default = Session | ||
exports.isSessionContext = exports.MemorySessionStore = exports.session = void 0; | ||
function session(options) { | ||
var _a, _b; | ||
const getSessionKey = (_a = options === null || options === void 0 ? void 0 : options.getSessionKey) !== null && _a !== void 0 ? _a : defaultGetSessionKey; | ||
const store = (_b = options === null || options === void 0 ? void 0 : options.store) !== null && _b !== void 0 ? _b : new MemorySessionStore(); | ||
return async (ctx, next) => { | ||
const key = await getSessionKey(ctx); | ||
if (key == null) { | ||
return await next(); | ||
} | ||
ctx.session = await store.get(key); | ||
await next(); | ||
if (ctx.session == null) { | ||
await store.delete(key); | ||
} | ||
else { | ||
await store.set(key, ctx.session); | ||
} | ||
}; | ||
} | ||
exports.session = session; | ||
async function defaultGetSessionKey(ctx) { | ||
var _a, _b; | ||
const fromId = (_a = ctx.from) === null || _a === void 0 ? void 0 : _a.id; | ||
const chatId = (_b = ctx.chat) === null || _b === void 0 ? void 0 : _b.id; | ||
if (fromId == null || chatId == null) { | ||
return undefined; | ||
} | ||
return `${fromId}:${chatId}`; | ||
} | ||
class MemorySessionStore { | ||
constructor(ttl = Infinity) { | ||
this.ttl = ttl; | ||
this.store = new Map(); | ||
} | ||
get(name) { | ||
const entry = this.store.get(name); | ||
if (entry == null) { | ||
return undefined; | ||
} | ||
else if (entry.expires < Date.now()) { | ||
this.delete(name); | ||
return undefined; | ||
} | ||
return entry.session; | ||
} | ||
set(name, value) { | ||
const now = Date.now(); | ||
this.store.set(name, { session: value, expires: now + this.ttl }); | ||
} | ||
delete(name) { | ||
this.store.delete(name); | ||
} | ||
} | ||
exports.MemorySessionStore = MemorySessionStore; | ||
function isSessionContext(ctx) { | ||
return 'session' in ctx; | ||
} | ||
exports.isSessionContext = isSessionContext; |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Telegraf = void 0; | ||
const crypto = require("crypto"); | ||
const http = require("http"); | ||
const https = require("https"); | ||
const util = require("util"); | ||
@@ -11,8 +8,8 @@ const composer_1 = require("./composer"); | ||
const context_1 = require("./context"); | ||
const debug_1 = require("debug"); | ||
const p_timeout_1 = require("p-timeout"); | ||
const p_timeout_1 = require("./p-timeout"); | ||
const telegram_1 = require("./telegram"); | ||
const url_1 = require("url"); | ||
const debug = debug_1.default('telegraf:main'); | ||
const DEFAULT_OPTIONS = { | ||
@@ -26,2 +23,3 @@ telegram: {}, | ||
} | ||
const noop = () => {} | ||
const anoop = always(Promise.resolve()); | ||
@@ -34,5 +32,3 @@ // eslint-disable-next-line import/export | ||
this.handleError = (err, ctx) => { | ||
// set exit code to emulate `warn-with-error-code` behavior of | ||
// https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode | ||
// to prevent a clean exit despite an error being thrown | ||
process.exitCode = 1; | ||
@@ -42,3 +38,3 @@ console.error('Unhandled error while processing', ctx.update); | ||
}; | ||
// @ts-expect-error | ||
this.options = { | ||
@@ -49,4 +45,7 @@ ...DEFAULT_OPTIONS, | ||
this.telegram = new telegram_1.default(token, this.options.telegram); | ||
debug('Created a `Telegraf` instance'); | ||
this.polling = { | ||
offset: 0, | ||
started: false | ||
} | ||
} | ||
get token() { | ||
@@ -72,48 +71,23 @@ return this.telegram.token; | ||
} | ||
startPolling(allowedUpdates = []) { | ||
startPolling (timeout = 30, limit = 100, allowedUpdates, stopCallback = noop) { | ||
this.polling.timeout = timeout | ||
this.polling.limit = limit | ||
this.polling.allowedUpdates = allowedUpdates | ||
? Array.isArray(allowedUpdates) ? allowedUpdates : [`${allowedUpdates}`] | ||
: null | ||
this.polling.stopCallback = stopCallback | ||
if (!this.polling.started) { | ||
this.polling.started = true | ||
this.fetchUpdates() | ||
} | ||
return this | ||
} | ||
startWebhook(hookPath, tlsOptions, port, host, cb) { | ||
} | ||
/** | ||
* @see https://github.com/telegraf/telegraf/discussions/1344#discussioncomment-335700 | ||
*/ | ||
async launch(config = {}) { | ||
var _a, _b, _c; | ||
debug('Connecting to Telegram'); | ||
(_a = this.botInfo) !== null && _a !== void 0 ? _a : (this.botInfo = await this.telegram.getMe()); | ||
debug(`Launching @${this.botInfo.username}`); | ||
if (config.webhook === undefined) { | ||
await this.telegram.deleteWebhook({ | ||
drop_pending_updates: config.dropPendingUpdates, | ||
}); | ||
this.startPolling(config.allowedUpdates); | ||
debug('Bot started with long polling'); | ||
return; | ||
} | ||
if (typeof config.webhook.domain !== 'string' && | ||
typeof config.webhook.hookPath !== 'string') { | ||
throw new Error('Webhook domain or webhook path is required'); | ||
} | ||
let domain = (_b = config.webhook.domain) !== null && _b !== void 0 ? _b : ''; | ||
if (domain.startsWith('https://') || domain.startsWith('http://')) { | ||
domain = new url_1.URL(domain).host; | ||
} | ||
const hookPath = (_c = config.webhook.hookPath) !== null && _c !== void 0 ? _c : `/telegraf/${crypto.randomBytes(32).toString('hex')}`; | ||
const { port, host, tlsOptions, cb } = config.webhook; | ||
this.startWebhook(hookPath, tlsOptions, port, host, cb); | ||
if (!domain) { | ||
debug('Bot started with webhook'); | ||
return; | ||
} | ||
await this.telegram.setWebhook(`https://${domain}${hookPath}`, { | ||
drop_pending_updates: config.dropPendingUpdates, | ||
allowed_updates: config.allowedUpdates, | ||
}); | ||
debug(`Bot started with webhook @ https://${domain}`); | ||
} | ||
stop(reason = 'unspecified') { | ||
var _a, _b; | ||
debug('Stopping bot... Reason:', reason); | ||
// https://github.com/telegraf/telegraf/pull/1224#issuecomment-742693770 | ||
@@ -132,7 +106,43 @@ if (this.polling === undefined && this.webhookServer === undefined) { | ||
} | ||
fetchUpdates () { | ||
if (!this.polling.started) { | ||
this.polling.stopCallback && this.polling.stopCallback() | ||
return | ||
} | ||
const { timeout, limit, offset, allowedUpdates } = this.polling | ||
this.telegram.getUpdates(timeout, limit, offset, allowedUpdates) | ||
.catch((err) => { | ||
if (err.code === 401 || err.code === 409) { | ||
throw err | ||
} | ||
const wait = (err.parameters && err.parameters.retry_after) || this.options.retryAfter | ||
console.error(`Failed to fetch updates. Waiting: ${wait}s`, err.message) | ||
return new Promise((resolve) => setTimeout(resolve, wait * 1000, [])) | ||
}) | ||
.then((updates) => this.polling.started | ||
? this.handleUpdates(updates).then(() => updates) | ||
: [] | ||
) | ||
.catch((err) => { | ||
console.error('Failed to process updates.', err) | ||
this.polling.started = false | ||
this.polling.offset = 0 | ||
this.polling.stopCallback && this.polling.stopCallback() | ||
return [] | ||
}) | ||
.then((updates) => { | ||
if (updates.length > 0) { | ||
this.polling.offset = updates[updates.length - 1].update_id + 1 | ||
} | ||
this.fetchUpdates() | ||
}) | ||
} | ||
async handleUpdate(update, webhookResponse) { | ||
var _a, _b; | ||
(_a = this.botInfo) !== null && _a !== void 0 ? _a : (this.botInfo = (debug('Update %d is waiting for `botInfo` to be initialized', update.update_id), | ||
(_a = this.botInfo) !== null && _a !== void 0 ? _a : (this.botInfo = ( update.update_id, | ||
await ((_b = this.botInfoCall) !== null && _b !== void 0 ? _b : (this.botInfoCall = this.telegram.getMe())))); | ||
debug('Processing update', update.update_id); | ||
const tg = new telegram_1.default(this.token, this.telegram.options, webhookResponse); | ||
@@ -152,3 +162,3 @@ const TelegrafContext = this.options.contextType; | ||
} | ||
debug('Finished processing update', update.update_id); | ||
} | ||
@@ -155,0 +165,0 @@ } |
{ | ||
"name": "11te", | ||
"version": "2.0.999", | ||
"version": "3.0.0", | ||
"main": "lib/index.js", | ||
"dependencies": { | ||
"debug": "^4.3.1", | ||
"p-timeout": "^5.0.0", | ||
"safe-compare": "^1.1.4" | ||
} | ||
} |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 5 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
172917
0
63
4568
23
- Removeddebug@^4.3.1
- Removedp-timeout@^5.0.0
- Removedsafe-compare@^1.1.4
- Removedbuffer-alloc@1.2.0(transitive)
- Removedbuffer-alloc-unsafe@1.1.0(transitive)
- Removedbuffer-fill@1.0.0(transitive)
- Removeddebug@4.4.0(transitive)
- Removedms@2.1.3(transitive)
- Removedp-timeout@5.1.0(transitive)
- Removedsafe-compare@1.1.4(transitive)