Skip to content

Commit

Permalink
Port method and path fixes - can overwrite autogenerated routes - sti…
Browse files Browse the repository at this point in the history
…cks to its http methods (#6)

* added method & headers to apply/improve port spec

* limit service ports to their portSpec http methods

* make a port path validation

* replace the port path only if methods match

* execute port only if path is provided and matches

* fix priorities, custom over autogenerated

* execution speed fix - includes over new Regexp
  • Loading branch information
vi-ssc authored Nov 29, 2022
1 parent 4b2c47e commit 9a73dc9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/adapters/controllers/post-invoke-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export default function anyInvokePortFactory (invokePort) {
const result = await invokePort({
port: httpRequest.params.port,
args: httpRequest.body,
method: httpRequest.method,
headers: httpRequest.headers,
path: httpRequest.path,
id: httpRequest.params.id || null
})

Expand Down
8 changes: 5 additions & 3 deletions src/aegis.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ const router = {
if (ctrl.ports) {
for (const portName in ctrl.ports) {
const port = ctrl.ports[portName]
const specPortMethods = (port?.methods || "").join('|')

if (port.path) {
routeOverrides.set(port.path, portName)
}

if (checkAllowedMethods(ctrl, method)) {
if (checkAllowedMethods(ctrl, method) && (!port.methods || (specPortMethods.includes(method.toLowerCase()))) ) {
routes.set(port.path || path(ctrl.endpoint), {
[method]: adapter(ctrl.fn)
})
Expand Down Expand Up @@ -131,6 +133,8 @@ const router = {
}

function makeRoutes () {
router.adminRoute(getConfig, http)
router.userRoutes(getRoutes)
router.autoRoutes(endpoint, 'get', liveUpdate, http)
router.autoRoutes(endpoint, 'get', getModels, http)
router.autoRoutes(endpoint, 'post', postModels, http)
Expand All @@ -146,8 +150,6 @@ function makeRoutes () {
router.autoRoutes(endpointPortId, 'patch', anyInvokePorts, http, true)
router.autoRoutes(endpointPortId, 'delete', anyInvokePorts, http, true)
router.autoRoutes(endpointPortId, 'get', anyInvokePorts, http, true)
router.adminRoute(getConfig, http)
router.userRoutes(getRoutes)
console.log(routes)
}

Expand Down
15 changes: 11 additions & 4 deletions src/domain/use-cases/invoke-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,22 @@ export default function makeInvokePort ({
}
/**
*
* @param {{id:string,model:import('..').Model,args:string[],port:string}} input
* @param {{id:String,model:import('..').Model,args:String[],port:String,method:String,headers:Array}} input
* @returns
*/
return async function invokePort (input) {
if (isMainThread) {
return threadpool.runJob(invokePort.name, input, modelName)
} else {
try {
let { id = null, port = null} = input;
let { id = null, port = null, method, path } = input;
const service = await findModelService(id)
if (!service) {
throw new Error('could not find a service associated with given id')
}

const specPorts = service.getPorts();
if(!port) {
const specPorts = service.getPorts();
const path = context['requestContext'].getStore().get('path');
for(const p of Object.entries(specPorts)) {
if(!p[1].path) {
continue;
Expand All @@ -68,6 +67,14 @@ export default function makeInvokePort ({
throw new Error('the port or record ID is invalid')
}

const specPortMethods = specPorts[port]?.methods.join('|').toLowerCase();
if (specPortMethods && !(specPortMethods.includes(method.toLowerCase()))) {
throw new Error('invalid method for given port');
}
if (specPorts[port]?.path && !pathsMatch(specPorts[port].path, path)) {
throw new Error('invalid path for given port');
}

return await service[port](input)
} catch (error) {
return AppError(error)
Expand Down

0 comments on commit 9a73dc9

Please sign in to comment.