Repository navigation
Expand file tree
/
Copy pathpatch.js
More file actions
179 lines (141 loc) Β· 5.03 KB
/
Copy pathpatch.js
File metadata and controls
179 lines (141 loc) Β· 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const targetFiles = [
path.join(
__dirname,
'node_modules/zapo-js/dist/esm/transport/node/builders/message.js'
),
path.join(
__dirname,
'node_modules/zapo-js/dist/transport/node/builders/message.js'
)
]
const MARKER = '/* OVERRIDE_CUSTOM_NODES_PATCH */'
const regex =
/if\s*\(\s*input\.customNodes\s*\)\s*\{\s*for\s*\(\s*const\s+node\s+of\s+input\.customNodes\s*\)\s*\{\s*content\.push\(\s*node\s*\);\s*\}\s*\}/
const patchReplacement = `${MARKER}
if (input.customNodes) {
for (const node of input.customNodes) {
if (!node || !node.tag) continue;
const existingIndex = content.findIndex(
item => item && item.tag === node.tag
);
if (existingIndex !== -1) {
content[existingIndex] = node;
} else {
content.push(node);
}
}
}`
let patchedCount = 0
let skippedCount = 0
let missingCount = 0
let failedCount = 0
const results = []
for (const file of targetFiles) {
const relativePath = path.relative(__dirname, file)
if (!fs.existsSync(file)) {
missingCount++
results.push(`βοΈ Gak ketemu: ${relativePath}`)
continue
}
let content = fs.readFileSync(file, 'utf8')
if (content.includes(MARKER)) {
skippedCount++
results.push(`βΉοΈ Udah ter-patch: ${relativePath}`)
continue
}
if (!regex.test(content)) {
failedCount++
results.push(
`β οΈ Pattern gak match: ${relativePath}`
)
continue
}
content = content.replace(regex, patchReplacement)
fs.writeFileSync(file, content, 'utf8')
patchedCount++
results.push(`β
Berhasil patch: ${relativePath}`)
}
console.log(`
ββββββββββββββββββββββββββββββββββββββ
ZAPO-JS CUSTOM NODES PATCH
ββββββββββββββββββββββββββββββββββββββ
`)
console.log(results.join('\n'))
console.log(`
ββββββββββββββββββββββββββββββββββββββ
Total:
${patchedCount} patched
${skippedCount} sudah ter-patch
${missingCount} tidak ditemukan
${failedCount} gagal match
ββββββββββββββββββββββββββββββββββββββ
`)
const albumTargets = [
...['dist', 'dist/esm'].flatMap((base) => [
path.join(__dirname, `node_modules/zapo-js/${base}/protocol/message.js`),
path.join(__dirname, `node_modules/zapo-js/${base}/message/encode/content.js`)
])
]
const ALBUM_MARKER = '/* OVERRIDE_ALBUM_COLLECTION_PATCH */'
const albumResults = []
let albumPatched = 0
let albumSkipped = 0
for (const file of albumTargets) {
const relativePath = path.relative(__dirname, file)
if (!fs.existsSync(file)) {
albumResults.push(`βοΈ Gak ketemu: ${relativePath}`)
continue
}
let content = fs.readFileSync(file, 'utf8')
if (content.includes(ALBUM_MARKER)) {
albumSkipped++
albumResults.push(`βΉοΈ Udah ter-patch: ${relativePath}`)
continue
}
const isEsm = file.includes('/esm/')
const nsPrefix = isEsm ? '' : 'constants_1.'
if (file.endsWith('protocol/message.js')) {
const oldConstant = "GROUP_HISTORY: 'group_history'\n})"
const newConstant = `GROUP_HISTORY: 'group_history',
${ALBUM_MARKER}
COLLECTION: 'collection'
})`
if (!content.includes(oldConstant)) {
albumResults.push(`β οΈ Pattern konstanta gak match: ${relativePath}`)
continue
}
content = content.replace(oldConstant, newConstant)
} else {
const indent = isEsm ? ' ' : ' '
const oldResolver = `${indent}if (msg.messageHistoryBundle)\n${indent} return ${nsPrefix}WA_ENC_MEDIA_TYPES.GROUP_HISTORY;`
const newResolver = `${indent}${ALBUM_MARKER}\n` +
`${indent}if (msg.albumMessage)\n${indent} return ${nsPrefix}WA_ENC_MEDIA_TYPES.COLLECTION;\n` +
oldResolver
if (!content.includes(oldResolver)) {
albumResults.push(`β οΈ Pattern resolver gak match: ${relativePath}`)
continue
}
content = content.replace(oldResolver, newResolver)
}
fs.writeFileSync(file, content, 'utf8')
albumPatched++
albumResults.push(`β
Berhasil patch: ${relativePath}`)
}
console.log(`
ββββββββββββββββββββββββββββββββββββββ
ZAPO-JS ALBUM COLLECTION PATCH
ββββββββββββββββββββββββββββββββββββββ
`)
console.log(albumResults.join('\n'))
console.log(`
ββββββββββββββββββββββββββββββββββββββ
Total:
${albumPatched} patched
${albumSkipped} sudah ter-patch
ββββββββββββββββββββββββββββββββββββββ
`)