Skip to content

Commit 77392b8

Browse files
fix(contentful-export): Fix proxy issue
1 parent c76b4e1 commit 77392b8

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

lib/parseOptions.js

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { resolve } from 'path'
22
import { version } from '../package'
3+
import { proxyStringToObject } from './utils/proxy'
4+
35
export default function parseOptions (params) {
46
const defaultOptions = {
57
exportDir: process.cwd(),
@@ -47,27 +49,7 @@ export default function parseOptions (params) {
4749
}
4850

4951
if (typeof options.proxy === 'string') {
50-
const chunks = options.proxy.split('@')
51-
if (chunks.length > 1) {
52-
// Advanced proxy config with auth credentials
53-
const auth = chunks[0].split(':')
54-
const host = chunks[1].split(':')
55-
options.proxy = {
56-
host: host[0],
57-
port: parseInt(host[1]),
58-
auth: {
59-
username: auth[0],
60-
password: auth[1]
61-
}
62-
}
63-
} else {
64-
// Simple proxy config without auth credentials
65-
const host = chunks[0].split(':')
66-
options.proxy = {
67-
host: host[0],
68-
port: parseInt(host[1])
69-
}
70-
}
52+
options.proxy = proxyStringToObject(options.proxy)
7153
}
7254

7355
options.managementApplication = options.managementApplication || `contentful.export/${version}`

lib/usageParams.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default yargs
5858
default: 'api.contentful.com'
5959
})
6060
.option('proxy', {
61-
describe: 'Proxy configuration in HTTP auth format: host:port or user:password@host:port',
61+
describe: 'Proxy configuration in HTTP auth format: [http|https]://host:port or [http|https]://user:password@host:port',
6262
type: 'string'
6363
})
6464
.option('error-log-file', {

lib/utils/proxy.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { parse, format } from 'url'
2+
import { toInteger } from 'lodash'
3+
4+
function serializeAuth ({ username, password } = {}) {
5+
if (!username) {
6+
return ''
7+
}
8+
9+
if (!password) {
10+
return username
11+
}
12+
13+
return `${username}:${password}`
14+
}
15+
16+
function parseAuth (authString) {
17+
// authString may be a falsy value like `null`
18+
const [username, password] = (authString || '').split(':')
19+
return { username, password }
20+
}
21+
22+
export function proxyStringToObject (proxyString) {
23+
if (!proxyString.startsWith('http')) {
24+
return proxyStringToObject(`http://${proxyString}`)
25+
}
26+
27+
const {
28+
hostname: host,
29+
port: portString,
30+
auth: authString,
31+
protocol
32+
} = parse(proxyString)
33+
34+
const auth = parseAuth(authString)
35+
const port = toInteger(portString)
36+
const isHttps = protocol === 'https:'
37+
38+
if (!auth.username) {
39+
return { host, port, isHttps }
40+
}
41+
42+
return {
43+
host,
44+
port,
45+
auth,
46+
isHttps
47+
}
48+
}
49+
50+
export function proxyObjectToString (proxyObject) {
51+
const { host: hostname, port, auth: authObject } = proxyObject
52+
const auth = serializeAuth(authObject)
53+
54+
const formatted = format({ hostname, port, auth })
55+
56+
// Ugly fix for Node 6 vs Node 8 behavior
57+
return formatted.replace(/^\/\//, '')
58+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"dependencies": {
5252
"bluebird": "^3.3.3",
5353
"cli-table2": "^0.2.0",
54-
"contentful-batch-libs": "^5.7.0",
54+
"contentful-batch-libs": "^5.9.2",
5555
"fs-extra": "^2.1.2",
5656
"json-stringify-safe": "^5.0.1",
5757
"listr": "^0.11.0",

0 commit comments

Comments
 (0)