-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Rust functions runtime (#3037)
- Loading branch information
1 parent
9f8d7a4
commit 9248675
Showing
7 changed files
with
205 additions
and
115 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const { dirname, extname, join, resolve } = require('path') | ||
const { platform } = require('process') | ||
|
||
const execa = require('execa') | ||
const findUp = require('find-up') | ||
const toml = require('toml') | ||
|
||
const isWindows = platform === 'win32' | ||
|
||
const { readFileAsync } = require('../../../fs') | ||
const { getPathInProject } = require('../../../settings') | ||
const { runFunctionsProxy } = require('../../local-proxy') | ||
|
||
const build = async ({ func }) => { | ||
const functionDirectory = dirname(func.mainFile) | ||
const cacheDirectory = resolve(getPathInProject(['functions-serve'])) | ||
const targetDirectory = join(cacheDirectory, func.name) | ||
const crateName = await getCrateName(functionDirectory) | ||
const binaryName = `${crateName}${isWindows ? '.exe' : ''}` | ||
const binaryPath = join(targetDirectory, 'debug', binaryName) | ||
|
||
await execa('cargo', ['build', '--target-dir', targetDirectory], { | ||
cwd: functionDirectory, | ||
}) | ||
|
||
return { | ||
binaryPath, | ||
srcFiles: [functionDirectory], | ||
} | ||
} | ||
|
||
const getBuildFunction = | ||
({ func }) => | ||
() => | ||
build({ func }) | ||
|
||
const getCrateName = async (cwd) => { | ||
const manifestPath = await findUp('Cargo.toml', { cwd, type: 'file' }) | ||
const manifest = await readFileAsync(manifestPath) | ||
const { package } = toml.parse(manifest) | ||
|
||
return package.name | ||
} | ||
|
||
const invokeFunction = async ({ context, event, func, timeout }) => { | ||
const { stdout } = await runFunctionsProxy({ | ||
binaryPath: func.buildData.binaryPath, | ||
context, | ||
directory: dirname(func.mainFile), | ||
event, | ||
name: func.name, | ||
timeout, | ||
}) | ||
|
||
try { | ||
const { body, headers, statusCode } = JSON.parse(stdout) | ||
|
||
return { | ||
body, | ||
headers, | ||
statusCode, | ||
} | ||
} catch (error) { | ||
return { | ||
statusCode: 500, | ||
} | ||
} | ||
} | ||
|
||
const onRegister = (func) => { | ||
const isSource = extname(func.mainFile) === '.rs' | ||
|
||
return isSource ? func : null | ||
} | ||
|
||
module.exports = { getBuildFunction, invokeFunction, name: 'rs', onRegister } |
9248675
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📊 Benchmark results
Package size: 331 MB