javascript - Overriding node.js querystring.escape within a single module -
i use querystring.stringify on object. requirements string off-standard, asterisk, slashes, , apostrophe needing escaped. querystring doesn't escape these (they wouldn't need be) documentation says querystring.escape exposed can override our own function. following work me:
querystring.escape = function(str) { str = encodeuricomponent(str) .replace(/\*/g, '%2a') .replace(/\(/g, '%28') .replace(/\)/g, '%29') .replace(/'/g, '%27'); return str; }; my concern that, if understand correctly, might change behavior of other modules might require querystring (with normal escape function) in future. node.js documentation says modules loaded once , original instance returned subsequent require calls. there way me force particular instance of querystring unique?
obviously can write wrapper replacement after conventional call querystring.stringify, i'm curious because seemed weird me standard node module have 'global' setting, unless there's way require unique instance afterall.
is there way me force particular instance of querystring unique?
not really. node's module caching per-process , based on module's filepath.
an alteration wouldn't cross into/from child processes or clusters. so, possibly isolate script own querystring through 1 of those.
but, within same process, node doesn't offer official way bypass retrieve unique instance single module.
this might change behavior of other modules might require querystring (with normal escape function) in future.
well, url-encoded value still valid if has additional characters encoded. it's don't need be.
and, suppose, possible affect modules place expectations on encoded values. but, that's odd choice (the exception being unit tests own querystring.escape). so, long can decoded properly, should fine.
querystring.escape = function (str) { /* ... */ }; // 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, ability override querystring.escape , querystring.unescape design:
the escape function used
querystring.stringify, provided overridden if necessary.
Comments
Post a Comment