Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

youzanyun-open-sdk

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

youzanyun-open-sdk - npm Package Compare versions

Comparing version 1.1.2 to 1.2.0

124

lib/token/manager.js

@@ -0,5 +1,10 @@

const delay = require('delay');
const random = require('random');
const Token = require('./token');
const CloudTypeSelf = 'Self';
const CloudTypeTool = 'Tool';
const TokenLockKey = 'yzy_token_lock';
const TokenKdtsKey = 'yzy_token_kdts';
module.exports = class TokenManager {

@@ -28,3 +33,3 @@ constructor({

this.cloudType = CloudTypeSelf;
this.tokenCacheKeyPrefix = `token_${CloudTypeSelf}_`;
this.tokenCacheKeyPrefix = `yzy_token_${CloudTypeSelf}_`;
}

@@ -40,14 +45,112 @@

this.cloudType = CloudTypeTool;
this.tokenCacheKeyPrefix = `token_${CloudTypeTool}_`;
this.tokenCacheKeyPrefix = `yzy_token_${CloudTypeTool}_`;
}
async getTokenFromCode(kdtId, code) {
let cacheKey = `${this.tokenCacheKeyPrefix}${kdtId}`;
let token = await this.tokenService.getCloudToolToken(code);
this.tokenCache[cacheKey] = token;
// 将token放入redis
return token;
async getTokenFromCode(code) {
await this.lockTokenOperate();
try {
let token = await this.tokenService.getCloudToolToken(code);
let kdtId = token.authority_id;
let cacheKey = `${this.tokenCacheKeyPrefix}${kdtId}`;
this.tokenCache[cacheKey] = token;
// 将token放入redis
let ioredisClient = this.ioredisClient;
await ioredisClient.set(cacheKey, JSON.stringify(token));
let kdtsString = await ioredisClient.get(TokenKdtsKey) || '';
if (kdtsString.indexOf(kdtId + '') == -1) {
await ioredisClient.set(TokenKdtsKey, kdtsString + ',' + kdtId)
}
return token;
} finally {
await this.releaseTokenOperate();
}
}
async lockTokenOperate() {
let ioredisClient = this.ioredisClient;
let tryCount = 0;
let totalCount = random.int(2, 20);
while (tryCount < totalCount) { // 随机等待加锁时间
let lockReturn = await ioredisClient.setnx(TokenLockKey, '' + Date.now());
if (lockReturn == 1) {
return true;
}
tryCount++;
await delay(1000);
}
// 超时检测
let lockValue = await ioredisClient.get(TokenLockKey);
let diff = Date.now() - lockValue;
if (!Number.isInteger(diff) || diff > 1000 * 60 * 2) { // 2分钟超时
await ioredisClient.del(TokenLockKey);
let lockReturn = await ioredisClient.setnx(TokenLockKey, '' + Date.now());
if (lockReturn == 1) {
return true;
}
}
throw new Error('lock加锁失败');
}
async releaseTokenOperate() {
await this.ioredisClient.del(TokenLockKey);
return true;
}
/**
* 刷新所有token
*/
async refreshAllToken() {
let ioredisClient = this.ioredisClient;
let kdtsString = await ioredisClient.get(TokenKdtsKey) || '';
let kdts = kdtsString.split(',');
for (let kdtId of kdts) {
if (!kdtId) continue;
// 刷新每个店铺
let token;
let cacheKey = `${this.tokenCacheKeyPrefix}${kdtId}`;
// 获取远端token
let tokenString = await ioredisClient.get(cacheKey);
if (tokenString) {
token = JSON.parse(tokenString);
}
// 刷新
let newtoken = await this.tokenService.refreshCloudToolToken(token.refresh_token);
this.tokenCache[cacheKey] = newtoken;
await ioredisClient.set(cacheKey, JSON.stringify(newtoken));
}
return true;
}
async getAllToken() {
let ioredisClient = this.ioredisClient;
let kdtsString = await ioredisClient.get(TokenKdtsKey) || '';
let lockValue = await ioredisClient.get(TokenLockKey);
let result = {
kdts: kdtsString,
lockValue,
tokenList: []
};
let kdts = kdtsString.split(',');
for (let kdtId of kdts) {
if (!kdtId) continue;
// 刷新每个店铺
let token;
let cacheKey = `${this.tokenCacheKeyPrefix}${kdtId}`;
// 获取远端token
let tokenString = await ioredisClient.get(cacheKey);
if (tokenString) {
token = JSON.parse(tokenString);
}
result.tokenList.push(token);
}
return result;
}
/**
* 返回一个有效的token

@@ -117,4 +220,5 @@ */

_isTokenValid(token) {
return token.expires < Date.now() - 1000 * 60 * 60 * 2;
let isValid = token.expires > Date.now() - 1000 * 60 * 60 * 2;
return isValid;
}
};

2

package.json
{
"name": "youzanyun-open-sdk",
"version": "1.1.2",
"version": "1.2.0",
"description": "",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc