JavaScript best practices for PAC files
The JavaScript skills needed for most PAC file development are modest. Occasionally, an advanced understanding is needed. A good Internet resource is the website www.findproxyforurl.com.
Whether you are creating a new PAC file or assuming responsibilities for an existing file, these best practices are worth consideration. The list is inspired by and incorporates many entries from a blog post by Lee Harvey titled “Proxy Automatic Config (PAC) File Tips” (post no longer available online).
- Thoroughly review and understand the PAC file before making changes.
- Use the PAC or WPAD facility in Content Gateway to maintain the PAC or WPAD file. If you choose to edit the file separately, be sure to use a text editor that does not add or change formatting (e.g. vi, notepad, etc.).
- Comment the code consistent with programming best practices. Successors should have no questions about the intent of the code.
- Keep the file as small and efficient as possible.
- Validate support for built-in JavaScript functions before using them.
- Check URL and host parameters before using them.
- Check simple rule exceptions first.
- Place high-probability checks near the top.
- Use efficient regular expressions, and avoid capturing matches that will not be used.
- Because “return” is immediate, avoid using “else” with “if” statements.
- Single-line if() statements do not require begin { and end } brackets.
- Carefully consider the use (overuse) of isResolvable(), dnsResolve(), and isInNet() for potential DNS performance issues.
- Avoid using external or global variables and functions.
- Because .pac files are text and can be downloaded and viewed by anyone, use appropriate file permissions and avoid revealing secrets.
- When possible, sort lists of IP addresses and/or domains to ease future maintenance efforts.
- When possible, group common return values into single conditional if() checks.
- For single proxy server environments, return the proxy’s static IP address to bypass the DNS lookup overhead.
- Test all conditions and exceptions used in your .pac file prior to deployment. Verify that your JavaScript is error-free.