gpt4 book ai didi

javascript - 禁止浏览器访问敏感文件

转载 作者:行者123 更新时间:2023-12-02 16:48:56 24 4
gpt4 key购买 nike

Browserify 允许在服务器和客户端上使用相同的模块,这很好。

但是,我不想将某些文件包含在 browserify bundle 中,例如包含敏感数据的属性文件。显然我不会故意包含它,但我想更进一步,防止它发生,比如意外发生或由不熟悉设置的开发人员造成。

现在我有这个:

// fail browserify but not node
try { require('./kill/browserify'); }
catch(ex) {}

...我认为应该可以工作,因为 Node 是动态包含的,而 browserify 是静态包含的,但我想知道是否有一种不那么黑客的方式?就像某种评论指令:

// is something like this possible?
/* @browserify disallow */

最佳答案

怎么样exclude

--exclude, -u Omit a file from the output bundle. Files can be globs.

If your code tries to require() that file it will throw unless you've provided another mechanism for loading it.

上面的答案仅在运行时有效,正如 @greim 提到的。另一个在捆绑时起作用的解决方案是使用 browserify 转换器将 protected 文件别名为不存在的文件。

使用别名:

aliasify = require('aliasify').configure({
aliases: {
"file-protected": "./path/to/protected"
}
});

var b = browserify();
b.transform(aliasify);
...

关于javascript - 禁止浏览器访问敏感文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26854796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com