Comparing version 2.0.0 to 2.0.1
@@ -5,2 +5,9 @@ # Changelog | ||
## [2.0.1][] - 2021-09-03 | ||
- Simplify Channel/Session machinery | ||
- Collections: sessions, channels | ||
- Decompose: extract transport and static modules | ||
- Fix: empty packet structure error | ||
## [2.0.0][] - 2021-08-19 | ||
@@ -139,3 +146,4 @@ | ||
[unreleased]: https://github.com/metarhia/metacom/compare/v2.0.0...HEAD | ||
[unreleased]: https://github.com/metarhia/metacom/compare/v2.0.1...HEAD | ||
[2.0.1]: https://github.com/metarhia/metacom/compare/v2.0.0...v2.0.1 | ||
[2.0.0]: https://github.com/metarhia/metacom/compare/v1.8.1...v2.0.0 | ||
@@ -142,0 +150,0 @@ [1.8.2]: https://github.com/metarhia/metacom/compare/v1.8.1...v1.8.2 |
'use strict'; | ||
const http = require('http'); | ||
const path = require('path'); | ||
const metautil = require('metautil'); | ||
const transport = require('./transport.js'); | ||
const { MIME_TYPES, HEADERS } = transport.http; | ||
const { COOKIE_DELETE, COOKIE_HOST, TOKEN } = transport.http; | ||
const MIME_TYPES = { | ||
html: 'text/html; charset=UTF-8', | ||
json: 'application/json; charset=UTF-8', | ||
js: 'application/javascript; charset=UTF-8', | ||
css: 'text/css', | ||
png: 'image/png', | ||
ico: 'image/x-icon', | ||
svg: 'image/svg+xml', | ||
}; | ||
const EMPTY_PACKET = Buffer.from('{}'); | ||
const HEADERS = { | ||
'X-XSS-Protection': '1; mode=block', | ||
'X-Content-Type-Options': 'nosniff', | ||
'Strict-Transport-Security': 'max-age=31536000; includeSubdomains; preload', | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS', | ||
'Access-Control-Allow-Headers': 'Content-Type', | ||
}; | ||
const TOKEN = 'token'; | ||
const EPOCH = 'Thu, 01 Jan 1970 00:00:00 GMT'; | ||
const FUTURE = 'Fri, 01 Jan 2100 00:00:00 GMT'; | ||
const LOCATION = 'Path=/; Domain'; | ||
const COOKIE_DELETE = `${TOKEN}=deleted; Expires=${EPOCH}; ${LOCATION}=`; | ||
const COOKIE_HOST = `Expires=${FUTURE}; ${LOCATION}`; | ||
class Session { | ||
@@ -37,3 +15,2 @@ constructor(token, channel, data) { | ||
this.channel = channel; | ||
this.channels = new Map([channel]); | ||
this.data = data; | ||
@@ -55,4 +32,4 @@ this.context = new Proxy(data, { | ||
const sessions = new Map(); | ||
const channels = new Map(); | ||
const sessions = new Map(); // token: Session | ||
const channels = new Map(); // Client: Channel | ||
@@ -89,2 +66,3 @@ class Client { | ||
const channel = channels.get(this); | ||
if (channel.session) sessions.delete(channel.session.token); | ||
const session = new Session(token, channel, data); | ||
@@ -123,19 +101,2 @@ channel.session = session; | ||
static() { | ||
const { req, res, ip, application } = this; | ||
const { url, method } = req; | ||
const filePath = url === '/' ? '/index.html' : url; | ||
const fileExt = path.extname(filePath).substring(1); | ||
const mimeType = MIME_TYPES[fileExt] || MIME_TYPES.html; | ||
res.writeHead(200, { ...HEADERS, 'Content-Type': mimeType }); | ||
if (res.writableEnded) return; | ||
const data = application.getStaticFile(filePath); | ||
if (data) { | ||
res.end(data); | ||
application.console.log(`${ip}\t${method}\t${url}`); | ||
return; | ||
} | ||
this.error(404); | ||
} | ||
redirect(location) { | ||
@@ -148,2 +109,9 @@ const { res } = this; | ||
send(ext, data) { | ||
const { res } = this; | ||
const mimeType = MIME_TYPES[ext] || MIME_TYPES.html; | ||
res.writeHead(200, { ...HEADERS, 'Content-Type': mimeType }); | ||
res.end(data); | ||
} | ||
options() { | ||
@@ -175,3 +143,3 @@ const { res } = this; | ||
message(data) { | ||
if (data === '{}') { | ||
if (Buffer.compare(EMPTY_PACKET, data) === 0) { | ||
this.connection.send('{}'); | ||
@@ -298,6 +266,6 @@ return; | ||
destroy() { | ||
channels.delete(this.client); | ||
for (const callback of this.client.events.close) callback(); | ||
if (!this.session) return; | ||
const token = this.session.token; | ||
sessions.delete(token); | ||
sessions.delete(this.session.token); | ||
} | ||
@@ -304,0 +272,0 @@ } |
@@ -6,8 +6,7 @@ 'use strict'; | ||
const { threadId } = require('worker_threads'); | ||
const metautil = require('metautil'); | ||
const { Semaphore } = metautil; | ||
const ws = require('ws'); | ||
const { Channel, channels } = require('./channel.js'); | ||
const { serveStatic } = require('./static.js'); | ||
@@ -65,3 +64,2 @@ const SHORT_TIMEOUT = 500; | ||
connection.on('close', () => { | ||
channels.delete(channel.client); | ||
channel.destroy(); | ||
@@ -84,3 +82,2 @@ }); | ||
const channel = await new Channel(req, res, null, this.application); | ||
const { client } = channel; | ||
@@ -90,3 +87,2 @@ res.on('close', () => { | ||
finished = true; | ||
channels.delete(client); | ||
channel.destroy(); | ||
@@ -104,3 +100,3 @@ }); | ||
if (url.startsWith('/api')) this.request(channel); | ||
else channel.static(); | ||
else serveStatic(channel); | ||
} | ||
@@ -107,0 +103,0 @@ |
{ | ||
"name": "metacom", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"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
35340
15
823