-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathlive-tunnel.ts
203 lines (172 loc) · 4.93 KB
/
live-tunnel.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { platform } from 'process'
import fetch from 'node-fetch'
import pWaitFor from 'p-wait-for'
import { v4 as uuidv4 } from 'uuid'
import { fetchLatestVersion, shouldFetchLatestVersion } from '../lib/exec-fetcher.js'
import { getPathInHome } from '../lib/settings.js'
import { NETLIFYDEVERR, NETLIFYDEVLOG, chalk, exit, log } from './command-helpers.js'
import execa from './execa.js'
import type CLIState from './cli-state.js'
const PACKAGE_NAME = 'live-tunnel-client'
const EXEC_NAME = PACKAGE_NAME
const SLUG_LOCAL_STATE_KEY = 'liveTunnelSlug'
// 1 second
const TUNNEL_POLL_INTERVAL = 1e3
// 5 minutes
const TUNNEL_POLL_TIMEOUT = 3e5
interface LiveSession {
id: string
session_url: string
state: string
}
const createTunnel = async function ({
netlifyApiToken,
siteId,
slug,
}: {
netlifyApiToken: string
siteId: string
slug: string
}) {
await installTunnelClient()
const url = `https://api.netlify.com/api/v1/live_sessions?site_id=${siteId}&slug=${slug}`
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) {
// TODO(serhalp): Use typed `netlify` API client?
throw new Error(
data != null && typeof data === 'object' && 'message' in data && typeof data.message === 'string'
? data.message
: '',
)
}
return data as LiveSession
}
const connectTunnel = function ({
localPort,
netlifyApiToken,
session,
}: {
localPort: number
netlifyApiToken: string
session: LiveSession
}) {
const execPath = getPathInHome(['tunnel', 'bin', EXEC_NAME])
const args = ['connect', '-s', session.id, '-t', netlifyApiToken, '-l', localPort.toString()]
if (process.env.DEBUG) {
args.push('-v')
log(execPath, args.toString())
}
const ps = execa(execPath, args, { stdio: 'inherit' })
void ps.on('close', (code) => exit(code ?? undefined))
void ps.on('SIGINT', () => exit())
void ps.on('SIGTERM', () => exit())
}
const installTunnelClient = async function () {
const binPath = getPathInHome(['tunnel', 'bin'])
const shouldFetch = await shouldFetchLatestVersion({
binPath,
packageName: PACKAGE_NAME,
execArgs: ['version'],
pattern: `${PACKAGE_NAME}\\/v?([^\\s]+)`,
execName: EXEC_NAME,
})
if (!shouldFetch) {
return
}
log(`${NETLIFYDEVLOG} Installing Live Tunnel Client`)
await fetchLatestVersion({
packageName: PACKAGE_NAME,
execName: EXEC_NAME,
destination: binPath,
extension: platform === 'win32' ? 'zip' : 'tar.gz',
})
}
export const startLiveTunnel = async ({
localPort,
netlifyApiToken,
siteId,
slug,
}: {
localPort: number
netlifyApiToken?: string | null | undefined
siteId?: string | undefined
slug: string
}) => {
if (!siteId) {
console.error(
`${NETLIFYDEVERR} Error: no siteId defined, did you forget to run ${chalk.yellow(
'netlify init',
)} or ${chalk.yellow('netlify link')}?`,
)
return exit(1)
}
if (!netlifyApiToken) {
console.error(
`${NETLIFYDEVERR} Error: no Netlify auth token defined, did you forget to run ${chalk.yellow(
'netlify login',
)} or define 'NETLIFY_AUTH_TOKEN'?`,
)
return exit(1)
}
const session = await createTunnel({
siteId,
netlifyApiToken,
slug,
})
const isLiveTunnelReady = async (): Promise<boolean> => {
const url = `https://api.netlify.com/api/v1/live_sessions/${session.id}`
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${netlifyApiToken}`,
},
})
const data = await response.json()
if (response.status !== 200) {
// TODO(serhalp): Use typed `netlify` API client?
throw new Error(
data != null && typeof data === 'object' && 'message' in data && typeof data.message === 'string'
? data.message
: '',
)
}
return (data as LiveSession).state === 'online'
}
connectTunnel({ session, netlifyApiToken, localPort })
// Waiting for the live session to have a state of `online`.
await pWaitFor(isLiveTunnelReady, {
interval: TUNNEL_POLL_INTERVAL,
timeout: TUNNEL_POLL_TIMEOUT,
})
return session.session_url
}
export const getLiveTunnelSlug = (state: CLIState, override?: string) => {
if (override !== undefined) {
return override
}
const newSlug = generateRandomSlug()
try {
const existingSlug = state.get(SLUG_LOCAL_STATE_KEY)
if (existingSlug !== undefined) {
return existingSlug
}
state.set(SLUG_LOCAL_STATE_KEY, newSlug)
} catch (error) {
log(
`${NETLIFYDEVERR} Could not read or write local state file: ${
error instanceof Error ? error.message : error?.toString() ?? ''
}`,
)
}
return newSlug
}
const generateRandomSlug = () => uuidv4().slice(0, 8)