Skip to content

Commit 6bc4151

Browse files
authored
Merge pull request #359 from FoxxMD/objectHashReplace
fix: Update object hashing function
2 parents e061540 + 7cee9f0 commit 6bc4151

File tree

7 files changed

+28
-12
lines changed

7 files changed

+28
-12
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"mopidy": "^1.3.0",
101101
"mpd-api": "^1.1.2",
102102
"nanoid": "^3.3.1",
103+
"node-object-hash": "^3.1.1",
103104
"normalize-url": "^8.0.1",
104105
"ntfy": "^1.7.6",
105106
"object-hash": "^3.0.0",

src/backend/common/vendor/chromecast/ChromecastClientUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Media, MediaController, Result } from "@foxxmd/chromecast-client";
2-
import objectHash from "object-hash";
32
import { PlayObject } from "../../../../core/Atomic.js";
43
import { REPORTED_PLAYER_STATUSES, ReportedPlayerStatus } from "../../infrastructure/Atomic.js";
54
import { PlatformApplication, PlatformType } from "./interfaces.js";
5+
import { hashObject } from "../../../utils/StringUtils.js";
66

77
export const chromePlayerStateToReported = (state: string): ReportedPlayerStatus => {
88
switch (state) {
@@ -71,5 +71,5 @@ export const genPlayHash = (play: PlayObject) => {
7171
}
7272
} = play;
7373

74-
return objectHash.sha1({artists, track, album, albumArtists, mediaType});
74+
return hashObject({artists, track, album, albumArtists, mediaType});
7575
}

