diff --git a/lib/plankton/plankton.d.ts b/lib/plankton/plankton.d.ts index e094935..d03ea69 100644 --- a/lib/plankton/plankton.d.ts +++ b/lib/plankton/plankton.d.ts @@ -1,11 +1,11 @@ /** * @author fenris */ -declare type int = number; +type int = number; /** * @author fenris */ -declare type float = number; +type float = number; declare var process: any; declare var require: any; declare class Buffer { @@ -22,7 +22,7 @@ declare namespace lib_plankton.base { /** * @author fenris */ -declare type type_pseudopointer = { +type type_pseudopointer = { value: type_value; }; /** @@ -2315,7 +2315,7 @@ declare namespace lib_plankton.storage.memory { clear(): Promise; write(key: any, value: any): Promise; delete(key: any): Promise; - read(key: any): Promise; + read(key: any): Promise>; search(term: any): Promise<{ key: string; preview: string; @@ -3073,22 +3073,25 @@ declare namespace lib_plankton.session { }; /** */ - function begin(name: string, options?: { + function begin(name: string, { "lifetime": lifetime, "data": data, }?: { lifetime?: int; data?: any; }): Promise; /** */ - function get(key: string): Promise; + function get(key: string, { "prolongation": prolongation, }?: { + prolongation?: (null | int); + }): Promise; /** */ function end(key: string): Promise; /** */ - function setup(options?: { + function setup({ "key_length": key_length, "key_max_attempts": key_max_attempts, "default_lifetime": default_lifetime, "default_prolongation": default_prolongation, "data_chest": data_chest, "clear": clear, }?: { key_length?: int; key_max_attempts?: int; default_lifetime?: int; + default_prolongation?: (null | int); data_chest?: lib_plankton.storage.type_chest; clear?: boolean; }): Promise; diff --git a/lib/plankton/plankton.js b/lib/plankton/plankton.js index 58e904a..d423569 100644 --- a/lib/plankton/plankton.js +++ b/lib/plankton/plankton.js @@ -1486,7 +1486,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) { function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); - while (_) try { + while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { @@ -6814,7 +6814,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) { function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); - while (_) try { + while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { @@ -10266,7 +10266,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) { function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); - while (_) try { + while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { @@ -10325,21 +10325,37 @@ var lib_plankton; } /** */ - function begin(name, options) { - if (options === void 0) { options = {}; } + function session_encode(session) { + return { + "key": session.key, + "name": session.name, + "expiry": Math.floor(session.expiry), + "data": JSON.stringify(session.data) + }; + } + /** + */ + function session_decode(session_raw) { + return { + "key": session_raw.key, + "name": session_raw.name, + "expiry": Math.floor(session_raw.expiry), + "data": JSON.parse(session_raw.data) + }; + } + /** + */ + function begin(name, _a) { + var _b = _a === void 0 ? {} : _a, _c = _b["lifetime"], lifetime = _c === void 0 ? _conf.default_lifetime : _c, _d = _b["data"], data = _d === void 0 ? null : _d; return __awaiter(this, void 0, void 0, function () { var key, attempts, session_old, error_1, session_2, session_raw; - return __generator(this, function (_a) { - switch (_a.label) { + return __generator(this, function (_e) { + switch (_e.label) { case 0: - options = Object.assign({ - "lifetime": _conf.default_lifetime, - "data": null - }, options); check_conf(); key = null; attempts = 0; - _a.label = 1; + _e.label = 1; case 1: if (!true) return [3 /*break*/, 6]; attempts += 1; @@ -10349,15 +10365,15 @@ var lib_plankton; function (x) { return x.join(""); }, ]); session_old = null; - _a.label = 2; + _e.label = 2; case 2: - _a.trys.push([2, 4, , 5]); + _e.trys.push([2, 4, , 5]); return [4 /*yield*/, _conf.data_chest.read(key)]; case 3: - session_old = _a.sent(); + session_old = _e.sent(); return [3 /*break*/, 5]; case 4: - error_1 = _a.sent(); + error_1 = _e.sent(); session_old = null; return [3 /*break*/, 5]; case 5: @@ -10383,26 +10399,45 @@ var lib_plankton; session_2 = { "key": key, "name": name, - "expiry": (lib_plankton.base.get_current_timestamp() + options.lifetime), - "data": options.data + "expiry": (lib_plankton.base.get_current_timestamp() + lifetime), + "data": data }; - session_raw = { - "key": session_2.key, - "name": session_2.name, - "expiry": Math.floor(session_2.expiry), - "data": JSON.stringify(session_2.data) - }; - lib_plankton.call.timeout(function () { - lib_plankton.log.info("session_dropping_due_to_being_expired", { - "key": key, - "name": name, - "lifetime": options.lifetime - }); - end(key); - }, options.lifetime); + session_raw = session_encode(session_2); + /* + lib_plankton.call.timeout( + () => { + lib_plankton.log.info( + "session.dropping_due_to_being_expired", + { + "key": key, + "name": name, + "lifetime": lifetime, + } + ); + end(key); + }, + lifetime + ); + */ return [4 /*yield*/, _conf.data_chest.write(key, session_raw)]; case 8: - _a.sent(); + /* + lib_plankton.call.timeout( + () => { + lib_plankton.log.info( + "session.dropping_due_to_being_expired", + { + "key": key, + "name": name, + "lifetime": lifetime, + } + ); + end(key); + }, + lifetime + ); + */ + _e.sent(); return [2 /*return*/, Promise.resolve(key)]; } }); @@ -10411,29 +10446,51 @@ var lib_plankton; session_1.begin = begin; /** */ - function get(key) { - check_conf(); - return (_conf.data_chest.read(key) - .then(function (session_raw) { - var session = { - "key": session_raw["key"], - "name": session_raw["name"], - "expiry": session_raw["expiry"], - "data": JSON.parse(session_raw["data"]) - }; - var now = lib_plankton.base.get_current_timestamp(); - if (now > session.expiry) { - lib_plankton.log.info("session_dropping_due_to_being_stale", { - "key": session.key, - "name": session.name - }); - end(key); - return Promise.reject(); - } - else { - return Promise.resolve(session); - } - })); + function get(key, _a) { + var _b = _a === void 0 ? {} : _a, _c = _b["prolongation"], prolongation = _c === void 0 ? _conf.default_prolongation : _c; + return __awaiter(this, void 0, void 0, function () { + var session_raw, session, now, expiry_old, expiry_new; + return __generator(this, function (_d) { + switch (_d.label) { + case 0: + check_conf(); + return [4 /*yield*/, _conf.data_chest.read(key)]; + case 1: + session_raw = _d.sent(); + session = session_decode(session_raw); + now = lib_plankton.base.get_current_timestamp(); + if (!(now <= session.expiry)) return [3 /*break*/, 6]; + if (!(prolongation === null)) return [3 /*break*/, 2]; + return [3 /*break*/, 5]; + case 2: + expiry_old = session.expiry; + expiry_new = Math.floor(now + prolongation); + if (!(expiry_new < session.expiry)) return [3 /*break*/, 3]; + return [3 /*break*/, 5]; + case 3: + lib_plankton.log.info("session.prolongating", { + "key": key, + "now": now, + "prolongation": prolongation, + "expiry_old": expiry_old, + "expiry_new": expiry_new + }); + session.expiry = expiry_new; + return [4 /*yield*/, _conf.data_chest.write(key, session_encode(session))]; + case 4: + _d.sent(); + _d.label = 5; + case 5: return [2 /*return*/, Promise.resolve(session)]; + case 6: + lib_plankton.log.info("session.dropping_due_to_being_stale", { + "key": session.key, + "name": session.name + }); + end(key); + return [2 /*return*/, Promise.reject()]; + } + }); + }); } session_1.get = get; /** @@ -10445,29 +10502,23 @@ var lib_plankton; session_1.end = end; /** */ - function setup(options) { - if (options === void 0) { options = {}; } + function setup(_a) { + var _b = _a === void 0 ? {} : _a, _c = _b["key_length"], key_length = _c === void 0 ? 16 : _c, _d = _b["key_max_attempts"], key_max_attempts = _d === void 0 ? 3 : _d, _e = _b["default_lifetime"], default_lifetime = _e === void 0 ? 900 : _e, _f = _b["default_prolongation"], default_prolongation = _f === void 0 ? null : _f, _g = _b["data_chest"], data_chest = _g === void 0 ? lib_plankton.storage.memory.implementation_chest({}) : _g, _h = _b["clear"], clear = _h === void 0 ? false : _h; return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - switch (_a.label) { + return __generator(this, function (_j) { + switch (_j.label) { case 0: - options = Object.assign({ - "key_length": 16, - "key_max_attempts": 3, - "default_lifetime": 900, - "data_chest": lib_plankton.storage.memory.implementation_chest({}), - "clear": false - }, options); _conf = { - "key_length": options.key_length, - "key_max_attempts": options.key_max_attempts, - "default_lifetime": options.default_lifetime, - "data_chest": options.data_chest + "key_length": key_length, + "key_max_attempts": key_max_attempts, + "default_lifetime": default_lifetime, + "default_prolongation": default_prolongation, + "data_chest": data_chest }; - if (!options.clear) return [3 /*break*/, 2]; + if (!clear) return [3 /*break*/, 2]; return [4 /*yield*/, _conf.data_chest.clear()]; case 1: - _a.sent(); + _j.sent(); return [3 /*break*/, 2]; case 2: return [2 /*return*/, Promise.resolve(undefined)]; } @@ -15148,7 +15199,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) { function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); - while (_) try { + while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) {