Skip to content

Commit

Permalink
fix: serve function support in monorepos (#6608)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
mrstork and kodiakhq[bot] authored May 17, 2024
1 parent c82114c commit 51f1cf6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
16 changes: 8 additions & 8 deletions src/lib/functions/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { jwtDecode } from 'jwt-decode'
import type BaseCommand from '../../commands/base-command.js'
import type { $TSFixMe } from '../../commands/types.js'
import { NETLIFYDEVERR, NETLIFYDEVLOG, error as errorExit, log } from '../../utils/command-helpers.js'
import { UNLINKED_SITE_MOCK_ID } from '../../utils/dev.js'
import { isFeatureFlagEnabled } from '../../utils/feature-flags.js'
import {
CLOCKWORK_USERAGENT,
Expand All @@ -22,7 +23,6 @@ import type { BlobsContext } from '../blobs/blobs.js'
import { headers as efHeaders } from '../edge-functions/headers.js'
import { getGeoLocation } from '../geo-location.js'

import { UNLINKED_SITE_MOCK_ID } from '../../utils/dev.js'
import { handleBackgroundFunction, handleBackgroundFunctionResult } from './background.js'
import { createFormSubmissionHandler } from './form-submissions-handler.js'
import { FunctionsRegistry } from './registry.js'
Expand Down Expand Up @@ -56,13 +56,13 @@ const buildClientContext = function (headers) {
const user = jwtDecode(parts[1])

const netlifyContext = JSON.stringify({
identity: identity,
user: user,
identity,
user,
})

return {
identity: identity,
user: user,
identity,
user,
custom: {
netlify: Buffer.from(netlifyContext).toString('base64'),
},
Expand Down Expand Up @@ -320,15 +320,15 @@ export const startFunctionsServer = async (
siteUrl,
timeouts,
} = options
const internalFunctionsDir = await getInternalFunctionsDir({ base: site.root })
const internalFunctionsDir = await getInternalFunctionsDir({ base: site.root, packagePath: command.workspacePackage })
const functionsDirectories: string[] = []
let manifest

// If the `loadDistFunctions` parameter is sent, the functions server will
// use the built functions created by zip-it-and-ship-it rather than building
// them from source.
if (loadDistFunctions) {
const distPath = await getFunctionsDistPath({ base: site.root })
const distPath = await getFunctionsDistPath({ base: site.root, packagePath: command.workspacePackage })

if (distPath) {
functionsDirectories.push(distPath)
Expand All @@ -354,7 +354,7 @@ export const startFunctionsServer = async (
}

try {
const functionsServePath = getFunctionsServePath({ base: site.root })
const functionsServePath = getFunctionsServePath({ base: site.root, packagePath: command.workspacePackage })

await fs.rm(functionsServePath, { force: true, recursive: true })
} catch {
Expand Down
9 changes: 3 additions & 6 deletions src/utils/functions/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,21 @@ export const SERVE_FUNCTIONS_FOLDER = 'functions-serve'
export const getFunctionsDir = ({ config, options }, defaultValue) =>
options.functions || config.dev?.functions || config.functionsDirectory || config.dev?.Functions || defaultValue

// @ts-expect-error TS(7031) FIXME: Binding element 'base' implicitly has an 'any' typ... Remove this comment to see the full error message
export const getFunctionsManifestPath = async ({ base, packagePath = '' }) => {
export const getFunctionsManifestPath = async ({ base, packagePath = '' }: { base: string; packagePath?: string }) => {
const path = resolve(base, packagePath, getPathInProject(['functions', 'manifest.json']))
const isFile = await isFileAsync(path)

return isFile ? path : null
}

// @ts-expect-error TS(7031) FIXME: Binding element 'base' implicitly has an 'any' typ... Remove this comment to see the full error message
export const getFunctionsDistPath = async ({ base, packagePath = '' }) => {
export const getFunctionsDistPath = async ({ base, packagePath = '' }: { base: string; packagePath?: string }) => {
const path = resolve(base, packagePath, getPathInProject(['functions']))
const isDirectory = await isDirectoryAsync(path)

return isDirectory ? path : null
}

// @ts-expect-error TS(7031) FIXME: Binding element 'base' implicitly has an 'any' typ... Remove this comment to see the full error message
export const getFunctionsServePath = ({ base, packagePath = '' }) => {
export const getFunctionsServePath = ({ base, packagePath = '' }: { base: string; packagePath?: string }) => {
const path = resolve(base, packagePath, getPathInProject([SERVE_FUNCTIONS_FOLDER]))

return path
Expand Down

1 comment on commit 51f1cf6

@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,359
  • Package size: 313 MB
  • Number of ts-expect-error directives: 980

Please sign in to comment.