Skip to content

Commit

Permalink
Remove js-sha256 dependency (#12)
Browse files Browse the repository at this point in the history
* Make a custom generated "stamp" instead of a hash as crypto provide it in promise, making sha cost perofmance and we don't need cryptographic hash to keep the object changes
  • Loading branch information
balazskreith authored Mar 24, 2023
1 parent c14fb55 commit dcff4c8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 45 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@observertc/client-monitor-js",
"version": "1.3.1",
"version": "1.3.2",
"description": "ObserveRTC Client Integration Javascript Library",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -31,7 +31,6 @@
"@types/uuid": "^8.3.4",
"bowser": "^2.11.0",
"js-base64": "^3.7.2",
"js-sha256": "^0.9.0",
"loglevel": "^1.8.0",
"protobufjs": "^6.11.3",
"uuid": "^8.3.2"
Expand Down
10 changes: 5 additions & 5 deletions src/ClientDevices.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Bowser from "bowser";
import { Browser, Engine, OperationSystem, Platform } from "@observertc/monitor-schemas";
import { createLogger } from "./utils/logger";
import { hash } from "./utils/hash";
import { makeStamp } from "./utils/hash";

// import * as proto from "./ProtobufSamples"
const logger = createLogger("ClientDevices");
Expand Down Expand Up @@ -67,10 +67,10 @@ export class ClientDevices {
}
}
this._actualHashes = {
os: hash(this._os),
browser: hash(this._browser),
platform: hash(this._platform),
engine: hash(this._engine),
os: makeStamp(this._os),
browser: makeStamp(this._browser),
platform: makeStamp(this._platform),
engine: makeStamp(this._engine),
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/entries/PeerConnectionEntryImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
PeerConnectionEntry,
AudioPlayoutEntry,
} from "./StatsEntryInterfaces";
import { hash as objHash } from "../utils/hash";
import { makeStamp as objHash } from "../utils/hash";
import { W3CStats as W3C } from "@observertc/monitor-schemas";

/*eslint-disable @typescript-eslint/no-explicit-any */
Expand Down
56 changes: 22 additions & 34 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
import { sha256 } from "js-sha256";

/*eslint-disable @typescript-eslint/no-explicit-any */
export function hash(obj: any): string {
const digester = sha256.create();
export function makeStamp(obj: any): string {
// const digester = sha256.create();
if (obj === undefined) {
digester.update("undefined");
return digester.hex();
// digester.update("undefined");
return '0';
}
if (obj === null) {
digester.update("undefined");
return digester.hex();
// digester.update("undefined");
return '1';
}
const type = typeof obj;
if (type === "function") {
digester.update("function");
return digester.hex();
// digester.update("function");
return '2';
}

if (type === "object") {
const stringbuffer: string[] = [];
for (const [key, value] of Object.entries(obj)) {
const valueHash = hash(value);
digester.update(key + valueHash);
stringbuffer.push(makeStamp(value));
}
return digester.hex();
}
switch (type) {
case "bigint":
digester.update((obj as bigint).toString());
break;
case "boolean":
digester.update((obj as boolean).toString());
break;
case "number":
digester.update((obj as number).toString());
break;
case "string":
digester.update(obj as string);
break;
case "undefined":
digester.update("undefined");
break;
case "symbol":
digester.update((obj as symbol).toString());
break;
let result = stringbuffer.join('');
while (256 < result.length) {
const charcodes: number[] = [];
for (let i = 0; i < result.length; i += 2) {
const charCode = result.charCodeAt(i) ^ result.charCodeAt(i + 1);
charcodes.push(charCode);
}
result = String.fromCharCode(...charcodes);
}
return result;
}
return digester.hex();
return `${obj}`;
}
6 changes: 3 additions & 3 deletions tests/utils/hash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hash } from "../../src/utils/hash";
import { makeStamp as hash } from "../../src/utils/hash";
describe("hash", () => {
describe("executions on differnet type of inputs", () => {
it("When undefined is hashed Then its not undefined", () => {
Expand All @@ -21,10 +21,10 @@ describe("hash", () => {
const hashedObj = hash(source);
expect(hashedObj).not.toBe(source);
});
it("When string is hashed Then its not the same", () => {
it("When string is hashed Then its the same", () => {
const source = "Simpsons";
const hashedObj = hash(source);
expect(hashedObj).not.toBe(source);
expect(hashedObj).toBe(source);
});
it("When function is hashed Then its not the same", () => {
const source = function (): string { return "something"; }
Expand Down

0 comments on commit dcff4c8

Please sign in to comment.