Skip to content

Commit

Permalink
chore: refactor netlify-function changes
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Jul 13, 2021
1 parent 0cda84f commit 44ec153
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 58 deletions.
17 changes: 0 additions & 17 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
"@ungap/from-entries": "^0.2.1",
"ansi-styles": "^5.0.0",
"ascii-table": "0.0.9",
"async-mutex": "^0.3.1",
"backoff": "^2.5.0",
"better-opn": "^2.1.1",
"body-parser": "^1.19.0",
Expand Down
66 changes: 26 additions & 40 deletions src/lib/functions/netlify-function.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
const { Mutex, withTimeout } = require('async-mutex')

const { difference } = require('../../utils/difference')

const BACKGROUND_SUFFIX = '-background'

// 3 minutes
const MUTEX_TIMEOUT = 18e4

class NetlifyFunction {
constructor({
config,
Expand Down Expand Up @@ -36,7 +31,6 @@ class NetlifyFunction {
// List of the function's source files. This starts out as an empty set
// and will get populated on every build.
this.srcFiles = new Set()
this.mutex = withTimeout(new Mutex(), MUTEX_TIMEOUT)
}

// The `build` method transforms source files into invocable functions. Its
Expand All @@ -45,29 +39,25 @@ class NetlifyFunction {
// - `srcFilesDiff`: Files that were added and removed since the last time
// the function was built.
async build({ cache }) {
try {
return await this.mutex.runExclusive(async () => {
const buildFunction = await this.runtime.getBuildFunction({
config: this.config,
errorExit: this.errorExit,
func: this,
functionsDirectory: this.functionsDirectory,
projectRoot: this.projectRoot,
})
const buildFunction = await this.runtime.getBuildFunction({
config: this.config,
errorExit: this.errorExit,
func: this,
functionsDirectory: this.functionsDirectory,
projectRoot: this.projectRoot,
})

try {
const { srcFiles, ...buildData } = await buildFunction({ cache })
const srcFilesSet = new Set(srcFiles)
const srcFilesDiff = this.getSrcFilesDiff(srcFilesSet)
this.buildQueue = buildFunction({ cache })

this.buildData = buildData
this.srcFiles = srcFilesSet
try {
const { srcFiles, ...buildData } = await this.buildQueue
const srcFilesSet = new Set(srcFiles)
const srcFilesDiff = this.getSrcFilesDiff(srcFilesSet)

return { srcFilesDiff }
} catch (error) {
return { error }
}
})
this.buildData = buildData
this.srcFiles = srcFilesSet

return { srcFilesDiff }
} catch (error) {
return { error }
}
Expand All @@ -87,22 +77,18 @@ class NetlifyFunction {

// Invokes the function and returns its response object.
async invoke(event, context) {
try {
return await this.mutex.runExclusive(async () => {
const timeout = this.isBackground ? this.timeoutBackground : this.timeoutSynchronous
await this.buildQueue

try {
const result = await this.runtime.invokeFunction({
context,
event,
func: this,
timeout,
})
return { result, error: null }
} catch (error) {
return { result: null, error }
}
const timeout = this.isBackground ? this.timeoutBackground : this.timeoutSynchronous

try {
const result = await this.runtime.invokeFunction({
context,
event,
func: this,
timeout,
})
return { result, error: null }
} catch (error) {
return { result: null, error }
}
Expand Down

0 comments on commit 44ec153

Please sign in to comment.