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:
committed by
GitHub
parent
9e04cce8d3
commit
4d4339dae6
@@ -1,3 +1,3 @@
|
||||
yarn pbjs -t static-module -w es6 --no-bundle -o ./index.js ./WAProto.proto;
|
||||
yarn pbts -o ./index.d.ts ./index.js;
|
||||
yarn pbjs -t static-module --no-convert --no-beautify -w es6 --no-bundle --no-delimited --no-verify --no-comments -o ./index.js ./WAProto.proto;
|
||||
yarn pbjs -t static-module --no-convert --no-beautify -w es6 --no-bundle --no-delimited --no-verify ./WAProto.proto | yarn pbts --no-comments -o ./index.d.ts -;
|
||||
node ./fix-imports.js
|
||||
|
||||
Vendored
+2773
-45558
File diff suppressed because it is too large
Load Diff
+16911
-144632
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,6 @@
|
||||
import { proto } from '../../../WAProto/index.js'
|
||||
import { CiphertextMessage } from './ciphertext-message'
|
||||
|
||||
interface SenderKeyDistributionMessageStructure {
|
||||
id: number
|
||||
iteration: number
|
||||
chainKey: string | Uint8Array
|
||||
signingKey: string | Uint8Array
|
||||
}
|
||||
|
||||
export class SenderKeyDistributionMessage extends CiphertextMessage {
|
||||
private readonly id: number
|
||||
private readonly iteration: number
|
||||
@@ -27,9 +20,7 @@ export class SenderKeyDistributionMessage extends CiphertextMessage {
|
||||
if (serialized) {
|
||||
try {
|
||||
const message = serialized.slice(1)
|
||||
const distributionMessage = proto.SenderKeyDistributionMessage.decode(
|
||||
message
|
||||
).toJSON() as SenderKeyDistributionMessageStructure
|
||||
const distributionMessage = proto.SenderKeyDistributionMessage.decode(message)
|
||||
|
||||
this.serialized = serialized
|
||||
this.id = distributionMessage.id
|
||||
|
||||
@@ -3,12 +3,6 @@ import { calculateSignature, verifySignature } from 'libsignal/src/curve'
|
||||
import { proto } from '../../../WAProto/index.js'
|
||||
import { CiphertextMessage } from './ciphertext-message'
|
||||
|
||||
interface SenderKeyMessageStructure {
|
||||
id: number
|
||||
iteration: number
|
||||
ciphertext: string | Buffer
|
||||
}
|
||||
|
||||
export class SenderKeyMessage extends CiphertextMessage {
|
||||
private readonly SIGNATURE_LENGTH = 64
|
||||
private readonly messageVersion: number
|
||||
@@ -31,7 +25,7 @@ export class SenderKeyMessage extends CiphertextMessage {
|
||||
const version = serialized[0]!
|
||||
const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH)
|
||||
const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH)
|
||||
const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON() as SenderKeyMessageStructure
|
||||
const senderKeyMessage = proto.SenderKeyMessage.decode(message)
|
||||
|
||||
this.serialized = serialized
|
||||
this.messageVersion = (version & 0xff) >> 4
|
||||
|
||||
@@ -286,7 +286,7 @@ export const makeCommunitiesSocket = (config: SocketConfig) => {
|
||||
// update the invite message to be expired
|
||||
if (key.id) {
|
||||
// create new invite message that is expired
|
||||
inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage)
|
||||
inviteMessage = proto.Message.GroupInviteMessage.create(inviteMessage)
|
||||
inviteMessage.inviteExpiration = 0
|
||||
inviteMessage.inviteCode = ''
|
||||
ev.emit('messages.update', [
|
||||
|
||||
@@ -239,7 +239,7 @@ export const makeGroupsSocket = (config: SocketConfig) => {
|
||||
// update the invite message to be expired
|
||||
if (key.id) {
|
||||
// create new invite message that is expired
|
||||
inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage)
|
||||
inviteMessage = proto.Message.GroupInviteMessage.create(inviteMessage)
|
||||
inviteMessage.inviteExpiration = 0
|
||||
inviteMessage.inviteCode = ''
|
||||
ev.emit('messages.update', [
|
||||
|
||||
@@ -749,7 +749,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
msg.participant ??= node.attrs.participant
|
||||
msg.messageTimestamp = +node.attrs.t!
|
||||
|
||||
const fullMsg = proto.WebMessageInfo.fromObject(msg)
|
||||
const fullMsg = proto.WebMessageInfo.create(msg as proto.IWebMessageInfo)
|
||||
await upsertMessage(fullMsg, 'append')
|
||||
}
|
||||
})
|
||||
@@ -1182,7 +1182,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
? Buffer.from(plaintextNode.content, 'binary')
|
||||
: Buffer.from(plaintextNode.content as Uint8Array)
|
||||
const messageProto = proto.Message.decode(contentBuf)
|
||||
const fullMessage = proto.WebMessageInfo.fromObject({
|
||||
const fullMessage = proto.WebMessageInfo.create({
|
||||
key: {
|
||||
remoteJid: from,
|
||||
id: child.attrs.message_id || child.attrs.server_id,
|
||||
@@ -1313,7 +1313,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
msg.message = { call: { callKey: Buffer.from(call.id) } }
|
||||
}
|
||||
|
||||
const protoMsg = proto.WebMessageInfo.fromObject(msg)
|
||||
const protoMsg = proto.WebMessageInfo.create(msg)
|
||||
upsertMessage(protoMsg, call.offline ? 'append' : 'notify')
|
||||
}
|
||||
})
|
||||
|
||||
@@ -756,15 +756,15 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
try {
|
||||
const media = await decryptMediaRetryData(result.media!, mediaKey, result.key.id!)
|
||||
if (media.result !== proto.MediaRetryNotification.ResultType.SUCCESS) {
|
||||
const resultStr = proto.MediaRetryNotification.ResultType[media.result!]
|
||||
const resultStr = proto.MediaRetryNotification.ResultType[media.result]
|
||||
throw new Boom(`Media re-upload failed by device (${resultStr})`, {
|
||||
data: media,
|
||||
statusCode: getStatusCodeForMediaRetry(media.result!) || 404
|
||||
statusCode: getStatusCodeForMediaRetry(media.result) || 404
|
||||
})
|
||||
}
|
||||
|
||||
content.directPath = media.directPath
|
||||
content.url = getUrlFromDirectPath(content.directPath!)
|
||||
content.url = getUrlFromDirectPath(content.directPath)
|
||||
|
||||
logger.debug({ directPath: media.directPath, key: result.key }, 'media update successful')
|
||||
} catch (err: any) {
|
||||
|
||||
@@ -223,7 +223,7 @@ export const makeSocket = (config: SocketConfig) => {
|
||||
let helloMsg: proto.IHandshakeMessage = {
|
||||
clientHello: { ephemeral: ephemeralKeyPair.public }
|
||||
}
|
||||
helloMsg = proto.HandshakeMessage.fromObject(helloMsg)
|
||||
helloMsg = proto.HandshakeMessage.create(helloMsg)
|
||||
|
||||
logger.info({ browser, helloMsg }, 'connected to WA')
|
||||
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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
@@ -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) => {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user