Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Merge e1ffc1b into 44f13a6
Browse files Browse the repository at this point in the history
  • Loading branch information
jperata committed Dec 28, 2017
2 parents 44f13a6 + e1ffc1b commit 084b142
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lib/client/bespoke-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class BespokeClient {

const tcpClient = new TCPClient(request.id() + "");
const httpBuffer = new HTTPBuffer();
tcpClient.transmit(self.targetDomain, self.targetPort, request.toTCP(), function(data: Buffer, error: NetworkErrorType, message: string) {
tcpClient.transmit(self.targetDomain, self.targetPort, request.rawContents, function(data: Buffer, error: NetworkErrorType, message: string) {

if (data != null) {
// Grab the body of the response payload
Expand Down Expand Up @@ -211,7 +211,7 @@ export class BespokeClient {
} else if (socketMessage.contains(Global.KeepAliveMessage)) {
this.keepAlive.received();
} else {
this.onWebhookReceived(WebhookRequest.fromString(this.socketHandler.socket, socketMessage.asString(), socketMessage.getMessageID()));
this.onWebhookReceived(WebhookRequest.fromBuffer(this.socketHandler.socket, socketMessage.getMessage(), socketMessage.getMessageID()));
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/client/lambda-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class LambdaServer {
path = this.file;
} else {
if (onlyUrl !== "/") {

// verify that path does not contain more than one '.' or node_modules anywhere
if (/(.*\..*\.)|(.*node_modules)/.test(onlyUrl)) {
context.fail(Error(`LambdaServer input url should not contain more than '.' or node_modules. found: ${onlyUrl}`));
Expand All @@ -118,6 +119,7 @@ export class LambdaServer {
return;
}
}

LoggingHelper.debug(Logger, "Invoking Lambda: " + path);

const lambda = this.moduleManager.module(path);
Expand Down
2 changes: 1 addition & 1 deletion lib/client/tcp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class TCPClient {
public onCloseCallback: () => void;
public constructor (public id: string) {}

public transmit(host: string, port: number, requestData: string, callback: TCPClientCallback) {
public transmit(host: string, port: number, requestData: Buffer, callback: TCPClientCallback) {
let self = this;
let client = new net.Socket();
LoggingHelper.info(Logger, "Forwarding " + host + ":" + port);
Expand Down
8 changes: 3 additions & 5 deletions lib/core/socket-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ export class SocketHandler {
this.buffer = Buffer.concat([this.buffer, data]);
}

const dataString = this.buffer.toString();

const delimiterIndex = dataString.indexOf(Global.MessageDelimiter);
const delimiterIndex = this.buffer.indexOf(Global.MessageDelimiter);

if (delimiterIndex > -1) {
const messageIDIndex = delimiterIndex - Global.MessageIDLength;
Expand All @@ -86,14 +84,14 @@ export class SocketHandler {

const message = this.buffer.slice(0, messageIDIndex);
// Grab the message ID - it precedes the delimiter
const messageIDString = dataString.slice(delimiterIndex - Global.MessageIDLength, delimiterIndex);
const messageIDString = this.buffer.slice(delimiterIndex - Global.MessageIDLength, delimiterIndex).toString();
const messageID: number = parseInt(messageIDString);
if (isNaN(messageID) || (messageID + "").length < 13) {
badMessage = true;
}

if (badMessage) {
LoggingHelper.error(Logger, "Bad message received: " + dataString);
LoggingHelper.error(Logger, "Bad message received: " + this.buffer.toString());
} else {
const socketMessage = new SocketMessage(message, messageID);

Expand Down
29 changes: 15 additions & 14 deletions lib/core/webhook-request.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
import * as querystring from "querystring";
import {BufferUtil} from "./buffer-util";
import {Socket} from "net";

export class WebhookRequest {
public rawContents: Buffer;
public method: string;
public uri: string;
public body: string;
public rawBody: Buffer;
public queryParameters: {[id: string]: string | string[]} = {};
public headers: { [id: string]: string };
private requestID: number;

public constructor(public sourceSocket?: Socket) {
this.rawContents = new Buffer("");
this.rawBody = new Buffer("");
this.body = "";
this.requestID = new Date().getTime();
if (this.sourceSocket === undefined) {
this.sourceSocket = null;
}
}

public static fromString(sourceSocket: Socket, payload: string, id?: number): WebhookRequest {
let webhookRequest = new WebhookRequest(sourceSocket);
webhookRequest.append(BufferUtil.fromString(payload));
public static fromBuffer(sourceSocket: Socket, payload: Buffer, id?: number): WebhookRequest {
const webhookRequest = new WebhookRequest(sourceSocket);
webhookRequest.append(payload);
webhookRequest.requestID = id;
return webhookRequest;
}

public append(data: Buffer) {
this.rawContents = Buffer.concat([this.rawContents, data]);

if (this.headers == null) {
this.headers = {};
let contentsString: string = this.rawContents.toString();
let endIndex = contentsString.indexOf("\r\n\r\n");
const endIndex = this.rawContents.indexOf("\r\n\r\n");
if (endIndex !== -1) {
this.parseHeaders(contentsString.substr(0, endIndex));
this.parseHeaders(this.rawContents.slice(0, endIndex).toString());

if (endIndex + 4 < contentsString.length) {
let bodyPart: string = contentsString.substr((endIndex + 4));
if (endIndex + 4 < this.rawContents.length) {
const bodyPart: Buffer = this.rawContents.slice((endIndex + 4));
this.appendBody(bodyPart);
}
}
} else {
this.appendBody(data.toString());
this.appendBody(data);
}
}

public appendBody(bodyPart: string) {
this.body += bodyPart;
public appendBody(bodyPart: Buffer) {
this.rawBody = Buffer.concat([this.rawBody, bodyPart]);
this.body += bodyPart.toString();
}

public done(): boolean {
if (this.method === "GET") {
return true;
}
return (this.body.length === this.contentLength());

return (this.rawBody.length === this.contentLength());
}

public contentLength(): number {
Expand Down
2 changes: 1 addition & 1 deletion lib/server/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Node {
public forward(request: WebhookRequest): void {
console.log("NODE " + this.id + " MSG-ID: " + request.id() + " Forwarding");
this.requests[request.id()] = request;
this.socketHandler.send(new SocketMessage(request.toTCP(), request.id()));
this.socketHandler.send(new SocketMessage(request.rawContents, request.id()));
}

public handlingRequest(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion test/bin/bst-invalid-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe("bst commands", function() {
mockery.disable();
});

this.timeout(2000);
this.timeout(3000);

describe("Invalid deploy command", function() {
it("Prints error with invalid command", function(done) {
Expand Down
15 changes: 15 additions & 0 deletions test/client/lambda-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ describe("LambdaServer", function() {

});

it("Starts Correctly With a custom function with strange characters", function(done) {
let runner = new LambdaServer("ExampleLambdaCustomFunction.js", 10000, false, "myHandler");
runner.start();

let client = new HTTPClient();
let inputData = {"data": "Testü"};
client.post("localhost", 10000, "", JSON.stringify(inputData), function(data: Buffer) {
let responseString = data.toString();
assert.equal(responseString, "{\"success\":true}");
runner.stop();
done();
});

});

it("Handles Lambda Fail Correctly", function(done) {
let runner = new LambdaServer("ExampleLambda.js", 10000);
runner.start();
Expand Down
2 changes: 1 addition & 1 deletion test/server/node-manager-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe("NodeManager", function() {
done();
});
};
client.transmit("localhost", 9000, "{1234567890123" + Global.MessageDelimiter, function () {
client.transmit("localhost", 9000, Buffer.from("{1234567890123" + Global.MessageDelimiter), function () {
assert(false, "This should not be reached - no data should be sent back.");
});
});
Expand Down

0 comments on commit 084b142

Please sign in to comment.