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