Skip to content

Commit 6f82e8c

Browse files
authoredJun 13, 2024··
feat: notify of dev server activity as a keep alive mechanism (#6690)
* feat: notify of dev server activity as a keep alive mechanism * chore: use open-api markDevServerActivity
1 parent df2202e commit 6f82e8c

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed
 

‎src/commands/dev/dev.ts

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
218218

219219
await startProxyServer({
220220
addonsUrls,
221+
api,
221222
blobsContext,
222223
command,
223224
config: mutatedConfig,

‎src/utils/proxy-server.ts

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const generateInspectSettings = (
4242
export const startProxyServer = async ({
4343
accountId,
4444
addonsUrls,
45+
api,
4546
blobsContext,
4647
command,
4748
config,
@@ -64,6 +65,7 @@ export const startProxyServer = async ({
6465
}: {
6566
accountId: string
6667
addonsUrls: $TSFixMe
68+
api?: NetlifyOptions['api']
6769
blobsContext?: BlobsContextWithEdgeAccess
6870
command: BaseCommand
6971
config: NetlifyOptions['config']
@@ -106,6 +108,7 @@ export const startProxyServer = async ({
106108
siteInfo,
107109
accountId,
108110
repositoryRoot,
111+
api,
109112
})
110113
if (!url) {
111114
log(NETLIFYDEVERR, `Unable to start proxy server on port '${settings.port}'`)

‎src/utils/proxy.ts

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import http, { ServerResponse } from 'http'
55
import https from 'https'
66
import { isIPv6 } from 'net'
77
import path from 'path'
8+
import process from 'process'
89
import { Duplex } from 'stream'
910
import util from 'util'
1011
import zlib from 'zlib'
@@ -24,6 +25,7 @@ import { locatePath } from 'locate-path'
2425
import { Match } from 'netlify-redirector'
2526
import pFilter from 'p-filter'
2627
import toReadableStream from 'to-readable-stream'
28+
import throttle from 'lodash/throttle.js'
2729

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

235+
const notifyActivity = throttle((api: NetlifyOptions['api'], siteId: string, devServerId: string) => {
236+
// eslint-disable-next-line promise/prefer-await-to-callbacks, promise/prefer-await-to-then
237+
api.markDevServerActivity({ siteId, devServerId }).catch((error) => {
238+
console.error(`${NETLIFYDEVWARN} Failed to notify activity`, error)
239+
})
240+
}, 30 * 1000)
241+
233242
const serveRedirect = async function ({
234243
env,
235244
functionsRegistry,
@@ -718,6 +727,7 @@ const initializeProxy = async function ({
718727
const onRequest = async (
719728
{
720729
addonsUrls,
730+
api,
721731
edgeFunctionsProxy,
722732
env,
723733
functionsRegistry,
@@ -790,6 +800,10 @@ const onRequest = async (
790800
framework: settings.framework,
791801
}
792802

803+
if (api && process.env.NETLIFY_DEV_SERVER_ID) {
804+
notifyActivity(api, siteInfo.id, process.env.NETLIFY_DEV_SERVER_ID)
805+
}
806+
793807
if (match) {
794808
// We don't want to generate an ETag for 3xx redirects.
795809
// @ts-expect-error TS(7031) FIXME: Binding element 'statusCode' implicitly has an 'an... Remove this comment to see the full error message
@@ -831,6 +845,7 @@ type EdgeFunctionsProxy = Awaited<ReturnType<typeof initializeEdgeFunctionsProxy
831845
export const startProxy = async function ({
832846
accountId,
833847
addonsUrls,
848+
api,
834849
blobsContext,
835850
command,
836851
config,
@@ -920,6 +935,7 @@ export const startProxy = async function ({
920935
imageProxy,
921936
siteInfo,
922937
env,
938+
api,
923939
})
924940
const primaryServer = settings.https
925941
? https.createServer({ cert: settings.https.cert, key: settings.https.key }, onRequestWithOptions)

2 commit comments

Comments
 (2)

github-actions[bot] commented on Jun 13, 2024

@github-actions[bot]

📊 Benchmark results

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

github-actions[bot] commented on Jun 13, 2024

@github-actions[bot]

📊 Benchmark results

  • Dependency count: 1,308
  • Package size: 437 MB
  • Number of ts-expect-error directives: 978
Please sign in to comment.