Skip to content

Commit

Permalink
server-side has getPeer method and on metadata and on cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnaganog committed Aug 14, 2019
1 parent f0243c5 commit ac1d6e5
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 58 deletions.
4 changes: 2 additions & 2 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"/*, {
certificate
});
}*/);

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

Expand Down
5 changes: 3 additions & 2 deletions examples/firecomm/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ server.addService(
let certPath = path.join(__dirname, "/server.crt");
let keyPath = path.join(__dirname, "/server.key");

const result = server.bind(["0.0.0.0:3000", "192.168.10.82:3001"], {
// const result =
server.bind(["0.0.0.0:3000", "192.168.10.82:3001"]/*, {
privateKey: keyPath,
certificate: certPath
});
}*/);
// console.log({ result });
// console.log({ server });
// console.log(server.__proto__);
Expand Down
3 changes: 3 additions & 0 deletions lib/callFactories/callDecorators/basicCallDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module.exports = function(customCall, originalCall) {
customCall.call = originalCall;
customCall.state = null;
customCall.head = originalCall.metadata;
customCall.getPeer = function() {
return originalCall.getPeer();
};
};
10 changes: 7 additions & 3 deletions lib/callFactories/callDecorators/clientStreamDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
module.exports = function(customCall, originalCall) {
customCall.on = function(event, callback) {
if (typeof event === "function") {
this.call.on("data", event);
originalCall.on("data", event);
} else if (typeof event === "string") {
if (event === "data") {
this.call.on("data", callback);
originalCall.on("data", callback);
} else if (event === "metadata") {
callback(this.head);
} else if (event === "cancelled") {
originalCall.on("cancelled", callback);
} else {
this.call.on(event, callback);
originalCall.on(event, callback);
}
} else {
throw new Error(
Expand Down
6 changes: 4 additions & 2 deletions lib/callFactories/callDecorators/clientUnaryDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ module.exports = function clientUnaryDecorator(customCall, originalCall) {
} else {
if (typeof string !== "string") {
throw new Error(
'.on takes "data" parameter and a callback to be invoked on data, or just the callback.'
'.on takes "data" or "metadata" parameter and a callback to be invoked on data, or just the callback.'
);
} else {
if (string === "data") {
callback(this.body);
} else if (string === "metadata") {
callback(this.head);
} else if (event === "cancelled") {
originalCall.on("cancelled", callback);
} else {
throw new Error('Only the "data" event is support by unary .on');
throw new Error('Only the "data" and "metadata" events are supported by unary .on');
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions lib/callFactories/callDecorators/serverStreamDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function serverStreamDecorator(customCall, originalCall) {
"Please pass your error details as an Error class. Firecomm supports adding additional error metadata in the trailers property using context.setStatus()"
);
}
this.call.emit("error", err);
originalCall.emit("error", err);
return this;
};

Expand All @@ -41,12 +41,16 @@ module.exports = function serverStreamDecorator(customCall, originalCall) {
return this;
};

// customCall.write = function(...args) {
// customCall.send(...args);
// }

customCall.send = function(message) {
if (!this.metadataSent && this.metadata) {
const metadata = generateMeta(this.metadata);
this.call.sendMetadata(metadata);
originalCall.sendMetadata(metadata);
}
this.call.write(message);
originalCall.write(message);
};
return this;
};
55 changes: 9 additions & 46 deletions lib/callFactories/callDecorators/serverUnaryDecorator.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
const grpc = require("grpc");
const generateMeta = require("../../utils/generateMeta");
// server unary

// .send () // writes
// .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.write = function(...args) {
// customCall.send(...args);
// }

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

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

clone.throw = function(err, trailers) {
customCall.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 All @@ -60,40 +59,4 @@ module.exports = function serverUnaryDecorator(clone, sendCallback) {
this.sendCallback(err, {}, this.trailers);
return this;
};
// ServerUnaryCallClone.send = function(message = {}) {
// if (this.metaData) {
// this.sendMetadata(this.metaData);
// }
// this.sendCallback(this.err, message, this.trailer);
// };
// return ServerUnaryCallClone;
// }

// ServerUnaryCallClone.throw = function(err) {
// 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()"
// );
// }
// this.sendCallback(err, {}, this.trailer);
// };
// ServerUnaryCallClone.setMeta = function(metaObject) {
// if (!this.metaData) {
// this.metaData = new grpc.Metadata();
// }
// const keys = Object.keys(metaObject);
// for (let i = 0; i < keys.length; i++) {
// this.metaData.set(keys[i], metaObject[keys[i]]);
// }
// };

// ServerUnaryCallClone.setStatus = function(metaObject) {
// if (!this.trailer) {
// this.trailer = new grpc.Metadata();
// }
// const keys = Object.keys(metaObject);
// for (let i = 0; i < keys.length; i++) {
// this.trailer.set(keys[i], metaObject[keys[i]]);
// }
// };
};

0 comments on commit ac1d6e5

Please sign in to comment.