README.md
 1  pac-resolver
 2  ============
 3  ### Generates an asynchronous resolver function from a [PAC file][pac-wikipedia]
 4  [![Build Status](https://github.com/TooTallNate/node-pac-resolver/workflows/Node%20CI/badge.svg)](https://github.com/TooTallNate/node-pac-resolver/actions?workflow=Node+CI)
 5  
 6  
 7  This module accepts a JavaScript String of code, which is meant to be a
 8  [PAC proxy file][pac-wikipedia], and returns a generated asynchronous
 9  `FindProxyForURL()` function.
10  
11  
12  Installation
13  ------------
14  
15  Install with `npm`:
16  
17  ```bash
18  $ npm install pac-resolver
19  ```
20  
21  
22  Example
23  -------
24  
25  Given the PAC proxy file named `proxy.pac`:
26  
27  ```js
28  function FindProxyForURL(url, host) {
29    if (isInNet(myIpAddress(), "10.1.10.0", "255.255.255.0")) {
30      return "PROXY 1.2.3.4:8080";
31    } else {
32      return "DIRECT";
33    }
34  }
35  ```
36  
37  You can consume this PAC file with `pac-resolver` like so:
38  
39  ```js
40  var fs = require('fs');
41  var pac = require('pac-resolver');
42  
43  var FindProxyForURL = pac(fs.readFileSync('proxy.pac'));
44  
45  FindProxyForURL('http://foo.com/').then((res) => {
46    console.log(res);
47    // "DIRECT"
48  });
49  ```
50  
51  
52  API
53  ---
54  
55  ### pac(String pacFileContents[, Object options]) → Function
56  
57  Returns an asynchronous `FindProxyForURL()` function based off of the given JS
58  string `pacFileContents` PAC proxy file. An optional `options` object may be
59  passed in which respects the following options:
60  
61   * `filename` - String - the filename to use in error stack traces. Defaults to `proxy.pac`.
62   * `sandbox` - Object - a map of functions to include in the sandbox of the
63   JavaScript environment where the JS code will be executed. i.e. if you wanted to
64   include the common `alert` function you could pass `alert: console.log`. For
65   async functions, you must set the `async = true` property on the function
66   instance, and the JS code will be able to invoke the function as if it were
67   synchronous.
68  
69  
70  License
71  -------
72  
73  (The MIT License)
74  
75  Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
76  
77  Permission is hereby granted, free of charge, to any person obtaining
78  a copy of this software and associated documentation files (the
79  'Software'), to deal in the Software without restriction, including
80  without limitation the rights to use, copy, modify, merge, publish,
81  distribute, sublicense, and/or sell copies of the Software, and to
82  permit persons to whom the Software is furnished to do so, subject to
83  the following conditions:
84  
85  The above copyright notice and this permission notice shall be
86  included in all copies or substantial portions of the Software.
87  
88  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
89  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
90  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
91  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
92  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
93  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
94  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
95  
96  [pac-file-docs]: https://web.archive.org/web/20070602031929/http://wp.netscape.com/eng/mozilla/2.0/relnotes/demo/proxy-live.html
97  [pac-wikipedia]: http://wikipedia.org/wiki/Proxy_auto-config