Comparing version 1.4.0 to 1.5.0
@@ -5,2 +5,8 @@ # Changelog | ||
## [1.5.0][] - 2021-02-19 | ||
- Move Semaphore and timeout to metautil | ||
- Decompose Channel.prototype.rpc | ||
- Use new impress class Procedure | ||
## [1.4.0][] - 2021-02-17 | ||
@@ -50,3 +56,4 @@ | ||
[unreleased]: https://github.com/metarhia/metacom/compare/v1.4.0...HEAD | ||
[unreleased]: https://github.com/metarhia/metacom/compare/v1.5.0...HEAD | ||
[1.5.0]: https://github.com/metarhia/metacom/compare/v1.4.0...v1.5.0 | ||
[1.4.0]: https://github.com/metarhia/metacom/compare/v1.3.1...v1.4.0 | ||
@@ -53,0 +60,0 @@ [1.3.1]: https://github.com/metarhia/metacom/compare/v1.3.0...v1.3.1 |
@@ -146,3 +146,3 @@ 'use strict'; | ||
error(code, err, callId) { | ||
error(code, err = null, callId) { | ||
const { req, res, connection, ip, application } = this; | ||
@@ -188,6 +188,11 @@ const { url, method } = req; | ||
async rpc(callId, interfaceName, methodName, args) { | ||
const { res, connection, application, session, client } = this; | ||
const { semaphore } = application.server; | ||
const { application, session, client } = this; | ||
const [iname, ver = '*'] = interfaceName.split('.'); | ||
const proc = application.getMethod(iname, ver, methodName); | ||
if (!proc) { | ||
this.error(404, null, callId); | ||
return; | ||
} | ||
try { | ||
await semaphore.enter(); | ||
await proc.enter(); | ||
} catch { | ||
@@ -197,42 +202,41 @@ this.error(504, null, callId); | ||
} | ||
const [iname, ver = '*'] = interfaceName.split('.'); | ||
const context = session ? session.context : { client }; | ||
const publicMethod = proc.access === 'public'; | ||
if (!this.session && !publicMethod) { | ||
this.error(403, null, callId); | ||
return; | ||
} | ||
let result = null; | ||
try { | ||
const context = session ? session.context : { client }; | ||
const proc = application.getMethod(iname, ver, methodName); | ||
if (!proc) { | ||
this.error(404, null, callId); | ||
return; | ||
} | ||
if (!this.session && proc.access !== 'public') { | ||
this.error(403, null, callId); | ||
return; | ||
} | ||
const result = await application.invoke(context, proc, args); | ||
if (result instanceof Error) { | ||
this.error(result.code, result, callId); | ||
return; | ||
} | ||
const id = result ? result.systemUserId : 0; | ||
if (!this.session && id && proc.access === 'public') { | ||
this.session = application.auth.startSession(this, id); | ||
result.token = this.token; | ||
} | ||
const data = JSON.stringify({ callback: callId, result }); | ||
if (connection) { | ||
connection.send(data); | ||
} else { | ||
res.writeHead(200, { 'Content-Type': MIME_TYPES.json, ...HEADERS }); | ||
res.end(data); | ||
} | ||
const { ip, token } = this; | ||
const who = id > 0 ? id : token; | ||
const record = `${ip}\t${who}\t${interfaceName}/${methodName}`; | ||
application.console.log(record); | ||
result = await proc.invoke(context, args); | ||
} catch (err) { | ||
this.error(500, err, callId); | ||
} finally { | ||
semaphore.leave(); | ||
proc.leave(); | ||
} | ||
this.reply(callId, result, publicMethod); | ||
const record = `${this.ip}\t${interfaceName}/${methodName}`; | ||
application.console.log(record); | ||
} | ||
reply(callId, result, publicMethod) { | ||
const { res, connection, application } = this; | ||
if (result instanceof Error) { | ||
this.error(result.code, result, callId); | ||
return; | ||
} | ||
const id = result ? result.systemUserId : 0; | ||
if (!this.session && id && publicMethod) { | ||
this.session = application.auth.startSession(this, id); | ||
result.token = this.token; | ||
} | ||
const data = JSON.stringify({ callback: callId, result }); | ||
if (connection) { | ||
connection.send(data); | ||
} else { | ||
res.writeHead(200, { 'Content-Type': MIME_TYPES.json, ...HEADERS }); | ||
res.end(data); | ||
} | ||
} | ||
startSession() { | ||
@@ -239,0 +243,0 @@ const token = this.generateToken(); |
@@ -8,5 +8,5 @@ 'use strict'; | ||
const metautil = require('metautil'); | ||
const { Semaphore } = metautil; | ||
const ws = require('ws'); | ||
const { Semaphore } = require('./semaphore.js'); | ||
const { Channel, channels } = require('./channel.js'); | ||
@@ -18,7 +18,2 @@ | ||
const timeout = (msec) => | ||
new Promise((resolve) => { | ||
setTimeout(resolve, msec); | ||
}); | ||
const receiveBody = async (req) => { | ||
@@ -137,6 +132,6 @@ const buffers = []; | ||
if (channels.size === 0) { | ||
await timeout(SHORT_TIMEOUT); | ||
await metautil.timeout(SHORT_TIMEOUT); | ||
return; | ||
} | ||
await timeout(SHUTDOWN_TIMEOUT); | ||
await metautil.timeout(SHUTDOWN_TIMEOUT); | ||
this.closeChannels(); | ||
@@ -143,0 +138,0 @@ } |
@@ -6,3 +6,2 @@ 'use strict'; | ||
const { Channel } = require('./lib/channel.js'); | ||
const { Semaphore } = require('./lib/semaphore.js'); | ||
@@ -12,2 +11,1 @@ module.exports = Metacom; | ||
module.exports.Channel = Channel; | ||
module.exports.Semaphore = Semaphore; |
{ | ||
"name": "metacom", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -53,3 +53,3 @@ "description": "Communication protocol for Metarhia stack with rpc, events, binary streams, memory and db access", | ||
"dependencies": { | ||
"metautil": "^3.2.0", | ||
"metautil": "^3.3.0", | ||
"ws": "^7.4.3" | ||
@@ -56,0 +56,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
28299
11
718
Updatedmetautil@^3.3.0