Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ function readMessage() {

let messageLength = buffer.readUInt32LE(0)
let messageBuffer = Buffer.alloc(messageLength)
fs.readSync(0, messageBuffer, 0, messageLength, null)
let offset = 0
while (offset < messageLength) {
offset += fs.readSync(0, messageBuffer, offset, messageLength - offset, null)
}

return JSON.parse(messageBuffer.toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ if (process.platform !== 'linux') {
const result = await browser.crx.exec('runtime.sendNativeMessage', hostApplication, value)
expect(result).to.deep.equal(value)
})

it('receives a message larger than one stdout chunk', async () => {
const value = { json: randomUUID(), padding: 'x'.repeat(256 * 1024) }
const result = await browser.crx.exec('runtime.sendNativeMessage', hostApplication, value)
expect(result).to.deep.equal(value)
})
})
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class NativeMessagingHost {
private connectionId: string
private connected: boolean = false
private pending?: any[]
private received?: Buffer
private keepAlive: boolean
private resolveResponse?: (message: any) => void

Expand Down Expand Up @@ -214,13 +215,28 @@ export class NativeMessagingHost {
}

private receive = (data: Buffer) => {
const length = data.readUInt32LE(0)
const message = JSON.parse(data.subarray(4, 4 + length).toString())
d('receive: %s', message)
if (this.keepAlive && this.sender) {
this.sender.send(`crx-native-msg-${this.connectionId}`, message)
} else {
this.resolveResponse?.(message)
// stdout chunks don't line up with messages: a message larger than the
// pipe's buffer spans several chunks, and one chunk may carry several.
this.received = this.received ? Buffer.concat([this.received, data]) : data
while (this.received.length >= 4) {
const length = this.received.readUInt32LE(0)
if (this.received.length < 4 + length) break
const body = this.received.subarray(4, 4 + length)
this.received = this.received.subarray(4 + length)

let message: any
try {
message = JSON.parse(body.toString())
} catch (error) {
d('receive: unable to parse message', error)
continue
}
d('receive: %s', message)
if (this.keepAlive && this.sender) {
this.sender.send(`crx-native-msg-${this.connectionId}`, message)
} else {
this.resolveResponse?.(message)
}
}
}

Expand Down