Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e0e546

Browse files
committed
Fix line-oriented callbacks
stdoutcb/stderrcb assume that an input string is a single line, but in fact it can contain multiple lines, or even be a part of a line. One of the consequence is that cocopa fails to parse compiler output when it contains long lines (microsoft#1275). This patch fixes the issue by introducing a buffer to accumulate received data before passing them to callbacks. Fixes microsoft#1275.
1 parent 0890134 commit 3e0e546

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/arduino/arduino.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,25 @@ export class ArduinoApp {
746746
}
747747
return ret;
748748
}
749-
const stdoutcb = (line: string) => {
749+
750+
// Wrap line-oriented callbacks to accept arbitrary chunks of data.
751+
const wrapLineCallback = (callback: (line: string) => void) => {
752+
let buffer = "";
753+
return (data: string) => {
754+
buffer += data;
755+
for (;;) {
756+
const pos = buffer.indexOf(os.EOL);
757+
if (pos < 0) {
758+
break;
759+
}
760+
const line = buffer.substring(0, pos + os.EOL.length);
761+
buffer = buffer.substring(pos + os.EOL.length);
762+
callback(line);
763+
}
764+
};
765+
}
766+
767+
const stdoutcb = wrapLineCallback((line: string) => {
750768
if (cocopa.callback) {
751769
cocopa.callback(line);
752770
}
@@ -758,8 +776,8 @@ export class ArduinoApp {
758776
arduinoChannel.channel.append(line);
759777
}
760778
}
761-
}
762-
const stderrcb = (line: string) => {
779+
});
780+
const stderrcb = wrapLineCallback((line: string) => {
763781
if (os.platform() === "win32") {
764782
line = line.trim();
765783
if (line.length <= 0) {
@@ -784,7 +802,7 @@ export class ArduinoApp {
784802
}
785803
}
786804
arduinoChannel.channel.append(line);
787-
}
805+
});
788806

789807
return await util.spawn(
790808
this._settings.commandPath,

0 commit comments

Comments
 (0)