mirror of
https://github.com/emn178/js-sha1.git
synced 2026-11-17 14:18:44 +08:00
### Added
- ESM builds and package `exports`. ### Fixed - Incorrect hash for strings with unpaired UTF-16 surrogates; they are now encoded as U+FFFD, the same as `TextEncoder`. - Detect browsers with a `process` polyfill (e.g. BrowserFS) and Electron renderers as non-Node.js environments. ### Changed - Replace Travis CI with GitHub Actions. - Update dev dependencies and fix security issues. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
fd520b41fa
commit
3e1a92f2a4
+49
-50
@@ -73,6 +73,55 @@ define.amd = true;
|
||||
require('../src/sha1.js');
|
||||
unset();
|
||||
|
||||
// browser with a process polyfill (e.g. BrowserFS)
|
||||
window = global;
|
||||
runCommonJsTest();
|
||||
|
||||
// Electron renderer
|
||||
process.type = 'renderer';
|
||||
sha1 = require('../src/sha1.js');
|
||||
delete process.type;
|
||||
require('./test.js');
|
||||
require('./hmac-test.js');
|
||||
unset();
|
||||
|
||||
// environment detection in isolated contexts (no Node.js globals)
|
||||
describe('environment detection', function () {
|
||||
var vm = require('vm');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var source = fs.readFileSync(path.join(__dirname, '../src/sha1.js'), 'utf8');
|
||||
|
||||
function run(sandbox) {
|
||||
vm.runInContext(source, vm.createContext(sandbox));
|
||||
return sandbox.sha1;
|
||||
}
|
||||
|
||||
function check(sha1) {
|
||||
expect(sha1('abc')).to.be('a9993e364706816aba3e25717850c26c9cd0d89d');
|
||||
expect(sha1('中文')).to.be('7be2d2d20c106eee0836c9bc2b939890a78e8fb3');
|
||||
expect(sha1.hmac('key', 'abc')).to.be('4fd0b215276ef12f2b3e4c8ecac2811498b656fc');
|
||||
}
|
||||
|
||||
it('should not treat a browser with a process polyfill as Node.js', function () {
|
||||
var sandbox = { process: { versions: { node: '18.0.0' }, env: {} } };
|
||||
sandbox.window = sandbox.self = sandbox;
|
||||
check(run(sandbox));
|
||||
});
|
||||
|
||||
it('should not treat an Electron renderer as Node.js', function () {
|
||||
var sandbox = { process: { type: 'renderer', versions: { node: '20.0.0' } } };
|
||||
sandbox.window = sandbox.self = sandbox;
|
||||
check(run(sandbox));
|
||||
});
|
||||
|
||||
it('should expose to the global scope in a web worker', function () {
|
||||
var sandbox = {};
|
||||
sandbox.self = sandbox;
|
||||
check(run(sandbox));
|
||||
});
|
||||
});
|
||||
|
||||
// webworker
|
||||
WORKER = 'tests/worker.js';
|
||||
SOURCE = 'src/sha1.js';
|
||||
@@ -106,53 +155,3 @@ importScripts = function () {};
|
||||
|
||||
sha1 = require('../src/sha1.js');
|
||||
require('./worker-test.js');
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// sha1 = require('../src/sha1.js');
|
||||
// require('./test.js');
|
||||
|
||||
// delete require.cache[require.resolve('../src/sha1.js')];
|
||||
// delete require.cache[require.resolve('./test.js')];
|
||||
// delete require.cache[require.resolve('./hmac-test.js')];
|
||||
// sha1 = null;
|
||||
|
||||
// // Webpack browser env
|
||||
// JS_SHA1_NO_NODE_JS = true;
|
||||
// window = global;
|
||||
// sha1 = require('../src/sha1.js');
|
||||
// require('./test.js');
|
||||
|
||||
// delete require.cache[require.resolve('../src/sha1.js')];
|
||||
// delete require.cache[require.resolve('./test.js')];
|
||||
// delete require.cache[require.resolve('./hmac-test.js')];
|
||||
// sha1 = null;
|
||||
|
||||
// // browser env
|
||||
// JS_SHA1_NO_NODE_JS = true;
|
||||
// JS_SHA1_NO_COMMON_JS = true;
|
||||
// window = global;
|
||||
// require('../src/sha1.js');
|
||||
// require('./test.js');
|
||||
|
||||
// delete require.cache[require.resolve('../src/sha1.js')];
|
||||
// delete require.cache[require.resolve('./test.js')];
|
||||
// delete require.cache[require.resolve('./hmac-test.js')];
|
||||
// sha1 = null;
|
||||
|
||||
// // browser AMD
|
||||
// JS_SHA1_NO_NODE_JS = true;
|
||||
// JS_SHA1_NO_COMMON_JS = true;
|
||||
// window = global;
|
||||
// define = function (func) {
|
||||
// sha1 = func();
|
||||
// require('./test.js');
|
||||
// require('./hmac-test.js');
|
||||
// };
|
||||
// define.amd = true;
|
||||
|
||||
// require('../src/sha1.js');
|
||||
|
||||
Reference in New Issue
Block a user