import { Service } from "../lib/service" import { MemoryLockService } from "./memoryLockService" import { MongodbService } from "./mongodbService" import * as mongodb from "mongodb" import * as Leaf from "leaf-ts" import * as UUID from "uuid" export type Callback = (e?: E, result?: T) => void export const Errors = Leaf.ErrorDoc.build({ UnknownError: {}, NotFound: {}, }) export class TTLSessionStorage extends Service { name = "TTLSessionStorage" dependencies = ["MongodbService"] services: { MongodbService: MongodbService } constructor() { super(null, null) } initialize() { this.ttlCollection = this.services.MongodbService.db.collection(this.collectionName) this.ttlCollection.createIndex({ expire: 1 }, { expireAfterSeconds: 0 }) this.ttlCollection.createIndex({ id: 1 }, { unique: true }) } public defaultTTL = 1000 * 60 * 24 private collectionName = "ttl" private ttlCollection: mongodb.Collection createTTLSession(option: { id?: string data: any ttl?: number }, callback: Callback) { let ttl = option.ttl || this.defaultTTL let expire = new Date(Date.now() + ttl) let id = option.id || UUID.v4() let session: TTLSessionData = { id, data: option.data, expire } this.ttlCollection.insertOne(session, (err) => { callback(err, id) }) } updateOrCreateTTLSession(option: { id: string data: T ttl: number }, callback: Callback<{ id: string data: T expire: Date }>) { let ttl = option.ttl || this.defaultTTL let updates = {} updates = { $setOnInsert: { id: option.id, expire: new Date(Date.now() + ttl) }, $set: { data: option.data, } } // user specified ttl, means we should update the ttl as well if (option.ttl) { // have to $set and $setOnInsert can't be both exists delete updates["$setOnInsert"]["expire"] updates["$set"]["expire"] = new Date(Date.now() + option.ttl) } this.ttlCollection.findOneAndUpdate({ id: option.id }, updates, { returnOriginal: false, upsert: true }, (err, doc) => { if (err) { callback(err) return } callback(null, doc.value) }) } getOrCreateTTLSession(option: { id: string data: T ttl: number }, callback: Callback<{ id: string data: T expire: Date }>) { throw new Error("Deprecated, please using touchOrCreateTTLSession or updateOrCreateTTLSession instead") } touchOrCreateTTLSession(option: { id: string data: T ttl: number }, callback: Callback<{ id: string data: T expire: Date }>) { let ttl = option.ttl || this.defaultTTL let updates = {} // never update data using this function updates = { $setOnInsert: { id: option.id, data: option.data, expire: new Date(Date.now() + ttl), }, } // updates expires // so using $set not $setOnInsert if (option.ttl) { // have to $set and $setOnInsert can't be both exists delete updates["$setOnInsert"]["expire"] updates["$set"] = { expire: new Date(Date.now() + option.ttl) } } this.ttlCollection.findOneAndUpdate({ id: option.id }, updates, { returnOriginal: false, upsert: true }, (err, doc) => { if (err) { callback(err) return } callback(null, doc.value) }) } deleteTTLSession(option: { id: string }, callback: Callback) { this.ttlCollection.findOneAndDelete({ id: option.id }, (err) => { callback() }) } updateTTLSession(option: { id: string data: any refreshTTL?: number }, callback: Callback) { let updates = { data: option.data } if (typeof option.refreshTTL == "number") { updates["expire"] = new Date(Date.now() + option.refreshTTL) } this.ttlCollection.findOneAndUpdate({ id: option.id }, { $set: updates }, (err, result) => { if (err) { callback(err) return } if (result.ok !== 1) { callback(new Errors.UnknownError("Fail to find and replace TTLSession", { via: result.lastErrorObject })) return } callback(null, result.value) }) } getTTLSessionDataById(option: { id: string }, callback: Callback) { this.ttlCollection.findOne({ id: option.id }, (err, result: TTLSessionData) => { if (err) { callback(err) return } if (!result) { callback(new Errors.NotFound()) return } let data: T = result.data callback(null, data) }) } } export interface TTLSessionData { id: string data: any expire: Date }