Closed Neekoy closed 6 years ago
I'm about to go on a long camping trip and don't have time to investigate until after I return. Off the top of my head, I would probably suggest changing all of the formatters to use res.send()
instead of done()
and poke around in the restify formatters to see how they changed.
So I've been trying to do what you suggested but got stuck a bit. The formatter we're erroring for is specifically for "application/json" so I edited the following file: lib/formatter/json-formatter.js
"use strict";
/**
* @param {opentoken~genericFormatter} genericFormatter
* @return {Function}
*/
module.exports = (genericFormatter) => {
return genericFormatter.formatWithFallback((req, res, body, done) => {
var content;
if (Buffer.isBuffer(body)) {
content = body.toString("base64");
} else if (body) {
content = JSON.stringify(body);
} else {
content = "null";
}
// This is how it worked by default, changing it to use res.send instead of done
// done(null, new Buffer(`${content}\n`, "binary"));
let responseCode = res.statusCode;
let responseBody = new Buffer(`${content}\n`, "binary");
let responseHeaders = res.req.headers;
let responseFormat = res.req.headers['content-type'];
// Send function needs the following parameters
// Response.prototype.__send =
// function __send(maybeCode, maybeBody, maybeHeaders, maybeCallback, format)
res.send(responseCode, responseBody, responseHeaders, null, responseFormat);
});
};
I can't seem to figure out what callback function I should feed the send function so it would go through. Any help would be appreciated.
Restify was updated to version 5. This project was aimed at version 4. I'm pretty sure at the bottom of the 4TO5GUIDE.md
in Restify, it covers this situation. However, I don't know if this really is a problem because the code only calls res.send()
in a synchronous way and the formatters are set up properly from what I can tell.
I have replicated the issue you kindly reported. Thanks for providing so many details! The master branch has been updated and works with the example scripts. I would love to know if this also works for you. Updated code has been pushed to the master
branch.
Turns out it is easier to use sync formatters instead of async send calls, so I'm changing the code to do that. I'm also fixing tests. Should be ready soon. Look for a commit that mentions this issue.
I'm having a huge amount of problems with the functional tests, which is something I had initially when writing them. In essence, node's internal HttpResponse object's .get method calls .getHeaders. That's implemented/overridden by Restify and it returns (this._headers || {}), which shouldn't be a problem. However, this._headers is defined as a getter that calls node's .get method, which calls .getHeaders. I need to break this loop somewhere, but I don't really know where the spot should be. :-/
You could just assume that it works fine. I could rewrite the functional tests to actually start the server and perform real tests, but then I wouldn't get the coverage stats for those conditions. Not sure what is my next step here, but at least you should have a working product.
Brilliant - thank you a whole lot. I gave it a couple of tests and it seems that it's working properly now with the upgrade to restify 5.
This can be marked as resolved I believe, thank you again :)
Hello,
I tried running the code locally, but unfortunately I'm getting the following error:
I added some debug info to that function, and it would seem that most of the information is missing when the request is made:
The above console logs return the following:
Some system info:
I tried several things, but I can't seem to get it going reliably. Can you please advise onto how I can fix this?
Cheers.