Skip to content

Commit

Permalink
feat(cli):support more format type to publish protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
ni00 committed Jun 27, 2023
1 parent b3e7a99 commit a127fd9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
5 changes: 5 additions & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ export class Commander {
)
.option('-Pp, --protobuf-path <PATH>', 'the .proto file that defines the message format of protobuf')
.option('-Pmn, --protobuf-message-name <NAME>', 'the name of the protobuf message type')
.option(
'-Pf, --protobuf-format <TYPE>',
'the format type of message body, support base64, json, hex',
parseFormat,
)
.action(pub)

this.program
Expand Down
10 changes: 6 additions & 4 deletions cli/src/lib/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ const send = (
message: string | Buffer
protobufPath: string | undefined
protobufMessageName: string | undefined
protobufFormat: FormatType | undefined
opts: IClientPublishOptions
},
) => {
const client = mqtt.connect(connOpts)
basicLog.connecting(config, connOpts.hostname!, connOpts.port, pubOpts.topic, pubOpts.message.toString())
client.on('connect', () => {
basicLog.connected()
const { topic, message, protobufPath, protobufMessageName } = pubOpts
const { topic, message, protobufPath, protobufMessageName, protobufFormat } = pubOpts
basicLog.publishing()

let bufferMessage = serializeProtobufToBuffer(message, protobufPath, protobufMessageName)
let bufferMessage = serializeProtobufToBuffer(message, protobufPath, protobufMessageName, protobufFormat)
client.publish(topic, bufferMessage, pubOpts.opts, (err) => {
if (err) {
signale.warn(err)
Expand Down Expand Up @@ -59,6 +60,7 @@ const multisend = (
message: string | Buffer
protobufPath: string | undefined
protobufMessageName: string | undefined
protobufFormat: FormatType | undefined
opts: IClientPublishOptions
},
maximumReconnectTimes: number,
Expand All @@ -71,9 +73,9 @@ const multisend = (
objectMode: true,
})
sender._write = (line, _enc, cb) => {
const { topic, opts, protobufPath, protobufMessageName } = pubOpts
const { topic, opts, protobufPath, protobufMessageName, protobufFormat } = pubOpts

let bufferMessage = serializeProtobufToBuffer(line.trim(), protobufPath, protobufMessageName)
let bufferMessage = serializeProtobufToBuffer(line.trim(), protobufPath, protobufMessageName, protobufFormat)
client.publish(topic, bufferMessage, opts, cb)
}

Expand Down
6 changes: 5 additions & 1 deletion cli/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ declare global {
connUserProperties?: Record<string, string | string[]>
protobufPath?: string
protobufMessageName?: string
protobufFormat?: FormatType
}

interface SubscribeOptions extends ConnectOptions {
Expand All @@ -97,7 +98,10 @@ declare global {
interval: number
}

type OmitPublishOptions = Omit<PublishOptions, 'stdin' | 'multiline' | 'protobufPath' | 'protobufMessageName'>
type OmitPublishOptions = Omit<
PublishOptions,
'stdin' | 'multiline' | 'protobufPath' | 'protobufMessageName' | 'protobufFormat'
>

interface BenchPublishOptions extends OmitPublishOptions {
count: number
Expand Down
2 changes: 1 addition & 1 deletion cli/src/utils/convertPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chalk from 'chalk'

const convertJSON = (value: Buffer) => {
try {
return JSON.parse(value.toString())
return JSON.stringify(JSON.parse(value.toString()), null, 2)
} catch (err) {
return chalk.red(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cli/src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ const parsePublishOptions = (options: PublishOptions) => {
contentType,
protobufPath,
protobufMessageName,
protobufFormat,
} = options

const publishOptions: IClientPublishOptions = {
Expand All @@ -316,7 +317,7 @@ const parsePublishOptions = (options: PublishOptions) => {
)
}

return { topic, message, protobufPath, protobufMessageName, opts: publishOptions }
return { topic, message, protobufPath, protobufMessageName, protobufFormat, opts: publishOptions }
}

const parseSubscribeOptions = (options: SubscribeOptions) => {
Expand Down
19 changes: 18 additions & 1 deletion cli/src/utils/protobuf.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import protobuf from 'protobufjs'

const convertObject = (raw: string | Buffer, format?: FormatType | undefined) => {
switch (format) {
case 'base64':
return JSON.parse(Buffer.from(raw.toString('utf-8'), 'base64').toString('utf-8'))
case 'hex':
return JSON.parse(Buffer.from(raw.toString('utf-8').replaceAll(' ', ''), 'hex').toString('utf-8'))
default:
return JSON.parse(raw.toString('utf-8'))
}
}

export const serializeProtobufToBuffer = (
raw: string | Buffer,
protobufPath: string | undefined,
protobufMessageName: string | undefined,
format?: FormatType | undefined,
): Buffer => {
let bufferMessage = Buffer.from(raw)
if (protobufPath && protobufMessageName) {
try {
const root = protobuf.loadSync(protobufPath)
const Message = root.lookupType(protobufMessageName)
const data = Message.create(JSON.parse(raw.toString()))
const rawData = convertObject(raw, format)
const err = Message.verify(rawData)
if (err) {
throw Error(err)
}
const data = Message.create(rawData)
const serializedMessage = Message.encode(data).finish()
bufferMessage = Buffer.from(serializedMessage)
} catch (err) {
Expand Down

0 comments on commit a127fd9

Please sign in to comment.