Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit 5ff098b

Browse files
authored
Merge pull request #7 from netlify/raees/yaml-parser
Add support for YAML config
2 parents 422f887 + 1b07989 commit 5ff098b

File tree

7 files changed

+372
-70
lines changed

7 files changed

+372
-70
lines changed

common.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,76 @@ function parseURL(url) {
99
return URLclass.parse(url)
1010
}
1111

12-
module.exports = {
12+
function splatForwardRule(path, obj, dest) {
13+
return (
14+
path.match(/\/\*$/) &&
15+
dest == null &&
16+
obj.status &&
17+
obj.status >= 200 &&
18+
obj.status < 300 &&
19+
obj.force
20+
)
21+
}
22+
23+
function isPlainObj(o) {
24+
return typeof o == 'object' && o.constructor == Object
25+
}
26+
27+
function fetch(obj, options) {
28+
for (const i in options) {
29+
if (obj.hasOwnProperty(options[i])) {
30+
return obj[options[i]]
31+
}
32+
}
33+
return null
34+
}
35+
36+
function redirectMatch(obj) {
37+
const origin = fetch(obj, ['from', 'origin'])
38+
const redirect =
39+
origin && origin.match(exp.FULL_URL_MATCHER)
40+
? exp.parseFullOrigin(origin)
41+
: { path: origin }
42+
if (redirect == null || (redirect.path == null && redirect.host == null)) {
43+
return null
44+
}
45+
46+
const dest = fetch(obj, ['to', 'destination'])
47+
if (splatForwardRule(redirect.path, obj, dest)) {
48+
redirect.to = redirect.path.replace(/\/\*$/, '/:splat')
49+
} else {
50+
redirect.to = dest
51+
}
52+
53+
if (redirect.to == null) {
54+
return null
55+
}
56+
57+
redirect.params = fetch(obj, ['query', 'params', 'parameters'])
58+
redirect.status = fetch(obj, ['status'])
59+
redirect.force = fetch(obj, ['force'])
60+
redirect.conditions = fetch(obj, ['conditions'])
61+
redirect.headers = fetch(obj, ['headers'])
62+
redirect.signed = fetch(obj, ['sign', 'signing', 'signed'])
63+
64+
Object.keys(redirect).forEach(key => {
65+
if (redirect[key] === null) {
66+
delete redirect[key]
67+
}
68+
})
69+
70+
if (redirect.headers && !isPlainObj(redirect.headers)) {
71+
return null
72+
}
73+
74+
return redirect
75+
}
76+
77+
const exp = {
78+
splatForwardRule,
79+
isPlainObj,
80+
redirectMatch,
81+
1382
FULL_URL_MATCHER: /^(https?):\/\/(.+)$/,
1483
FORWARD_STATUS_MATCHER: /^2\d\d!?$/,
1584

@@ -37,3 +106,5 @@ module.exports = {
37106
}
38107
},
39108
}
109+
110+
module.exports = exp

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const lineParser = require('./line-parser')
22
const tomlParser = require('./toml-parser')
3+
const yamlParser = require('./yaml-parser')
34

45
exports.parseRedirectsFormat = lineParser.parse
56
exports.parseTomlFormat = tomlParser.parse
7+
exports.parseYamlFormat = yamlParser.parse

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"@iarna/toml": "^2.2.3",
1818
"husky": "^2.2.0",
1919
"lint-staged": "^8.1.6",
20-
"prettier": "^1.17.0"
20+
"prettier": "^1.17.0",
21+
"yamljs": "^0.3.0"
2122
},
2223
"devDependencies": {
2324
"auto-changelog": "^1.13.0",

toml-parser.js

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,11 @@
11
const TOML = require('@iarna/toml')
22
const Result = require('./result')
3-
const common = require('./common')
4-
5-
function splatForwardRule(path, obj, dest) {
6-
return (
7-
path.match(/\/\*$/) &&
8-
dest == null &&
9-
obj.status &&
10-
obj.status >= 200 &&
11-
obj.status < 300 &&
12-
obj.force
13-
)
14-
}
15-
16-
function isPlainObj(o) {
17-
return typeof o == 'object' && o.constructor == Object
18-
}
19-
20-
function fetch(obj, options) {
21-
for (const i in options) {
22-
if (obj.hasOwnProperty(options[i])) {
23-
return obj[options[i]]
24-
}
25-
}
26-
return null
27-
}
28-
29-
function redirectMatch(obj) {
30-
const origin = fetch(obj, ['from', 'origin'])
31-
const redirect =
32-
origin && origin.match(common.FULL_URL_MATCHER)
33-
? common.parseFullOrigin(origin)
34-
: { path: origin }
35-
if (redirect == null || (redirect.path == null && redirect.host == null)) {
36-
return null
37-
}
38-
39-
const dest = fetch(obj, ['to', 'destination'])
40-
if (splatForwardRule(redirect.path, obj, dest)) {
41-
redirect.to = redirect.path.replace(/\/\*$/, '/:splat')
42-
} else {
43-
redirect.to = dest
44-
}
45-
46-
if (redirect.to == null) {
47-
return null
48-
}
49-
50-
redirect.params = fetch(obj, ['query', 'params', 'parameters'])
51-
redirect.status = fetch(obj, ['status'])
52-
redirect.force = fetch(obj, ['force'])
53-
redirect.conditions = fetch(obj, ['conditions'])
54-
redirect.headers = fetch(obj, ['headers'])
55-
redirect.signed = fetch(obj, ['sign', 'signing', 'signed'])
56-
57-
Object.keys(redirect).forEach(key => {
58-
if (redirect[key] === null) {
59-
delete redirect[key]
60-
}
61-
})
62-
63-
if (redirect.headers && !isPlainObj(redirect.headers)) {
64-
return null
65-
}
66-
67-
return redirect
68-
}
3+
const {
4+
isPlainObj,
5+
redirectMatch,
6+
isInvalidSource,
7+
isProxy,
8+
} = require('./common')
699

7010
function parse(source) {
7111
const result = new Result()
@@ -87,14 +27,14 @@ function parse(source) {
8727
return
8828
}
8929

90-
if (common.isInvalidSource(redirect)) {
30+
if (isInvalidSource(redirect)) {
9131
result.addError(idx, JSON.stringify(obj), {
9232
reason: 'Invalid /.netlify path in redirect source',
9333
})
9434
return
9535
}
9636

97-
if (common.isProxy(redirect)) {
37+
if (isProxy(redirect)) {
9838
redirect.proxy = true
9939
}
10040

0 commit comments

Comments
 (0)