Comparing version 2.0.7 to 2.0.8
@@ -5,2 +5,9 @@ # Changelog | ||
## [2.0.8][] - 2021-02-17 | ||
- Use schema validation in `application.invoke` | ||
- Optimize module loader and signature cache | ||
- Reworked application api in-memory structure | ||
- Precompile api validation schemas | ||
## [2.0.7][] - 2021-02-13 | ||
@@ -77,3 +84,4 @@ | ||
[unreleased]: https://github.com/metarhia/impress/compare/v2.0.7...HEAD | ||
[unreleased]: https://github.com/metarhia/impress/compare/v2.0.8...HEAD | ||
[2.0.8]: https://github.com/metarhia/impress/compare/v2.0.7...v2.0.8 | ||
[2.0.7]: https://github.com/metarhia/impress/compare/v2.0.6...v2.0.7 | ||
@@ -80,0 +88,0 @@ [2.0.6]: https://github.com/metarhia/impress/compare/v2.0.5...v2.0.6 |
@@ -6,2 +6,3 @@ 'use strict'; | ||
const { metautil, metavm } = metarhia; | ||
const { Schema } = require('./schema.js'); | ||
@@ -21,2 +22,9 @@ const EMPTY_CONTEXT = Object.freeze({}); | ||
const getSignature = (method) => { | ||
const src = method.toString(); | ||
const signature = metautil.between(src, '({', '})'); | ||
if (signature === '') return []; | ||
return signature.split(',').map((s) => s.trim()); | ||
}; | ||
class Application extends events.EventEmitter { | ||
@@ -110,14 +118,23 @@ constructor() { | ||
getMethod(iname, ver, methodName, context) { | ||
getMethod(iname, ver, methodName) { | ||
const iface = this.api[iname]; | ||
if (!iface) return null; | ||
const version = ver === '*' ? iface.default : parseInt(ver); | ||
const version = ver === '*' ? iface.default : parseInt(ver, 10); | ||
const methods = iface[version.toString()]; | ||
if (!methods) return null; | ||
const method = methods[methodName]; | ||
if (!method) return null; | ||
const exp = method(context); | ||
return typeof exp === 'object' ? exp : { access: 'logged', method: exp }; | ||
const proc = methods[methodName]; | ||
return proc; | ||
} | ||
preprocessProc(script) { | ||
const exp = script(EMPTY_CONTEXT); | ||
if (typeof exp === 'object') { | ||
let { parameters, returns } = exp; | ||
if (parameters) parameters = Schema.from(parameters); | ||
if (returns) returns = Schema.from(returns); | ||
return { ...exp, parameters, returns, script }; | ||
} | ||
return { method: exp, script }; | ||
} | ||
async loadMethod(fileName) { | ||
@@ -133,2 +150,3 @@ const rel = fileName.substring(this.apiPath.length + 1); | ||
if (!script) return; | ||
const proc = this.preprocessProc(script); | ||
let iface = this.api[iname]; | ||
@@ -141,28 +159,19 @@ const { api } = this.sandbox; | ||
} | ||
if (version > iface.default) iface.default = version; | ||
let methods = iface[ver]; | ||
if (!methods) iface[ver] = methods = {}; | ||
methods[name] = script; | ||
const exp = script(EMPTY_CONTEXT); | ||
internalInterface[name] = exp; | ||
this.cacheSignature(iname + '.' + version, name, exp); | ||
if (version > iface.default) iface.default = version; | ||
const { method } = proc; | ||
methods[name] = proc; | ||
internalInterface[name] = method; | ||
this.cacheSignature(iname + '.' + version, name, method); | ||
} | ||
cacheSignature(interfaceName, methodName, exp) { | ||
cacheSignature(interfaceName, methodName, method) { | ||
let interfaceMethods = this.signatures[interfaceName]; | ||
if (!interfaceMethods) { | ||
interfaceMethods = {}; | ||
this.signatures[interfaceName] = interfaceMethods; | ||
this.signatures[interfaceName] = interfaceMethods = {}; | ||
} | ||
interfaceMethods[methodName] = this.getSignature(exp); | ||
interfaceMethods[methodName] = getSignature(method); | ||
} | ||
getSignature(exp) { | ||
const fn = typeof exp === 'object' ? exp.method : exp; | ||
const src = fn.toString(); | ||
const signature = metautil.between(src, '({', '})'); | ||
if (signature === '') return []; | ||
return signature.split(',').map((s) => s.trim()); | ||
} | ||
addModule(namespaces, exports, iface) { | ||
@@ -212,8 +221,22 @@ let level = this.sandbox; | ||
async execute(fn) { | ||
try { | ||
await fn(); | ||
} catch (err) { | ||
execute(method) { | ||
return method().catch((err) => { | ||
this.console.error(err.stack); | ||
}); | ||
} | ||
async invoke(context, proc, args = {}) { | ||
const exp = proc.script(context); | ||
const method = typeof exp === 'object' ? exp.method : exp; | ||
if (proc.parameters) { | ||
const { valid } = proc.parameters.check(args); | ||
if (!valid) return new Error('Invalid parameter type'); | ||
} | ||
const result = await method(args); | ||
if (result instanceof this.Error) return result; | ||
if (proc.returns) { | ||
const { valid } = proc.returns.check(result); | ||
if (!valid) return new Error('Invalid result type'); | ||
} | ||
return result; | ||
} | ||
@@ -220,0 +243,0 @@ |
{ | ||
"name": "impress", | ||
"version": "2.0.7", | ||
"version": "2.0.8", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -62,3 +62,3 @@ "description": "Impress application server for Node.js and Metarhia private cloud", | ||
"@metarhia/config": "^2.1.0", | ||
"metacom": "^1.3.1", | ||
"metacom": "^1.4.0", | ||
"metalog": "^3.1.0", | ||
@@ -69,3 +69,3 @@ "metautil": "^3.2.0", | ||
"devDependencies": { | ||
"eslint": "^7.19.0", | ||
"eslint": "^7.20.0", | ||
"eslint-config-metarhia": "^7.0.1", | ||
@@ -72,0 +72,0 @@ "eslint-config-prettier": "^7.2.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35047
713
Updatedmetacom@^1.4.0