Skip to content

Commit

Permalink
Merge pull request #49 from oslabs-beta/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
Dnaganog committed Aug 11, 2019
2 parents acf2744 + ec94092 commit 71afd6c
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("Server unary decorator tests.", () => {
});

describe("Tests for metadata passing", () => {
it("Passes send message into the send callback as the second parameter.", () => {
xit("Passes send message into the send callback as the second parameter.", () => {
const fakeMetadata = { message: "hello" };
object.set(fakeMetadata);
object.send({ fakemessage: "fakemessage" });
Expand Down
98 changes: 94 additions & 4 deletions examples/firecomm/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const firecomm = require("../../index");

let certificate = path.join(__dirname, "/server.crt");

// const stub = new firecomm.Stub(routeguide.RouteGuide, "localhost:3000");
const stub = new firecomm.Stub(routeguide.RouteGuide, "localhost:3000");

const healthStub = new firecomm.HealthStub("localhost:3000");
// const healthStub = new firecomm.HealthStub("localhost:3000");

// healthStub
// .check()
Expand Down Expand Up @@ -55,11 +55,101 @@ const firstChat = {
// .send({ message: "from client" })
// .on(data => console.log(data));

// duplexStream.on(({ message }) => {
// console.log(message);
stub.unaryChat({ hello: "metadata" })
.send({firstChat})
.on(data => console.log(data))
.on('metadata', data => console.log(data))
.on('status', data => console.log(data))
.catch(err => console.error(err))

// const testClientStream = () => {
// const clientStream = stub.clientStream((err, res) => {
// if (err) console.log(err);
// console.log({ res });
// });
// clientStream.write(firstChat);
// clientStream.end();
// };

// testClientStream();

// const newClient = stub.clientStream(
// {hello: 'world yo',
// options: {
// idempotentRequest: true,
// cacheableRequest: true,
// corked: true,
// waitForReady: false,
// }})
// .send({message: 'yolo'})
// .send(firstChat)
// .on((data) => console.log({ data }))
// .on('status', (status) => console.log({ status }))
// .on('metadata', (metadata) => console.log(metadata, metadata.getMap()))
// .catch(err => console.log({ err }))

// setInterval(()=>{
// newClient.send({message:'please'})
// }, 1000)

// const testServerStream = () => {
// const serverStream = stub.serverStream(firstChat);
// const serverStream = stub.serverStream(
// {
// stream: 'server',
// options: {
// idempotentRequest: true,
// cacheableRequest: true,
// corked: true,
// waitForReady: false,
// }
// }
// );
// serverStream.write(firstChat);
// serverStream.on("data", data => {
// console.log(data);
// })
// serverStream.on("status", status => {
// console.log(status);
// })
// serverStream.on("metadata", metadata => {
// console.log(metadata);
// })
// .catch((err) => console.error(err));
// };
// testServerStream();


// const duplexStream = stub.bidiChat(
// {bidi: 'meta',
// options: {
// idempotentRequest: true,
// cacheableRequest: true,
// corked: true,
// waitForReady: false,
// }}
// );
// duplexStream.write({ message: "from client" });
// duplexStream.on("data", ({message}) => {
// // console.log(message);
// duplexStream.send({ message: "from client2" });
// });

// duplexStream.catch(err => {
// console.log({ err });
// });

// const duplexStream = stub.bidiChat({meta: 'data'}, [interceptorProvider])
// .send({ message: "from client" })
// .on((data) => console.log(data))

// duplexStream.on(({message}) => {
// console.log(message);
// duplexStream.send({ message: "from client2" });
// })

// duplexStream.catch((err => {
// console.log({ err });
// }));

// testBidiChat();
28 changes: 17 additions & 11 deletions examples/firecomm/methodHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,41 @@ function unaryChat(call) {
// ctx.setMeta({'hello': 'world'})
// ctx.send({message: 'what\'s up'});
// throw new Error("uncaught error");
ctx.send({ message: "it works" });
call.send({ message: "it works" });
}

function serverStream(context) {
function serverStream(call) {
// console.log(context);
call.set({server: 'streamed metadata'})
let count = 0;
setInterval(() => {
count += 1;
context.send({ message: " World" + count });
call.send({ message: " World" + count });
}, 1000);
}

function clientStream(context) {
function clientStream(call) {
// console.log(context.__proto__);
// console.log('serverStream context: ', context);
// console.log(context.metadata, context.metaData);
context.on("data", data => {
console.log(call.head);
call.set(
{please: 'work', dear: 'dog'}
)
// console.log(call.metadata);
call.on("data", data => {
console.log(data);
});
setTimeout(() => context.send({ message: "world" }), 3000);
setTimeout(() => call.send({ message: "world" }), 5000);
}

function bidiChat(context) {
function bidiChat(call) {
// console.log('context keys', Object.keys(context));
// console.log('context proto', context.__proto__)

context.on("data", data => {
console.log("data:", data);
context.send({ message: data.message + " World" });
console.log(call.head);
call.on("data", data => {
// console.log("data:", data);
call.send({ message: data.message + " World" });
});
}

Expand Down
1 change: 0 additions & 1 deletion lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ module.exports = function build(
const packageName = Object.keys(pkg)[0];
pkg = pkg[packageName];
const serviceKeys = Object.keys(pkg);
console.log({ serviceKeys });
for (let i = 0; i < serviceKeys.length; i++) {
if (pkg[serviceKeys[i]].hasOwnProperty("service")) {
pkg[serviceKeys[i]]._serviceName = serviceKeys[i];
Expand Down
14 changes: 7 additions & 7 deletions lib/callFactories/callDecorators/serverUnaryDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ const generateMeta = require("../../utils/generateMeta");
// .throw passes everything into call... if they pass a second parameter it becomes trailers
// set() adds things to the metadata...

module.exports = function serverUnaryDecorator(customCall, sendCallback) {
customCall.metadata = undefined;
customCall.sendCallback = sendCallback;
module.exports = function serverUnaryDecorator(clone, sendCallback) {
clone.metadata = undefined;
clone.sendCallback = sendCallback;

customCall.send = function(message, flags) {
clone.send = function(message, flags) {
if (this.metadata) {
this.call.sendMetadata(this.metadata);
this.call.sendMetadata(generateMeta(this.metadata));
}
this.sendCallback(null, message, null, flags);
};

customCall.set = function(object) {
clone.set = function(object, options) {
// loop through
if (typeof object !== "object") {
throw new Error("Set should be passed an object with string values.");
Expand All @@ -32,7 +32,7 @@ module.exports = function serverUnaryDecorator(customCall, sendCallback) {
}
};

customCall.throw = function(err, trailers) {
clone.throw = function(err, trailers) {
if (!(err instanceof Error)) {
throw new Error(
"Please pass your error details as an Error class. Firecomm supports adding additional error metadata in the trailers property using call.setStatus()"
Expand Down
129 changes: 61 additions & 68 deletions lib/clientCalls/clientStreamCall.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,87 @@
const generateMeta = require("../utils/generateMeta");

// (metaObject, interceptorArray, callback)

module.exports = function clientStreamCall(that, methodName, first, second) {
let metadata = null;
let interceptors = null;
let callback = () => {};
if (typeof first === "object") {
if (Array.isArray(first)) {
interceptors = { interceptors: first };
} else {
metadata = generateMeta(first);
const {options} = first;
delete first.options;
metadata = generateMeta(first, options);
}
}
if (typeof second === "object") {
if (Array.isArray(second)) {
interceptors = { interceptors: second };
} else {
metadata = generateMeta(second);
const {options} = second;
delete second.options;
metadata = generateMeta(second, options);
}
}
const sender = that[methodName](metadata, interceptors, callback);
console.log(metadata);
console.log(interceptors);
let callback = function(err, res) {
if (err) clientStreamObj.$catch(err);
clientStreamObj.$on(res);
};
let sender = that[methodName](metadata, interceptors, callback);
const clientStreamObj = {
// throw: function(metadata) {
// sender.throw()
// },
getPeer: function() {
return sender.getPeer();
},
write: function(...args) {
return this.send(...args)
},
send: function(message, flags, flushCallback) {
sender.write(message, flags, flushCallback);
return clientStreamObj;
},
$catch: function() {
throw new Error(
"Unary Call: .catch and .on must be defined before .send"
);
},
catch: function(first) {
if (typeof first !== "function") {
throw new Error("Client Stream: catch takes a callback");
}
clientStreamObj.$catch = first;
return clientStreamObj;
},
$on: () => {
throw new Error(
"Client Stream: .catch and .on must be invoked to .send"
);
},
on: function(first, second) {
let listenerCallback;
if (typeof first !== "function" && typeof second !== "function") {
throw new Error("Client Stream: on takes a callback");
}
if (typeof first === "function") {
listenerCallback = first;
} else {
listenerCallback = second;
}
"Client Stream: .catch and .on must be defined to .send"
);
},
catch: function(first) {
if (typeof first !== "function") {
throw new Error("Client Stream: catch takes a callback");
}
clientStreamObj.$catch = first;
return clientStreamObj;
},
$on: (res) => {
throw new Error(
"Client Stream: .catch and .on must be invoked to .send"
);
},
on: function(first, second) {
let listenerCallback;
if (typeof first !== "function" && typeof second !== "function") {
throw new Error("Client Stream: on takes a callback");
}
if (typeof first === "function") {
listenerCallback = first;
} else {
listenerCallback = second;
}
switch (first) {
case 'status':
sender.on('status', second);
break;
case 'metadata':
sender.on('metadata', second);
break;
case 'error':
clientStreamObj.catch(second);
break;
default:
clientStreamObj.$on = listenerCallback;
return clientStreamObj;
}
};
callback = function(err, res) {
if (err) clientStreamObj.$catch(err);
clientStreamObj.$on(res);
return clientStreamObj;
};
}
return clientStreamObj;
};

// module.exports = function clientStreamCall(that, methodName, ...args) {
// let interceptors = undefined;
// let metadata = undefined;
// let callback = undefined;
// let hasFunction = false;
// args = args.filter(arg => args !== undefined);
// for (let i = 0; i < args.length; i++) {
// if (typeof args[i] === "function") {
// callback = args[i];
// hasFunction = true;
// } else {
// if (typeof (args[i] === "object")) {
// if (Array.isArray(args[i])) {
// interceptors = { interceptors: args[i] };
// } else {
// metadata = generateMeta(args[i]);
// }
// }
// }
// }
// if (!hasFunction) {
// throw new Error("Must include a callback function to client Stream Call");
// }
// return that[methodName](metadata, interceptors, callback);
// };
}
};
return clientStreamObj;
};

0 comments on commit 71afd6c

Please sign in to comment.