This repository has been archived by the owner on Sep 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.0.2
- Loading branch information
Showing
10 changed files
with
247 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const crypto = require('crypto') | ||
|
||
class RequestController { | ||
constructor (queue) { | ||
this.queue = queue | ||
this.callbacks = {} | ||
} | ||
|
||
/** | ||
* Add request to controller | ||
* @param {Request} request | ||
*/ | ||
add (request) { | ||
let id = crypto.createHmac('sha256', 'https://vk.cc/7A4bYf') | ||
.update(request.method + Date.now() + Math.random()) | ||
.digest('hex') | ||
|
||
request.id = id | ||
|
||
let callback = {} | ||
|
||
let promise = new Promise((resolve, reject) => { | ||
callback.resolve = resolve | ||
callback.reject = reject | ||
}) | ||
|
||
this.callbacks[id] = callback | ||
this.queue.requests.push(request) | ||
|
||
return promise | ||
} | ||
|
||
/** | ||
* Emits when request successed | ||
* @param {String} id ID of request | ||
* @param {Object} data Response data | ||
*/ | ||
complete (id, data) { | ||
let request = this.callbacks[id] | ||
request.resolve(data) | ||
} | ||
|
||
/** | ||
* Emits when request failed | ||
* @param {String} id ID of request | ||
* @param {Object} data Response data | ||
*/ | ||
error (id, data) { | ||
let request = this.callbacks[id] | ||
|
||
request.reject(data) | ||
} | ||
} | ||
|
||
module.exports = RequestController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
const API = require('./api/') | ||
const API = require('./api') | ||
|
||
module.exports = API | ||
class Next { | ||
/** | ||
* Creates an insance of VK-Next | ||
* @param {String} token VK API token | ||
* @param {BaseQueue} queue Queue used for requests | ||
* @param {Boolean} isGroup Is token recieved by group | ||
*/ | ||
constructor (token, queue, isGroup) { | ||
this.queue = queue | ||
this.api = new API(token) | ||
|
||
this.queue.token = token | ||
this.queue.isGroup = isGroup | ||
|
||
this.queue.start() | ||
this.api.init(this.queue) | ||
} | ||
} | ||
|
||
module.exports = Next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Request { | ||
/** | ||
* Create VK API requset | ||
* @param {String} method Request method | ||
* @param {Object} params Request params | ||
*/ | ||
constructor (method, params = []) { | ||
this.method = method | ||
this.params = params | ||
} | ||
|
||
buildString () { | ||
let params = [] | ||
|
||
for (let key of Object.keys(this.params)) { | ||
params.push(`{"${key}": ${this.params[key]}}`) | ||
} | ||
|
||
return `API.${this.method}(${params.toString()})` | ||
} | ||
} | ||
|
||
module.exports = Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const RequestController = require('../controllers/RequestController') | ||
const request = require('../utils/request') | ||
|
||
class BaseQueue { | ||
/** | ||
* @param {Boolean} isGroup Is token recieved for group | ||
*/ | ||
constructor () { | ||
this.controller = new RequestController(this) | ||
} | ||
|
||
get token () { | ||
return this._token | ||
} | ||
|
||
set token (token) { | ||
this._token = token | ||
} | ||
|
||
get isGroup () { | ||
return this._isGroup | ||
} | ||
|
||
set isGroup (isGroup) { | ||
this._isGroup = isGroup | ||
} | ||
|
||
/** | ||
* Add request to queue | ||
*/ | ||
add () { | ||
throw new Error('Not implemented') | ||
} | ||
|
||
/** | ||
* Start processing queue | ||
*/ | ||
start () { | ||
throw new Error('Not implemented') | ||
} | ||
|
||
/** | ||
* Stop processing queue | ||
*/ | ||
stop () { | ||
throw new Error('Not implemented') | ||
} | ||
|
||
/** | ||
* Get chunk | ||
*/ | ||
_getChunk () { | ||
throw new Error('Not implemented') | ||
} | ||
|
||
/** | ||
* Process a chunk | ||
* @param {Object} chunk Array of requests | ||
*/ | ||
async _process (chunk) { | ||
let requests = [] | ||
chunk.map(request => { | ||
requests.push(request.buildString()) | ||
}) | ||
|
||
let code = `return([${requests}]);` | ||
|
||
if (requests.length > 0) { | ||
let executeResponse = await request('execute', { | ||
code | ||
}, this.token) | ||
|
||
let parsedResponse = JSON.parse(executeResponse) | ||
|
||
if (parsedResponse['error']) { | ||
throw new Error(parsedResponse['error']['error_msg']) | ||
} | ||
|
||
let responses = parsedResponse['response'] | ||
let errors = parsedResponse['execute_errors'] | ||
let errorResponses = [] | ||
|
||
for (let i in responses) { | ||
let response = responses[i] | ||
if (response === false) { | ||
errorResponses.push(chunk[i].id) | ||
} else { | ||
this.controller.complete(chunk[i].id, response) | ||
} | ||
} | ||
|
||
for (let i in errors) { | ||
let error = errors[i] | ||
|
||
this.controller.error(errorResponses[i], error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = BaseQueue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const BaseQueue = require('./BaseQueue') | ||
const Request = require('../models/Request') | ||
|
||
class LocalQueue extends BaseQueue { | ||
constructor () { | ||
super() | ||
|
||
this.requests = [] | ||
} | ||
|
||
add (method, params) { | ||
let promise = this.controller.add(new Request(method, params)) | ||
|
||
return promise | ||
} | ||
|
||
start () { | ||
this.interval = setInterval(() => { | ||
let chunk = this._getChunk() | ||
this._process(chunk) | ||
}, 1000 / this.isGroup ? 20 : 3) | ||
} | ||
|
||
stop () { | ||
if (this.interval) clearInterval(this.interval) | ||
} | ||
|
||
_getChunk () { | ||
return this.requests.splice(0, 24) | ||
} | ||
} | ||
|
||
module.exports = LocalQueue |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters