blob: 2b99643738aa49e5ec1bda2717fa06af67ed3660 [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
Randolf Jung3e526312023-08-08 06:20:3958[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