Skip to content

Commit

Permalink
feat: improvements to V2 functions (#6047)
Browse files Browse the repository at this point in the history
* feat: improvements to V2 functions

* chore: fix test

* chore: fix test

* refactor: put Lambda compat log behind flag

* refactor: update link
  • Loading branch information
eduardoboucas authored Oct 10, 2023
1 parent 970b3df commit 7d96cde
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 46 deletions.
45 changes: 44 additions & 1 deletion src/lib/functions/netlify-function.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check
import { basename, extname } from 'path'
import { version as nodeVersion } from 'process'

import CronParser from 'cron-parser'
Expand All @@ -7,6 +8,7 @@ import semver from 'semver'
import { error as errorExit } from '../../utils/command-helpers.mjs'
import { BACKGROUND } from '../../utils/functions/get-functions.mjs'

const TYPESCRIPT_EXTENSIONS = new Set(['.cts', '.mts', '.ts'])
const V2_MIN_NODE_VERSION = '18.0.0'

// Returns a new set with all elements of `setA` that don't exist in `setB`.
Expand Down Expand Up @@ -57,6 +59,35 @@ export default class NetlifyFunction {
this.srcFiles = new Set()
}

get filename() {
if (!this.buildData?.mainFile) {
return null
}

return basename(this.buildData.mainFile)
}

getRecommendedExtension() {
if (this.buildData?.runtimeAPIVersion !== 2) {
return
}

const extension = this.buildData?.mainFile ? extname(this.buildData.mainFile) : undefined
const moduleFormat = this.buildData?.outputModuleFormat

if (moduleFormat === 'esm') {
return
}

if (extension === '.ts') {
return '.mts'
}

if (extension === '.js') {
return '.mjs'
}
}

hasValidName() {
// same as https://github.com/netlify/bitballoon/blob/fbd7881e6c8e8c48e7a0145da4ee26090c794108/app/models/deploy.rb#L482
// eslint-disable-next-line unicorn/better-regex
Expand All @@ -73,6 +104,14 @@ export default class NetlifyFunction {
return !(this.buildData?.runtimeAPIVersion === 2 && semver.lt(nodeVersion, V2_MIN_NODE_VERSION))
}

isTypeScript() {
if (this.filename === null) {
return false
}

return TYPESCRIPT_EXTENSIONS.has(extname(this.filename))
}

async getNextRun() {
if (!(await this.isScheduled())) {
return null
Expand Down Expand Up @@ -111,7 +150,7 @@ export default class NetlifyFunction {
throw new Error(
`Function requires Node.js version ${V2_MIN_NODE_VERSION} or above, but ${nodeVersion.slice(
1,
)} is installed. Refer to https://ntl.fyi/functions-node18 for information on how to update.`,
)} is installed. Refer to https://ntl.fyi/functions-runtime for information on how to update.`,
)
}

Expand Down Expand Up @@ -195,6 +234,10 @@ export default class NetlifyFunction {
})
}

get runtimeAPIVersion() {
return this.buildData?.runtimeAPIVersion ?? 1
}

get url() {
// This line fixes the issue here https://github.com/netlify/cli/issues/4116
// Not sure why `settings.port` was used here nor does a valid reference exist.
Expand Down
Loading

2 comments on commit 7d96cde

@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,386
  • Package size: 389 MB

@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,386
  • Package size: 389 MB

Please sign in to comment.