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 | |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 58 | [pac-file-docs]: https://web.archive.org/web/20070602031929/http://wp.netscape.com/eng/mozilla/2.0/relnotes/demo/proxy-live.html |
| 59 | [pac-wikipedia]: http://wikipedia.org/wiki/Proxy_auto-config |