mirror of
https://github.com/emn178/js-sha256.git
synced 2026-08-12 20:32:16 +08:00
fix recognize environment is node when use browserfs #53
This commit is contained in:
@@ -36,6 +36,23 @@ function runWindowTest() {
|
||||
unset();
|
||||
}
|
||||
|
||||
function runBrowserWithProcessPolyfillTest() {
|
||||
var Module = require('module');
|
||||
var originalRequire = Module.prototype.require;
|
||||
window = global;
|
||||
Module.prototype.require = function (id) {
|
||||
if (id === 'crypto' || id === 'buffer') {
|
||||
throw new Error('Node.js module loaded in browser environment');
|
||||
}
|
||||
return originalRequire.apply(this, arguments);
|
||||
};
|
||||
try {
|
||||
runCommonJsTest();
|
||||
} finally {
|
||||
Module.prototype.require = originalRequire;
|
||||
}
|
||||
}
|
||||
|
||||
// Node.js env
|
||||
BUFFER = true;
|
||||
runCommonJsTest();
|
||||
@@ -44,6 +61,9 @@ runCommonJsTest();
|
||||
JS_SHA256_NO_BUFFER_FROM = true
|
||||
runCommonJsTest();
|
||||
|
||||
// browser env with a Node.js process polyfill
|
||||
runBrowserWithProcessPolyfillTest();
|
||||
|
||||
// Webpack browser env
|
||||
JS_SHA256_NO_NODE_JS = true;
|
||||
window = global;
|
||||
|
||||
Reference in New Issue
Block a user