-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
158 lines (148 loc) · 4.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const LITERAL_NAMES = {
CURL: 'curl',
INCLUDE_HEADERS: 'include-headers',
SILENT: 'run-silent',
USE_GET: 'use-get',
COMPRESSED: 'compressed',
STATES: {
HEADER: 'header-state',
CUSTOM_METHOD: 'custom-method-state',
DATA: 'data-state',
USER: 'user-state',
},
STRING: 'string',
OTHERS: 'others',
};
const literals = [
{
regex: /^\bcurl\b/,
name: LITERAL_NAMES.CURL
},
{
regex: /^(-H|--header)/,
name: LITERAL_NAMES.STATES.HEADER
},
{
regex: /^-X/,
name: LITERAL_NAMES.STATES.CUSTOM_METHOD
},
{
regex: /^(-u|--user)/,
name: LITERAL_NAMES.STATES.USER
},
{
regex: /^(-d|--data)/,
name: LITERAL_NAMES.STATES.DATA
},
{
regex: /^--compressed/,
name: LITERAL_NAMES.COMPRESSED
},
{
regex: /^(-i|--include)/,
name: LITERAL_NAMES.INCLUDE_HEADERS
},
{
regex: /^(-s|--silent)/,
name: LITERAL_NAMES.SILENT
},
{
regex: /^(-G|--get)/,
name: LITERAL_NAMES.USE_GET
},
{
regex: /^'[^']*'/,
name: LITERAL_NAMES.STRING
},
{
regex: /^"[^"]*"/,
name: LITERAL_NAMES.STRING
},
{
regex: /^[^\n\r\s]*/,
name: LITERAL_NAMES.STRING
},
{
regex: /^.*/,
name: LITERAL_NAMES.OTHERS
},
];
const trimValue = val => val.trim().replace(/^('|")\s*/, '').replace(/\s*('|")$/, '');
const isURL = val => /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/.test(val);
const parseCurl = curlCmd => {
let cmd = curlCmd.trim();
const tokens = [];
const result = {
url: '',
method: 'GET',
headers: {},
auth: {}
};
while (cmd) {
literals.some(literal => {
if (literal.regex.test(cmd)) {
tokens.push({
name: literal.name,
value: cmd.match(literal.regex)
});
cmd = cmd.replace(literal.regex, '').trim();
return true;
}
});
}
// console.log(JSON.stringify(tokens, null, 2));
if (tokens.length) {
if (tokens.shift().name !== 'curl') {
throw new Error('Not a curl command.');
}
let state = '';
tokens.forEach(token => {
if (!state) {
switch(token.name) {
case LITERAL_NAMES.STATES.HEADER:
state = LITERAL_NAMES.STATES.HEADER;
break;
case LITERAL_NAMES.STATES.DATA:
state = LITERAL_NAMES.STATES.DATA;
break;
case LITERAL_NAMES.STATES.CUSTOM_METHOD:
state = LITERAL_NAMES.STATES.CUSTOM_METHOD;
break;
case LITERAL_NAMES.STATES.USER:
state = LITERAL_NAMES.STATES.USER;
break;
case LITERAL_NAMES.STRING: {
const val = trimValue(token.value[0]);
if (isURL(val)) {
result.url = val;
}
break;
}
}
} else {
switch(state) {
case LITERAL_NAMES.STATES.HEADER: {
const pair = trimValue(token.value[0]).split(/\s*:\s*/);
result.headers[pair[0]] = pair[1];
break;
}
case LITERAL_NAMES.STATES.DATA: {
const pair = token.value[0];
result.data = trimValue(token.value[0]).trim();
break;
}
case LITERAL_NAMES.STATES.CUSTOM_METHOD: {
const pair = token.value[0];
result.method = trimValue(token.value[0]).trim();
break;
}
}
state = '';
}
});
}
return result;
};
module.exports = {
parseCurl
};