public-vk-api
Advanced tools
Comparing version 0.0.1 to 0.0.2
435
index.js
@@ -1,434 +0,1 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
var _isomorphicFetch = require('isomorphic-fetch'); | ||
var _isomorphicFetch2 = _interopRequireDefault(_isomorphicFetch); | ||
function executeRemoteCodeByUrl(src) { | ||
var script = document.createElement('script'); | ||
script.type = 'text/javascript'; | ||
script.src = src; | ||
document.head.appendChild(script); | ||
} | ||
function encodeToSearchString(options) { | ||
return Object.keys(options).filter(function (key) { | ||
return key && options[key]; | ||
}).reduce(function (string, key) { | ||
return string + '&' + (key + '=' + encodeURIComponent(options[key])); | ||
}, '').slice(1); | ||
} | ||
var Query = (function () { | ||
_createClass(Query, null, [{ | ||
key: 'v', | ||
value: '5.25', | ||
enumerable: true | ||
}]); | ||
function Query(method) { | ||
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var postfix = arguments.length <= 2 || arguments[2] === undefined ? '' : arguments[2]; | ||
_classCallCheck(this, Query); | ||
Object.assign(this, { method: method, opts: _extends({ v: Query.v }, opts), postfix: postfix }); | ||
} | ||
return Query; | ||
})(); | ||
var QueryRunner = (function () { | ||
function QueryRunner() { | ||
_classCallCheck(this, QueryRunner); | ||
this.spliceSize = QueryRunner.spliceSizeHighThreshold; | ||
this.stack = []; | ||
this._loopRunning = false; | ||
} | ||
_createClass(QueryRunner, [{ | ||
key: 'addQuery', | ||
value: function addQuery(query) { | ||
var _this = this; | ||
return new Promise(function (resolve) { | ||
_this.stack.push(Object.assign(query, { resolve: resolve })); | ||
_this.startLoop(); | ||
}); | ||
} | ||
/** @private */ | ||
}, { | ||
key: 'removeQueryFromStack', | ||
value: function removeQueryFromStack(query) { | ||
this.stack.splice(this.stack.indexOf(query), 1); | ||
} | ||
/** @private */ | ||
}, { | ||
key: 'getNextQuery', | ||
value: function getNextQuery() { | ||
var _this2 = this; | ||
if (this.stack.length === 0) {} else if (!this.access_token) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = this.stack[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var query = _step.value; | ||
if (query.method !== 'execute') { | ||
this.removeQueryFromStack(query); | ||
return query; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator['return']) { | ||
_iterator['return'](); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} else if (this.stack[0].method === 'execute') { | ||
var query = this.stack[0]; | ||
this.removeQueryFromStack(query); | ||
return query; | ||
} else { | ||
var _iteratorNormalCompletion2; | ||
var _didIteratorError2; | ||
var _iteratorError2; | ||
var _iterator2, _step2; | ||
var _iteratorNormalCompletion3; | ||
var _didIteratorError3; | ||
var _iteratorError3; | ||
var _iterator3, _step3; | ||
var _ret = (function () { | ||
var aggregatedQueryStack = []; | ||
_iteratorNormalCompletion2 = true; | ||
_didIteratorError2 = false; | ||
_iteratorError2 = undefined; | ||
try { | ||
for (_iterator2 = _this2.stack[Symbol.iterator](); !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var query = _step2.value; | ||
if (query.method !== 'execute') { | ||
aggregatedQueryStack.push(query); | ||
if (aggregatedQueryStack.length >= _this2.spliceSize) break; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2['return']) { | ||
_iterator2['return'](); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
_iteratorNormalCompletion3 = true; | ||
_didIteratorError3 = false; | ||
_iteratorError3 = undefined; | ||
try { | ||
for (_iterator3 = aggregatedQueryStack[Symbol.iterator](); !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var query = _step3.value; | ||
_this2.removeQueryFromStack(query); | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3['return']) { | ||
_iterator3['return'](); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
return { | ||
v: Object.assign(new Query('execute', { code: QueryRunner.generateExecuteCode(aggregatedQueryStack) }), { | ||
resolve: function resolve(_ref) { | ||
var response = _ref.response; | ||
for (var i = 0; i < response.length; i++) aggregatedQueryStack[i].resolve(response[i]); | ||
} | ||
}) | ||
}; | ||
})(); | ||
if (typeof _ret === 'object') return _ret.v; | ||
} | ||
} | ||
/** @private */ | ||
}, { | ||
key: 'startLoop', | ||
value: function startLoop() { | ||
if (!this._loopRunning) this.runLoop(); | ||
} | ||
/** @private */ | ||
}, { | ||
key: 'processError', | ||
value: function processError(error) { | ||
console.warn(error); | ||
//todo | ||
} | ||
/** @private */ | ||
}, { | ||
key: 'runLoop', | ||
value: function runLoop() { | ||
var query, result; | ||
return regeneratorRuntime.async(function runLoop$(context$2$0) { | ||
while (1) switch (context$2$0.prev = context$2$0.next) { | ||
case 0: | ||
query = undefined; | ||
case 1: | ||
if (!(query = this.getNextQuery())) { | ||
context$2$0.next = 8; | ||
break; | ||
} | ||
context$2$0.next = 4; | ||
return regeneratorRuntime.awrap(this.executeQuery(query)); | ||
case 4: | ||
result = context$2$0.sent; | ||
result === undefined ? this.addQuery(query) : query.resolve(result); | ||
/*FILO stack*/ | ||
context$2$0.next = 1; | ||
break; | ||
case 8: | ||
case 'end': | ||
return context$2$0.stop(); | ||
} | ||
}, null, this); | ||
} | ||
/** @private */ | ||
}, { | ||
key: 'executeQuery', | ||
value: function executeQuery(query) { | ||
var _this3 = this; | ||
return (0, _isomorphicFetch2['default'])(QueryRunner.basePath + '/' + query.method + '?' + encodeToSearchString(this.access_token ? _extends({}, query.opts, { access_token: this.access_token }) : query.opts)).then(function (response) { | ||
return response.json(); | ||
}).then(function () { | ||
var _ref2 = arguments.length <= 0 || arguments[0] === undefined ? { error: true } : arguments[0]; | ||
var error = _ref2.error; | ||
var response = _objectWithoutProperties(_ref2, ['error']); | ||
return error ? Promise.reject(error) : response; | ||
})['catch'](function (error) { | ||
_this3.processError(error); | ||
return undefined; | ||
}); | ||
} | ||
}], [{ | ||
key: 'generateExecuteCode', | ||
value: function generateExecuteCode(queries) { | ||
return 'var results = [], result;' + queries.map(function (q) { | ||
return { | ||
call: 'API.' + q.method + '(' + JSON.stringify(q.opts, function (key, value) { | ||
return typeof value === 'string' ? value.replace(/[^\\]('")/img, "\$1").replace(/&/img, '') : value; | ||
}) + ')', | ||
postfix: q.postfix | ||
}; | ||
}).map(function (q) { | ||
return q.postfix ? 'result = ' + q.call + q.postfix + ';results.push(result);' : 'results.push(' + q.call + ');'; | ||
}).join('\n') + 'return results;'; | ||
} | ||
}, { | ||
key: 'spliceSizeLowThreshold', | ||
value: 4, | ||
enumerable: true | ||
}, { | ||
key: 'spliceSizeHighThreshold', | ||
value: 25, | ||
enumerable: true | ||
}, { | ||
key: 'basePath', | ||
value: 'https://api.vk.com/method', | ||
enumerable: true | ||
}]); | ||
return QueryRunner; | ||
})(); | ||
var VK = (function () { | ||
_createClass(VK, null, [{ | ||
key: 'registerAuthStatusCallback', | ||
value: function registerAuthStatusCallback(cb) { | ||
if (!window.VK) { | ||
window.VK = { Auth: { lsCb: [cb] } }; | ||
return 0; | ||
} else if (window.VK instanceof Object && window.VK.Auth && window.VK.Auth.lsCb) { | ||
var key = 0; | ||
do key++; while (window.VK.Auth.lsCb.hasOwnProperty(String(key))); | ||
window.VK.Auth.lsCb[key] = cb; | ||
return key; | ||
} else { | ||
console.error('window.VK object is mocked with', window.VK); | ||
throw new Error('window.VK object is invalid'); | ||
} | ||
} | ||
}, { | ||
key: 'loginBasePath', | ||
value: 'https://login.vk.com/?', | ||
enumerable: true | ||
}]); | ||
function VK(application_id) { | ||
_classCallCheck(this, VK); | ||
this.queryRunner = new QueryRunner(); | ||
Object.assign(this, { application_id: application_id }); | ||
} | ||
_createClass(VK, [{ | ||
key: 'call', | ||
value: function call(method) { | ||
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var postfix = arguments.length <= 2 || arguments[2] === undefined ? '' : arguments[2]; | ||
return this.queryRunner.addQuery(new Query(method, opts, postfix)); | ||
} | ||
}, { | ||
key: 'getLoginStatus', | ||
value: function getLoginStatus() { | ||
var _this4 = this; | ||
return new Promise(function (res) { | ||
return VK.registerAuthStatusCallback(res); | ||
}).then(function (rnd) { | ||
return _this4._executeOpenapiRequest({ rnd: rnd }); | ||
}); | ||
} | ||
}, { | ||
key: 'login', | ||
value: function login(permissions) { | ||
var popup, _ref3, user, access_token, sig, secret, expire; | ||
return regeneratorRuntime.async(function login$(context$2$0) { | ||
while (1) switch (context$2$0.prev = context$2$0.next) { | ||
case 0: | ||
popup = window.open('https://oauth.vk.com/authorize?client_id=' + this.application_id + '&display=popup&redirect_uri=close.html&response_type=token&scope=' + permissions, 'vk_auth', 'width=500,height=500'); | ||
if (popup) { | ||
context$2$0.next = 3; | ||
break; | ||
} | ||
throw new Error('cannot open popup'); | ||
case 3: | ||
if (!popup.parent) { | ||
context$2$0.next = 8; | ||
break; | ||
} | ||
context$2$0.next = 6; | ||
return regeneratorRuntime.awrap(new Promise(function (res) { | ||
setTimeout(res, 100); | ||
})); | ||
case 6: | ||
context$2$0.next = 3; | ||
break; | ||
case 8: | ||
context$2$0.next = 10; | ||
return regeneratorRuntime.awrap(this.getLoginStatus()); | ||
case 10: | ||
_ref3 = context$2$0.sent; | ||
user = _ref3.user; | ||
access_token = _ref3.access_token; | ||
sig = _ref3.sig; | ||
secret = _ref3.secret; | ||
expire = _ref3.expire; | ||
this.queryRunner.access_token = access_token; | ||
Object.assign(this, { user: user, access_token: access_token, sig: sig, secret: secret, expire: expire }); | ||
case 18: | ||
case 'end': | ||
return context$2$0.stop(); | ||
} | ||
}, null, this); | ||
} | ||
}, { | ||
key: 'logout', | ||
value: function logout() { | ||
if (!this.sig) return; | ||
this._executeOpenapiRequest({ do_logout: 1, token: this.sig }); | ||
this.queryRunner.access_token = undefined; | ||
Object.assign(this, { user: null, access_token: null, sig: null, secret: null, expire: null }); | ||
document.cookie = 'vk_app_' + this.application_id + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | ||
} | ||
}, { | ||
key: '_executeOpenapiRequest', | ||
value: function _executeOpenapiRequest(opts) { | ||
executeRemoteCodeByUrl(VK.loginBasePath + '?' + encodeToSearchString(_extends({ | ||
act: 'openapi', | ||
oauth: 1, | ||
aid: this.application_id, | ||
location: encodeURIComponent(window.location.hostname) | ||
}, opts))); | ||
} | ||
}]); | ||
return VK; | ||
})(); | ||
exports.VK = VK; | ||
require('./dist/index.js') |
{ | ||
"name": "public-vk-api", | ||
"version": "0.0.1", | ||
"description": "", | ||
"version": "0.0.2", | ||
"main": "index.js", | ||
"scripts": { | ||
"prepublish": "cp ./lib/index.js ./index.es6.js; ./node_modules/.bin/babel ./lib/index.js > ./index.js" | ||
"test": "./node_modules/.bin/jest --verbose", | ||
"build": "./node_modules/.bin/babel lib --out-dir dist --source-maps inline", | ||
"prepublish": "npm run build", | ||
"semantic-release": "" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"jest": { | ||
"scriptPreprocessor": "node_modules/babel-jest" | ||
}, | ||
"engines": { | ||
"node": ">=0.12" | ||
}, | ||
"devDependencies": { | ||
"babel": "^5.8.29", | ||
"flow-bin": "^0.18.1" | ||
"babel-jest": "^5.3.0", | ||
"jest": "^0.1.40", | ||
"jest-cli": "^0.6.1", | ||
"semantic-release": "^4.3.5", | ||
"flow": "^0.2.3" | ||
}, | ||
"dependencies": { | ||
"crosstab": "^0.2.11", | ||
"isomorphic-fetch": "^2.2.0" | ||
}, | ||
"description": "VK.com Public Api - unofficial implementation for Public Radio project. Provides simple, fast and safe way to call VK methods", | ||
"author": "Vsevolod Rodionov <vsevolod.rodionov@gmail.com>", | ||
"homepage": "https://github.com/PublicRadio/public-vk-api", | ||
"keywords": [ | ||
"vk", | ||
"api", | ||
"publicradio" | ||
], | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/PublicRadio/public-vk-api" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
74125
20
2
1
19
0
2
6
424
+ Addedcrosstab@^0.2.11
+ Addedcrosstab@0.2.12(transitive)