Skip to content

Commit

Permalink
feat: support socks proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
zce committed Apr 19, 2022
1 parent 172da95 commit 30ff1b9
Showing 7 changed files with 83 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -280,6 +280,22 @@ registry = https://zce.coding.net/p/{owner}/d/{name}/git/archive/{branch}
official = caz
```

#### Socks Proxy

CAZ supports socks proxy config.

`~/.cazrc`:

```ini
proxy = socks5://127.0.0.1:1080
```

or environment variable:

```shell
$ ALL_PROXY=socks5://127.0.0.1:1080 caz nm my-project
```

### Create Your Template

```shell
16 changes: 16 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -280,6 +280,22 @@ registry = https://zce.coding.net/p/{owner}/d/{name}/git/archive/{branch}
official = caz
```

#### 请求代理

CAZ 支持网络请求代理配置。

`~/.cazrc`:

```ini
proxy = socks5://127.0.0.1:1080
```

或者在使用环境变量:

```shell
$ ALL_PROXY=socks5://127.0.0.1:1080 caz nm my-project
```

### 创建你的模板

```shell
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -104,6 +104,7 @@
"ora": "6.1.0",
"prompts": "2.4.2",
"semver": "7.3.7",
"socks-proxy-agent": "6.2.0",
"ts-jest": "27.1.4",
"ts-standard": "11.0.0",
"tsup": "5.12.5",
28 changes: 28 additions & 0 deletions src/core/config.spec.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ test('unit:core:config', async () => {
expect(config.registry).toBe('https://github.com/{owner}/{name}/archive/refs/heads/{branch}.zip')
expect(config.official).toBe('caz-templates')
expect(config.branch).toBe('master')
expect(config.proxy).toBe(undefined)
expect(config.commitMessage).toBe('feat: initial commit')
})

@@ -20,10 +21,37 @@ test('unit:core:config:custom', async () => {
expect(conf.registry).toBe('https://gitlab.com/{owner}/{name}/archive/refs/heads/{branch}.zip')
expect(conf.official).toBe('faker')
expect(conf.branch).toBe('dev')
expect(conf.proxy).toBe('socks://127.0.0.1:1080')
expect(conf.commitMessage).toBe('feat: initial commit')
homedir.mockRestore()
})

test('unit:core:config:env', async () => {
jest.resetModules()
process.env.ALL_PROXY = 'socks://127.0.0.1:11111'
expect((await import('./config')).default.proxy).toBe('socks://127.0.0.1:11111')

jest.resetModules()
process.env.HTTPS_PROXY = 'socks://127.0.0.1:22222'
expect((await import('./config')).default.proxy).toBe('socks://127.0.0.1:22222')

jest.resetModules()
process.env.https_proxy = 'socks://127.0.0.1:22222'
expect((await import('./config')).default.proxy).toBe('socks://127.0.0.1:22222')

jest.resetModules()
process.env.HTTP_PROXY = 'socks://127.0.0.1:22222'
expect((await import('./config')).default.proxy).toBe('socks://127.0.0.1:22222')

jest.resetModules()
process.env.http_proxy = 'socks://127.0.0.1:22222'
expect((await import('./config')).default.proxy).toBe('socks://127.0.0.1:22222')

jest.resetModules()
process.env.NO_PROXY = '1'
expect((await import('./config')).default.proxy).toBe(undefined)
})

test('unit:core:config:npm', async () => {
const homedir = mockHomedir()
expect(config.npm?.['init-author-name']).toBe('zce')
16 changes: 15 additions & 1 deletion src/core/config.ts
Original file line number Diff line number Diff line change
@@ -16,14 +16,28 @@ const parseIni = (filename: string): Record<string, any> | undefined => {
}

const defaults = {
// template download registry
// {owner} & {name} & {branch} will eventually be replaced by the corresponding value
registry: 'https://github.com/{owner}/{name}/archive/refs/heads/{branch}.zip',
// template offlicial owner name
official: 'caz-templates',
// default template branch name
branch: 'master',
// download socks proxy config
proxy: undefined as string | undefined,
// git init commit message
commitMessage: 'feat: initial commit'
}

const config = parseIni(path.join(os.homedir(), `.${name}rc`))
const config = parseIni(path.join(os.homedir(), `.${name}rc`)) ?? {}

// env proxy config
const envProxy = process.env.http_proxy ?? process.env.HTTP_PROXY ?? process.env.https_proxy ?? process.env.HTTPS_PROXY ?? process.env.ALL_PROXY
config.proxy = envProxy ?? config.proxy

if (process.env.no_proxy != null || process.env.NO_PROXY != null) {
delete config.proxy // disable proxy
}

export default {
...defaults,
7 changes: 6 additions & 1 deletion src/core/http.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { pipeline } from 'stream'
import { promisify } from 'util'
import { promises as fs, createWriteStream } from 'fs'
import fetch, { RequestInfo, RequestInit, Response } from 'node-fetch'
import { SocksProxyAgent } from 'socks-proxy-agent'
import config from './config'

const pipe = promisify(pipeline)
@@ -12,7 +13,11 @@ const pipe = promisify(pipeline)
* @param url url
* @param init init
*/
export const request = async (url: RequestInfo, init?: RequestInit): Promise<Response> => {
export const request = async (url: RequestInfo, init: RequestInit = {}): Promise<Response> => {
/* istanbul ignore if */
if (config.proxy != null) {
init.agent = new SocksProxyAgent(config.proxy)
}
const response = await fetch(url, init)
// res.status >= 200 && res.status < 300
if (response.ok) return response
1 change: 1 addition & 0 deletions test/fixtures/.cazrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
registry = https://gitlab.com/{owner}/{name}/archive/refs/heads/{branch}.zip
official = faker
branch = dev
proxy = socks://127.0.0.1:1080

0 comments on commit 30ff1b9

Please sign in to comment.