cloud189-sdk 1.0.6-alpha.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/CloudClient.js +18 -17
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -5
- package/dist/log.d.ts +7 -22
- package/dist/log.js +8 -99
- package/dist/store/file-token-store.d.ts +1 -0
- package/dist/store/file-token-store.js +11 -0
- package/dist/util.d.ts +1 -0
- package/dist/util.js +3 -2
- package/package.json +10 -6
package/README.md
CHANGED
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
<a href="https://npmcharts.com/compare/cloud189-sdk?minimal=true">
|
|
15
15
|
<img src="http://img.shields.io/npm/dm/cloud189-sdk.svg">
|
|
16
16
|
</a>
|
|
17
|
+
<a href="https://coveralls.io/github/wes-lin/cloud189-sdk">
|
|
18
|
+
<img src="https://coveralls.io/repos/github/wes-lin/cloud189-sdk/badge.svg?branch=dev">
|
|
19
|
+
</a>
|
|
17
20
|
<a href="LICENSE">
|
|
18
21
|
<img src="https://img.shields.io/badge/License-MIT-yellow.svg">
|
|
19
22
|
</a>
|
package/dist/CloudClient.js
CHANGED
|
@@ -62,7 +62,7 @@ class CloudAuthClient {
|
|
|
62
62
|
hooks: {
|
|
63
63
|
afterResponse: [
|
|
64
64
|
async (response, retryWithMergedOptions) => {
|
|
65
|
-
log_1.
|
|
65
|
+
log_1.logger.debug(`url: ${response.requestUrl}, response: ${response.body})}`);
|
|
66
66
|
(0, error_1.checkError)(response.body.toString());
|
|
67
67
|
return response;
|
|
68
68
|
}
|
|
@@ -110,7 +110,7 @@ class CloudAuthClient {
|
|
|
110
110
|
* 用户名密码登录
|
|
111
111
|
* */
|
|
112
112
|
async loginByPassword(username, password) {
|
|
113
|
-
log_1.
|
|
113
|
+
log_1.logger.debug('loginByPassword...');
|
|
114
114
|
try {
|
|
115
115
|
const res = await Promise.all([
|
|
116
116
|
//1.获取公钥
|
|
@@ -134,7 +134,7 @@ class CloudAuthClient {
|
|
|
134
134
|
return await this.getSessionForPC({ redirectURL: loginRes.toUrl });
|
|
135
135
|
}
|
|
136
136
|
catch (e) {
|
|
137
|
-
log_1.
|
|
137
|
+
log_1.logger.error(e);
|
|
138
138
|
throw e;
|
|
139
139
|
}
|
|
140
140
|
}
|
|
@@ -142,14 +142,14 @@ class CloudAuthClient {
|
|
|
142
142
|
* token登录
|
|
143
143
|
*/
|
|
144
144
|
async loginByAccessToken(accessToken) {
|
|
145
|
-
log_1.
|
|
145
|
+
log_1.logger.debug('loginByAccessToken...');
|
|
146
146
|
return await this.getSessionForPC({ accessToken });
|
|
147
147
|
}
|
|
148
148
|
/**
|
|
149
149
|
* sso登录
|
|
150
150
|
*/
|
|
151
151
|
async loginBySsoCooike(cookie) {
|
|
152
|
-
log_1.
|
|
152
|
+
log_1.logger.debug('loginBySsoCooike...');
|
|
153
153
|
const res = await this.request.get(`${const_1.WEB_URL}/api/portal/unifyLoginForPC.action`, {
|
|
154
154
|
searchParams: {
|
|
155
155
|
appId: const_1.AppID,
|
|
@@ -194,7 +194,7 @@ class CloudClient {
|
|
|
194
194
|
_CloudClient_accessTokenPromise.set(this, void 0);
|
|
195
195
|
_CloudClient_valid.set(this, (options) => {
|
|
196
196
|
if (!options.token && (!options.username || !options.password)) {
|
|
197
|
-
log_1.
|
|
197
|
+
log_1.logger.error('valid');
|
|
198
198
|
throw new Error('Please provide username and password or token !');
|
|
199
199
|
}
|
|
200
200
|
});
|
|
@@ -249,18 +249,18 @@ class CloudClient {
|
|
|
249
249
|
],
|
|
250
250
|
afterResponse: [
|
|
251
251
|
async (response, retryWithMergedOptions) => {
|
|
252
|
-
log_1.
|
|
252
|
+
log_1.logger.debug(`url: ${response.requestUrl}, response: ${response.body}`);
|
|
253
253
|
if (response.statusCode === 400) {
|
|
254
254
|
const { errorCode, errorMsg } = JSON.parse(response.body.toString());
|
|
255
255
|
if (errorCode === 'InvalidAccessToken') {
|
|
256
|
-
log_1.
|
|
257
|
-
log_1.
|
|
256
|
+
log_1.logger.debug('InvalidAccessToken retry...');
|
|
257
|
+
log_1.logger.debug('Refresh AccessToken');
|
|
258
258
|
this.session.accessToken = '';
|
|
259
259
|
return retryWithMergedOptions({});
|
|
260
260
|
}
|
|
261
261
|
else if (errorCode === 'InvalidSessionKey') {
|
|
262
|
-
log_1.
|
|
263
|
-
log_1.
|
|
262
|
+
log_1.logger.debug('InvalidSessionKey retry...');
|
|
263
|
+
log_1.logger.debug('Refresh InvalidSessionKey');
|
|
264
264
|
this.session.sessionKey = '';
|
|
265
265
|
return retryWithMergedOptions({});
|
|
266
266
|
}
|
|
@@ -278,7 +278,7 @@ class CloudClient {
|
|
|
278
278
|
return await this.authClient.loginByAccessToken(accessToken);
|
|
279
279
|
}
|
|
280
280
|
catch (e) {
|
|
281
|
-
log_1.
|
|
281
|
+
log_1.logger.error(e);
|
|
282
282
|
}
|
|
283
283
|
}
|
|
284
284
|
if (refreshToken) {
|
|
@@ -286,12 +286,13 @@ class CloudClient {
|
|
|
286
286
|
const refreshTokenSession = await this.authClient.refreshToken(refreshToken);
|
|
287
287
|
await this.tokenStore.update({
|
|
288
288
|
accessToken: refreshTokenSession.accessToken,
|
|
289
|
+
refreshToken: refreshTokenSession.refreshToken,
|
|
289
290
|
expiresIn: new Date(Date.now() + refreshTokenSession.expiresIn * 1000).getTime()
|
|
290
291
|
});
|
|
291
292
|
return await this.authClient.loginByAccessToken(refreshTokenSession.accessToken);
|
|
292
293
|
}
|
|
293
294
|
catch (e) {
|
|
294
|
-
log_1.
|
|
295
|
+
log_1.logger.error(e);
|
|
295
296
|
}
|
|
296
297
|
}
|
|
297
298
|
if (this.ssonCookie) {
|
|
@@ -300,12 +301,12 @@ class CloudClient {
|
|
|
300
301
|
await this.tokenStore.update({
|
|
301
302
|
accessToken: loginToken.accessToken,
|
|
302
303
|
refreshToken: loginToken.refreshToken,
|
|
303
|
-
expiresIn: new Date(Date.now() +
|
|
304
|
+
expiresIn: new Date(Date.now() + 6 * 24 * 60 * 60 * 1000).getTime()
|
|
304
305
|
});
|
|
305
306
|
return loginToken;
|
|
306
307
|
}
|
|
307
308
|
catch (e) {
|
|
308
|
-
log_1.
|
|
309
|
+
log_1.logger.error(e);
|
|
309
310
|
}
|
|
310
311
|
}
|
|
311
312
|
if (this.username && this.password) {
|
|
@@ -314,12 +315,12 @@ class CloudClient {
|
|
|
314
315
|
await this.tokenStore.update({
|
|
315
316
|
accessToken: loginToken.accessToken,
|
|
316
317
|
refreshToken: loginToken.refreshToken,
|
|
317
|
-
expiresIn: new Date(Date.now() +
|
|
318
|
+
expiresIn: new Date(Date.now() + 6 * 24 * 60 * 60 * 1000).getTime()
|
|
318
319
|
});
|
|
319
320
|
return loginToken;
|
|
320
321
|
}
|
|
321
322
|
catch (e) {
|
|
322
|
-
log_1.
|
|
323
|
+
log_1.logger.error(e);
|
|
323
324
|
}
|
|
324
325
|
}
|
|
325
326
|
throw new Error('Can not get session.');
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,10 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.FileTokenStore = exports.MemoryStore = exports.Store = void 0;
|
|
18
17
|
__exportStar(require("./CloudClient"), exports);
|
|
19
18
|
__exportStar(require("./types"), exports);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
Object.defineProperty(exports, "MemoryStore", { enumerable: true, get: function () { return store_1.MemoryStore; } });
|
|
23
|
-
Object.defineProperty(exports, "FileTokenStore", { enumerable: true, get: function () { return store_1.FileTokenStore; } });
|
|
19
|
+
__exportStar(require("./store"), exports);
|
|
20
|
+
__exportStar(require("./log"), exports);
|
package/dist/log.d.ts
CHANGED
|
@@ -1,22 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export
|
|
8
|
-
export declare class Logger {
|
|
9
|
-
protected readonly stream: WritableStream;
|
|
10
|
-
constructor(stream: WritableStream);
|
|
11
|
-
messageTransformer: (message: string, level: LogLevel) => string;
|
|
12
|
-
get isDebugEnabled(): boolean;
|
|
13
|
-
info(messageOrFields: Fields | null | string, message?: string): void;
|
|
14
|
-
error(messageOrFields: Fields | null | string, message?: string): void;
|
|
15
|
-
warn(messageOrFields: Fields | null | string, message?: string): void;
|
|
16
|
-
debug(messageOrFields: Fields | null | string, message?: string): void;
|
|
17
|
-
private doLog;
|
|
18
|
-
private _doLog;
|
|
19
|
-
static createMessage(message: string, fields: Fields | null, level: LogLevel, color: (it: string) => string, messagePadding?: number): string;
|
|
20
|
-
log(message: string): void;
|
|
21
|
-
}
|
|
22
|
-
export declare const log: Logger;
|
|
1
|
+
import { Logger } from '@netdrive-sdk/log';
|
|
2
|
+
/**
|
|
3
|
+
* 日志记录
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
declare const logger: Logger;
|
|
7
|
+
export { logger };
|
package/dist/log.js
CHANGED
|
@@ -1,101 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
get isDebugEnabled() {
|
|
16
|
-
return process.env.CLOUD189_VERBOSE == '1';
|
|
17
|
-
}
|
|
18
|
-
info(messageOrFields, message) {
|
|
19
|
-
this.doLog(message, messageOrFields, 'info');
|
|
20
|
-
}
|
|
21
|
-
error(messageOrFields, message) {
|
|
22
|
-
this.doLog(message, messageOrFields, 'error');
|
|
23
|
-
}
|
|
24
|
-
warn(messageOrFields, message) {
|
|
25
|
-
this.doLog(message, messageOrFields, 'warn');
|
|
26
|
-
}
|
|
27
|
-
debug(messageOrFields, message) {
|
|
28
|
-
if (this.isDebugEnabled) {
|
|
29
|
-
this.doLog(message, messageOrFields, 'debug');
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
doLog(message, messageOrFields, level) {
|
|
33
|
-
if (message === undefined) {
|
|
34
|
-
this._doLog(messageOrFields, null, level);
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
this._doLog(message, messageOrFields, level);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
_doLog(message, fields, level) {
|
|
41
|
-
// noinspection SuspiciousInstanceOfGuard
|
|
42
|
-
if (message instanceof Error) {
|
|
43
|
-
message = message.stack || message.toString();
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
message = message.toString();
|
|
47
|
-
}
|
|
48
|
-
const levelIndicator = level === 'error' ? '⨯' : '•';
|
|
49
|
-
const color = LEVEL_TO_COLOR[level];
|
|
50
|
-
this.stream.write(`${' '.repeat(exports.PADDING)}${color(levelIndicator)} `);
|
|
51
|
-
this.stream.write(Logger.createMessage(this.messageTransformer(message, level), fields, level, color, exports.PADDING + 2 /* level indicator and space */));
|
|
52
|
-
this.stream.write('\n');
|
|
53
|
-
}
|
|
54
|
-
static createMessage(message, fields, level, color, messagePadding = 0) {
|
|
55
|
-
if (fields == null) {
|
|
56
|
-
return message;
|
|
57
|
-
}
|
|
58
|
-
const fieldPadding = ' '.repeat(Math.max(2, 16 - message.length));
|
|
59
|
-
let text = (level === 'error' ? color(message) : message) + fieldPadding;
|
|
60
|
-
const fieldNames = Object.keys(fields);
|
|
61
|
-
let counter = 0;
|
|
62
|
-
for (const name of fieldNames) {
|
|
63
|
-
let fieldValue = fields[name];
|
|
64
|
-
let valuePadding = null;
|
|
65
|
-
// Remove unnecessary line breaks
|
|
66
|
-
if (fieldValue != null && typeof fieldValue === 'string' && fieldValue.includes('\n')) {
|
|
67
|
-
valuePadding = ' '.repeat(messagePadding + message.length + fieldPadding.length + 2);
|
|
68
|
-
fieldValue = fieldValue.replace(/\n\s*\n/g, `\n${valuePadding}`);
|
|
69
|
-
}
|
|
70
|
-
else if (Array.isArray(fieldValue)) {
|
|
71
|
-
fieldValue = JSON.stringify(fieldValue);
|
|
72
|
-
}
|
|
73
|
-
text += `${color(name)}=${fieldValue}`;
|
|
74
|
-
if (++counter !== fieldNames.length) {
|
|
75
|
-
if (valuePadding == null) {
|
|
76
|
-
text += ' ';
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
text += '\n' + valuePadding;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return text;
|
|
84
|
-
}
|
|
85
|
-
log(message) {
|
|
86
|
-
if (printer == null) {
|
|
87
|
-
this.stream.write(`${message}\n`);
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
printer(message);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
exports.Logger = Logger;
|
|
95
|
-
const LEVEL_TO_COLOR = {
|
|
96
|
-
info: chalk_1.default.blue,
|
|
97
|
-
warn: chalk_1.default.yellow,
|
|
98
|
-
error: chalk_1.default.red,
|
|
99
|
-
debug: chalk_1.default.white
|
|
100
|
-
};
|
|
101
|
-
exports.log = new Logger(process.stdout);
|
|
3
|
+
exports.logger = void 0;
|
|
4
|
+
const log_1 = require("@netdrive-sdk/log");
|
|
5
|
+
/**
|
|
6
|
+
* 日志记录
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
const logger = new log_1.Logger();
|
|
10
|
+
exports.logger = logger;
|
|
@@ -27,11 +27,15 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
27
27
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
28
28
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
29
29
|
};
|
|
30
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
31
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
32
|
+
};
|
|
30
33
|
var _FileTokenStore_instances, _FileTokenStore_loadFromFile, _FileTokenStore_saveToFile;
|
|
31
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
35
|
exports.FileTokenStore = void 0;
|
|
33
36
|
const fs = __importStar(require("node:fs"));
|
|
34
37
|
const promisesFs = __importStar(require("node:fs/promises"));
|
|
38
|
+
const path_1 = __importDefault(require("path"));
|
|
35
39
|
const memstore_1 = require("./memstore");
|
|
36
40
|
/**
|
|
37
41
|
* @public
|
|
@@ -44,11 +48,18 @@ class FileTokenStore extends memstore_1.MemoryStore {
|
|
|
44
48
|
if (!filePath) {
|
|
45
49
|
throw new Error('Unknown file for read/write token');
|
|
46
50
|
}
|
|
51
|
+
this.ensureTokenDirectory(filePath);
|
|
47
52
|
const dataJson = __classPrivateFieldGet(this, _FileTokenStore_instances, "m", _FileTokenStore_loadFromFile).call(this, filePath);
|
|
48
53
|
if (dataJson) {
|
|
49
54
|
super.update(dataJson);
|
|
50
55
|
}
|
|
51
56
|
}
|
|
57
|
+
ensureTokenDirectory(filePath) {
|
|
58
|
+
const dir = path_1.default.dirname(filePath);
|
|
59
|
+
if (!fs.existsSync(dir)) {
|
|
60
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
52
63
|
update(token) {
|
|
53
64
|
super.update(token);
|
|
54
65
|
return __classPrivateFieldGet(this, _FileTokenStore_instances, "m", _FileTokenStore_saveToFile).call(this, this.filePath, this.store);
|
package/dist/util.d.ts
CHANGED
package/dist/util.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.rsaEncrypt = exports.getSignature = void 0;
|
|
6
|
+
exports.rsaEncrypt = exports.getSignature = exports.sortParameter = void 0;
|
|
7
7
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
8
|
const sortParameter = (data) => {
|
|
9
9
|
if (!data) {
|
|
@@ -13,8 +13,9 @@ const sortParameter = (data) => {
|
|
|
13
13
|
e.sort((a, b) => (a > b ? 1 : a < b ? -1 : 0));
|
|
14
14
|
return e.join('&');
|
|
15
15
|
};
|
|
16
|
+
exports.sortParameter = sortParameter;
|
|
16
17
|
const getSignature = (data) => {
|
|
17
|
-
const parameter = sortParameter(data);
|
|
18
|
+
const parameter = (0, exports.sortParameter)(data);
|
|
18
19
|
return crypto_1.default.createHash('md5').update(parameter).digest('hex');
|
|
19
20
|
};
|
|
20
21
|
exports.getSignature = getSignature;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cloud189-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "基于node.js的第三方天翼云盘SDK",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -10,24 +10,25 @@
|
|
|
10
10
|
"!__tests__"
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
13
|
-
"start": "node ./
|
|
13
|
+
"start": "node ./example/app.js",
|
|
14
14
|
"build": "tsc",
|
|
15
15
|
"docs:build": "npm run build && cd docs && npm run build",
|
|
16
16
|
"docs:dev": "npm run build && cd docs && npm run dev",
|
|
17
|
-
"test": "mocha",
|
|
18
|
-
"coverage": "nyc npm run test"
|
|
17
|
+
"test": "node --security-revert=CVE-2023-46809 ./node_modules/mocha/bin/mocha",
|
|
18
|
+
"coverage": "nyc npm run test",
|
|
19
|
+
"coveralls": "nyc report --reporter=text-lcov > coverage/lcov.info"
|
|
19
20
|
},
|
|
20
21
|
"author": "wes lin",
|
|
21
22
|
"repository": {
|
|
22
23
|
"type": "git",
|
|
23
|
-
"url": "https://github.com/wes-lin/cloud189-sdk.git"
|
|
24
|
+
"url": "git+https://github.com/wes-lin/cloud189-sdk.git"
|
|
24
25
|
},
|
|
25
26
|
"keywords": [
|
|
26
27
|
"天翼网盘sdk"
|
|
27
28
|
],
|
|
28
29
|
"license": "MIT",
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"
|
|
31
|
+
"@netdrive-sdk/log": "1.0.0",
|
|
31
32
|
"got": "11.8.2"
|
|
32
33
|
},
|
|
33
34
|
"engines": {
|
|
@@ -37,12 +38,15 @@
|
|
|
37
38
|
"@types/chai": "^4.3.16",
|
|
38
39
|
"@types/mocha": "^10.0.6",
|
|
39
40
|
"@types/node": "^20.12.13",
|
|
41
|
+
"@types/sinon": "^17.0.4",
|
|
40
42
|
"@typescript-eslint/parser": "^6.17.0",
|
|
41
43
|
"chai": "4.4.1",
|
|
44
|
+
"coveralls": "^3.1.1",
|
|
42
45
|
"mocha": "^10.4.0",
|
|
43
46
|
"nock": "14.0.0-beta.7",
|
|
44
47
|
"nyc": "^15.1.0",
|
|
45
48
|
"prettier": "^3.3.2",
|
|
49
|
+
"sinon": "^20.0.0",
|
|
46
50
|
"ts-node": "^10.9.2",
|
|
47
51
|
"typescript": "4.9.5"
|
|
48
52
|
}
|