Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Added session util
Browse files Browse the repository at this point in the history
  • Loading branch information
ciricc committed Apr 21, 2018
1 parent 03577fa commit 7b667e2
Showing 1 changed file with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions src/utils/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
"use strict";

const fs = require('fs');



class EasyVKSession {

constructor (vk, dataSession = {}) {

let self = this;

let _props = {
session: dataSession,
path: vk.params.session_file,
vk: vk
}

let canChanged = ["path"];


for (let prop in _props) {
let settings = {
value: _props[prop]
}

if (canChanged.indexOf(prop) != -1) {
settings.configurable = true;
}

Object.defineProperty(self, prop, settings);

}

//Use session data with methods
for (let prop in self.session) {

Object.defineProperty(self, prop, {
enumerable: true,
configurable: true,
value: self.session[prop],
});

}

return self;
}

/*
* This method saved your session in file
*
* @return {Promise}
*
*/

async save () {

let self = this;

return new Promise((resolve, reject) => {

let s;

if (!(self.path)) {
return reject(new Error('You need set path for session file!'));
}

s = JSON.stringify(self);

let buf = Buffer.from(s, 'utf8');

fs.writeFile(self.path, buf, (err) => {

if (err) {
reject(err);
}

return resolve({
vk: self.vk
});

});

});

}


/*
* This function saves your session and cleaning it, make this empty
* @returns Promise
*
*/

async clear () {

let self = this;

return new Promise ((resolve, reject) => {

for (let prop in self) {

Object.defineProperty(self, prop, {
value: undefined,
enumerable: true,
configurable: true
});

}


self.save().then(resolve, reject);

});

}

/*
* This function set up your path session, ou can change sesson path
*
* @param {String} path is absolute path for you file
* @return {Promise}
*
*/

async setPath (path = "") {
let self = this;

return new Promise((resolve, reject) => {

fs.writeFile(path, '', (err) => {

if (err) {
return reject(err);
}



Object.defineProperty(self, 'path', {
configurable: true,
value: path
});


//Update for easyvk functions, for latest releases
self.vk.params.session_file = path;

return resolve({
vk: self.vk
});

});

});
}


}



module.exports = EasyVKSession;

0 comments on commit 7b667e2

Please sign in to comment.