feat: Merge isohash functionality into smarthash for cross-environment hash support

- Updated test files to use new tapbundle import from @git.zone/tstest.
- Created a new plan for merging isohash into smarthash, detailing objectives and implementation steps.
- Added browser-specific tests for SHA256 hashing functions in test/test.browser.ts.
- Implemented browser-compatible hashing functions in ts_web/index.ts using Web Crypto API.
- Introduced plugins for environment detection and JSON handling in ts_web/plugins.ts.
- Ensured that existing smarthash functionality remains intact and consistent across environments.
This commit is contained in:
Juergen Kunz
2025-06-19 22:44:47 +00:00
parent df0f761bdb
commit 0bae2d6eec
8 changed files with 7756 additions and 4265 deletions

65
test/test.browser.ts Normal file
View File

@ -0,0 +1,65 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as smarthash from '../ts_web/index.js';
tap.test('sha256FromString should work in browser environment', async () => {
const testHash = await smarthash.sha256FromString('test');
const testHash2 = await smarthash.sha256FromString('testString');
const testHash3 = await smarthash.sha256FromString('test');
expect(testHash).toEqual(testHash3);
expect(testHash).not.toEqual(testHash2);
expect(testHash).toBeTypeofString();
expect(testHash).toHaveLength(64); // SHA256 hash is 64 hex characters
});
tap.test('sha256FromBuffer should work with Uint8Array in browser', async () => {
const encoder = new TextEncoder();
const buffer = encoder.encode('test');
const hashFromBuffer = await smarthash.sha256FromBuffer(buffer);
const hashFromString = await smarthash.sha256FromString('test');
expect(hashFromBuffer).toEqual(hashFromString);
});
tap.test('sha265FromObject should produce reproducible hashes', async () => {
const hash1 = await smarthash.sha265FromObject({
hithere: 1,
wow: 'two',
});
const hash2 = await smarthash.sha265FromObject({
wow: 'two',
hithere: 1,
});
const hash3 = await smarthash.sha265FromObject({
wow: 'twoe',
hithere: 1,
});
expect(hash1).toEqual(hash2);
expect(hash1).not.toEqual(hash3);
});
tap.test('sha256FromStringSync should throw in browser environment', async () => {
expect(() => {
smarthash.sha256FromStringSync('test');
}).toThrow();
});
tap.test('sha256FromStream should throw in browser environment', async () => {
expect(() => {
smarthash.sha256FromStream(null);
}).toThrow();
});
tap.test('sha256FromFile should throw in browser environment', async () => {
await expect(smarthash.sha256FromFile('./test.txt')).rejects.toThrow();
});
tap.test('md5FromString should throw in browser environment', async () => {
await expect(smarthash.md5FromString('test')).rejects.toThrow();
});
export default tap.start();

View File

@ -1,4 +1,4 @@
import { tap, expect } from '@push.rocks/tapbundle';
import { tap, expect } from '@git.zone/tstest/tapbundle';
import * as fs from 'fs';
import * as smarthash from '../ts/index.js';
@ -52,4 +52,4 @@ tap.test('should create md5hash from string', async () => {
expect(md5Hash).toEqual('c6f7c372641dd25e0fddf0215375561f');
});
tap.start();
export default tap.start();