feat(WAProto): Optimize protobuf generation for 80%+ bundle size reduction (#1665)

* chore: remove comments and veirfy from generated proto files (improve size)

* refactor: replace fromObject with create for proto message instantiation and flag no beautiful

* chore: lint issues
This commit is contained in:
João Lucas de Oliveira Lopes
2025-09-07 08:08:11 -03:00
committed by GitHub
parent 9e04cce8d3
commit 4d4339dae6
15 changed files with 19728 additions and 190249 deletions
+3 -3
View File
@@ -161,7 +161,7 @@ export const encodeSyncdPatch = async (
state = { ...state, indexValueMap: { ...state.indexValueMap } }
const indexBuffer = Buffer.from(JSON.stringify(index))
const dataProto = proto.SyncActionData.fromObject({
const dataProto = proto.SyncActionData.create({
index: indexBuffer,
value: syncAction,
padding: new Uint8Array(0),
@@ -243,13 +243,13 @@ export const decodeSyncdMutations = async (
const syncAction = proto.SyncActionData.decode(result)
if (validateMacs) {
const hmac = hmacSign(syncAction.index!, key.indexKey)
const hmac = hmacSign(syncAction.index, key.indexKey)
if (Buffer.compare(hmac, record.index!.blob!) !== 0) {
throw new Boom('HMAC index verification failed')
}
}
const indexStr = Buffer.from(syncAction.index!).toString()
const indexStr = Buffer.from(syncAction.index).toString()
onMutation({ syncAction, index: JSON.parse(indexStr) })
ltGenerator.mix({
+1 -1
View File
@@ -154,7 +154,7 @@ export const decryptMessageNode = (
for (const { tag, attrs, content } of stanza.content) {
if (tag === 'verified_name' && content instanceof Uint8Array) {
const cert = proto.VerifiedNameCertificate.decode(content)
const details = proto.VerifiedNameCertificate.Details.decode(cert.details!)
const details = proto.VerifiedNameCertificate.Details.decode(cert.details)
fullMessage.verifiedBizName = details.verifiedName
}
+15 -15
View File
@@ -177,9 +177,9 @@ export const prepareWAMessageMedia = async (
await fs.unlink(filePath)
const obj = WAProto.Message.fromObject({
const obj = WAProto.Message.create({
// todo: add more support here
[`${mediaType}Message`]: (MessageTypeProto as any)[mediaType].fromObject({
[`${mediaType}Message`]: (MessageTypeProto as any)[mediaType].create({
url: mediaUrl,
directPath,
fileSha256,
@@ -278,8 +278,8 @@ export const prepareWAMessageMedia = async (
}
})
const obj = WAProto.Message.fromObject({
[`${mediaType}Message`]: MessageTypeProto[mediaType as keyof typeof MessageTypeProto].fromObject({
const obj = WAProto.Message.create({
[`${mediaType}Message`]: MessageTypeProto[mediaType as keyof typeof MessageTypeProto].create({
url: mediaUrl,
directPath,
mediaKey,
@@ -289,7 +289,7 @@ export const prepareWAMessageMedia = async (
mediaKeyTimestamp: unixTimestampSeconds(),
...uploadData,
media: undefined
})
} as any)
})
if (uploadData.ptv) {
@@ -317,7 +317,7 @@ export const prepareDisappearingMessageSettingContent = (ephemeralExpiration?: n
}
}
}
return WAProto.Message.fromObject(content)
return WAProto.Message.create(content)
}
/**
@@ -404,18 +404,18 @@ export const generateWAMessageContent = async (
}
if (contactLen === 1) {
m.contactMessage = WAProto.Message.ContactMessage.fromObject(message.contacts.contacts[0]!)
m.contactMessage = WAProto.Message.ContactMessage.create(message.contacts.contacts[0])
} else {
m.contactsArrayMessage = WAProto.Message.ContactsArrayMessage.fromObject(message.contacts)
m.contactsArrayMessage = WAProto.Message.ContactsArrayMessage.create(message.contacts)
}
} else if ('location' in message) {
m.locationMessage = WAProto.Message.LocationMessage.fromObject(message.location)
m.locationMessage = WAProto.Message.LocationMessage.create(message.location)
} else if ('react' in message) {
if (!message.react.senderTimestampMs) {
message.react.senderTimestampMs = Date.now()
}
m.reactionMessage = WAProto.Message.ReactionMessage.fromObject(message.react)
m.reactionMessage = WAProto.Message.ReactionMessage.create(message.react)
} else if ('delete' in message) {
m.protocolMessage = {
key: message.delete,
@@ -481,7 +481,7 @@ export const generateWAMessageContent = async (
m.ptvMessage = videoMessage
} else if ('product' in message) {
const { imageMessage } = await prepareWAMessageMedia({ image: message.product.productImage }, options)
m.productMessage = WAProto.Message.ProductMessage.fromObject({
m.productMessage = WAProto.Message.ProductMessage.create({
...message,
product: {
...message.product,
@@ -574,7 +574,7 @@ export const generateWAMessageContent = async (
}
}
return WAProto.Message.fromObject(m)
return WAProto.Message.create(m)
}
export const generateWAMessageFromContent = (
@@ -601,7 +601,7 @@ export const generateWAMessageFromContent = (
let quotedMsg = normalizeMessageContent(quoted.message)!
const msgType = getContentType(quotedMsg)!
// strip any redundant properties
quotedMsg = proto.Message.fromObject({ [msgType]: quotedMsg[msgType] })
quotedMsg = proto.Message.create({ [msgType]: quotedMsg[msgType] })
const quotedContent = quotedMsg[msgType]
if (typeof quotedContent === 'object' && quotedContent && 'contextInfo' in quotedContent) {
@@ -644,7 +644,7 @@ export const generateWAMessageFromContent = (
}
}
message = WAProto.Message.fromObject(message)
message = WAProto.Message.create(message)
const messageJSON = {
key: {
@@ -658,7 +658,7 @@ export const generateWAMessageFromContent = (
participant: isJidGroup(jid) || isJidStatusBroadcast(jid) ? userJid : undefined,
status: WAMessageStatus.PENDING
}
return WAProto.WebMessageInfo.fromObject(messageJSON)
return WAProto.WebMessageInfo.create(messageJSON)
}
export const generateWAMessage = async (jid: string, content: AnyMessageContent, options: MessageGenerationOptions) => {
+1 -1
View File
@@ -106,7 +106,7 @@ export const useMultiFileAuthState = async (
ids.map(async id => {
let value = await readData(`${type}-${id}.json`)
if (type === 'app-state-sync-key' && value) {
value = proto.Message.AppStateSyncKeyData.fromObject(value)
value = proto.Message.AppStateSyncKeyData.create(value)
}
data[id] = value
+11 -11
View File
@@ -62,7 +62,7 @@ export const generateLoginNode = (userJid: string, config: SocketConfig): proto.
username: +user,
device: device
}
return proto.ClientPayload.fromObject(payload)
return proto.ClientPayload.create(payload)
}
const getPlatformType = (platform: string): proto.DeviceProps.PlatformType => {
@@ -107,7 +107,7 @@ export const generateRegistrationNode = (
}
}
return proto.ClientPayload.fromObject(registerPayload)
return proto.ClientPayload.create(registerPayload)
}
export const configureSuccessfulPairing = (
@@ -138,26 +138,26 @@ export const configureSuccessfulPairing = (
const isHostedAccount = accountType !== undefined && accountType === proto.ADVEncryptionType.HOSTED
const hmacPrefix = isHostedAccount ? Buffer.from([6, 5]) : Buffer.alloc(0)
const advSign = hmacSign(Buffer.concat([hmacPrefix, details!]), Buffer.from(advSecretKey, 'base64'))
if (Buffer.compare(hmac!, advSign) !== 0) {
const advSign = hmacSign(Buffer.concat([hmacPrefix, details]), Buffer.from(advSecretKey, 'base64'))
if (Buffer.compare(hmac, advSign) !== 0) {
throw new Boom('Invalid account signature')
}
const account = proto.ADVSignedDeviceIdentity.decode(details!)
const account = proto.ADVSignedDeviceIdentity.decode(details)
const { accountSignatureKey, accountSignature, details: deviceDetails } = account
const accountMsg = Buffer.concat([Buffer.from([6, 0]), deviceDetails!, signedIdentityKey.public])
if (!Curve.verify(accountSignatureKey!, accountMsg, accountSignature!)) {
const accountMsg = Buffer.concat([Buffer.from([6, 0]), deviceDetails, signedIdentityKey.public])
if (!Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
throw new Boom('Failed to verify account signature')
}
const devicePrefix = isHostedAccount ? Buffer.from([6, 6]) : Buffer.from([6, 1])
const deviceMsg = Buffer.concat([devicePrefix, deviceDetails!, signedIdentityKey.public, accountSignatureKey!])
const deviceMsg = Buffer.concat([devicePrefix, deviceDetails, signedIdentityKey.public, accountSignatureKey])
account.deviceSignature = Curve.sign(signedIdentityKey.private, deviceMsg)
const identity = createSignalIdentity(jid!, accountSignatureKey!)
const identity = createSignalIdentity(jid!, accountSignatureKey)
const accountEnc = encodeSignedDeviceIdentity(account, false)
const deviceIdentity = proto.ADVDeviceIdentity.decode(account.details!)
const deviceIdentity = proto.ADVDeviceIdentity.decode(account.details)
const reply: BinaryNode = {
tag: 'iq',
@@ -173,7 +173,7 @@ export const configureSuccessfulPairing = (
content: [
{
tag: 'device-identity',
attrs: { 'key-index': deviceIdentity.keyIndex!.toString() },
attrs: { 'key-index': deviceIdentity.keyIndex.toString() },
content: accountEnc
}
]