I would like to use querystring.stringify on an object. The requirements for the string are slightly off-standard, with asterisk, slashes, and apostrophe all needing to be escaped. Querystring doesn't escape these (they normally wouldn't need to be) but the documentation says that querystring.escape is exposed specifically so that we can override it with our own function. The following would work for me:
querystring.escape = function(str) {
str = encodeURIComponent(str)
.replace(/\*/g, '%2A')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/'/g, '%27');
return str;
};
My only concern is that, if I understand correctly, this might change the behavior of other modules that might also require querystring (with its normal escape function) in the future. The node.js documentation says that modules are only loaded once and that original instance is returned to subsequent require calls. Is there a way for me to force this particular instance of querystring to be unique?
Obviously I can just write a wrapper that does the replacement after a conventional call to querystring.stringify, but I'm curious because it seemed weird to me that a standard node module would really have a 'global' setting, unless there's actually some way to require a unique instance afterall.
This package has been deprecated The querystring API is considered Legacy.
escape( ) function is used to produce a percent-encoded query string from a normal string. This method is very similar to the browser's encodeURIComponent functions. This method performs percent-encoding on the given string it means it encodes any string into a URL query string by using the % symbol.
encode() # The querystring. encode() function is an alias for querystring. stringify() .
The Query String module used to provides utilities for parsing and formatting URL query strings.It can be used to convert query string into JSON object and vice-versa. The Query String is the part of the URL that starts after the question mark(?).
Is there a way for me to force this particular instance of querystring to be unique?
Not really. Node's module caching is per-process and based on the module's filepath.
An alteration wouldn't cross into/from Child Processes or Clusters. So, you could possibly isolate your script with its own querystring
through one of those.
But, within the same process, Node doesn't offer an official way to bypass this to retrieve a unique instance just for a single module.
this might change the behavior of other modules that might also require querystring (with its normal escape function) in the future.
Well, a URL-encoded value is still valid if it has additional characters encoded. It's just that normally they don't need to be.
And, I suppose, it is possible to affect modules that place expectations on encoded values. But, that's usually an odd choice (the exception being unit tests for your own querystring.escape
). So, as long as it can be decoded properly, it should be fine.
querystring.escape = function (str) { /* ... */ }; // your function here
var sample = "a(b*c)'d";
var encoded = querystring.escape(sample);
console.log(encoded); // a%28b%2ac%29'd
var decoded = querystring.unescape(encoded);
console.log(decoded); // a(b*c)'d
console.log(decoded === sample); // true
And, the ability to override querystring.escape
and querystring.unescape
is by design:
The escape function used by
querystring.stringify
, provided so that it could be overridden if necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With