npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webantic/nginx-config-parser

v1.6.1

Published

Convert nginx conf strings to JSON and back!

Downloads

3,560

Readme

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