From 6593efd95a7b750393b79502efe449970c938c75 Mon Sep 17 00:00:00 2001 From: Jesse MacFadyen Date: Wed, 3 Apr 2024 17:53:11 -0700 Subject: [PATCH] handle action that does not export main, formatting, cleanup console logs --- src/lib/bundle-serve.js | 122 +++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 64 deletions(-) diff --git a/src/lib/bundle-serve.js b/src/lib/bundle-serve.js index f4e1132..04779d9 100644 --- a/src/lib/bundle-serve.js +++ b/src/lib/bundle-serve.js @@ -6,7 +6,7 @@ const crypto = require('node:crypto') let actionConfig = null -module.exports = async (bundler, options, log = () => {}, _actionConfig) => { +module.exports = async (bundler, options, log = () => { }, _actionConfig) => { actionConfig = _actionConfig process.env.__OW_API_KEY = process.env.AIO_runtime_auth @@ -21,7 +21,7 @@ module.exports = async (bundler, options, log = () => {}, _actionConfig) => { } try { - let {bundleGraph, buildTime} = await bundler.run() + let { bundleGraph, buildTime } = await bundler.run() let bundles = bundleGraph.getBundles() console.log(`✨ Built ${bundles.length} bundles in ${buildTime}ms!`) } catch (err) { @@ -56,23 +56,13 @@ module.exports = async (bundler, options, log = () => {}, _actionConfig) => { const serveAction = async (req, res, next) => { const url = req.params[0] const [packageName, actionName, ...path] = url.split('/') - - // console.log('packageName is ', packageName) - // console.log('actionName is ', actionName) - // console.log('path is ', path) - // console.log('actionConfig[packageName] is', actionConfig[packageName]) - const action = actionConfig[packageName]?.actions[actionName] - // console.log('action is conductor? ', action?.annotations) if (!action) { // action could be a sequence ... todo: refactor these 2 paths to 1 action runner const sequence = actionConfig[packageName]?.sequences[actionName] if (sequence) { - console.log('sequence be ', sequence) const actions = sequence.actions?.split(',') - console.log('actions are', actions) - const params = { __ow_body: req.body, __ow_headers: req.headers, @@ -95,13 +85,18 @@ const serveAction = async (req, res, next) => { process.env.__OW_ACTIVATION_ID = crypto.randomBytes(16).toString('hex') delete require.cache[action.function] const actionFunction = require(action.function).main - response = await actionFunction(response ?? params) - if (response.statusCode === 404) { - throw response + if (actionFunction) { + response = await actionFunction(response ?? params) + if (response.statusCode === 404) { + throw response + } + } else { + return res + .status(500) + .send({ error: `${actionName} action not found, or does not export main` }) } } } - console.log('response is', response) const headers = response.headers || {} const status = response.statusCode || 200 return res @@ -114,58 +109,57 @@ const serveAction = async (req, res, next) => { .send({ error: 'not found (yet)' }) } } else { - - // check if action is protected - if (action?.annotations?.['require-adobe-auth']) { - console.log('require-adobe-auth is true') - // check if user is authenticated - if (!req.headers.authorization) { - console.log('no authorization header') - return res - .status(401) - .send({ error: 'unauthorized' }) + // check if action is protected + if (action?.annotations?.['require-adobe-auth']) { + // check if user is authenticated + if (!req.headers.authorization) { + return res + .status(401) + .send({ error: 'unauthorized' }) + } } - } - // todo: what can we learn from action.annotations? - // todo: action.include? - // todo: rules, triggers, ... - // generate an activationID just like openwhisk - process.env.__OW_ACTIVATION_ID = crypto.randomBytes(16).toString('hex') - delete require.cache[action.function] - const actionFunction = require(action.function).main - - const params = { - __ow_body: req.body, - __ow_headers: req.headers, - __ow_path: path.join('/'), - __ow_query: req.query, - __ow_method: req.method.toLowerCase(), - ...req.query, - ...action.inputs, - ...(req.is('application/json') ? req.body : {}) - } - params.__ow_headers['x-forwarded-for'] = '127.0.0.1' - console.log('params = ', params) - - if (actionFunction) { - try { - const response = await actionFunction(params) - console.log('response is', response) - const headers = response.headers || {} - const status = response.statusCode || 200 - - return res - .set(headers || {}) - .status(status || 200) - .send(response.body) - } catch (e) { + // todo: what can we learn from action.annotations? + // todo: action.include? + // todo: rules, triggers, ... + // generate an activationID just like openwhisk + process.env.__OW_ACTIVATION_ID = crypto.randomBytes(16).toString('hex') + delete require.cache[action.function] + const actionFunction = require(action.function).main + console.log('actionFunction is', actionFunction) + + const params = { + __ow_body: req.body, + __ow_headers: req.headers, + __ow_path: path.join('/'), + __ow_query: req.query, + __ow_method: req.method.toLowerCase(), + ...req.query, + ...action.inputs, + ...(req.is('application/json') ? req.body : {}) + } + params.__ow_headers['x-forwarded-for'] = '127.0.0.1' + console.log('params = ', params) + + if (actionFunction) { + try { + const response = await actionFunction(params) + const headers = response.headers || {} + const status = response.statusCode || 200 + + return res + .set(headers || {}) + .status(status || 200) + .send(response.body) + } catch (e) { + return res + .status(500) + .send({ error: e.message }) + } + } else { return res .status(500) - .send({ error: e.message }) + .send({ error: `${actionName} action not found, or does not export main` }) } - } else { } - res.send(action) -} }