Comparing version 0.0.1 to 0.0.2
@@ -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; |
@@ -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; |
{ | ||
"name": "0te", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "lib/index.js", | ||
"dependencies": { | ||
"debug": "^4.3.1", | ||
"p-timeout": "^5.0.0", | ||
"safe-compare": "^1.1.4" | ||
"p-timeout": "^5.0.0" | ||
} | ||
} |
109976
2
2714
- Removedsafe-compare@^1.1.4
- Removedbuffer-alloc@1.2.0(transitive)
- Removedbuffer-alloc-unsafe@1.1.0(transitive)
- Removedbuffer-fill@1.0.0(transitive)
- Removedsafe-compare@1.1.4(transitive)