Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo Adapter and Custom Port fixes #248

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/bump-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Bump version
on:
push:
branches:
- develop
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Bump version and push tag
id: tag_version
uses: mathieudutour/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Create a GitHub release
uses: ncipollo/release-action@v1
with:
tag: ${{ steps.tag_version.outputs.new_tag }}
name: Release ${{ steps.tag_version.outputs.new_tag }}
body: ${{ steps.tag_version.outputs.changelog }}

#### https://github.com/marketplace/actions/github-tag
90 changes: 0 additions & 90 deletions .github/workflows/codesee-arch-diagram.yml

This file was deleted.

51 changes: 0 additions & 51 deletions .github/workflows/npm-publish.yml

This file was deleted.

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
20 changes: 14 additions & 6 deletions src/adapters/datasources/datasource-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ const HIGHWATERMARK = 50

const mongodb = require('mongodb')
const { MongoClient } = mongodb
const { Transform, Writable } = require('stream')
const qpm = require('query-params-mongo')
const processQuery = qpm()
const { DataSourceMemory } = require("./datasource-memory")
const { Transform, Writable } = require("stream")
const qpm = require("query-params-mongo")
const processQuery = qpm({
autoDetect: [
{ valuePattern: /^null$/i, dataType: 'nullstring' }
],
converters: {
nullstring: val=>{ return { $type: 10 } } // reference BSON datatypes https://www.mongodb.com/docs/manual/reference/bson-types/
}
})

const url = process.env.MONGODB_URL || 'mongodb://localhost:27017'
const configRoot = require('../../config').hostConfig
const url = process.env.MONGODB_URL || "mongodb://localhost:27017"
const configRoot = require("../../config").hostConfig
const dsOptions = configRoot.adapters.datasources.DataSourceMongoDb.options || {
runOffline: true,
numConns: 2
Expand Down Expand Up @@ -238,7 +246,7 @@ export class DataSourceMongoDb extends DataSource {

processOptions (param) {
const { options = {}, query = {} } = param
return { ...options, ...processQuery(query) }
return { ...processQuery(query), ...options } // options must overwite the query not otherwise
}

/**
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
77 changes: 63 additions & 14 deletions src/domain/use-cases/invoke-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,46 @@ 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 {
const { 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 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');
const [ [ portName ] ] = Object.entries(specPorts).filter((port) => port[1].path === path);
if(!portName) {
throw new Error('no port specified');
}
if(!service[portName]) {
throw new Error('no port found');
for(const p of Object.entries(specPorts)) {
if(!p[1].path) {
continue;
}
if (pathsMatch(p[1].path, path)) {
port = p[0];
break;
}
}
}

if(!port) {
throw new Error('the port is undefined')
}
if(!service[port]) {
throw new Error('the port or record ID is invalid')
}

return await service[portName](input);
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)
Expand All @@ -68,4 +81,40 @@ export default function makeInvokePort ({
}
}
}
}

// Performant way of checking if paths are the same
// given one path with params and one with the param pattern
// this accounts for route params
// since a route param can be anything, we can just compare
/// each path segment skipping over the param field
function pathsMatch(pathWithParamRegex, pathWithParams) {
const splitPathWithParams = pathWithParams.split('/');
const splitPathWithParamRegex = pathWithParamRegex.split('/');

// We know if the length is different, the paths are different
if(splitPathWithParams.length !== splitPathWithParamRegex.length) {
return false;
}

// we loop through the path with params and check if the path with param regex and the called path match
// if they do not match, we return false
// if we get to a segment with a route param we continue
// if we get to the end of the loop and all segments match, we return true
for (let index = 0; index < splitPathWithParams.length; index++) {
const param = splitPathWithParams[index];
const paramRegex = splitPathWithParamRegex[index];

// regex path includes colon meaning route param so we continue
if(paramRegex.includes(':')) {
continue;
}

// if not equal, we return false the paths don't match
if(param !== paramRegex) {
return false;
}
}

return true;
}