123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.SessionTokenCache = void 0;
- var _node = _interopRequireDefault(require("parse/node"));
- var _lruCache = require("lru-cache");
- var _logger = _interopRequireDefault(require("../logger"));
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- function userForSessionToken(sessionToken) {
- var q = new _node.default.Query('_Session');
- q.equalTo('sessionToken', sessionToken);
- return q.first({
- useMasterKey: true
- }).then(function (session) {
- if (!session) {
- return Promise.reject('No session found for session token');
- }
- return session.get('user');
- });
- }
- class SessionTokenCache {
- constructor(timeout = 30 * 24 * 60 * 60 * 1000, maxSize = 10000) {
- this.cache = new _lruCache.LRUCache({
- max: maxSize,
- ttl: timeout
- });
- }
- getUserId(sessionToken) {
- if (!sessionToken) {
- return Promise.reject('Empty sessionToken');
- }
- const userId = this.cache.get(sessionToken);
- if (userId) {
- _logger.default.verbose('Fetch userId %s of sessionToken %s from Cache', userId, sessionToken);
- return Promise.resolve(userId);
- }
- return userForSessionToken(sessionToken).then(user => {
- _logger.default.verbose('Fetch userId %s of sessionToken %s from Parse', user.id, sessionToken);
- const userId = user.id;
- this.cache.set(sessionToken, userId);
- return Promise.resolve(userId);
- }, error => {
- _logger.default.error('Can not fetch userId for sessionToken %j, error %j', sessionToken, error);
- return Promise.reject(error);
- });
- }
- }
- exports.SessionTokenCache = SessionTokenCache;
- //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfbm9kZSIsIl9pbnRlcm9wUmVxdWlyZURlZmF1bHQiLCJyZXF1aXJlIiwiX2xydUNhY2hlIiwiX2xvZ2dlciIsImUiLCJfX2VzTW9kdWxlIiwiZGVmYXVsdCIsInVzZXJGb3JTZXNzaW9uVG9rZW4iLCJzZXNzaW9uVG9rZW4iLCJxIiwiUGFyc2UiLCJRdWVyeSIsImVxdWFsVG8iLCJmaXJzdCIsInVz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