@sugansociety/gallagherrich 4.16.8
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.
- package/LICENSE +22 -0
- package/README.md +1024 -0
- package/filters.d.ts +1 -0
- package/filters.js +1 -0
- package/format.d.ts +1 -0
- package/format.js +1 -0
- package/future.d.ts +1 -0
- package/future.js +1 -0
- package/lib/button.js +102 -0
- package/lib/cli.mjs +105 -0
- package/lib/composer.js +583 -0
- package/lib/context.js +1280 -0
- package/lib/core/helpers/args.js +59 -0
- package/lib/core/helpers/check.js +57 -0
- package/lib/core/helpers/compact.js +18 -0
- package/lib/core/helpers/deunionize.js +14 -0
- package/lib/core/helpers/formatting.js +92 -0
- package/lib/core/helpers/util.js +51 -0
- package/lib/core/network/client.js +321 -0
- package/lib/core/network/error.js +22 -0
- package/lib/core/network/multipart-stream.js +62 -0
- package/lib/core/network/polling.js +88 -0
- package/lib/core/network/webhook.js +55 -0
- package/lib/core/types/rich-message.js +288 -0
- package/lib/core/types/typegram.js +29 -0
- package/lib/filters.js +70 -0
- package/lib/filters.js.map +1 -0
- package/lib/format.js +39 -0
- package/lib/format.js.map +1 -0
- package/lib/future.js +167 -0
- package/lib/future.js.map +1 -0
- package/lib/index.js +50 -0
- package/lib/input.js +62 -0
- package/lib/markup.js +112 -0
- package/lib/markup.js.map +1 -0
- package/lib/middleware.js +3 -0
- package/lib/reactions.js +85 -0
- package/lib/router.js +47 -0
- package/lib/scenes/base.js +40 -0
- package/lib/scenes/context.js +105 -0
- package/lib/scenes/index.js +22 -0
- package/lib/scenes/stage.js +50 -0
- package/lib/scenes/wizard/context.js +32 -0
- package/lib/scenes/wizard/index.js +46 -0
- package/lib/scenes.js +18 -0
- package/lib/scenes.js.map +1 -0
- package/lib/session.js +167 -0
- package/lib/session.js.map +1 -0
- package/lib/telegraf.js +273 -0
- package/lib/telegram-types.js +7 -0
- package/lib/telegram.js +1270 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/lib/utils.js +6 -0
- package/lib/utils.js.map +1 -0
- package/markup.d.ts +1 -0
- package/markup.js +1 -0
- package/package.json +136 -0
- package/scenes.d.ts +1 -0
- package/scenes.js +1 -0
- package/session.d.ts +1 -0
- package/session.js +1 -0
- package/src/button.ts +182 -0
- package/src/composer.ts +1008 -0
- package/src/context.ts +1747 -0
- package/src/core/helpers/args.ts +63 -0
- package/src/core/helpers/check.ts +71 -0
- package/src/core/helpers/compact.ts +18 -0
- package/src/core/helpers/deunionize.ts +26 -0
- package/src/core/helpers/formatting.ts +119 -0
- package/src/core/helpers/util.ts +96 -0
- package/src/core/network/client.ts +396 -0
- package/src/core/network/error.ts +29 -0
- package/src/core/network/multipart-stream.ts +45 -0
- package/src/core/network/polling.ts +94 -0
- package/src/core/network/webhook.ts +58 -0
- package/src/core/types/rich-message.ts +375 -0
- package/src/core/types/typegram.ts +55 -0
- package/src/filters.ts +109 -0
- package/src/format.ts +110 -0
- package/src/future.ts +231 -0
- package/src/index.ts +18 -0
- package/src/input.ts +59 -0
- package/src/markup.ts +142 -0
- package/src/middleware.ts +24 -0
- package/src/reactions.ts +118 -0
- package/src/router.ts +55 -0
- package/src/scenes/base.ts +52 -0
- package/src/scenes/context.ts +136 -0
- package/src/scenes/index.ts +21 -0
- package/src/scenes/stage.ts +71 -0
- package/src/scenes/wizard/context.ts +58 -0
- package/src/scenes/wizard/index.ts +63 -0
- package/src/scenes.ts +1 -0
- package/src/session.ts +204 -0
- package/src/telegraf.ts +380 -0
- package/src/telegram-types.ts +256 -0
- package/src/telegram.ts +1676 -0
- package/src/types.ts +2 -0
- package/src/utils.ts +1 -0
- package/types.d.ts +1 -0
- package/types.js +1 -0
- package/typings/button.d.ts +36 -0
- package/typings/button.d.ts.map +1 -0
- package/typings/composer.d.ts +227 -0
- package/typings/composer.d.ts.map +1 -0
- package/typings/context.d.ts +699 -0
- package/typings/context.d.ts.map +1 -0
- package/typings/core/helpers/args.d.ts +11 -0
- package/typings/core/helpers/args.d.ts.map +1 -0
- package/typings/core/helpers/check.d.ts +56 -0
- package/typings/core/helpers/check.d.ts.map +1 -0
- package/typings/core/helpers/compact.d.ts +4 -0
- package/typings/core/helpers/compact.d.ts.map +1 -0
- package/typings/core/helpers/deunionize.d.ts +18 -0
- package/typings/core/helpers/deunionize.d.ts.map +1 -0
- package/typings/core/helpers/formatting.d.ts +30 -0
- package/typings/core/helpers/formatting.d.ts.map +1 -0
- package/typings/core/helpers/util.d.ts +27 -0
- package/typings/core/helpers/util.d.ts.map +1 -0
- package/typings/core/network/client.d.ts +55 -0
- package/typings/core/network/client.d.ts.map +1 -0
- package/typings/core/network/error.d.ts +16 -0
- package/typings/core/network/error.d.ts.map +1 -0
- package/typings/core/network/multipart-stream.d.ts +20 -0
- package/typings/core/network/multipart-stream.d.ts.map +1 -0
- package/typings/core/network/polling.d.ts +16 -0
- package/typings/core/network/polling.d.ts.map +1 -0
- package/typings/core/network/webhook.d.ts +7 -0
- package/typings/core/network/webhook.d.ts.map +1 -0
- package/typings/core/types/rich-message.d.ts +144 -0
- package/typings/core/types/rich-message.d.ts.map +1 -0
- package/typings/core/types/typegram.d.ts +46 -0
- package/typings/core/types/typegram.d.ts.map +1 -0
- package/typings/filters.d.ts +18 -0
- package/typings/filters.d.ts.map +1 -0
- package/typings/format.d.ts +22 -0
- package/typings/format.d.ts.map +1 -0
- package/typings/future.d.ts +12 -0
- package/typings/future.d.ts.map +1 -0
- package/typings/index.d.ts +16 -0
- package/typings/index.d.ts.map +1 -0
- package/typings/input.d.ts +59 -0
- package/typings/input.d.ts.map +1 -0
- package/typings/markup.d.ts +27 -0
- package/typings/markup.d.ts.map +1 -0
- package/typings/middleware.d.ts +8 -0
- package/typings/middleware.d.ts.map +1 -0
- package/typings/reactions.d.ts +32 -0
- package/typings/reactions.d.ts.map +1 -0
- package/typings/router.d.ts +21 -0
- package/typings/router.d.ts.map +1 -0
- package/typings/scenes/base.d.ts +22 -0
- package/typings/scenes/base.d.ts.map +1 -0
- package/typings/scenes/context.d.ts +36 -0
- package/typings/scenes/context.d.ts.map +1 -0
- package/typings/scenes/index.d.ts +11 -0
- package/typings/scenes/index.d.ts.map +1 -0
- package/typings/scenes/stage.d.ts +24 -0
- package/typings/scenes/stage.d.ts.map +1 -0
- package/typings/scenes/wizard/context.d.ts +29 -0
- package/typings/scenes/wizard/context.d.ts.map +1 -0
- package/typings/scenes/wizard/index.d.ts +16 -0
- package/typings/scenes/wizard/index.d.ts.map +1 -0
- package/typings/scenes.d.ts +2 -0
- package/typings/scenes.d.ts.map +1 -0
- package/typings/session.d.ts +55 -0
- package/typings/session.d.ts.map +1 -0
- package/typings/telegraf.d.ts +119 -0
- package/typings/telegraf.d.ts.map +1 -0
- package/typings/telegram-types.d.ts +141 -0
- package/typings/telegram-types.d.ts.map +1 -0
- package/typings/telegram.d.ts +694 -0
- package/typings/telegram.d.ts.map +1 -0
- package/typings/types.d.ts +3 -0
- package/typings/types.d.ts.map +1 -0
- package/typings/utils.d.ts +2 -0
- package/typings/utils.d.ts.map +1 -0
- package/utils.d.ts +1 -0
- package/utils.js +1 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const composer_1 = __importDefault(require("../composer"));
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const debug = (0, debug_1.default)('telegraf:scenes:context');
|
|
9
|
+
const noop = () => Promise.resolve();
|
|
10
|
+
const now = () => Math.floor(Date.now() / 1000);
|
|
11
|
+
class SceneContextScene {
|
|
12
|
+
constructor(ctx, scenes, options) {
|
|
13
|
+
this.ctx = ctx;
|
|
14
|
+
this.scenes = scenes;
|
|
15
|
+
this.leaving = false;
|
|
16
|
+
// @ts-expect-error {} might not be assignable to D
|
|
17
|
+
const fallbackSessionDefault = {};
|
|
18
|
+
this.options = { defaultSession: fallbackSessionDefault, ...options };
|
|
19
|
+
}
|
|
20
|
+
get session() {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
const defaultSession = Object.assign({}, this.options.defaultSession);
|
|
23
|
+
let session = (_b = (_a = this.ctx.session) === null || _a === void 0 ? void 0 : _a.__scenes) !== null && _b !== void 0 ? _b : defaultSession;
|
|
24
|
+
if (session.expires !== undefined && session.expires < now()) {
|
|
25
|
+
session = defaultSession;
|
|
26
|
+
}
|
|
27
|
+
if (this.ctx.session === undefined) {
|
|
28
|
+
this.ctx.session = { __scenes: session };
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
this.ctx.session.__scenes = session;
|
|
32
|
+
}
|
|
33
|
+
return session;
|
|
34
|
+
}
|
|
35
|
+
get state() {
|
|
36
|
+
var _a;
|
|
37
|
+
var _b;
|
|
38
|
+
return ((_a = (_b = this.session).state) !== null && _a !== void 0 ? _a : (_b.state = {}));
|
|
39
|
+
}
|
|
40
|
+
set state(value) {
|
|
41
|
+
this.session.state = { ...value };
|
|
42
|
+
}
|
|
43
|
+
get current() {
|
|
44
|
+
var _a;
|
|
45
|
+
const sceneId = (_a = this.session.current) !== null && _a !== void 0 ? _a : this.options.default;
|
|
46
|
+
return sceneId === undefined || !this.scenes.has(sceneId)
|
|
47
|
+
? undefined
|
|
48
|
+
: this.scenes.get(sceneId);
|
|
49
|
+
}
|
|
50
|
+
reset() {
|
|
51
|
+
if (this.ctx.session !== undefined)
|
|
52
|
+
this.ctx.session.__scenes = Object.assign({}, this.options.defaultSession);
|
|
53
|
+
}
|
|
54
|
+
async enter(sceneId, initialState = {}, silent = false) {
|
|
55
|
+
var _a, _b;
|
|
56
|
+
if (!this.scenes.has(sceneId)) {
|
|
57
|
+
throw new Error(`Can't find scene: ${sceneId}`);
|
|
58
|
+
}
|
|
59
|
+
if (!silent) {
|
|
60
|
+
await this.leave();
|
|
61
|
+
}
|
|
62
|
+
debug('Entering scene', sceneId, initialState, silent);
|
|
63
|
+
this.session.current = sceneId;
|
|
64
|
+
this.state = initialState;
|
|
65
|
+
const ttl = (_b = (_a = this.current) === null || _a === void 0 ? void 0 : _a.ttl) !== null && _b !== void 0 ? _b : this.options.ttl;
|
|
66
|
+
if (ttl !== undefined) {
|
|
67
|
+
this.session.expires = now() + ttl;
|
|
68
|
+
}
|
|
69
|
+
if (this.current === undefined || silent) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const handler = 'enterMiddleware' in this.current &&
|
|
73
|
+
typeof this.current.enterMiddleware === 'function'
|
|
74
|
+
? this.current.enterMiddleware()
|
|
75
|
+
: this.current.middleware();
|
|
76
|
+
return await handler(this.ctx, noop);
|
|
77
|
+
}
|
|
78
|
+
reenter() {
|
|
79
|
+
return this.session.current === undefined
|
|
80
|
+
? undefined
|
|
81
|
+
: this.enter(this.session.current, this.state);
|
|
82
|
+
}
|
|
83
|
+
async leave() {
|
|
84
|
+
if (this.leaving)
|
|
85
|
+
return;
|
|
86
|
+
debug('Leaving scene');
|
|
87
|
+
try {
|
|
88
|
+
this.leaving = true;
|
|
89
|
+
if (this.current === undefined) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const handler = 'leaveMiddleware' in this.current &&
|
|
93
|
+
typeof this.current.leaveMiddleware === 'function'
|
|
94
|
+
? this.current.leaveMiddleware()
|
|
95
|
+
: composer_1.default.passThru();
|
|
96
|
+
await handler(this.ctx, noop);
|
|
97
|
+
return this.reset();
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
this.leaving = false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.default = SceneContextScene;
|
|
105
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @see https://github.com/telegraf/telegraf/issues/705#issuecomment-549056045
|
|
4
|
+
* @see https://www.npmjs.com/package/telegraf-stateless-question
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.WizardContextWizard = exports.WizardScene = exports.BaseScene = exports.SceneContextScene = exports.Stage = void 0;
|
|
12
|
+
var stage_1 = require("./stage");
|
|
13
|
+
Object.defineProperty(exports, "Stage", { enumerable: true, get: function () { return stage_1.Stage; } });
|
|
14
|
+
var context_1 = require("./context");
|
|
15
|
+
Object.defineProperty(exports, "SceneContextScene", { enumerable: true, get: function () { return __importDefault(context_1).default; } });
|
|
16
|
+
var base_1 = require("./base");
|
|
17
|
+
Object.defineProperty(exports, "BaseScene", { enumerable: true, get: function () { return base_1.BaseScene; } });
|
|
18
|
+
var wizard_1 = require("./wizard");
|
|
19
|
+
Object.defineProperty(exports, "WizardScene", { enumerable: true, get: function () { return wizard_1.WizardScene; } });
|
|
20
|
+
var context_2 = require("./wizard/context");
|
|
21
|
+
Object.defineProperty(exports, "WizardContextWizard", { enumerable: true, get: function () { return __importDefault(context_2).default; } });
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Stage = void 0;
|
|
7
|
+
const session_1 = require("../session");
|
|
8
|
+
const context_1 = __importDefault(require("./context"));
|
|
9
|
+
const composer_1 = require("../composer");
|
|
10
|
+
class Stage extends composer_1.Composer {
|
|
11
|
+
constructor(scenes = [], options) {
|
|
12
|
+
super();
|
|
13
|
+
this.options = { ...options };
|
|
14
|
+
this.scenes = new Map();
|
|
15
|
+
scenes.forEach((scene) => this.register(scene));
|
|
16
|
+
}
|
|
17
|
+
register(...scenes) {
|
|
18
|
+
scenes.forEach((scene) => {
|
|
19
|
+
if ((scene === null || scene === void 0 ? void 0 : scene.id) == null || typeof scene.middleware !== 'function') {
|
|
20
|
+
throw new Error('telegraf: Unsupported scene');
|
|
21
|
+
}
|
|
22
|
+
this.scenes.set(scene.id, scene);
|
|
23
|
+
});
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
middleware() {
|
|
27
|
+
const handler = composer_1.Composer.compose([
|
|
28
|
+
(ctx, next) => {
|
|
29
|
+
const scenes = this.scenes;
|
|
30
|
+
const scene = new context_1.default(ctx, scenes, this.options);
|
|
31
|
+
ctx.scene = scene;
|
|
32
|
+
return next();
|
|
33
|
+
},
|
|
34
|
+
super.middleware(),
|
|
35
|
+
composer_1.Composer.lazy((ctx) => { var _a; return (_a = ctx.scene.current) !== null && _a !== void 0 ? _a : composer_1.Composer.passThru(); }),
|
|
36
|
+
]);
|
|
37
|
+
return composer_1.Composer.optional(session_1.isSessionContext, handler);
|
|
38
|
+
}
|
|
39
|
+
static enter(...args) {
|
|
40
|
+
return (ctx) => ctx.scene.enter(...args);
|
|
41
|
+
}
|
|
42
|
+
static reenter(...args) {
|
|
43
|
+
return (ctx) => ctx.scene.reenter(...args);
|
|
44
|
+
}
|
|
45
|
+
static leave(...args) {
|
|
46
|
+
return (ctx) => ctx.scene.leave(...args);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.Stage = Stage;
|
|
50
|
+
//# sourceMappingURL=stage.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class WizardContextWizard {
|
|
4
|
+
constructor(ctx, steps) {
|
|
5
|
+
var _a;
|
|
6
|
+
this.ctx = ctx;
|
|
7
|
+
this.steps = steps;
|
|
8
|
+
this.state = ctx.scene.state;
|
|
9
|
+
this.cursor = (_a = ctx.scene.session.cursor) !== null && _a !== void 0 ? _a : 0;
|
|
10
|
+
}
|
|
11
|
+
get step() {
|
|
12
|
+
return this.steps[this.cursor];
|
|
13
|
+
}
|
|
14
|
+
get cursor() {
|
|
15
|
+
return this.ctx.scene.session.cursor;
|
|
16
|
+
}
|
|
17
|
+
set cursor(cursor) {
|
|
18
|
+
this.ctx.scene.session.cursor = cursor;
|
|
19
|
+
}
|
|
20
|
+
selectStep(index) {
|
|
21
|
+
this.cursor = index;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
next() {
|
|
25
|
+
return this.selectStep(this.cursor + 1);
|
|
26
|
+
}
|
|
27
|
+
back() {
|
|
28
|
+
return this.selectStep(this.cursor - 1);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.default = WizardContextWizard;
|
|
32
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.WizardScene = void 0;
|
|
7
|
+
const base_1 = __importDefault(require("../base"));
|
|
8
|
+
const context_1 = __importDefault(require("./context"));
|
|
9
|
+
const composer_1 = __importDefault(require("../../composer"));
|
|
10
|
+
class WizardScene extends base_1.default {
|
|
11
|
+
constructor(id, options, ...steps) {
|
|
12
|
+
let opts;
|
|
13
|
+
let s;
|
|
14
|
+
if (typeof options === 'function' || 'middleware' in options) {
|
|
15
|
+
opts = undefined;
|
|
16
|
+
s = [options, ...steps];
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
opts = options;
|
|
20
|
+
s = steps;
|
|
21
|
+
}
|
|
22
|
+
super(id, opts);
|
|
23
|
+
this.steps = s;
|
|
24
|
+
}
|
|
25
|
+
middleware() {
|
|
26
|
+
return composer_1.default.compose([
|
|
27
|
+
(ctx, next) => {
|
|
28
|
+
ctx.wizard = new context_1.default(ctx, this.steps);
|
|
29
|
+
return next();
|
|
30
|
+
},
|
|
31
|
+
super.middleware(),
|
|
32
|
+
(ctx, next) => {
|
|
33
|
+
if (ctx.wizard.step === undefined) {
|
|
34
|
+
ctx.wizard.selectStep(0);
|
|
35
|
+
return ctx.scene.leave();
|
|
36
|
+
}
|
|
37
|
+
return composer_1.default.unwrap(ctx.wizard.step)(ctx, next);
|
|
38
|
+
},
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
enterMiddleware() {
|
|
42
|
+
return composer_1.default.compose([this.enterHandler, this.middleware()]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.WizardScene = WizardScene;
|
|
46
|
+
//# sourceMappingURL=index.js.map
|
package/lib/scenes.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./scenes/index.js"), exports);
|
|
18
|
+
//# sourceMappingURL=scenes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scenes.js","sourceRoot":"","sources":["../src/scenes.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAiC"}
|
package/lib/session.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isSessionContext = exports.MemorySessionStore = exports.session = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const debug = (0, debug_1.default)('telegraf:session');
|
|
9
|
+
/**
|
|
10
|
+
* Returns middleware that adds `ctx.session` for storing arbitrary state per session key.
|
|
11
|
+
*
|
|
12
|
+
* The default `getSessionKey` is `${ctx.from.id}:${ctx.chat.id}`.
|
|
13
|
+
* If either `ctx.from` or `ctx.chat` is `undefined`, default session key and thus `ctx.session` are also `undefined`.
|
|
14
|
+
*
|
|
15
|
+
* > ⚠️ Session data is kept only in memory by default, which means that all data will be lost when the process is terminated.
|
|
16
|
+
* >
|
|
17
|
+
* > If you want to persist data across process restarts, or share it among multiple instances, you should use
|
|
18
|
+
* [@telegraf/session](https://www.npmjs.com/package/@telegraf/session), or pass custom `storage`.
|
|
19
|
+
*
|
|
20
|
+
* @see {@link https://github.com/feathers-studio/telegraf-docs/blob/b694bcc36b4f71fb1cd650a345c2009ab4d2a2a5/guide/session.md Telegraf Docs | Session}
|
|
21
|
+
* @see {@link https://github.com/feathers-studio/telegraf-docs/blob/master/examples/session-bot.ts Example}
|
|
22
|
+
*/
|
|
23
|
+
function session(options) {
|
|
24
|
+
var _a, _b, _c;
|
|
25
|
+
const prop = (_a = options === null || options === void 0 ? void 0 : options.property) !== null && _a !== void 0 ? _a : 'session';
|
|
26
|
+
const getSessionKey = (_b = options === null || options === void 0 ? void 0 : options.getSessionKey) !== null && _b !== void 0 ? _b : defaultGetSessionKey;
|
|
27
|
+
const store = (_c = options === null || options === void 0 ? void 0 : options.store) !== null && _c !== void 0 ? _c : new MemorySessionStore();
|
|
28
|
+
// caches value from store in-memory while simultaneous updates share it
|
|
29
|
+
// when counter reaches 0, the cached ref will be freed from memory
|
|
30
|
+
const cache = new Map();
|
|
31
|
+
// temporarily stores concurrent requests
|
|
32
|
+
const concurrents = new Map();
|
|
33
|
+
// this function must be handled with care
|
|
34
|
+
// read full description on the original PR: https://github.com/telegraf/telegraf/pull/1713
|
|
35
|
+
// make sure to update the tests in test/session.js if you make any changes or fix bugs here
|
|
36
|
+
return async (ctx, next) => {
|
|
37
|
+
var _a;
|
|
38
|
+
const updId = ctx.update.update_id;
|
|
39
|
+
let released = false;
|
|
40
|
+
function releaseChecks() {
|
|
41
|
+
if (released && process.env.EXPERIMENTAL_SESSION_CHECKS)
|
|
42
|
+
throw new Error("Session was accessed or assigned to after the middleware chain exhausted. This is a bug in your code. You're probably accessing session asynchronously and missing awaits.");
|
|
43
|
+
}
|
|
44
|
+
// because this is async, requests may still race here, but it will get autocorrected at (1)
|
|
45
|
+
// v5 getSessionKey should probably be synchronous to avoid that
|
|
46
|
+
const key = await getSessionKey(ctx);
|
|
47
|
+
if (!key) {
|
|
48
|
+
// Leaving this here could be useful to check for `prop in ctx` in future middleware
|
|
49
|
+
ctx[prop] = undefined;
|
|
50
|
+
return await next();
|
|
51
|
+
}
|
|
52
|
+
let cached = cache.get(key);
|
|
53
|
+
if (cached) {
|
|
54
|
+
debug(`(${updId}) found cached session, reusing from cache`);
|
|
55
|
+
++cached.counter;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
debug(`(${updId}) did not find cached session`);
|
|
59
|
+
// if another concurrent request has already sent a store request, fetch that instead
|
|
60
|
+
let promise = concurrents.get(key);
|
|
61
|
+
if (promise)
|
|
62
|
+
debug(`(${updId}) found a concurrent request, reusing promise`);
|
|
63
|
+
else {
|
|
64
|
+
debug(`(${updId}) fetching from upstream store`);
|
|
65
|
+
promise = store.get(key);
|
|
66
|
+
}
|
|
67
|
+
// synchronously store promise so concurrent requests can share response
|
|
68
|
+
concurrents.set(key, promise);
|
|
69
|
+
const upstream = await promise;
|
|
70
|
+
// all concurrent awaits will have promise in their closure, safe to remove now
|
|
71
|
+
concurrents.delete(key);
|
|
72
|
+
debug(`(${updId}) updating cache`);
|
|
73
|
+
// another request may have beaten us to the punch
|
|
74
|
+
const c = cache.get(key);
|
|
75
|
+
if (c) {
|
|
76
|
+
// another request did beat us to the punch
|
|
77
|
+
c.counter++;
|
|
78
|
+
// (1) preserve cached reference; in-memory reference is always newer than from store
|
|
79
|
+
cached = c;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
// we're the first, so we must cache the reference
|
|
83
|
+
cached = { ref: upstream !== null && upstream !== void 0 ? upstream : (_a = options === null || options === void 0 ? void 0 : options.defaultSession) === null || _a === void 0 ? void 0 : _a.call(options, ctx), counter: 1 };
|
|
84
|
+
cache.set(key, cached);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// TS already knows cached is always defined by this point, but does not guard cached.
|
|
88
|
+
// It will, however, guard `c` here.
|
|
89
|
+
const c = cached;
|
|
90
|
+
let touched = false;
|
|
91
|
+
Object.defineProperty(ctx, prop, {
|
|
92
|
+
get() {
|
|
93
|
+
releaseChecks();
|
|
94
|
+
touched = true;
|
|
95
|
+
return c.ref;
|
|
96
|
+
},
|
|
97
|
+
set(value) {
|
|
98
|
+
releaseChecks();
|
|
99
|
+
touched = true;
|
|
100
|
+
c.ref = value;
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
try {
|
|
104
|
+
await next();
|
|
105
|
+
released = true;
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
if (--c.counter === 0) {
|
|
109
|
+
// decrement to avoid memory leak
|
|
110
|
+
debug(`(${updId}) refcounter reached 0, removing cached`);
|
|
111
|
+
cache.delete(key);
|
|
112
|
+
}
|
|
113
|
+
debug(`(${updId}) middlewares completed, checking session`);
|
|
114
|
+
// only update store if ctx.session was touched
|
|
115
|
+
if (touched)
|
|
116
|
+
if (c.ref == null) {
|
|
117
|
+
debug(`(${updId}) ctx.${prop} missing, removing from store`);
|
|
118
|
+
await store.delete(key);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
debug(`(${updId}) ctx.${prop} found, updating store`);
|
|
122
|
+
await store.set(key, c.ref);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
exports.session = session;
|
|
128
|
+
function defaultGetSessionKey(ctx) {
|
|
129
|
+
var _a, _b;
|
|
130
|
+
const fromId = (_a = ctx.from) === null || _a === void 0 ? void 0 : _a.id;
|
|
131
|
+
const chatId = (_b = ctx.chat) === null || _b === void 0 ? void 0 : _b.id;
|
|
132
|
+
if (fromId == null || chatId == null)
|
|
133
|
+
return undefined;
|
|
134
|
+
return `${fromId}:${chatId}`;
|
|
135
|
+
}
|
|
136
|
+
/** @deprecated Use `Map` */
|
|
137
|
+
class MemorySessionStore {
|
|
138
|
+
constructor(ttl = Infinity) {
|
|
139
|
+
this.ttl = ttl;
|
|
140
|
+
this.store = new Map();
|
|
141
|
+
}
|
|
142
|
+
get(name) {
|
|
143
|
+
const entry = this.store.get(name);
|
|
144
|
+
if (entry == null) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
else if (entry.expires < Date.now()) {
|
|
148
|
+
this.delete(name);
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
return entry.session;
|
|
152
|
+
}
|
|
153
|
+
set(name, value) {
|
|
154
|
+
const now = Date.now();
|
|
155
|
+
this.store.set(name, { session: value, expires: now + this.ttl });
|
|
156
|
+
}
|
|
157
|
+
delete(name) {
|
|
158
|
+
this.store.delete(name);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.MemorySessionStore = MemorySessionStore;
|
|
162
|
+
/** @deprecated session can use custom properties now. Directly use `'session' in ctx` instead */
|
|
163
|
+
function isSessionContext(ctx) {
|
|
164
|
+
return 'session' in ctx;
|
|
165
|
+
}
|
|
166
|
+
exports.isSessionContext = isSessionContext;
|
|
167
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":";;;;;;AAGA,kDAAqB;AACrB,MAAM,KAAK,GAAG,IAAA,eAAC,EAAC,kBAAkB,CAAC,CAAA;AA6BnC;;;;;;;;;;;;;GAaG;AACH,SAAgB,OAAO,CAMrB,OAAiC;;IACjC,MAAM,IAAI,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,mCAAK,SAAe,CAAA;IAClD,MAAM,aAAa,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,aAAa,mCAAI,oBAAoB,CAAA;IACpE,MAAM,KAAK,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,mCAAI,IAAI,kBAAkB,EAAE,CAAA;IACxD,wEAAwE;IACxE,mEAAmE;IACnE,MAAM,KAAK,GAAG,IAAI,GAAG,EAAwC,CAAA;IAC7D,yCAAyC;IACzC,MAAM,WAAW,GAAG,IAAI,GAAG,EAAuC,CAAA;IAElE,0CAA0C;IAC1C,2FAA2F;IAC3F,4FAA4F;IAC5F,OAAO,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;;QACzB,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,SAAS,CAAA;QAElC,IAAI,QAAQ,GAAG,KAAK,CAAA;QAEpB,SAAS,aAAa;YACpB,IAAI,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,2BAA2B;gBACrD,MAAM,IAAI,KAAK,CACb,4KAA4K,CAC7K,CAAA;QACL,CAAC;QAED,4F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