Skip to content

Commit

Permalink
more logging, fixed data encoding (video, images)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Gottfried committed Feb 19, 2020
1 parent 89b7fbc commit b84d622
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
14 changes: 10 additions & 4 deletions client/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
//-- vim: ft=javascript tabstop=2 softtabstop=2 expandtab shiftwidth=2
'use strict';
const path = require('path');
const http = require('http');
const checksum = require('../lib').checksum;
const WebSocket = require('ws');
const WsJsonProtocol = require('../lib/ws-json');

Expand Down Expand Up @@ -36,8 +37,11 @@ class RequestForwarder extends Object {
let _send = this._send.bind(this);
let sender = function sender(event_id) {
return function (data) {
if (event_id != 'data')
if (event_id == 'data') {
console.log(`<: ${message.channel}: ${event_id} ${ireq.method} ${oreq_uri.pathname} ${data.length} ${checksum(data)}`);
} else {
console.log(`<: ${message.channel}: ${event_id} ${ireq.method} ${oreq_uri.pathname}`);
}
_send({
channel: message.channel,
id: message.id,
Expand All @@ -48,7 +52,7 @@ class RequestForwarder extends Object {
}
console.log(` :> ${message.channel}: ${ireq.method} ${oreq_uri.toString()}`);
req = http.request(oreq_uri.toString(), req_params, function handleResponse(res) {
res.setEncoding('utf8');
// res.setEncoding('utf8');
console.log(`<: ${message.channel}: ${res.statusCode} ${res.statusMessage} / ${ireq.method} ${oreq_uri.pathname}`);
sender('headers')({
statusCode: res.statusCode,
Expand All @@ -65,7 +69,9 @@ class RequestForwarder extends Object {
req = this._activeChannels[message.channel];
if (req) {
try {
console.log(` :> DATA[${message.data}]`);
if (message.data instanceof Object) {
message.data = Buffer.from(message.data);
console.log(` :> ${digest(message.data)}]`);
req.write(message.data);
} catch(err) {
console.log('data is object', message);
Expand Down
16 changes: 16 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//-- vim: ft=javascript tabstop=2 softtabstop=2 expandtab shiftwidth=2
'use strict';

const crypto = require('crypto');

function checksum(data, {length=5}={}) {
try {
return crypto.createHash('md5').update(data).digest('hex').slice(0,length).replace(/(.{4})/g, ':$1').slice(1);
} catch(err) {
console.error(err, data);
throw err;
}
}

exports.checksum = checksum;
module.exports.WsJsonProtocol = require('./ws-json').WsJsonProtocol;
14 changes: 10 additions & 4 deletions server/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
//-- vim: ft=javascript tabstop=2 softtabstop=2 expandtab shiftwidth=2
const REQ_SIZE_LIMIT = 1024*1024;
const checksum = require('../lib').checksum;
const uuid = require('uuid');
const { HttpError, BadGateway, NotFound } = require('./HttpError');

Expand All @@ -27,12 +28,17 @@ class ForwardedRequest extends Object {
}

on_headers(message) {
console.log(`<: ${this.channelUrl}: ${message.data.statusCode} ${message.data.statusMessage} / ${message.event} ${this.request.method} ${this.request.url}`);
console.log(`<: ${this.channelUrl}: ${message.data.statusCode} ${message.data.statusMessage}
${JSON.stringify(message.data.headers)}
/ ${message.event} ${this.request.method} ${this.request.url}`);
this.response.writeHead(message.data.statusCode, message.data.statusMessage, message.data.headers);
}

on_data(message) {
// console.log(`<: ${this.channelUrl}: data ${message.data.length}`);
if (message.data instanceof Object) {
message.data = Buffer.from(message.data)
}
console.log(`<: ${this.channelUrl}: data ${checksum(message.data)}`);
this.response.write(message.data);
}

Expand Down Expand Up @@ -60,8 +66,8 @@ class ForwardedRequest extends Object {
}

resendDataChunk(chunk) {
console.log(` :> ${this.channelUrl} data`);
this.sendMessage('data', chunk.toString());
console.log(` :> ${this.channelUrl} data${checksum(chunk)}`);
this.sendMessage('data', chunk.toString()); // FIXME: is it binary safe?
}

resendError(error) {
Expand Down

0 comments on commit b84d622

Please sign in to comment.