简体   繁体   English

在单个模块中覆盖node.js querystring.escape

[英]Overriding node.js querystring.escape within a single module

I would like to use querystring.stringify on an object. 我想在对象上使用querystring.stringify。 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. Querystring不会逃避这些(它们通常不需要)但是文档说querystring.escape是专门公开的,所以我们可以使用我们自己的函数覆盖它。 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. node.js文档说,模块只加载一次,原始实例返回到后续的require调用。 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. 显然我可以编写一个在传统调用querystring.stringify之后进行替换的包装器,但我很好奇,因为标准节点模块确实有一个“全局”设置对我来说似乎很奇怪,除非实际上有某种方式毕竟需要一个独特的实例。

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. 因此,您可以通过其中一个使用自己的querystring来隔离脚本。

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. 但是,在同一个过程中,Node没有提供绕过此功能的官方方法来检索单个模块的唯一实例。


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. 好吧,如果URL编码的值有其他字符编码,则它仍然有效。 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 ). 但是,这通常是一个奇怪的选择(例外情况是你自己的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: 并且,覆盖querystring.escapequerystring.unescape的能力是设计的:

The escape function used by querystring.stringify , provided so that it could be overridden if necessary . 提供querystring.stringify使用的转义函数, 以便在必要时可以覆盖它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM