-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to expose functions on a public domain
- Loading branch information
Showing
8 changed files
with
161 additions
and
94 deletions.
There are no files selected for viewing
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,16 @@ | ||
const Router = require('express').Router; | ||
const bodyParser = require('body-parser'); | ||
|
||
const functionRunHandler = require('./functionRunHandler'); | ||
|
||
const router = new Router(); | ||
const { bodyParserLimit } = require('../../support/config'); | ||
|
||
|
||
router.all( | ||
'/:namespace/:id', | ||
bodyParser.json({ limit: bodyParserLimit }), | ||
(req, res) => functionRunHandler(req, res, { exposed: true }) | ||
); | ||
|
||
module.exports = router; |
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,96 @@ | ||
const ErrorTracker = require('../../domain/ErrorTracker'); | ||
const Metric = require('../../domain/Metric'); | ||
const { DefaultLogStorage } = require('../../domain/LogStorage'); | ||
|
||
|
||
function codeFileName(namespace, codeId) { | ||
return `${namespace}/${codeId}.js`; | ||
} | ||
|
||
|
||
async function functionRunHandler(req, res, { exposed }) { | ||
const { namespace, id } = req.params; | ||
const memoryStorage = req.app.get('memoryStorage'); | ||
const sandbox = req.app.get('sandbox'); | ||
const filename = codeFileName(namespace, id); | ||
const metric = new Metric('function-run'); | ||
const logStorage = new DefaultLogStorage(namespace, id, req); | ||
|
||
let code; | ||
|
||
try { | ||
code = await memoryStorage.getCodeByCache(namespace, id, { | ||
preCache: (preCode) => { | ||
preCode.script = sandbox.compileCode(filename, preCode.code); | ||
return preCode; | ||
}, | ||
}); | ||
|
||
if (!code) { | ||
const error = new Error(`Code '${namespace}/${id}' is not found`); | ||
error.statusCode = 404; | ||
throw error; | ||
} | ||
if (exposed && !code.exposed) { | ||
const error = new Error('Unauthorized'); | ||
error.statusCode = 403; | ||
throw error; | ||
} | ||
} catch (err) { | ||
res.status(err.statusCode || 500).json({ error: err.message }); | ||
return; | ||
} | ||
|
||
try { | ||
const options = { | ||
console: logStorage.console, | ||
env: code.env, | ||
}; | ||
const result = await sandbox.runScript(code.script, req, options); | ||
|
||
res.set(result.headers); | ||
res.status(result.status); | ||
res.json(result.body); | ||
|
||
const spent = metric.finish({ | ||
filename, | ||
status: result.status, | ||
}); | ||
|
||
logStorage.flush({ | ||
status: result.status, | ||
requestTime: spent, | ||
}); | ||
} catch (err) { | ||
logStorage.console.error(`Failed to run function: ${err}`); | ||
logStorage.console.error(err.stack); | ||
const status = err.statusCode || 500; | ||
res.status(status).json({ error: err.message }); | ||
|
||
const spent = metric.finish({ | ||
filename, | ||
status, | ||
error: err.message, | ||
}); | ||
|
||
const logResult = logStorage.flush({ | ||
status, | ||
requestTime: spent, | ||
}); | ||
|
||
const { namespaceSettings } = code; | ||
const { sentryDSN } = namespaceSettings || {}; | ||
|
||
const extra = Object.assign({ body: req.body }, logResult || {}); | ||
const errTracker = new ErrorTracker({ | ||
sentryDSN, | ||
filename, | ||
extra, | ||
tags: { codeHash: code.hash }, | ||
code: code.code, | ||
}); | ||
errTracker.notify(err); | ||
} | ||
} | ||
|
||
module.exports = functionRunHandler; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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