-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathparseControlResponse.ts
More file actions
70 lines (63 loc) · 2.23 KB
/
parseControlResponse.ts
File metadata and controls
70 lines (63 loc) · 2.23 KB
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
const LF = "\n"
export interface ParsedResponse {
readonly messages: string[]
readonly rest: string
}
/**
* Parse an FTP control response as a collection of messages. A message is a complete
* single- or multiline response. A response can also contain multiple multiline responses
* that will each be represented by a message. A response can also be incomplete
* and be completed on the next incoming data chunk for which case this function also
* describes a `rest`. This function converts all CRLF to LF.
*/
export function parseControlResponse(text: string): ParsedResponse {
const lines = text.split(/\r?\n/).filter(isNotBlank)
const messages = []
let startAt = 0
let tokenRegex: RegExp | undefined
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
// No group has been opened.
if (!tokenRegex) {
if (isMultiline(line)) {
// Open a group by setting an expected token.
const token = line.substr(0, 3)
tokenRegex = new RegExp(`^${token}(?:$| )`)
startAt = i
}
else if (isSingleLine(line)) {
// Single lines can be grouped immediately.
messages.push(line)
}
}
// Group has been opened, expect closing token.
else if (tokenRegex.test(line)) {
tokenRegex = undefined
messages.push(lines.slice(startAt, i + 1).join(LF))
}
}
// The last group might not have been closed, report it as a rest.
const rest = tokenRegex ? lines.slice(startAt).join(LF) + LF : ""
return { messages, rest }
}
export function isSingleLine(line: string) {
return /^\d\d\d(?:$| )/.test(line)
}
export function isMultiline(line: string) {
return /^\d\d\d-/.test(line)
}
/**
* Return true if an FTP return code describes a positive completion.
*/
export function positiveCompletion(code: number): boolean {
return code >= 200 && code < 300
}
/**
* Return true if an FTP return code describes a positive intermediate response.
*/
export function positiveIntermediate(code: number): boolean {
return code >= 300 && code < 400
}
function isNotBlank(str: string): boolean {
return str.trim() !== ""
}