dsh-github-studio 0.6.6 → 0.7.1
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 +39 -11
- package/lib/client.js +41 -6
- package/lib/core.js +4 -3
- package/lib/core.mjs +4 -3
- package/lib/credential-store.js +174 -0
- package/lib/credential-store.mjs +174 -0
- package/lib/diagnostics.js +2 -2
- package/lib/diagnostics.mjs +2 -2
- package/lib/dsh-go.js +148 -0
- package/lib/dsh-go.mjs +148 -0
- package/lib/gh.js +104 -12
- package/lib/gh.mjs +104 -12
- package/lib/http.js +2 -1
- package/lib/http.mjs +2 -1
- package/lib/index.js +9 -2
- package/lib/network.js +32 -0
- package/lib/network.mjs +32 -0
- package/lib/oauth-setup.js +2 -0
- package/lib/oauth-setup.mjs +2 -0
- package/package.json +23 -1
package/lib/gh.mjs
CHANGED
|
@@ -1,17 +1,109 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import {setTimeout as delay} from 'node:timers/promises';
|
|
2
|
+
import {networkFailure} from './network.mjs';
|
|
3
|
+
import {credentialStore as platformCredentialStore} from './credential-store.mjs';
|
|
4
|
+
import {mkdir,readFile,writeFile} from 'node:fs/promises';
|
|
3
5
|
import path from 'node:path';
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
+
export {command,windowsSeal} from './credential-store.mjs';
|
|
7
|
+
|
|
6
8
|
export class GhAuth{
|
|
7
|
-
constructor(root,{fetchImpl=fetch,seal=
|
|
9
|
+
constructor(root,{fetchImpl=fetch,seal,credentialStore=platformCredentialStore(root,seal),sleep=(ms,signal)=>delay(ms,undefined,{signal}),diagnostics=()=>{},now=Date.now}={}){
|
|
10
|
+
Object.assign(this,{root,fetch:fetchImpl,credentialStore,sleep,diagnostics,now});this.login={state:'idle'};this.epoch=0;
|
|
11
|
+
}
|
|
8
12
|
async clientId(){try{return JSON.parse(await readFile(path.join(this.root,'oauth.json'),'utf8')).clientId;}catch{if(process.env.DSH_GITHUB_CLIENT_ID)return process.env.DSH_GITHUB_CLIENT_ID;try{return JSON.parse(await readFile(new URL('./publisher.json',import.meta.url),'utf8')).clientId??'';}catch{return '';}}}
|
|
9
|
-
async configure(clientId){if(!/^[A-Za-z0-9_.-]{5,120}$/.test(clientId))throw Error('请输入有效的 OAuth App Client ID。');if(['preparing','waiting'].includes(this.login.state))throw Error('
|
|
10
|
-
async hasCredential(){try{await
|
|
11
|
-
async
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
async configure(clientId){if(!/^[A-Za-z0-9_.-]{5,120}$/.test(clientId))throw Error('请输入有效的 OAuth App Client ID。');if(['preparing','waiting'].includes(this.login.state))throw Error('请先完成或取消当前登录。');await mkdir(this.root,{recursive:true});await writeFile(path.join(this.root,'oauth.json'),JSON.stringify({clientId}));}
|
|
14
|
+
async hasCredential(){try{return await this.credentialStore.exists();}catch{return false;}}
|
|
15
|
+
async credential(){
|
|
16
|
+
let plain;
|
|
17
|
+
try { plain = await this.credentialStore.read(); }
|
|
18
|
+
catch (error) {
|
|
19
|
+
this.diagnostics('credential_read',{reason:error.reason??'read_failed',hasCredential:error.hasCredential});
|
|
20
|
+
throw error.status ? error : Object.assign(Error('无法读取本机 GitHub 凭据。'),{status:503});
|
|
21
|
+
}
|
|
22
|
+
if(plain===null){
|
|
23
|
+
this.diagnostics('credential_read',{reason:'missing',hasCredential:false});
|
|
24
|
+
throw Object.assign(Error('请先在设置 → GitHub 中登录。'),{status:401});
|
|
25
|
+
}
|
|
26
|
+
if(typeof plain!=='string'||!plain.trim()){this.diagnostics('credential_read',{reason:'invalid_local',hasCredential:true});throw Object.assign(Error('本机 GitHub 凭据为空,请重新连接。'),{status:401});}
|
|
27
|
+
let data;if(plain.startsWith('{')){try{data=JSON.parse(plain);}catch{throw Object.assign(Error('本机 GitHub 凭据损坏,请重新连接。'),{status:401});}}else data={access_token:plain};
|
|
28
|
+
if(typeof data?.access_token!=='string'||!data.access_token.trim())throw Object.assign(Error('本机 GitHub 凭据无效,请重新连接。'),{status:401});
|
|
29
|
+
this.diagnostics('credential_read',{reason:'ok',hasCredential:true});return data;
|
|
30
|
+
}
|
|
31
|
+
async save(r,epoch,clientId){
|
|
32
|
+
if(typeof r.access_token!=='string'||!r.access_token.trim())throw Error('GitHub 未返回有效凭据,请重新授权。');
|
|
33
|
+
const data={access_token:r.access_token,clientId,...(typeof r.refresh_token==='string'?{refresh_token:r.refresh_token}:{}),
|
|
34
|
+
...(Number(r.expires_in)>0?{expiresAt:this.now()+Number(r.expires_in)*1000}:{}),
|
|
35
|
+
...(Number(r.refresh_token_expires_in)>0?{refreshExpiresAt:this.now()+Number(r.refresh_token_expires_in)*1000}:{})};
|
|
36
|
+
const checkCurrent = ()=>{
|
|
37
|
+
if(epoch!==this.epoch)throw new DOMException('登录已取消。','AbortError');
|
|
38
|
+
};
|
|
39
|
+
await this.mutate(async()=>{
|
|
40
|
+
checkCurrent();
|
|
41
|
+
await this.credentialStore.write(JSON.stringify(data),checkCurrent);
|
|
42
|
+
});
|
|
43
|
+
this.diagnostics('credential_saved',{reason:'ok',hasCredential:true});return data.access_token;
|
|
44
|
+
}
|
|
45
|
+
async token(){const d=await this.credential();if(!['preparing','waiting'].includes(this.login.state)&&d.refresh_token&&d.expiresAt&&this.now()>=d.expiresAt-60000)return this.refresh(d);return d.access_token;}
|
|
46
|
+
async refreshAfter401(rejected){if(['preparing','waiting'].includes(this.login.state))return null;const d=await this.credential();if(d.access_token!==rejected)return d.access_token;if(!d.refresh_token)return null;return this.refresh(d);}
|
|
47
|
+
async refresh(d){
|
|
48
|
+
if(this.refreshing)return this.refreshing;
|
|
49
|
+
const epoch=this.epoch,signal=this.refreshController=new AbortController();
|
|
50
|
+
this.refreshing=(async()=>{if(d.refreshExpiresAt&&this.now()>=d.refreshExpiresAt)throw Object.assign(Error('GitHub 续期凭据已过期,请重新登录。'),{status:401});
|
|
51
|
+
// Rotation can succeed before a response is lost. Never blindly replay refresh requests.
|
|
52
|
+
const r=await this.request('https://github.com/login/oauth/access_token',{client_id:d.clientId??await this.clientId(),grant_type:'refresh_token',refresh_token:d.refresh_token},{signal:signal.signal,attempts:1});
|
|
53
|
+
if(r.error||!r.refresh_token)throw Object.assign(Error('GitHub 登录已失效或续期响应不完整,请重新授权。'),{status:401});
|
|
54
|
+
return this.save(r,epoch,d.clientId??await this.clientId());
|
|
55
|
+
})();try{return await this.refreshing;}finally{this.refreshing=null;this.refreshController=null;}
|
|
56
|
+
}
|
|
57
|
+
async request(url,fields,{signal,attempts=3,deadline=Infinity,onRetry=()=>{}}={}){
|
|
58
|
+
for(let attempt=0;;attempt++){
|
|
59
|
+
signal?.throwIfAborted();const remaining=deadline-this.now();if(remaining<=0)throw Object.assign(Error('设备授权已过期,请重新登录。'),{reason:'expired_token'});
|
|
60
|
+
try{
|
|
61
|
+
const timeout=AbortSignal.timeout(Math.max(1,Math.min(30000,Math.ceil(remaining))));
|
|
62
|
+
const r=await this.fetch(url,{method:'POST',headers:{Accept:'application/json','Content-Type':'application/x-www-form-urlencoded'},body:new URLSearchParams(fields),signal:signal?AbortSignal.any([signal,timeout]):timeout,redirect:'error'});
|
|
63
|
+
if(!r.ok){await r.body?.cancel();throw Object.assign(Error('GitHub 授权服务暂时不可用('+r.status+')。'),{status:r.status,retryable:r.status===429||r.status>=500,retryAfter:Number(r.headers.get('retry-after'))||0,reason:'server_error'});}
|
|
64
|
+
try{return await r.json();}catch{throw Object.assign(Error('GitHub 返回的授权数据不完整,请检查加速线路。'),{retryable:true,reason:'network_error'});}
|
|
65
|
+
}catch(error){
|
|
66
|
+
if(signal?.aborted)throw signal.reason;
|
|
67
|
+
const e=error.reason||error.status?error:networkFailure(error);
|
|
68
|
+
if(!e.retryable||attempt+1>=attempts)throw e;
|
|
69
|
+
const wait=Math.max(1000*2**Math.min(attempt,4),(e.retryAfter??0)*1000);
|
|
70
|
+
if(this.now()+wait>=deadline)throw Object.assign(Error('设备授权已过期,请重新登录。'),{reason:'expired_token'});
|
|
71
|
+
onRetry(e);await this.sleep(wait,signal);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async start(){
|
|
76
|
+
if(['preparing','waiting'].includes(this.login.state))return this.login;
|
|
77
|
+
const epoch=++this.epoch,controller=this.controller=new AbortController(),signal=controller.signal;
|
|
78
|
+
this.refreshController?.abort();this.login={state:'preparing'};let clientId;
|
|
79
|
+
try{clientId=await this.clientId();if(!clientId)throw Error('请先配置 GitHub OAuth App Client ID,并启用 Device Flow。');}
|
|
80
|
+
catch(e){this.login={state:'error',message:e.message};throw e;}
|
|
81
|
+
if(epoch!==this.epoch)return this.login;
|
|
82
|
+
this.diagnostics('oauth_started',{category:'authorization'});
|
|
83
|
+
this.task=(async()=>{try{
|
|
84
|
+
const notify=e=>{if(epoch===this.epoch){this.login={...this.login,message:e.message+' 正在自动重试…'};this.diagnostics('oauth_retry',{category:'authorization',reason:e.reason});}};
|
|
85
|
+
const d=await this.request('https://github.com/login/device/code',{client_id:clientId,scope:'repo offline_access'},{signal,onRetry:notify});
|
|
86
|
+
if(d.error||!d.device_code||!d.user_code)throw Object.assign(Error('无法开始 GitHub 授权,请检查 Client ID 及 Device Flow 配置。'),{reason:d.error});
|
|
87
|
+
signal.throwIfAborted();this.login={state:'waiting',code:d.user_code,url:'https://github.com/login/device'};
|
|
88
|
+
this.diagnostics('oauth_waiting',{category:'authorization'});
|
|
89
|
+
let interval=Math.max(5,Number(d.interval)||5),failures=0;const deadline=this.now()+Math.min(900,Number(d.expires_in)||900)*1000;
|
|
90
|
+
while(this.now()<deadline){
|
|
91
|
+
const wait=Math.max(interval,Math.min(60,interval*2**Math.min(failures,4)))*1000;
|
|
92
|
+
if(this.now()+wait>=deadline)break;await this.sleep(wait,signal);signal.throwIfAborted();if(this.now()>=deadline)break;
|
|
93
|
+
let r;try{r=await this.request('https://github.com/login/oauth/access_token',{client_id:clientId,device_code:d.device_code,grant_type:'urn:ietf:params:oauth:grant-type:device_code'},{signal,attempts:1,deadline});}
|
|
94
|
+
catch(e){if(signal.aborted||!e.retryable)throw e;failures++;interval=Math.max(interval,e.retryAfter??0);notify(e);continue;}
|
|
95
|
+
failures=0;this.login={...this.login,message:undefined};
|
|
96
|
+
if(r.access_token){await this.save(r,epoch,clientId);signal.throwIfAborted();this.login={state:'complete'};return;}
|
|
97
|
+
if(r.error==='authorization_pending')continue;
|
|
98
|
+
if(r.error==='slow_down'){interval=Math.max(interval+5,Number(r.interval)||0);continue;}
|
|
99
|
+
throw Object.assign(Error(r.error==='access_denied'?'你已取消 GitHub 授权。':'GitHub 授权已过期或被拒绝,请重新登录。'),{reason:r.error});
|
|
100
|
+
}
|
|
101
|
+
throw Object.assign(Error('设备授权已过期,请重新登录。'),{reason:'expired_token'});
|
|
102
|
+
}catch(e){if(epoch!==this.epoch)return;this.diagnostics('oauth_failed',{category:'authorization',reason:e.reason??'authorization_failed'});this.login={state:'error',message:e.message};}})();return this.login;
|
|
103
|
+
}
|
|
104
|
+
cancel(){++this.epoch;this.controller?.abort();this.refreshController?.abort();this.login={state:'idle'};}
|
|
105
|
+
mutate(fn){const task=(this.mutation??Promise.resolve()).catch(()=>{}).then(fn);this.mutation=task;return task;}
|
|
106
|
+
async logout(){this.cancel();await this.mutate(()=>this.credentialStore.remove());this.diagnostics('logout',{reason:'local_disconnect',hasCredential:false});}
|
|
107
|
+
dispose(){this.cancel();}
|
|
16
108
|
}
|
|
17
109
|
|
package/lib/http.js
CHANGED
|
@@ -6,7 +6,7 @@ import {Transform} from 'node:stream';
|
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
|
|
8
8
|
export const API='/dsh-github/api/';
|
|
9
|
-
const ROUTES={state:'GET',repos:'GET',contents:'GET',releases:'GET','auth/start':'POST','auth/configure':'POST','auth/logout':'POST',prepare:'POST',confirm:'POST',reject:'POST',upload:'POST','attachment/remove':'POST','ai/copy':'POST','ai/program':'POST','draft/get':'POST','draft/save':'POST','draft/clear':'POST'};
|
|
9
|
+
const ROUTES={state:'GET',repos:'GET',contents:'GET',releases:'GET','auth/start':'POST','auth/cancel':'POST','auth/configure':'POST','auth/logout':'POST',prepare:'POST',confirm:'POST',reject:'POST',upload:'POST','attachment/remove':'POST','ai/copy':'POST','ai/program':'POST','draft/get':'POST','draft/save':'POST','draft/clear':'POST'};
|
|
10
10
|
const json=(res,status,value)=>{res.writeHead(status,{'Content-Type':'application/json; charset=utf-8','Cache-Control':'no-store','X-Content-Type-Options':'nosniff'});res.end(JSON.stringify(value));};
|
|
11
11
|
async function body(req){let size=0;const parts=[];for await(const b of req){size+=b.length;if(size>3*1024*1024)throw Error('请求内容过大。');parts.push(b);}return JSON.parse(Buffer.concat(parts).toString('utf8')||'{}');}
|
|
12
12
|
export function admitted(req,connection){
|
|
@@ -35,6 +35,7 @@ export function routes({api,ops,auth,root,connection,releaseCopy,drafts}){
|
|
|
35
35
|
else if(name==='ai/program'){if(!releaseCopy)throw Error('AI介绍服务未就绪。');result=await releaseCopy.describeProgram(a);}
|
|
36
36
|
else if(name==='ai/copy'){if(!releaseCopy)throw Error('AI文案服务尚未就绪。');result=await releaseCopy.create(a);}
|
|
37
37
|
else if(name==='auth/start')result=await auth.start();
|
|
38
|
+
else if(name==='auth/cancel'){auth.cancel();result={ok:true};}
|
|
38
39
|
else if(name==='auth/configure'){await auth.configure(a.clientId);result={ok:true};}
|
|
39
40
|
else if(name==='auth/logout'){await auth.logout();ops.clearPending();result={ok:true};}
|
|
40
41
|
else if(name==='prepare'){result=await ops.prepare(a.kind,a.args);if(drafts&&a.kind==='create_repository')await drafts.clear({kind:'repository'});if(drafts&&a.kind==='create_release')await drafts.clear({kind:'release',owner:a.args.owner,repo:a.args.repo});}
|
package/lib/http.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import {Transform} from 'node:stream';
|
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
|
|
8
8
|
export const API='/dsh-github/api/';
|
|
9
|
-
const ROUTES={state:'GET',repos:'GET',contents:'GET',releases:'GET','auth/start':'POST','auth/configure':'POST','auth/logout':'POST',prepare:'POST',confirm:'POST',reject:'POST',upload:'POST','attachment/remove':'POST','ai/copy':'POST','ai/program':'POST','draft/get':'POST','draft/save':'POST','draft/clear':'POST'};
|
|
9
|
+
const ROUTES={state:'GET',repos:'GET',contents:'GET',releases:'GET','auth/start':'POST','auth/cancel':'POST','auth/configure':'POST','auth/logout':'POST',prepare:'POST',confirm:'POST',reject:'POST',upload:'POST','attachment/remove':'POST','ai/copy':'POST','ai/program':'POST','draft/get':'POST','draft/save':'POST','draft/clear':'POST'};
|
|
10
10
|
const json=(res,status,value)=>{res.writeHead(status,{'Content-Type':'application/json; charset=utf-8','Cache-Control':'no-store','X-Content-Type-Options':'nosniff'});res.end(JSON.stringify(value));};
|
|
11
11
|
async function body(req){let size=0;const parts=[];for await(const b of req){size+=b.length;if(size>3*1024*1024)throw Error('请求内容过大。');parts.push(b);}return JSON.parse(Buffer.concat(parts).toString('utf8')||'{}');}
|
|
12
12
|
export function admitted(req,connection){
|
|
@@ -35,6 +35,7 @@ export function routes({api,ops,auth,root,connection,releaseCopy,drafts}){
|
|
|
35
35
|
else if(name==='ai/program'){if(!releaseCopy)throw Error('AI介绍服务未就绪。');result=await releaseCopy.describeProgram(a);}
|
|
36
36
|
else if(name==='ai/copy'){if(!releaseCopy)throw Error('AI文案服务尚未就绪。');result=await releaseCopy.create(a);}
|
|
37
37
|
else if(name==='auth/start')result=await auth.start();
|
|
38
|
+
else if(name==='auth/cancel'){auth.cancel();result={ok:true};}
|
|
38
39
|
else if(name==='auth/configure'){await auth.configure(a.clientId);result={ok:true};}
|
|
39
40
|
else if(name==='auth/logout'){await auth.logout();ops.clearPending();result={ok:true};}
|
|
40
41
|
else if(name==='prepare'){result=await ops.prepare(a.kind,a.args);if(drafts&&a.kind==='create_repository')await drafts.clear({kind:'repository'});if(drafts&&a.kind==='create_release')await drafts.clear({kind:'release',owner:a.args.owner,repo:a.args.repo});}
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import os from 'node:os';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import {createTransport} from './network.mjs';
|
|
3
4
|
import {GhAuth} from './gh.mjs';
|
|
5
|
+
import {harnessCredentialStore} from './credential-store.mjs';
|
|
6
|
+
import {registerMobilePanel} from './dsh-go.mjs';
|
|
4
7
|
import {GitHub,Operations} from './core.mjs';
|
|
5
8
|
import {routes} from './http.mjs';
|
|
6
9
|
import {definitions,localAgent} from './tools.mjs';
|
|
@@ -13,12 +16,16 @@ export function apply(ctx){
|
|
|
13
16
|
ctx.inject(['webServer','tools','connection'],async host=>{
|
|
14
17
|
const root=path.join(process.env.DSH_HOME??path.join(os.homedir(),'.dsh'),'github-connect');
|
|
15
18
|
const diagnostics=createDiagnostics(root);diagnostics('plugin_started');
|
|
16
|
-
const
|
|
19
|
+
const transport=createTransport();
|
|
20
|
+
// Read the live service so Linux does not depend on plugin startup order.
|
|
21
|
+
const credentialStore=process.platform==='linux'?harnessCredentialStore(()=>host.get('credentials')):undefined;
|
|
22
|
+
const auth=new GhAuth(root,{diagnostics,fetchImpl:transport.fetch,credentialStore}),api=new GitHub({auth,diagnostics,fetchImpl:transport.fetch}),ops=new Operations(api,{root});await ops.init();
|
|
17
23
|
const releaseCopy=new ReleaseCopy({api,ctx:host,root,ops});
|
|
18
24
|
const drafts=new Drafts({root});
|
|
19
25
|
for(const route of routes({api,ops,auth,root,releaseCopy,drafts,connection:()=>host.get('connection')}))host.effect(()=>host.webServer.register(route));
|
|
20
26
|
for(const definition of definitions(api,ops))host.effect(()=>host.tools.register(definition,{when:({agent})=>localAgent(agent)}));
|
|
21
|
-
host.
|
|
27
|
+
host.inject?.(['dshGo'],mobile=>registerMobilePanel(mobile,{auth,api,ops}));
|
|
28
|
+
host.on('dispose',()=>{diagnostics('plugin_stopped');auth.dispose();transport.close().catch(()=>{});ops.clearPending();});
|
|
22
29
|
});
|
|
23
30
|
}
|
|
24
31
|
|
package/lib/network.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import tls from 'node:tls';
|
|
2
|
+
import {EnvHttpProxyAgent,fetch as undiciFetch} from 'undici';
|
|
3
|
+
|
|
4
|
+
const messages={
|
|
5
|
+
tls_error:'GitHub HTTPS 证书校验失败。请检查加速器证书是否已加入系统信任证书,或暂时关闭 GitHub 加速。',
|
|
6
|
+
dns_error:'无法解析 GitHub 地址,请检查 DNS 或加速器的域名接管。',
|
|
7
|
+
timeout:'连接 GitHub 超时,请检查网络或代理线路。',
|
|
8
|
+
connection_error:'GitHub 连接中断或代理端口不可用,请检查加速器和代理设置。',
|
|
9
|
+
network_error:'无法连接 GitHub,请检查网络与代理设置。'
|
|
10
|
+
};
|
|
11
|
+
export function networkFailure(error){
|
|
12
|
+
if(error?.reason&&messages[error.reason])return error;
|
|
13
|
+
const errors=[error,error?.cause,...(error?.cause?.errors??[])];
|
|
14
|
+
const codes=errors.map(e=>e?.code??e?.name??'');
|
|
15
|
+
const reason=codes.some(c=>/CERT|SELF_SIGNED|TLS|SSL/.test(c))?'tls_error':
|
|
16
|
+
codes.some(c=>/ENOTFOUND|EAI_AGAIN/.test(c))?'dns_error':
|
|
17
|
+
codes.some(c=>/TIMEOUT|TimeoutError|ETIMEDOUT/.test(c))?'timeout':
|
|
18
|
+
codes.some(c=>/ECONN|EHOST|ENET|UND_ERR_SOCKET/.test(c))?'connection_error':'network_error';
|
|
19
|
+
return Object.assign(Error(messages[reason]),{reason,retryable:reason!=='tls_error',cause:error});
|
|
20
|
+
}
|
|
21
|
+
export function createTransport({env=process.env,fetchImpl=undiciFetch}={}){
|
|
22
|
+
const ca=[...new Set([...(tls.getCACertificates?.('default')??tls.rootCertificates),...(tls.getCACertificates?.('system')??[])])];
|
|
23
|
+
const explicit=env.DSH_GITHUB_PROXY;
|
|
24
|
+
const httpProxy=explicit??env.http_proxy??env.HTTP_PROXY??'';
|
|
25
|
+
const httpsProxy=explicit??env.https_proxy??env.HTTPS_PROXY??httpProxy;
|
|
26
|
+
for(const address of [httpProxy,httpsProxy])if(address){let url;try{url=new URL(address);}catch{throw Error('GitHub 代理地址无效,请设置 HTTP 或 HTTPS 代理地址。');}if(!['http:','https:'].includes(url.protocol))throw Error('GitHub 代理目前仅支持 HTTP/HTTPS;请使用加速器的 HTTP 代理端口。');}
|
|
27
|
+
const dispatcher=new EnvHttpProxyAgent({httpProxy,httpsProxy,noProxy:env.no_proxy??env.NO_PROXY??'',connect:{ca,autoSelectFamily:true},requestTls:{ca},proxyTls:{ca}});
|
|
28
|
+
return {
|
|
29
|
+
async fetch(url,options){try{return await fetchImpl(url,{...options,dispatcher});}catch(e){if(options?.signal?.aborted&&options.signal.reason?.name==='AbortError')throw options.signal.reason;throw networkFailure(e);}},
|
|
30
|
+
close:()=>dispatcher.destroy()
|
|
31
|
+
};
|
|
32
|
+
}
|
package/lib/network.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import tls from 'node:tls';
|
|
2
|
+
import {EnvHttpProxyAgent,fetch as undiciFetch} from 'undici';
|
|
3
|
+
|
|
4
|
+
const messages={
|
|
5
|
+
tls_error:'GitHub HTTPS 证书校验失败。请检查加速器证书是否已加入系统信任证书,或暂时关闭 GitHub 加速。',
|
|
6
|
+
dns_error:'无法解析 GitHub 地址,请检查 DNS 或加速器的域名接管。',
|
|
7
|
+
timeout:'连接 GitHub 超时,请检查网络或代理线路。',
|
|
8
|
+
connection_error:'GitHub 连接中断或代理端口不可用,请检查加速器和代理设置。',
|
|
9
|
+
network_error:'无法连接 GitHub,请检查网络与代理设置。'
|
|
10
|
+
};
|
|
11
|
+
export function networkFailure(error){
|
|
12
|
+
if(error?.reason&&messages[error.reason])return error;
|
|
13
|
+
const errors=[error,error?.cause,...(error?.cause?.errors??[])];
|
|
14
|
+
const codes=errors.map(e=>e?.code??e?.name??'');
|
|
15
|
+
const reason=codes.some(c=>/CERT|SELF_SIGNED|TLS|SSL/.test(c))?'tls_error':
|
|
16
|
+
codes.some(c=>/ENOTFOUND|EAI_AGAIN/.test(c))?'dns_error':
|
|
17
|
+
codes.some(c=>/TIMEOUT|TimeoutError|ETIMEDOUT/.test(c))?'timeout':
|
|
18
|
+
codes.some(c=>/ECONN|EHOST|ENET|UND_ERR_SOCKET/.test(c))?'connection_error':'network_error';
|
|
19
|
+
return Object.assign(Error(messages[reason]),{reason,retryable:reason!=='tls_error',cause:error});
|
|
20
|
+
}
|
|
21
|
+
export function createTransport({env=process.env,fetchImpl=undiciFetch}={}){
|
|
22
|
+
const ca=[...new Set([...(tls.getCACertificates?.('default')??tls.rootCertificates),...(tls.getCACertificates?.('system')??[])])];
|
|
23
|
+
const explicit=env.DSH_GITHUB_PROXY;
|
|
24
|
+
const httpProxy=explicit??env.http_proxy??env.HTTP_PROXY??'';
|
|
25
|
+
const httpsProxy=explicit??env.https_proxy??env.HTTPS_PROXY??httpProxy;
|
|
26
|
+
for(const address of [httpProxy,httpsProxy])if(address){let url;try{url=new URL(address);}catch{throw Error('GitHub 代理地址无效,请设置 HTTP 或 HTTPS 代理地址。');}if(!['http:','https:'].includes(url.protocol))throw Error('GitHub 代理目前仅支持 HTTP/HTTPS;请使用加速器的 HTTP 代理端口。');}
|
|
27
|
+
const dispatcher=new EnvHttpProxyAgent({httpProxy,httpsProxy,noProxy:env.no_proxy??env.NO_PROXY??'',connect:{ca,autoSelectFamily:true},requestTls:{ca},proxyTls:{ca}});
|
|
28
|
+
return {
|
|
29
|
+
async fetch(url,options){try{return await fetchImpl(url,{...options,dispatcher});}catch(e){if(options?.signal?.aborted&&options.signal.reason?.name==='AbortError')throw options.signal.reason;throw networkFailure(e);}},
|
|
30
|
+
close:()=>dispatcher.destroy()
|
|
31
|
+
};
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-github-studio",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "DeepSeek Harness GitHub 工作台:账号连接、仓库工具、AI 项目介绍与 Release 发布",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -28,6 +28,24 @@
|
|
|
28
28
|
"platform": "web"
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
|
+
"dshGo": {
|
|
32
|
+
"v": 1,
|
|
33
|
+
"title": "GitHub Studio",
|
|
34
|
+
"icon": "code",
|
|
35
|
+
"contributes": {
|
|
36
|
+
"panels": [
|
|
37
|
+
{
|
|
38
|
+
"id": "github-account",
|
|
39
|
+
"title": "GitHub",
|
|
40
|
+
"icon": "code",
|
|
41
|
+
"placement": [
|
|
42
|
+
"plugin",
|
|
43
|
+
"features"
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
},
|
|
31
49
|
"author": "zhaohaix",
|
|
32
50
|
"homepage": "https://github.com/zhaohaix/DeepSeek-GitHub#readme",
|
|
33
51
|
"repository": {
|
|
@@ -67,6 +85,10 @@
|
|
|
67
85
|
"remark-gfm": "^4.0.1"
|
|
68
86
|
},
|
|
69
87
|
"dependencies": {
|
|
88
|
+
"undici": "^7.30.2",
|
|
70
89
|
"yauzl": "^3.4.0"
|
|
90
|
+
},
|
|
91
|
+
"engines": {
|
|
92
|
+
"node": ">=24"
|
|
71
93
|
}
|
|
72
94
|
}
|