Comparing version 0.1.0-alpha.8 to 0.1.0-alpha.9
@@ -21,4 +21,23 @@ 'use strict'; | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS', | ||
'Access-Control-Allow-Headers': 'Content-Type', | ||
'Content-Security-Policy': [ | ||
`default-src 'self' ws:`, | ||
`style-src 'self' https://fonts.googleapis.com`, | ||
`font-src 'self' https://fonts.gstatic.com`, | ||
].join('; '), | ||
}; | ||
class Client { | ||
constructor(connection) { | ||
this.callId = 0; | ||
this.connection = connection; | ||
} | ||
emit(name, data) { | ||
const packet = { event: --this.callId, [name]: data }; | ||
this.connection.send(JSON.stringify(packet)); | ||
} | ||
} | ||
class Channel { | ||
@@ -31,4 +50,12 @@ constructor(req, res, connection, application) { | ||
this.application = application; | ||
this.client = new Client(connection); | ||
this.session = null; | ||
return this.init(); | ||
} | ||
async init() { | ||
this.session = await this.application.auth.restore(this); | ||
return this; | ||
} | ||
static() { | ||
@@ -54,6 +81,13 @@ const { req, res, ip, application } = this; | ||
if (res.headersSent) return; | ||
res.writeHead(302, { Location: location }); | ||
res.writeHead(302, { Location: location, ...HEADERS }); | ||
res.end(); | ||
} | ||
options() { | ||
const { res } = this; | ||
if (res.headersSent) return; | ||
res.writeHead(200, HEADERS); | ||
res.end(); | ||
} | ||
error(code, err, callId = err) { | ||
@@ -66,11 +100,12 @@ const { req, res, connection, ip, application } = this; | ||
application.logger.error(`${ip}\t${method}\t${url}\t${code}\t${reason}`); | ||
const { Error } = this.application; | ||
const message = err instanceof Error ? err.message : status; | ||
const error = { message, code }; | ||
if (connection) { | ||
const packet = { callback: callId, error: { code, message: status } }; | ||
connection.send(JSON.stringify(packet)); | ||
connection.send(JSON.stringify({ callback: callId, error })); | ||
return; | ||
} | ||
if (res.writableEnded) return; | ||
res.writeHead(status, { 'Content-Type': MIME_TYPES.json }); | ||
const packet = { code, error: status }; | ||
res.end(JSON.stringify(packet)); | ||
res.writeHead(code, { 'Content-Type': MIME_TYPES.json, ...HEADERS }); | ||
res.end(JSON.stringify({ error })); | ||
} | ||
@@ -98,3 +133,3 @@ | ||
async rpc(callId, interfaceName, methodName, args) { | ||
const { res, connection, ip, application } = this; | ||
const { res, connection, ip, application, session, client } = this; | ||
const { semaphore } = application.server; | ||
@@ -109,4 +144,4 @@ try { | ||
try { | ||
let session = await application.auth.restore(this); | ||
const proc = application.getMethod(iname, ver, methodName, session); | ||
const context = session ? session.context : { client }; | ||
const proc = application.getMethod(iname, ver, methodName, context); | ||
if (!proc) { | ||
@@ -116,3 +151,3 @@ this.error(404, callId); | ||
} | ||
if (!session && proc.access !== 'public') { | ||
if (!this.session && proc.access !== 'public') { | ||
this.error(403, callId); | ||
@@ -122,11 +157,19 @@ return; | ||
const result = await proc.method(args); | ||
if (result instanceof Error) { | ||
this.error(result.code, result, callId); | ||
return; | ||
} | ||
const userId = result ? result.userId : undefined; | ||
if (!session && userId && proc.access === 'public') { | ||
session = application.auth.start(this, userId); | ||
result.token = session.token; | ||
if (!this.session && userId && proc.access === 'public') { | ||
this.session = application.auth.start(this, userId); | ||
result.token = this.session.token; | ||
} | ||
const data = JSON.stringify({ callback: callId, result }); | ||
if (connection) connection.send(data); | ||
else res.end(data); | ||
const token = session ? session.token : 'anonymous'; | ||
if (connection) { | ||
connection.send(data); | ||
} else { | ||
res.writeHead(200, { 'Content-Type': MIME_TYPES.json, ...HEADERS }); | ||
res.end(data); | ||
} | ||
const token = this.session ? this.session.token : 'anonymous'; | ||
const record = `${ip}\t${token}\t${interfaceName}/${methodName}`; | ||
@@ -142,2 +185,2 @@ application.logger.access(record); | ||
module.exports = Channel; | ||
module.exports = { Channel }; |
@@ -42,2 +42,2 @@ 'use strict'; | ||
module.exports = Semaphore; | ||
module.exports = { Semaphore }; |
@@ -10,3 +10,3 @@ 'use strict'; | ||
const Semaphore = require('./semaphore.js'); | ||
const { Semaphore } = require('./semaphore.js'); | ||
@@ -13,0 +13,0 @@ const SHUTDOWN_TIMEOUT = 5000; |
'use strict'; | ||
const { Metacom } = require('./lib/client.js'); | ||
const { Server } = require('./lib/server.js'); | ||
const { Metacom } = require('./lib/client.js'); | ||
const { Channel } = require('./lib/channel.js'); | ||
const { Semaphore } = require('./lib/semaphore.js'); | ||
module.exports = Metacom; | ||
module.exports.Server = Server; | ||
module.exports.Channel = Channel; | ||
module.exports.Semaphore = Semaphore; |
{ | ||
"name": "metacom", | ||
"version": "0.1.0-alpha.8", | ||
"version": "0.1.0-alpha.9", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "Communication protocol for Metarhia stack with rpc, events, binary streams, memory and db access", |
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
20831
570