jshttp / negotiator

An HTTP content negotiator for Node.js
MIT License
320 stars 36 forks source link

[Question] More preferredCharset() stuff #56

Closed AnyhowStep closed 5 years ago

AnyhowStep commented 5 years ago
preferredCharsets("first,second,third")
//Actual Output: ["first", "second", "third"]

preferredCharsets("first,second,third", [])
//Actual Output: []
//Should the output, instead be, ["first", "second", "third"] ?

I feel like passing undefined and passing an empty array should mean the same thing. But, I'm not sure how charset negotiation works.

https://github.com/jshttp/negotiator/blob/89ab00343fd99d21df2a1ce03b832c1c0d27a599/lib/charset.js#L126


This other question is more contrived,

preferredCharsets("SECOND,first,second,third", ["SeCoNd", "first"])
//Actual Output: ["first", "SeCoNd"]
//Should the output, instead be, ["SeCoNd", "first"] ?

I'm looking at this line, in particular,

https://github.com/jshttp/negotiator/blob/89ab00343fd99d21df2a1ce03b832c1c0d27a599/lib/charset.js#L88

It says, priority.o - spec.o. So, getCharsetPriority() will set the o value of "SeCoNd" to 2, the index of "second", instead of 0, the index of "SECOND".

If we change that line to spec.o - priority.o, the o value of "SeCoNd" becomes 0, and will come before "first".

But, of course, realistically, who's going to send such a header where there are duplicate charsets, and they're not consecutive, with lower-priority available charsets requested in between?


You shouldn't have to do anything about the second question. I just like reading random pieces of code and poking at them :x

dougwilson commented 5 years ago

I feel like passing undefined and passing an empty array should mean the same thing. But, I'm not sure how charset negotiation works.

So it's not that you're passing undefined in this API: it's that those are two different function signatures (https://github.com/jshttp/negotiator#methods-2):

charset() Returns the most preferred charset from the client.

and

charset(availableCharsets) Returns the most preferred charset from a list of available charsets.

AnyhowStep commented 5 years ago

Oh. I should pay more attention to the documentation than the internals... Thank you