blob: a7ea4db4c67dcdf1f06c823b82066e7d0f479300 [file] [log] [blame] [view]
Randolf Jung3e526312023-08-08 06:20:391pac-resolver
2============
3### Generates an asynchronous resolver function from a [PAC file][pac-wikipedia]
4
5
6This 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
10Example
11-------
12
13Given the PAC proxy file named `proxy.pac`:
14
15```js
16function 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
25You can consume this PAC file with `pac-resolver` like so:
26
27```ts
28import { readFileSync } from 'fs';
29import { createPacResolver } from 'pac-resolver';
30
31const FindProxyForURL = createPacResolver(readFileSync('proxy.pac'));
32
33const res = await FindProxyForURL('http://foo.com/');
34console.log(res);
35// "DIRECT"
36```
37
38
39API
40---
41
42### pac(qjs: QuickJSWASMModule, pacFileContents: string | Buffer, options?: PacResolverOptions) → Function
43
44Returns an asynchronous `FindProxyForURL()` function based off of the given JS
45string `pacFileContents` PAC proxy file. An optional `options` object may be
46passed 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
59License
60-------
61
62(The MIT License)
63
64Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
65
66Permission is hereby granted, free of charge, to any person obtaining
67a copy of this software and associated documentation files (the
68'Software'), to deal in the Software without restriction, including
69without limitation the rights to use, copy, modify, merge, publish,
70distribute, sublicense, and/or sell copies of the Software, and to
71permit persons to whom the Software is furnished to do so, subject to
72the following conditions:
73
74The above copyright notice and this permission notice shall be
75included in all copies or substantial portions of the Software.
76
77THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
78EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
79MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
80IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
81CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
82TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
83SOFTWARE 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