Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/utils/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const headersForPath = function (rules, path) {
return pathObject
}

const HEADER_SEPARATOR = ':'

const parseHeadersFile = function (filePath) {
if (!fs.existsSync(filePath)) {
return {}
Expand Down Expand Up @@ -94,9 +96,9 @@ const parseHeadersFile = function (filePath) {
throw new Error('path should come before headers')
}

if (line.includes(':')) {
const [key = '', value = ''] = line.split(':', 2)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a port of our Ruby code. TIL limit in Ruby's split doesn't mean the same thing as limit in JavaScript's split.

const [trimmedKey, trimmedValue] = [key.trim(), value.trim()]
if (line.includes(HEADER_SEPARATOR)) {
const [key = '', ...value] = line.split(HEADER_SEPARATOR)
const [trimmedKey, trimmedValue] = [key.trim(), value.join(HEADER_SEPARATOR).trim()]
if (trimmedKey.length === 0 || trimmedValue.length === 0) {
throw new Error(`invalid header at line: ${index}\n${line}\n`)
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils/headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const headers = [
path: '/directory/*/test.html',
headers: ['X-Frame-Options: test'],
},
{
path: '/with-colon',
headers: ['Custom-header: http://www.example.com'],
},
]

test.before(async (t) => {
Expand Down Expand Up @@ -99,6 +103,9 @@ test('_headers: validate rules', (t) => {
'/directory/*/test.html': {
'X-Frame-Options': ['test'],
},
'/with-colon': {
'Custom-header': ['http://www.example.com'],
},
})
})

Expand Down