GithubHelp home page GithubHelp logo

danswar / nginx-config-parser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webantic/nginx-config-parser

0.0 0.0 0.0 177 KB

Parse nginx config strings into js objects and back

License: Other

JavaScript 100.00%

nginx-config-parser's Introduction

nginx-config-parser

var ConfigParser = require('@webantic/nginx-config-parser')
var parser = new ConfigParser()

// parse straight from file. by default, will try to resolve includes
var config = parser.readConfigFile('/path/to/file.conf')

// to keep deterministic behaviour, set parseIncludes = false in the options
var configWithoutIncludes = parser.readConfigFile('/path/to/file.conf', { parseIncludes: false })

// write direct to file (overwriting existing one)
parser.writeConfigFile('/path/to/newfile.conf', config, true)


var sampleConfig = {
  "server": {
    "server_name": "_",
    "location /": {
      "try_files": "*.html"
    }
  }
}

// to multi-line config string
var configString = parser.toConf(sampleConfig)
// and back again
var configJson = parser.toJSON(configString)

// shorthand (will change object --> string and string --> object)
parser.parse(configString)

Notes

Includes

.readConfigFile() will attempt to resolve includes and bundle them in the generated JavaScript object by default. If you call .toConf() (or .parse()) on the generated object, the generated conf string will differ from the original one as there is no way to replace the included content with the original include ... line. To control this behaviour, supply an options argument setting parseIncludes to false.

parser.readConfigFile(filePath, callback, options)
// or
parser.readConfigFile(filePath, options)

By default, the .toJSON() method will not attempt to resolve includes (because the module has no idea where to look for the included files when it is only supplied a conf string instead of a file path). To force the module to attempt to resolve includes, you must set options.parseIncludes to true when calling the method. If you supply a value for options.includesRoot, the module will use that as the base path to search in. If you do not provide a value for options.includesRoot, the module will attempt to resolve the files in the CWD.

If a referenced include cannot be resolved, this method will throw an IncludeResolutionError. To ignore this error (which is the default behaviour in nginx), set options.ignoreIncludeErrors to true.

Lua blocks / openresty

If the config contains a block which ends with the string "by_lua_block", the parser will not tokenise the contents of the block. Instead, the raw contents of the block will be stored under a special key _lua as an array of strings. Each string in the array represents a single line from the block. For example:

var config = [
  'access_by_lua_block {',
  '  ngx.var.url = ngx.unescape_uri(ngx.req.get_uri_args().url);',
  '}'
].join('\n')

const parsed = parser.parse(config)
console.log(JSON.stringify(parsed, null, 2))

// {
//   access_by_lua_block: {
//     _lua: [
//       'ngx.var.url = ngx.unescape_uri(ngx.req.get_uri_args().url);'
//     ]
//   }
// }

Multiline

When parsing multiline blocks, the behaviour is non-deterministic. Effectively, this means that your values will be collapsed onto a single line when flipping to JSON and back to conf.

const configString = `
http {
  proxy_cache_path /var/cache/nginx/users
    keys_zone=users:1m
    levels=2
    use_temp_path=off
    inactive=1d
    max_size=16m;
}
`;

const json = parser.toJSON(configString);

const expectedOutput = `
http {
  proxy_cache_path /var/cache/nginx/users keys_zone=users:1m levels=2 use_temp_path=off inactive=1d max_size=16m;
}
`;

parser.toConf(json) === expectedOutput; // true

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.