suanpan_node_sdk 2.2.4 → 2.2.5
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/dist/api/index.d.ts +10 -10
- package/dist/api/index.js +56 -52
- package/dist/api/index.js.map +1 -1
- package/dist/core/common/constant.d.ts +6 -6
- package/dist/core/common/constant.js +50 -50
- package/dist/core/common/exec.d.ts +1 -1
- package/dist/core/common/exec.js +53 -53
- package/dist/core/common/log.d.ts +3 -3
- package/dist/core/common/log.js +15 -15
- package/dist/core/event/index.d.ts +6 -6
- package/dist/core/event/index.js +10 -10
- package/dist/core/logkit/index.d.ts +33 -33
- package/dist/core/logkit/index.js +125 -125
- package/dist/core/logkit/index.js.map +1 -1
- package/dist/core/message/channel.d.ts +7 -7
- package/dist/core/message/channel.js +2 -2
- package/dist/core/message/channelFactory.d.ts +4 -4
- package/dist/core/message/channelFactory.js +10 -10
- package/dist/core/message/index.d.ts +6 -6
- package/dist/core/message/index.js +22 -18
- package/dist/core/message/index.js.map +1 -1
- package/dist/core/message/message.d.ts +19 -19
- package/dist/core/message/message.js +59 -59
- package/dist/core/message/message.js.map +1 -1
- package/dist/core/message/messageBuilder.d.ts +12 -12
- package/dist/core/message/messageBuilder.js +50 -50
- package/dist/core/message/messageBuilder.js.map +1 -1
- package/dist/core/message/messageChain.d.ts +9 -9
- package/dist/core/message/messageChain.js +29 -29
- package/dist/core/message/messageListener.d.ts +4 -4
- package/dist/core/message/messageListener.js +11 -11
- package/dist/core/message/mq/index.d.ts +9 -9
- package/dist/core/message/mq/index.js +50 -50
- package/dist/core/message/mq/redis.d.ts +12 -12
- package/dist/core/message/mq/redis.js +101 -97
- package/dist/core/message/mq/redis.js.map +1 -1
- package/dist/core/parameter/commandArgs.d.ts +2 -2
- package/dist/core/parameter/commandArgs.js +22 -22
- package/dist/core/parameter/commandArgs.js.map +1 -1
- package/dist/core/parameter/environment.d.ts +86 -86
- package/dist/core/parameter/environment.js +103 -103
- package/dist/core/parameter/index.d.ts +7 -7
- package/dist/core/parameter/index.js +33 -33
- package/dist/core/script/script-babel/babel.d.ts +1 -1
- package/dist/core/script/script-babel/babel.js +23 -23
- package/dist/core/script/script-babel/plugin.d.ts +7 -7
- package/dist/core/script/script-babel/plugin.js +16 -16
- package/dist/core/script/script-constant.d.ts +36 -36
- package/dist/core/script/script-constant.js +70 -70
- package/dist/core/script/script-context.d.ts +1 -1
- package/dist/core/script/script-context.js +295 -295
- package/dist/core/script/script-front.d.ts +1 -1
- package/dist/core/script/script-front.js +201 -201
- package/dist/core/script/script-insert.d.ts +18 -18
- package/dist/core/script/script-insert.js +43 -43
- package/dist/core/script/script-manager.d.ts +74 -74
- package/dist/core/script/script-manager.js +261 -261
- package/dist/core/script/script-storage.d.ts +22 -22
- package/dist/core/script/script-storage.js +241 -241
- package/dist/core/script/script-support.d.ts +11 -11
- package/dist/core/script/script-support.js +140 -140
- package/dist/core/script/script-template.d.ts +1 -1
- package/dist/core/script/script-template.js +6 -6
- package/dist/core/service/port.d.ts +5 -5
- package/dist/core/service/port.js +53 -53
- package/dist/core/service/port.js.map +1 -1
- package/dist/core/storage/index.d.ts +5 -5
- package/dist/core/storage/index.js +21 -17
- package/dist/core/storage/index.js.map +1 -1
- package/dist/core/storage/minioStorage.d.ts +31 -28
- package/dist/core/storage/minioStorage.js +224 -209
- package/dist/core/storage/minioStorage.js.map +1 -1
- package/dist/core/storage/objectStorage.d.ts +23 -19
- package/dist/core/storage/objectStorage.js +3 -3
- package/dist/core/storage/ossStorage.d.ts +29 -26
- package/dist/core/storage/ossStorage.js +215 -193
- package/dist/core/storage/ossStorage.js.map +1 -1
- package/dist/core/storage/storageFlusher.d.ts +14 -14
- package/dist/core/storage/storageFlusher.js +54 -54
- package/dist/core/storage/storageLogger.d.ts +16 -16
- package/dist/core/storage/storageLogger.js +100 -100
- package/dist/core/storage/storageLogger.js.map +1 -1
- package/dist/core/storage/storagePath.d.ts +16 -16
- package/dist/core/storage/storagePath.js +49 -49
- package/dist/core/storage/wrapperStorage.d.ts +17 -17
- package/dist/core/storage/wrapperStorage.js +47 -47
- package/dist/core/storage/wrapperStorage.js.map +1 -1
- package/dist/core/utils/flusher.d.ts +18 -18
- package/dist/core/utils/flusher.js +66 -62
- package/dist/core/utils/flusher.js.map +1 -1
- package/dist/core/utils/http.d.ts +3 -3
- package/dist/core/utils/http.js +35 -35
- package/dist/core/utils/http.js.map +1 -1
- package/dist/core/utils/index.d.ts +57 -56
- package/dist/core/utils/index.js +247 -243
- package/dist/core/utils/index.js.map +1 -1
- package/dist/core/utils/number.d.ts +2 -2
- package/dist/core/utils/number.js +19 -19
- package/dist/core/utils/retry.d.ts +1 -1
- package/dist/core/utils/retry.js +34 -34
- package/dist/core/utils/retry.js.map +1 -1
- package/dist/core/utils/suanpan.d.ts +21 -20
- package/dist/core/utils/suanpan.js +103 -92
- package/dist/core/utils/suanpan.js.map +1 -1
- package/dist/core/web/app.d.ts +2 -2
- package/dist/core/web/app.js +38 -38
- package/dist/core/web/app.js.map +1 -1
- package/dist/core/web/server.d.ts +3 -3
- package/dist/core/web/server.js +64 -60
- package/dist/core/web/server.js.map +1 -1
- package/dist/main.d.ts +5 -5
- package/dist/main.js +28 -24
- package/dist/main.js.map +1 -1
- package/dist/types/conmonTypes.d.ts +21 -21
- package/dist/types/conmonTypes.js +16 -16
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.js +19 -15
- package/dist/types/index.js.map +1 -1
- package/dist/types/storageTypes.d.ts +59 -58
- package/dist/types/storageTypes.js +9 -9
- package/dist/types/streamTypes.d.ts +21 -21
- package/dist/types/streamTypes.js +2 -2
- package/package.json +1 -1
|
@@ -1,242 +1,242 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
const _ = require('lodash');
|
|
12
|
-
const got = require('got');
|
|
13
|
-
const OSS = require('ali-oss');
|
|
14
|
-
const Minio = require('minio');
|
|
15
|
-
const crypto = require('crypto');
|
|
16
|
-
const { getConfig, tryJSONParse, readableToString } = require('../utils');
|
|
17
|
-
let ossClient;
|
|
18
|
-
let stsToken;
|
|
19
|
-
const minioBucketName = 'suanpan';
|
|
20
|
-
function getIdField() {
|
|
21
|
-
if (getConfig('app.nodeType') === 'suanpan') { // 个人版
|
|
22
|
-
return getConfig('app.defaultUserId');
|
|
23
|
-
}
|
|
24
|
-
return process.env.SP_USER_ID;
|
|
25
|
-
}
|
|
26
|
-
function isMixedMode() {
|
|
27
|
-
return getConfig('app.mixedMode');
|
|
28
|
-
}
|
|
29
|
-
function getMasterUrl() {
|
|
30
|
-
if (isMixedMode()) {
|
|
31
|
-
// suanpan server is running outside k8s cluster
|
|
32
|
-
const serverEndpoint = `http://${getConfig('app.host')}:7000`;
|
|
33
|
-
console.log(`${process.env.spentry} detected mixed mode server running at ${serverEndpoint}`);
|
|
34
|
-
return serverEndpoint;
|
|
35
|
-
}
|
|
36
|
-
return `http://suanpan-service.${getConfig('app.cs.masterNamespace')}.svc.cluster.local:7000`;
|
|
37
|
-
}
|
|
38
|
-
function buildHeaders() {
|
|
39
|
-
const headers = {};
|
|
40
|
-
const idField = getIdField();
|
|
41
|
-
headers[getConfig('app.cors.idField')] = idField;
|
|
42
|
-
headers[getConfig('app.cors.signatureField')] = crypto.createHmac('sha1', getConfig('oss.accessSecret')).update(idField).digest('base64');
|
|
43
|
-
headers[getConfig('app.cors.signVersionField')] = 'v1';
|
|
44
|
-
return headers;
|
|
45
|
-
}
|
|
46
|
-
function getTokenGrantHost() {
|
|
47
|
-
if (getConfig('app.nodeType') === 'suanpan') {
|
|
48
|
-
return getConfig('app.affinity');
|
|
49
|
-
}
|
|
50
|
-
return getMasterUrl();
|
|
51
|
-
}
|
|
52
|
-
function getStsToken() {
|
|
53
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
if (stsToken &&
|
|
55
|
-
stsToken.Credentials &&
|
|
56
|
-
stsToken.Credentials.Expiration &&
|
|
57
|
-
(new Date(stsToken.Credentials.Expiration).getTime() - (new Date()).getTime() > 1800000)) {
|
|
58
|
-
return { token: stsToken, refresh: false };
|
|
59
|
-
}
|
|
60
|
-
console.log('stsToken timeout, refresh');
|
|
61
|
-
const tokenGrantHost = getTokenGrantHost();
|
|
62
|
-
const headers = buildHeaders();
|
|
63
|
-
const gotOptions = {
|
|
64
|
-
prefixUrl: tokenGrantHost,
|
|
65
|
-
headers: headers
|
|
66
|
-
};
|
|
67
|
-
console.log(`from ${process.env.spentry} request STS token ${tokenGrantHost}/oss/token, req headers: ${JSON.stringify(headers)}`);
|
|
68
|
-
const response = yield got('oss/token', gotOptions);
|
|
69
|
-
stsToken = JSON.parse(response.body);
|
|
70
|
-
return { token: stsToken, refresh: true };
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
function getOssRegionFromEndpoint(endpoint) {
|
|
74
|
-
return endpoint.split('//')[1].split('.')[0];
|
|
75
|
-
}
|
|
76
|
-
function getOssClient(ossType) {
|
|
77
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
-
const endpoint = getConfig('oss.endpoint');
|
|
79
|
-
if (ossType === 'minio') {
|
|
80
|
-
if (ossClient) {
|
|
81
|
-
return ossClient;
|
|
82
|
-
}
|
|
83
|
-
const endpointURL = new URL(endpoint);
|
|
84
|
-
return new Minio.Client({
|
|
85
|
-
endPoint: endpointURL.hostname,
|
|
86
|
-
port: Number(endpointURL.port),
|
|
87
|
-
useSSL: endpointURL.protocol === 'https',
|
|
88
|
-
accessKey: getConfig('oss.accessKey'),
|
|
89
|
-
secretKey: getConfig('oss.accessSecret')
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
else if (ossType === 'oss') {
|
|
93
|
-
if (ossClient) {
|
|
94
|
-
return ossClient;
|
|
95
|
-
}
|
|
96
|
-
return new OSS({
|
|
97
|
-
bucket: getConfig('oss.bucket'),
|
|
98
|
-
region: getOssRegionFromEndpoint(endpoint),
|
|
99
|
-
accessKeyId: getConfig('oss.accessKey'),
|
|
100
|
-
accessKeySecret: getConfig('oss.accessSecret'),
|
|
101
|
-
secure: true
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
else if (ossType === 'suanpan') {
|
|
105
|
-
const { token, refresh } = yield getStsToken();
|
|
106
|
-
if (ossClient && !refresh) {
|
|
107
|
-
return ossClient;
|
|
108
|
-
}
|
|
109
|
-
return new OSS({
|
|
110
|
-
region: getOssRegionFromEndpoint(endpoint),
|
|
111
|
-
accessKeyId: token.Credentials.AccessKeyId,
|
|
112
|
-
accessKeySecret: token.Credentials.SecurityToken,
|
|
113
|
-
bucket: getConfig('oss.bucket'),
|
|
114
|
-
stsToken: token.Credentials.SecurityToken,
|
|
115
|
-
secure: true
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
throw new Error(`unkown oss storage type: ${ossType}`);
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
function ossObjectNameChecker(ossClient, objectName) {
|
|
122
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
-
try {
|
|
124
|
-
yield ossClient.head(objectName);
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
catch (e) {
|
|
128
|
-
if (e.name === 'NoSuchKeyError') {
|
|
129
|
-
return false;
|
|
130
|
-
}
|
|
131
|
-
throw e;
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
function minioObjectNameChecker(minioClient, objectName) {
|
|
136
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
137
|
-
try {
|
|
138
|
-
yield minioClient.getObject(minioBucketName, objectName);
|
|
139
|
-
return true;
|
|
140
|
-
}
|
|
141
|
-
catch (ex) {
|
|
142
|
-
if (ex.code && !_.includes(['NoSuchKey', 'NotFound'], ex.code)) {
|
|
143
|
-
throw ex;
|
|
144
|
-
}
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* check objectName exist
|
|
151
|
-
* @param {*} objectName
|
|
152
|
-
*/
|
|
153
|
-
function checkObjectNameExist(objectName) {
|
|
154
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
155
|
-
const ossType = getConfig('oss.type');
|
|
156
|
-
const client = yield getOssClient(ossType);
|
|
157
|
-
if (ossType === 'minio') {
|
|
158
|
-
return minioObjectNameChecker(client, objectName);
|
|
159
|
-
}
|
|
160
|
-
return ossObjectNameChecker(client, objectName);
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Delete multi objects in one request.
|
|
165
|
-
* @param {*} objectNames
|
|
166
|
-
*/
|
|
167
|
-
function remove(objectNames) {
|
|
168
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
-
const ossType = getConfig('oss.type');
|
|
170
|
-
const client = yield getOssClient(ossType);
|
|
171
|
-
const objectsList = _.isArray(objectNames) ? [...objectNames] : [objectNames];
|
|
172
|
-
if (ossType === 'minio') {
|
|
173
|
-
yield client.removeObjects(minioBucketName, objectsList);
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
yield client.deleteMulti(objectsList);
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
function getStream(objectName) {
|
|
181
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
-
const ossType = getConfig('oss.type');
|
|
183
|
-
const client = yield getOssClient(ossType);
|
|
184
|
-
if (ossType === 'minio') {
|
|
185
|
-
return yield client.getObject(minioBucketName, objectName);
|
|
186
|
-
}
|
|
187
|
-
const result = yield client.getStream(objectName);
|
|
188
|
-
return result.stream;
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
function getString(objectName) {
|
|
192
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
193
|
-
const stream = yield getStream(objectName);
|
|
194
|
-
return yield readableToString(stream);
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
function downloadToLocal(objectName, localPath) {
|
|
198
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
-
const ossType = getConfig('oss.type');
|
|
200
|
-
const client = yield getOssClient(ossType);
|
|
201
|
-
if (ossType === 'minio') {
|
|
202
|
-
yield client.fGetObject(minioBucketName, localPath);
|
|
203
|
-
}
|
|
204
|
-
else {
|
|
205
|
-
yield client.get(objectName, localPath);
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
function getJson(objectName) {
|
|
210
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
-
const str = yield getString(objectName);
|
|
212
|
-
return tryJSONParse(str, str);
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* https://github.com/ali-sdk/ali-oss#putname-file-options
|
|
217
|
-
* @param {*} objectName object name store on OSS
|
|
218
|
-
* @param {*} toBeUploaded String(file path)|Buffer|ReadStream
|
|
219
|
-
*/
|
|
220
|
-
function upload(objectName, toBeUploaded) {
|
|
221
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
-
const ossType = getConfig('oss.type');
|
|
223
|
-
const client = yield getOssClient(ossType);
|
|
224
|
-
if (ossType === 'minio') {
|
|
225
|
-
yield client.putObject(minioBucketName, objectName, toBeUploaded);
|
|
226
|
-
}
|
|
227
|
-
else {
|
|
228
|
-
yield client.put(objectName, toBeUploaded);
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
module.exports = {
|
|
233
|
-
getOssClient,
|
|
234
|
-
remove,
|
|
235
|
-
upload,
|
|
236
|
-
getString,
|
|
237
|
-
getStream,
|
|
238
|
-
getJson,
|
|
239
|
-
downloadToLocal,
|
|
240
|
-
fileExist: checkObjectNameExist
|
|
241
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
const _ = require('lodash');
|
|
12
|
+
const got = require('got');
|
|
13
|
+
const OSS = require('ali-oss');
|
|
14
|
+
const Minio = require('minio');
|
|
15
|
+
const crypto = require('crypto');
|
|
16
|
+
const { getConfig, tryJSONParse, readableToString } = require('../utils');
|
|
17
|
+
let ossClient;
|
|
18
|
+
let stsToken;
|
|
19
|
+
const minioBucketName = 'suanpan';
|
|
20
|
+
function getIdField() {
|
|
21
|
+
if (getConfig('app.nodeType') === 'suanpan') { // 个人版
|
|
22
|
+
return getConfig('app.defaultUserId');
|
|
23
|
+
}
|
|
24
|
+
return process.env.SP_USER_ID;
|
|
25
|
+
}
|
|
26
|
+
function isMixedMode() {
|
|
27
|
+
return getConfig('app.mixedMode');
|
|
28
|
+
}
|
|
29
|
+
function getMasterUrl() {
|
|
30
|
+
if (isMixedMode()) {
|
|
31
|
+
// suanpan server is running outside k8s cluster
|
|
32
|
+
const serverEndpoint = `http://${getConfig('app.host')}:7000`;
|
|
33
|
+
console.log(`${process.env.spentry} detected mixed mode server running at ${serverEndpoint}`);
|
|
34
|
+
return serverEndpoint;
|
|
35
|
+
}
|
|
36
|
+
return `http://suanpan-service.${getConfig('app.cs.masterNamespace')}.svc.cluster.local:7000`;
|
|
37
|
+
}
|
|
38
|
+
function buildHeaders() {
|
|
39
|
+
const headers = {};
|
|
40
|
+
const idField = getIdField();
|
|
41
|
+
headers[getConfig('app.cors.idField')] = idField;
|
|
42
|
+
headers[getConfig('app.cors.signatureField')] = crypto.createHmac('sha1', getConfig('oss.accessSecret')).update(idField).digest('base64');
|
|
43
|
+
headers[getConfig('app.cors.signVersionField')] = 'v1';
|
|
44
|
+
return headers;
|
|
45
|
+
}
|
|
46
|
+
function getTokenGrantHost() {
|
|
47
|
+
if (getConfig('app.nodeType') === 'suanpan') {
|
|
48
|
+
return getConfig('app.affinity');
|
|
49
|
+
}
|
|
50
|
+
return getMasterUrl();
|
|
51
|
+
}
|
|
52
|
+
function getStsToken() {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
if (stsToken &&
|
|
55
|
+
stsToken.Credentials &&
|
|
56
|
+
stsToken.Credentials.Expiration &&
|
|
57
|
+
(new Date(stsToken.Credentials.Expiration).getTime() - (new Date()).getTime() > 1800000)) {
|
|
58
|
+
return { token: stsToken, refresh: false };
|
|
59
|
+
}
|
|
60
|
+
console.log('stsToken timeout, refresh');
|
|
61
|
+
const tokenGrantHost = getTokenGrantHost();
|
|
62
|
+
const headers = buildHeaders();
|
|
63
|
+
const gotOptions = {
|
|
64
|
+
prefixUrl: tokenGrantHost,
|
|
65
|
+
headers: headers
|
|
66
|
+
};
|
|
67
|
+
console.log(`from ${process.env.spentry} request STS token ${tokenGrantHost}/oss/token, req headers: ${JSON.stringify(headers)}`);
|
|
68
|
+
const response = yield got('oss/token', gotOptions);
|
|
69
|
+
stsToken = JSON.parse(response.body);
|
|
70
|
+
return { token: stsToken, refresh: true };
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function getOssRegionFromEndpoint(endpoint) {
|
|
74
|
+
return endpoint.split('//')[1].split('.')[0];
|
|
75
|
+
}
|
|
76
|
+
function getOssClient(ossType) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
const endpoint = getConfig('oss.endpoint');
|
|
79
|
+
if (ossType === 'minio') {
|
|
80
|
+
if (ossClient) {
|
|
81
|
+
return ossClient;
|
|
82
|
+
}
|
|
83
|
+
const endpointURL = new URL(endpoint);
|
|
84
|
+
return new Minio.Client({
|
|
85
|
+
endPoint: endpointURL.hostname,
|
|
86
|
+
port: Number(endpointURL.port),
|
|
87
|
+
useSSL: endpointURL.protocol === 'https',
|
|
88
|
+
accessKey: getConfig('oss.accessKey'),
|
|
89
|
+
secretKey: getConfig('oss.accessSecret')
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else if (ossType === 'oss') {
|
|
93
|
+
if (ossClient) {
|
|
94
|
+
return ossClient;
|
|
95
|
+
}
|
|
96
|
+
return new OSS({
|
|
97
|
+
bucket: getConfig('oss.bucket'),
|
|
98
|
+
region: getOssRegionFromEndpoint(endpoint),
|
|
99
|
+
accessKeyId: getConfig('oss.accessKey'),
|
|
100
|
+
accessKeySecret: getConfig('oss.accessSecret'),
|
|
101
|
+
secure: true
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else if (ossType === 'suanpan') {
|
|
105
|
+
const { token, refresh } = yield getStsToken();
|
|
106
|
+
if (ossClient && !refresh) {
|
|
107
|
+
return ossClient;
|
|
108
|
+
}
|
|
109
|
+
return new OSS({
|
|
110
|
+
region: getOssRegionFromEndpoint(endpoint),
|
|
111
|
+
accessKeyId: token.Credentials.AccessKeyId,
|
|
112
|
+
accessKeySecret: token.Credentials.SecurityToken,
|
|
113
|
+
bucket: getConfig('oss.bucket'),
|
|
114
|
+
stsToken: token.Credentials.SecurityToken,
|
|
115
|
+
secure: true
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
throw new Error(`unkown oss storage type: ${ossType}`);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
function ossObjectNameChecker(ossClient, objectName) {
|
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
try {
|
|
124
|
+
yield ossClient.head(objectName);
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
if (e.name === 'NoSuchKeyError') {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
throw e;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function minioObjectNameChecker(minioClient, objectName) {
|
|
136
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
137
|
+
try {
|
|
138
|
+
yield minioClient.getObject(minioBucketName, objectName);
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
catch (ex) {
|
|
142
|
+
if (ex.code && !_.includes(['NoSuchKey', 'NotFound'], ex.code)) {
|
|
143
|
+
throw ex;
|
|
144
|
+
}
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* check objectName exist
|
|
151
|
+
* @param {*} objectName
|
|
152
|
+
*/
|
|
153
|
+
function checkObjectNameExist(objectName) {
|
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
155
|
+
const ossType = getConfig('oss.type');
|
|
156
|
+
const client = yield getOssClient(ossType);
|
|
157
|
+
if (ossType === 'minio') {
|
|
158
|
+
return minioObjectNameChecker(client, objectName);
|
|
159
|
+
}
|
|
160
|
+
return ossObjectNameChecker(client, objectName);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Delete multi objects in one request.
|
|
165
|
+
* @param {*} objectNames
|
|
166
|
+
*/
|
|
167
|
+
function remove(objectNames) {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
const ossType = getConfig('oss.type');
|
|
170
|
+
const client = yield getOssClient(ossType);
|
|
171
|
+
const objectsList = _.isArray(objectNames) ? [...objectNames] : [objectNames];
|
|
172
|
+
if (ossType === 'minio') {
|
|
173
|
+
yield client.removeObjects(minioBucketName, objectsList);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
yield client.deleteMulti(objectsList);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
function getStream(objectName) {
|
|
181
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
+
const ossType = getConfig('oss.type');
|
|
183
|
+
const client = yield getOssClient(ossType);
|
|
184
|
+
if (ossType === 'minio') {
|
|
185
|
+
return yield client.getObject(minioBucketName, objectName);
|
|
186
|
+
}
|
|
187
|
+
const result = yield client.getStream(objectName);
|
|
188
|
+
return result.stream;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
function getString(objectName) {
|
|
192
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
193
|
+
const stream = yield getStream(objectName);
|
|
194
|
+
return yield readableToString(stream);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
function downloadToLocal(objectName, localPath) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
const ossType = getConfig('oss.type');
|
|
200
|
+
const client = yield getOssClient(ossType);
|
|
201
|
+
if (ossType === 'minio') {
|
|
202
|
+
yield client.fGetObject(minioBucketName, localPath);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
yield client.get(objectName, localPath);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
function getJson(objectName) {
|
|
210
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
+
const str = yield getString(objectName);
|
|
212
|
+
return tryJSONParse(str, str);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* https://github.com/ali-sdk/ali-oss#putname-file-options
|
|
217
|
+
* @param {*} objectName object name store on OSS
|
|
218
|
+
* @param {*} toBeUploaded String(file path)|Buffer|ReadStream
|
|
219
|
+
*/
|
|
220
|
+
function upload(objectName, toBeUploaded) {
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
const ossType = getConfig('oss.type');
|
|
223
|
+
const client = yield getOssClient(ossType);
|
|
224
|
+
if (ossType === 'minio') {
|
|
225
|
+
yield client.putObject(minioBucketName, objectName, toBeUploaded);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
yield client.put(objectName, toBeUploaded);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
module.exports = {
|
|
233
|
+
getOssClient,
|
|
234
|
+
remove,
|
|
235
|
+
upload,
|
|
236
|
+
getString,
|
|
237
|
+
getStream,
|
|
238
|
+
getJson,
|
|
239
|
+
downloadToLocal,
|
|
240
|
+
fileExist: checkObjectNameExist
|
|
241
|
+
};
|
|
242
242
|
//# sourceMappingURL=script-storage.js.map
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export function validate(when: any, targetNodeId: any, targetPortId: any): void;
|
|
2
|
-
export function mergeDeep(...objects: any[]): any;
|
|
3
|
-
export function assertString(propertyValue: any, propertyName: any): void;
|
|
4
|
-
export function assertFunction(cb: any): void;
|
|
5
|
-
export function asyncFunction(func: any): boolean;
|
|
6
|
-
export function buildSendData(data: any, targetPortId: any, sendValue: any, dataType: any, extra: any): any;
|
|
7
|
-
export function getNodeQueueName(nodeId: any): string;
|
|
8
|
-
export function buildDashBoardData(targetPortId: any, sendValue: any, dataType: any): any;
|
|
9
|
-
export function filterNotExistFileds(ctx: any, required: any): any;
|
|
10
|
-
export function generateWrapperHandler(cb: any): (data: any, ctx: any) => any;
|
|
11
|
-
export function generateWrapperInitializer(initializerCallBack: any): (ctx: any) => any;
|
|
1
|
+
export function validate(when: any, targetNodeId: any, targetPortId: any): void;
|
|
2
|
+
export function mergeDeep(...objects: any[]): any;
|
|
3
|
+
export function assertString(propertyValue: any, propertyName: any): void;
|
|
4
|
+
export function assertFunction(cb: any): void;
|
|
5
|
+
export function asyncFunction(func: any): boolean;
|
|
6
|
+
export function buildSendData(data: any, targetPortId: any, sendValue: any, dataType: any, extra: any): any;
|
|
7
|
+
export function getNodeQueueName(nodeId: any): string;
|
|
8
|
+
export function buildDashBoardData(targetPortId: any, sendValue: any, dataType: any): any;
|
|
9
|
+
export function filterNotExistFileds(ctx: any, required: any): any;
|
|
10
|
+
export function generateWrapperHandler(cb: any): (data: any, ctx: any) => any;
|
|
11
|
+
export function generateWrapperInitializer(initializerCallBack: any): (ctx: any) => any;
|