@workskills/getskill 1.1.1 → 1.1.3
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/index.js +174 -28
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -219,11 +219,28 @@ function ensureDirectoryExists(dirPath) {
|
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
/**
|
|
222
|
-
* HTTP/HTTPS
|
|
222
|
+
* HTTP/HTTPS 请求封装(支持重定向和错误处理)
|
|
223
|
+
* @param {string} url - 请求 URL
|
|
224
|
+
* @param {Object} options - 请求选项
|
|
225
|
+
* @param {number} redirectCount - 重定向次数(内部使用)
|
|
226
|
+
* @returns {Promise<Object>} 响应对象
|
|
223
227
|
*/
|
|
224
|
-
function request(url, options = {}) {
|
|
228
|
+
function request(url, options = {}, redirectCount = 0) {
|
|
229
|
+
const MAX_REDIRECTS = 5; // 最大重定向次数
|
|
230
|
+
|
|
225
231
|
return new Promise((resolve, reject) => {
|
|
226
|
-
|
|
232
|
+
// 检查重定向次数
|
|
233
|
+
if (redirectCount >= MAX_REDIRECTS) {
|
|
234
|
+
return reject(new Error(`Too many redirects (${MAX_REDIRECTS})`));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
let urlObj;
|
|
238
|
+
try {
|
|
239
|
+
urlObj = new URL(url);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
return reject(new Error(`Invalid URL: ${url}`));
|
|
242
|
+
}
|
|
243
|
+
|
|
227
244
|
const protocol = urlObj.protocol === 'https:' ? https : http;
|
|
228
245
|
|
|
229
246
|
const reqOptions = {
|
|
@@ -231,10 +248,33 @@ function request(url, options = {}) {
|
|
|
231
248
|
port: urlObj.port,
|
|
232
249
|
path: urlObj.pathname + urlObj.search,
|
|
233
250
|
method: options.method || 'GET',
|
|
234
|
-
headers: options.headers || {}
|
|
251
|
+
headers: options.headers || {},
|
|
252
|
+
timeout: options.timeout || 30000 // 默认 30 秒超时
|
|
235
253
|
};
|
|
236
254
|
|
|
237
255
|
const req = protocol.request(reqOptions, (res) => {
|
|
256
|
+
// 处理重定向 (301, 302, 303, 307, 308)
|
|
257
|
+
if ([301, 302, 303, 307, 308].includes(res.statusCode)) {
|
|
258
|
+
const redirectUrl = res.headers.location;
|
|
259
|
+
|
|
260
|
+
if (!redirectUrl) {
|
|
261
|
+
return reject(new Error(`Redirect location header missing (${res.statusCode})`));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// 解析重定向 URL(可能是相对路径)
|
|
265
|
+
let newUrl;
|
|
266
|
+
try {
|
|
267
|
+
newUrl = new URL(redirectUrl, url).toString();
|
|
268
|
+
} catch (error) {
|
|
269
|
+
return reject(new Error(`Invalid redirect URL: ${redirectUrl}`));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// 递归处理重定向
|
|
273
|
+
return request(newUrl, options, redirectCount + 1)
|
|
274
|
+
.then(resolve)
|
|
275
|
+
.catch(reject);
|
|
276
|
+
}
|
|
277
|
+
|
|
238
278
|
let data = '';
|
|
239
279
|
|
|
240
280
|
res.on('data', (chunk) => {
|
|
@@ -249,13 +289,30 @@ function request(url, options = {}) {
|
|
|
249
289
|
body: data
|
|
250
290
|
});
|
|
251
291
|
} else {
|
|
252
|
-
reject(new Error(`Request failed with status code ${res.statusCode}`));
|
|
292
|
+
reject(new Error(`Request failed with status code ${res.statusCode}: ${data.substring(0, 200)}`));
|
|
253
293
|
}
|
|
254
294
|
});
|
|
295
|
+
|
|
296
|
+
res.on('error', (err) => {
|
|
297
|
+
reject(new Error(`Response error: ${err.message}`));
|
|
298
|
+
});
|
|
255
299
|
});
|
|
256
300
|
|
|
257
301
|
req.on('error', (err) => {
|
|
258
|
-
|
|
302
|
+
if (err.code === 'ECONNREFUSED') {
|
|
303
|
+
reject(new Error(`Connection refused: ${urlObj.hostname}`));
|
|
304
|
+
} else if (err.code === 'ENOTFOUND') {
|
|
305
|
+
reject(new Error(`Host not found: ${urlObj.hostname}`));
|
|
306
|
+
} else if (err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT') {
|
|
307
|
+
reject(new Error(`Request timeout: ${url}`));
|
|
308
|
+
} else {
|
|
309
|
+
reject(new Error(`Network error: ${err.message}`));
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
req.on('timeout', () => {
|
|
314
|
+
req.destroy();
|
|
315
|
+
reject(new Error(`Request timeout after ${reqOptions.timeout}ms`));
|
|
259
316
|
});
|
|
260
317
|
|
|
261
318
|
if (options.body) {
|
|
@@ -292,6 +349,12 @@ function executeCommand(command, cwd) {
|
|
|
292
349
|
* @returns {Promise<Array>} 技能列表(包含 git 地址)
|
|
293
350
|
*/
|
|
294
351
|
async function searchSkills(keyword, options = {}) {
|
|
352
|
+
// 参数验证
|
|
353
|
+
if (!keyword || typeof keyword !== 'string') {
|
|
354
|
+
console.error('搜索失败: 关键词无效');
|
|
355
|
+
return [];
|
|
356
|
+
}
|
|
357
|
+
|
|
295
358
|
const {
|
|
296
359
|
sort = 'updated',
|
|
297
360
|
order = 'desc',
|
|
@@ -299,14 +362,48 @@ async function searchSkills(keyword, options = {}) {
|
|
|
299
362
|
limit = 20
|
|
300
363
|
} = options;
|
|
301
364
|
|
|
302
|
-
const url = `${BASE_URL}/
|
|
365
|
+
const url = `${BASE_URL}/api/v1/repos/search?q=${encodeURIComponent(keyword)}&sort=${sort}&order=${order}&page=${page}&limit=${limit}`;
|
|
303
366
|
|
|
304
367
|
try {
|
|
305
|
-
const response = await request(url);
|
|
306
|
-
|
|
368
|
+
const response = await request(url, { timeout: 15000 });
|
|
369
|
+
|
|
370
|
+
// 检查响应体是否为空
|
|
371
|
+
if (!response.body || response.body.trim() === '') {
|
|
372
|
+
console.warn('搜索警告: 服务器返回空响应');
|
|
373
|
+
return [];
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// 尝试解析 JSON
|
|
377
|
+
let result;
|
|
378
|
+
try {
|
|
379
|
+
result = JSON.parse(response.body);
|
|
380
|
+
} catch (parseError) {
|
|
381
|
+
console.error('搜索失败: JSON 解析错误', parseError.message);
|
|
382
|
+
console.debug('响应内容:', response.body.substring(0, 200));
|
|
383
|
+
return [];
|
|
384
|
+
}
|
|
307
385
|
|
|
308
386
|
// 解析返回格式: { ok: true, data: [{ repository: {...} }] }
|
|
309
|
-
if (!result
|
|
387
|
+
if (!result) {
|
|
388
|
+
console.warn('搜索警告: 结果为空');
|
|
389
|
+
return [];
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// 检查 API 响应状态
|
|
393
|
+
if (result.ok === false) {
|
|
394
|
+
console.warn('搜索警告: API 返回失败状态', result.message || '');
|
|
395
|
+
return [];
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// 检查 data 字段
|
|
399
|
+
if (!result.data) {
|
|
400
|
+
console.warn('搜索警告: 响应中缺少 data 字段');
|
|
401
|
+
return [];
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// 确保 data 是数组
|
|
405
|
+
if (!Array.isArray(result.data)) {
|
|
406
|
+
console.warn('搜索警告: data 不是数组类型');
|
|
310
407
|
return [];
|
|
311
408
|
}
|
|
312
409
|
|
|
@@ -321,10 +418,24 @@ async function searchSkills(keyword, options = {}) {
|
|
|
321
418
|
git_url: repo.html_url ? `${repo.html_url}.git` : '',
|
|
322
419
|
clone_url: repo.clone_url || ''
|
|
323
420
|
};
|
|
324
|
-
});
|
|
421
|
+
}).filter(item => item.full_name); // 过滤掉没有 full_name 的项
|
|
422
|
+
|
|
325
423
|
} catch (error) {
|
|
326
|
-
|
|
327
|
-
|
|
424
|
+
// 友好的错误提示
|
|
425
|
+
if (error.message.includes('Host not found')) {
|
|
426
|
+
console.error('搜索失败: 无法连接到服务器,请检查网络或 API 地址配置');
|
|
427
|
+
} else if (error.message.includes('timeout')) {
|
|
428
|
+
console.error('搜索失败: 请求超时,请稍后重试');
|
|
429
|
+
} else if (error.message.includes('Connection refused')) {
|
|
430
|
+
console.error('搜索失败: 服务器拒绝连接,请检查 API 地址是否正确');
|
|
431
|
+
} else if (error.message.includes('status code')) {
|
|
432
|
+
console.error('搜索失败: 服务器返回错误', error.message);
|
|
433
|
+
} else {
|
|
434
|
+
console.error('搜索失败:', error.message);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// 返回空数组而不是抛出异常,避免程序崩溃
|
|
438
|
+
return [];
|
|
328
439
|
}
|
|
329
440
|
}
|
|
330
441
|
|
|
@@ -334,11 +445,28 @@ async function searchSkills(keyword, options = {}) {
|
|
|
334
445
|
* @returns {Promise<Object>} 技能详情
|
|
335
446
|
*/
|
|
336
447
|
async function getSkillDetail(skillId) {
|
|
448
|
+
// 参数验证
|
|
449
|
+
if (!skillId || typeof skillId !== 'string') {
|
|
450
|
+
throw new Error('技能 ID 或名称无效');
|
|
451
|
+
}
|
|
452
|
+
|
|
337
453
|
const url = `${BASE_URL}/repo/${encodeURIComponent(skillId)}`;
|
|
338
454
|
|
|
339
455
|
try {
|
|
340
|
-
const response = await request(url);
|
|
341
|
-
|
|
456
|
+
const response = await request(url, { timeout: 15000 });
|
|
457
|
+
|
|
458
|
+
// 检查响应体是否为空
|
|
459
|
+
if (!response.body || response.body.trim() === '') {
|
|
460
|
+
throw new Error('服务器返回空响应');
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// 尝试解析 JSON
|
|
464
|
+
let result;
|
|
465
|
+
try {
|
|
466
|
+
result = JSON.parse(response.body);
|
|
467
|
+
} catch (parseError) {
|
|
468
|
+
throw new Error(`JSON 解析错误: ${parseError.message}`);
|
|
469
|
+
}
|
|
342
470
|
|
|
343
471
|
// 如果返回的数据格式类似搜索接口,需要解析 repository
|
|
344
472
|
if (result.repository) {
|
|
@@ -363,8 +491,20 @@ async function getSkillDetail(skillId) {
|
|
|
363
491
|
clone_url: result.clone_url || ''
|
|
364
492
|
};
|
|
365
493
|
} catch (error) {
|
|
366
|
-
|
|
367
|
-
|
|
494
|
+
// 友好的错误提示
|
|
495
|
+
if (error.message.includes('Host not found')) {
|
|
496
|
+
throw new Error('无法连接到服务器,请检查网络或 API 地址配置');
|
|
497
|
+
} else if (error.message.includes('timeout')) {
|
|
498
|
+
throw new Error('请求超时,请稍后重试');
|
|
499
|
+
} else if (error.message.includes('Connection refused')) {
|
|
500
|
+
throw new Error('服务器拒绝连接,请检查 API 地址是否正确');
|
|
501
|
+
} else if (error.message.includes('status code 404')) {
|
|
502
|
+
throw new Error(`技能不存在: ${skillId}`);
|
|
503
|
+
} else if (error.message.includes('status code')) {
|
|
504
|
+
throw new Error(`服务器错误: ${error.message}`);
|
|
505
|
+
} else {
|
|
506
|
+
throw new Error(`获取技能详情失败: ${error.message}`);
|
|
507
|
+
}
|
|
368
508
|
}
|
|
369
509
|
}
|
|
370
510
|
|
|
@@ -547,17 +687,23 @@ async function cli() {
|
|
|
547
687
|
process.exit(1);
|
|
548
688
|
}
|
|
549
689
|
const results = await searchSkills(args[1]);
|
|
550
|
-
|
|
551
|
-
results.
|
|
552
|
-
console.log(
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
console.log(
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
690
|
+
|
|
691
|
+
if (results.length === 0) {
|
|
692
|
+
console.log('未找到相关技能');
|
|
693
|
+
console.log('提示: 尝试使用不同的关键词,或访问 ' + BASE_URL + ' 查看所有可用技能');
|
|
694
|
+
} else {
|
|
695
|
+
console.log(`找到 ${results.length} 个技能:\n`);
|
|
696
|
+
results.forEach((skill, index) => {
|
|
697
|
+
console.log(`${index + 1}. ${skill.full_name}`);
|
|
698
|
+
if (skill.description) {
|
|
699
|
+
console.log(` 描述: ${skill.description}`);
|
|
700
|
+
}
|
|
701
|
+
if (skill.git_url) {
|
|
702
|
+
console.log(` Git: ${skill.git_url}`);
|
|
703
|
+
}
|
|
704
|
+
console.log('');
|
|
705
|
+
});
|
|
706
|
+
}
|
|
561
707
|
break;
|
|
562
708
|
|
|
563
709
|
case 'install':
|