stk500-esm
Version:
A modern, ESM-compatible, TypeScript implementation of the STK500v1 protocol for programming Arduino boards directly from Node.js or the browser.
56 lines (55 loc) • 2.1 kB
JavaScript
import Constants from "./constants.js";
const startingBytes = [Constants.Resp_STK_INSYNC];
/**
* Receives data from a stream, looking for specific starting bytes.
*
* @param stream - The read/write stream to receive data from.
* @param timeout - The maximum time to wait for data, in milliseconds.
* @param responseLength - The expected length of the response.
* @returns A promise that resolves with the received data as a Uint8Array.
* @throws Will throw an error if the timeout is reached or if the received data exceeds the expected length.
*/
export default function receiveData(stream, timeout, responseLength) {
return new Promise((resolve, reject) => {
let buffer = new Uint8Array(0);
let started = false;
let timeoutId;
const handleChunk = (data) => {
if (!started) {
const startIndex = Array.from(data).findIndex((byte) => startingBytes.includes(byte));
if (startIndex !== -1) {
data = data.subarray(startIndex);
started = true;
}
else {
return; // Skip this chunk if starting byte not found
}
}
buffer = new Uint8Array([...buffer, ...data]);
if (buffer.length > responseLength) {
finished(new Error(`Buffer overflow ${buffer.length} > ${responseLength}`));
}
else if (buffer.length === responseLength) {
finished();
}
};
const finished = (err) => {
if (timeoutId) {
clearTimeout(timeoutId);
}
stream.removeListener("data", handleChunk);
if (err) {
reject(err);
}
else {
resolve(buffer);
}
};
if (timeout && timeout > 0) {
timeoutId = setTimeout(() => {
finished(new Error(`receiveData timeout after ${timeout}ms`));
}, timeout);
}
stream.on("data", handleChunk);
});
}