Skip to content

Commit 4f57f10

Browse files
authored
fix: add missing error handling for unauthed live tunnel use (#7183)
* fix: add missing error handling for unauthed live tunnel * build: fix some suppressed eslint errors
1 parent 13e0e8d commit 4f57f10

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

eslint_temporary_suppressions.js

-10
Original file line numberDiff line numberDiff line change
@@ -1013,16 +1013,6 @@ export default [
10131013
'@typescript-eslint/no-unsafe-member-access': 'off',
10141014
},
10151015
},
1016-
{
1017-
files: ['src/utils/live-tunnel.ts'],
1018-
rules: {
1019-
'@typescript-eslint/no-unsafe-argument': 'off',
1020-
'@typescript-eslint/no-floating-promises': 'off',
1021-
'n/no-process-exit': 'off',
1022-
'@typescript-eslint/unbound-method': 'off',
1023-
'@typescript-eslint/restrict-template-expressions': 'off',
1024-
},
1025-
},
10261016
{
10271017
files: ['src/utils/parse-raw-flags.ts'],
10281018
rules: {

src/utils/live-tunnel.ts

+29-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import process from 'process'
1+
import { platform } from 'process'
22

33
import fetch from 'node-fetch'
44
import pWaitFor from 'p-wait-for'
@@ -7,7 +7,7 @@ import { v4 as uuidv4 } from 'uuid'
77
import { fetchLatestVersion, shouldFetchLatestVersion } from '../lib/exec-fetcher.js'
88
import { getPathInHome } from '../lib/settings.js'
99

10-
import { NETLIFYDEVERR, NETLIFYDEVLOG, chalk, log } from './command-helpers.js'
10+
import { NETLIFYDEVERR, NETLIFYDEVLOG, chalk, exit, log } from './command-helpers.js'
1111
import execa from './execa.js'
1212
import type CLIState from './cli-state.js'
1313

@@ -50,8 +50,12 @@ const createTunnel = async function ({
5050
const data = await response.json()
5151

5252
if (response.status !== 201) {
53-
// @ts-expect-error(serhalp) -- Use typed `netlify` API client?
54-
throw new Error(data.message)
53+
// TODO(serhalp): Use typed `netlify` API client?
54+
throw new Error(
55+
data != null && typeof data === 'object' && 'message' in data && typeof data.message === 'string'
56+
? data.message
57+
: '',
58+
)
5559
}
5660

5761
return data as LiveSession
@@ -74,9 +78,9 @@ const connectTunnel = function ({
7478
}
7579

7680
const ps = execa(execPath, args, { stdio: 'inherit' })
77-
ps.on('close', (code) => process.exit(code ?? undefined))
78-
ps.on('SIGINT', process.exit)
79-
ps.on('SIGTERM', process.exit)
81+
void ps.on('close', (code) => exit(code ?? undefined))
82+
void ps.on('SIGINT', () => exit())
83+
void ps.on('SIGTERM', () => exit())
8084
}
8185

8286
const installTunnelClient = async function () {
@@ -98,7 +102,7 @@ const installTunnelClient = async function () {
98102
packageName: PACKAGE_NAME,
99103
execName: EXEC_NAME,
100104
destination: binPath,
101-
extension: process.platform === 'win32' ? 'zip' : 'tar.gz',
105+
extension: platform === 'win32' ? 'zip' : 'tar.gz',
102106
})
103107
}
104108

@@ -119,13 +123,20 @@ export const startLiveTunnel = async ({
119123
'netlify init',
120124
)} or ${chalk.yellow('netlify link')}?`,
121125
)
122-
process.exit(1)
126+
return exit(1)
127+
}
128+
if (!netlifyApiToken) {
129+
console.error(
130+
`${NETLIFYDEVERR} Error: no Netlify auth token defined, did you forget to run ${chalk.yellow(
131+
'netlify login',
132+
)} or define 'NETLIFY_AUTH_TOKEN'?`,
133+
)
134+
return exit(1)
123135
}
124136

125137
const session = await createTunnel({
126138
siteId,
127-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- XXX(serhalp): removed in favor of runtime user feedback in stacked PR
128-
netlifyApiToken: netlifyApiToken!,
139+
netlifyApiToken,
129140
slug,
130141
})
131142

@@ -141,15 +152,18 @@ export const startLiveTunnel = async ({
141152
const data = await response.json()
142153

143154
if (response.status !== 200) {
144-
// @ts-expect-error(serhalp) -- Use typed `netlify` API client?
145-
throw new Error(data.message)
155+
// TODO(serhalp): Use typed `netlify` API client?
156+
throw new Error(
157+
data != null && typeof data === 'object' && 'message' in data && typeof data.message === 'string'
158+
? data.message
159+
: '',
160+
)
146161
}
147162

148163
return (data as LiveSession).state === 'online'
149164
}
150165

151-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- XXX(serhalp): removed in favor of runtime user feedback in stacked PR
152-
connectTunnel({ session, netlifyApiToken: netlifyApiToken!, localPort })
166+
connectTunnel({ session, netlifyApiToken, localPort })
153167

154168
// Waiting for the live session to have a state of `online`.
155169
await pWaitFor(isLiveTunnelReady, {

0 commit comments

Comments
 (0)