### Added

- Modern and lite builds generated from the same source.
- support ESM.
- Set up GitHub release action / provenance

### Fixed
- Fix uploading coverage to coveralls
- fix recognize environment is node when use browserfs
This commit is contained in:
Yi-Cyuan Chen
2026-07-20 10:19:42 +08:00
parent 6f25176e1e
commit b02f0d93f3
37 changed files with 10048 additions and 1073 deletions
+60
View File
@@ -0,0 +1,60 @@
var expect = require('expect.js');
var fs = require('fs');
var vm = require('vm');
var EXPECTED_MD5 = '900150983cd24fb0d6963f7d28e17f72';
var EXPECTED_HMAC = 'd2fe98063f876b03193afb49b4979591';
var load = function (file) {
delete require.cache[require.resolve(file)];
return require(file);
};
var testCore = function (md5) {
expect(md5('abc')).to.be(EXPECTED_MD5);
expect(md5.create().update('a').update('bc').hex()).to.be(EXPECTED_MD5);
expect(md5.array('abc')).to.have.length(16);
expect(md5.digest('abc')).to.have.length(16);
expect(md5.arrayBuffer('abc').byteLength).to.be(16);
};
var loadBrowser = function (file) {
var context = {
window: {},
ArrayBuffer: ArrayBuffer,
Uint8Array: Uint8Array,
Uint32Array: Uint32Array
};
vm.runInNewContext(fs.readFileSync(file, 'utf8'), context);
return context.md5 || context.window.md5;
};
var legacy = load('../build/md5.min.js');
testCore(legacy);
expect(legacy.base64).to.be.a('function');
expect(legacy.buffer).to.be.a('function');
expect(legacy.hmac('key', 'abc')).to.be(EXPECTED_HMAC);
var modern = load('../build/md5.modern.min.js');
testCore(modern);
expect(modern.base64).to.be.a('function');
expect(modern.buffer).to.be.a('function');
expect(modern.hmac('key', 'abc')).to.be(EXPECTED_HMAC);
var lite = load('../build/md5-lite.min.js');
testCore(lite);
expect(lite.base64).to.be(undefined);
expect(lite.buffer).to.be(undefined);
expect(lite.hmac).to.be(undefined);
testCore(load('../build/md5.js'));
testCore(load('../build/md5.modern.js'));
testCore(load('../build/md5-lite.js'));
testCore(loadBrowser('build/md5.modern.min.js'));
var browserLite = loadBrowser('build/md5-lite.min.js');
testCore(browserLite);
expect(browserLite.base64).to.be(undefined);
expect(browserLite.buffer).to.be(undefined);
expect(browserLite.hmac).to.be(undefined);
console.log('Build variants: PASS');
+24
View File
@@ -0,0 +1,24 @@
import expect from 'expect.js';
import md5, {md5 as namedMd5} from 'js-md5';
import browserMd5 from 'js-md5/build/md5.mjs';
import minBrowserMd5 from 'js-md5/build/md5.min.mjs';
import modernMd5 from 'js-md5/build/md5.modern.mjs';
import minModernMd5 from 'js-md5/build/md5.modern.min.mjs';
import liteMd5 from 'js-md5/build/md5-lite.mjs';
import minLiteMd5 from 'js-md5/build/md5-lite.min.mjs';
const EXPECTED_MD5 = '900150983cd24fb0d6963f7d28e17f72';
[md5, namedMd5, browserMd5, minBrowserMd5, modernMd5, minModernMd5,
liteMd5, minLiteMd5].forEach(hash => {
expect(hash('abc')).to.be(EXPECTED_MD5);
});
expect(md5.hmac).to.be.a('function');
expect(browserMd5.hmac).to.be.a('function');
expect(modernMd5.hmac).to.be.a('function');
expect(liteMd5.hmac).to.be(undefined);
expect(liteMd5.base64).to.be(undefined);
expect(liteMd5.buffer).to.be(undefined);
console.log('ESM imports: PASS');
+21
View File
@@ -30,6 +30,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();
@@ -38,8 +55,12 @@ runCommonJsTest();
JS_MD5_NO_BUFFER_FROM = true
runCommonJsTest();
// browser env with a Node.js process polyfill
runBrowserWithProcessPolyfillTest();
// Webpack browser env
JS_MD5_NO_NODE_JS = true;
window = global;
runCommonJsTest();
// browser env