Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion application/api/chat.1/send.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
async ({ room, message }) => {
domain.chat.send(room, message);
return 'ok';
};
1 change: 0 additions & 1 deletion application/api/chat.1/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ async ({ room }) => {
context.client.on('close', () => {
clients.delete(context.client);
});
return 'ok';
};
18 changes: 11 additions & 7 deletions application/api/example.1/subscribe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
async () => {
setInterval(async () => {
const stats = await lib.resmon.getStatistics();
context.client.emit('example/resmon', stats);
}, config.resmon.interval);
return { subscribed: 'resmon' };
};
({
access: 'public',

method: async () => {
setInterval(async () => {
const stats = await lib.resmon.getStatistics();
context.client.emit('example/resmon', stats);
}, config.resmon.interval);
return { subscribed: 'resmon' };
},
});
15 changes: 6 additions & 9 deletions application/static/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,15 @@ function commandLoop() {

class Application {
constructor() {
this.logged = false;
this.controlInput = document.getElementById('controlInput');
this.controlBrowse = document.getElementById('controlBrowse');
this.keyboard = new Keyboard(this);
this.scroller = new Scroller(this);
const protocol = location.protocol === 'http:' ? 'ws' : 'wss';
this.metacom = Metacom.create(`${protocol}://${location.host}/api`);
window.api = this.metacom.api;
window.application = this;
}

clear() {
Expand Down Expand Up @@ -464,16 +467,14 @@ class Application {
}

window.addEventListener('load', async () => {
window.application = new Application();
window.api = window.application.metacom.api;
const application = new Application();
await application.metacom.load('auth', 'console', 'example', 'files');
const token = localStorage.getItem('metarhia.session.token');
let logged = false;
if (token) {
const res = await api.auth.restore({ token });
logged = res.status === 'logged';
application.logged = res.status === 'logged';
}
if (!logged) {
if (!application.logged) {
const res = await api.auth.signin({ login: 'marcus', password: 'marcus' });
if (res.token) {
localStorage.setItem('metarhia.session.token', res.token);
Expand All @@ -483,7 +484,3 @@ window.addEventListener('load', async () => {
application.print(text);
commandLoop();
});

if (navigator.serviceWorker) {
navigator.serviceWorker.register('/worker.js');
}
5 changes: 1 addition & 4 deletions application/static/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ class EventEmitter {
}

clear(name) {
if (!name) {
this.events.clear();
return;
}
if (!name) return void this.events.clear();
this.events.delete(name);
}

Expand Down
1 change: 0 additions & 1 deletion application/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<title>Metarhia Console</title>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="connect-src 'self' wss:; style-src 'self' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com">
<link rel="manifest" href="manifest.json">
<link rel="stylesheet" href="console.css">
<script src="console.js" type="module"></script>
<meta name="HandheldFriendly" content="true" />
Expand Down
92 changes: 45 additions & 47 deletions application/static/metacom.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Metacom extends EventEmitter {
this.active = false;
this.connected = false;
this.opening = null;
this.lastActivity = new Date().getTime();
this.lastActivity = Date.now();
this.callTimeout = options.callTimeout || CALL_TIMEOUT;
this.pingInterval = options.pingInterval || PING_INTERVAL;
this.reconnectTimeout = options.reconnectTimeout || RECONNECT_TIMEOUT;
Expand Down Expand Up @@ -86,52 +86,55 @@ class Metacom extends EventEmitter {

async message(data) {
if (data === '{}') return;
this.lastActivity = new Date().getTime();
this.lastActivity = Date.now();
let packet;
try {
packet = JSON.parse(data);
} catch {
return;
}
const { type, id, method } = packet;
if (id) {
if (type === 'callback') {
const promised = this.calls.get(id);
if (!promised) return;
const [resolve, reject, timeout] = promised;
this.calls.delete(id);
clearTimeout(timeout);
if (packet.error) {
reject(new MetacomError(packet.error));
return;
}
resolve(packet.result);
} else if (type === 'event') {
const [unit, name] = method.split('/');
const metacomUnit = this.api[unit];
if (metacomUnit) metacomUnit.emit(name, packet.data);
} else if (type === 'stream') {
const { name, size, status } = packet;
const stream = this.streams.get(id);
if (name && typeof name === 'string' && Number.isSafeInteger(size)) {
if (stream) {
console.error(new Error(`Stream ${name} is already initialized`));
} else {
const streamData = { id, name, size };
const stream = new MetaReadable(streamData);
this.streams.set(id, stream);
}
} else if (!stream) {
console.error(new Error(`Stream ${id} is not initialized`));
} else if (status === 'end') {
await stream.close();
this.streams.delete(id);
} else if (status === 'terminate') {
await stream.terminate();
this.streams.delete(id);
const { type, id, name } = packet;
if (type === 'event') {
const [unit, eventName] = name.split('/');
const metacomUnit = this.api[unit];
if (metacomUnit) metacomUnit.emit(eventName, packet.data);
return;
}
if (!id) {
console.error(new Error('Packet structure error'));
return;
}
if (type === 'callback') {
const promised = this.calls.get(id);
if (!promised) return;
const [resolve, reject, timeout] = promised;
this.calls.delete(id);
clearTimeout(timeout);
if (packet.error) {
return void reject(new MetacomError(packet.error));
}
resolve(packet.result);
} else if (type === 'stream') {
const { name, size, status } = packet;
const stream = this.streams.get(id);
if (name && typeof name === 'string' && Number.isSafeInteger(size)) {
if (stream) {
console.error(new Error(`Stream ${name} is already initialized`));
} else {
console.error(new Error('Stream packet structure error'));
const streamData = { id, name, size };
const stream = new MetaReadable(streamData);
this.streams.set(id, stream);
}
} else if (!stream) {
console.error(new Error(`Stream ${id} is not initialized`));
} else if (status === 'end') {
await stream.close();
this.streams.delete(id);
} else if (status === 'terminate') {
await stream.terminate();
this.streams.delete(id);
} else {
console.error(new Error('Stream packet structure error'));
}
}
}
Expand All @@ -158,11 +161,6 @@ class Metacom extends EventEmitter {
for (const methodName of methodNames) {
methods[methodName] = request(methodName);
}
methods.on('*', (event, data) => {
const name = unit + '/' + event;
const packet = { type: 'event', name, data };
this.send(JSON.stringify(packet));
});
this.api[unit] = methods;
}
}
Expand Down Expand Up @@ -220,7 +218,7 @@ class WebsocketTransport extends Metacom {

this.ping = setInterval(() => {
if (this.active) {
const interval = new Date().getTime() - this.lastActivity;
const interval = Date.now() - this.lastActivity;
if (interval > this.pingInterval) this.send('{}');
}
}, this.pingInterval);
Expand All @@ -247,7 +245,7 @@ class WebsocketTransport extends Metacom {

send(data) {
if (!this.connected) return;
this.lastActivity = new Date().getTime();
this.lastActivity = Date.now();
this.socket.send(data);
}
}
Expand All @@ -265,7 +263,7 @@ class HttpTransport extends Metacom {
}

send(data) {
this.lastActivity = new Date().getTime();
this.lastActivity = Date.now();
fetch(this.url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand Down
10 changes: 5 additions & 5 deletions application/static/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MetaReadable extends EventEmitter {
}

async finalize(writable) {
const waitWritableEvent = EventEmitter.once.bind(writable);
const waitWritableEvent = EventEmitter.once.bind(this, writable);
const onError = () => this.terminate();
writable.once('error', onError);
for await (const chunk of this) {
Expand Down Expand Up @@ -132,12 +132,12 @@ class MetaReadable extends EventEmitter {
}

class MetaWritable extends EventEmitter {
constructor(id, name, size, transport) {
constructor(transport, options = {}) {
super();
this.id = id;
this.name = name;
this.size = size;
this.transport = transport;
this.id = options.id;
this.name = options.name;
this.size = options.size;
this.init();
}

Expand Down
Loading