@lynovratech/baileys 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,81 @@
1
+ import { readFileSync, writeFileSync } from 'fs';
2
+ import { exit } from 'process';
3
+
4
+ const filePath = './index.js'
5
+
6
+ try {
7
+ let content = readFileSync(filePath, 'utf8')
8
+
9
+ content = content.replace(/import \* as (\$protobuf) from/g, 'import $1 from')
10
+ content = content.replace(/(['"])protobufjs\/minimal(['"])/g, '$1protobufjs/minimal.js$2')
11
+
12
+ const marker = 'const $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});\n\n'
13
+ const longToStringHelper =
14
+ 'function longToString(value, unsigned) {\n' +
15
+ '\tif (typeof value === "string") {\n' +
16
+ '\t\treturn value;\n' +
17
+ '\t}\n' +
18
+ '\tif (typeof value === "number") {\n' +
19
+ '\t\treturn String(value);\n' +
20
+ '\t}\n' +
21
+ '\tif (!$util.Long) {\n' +
22
+ '\t\treturn String(value);\n' +
23
+ '\t}\n' +
24
+ '\tconst normalized = $util.Long.fromValue(value);\n' +
25
+ '\tconst prepared = unsigned && normalized && typeof normalized.toUnsigned === "function"\n' +
26
+ '\t\t? normalized.toUnsigned()\n' +
27
+ '\t\t: normalized;\n' +
28
+ '\treturn prepared.toString();\n' +
29
+ '}\n\n'
30
+ const longToNumberHelper =
31
+ 'function longToNumber(value, unsigned) {\n' +
32
+ '\tif (typeof value === "number") {\n' +
33
+ '\t\treturn value;\n' +
34
+ '\t}\n' +
35
+ '\tif (typeof value === "string") {\n' +
36
+ '\t\tconst numeric = Number(value);\n' +
37
+ '\t\treturn numeric;\n' +
38
+ '\t}\n' +
39
+ '\tif (!$util.Long) {\n' +
40
+ '\t\treturn Number(value);\n' +
41
+ '\t}\n' +
42
+ '\tconst normalized = $util.Long.fromValue(value);\n' +
43
+ '\tconst prepared = unsigned && normalized && typeof normalized.toUnsigned === "function"\n' +
44
+ '\t\t? normalized.toUnsigned()\n' +
45
+ '\t\t: typeof normalized.toSigned === "function"\n' +
46
+ '\t\t\t? normalized.toSigned()\n' +
47
+ '\t\t\t: normalized;\n' +
48
+ '\treturn prepared.toNumber();\n' +
49
+ '}\n\n'
50
+
51
+ if (!content.includes('function longToString(')) {
52
+ const markerIndex = content.indexOf(marker)
53
+ if (markerIndex === -1) {
54
+ throw new Error('Unable to inject Long helpers: marker not found in WAProto index output')
55
+ }
56
+
57
+ content = content.replace(marker, `${marker}${longToStringHelper}${longToNumberHelper}`)
58
+ } else {
59
+ const longToStringRegex = /function longToString\(value, unsigned\) {\n[\s\S]*?\n}\n\n/
60
+ const longToNumberRegex = /function longToNumber\(value, unsigned\) {\n[\s\S]*?\n}\n\n/
61
+
62
+ if (!longToStringRegex.test(content) || !longToNumberRegex.test(content)) {
63
+ throw new Error('Unable to update Long helpers: existing definitions not found')
64
+ }
65
+
66
+ content = content.replace(longToStringRegex, longToStringHelper)
67
+ content = content.replace(longToNumberRegex, longToNumberHelper)
68
+ }
69
+
70
+ const longPattern = /([ \t]+d\.(\w+) = )o\.longs === String \? \$util\.Long\.prototype\.toString\.call\(m\.\2\) : o\.longs === Number \? new \$util\.LongBits\(m\.\2\.low >>> 0, m\.\2\.high >>> 0\)\.toNumber\((true)?\) : m\.\2;/g
71
+ content = content.replace(longPattern, (_match, prefix, field, unsignedFlag) => {
72
+ const unsignedArg = unsignedFlag ? ', true' : ''
73
+ return `${prefix}o.longs === String ? longToString(m.${field}${unsignedArg}) : o.longs === Number ? longToNumber(m.${field}${unsignedArg}) : m.${field};`
74
+ })
75
+
76
+ writeFileSync(filePath, content, 'utf8')
77
+ console.log(`✅ Fixed imports in ${filePath}`)
78
+ } catch (error) {
79
+ console.error(`❌ Error fixing imports: ${error.message}`)
80
+ exit(1)
81
+ }