Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add mung.write function #14

Merged
merged 11 commits into from
Apr 7, 2018

Conversation

hedgeday
Copy link
Contributor

@hedgeday hedgeday commented Apr 4, 2018

No description provided.

index.js Outdated
// If response type is not application/json,
// just call the original res.write function
if (!(contentType && ~contentType.indexOf('application/json'))) {
return original.call(res, body);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about ...args?

index.js Outdated
}

// If null, then 204 No Content
if (!modifiedJSON) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mung;.json allows the result object to be modified in-place or a new object returned. This only allows a new object to be returned by the mung function.

index.js Outdated
return res.status(204).end();
}

res.set('Content-Length', Buffer.byteLength(JSON.stringify(modifiedJSON), 'utf8'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about cases when charset other than utf8 is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I can add encoding as an option.

@richardschneider
Copy link
Owner

richardschneider commented Apr 5, 2018

@hedgeday thanks for all your hard work!

Could you also modify the readme to describe this new function.

Also, please update the contributors section of package.json with you name and email.

@hedgeday
Copy link
Contributor Author

hedgeday commented Apr 5, 2018

@richardschneider Happy to contribute! 👍

I was thinking about the use case overall, and I'm starting to think it would make sense to have a more generic mung.write function, with the "JSON" aspect from my specific use case being a responsibility of the client that uses this middleware. That should make this function more semantically correct (as in, "monkey patch/hook a function that is available on the res object in express") and generically useful.

Also for the record it looks like your comments got buried after I patched that commit with minor formatting changes (my bad), so I'm posting it here again.

I'll update the encoding value to use the node res.write default (which, as per the docs, is 'utf-8') and add tests.

I'll update this PR to reflect those changes.

@hedgeday hedgeday changed the title feat: Add writeJson function feat: Add mung.write function Apr 6, 2018
test/write.js Outdated
})

function appendText (chunk, req, res) {
return (chunk + ' with more content')
Copy link
Contributor Author

@hedgeday hedgeday Apr 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you mentioned that mung.json has the ability to modify the passed-in object in place, but in the case of res.write, since we are dealing with immutable strings and/or buffers, the function will have to return the manipulated value. I can add this constraint to the README along with the description of this function.

index.js Outdated
const original = res.write;
const mungError = options.mungError;

res.write = function (chunk, encoding, ...args) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res.write has three arguments, Lets use them specifically.

index.js Outdated
}

try {
let modifiedChunk = fn(chunk, req, res);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pass the encoding to the mung function. Make sure null is passed if its really the callback.

Something like

let enc = typeof encoding === 'function' ? null : enc;
let modifiedChunk = fn(chunk, enc, req, res);

index.js Outdated

// Encoding defaults to utf-8 as per the spec defined in the node.js docs.
// The encoding value can be overridden
res.set('Content-Length', Buffer.byteLength(modifiedChunk, encoding));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res.write hook should not set the Content-Length.

index.js Outdated
// Encoding defaults to utf-8 as per the spec defined in the node.js docs.
// The encoding value can be overridden
res.set('Content-Length', Buffer.byteLength(modifiedChunk, encoding));
return original.apply(res, [ modifiedChunk, [].slice.call(arguments, 1) ])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass the named arguments

@richardschneider
Copy link
Owner

Really like the new changes, much better approach! See the minor changes I'll like.

index.js Outdated
const mungError = options.mungError;

res.write = function (chunk, encoding, callback) {
if (res.headersSent) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res.write can be called after the headers are sent. Please remove the check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's true. Will update.

On that note, since res.write can be called multiple times before res.end, I'm thinking I'll also remove the 204 status check (we can't assume No content just because one call to res.write returns null).

index.js Outdated
res
);

if (res.headersSent) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res.write can be called after the headers are sent. Please remove the check.

…ove 204 response setting, minor formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants