vasilevich / nginxbeautifier

Format and beautify nginx config files
https://nginxbeautifier.com
Apache License 2.0
167 stars 20 forks source link

lua blocks parsing #30

Closed realies closed 3 months ago

realies commented 4 months ago

blocks like this:

        location / {
            access_by_lua_block {
                local http = require("resty.http")

                local target_ip = "192.168.0.1"
                local target_port = 8080
                local mac_address = "aa:bb:cc:dd:ee:ff"

                local httpc = http.new()
                httpc:set_timeout(1000)  -- 1 second timeout

                local res, err = httpc:request_uri("http://" .. target_ip .. ":" .. target_port, {method = "HEAD"})

                if not res then
                    ngx.log(ngx.NOTICE, "Target is not responding, attempting to send WoL packet")
                    local command = string.format("wakeonlan %s", mac_address)
                    local success = os.execute(command)
                    if success then
                        ngx.log(ngx.NOTICE, "Sent WoL packet to " .. mac_address)
                        ngx.sleep(30)
                    else
                        ngx.log(ngx.ERR, "Failed to send WoL packet")
                    end
                else
                    ngx.log(ngx.NOTICE, "Target is already responsive")
                end
            }

            proxy_pass http://192.168.0.1:8080;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }

cause:

npmplus  | /usr/local/share/.config/yarn/global/node_modules/nginxbeautifier/nginxbeautifier.js:142
npmplus  |         return input.match(catchRegex)[1];
npmplus  |                                       ^
npmplus  | 
npmplus  | TypeError: Cannot read properties of null (reading '1')
npmplus  |     at extractTextBySeperator (/usr/local/share/.config/yarn/global/node_modules/nginxbeautifier/nginxbeautifier.js:142:39)
npmplus  |     at extractAllPossibleText (/usr/local/share/.config/yarn/global/node_modules/nginxbeautifier/nginxbeautifier.js:164:29)
npmplus  |     at strip_line (/usr/local/share/.config/yarn/global/node_modules/nginxbeautifier/nginxbeautifier.js:193:35)
npmplus  |     at Object.clean_lines (/usr/local/share/.config/yarn/global/node_modules/nginxbeautifier/nginxbeautifier.js:225:50)
npmplus  |     at Object.<anonymous> (/usr/local/share/.config/yarn/global/node_modules/nginxbeautifier/index.js:235:38)
npmplus  |     at Module._compile (node:internal/modules/cjs/loader:1358:14)
npmplus  |     at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
npmplus  |     at Module.load (node:internal/modules/cjs/loader:1208:32)
npmplus  |     at Module._load (node:internal/modules/cjs/loader:1024:12)
npmplus  |     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
npmplus  | 
npmplus  | Node.js v20.13.1

should lua blocks be beautified as well?

realies commented 4 months ago

the issue appears to be coming from lack of spaces around the curly braces

-      local res, err = httpc:request_uri("http://" .. target_ip .. ":" .. target_port, {method = "HEAD"})
+      local res, err = httpc:request_uri("http://" .. target_ip .. ":" .. target_port, { method = "HEAD" })
vasilevich commented 4 months ago

hi, feel free to make a pull request

thanks

realies commented 4 months ago

do you have any idea from which regex expression this could be coming from?

realies commented 3 months ago

this is actually an issue in @denysvitali's older fork that's published on npm