Skip to content

Commit

Permalink
feat: expose site feature flags to EF registry (#6331)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas authored Jan 18, 2024
1 parent 96e47e0 commit b862c9e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/lib/edge-functions/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as bundler from '@netlify/edge-bundler'
import getAvailablePort from 'get-port'

import { NETLIFYDEVERR, chalk, error as printError } from '../../utils/command-helpers.js'
import { getFeatureFlagsFromSiteInfo } from '../../utils/feature-flags.js'
import { getGeoLocation } from '../geo-location.js'
import { getPathInProject } from '../settings.js'
import { startSpinner, stopSpinner } from '../spinner.js'
Expand Down Expand Up @@ -136,11 +137,9 @@ export const initializeProxy = async ({
}) => {
const userFunctionsPath = config.build.edge_functions
const isolatePort = await getAvailablePort()
const buildFeatureFlags = {
edge_functions_npm_modules: true,
}
const runtimeFeatureFlags = ['edge_functions_bootstrap_failure_mode', 'edge_functions_bootstrap_populate_environment']
const protocol = settings.https ? 'https' : 'http'
const buildFeatureFlags = { ...getFeatureFlagsFromSiteInfo(siteInfo), edge_functions_npm_modules: true }

// Initializes the server, bootstrapping the Deno CLI and downloading it from
// the network if needed. We don't want to wait for that to be completed, or
Expand Down Expand Up @@ -268,6 +267,7 @@ const prepareServer = async ({
debug,
directories: [directory].filter(Boolean),
env: configEnv,
featureFlags,
getUpdatedConfig,
importMapFromTOML: config.functions['*'].deno_import_map,
projectDir,
Expand Down
14 changes: 9 additions & 5 deletions src/lib/edge-functions/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
watchDebounced,
isNodeError,
} from '../../utils/command-helpers.js'
import type { FeatureFlags } from '../../utils/feature-flags.js'
import { MultiMap } from '../../utils/multimap.js'
import { getPathInProject } from '../settings.js'

Expand All @@ -31,15 +32,14 @@ type RunIsolate = Awaited<ReturnType<typeof import('@netlify/edge-bundler').serv

type ModuleJson = ModuleGraph['modules'][number]

const featureFlags = { edge_functions_correct_order: true }

interface EdgeFunctionsRegistryOptions {
bundler: typeof import('@netlify/edge-bundler')
config: Config
configPath: string
debug: boolean
directories: string[]
env: Record<string, { sources: string[]; value: string }>
featureFlags: FeatureFlags
getUpdatedConfig: () => Promise<Config>
projectDir: string
runIsolate: RunIsolate
Expand Down Expand Up @@ -80,10 +80,11 @@ function traverseLocalDependencies(
}

export class EdgeFunctionsRegistry {
public importMapFromDeployConfig?: string

private buildError: Error | null = null
private bundler: typeof import('@netlify/edge-bundler')
private configPath: string
public importMapFromDeployConfig?: string
private importMapFromTOML?: string
private declarationsFromDeployConfig: Declaration[] = []
private declarationsFromTOML: Declaration[]
Expand All @@ -94,6 +95,7 @@ export class EdgeFunctionsRegistry {
private directories: string[]
private directoryWatchers = new Map<string, import('chokidar').FSWatcher>()
private env: Record<string, string>
private featureFlags: FeatureFlags

private userFunctions: EdgeFunction[] = []
private internalFunctions: EdgeFunction[] = []
Expand All @@ -117,6 +119,7 @@ export class EdgeFunctionsRegistry {
configPath,
directories,
env,
featureFlags,
getUpdatedConfig,
importMapFromTOML,
projectDir,
Expand All @@ -126,6 +129,7 @@ export class EdgeFunctionsRegistry {
this.bundler = bundler
this.configPath = configPath
this.directories = directories
this.featureFlags = featureFlags
this.getUpdatedConfig = getUpdatedConfig
this.runIsolate = runIsolate
this.servePath = servePath
Expand Down Expand Up @@ -240,14 +244,14 @@ export class EdgeFunctionsRegistry {
userFunctionConfigs,
internalFunctionConfigs,
this.declarationsFromDeployConfig,
featureFlags,
this.featureFlags,
)
const { declarationsWithoutFunction, manifest, unroutedFunctions } = this.bundler.generateManifest({
declarations,
userFunctionConfig: userFunctionConfigs,
internalFunctionConfig: internalFunctionConfigs,
functions: this.functions,
featureFlags,
featureFlags: this.featureFlags,
})
const routes = [...manifest.routes, ...manifest.post_cache_routes].map((route) => ({
...route,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/functions/runtimes/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const workerURL = new URL('worker.js', import.meta.url)

// @ts-expect-error TS(7031) FIXME: Binding element 'context' implicitly has an 'any' ... Remove this comment to see the full error message
export const invokeFunction = async ({ context, environment, event, func, timeout }) => {
if (func.buildData.runtimeAPIVersion !== 2) {
if (func.buildData?.runtimeAPIVersion !== 2) {
return await invokeFunctionDirectly({ context, event, func, timeout })
}

Expand Down
4 changes: 3 additions & 1 deletion src/utils/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export const isFeatureFlagEnabled = (flagName: string, siteInfo): boolean =>
*/
export const getFeatureFlagsFromSiteInfo = (siteInfo: {
feature_flags?: Record<string, boolean | string | number>
}): Record<string, boolean | string | number> => ({
}): FeatureFlags => ({
...(siteInfo.feature_flags || {}),
// see https://github.com/netlify/pod-dev-foundations/issues/581#issuecomment-1731022753
zisi_golang_use_al2: isFeatureFlagEnabled('cli_golang_use_al2', siteInfo),
})

export type FeatureFlags = Record<string, boolean | string | number>

2 comments on commit b862c9e

@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,294
  • Package size: 307 MB
  • Number of ts-expect-error directives: 1,179

@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,294
  • Package size: 307 MB
  • Number of ts-expect-error directives: 1,179

Please sign in to comment.