UNPKG

1.69 kBJavaScriptView Raw
1const { homedir } = require("os");
2const { join } = require("path");
3const { parse } = require("yaml");
4const { readFileSync } = require("fs");
5
6function defaultConfigPath() {
7 return join(homedir(), ".ngrok2", "ngrok.yml");
8}
9
10function defaults(opts) {
11 opts = opts || { proto: "http", addr: 80 };
12 if (opts.name) {
13 const configPath = opts.configPath || defaultConfigPath();
14 const config = parse(readFileSync(configPath, "utf8"));
15 if (config.tunnels && config.tunnels[opts.name]) {
16 opts = Object.assign(opts, config.tunnels[opts.name]);
17 }
18 }
19 if (typeof opts === "function") opts = { proto: "http", addr: 80 };
20 if (typeof opts !== "object") opts = { proto: "http", addr: opts };
21 if (!opts.proto) opts.proto = "http";
22 if (!opts.addr) opts.addr = opts.port || opts.host || 80;
23 if (opts.httpauth) opts.auth = opts.httpauth;
24 return opts;
25}
26
27function validate(opts) {
28 if (opts.web_addr === false || opts.web_addr === "false") {
29 throw new Error(
30 "web_addr:false is not supported, module depends on internal ngrok api"
31 );
32 }
33}
34
35function isRetriable(err) {
36 if (!err.response) {
37 return false;
38 }
39 const statusCode = err.response.statusCode;
40 const body = err.body;
41 const notReady500 = statusCode === 500 && /panic/.test(body);
42 const notReady502 =
43 statusCode === 502 &&
44 body.details &&
45 body.details.err === "tunnel session not ready yet";
46 const notReady503 =
47 statusCode === 503 &&
48 body.details &&
49 body.details.err ===
50 "a successful ngrok tunnel session has not yet been established";
51 return notReady500 || notReady502 || notReady503;
52}
53
54module.exports = {
55 defaults,
56 validate,
57 isRetriable,
58};