Skip to content

Commit 11fd778

Browse files
committed
Updates to the patch
We were using hard-coded variables names before, but I fixed it so now it dynamically
1 parent 4bdf8c3 commit 11fd778

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

src/patches/preventUnsupportedUpdates.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,27 @@ import { LocationResult, showDiff } from './index';
1111
* and the update decision is made.
1212
*
1313
* The pattern we're looking for (minified):
14-
* BUILD_TIME:"..."}.VERSION,CHANNEL_VAR=hq()?.autoUpdatesChannel??"latest",VERSION_VAR=await FUNC(CHANNEL_VAR),OTHER_VAR=FUNC2();
14+
* BUILD_TIME:"..."}.VERSION,CHANNEL_VAR=FUNC()?.autoUpdatesChannel??"latest",VERSION_VAR=await FUNC(CHANNEL_VAR),OTHER_VAR=FUNC2();
15+
*
16+
* Note: The function name for autoUpdatesChannel (e.g., hq, z5) varies between builds.
1517
*
1618
* We'll inject code after VERSION_VAR assignment to check if it's supported.
1719
*/
1820
const getAutoUpdaterLocation = (oldFile: string): LocationResult | null => {
1921
// Pattern to match the auto-updater version fetch in minified code
2022
// The key markers are:
2123
// - BUILD_TIME:"..." followed by }.VERSION,
22-
// - hq()?.autoUpdatesChannel??"latest"
24+
// - FUNC()?.autoUpdatesChannel??"latest" (function name varies between builds)
2325
// - await FUNC(VAR) pattern
24-
// Captures: [1]=channel var, [2]=version var, [3]=fetch function, [4]=next var, [5]=next func
26+
// Captures:
27+
// [1] = channel var (e.g., _)
28+
// [2] = autoUpdatesChannel function (e.g., z5, hq - varies between builds)
29+
// [3] = version var (e.g., G)
30+
// [4] = fetch function (e.g., v3A)
31+
// [5] = next var (e.g., Z)
32+
// [6] = next func (e.g., Oc)
2533
const pattern =
26-
/BUILD_TIME:"[^"]+"\}\.VERSION,([$\w]+)=hq\(\)\?\.autoUpdatesChannel\?\?"latest",([$\w]+)=await ([$\w]+)\(\1\),([$\w]+)=([$\w]+)\(\);/;
34+
/BUILD_TIME:"[^"]+"\}\.VERSION,([$\w]+)=([$\w]+)\(\)\?\.autoUpdatesChannel\?\?"latest",([$\w]+)=await ([$\w]+)\(\1\),([$\w]+)=([$\w]+)\(\);/;
2735

2836
const match = oldFile.match(pattern);
2937

@@ -40,18 +48,19 @@ const getAutoUpdaterLocation = (oldFile: string): LocationResult | null => {
4048
identifiers: [
4149
match[0], // Full match
4250
match[1], // channel var (e.g., _)
43-
match[2], // version var (e.g., Z)
44-
match[3], // fetch function (e.g., _t)
45-
match[4], // next var (e.g., G)
46-
match[5], // next var's function (e.g., Ed)
51+
match[2], // autoUpdatesChannel function (e.g., z5)
52+
match[3], // version var (e.g., G)
53+
match[4], // fetch function (e.g., v3A)
54+
match[5], // next var (e.g., Z)
55+
match[6], // next var's function (e.g., Oc)
4756
],
4857
};
4958
};
5059

5160
/**
5261
* Gets the variable name used for the current version.
5362
* This is typically $ in the code pattern:
54-
* let $={...}.VERSION,
63+
* let $={...ISSUES_EXPLAINER:...}.VERSION,
5564
*/
5665
const getCurrentVersionVar = (
5766
oldFile: string,
@@ -97,26 +106,32 @@ export const writePreventUnsupportedUpdates = (
97106
return null;
98107
}
99108

109+
// Extract captured groups from the pattern match
110+
// identifiers: [0]=full match, [1]=channel var, [2]=autoUpdatesChannel func,
111+
// [3]=version var, [4]=fetch func, [5]=next var, [6]=next func
100112
const channelVar = location.identifiers![1];
101-
const versionVar = location.identifiers![2];
102-
const fetchFunc = location.identifiers![3];
103-
const nextVar = location.identifiers![4];
104-
const nextFunc = location.identifiers![5];
113+
const autoUpdatesChannelFunc = location.identifiers![2];
114+
const versionVar = location.identifiers![3];
115+
const fetchFunc = location.identifiers![4];
116+
const nextVar = location.identifiers![5];
117+
const nextFunc = location.identifiers![6];
105118

106119
// Construct the replacement with the tweakcc version check injected
107120
// The check wraps the version fetch to check if tweakcc supports the version.
108121
// If the prompts file doesn't exist (404) or check fails, it returns the current version to block the update.
109122
// Fails closed: if we can't verify support, we block the update to be safe.
110-
const tweakccVersionCheck = `${versionVar}=await(async()=>{let v=await ${fetchFunc}(${channelVar});if(!v)return v;try{const r=await fetch(\`https://raw.githubusercontent.com/Piebald-AI/tweakcc/refs/heads/main/data/prompts/prompts-\${v}.json\`,{method:'HEAD'});if(!r.ok)return ${currentVersionVar};}catch(e){return ${currentVersionVar};}return v;})(),`;
123+
// Wrapped in outer try-catch to ensure no errors propagate that could affect module initialization.
124+
const tweakccVersionCheck = `${versionVar}=await(async()=>{try{let v=await ${fetchFunc}(${channelVar});if(!v)return v;try{const r=await fetch(\`https://raw.githubusercontent.com/georpar/tweakcc/refs/heads/main/data/prompts/prompts-\${v}.json\`,{method:'HEAD'});if(!r.ok)return ${currentVersionVar};}catch(e){return ${currentVersionVar};}return v;}catch(e){return null;}})(),`;
111125

112-
// Reconstruct the original prefix (BUILD_TIME part) which we matched but want to preserve
126+
// Extract the BUILD_TIME portion from the matched string (minified format)
113127
const buildTimeMatch = location.identifiers![0].match(/BUILD_TIME:"[^"]+"\}/);
114128
const buildTimePrefix = buildTimeMatch ? buildTimeMatch[0] : '';
115129

130+
// Reconstruct the replacement using the captured function name (not hardcoded)
116131
const replacement =
117132
buildTimePrefix +
118133
`.VERSION,` +
119-
`${channelVar}=hq()?.autoUpdatesChannel??"latest",` +
134+
`${channelVar}=${autoUpdatesChannelFunc}()?.autoUpdatesChannel??"latest",` +
120135
tweakccVersionCheck +
121136
`${nextVar}=${nextFunc}();`;
122137

0 commit comments

Comments
 (0)