Finish app conversion and enforce code style

This commit is contained in:
hensm
2019-02-26 01:30:30 +00:00
parent b7571791e2
commit 1e49fbe9be
19 changed files with 541 additions and 186 deletions

View File

@@ -4,9 +4,7 @@ import { Transform } from "stream";
import { Message } from "./types";
interface ResponseHandlerFunction {
(message: Message): Promise<any>
}
type ResponseHandlerFunction = (message: Message) => Promise<any>;
/**
* Takes a handler function that implements the transform
@@ -18,9 +16,9 @@ export const response = (handler: ResponseHandlerFunction) => new Transform({
, transform (chunk: Message, encoding, callback) {
Promise.resolve(handler(chunk))
.then(response => {
if (response) {
callback(null, response);
.then(res => {
if (res) {
callback(null, res);
} else {
callback(null);
}
@@ -94,13 +92,13 @@ export const encode = new Transform({
writableObjectMode: true
, transform (chunk, encoding, callback) {
const message_length = Buffer.alloc(4);
const messageLength = Buffer.alloc(4);
const message = Buffer.from(JSON.stringify(chunk));
// Write message length
message_length.writeUInt32LE(message.length, 0);
messageLength.writeUInt32LE(message.length, 0);
// Output joined message length and content
callback(null, Buffer.concat([message_length, message]));
callback(null, Buffer.concat([messageLength, message]));
}
});