Skip to content

Commit 2e3584d

Browse files
authored
Merge pull request #978 from wherelse/GitProxy
add http proxy for git
2 parents de67f7e + 851ed4c commit 2e3584d

File tree

11 files changed

+383
-247
lines changed

11 files changed

+383
-247
lines changed

.eslintrc.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@ module.exports = {
33
env: {
44
node: true,
55
},
6-
extends: [
7-
'plugin:vue/essential',
8-
'@vue/airbnb',
9-
'@vue/typescript',
10-
],
6+
extends: ['plugin:vue/essential', '@vue/airbnb', '@vue/typescript'],
117
rules: {
128
// js 和 ts 不需要检查 import 的文件后缀
13-
'import/extensions': ['error', 'always', {
14-
'js': 'never',
15-
'ts': 'never',
16-
}],
17-
'no-restricted-syntax': ['error', 'WithStatement', 'BinaryExpression[operator=\'in\']'],
9+
'import/extensions': [
10+
'error',
11+
'always',
12+
{
13+
js: 'never',
14+
ts: 'never',
15+
},
16+
],
17+
'no-restricted-syntax': [
18+
'error',
19+
'WithStatement',
20+
'BinaryExpression[operator=\'in\']',
21+
],
1822
// 可以 debugger
1923
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
2024
// 不要分号
21-
'semi': [2, 'never'],
25+
semi: [2, 'never'],
2226
// 全部单引号
23-
'quotes': [2, 'single'],
27+
quotes: [2, 'single'],
2428
// 对象缩写
2529
'object-shorthand': 0,
2630
// 可以使用 console
@@ -38,14 +42,20 @@ module.exports = {
3842
// 不禁止箭头函数直接return对象
3943
'arrow-body-style': 0,
4044
// 允许空行
41-
'no-trailing-spaces': ['error', { 'skipBlankLines': true }],
45+
'no-trailing-spaces': ['error', { skipBlankLines: true }],
4246
// 允许short circuit evaluations
43-
'no-unused-expressions': ['error', { 'allowShortCircuit': true, 'allowTernary': true }],
47+
'no-unused-expressions': [
48+
'error',
49+
{ allowShortCircuit: true, allowTernary: true },
50+
],
4451
// 最长字符
45-
'max-len': ['error', { 'code': 1500 }],
46-
'vue/no-parsing-error': [2, {
47-
'invalid-first-character-of-tag-name': false,
48-
}],
52+
'max-len': ['error', { code: 1500 }],
53+
'vue/no-parsing-error': [
54+
2,
55+
{
56+
'invalid-first-character-of-tag-name': false,
57+
},
58+
],
4959
// no-plusplus
5060
'no-plusplus': 0,
5161
'class-methods-use-this': 0,

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@
3333
"feed": "^2.0.4",
3434
"fs-extra": "^7.0.1",
3535
"gray-matter": "^4.0.1",
36-
"isomorphic-git": "^0.78.3",
36+
"hpagent": "^1.0.0",
37+
"isomorphic-git": "^1.17.1",
3738
"junk": "^3.1.0",
3839
"less": "^3.9.0",
3940
"lowdb": "^1.0.0",
4041
"macaddress": "^0.2.9",
41-
"markdown-it": "^8.4.2",
42+
"markdown-it": "^13.0.1",
4243
"markdown-it-abbr": "^1.0.4",
43-
"markdown-it-emoji": "^1.4.0",
44-
"markdown-it-footnote": "^3.0.1",
45-
"markdown-it-image-lazy-loading": "^1.0.2",
44+
"markdown-it-emoji": "^2.0.2",
45+
"markdown-it-footnote": "^3.0.3",
46+
"markdown-it-image-lazy-loading": "^1.2.0",
4647
"markdown-it-implicit-figures": "^0.9.0",
4748
"markdown-it-imsize": "^2.0.1",
4849
"markdown-it-mark": "^2.0.0",
@@ -55,6 +56,7 @@
5556
"normalize-path": "^3.0.0",
5657
"prismjs": "^1.16.0",
5758
"shortid": "^2.2.14",
59+
"simple-get": "^4.0.1",
5860
"slug": "^0.9.3",
5961
"ssh2-sftp-client": "^4.2.4",
6062
"striptags": "^3.1.1",
@@ -98,7 +100,7 @@
98100
"@vue/eslint-config-typescript": "^4.0.0",
99101
"babel-eslint": "^10.0.1",
100102
"babel-plugin-prismjs": "^1.0.2",
101-
"electron": "^7.2.4",
103+
"electron": "^7.3.3",
102104
"eslint": "^5.16.0",
103105
"eslint-plugin-vue": "^5.2.2",
104106
"husky": "^1.3.1",

src/assets/locales.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ const message = {
123123
showFullText: '显示全文',
124124
showAbstractOnly: '仅显示摘要',
125125
numberArticlesRSS: 'RSS/Feed 文章数量',
126+
Proxy: 'HTTP代理',
127+
ProxyAddress: '地址',
128+
ProxyPort: '端口',
126129
},
127130
zh_TW: {
128131
preview: '預 覽',
@@ -249,6 +252,9 @@ const message = {
249252
showFullText: '显示全文',
250253
showAbstractOnly: '仅显示摘要',
251254
numberArticlesRSS: 'RSS/Feed 文章数量',
255+
Proxy: 'HTTP代理',
256+
ProxyAddress: '地址',
257+
ProxyPort: '端口',
252258
},
253259
en: {
254260
preview: 'Preview',
@@ -374,6 +380,9 @@ const message = {
374380
showFullText: 'Show full text',
375381
showAbstractOnly: 'Show abstract only',
376382
numberArticlesRSS: 'Number articles RSS/Feed',
383+
Proxy: 'HTTP Proxy',
384+
ProxyAddress: 'Proxy Address',
385+
ProxyPort: 'Proxy Port',
377386
},
378387
fr_FR: {
379388
preview: 'Aperçu',
@@ -499,6 +508,9 @@ const message = {
499508
showFullText: 'Tout afficher',
500509
showAbstractOnly: 'Afficher seulement le résumé',
501510
numberArticlesRSS: 'Nombre d\'articles RSS/Feed',
511+
Proxy: 'HTTP Proxy',
512+
ProxyAddress: 'Proxy Address',
513+
ProxyPort: 'Proxy Port',
502514
},
503515
ja_JP: {
504516
preview: 'プレビュー',
@@ -624,6 +636,9 @@ const message = {
624636
showFullText: '全文を表示します',
625637
showAbstractOnly: '摘要のみを表示します',
626638
numberArticlesRSS: 'RSS/Feed 文書数量',
639+
Proxy: 'HTTPプロキシ',
640+
ProxyAddress: 'プロキシアドレス',
641+
ProxyPort: 'プロキシポート',
627642
},
628643
}
629644

src/interfaces/setting.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export interface ISetting {
1313
password: string
1414
privateKey: string
1515
remotePath: string
16+
proxyPath: string
17+
proxyPort: string
18+
enabledProxy: 'direct' | 'proxy'
1619
[index: string]: string
1720
}
1821

src/server/app.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export default class App {
7878
password: '',
7979
privateKey: '',
8080
remotePath: '',
81+
proxyPath: '',
82+
proxyPort: '',
83+
enabledProxy: 'direct',
8184
},
8285
commentSetting: {
8386
showComment: false,

src/server/deploy.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import fs from 'fs'
22
import moment from 'moment'
33
// @ts-ignore
4-
import * as git from 'isomorphic-git/dist/for-node/isomorphic-git'
54
import Model from './model'
5+
import GitProxy from './plugins/deploys/gitproxy'
6+
7+
const git = require('isomorphic-git')
68

79
export default class Deploy extends Model {
810
outputDir: string = `${this.appDir}/output`
@@ -11,9 +13,10 @@ export default class Deploy extends Model {
1113

1214
platformAddress = ''
1315

16+
http = new GitProxy(this)
17+
1418
constructor(appInstance: any) {
1519
super(appInstance)
16-
1720
const { setting } = this.db
1821
this.platformAddress = ({
1922
github: 'github.com',
@@ -33,7 +36,7 @@ export default class Deploy extends Model {
3336
async remoteDetect() {
3437
const result = {
3538
success: true,
36-
message: '',
39+
message: [''],
3740
}
3841
try {
3942
const { setting } = this.db
@@ -53,13 +56,13 @@ export default class Deploy extends Model {
5356
}
5457
if (!isRepo) {
5558
await git.init({ fs, dir: this.outputDir })
56-
await git.config({
59+
await git.setConfig({
5760
fs,
5861
dir: this.outputDir,
5962
path: 'user.name',
6063
value: setting.username,
6164
})
62-
await git.config({
65+
await git.setConfig({
6366
fs,
6467
dir: this.outputDir,
6568
path: 'user.email',
@@ -71,13 +74,13 @@ export default class Deploy extends Model {
7174
fs, dir: this.outputDir, remote: 'origin', url: this.remoteUrl, force: true,
7275
})
7376
const info = await git.getRemoteInfo({
74-
core: 'default',
77+
http: this.http,
7578
url: this.remoteUrl,
7679
})
7780
console.log('info', info)
78-
result.message = info
81+
result.message = info.capabilities
7982
} catch (e) {
80-
console.log('Test Remote Error: ', e.message)
83+
console.log('Test Remote Error: ', e)
8184
result.success = false
8285
result.message = e.message
8386
}
@@ -113,13 +116,13 @@ export default class Deploy extends Model {
113116

114117
try {
115118
await git.init({ fs, dir: this.outputDir })
116-
await git.config({
119+
await git.setConfig({
117120
fs,
118121
dir: this.outputDir,
119122
path: 'user.name',
120123
value: setting.username,
121124
})
122-
await git.config({
125+
await git.setConfig({
123126
fs,
124127
dir: this.outputDir,
125128
path: 'user.email',
@@ -138,6 +141,7 @@ export default class Deploy extends Model {
138141
await this.checkCurrentBranch()
139142
const pushRes = await git.push({
140143
fs,
144+
http: this.http,
141145
dir: this.outputDir,
142146
remote: 'origin',
143147
ref: setting.branch,
@@ -184,6 +188,7 @@ export default class Deploy extends Model {
184188

185189
const pushRes = await git.push({
186190
fs,
191+
http: this.http,
187192
dir: this.outputDir,
188193
remote: 'origin',
189194
ref: setting.branch,
@@ -221,7 +226,7 @@ export default class Deploy extends Model {
221226
await git.branch({ fs, dir: this.outputDir, ref: setting.branch })
222227
}
223228

224-
await git.fastCheckout({ fs, dir: this.outputDir, ref: setting.branch })
229+
await git.checkout({ fs, dir: this.outputDir, ref: setting.branch })
225230
}
226231
}
227232
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { IApplicationDb } from '../../interfaces/application'
2+
3+
const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent')
4+
const get = require('simple-get')
5+
6+
export default class GitProxy {
7+
db: IApplicationDb
8+
9+
constructor(appInstance: any) {
10+
this.db = appInstance.db
11+
// console.log('instance git proxy',this.db.setting)
12+
}
13+
14+
public async request({
15+
url,
16+
method,
17+
headers,
18+
body,
19+
}: {
20+
url: any;
21+
method: any;
22+
headers: any;
23+
body: any;
24+
}) {
25+
const { setting } = this.db
26+
body = await this.mergeBuffers(body)
27+
const proxy = url.startsWith('https:')
28+
? { Agent: HttpsProxyAgent }
29+
: { Agent: HttpProxyAgent }
30+
const agent = setting.enabledProxy === 'proxy'
31+
? new proxy.Agent({
32+
proxy: `http://${setting.proxyPath}:${setting.proxyPort}`,
33+
})
34+
: undefined
35+
// const agent = new proxy.Agent({ proxy: 'http://127.0.0.1:1081' })
36+
return new Promise((resolve, reject) => get(
37+
{
38+
url,
39+
method,
40+
agent,
41+
headers,
42+
body,
43+
},
44+
(err: any, res: any) => (err ? reject(err) : resolve(this.transformResponse(res))),
45+
))
46+
}
47+
48+
private async mergeBuffers(data: any[] | Uint8Array) {
49+
if (!Array.isArray(data)) return data
50+
if (data.length === 1 && data[0] instanceof Buffer) return data[0]
51+
const buffers = []
52+
let offset = 0
53+
let size = 0
54+
for await (const chunk of data) {
55+
buffers.push(chunk)
56+
size += chunk.byteLength
57+
}
58+
data = new Uint8Array(size)
59+
for (const buffer of buffers) {
60+
data.set(buffer, offset)
61+
offset += buffer.byteLength
62+
}
63+
return Buffer.from(data.buffer)
64+
}
65+
66+
private async transformResponse(res: {
67+
url: any;
68+
method: any;
69+
statusCode: any;
70+
statusMessage: any;
71+
headers: any;
72+
}) {
73+
const {
74+
url, method, statusCode, statusMessage, headers,
75+
} = res
76+
return {
77+
url,
78+
method,
79+
statusCode,
80+
statusMessage,
81+
headers,
82+
body: res,
83+
}
84+
}
85+
}

src/server/plugins/deploys/sftp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export default class SftpDeploy extends Model {
119119
await client.mkdir(remotePath)
120120
const res = await client.putDirectory(localPath, remotePath, {
121121
recursive: true,
122-
concurrency: 10,
122+
concurrency: 1, // 解决同步丢失js、css、图片文件问题
123123
validate: function (itemPath: string) {
124124
const baseName = path.basename(itemPath)
125125
return baseName.substr(0, 1) !== '.' // do not allow dot files

src/store/modules/site.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ const siteState: Site = {
6262
password: '',
6363
privateKey: '',
6464
remotePath: '',
65+
proxyPath: '',
66+
proxyPort: '',
67+
enabledProxy: 'direct',
6568
},
6669
commentSetting: {
6770
showComment: false,

0 commit comments

Comments
 (0)