@yvhitxcel/opencode-remote 0.15.0
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 +82 -0
- package/bin/opencode-remote.js +70 -0
- package/bin/opencode-weixin.js +10 -0
- package/dist/AGENTS.md +20 -0
- package/dist/MEMORY.md +21 -0
- package/dist/bot-runner.js +180 -0
- package/dist/cli.js +256 -0
- package/dist/core/approval.js +95 -0
- package/dist/core/auth.js +119 -0
- package/dist/core/config.js +61 -0
- package/dist/core/notifications.js +134 -0
- package/dist/core/qiniu.js +267 -0
- package/dist/core/registry.js +86 -0
- package/dist/core/router.js +344 -0
- package/dist/core/session.js +403 -0
- package/dist/core/setup.js +418 -0
- package/dist/core/types.js +16 -0
- package/dist/feishu/adapter.js +72 -0
- package/dist/feishu/bot.js +168 -0
- package/dist/feishu/commands.js +601 -0
- package/dist/feishu/handler.js +380 -0
- package/dist/index.js +60 -0
- package/dist/opencode/client.js +823 -0
- package/dist/package-lock.json +762 -0
- package/dist/patch_spawn.js +28 -0
- package/dist/plugins/agents/acp/acp-adapter.js +42 -0
- package/dist/plugins/agents/claude-code/index.js +69 -0
- package/dist/plugins/agents/codex/index.js +44 -0
- package/dist/plugins/agents/copilot/index.js +44 -0
- package/dist/plugins/agents/opencode/index.js +66 -0
- package/dist/telegram/bot.js +288 -0
- package/dist/utils/message-split.js +38 -0
- package/dist/web/code-viewer.js +266 -0
- package/dist/weixin/adapter.js +135 -0
- package/dist/weixin/api.js +179 -0
- package/dist/weixin/bot.js +183 -0
- package/dist/weixin/commands.js +758 -0
- package/dist/weixin/handler.js +577 -0
- package/dist/weixin/node_modules/encodeurl/LICENSE +22 -0
- package/dist/weixin/node_modules/encodeurl/README.md +109 -0
- package/dist/weixin/node_modules/encodeurl/index.js +60 -0
- package/dist/weixin/node_modules/encodeurl/package.json +40 -0
- package/dist/weixin/node_modules/qiniu/.claude/settings.local.json +7 -0
- package/dist/weixin/node_modules/qiniu/.github/workflows/ci-test.yml +36 -0
- package/dist/weixin/node_modules/qiniu/.github/workflows/npm-publish.yml +20 -0
- package/dist/weixin/node_modules/qiniu/.github/workflows/version-check.yml +19 -0
- package/dist/weixin/node_modules/qiniu/.idea/MarsCodeWorkspaceAppSettings.xml +7 -0
- package/dist/weixin/node_modules/qiniu/.idea/codeStyles/Project.xml +44 -0
- package/dist/weixin/node_modules/qiniu/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/dist/weixin/node_modules/qiniu/.idea/git_toolbox_blame.xml +6 -0
- package/dist/weixin/node_modules/qiniu/.idea/inspectionProfiles/Project_Default.xml +6 -0
- package/dist/weixin/node_modules/qiniu/.idea/jsLibraryMappings.xml +6 -0
- package/dist/weixin/node_modules/qiniu/.idea/modules.xml +8 -0
- package/dist/weixin/node_modules/qiniu/.idea/nodejs-sdk.iml +12 -0
- package/dist/weixin/node_modules/qiniu/.idea/vcs.xml +6 -0
- package/dist/weixin/node_modules/qiniu/CHANGELOG.md +292 -0
- package/dist/weixin/node_modules/qiniu/README.md +56 -0
- package/dist/weixin/node_modules/qiniu/StorageResponseInterface.d.ts +239 -0
- package/dist/weixin/node_modules/qiniu/codecov.yml +28 -0
- package/dist/weixin/node_modules/qiniu/index.d.ts +1995 -0
- package/dist/weixin/node_modules/qiniu/index.js +32 -0
- package/dist/weixin/node_modules/qiniu/node_modules/encodeurl/HISTORY.md +14 -0
- package/dist/weixin/node_modules/qiniu/node_modules/encodeurl/LICENSE +22 -0
- package/dist/weixin/node_modules/qiniu/node_modules/encodeurl/README.md +128 -0
- package/dist/weixin/node_modules/qiniu/node_modules/encodeurl/index.js +60 -0
- package/dist/weixin/node_modules/qiniu/node_modules/encodeurl/package.json +40 -0
- package/dist/weixin/node_modules/qiniu/package.json +80 -0
- package/dist/weixin/node_modules/qiniu/qiniu/auth/digest.js +13 -0
- package/dist/weixin/node_modules/qiniu/qiniu/cdn.js +149 -0
- package/dist/weixin/node_modules/qiniu/qiniu/conf.js +254 -0
- package/dist/weixin/node_modules/qiniu/qiniu/fop.js +112 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/client.js +253 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/endpoint.js +66 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/endpointsProvider.js +27 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/endpointsRetryPolicy.js +76 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/middleware/base.js +31 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/middleware/index.js +9 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/middleware/qiniuAuth.js +53 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/middleware/retryDomains.js +101 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/middleware/ua.js +36 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/region.js +349 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/regionsProvider.js +788 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/regionsRetryPolicy.js +242 -0
- package/dist/weixin/node_modules/qiniu/qiniu/httpc/responseWrapper.js +40 -0
- package/dist/weixin/node_modules/qiniu/qiniu/retry/index.js +4 -0
- package/dist/weixin/node_modules/qiniu/qiniu/retry/retrier.js +99 -0
- package/dist/weixin/node_modules/qiniu/qiniu/retry/retryPolicy.js +55 -0
- package/dist/weixin/node_modules/qiniu/qiniu/rpc.js +237 -0
- package/dist/weixin/node_modules/qiniu/qiniu/rtc/app.js +123 -0
- package/dist/weixin/node_modules/qiniu/qiniu/rtc/credentials.js +57 -0
- package/dist/weixin/node_modules/qiniu/qiniu/rtc/room.js +118 -0
- package/dist/weixin/node_modules/qiniu/qiniu/rtc/util.js +16 -0
- package/dist/weixin/node_modules/qiniu/qiniu/sms/message.js +58 -0
- package/dist/weixin/node_modules/qiniu/qiniu/storage/form.js +442 -0
- package/dist/weixin/node_modules/qiniu/qiniu/storage/internal.js +214 -0
- package/dist/weixin/node_modules/qiniu/qiniu/storage/resume.js +1272 -0
- package/dist/weixin/node_modules/qiniu/qiniu/storage/rs.js +1764 -0
- package/dist/weixin/node_modules/qiniu/qiniu/util.js +382 -0
- package/dist/weixin/node_modules/qiniu/qiniu/zone.js +230 -0
- package/dist/weixin/node_modules/qiniu/tsconfig.json +112 -0
- package/dist/weixin/types.js +25 -0
- package/package.json +56 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
const url = require('url');
|
|
2
|
+
const crypto = require('crypto');
|
|
3
|
+
|
|
4
|
+
// Check Timestamp Expired or not
|
|
5
|
+
exports.isTimestampExpired = function (timestamp) {
|
|
6
|
+
return timestamp < Math.trunc(Date.now() / 1000);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// Format Data
|
|
10
|
+
exports.formatDateUTC = function (date, layout) {
|
|
11
|
+
function pad (num, digit) {
|
|
12
|
+
const d = digit || 2;
|
|
13
|
+
let result = num.toString();
|
|
14
|
+
while (result.length < d) {
|
|
15
|
+
result = '0' + result;
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const d = new Date(date);
|
|
21
|
+
const year = d.getUTCFullYear();
|
|
22
|
+
const month = d.getUTCMonth() + 1;
|
|
23
|
+
const day = d.getUTCDate();
|
|
24
|
+
const hour = d.getUTCHours();
|
|
25
|
+
const minute = d.getUTCMinutes();
|
|
26
|
+
const second = d.getUTCSeconds();
|
|
27
|
+
const millisecond = d.getUTCMilliseconds();
|
|
28
|
+
|
|
29
|
+
let result = layout || 'YYYY-MM-DDTHH:MM:ss.SSSZ';
|
|
30
|
+
|
|
31
|
+
result = result.replace(/YYYY/g, year.toString())
|
|
32
|
+
.replace(/MM/g, pad(month))
|
|
33
|
+
.replace(/DD/g, pad(day))
|
|
34
|
+
.replace(/HH/g, pad(hour))
|
|
35
|
+
.replace(/mm/g, pad(minute))
|
|
36
|
+
.replace(/ss/g, pad(second))
|
|
37
|
+
.replace(/SSS/g, pad(millisecond, 3));
|
|
38
|
+
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Encoded Entry
|
|
43
|
+
exports.encodedEntry = function (bucket, key) {
|
|
44
|
+
let strToEncode = bucket;
|
|
45
|
+
if (key !== undefined) {
|
|
46
|
+
strToEncode += ':' + key;
|
|
47
|
+
}
|
|
48
|
+
return exports.urlsafeBase64Encode(strToEncode);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
exports.decodedEntry = function (entry) {
|
|
52
|
+
const [bucket, key] = exports.urlSafeBase64Decode(entry).split(':');
|
|
53
|
+
return [bucket, key];
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Get accessKey from uptoken
|
|
57
|
+
exports.getAKFromUptoken = function (uploadToken) {
|
|
58
|
+
var sepIndex = uploadToken.indexOf(':');
|
|
59
|
+
return uploadToken.substring(0, sepIndex);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// Get bucket from uptoken
|
|
63
|
+
exports.getBucketFromUptoken = function (uploadToken) {
|
|
64
|
+
var sepIndex = uploadToken.lastIndexOf(':');
|
|
65
|
+
var encodedPutPolicy = uploadToken.substring(sepIndex + 1);
|
|
66
|
+
var putPolicy = exports.urlSafeBase64Decode(encodedPutPolicy);
|
|
67
|
+
var putPolicyObj = JSON.parse(putPolicy);
|
|
68
|
+
var scope = putPolicyObj.scope;
|
|
69
|
+
var scopeSepIndex = scope.indexOf(':');
|
|
70
|
+
if (scopeSepIndex == -1) {
|
|
71
|
+
return scope;
|
|
72
|
+
} else {
|
|
73
|
+
return scope.substring(0, scopeSepIndex);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
exports.base64ToUrlSafe = function (v) {
|
|
78
|
+
return v.replace(/\//g, '_').replace(/\+/g, '-');
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
exports.urlSafeToBase64 = function (v) {
|
|
82
|
+
return v.replace(/_/g, '/').replace(/-/g, '+');
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// UrlSafe Base64 Decode
|
|
86
|
+
exports.urlsafeBase64Encode = function (jsonFlags) {
|
|
87
|
+
var encoded = Buffer.from(jsonFlags).toString('base64');
|
|
88
|
+
return exports.base64ToUrlSafe(encoded);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// UrlSafe Base64 Decode
|
|
92
|
+
exports.urlSafeBase64Decode = function (fromStr) {
|
|
93
|
+
return Buffer.from(exports.urlSafeToBase64(fromStr), 'base64').toString();
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// Hmac-sha1 Crypt
|
|
97
|
+
exports.hmacSha1 = function (encodedFlags, secretKey) {
|
|
98
|
+
/*
|
|
99
|
+
*return value already encoded with base64
|
|
100
|
+
* */
|
|
101
|
+
var hmac = crypto.createHmac('sha1', secretKey);
|
|
102
|
+
hmac.update(encodedFlags);
|
|
103
|
+
return hmac.digest('base64');
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// get md5
|
|
107
|
+
exports.getMd5 = function (data) {
|
|
108
|
+
var md5 = crypto.createHash('md5');
|
|
109
|
+
return md5.update(data).digest('hex');
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// 创建 AccessToken 凭证
|
|
113
|
+
// @param mac AK&SK对象
|
|
114
|
+
// @param requestURI 请求URL
|
|
115
|
+
// @param reqBody 请求Body,仅当请求的 ContentType 为
|
|
116
|
+
// application/x-www-form-urlencoded时才需要传入该参数
|
|
117
|
+
exports.generateAccessToken = function (mac, requestURI, reqBody) {
|
|
118
|
+
const u = new url.URL(requestURI);
|
|
119
|
+
const path = u.pathname + u.search;
|
|
120
|
+
let access = path + '\n';
|
|
121
|
+
|
|
122
|
+
if (reqBody) {
|
|
123
|
+
access += reqBody;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const digest = exports.hmacSha1(access, mac.secretKey);
|
|
127
|
+
const safeDigest = exports.base64ToUrlSafe(digest);
|
|
128
|
+
return 'QBox ' + mac.accessKey + ':' + safeDigest;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const isTokenTable = {
|
|
132
|
+
'!': true,
|
|
133
|
+
'#': true,
|
|
134
|
+
$: true,
|
|
135
|
+
'%': true,
|
|
136
|
+
'&': true,
|
|
137
|
+
'\\': true,
|
|
138
|
+
'*': true,
|
|
139
|
+
'+': true,
|
|
140
|
+
'-': true,
|
|
141
|
+
'.': true,
|
|
142
|
+
0: true,
|
|
143
|
+
1: true,
|
|
144
|
+
2: true,
|
|
145
|
+
3: true,
|
|
146
|
+
4: true,
|
|
147
|
+
5: true,
|
|
148
|
+
6: true,
|
|
149
|
+
7: true,
|
|
150
|
+
8: true,
|
|
151
|
+
9: true,
|
|
152
|
+
A: true,
|
|
153
|
+
B: true,
|
|
154
|
+
C: true,
|
|
155
|
+
D: true,
|
|
156
|
+
E: true,
|
|
157
|
+
F: true,
|
|
158
|
+
G: true,
|
|
159
|
+
H: true,
|
|
160
|
+
I: true,
|
|
161
|
+
J: true,
|
|
162
|
+
K: true,
|
|
163
|
+
L: true,
|
|
164
|
+
M: true,
|
|
165
|
+
N: true,
|
|
166
|
+
O: true,
|
|
167
|
+
P: true,
|
|
168
|
+
Q: true,
|
|
169
|
+
R: true,
|
|
170
|
+
S: true,
|
|
171
|
+
T: true,
|
|
172
|
+
U: true,
|
|
173
|
+
W: true,
|
|
174
|
+
V: true,
|
|
175
|
+
X: true,
|
|
176
|
+
Y: true,
|
|
177
|
+
Z: true,
|
|
178
|
+
'^': true,
|
|
179
|
+
_: true,
|
|
180
|
+
'`': true,
|
|
181
|
+
a: true,
|
|
182
|
+
b: true,
|
|
183
|
+
c: true,
|
|
184
|
+
d: true,
|
|
185
|
+
e: true,
|
|
186
|
+
f: true,
|
|
187
|
+
g: true,
|
|
188
|
+
h: true,
|
|
189
|
+
i: true,
|
|
190
|
+
j: true,
|
|
191
|
+
k: true,
|
|
192
|
+
l: true,
|
|
193
|
+
m: true,
|
|
194
|
+
n: true,
|
|
195
|
+
o: true,
|
|
196
|
+
p: true,
|
|
197
|
+
q: true,
|
|
198
|
+
r: true,
|
|
199
|
+
s: true,
|
|
200
|
+
t: true,
|
|
201
|
+
u: true,
|
|
202
|
+
v: true,
|
|
203
|
+
w: true,
|
|
204
|
+
x: true,
|
|
205
|
+
y: true,
|
|
206
|
+
z: true,
|
|
207
|
+
'|': true,
|
|
208
|
+
'~': true
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* 是否合法的 header field name 字符
|
|
212
|
+
* @param ch string
|
|
213
|
+
* @returns boolean|undefined
|
|
214
|
+
*/
|
|
215
|
+
function validHeaderKeyChar (ch) {
|
|
216
|
+
if (ch.charCodeAt(0) >= 128) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
return isTokenTable[ch];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 规范化 header field name
|
|
224
|
+
* @param fieldName string
|
|
225
|
+
* @returns string
|
|
226
|
+
*/
|
|
227
|
+
exports.canonicalMimeHeaderKey = function (fieldName) {
|
|
228
|
+
for (const ch of fieldName) {
|
|
229
|
+
if (!validHeaderKeyChar(ch)) {
|
|
230
|
+
return fieldName;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return fieldName.split('-')
|
|
234
|
+
.map(function (text) {
|
|
235
|
+
return text.substring(0, 1).toUpperCase() + text.substring(1).toLowerCase();
|
|
236
|
+
})
|
|
237
|
+
.join('-');
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 创建 AccessToken 凭证
|
|
242
|
+
* @param {Mac} mac AK&SK对象
|
|
243
|
+
* @param {string} requestURI 请求URL
|
|
244
|
+
* @param {string} reqMethod 请求方法,例如 GET,POST
|
|
245
|
+
* @param {string} reqContentType 请求类型,例如 application/json 或者 application/x-www-form-urlencoded
|
|
246
|
+
* @param {string | Buffer} reqBody 请求Body,仅当请求的 ContentType 为 application/json 或者
|
|
247
|
+
* application/x-www-form-urlencoded 时才需要传入该参数
|
|
248
|
+
* @param {Object.<string, string>} reqHeaders 请求头部
|
|
249
|
+
*/
|
|
250
|
+
exports.generateAccessTokenV2 = function (mac, requestURI, reqMethod, reqContentType, reqBody, reqHeaders) {
|
|
251
|
+
const u = new url.URL(requestURI);
|
|
252
|
+
const path = u.pathname;
|
|
253
|
+
const search = u.search;
|
|
254
|
+
const host = u.host;
|
|
255
|
+
const port = u.port;
|
|
256
|
+
|
|
257
|
+
let access = reqMethod.toUpperCase() + ' ' + path;
|
|
258
|
+
if (search) {
|
|
259
|
+
access += search;
|
|
260
|
+
}
|
|
261
|
+
// add host
|
|
262
|
+
access += '\nHost: ' + host;
|
|
263
|
+
// add port
|
|
264
|
+
if (port) {
|
|
265
|
+
access += ':' + port;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// add content type
|
|
269
|
+
if (reqContentType) {
|
|
270
|
+
access += '\nContent-Type: ' + reqContentType;
|
|
271
|
+
} else {
|
|
272
|
+
access += '\nContent-Type: application/x-www-form-urlencoded';
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// add headers
|
|
276
|
+
if (reqHeaders) {
|
|
277
|
+
const canonicalHeaders = Object.keys(reqHeaders)
|
|
278
|
+
.reduce(function (acc, k) {
|
|
279
|
+
acc[exports.canonicalMimeHeaderKey(k)] = reqHeaders[k];
|
|
280
|
+
return acc;
|
|
281
|
+
}, {});
|
|
282
|
+
const headerText = Object.keys(canonicalHeaders)
|
|
283
|
+
.filter(function (k) {
|
|
284
|
+
return k.startsWith('X-Qiniu-') && k.length > 'X-Qiniu-'.length;
|
|
285
|
+
})
|
|
286
|
+
.sort()
|
|
287
|
+
.map(function (k) {
|
|
288
|
+
return k + ': ' + canonicalHeaders[k];
|
|
289
|
+
})
|
|
290
|
+
.join('\n');
|
|
291
|
+
if (headerText) {
|
|
292
|
+
access += '\n' + headerText;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
access += '\n\n';
|
|
297
|
+
|
|
298
|
+
// add reqbody
|
|
299
|
+
if (reqBody && reqContentType !== 'application/octet-stream') {
|
|
300
|
+
access += reqBody;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const digest = exports.hmacSha1(access, mac.secretKey);
|
|
304
|
+
const safeDigest = exports.base64ToUrlSafe(digest);
|
|
305
|
+
return 'Qiniu ' + mac.accessKey + ':' + safeDigest;
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
// 校验七牛上传回调的Authorization
|
|
309
|
+
// @param mac AK&SK对象
|
|
310
|
+
// @param requestURI 回调的URL中的requestURI
|
|
311
|
+
// @param reqBody 请求Body,仅当请求的ContentType为
|
|
312
|
+
// application/x-www-form-urlencoded时才需要传入该参数
|
|
313
|
+
// @param callbackAuth 回调时请求的Authorization头部值
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* @param {Mac} mac AK&SK对象
|
|
317
|
+
* @param {string} requestURI 请求URL
|
|
318
|
+
* @param {string | Buffer} reqBody 请求Body,仅当请求的 ContentType 为 application/json 或 application/x-www-form-urlencoded 时才需要传入该参数
|
|
319
|
+
* @param {string} callbackAuth 回调时请求的 Authorization 头部值
|
|
320
|
+
* @param {Object} [extra] 当回调为 Qiniu 签名时需要传入
|
|
321
|
+
* @param {string} extra.reqMethod 请求方法,例如 GET,POST
|
|
322
|
+
* @param {string} [extra.reqContentType] 请求类型,例如 application/json 或者 application/x-www-form-urlencoded
|
|
323
|
+
* @param {Object.<string, string>} [extra.reqHeaders] 请求头部
|
|
324
|
+
* @return {boolean}
|
|
325
|
+
*/
|
|
326
|
+
exports.isQiniuCallback = function (
|
|
327
|
+
mac,
|
|
328
|
+
requestURI,
|
|
329
|
+
reqBody,
|
|
330
|
+
callbackAuth,
|
|
331
|
+
extra
|
|
332
|
+
) {
|
|
333
|
+
let auth;
|
|
334
|
+
if (callbackAuth.startsWith('Qiniu')) {
|
|
335
|
+
auth = exports.generateAccessTokenV2(
|
|
336
|
+
mac,
|
|
337
|
+
requestURI,
|
|
338
|
+
extra.reqMethod,
|
|
339
|
+
extra.reqContentType,
|
|
340
|
+
reqBody,
|
|
341
|
+
extra.reqHeaders
|
|
342
|
+
);
|
|
343
|
+
} else {
|
|
344
|
+
auth = exports.generateAccessToken(mac, requestURI, reqBody);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return auth === callbackAuth;
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* @deprecated use Config.prototype.getRegionsProvider instead
|
|
352
|
+
*/
|
|
353
|
+
exports.prepareZone = function (ctx, accessKey, bucket, callback) {
|
|
354
|
+
// resolve cycle dependency by require dynamically
|
|
355
|
+
const zone = require('./zone');
|
|
356
|
+
var useCache = false;
|
|
357
|
+
if (ctx.config.zone !== '' && ctx.config.zone != null) {
|
|
358
|
+
if (ctx.config.zoneExpire === -1) {
|
|
359
|
+
useCache = true;
|
|
360
|
+
} else {
|
|
361
|
+
if (!exports.isTimestampExpired(ctx.config.zoneExpire)) {
|
|
362
|
+
useCache = true;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (useCache) {
|
|
368
|
+
callback(null, ctx);
|
|
369
|
+
} else {
|
|
370
|
+
zone.getZoneInfo(accessKey, bucket, function (err, cZoneInfo,
|
|
371
|
+
cZoneExpire) {
|
|
372
|
+
if (err) {
|
|
373
|
+
callback(err);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
// update object
|
|
377
|
+
ctx.config.zone = cZoneInfo;
|
|
378
|
+
ctx.config.zoneExpire = cZoneExpire + Math.trunc(Date.now() / 1000);
|
|
379
|
+
callback(null, ctx);
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
};
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
const rpc = require('./rpc');
|
|
2
|
+
const { RetryDomainsMiddleware } = require('./httpc/middleware');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated use qiniu/httpc/region.js instead
|
|
6
|
+
* @param {string[]} srcUpHosts
|
|
7
|
+
* @param {string[]} cdnUpHosts
|
|
8
|
+
* @param {string} ioHost
|
|
9
|
+
* @param {string} rsHost
|
|
10
|
+
* @param {string} rsfHost
|
|
11
|
+
* @param {string} apiHost
|
|
12
|
+
* @constructor
|
|
13
|
+
*/
|
|
14
|
+
function Zone (
|
|
15
|
+
srcUpHosts,
|
|
16
|
+
cdnUpHosts,
|
|
17
|
+
ioHost,
|
|
18
|
+
rsHost,
|
|
19
|
+
rsfHost,
|
|
20
|
+
apiHost
|
|
21
|
+
) {
|
|
22
|
+
this.srcUpHosts = srcUpHosts || [];
|
|
23
|
+
this.cdnUpHosts = cdnUpHosts || [];
|
|
24
|
+
this.ioHost = ioHost || '';
|
|
25
|
+
this.rsHost = rsHost;
|
|
26
|
+
this.rsfHost = rsfHost;
|
|
27
|
+
this.apiHost = apiHost;
|
|
28
|
+
|
|
29
|
+
// set specific hosts if possible
|
|
30
|
+
const dotIndex = this.ioHost.indexOf('.');
|
|
31
|
+
if (dotIndex !== -1) {
|
|
32
|
+
const ioTag = this.ioHost.substring(0, dotIndex);
|
|
33
|
+
const zoneSepIndex = ioTag.indexOf('-');
|
|
34
|
+
if (zoneSepIndex !== -1) {
|
|
35
|
+
const zoneTag = ioTag.substring(zoneSepIndex + 1);
|
|
36
|
+
switch (zoneTag) {
|
|
37
|
+
case 'z1':
|
|
38
|
+
!this.rsHost && (this.rsHost = 'rs-z1.qbox.me');
|
|
39
|
+
!this.rsfHost && (this.rsfHost = 'rsf-z1.qbox.me');
|
|
40
|
+
!this.apiHost && (this.apiHost = 'api-z1.qiniuapi.com');
|
|
41
|
+
break;
|
|
42
|
+
case 'z2':
|
|
43
|
+
!this.rsHost && (this.rsHost = 'rs-z2.qbox.me');
|
|
44
|
+
!this.rsfHost && (this.rsfHost = 'rsf-z2.qbox.me');
|
|
45
|
+
!this.apiHost && (this.apiHost = 'api-z2.qiniuapi.com');
|
|
46
|
+
break;
|
|
47
|
+
case 'na0':
|
|
48
|
+
!this.rsHost && (this.rsHost = 'rs-na0.qbox.me');
|
|
49
|
+
!this.rsfHost && (this.rsfHost = 'rsf-na0.qbox.me');
|
|
50
|
+
!this.apiHost && (this.apiHost = 'api-na0.qiniuapi.com');
|
|
51
|
+
break;
|
|
52
|
+
case 'as0':
|
|
53
|
+
!this.rsHost && (this.rsHost = 'rs-as0.qbox.me');
|
|
54
|
+
!this.rsfHost && (this.rsfHost = 'rsf-as0.qbox.me');
|
|
55
|
+
!this.apiHost && (this.apiHost = 'api-as0.qiniuapi.com');
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
!this.rsHost && (this.rsHost = 'rs.qiniu.com');
|
|
62
|
+
!this.rsfHost && (this.rsfHost = 'rsf.qiniu.com');
|
|
63
|
+
!this.apiHost && (this.apiHost = 'api.qiniuapi.com');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
exports.Zone = Zone;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* huadong
|
|
70
|
+
* @type {Zone}
|
|
71
|
+
* @deprecated use Region.fromRegionId('z0') instead
|
|
72
|
+
*/
|
|
73
|
+
exports.Zone_z0 = new Zone([
|
|
74
|
+
'up.qiniup.com'
|
|
75
|
+
], [
|
|
76
|
+
'upload.qiniup.com'
|
|
77
|
+
], 'iovip.qbox.me',
|
|
78
|
+
'rs.qbox.me',
|
|
79
|
+
'rsf.qbox.me',
|
|
80
|
+
'api.qiniuapi.com');
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* huadong2
|
|
84
|
+
* @type {Zone}
|
|
85
|
+
* @deprecated use Region.fromRegionId('cn-east-2') instead
|
|
86
|
+
*/
|
|
87
|
+
exports.Zone_cn_east_2 = new Zone([
|
|
88
|
+
'up-cn-east-2.qiniup.com'
|
|
89
|
+
], [
|
|
90
|
+
'upload-cn-east-2.qiniup.com'
|
|
91
|
+
], 'iovip-cn-east-2.qiniuio.com',
|
|
92
|
+
'rs-cn-east-2.qiniuapi.com',
|
|
93
|
+
'rsf-cn-east-2.qiniuapi.com',
|
|
94
|
+
'api-cn-east-2.qiniuapi.com');
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* huabei
|
|
98
|
+
* @type {Zone}
|
|
99
|
+
* @deprecated use Region.fromRegionId('z1') instead
|
|
100
|
+
*/
|
|
101
|
+
exports.Zone_z1 = new Zone([
|
|
102
|
+
'up-z1.qiniup.com'
|
|
103
|
+
], [
|
|
104
|
+
'upload-z1.qiniup.com'
|
|
105
|
+
], 'iovip-z1.qbox.me',
|
|
106
|
+
'rs-z1.qbox.me',
|
|
107
|
+
'rsf-z1.qbox.me',
|
|
108
|
+
'api-z1.qiniuapi.com');
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* huanan
|
|
112
|
+
* @type {Zone}
|
|
113
|
+
* @deprecated use Region.fromRegionId('z2') instead
|
|
114
|
+
*/
|
|
115
|
+
exports.Zone_z2 = new Zone([
|
|
116
|
+
'up-z2.qiniup.com'
|
|
117
|
+
], [
|
|
118
|
+
'upload-z2.qiniup.com'
|
|
119
|
+
], 'iovip-z2.qbox.me',
|
|
120
|
+
'rs-z2.qbox.me',
|
|
121
|
+
'rsf-z2.qbox.me',
|
|
122
|
+
'api-z2.qiniuapi.com');
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* beimei
|
|
126
|
+
* @type {Zone}
|
|
127
|
+
* @deprecated use Region.fromRegionId('na0') instead
|
|
128
|
+
*/
|
|
129
|
+
exports.Zone_na0 = new Zone([
|
|
130
|
+
'up-na0.qiniup.com'
|
|
131
|
+
], [
|
|
132
|
+
'upload-na0.qiniup.com'
|
|
133
|
+
], 'iovip-na0.qbox.me',
|
|
134
|
+
'rs-na0.qbox.me',
|
|
135
|
+
'rsf-na0.qbox.me',
|
|
136
|
+
'api-na0.qiniuapi.com');
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* singapore
|
|
140
|
+
* @type {Zone}
|
|
141
|
+
* @deprecated use Region.fromRegionId('as0') instead
|
|
142
|
+
*/
|
|
143
|
+
exports.Zone_as0 = new Zone([
|
|
144
|
+
'up-as0.qiniup.com'
|
|
145
|
+
], [
|
|
146
|
+
'upload-as0.qiniup.com'
|
|
147
|
+
], 'iovip-as0.qbox.me',
|
|
148
|
+
'rs-as0.qbox.me',
|
|
149
|
+
'rsf-as0.qbox.me',
|
|
150
|
+
'api-as0.qiniuapi.com');
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @deprecated use QueryRegionsProvider instead
|
|
154
|
+
* @param {string} accessKey
|
|
155
|
+
* @param {string} bucket
|
|
156
|
+
* @param {function(Error | null, any, any)} callbackFunc
|
|
157
|
+
*/
|
|
158
|
+
exports.getZoneInfo = function (accessKey, bucket, callbackFunc) {
|
|
159
|
+
// resolve cycle dependency by require dynamically
|
|
160
|
+
const conf = require('./conf');
|
|
161
|
+
const apiAddr = 'https://' + conf.QUERY_REGION_HOST + '/v4/query';
|
|
162
|
+
|
|
163
|
+
rpc.qnHttpClient.get({
|
|
164
|
+
url: apiAddr,
|
|
165
|
+
params: {
|
|
166
|
+
ak: accessKey,
|
|
167
|
+
bucket: bucket
|
|
168
|
+
},
|
|
169
|
+
middlewares: [
|
|
170
|
+
new RetryDomainsMiddleware({
|
|
171
|
+
backupDomains: conf.QUERY_REGION_BACKUP_HOSTS
|
|
172
|
+
})
|
|
173
|
+
],
|
|
174
|
+
callback: function (respErr, respData, respInfo) {
|
|
175
|
+
if (respErr) {
|
|
176
|
+
callbackFunc(respErr, null, null);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (respInfo.statusCode !== 200) {
|
|
181
|
+
// not ok
|
|
182
|
+
respErr = new Error(respInfo.statusCode + '\n' + respData);
|
|
183
|
+
callbackFunc(respErr, null, null);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let zoneData;
|
|
188
|
+
try {
|
|
189
|
+
const hosts = respData.hosts;
|
|
190
|
+
if (!hosts || !hosts.length) {
|
|
191
|
+
respErr = new Error('no host available: ' + respData);
|
|
192
|
+
callbackFunc(respErr, null, null);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
zoneData = hosts[0];
|
|
196
|
+
} catch (err) {
|
|
197
|
+
callbackFunc(err, null, null);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
let srcUpHosts = [];
|
|
201
|
+
let cdnUpHosts = [];
|
|
202
|
+
let zoneExpire = 0;
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
zoneExpire = zoneData.ttl;
|
|
206
|
+
// read src hosts
|
|
207
|
+
srcUpHosts = zoneData.up.domains;
|
|
208
|
+
|
|
209
|
+
// read acc hosts
|
|
210
|
+
cdnUpHosts = zoneData.up.domains;
|
|
211
|
+
|
|
212
|
+
const ioHost = zoneData.io.domains[0];
|
|
213
|
+
const rsHost = zoneData.rs.domains[0];
|
|
214
|
+
const rsfHost = zoneData.rsf.domains[0];
|
|
215
|
+
const apiHost = zoneData.api.domains[0];
|
|
216
|
+
const zoneInfo = new Zone(
|
|
217
|
+
srcUpHosts,
|
|
218
|
+
cdnUpHosts,
|
|
219
|
+
ioHost,
|
|
220
|
+
rsHost,
|
|
221
|
+
rsfHost,
|
|
222
|
+
apiHost
|
|
223
|
+
);
|
|
224
|
+
callbackFunc(null, zoneInfo, zoneExpire);
|
|
225
|
+
} catch (e) {
|
|
226
|
+
callbackFunc(e, null, null);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
};
|