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

Commit

Permalink
Merge pull request #65 from bespoken/JSONOutput#56
Browse files Browse the repository at this point in the history
Json output#56
  • Loading branch information
OpenDog committed Sep 6, 2016
2 parents fdf09e7 + 35d7b6a commit ce4fc7b
Show file tree
Hide file tree
Showing 39 changed files with 556 additions and 184 deletions.
1 change: 0 additions & 1 deletion bin/bst-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Startup script for running BST
import {BespokeServer} from "../lib/server/bespoke-server";
import * as program from "commander";
import {WebhookRequest} from "../lib/core/webhook-request";
import {Global} from "../lib/core/global";

program.version(Global.version());
Expand Down
3 changes: 0 additions & 3 deletions bin/bst-sleep.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/usr/bin/env node
import * as program from "commander";
import {Global} from "../lib/core/global";
import {LoggingHelper} from "../lib/core/logging-helper";
import {BSTProxy} from "../lib/client/bst-proxy";
import {BSTSpeak} from "../lib/client/bst-speak";

Global.initializeCLI();

Expand Down
2 changes: 0 additions & 2 deletions bin/bst-speak.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env node
import * as program from "commander";
import {Global} from "../lib/core/global";
import {LoggingHelper} from "../lib/core/logging-helper";
import {BSTProxy} from "../lib/client/bst-proxy";
import {BSTSpeak} from "../lib/client/bst-speak";

Global.initializeCLI();
Expand Down
2 changes: 0 additions & 2 deletions lib/alexa/intent-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {FileUtil} from "../core/file-util";
import {LoggingHelper} from "../core/logging-helper";
const Logger = "INTENTS";

export class IntentSchema {
public constructor(public schemaJSON: any) {
Expand Down
1 change: 0 additions & 1 deletion lib/alexa/service-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {IntentSchema} from "./intent-schema";
import {InteractionModel} from "./interaction-model";
let uuid = require("node-uuid");

Expand Down
29 changes: 23 additions & 6 deletions lib/client/bespoke-client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as net from "net";
import {Socket} from "net";
import {Global} from "../core/global";
import {SocketHandler} from "../core/socket-handler";
import {WebhookReceivedCallback} from "../server/webhook-manager";
import {WebhookRequest} from "../core/webhook-request";
import {TCPClient} from "./tcp-client";
import {NetworkErrorType} from "../core/global";
import {LoggingHelper} from "../core/logging-helper";
import {KeepAlive} from "./keep-alive";
import {StringUtil} from "../core/string-util";
import {HTTPBuffer} from "../core/http-buffer";

const Logger = "BST-CLIENT";

Expand Down Expand Up @@ -73,12 +72,30 @@ export class BespokeClient {

private onWebhookReceived(request: WebhookRequest): void {
let self = this;
LoggingHelper.info(Logger, "OnWebhook: " + request.toString());
// Print out the contents of the request body to the console
LoggingHelper.info(Logger, "RequestReceived: " + request.toString() + " ID: " + request.id());
LoggingHelper.verbose(Logger, "Payload:\n" + StringUtil.prettyPrintJSON(request.body));

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

if (data != null) {
self.socketHandler.send(data, request.id());
// Grab the body of the response payload
httpBuffer.append(data);

// Don't send the data until we have it all
if (httpBuffer.complete()) {
LoggingHelper.info(Logger, "ResponseReceived ID: " + request.id());
let payload: string = null;
if (httpBuffer.isJSON()) {
payload = StringUtil.prettyPrintJSON(httpBuffer.body().toString());
} else {
payload = httpBuffer.body().toString();
}
LoggingHelper.verbose(Logger, "Payload:\n" + payload);
self.socketHandler.send(httpBuffer.raw().toString(), request.id());
}
} else if (error !== null && error !== undefined) {
if (error === NetworkErrorType.CONNECTION_REFUSED) {
LoggingHelper.error(Logger, "CLIENT Connection Refused, Port " + self.targetPort + ". Is your server running?");
Expand Down
3 changes: 0 additions & 3 deletions lib/client/bst-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/// <reference path="../../typings/index.d.ts" />

import {Tool} from "../core/tool";
import ICommand = commander.ICommand;
import {BespokeClient} from "./bespoke-client";
import {LambdaRunner} from "./lambda-runner";
import {URLMangler} from "./url-mangler";
import {BSTProcess} from "./bst-config";
import {Global} from "../core/global";
import {LoggingHelper} from "../core/logging-helper";

export enum ProxyType {
HTTP,
Expand Down
2 changes: 0 additions & 2 deletions lib/client/keep-alive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// The socket seems to timeout after 5 minutes of inactivity, so this is a safe period
import {LoggingHelper} from "../core/logging-helper";
import {SocketHandler} from "../core/socket-handler";
import {Global} from "../core/global";

Expand Down
6 changes: 2 additions & 4 deletions lib/client/lambda-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ export class LambdaRunner {
path = [process.cwd(), this.file].join("/");
}

LoggingHelper.info(Logger, "Invoked Lambda: " + this.file);
let bodyJSON: any = JSON.parse(body);
LoggingHelper.info(Logger, "Invoking Lambda: " + this.file);
if (this.lambda === null || this.dirty) {
this.lambda = NodeUtil.load(path);
this.dirty = false;
Expand All @@ -88,6 +87,7 @@ export class LambdaRunner {
// let lambda = System.import("./" + file);
let context: LambdaContext = new LambdaContext(response);
try {
let bodyJSON: any = JSON.parse(body);
this.lambda.handler(bodyJSON, context);
} catch (e) {
context.fail("Exception: " + e.message);
Expand Down Expand Up @@ -128,8 +128,6 @@ export class LambdaContext {
}

private done(success: boolean, body: any) {
let self = this;

let statusCode: number = 200;
let contentType: string = "application/json";
let bodyString: string = null;
Expand Down
5 changes: 2 additions & 3 deletions lib/client/tcp-client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/// <reference path="../../typings/index.d.ts" />

import * as net from "net";
import EventEmitter = NodeJS.EventEmitter;
import {NetworkErrorType} from "../core/global";
import {LoggingHelper} from "../core/logging-helper";

let Logger = "TCP-CLIENT";

export interface TCPClientCallback {
(data: string, errorType: NetworkErrorType, errorMessage: string): void;
(data: Buffer, errorType: NetworkErrorType, errorMessage: string): void;
}

export class TCPClient {
Expand Down Expand Up @@ -37,7 +36,7 @@ export class TCPClient {
// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on("data", function(data: Buffer) {
callback(data.toString(), null, null);
callback(data, null, null);
});

// Add a 'close' event handler for the client socket
Expand Down
30 changes: 30 additions & 0 deletions lib/core/buffer-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,34 @@ export class BufferUtil {
public static fromString(s: string): Buffer {
return new Buffer(s);
}

// Find the specified value within the buffer
public static scan(body: Buffer, sequence: Array<number>): number {
let index = -1;
for (let i = 0; i < body.length; i++) {
let val = body[i];
if (val === sequence[0]) {
let match = true;
for (let ii = 1; ii < sequence.length; ii++) {
let bodyIndex = i + ii;
if (bodyIndex >= body.length) {
match = false;
break;
}

val = body[bodyIndex];
if (val !== sequence[ii]) {
match = false;
break;
}
}

if (match) {
index = i;
break;
}
}
}
return index;
}
}
Loading

0 comments on commit ce4fc7b

Please sign in to comment.