This repository was archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlive-tunnel.js
142 lines (121 loc) · 3.34 KB
/
live-tunnel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const fetch = require("node-fetch");
const fs = require("fs");
const os = require("os");
const path = require("path");
const execa = require("execa");
const chalk = require("chalk");
const { fetchLatest, updateAvailable } = require("gh-release-fetch");
const {
NETLIFYDEVLOG,
NETLIFYDEVWARN,
NETLIFYDEVERR
} = require("netlify-cli-logo");
async function createTunnel(siteId, netlifyApiToken, log) {
await installTunnelClient(log);
if (!siteId) {
console.error(
`${NETLIFYDEVERR} Error: no siteId defined, did you forget to run ${chalk.yellow(
"netlify init"
)} or ${chalk.yellow("netlify link")}?`
);
process.exit(1);
}
log(`${NETLIFYDEVLOG} Creating Live Tunnel for ` + siteId);
const url = `https://api.netlify.com/api/v1/live_sessions?site_id=${siteId}`;
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${netlifyApiToken}`
},
body: JSON.stringify({})
});
const data = await response.json();
if (response.status !== 201) {
throw new Error(data.message);
}
return data;
}
async function connectTunnel(session, netlifyApiToken, localPort, log, error) {
const execPath = path.join(
os.homedir(),
".netlify",
"tunnel",
"bin",
"live-tunnel-client"
);
const args = [
"connect",
"-s",
session.id,
"-t",
netlifyApiToken,
"-l",
localPort
];
if (process.env.DEBUG) {
args.push("-v");
log(execPath, args);
}
const ps = execa(execPath, args, { stdio: "inherit" });
ps.on("close", code => process.exit(code));
ps.on("SIGINT", process.exit);
ps.on("SIGTERM", process.exit);
}
async function installTunnelClient(log) {
const binPath = path.join(os.homedir(), ".netlify", "tunnel", "bin");
const execPath = path.join(binPath, "live-tunnel-client");
const newVersion = await fetchTunnelClient(execPath);
if (!newVersion) {
return;
}
log(`${NETLIFYDEVLOG} Installing Live Tunnel Client`);
const win = isWindows();
const platform = win ? "windows" : process.platform;
const extension = win ? "zip" : "tar.gz";
release = {
repository: "netlify/live-tunnel-client",
package: `live-tunnel-client-${platform}-amd64.${extension}`,
destination: binPath,
extract: true
};
await fetchLatest(release);
}
async function fetchTunnelClient(execPath) {
if (!execExist(execPath)) {
return true;
}
const { stdout } = await execa(execPath, ["version"]);
if (!stdout) {
return false;
}
const match = stdout.match(/^live-tunnel-client\/v?([^\s]+)/);
if (!match) {
return false;
}
return updateAvailable("netlify/live-tunnel-client", match[1]);
}
function execExist(binPath) {
if (!fs.existsSync(binPath)) {
return false;
}
const stat = fs.statSync(binPath);
return stat && stat.isFile() && isExe(stat.mode, stat.gid, stat.uid);
}
function isExe(mode, gid, uid) {
if (isWindows()) {
return true;
}
const isGroup = gid ? process.getgid && gid === process.getgid() : true;
const isUser = uid ? process.getuid && uid === process.getuid() : true;
return Boolean(
mode & 0o0001 || (mode & 0o0010 && isGroup) || (mode & 0o0100 && isUser)
);
}
function isWindows() {
return process.platform === "win32";
}
module.exports = {
createTunnel: createTunnel,
connectTunnel: connectTunnel
};