Comparing version 1.7.5 to 1.8.0
@@ -5,2 +5,8 @@ # Changelog | ||
## [1.8.0][] - 2021-07-07 | ||
- Add http hooks with custom method names | ||
- Fix Metacom typings | ||
- Improve url parsing | ||
## [1.7.5][] - 2021-07-01 | ||
@@ -114,3 +120,4 @@ | ||
[unreleased]: https://github.com/metarhia/metacom/compare/v1.7.5...HEAD | ||
[unreleased]: https://github.com/metarhia/metacom/compare/v1.8.0...HEAD | ||
[1.8.0]: https://github.com/metarhia/metacom/compare/v1.7.5...v1.8.0 | ||
[1.7.5]: https://github.com/metarhia/metacom/compare/v1.7.4...v1.7.5 | ||
@@ -117,0 +124,0 @@ [1.7.4]: https://github.com/metarhia/metacom/compare/v1.7.3...v1.7.4 |
@@ -230,2 +230,23 @@ 'use strict'; | ||
async hook(router, interfaceName, methodName, args) { | ||
const { application, client, res } = this; | ||
const callId = -1; | ||
if (!router) { | ||
this.error(404, null, callId); | ||
return; | ||
} | ||
const context = { client }; | ||
let result = null; | ||
try { | ||
result = await router.invoke(context, { method: methodName, args }); | ||
} catch (err) { | ||
this.error(500, err, callId); | ||
} | ||
const data = JSON.stringify(result); | ||
res.writeHead(200, { 'Content-Type': MIME_TYPES.json, ...HEADERS }); | ||
res.end(data); | ||
const record = `${this.ip}\t${interfaceName}/${methodName}`; | ||
application.console.log(record); | ||
} | ||
reply(callId, result) { | ||
@@ -232,0 +253,0 @@ const { res, connection } = this; |
@@ -23,2 +23,10 @@ 'use strict'; | ||
const split = (s, separator) => { | ||
const i = s.indexOf(separator); | ||
if (i < 0) return [s, '']; | ||
return [s.slice(0, i), s.slice(i + separator.length)]; | ||
}; | ||
const parseParams = (params) => Object.fromEntries(new URLSearchParams(params)); | ||
class Server { | ||
@@ -130,6 +138,9 @@ constructor(config, application) { | ||
body.then((data) => { | ||
const { pathname, searchParams } = new URL('http://' + req.url); | ||
const [, interfaceName, methodName] = pathname.split('/'); | ||
const args = data ? JSON.parse(data) : Object.fromEntries(searchParams); | ||
channel.rpc(-1, interfaceName, methodName, args); | ||
const pathname = req.url.slice('/api/'.length); | ||
const [path, params] = split(pathname, '?'); | ||
const args = data ? JSON.parse(data) : parseParams(params); | ||
const [interfaceName, methodName] = split(path, '/'); | ||
const hook = this.application.getHook(interfaceName); | ||
if (hook) channel.hook(hook.router, interfaceName, methodName, args); | ||
else channel.rpc(-1, interfaceName, methodName, args); | ||
}); | ||
@@ -136,0 +147,0 @@ } |
{ | ||
"name": "metacom", | ||
"version": "1.7.5", | ||
"version": "1.8.0", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -57,8 +57,8 @@ "description": "Communication protocol for Metarhia stack with rpc, events, binary streams, memory and db access", | ||
"metautil": "^3.5.4", | ||
"ws": "^7.5.1" | ||
"ws": "^7.5.2" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^15.14.0", | ||
"@types/ws": "^7.4.5", | ||
"eslint": "^7.29.0", | ||
"@types/node": "^16.0.1", | ||
"@types/ws": "^7.4.6", | ||
"eslint": "^7.30.0", | ||
"eslint-config-metarhia": "^7.0.1", | ||
@@ -65,0 +65,0 @@ "eslint-config-prettier": "^8.3.0", |
@@ -21,4 +21,5 @@ # Metacom Communication Protocol for Metarhia | ||
// Open connection (both platforms) and make calls | ||
const metacom = new Metacom('https://domainname.com:8000'); | ||
const metacom = Metacom.create('https://domainname.com:8000'); | ||
(async () => { | ||
const { api } = metacom; | ||
try { | ||
@@ -25,0 +26,0 @@ await metacom.load('auth'); // Load `auth` interface |
@@ -16,3 +16,3 @@ import { EventEmitter } from 'events'; | ||
ready(): Promise<void>; | ||
load(interfaces: Array<string>): Promise<void>; | ||
load(...interfaces: Array<string>): Promise<void>; | ||
httpCall( | ||
@@ -19,0 +19,0 @@ iname: string, |
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
34614
841
34
Updatedws@^7.5.2