Skip to content

Commit

Permalink
feat: notify of dev server activity as a keep alive mechanism (#6690)
Browse files Browse the repository at this point in the history
* feat: notify of dev server activity as a keep alive mechanism
* chore: use open-api markDevServerActivity
  • Loading branch information
davbree committed Jun 13, 2024
1 parent df2202e commit 6f82e8c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/commands/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {

await startProxyServer({
addonsUrls,
api,
blobsContext,
command,
config: mutatedConfig,
Expand Down
3 changes: 3 additions & 0 deletions src/utils/proxy-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const generateInspectSettings = (
export const startProxyServer = async ({
accountId,
addonsUrls,
api,
blobsContext,
command,
config,
Expand All @@ -64,6 +65,7 @@ export const startProxyServer = async ({
}: {
accountId: string
addonsUrls: $TSFixMe
api?: NetlifyOptions['api']
blobsContext?: BlobsContextWithEdgeAccess
command: BaseCommand
config: NetlifyOptions['config']
Expand Down Expand Up @@ -106,6 +108,7 @@ export const startProxyServer = async ({
siteInfo,
accountId,
repositoryRoot,
api,
})
if (!url) {
log(NETLIFYDEVERR, `Unable to start proxy server on port '${settings.port}'`)
Expand Down
16 changes: 16 additions & 0 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import http, { ServerResponse } from 'http'
import https from 'https'
import { isIPv6 } from 'net'
import path from 'path'
import process from 'process'
import { Duplex } from 'stream'
import util from 'util'
import zlib from 'zlib'
Expand All @@ -24,6 +25,7 @@ import { locatePath } from 'locate-path'
import { Match } from 'netlify-redirector'
import pFilter from 'p-filter'
import toReadableStream from 'to-readable-stream'
import throttle from 'lodash/throttle.js'

import { BaseCommand } from '../commands/index.js'
import { $TSFixMe, NetlifyOptions } from '../commands/types.js'
Expand Down Expand Up @@ -230,6 +232,13 @@ const alternativePathsFor = function (url) {
return paths
}

const notifyActivity = throttle((api: NetlifyOptions['api'], siteId: string, devServerId: string) => {
// eslint-disable-next-line promise/prefer-await-to-callbacks, promise/prefer-await-to-then
api.markDevServerActivity({ siteId, devServerId }).catch((error) => {
console.error(`${NETLIFYDEVWARN} Failed to notify activity`, error)
})
}, 30 * 1000)

const serveRedirect = async function ({
env,
functionsRegistry,
Expand Down Expand Up @@ -718,6 +727,7 @@ const initializeProxy = async function ({
const onRequest = async (
{
addonsUrls,
api,
edgeFunctionsProxy,
env,
functionsRegistry,
Expand Down Expand Up @@ -790,6 +800,10 @@ const onRequest = async (
framework: settings.framework,
}

if (api && process.env.NETLIFY_DEV_SERVER_ID) {
notifyActivity(api, siteInfo.id, process.env.NETLIFY_DEV_SERVER_ID)
}

if (match) {
// We don't want to generate an ETag for 3xx redirects.
// @ts-expect-error TS(7031) FIXME: Binding element 'statusCode' implicitly has an 'an... Remove this comment to see the full error message
Expand Down Expand Up @@ -831,6 +845,7 @@ type EdgeFunctionsProxy = Awaited<ReturnType<typeof initializeEdgeFunctionsProxy
export const startProxy = async function ({
accountId,
addonsUrls,
api,
blobsContext,
command,
config,
Expand Down Expand Up @@ -920,6 +935,7 @@ export const startProxy = async function ({
imageProxy,
siteInfo,
env,
api,
})
const primaryServer = settings.https
? https.createServer({ cert: settings.https.cert, key: settings.https.key }, onRequestWithOptions)
Expand Down

2 comments on commit 6f82e8c

@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

  • Dependency count: 1,308
  • Package size: 437 MB
  • Number of ts-expect-error directives: 978

@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

  • Dependency count: 1,308
  • Package size: 437 MB
  • Number of ts-expect-error directives: 978

Please sign in to comment.