Comparing version 2.0.3 to 2.0.4
@@ -5,2 +5,10 @@ # Changelog | ||
## [2.0.4][] - 2021-10-12 | ||
- Return index.html not only from the root folder | ||
- Fix parse broken JSON packets | ||
- Fix detecting ping packets (empty objects) | ||
- Fix error logging and passing to client | ||
- Validation `call` identifier type | ||
## [2.0.3][] - 2021-09-23 | ||
@@ -158,3 +166,4 @@ | ||
[unreleased]: https://github.com/metarhia/metacom/compare/v2.0.3...HEAD | ||
[unreleased]: https://github.com/metarhia/metacom/compare/v2.0.4...HEAD | ||
[2.0.4]: https://github.com/metarhia/metacom/compare/v2.0.3...v2.0.4 | ||
[2.0.3]: https://github.com/metarhia/metacom/compare/v2.0.2...v2.0.3 | ||
@@ -161,0 +170,0 @@ [2.0.2]: https://github.com/metarhia/metacom/compare/v2.0.1...v2.0.2 |
'use strict'; | ||
const http = require('http'); | ||
const metautil = require('metautil'); | ||
@@ -94,14 +95,13 @@ | ||
if (Buffer.compare(EMPTY_PACKET, data) === 0) { | ||
this.connection.send('{}'); | ||
this.send('{}'); | ||
return; | ||
} | ||
let packet; | ||
try { | ||
packet = JSON.parse(data); | ||
} catch (err) { | ||
this.error(500, new Error('JSON parsing error')); | ||
const packet = metautil.jsonParse(data); | ||
if (!packet) { | ||
const error = new Error('JSON parsing error'); | ||
this.error(400, { error, pass: true }); | ||
return; | ||
} | ||
const [callType, target] = Object.keys(packet); | ||
const callId = packet[callType]; | ||
const callId = parseInt(packet[callType], 10); | ||
const args = packet[target]; | ||
@@ -113,3 +113,4 @@ if (callId && args) { | ||
} | ||
this.error(500, new Error('Packet structure error')); | ||
const error = new Error('Packet structure error'); | ||
this.error(400, { callId, error, pass: true }); | ||
} | ||
@@ -122,3 +123,3 @@ | ||
if (!proc) { | ||
this.error(404, null, callId); | ||
this.error(404, { callId }); | ||
return; | ||
@@ -129,3 +130,3 @@ } | ||
} catch { | ||
this.error(504, null, callId); | ||
this.error(503, { callId }); | ||
return; | ||
@@ -135,3 +136,3 @@ } | ||
if (!this.session && proc.access !== 'public') { | ||
this.error(403, null, callId); | ||
this.error(403, { callId }); | ||
return; | ||
@@ -142,5 +143,6 @@ } | ||
result = await proc.invoke(context, args); | ||
} catch (err) { | ||
const code = err.message === 'Timeout reached' ? 408 : 500; | ||
this.error(code, err, callId); | ||
} catch (error) { | ||
const timedout = error.message === 'Timeout reached'; | ||
const code = timedout ? 408 : 500; | ||
this.error(code, { callId, error, pass: timedout }); | ||
return; | ||
@@ -151,3 +153,3 @@ } finally { | ||
if (typeof result === 'object' && result.constructor.name === 'Error') { | ||
this.error(result.code, result, callId); | ||
this.error(result.code, { callId, error: result, pass: true }); | ||
return; | ||
@@ -160,2 +162,14 @@ } | ||
error(code, { callId, error = null, pass = false } = {}) { | ||
const { req, ip, application } = this; | ||
const { url, method } = req; | ||
const status = http.STATUS_CODES[code]; | ||
const message = pass ? error.message : status || 'Unknown error'; | ||
const httpCode = status ? code : 500; | ||
const reason = `${httpCode}\t${error ? error.stack : status}`; | ||
application.console.error(`${ip}\t${method}\t${url}\t${reason}`); | ||
const packet = { callback: callId, error: { message, code } }; | ||
this.send(packet, httpCode); | ||
} | ||
async restoreSession() { | ||
@@ -162,0 +176,0 @@ const { cookie } = this.req.headers; |
'use strict'; | ||
const http = require('http'); | ||
const metautil = require('metautil'); | ||
@@ -54,15 +53,2 @@ const { Channel } = require('./channel.js'); | ||
error(code, err = null, callId) { | ||
const { req, ip, application } = this; | ||
const { url, method } = req; | ||
const status = http.STATUS_CODES[code]; | ||
const reason = err ? err.stack : status; | ||
application.console.error(`${ip}\t${method}\t${url}\t${code}\t${reason}`); | ||
const message = status || 'Unknown error'; | ||
const httpCode = status ? code : 500; | ||
const error = { message, code }; | ||
if (callId) this.send({ callback: callId, error }); | ||
else this.send({ error }, httpCode); | ||
} | ||
redirect(location) { | ||
@@ -86,3 +72,3 @@ const { res } = this; | ||
if (!proc) { | ||
this.error(404, null, callId); | ||
this.error(404, { callId }); | ||
return; | ||
@@ -94,4 +80,4 @@ } | ||
result = await proc.invoke(context, { method: methodName, args }); | ||
} catch (err) { | ||
this.error(500, err, callId); | ||
} catch (error) { | ||
this.error(500, { callId, error }); | ||
return; | ||
@@ -98,0 +84,0 @@ } |
@@ -15,10 +15,2 @@ 'use strict'; | ||
const receiveBody = async (req) => { | ||
const buffers = []; | ||
for await (const chunk of req) { | ||
buffers.push(chunk); | ||
} | ||
return Buffer.concat(buffers); | ||
}; | ||
class Server { | ||
@@ -96,3 +88,3 @@ constructor(config, application) { | ||
} | ||
const body = receiveBody(req); | ||
const body = metautil.receiveBody(req); | ||
if (req.url === '/api') { | ||
@@ -104,5 +96,14 @@ body.then((data) => { | ||
body.then((data) => { | ||
let args = null; | ||
if (data.length > 0) { | ||
args = metautil.jsonParse(data); | ||
if (!args) { | ||
const error = new Error('JSON parsing error'); | ||
channel.error(500, { error, pass: true }); | ||
return; | ||
} | ||
} | ||
const pathname = req.url.slice('/api/'.length); | ||
const [path, params] = metautil.split(pathname, '?'); | ||
const args = data ? JSON.parse(data) : metautil.parseParams(params); | ||
if (!args) args = metautil.parseParams(params); | ||
const [interfaceName, methodName] = metautil.split(path, '/'); | ||
@@ -114,4 +115,4 @@ const hook = this.application.getHook(interfaceName); | ||
} | ||
body.catch((err) => { | ||
channel.error(500, err); | ||
body.catch((error) => { | ||
channel.error(500, { error }); | ||
}); | ||
@@ -118,0 +119,0 @@ } |
@@ -9,3 +9,3 @@ 'use strict'; | ||
const { url } = req; | ||
const filePath = url === '/' ? '/index.html' : url; | ||
const filePath = url.endsWith('/') ? url + 'index.html' : url; | ||
const fileExt = path.extname(filePath).substring(1); | ||
@@ -12,0 +12,0 @@ const data = application.getStaticFile(filePath); |
'use strict'; | ||
const http = require('http'); | ||
const { Channel } = require('./channel.js'); | ||
@@ -25,12 +24,2 @@ | ||
} | ||
error(code, err = null, callId) { | ||
const { req, ip, application } = this; | ||
const { url, method } = req; | ||
const status = http.STATUS_CODES[code]; | ||
const reason = err ? err.stack : status; | ||
application.console.error(`${ip}\t${method}\t${url}\t${code}\t${reason}`); | ||
const message = err ? err.message : code.toString(); | ||
this.send({ callback: callId, error: { message, code } }); | ||
} | ||
} | ||
@@ -37,0 +26,0 @@ |
@@ -43,2 +43,8 @@ import { EventEmitter } from 'events'; | ||
export interface ErrorOptions { | ||
callId?: number; | ||
error?: Error; | ||
pass?: boolean; | ||
} | ||
export class Channel { | ||
@@ -61,3 +67,3 @@ application: object; | ||
destroy(): void; | ||
error(code: number, err?: boolean, callId?: number): void; | ||
error(code: number, errorOptions?: ErrorOptions): void; | ||
} | ||
@@ -64,0 +70,0 @@ |
{ | ||
"name": "metacom", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -56,17 +56,17 @@ "description": "Communication protocol for Metarhia stack with rpc, events, binary streams, memory and db access", | ||
"dependencies": { | ||
"metautil": "^3.5.15", | ||
"ws": "^8.2.2" | ||
"metautil": "^3.5.16", | ||
"ws": "^8.2.3" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^16.9.6", | ||
"@types/ws": "^7.4.7", | ||
"@types/node": "^16.10.4", | ||
"@types/ws": "^8.2.0", | ||
"eslint": "^7.31.0", | ||
"eslint-config-metarhia": "^7.0.1", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.24.2", | ||
"eslint-plugin-import": "^2.25.1", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"metatests": "^0.7.2", | ||
"prettier": "^2.4.1", | ||
"typescript": "^4.4.3" | ||
"typescript": "^4.4.4" | ||
} | ||
} |
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
38483
7
917
Updatedmetautil@^3.5.16
Updatedws@^8.2.3