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

Commit e9a5417

Browse files
author
sw-yx
committed
prettify and add changelogs
1 parent de34ce9 commit e9a5417

File tree

8 files changed

+1990
-321
lines changed

8 files changed

+1990
-321
lines changed

common.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ module.exports = {
1717
return redirect.path.match(/^\/\.netlify/)
1818
},
1919
isProxy: function(redirect) {
20-
return redirect.proxy || (redirect.to.match(/^https?:\/\//) && redirect.status === 200)
20+
return (
21+
redirect.proxy ||
22+
(redirect.to.match(/^https?:\/\//) && redirect.status === 200)
23+
)
2124
},
2225
parseFullOrigin: function(origin) {
2326
let url = null
@@ -27,6 +30,10 @@ module.exports = {
2730
return null
2831
}
2932

30-
return { host: url.host, scheme: url.protocol.replace(/:$/, ''), path: url.path }
31-
}
33+
return {
34+
host: url.host,
35+
scheme: url.protocol.replace(/:$/, ''),
36+
path: url.path,
37+
}
38+
},
3239
}

line-parser.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ const Result = require('./result')
22
const common = require('./common')
33

44
function splatForwardRule(redirect, nextPart) {
5-
return redirect.path.match(/\/\*$/) && nextPart.match(common.FORWARD_STATUS_MATCHER)
5+
return (
6+
redirect.path.match(/\/\*$/) &&
7+
nextPart.match(common.FORWARD_STATUS_MATCHER)
8+
)
69
}
710

811
function arrayToObj(source) {
@@ -35,15 +38,19 @@ function redirectMatch(line) {
3538
}
3639

3740
const origin = parts.shift()
38-
const redirect = origin.match(common.FULL_URL_MATCHER) ? common.parseFullOrigin(origin) : { path: origin }
41+
const redirect = origin.match(common.FULL_URL_MATCHER)
42+
? common.parseFullOrigin(origin)
43+
: { path: origin }
3944
if (redirect == null || !parts.length) {
4045
return null
4146
}
4247

4348
if (splatForwardRule(redirect, parts[0])) {
4449
redirect.to = redirect.path.replace(/\/\*$/, '/:splat')
4550
} else {
46-
const newHostRuleIdx = parts.findIndex(el => el.match(/^\//) || el.match(common.FULL_URL_MATCHER))
51+
const newHostRuleIdx = parts.findIndex(
52+
el => el.match(/^\//) || el.match(common.FULL_URL_MATCHER)
53+
)
4754
if (newHostRuleIdx < 0) {
4855
return null
4956
}
@@ -99,7 +106,9 @@ function parse(text) {
99106
}
100107

101108
if (common.isInvalidSource(redirect)) {
102-
result.addError(idx, line, { reason: 'Invalid /.netlify path in redirect source' })
109+
result.addError(idx, line, {
110+
reason: 'Invalid /.netlify path in redirect source',
111+
})
103112
return
104113
}
105114

line-parser.test.js

Lines changed: 118 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test('simple redirects', t => {
1818
{ path: '/home', to: '/' },
1919
{ path: '/blog/my-post.php', to: '/blog/my-post' },
2020
{ path: '/blog/my-post-ads.php', to: '/blog/my-post#ads' },
21-
{ path: '/news', to: '/blog' }
21+
{ path: '/news', to: '/blog' },
2222
],
2323
result.success
2424
)
@@ -38,7 +38,7 @@ test('redirects with status codes', t => {
3838
{ path: '/home', to: '/', status: 301 },
3939
{ path: '/my-redirect', to: '/', status: 302 },
4040
{ path: '/pass-through', to: '/', status: 200 },
41-
{ path: '/ecommerce', to: '/store-closed', status: 404 }
41+
{ path: '/ecommerce', to: '/store-closed', status: 404 },
4242
],
4343
result.success
4444
)
@@ -55,7 +55,12 @@ test('redirects with parameter matches', t => {
5555
[
5656
{ path: '/', to: '/news', params: { page: 'news' } },
5757
{ path: '/blog', to: '/blog/:post_id', params: { post: ':post_id' } },
58-
{ path: '/', to: '/about', params: { _escaped_fragment_: '/about' }, status: 301 }
58+
{
59+
path: '/',
60+
to: '/about',
61+
params: { _escaped_fragment_: '/about' },
62+
status: 301,
63+
},
5964
],
6065
result.success
6166
)
@@ -66,7 +71,14 @@ test('redirects with full hostname', t => {
6671

6772
const result = parser.parse(source)
6873
t.deepEqual(
69-
[{ host: 'hello.bitballoon.com', scheme: 'http', path: '/*', to: 'http://www.hello.com/:splat' }],
74+
[
75+
{
76+
host: 'hello.bitballoon.com',
77+
scheme: 'http',
78+
path: '/*',
79+
to: 'http://www.hello.com/:splat',
80+
},
81+
],
7082
result.success
7183
)
7284
})
@@ -75,42 +87,94 @@ test('proxy instruction', t => {
7587
const source = `/api/* https://api.bitballoon.com/* 200`
7688

7789
const result = parser.parse(source)
78-
t.deepEqual([{ path: '/api/*', to: 'https://api.bitballoon.com/*', status: 200, proxy: true }], result.success)
90+
t.deepEqual(
91+
[
92+
{
93+
path: '/api/*',
94+
to: 'https://api.bitballoon.com/*',
95+
status: 200,
96+
proxy: true,
97+
},
98+
],
99+
result.success
100+
)
79101
})
80102

81103
test('redirect with country conditions', t => {
82104
const source = `/ /china 302 Country=ch,tw`
83105

84106
const result = parser.parse(source)
85-
t.deepEqual([{ path: '/', to: '/china', status: 302, conditions: { Country: 'ch,tw' } }], result.success)
107+
t.deepEqual(
108+
[
109+
{
110+
path: '/',
111+
to: '/china',
112+
status: 302,
113+
conditions: { Country: 'ch,tw' },
114+
},
115+
],
116+
result.success
117+
)
86118
})
87119

88120
test('redirect with country and language conditions', t => {
89121
const source = `/ /china 302 Country=il Language=en`
90122

91123
const result = parser.parse(source)
92-
t.deepEqual([{ path: '/', to: '/china', status: 302, conditions: { Country: 'il', Language: 'en' } }], result.success)
124+
t.deepEqual(
125+
[
126+
{
127+
path: '/',
128+
to: '/china',
129+
status: 302,
130+
conditions: { Country: 'il', Language: 'en' },
131+
},
132+
],
133+
result.success
134+
)
93135
})
94136

95137
test('splat based redirect with no force instruction', t => {
96138
const source = `/* https://www.bitballoon.com/:splat 301`
97139

98140
const result = parser.parse(source)
99-
t.deepEqual([{ path: '/*', to: 'https://www.bitballoon.com/:splat', status: 301 }], result.success)
141+
t.deepEqual(
142+
[{ path: '/*', to: 'https://www.bitballoon.com/:splat', status: 301 }],
143+
result.success
144+
)
100145
})
101146

102147
test('splat based redirect with force instruction', t => {
103148
const source = `/* https://www.bitballoon.com/:splat 301!`
104149

105150
const result = parser.parse(source)
106-
t.deepEqual([{ path: '/*', to: 'https://www.bitballoon.com/:splat', status: 301, force: true }], result.success)
151+
t.deepEqual(
152+
[
153+
{
154+
path: '/*',
155+
to: 'https://www.bitballoon.com/:splat',
156+
status: 301,
157+
force: true,
158+
},
159+
],
160+
result.success
161+
)
107162
})
108163

109164
test('redirect rule with equal', t => {
110165
const source = `/test https://www.bitballoon.com/test=hello 301`
111166

112167
const result = parser.parse(source)
113-
t.deepEqual([{ path: '/test', to: 'https://www.bitballoon.com/test=hello', status: 301 }], result.success)
168+
t.deepEqual(
169+
[
170+
{
171+
path: '/test',
172+
to: 'https://www.bitballoon.com/test=hello',
173+
status: 301,
174+
},
175+
],
176+
result.success
177+
)
114178
})
115179

116180
test('some real world edge case rules', t => {
@@ -123,18 +187,27 @@ test('some real world edge case rules', t => {
123187
to: '/donate/usa?source=:source&email=:email',
124188
params: { source: ':source', email: ':email' },
125189
status: 302,
126-
conditions: { Country: 'us' }
127-
}
128-
]
190+
conditions: { Country: 'us' },
191+
},
192+
],
129193
},
130194
{
131195
source: `/ https://origin.wework.com 200`,
132-
result: [{ path: '/', to: 'https://origin.wework.com', status: 200, proxy: true }]
196+
result: [
197+
{
198+
path: '/',
199+
to: 'https://origin.wework.com',
200+
status: 200,
201+
proxy: true,
202+
},
203+
],
133204
},
134205
{
135206
source: `/:lang/locations/* /locations/:splat 200`,
136-
result: [{ path: '/:lang/locations/*', to: '/locations/:splat', status: 200 }]
137-
}
207+
result: [
208+
{ path: '/:lang/locations/*', to: '/locations/:splat', status: 200 },
209+
],
210+
},
138211
]
139212
cases.forEach(testcase => {
140213
const result = parser.parse(testcase.source)
@@ -203,10 +276,15 @@ test('complicated _redirects file', t => {
203276
})
204277

205278
test('long _redirects file', t => {
206-
const source = fs.readFileSync('./test-files/redirects', { encoding: 'utf-8' })
279+
const source = fs.readFileSync('./test-files/redirects', {
280+
encoding: 'utf-8',
281+
})
207282

208283
const result = parser.parse(source)
209-
t.deepEqual([640, 734, 917, 918, 919, 920, 987], result.errors.map(e => e.lineNum))
284+
t.deepEqual(
285+
[640, 734, 917, 918, 919, 920, 987],
286+
result.errors.map(e => e.lineNum)
287+
)
210288
t.truthy(result.success.length > 0)
211289
})
212290

@@ -221,7 +299,7 @@ test('redirect with proxy signing', t => {
221299
status: 200,
222300
force: true,
223301
signed: 'API_SECRET',
224-
proxy: true
302+
proxy: true,
225303
},
226304
result.success[0]
227305
)
@@ -249,7 +327,7 @@ https://www.ximble.com/* https://www.ximble.com/au/:splat 301! Country=au
249327
to: 'https://www.ximble.com/au/:splat',
250328
status: 301,
251329
force: true,
252-
conditions: { Country: 'au' }
330+
conditions: { Country: 'au' },
253331
},
254332
result.success[0]
255333
)
@@ -259,15 +337,32 @@ test('redirect role conditions', t => {
259337
const source = `/admin/* /admin/:splat 200 Role=admin`
260338

261339
const result = parser.parse(source)
262-
t.deepEqual([{ path: '/admin/*', to: '/admin/:splat', status: 200, conditions: { Role: 'admin' } }], result.success)
340+
t.deepEqual(
341+
[
342+
{
343+
path: '/admin/*',
344+
to: '/admin/:splat',
345+
status: 200,
346+
conditions: { Role: 'admin' },
347+
},
348+
],
349+
result.success
350+
)
263351
})
264352

265353
test('redirect with multiple roles', t => {
266354
const source = `/member/* /member/:splat 200 Role=admin,member`
267355

268356
const result = parser.parse(source)
269357
t.deepEqual(
270-
[{ path: '/member/*', to: '/member/:splat', status: 200, conditions: { Role: 'admin,member' } }],
358+
[
359+
{
360+
path: '/member/*',
361+
to: '/member/:splat',
362+
status: 200,
363+
conditions: { Role: 'admin,member' },
364+
},
365+
],
271366
result.success
272367
)
273368
})
@@ -281,7 +376,7 @@ test('parse forward rule', t => {
281376
t.deepEqual(
282377
[
283378
{ path: '/admin/*', to: '/admin/:splat', status: 200 },
284-
{ path: '/admin/*', to: '/admin/:splat', status: 200, force: true }
379+
{ path: '/admin/*', to: '/admin/:splat', status: 200, force: true },
285380
],
286381
result.success
287382
)

0 commit comments

Comments
 (0)