src/backend/sources/JellyfinApiSource.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Logger } from "@foxxmd/logging";
22
import { WS } from "iso-websocket";
3-
import objectHash from 'object-hash';
43
// @ts-expect-error weird typings?
54
import { Api, Jellyfin } from "@jellyfin/sdk";
65
import {
@@ -58,7 +57,7 @@ import {
5857
import { JellyApiSourceConfig } from "../common/infrastructure/config/source/jellyfin.js";
5958
import { genGroupIdStr, getPlatformIdFromData, isDebugMode, parseBool, } from "../utils.js";
6059
import { joinedUrl } from "../utils/NetworkUtils.js";
61-
import { parseArrayFromMaybeString } from "../utils/StringUtils.js";
60+
import { hashObject, parseArrayFromMaybeString } from "../utils/StringUtils.js";
6261
import { MemoryPositionalSource } from "./MemoryPositionalSource.js";
6362
import { FixedSizeList } from "fixed-size-list";
6463

@@ -98,7 +97,7 @@ export default class JellyfinApiSource extends MemoryPositionalSource {
9897
this.canPoll = true;
9998
this.multiPlatform = true;
10099
this.requiresAuth = true;
101-
this.deviceId = `${name}-ms${internal.version}-${truncateStringToLength(10, '')(objectHash.sha1(config))}`;
100+
this.deviceId = `${name}-ms${internal.version}-${truncateStringToLength(10, '')(hashObject(config))}`;
102101

103102
this.client = new Jellyfin({
104103
clientInfo: {

src/backend/sources/MemorySource.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Logger } from "@foxxmd/logging";
22
import dayjs, { Dayjs } from "dayjs";
33
import { EventEmitter } from "events";
4-
import objectHash from 'object-hash';
54
import { SimpleIntervalJob, Task, ToadScheduler } from "toad-scheduler";
65
import { PlayObject, SOURCE_SOT, SOURCE_SOT_TYPES, SourcePlayerObj } from "../../core/Atomic.js";
76
import { buildTrackString } from "../../core/StringUtils.js";
@@ -28,6 +27,7 @@ import { timePassesScrobbleThreshold, timeToHumanTimestamp } from "../utils/Time
2827
import AbstractSource from "./AbstractSource.js";
2928
import { AbstractPlayerState, createPlayerOptions, PlayerStateOptions } from "./PlayerState/AbstractPlayerState.js";
3029
import { GenericPlayerState } from "./PlayerState/GenericPlayerState.js";
30+
import { hashObject } from "../utils/StringUtils.js";
3131

3232
const EXPECTED_NON_DISCOVERED_REASON = 'not added because an identical play with the same timestamp was already discovered.';
3333

@@ -86,7 +86,7 @@ export default class MemorySource extends AbstractSource {
8686
} else if (isStale) {
8787
label = 'Stale Player Cleanup';
8888
const state = player.getApiState();
89-
const stateHash = objectHash.sha1(state);
89+
const stateHash = hashObject(state);
9090
if(stateHash !== this.playerState.get(key)) {
9191
this.playerState.set(key, stateHash);
9292
this.emitEvent('playerUpdate', {
@@ -265,7 +265,7 @@ export default class MemorySource extends AbstractSource {
265265
player.logSummary();
266266
}
267267
const apiState = player.getApiState();
268-
this.playerState.set(key, objectHash.sha1(apiState))
268+
this.playerState.set(key, hashObject(apiState))
269269
this.emitEvent('playerUpdate', {
270270
...apiState,
271271
options: {

src/backend/sources/PlexApiSource.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import objectHash from 'object-hash';
21
import EventEmitter from "events";
32
import { PlayObject } from "../../core/Atomic.js";
43
import { buildTrackString, combinePartsToString, truncateStringToLength } from "../../core/StringUtils.js";
@@ -12,7 +11,7 @@ import {
1211
PlayPlatformId, REPORTED_PLAYER_STATUSES
1312
} from "../common/infrastructure/Atomic.js";
1413
import { genGroupIdStr, getFirstNonEmptyString, getPlatformIdFromData, isDebugMode, parseBool, } from "../utils.js";
15-
import { buildStatePlayerPlayIdententifyingInfo, parseArrayFromMaybeString } from "../utils/StringUtils.js";
14+
import { buildStatePlayerPlayIdententifyingInfo, hashObject, parseArrayFromMaybeString } from "../utils/StringUtils.js";
1615
import { GetSessionsMetadata } from "@lukehagar/plexjs/sdk/models/operations/getsessions.js";
1716
import { PlexAPI } from "@lukehagar/plexjs";
1817
import { PlexApiSourceConfig } from "../common/infrastructure/config/source/plex.js";
@@ -66,7 +65,7 @@ export default class PlexApiSource extends MemoryPositionalSource {
6665
this.multiPlatform = true;
6766
this.requiresAuth = true;
6867
this.requiresAuthInteraction = false;
69-
this.deviceId = `${name}-ms${internal.version}-${truncateStringToLength(10, '')(objectHash.sha1(config))}`;
68+
this.deviceId = `${name}-ms${internal.version}-${truncateStringToLength(10, '')(hashObject(config))}`;
7069
this.uniqueDropReasons = new FixedSizeList<string>(100);
7170
this.mediaIdsSeen = new FixedSizeList<string>(100);
7271
}

src/backend/utils/StringUtils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { strategies, stringSameness, StringSamenessResult } from "@foxxmd/string-sameness";
2+
import { hasher } from 'node-object-hash';
23
import { PlayObject } from "../../core/Atomic.js";
34
import { asPlayerStateData, DELIMITERS, PlayerStateDataMaybePlay } from "../common/infrastructure/Atomic.js";
45
import { genGroupIdStr, getPlatformIdFromData, intersect, parseRegexSingleOrFail } from "../utils.js";
@@ -419,4 +420,9 @@ export const normalizeListenbrainzUrl = (urlVal: string): string | undefined =>
419420
return urlVal.replace(LZ_VERSION_PATH, '');
420421
}
421422
return undefined;
422-
}
423+
}
424+
425+
type HashFunction = (obj: object) => string;
426+
const defaultHasher = hasher();
427+
const defaultHashFunc: HashFunction = (obj) => defaultHasher.hash(obj);
428+
export const hashObject = (obj: object, h: HashFunction = defaultHashFunc): string => h(obj);

0 commit comments

Comments
 (0)