Skip to content

Commit

Permalink
fix: move try catch to prepareServer (#4732)
Browse files Browse the repository at this point in the history
* fix: move try catch to prepareServer
  • Loading branch information
jackiewmacharia authored Jul 1, 2022
1 parent 55d7013 commit 8248c35
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions src/lib/edge-functions/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { cwd, env } = require('process')
const getAvailablePort = require('get-port')
const { v4: generateUUID } = require('uuid')

const { NETLIFYDEVERR, NETLIFYDEVWARN, chalk, error, log } = require('../../utils/command-helpers')
const { NETLIFYDEVERR, NETLIFYDEVWARN, chalk, error: printError, log } = require('../../utils/command-helpers')
const { getGeoLocation } = require('../geo-location')
const { getPathInProject } = require('../settings')
const { startSpinner, stopSpinner } = require('../spinner')
Expand Down Expand Up @@ -77,24 +77,18 @@ const initializeProxy = async ({
projectDir,
})
const hasEdgeFunctions = userFunctionsPath !== undefined || internalFunctions.length !== 0
let hasServerError = false

return async (req) => {
if (req.headers[headers.Passthrough] !== undefined || !hasEdgeFunctions || hasServerError) {
if (req.headers[headers.Passthrough] !== undefined || !hasEdgeFunctions) {
return
}

let promiseResult
const [geoLocation, registry] = await Promise.all([
getGeoLocation({ mode: geolocationMode, offline, state }),
server,
])

try {
promiseResult = await Promise.all([getGeoLocation({ mode: geolocationMode, offline, state }), server])
} catch (error_) {
error(error_.message, { exit: false })
hasServerError = true
return
}

const [geoLocation, { registry }] = promiseResult
if (!registry) return

// Setting header with geolocation.
req.headers[headers.Geo] = JSON.stringify(geoLocation)
Expand Down Expand Up @@ -152,34 +146,39 @@ const prepareServer = async ({
port,
projectDir,
}) => {
const bundler = await import('@netlify/edge-bundler')
const distImportMapPath = getPathInProject([DIST_IMPORT_MAP_PATH])
const runIsolate = await bundler.serve({
...getDownloadUpdateFunctions(),
certificatePath,
debug: env.NETLIFY_DENO_DEBUG === 'true',
distImportMapPath,
formatExportTypeError: (name) =>
`${NETLIFYDEVERR} ${chalk.red('Failed')} to load Edge Function ${chalk.yellow(
name,
)}. The file does not seem to have a function as the default export.`,
formatImportError: (name) => `${NETLIFYDEVERR} ${chalk.red('Failed')} to run Edge Function ${chalk.yellow(name)}:`,
importMaps,
inspectSettings,
port,
})
const registry = new EdgeFunctionsRegistry({
bundler,
config,
configPath,
directories,
getUpdatedConfig,
internalFunctions,
projectDir,
runIsolate,
})
try {
const bundler = await import('@netlify/edge-bundler')
const distImportMapPath = getPathInProject([DIST_IMPORT_MAP_PATH])
const runIsolate = await bundler.serve({
...getDownloadUpdateFunctions(),
certificatePath,
debug: env.NETLIFY_DENO_DEBUG === 'true',
distImportMapPath,
formatExportTypeError: (name) =>
`${NETLIFYDEVERR} ${chalk.red('Failed')} to load Edge Function ${chalk.yellow(
name,
)}. The file does not seem to have a function as the default export.`,
formatImportError: (name) =>
`${NETLIFYDEVERR} ${chalk.red('Failed')} to run Edge Function ${chalk.yellow(name)}:`,
importMaps,
inspectSettings,
port,
})
const registry = new EdgeFunctionsRegistry({
bundler,
config,
configPath,
directories,
getUpdatedConfig,
internalFunctions,
projectDir,
runIsolate,
})

return { registry, runIsolate }
return registry
} catch (error) {
printError(error.message, { exit: false })
}
}

module.exports = { handleProxyRequest, initializeProxy, isEdgeFunctionsRequest }

1 comment on commit 8248c35

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 221 MB

Please sign in to comment.