Skip to content
Open
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 lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ const fs = require('fs')
const { resolve, dirname } = require('path')
const glob = require('glob')

const DOT_TOKEN = '<_DOT_TOKEN_>'

/**
* Converts ! in back to .
* Converts DOT_TOKEN in back to .
* @param key
* @return {*|void|string|never}
*/
function unsafeKey (key) {
return key.replace(/(!)/g, '.')
return key.replace(new RegExp(DOT_TOKEN, 'g'), '.')
}

/**
Expand All @@ -19,7 +21,7 @@ function unsafeKey (key) {
* @return {*|void|string|never}
*/
function safeKey (key) {
return key.replace(/(\.)/g, '!')
return key.replace(/(\.)/g, DOT_TOKEN)
}

class IncludeResolutionError extends ReferenceError {}
Expand Down
146 changes: 145 additions & 1 deletion test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('toJSON', () => {
parser.toJSON(configString).should.deep.equal({ server: {} })
})

it('should support dots in keys (like if)', () => {
it('should support dots in keys (like `if`)', () => {
const configString = [
'server {',
' if ($host = example.example.com) {',
Expand All @@ -44,6 +44,150 @@ describe('toJSON', () => {
})
})

it('should support = operator in `if`', () => {
const configString = [
'server {',
' if ($var = foo) {',
' return 301 https://$host$request_uri;',
' }',
'}'
].join('\n')

parser.toJSON(configString).should.deep.equal({
server: {
'if ($var = foo)': {
return: '301 https://$host$request_uri'
}
}
})
})

it('should support != operator in `if`', () => {
const configString = [
'server {',
' if ($var != foo) {',
' return 301 https://$host$request_uri;',
' }',
'}'
].join('\n')

parser.toJSON(configString).should.deep.equal({
server: {
'if ($var != foo)': {
return: '301 https://$host$request_uri'
}
}
})
})

it('should support ~ operator in `if`', () => {
const configString = [
'server {',
' if ($var ~ "foo") {',
' return 301 https://$host$request_uri;',
' }',
'}'
].join('\n')

parser.toJSON(configString).should.deep.equal({
server: {
'if ($var ~ "foo")': {
return: '301 https://$host$request_uri'
}
}
})
})

it('should support !~ operator in if', () => {
const configString = [
'server {',
' if ($var !~ "foo") {',
' return 301 https://$host$request_uri;',
' }',
'}'
].join('\n')

parser.toJSON(configString).should.deep.equal({
server: {
'if ($var !~ "foo")': {
return: '301 https://$host$request_uri'
}
}
})
})

it('should support ~* operator in `if`', () => {
const configString = [
'server {',
' if ($var ~* "foo") {',
' return 301 https://$host$request_uri;',
' }',
'}'
].join('\n')

parser.toJSON(configString).should.deep.equal({
server: {
'if ($var ~* "foo")': {
return: '301 https://$host$request_uri'
}
}
})
})

it('should support !~* operator in `if`', () => {
const configString = [
'server {',
' if ($var !~* "foo") {',
' return 301 https://$host$request_uri;',
' }',
'}'
].join('\n')

parser.toJSON(configString).should.deep.equal({
server: {
'if ($var !~* "foo")': {
return: '301 https://$host$request_uri'
}
}
})
})

it('should support `-f`-like operators in `if`', () => {
const configString = [
'server {',
' if ($var -f "foo") {',
' return 301 https://$host$request_uri;',
' }',
'}'
].join('\n')

parser.toJSON(configString).should.deep.equal({
server: {
'if ($var -f "foo")': {
return: '301 https://$host$request_uri'
}
}
})
})

it('should support `!-f`-like operators in `if`', () => {
const configString = [
'server {',
' if ($var !-f "foo") {',
' return 301 https://$host$request_uri;',
' }',
'}'
].join('\n')

parser.toJSON(configString).should.deep.equal({
server: {
'if ($var !-f "foo")': {
return: '301 https://$host$request_uri'
}
}
})
})

it('should support nested directives', () => {
const configString = ['server {',
' listen 443;',
Expand Down