admin管理员组

文章数量:1414930

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.

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.

Share Improve this question edited Aug 1, 2013 at 2:07 Semicolon asked Aug 1, 2013 at 0:55 SemicolonSemicolon 7,5132 gold badges31 silver badges41 bronze badges 1
  • Thank you for this: github./flatiron/cradle/pull/313/… – ceremcem Commented Oct 16, 2016 at 12:13
Add a ment  | 

1 Answer 1

Reset to default 6

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.

本文标签: javascriptOverriding nodejs querystringescape within a single moduleStack Overflow