Skip to content

Commit 0b1d68d

Browse files
feat: support for Edge Functions debugging (#4615)
* feat: support for Edge Functions debugging Co-authored-by: jackiewmacharia <jackiewmacharia@users.noreply.github.com> Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
1 parent 64fca17 commit 0b1d68d

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

Diff for: docs/commands/dev.md

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ netlify dev
1919

2020
- `command` (*string*) - command to run
2121
- `dir` (*string*) - dir with static files
22+
- `edgeInspect` (*string*) - enable the V8 Inspector Protocol for Edge Functions, with an optional address in the host:port format
23+
- `edgeInspectBrk` (*string*) - enable the V8 Inspector Protocol for Edge Functions and pause execution on the first line of code, with an optional address in the host:port format
2224
- `framework` (*string*) - framework to use. Defaults to #auto which automatically detects a framework
2325
- `functions` (*string*) - specify a functions folder to serve
2426
- `functionsPort` (*string*) - port of functions server
@@ -43,6 +45,10 @@ netlify dev
4345
netlify dev -d public
4446
netlify dev -c "hugo server -w" --targetPort 1313
4547
netlify dev --graph
48+
netlify dev --edgeInspect
49+
netlify dev --edgeInspect=127.0.0.1:9229
50+
netlify dev --edgeInspectBrk
51+
netlify dev --edgeInspectBrk=127.0.0.1:9229
4652
BROWSER=none netlify dev # disable browser auto opening
4753
```
4854

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
"Tim Perry (https://twitter.com/pimterry)",
123123
"Tim Trautman (timtrautman.com)",
124124
"Tom Dyson (https://wagtail.io/)",
125+
"Tom Hu (https://twitter.com/thomasrockhu)",
125126
"Travis Horn <travis@travishorn.com> (https://twitter.com/horn_travis)",
126127
"Try Ajitiono <ballinst@gmail.com> (https://twitter.com/Ajiballinst)",
127128
"Victor Dadfar",

Diff for: src/commands/dev/dev.js

+74
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727
readGraphQLOperationsSourceFile,
2828
} = require('../../lib/one-graph/cli-netlify-graph')
2929
const {
30+
BANG,
3031
NETLIFYDEV,
3132
NETLIFYDEVERR,
3233
NETLIFYDEVLOG,
@@ -195,11 +196,19 @@ const startFrameworkServer = async function ({ settings }) {
195196
// 10 minutes
196197
const FRAMEWORK_PORT_TIMEOUT = 6e5
197198

199+
/**
200+
* @typedef {Object} InspectSettings
201+
* @property {boolean} enabled - Inspect enabled
202+
* @property {boolean} pause - Pause on breakpoints
203+
* @property {string|undefined} address - Host/port override (optional)
204+
*/
205+
198206
/**
199207
*
200208
* @param {object} params
201209
* @param {*} params.addonsUrls
202210
* @param {import('../base-command').NetlifyOptions["config"]} params.config
211+
* @param {InspectSettings} params.inspectSettings
203212
* @param {() => Promise<object>} params.getUpdatedConfig
204213
* @param {string} params.geolocationMode
205214
* @param {*} params.settings
@@ -213,6 +222,7 @@ const startProxyServer = async ({
213222
config,
214223
geolocationMode,
215224
getUpdatedConfig,
225+
inspectSettings,
216226
offline,
217227
settings,
218228
site,
@@ -224,6 +234,7 @@ const startProxyServer = async ({
224234
configPath: site.configPath,
225235
geolocationMode,
226236
getUpdatedConfig,
237+
inspectSettings,
227238
offline,
228239
projectDir: site.root,
229240
settings,
@@ -312,6 +323,46 @@ const startPollingForAPIAuthentication = async function (options) {
312323
await helper(siteInfo)
313324
}
314325

326+
/**
327+
* @param {boolean|string} edgeInspect
328+
* @param {boolean|string} edgeInspectBrk
329+
* @returns {InspectSettings}
330+
*/
331+
const generateInspectSettings = (edgeInspect, edgeInspectBrk) => {
332+
const enabled = Boolean(edgeInspect) || Boolean(edgeInspectBrk)
333+
const pause = Boolean(edgeInspectBrk)
334+
const getAddress = () => {
335+
if (edgeInspect) {
336+
return typeof edgeInspect === 'string' ? edgeInspect : undefined
337+
}
338+
if (edgeInspectBrk) {
339+
return typeof edgeInspectBrk === 'string' ? edgeInspectBrk : undefined
340+
}
341+
}
342+
343+
return {
344+
enabled,
345+
pause,
346+
address: getAddress(),
347+
}
348+
}
349+
350+
const validateShortFlagArgs = (args) => {
351+
if (args.startsWith('=')) {
352+
throw new Error(
353+
`Short flag options like -e or -E don't support the '=' sign
354+
${chalk.red(BANG)} Supported formats:
355+
netlify dev -e
356+
netlify dev -e 127.0.0.1:9229
357+
netlify dev -e127.0.0.1:9229
358+
netlify dev -E
359+
netlify dev -E 127.0.0.1:9229
360+
netlify dev -E127.0.0.1:9229`,
361+
)
362+
}
363+
return args
364+
}
365+
315366
/**
316367
* The dev command
317368
* @param {import('commander').OptionValues} options
@@ -380,11 +431,14 @@ const dev = async (options, command) => {
380431
return normalizedNewConfig
381432
}
382433

434+
const inspectSettings = generateInspectSettings(options.edgeInspect, options.edgeInspectBrk)
435+
383436
let url = await startProxyServer({
384437
addonsUrls,
385438
config,
386439
geolocationMode: options.geo,
387440
getUpdatedConfig,
441+
inspectSettings,
388442
offline: options.offline,
389443
settings,
390444
site,
@@ -522,11 +576,31 @@ const createDevCommand = (program) => {
522576
.hideHelp(),
523577
)
524578
.addOption(new Option('--graph', 'enable Netlify Graph support').hideHelp())
579+
.addOption(
580+
new Option(
581+
'-e, --edgeInspect [address]',
582+
'enable the V8 Inspector Protocol for Edge Functions, with an optional address in the host:port format',
583+
)
584+
.conflicts('edgeInspectBrk')
585+
.argParser(validateShortFlagArgs),
586+
)
587+
.addOption(
588+
new Option(
589+
'-E, --edgeInspectBrk [address]',
590+
'enable the V8 Inspector Protocol for Edge Functions and pause execution on the first line of code, with an optional address in the host:port format',
591+
)
592+
.conflicts('edgeInspect')
593+
.argParser(validateShortFlagArgs),
594+
)
525595
.addExamples([
526596
'netlify dev',
527597
'netlify dev -d public',
528598
'netlify dev -c "hugo server -w" --targetPort 1313',
529599
'netlify dev --graph',
600+
'netlify dev --edgeInspect',
601+
'netlify dev --edgeInspect=127.0.0.1:9229',
602+
'netlify dev --edgeInspectBrk',
603+
'netlify dev --edgeInspectBrk=127.0.0.1:9229',
530604
'BROWSER=none netlify dev # disable browser auto opening',
531605
])
532606
.action(dev)

Diff for: src/lib/edge-functions/proxy.js

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const initializeProxy = async ({
4747
configPath,
4848
geolocationMode,
4949
getUpdatedConfig,
50+
inspectSettings,
5051
offline,
5152
projectDir,
5253
settings,
@@ -67,6 +68,7 @@ const initializeProxy = async ({
6768
directories: [internalFunctionsPath, userFunctionsPath].filter(Boolean),
6869
getUpdatedConfig,
6970
importMaps: [importMap].filter(Boolean),
71+
inspectSettings,
7072
internalFunctions,
7173
port: isolatePort,
7274
projectDir,
@@ -133,6 +135,7 @@ const prepareServer = async ({
133135
directories,
134136
getUpdatedConfig,
135137
importMaps,
138+
inspectSettings,
136139
internalFunctions,
137140
port,
138141
projectDir,
@@ -150,6 +153,7 @@ const prepareServer = async ({
150153
)}. The file does not seem to have a function as the default export.`,
151154
formatImportError: (name) => `${NETLIFYDEVERR} ${chalk.red('Failed')} to run Edge Function ${chalk.yellow(name)}:`,
152155
importMaps,
156+
inspectSettings,
153157
port,
154158
})
155159
const registry = new EdgeFunctionsRegistry({

Diff for: src/utils/proxy.js

+2
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ const startProxy = async function ({
467467
configPath,
468468
geolocationMode,
469469
getUpdatedConfig,
470+
inspectSettings,
470471
offline,
471472
projectDir,
472473
settings,
@@ -478,6 +479,7 @@ const startProxy = async function ({
478479
configPath,
479480
geolocationMode,
480481
getUpdatedConfig,
482+
inspectSettings,
481483
offline,
482484
projectDir,
483485
settings,

0 commit comments

Comments
 (0)