1 | const { readFile } = require('fs')
|
2 | const { promisify } = require('util')
|
3 |
|
4 | const pathExists = require('path-exists')
|
5 |
|
6 | const { splitResults } = require('./results')
|
7 | const { isUrl } = require('./url')
|
8 |
|
9 | const pReadFile = promisify(readFile)
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const parseFileRedirects = async function (redirectFile) {
|
30 | if (!(await pathExists(redirectFile))) {
|
31 | return splitResults([])
|
32 | }
|
33 |
|
34 | const text = await pReadFile(redirectFile, 'utf8')
|
35 | const results = text.split('\n').map(normalizeLine).filter(hasRedirect).map(parseRedirect)
|
36 | return splitResults(results)
|
37 | }
|
38 |
|
39 | const normalizeLine = function (line, index) {
|
40 | return { line: line.trim(), index }
|
41 | }
|
42 |
|
43 | const hasRedirect = function ({ line }) {
|
44 | return line !== '' && !isComment(line)
|
45 | }
|
46 |
|
47 | const parseRedirect = function ({ line, index }) {
|
48 | try {
|
49 | return parseRedirectLine(line)
|
50 | } catch (error) {
|
51 | return new Error(`Could not parse redirect line ${index + 1}:
|
52 | ${line}
|
53 | ${error.message}`)
|
54 | }
|
55 | }
|
56 |
|
57 |
|
58 | const parseRedirectLine = function (line) {
|
59 | const [from, ...parts] = trimComment(line.split(LINE_TOKENS_REGEXP))
|
60 |
|
61 | if (parts.length === 0) {
|
62 | throw new Error('Missing destination path/URL')
|
63 | }
|
64 |
|
65 | const {
|
66 | queryParts,
|
67 | to,
|
68 | lastParts: [statusPart, ...conditionsParts],
|
69 | } = parseParts(from, parts)
|
70 |
|
71 | const query = parsePairs(queryParts)
|
72 | const { status, force } = parseStatus(statusPart)
|
73 | const { Sign, signed = Sign, ...conditions } = parsePairs(conditionsParts)
|
74 | return { from, query, to, status, force, conditions, signed }
|
75 | }
|
76 |
|
77 |
|
78 | const trimComment = function (parts) {
|
79 | const commentIndex = parts.findIndex(isComment)
|
80 | return commentIndex === -1 ? parts : parts.slice(0, commentIndex)
|
81 | }
|
82 |
|
83 | const isComment = function (part) {
|
84 | return part.startsWith('#')
|
85 | }
|
86 |
|
87 | const LINE_TOKENS_REGEXP = /\s+/g
|
88 |
|
89 |
|
90 |
|
91 | const parseParts = function (from, parts) {
|
92 |
|
93 |
|
94 |
|
95 | if (isStatusCode(parts[0])) {
|
96 | return { queryParts: [], to: undefined, lastParts: parts }
|
97 | }
|
98 |
|
99 | const toIndex = parts.findIndex(isToPart)
|
100 | if (toIndex === -1) {
|
101 | throw new Error('The destination path/URL must start with "/", "http:" or "https:"')
|
102 | }
|
103 |
|
104 | const queryParts = parts.slice(0, toIndex)
|
105 | const to = parts[toIndex]
|
106 | const lastParts = parts.slice(toIndex + 1)
|
107 | return { queryParts, to, lastParts }
|
108 | }
|
109 |
|
110 | const isToPart = function (part) {
|
111 | return part.startsWith('/') || isUrl(part)
|
112 | }
|
113 |
|
114 | const isStatusCode = function (part) {
|
115 | return Number.isInteger(getStatusCode(part))
|
116 | }
|
117 |
|
118 |
|
119 | const parseStatus = function (statusPart) {
|
120 | if (statusPart === undefined) {
|
121 | return {}
|
122 | }
|
123 |
|
124 | const status = getStatusCode(statusPart)
|
125 | const force = statusPart.endsWith('!')
|
126 | return { status, force }
|
127 | }
|
128 |
|
129 | const getStatusCode = function (statusPart) {
|
130 | return Number.parseInt(statusPart)
|
131 | }
|
132 |
|
133 |
|
134 | const parsePairs = function (conditions) {
|
135 | return Object.assign({}, ...conditions.map(parsePair))
|
136 | }
|
137 |
|
138 | const parsePair = function (condition) {
|
139 | const [key, value] = condition.split('=')
|
140 | return { [key]: value }
|
141 | }
|
142 |
|
143 | module.exports = { parseFileRedirects }
|